OSDN Git Service

* config/darwin.c (darwin_assemble_visibility): Treat
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 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     case dw_val_class_high_pc:
1316       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1317     case dw_val_class_str:
1318       return a->v.val_str == b->v.val_str;
1319     case dw_val_class_flag:
1320       return a->v.val_flag == b->v.val_flag;
1321     case dw_val_class_file:
1322       return a->v.val_file == b->v.val_file;
1323     case dw_val_class_decl_ref:
1324       return a->v.val_decl_ref == b->v.val_decl_ref;
1325     
1326     case dw_val_class_const_double:
1327       return (a->v.val_double.high == b->v.val_double.high
1328               && a->v.val_double.low == b->v.val_double.low);
1329
1330     case dw_val_class_vec:
1331       {
1332         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1333         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1334
1335         return (a_len == b_len
1336                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1337       }
1338
1339     case dw_val_class_data8:
1340       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1341
1342     case dw_val_class_vms_delta:
1343       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1344               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1345     }
1346   gcc_unreachable ();
1347 }
1348
1349 /* Compare two location atoms for exact equality.  */
1350
1351 static bool
1352 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1353 {
1354   if (a->dw_loc_opc != b->dw_loc_opc)
1355     return false;
1356
1357   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1358      address size, but since we always allocate cleared storage it
1359      should be zero for other types of locations.  */
1360   if (a->dtprel != b->dtprel)
1361     return false;
1362
1363   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1364           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1365 }
1366
1367 /* Compare two complete location expressions for exact equality.  */
1368
1369 bool
1370 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1371 {
1372   while (1)
1373     {
1374       if (a == b)
1375         return true;
1376       if (a == NULL || b == NULL)
1377         return false;
1378       if (!loc_descr_equal_p_1 (a, b))
1379         return false;
1380
1381       a = a->dw_loc_next;
1382       b = b->dw_loc_next;
1383     }
1384 }
1385
1386
1387 /* Add a constant OFFSET to a location expression.  */
1388
1389 static void
1390 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1391 {
1392   dw_loc_descr_ref loc;
1393   HOST_WIDE_INT *p;
1394
1395   gcc_assert (*list_head != NULL);
1396
1397   if (!offset)
1398     return;
1399
1400   /* Find the end of the chain.  */
1401   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1402     ;
1403
1404   p = NULL;
1405   if (loc->dw_loc_opc == DW_OP_fbreg
1406       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1407     p = &loc->dw_loc_oprnd1.v.val_int;
1408   else if (loc->dw_loc_opc == DW_OP_bregx)
1409     p = &loc->dw_loc_oprnd2.v.val_int;
1410
1411   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1412      offset.  Don't optimize if an signed integer overflow would happen.  */
1413   if (p != NULL
1414       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1415           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1416     *p += offset;
1417
1418   else if (offset > 0)
1419     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1420
1421   else
1422     {
1423       loc->dw_loc_next = int_loc_descriptor (-offset);
1424       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1425     }
1426 }
1427
1428 /* Add a constant OFFSET to a location list.  */
1429
1430 static void
1431 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1432 {
1433   dw_loc_list_ref d;
1434   for (d = list_head; d != NULL; d = d->dw_loc_next)
1435     loc_descr_plus_const (&d->expr, offset);
1436 }
1437
1438 #define DWARF_REF_SIZE  \
1439   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1440
1441 static unsigned long int get_base_type_offset (dw_die_ref);
1442
1443 /* Return the size of a location descriptor.  */
1444
1445 static unsigned long
1446 size_of_loc_descr (dw_loc_descr_ref loc)
1447 {
1448   unsigned long size = 1;
1449
1450   switch (loc->dw_loc_opc)
1451     {
1452     case DW_OP_addr:
1453       size += DWARF2_ADDR_SIZE;
1454       break;
1455     case DW_OP_const1u:
1456     case DW_OP_const1s:
1457       size += 1;
1458       break;
1459     case DW_OP_const2u:
1460     case DW_OP_const2s:
1461       size += 2;
1462       break;
1463     case DW_OP_const4u:
1464     case DW_OP_const4s:
1465       size += 4;
1466       break;
1467     case DW_OP_const8u:
1468     case DW_OP_const8s:
1469       size += 8;
1470       break;
1471     case DW_OP_constu:
1472       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1473       break;
1474     case DW_OP_consts:
1475       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1476       break;
1477     case DW_OP_pick:
1478       size += 1;
1479       break;
1480     case DW_OP_plus_uconst:
1481       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1482       break;
1483     case DW_OP_skip:
1484     case DW_OP_bra:
1485       size += 2;
1486       break;
1487     case DW_OP_breg0:
1488     case DW_OP_breg1:
1489     case DW_OP_breg2:
1490     case DW_OP_breg3:
1491     case DW_OP_breg4:
1492     case DW_OP_breg5:
1493     case DW_OP_breg6:
1494     case DW_OP_breg7:
1495     case DW_OP_breg8:
1496     case DW_OP_breg9:
1497     case DW_OP_breg10:
1498     case DW_OP_breg11:
1499     case DW_OP_breg12:
1500     case DW_OP_breg13:
1501     case DW_OP_breg14:
1502     case DW_OP_breg15:
1503     case DW_OP_breg16:
1504     case DW_OP_breg17:
1505     case DW_OP_breg18:
1506     case DW_OP_breg19:
1507     case DW_OP_breg20:
1508     case DW_OP_breg21:
1509     case DW_OP_breg22:
1510     case DW_OP_breg23:
1511     case DW_OP_breg24:
1512     case DW_OP_breg25:
1513     case DW_OP_breg26:
1514     case DW_OP_breg27:
1515     case DW_OP_breg28:
1516     case DW_OP_breg29:
1517     case DW_OP_breg30:
1518     case DW_OP_breg31:
1519       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1520       break;
1521     case DW_OP_regx:
1522       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1523       break;
1524     case DW_OP_fbreg:
1525       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1526       break;
1527     case DW_OP_bregx:
1528       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1529       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1530       break;
1531     case DW_OP_piece:
1532       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1533       break;
1534     case DW_OP_bit_piece:
1535       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1536       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1537       break;
1538     case DW_OP_deref_size:
1539     case DW_OP_xderef_size:
1540       size += 1;
1541       break;
1542     case DW_OP_call2:
1543       size += 2;
1544       break;
1545     case DW_OP_call4:
1546       size += 4;
1547       break;
1548     case DW_OP_call_ref:
1549       size += DWARF_REF_SIZE;
1550       break;
1551     case DW_OP_implicit_value:
1552       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1553               + loc->dw_loc_oprnd1.v.val_unsigned;
1554       break;
1555     case DW_OP_GNU_implicit_pointer:
1556       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1557       break;
1558     case DW_OP_GNU_entry_value:
1559       {
1560         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1561         size += size_of_uleb128 (op_size) + op_size;
1562         break;
1563       }
1564     case DW_OP_GNU_const_type:
1565       {
1566         unsigned long o
1567           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1568         size += size_of_uleb128 (o) + 1;
1569         switch (loc->dw_loc_oprnd2.val_class)
1570           {
1571           case dw_val_class_vec:
1572             size += loc->dw_loc_oprnd2.v.val_vec.length
1573                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1574             break;
1575           case dw_val_class_const:
1576             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1577             break;
1578           case dw_val_class_const_double:
1579             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1580             break;
1581           default:
1582             gcc_unreachable ();
1583           }
1584         break;
1585       }
1586     case DW_OP_GNU_regval_type:
1587       {
1588         unsigned long o
1589           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1590         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1591                 + size_of_uleb128 (o);
1592       }
1593       break;
1594     case DW_OP_GNU_deref_type:
1595       {
1596         unsigned long o
1597           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1598         size += 1 + size_of_uleb128 (o);
1599       }
1600       break;
1601     case DW_OP_GNU_convert:
1602     case DW_OP_GNU_reinterpret:
1603       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1604         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1605       else
1606         {
1607           unsigned long o
1608             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1609           size += size_of_uleb128 (o);
1610         }
1611       break;
1612     case DW_OP_GNU_parameter_ref:
1613       size += 4;
1614       break;
1615     default:
1616       break;
1617     }
1618
1619   return size;
1620 }
1621
1622 /* Return the size of a series of location descriptors.  */
1623
1624 unsigned long
1625 size_of_locs (dw_loc_descr_ref loc)
1626 {
1627   dw_loc_descr_ref l;
1628   unsigned long size;
1629
1630   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1631      field, to avoid writing to a PCH file.  */
1632   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1633     {
1634       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1635         break;
1636       size += size_of_loc_descr (l);
1637     }
1638   if (! l)
1639     return size;
1640
1641   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1642     {
1643       l->dw_loc_addr = size;
1644       size += size_of_loc_descr (l);
1645     }
1646
1647   return size;
1648 }
1649
1650 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1651 static void get_ref_die_offset_label (char *, dw_die_ref);
1652 static unsigned long int get_ref_die_offset (dw_die_ref);
1653
1654 /* Output location description stack opcode's operands (if any).
1655    The for_eh_or_skip parameter controls whether register numbers are
1656    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1657    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1658    info).  This should be suppressed for the cases that have not been converted
1659    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1660
1661 static void
1662 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1663 {
1664   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1665   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1666
1667   switch (loc->dw_loc_opc)
1668     {
1669 #ifdef DWARF2_DEBUGGING_INFO
1670     case DW_OP_const2u:
1671     case DW_OP_const2s:
1672       dw2_asm_output_data (2, val1->v.val_int, NULL);
1673       break;
1674     case DW_OP_const4u:
1675       if (loc->dtprel)
1676         {
1677           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1678           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1679                                                val1->v.val_addr);
1680           fputc ('\n', asm_out_file);
1681           break;
1682         }
1683       /* FALLTHRU */
1684     case DW_OP_const4s:
1685       dw2_asm_output_data (4, val1->v.val_int, NULL);
1686       break;
1687     case DW_OP_const8u:
1688       if (loc->dtprel)
1689         {
1690           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1691           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1692                                                val1->v.val_addr);
1693           fputc ('\n', asm_out_file);
1694           break;
1695         }
1696       /* FALLTHRU */
1697     case DW_OP_const8s:
1698       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1699       dw2_asm_output_data (8, val1->v.val_int, NULL);
1700       break;
1701     case DW_OP_skip:
1702     case DW_OP_bra:
1703       {
1704         int offset;
1705
1706         gcc_assert (val1->val_class == dw_val_class_loc);
1707         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1708
1709         dw2_asm_output_data (2, offset, NULL);
1710       }
1711       break;
1712     case DW_OP_implicit_value:
1713       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1714       switch (val2->val_class)
1715         {
1716         case dw_val_class_const:
1717           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1718           break;
1719         case dw_val_class_vec:
1720           {
1721             unsigned int elt_size = val2->v.val_vec.elt_size;
1722             unsigned int len = val2->v.val_vec.length;
1723             unsigned int i;
1724             unsigned char *p;
1725
1726             if (elt_size > sizeof (HOST_WIDE_INT))
1727               {
1728                 elt_size /= 2;
1729                 len *= 2;
1730               }
1731             for (i = 0, p = val2->v.val_vec.array;
1732                  i < len;
1733                  i++, p += elt_size)
1734               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1735                                    "fp or vector constant word %u", i);
1736           }
1737           break;
1738         case dw_val_class_const_double:
1739           {
1740             unsigned HOST_WIDE_INT first, second;
1741
1742             if (WORDS_BIG_ENDIAN)
1743               {
1744                 first = val2->v.val_double.high;
1745                 second = val2->v.val_double.low;
1746               }
1747             else
1748               {
1749                 first = val2->v.val_double.low;
1750                 second = val2->v.val_double.high;
1751               }
1752             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1753                                  first, NULL);
1754             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1755                                  second, NULL);
1756           }
1757           break;
1758         case dw_val_class_addr:
1759           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1760           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1761           break;
1762         default:
1763           gcc_unreachable ();
1764         }
1765       break;
1766 #else
1767     case DW_OP_const2u:
1768     case DW_OP_const2s:
1769     case DW_OP_const4u:
1770     case DW_OP_const4s:
1771     case DW_OP_const8u:
1772     case DW_OP_const8s:
1773     case DW_OP_skip:
1774     case DW_OP_bra:
1775     case DW_OP_implicit_value:
1776       /* We currently don't make any attempt to make sure these are
1777          aligned properly like we do for the main unwind info, so
1778          don't support emitting things larger than a byte if we're
1779          only doing unwinding.  */
1780       gcc_unreachable ();
1781 #endif
1782     case DW_OP_const1u:
1783     case DW_OP_const1s:
1784       dw2_asm_output_data (1, val1->v.val_int, NULL);
1785       break;
1786     case DW_OP_constu:
1787       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1788       break;
1789     case DW_OP_consts:
1790       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1791       break;
1792     case DW_OP_pick:
1793       dw2_asm_output_data (1, val1->v.val_int, NULL);
1794       break;
1795     case DW_OP_plus_uconst:
1796       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1797       break;
1798     case DW_OP_breg0:
1799     case DW_OP_breg1:
1800     case DW_OP_breg2:
1801     case DW_OP_breg3:
1802     case DW_OP_breg4:
1803     case DW_OP_breg5:
1804     case DW_OP_breg6:
1805     case DW_OP_breg7:
1806     case DW_OP_breg8:
1807     case DW_OP_breg9:
1808     case DW_OP_breg10:
1809     case DW_OP_breg11:
1810     case DW_OP_breg12:
1811     case DW_OP_breg13:
1812     case DW_OP_breg14:
1813     case DW_OP_breg15:
1814     case DW_OP_breg16:
1815     case DW_OP_breg17:
1816     case DW_OP_breg18:
1817     case DW_OP_breg19:
1818     case DW_OP_breg20:
1819     case DW_OP_breg21:
1820     case DW_OP_breg22:
1821     case DW_OP_breg23:
1822     case DW_OP_breg24:
1823     case DW_OP_breg25:
1824     case DW_OP_breg26:
1825     case DW_OP_breg27:
1826     case DW_OP_breg28:
1827     case DW_OP_breg29:
1828     case DW_OP_breg30:
1829     case DW_OP_breg31:
1830       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1831       break;
1832     case DW_OP_regx:
1833       {
1834         unsigned r = val1->v.val_unsigned;
1835         if (for_eh_or_skip >= 0)
1836           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1837         gcc_assert (size_of_uleb128 (r) 
1838                     == size_of_uleb128 (val1->v.val_unsigned));
1839         dw2_asm_output_data_uleb128 (r, NULL);  
1840       }
1841       break;
1842     case DW_OP_fbreg:
1843       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1844       break;
1845     case DW_OP_bregx:
1846       {
1847         unsigned r = val1->v.val_unsigned;
1848         if (for_eh_or_skip >= 0)
1849           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1850         gcc_assert (size_of_uleb128 (r) 
1851                     == size_of_uleb128 (val1->v.val_unsigned));
1852         dw2_asm_output_data_uleb128 (r, NULL);  
1853         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1854       }
1855       break;
1856     case DW_OP_piece:
1857       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1858       break;
1859     case DW_OP_bit_piece:
1860       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1861       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1862       break;
1863     case DW_OP_deref_size:
1864     case DW_OP_xderef_size:
1865       dw2_asm_output_data (1, val1->v.val_int, NULL);
1866       break;
1867
1868     case DW_OP_addr:
1869       if (loc->dtprel)
1870         {
1871           if (targetm.asm_out.output_dwarf_dtprel)
1872             {
1873               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1874                                                    DWARF2_ADDR_SIZE,
1875                                                    val1->v.val_addr);
1876               fputc ('\n', asm_out_file);
1877             }
1878           else
1879             gcc_unreachable ();
1880         }
1881       else
1882         {
1883 #ifdef DWARF2_DEBUGGING_INFO
1884           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1885 #else
1886           gcc_unreachable ();
1887 #endif
1888         }
1889       break;
1890
1891     case DW_OP_GNU_implicit_pointer:
1892       {
1893         char label[MAX_ARTIFICIAL_LABEL_BYTES
1894                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
1895         gcc_assert (val1->val_class == dw_val_class_die_ref);
1896         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1897         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1898         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1899       }
1900       break;
1901
1902     case DW_OP_GNU_entry_value:
1903       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1904       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1905       break;
1906
1907     case DW_OP_GNU_const_type:
1908       {
1909         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1910         gcc_assert (o);
1911         dw2_asm_output_data_uleb128 (o, NULL);
1912         switch (val2->val_class)
1913           {
1914           case dw_val_class_const:
1915             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1916             dw2_asm_output_data (1, l, NULL);
1917             dw2_asm_output_data (l, val2->v.val_int, NULL);
1918             break;
1919           case dw_val_class_vec:
1920             {
1921               unsigned int elt_size = val2->v.val_vec.elt_size;
1922               unsigned int len = val2->v.val_vec.length;
1923               unsigned int i;
1924               unsigned char *p;
1925
1926               l = len * elt_size;
1927               dw2_asm_output_data (1, l, NULL);
1928               if (elt_size > sizeof (HOST_WIDE_INT))
1929                 {
1930                   elt_size /= 2;
1931                   len *= 2;
1932                 }
1933               for (i = 0, p = val2->v.val_vec.array;
1934                    i < len;
1935                    i++, p += elt_size)
1936                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1937                                      "fp or vector constant word %u", i);
1938             }
1939             break;
1940           case dw_val_class_const_double:
1941             {
1942               unsigned HOST_WIDE_INT first, second;
1943               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1944
1945               dw2_asm_output_data (1, 2 * l, NULL);
1946               if (WORDS_BIG_ENDIAN)
1947                 {
1948                   first = val2->v.val_double.high;
1949                   second = val2->v.val_double.low;
1950                 }
1951               else
1952                 {
1953                   first = val2->v.val_double.low;
1954                   second = val2->v.val_double.high;
1955                 }
1956               dw2_asm_output_data (l, first, NULL);
1957               dw2_asm_output_data (l, second, NULL);
1958             }
1959             break;
1960           default:
1961             gcc_unreachable ();
1962           }
1963       }
1964       break;
1965     case DW_OP_GNU_regval_type:
1966       {
1967         unsigned r = val1->v.val_unsigned;
1968         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1969         gcc_assert (o);
1970         if (for_eh_or_skip >= 0)
1971           {
1972             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1973             gcc_assert (size_of_uleb128 (r)
1974                         == size_of_uleb128 (val1->v.val_unsigned));
1975           }
1976         dw2_asm_output_data_uleb128 (r, NULL);
1977         dw2_asm_output_data_uleb128 (o, NULL);
1978       }
1979       break;
1980     case DW_OP_GNU_deref_type:
1981       {
1982         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1983         gcc_assert (o);
1984         dw2_asm_output_data (1, val1->v.val_int, NULL);
1985         dw2_asm_output_data_uleb128 (o, NULL);
1986       }
1987       break;
1988     case DW_OP_GNU_convert:
1989     case DW_OP_GNU_reinterpret:
1990       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1991         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992       else
1993         {
1994           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
1995           gcc_assert (o);
1996           dw2_asm_output_data_uleb128 (o, NULL);
1997         }
1998       break;
1999
2000     case DW_OP_GNU_parameter_ref:
2001       {
2002         unsigned long o;
2003         gcc_assert (val1->val_class == dw_val_class_die_ref);
2004         o = get_ref_die_offset (val1->v.val_die_ref.die);
2005         dw2_asm_output_data (4, o, NULL);
2006       }
2007       break;
2008
2009     default:
2010       /* Other codes have no operands.  */
2011       break;
2012     }
2013 }
2014
2015 /* Output a sequence of location operations.  
2016    The for_eh_or_skip parameter controls whether register numbers are
2017    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2018    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2019    info).  This should be suppressed for the cases that have not been converted
2020    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2021
2022 void
2023 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2024 {
2025   for (; loc != NULL; loc = loc->dw_loc_next)
2026     {
2027       enum dwarf_location_atom opc = loc->dw_loc_opc;
2028       /* Output the opcode.  */
2029       if (for_eh_or_skip >= 0 
2030           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2031         {
2032           unsigned r = (opc - DW_OP_breg0);
2033           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2034           gcc_assert (r <= 31);
2035           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2036         }
2037       else if (for_eh_or_skip >= 0 
2038                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2039         {
2040           unsigned r = (opc - DW_OP_reg0);
2041           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2042           gcc_assert (r <= 31);
2043           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2044         }
2045
2046       dw2_asm_output_data (1, opc,
2047                              "%s", dwarf_stack_op_name (opc));
2048
2049       /* Output the operand(s) (if any).  */
2050       output_loc_operands (loc, for_eh_or_skip);
2051     }
2052 }
2053
2054 /* Output location description stack opcode's operands (if any).
2055    The output is single bytes on a line, suitable for .cfi_escape.  */
2056
2057 static void
2058 output_loc_operands_raw (dw_loc_descr_ref loc)
2059 {
2060   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2061   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2062
2063   switch (loc->dw_loc_opc)
2064     {
2065     case DW_OP_addr:
2066     case DW_OP_implicit_value:
2067       /* We cannot output addresses in .cfi_escape, only bytes.  */
2068       gcc_unreachable ();
2069
2070     case DW_OP_const1u:
2071     case DW_OP_const1s:
2072     case DW_OP_pick:
2073     case DW_OP_deref_size:
2074     case DW_OP_xderef_size:
2075       fputc (',', asm_out_file);
2076       dw2_asm_output_data_raw (1, val1->v.val_int);
2077       break;
2078
2079     case DW_OP_const2u:
2080     case DW_OP_const2s:
2081       fputc (',', asm_out_file);
2082       dw2_asm_output_data_raw (2, val1->v.val_int);
2083       break;
2084
2085     case DW_OP_const4u:
2086     case DW_OP_const4s:
2087       fputc (',', asm_out_file);
2088       dw2_asm_output_data_raw (4, val1->v.val_int);
2089       break;
2090
2091     case DW_OP_const8u:
2092     case DW_OP_const8s:
2093       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2094       fputc (',', asm_out_file);
2095       dw2_asm_output_data_raw (8, val1->v.val_int);
2096       break;
2097
2098     case DW_OP_skip:
2099     case DW_OP_bra:
2100       {
2101         int offset;
2102
2103         gcc_assert (val1->val_class == dw_val_class_loc);
2104         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2105
2106         fputc (',', asm_out_file);
2107         dw2_asm_output_data_raw (2, offset);
2108       }
2109       break;
2110
2111     case DW_OP_regx:
2112       {
2113         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2114         gcc_assert (size_of_uleb128 (r) 
2115                     == size_of_uleb128 (val1->v.val_unsigned));
2116         fputc (',', asm_out_file);
2117         dw2_asm_output_data_uleb128_raw (r);
2118       }
2119       break;
2120       
2121     case DW_OP_constu:
2122     case DW_OP_plus_uconst:
2123     case DW_OP_piece:
2124       fputc (',', asm_out_file);
2125       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2126       break;
2127
2128     case DW_OP_bit_piece:
2129       fputc (',', asm_out_file);
2130       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2131       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2132       break;
2133
2134     case DW_OP_consts:
2135     case DW_OP_breg0:
2136     case DW_OP_breg1:
2137     case DW_OP_breg2:
2138     case DW_OP_breg3:
2139     case DW_OP_breg4:
2140     case DW_OP_breg5:
2141     case DW_OP_breg6:
2142     case DW_OP_breg7:
2143     case DW_OP_breg8:
2144     case DW_OP_breg9:
2145     case DW_OP_breg10:
2146     case DW_OP_breg11:
2147     case DW_OP_breg12:
2148     case DW_OP_breg13:
2149     case DW_OP_breg14:
2150     case DW_OP_breg15:
2151     case DW_OP_breg16:
2152     case DW_OP_breg17:
2153     case DW_OP_breg18:
2154     case DW_OP_breg19:
2155     case DW_OP_breg20:
2156     case DW_OP_breg21:
2157     case DW_OP_breg22:
2158     case DW_OP_breg23:
2159     case DW_OP_breg24:
2160     case DW_OP_breg25:
2161     case DW_OP_breg26:
2162     case DW_OP_breg27:
2163     case DW_OP_breg28:
2164     case DW_OP_breg29:
2165     case DW_OP_breg30:
2166     case DW_OP_breg31:
2167     case DW_OP_fbreg:
2168       fputc (',', asm_out_file);
2169       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2170       break;
2171
2172     case DW_OP_bregx:
2173       {
2174         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2175         gcc_assert (size_of_uleb128 (r) 
2176                     == size_of_uleb128 (val1->v.val_unsigned));
2177         fputc (',', asm_out_file);
2178         dw2_asm_output_data_uleb128_raw (r);
2179         fputc (',', asm_out_file);
2180         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2181       }
2182       break;
2183
2184     case DW_OP_GNU_implicit_pointer:
2185     case DW_OP_GNU_entry_value:
2186     case DW_OP_GNU_const_type:
2187     case DW_OP_GNU_regval_type:
2188     case DW_OP_GNU_deref_type:
2189     case DW_OP_GNU_convert:
2190     case DW_OP_GNU_reinterpret:
2191     case DW_OP_GNU_parameter_ref:
2192       gcc_unreachable ();
2193       break;
2194
2195     default:
2196       /* Other codes have no operands.  */
2197       break;
2198     }
2199 }
2200
2201 void
2202 output_loc_sequence_raw (dw_loc_descr_ref loc)
2203 {
2204   while (1)
2205     {
2206       enum dwarf_location_atom opc = loc->dw_loc_opc;
2207       /* Output the opcode.  */
2208       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2209         {
2210           unsigned r = (opc - DW_OP_breg0);
2211           r = DWARF2_FRAME_REG_OUT (r, 1);
2212           gcc_assert (r <= 31);
2213           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2214         }
2215       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2216         {
2217           unsigned r = (opc - DW_OP_reg0);
2218           r = DWARF2_FRAME_REG_OUT (r, 1);
2219           gcc_assert (r <= 31);
2220           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2221         }
2222       /* Output the opcode.  */
2223       fprintf (asm_out_file, "%#x", opc);
2224       output_loc_operands_raw (loc);
2225
2226       if (!loc->dw_loc_next)
2227         break;
2228       loc = loc->dw_loc_next;
2229
2230       fputc (',', asm_out_file);
2231     }
2232 }
2233
2234 /* This function builds a dwarf location descriptor sequence from a
2235    dw_cfa_location, adding the given OFFSET to the result of the
2236    expression.  */
2237
2238 struct dw_loc_descr_struct *
2239 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2240 {
2241   struct dw_loc_descr_struct *head, *tmp;
2242
2243   offset += cfa->offset;
2244
2245   if (cfa->indirect)
2246     {
2247       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2248       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2249       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2250       add_loc_descr (&head, tmp);
2251       if (offset != 0)
2252         {
2253           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2254           add_loc_descr (&head, tmp);
2255         }
2256     }
2257   else
2258     head = new_reg_loc_descr (cfa->reg, offset);
2259
2260   return head;
2261 }
2262
2263 /* This function builds a dwarf location descriptor sequence for
2264    the address at OFFSET from the CFA when stack is aligned to
2265    ALIGNMENT byte.  */
2266
2267 struct dw_loc_descr_struct *
2268 build_cfa_aligned_loc (dw_cfa_location *cfa,
2269                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2270 {
2271   struct dw_loc_descr_struct *head;
2272   unsigned int dwarf_fp
2273     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2274
2275   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2276   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2277     {
2278       head = new_reg_loc_descr (dwarf_fp, 0);
2279       add_loc_descr (&head, int_loc_descriptor (alignment));
2280       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2281       loc_descr_plus_const (&head, offset);
2282     }
2283   else
2284     head = new_reg_loc_descr (dwarf_fp, offset);
2285   return head;
2286 }
2287 \f
2288 /* And now, the support for symbolic debugging information.  */
2289
2290 /* .debug_str support.  */
2291 static int output_indirect_string (void **, void *);
2292
2293 static void dwarf2out_init (const char *);
2294 static void dwarf2out_finish (const char *);
2295 static void dwarf2out_assembly_start (void);
2296 static void dwarf2out_define (unsigned int, const char *);
2297 static void dwarf2out_undef (unsigned int, const char *);
2298 static void dwarf2out_start_source_file (unsigned, const char *);
2299 static void dwarf2out_end_source_file (unsigned);
2300 static void dwarf2out_function_decl (tree);
2301 static void dwarf2out_begin_block (unsigned, unsigned);
2302 static void dwarf2out_end_block (unsigned, unsigned);
2303 static bool dwarf2out_ignore_block (const_tree);
2304 static void dwarf2out_global_decl (tree);
2305 static void dwarf2out_type_decl (tree, int);
2306 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2307 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2308                                                  dw_die_ref);
2309 static void dwarf2out_abstract_function (tree);
2310 static void dwarf2out_var_location (rtx);
2311 static void dwarf2out_begin_function (tree);
2312 static void dwarf2out_set_name (tree, tree);
2313
2314 /* The debug hooks structure.  */
2315
2316 const struct gcc_debug_hooks dwarf2_debug_hooks =
2317 {
2318   dwarf2out_init,
2319   dwarf2out_finish,
2320   dwarf2out_assembly_start,
2321   dwarf2out_define,
2322   dwarf2out_undef,
2323   dwarf2out_start_source_file,
2324   dwarf2out_end_source_file,
2325   dwarf2out_begin_block,
2326   dwarf2out_end_block,
2327   dwarf2out_ignore_block,
2328   dwarf2out_source_line,
2329   dwarf2out_begin_prologue,
2330 #if VMS_DEBUGGING_INFO
2331   dwarf2out_vms_end_prologue,
2332   dwarf2out_vms_begin_epilogue,
2333 #else
2334   debug_nothing_int_charstar,
2335   debug_nothing_int_charstar,
2336 #endif
2337   dwarf2out_end_epilogue,
2338   dwarf2out_begin_function,
2339   debug_nothing_int,            /* end_function */
2340   dwarf2out_function_decl,      /* function_decl */
2341   dwarf2out_global_decl,
2342   dwarf2out_type_decl,          /* type_decl */
2343   dwarf2out_imported_module_or_decl,
2344   debug_nothing_tree,           /* deferred_inline_function */
2345   /* The DWARF 2 backend tries to reduce debugging bloat by not
2346      emitting the abstract description of inline functions until
2347      something tries to reference them.  */
2348   dwarf2out_abstract_function,  /* outlining_inline_function */
2349   debug_nothing_rtx,            /* label */
2350   debug_nothing_int,            /* handle_pch */
2351   dwarf2out_var_location,
2352   dwarf2out_switch_text_section,
2353   dwarf2out_set_name,
2354   1,                            /* start_end_main_source_file */
2355   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2356 };
2357 \f
2358 /* NOTE: In the comments in this file, many references are made to
2359    "Debugging Information Entries".  This term is abbreviated as `DIE'
2360    throughout the remainder of this file.  */
2361
2362 /* An internal representation of the DWARF output is built, and then
2363    walked to generate the DWARF debugging info.  The walk of the internal
2364    representation is done after the entire program has been compiled.
2365    The types below are used to describe the internal representation.  */
2366
2367 /* Whether to put type DIEs into their own section .debug_types instead
2368    of making them part of the .debug_info section.  Only supported for
2369    Dwarf V4 or higher and the user didn't disable them through
2370    -fno-debug-types-section.  It is more efficient to put them in a
2371    separate comdat sections since the linker will then be able to
2372    remove duplicates.  But not all tools support .debug_types sections
2373    yet.  */
2374
2375 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2376
2377 /* Various DIE's use offsets relative to the beginning of the
2378    .debug_info section to refer to each other.  */
2379
2380 typedef long int dw_offset;
2381
2382 /* Define typedefs here to avoid circular dependencies.  */
2383
2384 typedef struct dw_attr_struct *dw_attr_ref;
2385 typedef struct dw_line_info_struct *dw_line_info_ref;
2386 typedef struct pubname_struct *pubname_ref;
2387 typedef struct dw_ranges_struct *dw_ranges_ref;
2388 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2389 typedef struct comdat_type_struct *comdat_type_node_ref;
2390
2391 /* The entries in the line_info table more-or-less mirror the opcodes
2392    that are used in the real dwarf line table.  Arrays of these entries
2393    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2394    supported.  */
2395
2396 enum dw_line_info_opcode {
2397   /* Emit DW_LNE_set_address; the operand is the label index.  */
2398   LI_set_address,
2399
2400   /* Emit a row to the matrix with the given line.  This may be done
2401      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2402      special opcodes.  */
2403   LI_set_line,
2404
2405   /* Emit a DW_LNS_set_file.  */
2406   LI_set_file,
2407
2408   /* Emit a DW_LNS_set_column.  */
2409   LI_set_column,
2410
2411   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2412   LI_negate_stmt,
2413
2414   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2415   LI_set_prologue_end,
2416   LI_set_epilogue_begin,
2417
2418   /* Emit a DW_LNE_set_discriminator.  */
2419   LI_set_discriminator
2420 };
2421
2422 typedef struct GTY(()) dw_line_info_struct {
2423   enum dw_line_info_opcode opcode;
2424   unsigned int val;
2425 } dw_line_info_entry;
2426
2427 DEF_VEC_O(dw_line_info_entry);
2428 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2429
2430 typedef struct GTY(()) dw_line_info_table_struct {
2431   /* The label that marks the end of this section.  */
2432   const char *end_label;
2433
2434   /* The values for the last row of the matrix, as collected in the table.
2435      These are used to minimize the changes to the next row.  */
2436   unsigned int file_num;
2437   unsigned int line_num;
2438   unsigned int column_num;
2439   int discrim_num;
2440   bool is_stmt;
2441   bool in_use;
2442
2443   VEC(dw_line_info_entry, gc) *entries;
2444 } dw_line_info_table;
2445
2446 typedef dw_line_info_table *dw_line_info_table_p;
2447
2448 DEF_VEC_P(dw_line_info_table_p);
2449 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2450
2451 /* Each DIE attribute has a field specifying the attribute kind,
2452    a link to the next attribute in the chain, and an attribute value.
2453    Attributes are typically linked below the DIE they modify.  */
2454
2455 typedef struct GTY(()) dw_attr_struct {
2456   enum dwarf_attribute dw_attr;
2457   dw_val_node dw_attr_val;
2458 }
2459 dw_attr_node;
2460
2461 DEF_VEC_O(dw_attr_node);
2462 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2463
2464 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2465    The children of each node form a circular list linked by
2466    die_sib.  die_child points to the node *before* the "first" child node.  */
2467
2468 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2469   union die_symbol_or_type_node
2470     {
2471       const char * GTY ((tag ("0"))) die_symbol;
2472       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2473     }
2474   GTY ((desc ("%0.comdat_type_p"))) die_id;
2475   VEC(dw_attr_node,gc) * die_attr;
2476   dw_die_ref die_parent;
2477   dw_die_ref die_child;
2478   dw_die_ref die_sib;
2479   dw_die_ref die_definition; /* ref from a specification to its definition */
2480   dw_offset die_offset;
2481   unsigned long die_abbrev;
2482   int die_mark;
2483   unsigned int decl_id;
2484   enum dwarf_tag die_tag;
2485   /* Die is used and must not be pruned as unused.  */
2486   BOOL_BITFIELD die_perennial_p : 1;
2487   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2488   /* Lots of spare bits.  */
2489 }
2490 die_node;
2491
2492 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2493 #define FOR_EACH_CHILD(die, c, expr) do {       \
2494   c = die->die_child;                           \
2495   if (c) do {                                   \
2496     c = c->die_sib;                             \
2497     expr;                                       \
2498   } while (c != die->die_child);                \
2499 } while (0)
2500
2501 /* The pubname structure */
2502
2503 typedef struct GTY(()) pubname_struct {
2504   dw_die_ref die;
2505   const char *name;
2506 }
2507 pubname_entry;
2508
2509 DEF_VEC_O(pubname_entry);
2510 DEF_VEC_ALLOC_O(pubname_entry, gc);
2511
2512 struct GTY(()) dw_ranges_struct {
2513   /* If this is positive, it's a block number, otherwise it's a
2514      bitwise-negated index into dw_ranges_by_label.  */
2515   int num;
2516 };
2517
2518 /* A structure to hold a macinfo entry.  */
2519
2520 typedef struct GTY(()) macinfo_struct {
2521   unsigned char code;
2522   unsigned HOST_WIDE_INT lineno;
2523   const char *info;
2524 }
2525 macinfo_entry;
2526
2527 DEF_VEC_O(macinfo_entry);
2528 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2529
2530 struct GTY(()) dw_ranges_by_label_struct {
2531   const char *begin;
2532   const char *end;
2533 };
2534
2535 /* The comdat type node structure.  */
2536 typedef struct GTY(()) comdat_type_struct
2537 {
2538   dw_die_ref root_die;
2539   dw_die_ref type_die;
2540   dw_die_ref skeleton_die;
2541   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2542   struct comdat_type_struct *next;
2543 }
2544 comdat_type_node;
2545
2546 /* The limbo die list structure.  */
2547 typedef struct GTY(()) limbo_die_struct {
2548   dw_die_ref die;
2549   tree created_for;
2550   struct limbo_die_struct *next;
2551 }
2552 limbo_die_node;
2553
2554 typedef struct skeleton_chain_struct
2555 {
2556   dw_die_ref old_die;
2557   dw_die_ref new_die;
2558   struct skeleton_chain_struct *parent;
2559 }
2560 skeleton_chain_node;
2561
2562 /* Define a macro which returns nonzero for a TYPE_DECL which was
2563    implicitly generated for a type.
2564
2565    Note that, unlike the C front-end (which generates a NULL named
2566    TYPE_DECL node for each complete tagged type, each array type,
2567    and each function type node created) the C++ front-end generates
2568    a _named_ TYPE_DECL node for each tagged type node created.
2569    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2570    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2571    front-end, but for each type, tagged or not.  */
2572
2573 #define TYPE_DECL_IS_STUB(decl)                         \
2574   (DECL_NAME (decl) == NULL_TREE                        \
2575    || (DECL_ARTIFICIAL (decl)                           \
2576        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2577            /* This is necessary for stub decls that     \
2578               appear in nested inline functions.  */    \
2579            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2580                && (decl_ultimate_origin (decl)          \
2581                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2582
2583 /* Information concerning the compilation unit's programming
2584    language, and compiler version.  */
2585
2586 /* Fixed size portion of the DWARF compilation unit header.  */
2587 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2588   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2589
2590 /* Fixed size portion of the DWARF comdat type unit header.  */
2591 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2592   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2593    + DWARF_OFFSET_SIZE)
2594
2595 /* Fixed size portion of public names info.  */
2596 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2597
2598 /* Fixed size portion of the address range info.  */
2599 #define DWARF_ARANGES_HEADER_SIZE                                       \
2600   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2601                 DWARF2_ADDR_SIZE * 2)                                   \
2602    - DWARF_INITIAL_LENGTH_SIZE)
2603
2604 /* Size of padding portion in the address range info.  It must be
2605    aligned to twice the pointer size.  */
2606 #define DWARF_ARANGES_PAD_SIZE \
2607   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2608                 DWARF2_ADDR_SIZE * 2)                              \
2609    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2610
2611 /* Use assembler line directives if available.  */
2612 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2613 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2614 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2615 #else
2616 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2617 #endif
2618 #endif
2619
2620 /* Minimum line offset in a special line info. opcode.
2621    This value was chosen to give a reasonable range of values.  */
2622 #define DWARF_LINE_BASE  -10
2623
2624 /* First special line opcode - leave room for the standard opcodes.  */
2625 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2626
2627 /* Range of line offsets in a special line info. opcode.  */
2628 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2629
2630 /* Flag that indicates the initial value of the is_stmt_start flag.
2631    In the present implementation, we do not mark any lines as
2632    the beginning of a source statement, because that information
2633    is not made available by the GCC front-end.  */
2634 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2635
2636 /* Maximum number of operations per instruction bundle.  */
2637 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2638 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2639 #endif
2640
2641 /* This location is used by calc_die_sizes() to keep track
2642    the offset of each DIE within the .debug_info section.  */
2643 static unsigned long next_die_offset;
2644
2645 /* Record the root of the DIE's built for the current compilation unit.  */
2646 static GTY(()) dw_die_ref single_comp_unit_die;
2647
2648 /* A list of type DIEs that have been separated into comdat sections.  */
2649 static GTY(()) comdat_type_node *comdat_type_list;
2650
2651 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2652 static GTY(()) limbo_die_node *limbo_die_list;
2653
2654 /* A list of DIEs for which we may have to generate
2655    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2656 static GTY(()) limbo_die_node *deferred_asm_name;
2657
2658 /* Filenames referenced by this compilation unit.  */
2659 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2660
2661 /* A hash table of references to DIE's that describe declarations.
2662    The key is a DECL_UID() which is a unique number identifying each decl.  */
2663 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2664
2665 /* A hash table of references to DIE's that describe COMMON blocks.
2666    The key is DECL_UID() ^ die_parent.  */
2667 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2668
2669 typedef struct GTY(()) die_arg_entry_struct {
2670     dw_die_ref die;
2671     tree arg;
2672 } die_arg_entry;
2673
2674 DEF_VEC_O(die_arg_entry);
2675 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2676
2677 /* Node of the variable location list.  */
2678 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2679   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2680      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2681      in mode of the EXPR_LIST node and first EXPR_LIST operand
2682      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2683      location or NULL for padding.  For larger bitsizes,
2684      mode is 0 and first operand is a CONCAT with bitsize
2685      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2686      NULL as second operand.  */
2687   rtx GTY (()) loc;
2688   const char * GTY (()) label;
2689   struct var_loc_node * GTY (()) next;
2690 };
2691
2692 /* Variable location list.  */
2693 struct GTY (()) var_loc_list_def {
2694   struct var_loc_node * GTY (()) first;
2695
2696   /* Pointer to the last but one or last element of the
2697      chained list.  If the list is empty, both first and
2698      last are NULL, if the list contains just one node
2699      or the last node certainly is not redundant, it points
2700      to the last node, otherwise points to the last but one.
2701      Do not mark it for GC because it is marked through the chain.  */
2702   struct var_loc_node * GTY ((skip ("%h"))) last;
2703
2704   /* Pointer to the last element before section switch,
2705      if NULL, either sections weren't switched or first
2706      is after section switch.  */
2707   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2708
2709   /* DECL_UID of the variable decl.  */
2710   unsigned int decl_id;
2711 };
2712 typedef struct var_loc_list_def var_loc_list;
2713
2714 /* Call argument location list.  */
2715 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2716   rtx GTY (()) call_arg_loc_note;
2717   const char * GTY (()) label;
2718   tree GTY (()) block;
2719   bool tail_call_p;
2720   rtx GTY (()) symbol_ref;
2721   struct call_arg_loc_node * GTY (()) next;
2722 };
2723
2724
2725 /* Table of decl location linked lists.  */
2726 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2727
2728 /* Head and tail of call_arg_loc chain.  */
2729 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2730 static struct call_arg_loc_node *call_arg_loc_last;
2731
2732 /* Number of call sites in the current function.  */
2733 static int call_site_count = -1;
2734 /* Number of tail call sites in the current function.  */
2735 static int tail_call_site_count = -1;
2736
2737 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2738    DIEs.  */
2739 static VEC (dw_die_ref, heap) *block_map;
2740
2741 /* A cached location list.  */
2742 struct GTY (()) cached_dw_loc_list_def {
2743   /* The DECL_UID of the decl that this entry describes.  */
2744   unsigned int decl_id;
2745
2746   /* The cached location list.  */
2747   dw_loc_list_ref loc_list;
2748 };
2749 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2750
2751 /* Table of cached location lists.  */
2752 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2753
2754 /* A pointer to the base of a list of references to DIE's that
2755    are uniquely identified by their tag, presence/absence of
2756    children DIE's, and list of attribute/value pairs.  */
2757 static GTY((length ("abbrev_die_table_allocated")))
2758   dw_die_ref *abbrev_die_table;
2759
2760 /* Number of elements currently allocated for abbrev_die_table.  */
2761 static GTY(()) unsigned abbrev_die_table_allocated;
2762
2763 /* Number of elements in type_die_table currently in use.  */
2764 static GTY(()) unsigned abbrev_die_table_in_use;
2765
2766 /* Size (in elements) of increments by which we may expand the
2767    abbrev_die_table.  */
2768 #define ABBREV_DIE_TABLE_INCREMENT 256
2769
2770 /* A global counter for generating labels for line number data.  */
2771 static unsigned int line_info_label_num;
2772
2773 /* The current table to which we should emit line number information
2774    for the current function.  This will be set up at the beginning of
2775    assembly for the function.  */
2776 static dw_line_info_table *cur_line_info_table;
2777
2778 /* The two default tables of line number info.  */
2779 static GTY(()) dw_line_info_table *text_section_line_info;
2780 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2781
2782 /* The set of all non-default tables of line number info.  */
2783 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
2784
2785 /* A flag to tell pubnames/types export if there is an info section to
2786    refer to.  */
2787 static bool info_section_emitted;
2788
2789 /* A pointer to the base of a table that contains a list of publicly
2790    accessible names.  */
2791 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
2792
2793 /* A pointer to the base of a table that contains a list of publicly
2794    accessible types.  */
2795 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
2796
2797 /* A pointer to the base of a table that contains a list of macro
2798    defines/undefines (and file start/end markers).  */
2799 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
2800
2801 /* True if .debug_macinfo or .debug_macros section is going to be
2802    emitted.  */
2803 #define have_macinfo \
2804   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2805    && !VEC_empty (macinfo_entry, macinfo_table))
2806
2807 /* Array of dies for which we should generate .debug_ranges info.  */
2808 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2809
2810 /* Number of elements currently allocated for ranges_table.  */
2811 static GTY(()) unsigned ranges_table_allocated;
2812
2813 /* Number of elements in ranges_table currently in use.  */
2814 static GTY(()) unsigned ranges_table_in_use;
2815
2816 /* Array of pairs of labels referenced in ranges_table.  */
2817 static GTY ((length ("ranges_by_label_allocated")))
2818      dw_ranges_by_label_ref ranges_by_label;
2819
2820 /* Number of elements currently allocated for ranges_by_label.  */
2821 static GTY(()) unsigned ranges_by_label_allocated;
2822
2823 /* Number of elements in ranges_by_label currently in use.  */
2824 static GTY(()) unsigned ranges_by_label_in_use;
2825
2826 /* Size (in elements) of increments by which we may expand the
2827    ranges_table.  */
2828 #define RANGES_TABLE_INCREMENT 64
2829
2830 /* Whether we have location lists that need outputting */
2831 static GTY(()) bool have_location_lists;
2832
2833 /* Unique label counter.  */
2834 static GTY(()) unsigned int loclabel_num;
2835
2836 /* Unique label counter for point-of-call tables.  */
2837 static GTY(()) unsigned int poc_label_num;
2838
2839 /* Record whether the function being analyzed contains inlined functions.  */
2840 static int current_function_has_inlines;
2841
2842 /* The last file entry emitted by maybe_emit_file().  */
2843 static GTY(()) struct dwarf_file_data * last_emitted_file;
2844
2845 /* Number of internal labels generated by gen_internal_sym().  */
2846 static GTY(()) int label_num;
2847
2848 /* Cached result of previous call to lookup_filename.  */
2849 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2850
2851 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
2852
2853 /* Instances of generic types for which we need to generate debug
2854    info that describe their generic parameters and arguments. That
2855    generation needs to happen once all types are properly laid out so
2856    we do it at the end of compilation.  */
2857 static GTY(()) VEC(tree,gc) *generic_type_instances;
2858
2859 /* Offset from the "steady-state frame pointer" to the frame base,
2860    within the current function.  */
2861 static HOST_WIDE_INT frame_pointer_fb_offset;
2862 static bool frame_pointer_fb_offset_valid;
2863
2864 static VEC (dw_die_ref, heap) *base_types;
2865
2866 /* Forward declarations for functions defined in this file.  */
2867
2868 static int is_pseudo_reg (const_rtx);
2869 static tree type_main_variant (tree);
2870 static int is_tagged_type (const_tree);
2871 static const char *dwarf_tag_name (unsigned);
2872 static const char *dwarf_attr_name (unsigned);
2873 static const char *dwarf_form_name (unsigned);
2874 static tree decl_ultimate_origin (const_tree);
2875 static tree decl_class_context (tree);
2876 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2877 static inline enum dw_val_class AT_class (dw_attr_ref);
2878 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2879 static inline unsigned AT_flag (dw_attr_ref);
2880 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2881 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2882 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2883 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2884 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2885                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2886 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2887                                unsigned int, unsigned char *);
2888 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2889 static hashval_t debug_str_do_hash (const void *);
2890 static int debug_str_eq (const void *, const void *);
2891 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2892 static inline const char *AT_string (dw_attr_ref);
2893 static enum dwarf_form AT_string_form (dw_attr_ref);
2894 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2895 static void add_AT_specification (dw_die_ref, dw_die_ref);
2896 static inline dw_die_ref AT_ref (dw_attr_ref);
2897 static inline int AT_ref_external (dw_attr_ref);
2898 static inline void set_AT_ref_external (dw_attr_ref, int);
2899 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2900 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2901 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2902 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2903                              dw_loc_list_ref);
2904 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2905 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
2906 static inline rtx AT_addr (dw_attr_ref);
2907 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2908 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2909 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2910 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2911                            unsigned HOST_WIDE_INT);
2912 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2913                                unsigned long);
2914 static inline const char *AT_lbl (dw_attr_ref);
2915 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2916 static const char *get_AT_low_pc (dw_die_ref);
2917 static const char *get_AT_hi_pc (dw_die_ref);
2918 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2919 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2920 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2921 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2922 static bool is_cxx (void);
2923 static bool is_fortran (void);
2924 static bool is_ada (void);
2925 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2926 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2927 static void add_child_die (dw_die_ref, dw_die_ref);
2928 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2929 static dw_die_ref lookup_type_die (tree);
2930 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2931 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2932 static void equate_type_number_to_die (tree, dw_die_ref);
2933 static hashval_t decl_die_table_hash (const void *);
2934 static int decl_die_table_eq (const void *, const void *);
2935 static dw_die_ref lookup_decl_die (tree);
2936 static hashval_t common_block_die_table_hash (const void *);
2937 static int common_block_die_table_eq (const void *, const void *);
2938 static hashval_t decl_loc_table_hash (const void *);
2939 static int decl_loc_table_eq (const void *, const void *);
2940 static var_loc_list *lookup_decl_loc (const_tree);
2941 static void equate_decl_number_to_die (tree, dw_die_ref);
2942 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2943 static void print_spaces (FILE *);
2944 static void print_die (dw_die_ref, FILE *);
2945 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2946 static dw_die_ref pop_compile_unit (dw_die_ref);
2947 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2948 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2949 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2950 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2951 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2952 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2953 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2954                                    struct md5_ctx *, int *);
2955 struct checksum_attributes;
2956 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2957 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2958 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2959 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2960 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2961 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2962 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2963 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2964 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2965 static void compute_section_prefix (dw_die_ref);
2966 static int is_type_die (dw_die_ref);
2967 static int is_comdat_die (dw_die_ref);
2968 static int is_symbol_die (dw_die_ref);
2969 static void assign_symbol_names (dw_die_ref);
2970 static void break_out_includes (dw_die_ref);
2971 static int is_declaration_die (dw_die_ref);
2972 static int should_move_die_to_comdat (dw_die_ref);
2973 static dw_die_ref clone_as_declaration (dw_die_ref);
2974 static dw_die_ref clone_die (dw_die_ref);
2975 static dw_die_ref clone_tree (dw_die_ref);
2976 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
2977 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
2978 static void generate_skeleton_bottom_up (skeleton_chain_node *);
2979 static dw_die_ref generate_skeleton (dw_die_ref);
2980 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
2981                                                          dw_die_ref,
2982                                                          dw_die_ref);
2983 static void break_out_comdat_types (dw_die_ref);
2984 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
2985 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
2986 static void copy_decls_for_unworthy_types (dw_die_ref);
2987
2988 static hashval_t htab_cu_hash (const void *);
2989 static int htab_cu_eq (const void *, const void *);
2990 static void htab_cu_del (void *);
2991 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
2992 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
2993 static void add_sibling_attributes (dw_die_ref);
2994 static void build_abbrev_table (dw_die_ref, htab_t);
2995 static void output_location_lists (dw_die_ref);
2996 static int constant_size (unsigned HOST_WIDE_INT);
2997 static unsigned long size_of_die (dw_die_ref);
2998 static void calc_die_sizes (dw_die_ref);
2999 static void calc_base_type_die_sizes (void);
3000 static void mark_dies (dw_die_ref);
3001 static void unmark_dies (dw_die_ref);
3002 static void unmark_all_dies (dw_die_ref);
3003 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3004 static unsigned long size_of_aranges (void);
3005 static enum dwarf_form value_format (dw_attr_ref);
3006 static void output_value_format (dw_attr_ref);
3007 static void output_abbrev_section (void);
3008 static void output_die_symbol (dw_die_ref);
3009 static void output_die (dw_die_ref);
3010 static void output_compilation_unit_header (void);
3011 static void output_comp_unit (dw_die_ref, int);
3012 static void output_comdat_type_unit (comdat_type_node *);
3013 static const char *dwarf2_name (tree, int);
3014 static void add_pubname (tree, dw_die_ref);
3015 static void add_enumerator_pubname (const char *, dw_die_ref);
3016 static void add_pubname_string (const char *, dw_die_ref);
3017 static void add_pubtype (tree, dw_die_ref);
3018 static void output_pubnames (VEC (pubname_entry,gc) *);
3019 static void output_aranges (unsigned long);
3020 static unsigned int add_ranges_num (int);
3021 static unsigned int add_ranges (const_tree);
3022 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3023                                   bool *);
3024 static void output_ranges (void);
3025 static dw_line_info_table *new_line_info_table (void);
3026 static void output_line_info (void);
3027 static void output_file_names (void);
3028 static dw_die_ref base_type_die (tree);
3029 static int is_base_type (tree);
3030 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3031 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3032 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3033 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3034 static int type_is_enum (const_tree);
3035 static unsigned int dbx_reg_number (const_rtx);
3036 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3037 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3038 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3039                                                 enum var_init_status);
3040 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3041                                                      enum var_init_status);
3042 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3043                                          enum var_init_status);
3044 static int is_based_loc (const_rtx);
3045 static int resolve_one_addr (rtx *, void *);
3046 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3047                                                enum var_init_status);
3048 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3049                                         enum var_init_status);
3050 static dw_loc_list_ref loc_list_from_tree (tree, int);
3051 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3052 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3053 static tree field_type (const_tree);
3054 static unsigned int simple_type_align_in_bits (const_tree);
3055 static unsigned int simple_decl_align_in_bits (const_tree);
3056 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3057 static HOST_WIDE_INT field_byte_offset (const_tree);
3058 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3059                                          dw_loc_list_ref);
3060 static void add_data_member_location_attribute (dw_die_ref, tree);
3061 static bool add_const_value_attribute (dw_die_ref, rtx);
3062 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3063 static void insert_double (double_int, unsigned char *);
3064 static void insert_float (const_rtx, unsigned char *);
3065 static rtx rtl_for_decl_location (tree);
3066 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3067                                                    enum dwarf_attribute);
3068 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3069 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3070 static void add_name_attribute (dw_die_ref, const char *);
3071 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3072 static void add_comp_dir_attribute (dw_die_ref);
3073 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3074 static void add_subscript_info (dw_die_ref, tree, bool);
3075 static void add_byte_size_attribute (dw_die_ref, tree);
3076 static void add_bit_offset_attribute (dw_die_ref, tree);
3077 static void add_bit_size_attribute (dw_die_ref, tree);
3078 static void add_prototyped_attribute (dw_die_ref, tree);
3079 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3080 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3081 static void add_src_coords_attributes (dw_die_ref, tree);
3082 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3083 static void push_decl_scope (tree);
3084 static void pop_decl_scope (void);
3085 static dw_die_ref scope_die_for (tree, dw_die_ref);
3086 static inline int local_scope_p (dw_die_ref);
3087 static inline int class_scope_p (dw_die_ref);
3088 static inline int class_or_namespace_scope_p (dw_die_ref);
3089 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3090 static void add_calling_convention_attribute (dw_die_ref, tree);
3091 static const char *type_tag (const_tree);
3092 static tree member_declared_type (const_tree);
3093 #if 0
3094 static const char *decl_start_label (tree);
3095 #endif
3096 static void gen_array_type_die (tree, dw_die_ref);
3097 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3098 #if 0
3099 static void gen_entry_point_die (tree, dw_die_ref);
3100 #endif
3101 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3102 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3103 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3104 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3105 static void gen_formal_types_die (tree, dw_die_ref);
3106 static void gen_subprogram_die (tree, dw_die_ref);
3107 static void gen_variable_die (tree, tree, dw_die_ref);
3108 static void gen_const_die (tree, dw_die_ref);
3109 static void gen_label_die (tree, dw_die_ref);
3110 static void gen_lexical_block_die (tree, dw_die_ref, int);
3111 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3112 static void gen_field_die (tree, dw_die_ref);
3113 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3114 static dw_die_ref gen_compile_unit_die (const char *);
3115 static void gen_inheritance_die (tree, tree, dw_die_ref);
3116 static void gen_member_die (tree, dw_die_ref);
3117 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3118                                                 enum debug_info_usage);
3119 static void gen_subroutine_type_die (tree, dw_die_ref);
3120 static void gen_typedef_die (tree, dw_die_ref);
3121 static void gen_type_die (tree, dw_die_ref);
3122 static void gen_block_die (tree, dw_die_ref, int);
3123 static void decls_for_scope (tree, dw_die_ref, int);
3124 static inline int is_redundant_typedef (const_tree);
3125 static bool is_naming_typedef_decl (const_tree);
3126 static inline dw_die_ref get_context_die (tree);
3127 static void gen_namespace_die (tree, dw_die_ref);
3128 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3129 static dw_die_ref force_decl_die (tree);
3130 static dw_die_ref force_type_die (tree);
3131 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3132 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3133 static struct dwarf_file_data * lookup_filename (const char *);
3134 static void retry_incomplete_types (void);
3135 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3136 static void gen_generic_params_dies (tree);
3137 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3138 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3139 static void splice_child_die (dw_die_ref, dw_die_ref);
3140 static int file_info_cmp (const void *, const void *);
3141 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3142                                      const char *, const char *);
3143 static void output_loc_list (dw_loc_list_ref);
3144 static char *gen_internal_sym (const char *);
3145 static bool want_pubnames (void);
3146
3147 static void prune_unmark_dies (dw_die_ref);
3148 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3149 static void prune_unused_types_mark (dw_die_ref, int);
3150 static void prune_unused_types_walk (dw_die_ref);
3151 static void prune_unused_types_walk_attribs (dw_die_ref);
3152 static void prune_unused_types_prune (dw_die_ref);
3153 static void prune_unused_types (void);
3154 static int maybe_emit_file (struct dwarf_file_data *fd);
3155 static inline const char *AT_vms_delta1 (dw_attr_ref);
3156 static inline const char *AT_vms_delta2 (dw_attr_ref);
3157 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3158                                      const char *, const char *);
3159 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3160 static void gen_remaining_tmpl_value_param_die_attribute (void);
3161 static bool generic_type_p (tree);
3162 static void schedule_generic_params_dies_gen (tree t);
3163 static void gen_scheduled_generic_parms_dies (void);
3164
3165 /* Section names used to hold DWARF debugging information.  */
3166 #ifndef DEBUG_INFO_SECTION
3167 #define DEBUG_INFO_SECTION      ".debug_info"
3168 #endif
3169 #ifndef DEBUG_ABBREV_SECTION
3170 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3171 #endif
3172 #ifndef DEBUG_ARANGES_SECTION
3173 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3174 #endif
3175 #ifndef DEBUG_MACINFO_SECTION
3176 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3177 #endif
3178 #ifndef DEBUG_MACRO_SECTION
3179 #define DEBUG_MACRO_SECTION     ".debug_macro"
3180 #endif
3181 #ifndef DEBUG_LINE_SECTION
3182 #define DEBUG_LINE_SECTION      ".debug_line"
3183 #endif
3184 #ifndef DEBUG_LOC_SECTION
3185 #define DEBUG_LOC_SECTION       ".debug_loc"
3186 #endif
3187 #ifndef DEBUG_PUBNAMES_SECTION
3188 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3189 #endif
3190 #ifndef DEBUG_PUBTYPES_SECTION
3191 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3192 #endif
3193 #ifndef DEBUG_STR_SECTION
3194 #define DEBUG_STR_SECTION       ".debug_str"
3195 #endif
3196 #ifndef DEBUG_RANGES_SECTION
3197 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3198 #endif
3199
3200 /* Standard ELF section names for compiled code and data.  */
3201 #ifndef TEXT_SECTION_NAME
3202 #define TEXT_SECTION_NAME       ".text"
3203 #endif
3204
3205 /* Section flags for .debug_str section.  */
3206 #define DEBUG_STR_SECTION_FLAGS \
3207   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3208    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3209    : SECTION_DEBUG)
3210
3211 /* Labels we insert at beginning sections we can reference instead of
3212    the section names themselves.  */
3213
3214 #ifndef TEXT_SECTION_LABEL
3215 #define TEXT_SECTION_LABEL              "Ltext"
3216 #endif
3217 #ifndef COLD_TEXT_SECTION_LABEL
3218 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3219 #endif
3220 #ifndef DEBUG_LINE_SECTION_LABEL
3221 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3222 #endif
3223 #ifndef DEBUG_INFO_SECTION_LABEL
3224 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3225 #endif
3226 #ifndef DEBUG_ABBREV_SECTION_LABEL
3227 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3228 #endif
3229 #ifndef DEBUG_LOC_SECTION_LABEL
3230 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3231 #endif
3232 #ifndef DEBUG_RANGES_SECTION_LABEL
3233 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3234 #endif
3235 #ifndef DEBUG_MACINFO_SECTION_LABEL
3236 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3237 #endif
3238 #ifndef DEBUG_MACRO_SECTION_LABEL
3239 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3240 #endif
3241
3242
3243 /* Definitions of defaults for formats and names of various special
3244    (artificial) labels which may be generated within this file (when the -g
3245    options is used and DWARF2_DEBUGGING_INFO is in effect.
3246    If necessary, these may be overridden from within the tm.h file, but
3247    typically, overriding these defaults is unnecessary.  */
3248
3249 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3250 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3251 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3252 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3253 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3254 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3255 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3256 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3257 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3258 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3259
3260 #ifndef TEXT_END_LABEL
3261 #define TEXT_END_LABEL          "Letext"
3262 #endif
3263 #ifndef COLD_END_LABEL
3264 #define COLD_END_LABEL          "Letext_cold"
3265 #endif
3266 #ifndef BLOCK_BEGIN_LABEL
3267 #define BLOCK_BEGIN_LABEL       "LBB"
3268 #endif
3269 #ifndef BLOCK_END_LABEL
3270 #define BLOCK_END_LABEL         "LBE"
3271 #endif
3272 #ifndef LINE_CODE_LABEL
3273 #define LINE_CODE_LABEL         "LM"
3274 #endif
3275
3276 \f
3277 /* Return the root of the DIE's built for the current compilation unit.  */
3278 static dw_die_ref
3279 comp_unit_die (void)
3280 {
3281   if (!single_comp_unit_die)
3282     single_comp_unit_die = gen_compile_unit_die (NULL);
3283   return single_comp_unit_die;
3284 }
3285
3286 /* We allow a language front-end to designate a function that is to be
3287    called to "demangle" any name before it is put into a DIE.  */
3288
3289 static const char *(*demangle_name_func) (const char *);
3290
3291 void
3292 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3293 {
3294   demangle_name_func = func;
3295 }
3296
3297 /* Test if rtl node points to a pseudo register.  */
3298
3299 static inline int
3300 is_pseudo_reg (const_rtx rtl)
3301 {
3302   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3303           || (GET_CODE (rtl) == SUBREG
3304               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3305 }
3306
3307 /* Return a reference to a type, with its const and volatile qualifiers
3308    removed.  */
3309
3310 static inline tree
3311 type_main_variant (tree type)
3312 {
3313   type = TYPE_MAIN_VARIANT (type);
3314
3315   /* ??? There really should be only one main variant among any group of
3316      variants of a given type (and all of the MAIN_VARIANT values for all
3317      members of the group should point to that one type) but sometimes the C
3318      front-end messes this up for array types, so we work around that bug
3319      here.  */
3320   if (TREE_CODE (type) == ARRAY_TYPE)
3321     while (type != TYPE_MAIN_VARIANT (type))
3322       type = TYPE_MAIN_VARIANT (type);
3323
3324   return type;
3325 }
3326
3327 /* Return nonzero if the given type node represents a tagged type.  */
3328
3329 static inline int
3330 is_tagged_type (const_tree type)
3331 {
3332   enum tree_code code = TREE_CODE (type);
3333
3334   return (code == RECORD_TYPE || code == UNION_TYPE
3335           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3336 }
3337
3338 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3339
3340 static void
3341 get_ref_die_offset_label (char *label, dw_die_ref ref)
3342 {
3343   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3344 }
3345
3346 /* Return die_offset of a DIE reference to a base type.  */
3347
3348 static unsigned long int
3349 get_base_type_offset (dw_die_ref ref)
3350 {
3351   if (ref->die_offset)
3352     return ref->die_offset;
3353   if (comp_unit_die ()->die_abbrev)
3354     {
3355       calc_base_type_die_sizes ();
3356       gcc_assert (ref->die_offset);
3357     }
3358   return ref->die_offset;
3359 }
3360
3361 /* Return die_offset of a DIE reference other than base type.  */
3362
3363 static unsigned long int
3364 get_ref_die_offset (dw_die_ref ref)
3365 {
3366   gcc_assert (ref->die_offset);
3367   return ref->die_offset;
3368 }
3369
3370 /* Convert a DIE tag into its string name.  */
3371
3372 static const char *
3373 dwarf_tag_name (unsigned int tag)
3374 {
3375   const char *name = get_DW_TAG_name (tag);
3376
3377   if (name != NULL)
3378     return name;
3379
3380   return "DW_TAG_<unknown>";
3381 }
3382
3383 /* Convert a DWARF attribute code into its string name.  */
3384
3385 static const char *
3386 dwarf_attr_name (unsigned int attr)
3387 {
3388   const char *name;
3389
3390   switch (attr)
3391     {
3392 #if VMS_DEBUGGING_INFO
3393     case DW_AT_HP_prologue:
3394       return "DW_AT_HP_prologue";
3395 #else
3396     case DW_AT_MIPS_loop_unroll_factor:
3397       return "DW_AT_MIPS_loop_unroll_factor";
3398 #endif
3399
3400 #if VMS_DEBUGGING_INFO
3401     case DW_AT_HP_epilogue:
3402       return "DW_AT_HP_epilogue";
3403 #else
3404     case DW_AT_MIPS_stride:
3405       return "DW_AT_MIPS_stride";
3406 #endif
3407     }
3408
3409   name = get_DW_AT_name (attr);
3410
3411   if (name != NULL)
3412     return name;
3413
3414   return "DW_AT_<unknown>";
3415 }
3416
3417 /* Convert a DWARF value form code into its string name.  */
3418
3419 static const char *
3420 dwarf_form_name (unsigned int form)
3421 {
3422   const char *name = get_DW_FORM_name (form);
3423
3424   if (name != NULL)
3425     return name;
3426
3427   return "DW_FORM_<unknown>";
3428 }
3429 \f
3430 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3431    instance of an inlined instance of a decl which is local to an inline
3432    function, so we have to trace all of the way back through the origin chain
3433    to find out what sort of node actually served as the original seed for the
3434    given block.  */
3435
3436 static tree
3437 decl_ultimate_origin (const_tree decl)
3438 {
3439   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3440     return NULL_TREE;
3441
3442   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3443      nodes in the function to point to themselves; ignore that if
3444      we're trying to output the abstract instance of this function.  */
3445   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3446     return NULL_TREE;
3447
3448   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3449      most distant ancestor, this should never happen.  */
3450   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3451
3452   return DECL_ABSTRACT_ORIGIN (decl);
3453 }
3454
3455 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3456    of a virtual function may refer to a base class, so we check the 'this'
3457    parameter.  */
3458
3459 static tree
3460 decl_class_context (tree decl)
3461 {
3462   tree context = NULL_TREE;
3463
3464   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3465     context = DECL_CONTEXT (decl);
3466   else
3467     context = TYPE_MAIN_VARIANT
3468       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3469
3470   if (context && !TYPE_P (context))
3471     context = NULL_TREE;
3472
3473   return context;
3474 }
3475 \f
3476 /* Add an attribute/value pair to a DIE.  */
3477
3478 static inline void
3479 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3480 {
3481   /* Maybe this should be an assert?  */
3482   if (die == NULL)
3483     return;
3484
3485   if (die->die_attr == NULL)
3486     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
3487   VEC_safe_push (dw_attr_node, gc, die->die_attr, *attr);
3488 }
3489
3490 static inline enum dw_val_class
3491 AT_class (dw_attr_ref a)
3492 {
3493   return a->dw_attr_val.val_class;
3494 }
3495
3496 /* Add a flag value attribute to a DIE.  */
3497
3498 static inline void
3499 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3500 {
3501   dw_attr_node attr;
3502
3503   attr.dw_attr = attr_kind;
3504   attr.dw_attr_val.val_class = dw_val_class_flag;
3505   attr.dw_attr_val.v.val_flag = flag;
3506   add_dwarf_attr (die, &attr);
3507 }
3508
3509 static inline unsigned
3510 AT_flag (dw_attr_ref a)
3511 {
3512   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3513   return a->dw_attr_val.v.val_flag;
3514 }
3515
3516 /* Add a signed integer attribute value to a DIE.  */
3517
3518 static inline void
3519 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3520 {
3521   dw_attr_node attr;
3522
3523   attr.dw_attr = attr_kind;
3524   attr.dw_attr_val.val_class = dw_val_class_const;
3525   attr.dw_attr_val.v.val_int = int_val;
3526   add_dwarf_attr (die, &attr);
3527 }
3528
3529 static inline HOST_WIDE_INT
3530 AT_int (dw_attr_ref a)
3531 {
3532   gcc_assert (a && AT_class (a) == dw_val_class_const);
3533   return a->dw_attr_val.v.val_int;
3534 }
3535
3536 /* Add an unsigned integer attribute value to a DIE.  */
3537
3538 static inline void
3539 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3540                  unsigned HOST_WIDE_INT unsigned_val)
3541 {
3542   dw_attr_node attr;
3543
3544   attr.dw_attr = attr_kind;
3545   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3546   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3547   add_dwarf_attr (die, &attr);
3548 }
3549
3550 static inline unsigned HOST_WIDE_INT
3551 AT_unsigned (dw_attr_ref a)
3552 {
3553   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3554   return a->dw_attr_val.v.val_unsigned;
3555 }
3556
3557 /* Add an unsigned double integer attribute value to a DIE.  */
3558
3559 static inline void
3560 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3561                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3562 {
3563   dw_attr_node attr;
3564
3565   attr.dw_attr = attr_kind;
3566   attr.dw_attr_val.val_class = dw_val_class_const_double;
3567   attr.dw_attr_val.v.val_double.high = high;
3568   attr.dw_attr_val.v.val_double.low = low;
3569   add_dwarf_attr (die, &attr);
3570 }
3571
3572 /* Add a floating point attribute value to a DIE and return it.  */
3573
3574 static inline void
3575 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3576             unsigned int length, unsigned int elt_size, unsigned char *array)
3577 {
3578   dw_attr_node attr;
3579
3580   attr.dw_attr = attr_kind;
3581   attr.dw_attr_val.val_class = dw_val_class_vec;
3582   attr.dw_attr_val.v.val_vec.length = length;
3583   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3584   attr.dw_attr_val.v.val_vec.array = array;
3585   add_dwarf_attr (die, &attr);
3586 }
3587
3588 /* Add an 8-byte data attribute value to a DIE.  */
3589
3590 static inline void
3591 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3592               unsigned char data8[8])
3593 {
3594   dw_attr_node attr;
3595
3596   attr.dw_attr = attr_kind;
3597   attr.dw_attr_val.val_class = dw_val_class_data8;
3598   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3599   add_dwarf_attr (die, &attr);
3600 }
3601
3602 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  */
3603 static inline void
3604 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high)
3605 {
3606   dw_attr_node attr;
3607
3608   attr.dw_attr = DW_AT_low_pc;
3609   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3610   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_low);
3611   add_dwarf_attr (die, &attr);
3612
3613   attr.dw_attr = DW_AT_high_pc;
3614   if (dwarf_version < 4)
3615     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3616   else
3617     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3618   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_high);
3619   add_dwarf_attr (die, &attr);
3620 }
3621
3622 /* Hash and equality functions for debug_str_hash.  */
3623
3624 static hashval_t
3625 debug_str_do_hash (const void *x)
3626 {
3627   return htab_hash_string (((const struct indirect_string_node *)x)->str);
3628 }
3629
3630 static int
3631 debug_str_eq (const void *x1, const void *x2)
3632 {
3633   return strcmp ((((const struct indirect_string_node *)x1)->str),
3634                  (const char *)x2) == 0;
3635 }
3636
3637 /* Add STR to the indirect string hash table.  */
3638
3639 static struct indirect_string_node *
3640 find_AT_string (const char *str)
3641 {
3642   struct indirect_string_node *node;
3643   void **slot;
3644
3645   if (! debug_str_hash)
3646     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3647                                       debug_str_eq, NULL);
3648
3649   slot = htab_find_slot_with_hash (debug_str_hash, str,
3650                                    htab_hash_string (str), INSERT);
3651   if (*slot == NULL)
3652     {
3653       node = ggc_alloc_cleared_indirect_string_node ();
3654       node->str = ggc_strdup (str);
3655       *slot = node;
3656     }
3657   else
3658     node = (struct indirect_string_node *) *slot;
3659
3660   node->refcount++;
3661   return node;
3662 }
3663
3664 /* Add a string attribute value to a DIE.  */
3665
3666 static inline void
3667 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3668 {
3669   dw_attr_node attr;
3670   struct indirect_string_node *node;
3671
3672   node = find_AT_string (str);
3673
3674   attr.dw_attr = attr_kind;
3675   attr.dw_attr_val.val_class = dw_val_class_str;
3676   attr.dw_attr_val.v.val_str = node;
3677   add_dwarf_attr (die, &attr);
3678 }
3679
3680 static inline const char *
3681 AT_string (dw_attr_ref a)
3682 {
3683   gcc_assert (a && AT_class (a) == dw_val_class_str);
3684   return a->dw_attr_val.v.val_str->str;
3685 }
3686
3687 /* Find out whether a string should be output inline in DIE
3688    or out-of-line in .debug_str section.  */
3689
3690 static enum dwarf_form
3691 AT_string_form (dw_attr_ref a)
3692 {
3693   struct indirect_string_node *node;
3694   unsigned int len;
3695   char label[32];
3696
3697   gcc_assert (a && AT_class (a) == dw_val_class_str);
3698
3699   node = a->dw_attr_val.v.val_str;
3700   if (node->form)
3701     return node->form;
3702
3703   len = strlen (node->str) + 1;
3704
3705   /* If the string is shorter or equal to the size of the reference, it is
3706      always better to put it inline.  */
3707   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3708     return node->form = DW_FORM_string;
3709
3710   /* If we cannot expect the linker to merge strings in .debug_str
3711      section, only put it into .debug_str if it is worth even in this
3712      single module.  */
3713   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3714       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3715       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3716     return node->form = DW_FORM_string;
3717
3718   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3719   ++dw2_string_counter;
3720   node->label = xstrdup (label);
3721
3722   return node->form = DW_FORM_strp;
3723 }
3724
3725 /* Add a DIE reference attribute value to a DIE.  */
3726
3727 static inline void
3728 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3729 {
3730   dw_attr_node attr;
3731
3732 #ifdef ENABLE_CHECKING
3733   gcc_assert (targ_die != NULL);
3734 #else
3735   /* With LTO we can end up trying to reference something we didn't create
3736      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
3737   if (targ_die == NULL)
3738     return;
3739 #endif
3740
3741   attr.dw_attr = attr_kind;
3742   attr.dw_attr_val.val_class = dw_val_class_die_ref;
3743   attr.dw_attr_val.v.val_die_ref.die = targ_die;
3744   attr.dw_attr_val.v.val_die_ref.external = 0;
3745   add_dwarf_attr (die, &attr);
3746 }
3747
3748 /* Change DIE reference REF to point to NEW_DIE instead.  */
3749
3750 static inline void
3751 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3752 {
3753   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3754   ref->dw_attr_val.v.val_die_ref.die = new_die;
3755   ref->dw_attr_val.v.val_die_ref.external = 0;
3756 }
3757
3758 /* Add an AT_specification attribute to a DIE, and also make the back
3759    pointer from the specification to the definition.  */
3760
3761 static inline void
3762 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3763 {
3764   add_AT_die_ref (die, DW_AT_specification, targ_die);
3765   gcc_assert (!targ_die->die_definition);
3766   targ_die->die_definition = die;
3767 }
3768
3769 static inline dw_die_ref
3770 AT_ref (dw_attr_ref a)
3771 {
3772   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3773   return a->dw_attr_val.v.val_die_ref.die;
3774 }
3775
3776 static inline int
3777 AT_ref_external (dw_attr_ref a)
3778 {
3779   if (a && AT_class (a) == dw_val_class_die_ref)
3780     return a->dw_attr_val.v.val_die_ref.external;
3781
3782   return 0;
3783 }
3784
3785 static inline void
3786 set_AT_ref_external (dw_attr_ref a, int i)
3787 {
3788   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3789   a->dw_attr_val.v.val_die_ref.external = i;
3790 }
3791
3792 /* Add an FDE reference attribute value to a DIE.  */
3793
3794 static inline void
3795 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3796 {
3797   dw_attr_node attr;
3798
3799   attr.dw_attr = attr_kind;
3800   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
3801   attr.dw_attr_val.v.val_fde_index = targ_fde;
3802   add_dwarf_attr (die, &attr);
3803 }
3804
3805 /* Add a location description attribute value to a DIE.  */
3806
3807 static inline void
3808 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3809 {
3810   dw_attr_node attr;
3811
3812   attr.dw_attr = attr_kind;
3813   attr.dw_attr_val.val_class = dw_val_class_loc;
3814   attr.dw_attr_val.v.val_loc = loc;
3815   add_dwarf_attr (die, &attr);
3816 }
3817
3818 static inline dw_loc_descr_ref
3819 AT_loc (dw_attr_ref a)
3820 {
3821   gcc_assert (a && AT_class (a) == dw_val_class_loc);
3822   return a->dw_attr_val.v.val_loc;
3823 }
3824
3825 static inline void
3826 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
3827 {
3828   dw_attr_node attr;
3829
3830   attr.dw_attr = attr_kind;
3831   attr.dw_attr_val.val_class = dw_val_class_loc_list;
3832   attr.dw_attr_val.v.val_loc_list = loc_list;
3833   add_dwarf_attr (die, &attr);
3834   have_location_lists = true;
3835 }
3836
3837 static inline dw_loc_list_ref
3838 AT_loc_list (dw_attr_ref a)
3839 {
3840   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3841   return a->dw_attr_val.v.val_loc_list;
3842 }
3843
3844 static inline dw_loc_list_ref *
3845 AT_loc_list_ptr (dw_attr_ref a)
3846 {
3847   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3848   return &a->dw_attr_val.v.val_loc_list;
3849 }
3850
3851 /* Add an address constant attribute value to a DIE.  */
3852
3853 static inline void
3854 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
3855 {
3856   dw_attr_node attr;
3857
3858   attr.dw_attr = attr_kind;
3859   attr.dw_attr_val.val_class = dw_val_class_addr;
3860   attr.dw_attr_val.v.val_addr = addr;
3861   add_dwarf_attr (die, &attr);
3862 }
3863
3864 /* Get the RTX from to an address DIE attribute.  */
3865
3866 static inline rtx
3867 AT_addr (dw_attr_ref a)
3868 {
3869   gcc_assert (a && AT_class (a) == dw_val_class_addr);
3870   return a->dw_attr_val.v.val_addr;
3871 }
3872
3873 /* Add a file attribute value to a DIE.  */
3874
3875 static inline void
3876 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
3877              struct dwarf_file_data *fd)
3878 {
3879   dw_attr_node attr;
3880
3881   attr.dw_attr = attr_kind;
3882   attr.dw_attr_val.val_class = dw_val_class_file;
3883   attr.dw_attr_val.v.val_file = fd;
3884   add_dwarf_attr (die, &attr);
3885 }
3886
3887 /* Get the dwarf_file_data from a file DIE attribute.  */
3888
3889 static inline struct dwarf_file_data *
3890 AT_file (dw_attr_ref a)
3891 {
3892   gcc_assert (a && AT_class (a) == dw_val_class_file);
3893   return a->dw_attr_val.v.val_file;
3894 }
3895
3896 /* Add a vms delta attribute value to a DIE.  */
3897
3898 static inline void
3899 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
3900                   const char *lbl1, const char *lbl2)
3901 {
3902   dw_attr_node attr;
3903
3904   attr.dw_attr = attr_kind;
3905   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
3906   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
3907   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
3908   add_dwarf_attr (die, &attr);
3909 }
3910
3911 /* Add a label identifier attribute value to a DIE.  */
3912
3913 static inline void
3914 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
3915 {
3916   dw_attr_node attr;
3917
3918   attr.dw_attr = attr_kind;
3919   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3920   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3921   add_dwarf_attr (die, &attr);
3922 }
3923
3924 /* Add a section offset attribute value to a DIE, an offset into the
3925    debug_line section.  */
3926
3927 static inline void
3928 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3929                 const char *label)
3930 {
3931   dw_attr_node attr;
3932
3933   attr.dw_attr = attr_kind;
3934   attr.dw_attr_val.val_class = dw_val_class_lineptr;
3935   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3936   add_dwarf_attr (die, &attr);
3937 }
3938
3939 /* Add a section offset attribute value to a DIE, an offset into the
3940    debug_macinfo section.  */
3941
3942 static inline void
3943 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3944                const char *label)
3945 {
3946   dw_attr_node attr;
3947
3948   attr.dw_attr = attr_kind;
3949   attr.dw_attr_val.val_class = dw_val_class_macptr;
3950   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3951   add_dwarf_attr (die, &attr);
3952 }
3953
3954 /* Add an offset attribute value to a DIE.  */
3955
3956 static inline void
3957 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
3958                unsigned HOST_WIDE_INT offset)
3959 {
3960   dw_attr_node attr;
3961
3962   attr.dw_attr = attr_kind;
3963   attr.dw_attr_val.val_class = dw_val_class_offset;
3964   attr.dw_attr_val.v.val_offset = offset;
3965   add_dwarf_attr (die, &attr);
3966 }
3967
3968 /* Add an range_list attribute value to a DIE.  */
3969
3970 static void
3971 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
3972                    long unsigned int offset)
3973 {
3974   dw_attr_node attr;
3975
3976   attr.dw_attr = attr_kind;
3977   attr.dw_attr_val.val_class = dw_val_class_range_list;
3978   attr.dw_attr_val.v.val_offset = offset;
3979   add_dwarf_attr (die, &attr);
3980 }
3981
3982 /* Return the start label of a delta attribute.  */
3983
3984 static inline const char *
3985 AT_vms_delta1 (dw_attr_ref a)
3986 {
3987   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3988   return a->dw_attr_val.v.val_vms_delta.lbl1;
3989 }
3990
3991 /* Return the end label of a delta attribute.  */
3992
3993 static inline const char *
3994 AT_vms_delta2 (dw_attr_ref a)
3995 {
3996   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3997   return a->dw_attr_val.v.val_vms_delta.lbl2;
3998 }
3999
4000 static inline const char *
4001 AT_lbl (dw_attr_ref a)
4002 {
4003   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4004                     || AT_class (a) == dw_val_class_lineptr
4005                     || AT_class (a) == dw_val_class_macptr
4006                     || AT_class (a) == dw_val_class_high_pc));
4007   return a->dw_attr_val.v.val_lbl_id;
4008 }
4009
4010 /* Get the attribute of type attr_kind.  */
4011
4012 static dw_attr_ref
4013 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4014 {
4015   dw_attr_ref a;
4016   unsigned ix;
4017   dw_die_ref spec = NULL;
4018
4019   if (! die)
4020     return NULL;
4021
4022   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4023     if (a->dw_attr == attr_kind)
4024       return a;
4025     else if (a->dw_attr == DW_AT_specification
4026              || a->dw_attr == DW_AT_abstract_origin)
4027       spec = AT_ref (a);
4028
4029   if (spec)
4030     return get_AT (spec, attr_kind);
4031
4032   return NULL;
4033 }
4034
4035 /* Returns the parent of the declaration of DIE.  */
4036
4037 static dw_die_ref
4038 get_die_parent (dw_die_ref die)
4039 {
4040   dw_die_ref t;
4041
4042   if (!die)
4043     return NULL;
4044
4045   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4046       || (t = get_AT_ref (die, DW_AT_specification)))
4047     die = t;
4048
4049   return die->die_parent;
4050 }
4051
4052 /* Return the "low pc" attribute value, typically associated with a subprogram
4053    DIE.  Return null if the "low pc" attribute is either not present, or if it
4054    cannot be represented as an assembler label identifier.  */
4055
4056 static inline const char *
4057 get_AT_low_pc (dw_die_ref die)
4058 {
4059   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4060
4061   return a ? AT_lbl (a) : NULL;
4062 }
4063
4064 /* Return the "high pc" attribute value, typically associated with a subprogram
4065    DIE.  Return null if the "high pc" attribute is either not present, or if it
4066    cannot be represented as an assembler label identifier.  */
4067
4068 static inline const char *
4069 get_AT_hi_pc (dw_die_ref die)
4070 {
4071   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4072
4073   return a ? AT_lbl (a) : NULL;
4074 }
4075
4076 /* Return the value of the string attribute designated by ATTR_KIND, or
4077    NULL if it is not present.  */
4078
4079 static inline const char *
4080 get_AT_string (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_string (a) : NULL;
4085 }
4086
4087 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4088    if it is not present.  */
4089
4090 static inline int
4091 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4092 {
4093   dw_attr_ref a = get_AT (die, attr_kind);
4094
4095   return a ? AT_flag (a) : 0;
4096 }
4097
4098 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4099    if it is not present.  */
4100
4101 static inline unsigned
4102 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4103 {
4104   dw_attr_ref a = get_AT (die, attr_kind);
4105
4106   return a ? AT_unsigned (a) : 0;
4107 }
4108
4109 static inline dw_die_ref
4110 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4111 {
4112   dw_attr_ref a = get_AT (die, attr_kind);
4113
4114   return a ? AT_ref (a) : NULL;
4115 }
4116
4117 static inline struct dwarf_file_data *
4118 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4119 {
4120   dw_attr_ref a = get_AT (die, attr_kind);
4121
4122   return a ? AT_file (a) : NULL;
4123 }
4124
4125 /* Return TRUE if the language is C++.  */
4126
4127 static inline bool
4128 is_cxx (void)
4129 {
4130   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4131
4132   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4133 }
4134
4135 /* Return TRUE if the language is Fortran.  */
4136
4137 static inline bool
4138 is_fortran (void)
4139 {
4140   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4141
4142   return (lang == DW_LANG_Fortran77
4143           || lang == DW_LANG_Fortran90
4144           || lang == DW_LANG_Fortran95);
4145 }
4146
4147 /* Return TRUE if the language is Ada.  */
4148
4149 static inline bool
4150 is_ada (void)
4151 {
4152   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4153
4154   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4155 }
4156
4157 /* Remove the specified attribute if present.  */
4158
4159 static void
4160 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4161 {
4162   dw_attr_ref a;
4163   unsigned ix;
4164
4165   if (! die)
4166     return;
4167
4168   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4169     if (a->dw_attr == attr_kind)
4170       {
4171         if (AT_class (a) == dw_val_class_str)
4172           if (a->dw_attr_val.v.val_str->refcount)
4173             a->dw_attr_val.v.val_str->refcount--;
4174
4175         /* VEC_ordered_remove should help reduce the number of abbrevs
4176            that are needed.  */
4177         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4178         return;
4179       }
4180 }
4181
4182 /* Remove CHILD from its parent.  PREV must have the property that
4183    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4184
4185 static void
4186 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4187 {
4188   gcc_assert (child->die_parent == prev->die_parent);
4189   gcc_assert (prev->die_sib == child);
4190   if (prev == child)
4191     {
4192       gcc_assert (child->die_parent->die_child == child);
4193       prev = NULL;
4194     }
4195   else
4196     prev->die_sib = child->die_sib;
4197   if (child->die_parent->die_child == child)
4198     child->die_parent->die_child = prev;
4199 }
4200
4201 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4202    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4203
4204 static void
4205 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4206 {
4207   dw_die_ref parent = old_child->die_parent;
4208
4209   gcc_assert (parent == prev->die_parent);
4210   gcc_assert (prev->die_sib == old_child);
4211
4212   new_child->die_parent = parent;
4213   if (prev == old_child)
4214     {
4215       gcc_assert (parent->die_child == old_child);
4216       new_child->die_sib = new_child;
4217     }
4218   else
4219     {
4220       prev->die_sib = new_child;
4221       new_child->die_sib = old_child->die_sib;
4222     }
4223   if (old_child->die_parent->die_child == old_child)
4224     old_child->die_parent->die_child = new_child;
4225 }
4226
4227 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4228
4229 static void
4230 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4231 {
4232   dw_die_ref c;
4233   new_parent->die_child = old_parent->die_child;
4234   old_parent->die_child = NULL;
4235   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4236 }
4237
4238 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4239    matches TAG.  */
4240
4241 static void
4242 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4243 {
4244   dw_die_ref c;
4245
4246   c = die->die_child;
4247   if (c) do {
4248     dw_die_ref prev = c;
4249     c = c->die_sib;
4250     while (c->die_tag == tag)
4251       {
4252         remove_child_with_prev (c, prev);
4253         /* Might have removed every child.  */
4254         if (c == c->die_sib)
4255           return;
4256         c = c->die_sib;
4257       }
4258   } while (c != die->die_child);
4259 }
4260
4261 /* Add a CHILD_DIE as the last child of DIE.  */
4262
4263 static void
4264 add_child_die (dw_die_ref die, dw_die_ref child_die)
4265 {
4266   /* FIXME this should probably be an assert.  */
4267   if (! die || ! child_die)
4268     return;
4269   gcc_assert (die != child_die);
4270
4271   child_die->die_parent = die;
4272   if (die->die_child)
4273     {
4274       child_die->die_sib = die->die_child->die_sib;
4275       die->die_child->die_sib = child_die;
4276     }
4277   else
4278     child_die->die_sib = child_die;
4279   die->die_child = child_die;
4280 }
4281
4282 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4283    is the specification, to the end of PARENT's list of children.
4284    This is done by removing and re-adding it.  */
4285
4286 static void
4287 splice_child_die (dw_die_ref parent, dw_die_ref child)
4288 {
4289   dw_die_ref p;
4290
4291   /* We want the declaration DIE from inside the class, not the
4292      specification DIE at toplevel.  */
4293   if (child->die_parent != parent)
4294     {
4295       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4296
4297       if (tmp)
4298         child = tmp;
4299     }
4300
4301   gcc_assert (child->die_parent == parent
4302               || (child->die_parent
4303                   == get_AT_ref (parent, DW_AT_specification)));
4304
4305   for (p = child->die_parent->die_child; ; p = p->die_sib)
4306     if (p->die_sib == child)
4307       {
4308         remove_child_with_prev (child, p);
4309         break;
4310       }
4311
4312   add_child_die (parent, child);
4313 }
4314
4315 /* Return a pointer to a newly created DIE node.  */
4316
4317 static inline dw_die_ref
4318 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4319 {
4320   dw_die_ref die = ggc_alloc_cleared_die_node ();
4321
4322   die->die_tag = tag_value;
4323
4324   if (parent_die != NULL)
4325     add_child_die (parent_die, die);
4326   else
4327     {
4328       limbo_die_node *limbo_node;
4329
4330       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4331       limbo_node->die = die;
4332       limbo_node->created_for = t;
4333       limbo_node->next = limbo_die_list;
4334       limbo_die_list = limbo_node;
4335     }
4336
4337   return die;
4338 }
4339
4340 /* Return the DIE associated with the given type specifier.  */
4341
4342 static inline dw_die_ref
4343 lookup_type_die (tree type)
4344 {
4345   return TYPE_SYMTAB_DIE (type);
4346 }
4347
4348 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4349    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4350    anonymous type instead the one of the naming typedef.  */
4351
4352 static inline dw_die_ref
4353 strip_naming_typedef (tree type, dw_die_ref type_die)
4354 {
4355   if (type
4356       && TREE_CODE (type) == RECORD_TYPE
4357       && type_die
4358       && type_die->die_tag == DW_TAG_typedef
4359       && is_naming_typedef_decl (TYPE_NAME (type)))
4360     type_die = get_AT_ref (type_die, DW_AT_type);
4361   return type_die;
4362 }
4363
4364 /* Like lookup_type_die, but if type is an anonymous type named by a
4365    typedef[1], return the DIE of the anonymous type instead the one of
4366    the naming typedef.  This is because in gen_typedef_die, we did
4367    equate the anonymous struct named by the typedef with the DIE of
4368    the naming typedef. So by default, lookup_type_die on an anonymous
4369    struct yields the DIE of the naming typedef.
4370
4371    [1]: Read the comment of is_naming_typedef_decl to learn about what
4372    a naming typedef is.  */
4373
4374 static inline dw_die_ref
4375 lookup_type_die_strip_naming_typedef (tree type)
4376 {
4377   dw_die_ref die = lookup_type_die (type);
4378   return strip_naming_typedef (type, die);
4379 }
4380
4381 /* Equate a DIE to a given type specifier.  */
4382
4383 static inline void
4384 equate_type_number_to_die (tree type, dw_die_ref type_die)
4385 {
4386   TYPE_SYMTAB_DIE (type) = type_die;
4387 }
4388
4389 /* Returns a hash value for X (which really is a die_struct).  */
4390
4391 static hashval_t
4392 decl_die_table_hash (const void *x)
4393 {
4394   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4395 }
4396
4397 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4398
4399 static int
4400 decl_die_table_eq (const void *x, const void *y)
4401 {
4402   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4403 }
4404
4405 /* Return the DIE associated with a given declaration.  */
4406
4407 static inline dw_die_ref
4408 lookup_decl_die (tree decl)
4409 {
4410   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4411 }
4412
4413 /* Returns a hash value for X (which really is a var_loc_list).  */
4414
4415 static hashval_t
4416 decl_loc_table_hash (const void *x)
4417 {
4418   return (hashval_t) ((const var_loc_list *) x)->decl_id;
4419 }
4420
4421 /* Return nonzero if decl_id of var_loc_list X is the same as
4422    UID of decl *Y.  */
4423
4424 static int
4425 decl_loc_table_eq (const void *x, const void *y)
4426 {
4427   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4428 }
4429
4430 /* Return the var_loc list associated with a given declaration.  */
4431
4432 static inline var_loc_list *
4433 lookup_decl_loc (const_tree decl)
4434 {
4435   if (!decl_loc_table)
4436     return NULL;
4437   return (var_loc_list *)
4438     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4439 }
4440
4441 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
4442
4443 static hashval_t
4444 cached_dw_loc_list_table_hash (const void *x)
4445 {
4446   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4447 }
4448
4449 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4450    UID of decl *Y.  */
4451
4452 static int
4453 cached_dw_loc_list_table_eq (const void *x, const void *y)
4454 {
4455   return (((const cached_dw_loc_list *) x)->decl_id
4456           == DECL_UID ((const_tree) y));
4457 }
4458
4459 /* Equate a DIE to a particular declaration.  */
4460
4461 static void
4462 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4463 {
4464   unsigned int decl_id = DECL_UID (decl);
4465   void **slot;
4466
4467   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4468   *slot = decl_die;
4469   decl_die->decl_id = decl_id;
4470 }
4471
4472 /* Return how many bits covers PIECE EXPR_LIST.  */
4473
4474 static int
4475 decl_piece_bitsize (rtx piece)
4476 {
4477   int ret = (int) GET_MODE (piece);
4478   if (ret)
4479     return ret;
4480   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4481               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4482   return INTVAL (XEXP (XEXP (piece, 0), 0));
4483 }
4484
4485 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
4486
4487 static rtx *
4488 decl_piece_varloc_ptr (rtx piece)
4489 {
4490   if ((int) GET_MODE (piece))
4491     return &XEXP (piece, 0);
4492   else
4493     return &XEXP (XEXP (piece, 0), 1);
4494 }
4495
4496 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4497    Next is the chain of following piece nodes.  */
4498
4499 static rtx
4500 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4501 {
4502   if (bitsize <= (int) MAX_MACHINE_MODE)
4503     return alloc_EXPR_LIST (bitsize, loc_note, next);
4504   else
4505     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4506                                                GEN_INT (bitsize),
4507                                                loc_note), next);
4508 }
4509
4510 /* Return rtx that should be stored into loc field for
4511    LOC_NOTE and BITPOS/BITSIZE.  */
4512
4513 static rtx
4514 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4515                       HOST_WIDE_INT bitsize)
4516 {
4517   if (bitsize != -1)
4518     {
4519       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4520       if (bitpos != 0)
4521         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4522     }
4523   return loc_note;
4524 }
4525
4526 /* This function either modifies location piece list *DEST in
4527    place (if SRC and INNER is NULL), or copies location piece list
4528    *SRC to *DEST while modifying it.  Location BITPOS is modified
4529    to contain LOC_NOTE, any pieces overlapping it are removed resp.
4530    not copied and if needed some padding around it is added.
4531    When modifying in place, DEST should point to EXPR_LIST where
4532    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4533    to the start of the whole list and INNER points to the EXPR_LIST
4534    where earlier pieces cover PIECE_BITPOS bits.  */
4535
4536 static void
4537 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4538                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4539                    HOST_WIDE_INT bitsize, rtx loc_note)
4540 {
4541   int diff;
4542   bool copy = inner != NULL;
4543
4544   if (copy)
4545     {
4546       /* First copy all nodes preceding the current bitpos.  */
4547       while (src != inner)
4548         {
4549           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4550                                    decl_piece_bitsize (*src), NULL_RTX);
4551           dest = &XEXP (*dest, 1);
4552           src = &XEXP (*src, 1);
4553         }
4554     }
4555   /* Add padding if needed.  */
4556   if (bitpos != piece_bitpos)
4557     {
4558       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4559                                copy ? NULL_RTX : *dest);
4560       dest = &XEXP (*dest, 1);
4561     }
4562   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4563     {
4564       gcc_assert (!copy);
4565       /* A piece with correct bitpos and bitsize already exist,
4566          just update the location for it and return.  */
4567       *decl_piece_varloc_ptr (*dest) = loc_note;
4568       return;
4569     }
4570   /* Add the piece that changed.  */
4571   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4572   dest = &XEXP (*dest, 1);
4573   /* Skip over pieces that overlap it.  */
4574   diff = bitpos - piece_bitpos + bitsize;
4575   if (!copy)
4576     src = dest;
4577   while (diff > 0 && *src)
4578     {
4579       rtx piece = *src;
4580       diff -= decl_piece_bitsize (piece);
4581       if (copy)
4582         src = &XEXP (piece, 1);
4583       else
4584         {
4585           *src = XEXP (piece, 1);
4586           free_EXPR_LIST_node (piece);
4587         }
4588     }
4589   /* Add padding if needed.  */
4590   if (diff < 0 && *src)
4591     {
4592       if (!copy)
4593         dest = src;
4594       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4595       dest = &XEXP (*dest, 1);
4596     }
4597   if (!copy)
4598     return;
4599   /* Finally copy all nodes following it.  */
4600   while (*src)
4601     {
4602       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4603                                decl_piece_bitsize (*src), NULL_RTX);
4604       dest = &XEXP (*dest, 1);
4605       src = &XEXP (*src, 1);
4606     }
4607 }
4608
4609 /* Add a variable location node to the linked list for DECL.  */
4610
4611 static struct var_loc_node *
4612 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
4613 {
4614   unsigned int decl_id;
4615   var_loc_list *temp;
4616   void **slot;
4617   struct var_loc_node *loc = NULL;
4618   HOST_WIDE_INT bitsize = -1, bitpos = -1;
4619
4620   if (DECL_DEBUG_EXPR_IS_FROM (decl))
4621     {
4622       tree realdecl = DECL_DEBUG_EXPR (decl);
4623       if (realdecl && handled_component_p (realdecl))
4624         {
4625           HOST_WIDE_INT maxsize;
4626           tree innerdecl;
4627           innerdecl
4628             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
4629           if (!DECL_P (innerdecl)
4630               || DECL_IGNORED_P (innerdecl)
4631               || TREE_STATIC (innerdecl)
4632               || bitsize <= 0
4633               || bitpos + bitsize > 256
4634               || bitsize != maxsize)
4635             return NULL;
4636           decl = innerdecl;
4637         }
4638     }
4639
4640   decl_id = DECL_UID (decl);
4641   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
4642   if (*slot == NULL)
4643     {
4644       temp = ggc_alloc_cleared_var_loc_list ();
4645       temp->decl_id = decl_id;
4646       *slot = temp;
4647     }
4648   else
4649     temp = (var_loc_list *) *slot;
4650
4651   /* For PARM_DECLs try to keep around the original incoming value,
4652      even if that means we'll emit a zero-range .debug_loc entry.  */
4653   if (temp->last
4654       && temp->first == temp->last
4655       && TREE_CODE (decl) == PARM_DECL
4656       && GET_CODE (temp->first->loc) == NOTE
4657       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
4658       && DECL_INCOMING_RTL (decl)
4659       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
4660       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
4661          == GET_CODE (DECL_INCOMING_RTL (decl))
4662       && prev_real_insn (temp->first->loc) == NULL_RTX
4663       && (bitsize != -1
4664           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
4665                            NOTE_VAR_LOCATION_LOC (loc_note))
4666           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
4667               != NOTE_VAR_LOCATION_STATUS (loc_note))))
4668     {
4669       loc = ggc_alloc_cleared_var_loc_node ();
4670       temp->first->next = loc;
4671       temp->last = loc;
4672       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4673     }
4674   else if (temp->last)
4675     {
4676       struct var_loc_node *last = temp->last, *unused = NULL;
4677       rtx *piece_loc = NULL, last_loc_note;
4678       int piece_bitpos = 0;
4679       if (last->next)
4680         {
4681           last = last->next;
4682           gcc_assert (last->next == NULL);
4683         }
4684       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
4685         {
4686           piece_loc = &last->loc;
4687           do
4688             {
4689               int cur_bitsize = decl_piece_bitsize (*piece_loc);
4690               if (piece_bitpos + cur_bitsize > bitpos)
4691                 break;
4692               piece_bitpos += cur_bitsize;
4693               piece_loc = &XEXP (*piece_loc, 1);
4694             }
4695           while (*piece_loc);
4696         }
4697       /* TEMP->LAST here is either pointer to the last but one or
4698          last element in the chained list, LAST is pointer to the
4699          last element.  */
4700       if (label && strcmp (last->label, label) == 0)
4701         {
4702           /* For SRA optimized variables if there weren't any real
4703              insns since last note, just modify the last node.  */
4704           if (piece_loc != NULL)
4705             {
4706               adjust_piece_list (piece_loc, NULL, NULL,
4707                                  bitpos, piece_bitpos, bitsize, loc_note);
4708               return NULL;
4709             }
4710           /* If the last note doesn't cover any instructions, remove it.  */
4711           if (temp->last != last)
4712             {
4713               temp->last->next = NULL;
4714               unused = last;
4715               last = temp->last;
4716               gcc_assert (strcmp (last->label, label) != 0);
4717             }
4718           else
4719             {
4720               gcc_assert (temp->first == temp->last
4721                           || (temp->first->next == temp->last
4722                               && TREE_CODE (decl) == PARM_DECL));
4723               memset (temp->last, '\0', sizeof (*temp->last));
4724               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
4725               return temp->last;
4726             }
4727         }
4728       if (bitsize == -1 && NOTE_P (last->loc))
4729         last_loc_note = last->loc;
4730       else if (piece_loc != NULL
4731                && *piece_loc != NULL_RTX
4732                && piece_bitpos == bitpos
4733                && decl_piece_bitsize (*piece_loc) == bitsize)
4734         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
4735       else
4736         last_loc_note = NULL_RTX;
4737       /* If the current location is the same as the end of the list,
4738          and either both or neither of the locations is uninitialized,
4739          we have nothing to do.  */
4740       if (last_loc_note == NULL_RTX
4741           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
4742                             NOTE_VAR_LOCATION_LOC (loc_note)))
4743           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4744                != NOTE_VAR_LOCATION_STATUS (loc_note))
4745               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4746                    == VAR_INIT_STATUS_UNINITIALIZED)
4747                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
4748                       == VAR_INIT_STATUS_UNINITIALIZED))))
4749         {
4750           /* Add LOC to the end of list and update LAST.  If the last
4751              element of the list has been removed above, reuse its
4752              memory for the new node, otherwise allocate a new one.  */
4753           if (unused)
4754             {
4755               loc = unused;
4756               memset (loc, '\0', sizeof (*loc));
4757             }
4758           else
4759             loc = ggc_alloc_cleared_var_loc_node ();
4760           if (bitsize == -1 || piece_loc == NULL)
4761             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4762           else
4763             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
4764                                bitpos, piece_bitpos, bitsize, loc_note);
4765           last->next = loc;
4766           /* Ensure TEMP->LAST will point either to the new last but one
4767              element of the chain, or to the last element in it.  */
4768           if (last != temp->last)
4769             temp->last = last;
4770         }
4771       else if (unused)
4772         ggc_free (unused);
4773     }
4774   else
4775     {
4776       loc = ggc_alloc_cleared_var_loc_node ();
4777       temp->first = loc;
4778       temp->last = loc;
4779       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4780     }
4781   return loc;
4782 }
4783 \f
4784 /* Keep track of the number of spaces used to indent the
4785    output of the debugging routines that print the structure of
4786    the DIE internal representation.  */
4787 static int print_indent;
4788
4789 /* Indent the line the number of spaces given by print_indent.  */
4790
4791 static inline void
4792 print_spaces (FILE *outfile)
4793 {
4794   fprintf (outfile, "%*s", print_indent, "");
4795 }
4796
4797 /* Print a type signature in hex.  */
4798
4799 static inline void
4800 print_signature (FILE *outfile, char *sig)
4801 {
4802   int i;
4803
4804   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
4805     fprintf (outfile, "%02x", sig[i] & 0xff);
4806 }
4807
4808 /* Print the information associated with a given DIE, and its children.
4809    This routine is a debugging aid only.  */
4810
4811 static void
4812 print_die (dw_die_ref die, FILE *outfile)
4813 {
4814   dw_attr_ref a;
4815   dw_die_ref c;
4816   unsigned ix;
4817
4818   print_spaces (outfile);
4819   fprintf (outfile, "DIE %4ld: %s (%p)\n",
4820            die->die_offset, dwarf_tag_name (die->die_tag),
4821            (void*) die);
4822   print_spaces (outfile);
4823   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
4824   fprintf (outfile, " offset: %ld", die->die_offset);
4825   fprintf (outfile, " mark: %d\n", die->die_mark);
4826
4827   if (die->comdat_type_p)
4828     {
4829       print_spaces (outfile);
4830       fprintf (outfile, "  signature: ");
4831       print_signature (outfile, die->die_id.die_type_node->signature);
4832       fprintf (outfile, "\n");
4833     }
4834
4835   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4836     {
4837       print_spaces (outfile);
4838       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
4839
4840       switch (AT_class (a))
4841         {
4842         case dw_val_class_addr:
4843           fprintf (outfile, "address");
4844           break;
4845         case dw_val_class_offset:
4846           fprintf (outfile, "offset");
4847           break;
4848         case dw_val_class_loc:
4849           fprintf (outfile, "location descriptor");
4850           break;
4851         case dw_val_class_loc_list:
4852           fprintf (outfile, "location list -> label:%s",
4853                    AT_loc_list (a)->ll_symbol);
4854           break;
4855         case dw_val_class_range_list:
4856           fprintf (outfile, "range list");
4857           break;
4858         case dw_val_class_const:
4859           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
4860           break;
4861         case dw_val_class_unsigned_const:
4862           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
4863           break;
4864         case dw_val_class_const_double:
4865           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
4866                             HOST_WIDE_INT_PRINT_UNSIGNED")",
4867                    a->dw_attr_val.v.val_double.high,
4868                    a->dw_attr_val.v.val_double.low);
4869           break;
4870         case dw_val_class_vec:
4871           fprintf (outfile, "floating-point or vector constant");
4872           break;
4873         case dw_val_class_flag:
4874           fprintf (outfile, "%u", AT_flag (a));
4875           break;
4876         case dw_val_class_die_ref:
4877           if (AT_ref (a) != NULL)
4878             {
4879               if (AT_ref (a)->comdat_type_p)
4880                 {
4881                   fprintf (outfile, "die -> signature: ");
4882                   print_signature (outfile,
4883                                    AT_ref (a)->die_id.die_type_node->signature);
4884                 }
4885               else if (AT_ref (a)->die_id.die_symbol)
4886                 fprintf (outfile, "die -> label: %s",
4887                          AT_ref (a)->die_id.die_symbol);
4888               else
4889                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
4890               fprintf (outfile, " (%p)", (void *) AT_ref (a));
4891             }
4892           else
4893             fprintf (outfile, "die -> <null>");
4894           break;
4895         case dw_val_class_vms_delta:
4896           fprintf (outfile, "delta: @slotcount(%s-%s)",
4897                    AT_vms_delta2 (a), AT_vms_delta1 (a));
4898           break;
4899         case dw_val_class_lbl_id:
4900         case dw_val_class_lineptr:
4901         case dw_val_class_macptr:
4902         case dw_val_class_high_pc:
4903           fprintf (outfile, "label: %s", AT_lbl (a));
4904           break;
4905         case dw_val_class_str:
4906           if (AT_string (a) != NULL)
4907             fprintf (outfile, "\"%s\"", AT_string (a));
4908           else
4909             fprintf (outfile, "<null>");
4910           break;
4911         case dw_val_class_file:
4912           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
4913                    AT_file (a)->emitted_number);
4914           break;
4915         case dw_val_class_data8:
4916           {
4917             int i;
4918
4919             for (i = 0; i < 8; i++)
4920               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
4921             break;
4922           }
4923         default:
4924           break;
4925         }
4926
4927       fprintf (outfile, "\n");
4928     }
4929
4930   if (die->die_child != NULL)
4931     {
4932       print_indent += 4;
4933       FOR_EACH_CHILD (die, c, print_die (c, outfile));
4934       print_indent -= 4;
4935     }
4936   if (print_indent == 0)
4937     fprintf (outfile, "\n");
4938 }
4939
4940 /* Print the information collected for a given DIE.  */
4941
4942 DEBUG_FUNCTION void
4943 debug_dwarf_die (dw_die_ref die)
4944 {
4945   print_die (die, stderr);
4946 }
4947
4948 /* Print all DWARF information collected for the compilation unit.
4949    This routine is a debugging aid only.  */
4950
4951 DEBUG_FUNCTION void
4952 debug_dwarf (void)
4953 {
4954   print_indent = 0;
4955   print_die (comp_unit_die (), stderr);
4956 }
4957 \f
4958 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
4959    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
4960    DIE that marks the start of the DIEs for this include file.  */
4961
4962 static dw_die_ref
4963 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
4964 {
4965   const char *filename = get_AT_string (bincl_die, DW_AT_name);
4966   dw_die_ref new_unit = gen_compile_unit_die (filename);
4967
4968   new_unit->die_sib = old_unit;
4969   return new_unit;
4970 }
4971
4972 /* Close an include-file CU and reopen the enclosing one.  */
4973
4974 static dw_die_ref
4975 pop_compile_unit (dw_die_ref old_unit)
4976 {
4977   dw_die_ref new_unit = old_unit->die_sib;
4978
4979   old_unit->die_sib = NULL;
4980   return new_unit;
4981 }
4982
4983 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
4984 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
4985
4986 /* Calculate the checksum of a location expression.  */
4987
4988 static inline void
4989 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
4990 {
4991   int tem;
4992
4993   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
4994   CHECKSUM (tem);
4995   CHECKSUM (loc->dw_loc_oprnd1);
4996   CHECKSUM (loc->dw_loc_oprnd2);
4997 }
4998
4999 /* Calculate the checksum of an attribute.  */
5000
5001 static void
5002 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5003 {
5004   dw_loc_descr_ref loc;
5005   rtx r;
5006
5007   CHECKSUM (at->dw_attr);
5008
5009   /* We don't care that this was compiled with a different compiler
5010      snapshot; if the output is the same, that's what matters.  */
5011   if (at->dw_attr == DW_AT_producer)
5012     return;
5013
5014   switch (AT_class (at))
5015     {
5016     case dw_val_class_const:
5017       CHECKSUM (at->dw_attr_val.v.val_int);
5018       break;
5019     case dw_val_class_unsigned_const:
5020       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5021       break;
5022     case dw_val_class_const_double:
5023       CHECKSUM (at->dw_attr_val.v.val_double);
5024       break;
5025     case dw_val_class_vec:
5026       CHECKSUM (at->dw_attr_val.v.val_vec);
5027       break;
5028     case dw_val_class_flag:
5029       CHECKSUM (at->dw_attr_val.v.val_flag);
5030       break;
5031     case dw_val_class_str:
5032       CHECKSUM_STRING (AT_string (at));
5033       break;
5034
5035     case dw_val_class_addr:
5036       r = AT_addr (at);
5037       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5038       CHECKSUM_STRING (XSTR (r, 0));
5039       break;
5040
5041     case dw_val_class_offset:
5042       CHECKSUM (at->dw_attr_val.v.val_offset);
5043       break;
5044
5045     case dw_val_class_loc:
5046       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5047         loc_checksum (loc, ctx);
5048       break;
5049
5050     case dw_val_class_die_ref:
5051       die_checksum (AT_ref (at), ctx, mark);
5052       break;
5053
5054     case dw_val_class_fde_ref:
5055     case dw_val_class_vms_delta:
5056     case dw_val_class_lbl_id:
5057     case dw_val_class_lineptr:
5058     case dw_val_class_macptr:
5059     case dw_val_class_high_pc:
5060       break;
5061
5062     case dw_val_class_file:
5063       CHECKSUM_STRING (AT_file (at)->filename);
5064       break;
5065
5066     case dw_val_class_data8:
5067       CHECKSUM (at->dw_attr_val.v.val_data8);
5068       break;
5069
5070     default:
5071       break;
5072     }
5073 }
5074
5075 /* Calculate the checksum of a DIE.  */
5076
5077 static void
5078 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5079 {
5080   dw_die_ref c;
5081   dw_attr_ref a;
5082   unsigned ix;
5083
5084   /* To avoid infinite recursion.  */
5085   if (die->die_mark)
5086     {
5087       CHECKSUM (die->die_mark);
5088       return;
5089     }
5090   die->die_mark = ++(*mark);
5091
5092   CHECKSUM (die->die_tag);
5093
5094   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5095     attr_checksum (a, ctx, mark);
5096
5097   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5098 }
5099
5100 #undef CHECKSUM
5101 #undef CHECKSUM_STRING
5102
5103 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5104 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5105 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5106 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5107 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5108 #define CHECKSUM_ATTR(FOO) \
5109   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5110
5111 /* Calculate the checksum of a number in signed LEB128 format.  */
5112
5113 static void
5114 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5115 {
5116   unsigned char byte;
5117   bool more;
5118
5119   while (1)
5120     {
5121       byte = (value & 0x7f);
5122       value >>= 7;
5123       more = !((value == 0 && (byte & 0x40) == 0)
5124                 || (value == -1 && (byte & 0x40) != 0));
5125       if (more)
5126         byte |= 0x80;
5127       CHECKSUM (byte);
5128       if (!more)
5129         break;
5130     }
5131 }
5132
5133 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5134
5135 static void
5136 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5137 {
5138   while (1)
5139     {
5140       unsigned char byte = (value & 0x7f);
5141       value >>= 7;
5142       if (value != 0)
5143         /* More bytes to follow.  */
5144         byte |= 0x80;
5145       CHECKSUM (byte);
5146       if (value == 0)
5147         break;
5148     }
5149 }
5150
5151 /* Checksum the context of the DIE.  This adds the names of any
5152    surrounding namespaces or structures to the checksum.  */
5153
5154 static void
5155 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5156 {
5157   const char *name;
5158   dw_die_ref spec;
5159   int tag = die->die_tag;
5160
5161   if (tag != DW_TAG_namespace
5162       && tag != DW_TAG_structure_type
5163       && tag != DW_TAG_class_type)
5164     return;
5165
5166   name = get_AT_string (die, DW_AT_name);
5167
5168   spec = get_AT_ref (die, DW_AT_specification);
5169   if (spec != NULL)
5170     die = spec;
5171
5172   if (die->die_parent != NULL)
5173     checksum_die_context (die->die_parent, ctx);
5174
5175   CHECKSUM_ULEB128 ('C');
5176   CHECKSUM_ULEB128 (tag);
5177   if (name != NULL)
5178     CHECKSUM_STRING (name);
5179 }
5180
5181 /* Calculate the checksum of a location expression.  */
5182
5183 static inline void
5184 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5185 {
5186   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5187      were emitted as a DW_FORM_sdata instead of a location expression.  */
5188   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5189     {
5190       CHECKSUM_ULEB128 (DW_FORM_sdata);
5191       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5192       return;
5193     }
5194
5195   /* Otherwise, just checksum the raw location expression.  */
5196   while (loc != NULL)
5197     {
5198       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5199       CHECKSUM (loc->dw_loc_oprnd1);
5200       CHECKSUM (loc->dw_loc_oprnd2);
5201       loc = loc->dw_loc_next;
5202     }
5203 }
5204
5205 /* Calculate the checksum of an attribute.  */
5206
5207 static void
5208 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5209                        struct md5_ctx *ctx, int *mark)
5210 {
5211   dw_loc_descr_ref loc;
5212   rtx r;
5213
5214   if (AT_class (at) == dw_val_class_die_ref)
5215     {
5216       dw_die_ref target_die = AT_ref (at);
5217
5218       /* For pointer and reference types, we checksum only the (qualified)
5219          name of the target type (if there is a name).  For friend entries,
5220          we checksum only the (qualified) name of the target type or function.
5221          This allows the checksum to remain the same whether the target type
5222          is complete or not.  */
5223       if ((at->dw_attr == DW_AT_type
5224            && (tag == DW_TAG_pointer_type
5225                || tag == DW_TAG_reference_type
5226                || tag == DW_TAG_rvalue_reference_type
5227                || tag == DW_TAG_ptr_to_member_type))
5228           || (at->dw_attr == DW_AT_friend
5229               && tag == DW_TAG_friend))
5230         {
5231           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5232
5233           if (name_attr != NULL)
5234             {
5235               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5236
5237               if (decl == NULL)
5238                 decl = target_die;
5239               CHECKSUM_ULEB128 ('N');
5240               CHECKSUM_ULEB128 (at->dw_attr);
5241               if (decl->die_parent != NULL)
5242                 checksum_die_context (decl->die_parent, ctx);
5243               CHECKSUM_ULEB128 ('E');
5244               CHECKSUM_STRING (AT_string (name_attr));
5245               return;
5246             }
5247         }
5248
5249       /* For all other references to another DIE, we check to see if the
5250          target DIE has already been visited.  If it has, we emit a
5251          backward reference; if not, we descend recursively.  */
5252       if (target_die->die_mark > 0)
5253         {
5254           CHECKSUM_ULEB128 ('R');
5255           CHECKSUM_ULEB128 (at->dw_attr);
5256           CHECKSUM_ULEB128 (target_die->die_mark);
5257         }
5258       else
5259         {
5260           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5261
5262           if (decl == NULL)
5263             decl = target_die;
5264           target_die->die_mark = ++(*mark);
5265           CHECKSUM_ULEB128 ('T');
5266           CHECKSUM_ULEB128 (at->dw_attr);
5267           if (decl->die_parent != NULL)
5268             checksum_die_context (decl->die_parent, ctx);
5269           die_checksum_ordered (target_die, ctx, mark);
5270         }
5271       return;
5272     }
5273
5274   CHECKSUM_ULEB128 ('A');
5275   CHECKSUM_ULEB128 (at->dw_attr);
5276
5277   switch (AT_class (at))
5278     {
5279     case dw_val_class_const:
5280       CHECKSUM_ULEB128 (DW_FORM_sdata);
5281       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5282       break;
5283
5284     case dw_val_class_unsigned_const:
5285       CHECKSUM_ULEB128 (DW_FORM_sdata);
5286       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5287       break;
5288
5289     case dw_val_class_const_double:
5290       CHECKSUM_ULEB128 (DW_FORM_block);
5291       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5292       CHECKSUM (at->dw_attr_val.v.val_double);
5293       break;
5294
5295     case dw_val_class_vec:
5296       CHECKSUM_ULEB128 (DW_FORM_block);
5297       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5298       CHECKSUM (at->dw_attr_val.v.val_vec);
5299       break;
5300
5301     case dw_val_class_flag:
5302       CHECKSUM_ULEB128 (DW_FORM_flag);
5303       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5304       break;
5305
5306     case dw_val_class_str:
5307       CHECKSUM_ULEB128 (DW_FORM_string);
5308       CHECKSUM_STRING (AT_string (at));
5309       break;
5310
5311     case dw_val_class_addr:
5312       r = AT_addr (at);
5313       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5314       CHECKSUM_ULEB128 (DW_FORM_string);
5315       CHECKSUM_STRING (XSTR (r, 0));
5316       break;
5317
5318     case dw_val_class_offset:
5319       CHECKSUM_ULEB128 (DW_FORM_sdata);
5320       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5321       break;
5322
5323     case dw_val_class_loc:
5324       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5325         loc_checksum_ordered (loc, ctx);
5326       break;
5327
5328     case dw_val_class_fde_ref:
5329     case dw_val_class_lbl_id:
5330     case dw_val_class_lineptr:
5331     case dw_val_class_macptr:
5332     case dw_val_class_high_pc:
5333       break;
5334
5335     case dw_val_class_file:
5336       CHECKSUM_ULEB128 (DW_FORM_string);
5337       CHECKSUM_STRING (AT_file (at)->filename);
5338       break;
5339
5340     case dw_val_class_data8:
5341       CHECKSUM (at->dw_attr_val.v.val_data8);
5342       break;
5343
5344     default:
5345       break;
5346     }
5347 }
5348
5349 struct checksum_attributes
5350 {
5351   dw_attr_ref at_name;
5352   dw_attr_ref at_type;
5353   dw_attr_ref at_friend;
5354   dw_attr_ref at_accessibility;
5355   dw_attr_ref at_address_class;
5356   dw_attr_ref at_allocated;
5357   dw_attr_ref at_artificial;
5358   dw_attr_ref at_associated;
5359   dw_attr_ref at_binary_scale;
5360   dw_attr_ref at_bit_offset;
5361   dw_attr_ref at_bit_size;
5362   dw_attr_ref at_bit_stride;
5363   dw_attr_ref at_byte_size;
5364   dw_attr_ref at_byte_stride;
5365   dw_attr_ref at_const_value;
5366   dw_attr_ref at_containing_type;
5367   dw_attr_ref at_count;
5368   dw_attr_ref at_data_location;
5369   dw_attr_ref at_data_member_location;
5370   dw_attr_ref at_decimal_scale;
5371   dw_attr_ref at_decimal_sign;
5372   dw_attr_ref at_default_value;
5373   dw_attr_ref at_digit_count;
5374   dw_attr_ref at_discr;
5375   dw_attr_ref at_discr_list;
5376   dw_attr_ref at_discr_value;
5377   dw_attr_ref at_encoding;
5378   dw_attr_ref at_endianity;
5379   dw_attr_ref at_explicit;
5380   dw_attr_ref at_is_optional;
5381   dw_attr_ref at_location;
5382   dw_attr_ref at_lower_bound;
5383   dw_attr_ref at_mutable;
5384   dw_attr_ref at_ordering;
5385   dw_attr_ref at_picture_string;
5386   dw_attr_ref at_prototyped;
5387   dw_attr_ref at_small;
5388   dw_attr_ref at_segment;
5389   dw_attr_ref at_string_length;
5390   dw_attr_ref at_threads_scaled;
5391   dw_attr_ref at_upper_bound;
5392   dw_attr_ref at_use_location;
5393   dw_attr_ref at_use_UTF8;
5394   dw_attr_ref at_variable_parameter;
5395   dw_attr_ref at_virtuality;
5396   dw_attr_ref at_visibility;
5397   dw_attr_ref at_vtable_elem_location;
5398 };
5399
5400 /* Collect the attributes that we will want to use for the checksum.  */
5401
5402 static void
5403 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5404 {
5405   dw_attr_ref a;
5406   unsigned ix;
5407
5408   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5409     {
5410       switch (a->dw_attr)
5411         {
5412         case DW_AT_name:
5413           attrs->at_name = a;
5414           break;
5415         case DW_AT_type:
5416           attrs->at_type = a;
5417           break;
5418         case DW_AT_friend:
5419           attrs->at_friend = a;
5420           break;
5421         case DW_AT_accessibility:
5422           attrs->at_accessibility = a;
5423           break;
5424         case DW_AT_address_class:
5425           attrs->at_address_class = a;
5426           break;
5427         case DW_AT_allocated:
5428           attrs->at_allocated = a;
5429           break;
5430         case DW_AT_artificial:
5431           attrs->at_artificial = a;
5432           break;
5433         case DW_AT_associated:
5434           attrs->at_associated = a;
5435           break;
5436         case DW_AT_binary_scale:
5437           attrs->at_binary_scale = a;
5438           break;
5439         case DW_AT_bit_offset:
5440           attrs->at_bit_offset = a;
5441           break;
5442         case DW_AT_bit_size:
5443           attrs->at_bit_size = a;
5444           break;
5445         case DW_AT_bit_stride:
5446           attrs->at_bit_stride = a;
5447           break;
5448         case DW_AT_byte_size:
5449           attrs->at_byte_size = a;
5450           break;
5451         case DW_AT_byte_stride:
5452           attrs->at_byte_stride = a;
5453           break;
5454         case DW_AT_const_value:
5455           attrs->at_const_value = a;
5456           break;
5457         case DW_AT_containing_type:
5458           attrs->at_containing_type = a;
5459           break;
5460         case DW_AT_count:
5461           attrs->at_count = a;
5462           break;
5463         case DW_AT_data_location:
5464           attrs->at_data_location = a;
5465           break;
5466         case DW_AT_data_member_location:
5467           attrs->at_data_member_location = a;
5468           break;
5469         case DW_AT_decimal_scale:
5470           attrs->at_decimal_scale = a;
5471           break;
5472         case DW_AT_decimal_sign:
5473           attrs->at_decimal_sign = a;
5474           break;
5475         case DW_AT_default_value:
5476           attrs->at_default_value = a;
5477           break;
5478         case DW_AT_digit_count:
5479           attrs->at_digit_count = a;
5480           break;
5481         case DW_AT_discr:
5482           attrs->at_discr = a;
5483           break;
5484         case DW_AT_discr_list:
5485           attrs->at_discr_list = a;
5486           break;
5487         case DW_AT_discr_value:
5488           attrs->at_discr_value = a;
5489           break;
5490         case DW_AT_encoding:
5491           attrs->at_encoding = a;
5492           break;
5493         case DW_AT_endianity:
5494           attrs->at_endianity = a;
5495           break;
5496         case DW_AT_explicit:
5497           attrs->at_explicit = a;
5498           break;
5499         case DW_AT_is_optional:
5500           attrs->at_is_optional = a;
5501           break;
5502         case DW_AT_location:
5503           attrs->at_location = a;
5504           break;
5505         case DW_AT_lower_bound:
5506           attrs->at_lower_bound = a;
5507           break;
5508         case DW_AT_mutable:
5509           attrs->at_mutable = a;
5510           break;
5511         case DW_AT_ordering:
5512           attrs->at_ordering = a;
5513           break;
5514         case DW_AT_picture_string:
5515           attrs->at_picture_string = a;
5516           break;
5517         case DW_AT_prototyped:
5518           attrs->at_prototyped = a;
5519           break;
5520         case DW_AT_small:
5521           attrs->at_small = a;
5522           break;
5523         case DW_AT_segment:
5524           attrs->at_segment = a;
5525           break;
5526         case DW_AT_string_length:
5527           attrs->at_string_length = a;
5528           break;
5529         case DW_AT_threads_scaled:
5530           attrs->at_threads_scaled = a;
5531           break;
5532         case DW_AT_upper_bound:
5533           attrs->at_upper_bound = a;
5534           break;
5535         case DW_AT_use_location:
5536           attrs->at_use_location = a;
5537           break;
5538         case DW_AT_use_UTF8:
5539           attrs->at_use_UTF8 = a;
5540           break;
5541         case DW_AT_variable_parameter:
5542           attrs->at_variable_parameter = a;
5543           break;
5544         case DW_AT_virtuality:
5545           attrs->at_virtuality = a;
5546           break;
5547         case DW_AT_visibility:
5548           attrs->at_visibility = a;
5549           break;
5550         case DW_AT_vtable_elem_location:
5551           attrs->at_vtable_elem_location = a;
5552           break;
5553         default:
5554           break;
5555         }
5556     }
5557 }
5558
5559 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
5560
5561 static void
5562 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5563 {
5564   dw_die_ref c;
5565   dw_die_ref decl;
5566   struct checksum_attributes attrs;
5567
5568   CHECKSUM_ULEB128 ('D');
5569   CHECKSUM_ULEB128 (die->die_tag);
5570
5571   memset (&attrs, 0, sizeof (attrs));
5572
5573   decl = get_AT_ref (die, DW_AT_specification);
5574   if (decl != NULL)
5575     collect_checksum_attributes (&attrs, decl);
5576   collect_checksum_attributes (&attrs, die);
5577
5578   CHECKSUM_ATTR (attrs.at_name);
5579   CHECKSUM_ATTR (attrs.at_accessibility);
5580   CHECKSUM_ATTR (attrs.at_address_class);
5581   CHECKSUM_ATTR (attrs.at_allocated);
5582   CHECKSUM_ATTR (attrs.at_artificial);
5583   CHECKSUM_ATTR (attrs.at_associated);
5584   CHECKSUM_ATTR (attrs.at_binary_scale);
5585   CHECKSUM_ATTR (attrs.at_bit_offset);
5586   CHECKSUM_ATTR (attrs.at_bit_size);
5587   CHECKSUM_ATTR (attrs.at_bit_stride);
5588   CHECKSUM_ATTR (attrs.at_byte_size);
5589   CHECKSUM_ATTR (attrs.at_byte_stride);
5590   CHECKSUM_ATTR (attrs.at_const_value);
5591   CHECKSUM_ATTR (attrs.at_containing_type);
5592   CHECKSUM_ATTR (attrs.at_count);
5593   CHECKSUM_ATTR (attrs.at_data_location);
5594   CHECKSUM_ATTR (attrs.at_data_member_location);
5595   CHECKSUM_ATTR (attrs.at_decimal_scale);
5596   CHECKSUM_ATTR (attrs.at_decimal_sign);
5597   CHECKSUM_ATTR (attrs.at_default_value);
5598   CHECKSUM_ATTR (attrs.at_digit_count);
5599   CHECKSUM_ATTR (attrs.at_discr);
5600   CHECKSUM_ATTR (attrs.at_discr_list);
5601   CHECKSUM_ATTR (attrs.at_discr_value);
5602   CHECKSUM_ATTR (attrs.at_encoding);
5603   CHECKSUM_ATTR (attrs.at_endianity);
5604   CHECKSUM_ATTR (attrs.at_explicit);
5605   CHECKSUM_ATTR (attrs.at_is_optional);
5606   CHECKSUM_ATTR (attrs.at_location);
5607   CHECKSUM_ATTR (attrs.at_lower_bound);
5608   CHECKSUM_ATTR (attrs.at_mutable);
5609   CHECKSUM_ATTR (attrs.at_ordering);
5610   CHECKSUM_ATTR (attrs.at_picture_string);
5611   CHECKSUM_ATTR (attrs.at_prototyped);
5612   CHECKSUM_ATTR (attrs.at_small);
5613   CHECKSUM_ATTR (attrs.at_segment);
5614   CHECKSUM_ATTR (attrs.at_string_length);
5615   CHECKSUM_ATTR (attrs.at_threads_scaled);
5616   CHECKSUM_ATTR (attrs.at_upper_bound);
5617   CHECKSUM_ATTR (attrs.at_use_location);
5618   CHECKSUM_ATTR (attrs.at_use_UTF8);
5619   CHECKSUM_ATTR (attrs.at_variable_parameter);
5620   CHECKSUM_ATTR (attrs.at_virtuality);
5621   CHECKSUM_ATTR (attrs.at_visibility);
5622   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
5623   CHECKSUM_ATTR (attrs.at_type);
5624   CHECKSUM_ATTR (attrs.at_friend);
5625
5626   /* Checksum the child DIEs, except for nested types and member functions.  */
5627   c = die->die_child;
5628   if (c) do {
5629     dw_attr_ref name_attr;
5630
5631     c = c->die_sib;
5632     name_attr = get_AT (c, DW_AT_name);
5633     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
5634         && name_attr != NULL)
5635       {
5636         CHECKSUM_ULEB128 ('S');
5637         CHECKSUM_ULEB128 (c->die_tag);
5638         CHECKSUM_STRING (AT_string (name_attr));
5639       }
5640     else
5641       {
5642         /* Mark this DIE so it gets processed when unmarking.  */
5643         if (c->die_mark == 0)
5644           c->die_mark = -1;
5645         die_checksum_ordered (c, ctx, mark);
5646       }
5647   } while (c != die->die_child);
5648
5649   CHECKSUM_ULEB128 (0);
5650 }
5651
5652 #undef CHECKSUM
5653 #undef CHECKSUM_STRING
5654 #undef CHECKSUM_ATTR
5655 #undef CHECKSUM_LEB128
5656 #undef CHECKSUM_ULEB128
5657
5658 /* Generate the type signature for DIE.  This is computed by generating an
5659    MD5 checksum over the DIE's tag, its relevant attributes, and its
5660    children.  Attributes that are references to other DIEs are processed
5661    by recursion, using the MARK field to prevent infinite recursion.
5662    If the DIE is nested inside a namespace or another type, we also
5663    need to include that context in the signature.  The lower 64 bits
5664    of the resulting MD5 checksum comprise the signature.  */
5665
5666 static void
5667 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
5668 {
5669   int mark;
5670   const char *name;
5671   unsigned char checksum[16];
5672   struct md5_ctx ctx;
5673   dw_die_ref decl;
5674   dw_die_ref parent;
5675
5676   name = get_AT_string (die, DW_AT_name);
5677   decl = get_AT_ref (die, DW_AT_specification);
5678   parent = get_die_parent (die);
5679
5680   /* First, compute a signature for just the type name (and its surrounding
5681      context, if any.  This is stored in the type unit DIE for link-time
5682      ODR (one-definition rule) checking.  */
5683
5684   if (is_cxx() && name != NULL)
5685     {
5686       md5_init_ctx (&ctx);
5687
5688       /* Checksum the names of surrounding namespaces and structures.  */
5689       if (parent != NULL)
5690         checksum_die_context (parent, &ctx);
5691
5692       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
5693       md5_process_bytes (name, strlen (name) + 1, &ctx);
5694       md5_finish_ctx (&ctx, checksum);
5695
5696       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
5697     }
5698
5699   /* Next, compute the complete type signature.  */
5700
5701   md5_init_ctx (&ctx);
5702   mark = 1;
5703   die->die_mark = mark;
5704
5705   /* Checksum the names of surrounding namespaces and structures.  */
5706   if (parent != NULL)
5707     checksum_die_context (parent, &ctx);
5708
5709   /* Checksum the DIE and its children.  */
5710   die_checksum_ordered (die, &ctx, &mark);
5711   unmark_all_dies (die);
5712   md5_finish_ctx (&ctx, checksum);
5713
5714   /* Store the signature in the type node and link the type DIE and the
5715      type node together.  */
5716   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
5717           DWARF_TYPE_SIGNATURE_SIZE);
5718   die->comdat_type_p = true;
5719   die->die_id.die_type_node = type_node;
5720   type_node->type_die = die;
5721
5722   /* If the DIE is a specification, link its declaration to the type node
5723      as well.  */
5724   if (decl != NULL)
5725     {
5726       decl->comdat_type_p = true;
5727       decl->die_id.die_type_node = type_node;
5728     }
5729 }
5730
5731 /* Do the location expressions look same?  */
5732 static inline int
5733 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5734 {
5735   return loc1->dw_loc_opc == loc2->dw_loc_opc
5736          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5737          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5738 }
5739
5740 /* Do the values look the same?  */
5741 static int
5742 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
5743 {
5744   dw_loc_descr_ref loc1, loc2;
5745   rtx r1, r2;
5746
5747   if (v1->val_class != v2->val_class)
5748     return 0;
5749
5750   switch (v1->val_class)
5751     {
5752     case dw_val_class_const:
5753       return v1->v.val_int == v2->v.val_int;
5754     case dw_val_class_unsigned_const:
5755       return v1->v.val_unsigned == v2->v.val_unsigned;
5756     case dw_val_class_const_double:
5757       return v1->v.val_double.high == v2->v.val_double.high
5758              && v1->v.val_double.low == v2->v.val_double.low;
5759     case dw_val_class_vec:
5760       if (v1->v.val_vec.length != v2->v.val_vec.length
5761           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5762         return 0;
5763       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5764                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
5765         return 0;
5766       return 1;
5767     case dw_val_class_flag:
5768       return v1->v.val_flag == v2->v.val_flag;
5769     case dw_val_class_str:
5770       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5771
5772     case dw_val_class_addr:
5773       r1 = v1->v.val_addr;
5774       r2 = v2->v.val_addr;
5775       if (GET_CODE (r1) != GET_CODE (r2))
5776         return 0;
5777       return !rtx_equal_p (r1, r2);
5778
5779     case dw_val_class_offset:
5780       return v1->v.val_offset == v2->v.val_offset;
5781
5782     case dw_val_class_loc:
5783       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5784            loc1 && loc2;
5785            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5786         if (!same_loc_p (loc1, loc2, mark))
5787           return 0;
5788       return !loc1 && !loc2;
5789
5790     case dw_val_class_die_ref:
5791       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5792
5793     case dw_val_class_fde_ref:
5794     case dw_val_class_vms_delta:
5795     case dw_val_class_lbl_id:
5796     case dw_val_class_lineptr:
5797     case dw_val_class_macptr:
5798     case dw_val_class_high_pc:
5799       return 1;
5800
5801     case dw_val_class_file:
5802       return v1->v.val_file == v2->v.val_file;
5803
5804     case dw_val_class_data8:
5805       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
5806
5807     default:
5808       return 1;
5809     }
5810 }
5811
5812 /* Do the attributes look the same?  */
5813
5814 static int
5815 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5816 {
5817   if (at1->dw_attr != at2->dw_attr)
5818     return 0;
5819
5820   /* We don't care that this was compiled with a different compiler
5821      snapshot; if the output is the same, that's what matters. */
5822   if (at1->dw_attr == DW_AT_producer)
5823     return 1;
5824
5825   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5826 }
5827
5828 /* Do the dies look the same?  */
5829
5830 static int
5831 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5832 {
5833   dw_die_ref c1, c2;
5834   dw_attr_ref a1;
5835   unsigned ix;
5836
5837   /* To avoid infinite recursion.  */
5838   if (die1->die_mark)
5839     return die1->die_mark == die2->die_mark;
5840   die1->die_mark = die2->die_mark = ++(*mark);
5841
5842   if (die1->die_tag != die2->die_tag)
5843     return 0;
5844
5845   if (VEC_length (dw_attr_node, die1->die_attr)
5846       != VEC_length (dw_attr_node, die2->die_attr))
5847     return 0;
5848
5849   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
5850     if (!same_attr_p (a1, &VEC_index (dw_attr_node, die2->die_attr, ix), mark))
5851       return 0;
5852
5853   c1 = die1->die_child;
5854   c2 = die2->die_child;
5855   if (! c1)
5856     {
5857       if (c2)
5858         return 0;
5859     }
5860   else
5861     for (;;)
5862       {
5863         if (!same_die_p (c1, c2, mark))
5864           return 0;
5865         c1 = c1->die_sib;
5866         c2 = c2->die_sib;
5867         if (c1 == die1->die_child)
5868           {
5869             if (c2 == die2->die_child)
5870               break;
5871             else
5872               return 0;
5873           }
5874     }
5875
5876   return 1;
5877 }
5878
5879 /* Do the dies look the same?  Wrapper around same_die_p.  */
5880
5881 static int
5882 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5883 {
5884   int mark = 0;
5885   int ret = same_die_p (die1, die2, &mark);
5886
5887   unmark_all_dies (die1);
5888   unmark_all_dies (die2);
5889
5890   return ret;
5891 }
5892
5893 /* The prefix to attach to symbols on DIEs in the current comdat debug
5894    info section.  */
5895 static const char *comdat_symbol_id;
5896
5897 /* The index of the current symbol within the current comdat CU.  */
5898 static unsigned int comdat_symbol_number;
5899
5900 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5901    children, and set comdat_symbol_id accordingly.  */
5902
5903 static void
5904 compute_section_prefix (dw_die_ref unit_die)
5905 {
5906   const char *die_name = get_AT_string (unit_die, DW_AT_name);
5907   const char *base = die_name ? lbasename (die_name) : "anonymous";
5908   char *name = XALLOCAVEC (char, strlen (base) + 64);
5909   char *p;
5910   int i, mark;
5911   unsigned char checksum[16];
5912   struct md5_ctx ctx;
5913
5914   /* Compute the checksum of the DIE, then append part of it as hex digits to
5915      the name filename of the unit.  */
5916
5917   md5_init_ctx (&ctx);
5918   mark = 0;
5919   die_checksum (unit_die, &ctx, &mark);
5920   unmark_all_dies (unit_die);
5921   md5_finish_ctx (&ctx, checksum);
5922
5923   sprintf (name, "%s.", base);
5924   clean_symbol_name (name);
5925
5926   p = name + strlen (name);
5927   for (i = 0; i < 4; i++)
5928     {
5929       sprintf (p, "%.2x", checksum[i]);
5930       p += 2;
5931     }
5932
5933   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
5934   comdat_symbol_number = 0;
5935 }
5936
5937 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
5938
5939 static int
5940 is_type_die (dw_die_ref die)
5941 {
5942   switch (die->die_tag)
5943     {
5944     case DW_TAG_array_type:
5945     case DW_TAG_class_type:
5946     case DW_TAG_interface_type:
5947     case DW_TAG_enumeration_type:
5948     case DW_TAG_pointer_type:
5949     case DW_TAG_reference_type:
5950     case DW_TAG_rvalue_reference_type:
5951     case DW_TAG_string_type:
5952     case DW_TAG_structure_type:
5953     case DW_TAG_subroutine_type:
5954     case DW_TAG_union_type:
5955     case DW_TAG_ptr_to_member_type:
5956     case DW_TAG_set_type:
5957     case DW_TAG_subrange_type:
5958     case DW_TAG_base_type:
5959     case DW_TAG_const_type:
5960     case DW_TAG_file_type:
5961     case DW_TAG_packed_type:
5962     case DW_TAG_volatile_type:
5963     case DW_TAG_typedef:
5964       return 1;
5965     default:
5966       return 0;
5967     }
5968 }
5969
5970 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5971    Basically, we want to choose the bits that are likely to be shared between
5972    compilations (types) and leave out the bits that are specific to individual
5973    compilations (functions).  */
5974
5975 static int
5976 is_comdat_die (dw_die_ref c)
5977 {
5978   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5979      we do for stabs.  The advantage is a greater likelihood of sharing between
5980      objects that don't include headers in the same order (and therefore would
5981      put the base types in a different comdat).  jason 8/28/00 */
5982
5983   if (c->die_tag == DW_TAG_base_type)
5984     return 0;
5985
5986   if (c->die_tag == DW_TAG_pointer_type
5987       || c->die_tag == DW_TAG_reference_type
5988       || c->die_tag == DW_TAG_rvalue_reference_type
5989       || c->die_tag == DW_TAG_const_type
5990       || c->die_tag == DW_TAG_volatile_type)
5991     {
5992       dw_die_ref t = get_AT_ref (c, DW_AT_type);
5993
5994       return t ? is_comdat_die (t) : 0;
5995     }
5996
5997   return is_type_die (c);
5998 }
5999
6000 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6001    compilation unit.  */
6002
6003 static int
6004 is_symbol_die (dw_die_ref c)
6005 {
6006   return (is_type_die (c)
6007           || is_declaration_die (c)
6008           || c->die_tag == DW_TAG_namespace
6009           || c->die_tag == DW_TAG_module);
6010 }
6011
6012 /* Returns true iff C is a compile-unit DIE.  */
6013
6014 static inline bool
6015 is_cu_die (dw_die_ref c)
6016 {
6017   return c && c->die_tag == DW_TAG_compile_unit;
6018 }
6019
6020 /* Returns true iff C is a unit DIE of some sort.  */
6021
6022 static inline bool
6023 is_unit_die (dw_die_ref c)
6024 {
6025   return c && (c->die_tag == DW_TAG_compile_unit
6026                || c->die_tag == DW_TAG_partial_unit
6027                || c->die_tag == DW_TAG_type_unit);
6028 }
6029
6030 /* Returns true iff C is a namespace DIE.  */
6031
6032 static inline bool
6033 is_namespace_die (dw_die_ref c)
6034 {
6035   return c && c->die_tag == DW_TAG_namespace;
6036 }
6037
6038 /* Returns true iff C is a class or structure DIE.  */
6039
6040 static inline bool
6041 is_class_die (dw_die_ref c)
6042 {
6043   return c && (c->die_tag == DW_TAG_class_type
6044                || c->die_tag == DW_TAG_structure_type);
6045 }
6046
6047 static char *
6048 gen_internal_sym (const char *prefix)
6049 {
6050   char buf[256];
6051
6052   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6053   return xstrdup (buf);
6054 }
6055
6056 /* Assign symbols to all worthy DIEs under DIE.  */
6057
6058 static void
6059 assign_symbol_names (dw_die_ref die)
6060 {
6061   dw_die_ref c;
6062
6063   if (is_symbol_die (die) && !die->comdat_type_p)
6064     {
6065       if (comdat_symbol_id)
6066         {
6067           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6068
6069           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6070                    comdat_symbol_id, comdat_symbol_number++);
6071           die->die_id.die_symbol = xstrdup (p);
6072         }
6073       else
6074         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6075     }
6076
6077   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6078 }
6079
6080 struct cu_hash_table_entry
6081 {
6082   dw_die_ref cu;
6083   unsigned min_comdat_num, max_comdat_num;
6084   struct cu_hash_table_entry *next;
6085 };
6086
6087 /* Routines to manipulate hash table of CUs.  */
6088 static hashval_t
6089 htab_cu_hash (const void *of)
6090 {
6091   const struct cu_hash_table_entry *const entry =
6092     (const struct cu_hash_table_entry *) of;
6093
6094   return htab_hash_string (entry->cu->die_id.die_symbol);
6095 }
6096
6097 static int
6098 htab_cu_eq (const void *of1, const void *of2)
6099 {
6100   const struct cu_hash_table_entry *const entry1 =
6101     (const struct cu_hash_table_entry *) of1;
6102   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6103
6104   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6105 }
6106
6107 static void
6108 htab_cu_del (void *what)
6109 {
6110   struct cu_hash_table_entry *next,
6111     *entry = (struct cu_hash_table_entry *) what;
6112
6113   while (entry)
6114     {
6115       next = entry->next;
6116       free (entry);
6117       entry = next;
6118     }
6119 }
6120
6121 /* Check whether we have already seen this CU and set up SYM_NUM
6122    accordingly.  */
6123 static int
6124 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6125 {
6126   struct cu_hash_table_entry dummy;
6127   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6128
6129   dummy.max_comdat_num = 0;
6130
6131   slot = (struct cu_hash_table_entry **)
6132     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6133         INSERT);
6134   entry = *slot;
6135
6136   for (; entry; last = entry, entry = entry->next)
6137     {
6138       if (same_die_p_wrap (cu, entry->cu))
6139         break;
6140     }
6141
6142   if (entry)
6143     {
6144       *sym_num = entry->min_comdat_num;
6145       return 1;
6146     }
6147
6148   entry = XCNEW (struct cu_hash_table_entry);
6149   entry->cu = cu;
6150   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6151   entry->next = *slot;
6152   *slot = entry;
6153
6154   return 0;
6155 }
6156
6157 /* Record SYM_NUM to record of CU in HTABLE.  */
6158 static void
6159 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6160 {
6161   struct cu_hash_table_entry **slot, *entry;
6162
6163   slot = (struct cu_hash_table_entry **)
6164     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6165         NO_INSERT);
6166   entry = *slot;
6167
6168   entry->max_comdat_num = sym_num;
6169 }
6170
6171 /* Traverse the DIE (which is always comp_unit_die), and set up
6172    additional compilation units for each of the include files we see
6173    bracketed by BINCL/EINCL.  */
6174
6175 static void
6176 break_out_includes (dw_die_ref die)
6177 {
6178   dw_die_ref c;
6179   dw_die_ref unit = NULL;
6180   limbo_die_node *node, **pnode;
6181   htab_t cu_hash_table;
6182
6183   c = die->die_child;
6184   if (c) do {
6185     dw_die_ref prev = c;
6186     c = c->die_sib;
6187     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6188            || (unit && is_comdat_die (c)))
6189       {
6190         dw_die_ref next = c->die_sib;
6191
6192         /* This DIE is for a secondary CU; remove it from the main one.  */
6193         remove_child_with_prev (c, prev);
6194
6195         if (c->die_tag == DW_TAG_GNU_BINCL)
6196           unit = push_new_compile_unit (unit, c);
6197         else if (c->die_tag == DW_TAG_GNU_EINCL)
6198           unit = pop_compile_unit (unit);
6199         else
6200           add_child_die (unit, c);
6201         c = next;
6202         if (c == die->die_child)
6203           break;
6204       }
6205   } while (c != die->die_child);
6206
6207 #if 0
6208   /* We can only use this in debugging, since the frontend doesn't check
6209      to make sure that we leave every include file we enter.  */
6210   gcc_assert (!unit);
6211 #endif
6212
6213   assign_symbol_names (die);
6214   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6215   for (node = limbo_die_list, pnode = &limbo_die_list;
6216        node;
6217        node = node->next)
6218     {
6219       int is_dupl;
6220
6221       compute_section_prefix (node->die);
6222       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6223                         &comdat_symbol_number);
6224       assign_symbol_names (node->die);
6225       if (is_dupl)
6226         *pnode = node->next;
6227       else
6228         {
6229           pnode = &node->next;
6230           record_comdat_symbol_number (node->die, cu_hash_table,
6231                 comdat_symbol_number);
6232         }
6233     }
6234   htab_delete (cu_hash_table);
6235 }
6236
6237 /* Return non-zero if this DIE is a declaration.  */
6238
6239 static int
6240 is_declaration_die (dw_die_ref die)
6241 {
6242   dw_attr_ref a;
6243   unsigned ix;
6244
6245   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6246     if (a->dw_attr == DW_AT_declaration)
6247       return 1;
6248
6249   return 0;
6250 }
6251
6252 /* Return non-zero if this DIE is nested inside a subprogram.  */
6253
6254 static int
6255 is_nested_in_subprogram (dw_die_ref die)
6256 {
6257   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6258
6259   if (decl == NULL)
6260     decl = die;
6261   return local_scope_p (decl);
6262 }
6263
6264 /* Return non-zero if this DIE contains a defining declaration of a
6265    subprogram.  */
6266
6267 static int
6268 contains_subprogram_definition (dw_die_ref die)
6269 {
6270   dw_die_ref c;
6271
6272   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6273     return 1;
6274   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6275   return 0;
6276 }
6277
6278 /* Return non-zero if this is a type DIE that should be moved to a
6279    COMDAT .debug_types section.  */
6280
6281 static int
6282 should_move_die_to_comdat (dw_die_ref die)
6283 {
6284   switch (die->die_tag)
6285     {
6286     case DW_TAG_class_type:
6287     case DW_TAG_structure_type:
6288     case DW_TAG_enumeration_type:
6289     case DW_TAG_union_type:
6290       /* Don't move declarations, inlined instances, or types nested in a
6291          subprogram.  */
6292       if (is_declaration_die (die)
6293           || get_AT (die, DW_AT_abstract_origin)
6294           || is_nested_in_subprogram (die))
6295         return 0;
6296       /* A type definition should never contain a subprogram definition.  */
6297       gcc_assert (!contains_subprogram_definition (die));
6298       return 1;
6299     case DW_TAG_array_type:
6300     case DW_TAG_interface_type:
6301     case DW_TAG_pointer_type:
6302     case DW_TAG_reference_type:
6303     case DW_TAG_rvalue_reference_type:
6304     case DW_TAG_string_type:
6305     case DW_TAG_subroutine_type:
6306     case DW_TAG_ptr_to_member_type:
6307     case DW_TAG_set_type:
6308     case DW_TAG_subrange_type:
6309     case DW_TAG_base_type:
6310     case DW_TAG_const_type:
6311     case DW_TAG_file_type:
6312     case DW_TAG_packed_type:
6313     case DW_TAG_volatile_type:
6314     case DW_TAG_typedef:
6315     default:
6316       return 0;
6317     }
6318 }
6319
6320 /* Make a clone of DIE.  */
6321
6322 static dw_die_ref
6323 clone_die (dw_die_ref die)
6324 {
6325   dw_die_ref clone;
6326   dw_attr_ref a;
6327   unsigned ix;
6328
6329   clone = ggc_alloc_cleared_die_node ();
6330   clone->die_tag = die->die_tag;
6331
6332   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6333     add_dwarf_attr (clone, a);
6334
6335   return clone;
6336 }
6337
6338 /* Make a clone of the tree rooted at DIE.  */
6339
6340 static dw_die_ref
6341 clone_tree (dw_die_ref die)
6342 {
6343   dw_die_ref c;
6344   dw_die_ref clone = clone_die (die);
6345
6346   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6347
6348   return clone;
6349 }
6350
6351 /* Make a clone of DIE as a declaration.  */
6352
6353 static dw_die_ref
6354 clone_as_declaration (dw_die_ref die)
6355 {
6356   dw_die_ref clone;
6357   dw_die_ref decl;
6358   dw_attr_ref a;
6359   unsigned ix;
6360
6361   /* If the DIE is already a declaration, just clone it.  */
6362   if (is_declaration_die (die))
6363     return clone_die (die);
6364
6365   /* If the DIE is a specification, just clone its declaration DIE.  */
6366   decl = get_AT_ref (die, DW_AT_specification);
6367   if (decl != NULL)
6368     {
6369       clone = clone_die (decl);
6370       if (die->comdat_type_p)
6371         add_AT_die_ref (clone, DW_AT_signature, die);
6372       return clone;
6373     }
6374
6375   clone = ggc_alloc_cleared_die_node ();
6376   clone->die_tag = die->die_tag;
6377
6378   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6379     {
6380       /* We don't want to copy over all attributes.
6381          For example we don't want DW_AT_byte_size because otherwise we will no
6382          longer have a declaration and GDB will treat it as a definition.  */
6383
6384       switch (a->dw_attr)
6385         {
6386         case DW_AT_artificial:
6387         case DW_AT_containing_type:
6388         case DW_AT_external:
6389         case DW_AT_name:
6390         case DW_AT_type:
6391         case DW_AT_virtuality:
6392         case DW_AT_linkage_name:
6393         case DW_AT_MIPS_linkage_name:
6394           add_dwarf_attr (clone, a);
6395           break;
6396         case DW_AT_byte_size:
6397         default:
6398           break;
6399         }
6400     }
6401
6402   if (die->comdat_type_p)
6403     add_AT_die_ref (clone, DW_AT_signature, die);
6404
6405   add_AT_flag (clone, DW_AT_declaration, 1);
6406   return clone;
6407 }
6408
6409 /* Copy the declaration context to the new type unit DIE.  This includes
6410    any surrounding namespace or type declarations.  If the DIE has an
6411    AT_specification attribute, it also includes attributes and children
6412    attached to the specification, and returns a pointer to the original
6413    parent of the declaration DIE.  Returns NULL otherwise.  */
6414
6415 static dw_die_ref
6416 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6417 {
6418   dw_die_ref decl;
6419   dw_die_ref new_decl;
6420   dw_die_ref orig_parent = NULL;
6421
6422   decl = get_AT_ref (die, DW_AT_specification);
6423   if (decl == NULL)
6424     decl = die;
6425   else
6426     {
6427       unsigned ix;
6428       dw_die_ref c;
6429       dw_attr_ref a;
6430
6431       /* The original DIE will be changed to a declaration, and must
6432          be moved to be a child of the original declaration DIE.  */
6433       orig_parent = decl->die_parent;
6434
6435       /* Copy the type node pointer from the new DIE to the original
6436          declaration DIE so we can forward references later.  */
6437       decl->comdat_type_p = true;
6438       decl->die_id.die_type_node = die->die_id.die_type_node;
6439
6440       remove_AT (die, DW_AT_specification);
6441
6442       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
6443         {
6444           if (a->dw_attr != DW_AT_name
6445               && a->dw_attr != DW_AT_declaration
6446               && a->dw_attr != DW_AT_external)
6447             add_dwarf_attr (die, a);
6448         }
6449
6450       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6451     }
6452
6453   if (decl->die_parent != NULL
6454       && !is_unit_die (decl->die_parent))
6455     {
6456       new_decl = copy_ancestor_tree (unit, decl, NULL);
6457       if (new_decl != NULL)
6458         {
6459           remove_AT (new_decl, DW_AT_signature);
6460           add_AT_specification (die, new_decl);
6461         }
6462     }
6463
6464   return orig_parent;
6465 }
6466
6467 /* Generate the skeleton ancestor tree for the given NODE, then clone
6468    the DIE and add the clone into the tree.  */
6469
6470 static void
6471 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6472 {
6473   if (node->new_die != NULL)
6474     return;
6475
6476   node->new_die = clone_as_declaration (node->old_die);
6477
6478   if (node->parent != NULL)
6479     {
6480       generate_skeleton_ancestor_tree (node->parent);
6481       add_child_die (node->parent->new_die, node->new_die);
6482     }
6483 }
6484
6485 /* Generate a skeleton tree of DIEs containing any declarations that are
6486    found in the original tree.  We traverse the tree looking for declaration
6487    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
6488
6489 static void
6490 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6491 {
6492   skeleton_chain_node node;
6493   dw_die_ref c;
6494   dw_die_ref first;
6495   dw_die_ref prev = NULL;
6496   dw_die_ref next = NULL;
6497
6498   node.parent = parent;
6499
6500   first = c = parent->old_die->die_child;
6501   if (c)
6502     next = c->die_sib;
6503   if (c) do {
6504     if (prev == NULL || prev->die_sib == c)
6505       prev = c;
6506     c = next;
6507     next = (c == first ? NULL : c->die_sib);
6508     node.old_die = c;
6509     node.new_die = NULL;
6510     if (is_declaration_die (c))
6511       {
6512         /* Clone the existing DIE, move the original to the skeleton
6513            tree (which is in the main CU), and put the clone, with
6514            all the original's children, where the original came from.  */
6515         dw_die_ref clone = clone_die (c);
6516         move_all_children (c, clone);
6517
6518         replace_child (c, clone, prev);
6519         generate_skeleton_ancestor_tree (parent);
6520         add_child_die (parent->new_die, c);
6521         node.new_die = c;
6522         c = clone;
6523       }
6524     generate_skeleton_bottom_up (&node);
6525   } while (next != NULL);
6526 }
6527
6528 /* Wrapper function for generate_skeleton_bottom_up.  */
6529
6530 static dw_die_ref
6531 generate_skeleton (dw_die_ref die)
6532 {
6533   skeleton_chain_node node;
6534
6535   node.old_die = die;
6536   node.new_die = NULL;
6537   node.parent = NULL;
6538
6539   /* If this type definition is nested inside another type,
6540      always leave at least a declaration in its place.  */
6541   if (die->die_parent != NULL && is_type_die (die->die_parent))
6542     node.new_die = clone_as_declaration (die);
6543
6544   generate_skeleton_bottom_up (&node);
6545   return node.new_die;
6546 }
6547
6548 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6549    declaration.  The original DIE is moved to a new compile unit so that
6550    existing references to it follow it to the new location.  If any of the
6551    original DIE's descendants is a declaration, we need to replace the
6552    original DIE with a skeleton tree and move the declarations back into the
6553    skeleton tree.  */
6554
6555 static dw_die_ref
6556 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6557                                        dw_die_ref prev)
6558 {
6559   dw_die_ref skeleton, orig_parent;
6560
6561   /* Copy the declaration context to the type unit DIE.  If the returned
6562      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6563      that DIE.  */
6564   orig_parent = copy_declaration_context (unit, child);
6565
6566   skeleton = generate_skeleton (child);
6567   if (skeleton == NULL)
6568     remove_child_with_prev (child, prev);
6569   else
6570     {
6571       skeleton->comdat_type_p = true;
6572       skeleton->die_id.die_type_node = child->die_id.die_type_node;
6573
6574       /* If the original DIE was a specification, we need to put
6575          the skeleton under the parent DIE of the declaration.
6576          This leaves the original declaration in the tree, but
6577          it will be pruned later since there are no longer any
6578          references to it.  */
6579       if (orig_parent != NULL)
6580         {
6581           remove_child_with_prev (child, prev);
6582           add_child_die (orig_parent, skeleton);
6583         }
6584       else
6585         replace_child (child, skeleton, prev);
6586     }
6587
6588   return skeleton;
6589 }
6590
6591 /* Traverse the DIE and set up additional .debug_types sections for each
6592    type worthy of being placed in a COMDAT section.  */
6593
6594 static void
6595 break_out_comdat_types (dw_die_ref die)
6596 {
6597   dw_die_ref c;
6598   dw_die_ref first;
6599   dw_die_ref prev = NULL;
6600   dw_die_ref next = NULL;
6601   dw_die_ref unit = NULL;
6602
6603   first = c = die->die_child;
6604   if (c)
6605     next = c->die_sib;
6606   if (c) do {
6607     if (prev == NULL || prev->die_sib == c)
6608       prev = c;
6609     c = next;
6610     next = (c == first ? NULL : c->die_sib);
6611     if (should_move_die_to_comdat (c))
6612       {
6613         dw_die_ref replacement;
6614         comdat_type_node_ref type_node;
6615
6616         /* Create a new type unit DIE as the root for the new tree, and
6617            add it to the list of comdat types.  */
6618         unit = new_die (DW_TAG_type_unit, NULL, NULL);
6619         add_AT_unsigned (unit, DW_AT_language,
6620                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
6621         type_node = ggc_alloc_cleared_comdat_type_node ();
6622         type_node->root_die = unit;
6623         type_node->next = comdat_type_list;
6624         comdat_type_list = type_node;
6625
6626         /* Generate the type signature.  */
6627         generate_type_signature (c, type_node);
6628
6629         /* Copy the declaration context, attributes, and children of the
6630            declaration into the new type unit DIE, then remove this DIE
6631            from the main CU (or replace it with a skeleton if necessary).  */
6632         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
6633         type_node->skeleton_die = replacement;
6634
6635         /* Break out nested types into their own type units.  */
6636         break_out_comdat_types (c);
6637
6638         /* Add the DIE to the new compunit.  */
6639         add_child_die (unit, c);
6640
6641         if (replacement != NULL)
6642           c = replacement;
6643       }
6644     else if (c->die_tag == DW_TAG_namespace
6645              || c->die_tag == DW_TAG_class_type
6646              || c->die_tag == DW_TAG_structure_type
6647              || c->die_tag == DW_TAG_union_type)
6648       {
6649         /* Look for nested types that can be broken out.  */
6650         break_out_comdat_types (c);
6651       }
6652   } while (next != NULL);
6653 }
6654
6655 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
6656
6657 struct decl_table_entry
6658 {
6659   dw_die_ref orig;
6660   dw_die_ref copy;
6661 };
6662
6663 /* Routines to manipulate hash table of copied declarations.  */
6664
6665 static hashval_t
6666 htab_decl_hash (const void *of)
6667 {
6668   const struct decl_table_entry *const entry =
6669     (const struct decl_table_entry *) of;
6670
6671   return htab_hash_pointer (entry->orig);
6672 }
6673
6674 static int
6675 htab_decl_eq (const void *of1, const void *of2)
6676 {
6677   const struct decl_table_entry *const entry1 =
6678     (const struct decl_table_entry *) of1;
6679   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6680
6681   return entry1->orig == entry2;
6682 }
6683
6684 static void
6685 htab_decl_del (void *what)
6686 {
6687   struct decl_table_entry *entry = (struct decl_table_entry *) what;
6688
6689   free (entry);
6690 }
6691
6692 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6693    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
6694    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
6695    to check if the ancestor has already been copied into UNIT.  */
6696
6697 static dw_die_ref
6698 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6699 {
6700   dw_die_ref parent = die->die_parent;
6701   dw_die_ref new_parent = unit;
6702   dw_die_ref copy;
6703   void **slot = NULL;
6704   struct decl_table_entry *entry = NULL;
6705
6706   if (decl_table)
6707     {
6708       /* Check if the entry has already been copied to UNIT.  */
6709       slot = htab_find_slot_with_hash (decl_table, die,
6710                                        htab_hash_pointer (die), INSERT);
6711       if (*slot != HTAB_EMPTY_ENTRY)
6712         {
6713           entry = (struct decl_table_entry *) *slot;
6714           return entry->copy;
6715         }
6716
6717       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
6718       entry = XCNEW (struct decl_table_entry);
6719       entry->orig = die;
6720       entry->copy = NULL;
6721       *slot = entry;
6722     }
6723
6724   if (parent != NULL)
6725     {
6726       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6727       if (spec != NULL)
6728         parent = spec;
6729       if (!is_unit_die (parent))
6730         new_parent = copy_ancestor_tree (unit, parent, decl_table);
6731     }
6732
6733   copy = clone_as_declaration (die);
6734   add_child_die (new_parent, copy);
6735
6736   if (decl_table != NULL)
6737     {
6738       /* Record the pointer to the copy.  */
6739       entry->copy = copy;
6740     }
6741
6742   return copy;
6743 }
6744
6745 /* Like clone_tree, but additionally enter all the children into
6746    the hash table decl_table.  */
6747
6748 static dw_die_ref
6749 clone_tree_hash (dw_die_ref die, htab_t decl_table)
6750 {
6751   dw_die_ref c;
6752   dw_die_ref clone = clone_die (die);
6753   struct decl_table_entry *entry;
6754   void **slot = htab_find_slot_with_hash (decl_table, die,
6755                                           htab_hash_pointer (die), INSERT);
6756   /* Assert that DIE isn't in the hash table yet.  If it would be there
6757      before, the ancestors would be necessarily there as well, therefore
6758      clone_tree_hash wouldn't be called.  */
6759   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
6760   entry = XCNEW (struct decl_table_entry);
6761   entry->orig = die;
6762   entry->copy = clone;
6763   *slot = entry;
6764
6765   FOR_EACH_CHILD (die, c,
6766                   add_child_die (clone, clone_tree_hash (c, decl_table)));
6767
6768   return clone;
6769 }
6770
6771 /* Walk the DIE and its children, looking for references to incomplete
6772    or trivial types that are unmarked (i.e., that are not in the current
6773    type_unit).  */
6774
6775 static void
6776 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6777 {
6778   dw_die_ref c;
6779   dw_attr_ref a;
6780   unsigned ix;
6781
6782   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6783     {
6784       if (AT_class (a) == dw_val_class_die_ref)
6785         {
6786           dw_die_ref targ = AT_ref (a);
6787           void **slot;
6788           struct decl_table_entry *entry;
6789
6790           if (targ->die_mark != 0 || targ->comdat_type_p)
6791             continue;
6792
6793           slot = htab_find_slot_with_hash (decl_table, targ,
6794                                            htab_hash_pointer (targ), INSERT);
6795
6796           if (*slot != HTAB_EMPTY_ENTRY)
6797             {
6798               /* TARG has already been copied, so we just need to
6799                  modify the reference to point to the copy.  */
6800               entry = (struct decl_table_entry *) *slot;
6801               a->dw_attr_val.v.val_die_ref.die = entry->copy;
6802             }
6803           else
6804             {
6805               dw_die_ref parent = unit;
6806               dw_die_ref copy = clone_die (targ);
6807
6808               /* Record in DECL_TABLE that TARG has been copied.
6809                  Need to do this now, before the recursive call,
6810                  because DECL_TABLE may be expanded and SLOT
6811                  would no longer be a valid pointer.  */
6812               entry = XCNEW (struct decl_table_entry);
6813               entry->orig = targ;
6814               entry->copy = copy;
6815               *slot = entry;
6816
6817               FOR_EACH_CHILD (targ, c,
6818                               add_child_die (copy,
6819                                              clone_tree_hash (c, decl_table)));
6820
6821               /* Make sure the cloned tree is marked as part of the
6822                  type unit.  */
6823               mark_dies (copy);
6824
6825               /* If TARG has surrounding context, copy its ancestor tree
6826                  into the new type unit.  */
6827               if (targ->die_parent != NULL
6828                   && !is_unit_die (targ->die_parent))
6829                 parent = copy_ancestor_tree (unit, targ->die_parent,
6830                                              decl_table);
6831
6832               add_child_die (parent, copy);
6833               a->dw_attr_val.v.val_die_ref.die = copy;
6834
6835               /* Make sure the newly-copied DIE is walked.  If it was
6836                  installed in a previously-added context, it won't
6837                  get visited otherwise.  */
6838               if (parent != unit)
6839                 {
6840                   /* Find the highest point of the newly-added tree,
6841                      mark each node along the way, and walk from there.  */
6842                   parent->die_mark = 1;
6843                   while (parent->die_parent
6844                          && parent->die_parent->die_mark == 0)
6845                     {
6846                       parent = parent->die_parent;
6847                       parent->die_mark = 1;
6848                     }
6849                   copy_decls_walk (unit, parent, decl_table);
6850                 }
6851             }
6852         }
6853     }
6854
6855   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
6856 }
6857
6858 /* Copy declarations for "unworthy" types into the new comdat section.
6859    Incomplete types, modified types, and certain other types aren't broken
6860    out into comdat sections of their own, so they don't have a signature,
6861    and we need to copy the declaration into the same section so that we
6862    don't have an external reference.  */
6863
6864 static void
6865 copy_decls_for_unworthy_types (dw_die_ref unit)
6866 {
6867   htab_t decl_table;
6868
6869   mark_dies (unit);
6870   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
6871   copy_decls_walk (unit, unit, decl_table);
6872   htab_delete (decl_table);
6873   unmark_dies (unit);
6874 }
6875
6876 /* Traverse the DIE and add a sibling attribute if it may have the
6877    effect of speeding up access to siblings.  To save some space,
6878    avoid generating sibling attributes for DIE's without children.  */
6879
6880 static void
6881 add_sibling_attributes (dw_die_ref die)
6882 {
6883   dw_die_ref c;
6884
6885   if (! die->die_child)
6886     return;
6887
6888   if (die->die_parent && die != die->die_parent->die_child)
6889     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6890
6891   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6892 }
6893
6894 /* Output all location lists for the DIE and its children.  */
6895
6896 static void
6897 output_location_lists (dw_die_ref die)
6898 {
6899   dw_die_ref c;
6900   dw_attr_ref a;
6901   unsigned ix;
6902
6903   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6904     if (AT_class (a) == dw_val_class_loc_list)
6905       output_loc_list (AT_loc_list (a));
6906
6907   FOR_EACH_CHILD (die, c, output_location_lists (c));
6908 }
6909
6910 /* We want to limit the number of external references, because they are
6911    larger than local references: a relocation takes multiple words, and
6912    even a sig8 reference is always eight bytes, whereas a local reference
6913    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
6914    So if we encounter multiple external references to the same type DIE, we
6915    make a local typedef stub for it and redirect all references there.
6916
6917    This is the element of the hash table for keeping track of these
6918    references.  */
6919
6920 struct external_ref
6921 {
6922   dw_die_ref type;
6923   dw_die_ref stub;
6924   unsigned n_refs;
6925 };
6926
6927 /* Hash an external_ref.  */
6928
6929 static hashval_t
6930 hash_external_ref (const void *p)
6931 {
6932   const struct external_ref *r = (const struct external_ref *)p;
6933   return htab_hash_pointer (r->type);
6934 }
6935
6936 /* Compare external_refs.  */
6937
6938 static int
6939 external_ref_eq (const void *p1, const void *p2)
6940 {
6941   const struct external_ref *r1 = (const struct external_ref *)p1;
6942   const struct external_ref *r2 = (const struct external_ref *)p2;
6943   return r1->type == r2->type;
6944 }
6945
6946 /* Return a pointer to the external_ref for references to DIE.  */
6947
6948 static struct external_ref *
6949 lookup_external_ref (htab_t map, dw_die_ref die)
6950 {
6951   struct external_ref ref, *ref_p;
6952   void ** slot;
6953
6954   ref.type = die;
6955   slot = htab_find_slot (map, &ref, INSERT);
6956   if (*slot != HTAB_EMPTY_ENTRY)
6957     return (struct external_ref *) *slot;
6958
6959   ref_p = XCNEW (struct external_ref);
6960   ref_p->type = die;
6961   *slot = ref_p;
6962   return ref_p;
6963 }
6964
6965 /* Subroutine of optimize_external_refs, below.
6966
6967    If we see a type skeleton, record it as our stub.  If we see external
6968    references, remember how many we've seen.  */
6969
6970 static void
6971 optimize_external_refs_1 (dw_die_ref die, htab_t map)
6972 {
6973   dw_die_ref c;
6974   dw_attr_ref a;
6975   unsigned ix;
6976   struct external_ref *ref_p;
6977
6978   if (is_type_die (die)
6979       && (c = get_AT_ref (die, DW_AT_signature)))
6980     {
6981       /* This is a local skeleton; use it for local references.  */
6982       ref_p = lookup_external_ref (map, c);
6983       ref_p->stub = die;
6984     }
6985
6986   /* Scan the DIE references, and remember any that refer to DIEs from
6987      other CUs (i.e. those which are not marked).  */
6988   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6989     if (AT_class (a) == dw_val_class_die_ref
6990         && (c = AT_ref (a))->die_mark == 0
6991         && is_type_die (c))
6992       {
6993         ref_p = lookup_external_ref (map, c);
6994         ref_p->n_refs++;
6995       }
6996
6997   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
6998 }
6999
7000 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7001    points to an external_ref, DATA is the CU we're processing.  If we don't
7002    already have a local stub, and we have multiple refs, build a stub.  */
7003
7004 static int
7005 build_local_stub (void **slot, void *data)
7006 {
7007   struct external_ref *ref_p = (struct external_ref *)*slot;
7008
7009   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7010     {
7011       /* We have multiple references to this type, so build a small stub.
7012          Both of these forms are a bit dodgy from the perspective of the
7013          DWARF standard, since technically they should have names.  */
7014       dw_die_ref cu = (dw_die_ref) data;
7015       dw_die_ref type = ref_p->type;
7016       dw_die_ref stub = NULL;
7017
7018       if (type->comdat_type_p)
7019         {
7020           /* If we refer to this type via sig8, use AT_signature.  */
7021           stub = new_die (type->die_tag, cu, NULL_TREE);
7022           add_AT_die_ref (stub, DW_AT_signature, type);
7023         }
7024       else
7025         {
7026           /* Otherwise, use a typedef with no name.  */
7027           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7028           add_AT_die_ref (stub, DW_AT_type, type);
7029         }
7030
7031       stub->die_mark++;
7032       ref_p->stub = stub;
7033     }
7034   return 1;
7035 }
7036
7037 /* DIE is a unit; look through all the DIE references to see if there are
7038    any external references to types, and if so, create local stubs for
7039    them which will be applied in build_abbrev_table.  This is useful because
7040    references to local DIEs are smaller.  */
7041
7042 static htab_t
7043 optimize_external_refs (dw_die_ref die)
7044 {
7045   htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7046   optimize_external_refs_1 (die, map);
7047   htab_traverse (map, build_local_stub, die);
7048   return map;
7049 }
7050
7051 /* The format of each DIE (and its attribute value pairs) is encoded in an
7052    abbreviation table.  This routine builds the abbreviation table and assigns
7053    a unique abbreviation id for each abbreviation entry.  The children of each
7054    die are visited recursively.  */
7055
7056 static void
7057 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7058 {
7059   unsigned long abbrev_id;
7060   unsigned int n_alloc;
7061   dw_die_ref c;
7062   dw_attr_ref a;
7063   unsigned ix;
7064
7065   /* Scan the DIE references, and replace any that refer to
7066      DIEs from other CUs (i.e. those which are not marked) with
7067      the local stubs we built in optimize_external_refs.  */
7068   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7069     if (AT_class (a) == dw_val_class_die_ref
7070         && (c = AT_ref (a))->die_mark == 0)
7071       {
7072         struct external_ref *ref_p;
7073         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7074
7075         ref_p = lookup_external_ref (extern_map, c);
7076         if (ref_p->stub && ref_p->stub != die)
7077           change_AT_die_ref (a, ref_p->stub);
7078         else
7079           /* We aren't changing this reference, so mark it external.  */
7080           set_AT_ref_external (a, 1);
7081       }
7082
7083   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7084     {
7085       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7086       dw_attr_ref die_a, abbrev_a;
7087       unsigned ix;
7088       bool ok = true;
7089
7090       if (abbrev->die_tag != die->die_tag)
7091         continue;
7092       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7093         continue;
7094
7095       if (VEC_length (dw_attr_node, abbrev->die_attr)
7096           != VEC_length (dw_attr_node, die->die_attr))
7097         continue;
7098
7099       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7100         {
7101           abbrev_a = &VEC_index (dw_attr_node, abbrev->die_attr, ix);
7102           if ((abbrev_a->dw_attr != die_a->dw_attr)
7103               || (value_format (abbrev_a) != value_format (die_a)))
7104             {
7105               ok = false;
7106               break;
7107             }
7108         }
7109       if (ok)
7110         break;
7111     }
7112
7113   if (abbrev_id >= abbrev_die_table_in_use)
7114     {
7115       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7116         {
7117           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7118           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7119                                             n_alloc);
7120
7121           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7122                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7123           abbrev_die_table_allocated = n_alloc;
7124         }
7125
7126       ++abbrev_die_table_in_use;
7127       abbrev_die_table[abbrev_id] = die;
7128     }
7129
7130   die->die_abbrev = abbrev_id;
7131   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7132 }
7133 \f
7134 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7135
7136 static int
7137 constant_size (unsigned HOST_WIDE_INT value)
7138 {
7139   int log;
7140
7141   if (value == 0)
7142     log = 0;
7143   else
7144     log = floor_log2 (value);
7145
7146   log = log / 8;
7147   log = 1 << (floor_log2 (log) + 1);
7148
7149   return log;
7150 }
7151
7152 /* Return the size of a DIE as it is represented in the
7153    .debug_info section.  */
7154
7155 static unsigned long
7156 size_of_die (dw_die_ref die)
7157 {
7158   unsigned long size = 0;
7159   dw_attr_ref a;
7160   unsigned ix;
7161
7162   size += size_of_uleb128 (die->die_abbrev);
7163   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7164     {
7165       switch (AT_class (a))
7166         {
7167         case dw_val_class_addr:
7168           size += DWARF2_ADDR_SIZE;
7169           break;
7170         case dw_val_class_offset:
7171           size += DWARF_OFFSET_SIZE;
7172           break;
7173         case dw_val_class_loc:
7174           {
7175             unsigned long lsize = size_of_locs (AT_loc (a));
7176
7177             /* Block length.  */
7178             if (dwarf_version >= 4)
7179               size += size_of_uleb128 (lsize);
7180             else
7181               size += constant_size (lsize);
7182             size += lsize;
7183           }
7184           break;
7185         case dw_val_class_loc_list:
7186           size += DWARF_OFFSET_SIZE;
7187           break;
7188         case dw_val_class_range_list:
7189           size += DWARF_OFFSET_SIZE;
7190           break;
7191         case dw_val_class_const:
7192           size += size_of_sleb128 (AT_int (a));
7193           break;
7194         case dw_val_class_unsigned_const:
7195           {
7196             int csize = constant_size (AT_unsigned (a));
7197             if (dwarf_version == 3
7198                 && a->dw_attr == DW_AT_data_member_location
7199                 && csize >= 4)
7200               size += size_of_uleb128 (AT_unsigned (a));
7201             else
7202               size += csize;
7203           }
7204           break;
7205         case dw_val_class_const_double:
7206           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7207           if (HOST_BITS_PER_WIDE_INT >= 64)
7208             size++; /* block */
7209           break;
7210         case dw_val_class_vec:
7211           size += constant_size (a->dw_attr_val.v.val_vec.length
7212                                  * a->dw_attr_val.v.val_vec.elt_size)
7213                   + a->dw_attr_val.v.val_vec.length
7214                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7215           break;
7216         case dw_val_class_flag:
7217           if (dwarf_version >= 4)
7218             /* Currently all add_AT_flag calls pass in 1 as last argument,
7219                so DW_FORM_flag_present can be used.  If that ever changes,
7220                we'll need to use DW_FORM_flag and have some optimization
7221                in build_abbrev_table that will change those to
7222                DW_FORM_flag_present if it is set to 1 in all DIEs using
7223                the same abbrev entry.  */
7224             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7225           else
7226             size += 1;
7227           break;
7228         case dw_val_class_die_ref:
7229           if (AT_ref_external (a))
7230             {
7231               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7232                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7233                  is sized by target address length, whereas in DWARF3
7234                  it's always sized as an offset.  */
7235               if (use_debug_types)
7236                 size += DWARF_TYPE_SIGNATURE_SIZE;
7237               else if (dwarf_version == 2)
7238                 size += DWARF2_ADDR_SIZE;
7239               else
7240                 size += DWARF_OFFSET_SIZE;
7241             }
7242           else
7243             size += DWARF_OFFSET_SIZE;
7244           break;
7245         case dw_val_class_fde_ref:
7246           size += DWARF_OFFSET_SIZE;
7247           break;
7248         case dw_val_class_lbl_id:
7249           size += DWARF2_ADDR_SIZE;
7250           break;
7251         case dw_val_class_lineptr:
7252         case dw_val_class_macptr:
7253           size += DWARF_OFFSET_SIZE;
7254           break;
7255         case dw_val_class_str:
7256           if (AT_string_form (a) == DW_FORM_strp)
7257             size += DWARF_OFFSET_SIZE;
7258           else
7259             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7260           break;
7261         case dw_val_class_file:
7262           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7263           break;
7264         case dw_val_class_data8:
7265           size += 8;
7266           break;
7267         case dw_val_class_vms_delta:
7268           size += DWARF_OFFSET_SIZE;
7269           break;
7270         case dw_val_class_high_pc:
7271           size += DWARF2_ADDR_SIZE;
7272           break;
7273         default:
7274           gcc_unreachable ();
7275         }
7276     }
7277
7278   return size;
7279 }
7280
7281 /* Size the debugging information associated with a given DIE.  Visits the
7282    DIE's children recursively.  Updates the global variable next_die_offset, on
7283    each time through.  Uses the current value of next_die_offset to update the
7284    die_offset field in each DIE.  */
7285
7286 static void
7287 calc_die_sizes (dw_die_ref die)
7288 {
7289   dw_die_ref c;
7290
7291   gcc_assert (die->die_offset == 0
7292               || (unsigned long int) die->die_offset == next_die_offset);
7293   die->die_offset = next_die_offset;
7294   next_die_offset += size_of_die (die);
7295
7296   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7297
7298   if (die->die_child != NULL)
7299     /* Count the null byte used to terminate sibling lists.  */
7300     next_die_offset += 1;
7301 }
7302
7303 /* Size just the base type children at the start of the CU.
7304    This is needed because build_abbrev needs to size locs
7305    and sizing of type based stack ops needs to know die_offset
7306    values for the base types.  */
7307
7308 static void
7309 calc_base_type_die_sizes (void)
7310 {
7311   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7312   unsigned int i;
7313   dw_die_ref base_type;
7314 #if ENABLE_ASSERT_CHECKING
7315   dw_die_ref prev = comp_unit_die ()->die_child;
7316 #endif
7317
7318   die_offset += size_of_die (comp_unit_die ());
7319   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7320     {
7321 #if ENABLE_ASSERT_CHECKING
7322       gcc_assert (base_type->die_offset == 0
7323                   && prev->die_sib == base_type
7324                   && base_type->die_child == NULL
7325                   && base_type->die_abbrev);
7326       prev = base_type;
7327 #endif
7328       base_type->die_offset = die_offset;
7329       die_offset += size_of_die (base_type);
7330     }
7331 }
7332
7333 /* Set the marks for a die and its children.  We do this so
7334    that we know whether or not a reference needs to use FORM_ref_addr; only
7335    DIEs in the same CU will be marked.  We used to clear out the offset
7336    and use that as the flag, but ran into ordering problems.  */
7337
7338 static void
7339 mark_dies (dw_die_ref die)
7340 {
7341   dw_die_ref c;
7342
7343   gcc_assert (!die->die_mark);
7344
7345   die->die_mark = 1;
7346   FOR_EACH_CHILD (die, c, mark_dies (c));
7347 }
7348
7349 /* Clear the marks for a die and its children.  */
7350
7351 static void
7352 unmark_dies (dw_die_ref die)
7353 {
7354   dw_die_ref c;
7355
7356   if (! use_debug_types)
7357     gcc_assert (die->die_mark);
7358
7359   die->die_mark = 0;
7360   FOR_EACH_CHILD (die, c, unmark_dies (c));
7361 }
7362
7363 /* Clear the marks for a die, its children and referred dies.  */
7364
7365 static void
7366 unmark_all_dies (dw_die_ref die)
7367 {
7368   dw_die_ref c;
7369   dw_attr_ref a;
7370   unsigned ix;
7371
7372   if (!die->die_mark)
7373     return;
7374   die->die_mark = 0;
7375
7376   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7377
7378   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7379     if (AT_class (a) == dw_val_class_die_ref)
7380       unmark_all_dies (AT_ref (a));
7381 }
7382
7383 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7384    generated for the compilation unit.  */
7385
7386 static unsigned long
7387 size_of_pubnames (VEC (pubname_entry, gc) * names)
7388 {
7389   unsigned long size;
7390   unsigned i;
7391   pubname_ref p;
7392
7393   size = DWARF_PUBNAMES_HEADER_SIZE;
7394   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7395     if (names != pubtype_table
7396         || p->die->die_offset != 0
7397         || !flag_eliminate_unused_debug_types)
7398       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7399
7400   size += DWARF_OFFSET_SIZE;
7401   return size;
7402 }
7403
7404 /* Return the size of the information in the .debug_aranges section.  */
7405
7406 static unsigned long
7407 size_of_aranges (void)
7408 {
7409   unsigned long size;
7410
7411   size = DWARF_ARANGES_HEADER_SIZE;
7412
7413   /* Count the address/length pair for this compilation unit.  */
7414   if (text_section_used)
7415     size += 2 * DWARF2_ADDR_SIZE;
7416   if (cold_text_section_used)
7417     size += 2 * DWARF2_ADDR_SIZE;
7418   if (have_multiple_function_sections)
7419     {
7420       unsigned fde_idx;
7421       dw_fde_ref fde;
7422
7423       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7424         {
7425           if (!fde->in_std_section)
7426             size += 2 * DWARF2_ADDR_SIZE;
7427           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7428             size += 2 * DWARF2_ADDR_SIZE;
7429         }
7430     }
7431
7432   /* Count the two zero words used to terminated the address range table.  */
7433   size += 2 * DWARF2_ADDR_SIZE;
7434   return size;
7435 }
7436 \f
7437 /* Select the encoding of an attribute value.  */
7438
7439 static enum dwarf_form
7440 value_format (dw_attr_ref a)
7441 {
7442   switch (a->dw_attr_val.val_class)
7443     {
7444     case dw_val_class_addr:
7445       /* Only very few attributes allow DW_FORM_addr.  */
7446       switch (a->dw_attr)
7447         {
7448         case DW_AT_low_pc:
7449         case DW_AT_high_pc:
7450         case DW_AT_entry_pc:
7451         case DW_AT_trampoline:
7452           return DW_FORM_addr;
7453         default:
7454           break;
7455         }
7456       switch (DWARF2_ADDR_SIZE)
7457         {
7458         case 1:
7459           return DW_FORM_data1;
7460         case 2:
7461           return DW_FORM_data2;
7462         case 4:
7463           return DW_FORM_data4;
7464         case 8:
7465           return DW_FORM_data8;
7466         default:
7467           gcc_unreachable ();
7468         }
7469     case dw_val_class_range_list:
7470     case dw_val_class_loc_list:
7471       if (dwarf_version >= 4)
7472         return DW_FORM_sec_offset;
7473       /* FALLTHRU */
7474     case dw_val_class_vms_delta:
7475     case dw_val_class_offset:
7476       switch (DWARF_OFFSET_SIZE)
7477         {
7478         case 4:
7479           return DW_FORM_data4;
7480         case 8:
7481           return DW_FORM_data8;
7482         default:
7483           gcc_unreachable ();
7484         }
7485     case dw_val_class_loc:
7486       if (dwarf_version >= 4)
7487         return DW_FORM_exprloc;
7488       switch (constant_size (size_of_locs (AT_loc (a))))
7489         {
7490         case 1:
7491           return DW_FORM_block1;
7492         case 2:
7493           return DW_FORM_block2;
7494         case 4:
7495           return DW_FORM_block4;
7496         default:
7497           gcc_unreachable ();
7498         }
7499     case dw_val_class_const:
7500       return DW_FORM_sdata;
7501     case dw_val_class_unsigned_const:
7502       switch (constant_size (AT_unsigned (a)))
7503         {
7504         case 1:
7505           return DW_FORM_data1;
7506         case 2:
7507           return DW_FORM_data2;
7508         case 4:
7509           /* In DWARF3 DW_AT_data_member_location with
7510              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7511              constant, so we need to use DW_FORM_udata if we need
7512              a large constant.  */
7513           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7514             return DW_FORM_udata;
7515           return DW_FORM_data4;
7516         case 8:
7517           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7518             return DW_FORM_udata;
7519           return DW_FORM_data8;
7520         default:
7521           gcc_unreachable ();
7522         }
7523     case dw_val_class_const_double:
7524       switch (HOST_BITS_PER_WIDE_INT)
7525         {
7526         case 8:
7527           return DW_FORM_data2;
7528         case 16:
7529           return DW_FORM_data4;
7530         case 32:
7531           return DW_FORM_data8;
7532         case 64:
7533         default:
7534           return DW_FORM_block1;
7535         }
7536     case dw_val_class_vec:
7537       switch (constant_size (a->dw_attr_val.v.val_vec.length
7538                              * a->dw_attr_val.v.val_vec.elt_size))
7539         {
7540         case 1:
7541           return DW_FORM_block1;
7542         case 2:
7543           return DW_FORM_block2;
7544         case 4:
7545           return DW_FORM_block4;
7546         default:
7547           gcc_unreachable ();
7548         }
7549     case dw_val_class_flag:
7550       if (dwarf_version >= 4)
7551         {
7552           /* Currently all add_AT_flag calls pass in 1 as last argument,
7553              so DW_FORM_flag_present can be used.  If that ever changes,
7554              we'll need to use DW_FORM_flag and have some optimization
7555              in build_abbrev_table that will change those to
7556              DW_FORM_flag_present if it is set to 1 in all DIEs using
7557              the same abbrev entry.  */
7558           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7559           return DW_FORM_flag_present;
7560         }
7561       return DW_FORM_flag;
7562     case dw_val_class_die_ref:
7563       if (AT_ref_external (a))
7564         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7565       else
7566         return DW_FORM_ref;
7567     case dw_val_class_fde_ref:
7568       return DW_FORM_data;
7569     case dw_val_class_lbl_id:
7570       return DW_FORM_addr;
7571     case dw_val_class_lineptr:
7572     case dw_val_class_macptr:
7573       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7574     case dw_val_class_str:
7575       return AT_string_form (a);
7576     case dw_val_class_file:
7577       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7578         {
7579         case 1:
7580           return DW_FORM_data1;
7581         case 2:
7582           return DW_FORM_data2;
7583         case 4:
7584           return DW_FORM_data4;
7585         default:
7586           gcc_unreachable ();
7587         }
7588
7589     case dw_val_class_data8:
7590       return DW_FORM_data8;
7591
7592     case dw_val_class_high_pc:
7593       switch (DWARF2_ADDR_SIZE)
7594         {
7595           case 4:
7596             return DW_FORM_data4;
7597           case 8:
7598             return DW_FORM_data8;
7599           default:
7600             gcc_unreachable ();
7601         }
7602
7603     default:
7604       gcc_unreachable ();
7605     }
7606 }
7607
7608 /* Output the encoding of an attribute value.  */
7609
7610 static void
7611 output_value_format (dw_attr_ref a)
7612 {
7613   enum dwarf_form form = value_format (a);
7614
7615   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7616 }
7617
7618 /* Output the .debug_abbrev section which defines the DIE abbreviation
7619    table.  */
7620
7621 static void
7622 output_abbrev_section (void)
7623 {
7624   unsigned long abbrev_id;
7625
7626   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7627     {
7628       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7629       unsigned ix;
7630       dw_attr_ref a_attr;
7631
7632       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7633       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7634                                    dwarf_tag_name (abbrev->die_tag));
7635
7636       if (abbrev->die_child != NULL)
7637         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7638       else
7639         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7640
7641       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7642            ix++)
7643         {
7644           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7645                                        dwarf_attr_name (a_attr->dw_attr));
7646           output_value_format (a_attr);
7647         }
7648
7649       dw2_asm_output_data (1, 0, NULL);
7650       dw2_asm_output_data (1, 0, NULL);
7651     }
7652
7653   /* Terminate the table.  */
7654   dw2_asm_output_data (1, 0, NULL);
7655 }
7656
7657 /* Output a symbol we can use to refer to this DIE from another CU.  */
7658
7659 static inline void
7660 output_die_symbol (dw_die_ref die)
7661 {
7662   const char *sym = die->die_id.die_symbol;
7663
7664   gcc_assert (!die->comdat_type_p);
7665
7666   if (sym == 0)
7667     return;
7668
7669   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7670     /* We make these global, not weak; if the target doesn't support
7671        .linkonce, it doesn't support combining the sections, so debugging
7672        will break.  */
7673     targetm.asm_out.globalize_label (asm_out_file, sym);
7674
7675   ASM_OUTPUT_LABEL (asm_out_file, sym);
7676 }
7677
7678 /* Return a new location list, given the begin and end range, and the
7679    expression.  */
7680
7681 static inline dw_loc_list_ref
7682 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7683               const char *section)
7684 {
7685   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
7686
7687   retlist->begin = begin;
7688   retlist->end = end;
7689   retlist->expr = expr;
7690   retlist->section = section;
7691
7692   return retlist;
7693 }
7694
7695 /* Generate a new internal symbol for this location list node, if it
7696    hasn't got one yet.  */
7697
7698 static inline void
7699 gen_llsym (dw_loc_list_ref list)
7700 {
7701   gcc_assert (!list->ll_symbol);
7702   list->ll_symbol = gen_internal_sym ("LLST");
7703 }
7704
7705 /* Output the location list given to us.  */
7706
7707 static void
7708 output_loc_list (dw_loc_list_ref list_head)
7709 {
7710   dw_loc_list_ref curr = list_head;
7711
7712   if (list_head->emitted)
7713     return;
7714   list_head->emitted = true;
7715
7716   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7717
7718   /* Walk the location list, and output each range + expression.  */
7719   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7720     {
7721       unsigned long size;
7722       /* Don't output an entry that starts and ends at the same address.  */
7723       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
7724         continue;
7725       size = size_of_locs (curr->expr);
7726       /* If the expression is too large, drop it on the floor.  We could
7727          perhaps put it into DW_TAG_dwarf_procedure and refer to that
7728          in the expression, but >= 64KB expressions for a single value
7729          in a single range are unlikely very useful.  */
7730       if (size > 0xffff)
7731         continue;
7732       if (!have_multiple_function_sections)
7733         {
7734           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7735                                 "Location list begin address (%s)",
7736                                 list_head->ll_symbol);
7737           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7738                                 "Location list end address (%s)",
7739                                 list_head->ll_symbol);
7740         }
7741       else
7742         {
7743           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7744                                "Location list begin address (%s)",
7745                                list_head->ll_symbol);
7746           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7747                                "Location list end address (%s)",
7748                                list_head->ll_symbol);
7749         }
7750
7751       /* Output the block length for this list of location operations.  */
7752       gcc_assert (size <= 0xffff);
7753       dw2_asm_output_data (2, size, "%s", "Location expression size");
7754
7755       output_loc_sequence (curr->expr, -1);
7756     }
7757
7758   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7759                        "Location list terminator begin (%s)",
7760                        list_head->ll_symbol);
7761   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7762                        "Location list terminator end (%s)",
7763                        list_head->ll_symbol);
7764 }
7765
7766 /* Output a type signature.  */
7767
7768 static inline void
7769 output_signature (const char *sig, const char *name)
7770 {
7771   int i;
7772
7773   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7774     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
7775 }
7776
7777 /* Output the DIE and its attributes.  Called recursively to generate
7778    the definitions of each child DIE.  */
7779
7780 static void
7781 output_die (dw_die_ref die)
7782 {
7783   dw_attr_ref a;
7784   dw_die_ref c;
7785   unsigned long size;
7786   unsigned ix;
7787
7788   /* If someone in another CU might refer to us, set up a symbol for
7789      them to point to.  */
7790   if (! die->comdat_type_p && die->die_id.die_symbol)
7791     output_die_symbol (die);
7792
7793   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
7794                                (unsigned long)die->die_offset,
7795                                dwarf_tag_name (die->die_tag));
7796
7797   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7798     {
7799       const char *name = dwarf_attr_name (a->dw_attr);
7800
7801       switch (AT_class (a))
7802         {
7803         case dw_val_class_addr:
7804           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7805           break;
7806
7807         case dw_val_class_offset:
7808           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7809                                "%s", name);
7810           break;
7811
7812         case dw_val_class_range_list:
7813           {
7814             char *p = strchr (ranges_section_label, '\0');
7815
7816             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7817                      a->dw_attr_val.v.val_offset);
7818             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7819                                    debug_ranges_section, "%s", name);
7820             *p = '\0';
7821           }
7822           break;
7823
7824         case dw_val_class_loc:
7825           size = size_of_locs (AT_loc (a));
7826
7827           /* Output the block length for this list of location operations.  */
7828           if (dwarf_version >= 4)
7829             dw2_asm_output_data_uleb128 (size, "%s", name);
7830           else
7831             dw2_asm_output_data (constant_size (size), size, "%s", name);
7832
7833           output_loc_sequence (AT_loc (a), -1);
7834           break;
7835
7836         case dw_val_class_const:
7837           /* ??? It would be slightly more efficient to use a scheme like is
7838              used for unsigned constants below, but gdb 4.x does not sign
7839              extend.  Gdb 5.x does sign extend.  */
7840           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7841           break;
7842
7843         case dw_val_class_unsigned_const:
7844           {
7845             int csize = constant_size (AT_unsigned (a));
7846             if (dwarf_version == 3
7847                 && a->dw_attr == DW_AT_data_member_location
7848                 && csize >= 4)
7849               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
7850             else
7851               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
7852           }
7853           break;
7854
7855         case dw_val_class_const_double:
7856           {
7857             unsigned HOST_WIDE_INT first, second;
7858
7859             if (HOST_BITS_PER_WIDE_INT >= 64)
7860               dw2_asm_output_data (1,
7861                                    HOST_BITS_PER_DOUBLE_INT
7862                                    / HOST_BITS_PER_CHAR,
7863                                    NULL);
7864
7865             if (WORDS_BIG_ENDIAN)
7866               {
7867                 first = a->dw_attr_val.v.val_double.high;
7868                 second = a->dw_attr_val.v.val_double.low;
7869               }
7870             else
7871               {
7872                 first = a->dw_attr_val.v.val_double.low;
7873                 second = a->dw_attr_val.v.val_double.high;
7874               }
7875
7876             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7877                                  first, name);
7878             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7879                                  second, NULL);
7880           }
7881           break;
7882
7883         case dw_val_class_vec:
7884           {
7885             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7886             unsigned int len = a->dw_attr_val.v.val_vec.length;
7887             unsigned int i;
7888             unsigned char *p;
7889
7890             dw2_asm_output_data (constant_size (len * elt_size),
7891                                  len * elt_size, "%s", name);
7892             if (elt_size > sizeof (HOST_WIDE_INT))
7893               {
7894                 elt_size /= 2;
7895                 len *= 2;
7896               }
7897             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7898                  i < len;
7899                  i++, p += elt_size)
7900               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7901                                    "fp or vector constant word %u", i);
7902             break;
7903           }
7904
7905         case dw_val_class_flag:
7906           if (dwarf_version >= 4)
7907             {
7908               /* Currently all add_AT_flag calls pass in 1 as last argument,
7909                  so DW_FORM_flag_present can be used.  If that ever changes,
7910                  we'll need to use DW_FORM_flag and have some optimization
7911                  in build_abbrev_table that will change those to
7912                  DW_FORM_flag_present if it is set to 1 in all DIEs using
7913                  the same abbrev entry.  */
7914               gcc_assert (AT_flag (a) == 1);
7915               if (flag_debug_asm)
7916                 fprintf (asm_out_file, "\t\t\t%s %s\n",
7917                          ASM_COMMENT_START, name);
7918               break;
7919             }
7920           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7921           break;
7922
7923         case dw_val_class_loc_list:
7924           {
7925             char *sym = AT_loc_list (a)->ll_symbol;
7926
7927             gcc_assert (sym);
7928             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7929                                    "%s", name);
7930           }
7931           break;
7932
7933         case dw_val_class_die_ref:
7934           if (AT_ref_external (a))
7935             {
7936               if (AT_ref (a)->comdat_type_p)
7937                 {
7938                   comdat_type_node_ref type_node =
7939                     AT_ref (a)->die_id.die_type_node;
7940
7941                   gcc_assert (type_node);
7942                   output_signature (type_node->signature, name);
7943                 }
7944               else
7945                 {
7946                   const char *sym = AT_ref (a)->die_id.die_symbol;
7947                   int size;
7948
7949                   gcc_assert (sym);
7950                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
7951                      length, whereas in DWARF3 it's always sized as an
7952                      offset.  */
7953                   if (dwarf_version == 2)
7954                     size = DWARF2_ADDR_SIZE;
7955                   else
7956                     size = DWARF_OFFSET_SIZE;
7957                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
7958                                          name);
7959                 }
7960             }
7961           else
7962             {
7963               gcc_assert (AT_ref (a)->die_offset);
7964               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7965                                    "%s", name);
7966             }
7967           break;
7968
7969         case dw_val_class_fde_ref:
7970           {
7971             char l1[20];
7972
7973             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7974                                          a->dw_attr_val.v.val_fde_index * 2);
7975             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7976                                    "%s", name);
7977           }
7978           break;
7979
7980         case dw_val_class_vms_delta:
7981           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
7982                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
7983                                     "%s", name);
7984           break;
7985
7986         case dw_val_class_lbl_id:
7987           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7988           break;
7989
7990         case dw_val_class_lineptr:
7991           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7992                                  debug_line_section, "%s", name);
7993           break;
7994
7995         case dw_val_class_macptr:
7996           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7997                                  debug_macinfo_section, "%s", name);
7998           break;
7999
8000         case dw_val_class_str:
8001           if (AT_string_form (a) == DW_FORM_strp)
8002             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8003                                    a->dw_attr_val.v.val_str->label,
8004                                    debug_str_section,
8005                                    "%s: \"%s\"", name, AT_string (a));
8006           else
8007             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8008           break;
8009
8010         case dw_val_class_file:
8011           {
8012             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8013
8014             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8015                                  a->dw_attr_val.v.val_file->filename);
8016             break;
8017           }
8018
8019         case dw_val_class_data8:
8020           {
8021             int i;
8022
8023             for (i = 0; i < 8; i++)
8024               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8025                                    i == 0 ? "%s" : NULL, name);
8026             break;
8027           }
8028
8029         case dw_val_class_high_pc:
8030           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8031                                 get_AT_low_pc (die), "DW_AT_high_pc");
8032           break;
8033
8034         default:
8035           gcc_unreachable ();
8036         }
8037     }
8038
8039   FOR_EACH_CHILD (die, c, output_die (c));
8040
8041   /* Add null byte to terminate sibling list.  */
8042   if (die->die_child != NULL)
8043     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8044                          (unsigned long) die->die_offset);
8045 }
8046
8047 /* Output the compilation unit that appears at the beginning of the
8048    .debug_info section, and precedes the DIE descriptions.  */
8049
8050 static void
8051 output_compilation_unit_header (void)
8052 {
8053   int ver = dwarf_version;
8054
8055   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8056     dw2_asm_output_data (4, 0xffffffff,
8057       "Initial length escape value indicating 64-bit DWARF extension");
8058   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8059                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8060                        "Length of Compilation Unit Info");
8061   dw2_asm_output_data (2, ver, "DWARF version number");
8062   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8063                          debug_abbrev_section,
8064                          "Offset Into Abbrev. Section");
8065   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8066 }
8067
8068 /* Output the compilation unit DIE and its children.  */
8069
8070 static void
8071 output_comp_unit (dw_die_ref die, int output_if_empty)
8072 {
8073   const char *secname, *oldsym;
8074   char *tmp;
8075   htab_t extern_map;
8076
8077   /* Unless we are outputting main CU, we may throw away empty ones.  */
8078   if (!output_if_empty && die->die_child == NULL)
8079     return;
8080
8081   /* Even if there are no children of this DIE, we must output the information
8082      about the compilation unit.  Otherwise, on an empty translation unit, we
8083      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8084      will then complain when examining the file.  First mark all the DIEs in
8085      this CU so we know which get local refs.  */
8086   mark_dies (die);
8087
8088   extern_map = optimize_external_refs (die);
8089
8090   build_abbrev_table (die, extern_map);
8091
8092   htab_delete (extern_map);
8093
8094   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8095   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8096   calc_die_sizes (die);
8097
8098   oldsym = die->die_id.die_symbol;
8099   if (oldsym)
8100     {
8101       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8102
8103       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8104       secname = tmp;
8105       die->die_id.die_symbol = NULL;
8106       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8107     }
8108   else
8109     {
8110       switch_to_section (debug_info_section);
8111       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8112       info_section_emitted = true;
8113     }
8114
8115   /* Output debugging information.  */
8116   output_compilation_unit_header ();
8117   output_die (die);
8118
8119   /* Leave the marks on the main CU, so we can check them in
8120      output_pubnames.  */
8121   if (oldsym)
8122     {
8123       unmark_dies (die);
8124       die->die_id.die_symbol = oldsym;
8125     }
8126 }
8127
8128 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8129    and .debug_pubtypes.  This is configured per-target, but can be
8130    overridden by the -gpubnames or -gno-pubnames options.  */
8131
8132 static inline bool
8133 want_pubnames (void)
8134 {
8135   return (debug_generate_pub_sections != -1
8136           ? debug_generate_pub_sections
8137           : targetm.want_debug_pub_sections);
8138 }
8139
8140 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
8141
8142 static void
8143 add_AT_pubnames (dw_die_ref die)
8144 {
8145   if (want_pubnames ())
8146     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8147 }
8148
8149 /* Output a comdat type unit DIE and its children.  */
8150
8151 static void
8152 output_comdat_type_unit (comdat_type_node *node)
8153 {
8154   const char *secname;
8155   char *tmp;
8156   int i;
8157 #if defined (OBJECT_FORMAT_ELF)
8158   tree comdat_key;
8159 #endif
8160   htab_t extern_map;
8161
8162   /* First mark all the DIEs in this CU so we know which get local refs.  */
8163   mark_dies (node->root_die);
8164
8165   extern_map = optimize_external_refs (node->root_die);
8166
8167   build_abbrev_table (node->root_die, extern_map);
8168
8169   htab_delete (extern_map);
8170
8171   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8172   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8173   calc_die_sizes (node->root_die);
8174
8175 #if defined (OBJECT_FORMAT_ELF)
8176   secname = ".debug_types";
8177   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8178   sprintf (tmp, "wt.");
8179   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8180     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8181   comdat_key = get_identifier (tmp);
8182   targetm.asm_out.named_section (secname,
8183                                  SECTION_DEBUG | SECTION_LINKONCE,
8184                                  comdat_key);
8185 #else
8186   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8187   sprintf (tmp, ".gnu.linkonce.wt.");
8188   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8189     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8190   secname = tmp;
8191   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8192 #endif
8193
8194   /* Output debugging information.  */
8195   output_compilation_unit_header ();
8196   output_signature (node->signature, "Type Signature");
8197   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8198                        "Offset to Type DIE");
8199   output_die (node->root_die);
8200
8201   unmark_dies (node->root_die);
8202 }
8203
8204 /* Return the DWARF2/3 pubname associated with a decl.  */
8205
8206 static const char *
8207 dwarf2_name (tree decl, int scope)
8208 {
8209   if (DECL_NAMELESS (decl))
8210     return NULL;
8211   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8212 }
8213
8214 /* Add a new entry to .debug_pubnames if appropriate.  */
8215
8216 static void
8217 add_pubname_string (const char *str, dw_die_ref die)
8218 {
8219   pubname_entry e;
8220
8221   e.die = die;
8222   e.name = xstrdup (str);
8223   VEC_safe_push (pubname_entry, gc, pubname_table, e);
8224 }
8225
8226 static void
8227 add_pubname (tree decl, dw_die_ref die)
8228 {
8229   if (!want_pubnames ())
8230     return;
8231
8232   /* Don't add items to the table when we expect that the consumer will have
8233      just read the enclosing die.  For example, if the consumer is looking at a
8234      class_member, it will either be inside the class already, or will have just
8235      looked up the class to find the member.  Either way, searching the class is
8236      faster than searching the index.  */
8237   if ((TREE_PUBLIC (decl) && !is_class_die (die->die_parent))
8238       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8239     {
8240       const char *name = dwarf2_name (decl, 1);
8241
8242       if (name)
8243         add_pubname_string (name, die);
8244     }
8245 }
8246
8247 /* Add an enumerator to the pubnames section.  */
8248
8249 static void
8250 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8251 {
8252   pubname_entry e;
8253
8254   gcc_assert (scope_name);
8255   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8256   e.die = die;
8257   VEC_safe_push (pubname_entry, gc, pubname_table, e);
8258 }
8259
8260 /* Add a new entry to .debug_pubtypes if appropriate.  */
8261
8262 static void
8263 add_pubtype (tree decl, dw_die_ref die)
8264 {
8265   pubname_entry e;
8266
8267   if (!want_pubnames ())
8268     return;
8269
8270   if ((TREE_PUBLIC (decl)
8271        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8272       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8273     {
8274       tree scope = NULL;
8275       const char *scope_name = "";
8276       const char *sep = is_cxx () ? "::" : ".";
8277       const char *name;
8278
8279       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8280       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8281         {
8282           scope_name = lang_hooks.dwarf_name (scope, 1);
8283           if (scope_name != NULL && scope_name[0] != '\0')
8284             scope_name = concat (scope_name, sep, NULL);
8285           else
8286             scope_name = "";
8287         }
8288
8289       if (TYPE_P (decl))
8290         name = type_tag (decl);
8291       else
8292         name = lang_hooks.dwarf_name (decl, 1);
8293
8294       /* If we don't have a name for the type, there's no point in adding
8295          it to the table.  */
8296       if (name != NULL && name[0] != '\0')
8297         {
8298           e.die = die;
8299           e.name = concat (scope_name, name, NULL);
8300           VEC_safe_push (pubname_entry, gc, pubtype_table, e);
8301         }
8302
8303       /* Although it might be more consistent to add the pubinfo for the
8304          enumerators as their dies are created, they should only be added if the
8305          enum type meets the criteria above.  So rather than re-check the parent
8306          enum type whenever an enumerator die is created, just output them all
8307          here.  This isn't protected by the name conditional because anonymous
8308          enums don't have names.  */
8309       if (die->die_tag == DW_TAG_enumeration_type)
8310         {
8311           dw_die_ref c;
8312
8313           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8314         }
8315     }
8316 }
8317
8318 /* Output the public names table used to speed up access to externally
8319    visible names; or the public types table used to find type definitions.  */
8320
8321 static void
8322 output_pubnames (VEC (pubname_entry, gc) * names)
8323 {
8324   unsigned i;
8325   unsigned long pubnames_length = size_of_pubnames (names);
8326   pubname_ref pub;
8327
8328   if (!want_pubnames () || !info_section_emitted)
8329     return;
8330   if (names == pubname_table)
8331     switch_to_section (debug_pubnames_section);
8332   else
8333     switch_to_section (debug_pubtypes_section);
8334   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8335     dw2_asm_output_data (4, 0xffffffff,
8336       "Initial length escape value indicating 64-bit DWARF extension");
8337   if (names == pubname_table)
8338     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8339                          "Length of Public Names Info");
8340   else
8341     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8342                          "Length of Public Type Names Info");
8343   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8344   dw2_asm_output_data (2, 2, "DWARF Version");
8345   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8346                          debug_info_section,
8347                          "Offset of Compilation Unit Info");
8348   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8349                        "Compilation Unit Length");
8350
8351   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8352     {
8353       /* Enumerator names are part of the pubname table, but the parent
8354          DW_TAG_enumeration_type die may have been pruned.  Don't output
8355          them if that is the case.  */
8356       if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
8357         continue;
8358
8359       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8360       if (names == pubname_table)
8361         gcc_assert (pub->die->die_mark);
8362
8363       if (names != pubtype_table
8364           || pub->die->die_offset != 0
8365           || !flag_eliminate_unused_debug_types)
8366         {
8367           dw_offset die_offset = pub->die->die_offset;
8368
8369           /* If we're putting types in their own .debug_types sections,
8370              the .debug_pubtypes table will still point to the compile
8371              unit (not the type unit), so we want to use the offset of
8372              the skeleton DIE (if there is one).  */
8373           if (pub->die->comdat_type_p && names == pubtype_table)
8374             {
8375               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
8376
8377               if (type_node != NULL)
8378                 die_offset = (type_node->skeleton_die != NULL
8379                               ? type_node->skeleton_die->die_offset
8380                               : 0);
8381             }
8382
8383           dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
8384
8385           dw2_asm_output_nstring (pub->name, -1, "external name");
8386         }
8387     }
8388
8389   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8390 }
8391
8392 /* Output the information that goes into the .debug_aranges table.
8393    Namely, define the beginning and ending address range of the
8394    text section generated for this compilation unit.  */
8395
8396 static void
8397 output_aranges (unsigned long aranges_length)
8398 {
8399   unsigned i;
8400
8401   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8402     dw2_asm_output_data (4, 0xffffffff,
8403       "Initial length escape value indicating 64-bit DWARF extension");
8404   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8405                        "Length of Address Ranges Info");
8406   /* Version number for aranges is still 2, even in DWARF3.  */
8407   dw2_asm_output_data (2, 2, "DWARF Version");
8408   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8409                          debug_info_section,
8410                          "Offset of Compilation Unit Info");
8411   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8412   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8413
8414   /* We need to align to twice the pointer size here.  */
8415   if (DWARF_ARANGES_PAD_SIZE)
8416     {
8417       /* Pad using a 2 byte words so that padding is correct for any
8418          pointer size.  */
8419       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8420                            2 * DWARF2_ADDR_SIZE);
8421       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8422         dw2_asm_output_data (2, 0, NULL);
8423     }
8424
8425   /* It is necessary not to output these entries if the sections were
8426      not used; if the sections were not used, the length will be 0 and
8427      the address may end up as 0 if the section is discarded by ld
8428      --gc-sections, leaving an invalid (0, 0) entry that can be
8429      confused with the terminator.  */
8430   if (text_section_used)
8431     {
8432       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8433       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8434                             text_section_label, "Length");
8435     }
8436   if (cold_text_section_used)
8437     {
8438       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8439                            "Address");
8440       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8441                             cold_text_section_label, "Length");
8442     }
8443
8444   if (have_multiple_function_sections)
8445     {
8446       unsigned fde_idx;
8447       dw_fde_ref fde;
8448
8449       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8450         {
8451           if (DECL_IGNORED_P (fde->decl))
8452             continue;
8453           if (!fde->in_std_section)
8454             {
8455               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8456                                    "Address");
8457               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8458                                     fde->dw_fde_begin, "Length");
8459             }
8460           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8461             {
8462               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8463                                    "Address");
8464               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8465                                     fde->dw_fde_second_begin, "Length");
8466             }
8467         }
8468     }
8469
8470   /* Output the terminator words.  */
8471   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8472   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8473 }
8474
8475 /* Add a new entry to .debug_ranges.  Return the offset at which it
8476    was placed.  */
8477
8478 static unsigned int
8479 add_ranges_num (int num)
8480 {
8481   unsigned int in_use = ranges_table_in_use;
8482
8483   if (in_use == ranges_table_allocated)
8484     {
8485       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8486       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8487                                     ranges_table_allocated);
8488       memset (ranges_table + ranges_table_in_use, 0,
8489               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8490     }
8491
8492   ranges_table[in_use].num = num;
8493   ranges_table_in_use = in_use + 1;
8494
8495   return in_use * 2 * DWARF2_ADDR_SIZE;
8496 }
8497
8498 /* Add a new entry to .debug_ranges corresponding to a block, or a
8499    range terminator if BLOCK is NULL.  */
8500
8501 static unsigned int
8502 add_ranges (const_tree block)
8503 {
8504   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8505 }
8506
8507 /* Add a new entry to .debug_ranges corresponding to a pair of
8508    labels.  */
8509
8510 static void
8511 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8512                       bool *added)
8513 {
8514   unsigned int in_use = ranges_by_label_in_use;
8515   unsigned int offset;
8516
8517   if (in_use == ranges_by_label_allocated)
8518     {
8519       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8520       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8521                                        ranges_by_label,
8522                                        ranges_by_label_allocated);
8523       memset (ranges_by_label + ranges_by_label_in_use, 0,
8524               RANGES_TABLE_INCREMENT
8525               * sizeof (struct dw_ranges_by_label_struct));
8526     }
8527
8528   ranges_by_label[in_use].begin = begin;
8529   ranges_by_label[in_use].end = end;
8530   ranges_by_label_in_use = in_use + 1;
8531
8532   offset = add_ranges_num (-(int)in_use - 1);
8533   if (!*added)
8534     {
8535       add_AT_range_list (die, DW_AT_ranges, offset);
8536       *added = true;
8537     }
8538 }
8539
8540 static void
8541 output_ranges (void)
8542 {
8543   unsigned i;
8544   static const char *const start_fmt = "Offset %#x";
8545   const char *fmt = start_fmt;
8546
8547   for (i = 0; i < ranges_table_in_use; i++)
8548     {
8549       int block_num = ranges_table[i].num;
8550
8551       if (block_num > 0)
8552         {
8553           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8554           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8555
8556           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8557           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8558
8559           /* If all code is in the text section, then the compilation
8560              unit base address defaults to DW_AT_low_pc, which is the
8561              base of the text section.  */
8562           if (!have_multiple_function_sections)
8563             {
8564               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8565                                     text_section_label,
8566                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8567               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8568                                     text_section_label, NULL);
8569             }
8570
8571           /* Otherwise, the compilation unit base address is zero,
8572              which allows us to use absolute addresses, and not worry
8573              about whether the target supports cross-section
8574              arithmetic.  */
8575           else
8576             {
8577               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8578                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8579               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8580             }
8581
8582           fmt = NULL;
8583         }
8584
8585       /* Negative block_num stands for an index into ranges_by_label.  */
8586       else if (block_num < 0)
8587         {
8588           int lab_idx = - block_num - 1;
8589
8590           if (!have_multiple_function_sections)
8591             {
8592               gcc_unreachable ();
8593 #if 0
8594               /* If we ever use add_ranges_by_labels () for a single
8595                  function section, all we have to do is to take out
8596                  the #if 0 above.  */
8597               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8598                                     ranges_by_label[lab_idx].begin,
8599                                     text_section_label,
8600                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8601               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8602                                     ranges_by_label[lab_idx].end,
8603                                     text_section_label, NULL);
8604 #endif
8605             }
8606           else
8607             {
8608               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8609                                    ranges_by_label[lab_idx].begin,
8610                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8611               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8612                                    ranges_by_label[lab_idx].end,
8613                                    NULL);
8614             }
8615         }
8616       else
8617         {
8618           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8619           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8620           fmt = start_fmt;
8621         }
8622     }
8623 }
8624
8625 /* Data structure containing information about input files.  */
8626 struct file_info
8627 {
8628   const char *path;     /* Complete file name.  */
8629   const char *fname;    /* File name part.  */
8630   int length;           /* Length of entire string.  */
8631   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8632   int dir_idx;          /* Index in directory table.  */
8633 };
8634
8635 /* Data structure containing information about directories with source
8636    files.  */
8637 struct dir_info
8638 {
8639   const char *path;     /* Path including directory name.  */
8640   int length;           /* Path length.  */
8641   int prefix;           /* Index of directory entry which is a prefix.  */
8642   int count;            /* Number of files in this directory.  */
8643   int dir_idx;          /* Index of directory used as base.  */
8644 };
8645
8646 /* Callback function for file_info comparison.  We sort by looking at
8647    the directories in the path.  */
8648
8649 static int
8650 file_info_cmp (const void *p1, const void *p2)
8651 {
8652   const struct file_info *const s1 = (const struct file_info *) p1;
8653   const struct file_info *const s2 = (const struct file_info *) p2;
8654   const unsigned char *cp1;
8655   const unsigned char *cp2;
8656
8657   /* Take care of file names without directories.  We need to make sure that
8658      we return consistent values to qsort since some will get confused if
8659      we return the same value when identical operands are passed in opposite
8660      orders.  So if neither has a directory, return 0 and otherwise return
8661      1 or -1 depending on which one has the directory.  */
8662   if ((s1->path == s1->fname || s2->path == s2->fname))
8663     return (s2->path == s2->fname) - (s1->path == s1->fname);
8664
8665   cp1 = (const unsigned char *) s1->path;
8666   cp2 = (const unsigned char *) s2->path;
8667
8668   while (1)
8669     {
8670       ++cp1;
8671       ++cp2;
8672       /* Reached the end of the first path?  If so, handle like above.  */
8673       if ((cp1 == (const unsigned char *) s1->fname)
8674           || (cp2 == (const unsigned char *) s2->fname))
8675         return ((cp2 == (const unsigned char *) s2->fname)
8676                 - (cp1 == (const unsigned char *) s1->fname));
8677
8678       /* Character of current path component the same?  */
8679       else if (*cp1 != *cp2)
8680         return *cp1 - *cp2;
8681     }
8682 }
8683
8684 struct file_name_acquire_data
8685 {
8686   struct file_info *files;
8687   int used_files;
8688   int max_files;
8689 };
8690
8691 /* Traversal function for the hash table.  */
8692
8693 static int
8694 file_name_acquire (void ** slot, void *data)
8695 {
8696   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8697   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8698   struct file_info *fi;
8699   const char *f;
8700
8701   gcc_assert (fnad->max_files >= d->emitted_number);
8702
8703   if (! d->emitted_number)
8704     return 1;
8705
8706   gcc_assert (fnad->max_files != fnad->used_files);
8707
8708   fi = fnad->files + fnad->used_files++;
8709
8710   /* Skip all leading "./".  */
8711   f = d->filename;
8712   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8713     f += 2;
8714
8715   /* Create a new array entry.  */
8716   fi->path = f;
8717   fi->length = strlen (f);
8718   fi->file_idx = d;
8719
8720   /* Search for the file name part.  */
8721   f = strrchr (f, DIR_SEPARATOR);
8722 #if defined (DIR_SEPARATOR_2)
8723   {
8724     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8725
8726     if (g != NULL)
8727       {
8728         if (f == NULL || f < g)
8729           f = g;
8730       }
8731   }
8732 #endif
8733
8734   fi->fname = f == NULL ? fi->path : f + 1;
8735   return 1;
8736 }
8737
8738 /* Output the directory table and the file name table.  We try to minimize
8739    the total amount of memory needed.  A heuristic is used to avoid large
8740    slowdowns with many input files.  */
8741
8742 static void
8743 output_file_names (void)
8744 {
8745   struct file_name_acquire_data fnad;
8746   int numfiles;
8747   struct file_info *files;
8748   struct dir_info *dirs;
8749   int *saved;
8750   int *savehere;
8751   int *backmap;
8752   int ndirs;
8753   int idx_offset;
8754   int i;
8755
8756   if (!last_emitted_file)
8757     {
8758       dw2_asm_output_data (1, 0, "End directory table");
8759       dw2_asm_output_data (1, 0, "End file name table");
8760       return;
8761     }
8762
8763   numfiles = last_emitted_file->emitted_number;
8764
8765   /* Allocate the various arrays we need.  */
8766   files = XALLOCAVEC (struct file_info, numfiles);
8767   dirs = XALLOCAVEC (struct dir_info, numfiles);
8768
8769   fnad.files = files;
8770   fnad.used_files = 0;
8771   fnad.max_files = numfiles;
8772   htab_traverse (file_table, file_name_acquire, &fnad);
8773   gcc_assert (fnad.used_files == fnad.max_files);
8774
8775   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8776
8777   /* Find all the different directories used.  */
8778   dirs[0].path = files[0].path;
8779   dirs[0].length = files[0].fname - files[0].path;
8780   dirs[0].prefix = -1;
8781   dirs[0].count = 1;
8782   dirs[0].dir_idx = 0;
8783   files[0].dir_idx = 0;
8784   ndirs = 1;
8785
8786   for (i = 1; i < numfiles; i++)
8787     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8788         && memcmp (dirs[ndirs - 1].path, files[i].path,
8789                    dirs[ndirs - 1].length) == 0)
8790       {
8791         /* Same directory as last entry.  */
8792         files[i].dir_idx = ndirs - 1;
8793         ++dirs[ndirs - 1].count;
8794       }
8795     else
8796       {
8797         int j;
8798
8799         /* This is a new directory.  */
8800         dirs[ndirs].path = files[i].path;
8801         dirs[ndirs].length = files[i].fname - files[i].path;
8802         dirs[ndirs].count = 1;
8803         dirs[ndirs].dir_idx = ndirs;
8804         files[i].dir_idx = ndirs;
8805
8806         /* Search for a prefix.  */
8807         dirs[ndirs].prefix = -1;
8808         for (j = 0; j < ndirs; j++)
8809           if (dirs[j].length < dirs[ndirs].length
8810               && dirs[j].length > 1
8811               && (dirs[ndirs].prefix == -1
8812                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8813               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8814             dirs[ndirs].prefix = j;
8815
8816         ++ndirs;
8817       }
8818
8819   /* Now to the actual work.  We have to find a subset of the directories which
8820      allow expressing the file name using references to the directory table
8821      with the least amount of characters.  We do not do an exhaustive search
8822      where we would have to check out every combination of every single
8823      possible prefix.  Instead we use a heuristic which provides nearly optimal
8824      results in most cases and never is much off.  */
8825   saved = XALLOCAVEC (int, ndirs);
8826   savehere = XALLOCAVEC (int, ndirs);
8827
8828   memset (saved, '\0', ndirs * sizeof (saved[0]));
8829   for (i = 0; i < ndirs; i++)
8830     {
8831       int j;
8832       int total;
8833
8834       /* We can always save some space for the current directory.  But this
8835          does not mean it will be enough to justify adding the directory.  */
8836       savehere[i] = dirs[i].length;
8837       total = (savehere[i] - saved[i]) * dirs[i].count;
8838
8839       for (j = i + 1; j < ndirs; j++)
8840         {
8841           savehere[j] = 0;
8842           if (saved[j] < dirs[i].length)
8843             {
8844               /* Determine whether the dirs[i] path is a prefix of the
8845                  dirs[j] path.  */
8846               int k;
8847
8848               k = dirs[j].prefix;
8849               while (k != -1 && k != (int) i)
8850                 k = dirs[k].prefix;
8851
8852               if (k == (int) i)
8853                 {
8854                   /* Yes it is.  We can possibly save some memory by
8855                      writing the filenames in dirs[j] relative to
8856                      dirs[i].  */
8857                   savehere[j] = dirs[i].length;
8858                   total += (savehere[j] - saved[j]) * dirs[j].count;
8859                 }
8860             }
8861         }
8862
8863       /* Check whether we can save enough to justify adding the dirs[i]
8864          directory.  */
8865       if (total > dirs[i].length + 1)
8866         {
8867           /* It's worthwhile adding.  */
8868           for (j = i; j < ndirs; j++)
8869             if (savehere[j] > 0)
8870               {
8871                 /* Remember how much we saved for this directory so far.  */
8872                 saved[j] = savehere[j];
8873
8874                 /* Remember the prefix directory.  */
8875                 dirs[j].dir_idx = i;
8876               }
8877         }
8878     }
8879
8880   /* Emit the directory name table.  */
8881   idx_offset = dirs[0].length > 0 ? 1 : 0;
8882   for (i = 1 - idx_offset; i < ndirs; i++)
8883     dw2_asm_output_nstring (dirs[i].path,
8884                             dirs[i].length
8885                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
8886                             "Directory Entry: %#x", i + idx_offset);
8887
8888   dw2_asm_output_data (1, 0, "End directory table");
8889
8890   /* We have to emit them in the order of emitted_number since that's
8891      used in the debug info generation.  To do this efficiently we
8892      generate a back-mapping of the indices first.  */
8893   backmap = XALLOCAVEC (int, numfiles);
8894   for (i = 0; i < numfiles; i++)
8895     backmap[files[i].file_idx->emitted_number - 1] = i;
8896
8897   /* Now write all the file names.  */
8898   for (i = 0; i < numfiles; i++)
8899     {
8900       int file_idx = backmap[i];
8901       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8902
8903 #ifdef VMS_DEBUGGING_INFO
8904 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
8905
8906       /* Setting these fields can lead to debugger miscomparisons,
8907          but VMS Debug requires them to be set correctly.  */
8908
8909       int ver;
8910       long long cdt;
8911       long siz;
8912       int maxfilelen = strlen (files[file_idx].path)
8913                                + dirs[dir_idx].length
8914                                + MAX_VMS_VERSION_LEN + 1;
8915       char *filebuf = XALLOCAVEC (char, maxfilelen);
8916
8917       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
8918       snprintf (filebuf, maxfilelen, "%s;%d",
8919                 files[file_idx].path + dirs[dir_idx].length, ver);
8920
8921       dw2_asm_output_nstring
8922         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
8923
8924       /* Include directory index.  */
8925       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8926
8927       /* Modification time.  */
8928       dw2_asm_output_data_uleb128
8929         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
8930           ? cdt : 0,
8931          NULL);
8932
8933       /* File length in bytes.  */
8934       dw2_asm_output_data_uleb128
8935         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
8936           ? siz : 0,
8937          NULL);
8938 #else
8939       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8940                               "File Entry: %#x", (unsigned) i + 1);
8941
8942       /* Include directory index.  */
8943       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8944
8945       /* Modification time.  */
8946       dw2_asm_output_data_uleb128 (0, NULL);
8947
8948       /* File length in bytes.  */
8949       dw2_asm_output_data_uleb128 (0, NULL);
8950 #endif /* VMS_DEBUGGING_INFO */
8951     }
8952
8953   dw2_asm_output_data (1, 0, "End file name table");
8954 }
8955
8956
8957 /* Output one line number table into the .debug_line section.  */
8958
8959 static void
8960 output_one_line_info_table (dw_line_info_table *table)
8961 {
8962   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8963   unsigned int current_line = 1;
8964   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
8965   dw_line_info_entry *ent;
8966   size_t i;
8967
8968   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
8969     {
8970       switch (ent->opcode)
8971         {
8972         case LI_set_address:
8973           /* ??? Unfortunately, we have little choice here currently, and
8974              must always use the most general form.  GCC does not know the
8975              address delta itself, so we can't use DW_LNS_advance_pc.  Many
8976              ports do have length attributes which will give an upper bound
8977              on the address range.  We could perhaps use length attributes
8978              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
8979           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
8980
8981           /* This can handle any delta.  This takes
8982              4+DWARF2_ADDR_SIZE bytes.  */
8983           dw2_asm_output_data (1, 0, "set address %s", line_label);
8984           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8985           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8986           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8987           break;
8988
8989         case LI_set_line:
8990           if (ent->val == current_line)
8991             {
8992               /* We still need to start a new row, so output a copy insn.  */
8993               dw2_asm_output_data (1, DW_LNS_copy,
8994                                    "copy line %u", current_line);
8995             }
8996           else
8997             {
8998               int line_offset = ent->val - current_line;
8999               int line_delta = line_offset - DWARF_LINE_BASE;
9000
9001               current_line = ent->val;
9002               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9003                 {
9004                   /* This can handle deltas from -10 to 234, using the current
9005                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9006                      This takes 1 byte.  */
9007                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9008                                        "line %u", current_line);
9009                 }
9010               else
9011                 {
9012                   /* This can handle any delta.  This takes at least 4 bytes,
9013                      depending on the value being encoded.  */
9014                   dw2_asm_output_data (1, DW_LNS_advance_line,
9015                                        "advance to line %u", current_line);
9016                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9017                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9018                 }
9019             }
9020           break;
9021
9022         case LI_set_file:
9023           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9024           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9025           break;
9026
9027         case LI_set_column:
9028           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9029           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9030           break;
9031
9032         case LI_negate_stmt:
9033           current_is_stmt = !current_is_stmt;
9034           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9035                                "is_stmt %d", current_is_stmt);
9036           break;
9037
9038         case LI_set_prologue_end:
9039           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9040                                "set prologue end");
9041           break;
9042           
9043         case LI_set_epilogue_begin:
9044           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9045                                "set epilogue begin");
9046           break;
9047
9048         case LI_set_discriminator:
9049           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9050           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9051           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9052           dw2_asm_output_data_uleb128 (ent->val, NULL);
9053           break;
9054         }
9055     }
9056
9057   /* Emit debug info for the address of the end of the table.  */
9058   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9059   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9060   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9061   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9062
9063   dw2_asm_output_data (1, 0, "end sequence");
9064   dw2_asm_output_data_uleb128 (1, NULL);
9065   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9066 }
9067
9068 /* Output the source line number correspondence information.  This
9069    information goes into the .debug_line section.  */
9070
9071 static void
9072 output_line_info (void)
9073 {
9074   char l1[20], l2[20], p1[20], p2[20];
9075   int ver = dwarf_version;
9076   bool saw_one = false;
9077   int opc;
9078
9079   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9080   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9081   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9082   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9083
9084   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9085     dw2_asm_output_data (4, 0xffffffff,
9086       "Initial length escape value indicating 64-bit DWARF extension");
9087   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9088                         "Length of Source Line Info");
9089   ASM_OUTPUT_LABEL (asm_out_file, l1);
9090
9091   dw2_asm_output_data (2, ver, "DWARF Version");
9092   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9093   ASM_OUTPUT_LABEL (asm_out_file, p1);
9094
9095   /* Define the architecture-dependent minimum instruction length (in bytes).
9096      In this implementation of DWARF, this field is used for information
9097      purposes only.  Since GCC generates assembly language, we have no
9098      a priori knowledge of how many instruction bytes are generated for each
9099      source line, and therefore can use only the DW_LNE_set_address and
9100      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9101      this as '1', which is "correct enough" for all architectures,
9102      and don't let the target override.  */
9103   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9104
9105   if (ver >= 4)
9106     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9107                          "Maximum Operations Per Instruction");
9108   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9109                        "Default is_stmt_start flag");
9110   dw2_asm_output_data (1, DWARF_LINE_BASE,
9111                        "Line Base Value (Special Opcodes)");
9112   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9113                        "Line Range Value (Special Opcodes)");
9114   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9115                        "Special Opcode Base");
9116
9117   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9118     {
9119       int n_op_args;
9120       switch (opc)
9121         {
9122         case DW_LNS_advance_pc:
9123         case DW_LNS_advance_line:
9124         case DW_LNS_set_file:
9125         case DW_LNS_set_column:
9126         case DW_LNS_fixed_advance_pc:
9127         case DW_LNS_set_isa:
9128           n_op_args = 1;
9129           break;
9130         default:
9131           n_op_args = 0;
9132           break;
9133         }
9134
9135       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9136                            opc, n_op_args);
9137     }
9138
9139   /* Write out the information about the files we use.  */
9140   output_file_names ();
9141   ASM_OUTPUT_LABEL (asm_out_file, p2);
9142
9143   if (separate_line_info)
9144     {
9145       dw_line_info_table *table;
9146       size_t i;
9147
9148       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9149         if (table->in_use)
9150           {
9151             output_one_line_info_table (table);
9152             saw_one = true;
9153           }
9154     }
9155   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9156     {
9157       output_one_line_info_table (cold_text_section_line_info);
9158       saw_one = true;
9159     }
9160
9161   /* ??? Some Darwin linkers crash on a .debug_line section with no
9162      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9163      sufficient -- the address column must also be initialized.
9164      Make sure to output at least one set_address/end_sequence pair,
9165      choosing .text since that section is always present.  */
9166   if (text_section_line_info->in_use || !saw_one)
9167     output_one_line_info_table (text_section_line_info);
9168
9169   /* Output the marker for the end of the line number info.  */
9170   ASM_OUTPUT_LABEL (asm_out_file, l2);
9171 }
9172 \f
9173 /* Given a pointer to a tree node for some base type, return a pointer to
9174    a DIE that describes the given type.
9175
9176    This routine must only be called for GCC type nodes that correspond to
9177    Dwarf base (fundamental) types.  */
9178
9179 static dw_die_ref
9180 base_type_die (tree type)
9181 {
9182   dw_die_ref base_type_result;
9183   enum dwarf_type encoding;
9184
9185   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9186     return 0;
9187
9188   /* If this is a subtype that should not be emitted as a subrange type,
9189      use the base type.  See subrange_type_for_debug_p.  */
9190   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9191     type = TREE_TYPE (type);
9192
9193   switch (TREE_CODE (type))
9194     {
9195     case INTEGER_TYPE:
9196       if ((dwarf_version >= 4 || !dwarf_strict)
9197           && TYPE_NAME (type)
9198           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9199           && DECL_IS_BUILTIN (TYPE_NAME (type))
9200           && DECL_NAME (TYPE_NAME (type)))
9201         {
9202           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9203           if (strcmp (name, "char16_t") == 0
9204               || strcmp (name, "char32_t") == 0)
9205             {
9206               encoding = DW_ATE_UTF;
9207               break;
9208             }
9209         }
9210       if (TYPE_STRING_FLAG (type))
9211         {
9212           if (TYPE_UNSIGNED (type))
9213             encoding = DW_ATE_unsigned_char;
9214           else
9215             encoding = DW_ATE_signed_char;
9216         }
9217       else if (TYPE_UNSIGNED (type))
9218         encoding = DW_ATE_unsigned;
9219       else
9220         encoding = DW_ATE_signed;
9221       break;
9222
9223     case REAL_TYPE:
9224       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9225         {
9226           if (dwarf_version >= 3 || !dwarf_strict)
9227             encoding = DW_ATE_decimal_float;
9228           else
9229             encoding = DW_ATE_lo_user;
9230         }
9231       else
9232         encoding = DW_ATE_float;
9233       break;
9234
9235     case FIXED_POINT_TYPE:
9236       if (!(dwarf_version >= 3 || !dwarf_strict))
9237         encoding = DW_ATE_lo_user;
9238       else if (TYPE_UNSIGNED (type))
9239         encoding = DW_ATE_unsigned_fixed;
9240       else
9241         encoding = DW_ATE_signed_fixed;
9242       break;
9243
9244       /* Dwarf2 doesn't know anything about complex ints, so use
9245          a user defined type for it.  */
9246     case COMPLEX_TYPE:
9247       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9248         encoding = DW_ATE_complex_float;
9249       else
9250         encoding = DW_ATE_lo_user;
9251       break;
9252
9253     case BOOLEAN_TYPE:
9254       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9255       encoding = DW_ATE_boolean;
9256       break;
9257
9258     default:
9259       /* No other TREE_CODEs are Dwarf fundamental types.  */
9260       gcc_unreachable ();
9261     }
9262
9263   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9264
9265   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9266                    int_size_in_bytes (type));
9267   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9268   add_pubtype (type, base_type_result);
9269
9270   return base_type_result;
9271 }
9272
9273 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9274    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9275
9276 static inline int
9277 is_base_type (tree type)
9278 {
9279   switch (TREE_CODE (type))
9280     {
9281     case ERROR_MARK:
9282     case VOID_TYPE:
9283     case INTEGER_TYPE:
9284     case REAL_TYPE:
9285     case FIXED_POINT_TYPE:
9286     case COMPLEX_TYPE:
9287     case BOOLEAN_TYPE:
9288       return 1;
9289
9290     case ARRAY_TYPE:
9291     case RECORD_TYPE:
9292     case UNION_TYPE:
9293     case QUAL_UNION_TYPE:
9294     case ENUMERAL_TYPE:
9295     case FUNCTION_TYPE:
9296     case METHOD_TYPE:
9297     case POINTER_TYPE:
9298     case REFERENCE_TYPE:
9299     case NULLPTR_TYPE:
9300     case OFFSET_TYPE:
9301     case LANG_TYPE:
9302     case VECTOR_TYPE:
9303       return 0;
9304
9305     default:
9306       gcc_unreachable ();
9307     }
9308
9309   return 0;
9310 }
9311
9312 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9313    node, return the size in bits for the type if it is a constant, or else
9314    return the alignment for the type if the type's size is not constant, or
9315    else return BITS_PER_WORD if the type actually turns out to be an
9316    ERROR_MARK node.  */
9317
9318 static inline unsigned HOST_WIDE_INT
9319 simple_type_size_in_bits (const_tree type)
9320 {
9321   if (TREE_CODE (type) == ERROR_MARK)
9322     return BITS_PER_WORD;
9323   else if (TYPE_SIZE (type) == NULL_TREE)
9324     return 0;
9325   else if (host_integerp (TYPE_SIZE (type), 1))
9326     return tree_low_cst (TYPE_SIZE (type), 1);
9327   else
9328     return TYPE_ALIGN (type);
9329 }
9330
9331 /* Similarly, but return a double_int instead of UHWI.  */
9332
9333 static inline double_int
9334 double_int_type_size_in_bits (const_tree type)
9335 {
9336   if (TREE_CODE (type) == ERROR_MARK)
9337     return double_int::from_uhwi (BITS_PER_WORD);
9338   else if (TYPE_SIZE (type) == NULL_TREE)
9339     return double_int_zero;
9340   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9341     return tree_to_double_int (TYPE_SIZE (type));
9342   else
9343     return double_int::from_uhwi (TYPE_ALIGN (type));
9344 }
9345
9346 /*  Given a pointer to a tree node for a subrange type, return a pointer
9347     to a DIE that describes the given type.  */
9348
9349 static dw_die_ref
9350 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9351 {
9352   dw_die_ref subrange_die;
9353   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9354
9355   if (context_die == NULL)
9356     context_die = comp_unit_die ();
9357
9358   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9359
9360   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9361     {
9362       /* The size of the subrange type and its base type do not match,
9363          so we need to generate a size attribute for the subrange type.  */
9364       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9365     }
9366
9367   if (low)
9368     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9369   if (high)
9370     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9371
9372   return subrange_die;
9373 }
9374
9375 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9376    entry that chains various modifiers in front of the given type.  */
9377
9378 static dw_die_ref
9379 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9380                    dw_die_ref context_die)
9381 {
9382   enum tree_code code = TREE_CODE (type);
9383   dw_die_ref mod_type_die;
9384   dw_die_ref sub_die = NULL;
9385   tree item_type = NULL;
9386   tree qualified_type;
9387   tree name, low, high;
9388   dw_die_ref mod_scope;
9389
9390   if (code == ERROR_MARK)
9391     return NULL;
9392
9393   /* See if we already have the appropriately qualified variant of
9394      this type.  */
9395   qualified_type
9396     = get_qualified_type (type,
9397                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9398                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9399
9400   if (qualified_type == sizetype
9401       && TYPE_NAME (qualified_type)
9402       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9403     {
9404       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9405
9406       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9407                            && TYPE_PRECISION (t)
9408                            == TYPE_PRECISION (qualified_type)
9409                            && TYPE_UNSIGNED (t)
9410                            == TYPE_UNSIGNED (qualified_type));
9411       qualified_type = t;
9412     }
9413
9414   /* If we do, then we can just use its DIE, if it exists.  */
9415   if (qualified_type)
9416     {
9417       mod_type_die = lookup_type_die (qualified_type);
9418       if (mod_type_die)
9419         return mod_type_die;
9420     }
9421
9422   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9423
9424   /* Handle C typedef types.  */
9425   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9426       && !DECL_ARTIFICIAL (name))
9427     {
9428       tree dtype = TREE_TYPE (name);
9429
9430       if (qualified_type == dtype)
9431         {
9432           /* For a named type, use the typedef.  */
9433           gen_type_die (qualified_type, context_die);
9434           return lookup_type_die (qualified_type);
9435         }
9436       else if (is_const_type < TYPE_READONLY (dtype)
9437                || is_volatile_type < TYPE_VOLATILE (dtype)
9438                || (is_const_type <= TYPE_READONLY (dtype)
9439                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9440                    && DECL_ORIGINAL_TYPE (name) != type))
9441         /* cv-unqualified version of named type.  Just use the unnamed
9442            type to which it refers.  */
9443         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9444                                   is_const_type, is_volatile_type,
9445                                   context_die);
9446       /* Else cv-qualified version of named type; fall through.  */
9447     }
9448
9449   mod_scope = scope_die_for (type, context_die);
9450
9451   if (is_const_type
9452       /* If both is_const_type and is_volatile_type, prefer the path
9453          which leads to a qualified type.  */
9454       && (!is_volatile_type
9455           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9456           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9457     {
9458       mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9459       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9460     }
9461   else if (is_volatile_type)
9462     {
9463       mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9464       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9465     }
9466   else if (code == POINTER_TYPE)
9467     {
9468       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9469       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9470                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9471       item_type = TREE_TYPE (type);
9472       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9473         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9474                          TYPE_ADDR_SPACE (item_type));
9475     }
9476   else if (code == REFERENCE_TYPE)
9477     {
9478       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9479         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9480                                 type);
9481       else
9482         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9483       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9484                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9485       item_type = TREE_TYPE (type);
9486       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9487         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9488                          TYPE_ADDR_SPACE (item_type));
9489     }
9490   else if (code == INTEGER_TYPE
9491            && TREE_TYPE (type) != NULL_TREE
9492            && subrange_type_for_debug_p (type, &low, &high))
9493     {
9494       mod_type_die = subrange_type_die (type, low, high, context_die);
9495       item_type = TREE_TYPE (type);
9496     }
9497   else if (is_base_type (type))
9498     mod_type_die = base_type_die (type);
9499   else
9500     {
9501       gen_type_die (type, context_die);
9502
9503       /* We have to get the type_main_variant here (and pass that to the
9504          `lookup_type_die' routine) because the ..._TYPE node we have
9505          might simply be a *copy* of some original type node (where the
9506          copy was created to help us keep track of typedef names) and
9507          that copy might have a different TYPE_UID from the original
9508          ..._TYPE node.  */
9509       if (TREE_CODE (type) != VECTOR_TYPE)
9510         return lookup_type_die (type_main_variant (type));
9511       else
9512         /* Vectors have the debugging information in the type,
9513            not the main variant.  */
9514         return lookup_type_die (type);
9515     }
9516
9517   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9518      don't output a DW_TAG_typedef, since there isn't one in the
9519      user's program; just attach a DW_AT_name to the type.
9520      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9521      if the base type already has the same name.  */
9522   if (name
9523       && ((TREE_CODE (name) != TYPE_DECL
9524            && (qualified_type == TYPE_MAIN_VARIANT (type)
9525                || (!is_const_type && !is_volatile_type)))
9526           || (TREE_CODE (name) == TYPE_DECL
9527               && TREE_TYPE (name) == qualified_type
9528               && DECL_NAME (name))))
9529     {
9530       if (TREE_CODE (name) == TYPE_DECL)
9531         /* Could just call add_name_and_src_coords_attributes here,
9532            but since this is a builtin type it doesn't have any
9533            useful source coordinates anyway.  */
9534         name = DECL_NAME (name);
9535       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9536     }
9537   /* This probably indicates a bug.  */
9538   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9539     {
9540       name = TYPE_NAME (type);
9541       if (name
9542           && TREE_CODE (name) == TYPE_DECL)
9543         name = DECL_NAME (name);
9544       add_name_attribute (mod_type_die,
9545                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9546     }
9547
9548   if (qualified_type)
9549     equate_type_number_to_die (qualified_type, mod_type_die);
9550
9551   if (item_type)
9552     /* We must do this after the equate_type_number_to_die call, in case
9553        this is a recursive type.  This ensures that the modified_type_die
9554        recursion will terminate even if the type is recursive.  Recursive
9555        types are possible in Ada.  */
9556     sub_die = modified_type_die (item_type,
9557                                  TYPE_READONLY (item_type),
9558                                  TYPE_VOLATILE (item_type),
9559                                  context_die);
9560
9561   if (sub_die != NULL)
9562     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9563
9564   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9565   if (TYPE_ARTIFICIAL (type))
9566     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9567
9568   return mod_type_die;
9569 }
9570
9571 /* Generate DIEs for the generic parameters of T.
9572    T must be either a generic type or a generic function.
9573    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9574
9575 static void
9576 gen_generic_params_dies (tree t)
9577 {
9578   tree parms, args;
9579   int parms_num, i;
9580   dw_die_ref die = NULL;
9581
9582   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9583     return;
9584
9585   if (TYPE_P (t))
9586     die = lookup_type_die (t);
9587   else if (DECL_P (t))
9588     die = lookup_decl_die (t);
9589
9590   gcc_assert (die);
9591
9592   parms = lang_hooks.get_innermost_generic_parms (t);
9593   if (!parms)
9594     /* T has no generic parameter. It means T is neither a generic type
9595        or function. End of story.  */
9596     return;
9597
9598   parms_num = TREE_VEC_LENGTH (parms);
9599   args = lang_hooks.get_innermost_generic_args (t);
9600   for (i = 0; i < parms_num; i++)
9601     {
9602       tree parm, arg, arg_pack_elems;
9603
9604       parm = TREE_VEC_ELT (parms, i);
9605       arg = TREE_VEC_ELT (args, i);
9606       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9607       gcc_assert (parm && TREE_VALUE (parm) && arg);
9608
9609       if (parm && TREE_VALUE (parm) && arg)
9610         {
9611           /* If PARM represents a template parameter pack,
9612              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9613              by DW_TAG_template_*_parameter DIEs for the argument
9614              pack elements of ARG. Note that ARG would then be
9615              an argument pack.  */
9616           if (arg_pack_elems)
9617             template_parameter_pack_die (TREE_VALUE (parm),
9618                                          arg_pack_elems,
9619                                          die);
9620           else
9621             generic_parameter_die (TREE_VALUE (parm), arg,
9622                                    true /* Emit DW_AT_name */, die);
9623         }
9624     }
9625 }
9626
9627 /* Create and return a DIE for PARM which should be
9628    the representation of a generic type parameter.
9629    For instance, in the C++ front end, PARM would be a template parameter.
9630    ARG is the argument to PARM.
9631    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9632    name of the PARM.
9633    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9634    as a child node.  */
9635
9636 static dw_die_ref
9637 generic_parameter_die (tree parm, tree arg,
9638                        bool emit_name_p,
9639                        dw_die_ref parent_die)
9640 {
9641   dw_die_ref tmpl_die = NULL;
9642   const char *name = NULL;
9643
9644   if (!parm || !DECL_NAME (parm) || !arg)
9645     return NULL;
9646
9647   /* We support non-type generic parameters and arguments,
9648      type generic parameters and arguments, as well as
9649      generic generic parameters (a.k.a. template template parameters in C++)
9650      and arguments.  */
9651   if (TREE_CODE (parm) == PARM_DECL)
9652     /* PARM is a nontype generic parameter  */
9653     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9654   else if (TREE_CODE (parm) == TYPE_DECL)
9655     /* PARM is a type generic parameter.  */
9656     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9657   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9658     /* PARM is a generic generic parameter.
9659        Its DIE is a GNU extension. It shall have a
9660        DW_AT_name attribute to represent the name of the template template
9661        parameter, and a DW_AT_GNU_template_name attribute to represent the
9662        name of the template template argument.  */
9663     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9664                         parent_die, parm);
9665   else
9666     gcc_unreachable ();
9667
9668   if (tmpl_die)
9669     {
9670       tree tmpl_type;
9671
9672       /* If PARM is a generic parameter pack, it means we are
9673          emitting debug info for a template argument pack element.
9674          In other terms, ARG is a template argument pack element.
9675          In that case, we don't emit any DW_AT_name attribute for
9676          the die.  */
9677       if (emit_name_p)
9678         {
9679           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9680           gcc_assert (name);
9681           add_AT_string (tmpl_die, DW_AT_name, name);
9682         }
9683
9684       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9685         {
9686           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9687              TMPL_DIE should have a child DW_AT_type attribute that is set
9688              to the type of the argument to PARM, which is ARG.
9689              If PARM is a type generic parameter, TMPL_DIE should have a
9690              child DW_AT_type that is set to ARG.  */
9691           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9692           add_type_attribute (tmpl_die, tmpl_type, 0,
9693                               TREE_THIS_VOLATILE (tmpl_type),
9694                               parent_die);
9695         }
9696       else
9697         {
9698           /* So TMPL_DIE is a DIE representing a
9699              a generic generic template parameter, a.k.a template template
9700              parameter in C++ and arg is a template.  */
9701
9702           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9703              to the name of the argument.  */
9704           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9705           if (name)
9706             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9707         }
9708
9709       if (TREE_CODE (parm) == PARM_DECL)
9710         /* So PARM is a non-type generic parameter.
9711            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9712            attribute of TMPL_DIE which value represents the value
9713            of ARG.
9714            We must be careful here:
9715            The value of ARG might reference some function decls.
9716            We might currently be emitting debug info for a generic
9717            type and types are emitted before function decls, we don't
9718            know if the function decls referenced by ARG will actually be
9719            emitted after cgraph computations.
9720            So must defer the generation of the DW_AT_const_value to
9721            after cgraph is ready.  */
9722         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9723     }
9724
9725   return tmpl_die;
9726 }
9727
9728 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9729    PARM_PACK must be a template parameter pack. The returned DIE
9730    will be child DIE of PARENT_DIE.  */
9731
9732 static dw_die_ref
9733 template_parameter_pack_die (tree parm_pack,
9734                              tree parm_pack_args,
9735                              dw_die_ref parent_die)
9736 {
9737   dw_die_ref die;
9738   int j;
9739
9740   gcc_assert (parent_die && parm_pack);
9741
9742   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9743   add_name_and_src_coords_attributes (die, parm_pack);
9744   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9745     generic_parameter_die (parm_pack,
9746                            TREE_VEC_ELT (parm_pack_args, j),
9747                            false /* Don't emit DW_AT_name */,
9748                            die);
9749   return die;
9750 }
9751
9752 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9753    an enumerated type.  */
9754
9755 static inline int
9756 type_is_enum (const_tree type)
9757 {
9758   return TREE_CODE (type) == ENUMERAL_TYPE;
9759 }
9760
9761 /* Return the DBX register number described by a given RTL node.  */
9762
9763 static unsigned int
9764 dbx_reg_number (const_rtx rtl)
9765 {
9766   unsigned regno = REGNO (rtl);
9767
9768   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9769
9770 #ifdef LEAF_REG_REMAP
9771   if (crtl->uses_only_leaf_regs)
9772     {
9773       int leaf_reg = LEAF_REG_REMAP (regno);
9774       if (leaf_reg != -1)
9775         regno = (unsigned) leaf_reg;
9776     }
9777 #endif
9778
9779   regno = DBX_REGISTER_NUMBER (regno);
9780   gcc_assert (regno != INVALID_REGNUM);
9781   return regno;
9782 }
9783
9784 /* Optionally add a DW_OP_piece term to a location description expression.
9785    DW_OP_piece is only added if the location description expression already
9786    doesn't end with DW_OP_piece.  */
9787
9788 static void
9789 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9790 {
9791   dw_loc_descr_ref loc;
9792
9793   if (*list_head != NULL)
9794     {
9795       /* Find the end of the chain.  */
9796       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9797         ;
9798
9799       if (loc->dw_loc_opc != DW_OP_piece)
9800         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9801     }
9802 }
9803
9804 /* Return a location descriptor that designates a machine register or
9805    zero if there is none.  */
9806
9807 static dw_loc_descr_ref
9808 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9809 {
9810   rtx regs;
9811
9812   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9813     return 0;
9814
9815   /* We only use "frame base" when we're sure we're talking about the
9816      post-prologue local stack frame.  We do this by *not* running
9817      register elimination until this point, and recognizing the special
9818      argument pointer and soft frame pointer rtx's.
9819      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
9820   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
9821       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
9822     {
9823       dw_loc_descr_ref result = NULL;
9824
9825       if (dwarf_version >= 4 || !dwarf_strict)
9826         {
9827           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
9828                                        initialized);
9829           if (result)
9830             add_loc_descr (&result,
9831                            new_loc_descr (DW_OP_stack_value, 0, 0));
9832         }
9833       return result;
9834     }
9835
9836   regs = targetm.dwarf_register_span (rtl);
9837
9838   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9839     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9840   else
9841     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9842 }
9843
9844 /* Return a location descriptor that designates a machine register for
9845    a given hard register number.  */
9846
9847 static dw_loc_descr_ref
9848 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9849 {
9850   dw_loc_descr_ref reg_loc_descr;
9851
9852   if (regno <= 31)
9853     reg_loc_descr
9854       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9855   else
9856     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9857
9858   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9859     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9860
9861   return reg_loc_descr;
9862 }
9863
9864 /* Given an RTL of a register, return a location descriptor that
9865    designates a value that spans more than one register.  */
9866
9867 static dw_loc_descr_ref
9868 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9869                              enum var_init_status initialized)
9870 {
9871   int nregs, size, i;
9872   unsigned reg;
9873   dw_loc_descr_ref loc_result = NULL;
9874
9875   reg = REGNO (rtl);
9876 #ifdef LEAF_REG_REMAP
9877   if (crtl->uses_only_leaf_regs)
9878     {
9879       int leaf_reg = LEAF_REG_REMAP (reg);
9880       if (leaf_reg != -1)
9881         reg = (unsigned) leaf_reg;
9882     }
9883 #endif
9884   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9885   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9886
9887   /* Simple, contiguous registers.  */
9888   if (regs == NULL_RTX)
9889     {
9890       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9891
9892       loc_result = NULL;
9893       while (nregs--)
9894         {
9895           dw_loc_descr_ref t;
9896
9897           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9898                                       VAR_INIT_STATUS_INITIALIZED);
9899           add_loc_descr (&loc_result, t);
9900           add_loc_descr_op_piece (&loc_result, size);
9901           ++reg;
9902         }
9903       return loc_result;
9904     }
9905
9906   /* Now onto stupid register sets in non contiguous locations.  */
9907
9908   gcc_assert (GET_CODE (regs) == PARALLEL);
9909
9910   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9911   loc_result = NULL;
9912
9913   for (i = 0; i < XVECLEN (regs, 0); ++i)
9914     {
9915       dw_loc_descr_ref t;
9916
9917       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9918                                   VAR_INIT_STATUS_INITIALIZED);
9919       add_loc_descr (&loc_result, t);
9920       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9921       add_loc_descr_op_piece (&loc_result, size);
9922     }
9923
9924   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9925     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9926   return loc_result;
9927 }
9928
9929 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
9930
9931 /* Return a location descriptor that designates a constant i,
9932    as a compound operation from constant (i >> shift), constant shift
9933    and DW_OP_shl.  */
9934
9935 static dw_loc_descr_ref
9936 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9937 {
9938   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
9939   add_loc_descr (&ret, int_loc_descriptor (shift));
9940   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
9941   return ret;
9942 }
9943
9944 /* Return a location descriptor that designates a constant.  */
9945
9946 static dw_loc_descr_ref
9947 int_loc_descriptor (HOST_WIDE_INT i)
9948 {
9949   enum dwarf_location_atom op;
9950
9951   /* Pick the smallest representation of a constant, rather than just
9952      defaulting to the LEB encoding.  */
9953   if (i >= 0)
9954     {
9955       int clz = clz_hwi (i);
9956       int ctz = ctz_hwi (i);
9957       if (i <= 31)
9958         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9959       else if (i <= 0xff)
9960         op = DW_OP_const1u;
9961       else if (i <= 0xffff)
9962         op = DW_OP_const2u;
9963       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
9964                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
9965         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
9966            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
9967            while DW_OP_const4u is 5 bytes.  */
9968         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
9969       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9970                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
9971         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
9972            while DW_OP_const4u is 5 bytes.  */
9973         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9974       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
9975         op = DW_OP_const4u;
9976       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9977                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
9978         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
9979            while DW_OP_constu of constant >= 0x100000000 takes at least
9980            6 bytes.  */
9981         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9982       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
9983                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
9984                   >= HOST_BITS_PER_WIDE_INT)
9985         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
9986            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
9987            while DW_OP_constu takes in this case at least 6 bytes.  */
9988         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
9989       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
9990                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
9991                && size_of_uleb128 (i) > 6)
9992         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
9993         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
9994       else
9995         op = DW_OP_constu;
9996     }
9997   else
9998     {
9999       if (i >= -0x80)
10000         op = DW_OP_const1s;
10001       else if (i >= -0x8000)
10002         op = DW_OP_const2s;
10003       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10004         {
10005           if (size_of_int_loc_descriptor (i) < 5)
10006             {
10007               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10008               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10009               return ret;
10010             }
10011           op = DW_OP_const4s;
10012         }
10013       else
10014         {
10015           if (size_of_int_loc_descriptor (i)
10016               < (unsigned long) 1 + size_of_sleb128 (i))
10017             {
10018               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10019               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10020               return ret;
10021             }
10022           op = DW_OP_consts;
10023         }
10024     }
10025
10026   return new_loc_descr (op, i, 0);
10027 }
10028
10029 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10030    without actually allocating it.  */
10031
10032 static unsigned long
10033 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10034 {
10035   return size_of_int_loc_descriptor (i >> shift)
10036          + size_of_int_loc_descriptor (shift)
10037          + 1;
10038 }
10039
10040 /* Return size_of_locs (int_loc_descriptor (i)) without
10041    actually allocating it.  */
10042
10043 static unsigned long
10044 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10045 {
10046   unsigned long s;
10047
10048   if (i >= 0)
10049     {
10050       int clz, ctz;
10051       if (i <= 31)
10052         return 1;
10053       else if (i <= 0xff)
10054         return 2;
10055       else if (i <= 0xffff)
10056         return 3;
10057       clz = clz_hwi (i);
10058       ctz = ctz_hwi (i);
10059       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10060           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10061         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10062                                                     - clz - 5);
10063       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10064                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10065         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10066                                                     - clz - 8);
10067       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10068         return 5;
10069       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10070       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10071           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10072         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10073                                                     - clz - 8);
10074       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10075                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10076         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10077                                                     - clz - 16);
10078       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10079                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10080                && s > 6)
10081         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10082                                                     - clz - 32);
10083       else
10084         return 1 + s;
10085     }
10086   else
10087     {
10088       if (i >= -0x80)
10089         return 2;
10090       else if (i >= -0x8000)
10091         return 3;
10092       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10093         {
10094           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10095             {
10096               s = size_of_int_loc_descriptor (-i) + 1;
10097               if (s < 5)
10098                 return s;
10099             }
10100           return 5;
10101         }
10102       else
10103         {
10104           unsigned long r = 1 + size_of_sleb128 (i);
10105           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10106             {
10107               s = size_of_int_loc_descriptor (-i) + 1;
10108               if (s < r)
10109                 return s;
10110             }
10111           return r;
10112         }
10113     }
10114 }
10115
10116 /* Return loc description representing "address" of integer value.
10117    This can appear only as toplevel expression.  */
10118
10119 static dw_loc_descr_ref
10120 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10121 {
10122   int litsize;
10123   dw_loc_descr_ref loc_result = NULL;
10124
10125   if (!(dwarf_version >= 4 || !dwarf_strict))
10126     return NULL;
10127
10128   litsize = size_of_int_loc_descriptor (i);
10129   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10130      is more compact.  For DW_OP_stack_value we need:
10131      litsize + 1 (DW_OP_stack_value)
10132      and for DW_OP_implicit_value:
10133      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10134   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10135     {
10136       loc_result = int_loc_descriptor (i);
10137       add_loc_descr (&loc_result,
10138                      new_loc_descr (DW_OP_stack_value, 0, 0));
10139       return loc_result;
10140     }
10141
10142   loc_result = new_loc_descr (DW_OP_implicit_value,
10143                               size, 0);
10144   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10145   loc_result->dw_loc_oprnd2.v.val_int = i;
10146   return loc_result;
10147 }
10148
10149 /* Return a location descriptor that designates a base+offset location.  */
10150
10151 static dw_loc_descr_ref
10152 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10153                  enum var_init_status initialized)
10154 {
10155   unsigned int regno;
10156   dw_loc_descr_ref result;
10157   dw_fde_ref fde = cfun->fde;
10158
10159   /* We only use "frame base" when we're sure we're talking about the
10160      post-prologue local stack frame.  We do this by *not* running
10161      register elimination until this point, and recognizing the special
10162      argument pointer and soft frame pointer rtx's.  */
10163   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10164     {
10165       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10166
10167       if (elim != reg)
10168         {
10169           if (GET_CODE (elim) == PLUS)
10170             {
10171               offset += INTVAL (XEXP (elim, 1));
10172               elim = XEXP (elim, 0);
10173             }
10174           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10175                        && (elim == hard_frame_pointer_rtx
10176                            || elim == stack_pointer_rtx))
10177                       || elim == (frame_pointer_needed
10178                                   ? hard_frame_pointer_rtx
10179                                   : stack_pointer_rtx));
10180
10181           /* If drap register is used to align stack, use frame
10182              pointer + offset to access stack variables.  If stack
10183              is aligned without drap, use stack pointer + offset to
10184              access stack variables.  */
10185           if (crtl->stack_realign_tried
10186               && reg == frame_pointer_rtx)
10187             {
10188               int base_reg
10189                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10190                                       ? HARD_FRAME_POINTER_REGNUM
10191                                       : REGNO (elim));
10192               return new_reg_loc_descr (base_reg, offset);
10193             }
10194
10195           gcc_assert (frame_pointer_fb_offset_valid);
10196           offset += frame_pointer_fb_offset;
10197           return new_loc_descr (DW_OP_fbreg, offset, 0);
10198         }
10199     }
10200
10201   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10202
10203   if (!optimize && fde
10204       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10205     {
10206       /* Use cfa+offset to represent the location of arguments passed
10207          on the stack when drap is used to align stack.
10208          Only do this when not optimizing, for optimized code var-tracking
10209          is supposed to track where the arguments live and the register
10210          used as vdrap or drap in some spot might be used for something
10211          else in other part of the routine.  */
10212       return new_loc_descr (DW_OP_fbreg, offset, 0);
10213     }
10214
10215   if (regno <= 31)
10216     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10217                             offset, 0);
10218   else
10219     result = new_loc_descr (DW_OP_bregx, regno, offset);
10220
10221   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10222     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10223
10224   return result;
10225 }
10226
10227 /* Return true if this RTL expression describes a base+offset calculation.  */
10228
10229 static inline int
10230 is_based_loc (const_rtx rtl)
10231 {
10232   return (GET_CODE (rtl) == PLUS
10233           && ((REG_P (XEXP (rtl, 0))
10234                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10235                && CONST_INT_P (XEXP (rtl, 1)))));
10236 }
10237
10238 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10239    failed.  */
10240
10241 static dw_loc_descr_ref
10242 tls_mem_loc_descriptor (rtx mem)
10243 {
10244   tree base;
10245   dw_loc_descr_ref loc_result;
10246
10247   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10248     return NULL;
10249
10250   base = get_base_address (MEM_EXPR (mem));
10251   if (base == NULL
10252       || TREE_CODE (base) != VAR_DECL
10253       || !DECL_THREAD_LOCAL_P (base))
10254     return NULL;
10255
10256   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10257   if (loc_result == NULL)
10258     return NULL;
10259
10260   if (MEM_OFFSET (mem))
10261     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10262
10263   return loc_result;
10264 }
10265
10266 /* Output debug info about reason why we failed to expand expression as dwarf
10267    expression.  */
10268
10269 static void
10270 expansion_failed (tree expr, rtx rtl, char const *reason)
10271 {
10272   if (dump_file && (dump_flags & TDF_DETAILS))
10273     {
10274       fprintf (dump_file, "Failed to expand as dwarf: ");
10275       if (expr)
10276         print_generic_expr (dump_file, expr, dump_flags);
10277       if (rtl)
10278         {
10279           fprintf (dump_file, "\n");
10280           print_rtl (dump_file, rtl);
10281         }
10282       fprintf (dump_file, "\nReason: %s\n", reason);
10283     }
10284 }
10285
10286 /* Helper function for const_ok_for_output, called either directly
10287    or via for_each_rtx.  */
10288
10289 static int
10290 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10291 {
10292   rtx rtl = *rtlp;
10293
10294   if (GET_CODE (rtl) == UNSPEC)
10295     {
10296       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10297          we can't express it in the debug info.  */
10298 #ifdef ENABLE_CHECKING
10299       /* Don't complain about TLS UNSPECs, those are just too hard to
10300          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
10301          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10302          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
10303       if (XVECLEN (rtl, 0) == 0
10304           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10305           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10306         inform (current_function_decl
10307                 ? DECL_SOURCE_LOCATION (current_function_decl)
10308                 : UNKNOWN_LOCATION,
10309 #if NUM_UNSPEC_VALUES > 0
10310                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10311                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10312                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10313                 XINT (rtl, 1));
10314 #else
10315                 "non-delegitimized UNSPEC %d found in variable location",
10316                 XINT (rtl, 1));
10317 #endif
10318 #endif
10319       expansion_failed (NULL_TREE, rtl,
10320                         "UNSPEC hasn't been delegitimized.\n");
10321       return 1;
10322     }
10323
10324   if (targetm.const_not_ok_for_debug_p (rtl))
10325     {
10326       expansion_failed (NULL_TREE, rtl,
10327                         "Expression rejected for debug by the backend.\n");
10328       return 1;
10329     }
10330
10331   if (GET_CODE (rtl) != SYMBOL_REF)
10332     return 0;
10333
10334   if (CONSTANT_POOL_ADDRESS_P (rtl))
10335     {
10336       bool marked;
10337       get_pool_constant_mark (rtl, &marked);
10338       /* If all references to this pool constant were optimized away,
10339          it was not output and thus we can't represent it.  */
10340       if (!marked)
10341         {
10342           expansion_failed (NULL_TREE, rtl,
10343                             "Constant was removed from constant pool.\n");
10344           return 1;
10345         }
10346     }
10347
10348   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10349     return 1;
10350
10351   /* Avoid references to external symbols in debug info, on several targets
10352      the linker might even refuse to link when linking a shared library,
10353      and in many other cases the relocations for .debug_info/.debug_loc are
10354      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10355      to be defined within the same shared library or executable are fine.  */
10356   if (SYMBOL_REF_EXTERNAL_P (rtl))
10357     {
10358       tree decl = SYMBOL_REF_DECL (rtl);
10359
10360       if (decl == NULL || !targetm.binds_local_p (decl))
10361         {
10362           expansion_failed (NULL_TREE, rtl,
10363                             "Symbol not defined in current TU.\n");
10364           return 1;
10365         }
10366     }
10367
10368   return 0;
10369 }
10370
10371 /* Return true if constant RTL can be emitted in DW_OP_addr or
10372    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10373    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10374
10375 static bool
10376 const_ok_for_output (rtx rtl)
10377 {
10378   if (GET_CODE (rtl) == SYMBOL_REF)
10379     return const_ok_for_output_1 (&rtl, NULL) == 0;
10380
10381   if (GET_CODE (rtl) == CONST)
10382     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10383
10384   return true;
10385 }
10386
10387 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10388    if possible, NULL otherwise.  */
10389
10390 static dw_die_ref
10391 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10392 {
10393   dw_die_ref type_die;
10394   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10395
10396   if (type == NULL)
10397     return NULL;
10398   switch (TREE_CODE (type))
10399     {
10400     case INTEGER_TYPE:
10401     case REAL_TYPE:
10402       break;
10403     default:
10404       return NULL;
10405     }
10406   type_die = lookup_type_die (type);
10407   if (!type_die)
10408     type_die = modified_type_die (type, false, false, comp_unit_die ());
10409   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10410     return NULL;
10411   return type_die;
10412 }
10413
10414 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10415    type matching MODE, or, if MODE is narrower than or as wide as
10416    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10417    possible.  */
10418
10419 static dw_loc_descr_ref
10420 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10421 {
10422   enum machine_mode outer_mode = mode;
10423   dw_die_ref type_die;
10424   dw_loc_descr_ref cvt;
10425
10426   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10427     {
10428       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10429       return op;
10430     }
10431   type_die = base_type_for_mode (outer_mode, 1);
10432   if (type_die == NULL)
10433     return NULL;
10434   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10435   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10436   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10437   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10438   add_loc_descr (&op, cvt);
10439   return op;
10440 }
10441
10442 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10443
10444 static dw_loc_descr_ref
10445 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10446                         dw_loc_descr_ref op1)
10447 {
10448   dw_loc_descr_ref ret = op0;
10449   add_loc_descr (&ret, op1);
10450   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10451   if (STORE_FLAG_VALUE != 1)
10452     {
10453       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10454       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10455     }
10456   return ret;
10457 }
10458
10459 /* Return location descriptor for signed comparison OP RTL.  */
10460
10461 static dw_loc_descr_ref
10462 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10463                          enum machine_mode mem_mode)
10464 {
10465   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10466   dw_loc_descr_ref op0, op1;
10467   int shift;
10468
10469   if (op_mode == VOIDmode)
10470     op_mode = GET_MODE (XEXP (rtl, 1));
10471   if (op_mode == VOIDmode)
10472     return NULL;
10473
10474   if (dwarf_strict
10475       && (GET_MODE_CLASS (op_mode) != MODE_INT
10476           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10477     return NULL;
10478
10479   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10480                             VAR_INIT_STATUS_INITIALIZED);
10481   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10482                             VAR_INIT_STATUS_INITIALIZED);
10483
10484   if (op0 == NULL || op1 == NULL)
10485     return NULL;
10486
10487   if (GET_MODE_CLASS (op_mode) != MODE_INT
10488       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10489     return compare_loc_descriptor (op, op0, op1);
10490
10491   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10492     {
10493       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10494       dw_loc_descr_ref cvt;
10495
10496       if (type_die == NULL)
10497         return NULL;
10498       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10499       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10500       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10501       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10502       add_loc_descr (&op0, cvt);
10503       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10504       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10505       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10506       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10507       add_loc_descr (&op1, cvt);
10508       return compare_loc_descriptor (op, op0, op1);
10509     }
10510
10511   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10512   /* For eq/ne, if the operands are known to be zero-extended,
10513      there is no need to do the fancy shifting up.  */
10514   if (op == DW_OP_eq || op == DW_OP_ne)
10515     {
10516       dw_loc_descr_ref last0, last1;
10517       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10518         ;
10519       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10520         ;
10521       /* deref_size zero extends, and for constants we can check
10522          whether they are zero extended or not.  */
10523       if (((last0->dw_loc_opc == DW_OP_deref_size
10524             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10525            || (CONST_INT_P (XEXP (rtl, 0))
10526                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10527                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10528           && ((last1->dw_loc_opc == DW_OP_deref_size
10529                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10530               || (CONST_INT_P (XEXP (rtl, 1))
10531                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10532                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10533         return compare_loc_descriptor (op, op0, op1);
10534
10535       /* EQ/NE comparison against constant in narrower type than
10536          DWARF2_ADDR_SIZE can be performed either as
10537          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10538          DW_OP_{eq,ne}
10539          or
10540          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10541          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10542       if (CONST_INT_P (XEXP (rtl, 1))
10543           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10544           && (size_of_int_loc_descriptor (shift) + 1
10545               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10546               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10547                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10548                                                & GET_MODE_MASK (op_mode))))
10549         {
10550           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10551           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10552           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10553                                     & GET_MODE_MASK (op_mode));
10554           return compare_loc_descriptor (op, op0, op1);
10555         }
10556     }
10557   add_loc_descr (&op0, int_loc_descriptor (shift));
10558   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10559   if (CONST_INT_P (XEXP (rtl, 1)))
10560     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10561   else
10562     {
10563       add_loc_descr (&op1, int_loc_descriptor (shift));
10564       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10565     }
10566   return compare_loc_descriptor (op, op0, op1);
10567 }
10568
10569 /* Return location descriptor for unsigned comparison OP RTL.  */
10570
10571 static dw_loc_descr_ref
10572 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10573                          enum machine_mode mem_mode)
10574 {
10575   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10576   dw_loc_descr_ref op0, op1;
10577
10578   if (op_mode == VOIDmode)
10579     op_mode = GET_MODE (XEXP (rtl, 1));
10580   if (op_mode == VOIDmode)
10581     return NULL;
10582   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10583     return NULL;
10584
10585   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10586     return NULL;
10587
10588   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10589                             VAR_INIT_STATUS_INITIALIZED);
10590   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10591                             VAR_INIT_STATUS_INITIALIZED);
10592
10593   if (op0 == NULL || op1 == NULL)
10594     return NULL;
10595
10596   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10597     {
10598       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10599       dw_loc_descr_ref last0, last1;
10600       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10601         ;
10602       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10603         ;
10604       if (CONST_INT_P (XEXP (rtl, 0)))
10605         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10606       /* deref_size zero extends, so no need to mask it again.  */
10607       else if (last0->dw_loc_opc != DW_OP_deref_size
10608                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10609         {
10610           add_loc_descr (&op0, int_loc_descriptor (mask));
10611           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10612         }
10613       if (CONST_INT_P (XEXP (rtl, 1)))
10614         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10615       /* deref_size zero extends, so no need to mask it again.  */
10616       else if (last1->dw_loc_opc != DW_OP_deref_size
10617                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10618         {
10619           add_loc_descr (&op1, int_loc_descriptor (mask));
10620           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10621         }
10622     }
10623   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10624     {
10625       HOST_WIDE_INT bias = 1;
10626       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10627       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10628       if (CONST_INT_P (XEXP (rtl, 1)))
10629         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10630                                   + INTVAL (XEXP (rtl, 1)));
10631       else
10632         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10633                                             bias, 0));
10634     }
10635   return compare_loc_descriptor (op, op0, op1);
10636 }
10637
10638 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10639
10640 static dw_loc_descr_ref
10641 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10642                        enum machine_mode mem_mode)
10643 {
10644   enum dwarf_location_atom op;
10645   dw_loc_descr_ref op0, op1, ret;
10646   dw_loc_descr_ref bra_node, drop_node;
10647
10648   if (dwarf_strict
10649       && (GET_MODE_CLASS (mode) != MODE_INT
10650           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10651     return NULL;
10652
10653   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10654                             VAR_INIT_STATUS_INITIALIZED);
10655   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10656                             VAR_INIT_STATUS_INITIALIZED);
10657
10658   if (op0 == NULL || op1 == NULL)
10659     return NULL;
10660
10661   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10662   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10663   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10664   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10665     {
10666       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10667         {
10668           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10669           add_loc_descr (&op0, int_loc_descriptor (mask));
10670           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10671           add_loc_descr (&op1, int_loc_descriptor (mask));
10672           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10673         }
10674       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10675         {
10676           HOST_WIDE_INT bias = 1;
10677           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10678           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10679           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10680         }
10681     }
10682   else if (GET_MODE_CLASS (mode) == MODE_INT
10683            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10684     {
10685       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10686       add_loc_descr (&op0, int_loc_descriptor (shift));
10687       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10688       add_loc_descr (&op1, int_loc_descriptor (shift));
10689       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10690     }
10691   else if (GET_MODE_CLASS (mode) == MODE_INT
10692            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10693     {
10694       dw_die_ref type_die = base_type_for_mode (mode, 0);
10695       dw_loc_descr_ref cvt;
10696       if (type_die == NULL)
10697         return NULL;
10698       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10699       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10700       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10701       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10702       add_loc_descr (&op0, cvt);
10703       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10704       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10705       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10706       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10707       add_loc_descr (&op1, cvt);
10708     }
10709
10710   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10711     op = DW_OP_lt;
10712   else
10713     op = DW_OP_gt;
10714   ret = op0;
10715   add_loc_descr (&ret, op1);
10716   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10717   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10718   add_loc_descr (&ret, bra_node);
10719   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10720   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10721   add_loc_descr (&ret, drop_node);
10722   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10723   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10724   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10725       && GET_MODE_CLASS (mode) == MODE_INT
10726       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10727     ret = convert_descriptor_to_mode (mode, ret);
10728   return ret;
10729 }
10730
10731 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10732    but after converting arguments to type_die, afterwards
10733    convert back to unsigned.  */
10734
10735 static dw_loc_descr_ref
10736 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10737              enum machine_mode mode, enum machine_mode mem_mode)
10738 {
10739   dw_loc_descr_ref cvt, op0, op1;
10740
10741   if (type_die == NULL)
10742     return NULL;
10743   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10744                             VAR_INIT_STATUS_INITIALIZED);
10745   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10746                             VAR_INIT_STATUS_INITIALIZED);
10747   if (op0 == NULL || op1 == NULL)
10748     return NULL;
10749   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10750   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10751   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10752   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10753   add_loc_descr (&op0, cvt);
10754   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10755   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10756   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10757   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10758   add_loc_descr (&op1, cvt);
10759   add_loc_descr (&op0, op1);
10760   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10761   return convert_descriptor_to_mode (mode, op0);
10762 }
10763
10764 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10765    const0 is DW_OP_lit0 or corresponding typed constant,
10766    const1 is DW_OP_lit1 or corresponding typed constant
10767    and constMSB is constant with just the MSB bit set
10768    for the mode):
10769        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10770    L1: const0 DW_OP_swap
10771    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10772        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10773    L3: DW_OP_drop
10774    L4: DW_OP_nop
10775
10776    CTZ is similar:
10777        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10778    L1: const0 DW_OP_swap
10779    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10780        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10781    L3: DW_OP_drop
10782    L4: DW_OP_nop
10783
10784    FFS is similar:
10785        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10786    L1: const1 DW_OP_swap
10787    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10788        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10789    L3: DW_OP_drop
10790    L4: DW_OP_nop  */
10791
10792 static dw_loc_descr_ref
10793 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10794                     enum machine_mode mem_mode)
10795 {
10796   dw_loc_descr_ref op0, ret, tmp;
10797   HOST_WIDE_INT valv;
10798   dw_loc_descr_ref l1jump, l1label;
10799   dw_loc_descr_ref l2jump, l2label;
10800   dw_loc_descr_ref l3jump, l3label;
10801   dw_loc_descr_ref l4jump, l4label;
10802   rtx msb;
10803
10804   if (GET_MODE_CLASS (mode) != MODE_INT
10805       || GET_MODE (XEXP (rtl, 0)) != mode
10806       || (GET_CODE (rtl) == CLZ
10807           && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
10808     return NULL;
10809
10810   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10811                             VAR_INIT_STATUS_INITIALIZED);
10812   if (op0 == NULL)
10813     return NULL;
10814   ret = op0;
10815   if (GET_CODE (rtl) == CLZ)
10816     {
10817       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10818         valv = GET_MODE_BITSIZE (mode);
10819     }
10820   else if (GET_CODE (rtl) == FFS)
10821     valv = 0;
10822   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10823     valv = GET_MODE_BITSIZE (mode);
10824   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10825   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10826   add_loc_descr (&ret, l1jump);
10827   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10828   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10829                             VAR_INIT_STATUS_INITIALIZED);
10830   if (tmp == NULL)
10831     return NULL;
10832   add_loc_descr (&ret, tmp);
10833   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10834   add_loc_descr (&ret, l4jump);
10835   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10836                                 ? const1_rtx : const0_rtx,
10837                                 mode, mem_mode,
10838                                 VAR_INIT_STATUS_INITIALIZED);
10839   if (l1label == NULL)
10840     return NULL;
10841   add_loc_descr (&ret, l1label);
10842   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10843   l2label = new_loc_descr (DW_OP_dup, 0, 0);
10844   add_loc_descr (&ret, l2label);
10845   if (GET_CODE (rtl) != CLZ)
10846     msb = const1_rtx;
10847   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10848     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10849                    << (GET_MODE_BITSIZE (mode) - 1));
10850   else
10851     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10852                                   << (GET_MODE_BITSIZE (mode)
10853                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
10854   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10855     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10856                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10857                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10858   else
10859     tmp = mem_loc_descriptor (msb, mode, mem_mode,
10860                               VAR_INIT_STATUS_INITIALIZED);
10861   if (tmp == NULL)
10862     return NULL;
10863   add_loc_descr (&ret, tmp);
10864   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10865   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10866   add_loc_descr (&ret, l3jump);
10867   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10868                             VAR_INIT_STATUS_INITIALIZED);
10869   if (tmp == NULL)
10870     return NULL;
10871   add_loc_descr (&ret, tmp);
10872   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10873                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
10874   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10875   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10876   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10877   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10878   add_loc_descr (&ret, l2jump);
10879   l3label = new_loc_descr (DW_OP_drop, 0, 0);
10880   add_loc_descr (&ret, l3label);
10881   l4label = new_loc_descr (DW_OP_nop, 0, 0);
10882   add_loc_descr (&ret, l4label);
10883   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10884   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10885   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10886   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10887   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10888   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10889   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10890   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10891   return ret;
10892 }
10893
10894 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10895    const1 is DW_OP_lit1 or corresponding typed constant):
10896        const0 DW_OP_swap
10897    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10898        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10899    L2: DW_OP_drop
10900
10901    PARITY is similar:
10902    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10903        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10904    L2: DW_OP_drop  */
10905
10906 static dw_loc_descr_ref
10907 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10908                          enum machine_mode mem_mode)
10909 {
10910   dw_loc_descr_ref op0, ret, tmp;
10911   dw_loc_descr_ref l1jump, l1label;
10912   dw_loc_descr_ref l2jump, l2label;
10913
10914   if (GET_MODE_CLASS (mode) != MODE_INT
10915       || GET_MODE (XEXP (rtl, 0)) != mode)
10916     return NULL;
10917
10918   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10919                             VAR_INIT_STATUS_INITIALIZED);
10920   if (op0 == NULL)
10921     return NULL;
10922   ret = op0;
10923   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10924                             VAR_INIT_STATUS_INITIALIZED);
10925   if (tmp == NULL)
10926     return NULL;
10927   add_loc_descr (&ret, tmp);
10928   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10929   l1label = new_loc_descr (DW_OP_dup, 0, 0);
10930   add_loc_descr (&ret, l1label);
10931   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10932   add_loc_descr (&ret, l2jump);
10933   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10934   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
10935   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10936                             VAR_INIT_STATUS_INITIALIZED);
10937   if (tmp == NULL)
10938     return NULL;
10939   add_loc_descr (&ret, tmp);
10940   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10941   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
10942                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
10943   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10944   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10945                             VAR_INIT_STATUS_INITIALIZED);
10946   add_loc_descr (&ret, tmp);
10947   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10948   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10949   add_loc_descr (&ret, l1jump);
10950   l2label = new_loc_descr (DW_OP_drop, 0, 0);
10951   add_loc_descr (&ret, l2label);
10952   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10953   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10954   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10955   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10956   return ret;
10957 }
10958
10959 /* BSWAP (constS is initial shift count, either 56 or 24):
10960        constS const0
10961    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
10962        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
10963        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
10964        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
10965    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
10966
10967 static dw_loc_descr_ref
10968 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
10969                       enum machine_mode mem_mode)
10970 {
10971   dw_loc_descr_ref op0, ret, tmp;
10972   dw_loc_descr_ref l1jump, l1label;
10973   dw_loc_descr_ref l2jump, l2label;
10974
10975   if (GET_MODE_CLASS (mode) != MODE_INT
10976       || BITS_PER_UNIT != 8
10977       || (GET_MODE_BITSIZE (mode) != 32
10978           &&  GET_MODE_BITSIZE (mode) != 64))
10979     return NULL;
10980
10981   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10982                             VAR_INIT_STATUS_INITIALIZED);
10983   if (op0 == NULL)
10984     return NULL;
10985
10986   ret = op0;
10987   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10988                             mode, mem_mode,
10989                             VAR_INIT_STATUS_INITIALIZED);
10990   if (tmp == NULL)
10991     return NULL;
10992   add_loc_descr (&ret, tmp);
10993   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10994                             VAR_INIT_STATUS_INITIALIZED);
10995   if (tmp == NULL)
10996     return NULL;
10997   add_loc_descr (&ret, tmp);
10998   l1label = new_loc_descr (DW_OP_pick, 2, 0);
10999   add_loc_descr (&ret, l1label);
11000   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11001                             mode, mem_mode,
11002                             VAR_INIT_STATUS_INITIALIZED);
11003   add_loc_descr (&ret, tmp);
11004   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11005   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11006   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11007   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11008                             VAR_INIT_STATUS_INITIALIZED);
11009   if (tmp == NULL)
11010     return NULL;
11011   add_loc_descr (&ret, tmp);
11012   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11013   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11014   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11015   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11016   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11017   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11018   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11019                             VAR_INIT_STATUS_INITIALIZED);
11020   add_loc_descr (&ret, tmp);
11021   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11022   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11023   add_loc_descr (&ret, l2jump);
11024   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11025                             VAR_INIT_STATUS_INITIALIZED);
11026   add_loc_descr (&ret, tmp);
11027   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11028   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11029   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11030   add_loc_descr (&ret, l1jump);
11031   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11032   add_loc_descr (&ret, l2label);
11033   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11034   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11035   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11036   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11037   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11038   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11039   return ret;
11040 }
11041
11042 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11043    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11044    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11045    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11046
11047    ROTATERT is similar:
11048    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11049    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11050    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11051
11052 static dw_loc_descr_ref
11053 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11054                        enum machine_mode mem_mode)
11055 {
11056   rtx rtlop1 = XEXP (rtl, 1);
11057   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11058   int i;
11059
11060   if (GET_MODE_CLASS (mode) != MODE_INT)
11061     return NULL;
11062
11063   if (GET_MODE (rtlop1) != VOIDmode
11064       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11065     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11066   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11067                             VAR_INIT_STATUS_INITIALIZED);
11068   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11069                             VAR_INIT_STATUS_INITIALIZED);
11070   if (op0 == NULL || op1 == NULL)
11071     return NULL;
11072   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11073     for (i = 0; i < 2; i++)
11074       {
11075         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11076           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11077                                         mode, mem_mode,
11078                                         VAR_INIT_STATUS_INITIALIZED);
11079         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11080           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11081                                    ? DW_OP_const4u
11082                                    : HOST_BITS_PER_WIDE_INT == 64
11083                                    ? DW_OP_const8u : DW_OP_constu,
11084                                    GET_MODE_MASK (mode), 0);
11085         else
11086           mask[i] = NULL;
11087         if (mask[i] == NULL)
11088           return NULL;
11089         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11090       }
11091   ret = op0;
11092   add_loc_descr (&ret, op1);
11093   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11094   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11095   if (GET_CODE (rtl) == ROTATERT)
11096     {
11097       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11098       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11099                                           GET_MODE_BITSIZE (mode), 0));
11100     }
11101   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11102   if (mask[0] != NULL)
11103     add_loc_descr (&ret, mask[0]);
11104   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11105   if (mask[1] != NULL)
11106     {
11107       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11108       add_loc_descr (&ret, mask[1]);
11109       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11110     }
11111   if (GET_CODE (rtl) == ROTATE)
11112     {
11113       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11114       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11115                                           GET_MODE_BITSIZE (mode), 0));
11116     }
11117   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11118   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11119   return ret;
11120 }
11121
11122 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11123    for DEBUG_PARAMETER_REF RTL.  */
11124
11125 static dw_loc_descr_ref
11126 parameter_ref_descriptor (rtx rtl)
11127 {
11128   dw_loc_descr_ref ret;
11129   dw_die_ref ref;
11130
11131   if (dwarf_strict)
11132     return NULL;
11133   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11134   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11135   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11136   if (ref)
11137     {
11138       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11139       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11140       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11141     }
11142   else
11143     {
11144       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11145       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11146     }
11147   return ret;
11148 }
11149
11150 /* The following routine converts the RTL for a variable or parameter
11151    (resident in memory) into an equivalent Dwarf representation of a
11152    mechanism for getting the address of that same variable onto the top of a
11153    hypothetical "address evaluation" stack.
11154
11155    When creating memory location descriptors, we are effectively transforming
11156    the RTL for a memory-resident object into its Dwarf postfix expression
11157    equivalent.  This routine recursively descends an RTL tree, turning
11158    it into Dwarf postfix code as it goes.
11159
11160    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11161
11162    MEM_MODE is the mode of the memory reference, needed to handle some
11163    autoincrement addressing modes.
11164
11165    Return 0 if we can't represent the location.  */
11166
11167 dw_loc_descr_ref
11168 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11169                     enum machine_mode mem_mode,
11170                     enum var_init_status initialized)
11171 {
11172   dw_loc_descr_ref mem_loc_result = NULL;
11173   enum dwarf_location_atom op;
11174   dw_loc_descr_ref op0, op1;
11175
11176   if (mode == VOIDmode)
11177     mode = GET_MODE (rtl);
11178
11179   /* Note that for a dynamically sized array, the location we will generate a
11180      description of here will be the lowest numbered location which is
11181      actually within the array.  That's *not* necessarily the same as the
11182      zeroth element of the array.  */
11183
11184   rtl = targetm.delegitimize_address (rtl);
11185
11186   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11187     return NULL;
11188
11189   switch (GET_CODE (rtl))
11190     {
11191     case POST_INC:
11192     case POST_DEC:
11193     case POST_MODIFY:
11194       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11195
11196     case SUBREG:
11197       /* The case of a subreg may arise when we have a local (register)
11198          variable or a formal (register) parameter which doesn't quite fill
11199          up an entire register.  For now, just assume that it is
11200          legitimate to make the Dwarf info refer to the whole register which
11201          contains the given subreg.  */
11202       if (!subreg_lowpart_p (rtl))
11203         break;
11204       if (GET_MODE_CLASS (mode) == MODE_INT
11205           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11206           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11207 #ifdef POINTERS_EXTEND_UNSIGNED
11208               || (mode == Pmode && mem_mode != VOIDmode)
11209 #endif
11210              )
11211           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11212         {
11213           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11214                                                GET_MODE (SUBREG_REG (rtl)),
11215                                                mem_mode, initialized);
11216           break;
11217         }
11218       if (dwarf_strict)
11219         break;
11220       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11221         break;
11222       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11223           && (GET_MODE_CLASS (mode) != MODE_INT
11224               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11225         break;
11226       else
11227         {
11228           dw_die_ref type_die;
11229           dw_loc_descr_ref cvt;
11230
11231           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11232                                                GET_MODE (SUBREG_REG (rtl)),
11233                                                mem_mode, initialized);
11234           if (mem_loc_result == NULL)
11235             break;
11236           type_die = base_type_for_mode (mode,
11237                                          GET_MODE_CLASS (mode) == MODE_INT);
11238           if (type_die == NULL)
11239             {
11240               mem_loc_result = NULL;
11241               break;
11242             }
11243           if (GET_MODE_SIZE (mode)
11244               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11245             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11246           else
11247             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11248           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11249           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11250           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11251           add_loc_descr (&mem_loc_result, cvt);
11252         }
11253       break;
11254
11255     case REG:
11256       if (GET_MODE_CLASS (mode) != MODE_INT
11257           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11258               && rtl != arg_pointer_rtx
11259               && rtl != frame_pointer_rtx
11260 #ifdef POINTERS_EXTEND_UNSIGNED
11261               && (mode != Pmode || mem_mode == VOIDmode)
11262 #endif
11263               ))
11264         {
11265           dw_die_ref type_die;
11266
11267           if (dwarf_strict)
11268             break;
11269           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11270             break;
11271           type_die = base_type_for_mode (mode,
11272                                          GET_MODE_CLASS (mode) == MODE_INT);
11273           if (type_die == NULL)
11274             break;
11275           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11276                                           dbx_reg_number (rtl), 0);
11277           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11278           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11279           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11280           break;
11281         }
11282       /* Whenever a register number forms a part of the description of the
11283          method for calculating the (dynamic) address of a memory resident
11284          object, DWARF rules require the register number be referred to as
11285          a "base register".  This distinction is not based in any way upon
11286          what category of register the hardware believes the given register
11287          belongs to.  This is strictly DWARF terminology we're dealing with
11288          here. Note that in cases where the location of a memory-resident
11289          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11290          OP_CONST (0)) the actual DWARF location descriptor that we generate
11291          may just be OP_BASEREG (basereg).  This may look deceptively like
11292          the object in question was allocated to a register (rather than in
11293          memory) so DWARF consumers need to be aware of the subtle
11294          distinction between OP_REG and OP_BASEREG.  */
11295       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11296         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11297       else if (stack_realign_drap
11298                && crtl->drap_reg
11299                && crtl->args.internal_arg_pointer == rtl
11300                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11301         {
11302           /* If RTL is internal_arg_pointer, which has been optimized
11303              out, use DRAP instead.  */
11304           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11305                                             VAR_INIT_STATUS_INITIALIZED);
11306         }
11307       break;
11308
11309     case SIGN_EXTEND:
11310     case ZERO_EXTEND:
11311       if (GET_MODE_CLASS (mode) != MODE_INT)
11312         break;
11313       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11314                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11315       if (op0 == 0)
11316         break;
11317       else if (GET_CODE (rtl) == ZERO_EXTEND
11318                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11319                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11320                   < HOST_BITS_PER_WIDE_INT
11321                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11322                   to expand zero extend as two shifts instead of
11323                   masking.  */
11324                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11325         {
11326           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11327           mem_loc_result = op0;
11328           add_loc_descr (&mem_loc_result,
11329                          int_loc_descriptor (GET_MODE_MASK (imode)));
11330           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11331         }
11332       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11333         {
11334           int shift = DWARF2_ADDR_SIZE
11335                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11336           shift *= BITS_PER_UNIT;
11337           if (GET_CODE (rtl) == SIGN_EXTEND)
11338             op = DW_OP_shra;
11339           else
11340             op = DW_OP_shr;
11341           mem_loc_result = op0;
11342           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11343           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11344           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11345           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11346         }
11347       else if (!dwarf_strict)
11348         {
11349           dw_die_ref type_die1, type_die2;
11350           dw_loc_descr_ref cvt;
11351
11352           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11353                                           GET_CODE (rtl) == ZERO_EXTEND);
11354           if (type_die1 == NULL)
11355             break;
11356           type_die2 = base_type_for_mode (mode, 1);
11357           if (type_die2 == NULL)
11358             break;
11359           mem_loc_result = op0;
11360           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11361           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11362           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11363           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11364           add_loc_descr (&mem_loc_result, cvt);
11365           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11366           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11367           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11368           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11369           add_loc_descr (&mem_loc_result, cvt);
11370         }
11371       break;
11372
11373     case MEM:
11374       {
11375         rtx new_rtl = avoid_constant_pool_reference (rtl);
11376         if (new_rtl != rtl)
11377           {
11378             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11379                                                  initialized);
11380             if (mem_loc_result != NULL)
11381               return mem_loc_result;
11382           }
11383       }
11384       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11385                                            get_address_mode (rtl), mode,
11386                                            VAR_INIT_STATUS_INITIALIZED);
11387       if (mem_loc_result == NULL)
11388         mem_loc_result = tls_mem_loc_descriptor (rtl);
11389       if (mem_loc_result != NULL)
11390         {
11391           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11392               || GET_MODE_CLASS (mode) != MODE_INT)
11393             {
11394               dw_die_ref type_die;
11395               dw_loc_descr_ref deref;
11396
11397               if (dwarf_strict)
11398                 return NULL;
11399               type_die
11400                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11401               if (type_die == NULL)
11402                 return NULL;
11403               deref = new_loc_descr (DW_OP_GNU_deref_type,
11404                                      GET_MODE_SIZE (mode), 0);
11405               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11406               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11407               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11408               add_loc_descr (&mem_loc_result, deref);
11409             }
11410           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11411             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11412           else
11413             add_loc_descr (&mem_loc_result,
11414                            new_loc_descr (DW_OP_deref_size,
11415                                           GET_MODE_SIZE (mode), 0));
11416         }
11417       break;
11418
11419     case LO_SUM:
11420       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11421
11422     case LABEL_REF:
11423       /* Some ports can transform a symbol ref into a label ref, because
11424          the symbol ref is too far away and has to be dumped into a constant
11425          pool.  */
11426     case CONST:
11427     case SYMBOL_REF:
11428       if (GET_MODE_CLASS (mode) != MODE_INT
11429           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11430 #ifdef POINTERS_EXTEND_UNSIGNED
11431               && (mode != Pmode || mem_mode == VOIDmode)
11432 #endif
11433               ))
11434         break;
11435       if (GET_CODE (rtl) == SYMBOL_REF
11436           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11437         {
11438           dw_loc_descr_ref temp;
11439
11440           /* If this is not defined, we have no way to emit the data.  */
11441           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11442             break;
11443
11444           /* We used to emit DW_OP_addr here, but that's wrong, since
11445              DW_OP_addr should be relocated by the debug info consumer,
11446              while DW_OP_GNU_push_tls_address operand should not.  */
11447           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11448                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11449           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11450           temp->dw_loc_oprnd1.v.val_addr = rtl;
11451           temp->dtprel = true;
11452
11453           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11454           add_loc_descr (&mem_loc_result, temp);
11455
11456           break;
11457         }
11458
11459       if (!const_ok_for_output (rtl))
11460         break;
11461
11462     symref:
11463       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11464       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11465       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11466       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11467       break;
11468
11469     case CONCAT:
11470     case CONCATN:
11471     case VAR_LOCATION:
11472     case DEBUG_IMPLICIT_PTR:
11473       expansion_failed (NULL_TREE, rtl,
11474                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11475       return 0;
11476
11477     case ENTRY_VALUE:
11478       if (dwarf_strict)
11479         return NULL;
11480       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11481         {
11482           if (GET_MODE_CLASS (mode) != MODE_INT
11483               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11484             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11485                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11486           else
11487             op0
11488               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11489                                         VAR_INIT_STATUS_INITIALIZED);
11490         }
11491       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11492                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11493         {
11494           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11495                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11496           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11497             return NULL;
11498         }
11499       else
11500         gcc_unreachable ();
11501       if (op0 == NULL)
11502         return NULL;
11503       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11504       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11505       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11506       break;
11507
11508     case DEBUG_PARAMETER_REF:
11509       mem_loc_result = parameter_ref_descriptor (rtl);
11510       break;
11511
11512     case PRE_MODIFY:
11513       /* Extract the PLUS expression nested inside and fall into
11514          PLUS code below.  */
11515       rtl = XEXP (rtl, 1);
11516       goto plus;
11517
11518     case PRE_INC:
11519     case PRE_DEC:
11520       /* Turn these into a PLUS expression and fall into the PLUS code
11521          below.  */
11522       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11523                           GEN_INT (GET_CODE (rtl) == PRE_INC
11524                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11525                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11526
11527       /* ... fall through ...  */
11528
11529     case PLUS:
11530     plus:
11531       if (is_based_loc (rtl)
11532           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11533               || XEXP (rtl, 0) == arg_pointer_rtx
11534               || XEXP (rtl, 0) == frame_pointer_rtx)
11535           && GET_MODE_CLASS (mode) == MODE_INT)
11536         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11537                                           INTVAL (XEXP (rtl, 1)),
11538                                           VAR_INIT_STATUS_INITIALIZED);
11539       else
11540         {
11541           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11542                                                VAR_INIT_STATUS_INITIALIZED);
11543           if (mem_loc_result == 0)
11544             break;
11545
11546           if (CONST_INT_P (XEXP (rtl, 1))
11547               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11548             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11549           else
11550             {
11551               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11552                                         VAR_INIT_STATUS_INITIALIZED);
11553               if (op1 == 0)
11554                 break;
11555               add_loc_descr (&mem_loc_result, op1);
11556               add_loc_descr (&mem_loc_result,
11557                              new_loc_descr (DW_OP_plus, 0, 0));
11558             }
11559         }
11560       break;
11561
11562     /* If a pseudo-reg is optimized away, it is possible for it to
11563        be replaced with a MEM containing a multiply or shift.  */
11564     case MINUS:
11565       op = DW_OP_minus;
11566       goto do_binop;
11567
11568     case MULT:
11569       op = DW_OP_mul;
11570       goto do_binop;
11571
11572     case DIV:
11573       if (!dwarf_strict
11574           && GET_MODE_CLASS (mode) == MODE_INT
11575           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11576         {
11577           mem_loc_result = typed_binop (DW_OP_div, rtl,
11578                                         base_type_for_mode (mode, 0),
11579                                         mode, mem_mode);
11580           break;
11581         }
11582       op = DW_OP_div;
11583       goto do_binop;
11584
11585     case UMOD:
11586       op = DW_OP_mod;
11587       goto do_binop;
11588
11589     case ASHIFT:
11590       op = DW_OP_shl;
11591       goto do_shift;
11592
11593     case ASHIFTRT:
11594       op = DW_OP_shra;
11595       goto do_shift;
11596
11597     case LSHIFTRT:
11598       op = DW_OP_shr;
11599       goto do_shift;
11600
11601     do_shift:
11602       if (GET_MODE_CLASS (mode) != MODE_INT)
11603         break;
11604       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11605                                 VAR_INIT_STATUS_INITIALIZED);
11606       {
11607         rtx rtlop1 = XEXP (rtl, 1);
11608         if (GET_MODE (rtlop1) != VOIDmode
11609             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11610                < GET_MODE_BITSIZE (mode))
11611           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11612         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11613                                   VAR_INIT_STATUS_INITIALIZED);
11614       }
11615
11616       if (op0 == 0 || op1 == 0)
11617         break;
11618
11619       mem_loc_result = op0;
11620       add_loc_descr (&mem_loc_result, op1);
11621       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11622       break;
11623
11624     case AND:
11625       op = DW_OP_and;
11626       goto do_binop;
11627
11628     case IOR:
11629       op = DW_OP_or;
11630       goto do_binop;
11631
11632     case XOR:
11633       op = DW_OP_xor;
11634       goto do_binop;
11635
11636     do_binop:
11637       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11638                                 VAR_INIT_STATUS_INITIALIZED);
11639       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11640                                 VAR_INIT_STATUS_INITIALIZED);
11641
11642       if (op0 == 0 || op1 == 0)
11643         break;
11644
11645       mem_loc_result = op0;
11646       add_loc_descr (&mem_loc_result, op1);
11647       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11648       break;
11649
11650     case MOD:
11651       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11652         {
11653           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11654                                         base_type_for_mode (mode, 0),
11655                                         mode, mem_mode);
11656           break;
11657         }
11658
11659       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11660                                 VAR_INIT_STATUS_INITIALIZED);
11661       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11662                                 VAR_INIT_STATUS_INITIALIZED);
11663
11664       if (op0 == 0 || op1 == 0)
11665         break;
11666
11667       mem_loc_result = op0;
11668       add_loc_descr (&mem_loc_result, op1);
11669       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11670       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11671       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11672       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11673       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11674       break;
11675
11676     case UDIV:
11677       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11678         {
11679           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11680             {
11681               op = DW_OP_div;
11682               goto do_binop;
11683             }
11684           mem_loc_result = typed_binop (DW_OP_div, rtl,
11685                                         base_type_for_mode (mode, 1),
11686                                         mode, mem_mode);
11687         }
11688       break;
11689
11690     case NOT:
11691       op = DW_OP_not;
11692       goto do_unop;
11693
11694     case ABS:
11695       op = DW_OP_abs;
11696       goto do_unop;
11697
11698     case NEG:
11699       op = DW_OP_neg;
11700       goto do_unop;
11701
11702     do_unop:
11703       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11704                                 VAR_INIT_STATUS_INITIALIZED);
11705
11706       if (op0 == 0)
11707         break;
11708
11709       mem_loc_result = op0;
11710       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11711       break;
11712
11713     case CONST_INT:
11714       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11715 #ifdef POINTERS_EXTEND_UNSIGNED
11716           || (mode == Pmode
11717               && mem_mode != VOIDmode
11718               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11719 #endif
11720           )
11721         {
11722           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11723           break;
11724         }
11725       if (!dwarf_strict
11726           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11727               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
11728         {
11729           dw_die_ref type_die = base_type_for_mode (mode, 1);
11730           enum machine_mode amode;
11731           if (type_die == NULL)
11732             return NULL;
11733           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11734                                  MODE_INT, 0);
11735           if (INTVAL (rtl) >= 0
11736               && amode != BLKmode
11737               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11738               /* const DW_OP_GNU_convert <XXX> vs.
11739                  DW_OP_GNU_const_type <XXX, 1, const>.  */
11740               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11741                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11742             {
11743               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11744               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11745               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11746               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11747               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11748               add_loc_descr (&mem_loc_result, op0);
11749               return mem_loc_result;
11750             }
11751           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11752                                           INTVAL (rtl));
11753           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11754           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11755           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11756           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11757             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11758           else
11759             {
11760               mem_loc_result->dw_loc_oprnd2.val_class
11761                 = dw_val_class_const_double;
11762               mem_loc_result->dw_loc_oprnd2.v.val_double
11763                 = double_int::from_shwi (INTVAL (rtl));
11764             }
11765         }
11766       break;
11767
11768     case CONST_DOUBLE:
11769       if (!dwarf_strict)
11770         {
11771           dw_die_ref type_die;
11772
11773           /* Note that a CONST_DOUBLE rtx could represent either an integer
11774              or a floating-point constant.  A CONST_DOUBLE is used whenever
11775              the constant requires more than one word in order to be
11776              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11777           if (mode == VOIDmode
11778               || (GET_MODE (rtl) == VOIDmode
11779                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
11780             break;
11781           type_die = base_type_for_mode (mode,
11782                                          GET_MODE_CLASS (mode) == MODE_INT);
11783           if (type_die == NULL)
11784             return NULL;
11785           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11786           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11787           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11788           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11789           if (SCALAR_FLOAT_MODE_P (mode))
11790             {
11791               unsigned int length = GET_MODE_SIZE (mode);
11792               unsigned char *array
11793                   = (unsigned char*) ggc_alloc_atomic (length);
11794
11795               insert_float (rtl, array);
11796               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11797               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11798               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11799               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11800             }
11801           else
11802             {
11803               mem_loc_result->dw_loc_oprnd2.val_class
11804                 = dw_val_class_const_double;
11805               mem_loc_result->dw_loc_oprnd2.v.val_double
11806                 = rtx_to_double_int (rtl);
11807             }
11808         }
11809       break;
11810
11811     case EQ:
11812       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11813       break;
11814
11815     case GE:
11816       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11817       break;
11818
11819     case GT:
11820       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11821       break;
11822
11823     case LE:
11824       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11825       break;
11826
11827     case LT:
11828       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11829       break;
11830
11831     case NE:
11832       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11833       break;
11834
11835     case GEU:
11836       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11837       break;
11838
11839     case GTU:
11840       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11841       break;
11842
11843     case LEU:
11844       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11845       break;
11846
11847     case LTU:
11848       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11849       break;
11850
11851     case UMIN:
11852     case UMAX:
11853       if (GET_MODE_CLASS (mode) != MODE_INT)
11854         break;
11855       /* FALLTHRU */
11856     case SMIN:
11857     case SMAX:
11858       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11859       break;
11860
11861     case ZERO_EXTRACT:
11862     case SIGN_EXTRACT:
11863       if (CONST_INT_P (XEXP (rtl, 1))
11864           && CONST_INT_P (XEXP (rtl, 2))
11865           && ((unsigned) INTVAL (XEXP (rtl, 1))
11866               + (unsigned) INTVAL (XEXP (rtl, 2))
11867               <= GET_MODE_BITSIZE (mode))
11868           && GET_MODE_CLASS (mode) == MODE_INT
11869           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11870           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11871         {
11872           int shift, size;
11873           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11874                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
11875           if (op0 == 0)
11876             break;
11877           if (GET_CODE (rtl) == SIGN_EXTRACT)
11878             op = DW_OP_shra;
11879           else
11880             op = DW_OP_shr;
11881           mem_loc_result = op0;
11882           size = INTVAL (XEXP (rtl, 1));
11883           shift = INTVAL (XEXP (rtl, 2));
11884           if (BITS_BIG_ENDIAN)
11885             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11886                     - shift - size;
11887           if (shift + size != (int) DWARF2_ADDR_SIZE)
11888             {
11889               add_loc_descr (&mem_loc_result,
11890                              int_loc_descriptor (DWARF2_ADDR_SIZE
11891                                                  - shift - size));
11892               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11893             }
11894           if (size != (int) DWARF2_ADDR_SIZE)
11895             {
11896               add_loc_descr (&mem_loc_result,
11897                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11898               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11899             }
11900         }
11901       break;
11902
11903     case IF_THEN_ELSE:
11904       {
11905         dw_loc_descr_ref op2, bra_node, drop_node;
11906         op0 = mem_loc_descriptor (XEXP (rtl, 0),
11907                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
11908                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
11909                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
11910         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11911                                   VAR_INIT_STATUS_INITIALIZED);
11912         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11913                                   VAR_INIT_STATUS_INITIALIZED);
11914         if (op0 == NULL || op1 == NULL || op2 == NULL)
11915           break;
11916
11917         mem_loc_result = op1;
11918         add_loc_descr (&mem_loc_result, op2);
11919         add_loc_descr (&mem_loc_result, op0);
11920         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11921         add_loc_descr (&mem_loc_result, bra_node);
11922         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11923         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11924         add_loc_descr (&mem_loc_result, drop_node);
11925         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11926         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11927       }
11928       break;
11929
11930     case FLOAT_EXTEND:
11931     case FLOAT_TRUNCATE:
11932     case FLOAT:
11933     case UNSIGNED_FLOAT:
11934     case FIX:
11935     case UNSIGNED_FIX:
11936       if (!dwarf_strict)
11937         {
11938           dw_die_ref type_die;
11939           dw_loc_descr_ref cvt;
11940
11941           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11942                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
11943           if (op0 == NULL)
11944             break;
11945           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
11946               && (GET_CODE (rtl) == FLOAT
11947                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
11948                      <= DWARF2_ADDR_SIZE))
11949             {
11950               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11951                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
11952               if (type_die == NULL)
11953                 break;
11954               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11955               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11956               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11957               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11958               add_loc_descr (&op0, cvt);
11959             }
11960           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
11961           if (type_die == NULL)
11962             break;
11963           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11964           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11965           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11966           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11967           add_loc_descr (&op0, cvt);
11968           if (GET_MODE_CLASS (mode) == MODE_INT
11969               && (GET_CODE (rtl) == FIX
11970                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
11971             {
11972               op0 = convert_descriptor_to_mode (mode, op0);
11973               if (op0 == NULL)
11974                 break;
11975             }
11976           mem_loc_result = op0;
11977         }
11978       break;
11979
11980     case CLZ:
11981     case CTZ:
11982     case FFS:
11983       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
11984       break;
11985
11986     case POPCOUNT:
11987     case PARITY:
11988       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
11989       break;
11990
11991     case BSWAP:
11992       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
11993       break;
11994
11995     case ROTATE:
11996     case ROTATERT:
11997       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
11998       break;
11999
12000     case COMPARE:
12001     case TRUNCATE:
12002       /* In theory, we could implement the above.  */
12003       /* DWARF cannot represent the unsigned compare operations
12004          natively.  */
12005     case SS_MULT:
12006     case US_MULT:
12007     case SS_DIV:
12008     case US_DIV:
12009     case SS_PLUS:
12010     case US_PLUS:
12011     case SS_MINUS:
12012     case US_MINUS:
12013     case SS_NEG:
12014     case US_NEG:
12015     case SS_ABS:
12016     case SS_ASHIFT:
12017     case US_ASHIFT:
12018     case SS_TRUNCATE:
12019     case US_TRUNCATE:
12020     case UNORDERED:
12021     case ORDERED:
12022     case UNEQ:
12023     case UNGE:
12024     case UNGT:
12025     case UNLE:
12026     case UNLT:
12027     case LTGT:
12028     case FRACT_CONVERT:
12029     case UNSIGNED_FRACT_CONVERT:
12030     case SAT_FRACT:
12031     case UNSIGNED_SAT_FRACT:
12032     case SQRT:
12033     case ASM_OPERANDS:
12034     case VEC_MERGE:
12035     case VEC_SELECT:
12036     case VEC_CONCAT:
12037     case VEC_DUPLICATE:
12038     case UNSPEC:
12039     case HIGH:
12040     case FMA:
12041     case STRICT_LOW_PART:
12042     case CONST_VECTOR:
12043     case CONST_FIXED:
12044     case CLRSB:
12045       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12046          can't express it in the debug info.  This can happen e.g. with some
12047          TLS UNSPECs.  */
12048       break;
12049
12050     case CONST_STRING:
12051       resolve_one_addr (&rtl, NULL);
12052       goto symref;
12053
12054     default:
12055 #ifdef ENABLE_CHECKING
12056       print_rtl (stderr, rtl);
12057       gcc_unreachable ();
12058 #else
12059       break;
12060 #endif
12061     }
12062
12063   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12064     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12065
12066   return mem_loc_result;
12067 }
12068
12069 /* Return a descriptor that describes the concatenation of two locations.
12070    This is typically a complex variable.  */
12071
12072 static dw_loc_descr_ref
12073 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12074 {
12075   dw_loc_descr_ref cc_loc_result = NULL;
12076   dw_loc_descr_ref x0_ref
12077     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12078   dw_loc_descr_ref x1_ref
12079     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12080
12081   if (x0_ref == 0 || x1_ref == 0)
12082     return 0;
12083
12084   cc_loc_result = x0_ref;
12085   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12086
12087   add_loc_descr (&cc_loc_result, x1_ref);
12088   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12089
12090   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12091     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12092
12093   return cc_loc_result;
12094 }
12095
12096 /* Return a descriptor that describes the concatenation of N
12097    locations.  */
12098
12099 static dw_loc_descr_ref
12100 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12101 {
12102   unsigned int i;
12103   dw_loc_descr_ref cc_loc_result = NULL;
12104   unsigned int n = XVECLEN (concatn, 0);
12105
12106   for (i = 0; i < n; ++i)
12107     {
12108       dw_loc_descr_ref ref;
12109       rtx x = XVECEXP (concatn, 0, i);
12110
12111       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12112       if (ref == NULL)
12113         return NULL;
12114
12115       add_loc_descr (&cc_loc_result, ref);
12116       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12117     }
12118
12119   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12120     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12121
12122   return cc_loc_result;
12123 }
12124
12125 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12126    for DEBUG_IMPLICIT_PTR RTL.  */
12127
12128 static dw_loc_descr_ref
12129 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12130 {
12131   dw_loc_descr_ref ret;
12132   dw_die_ref ref;
12133
12134   if (dwarf_strict)
12135     return NULL;
12136   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12137               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12138               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12139   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12140   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12141   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12142   if (ref)
12143     {
12144       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12145       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12146       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12147     }
12148   else
12149     {
12150       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12151       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12152     }
12153   return ret;
12154 }
12155
12156 /* Output a proper Dwarf location descriptor for a variable or parameter
12157    which is either allocated in a register or in a memory location.  For a
12158    register, we just generate an OP_REG and the register number.  For a
12159    memory location we provide a Dwarf postfix expression describing how to
12160    generate the (dynamic) address of the object onto the address stack.
12161
12162    MODE is mode of the decl if this loc_descriptor is going to be used in
12163    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12164    allowed, VOIDmode otherwise.
12165
12166    If we don't know how to describe it, return 0.  */
12167
12168 static dw_loc_descr_ref
12169 loc_descriptor (rtx rtl, enum machine_mode mode,
12170                 enum var_init_status initialized)
12171 {
12172   dw_loc_descr_ref loc_result = NULL;
12173
12174   switch (GET_CODE (rtl))
12175     {
12176     case SUBREG:
12177       /* The case of a subreg may arise when we have a local (register)
12178          variable or a formal (register) parameter which doesn't quite fill
12179          up an entire register.  For now, just assume that it is
12180          legitimate to make the Dwarf info refer to the whole register which
12181          contains the given subreg.  */
12182       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12183         loc_result = loc_descriptor (SUBREG_REG (rtl),
12184                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12185       else
12186         goto do_default;
12187       break;
12188
12189     case REG:
12190       loc_result = reg_loc_descriptor (rtl, initialized);
12191       break;
12192
12193     case MEM:
12194       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12195                                        GET_MODE (rtl), initialized);
12196       if (loc_result == NULL)
12197         loc_result = tls_mem_loc_descriptor (rtl);
12198       if (loc_result == NULL)
12199         {
12200           rtx new_rtl = avoid_constant_pool_reference (rtl);
12201           if (new_rtl != rtl)
12202             loc_result = loc_descriptor (new_rtl, mode, initialized);
12203         }
12204       break;
12205
12206     case CONCAT:
12207       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12208                                           initialized);
12209       break;
12210
12211     case CONCATN:
12212       loc_result = concatn_loc_descriptor (rtl, initialized);
12213       break;
12214
12215     case VAR_LOCATION:
12216       /* Single part.  */
12217       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12218         {
12219           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12220           if (GET_CODE (loc) == EXPR_LIST)
12221             loc = XEXP (loc, 0);
12222           loc_result = loc_descriptor (loc, mode, initialized);
12223           break;
12224         }
12225
12226       rtl = XEXP (rtl, 1);
12227       /* FALLTHRU */
12228
12229     case PARALLEL:
12230       {
12231         rtvec par_elems = XVEC (rtl, 0);
12232         int num_elem = GET_NUM_ELEM (par_elems);
12233         enum machine_mode mode;
12234         int i;
12235
12236         /* Create the first one, so we have something to add to.  */
12237         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12238                                      VOIDmode, initialized);
12239         if (loc_result == NULL)
12240           return NULL;
12241         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12242         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12243         for (i = 1; i < num_elem; i++)
12244           {
12245             dw_loc_descr_ref temp;
12246
12247             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12248                                    VOIDmode, initialized);
12249             if (temp == NULL)
12250               return NULL;
12251             add_loc_descr (&loc_result, temp);
12252             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12253             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12254           }
12255       }
12256       break;
12257
12258     case CONST_INT:
12259       if (mode != VOIDmode && mode != BLKmode)
12260         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12261                                                     INTVAL (rtl));
12262       break;
12263
12264     case CONST_DOUBLE:
12265       if (mode == VOIDmode)
12266         mode = GET_MODE (rtl);
12267
12268       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12269         {
12270           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12271
12272           /* Note that a CONST_DOUBLE rtx could represent either an integer
12273              or a floating-point constant.  A CONST_DOUBLE is used whenever
12274              the constant requires more than one word in order to be
12275              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12276           loc_result = new_loc_descr (DW_OP_implicit_value,
12277                                       GET_MODE_SIZE (mode), 0);
12278           if (SCALAR_FLOAT_MODE_P (mode))
12279             {
12280               unsigned int length = GET_MODE_SIZE (mode);
12281               unsigned char *array
12282                   = (unsigned char*) ggc_alloc_atomic (length);
12283
12284               insert_float (rtl, array);
12285               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12286               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12287               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12288               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12289             }
12290           else
12291             {
12292               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12293               loc_result->dw_loc_oprnd2.v.val_double
12294                 = rtx_to_double_int (rtl);
12295             }
12296         }
12297       break;
12298
12299     case CONST_VECTOR:
12300       if (mode == VOIDmode)
12301         mode = GET_MODE (rtl);
12302
12303       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12304         {
12305           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12306           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12307           unsigned char *array = (unsigned char *)
12308             ggc_alloc_atomic (length * elt_size);
12309           unsigned int i;
12310           unsigned char *p;
12311
12312           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12313           switch (GET_MODE_CLASS (mode))
12314             {
12315             case MODE_VECTOR_INT:
12316               for (i = 0, p = array; i < length; i++, p += elt_size)
12317                 {
12318                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12319                   double_int val = rtx_to_double_int (elt);
12320
12321                   if (elt_size <= sizeof (HOST_WIDE_INT))
12322                     insert_int (val.to_shwi (), elt_size, p);
12323                   else
12324                     {
12325                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12326                       insert_double (val, p);
12327                     }
12328                 }
12329               break;
12330
12331             case MODE_VECTOR_FLOAT:
12332               for (i = 0, p = array; i < length; i++, p += elt_size)
12333                 {
12334                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12335                   insert_float (elt, p);
12336                 }
12337               break;
12338
12339             default:
12340               gcc_unreachable ();
12341             }
12342
12343           loc_result = new_loc_descr (DW_OP_implicit_value,
12344                                       length * elt_size, 0);
12345           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12346           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12347           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12348           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12349         }
12350       break;
12351
12352     case CONST:
12353       if (mode == VOIDmode
12354           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12355           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12356           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12357         {
12358           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12359           break;
12360         }
12361       /* FALLTHROUGH */
12362     case SYMBOL_REF:
12363       if (!const_ok_for_output (rtl))
12364         break;
12365     case LABEL_REF:
12366       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12367           && (dwarf_version >= 4 || !dwarf_strict))
12368         {
12369           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12370           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12371           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12372           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12373           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12374         }
12375       break;
12376
12377     case DEBUG_IMPLICIT_PTR:
12378       loc_result = implicit_ptr_descriptor (rtl, 0);
12379       break;
12380
12381     case PLUS:
12382       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12383           && CONST_INT_P (XEXP (rtl, 1)))
12384         {
12385           loc_result
12386             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12387           break;
12388         }
12389       /* FALLTHRU */
12390     do_default:
12391     default:
12392       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12393            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12394            && dwarf_version >= 4)
12395           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12396         {
12397           /* Value expression.  */
12398           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12399           if (loc_result)
12400             add_loc_descr (&loc_result,
12401                            new_loc_descr (DW_OP_stack_value, 0, 0));
12402         }
12403       break;
12404     }
12405
12406   return loc_result;
12407 }
12408
12409 /* We need to figure out what section we should use as the base for the
12410    address ranges where a given location is valid.
12411    1. If this particular DECL has a section associated with it, use that.
12412    2. If this function has a section associated with it, use that.
12413    3. Otherwise, use the text section.
12414    XXX: If you split a variable across multiple sections, we won't notice.  */
12415
12416 static const char *
12417 secname_for_decl (const_tree decl)
12418 {
12419   const char *secname;
12420
12421   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12422     {
12423       tree sectree = DECL_SECTION_NAME (decl);
12424       secname = TREE_STRING_POINTER (sectree);
12425     }
12426   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12427     {
12428       tree sectree = DECL_SECTION_NAME (current_function_decl);
12429       secname = TREE_STRING_POINTER (sectree);
12430     }
12431   else if (cfun && in_cold_section_p)
12432     secname = crtl->subsections.cold_section_label;
12433   else
12434     secname = text_section_label;
12435
12436   return secname;
12437 }
12438
12439 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12440
12441 static bool
12442 decl_by_reference_p (tree decl)
12443 {
12444   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12445            || TREE_CODE (decl) == VAR_DECL)
12446           && DECL_BY_REFERENCE (decl));
12447 }
12448
12449 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12450    for VARLOC.  */
12451
12452 static dw_loc_descr_ref
12453 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12454                enum var_init_status initialized)
12455 {
12456   int have_address = 0;
12457   dw_loc_descr_ref descr;
12458   enum machine_mode mode;
12459
12460   if (want_address != 2)
12461     {
12462       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12463       /* Single part.  */
12464       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12465         {
12466           varloc = PAT_VAR_LOCATION_LOC (varloc);
12467           if (GET_CODE (varloc) == EXPR_LIST)
12468             varloc = XEXP (varloc, 0);
12469           mode = GET_MODE (varloc);
12470           if (MEM_P (varloc))
12471             {
12472               rtx addr = XEXP (varloc, 0);
12473               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12474                                           mode, initialized);
12475               if (descr)
12476                 have_address = 1;
12477               else
12478                 {
12479                   rtx x = avoid_constant_pool_reference (varloc);
12480                   if (x != varloc)
12481                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12482                                                 initialized);
12483                 }
12484             }
12485           else
12486             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12487         }
12488       else
12489         return 0;
12490     }
12491   else
12492     {
12493       if (GET_CODE (varloc) == VAR_LOCATION)
12494         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12495       else
12496         mode = DECL_MODE (loc);
12497       descr = loc_descriptor (varloc, mode, initialized);
12498       have_address = 1;
12499     }
12500
12501   if (!descr)
12502     return 0;
12503
12504   if (want_address == 2 && !have_address
12505       && (dwarf_version >= 4 || !dwarf_strict))
12506     {
12507       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12508         {
12509           expansion_failed (loc, NULL_RTX,
12510                             "DWARF address size mismatch");
12511           return 0;
12512         }
12513       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12514       have_address = 1;
12515     }
12516   /* Show if we can't fill the request for an address.  */
12517   if (want_address && !have_address)
12518     {
12519       expansion_failed (loc, NULL_RTX,
12520                         "Want address and only have value");
12521       return 0;
12522     }
12523
12524   /* If we've got an address and don't want one, dereference.  */
12525   if (!want_address && have_address)
12526     {
12527       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12528       enum dwarf_location_atom op;
12529
12530       if (size > DWARF2_ADDR_SIZE || size == -1)
12531         {
12532           expansion_failed (loc, NULL_RTX,
12533                             "DWARF address size mismatch");
12534           return 0;
12535         }
12536       else if (size == DWARF2_ADDR_SIZE)
12537         op = DW_OP_deref;
12538       else
12539         op = DW_OP_deref_size;
12540
12541       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12542     }
12543
12544   return descr;
12545 }
12546
12547 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12548    if it is not possible.  */
12549
12550 static dw_loc_descr_ref
12551 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12552 {
12553   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12554     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12555   else if (dwarf_version >= 3 || !dwarf_strict)
12556     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12557   else
12558     return NULL;
12559 }
12560
12561 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12562    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12563
12564 static dw_loc_descr_ref
12565 dw_sra_loc_expr (tree decl, rtx loc)
12566 {
12567   rtx p;
12568   unsigned int padsize = 0;
12569   dw_loc_descr_ref descr, *descr_tail;
12570   unsigned HOST_WIDE_INT decl_size;
12571   rtx varloc;
12572   enum var_init_status initialized;
12573
12574   if (DECL_SIZE (decl) == NULL
12575       || !host_integerp (DECL_SIZE (decl), 1))
12576     return NULL;
12577
12578   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12579   descr = NULL;
12580   descr_tail = &descr;
12581
12582   for (p = loc; p; p = XEXP (p, 1))
12583     {
12584       unsigned int bitsize = decl_piece_bitsize (p);
12585       rtx loc_note = *decl_piece_varloc_ptr (p);
12586       dw_loc_descr_ref cur_descr;
12587       dw_loc_descr_ref *tail, last = NULL;
12588       unsigned int opsize = 0;
12589
12590       if (loc_note == NULL_RTX
12591           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12592         {
12593           padsize += bitsize;
12594           continue;
12595         }
12596       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12597       varloc = NOTE_VAR_LOCATION (loc_note);
12598       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12599       if (cur_descr == NULL)
12600         {
12601           padsize += bitsize;
12602           continue;
12603         }
12604
12605       /* Check that cur_descr either doesn't use
12606          DW_OP_*piece operations, or their sum is equal
12607          to bitsize.  Otherwise we can't embed it.  */
12608       for (tail = &cur_descr; *tail != NULL;
12609            tail = &(*tail)->dw_loc_next)
12610         if ((*tail)->dw_loc_opc == DW_OP_piece)
12611           {
12612             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12613                       * BITS_PER_UNIT;
12614             last = *tail;
12615           }
12616         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12617           {
12618             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12619             last = *tail;
12620           }
12621
12622       if (last != NULL && opsize != bitsize)
12623         {
12624           padsize += bitsize;
12625           continue;
12626         }
12627
12628       /* If there is a hole, add DW_OP_*piece after empty DWARF
12629          expression, which means that those bits are optimized out.  */
12630       if (padsize)
12631         {
12632           if (padsize > decl_size)
12633             return NULL;
12634           decl_size -= padsize;
12635           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12636           if (*descr_tail == NULL)
12637             return NULL;
12638           descr_tail = &(*descr_tail)->dw_loc_next;
12639           padsize = 0;
12640         }
12641       *descr_tail = cur_descr;
12642       descr_tail = tail;
12643       if (bitsize > decl_size)
12644         return NULL;
12645       decl_size -= bitsize;
12646       if (last == NULL)
12647         {
12648           HOST_WIDE_INT offset = 0;
12649           if (GET_CODE (varloc) == VAR_LOCATION
12650               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12651             {
12652               varloc = PAT_VAR_LOCATION_LOC (varloc);
12653               if (GET_CODE (varloc) == EXPR_LIST)
12654                 varloc = XEXP (varloc, 0);
12655             }
12656           do 
12657             {
12658               if (GET_CODE (varloc) == CONST
12659                   || GET_CODE (varloc) == SIGN_EXTEND
12660                   || GET_CODE (varloc) == ZERO_EXTEND)
12661                 varloc = XEXP (varloc, 0);
12662               else if (GET_CODE (varloc) == SUBREG)
12663                 varloc = SUBREG_REG (varloc);
12664               else
12665                 break;
12666             }
12667           while (1);
12668           /* DW_OP_bit_size offset should be zero for register
12669              or implicit location descriptions and empty location
12670              descriptions, but for memory addresses needs big endian
12671              adjustment.  */
12672           if (MEM_P (varloc))
12673             {
12674               unsigned HOST_WIDE_INT memsize
12675                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12676               if (memsize != bitsize)
12677                 {
12678                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12679                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12680                     return NULL;
12681                   if (memsize < bitsize)
12682                     return NULL;
12683                   if (BITS_BIG_ENDIAN)
12684                     offset = memsize - bitsize;
12685                 }
12686             }
12687
12688           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12689           if (*descr_tail == NULL)
12690             return NULL;
12691           descr_tail = &(*descr_tail)->dw_loc_next;
12692         }
12693     }
12694
12695   /* If there were any non-empty expressions, add padding till the end of
12696      the decl.  */
12697   if (descr != NULL && decl_size != 0)
12698     {
12699       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12700       if (*descr_tail == NULL)
12701         return NULL;
12702     }
12703   return descr;
12704 }
12705
12706 /* Return the dwarf representation of the location list LOC_LIST of
12707    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12708    function.  */
12709
12710 static dw_loc_list_ref
12711 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12712 {
12713   const char *endname, *secname;
12714   rtx varloc;
12715   enum var_init_status initialized;
12716   struct var_loc_node *node;
12717   dw_loc_descr_ref descr;
12718   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12719   dw_loc_list_ref list = NULL;
12720   dw_loc_list_ref *listp = &list;
12721
12722   /* Now that we know what section we are using for a base,
12723      actually construct the list of locations.
12724      The first location information is what is passed to the
12725      function that creates the location list, and the remaining
12726      locations just get added on to that list.
12727      Note that we only know the start address for a location
12728      (IE location changes), so to build the range, we use
12729      the range [current location start, next location start].
12730      This means we have to special case the last node, and generate
12731      a range of [last location start, end of function label].  */
12732
12733   secname = secname_for_decl (decl);
12734
12735   for (node = loc_list->first; node; node = node->next)
12736     if (GET_CODE (node->loc) == EXPR_LIST
12737         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12738       {
12739         if (GET_CODE (node->loc) == EXPR_LIST)
12740           {
12741             /* This requires DW_OP_{,bit_}piece, which is not usable
12742                inside DWARF expressions.  */
12743             if (want_address != 2)
12744               continue;
12745             descr = dw_sra_loc_expr (decl, node->loc);
12746             if (descr == NULL)
12747               continue;
12748           }
12749         else
12750           {
12751             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12752             varloc = NOTE_VAR_LOCATION (node->loc);
12753             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12754           }
12755         if (descr)
12756           {
12757             bool range_across_switch = false;
12758             /* If section switch happens in between node->label
12759                and node->next->label (or end of function) and
12760                we can't emit it as a single entry list,
12761                emit two ranges, first one ending at the end
12762                of first partition and second one starting at the
12763                beginning of second partition.  */
12764             if (node == loc_list->last_before_switch
12765                 && (node != loc_list->first || loc_list->first->next)
12766                 && current_function_decl)
12767               {
12768                 endname = cfun->fde->dw_fde_end;
12769                 range_across_switch = true;
12770               }
12771             /* The variable has a location between NODE->LABEL and
12772                NODE->NEXT->LABEL.  */
12773             else if (node->next)
12774               endname = node->next->label;
12775             /* If the variable has a location at the last label
12776                it keeps its location until the end of function.  */
12777             else if (!current_function_decl)
12778               endname = text_end_label;
12779             else
12780               {
12781                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12782                                              current_function_funcdef_no);
12783                 endname = ggc_strdup (label_id);
12784               }
12785
12786             *listp = new_loc_list (descr, node->label, endname, secname);
12787             if (TREE_CODE (decl) == PARM_DECL
12788                 && node == loc_list->first
12789                 && GET_CODE (node->loc) == NOTE
12790                 && strcmp (node->label, endname) == 0)
12791               (*listp)->force = true;
12792             listp = &(*listp)->dw_loc_next;
12793
12794             if (range_across_switch)
12795               {
12796                 if (GET_CODE (node->loc) == EXPR_LIST)
12797                   descr = dw_sra_loc_expr (decl, node->loc);
12798                 else
12799                   {
12800                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12801                     varloc = NOTE_VAR_LOCATION (node->loc);
12802                     descr = dw_loc_list_1 (decl, varloc, want_address,
12803                                            initialized);
12804                   }
12805                 gcc_assert (descr);
12806                 /* The variable has a location between NODE->LABEL and
12807                    NODE->NEXT->LABEL.  */
12808                 if (node->next)
12809                   endname = node->next->label;
12810                 else
12811                   endname = cfun->fde->dw_fde_second_end;
12812                 *listp = new_loc_list (descr,
12813                                        cfun->fde->dw_fde_second_begin,
12814                                        endname, secname);
12815                 listp = &(*listp)->dw_loc_next;
12816               }
12817           }
12818       }
12819
12820   /* Try to avoid the overhead of a location list emitting a location
12821      expression instead, but only if we didn't have more than one
12822      location entry in the first place.  If some entries were not
12823      representable, we don't want to pretend a single entry that was
12824      applies to the entire scope in which the variable is
12825      available.  */
12826   if (list && loc_list->first->next)
12827     gen_llsym (list);
12828
12829   return list;
12830 }
12831
12832 /* Return if the loc_list has only single element and thus can be represented
12833    as location description.   */
12834
12835 static bool
12836 single_element_loc_list_p (dw_loc_list_ref list)
12837 {
12838   gcc_assert (!list->dw_loc_next || list->ll_symbol);
12839   return !list->ll_symbol;
12840 }
12841
12842 /* To each location in list LIST add loc descr REF.  */
12843
12844 static void
12845 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12846 {
12847   dw_loc_descr_ref copy;
12848   add_loc_descr (&list->expr, ref);
12849   list = list->dw_loc_next;
12850   while (list)
12851     {
12852       copy = ggc_alloc_dw_loc_descr_node ();
12853       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12854       add_loc_descr (&list->expr, copy);
12855       while (copy->dw_loc_next)
12856         {
12857           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12858           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12859           copy->dw_loc_next = new_copy;
12860           copy = new_copy;
12861         }
12862       list = list->dw_loc_next;
12863     }
12864 }
12865
12866 /* Given two lists RET and LIST
12867    produce location list that is result of adding expression in LIST
12868    to expression in RET on each position in program.
12869    Might be destructive on both RET and LIST.
12870
12871    TODO: We handle only simple cases of RET or LIST having at most one
12872    element. General case would inolve sorting the lists in program order
12873    and merging them that will need some additional work.
12874    Adding that will improve quality of debug info especially for SRA-ed
12875    structures.  */
12876
12877 static void
12878 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12879 {
12880   if (!list)
12881     return;
12882   if (!*ret)
12883     {
12884       *ret = list;
12885       return;
12886     }
12887   if (!list->dw_loc_next)
12888     {
12889       add_loc_descr_to_each (*ret, list->expr);
12890       return;
12891     }
12892   if (!(*ret)->dw_loc_next)
12893     {
12894       add_loc_descr_to_each (list, (*ret)->expr);
12895       *ret = list;
12896       return;
12897     }
12898   expansion_failed (NULL_TREE, NULL_RTX,
12899                     "Don't know how to merge two non-trivial"
12900                     " location lists.\n");
12901   *ret = NULL;
12902   return;
12903 }
12904
12905 /* LOC is constant expression.  Try a luck, look it up in constant
12906    pool and return its loc_descr of its address.  */
12907
12908 static dw_loc_descr_ref
12909 cst_pool_loc_descr (tree loc)
12910 {
12911   /* Get an RTL for this, if something has been emitted.  */
12912   rtx rtl = lookup_constant_def (loc);
12913
12914   if (!rtl || !MEM_P (rtl))
12915     {
12916       gcc_assert (!rtl);
12917       return 0;
12918     }
12919   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12920
12921   /* TODO: We might get more coverage if we was actually delaying expansion
12922      of all expressions till end of compilation when constant pools are fully
12923      populated.  */
12924   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12925     {
12926       expansion_failed (loc, NULL_RTX,
12927                         "CST value in contant pool but not marked.");
12928       return 0;
12929     }
12930   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12931                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
12932 }
12933
12934 /* Return dw_loc_list representing address of addr_expr LOC
12935    by looking for inner INDIRECT_REF expression and turning
12936    it into simple arithmetics.  */
12937
12938 static dw_loc_list_ref
12939 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12940 {
12941   tree obj, offset;
12942   HOST_WIDE_INT bitsize, bitpos, bytepos;
12943   enum machine_mode mode;
12944   int unsignedp, volatilep = 0;
12945   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12946
12947   obj = get_inner_reference (TREE_OPERAND (loc, 0),
12948                              &bitsize, &bitpos, &offset, &mode,
12949                              &unsignedp, &volatilep, false);
12950   STRIP_NOPS (obj);
12951   if (bitpos % BITS_PER_UNIT)
12952     {
12953       expansion_failed (loc, NULL_RTX, "bitfield access");
12954       return 0;
12955     }
12956   if (!INDIRECT_REF_P (obj))
12957     {
12958       expansion_failed (obj,
12959                         NULL_RTX, "no indirect ref in inner refrence");
12960       return 0;
12961     }
12962   if (!offset && !bitpos)
12963     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12964   else if (toplev
12965            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12966            && (dwarf_version >= 4 || !dwarf_strict))
12967     {
12968       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12969       if (!list_ret)
12970         return 0;
12971       if (offset)
12972         {
12973           /* Variable offset.  */
12974           list_ret1 = loc_list_from_tree (offset, 0);
12975           if (list_ret1 == 0)
12976             return 0;
12977           add_loc_list (&list_ret, list_ret1);
12978           if (!list_ret)
12979             return 0;
12980           add_loc_descr_to_each (list_ret,
12981                                  new_loc_descr (DW_OP_plus, 0, 0));
12982         }
12983       bytepos = bitpos / BITS_PER_UNIT;
12984       if (bytepos > 0)
12985         add_loc_descr_to_each (list_ret,
12986                                new_loc_descr (DW_OP_plus_uconst,
12987                                               bytepos, 0));
12988       else if (bytepos < 0)
12989         loc_list_plus_const (list_ret, bytepos);
12990       add_loc_descr_to_each (list_ret,
12991                              new_loc_descr (DW_OP_stack_value, 0, 0));
12992     }
12993   return list_ret;
12994 }
12995
12996
12997 /* Generate Dwarf location list representing LOC.
12998    If WANT_ADDRESS is false, expression computing LOC will be computed
12999    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13000    if WANT_ADDRESS is 2, expression computing address useable in location
13001      will be returned (i.e. DW_OP_reg can be used
13002      to refer to register values).  */
13003
13004 static dw_loc_list_ref
13005 loc_list_from_tree (tree loc, int want_address)
13006 {
13007   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13008   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13009   int have_address = 0;
13010   enum dwarf_location_atom op;
13011
13012   /* ??? Most of the time we do not take proper care for sign/zero
13013      extending the values properly.  Hopefully this won't be a real
13014      problem...  */
13015
13016   switch (TREE_CODE (loc))
13017     {
13018     case ERROR_MARK:
13019       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13020       return 0;
13021
13022     case PLACEHOLDER_EXPR:
13023       /* This case involves extracting fields from an object to determine the
13024          position of other fields.  We don't try to encode this here.  The
13025          only user of this is Ada, which encodes the needed information using
13026          the names of types.  */
13027       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13028       return 0;
13029
13030     case CALL_EXPR:
13031       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13032       /* There are no opcodes for these operations.  */
13033       return 0;
13034
13035     case PREINCREMENT_EXPR:
13036     case PREDECREMENT_EXPR:
13037     case POSTINCREMENT_EXPR:
13038     case POSTDECREMENT_EXPR:
13039       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13040       /* There are no opcodes for these operations.  */
13041       return 0;
13042
13043     case ADDR_EXPR:
13044       /* If we already want an address, see if there is INDIRECT_REF inside
13045          e.g. for &this->field.  */
13046       if (want_address)
13047         {
13048           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13049                        (loc, want_address == 2);
13050           if (list_ret)
13051             have_address = 1;
13052           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13053                    && (ret = cst_pool_loc_descr (loc)))
13054             have_address = 1;
13055         }
13056         /* Otherwise, process the argument and look for the address.  */
13057       if (!list_ret && !ret)
13058         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13059       else
13060         {
13061           if (want_address)
13062             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13063           return NULL;
13064         }
13065       break;
13066
13067     case VAR_DECL:
13068       if (DECL_THREAD_LOCAL_P (loc))
13069         {
13070           rtx rtl;
13071           enum dwarf_location_atom first_op;
13072           enum dwarf_location_atom second_op;
13073           bool dtprel = false;
13074
13075           if (targetm.have_tls)
13076             {
13077               /* If this is not defined, we have no way to emit the
13078                  data.  */
13079               if (!targetm.asm_out.output_dwarf_dtprel)
13080                 return 0;
13081
13082                /* The way DW_OP_GNU_push_tls_address is specified, we
13083                   can only look up addresses of objects in the current
13084                   module.  We used DW_OP_addr as first op, but that's
13085                   wrong, because DW_OP_addr is relocated by the debug
13086                   info consumer, while DW_OP_GNU_push_tls_address
13087                   operand shouldn't be.  */
13088               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13089                 return 0;
13090               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13091               dtprel = true;
13092               second_op = DW_OP_GNU_push_tls_address;
13093             }
13094           else
13095             {
13096               if (!targetm.emutls.debug_form_tls_address
13097                   || !(dwarf_version >= 3 || !dwarf_strict))
13098                 return 0;
13099               /* We stuffed the control variable into the DECL_VALUE_EXPR
13100                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13101                  no longer appear in gimple code.  We used the control
13102                  variable in specific so that we could pick it up here.  */
13103               loc = DECL_VALUE_EXPR (loc);
13104               first_op = DW_OP_addr;
13105               second_op = DW_OP_form_tls_address;
13106             }
13107
13108           rtl = rtl_for_decl_location (loc);
13109           if (rtl == NULL_RTX)
13110             return 0;
13111
13112           if (!MEM_P (rtl))
13113             return 0;
13114           rtl = XEXP (rtl, 0);
13115           if (! CONSTANT_P (rtl))
13116             return 0;
13117
13118           ret = new_loc_descr (first_op, 0, 0);
13119           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13120           ret->dw_loc_oprnd1.v.val_addr = rtl;
13121           ret->dtprel = dtprel;
13122
13123           ret1 = new_loc_descr (second_op, 0, 0);
13124           add_loc_descr (&ret, ret1);
13125
13126           have_address = 1;
13127           break;
13128         }
13129       /* FALLTHRU */
13130
13131     case PARM_DECL:
13132     case RESULT_DECL:
13133       if (DECL_HAS_VALUE_EXPR_P (loc))
13134         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13135                                    want_address);
13136       /* FALLTHRU */
13137
13138     case FUNCTION_DECL:
13139       {
13140         rtx rtl;
13141         var_loc_list *loc_list = lookup_decl_loc (loc);
13142
13143         if (loc_list && loc_list->first)
13144           {
13145             list_ret = dw_loc_list (loc_list, loc, want_address);
13146             have_address = want_address != 0;
13147             break;
13148           }
13149         rtl = rtl_for_decl_location (loc);
13150         if (rtl == NULL_RTX)
13151           {
13152             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13153             return 0;
13154           }
13155         else if (CONST_INT_P (rtl))
13156           {
13157             HOST_WIDE_INT val = INTVAL (rtl);
13158             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13159               val &= GET_MODE_MASK (DECL_MODE (loc));
13160             ret = int_loc_descriptor (val);
13161           }
13162         else if (GET_CODE (rtl) == CONST_STRING)
13163           {
13164             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13165             return 0;
13166           }
13167         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13168           {
13169             ret = new_loc_descr (DW_OP_addr, 0, 0);
13170             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13171             ret->dw_loc_oprnd1.v.val_addr = rtl;
13172           }
13173         else
13174           {
13175             enum machine_mode mode, mem_mode;
13176
13177             /* Certain constructs can only be represented at top-level.  */
13178             if (want_address == 2)
13179               {
13180                 ret = loc_descriptor (rtl, VOIDmode,
13181                                       VAR_INIT_STATUS_INITIALIZED);
13182                 have_address = 1;
13183               }
13184             else
13185               {
13186                 mode = GET_MODE (rtl);
13187                 mem_mode = VOIDmode;
13188                 if (MEM_P (rtl))
13189                   {
13190                     mem_mode = mode;
13191                     mode = get_address_mode (rtl);
13192                     rtl = XEXP (rtl, 0);
13193                     have_address = 1;
13194                   }
13195                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13196                                           VAR_INIT_STATUS_INITIALIZED);
13197               }
13198             if (!ret)
13199               expansion_failed (loc, rtl,
13200                                 "failed to produce loc descriptor for rtl");
13201           }
13202       }
13203       break;
13204
13205     case MEM_REF:
13206       /* ??? FIXME.  */
13207       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13208         return 0;
13209       /* Fallthru.  */
13210     case INDIRECT_REF:
13211       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13212       have_address = 1;
13213       break;
13214
13215     case COMPOUND_EXPR:
13216       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13217
13218     CASE_CONVERT:
13219     case VIEW_CONVERT_EXPR:
13220     case SAVE_EXPR:
13221     case MODIFY_EXPR:
13222       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13223
13224     case COMPONENT_REF:
13225     case BIT_FIELD_REF:
13226     case ARRAY_REF:
13227     case ARRAY_RANGE_REF:
13228     case REALPART_EXPR:
13229     case IMAGPART_EXPR:
13230       {
13231         tree obj, offset;
13232         HOST_WIDE_INT bitsize, bitpos, bytepos;
13233         enum machine_mode mode;
13234         int unsignedp, volatilep = 0;
13235
13236         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13237                                    &unsignedp, &volatilep, false);
13238
13239         gcc_assert (obj != loc);
13240
13241         list_ret = loc_list_from_tree (obj,
13242                                        want_address == 2
13243                                        && !bitpos && !offset ? 2 : 1);
13244         /* TODO: We can extract value of the small expression via shifting even
13245            for nonzero bitpos.  */
13246         if (list_ret == 0)
13247           return 0;
13248         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13249           {
13250             expansion_failed (loc, NULL_RTX,
13251                               "bitfield access");
13252             return 0;
13253           }
13254
13255         if (offset != NULL_TREE)
13256           {
13257             /* Variable offset.  */
13258             list_ret1 = loc_list_from_tree (offset, 0);
13259             if (list_ret1 == 0)
13260               return 0;
13261             add_loc_list (&list_ret, list_ret1);
13262             if (!list_ret)
13263               return 0;
13264             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13265           }
13266
13267         bytepos = bitpos / BITS_PER_UNIT;
13268         if (bytepos > 0)
13269           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13270         else if (bytepos < 0)
13271           loc_list_plus_const (list_ret, bytepos);
13272
13273         have_address = 1;
13274         break;
13275       }
13276
13277     case INTEGER_CST:
13278       if ((want_address || !host_integerp (loc, 0))
13279           && (ret = cst_pool_loc_descr (loc)))
13280         have_address = 1;
13281       else if (want_address == 2
13282                && host_integerp (loc, 0)
13283                && (ret = address_of_int_loc_descriptor
13284                            (int_size_in_bytes (TREE_TYPE (loc)),
13285                             tree_low_cst (loc, 0))))
13286         have_address = 1;
13287       else if (host_integerp (loc, 0))
13288         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13289       else
13290         {
13291           expansion_failed (loc, NULL_RTX,
13292                             "Integer operand is not host integer");
13293           return 0;
13294         }
13295       break;
13296
13297     case CONSTRUCTOR:
13298     case REAL_CST:
13299     case STRING_CST:
13300     case COMPLEX_CST:
13301       if ((ret = cst_pool_loc_descr (loc)))
13302         have_address = 1;
13303       else
13304       /* We can construct small constants here using int_loc_descriptor.  */
13305         expansion_failed (loc, NULL_RTX,
13306                           "constructor or constant not in constant pool");
13307       break;
13308
13309     case TRUTH_AND_EXPR:
13310     case TRUTH_ANDIF_EXPR:
13311     case BIT_AND_EXPR:
13312       op = DW_OP_and;
13313       goto do_binop;
13314
13315     case TRUTH_XOR_EXPR:
13316     case BIT_XOR_EXPR:
13317       op = DW_OP_xor;
13318       goto do_binop;
13319
13320     case TRUTH_OR_EXPR:
13321     case TRUTH_ORIF_EXPR:
13322     case BIT_IOR_EXPR:
13323       op = DW_OP_or;
13324       goto do_binop;
13325
13326     case FLOOR_DIV_EXPR:
13327     case CEIL_DIV_EXPR:
13328     case ROUND_DIV_EXPR:
13329     case TRUNC_DIV_EXPR:
13330       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13331         return 0;
13332       op = DW_OP_div;
13333       goto do_binop;
13334
13335     case MINUS_EXPR:
13336       op = DW_OP_minus;
13337       goto do_binop;
13338
13339     case FLOOR_MOD_EXPR:
13340     case CEIL_MOD_EXPR:
13341     case ROUND_MOD_EXPR:
13342     case TRUNC_MOD_EXPR:
13343       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13344         {
13345           op = DW_OP_mod;
13346           goto do_binop;
13347         }
13348       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13349       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13350       if (list_ret == 0 || list_ret1 == 0)
13351         return 0;
13352
13353       add_loc_list (&list_ret, list_ret1);
13354       if (list_ret == 0)
13355         return 0;
13356       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13357       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13358       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13359       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13360       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13361       break;
13362
13363     case MULT_EXPR:
13364       op = DW_OP_mul;
13365       goto do_binop;
13366
13367     case LSHIFT_EXPR:
13368       op = DW_OP_shl;
13369       goto do_binop;
13370
13371     case RSHIFT_EXPR:
13372       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13373       goto do_binop;
13374
13375     case POINTER_PLUS_EXPR:
13376     case PLUS_EXPR:
13377       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13378         {
13379           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13380           if (list_ret == 0)
13381             return 0;
13382
13383           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13384           break;
13385         }
13386
13387       op = DW_OP_plus;
13388       goto do_binop;
13389
13390     case LE_EXPR:
13391       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13392         return 0;
13393
13394       op = DW_OP_le;
13395       goto do_binop;
13396
13397     case GE_EXPR:
13398       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13399         return 0;
13400
13401       op = DW_OP_ge;
13402       goto do_binop;
13403
13404     case LT_EXPR:
13405       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13406         return 0;
13407
13408       op = DW_OP_lt;
13409       goto do_binop;
13410
13411     case GT_EXPR:
13412       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13413         return 0;
13414
13415       op = DW_OP_gt;
13416       goto do_binop;
13417
13418     case EQ_EXPR:
13419       op = DW_OP_eq;
13420       goto do_binop;
13421
13422     case NE_EXPR:
13423       op = DW_OP_ne;
13424       goto do_binop;
13425
13426     do_binop:
13427       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13428       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13429       if (list_ret == 0 || list_ret1 == 0)
13430         return 0;
13431
13432       add_loc_list (&list_ret, list_ret1);
13433       if (list_ret == 0)
13434         return 0;
13435       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13436       break;
13437
13438     case TRUTH_NOT_EXPR:
13439     case BIT_NOT_EXPR:
13440       op = DW_OP_not;
13441       goto do_unop;
13442
13443     case ABS_EXPR:
13444       op = DW_OP_abs;
13445       goto do_unop;
13446
13447     case NEGATE_EXPR:
13448       op = DW_OP_neg;
13449       goto do_unop;
13450
13451     do_unop:
13452       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13453       if (list_ret == 0)
13454         return 0;
13455
13456       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13457       break;
13458
13459     case MIN_EXPR:
13460     case MAX_EXPR:
13461       {
13462         const enum tree_code code =
13463           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13464
13465         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13466                       build2 (code, integer_type_node,
13467                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13468                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13469       }
13470
13471       /* ... fall through ...  */
13472
13473     case COND_EXPR:
13474       {
13475         dw_loc_descr_ref lhs
13476           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13477         dw_loc_list_ref rhs
13478           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13479         dw_loc_descr_ref bra_node, jump_node, tmp;
13480
13481         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13482         if (list_ret == 0 || lhs == 0 || rhs == 0)
13483           return 0;
13484
13485         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13486         add_loc_descr_to_each (list_ret, bra_node);
13487
13488         add_loc_list (&list_ret, rhs);
13489         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13490         add_loc_descr_to_each (list_ret, jump_node);
13491
13492         add_loc_descr_to_each (list_ret, lhs);
13493         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13494         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13495
13496         /* ??? Need a node to point the skip at.  Use a nop.  */
13497         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13498         add_loc_descr_to_each (list_ret, tmp);
13499         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13500         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13501       }
13502       break;
13503
13504     case FIX_TRUNC_EXPR:
13505       return 0;
13506
13507     default:
13508       /* Leave front-end specific codes as simply unknown.  This comes
13509          up, for instance, with the C STMT_EXPR.  */
13510       if ((unsigned int) TREE_CODE (loc)
13511           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13512         {
13513           expansion_failed (loc, NULL_RTX,
13514                             "language specific tree node");
13515           return 0;
13516         }
13517
13518 #ifdef ENABLE_CHECKING
13519       /* Otherwise this is a generic code; we should just lists all of
13520          these explicitly.  We forgot one.  */
13521       gcc_unreachable ();
13522 #else
13523       /* In a release build, we want to degrade gracefully: better to
13524          generate incomplete debugging information than to crash.  */
13525       return NULL;
13526 #endif
13527     }
13528
13529   if (!ret && !list_ret)
13530     return 0;
13531
13532   if (want_address == 2 && !have_address
13533       && (dwarf_version >= 4 || !dwarf_strict))
13534     {
13535       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13536         {
13537           expansion_failed (loc, NULL_RTX,
13538                             "DWARF address size mismatch");
13539           return 0;
13540         }
13541       if (ret)
13542         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13543       else
13544         add_loc_descr_to_each (list_ret,
13545                                new_loc_descr (DW_OP_stack_value, 0, 0));
13546       have_address = 1;
13547     }
13548   /* Show if we can't fill the request for an address.  */
13549   if (want_address && !have_address)
13550     {
13551       expansion_failed (loc, NULL_RTX,
13552                         "Want address and only have value");
13553       return 0;
13554     }
13555
13556   gcc_assert (!ret || !list_ret);
13557
13558   /* If we've got an address and don't want one, dereference.  */
13559   if (!want_address && have_address)
13560     {
13561       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13562
13563       if (size > DWARF2_ADDR_SIZE || size == -1)
13564         {
13565           expansion_failed (loc, NULL_RTX,
13566                             "DWARF address size mismatch");
13567           return 0;
13568         }
13569       else if (size == DWARF2_ADDR_SIZE)
13570         op = DW_OP_deref;
13571       else
13572         op = DW_OP_deref_size;
13573
13574       if (ret)
13575         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13576       else
13577         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13578     }
13579   if (ret)
13580     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13581
13582   return list_ret;
13583 }
13584
13585 /* Same as above but return only single location expression.  */
13586 static dw_loc_descr_ref
13587 loc_descriptor_from_tree (tree loc, int want_address)
13588 {
13589   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13590   if (!ret)
13591     return NULL;
13592   if (ret->dw_loc_next)
13593     {
13594       expansion_failed (loc, NULL_RTX,
13595                         "Location list where only loc descriptor needed");
13596       return NULL;
13597     }
13598   return ret->expr;
13599 }
13600
13601 /* Given a value, round it up to the lowest multiple of `boundary'
13602    which is not less than the value itself.  */
13603
13604 static inline HOST_WIDE_INT
13605 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13606 {
13607   return (((value + boundary - 1) / boundary) * boundary);
13608 }
13609
13610 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13611    pointer to the declared type for the relevant field variable, or return
13612    `integer_type_node' if the given node turns out to be an
13613    ERROR_MARK node.  */
13614
13615 static inline tree
13616 field_type (const_tree decl)
13617 {
13618   tree type;
13619
13620   if (TREE_CODE (decl) == ERROR_MARK)
13621     return integer_type_node;
13622
13623   type = DECL_BIT_FIELD_TYPE (decl);
13624   if (type == NULL_TREE)
13625     type = TREE_TYPE (decl);
13626
13627   return type;
13628 }
13629
13630 /* Given a pointer to a tree node, return the alignment in bits for
13631    it, or else return BITS_PER_WORD if the node actually turns out to
13632    be an ERROR_MARK node.  */
13633
13634 static inline unsigned
13635 simple_type_align_in_bits (const_tree type)
13636 {
13637   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13638 }
13639
13640 static inline unsigned
13641 simple_decl_align_in_bits (const_tree decl)
13642 {
13643   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13644 }
13645
13646 /* Return the result of rounding T up to ALIGN.  */
13647
13648 static inline double_int
13649 round_up_to_align (double_int t, unsigned int align)
13650 {
13651   double_int alignd = double_int::from_uhwi (align);
13652   t += alignd;
13653   t += double_int_minus_one;
13654   t = t.div (alignd, true, TRUNC_DIV_EXPR);
13655   t *= alignd;
13656   return t;
13657 }
13658
13659 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13660    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13661    or return 0 if we are unable to determine what that offset is, either
13662    because the argument turns out to be a pointer to an ERROR_MARK node, or
13663    because the offset is actually variable.  (We can't handle the latter case
13664    just yet).  */
13665
13666 static HOST_WIDE_INT
13667 field_byte_offset (const_tree decl)
13668 {
13669   double_int object_offset_in_bits;
13670   double_int object_offset_in_bytes;
13671   double_int bitpos_int;
13672
13673   if (TREE_CODE (decl) == ERROR_MARK)
13674     return 0;
13675
13676   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13677
13678   /* We cannot yet cope with fields whose positions are variable, so
13679      for now, when we see such things, we simply return 0.  Someday, we may
13680      be able to handle such cases, but it will be damn difficult.  */
13681   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13682     return 0;
13683
13684   bitpos_int = tree_to_double_int (bit_position (decl));
13685
13686 #ifdef PCC_BITFIELD_TYPE_MATTERS
13687   if (PCC_BITFIELD_TYPE_MATTERS)
13688     {
13689       tree type;
13690       tree field_size_tree;
13691       double_int deepest_bitpos;
13692       double_int field_size_in_bits;
13693       unsigned int type_align_in_bits;
13694       unsigned int decl_align_in_bits;
13695       double_int type_size_in_bits;
13696
13697       type = field_type (decl);
13698       type_size_in_bits = double_int_type_size_in_bits (type);
13699       type_align_in_bits = simple_type_align_in_bits (type);
13700
13701       field_size_tree = DECL_SIZE (decl);
13702
13703       /* The size could be unspecified if there was an error, or for
13704          a flexible array member.  */
13705       if (!field_size_tree)
13706         field_size_tree = bitsize_zero_node;
13707
13708       /* If the size of the field is not constant, use the type size.  */
13709       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13710         field_size_in_bits = tree_to_double_int (field_size_tree);
13711       else
13712         field_size_in_bits = type_size_in_bits;
13713
13714       decl_align_in_bits = simple_decl_align_in_bits (decl);
13715
13716       /* The GCC front-end doesn't make any attempt to keep track of the
13717          starting bit offset (relative to the start of the containing
13718          structure type) of the hypothetical "containing object" for a
13719          bit-field.  Thus, when computing the byte offset value for the
13720          start of the "containing object" of a bit-field, we must deduce
13721          this information on our own. This can be rather tricky to do in
13722          some cases.  For example, handling the following structure type
13723          definition when compiling for an i386/i486 target (which only
13724          aligns long long's to 32-bit boundaries) can be very tricky:
13725
13726          struct S { int field1; long long field2:31; };
13727
13728          Fortunately, there is a simple rule-of-thumb which can be used
13729          in such cases.  When compiling for an i386/i486, GCC will
13730          allocate 8 bytes for the structure shown above.  It decides to
13731          do this based upon one simple rule for bit-field allocation.
13732          GCC allocates each "containing object" for each bit-field at
13733          the first (i.e. lowest addressed) legitimate alignment boundary
13734          (based upon the required minimum alignment for the declared
13735          type of the field) which it can possibly use, subject to the
13736          condition that there is still enough available space remaining
13737          in the containing object (when allocated at the selected point)
13738          to fully accommodate all of the bits of the bit-field itself.
13739
13740          This simple rule makes it obvious why GCC allocates 8 bytes for
13741          each object of the structure type shown above.  When looking
13742          for a place to allocate the "containing object" for `field2',
13743          the compiler simply tries to allocate a 64-bit "containing
13744          object" at each successive 32-bit boundary (starting at zero)
13745          until it finds a place to allocate that 64- bit field such that
13746          at least 31 contiguous (and previously unallocated) bits remain
13747          within that selected 64 bit field.  (As it turns out, for the
13748          example above, the compiler finds it is OK to allocate the
13749          "containing object" 64-bit field at bit-offset zero within the
13750          structure type.)
13751
13752          Here we attempt to work backwards from the limited set of facts
13753          we're given, and we try to deduce from those facts, where GCC
13754          must have believed that the containing object started (within
13755          the structure type). The value we deduce is then used (by the
13756          callers of this routine) to generate DW_AT_location and
13757          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13758          the case of DW_AT_location, regular fields as well).  */
13759
13760       /* Figure out the bit-distance from the start of the structure to
13761          the "deepest" bit of the bit-field.  */
13762       deepest_bitpos = bitpos_int + field_size_in_bits;
13763
13764       /* This is the tricky part.  Use some fancy footwork to deduce
13765          where the lowest addressed bit of the containing object must
13766          be.  */
13767       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13768
13769       /* Round up to type_align by default.  This works best for
13770          bitfields.  */
13771       object_offset_in_bits
13772         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13773
13774       if (object_offset_in_bits.ugt (bitpos_int))
13775         {
13776           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13777
13778           /* Round up to decl_align instead.  */
13779           object_offset_in_bits
13780             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13781         }
13782     }
13783   else
13784 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13785     object_offset_in_bits = bitpos_int;
13786
13787   object_offset_in_bytes
13788     = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
13789                                  true, TRUNC_DIV_EXPR);
13790   return object_offset_in_bytes.to_shwi ();
13791 }
13792 \f
13793 /* The following routines define various Dwarf attributes and any data
13794    associated with them.  */
13795
13796 /* Add a location description attribute value to a DIE.
13797
13798    This emits location attributes suitable for whole variables and
13799    whole parameters.  Note that the location attributes for struct fields are
13800    generated by the routine `data_member_location_attribute' below.  */
13801
13802 static inline void
13803 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13804                              dw_loc_list_ref descr)
13805 {
13806   if (descr == 0)
13807     return;
13808   if (single_element_loc_list_p (descr))
13809     add_AT_loc (die, attr_kind, descr->expr);
13810   else
13811     add_AT_loc_list (die, attr_kind, descr);
13812 }
13813
13814 /* Add DW_AT_accessibility attribute to DIE if needed.  */
13815
13816 static void
13817 add_accessibility_attribute (dw_die_ref die, tree decl)
13818 {
13819   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13820      children, otherwise the default is DW_ACCESS_public.  In DWARF2
13821      the default has always been DW_ACCESS_public.  */
13822   if (TREE_PROTECTED (decl))
13823     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13824   else if (TREE_PRIVATE (decl))
13825     {
13826       if (dwarf_version == 2
13827           || die->die_parent == NULL
13828           || die->die_parent->die_tag != DW_TAG_class_type)
13829         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13830     }
13831   else if (dwarf_version > 2
13832            && die->die_parent
13833            && die->die_parent->die_tag == DW_TAG_class_type)
13834     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13835 }
13836
13837 /* Attach the specialized form of location attribute used for data members of
13838    struct and union types.  In the special case of a FIELD_DECL node which
13839    represents a bit-field, the "offset" part of this special location
13840    descriptor must indicate the distance in bytes from the lowest-addressed
13841    byte of the containing struct or union type to the lowest-addressed byte of
13842    the "containing object" for the bit-field.  (See the `field_byte_offset'
13843    function above).
13844
13845    For any given bit-field, the "containing object" is a hypothetical object
13846    (of some integral or enum type) within which the given bit-field lives.  The
13847    type of this hypothetical "containing object" is always the same as the
13848    declared type of the individual bit-field itself (for GCC anyway... the
13849    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13850    bytes) of the hypothetical "containing object" which will be given in the
13851    DW_AT_byte_size attribute for this bit-field.  (See the
13852    `byte_size_attribute' function below.)  It is also used when calculating the
13853    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13854    function below.)  */
13855
13856 static void
13857 add_data_member_location_attribute (dw_die_ref die, tree decl)
13858 {
13859   HOST_WIDE_INT offset;
13860   dw_loc_descr_ref loc_descr = 0;
13861
13862   if (TREE_CODE (decl) == TREE_BINFO)
13863     {
13864       /* We're working on the TAG_inheritance for a base class.  */
13865       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13866         {
13867           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13868              aren't at a fixed offset from all (sub)objects of the same
13869              type.  We need to extract the appropriate offset from our
13870              vtable.  The following dwarf expression means
13871
13872                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13873
13874              This is specific to the V3 ABI, of course.  */
13875
13876           dw_loc_descr_ref tmp;
13877
13878           /* Make a copy of the object address.  */
13879           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13880           add_loc_descr (&loc_descr, tmp);
13881
13882           /* Extract the vtable address.  */
13883           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13884           add_loc_descr (&loc_descr, tmp);
13885
13886           /* Calculate the address of the offset.  */
13887           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13888           gcc_assert (offset < 0);
13889
13890           tmp = int_loc_descriptor (-offset);
13891           add_loc_descr (&loc_descr, tmp);
13892           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13893           add_loc_descr (&loc_descr, tmp);
13894
13895           /* Extract the offset.  */
13896           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13897           add_loc_descr (&loc_descr, tmp);
13898
13899           /* Add it to the object address.  */
13900           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13901           add_loc_descr (&loc_descr, tmp);
13902         }
13903       else
13904         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13905     }
13906   else
13907     offset = field_byte_offset (decl);
13908
13909   if (! loc_descr)
13910     {
13911       if (dwarf_version > 2)
13912         {
13913           /* Don't need to output a location expression, just the constant. */
13914           if (offset < 0)
13915             add_AT_int (die, DW_AT_data_member_location, offset);
13916           else
13917             add_AT_unsigned (die, DW_AT_data_member_location, offset);
13918           return;
13919         }
13920       else
13921         {
13922           enum dwarf_location_atom op;
13923
13924           /* The DWARF2 standard says that we should assume that the structure
13925              address is already on the stack, so we can specify a structure
13926              field address by using DW_OP_plus_uconst.  */
13927           op = DW_OP_plus_uconst;
13928           loc_descr = new_loc_descr (op, offset, 0);
13929         }
13930     }
13931
13932   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13933 }
13934
13935 /* Writes integer values to dw_vec_const array.  */
13936
13937 static void
13938 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13939 {
13940   while (size != 0)
13941     {
13942       *dest++ = val & 0xff;
13943       val >>= 8;
13944       --size;
13945     }
13946 }
13947
13948 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
13949
13950 static HOST_WIDE_INT
13951 extract_int (const unsigned char *src, unsigned int size)
13952 {
13953   HOST_WIDE_INT val = 0;
13954
13955   src += size;
13956   while (size != 0)
13957     {
13958       val <<= 8;
13959       val |= *--src & 0xff;
13960       --size;
13961     }
13962   return val;
13963 }
13964
13965 /* Writes double_int values to dw_vec_const array.  */
13966
13967 static void
13968 insert_double (double_int val, unsigned char *dest)
13969 {
13970   unsigned char *p0 = dest;
13971   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13972
13973   if (WORDS_BIG_ENDIAN)
13974     {
13975       p0 = p1;
13976       p1 = dest;
13977     }
13978
13979   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13980   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13981 }
13982
13983 /* Writes floating point values to dw_vec_const array.  */
13984
13985 static void
13986 insert_float (const_rtx rtl, unsigned char *array)
13987 {
13988   REAL_VALUE_TYPE rv;
13989   long val[4];
13990   int i;
13991
13992   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13993   real_to_target (val, &rv, GET_MODE (rtl));
13994
13995   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
13996   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13997     {
13998       insert_int (val[i], 4, array);
13999       array += 4;
14000     }
14001 }
14002
14003 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14004    does not have a "location" either in memory or in a register.  These
14005    things can arise in GNU C when a constant is passed as an actual parameter
14006    to an inlined function.  They can also arise in C++ where declared
14007    constants do not necessarily get memory "homes".  */
14008
14009 static bool
14010 add_const_value_attribute (dw_die_ref die, rtx rtl)
14011 {
14012   switch (GET_CODE (rtl))
14013     {
14014     case CONST_INT:
14015       {
14016         HOST_WIDE_INT val = INTVAL (rtl);
14017
14018         if (val < 0)
14019           add_AT_int (die, DW_AT_const_value, val);
14020         else
14021           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14022       }
14023       return true;
14024
14025     case CONST_DOUBLE:
14026       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14027          floating-point constant.  A CONST_DOUBLE is used whenever the
14028          constant requires more than one word in order to be adequately
14029          represented.  */
14030       {
14031         enum machine_mode mode = GET_MODE (rtl);
14032
14033         if (SCALAR_FLOAT_MODE_P (mode))
14034           {
14035             unsigned int length = GET_MODE_SIZE (mode);
14036             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14037
14038             insert_float (rtl, array);
14039             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14040           }
14041         else
14042           add_AT_double (die, DW_AT_const_value,
14043                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14044       }
14045       return true;
14046
14047     case CONST_VECTOR:
14048       {
14049         enum machine_mode mode = GET_MODE (rtl);
14050         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14051         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14052         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14053           (length * elt_size);
14054         unsigned int i;
14055         unsigned char *p;
14056
14057         switch (GET_MODE_CLASS (mode))
14058           {
14059           case MODE_VECTOR_INT:
14060             for (i = 0, p = array; i < length; i++, p += elt_size)
14061               {
14062                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14063                 double_int val = rtx_to_double_int (elt);
14064
14065                 if (elt_size <= sizeof (HOST_WIDE_INT))
14066                   insert_int (val.to_shwi (), elt_size, p);
14067                 else
14068                   {
14069                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14070                     insert_double (val, p);
14071                   }
14072               }
14073             break;
14074
14075           case MODE_VECTOR_FLOAT:
14076             for (i = 0, p = array; i < length; i++, p += elt_size)
14077               {
14078                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14079                 insert_float (elt, p);
14080               }
14081             break;
14082
14083           default:
14084             gcc_unreachable ();
14085           }
14086
14087         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14088       }
14089       return true;
14090
14091     case CONST_STRING:
14092       if (dwarf_version >= 4 || !dwarf_strict)
14093         {
14094           dw_loc_descr_ref loc_result;
14095           resolve_one_addr (&rtl, NULL);
14096         rtl_addr:
14097           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14098           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14099           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14100           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14101           add_AT_loc (die, DW_AT_location, loc_result);
14102           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14103           return true;
14104         }
14105       return false;
14106
14107     case CONST:
14108       if (CONSTANT_P (XEXP (rtl, 0)))
14109         return add_const_value_attribute (die, XEXP (rtl, 0));
14110       /* FALLTHROUGH */
14111     case SYMBOL_REF:
14112       if (!const_ok_for_output (rtl))
14113         return false;
14114     case LABEL_REF:
14115       if (dwarf_version >= 4 || !dwarf_strict)
14116         goto rtl_addr;
14117       return false;
14118
14119     case PLUS:
14120       /* In cases where an inlined instance of an inline function is passed
14121          the address of an `auto' variable (which is local to the caller) we
14122          can get a situation where the DECL_RTL of the artificial local
14123          variable (for the inlining) which acts as a stand-in for the
14124          corresponding formal parameter (of the inline function) will look
14125          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14126          exactly a compile-time constant expression, but it isn't the address
14127          of the (artificial) local variable either.  Rather, it represents the
14128          *value* which the artificial local variable always has during its
14129          lifetime.  We currently have no way to represent such quasi-constant
14130          values in Dwarf, so for now we just punt and generate nothing.  */
14131       return false;
14132
14133     case HIGH:
14134     case CONST_FIXED:
14135       return false;
14136
14137     case MEM:
14138       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14139           && MEM_READONLY_P (rtl)
14140           && GET_MODE (rtl) == BLKmode)
14141         {
14142           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14143           return true;
14144         }
14145       return false;
14146
14147     default:
14148       /* No other kinds of rtx should be possible here.  */
14149       gcc_unreachable ();
14150     }
14151   return false;
14152 }
14153
14154 /* Determine whether the evaluation of EXPR references any variables
14155    or functions which aren't otherwise used (and therefore may not be
14156    output).  */
14157 static tree
14158 reference_to_unused (tree * tp, int * walk_subtrees,
14159                      void * data ATTRIBUTE_UNUSED)
14160 {
14161   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14162     *walk_subtrees = 0;
14163
14164   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14165       && ! TREE_ASM_WRITTEN (*tp))
14166     return *tp;
14167   /* ???  The C++ FE emits debug information for using decls, so
14168      putting gcc_unreachable here falls over.  See PR31899.  For now
14169      be conservative.  */
14170   else if (!cgraph_global_info_ready
14171            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14172     return *tp;
14173   else if (TREE_CODE (*tp) == VAR_DECL)
14174     {
14175       struct varpool_node *node = varpool_get_node (*tp);
14176       if (!node || !node->analyzed)
14177         return *tp;
14178     }
14179   else if (TREE_CODE (*tp) == FUNCTION_DECL
14180            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14181     {
14182       /* The call graph machinery must have finished analyzing,
14183          optimizing and gimplifying the CU by now.
14184          So if *TP has no call graph node associated
14185          to it, it means *TP will not be emitted.  */
14186       if (!cgraph_get_node (*tp))
14187         return *tp;
14188     }
14189   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14190     return *tp;
14191
14192   return NULL_TREE;
14193 }
14194
14195 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14196    for use in a later add_const_value_attribute call.  */
14197
14198 static rtx
14199 rtl_for_decl_init (tree init, tree type)
14200 {
14201   rtx rtl = NULL_RTX;
14202
14203   STRIP_NOPS (init);
14204
14205   /* If a variable is initialized with a string constant without embedded
14206      zeros, build CONST_STRING.  */
14207   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14208     {
14209       tree enttype = TREE_TYPE (type);
14210       tree domain = TYPE_DOMAIN (type);
14211       enum machine_mode mode = TYPE_MODE (enttype);
14212
14213       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14214           && domain
14215           && integer_zerop (TYPE_MIN_VALUE (domain))
14216           && compare_tree_int (TYPE_MAX_VALUE (domain),
14217                                TREE_STRING_LENGTH (init) - 1) == 0
14218           && ((size_t) TREE_STRING_LENGTH (init)
14219               == strlen (TREE_STRING_POINTER (init)) + 1))
14220         {
14221           rtl = gen_rtx_CONST_STRING (VOIDmode,
14222                                       ggc_strdup (TREE_STRING_POINTER (init)));
14223           rtl = gen_rtx_MEM (BLKmode, rtl);
14224           MEM_READONLY_P (rtl) = 1;
14225         }
14226     }
14227   /* Other aggregates, and complex values, could be represented using
14228      CONCAT: FIXME!  */
14229   else if (AGGREGATE_TYPE_P (type)
14230            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14231                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14232            || TREE_CODE (type) == COMPLEX_TYPE)
14233     ;
14234   /* Vectors only work if their mode is supported by the target.
14235      FIXME: generic vectors ought to work too.  */
14236   else if (TREE_CODE (type) == VECTOR_TYPE
14237            && !VECTOR_MODE_P (TYPE_MODE (type)))
14238     ;
14239   /* If the initializer is something that we know will expand into an
14240      immediate RTL constant, expand it now.  We must be careful not to
14241      reference variables which won't be output.  */
14242   else if (initializer_constant_valid_p (init, type)
14243            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14244     {
14245       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14246          possible.  */
14247       if (TREE_CODE (type) == VECTOR_TYPE)
14248         switch (TREE_CODE (init))
14249           {
14250           case VECTOR_CST:
14251             break;
14252           case CONSTRUCTOR:
14253             if (TREE_CONSTANT (init))
14254               {
14255                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14256                 bool constant_p = true;
14257                 tree value;
14258                 unsigned HOST_WIDE_INT ix;
14259
14260                 /* Even when ctor is constant, it might contain non-*_CST
14261                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14262                    belong into VECTOR_CST nodes.  */
14263                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14264                   if (!CONSTANT_CLASS_P (value))
14265                     {
14266                       constant_p = false;
14267                       break;
14268                     }
14269
14270                 if (constant_p)
14271                   {
14272                     init = build_vector_from_ctor (type, elts);
14273                     break;
14274                   }
14275               }
14276             /* FALLTHRU */
14277
14278           default:
14279             return NULL;
14280           }
14281
14282       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14283
14284       /* If expand_expr returns a MEM, it wasn't immediate.  */
14285       gcc_assert (!rtl || !MEM_P (rtl));
14286     }
14287
14288   return rtl;
14289 }
14290
14291 /* Generate RTL for the variable DECL to represent its location.  */
14292
14293 static rtx
14294 rtl_for_decl_location (tree decl)
14295 {
14296   rtx rtl;
14297
14298   /* Here we have to decide where we are going to say the parameter "lives"
14299      (as far as the debugger is concerned).  We only have a couple of
14300      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14301
14302      DECL_RTL normally indicates where the parameter lives during most of the
14303      activation of the function.  If optimization is enabled however, this
14304      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14305      that the parameter doesn't really live anywhere (as far as the code
14306      generation parts of GCC are concerned) during most of the function's
14307      activation.  That will happen (for example) if the parameter is never
14308      referenced within the function.
14309
14310      We could just generate a location descriptor here for all non-NULL
14311      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14312      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14313      where DECL_RTL is NULL or is a pseudo-reg.
14314
14315      Note however that we can only get away with using DECL_INCOMING_RTL as
14316      a backup substitute for DECL_RTL in certain limited cases.  In cases
14317      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14318      we can be sure that the parameter was passed using the same type as it is
14319      declared to have within the function, and that its DECL_INCOMING_RTL
14320      points us to a place where a value of that type is passed.
14321
14322      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14323      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14324      because in these cases DECL_INCOMING_RTL points us to a value of some
14325      type which is *different* from the type of the parameter itself.  Thus,
14326      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14327      such cases, the debugger would end up (for example) trying to fetch a
14328      `float' from a place which actually contains the first part of a
14329      `double'.  That would lead to really incorrect and confusing
14330      output at debug-time.
14331
14332      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14333      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14334      are a couple of exceptions however.  On little-endian machines we can
14335      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14336      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14337      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14338      when (on a little-endian machine) a non-prototyped function has a
14339      parameter declared to be of type `short' or `char'.  In such cases,
14340      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14341      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14342      passed `int' value.  If the debugger then uses that address to fetch
14343      a `short' or a `char' (on a little-endian machine) the result will be
14344      the correct data, so we allow for such exceptional cases below.
14345
14346      Note that our goal here is to describe the place where the given formal
14347      parameter lives during most of the function's activation (i.e. between the
14348      end of the prologue and the start of the epilogue).  We'll do that as best
14349      as we can. Note however that if the given formal parameter is modified
14350      sometime during the execution of the function, then a stack backtrace (at
14351      debug-time) will show the function as having been called with the *new*
14352      value rather than the value which was originally passed in.  This happens
14353      rarely enough that it is not a major problem, but it *is* a problem, and
14354      I'd like to fix it.
14355
14356      A future version of dwarf2out.c may generate two additional attributes for
14357      any given DW_TAG_formal_parameter DIE which will describe the "passed
14358      type" and the "passed location" for the given formal parameter in addition
14359      to the attributes we now generate to indicate the "declared type" and the
14360      "active location" for each parameter.  This additional set of attributes
14361      could be used by debuggers for stack backtraces. Separately, note that
14362      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14363      This happens (for example) for inlined-instances of inline function formal
14364      parameters which are never referenced.  This really shouldn't be
14365      happening.  All PARM_DECL nodes should get valid non-NULL
14366      DECL_INCOMING_RTL values.  FIXME.  */
14367
14368   /* Use DECL_RTL as the "location" unless we find something better.  */
14369   rtl = DECL_RTL_IF_SET (decl);
14370
14371   /* When generating abstract instances, ignore everything except
14372      constants, symbols living in memory, and symbols living in
14373      fixed registers.  */
14374   if (! reload_completed)
14375     {
14376       if (rtl
14377           && (CONSTANT_P (rtl)
14378               || (MEM_P (rtl)
14379                   && CONSTANT_P (XEXP (rtl, 0)))
14380               || (REG_P (rtl)
14381                   && TREE_CODE (decl) == VAR_DECL
14382                   && TREE_STATIC (decl))))
14383         {
14384           rtl = targetm.delegitimize_address (rtl);
14385           return rtl;
14386         }
14387       rtl = NULL_RTX;
14388     }
14389   else if (TREE_CODE (decl) == PARM_DECL)
14390     {
14391       if (rtl == NULL_RTX
14392           || is_pseudo_reg (rtl)
14393           || (MEM_P (rtl)
14394               && is_pseudo_reg (XEXP (rtl, 0))
14395               && DECL_INCOMING_RTL (decl)
14396               && MEM_P (DECL_INCOMING_RTL (decl))
14397               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14398         {
14399           tree declared_type = TREE_TYPE (decl);
14400           tree passed_type = DECL_ARG_TYPE (decl);
14401           enum machine_mode dmode = TYPE_MODE (declared_type);
14402           enum machine_mode pmode = TYPE_MODE (passed_type);
14403
14404           /* This decl represents a formal parameter which was optimized out.
14405              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14406              all cases where (rtl == NULL_RTX) just below.  */
14407           if (dmode == pmode)
14408             rtl = DECL_INCOMING_RTL (decl);
14409           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14410                    && SCALAR_INT_MODE_P (dmode)
14411                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14412                    && DECL_INCOMING_RTL (decl))
14413             {
14414               rtx inc = DECL_INCOMING_RTL (decl);
14415               if (REG_P (inc))
14416                 rtl = inc;
14417               else if (MEM_P (inc))
14418                 {
14419                   if (BYTES_BIG_ENDIAN)
14420                     rtl = adjust_address_nv (inc, dmode,
14421                                              GET_MODE_SIZE (pmode)
14422                                              - GET_MODE_SIZE (dmode));
14423                   else
14424                     rtl = inc;
14425                 }
14426             }
14427         }
14428
14429       /* If the parm was passed in registers, but lives on the stack, then
14430          make a big endian correction if the mode of the type of the
14431          parameter is not the same as the mode of the rtl.  */
14432       /* ??? This is the same series of checks that are made in dbxout.c before
14433          we reach the big endian correction code there.  It isn't clear if all
14434          of these checks are necessary here, but keeping them all is the safe
14435          thing to do.  */
14436       else if (MEM_P (rtl)
14437                && XEXP (rtl, 0) != const0_rtx
14438                && ! CONSTANT_P (XEXP (rtl, 0))
14439                /* Not passed in memory.  */
14440                && !MEM_P (DECL_INCOMING_RTL (decl))
14441                /* Not passed by invisible reference.  */
14442                && (!REG_P (XEXP (rtl, 0))
14443                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14444                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14445 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14446                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14447 #endif
14448                      )
14449                /* Big endian correction check.  */
14450                && BYTES_BIG_ENDIAN
14451                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14452                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14453                    < UNITS_PER_WORD))
14454         {
14455           enum machine_mode addr_mode = get_address_mode (rtl);
14456           int offset = (UNITS_PER_WORD
14457                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14458
14459           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14460                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
14461         }
14462     }
14463   else if (TREE_CODE (decl) == VAR_DECL
14464            && rtl
14465            && MEM_P (rtl)
14466            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14467            && BYTES_BIG_ENDIAN)
14468     {
14469       enum machine_mode addr_mode = get_address_mode (rtl);
14470       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14471       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14472
14473       /* If a variable is declared "register" yet is smaller than
14474          a register, then if we store the variable to memory, it
14475          looks like we're storing a register-sized value, when in
14476          fact we are not.  We need to adjust the offset of the
14477          storage location to reflect the actual value's bytes,
14478          else gdb will not be able to display it.  */
14479       if (rsize > dsize)
14480         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14481                            plus_constant (addr_mode, XEXP (rtl, 0),
14482                                           rsize - dsize));
14483     }
14484
14485   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14486      and will have been substituted directly into all expressions that use it.
14487      C does not have such a concept, but C++ and other languages do.  */
14488   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14489     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14490
14491   if (rtl)
14492     rtl = targetm.delegitimize_address (rtl);
14493
14494   /* If we don't look past the constant pool, we risk emitting a
14495      reference to a constant pool entry that isn't referenced from
14496      code, and thus is not emitted.  */
14497   if (rtl)
14498     rtl = avoid_constant_pool_reference (rtl);
14499
14500   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14501      in the current CU, resolve_addr will remove the expression referencing
14502      it.  */
14503   if (rtl == NULL_RTX
14504       && TREE_CODE (decl) == VAR_DECL
14505       && !DECL_EXTERNAL (decl)
14506       && TREE_STATIC (decl)
14507       && DECL_NAME (decl)
14508       && !DECL_HARD_REGISTER (decl)
14509       && DECL_MODE (decl) != VOIDmode)
14510     {
14511       rtl = make_decl_rtl_for_debug (decl);
14512       if (!MEM_P (rtl)
14513           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14514           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14515         rtl = NULL_RTX;
14516     }
14517
14518   return rtl;
14519 }
14520
14521 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14522    returned.  If so, the decl for the COMMON block is returned, and the
14523    value is the offset into the common block for the symbol.  */
14524
14525 static tree
14526 fortran_common (tree decl, HOST_WIDE_INT *value)
14527 {
14528   tree val_expr, cvar;
14529   enum machine_mode mode;
14530   HOST_WIDE_INT bitsize, bitpos;
14531   tree offset;
14532   int unsignedp, volatilep = 0;
14533
14534   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14535      it does not have a value (the offset into the common area), or if it
14536      is thread local (as opposed to global) then it isn't common, and shouldn't
14537      be handled as such.  */
14538   if (TREE_CODE (decl) != VAR_DECL
14539       || !TREE_STATIC (decl)
14540       || !DECL_HAS_VALUE_EXPR_P (decl)
14541       || !is_fortran ())
14542     return NULL_TREE;
14543
14544   val_expr = DECL_VALUE_EXPR (decl);
14545   if (TREE_CODE (val_expr) != COMPONENT_REF)
14546     return NULL_TREE;
14547
14548   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14549                               &mode, &unsignedp, &volatilep, true);
14550
14551   if (cvar == NULL_TREE
14552       || TREE_CODE (cvar) != VAR_DECL
14553       || DECL_ARTIFICIAL (cvar)
14554       || !TREE_PUBLIC (cvar))
14555     return NULL_TREE;
14556
14557   *value = 0;
14558   if (offset != NULL)
14559     {
14560       if (!host_integerp (offset, 0))
14561         return NULL_TREE;
14562       *value = tree_low_cst (offset, 0);
14563     }
14564   if (bitpos != 0)
14565     *value += bitpos / BITS_PER_UNIT;
14566
14567   return cvar;
14568 }
14569
14570 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14571    data attribute for a variable or a parameter.  We generate the
14572    DW_AT_const_value attribute only in those cases where the given variable
14573    or parameter does not have a true "location" either in memory or in a
14574    register.  This can happen (for example) when a constant is passed as an
14575    actual argument in a call to an inline function.  (It's possible that
14576    these things can crop up in other ways also.)  Note that one type of
14577    constant value which can be passed into an inlined function is a constant
14578    pointer.  This can happen for example if an actual argument in an inlined
14579    function call evaluates to a compile-time constant address.
14580
14581    CACHE_P is true if it is worth caching the location list for DECL,
14582    so that future calls can reuse it rather than regenerate it from scratch.
14583    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14584    since we will need to refer to them each time the function is inlined.  */
14585
14586 static bool
14587 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14588                                        enum dwarf_attribute attr)
14589 {
14590   rtx rtl;
14591   dw_loc_list_ref list;
14592   var_loc_list *loc_list;
14593   cached_dw_loc_list *cache;
14594   void **slot;
14595
14596   if (TREE_CODE (decl) == ERROR_MARK)
14597     return false;
14598
14599   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14600               || TREE_CODE (decl) == RESULT_DECL);
14601
14602   /* Try to get some constant RTL for this decl, and use that as the value of
14603      the location.  */
14604
14605   rtl = rtl_for_decl_location (decl);
14606   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14607       && add_const_value_attribute (die, rtl))
14608     return true;
14609
14610   /* See if we have single element location list that is equivalent to
14611      a constant value.  That way we are better to use add_const_value_attribute
14612      rather than expanding constant value equivalent.  */
14613   loc_list = lookup_decl_loc (decl);
14614   if (loc_list
14615       && loc_list->first
14616       && loc_list->first->next == NULL
14617       && NOTE_P (loc_list->first->loc)
14618       && NOTE_VAR_LOCATION (loc_list->first->loc)
14619       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14620     {
14621       struct var_loc_node *node;
14622
14623       node = loc_list->first;
14624       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14625       if (GET_CODE (rtl) == EXPR_LIST)
14626         rtl = XEXP (rtl, 0);
14627       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14628           && add_const_value_attribute (die, rtl))
14629          return true;
14630     }
14631   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14632      list several times.  See if we've already cached the contents.  */
14633   list = NULL;
14634   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14635     cache_p = false;
14636   if (cache_p)
14637     {
14638       cache = (cached_dw_loc_list *)
14639         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14640       if (cache)
14641         list = cache->loc_list;
14642     }
14643   if (list == NULL)
14644     {
14645       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14646       /* It is usually worth caching this result if the decl is from
14647          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14648       if (cache_p && list && list->dw_loc_next)
14649         {
14650           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14651                                            DECL_UID (decl), INSERT);
14652           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14653           cache->decl_id = DECL_UID (decl);
14654           cache->loc_list = list;
14655           *slot = cache;
14656         }
14657     }
14658   if (list)
14659     {
14660       add_AT_location_description (die, attr, list);
14661       return true;
14662     }
14663   /* None of that worked, so it must not really have a location;
14664      try adding a constant value attribute from the DECL_INITIAL.  */
14665   return tree_add_const_value_attribute_for_decl (die, decl);
14666 }
14667
14668 /* Add VARIABLE and DIE into deferred locations list.  */
14669
14670 static void
14671 defer_location (tree variable, dw_die_ref die)
14672 {
14673   deferred_locations entry;
14674   entry.variable = variable;
14675   entry.die = die;
14676   VEC_safe_push (deferred_locations, gc, deferred_locations_list, entry);
14677 }
14678
14679 /* Helper function for tree_add_const_value_attribute.  Natively encode
14680    initializer INIT into an array.  Return true if successful.  */
14681
14682 static bool
14683 native_encode_initializer (tree init, unsigned char *array, int size)
14684 {
14685   tree type;
14686
14687   if (init == NULL_TREE)
14688     return false;
14689
14690   STRIP_NOPS (init);
14691   switch (TREE_CODE (init))
14692     {
14693     case STRING_CST:
14694       type = TREE_TYPE (init);
14695       if (TREE_CODE (type) == ARRAY_TYPE)
14696         {
14697           tree enttype = TREE_TYPE (type);
14698           enum machine_mode mode = TYPE_MODE (enttype);
14699
14700           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14701             return false;
14702           if (int_size_in_bytes (type) != size)
14703             return false;
14704           if (size > TREE_STRING_LENGTH (init))
14705             {
14706               memcpy (array, TREE_STRING_POINTER (init),
14707                       TREE_STRING_LENGTH (init));
14708               memset (array + TREE_STRING_LENGTH (init),
14709                       '\0', size - TREE_STRING_LENGTH (init));
14710             }
14711           else
14712             memcpy (array, TREE_STRING_POINTER (init), size);
14713           return true;
14714         }
14715       return false;
14716     case CONSTRUCTOR:
14717       type = TREE_TYPE (init);
14718       if (int_size_in_bytes (type) != size)
14719         return false;
14720       if (TREE_CODE (type) == ARRAY_TYPE)
14721         {
14722           HOST_WIDE_INT min_index;
14723           unsigned HOST_WIDE_INT cnt;
14724           int curpos = 0, fieldsize;
14725           constructor_elt *ce;
14726
14727           if (TYPE_DOMAIN (type) == NULL_TREE
14728               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14729             return false;
14730
14731           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14732           if (fieldsize <= 0)
14733             return false;
14734
14735           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14736           memset (array, '\0', size);
14737           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14738             {
14739               tree val = ce->value;
14740               tree index = ce->index;
14741               int pos = curpos;
14742               if (index && TREE_CODE (index) == RANGE_EXPR)
14743                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14744                       * fieldsize;
14745               else if (index)
14746                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14747
14748               if (val)
14749                 {
14750                   STRIP_NOPS (val);
14751                   if (!native_encode_initializer (val, array + pos, fieldsize))
14752                     return false;
14753                 }
14754               curpos = pos + fieldsize;
14755               if (index && TREE_CODE (index) == RANGE_EXPR)
14756                 {
14757                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14758                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14759                   while (count-- > 0)
14760                     {
14761                       if (val)
14762                         memcpy (array + curpos, array + pos, fieldsize);
14763                       curpos += fieldsize;
14764                     }
14765                 }
14766               gcc_assert (curpos <= size);
14767             }
14768           return true;
14769         }
14770       else if (TREE_CODE (type) == RECORD_TYPE
14771                || TREE_CODE (type) == UNION_TYPE)
14772         {
14773           tree field = NULL_TREE;
14774           unsigned HOST_WIDE_INT cnt;
14775           constructor_elt *ce;
14776
14777           if (int_size_in_bytes (type) != size)
14778             return false;
14779
14780           if (TREE_CODE (type) == RECORD_TYPE)
14781             field = TYPE_FIELDS (type);
14782
14783           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14784             {
14785               tree val = ce->value;
14786               int pos, fieldsize;
14787
14788               if (ce->index != 0)
14789                 field = ce->index;
14790
14791               if (val)
14792                 STRIP_NOPS (val);
14793
14794               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14795                 return false;
14796
14797               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14798                   && TYPE_DOMAIN (TREE_TYPE (field))
14799                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14800                 return false;
14801               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14802                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14803                 return false;
14804               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14805               pos = int_byte_position (field);
14806               gcc_assert (pos + fieldsize <= size);
14807               if (val
14808                   && !native_encode_initializer (val, array + pos, fieldsize))
14809                 return false;
14810             }
14811           return true;
14812         }
14813       return false;
14814     case VIEW_CONVERT_EXPR:
14815     case NON_LVALUE_EXPR:
14816       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14817     default:
14818       return native_encode_expr (init, array, size) == size;
14819     }
14820 }
14821
14822 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14823    attribute is the const value T.  */
14824
14825 static bool
14826 tree_add_const_value_attribute (dw_die_ref die, tree t)
14827 {
14828   tree init;
14829   tree type = TREE_TYPE (t);
14830   rtx rtl;
14831
14832   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14833     return false;
14834
14835   init = t;
14836   gcc_assert (!DECL_P (init));
14837
14838   rtl = rtl_for_decl_init (init, type);
14839   if (rtl)
14840     return add_const_value_attribute (die, rtl);
14841   /* If the host and target are sane, try harder.  */
14842   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14843            && initializer_constant_valid_p (init, type))
14844     {
14845       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14846       if (size > 0 && (int) size == size)
14847         {
14848           unsigned char *array = (unsigned char *)
14849             ggc_alloc_cleared_atomic (size);
14850
14851           if (native_encode_initializer (init, array, size))
14852             {
14853               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14854               return true;
14855             }
14856         }
14857     }
14858   return false;
14859 }
14860
14861 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14862    attribute is the const value of T, where T is an integral constant
14863    variable with static storage duration
14864    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14865
14866 static bool
14867 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14868 {
14869
14870   if (!decl
14871       || (TREE_CODE (decl) != VAR_DECL
14872           && TREE_CODE (decl) != CONST_DECL)
14873       || (TREE_CODE (decl) == VAR_DECL
14874           && !TREE_STATIC (decl)))
14875     return false;
14876
14877     if (TREE_READONLY (decl)
14878         && ! TREE_THIS_VOLATILE (decl)
14879         && DECL_INITIAL (decl))
14880       /* OK */;
14881     else
14882       return false;
14883
14884   /* Don't add DW_AT_const_value if abstract origin already has one.  */
14885   if (get_AT (var_die, DW_AT_const_value))
14886     return false;
14887
14888   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14889 }
14890
14891 /* Convert the CFI instructions for the current function into a
14892    location list.  This is used for DW_AT_frame_base when we targeting
14893    a dwarf2 consumer that does not support the dwarf3
14894    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14895    expressions.  */
14896
14897 static dw_loc_list_ref
14898 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14899 {
14900   int ix;
14901   dw_fde_ref fde;
14902   dw_loc_list_ref list, *list_tail;
14903   dw_cfi_ref cfi;
14904   dw_cfa_location last_cfa, next_cfa;
14905   const char *start_label, *last_label, *section;
14906   dw_cfa_location remember;
14907
14908   fde = cfun->fde;
14909   gcc_assert (fde != NULL);
14910
14911   section = secname_for_decl (current_function_decl);
14912   list_tail = &list;
14913   list = NULL;
14914
14915   memset (&next_cfa, 0, sizeof (next_cfa));
14916   next_cfa.reg = INVALID_REGNUM;
14917   remember = next_cfa;
14918
14919   start_label = fde->dw_fde_begin;
14920
14921   /* ??? Bald assumption that the CIE opcode list does not contain
14922      advance opcodes.  */
14923   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14924     lookup_cfa_1 (cfi, &next_cfa, &remember);
14925
14926   last_cfa = next_cfa;
14927   last_label = start_label;
14928
14929   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14930     {
14931       /* If the first partition contained no CFI adjustments, the
14932          CIE opcodes apply to the whole first partition.  */
14933       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14934                                  fde->dw_fde_begin, fde->dw_fde_end, section);
14935       list_tail =&(*list_tail)->dw_loc_next;
14936       start_label = last_label = fde->dw_fde_second_begin;
14937     }
14938
14939   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14940     {
14941       switch (cfi->dw_cfi_opc)
14942         {
14943         case DW_CFA_set_loc:
14944         case DW_CFA_advance_loc1:
14945         case DW_CFA_advance_loc2:
14946         case DW_CFA_advance_loc4:
14947           if (!cfa_equal_p (&last_cfa, &next_cfa))
14948             {
14949               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14950                                          start_label, last_label, section);
14951
14952               list_tail = &(*list_tail)->dw_loc_next;
14953               last_cfa = next_cfa;
14954               start_label = last_label;
14955             }
14956           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14957           break;
14958
14959         case DW_CFA_advance_loc:
14960           /* The encoding is complex enough that we should never emit this.  */
14961           gcc_unreachable ();
14962
14963         default:
14964           lookup_cfa_1 (cfi, &next_cfa, &remember);
14965           break;
14966         }
14967       if (ix + 1 == fde->dw_fde_switch_cfi_index)
14968         {
14969           if (!cfa_equal_p (&last_cfa, &next_cfa))
14970             {
14971               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14972                                          start_label, last_label, section);
14973
14974               list_tail = &(*list_tail)->dw_loc_next;
14975               last_cfa = next_cfa;
14976               start_label = last_label;
14977             }
14978           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14979                                      start_label, fde->dw_fde_end, section);
14980           list_tail = &(*list_tail)->dw_loc_next;
14981           start_label = last_label = fde->dw_fde_second_begin;
14982         }
14983     }
14984
14985   if (!cfa_equal_p (&last_cfa, &next_cfa))
14986     {
14987       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14988                                  start_label, last_label, section);
14989       list_tail = &(*list_tail)->dw_loc_next;
14990       start_label = last_label;
14991     }
14992
14993   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14994                              start_label,
14995                              fde->dw_fde_second_begin
14996                              ? fde->dw_fde_second_end : fde->dw_fde_end,
14997                              section);
14998
14999   if (list && list->dw_loc_next)
15000     gen_llsym (list);
15001
15002   return list;
15003 }
15004
15005 /* Compute a displacement from the "steady-state frame pointer" to the
15006    frame base (often the same as the CFA), and store it in
15007    frame_pointer_fb_offset.  OFFSET is added to the displacement
15008    before the latter is negated.  */
15009
15010 static void
15011 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15012 {
15013   rtx reg, elim;
15014
15015 #ifdef FRAME_POINTER_CFA_OFFSET
15016   reg = frame_pointer_rtx;
15017   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15018 #else
15019   reg = arg_pointer_rtx;
15020   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15021 #endif
15022
15023   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15024   if (GET_CODE (elim) == PLUS)
15025     {
15026       offset += INTVAL (XEXP (elim, 1));
15027       elim = XEXP (elim, 0);
15028     }
15029
15030   frame_pointer_fb_offset = -offset;
15031
15032   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15033      in which to eliminate.  This is because it's stack pointer isn't 
15034      directly accessible as a register within the ISA.  To work around
15035      this, assume that while we cannot provide a proper value for
15036      frame_pointer_fb_offset, we won't need one either.  */
15037   frame_pointer_fb_offset_valid
15038     = ((SUPPORTS_STACK_ALIGNMENT
15039         && (elim == hard_frame_pointer_rtx
15040             || elim == stack_pointer_rtx))
15041        || elim == (frame_pointer_needed
15042                    ? hard_frame_pointer_rtx
15043                    : stack_pointer_rtx));
15044 }
15045
15046 /* Generate a DW_AT_name attribute given some string value to be included as
15047    the value of the attribute.  */
15048
15049 static void
15050 add_name_attribute (dw_die_ref die, const char *name_string)
15051 {
15052   if (name_string != NULL && *name_string != 0)
15053     {
15054       if (demangle_name_func)
15055         name_string = (*demangle_name_func) (name_string);
15056
15057       add_AT_string (die, DW_AT_name, name_string);
15058     }
15059 }
15060
15061 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15062    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15063    of TYPE accordingly.
15064
15065    ??? This is a temporary measure until after we're able to generate
15066    regular DWARF for the complex Ada type system.  */
15067
15068 static void 
15069 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15070                                      dw_die_ref context_die)
15071 {
15072   tree dtype;
15073   dw_die_ref dtype_die;
15074
15075   if (!lang_hooks.types.descriptive_type)
15076     return;
15077
15078   dtype = lang_hooks.types.descriptive_type (type);
15079   if (!dtype)
15080     return;
15081
15082   dtype_die = lookup_type_die (dtype);
15083   if (!dtype_die)
15084     {
15085       gen_type_die (dtype, context_die);
15086       dtype_die = lookup_type_die (dtype);
15087       gcc_assert (dtype_die);
15088     }
15089
15090   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15091 }
15092
15093 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15094
15095 static void
15096 add_comp_dir_attribute (dw_die_ref die)
15097 {
15098   const char *wd = get_src_pwd ();
15099   char *wd1;
15100
15101   if (wd == NULL)
15102     return;
15103
15104   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15105     {
15106       int wdlen;
15107
15108       wdlen = strlen (wd);
15109       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15110       strcpy (wd1, wd);
15111       wd1 [wdlen] = DIR_SEPARATOR;
15112       wd1 [wdlen + 1] = 0;
15113       wd = wd1;
15114     }
15115
15116     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15117 }
15118
15119 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15120    default.  */
15121
15122 static int
15123 lower_bound_default (void)
15124 {
15125   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15126     {
15127     case DW_LANG_C:
15128     case DW_LANG_C89:
15129     case DW_LANG_C99:
15130     case DW_LANG_C_plus_plus:
15131     case DW_LANG_ObjC:
15132     case DW_LANG_ObjC_plus_plus:
15133     case DW_LANG_Java:
15134       return 0;
15135     case DW_LANG_Fortran77:
15136     case DW_LANG_Fortran90:
15137     case DW_LANG_Fortran95:
15138       return 1;
15139     case DW_LANG_UPC:
15140     case DW_LANG_D:
15141     case DW_LANG_Python:
15142       return dwarf_version >= 4 ? 0 : -1;
15143     case DW_LANG_Ada95:
15144     case DW_LANG_Ada83:
15145     case DW_LANG_Cobol74:
15146     case DW_LANG_Cobol85:
15147     case DW_LANG_Pascal83:
15148     case DW_LANG_Modula2:
15149     case DW_LANG_PLI:
15150       return dwarf_version >= 4 ? 1 : -1;
15151     default:
15152       return -1;
15153     }
15154 }
15155
15156 /* Given a tree node describing an array bound (either lower or upper) output
15157    a representation for that bound.  */
15158
15159 static void
15160 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15161 {
15162   switch (TREE_CODE (bound))
15163     {
15164     case ERROR_MARK:
15165       return;
15166
15167     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15168     case INTEGER_CST:
15169       {
15170         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15171         int dflt;
15172
15173         /* Use the default if possible.  */
15174         if (bound_attr == DW_AT_lower_bound
15175             && host_integerp (bound, 0)
15176             && (dflt = lower_bound_default ()) != -1
15177             && tree_low_cst (bound, 0) == dflt)
15178           ;
15179
15180         /* Otherwise represent the bound as an unsigned value with the
15181            precision of its type.  The precision and signedness of the
15182            type will be necessary to re-interpret it unambiguously.  */
15183         else if (prec < HOST_BITS_PER_WIDE_INT)
15184           {
15185             unsigned HOST_WIDE_INT mask
15186               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15187             add_AT_unsigned (subrange_die, bound_attr,
15188                              TREE_INT_CST_LOW (bound) & mask);
15189           }
15190         else if (prec == HOST_BITS_PER_WIDE_INT
15191                  || TREE_INT_CST_HIGH (bound) == 0)
15192           add_AT_unsigned (subrange_die, bound_attr,
15193                            TREE_INT_CST_LOW (bound));
15194         else
15195           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15196                          TREE_INT_CST_LOW (bound));
15197       }
15198       break;
15199
15200     CASE_CONVERT:
15201     case VIEW_CONVERT_EXPR:
15202       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15203       break;
15204
15205     case SAVE_EXPR:
15206       break;
15207
15208     case VAR_DECL:
15209     case PARM_DECL:
15210     case RESULT_DECL:
15211       {
15212         dw_die_ref decl_die = lookup_decl_die (bound);
15213
15214         /* ??? Can this happen, or should the variable have been bound
15215            first?  Probably it can, since I imagine that we try to create
15216            the types of parameters in the order in which they exist in
15217            the list, and won't have created a forward reference to a
15218            later parameter.  */
15219         if (decl_die != NULL)
15220           {
15221             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15222             break;
15223           }
15224       }
15225       /* FALLTHRU */
15226
15227     default:
15228       {
15229         /* Otherwise try to create a stack operation procedure to
15230            evaluate the value of the array bound.  */
15231
15232         dw_die_ref ctx, decl_die;
15233         dw_loc_list_ref list;
15234
15235         list = loc_list_from_tree (bound, 2);
15236         if (list == NULL || single_element_loc_list_p (list))
15237           {
15238             /* If DW_AT_*bound is not a reference nor constant, it is
15239                a DWARF expression rather than location description.
15240                For that loc_list_from_tree (bound, 0) is needed.
15241                If that fails to give a single element list,
15242                fall back to outputting this as a reference anyway.  */
15243             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15244             if (list2 && single_element_loc_list_p (list2))
15245               {
15246                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15247                 break;
15248               }
15249           }
15250         if (list == NULL)
15251           break;
15252
15253         if (current_function_decl == 0)
15254           ctx = comp_unit_die ();
15255         else
15256           ctx = lookup_decl_die (current_function_decl);
15257
15258         decl_die = new_die (DW_TAG_variable, ctx, bound);
15259         add_AT_flag (decl_die, DW_AT_artificial, 1);
15260         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15261         add_AT_location_description (decl_die, DW_AT_location, list);
15262         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15263         break;
15264       }
15265     }
15266 }
15267
15268 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15269    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15270    Note that the block of subscript information for an array type also
15271    includes information about the element type of the given array type.  */
15272
15273 static void
15274 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15275 {
15276   unsigned dimension_number;
15277   tree lower, upper;
15278   dw_die_ref subrange_die;
15279
15280   for (dimension_number = 0;
15281        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15282        type = TREE_TYPE (type), dimension_number++)
15283     {
15284       tree domain = TYPE_DOMAIN (type);
15285
15286       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15287         break;
15288
15289       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15290          and (in GNU C only) variable bounds.  Handle all three forms
15291          here.  */
15292       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15293       if (domain)
15294         {
15295           /* We have an array type with specified bounds.  */
15296           lower = TYPE_MIN_VALUE (domain);
15297           upper = TYPE_MAX_VALUE (domain);
15298
15299           /* Define the index type.  */
15300           if (TREE_TYPE (domain))
15301             {
15302               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15303                  TREE_TYPE field.  We can't emit debug info for this
15304                  because it is an unnamed integral type.  */
15305               if (TREE_CODE (domain) == INTEGER_TYPE
15306                   && TYPE_NAME (domain) == NULL_TREE
15307                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15308                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15309                 ;
15310               else
15311                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15312                                     type_die);
15313             }
15314
15315           /* ??? If upper is NULL, the array has unspecified length,
15316              but it does have a lower bound.  This happens with Fortran
15317                dimension arr(N:*)
15318              Since the debugger is definitely going to need to know N
15319              to produce useful results, go ahead and output the lower
15320              bound solo, and hope the debugger can cope.  */
15321
15322           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15323           if (upper)
15324             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15325         }
15326
15327       /* Otherwise we have an array type with an unspecified length.  The
15328          DWARF-2 spec does not say how to handle this; let's just leave out the
15329          bounds.  */
15330     }
15331 }
15332
15333 static void
15334 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15335 {
15336   dw_die_ref decl_die;
15337   unsigned size;
15338
15339   switch (TREE_CODE (tree_node))
15340     {
15341     case ERROR_MARK:
15342       size = 0;
15343       break;
15344     case ENUMERAL_TYPE:
15345     case RECORD_TYPE:
15346     case UNION_TYPE:
15347     case QUAL_UNION_TYPE:
15348       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15349           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15350         {
15351           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15352           return;
15353         }
15354       size = int_size_in_bytes (tree_node);
15355       break;
15356     case FIELD_DECL:
15357       /* For a data member of a struct or union, the DW_AT_byte_size is
15358          generally given as the number of bytes normally allocated for an
15359          object of the *declared* type of the member itself.  This is true
15360          even for bit-fields.  */
15361       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15362       break;
15363     default:
15364       gcc_unreachable ();
15365     }
15366
15367   /* Note that `size' might be -1 when we get to this point.  If it is, that
15368      indicates that the byte size of the entity in question is variable.  We
15369      have no good way of expressing this fact in Dwarf at the present time,
15370      so just let the -1 pass on through.  */
15371   add_AT_unsigned (die, DW_AT_byte_size, size);
15372 }
15373
15374 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15375    which specifies the distance in bits from the highest order bit of the
15376    "containing object" for the bit-field to the highest order bit of the
15377    bit-field itself.
15378
15379    For any given bit-field, the "containing object" is a hypothetical object
15380    (of some integral or enum type) within which the given bit-field lives.  The
15381    type of this hypothetical "containing object" is always the same as the
15382    declared type of the individual bit-field itself.  The determination of the
15383    exact location of the "containing object" for a bit-field is rather
15384    complicated.  It's handled by the `field_byte_offset' function (above).
15385
15386    Note that it is the size (in bytes) of the hypothetical "containing object"
15387    which will be given in the DW_AT_byte_size attribute for this bit-field.
15388    (See `byte_size_attribute' above).  */
15389
15390 static inline void
15391 add_bit_offset_attribute (dw_die_ref die, tree decl)
15392 {
15393   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15394   tree type = DECL_BIT_FIELD_TYPE (decl);
15395   HOST_WIDE_INT bitpos_int;
15396   HOST_WIDE_INT highest_order_object_bit_offset;
15397   HOST_WIDE_INT highest_order_field_bit_offset;
15398   HOST_WIDE_INT bit_offset;
15399
15400   /* Must be a field and a bit field.  */
15401   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15402
15403   /* We can't yet handle bit-fields whose offsets are variable, so if we
15404      encounter such things, just return without generating any attribute
15405      whatsoever.  Likewise for variable or too large size.  */
15406   if (! host_integerp (bit_position (decl), 0)
15407       || ! host_integerp (DECL_SIZE (decl), 1))
15408     return;
15409
15410   bitpos_int = int_bit_position (decl);
15411
15412   /* Note that the bit offset is always the distance (in bits) from the
15413      highest-order bit of the "containing object" to the highest-order bit of
15414      the bit-field itself.  Since the "high-order end" of any object or field
15415      is different on big-endian and little-endian machines, the computation
15416      below must take account of these differences.  */
15417   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15418   highest_order_field_bit_offset = bitpos_int;
15419
15420   if (! BYTES_BIG_ENDIAN)
15421     {
15422       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15423       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15424     }
15425
15426   bit_offset
15427     = (! BYTES_BIG_ENDIAN
15428        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15429        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15430
15431   if (bit_offset < 0)
15432     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15433   else
15434     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15435 }
15436
15437 /* For a FIELD_DECL node which represents a bit field, output an attribute
15438    which specifies the length in bits of the given field.  */
15439
15440 static inline void
15441 add_bit_size_attribute (dw_die_ref die, tree decl)
15442 {
15443   /* Must be a field and a bit field.  */
15444   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15445               && DECL_BIT_FIELD_TYPE (decl));
15446
15447   if (host_integerp (DECL_SIZE (decl), 1))
15448     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15449 }
15450
15451 /* If the compiled language is ANSI C, then add a 'prototyped'
15452    attribute, if arg types are given for the parameters of a function.  */
15453
15454 static inline void
15455 add_prototyped_attribute (dw_die_ref die, tree func_type)
15456 {
15457   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15458       && prototype_p (func_type))
15459     add_AT_flag (die, DW_AT_prototyped, 1);
15460 }
15461
15462 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15463    by looking in either the type declaration or object declaration
15464    equate table.  */
15465
15466 static inline dw_die_ref
15467 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15468 {
15469   dw_die_ref origin_die = NULL;
15470
15471   if (TREE_CODE (origin) != FUNCTION_DECL)
15472     {
15473       /* We may have gotten separated from the block for the inlined
15474          function, if we're in an exception handler or some such; make
15475          sure that the abstract function has been written out.
15476
15477          Doing this for nested functions is wrong, however; functions are
15478          distinct units, and our context might not even be inline.  */
15479       tree fn = origin;
15480
15481       if (TYPE_P (fn))
15482         fn = TYPE_STUB_DECL (fn);
15483
15484       fn = decl_function_context (fn);
15485       if (fn)
15486         dwarf2out_abstract_function (fn);
15487     }
15488
15489   if (DECL_P (origin))
15490     origin_die = lookup_decl_die (origin);
15491   else if (TYPE_P (origin))
15492     origin_die = lookup_type_die (origin);
15493
15494   /* XXX: Functions that are never lowered don't always have correct block
15495      trees (in the case of java, they simply have no block tree, in some other
15496      languages).  For these functions, there is nothing we can really do to
15497      output correct debug info for inlined functions in all cases.  Rather
15498      than die, we'll just produce deficient debug info now, in that we will
15499      have variables without a proper abstract origin.  In the future, when all
15500      functions are lowered, we should re-add a gcc_assert (origin_die)
15501      here.  */
15502
15503   if (origin_die)
15504     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15505   return origin_die;
15506 }
15507
15508 /* We do not currently support the pure_virtual attribute.  */
15509
15510 static inline void
15511 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15512 {
15513   if (DECL_VINDEX (func_decl))
15514     {
15515       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15516
15517       if (host_integerp (DECL_VINDEX (func_decl), 0))
15518         add_AT_loc (die, DW_AT_vtable_elem_location,
15519                     new_loc_descr (DW_OP_constu,
15520                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15521                                    0));
15522
15523       /* GNU extension: Record what type this method came from originally.  */
15524       if (debug_info_level > DINFO_LEVEL_TERSE
15525           && DECL_CONTEXT (func_decl))
15526         add_AT_die_ref (die, DW_AT_containing_type,
15527                         lookup_type_die (DECL_CONTEXT (func_decl)));
15528     }
15529 }
15530 \f
15531 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15532    given decl.  This used to be a vendor extension until after DWARF 4
15533    standardized it.  */
15534
15535 static void
15536 add_linkage_attr (dw_die_ref die, tree decl)
15537 {
15538   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15539
15540   /* Mimic what assemble_name_raw does with a leading '*'.  */
15541   if (name[0] == '*')
15542     name = &name[1];
15543
15544   if (dwarf_version >= 4)
15545     add_AT_string (die, DW_AT_linkage_name, name);
15546   else
15547     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15548 }
15549
15550 /* Add source coordinate attributes for the given decl.  */
15551
15552 static void
15553 add_src_coords_attributes (dw_die_ref die, tree decl)
15554 {
15555   expanded_location s;
15556
15557   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
15558     return;
15559   s = expand_location (DECL_SOURCE_LOCATION (decl));
15560   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15561   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15562 }
15563
15564 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15565
15566 static void
15567 add_linkage_name (dw_die_ref die, tree decl)
15568 {
15569   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15570        && TREE_PUBLIC (decl)
15571        && !DECL_ABSTRACT (decl)
15572        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15573        && die->die_tag != DW_TAG_member)
15574     {
15575       /* Defer until we have an assembler name set.  */
15576       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15577         {
15578           limbo_die_node *asm_name;
15579
15580           asm_name = ggc_alloc_cleared_limbo_die_node ();
15581           asm_name->die = die;
15582           asm_name->created_for = decl;
15583           asm_name->next = deferred_asm_name;
15584           deferred_asm_name = asm_name;
15585         }
15586       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15587         add_linkage_attr (die, decl);
15588     }
15589 }
15590
15591 /* Add a DW_AT_name attribute and source coordinate attribute for the
15592    given decl, but only if it actually has a name.  */
15593
15594 static void
15595 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15596 {
15597   tree decl_name;
15598
15599   decl_name = DECL_NAME (decl);
15600   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15601     {
15602       const char *name = dwarf2_name (decl, 0);
15603       if (name)
15604         add_name_attribute (die, name);
15605       if (! DECL_ARTIFICIAL (decl))
15606         add_src_coords_attributes (die, decl);
15607
15608       add_linkage_name (die, decl);
15609     }
15610
15611 #ifdef VMS_DEBUGGING_INFO
15612   /* Get the function's name, as described by its RTL.  This may be different
15613      from the DECL_NAME name used in the source file.  */
15614   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15615     {
15616       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15617                    XEXP (DECL_RTL (decl), 0));
15618       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15619     }
15620 #endif /* VMS_DEBUGGING_INFO */
15621 }
15622
15623 #ifdef VMS_DEBUGGING_INFO
15624 /* Output the debug main pointer die for VMS */
15625
15626 void
15627 dwarf2out_vms_debug_main_pointer (void)
15628 {
15629   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15630   dw_die_ref die;
15631
15632   /* Allocate the VMS debug main subprogram die.  */
15633   die = ggc_alloc_cleared_die_node ();
15634   die->die_tag = DW_TAG_subprogram;
15635   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15636   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15637                                current_function_funcdef_no);
15638   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15639
15640   /* Make it the first child of comp_unit_die ().  */
15641   die->die_parent = comp_unit_die ();
15642   if (comp_unit_die ()->die_child)
15643     {
15644       die->die_sib = comp_unit_die ()->die_child->die_sib;
15645       comp_unit_die ()->die_child->die_sib = die;
15646     }
15647   else
15648     {
15649       die->die_sib = die;
15650       comp_unit_die ()->die_child = die;
15651     }
15652 }
15653 #endif /* VMS_DEBUGGING_INFO */
15654
15655 /* Push a new declaration scope.  */
15656
15657 static void
15658 push_decl_scope (tree scope)
15659 {
15660   VEC_safe_push (tree, gc, decl_scope_table, scope);
15661 }
15662
15663 /* Pop a declaration scope.  */
15664
15665 static inline void
15666 pop_decl_scope (void)
15667 {
15668   VEC_pop (tree, decl_scope_table);
15669 }
15670
15671 /* walk_tree helper function for uses_local_type, below.  */
15672
15673 static tree
15674 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15675 {
15676   if (!TYPE_P (*tp))
15677     *walk_subtrees = 0;
15678   else
15679     {
15680       tree name = TYPE_NAME (*tp);
15681       if (name && DECL_P (name) && decl_function_context (name))
15682         return *tp;
15683     }
15684   return NULL_TREE;
15685 }
15686
15687 /* If TYPE involves a function-local type (including a local typedef to a
15688    non-local type), returns that type; otherwise returns NULL_TREE.  */
15689
15690 static tree
15691 uses_local_type (tree type)
15692 {
15693   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15694   return used;
15695 }
15696
15697 /* Return the DIE for the scope that immediately contains this type.
15698    Non-named types that do not involve a function-local type get global
15699    scope.  Named types nested in namespaces or other types get their
15700    containing scope.  All other types (i.e. function-local named types) get
15701    the current active scope.  */
15702
15703 static dw_die_ref
15704 scope_die_for (tree t, dw_die_ref context_die)
15705 {
15706   dw_die_ref scope_die = NULL;
15707   tree containing_scope;
15708
15709   /* Non-types always go in the current scope.  */
15710   gcc_assert (TYPE_P (t));
15711
15712   /* Use the scope of the typedef, rather than the scope of the type
15713      it refers to.  */
15714   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15715     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15716   else
15717     containing_scope = TYPE_CONTEXT (t);
15718
15719   /* Use the containing namespace if there is one.  */
15720   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15721     {
15722       if (context_die == lookup_decl_die (containing_scope))
15723         /* OK */;
15724       else if (debug_info_level > DINFO_LEVEL_TERSE)
15725         context_die = get_context_die (containing_scope);
15726       else
15727         containing_scope = NULL_TREE;
15728     }
15729
15730   /* Ignore function type "scopes" from the C frontend.  They mean that
15731      a tagged type is local to a parmlist of a function declarator, but
15732      that isn't useful to DWARF.  */
15733   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15734     containing_scope = NULL_TREE;
15735
15736   if (SCOPE_FILE_SCOPE_P (containing_scope))
15737     {
15738       /* If T uses a local type keep it local as well, to avoid references
15739          to function-local DIEs from outside the function.  */
15740       if (current_function_decl && uses_local_type (t))
15741         scope_die = context_die;
15742       else
15743         scope_die = comp_unit_die ();
15744     }
15745   else if (TYPE_P (containing_scope))
15746     {
15747       /* For types, we can just look up the appropriate DIE.  */
15748       if (debug_info_level > DINFO_LEVEL_TERSE)
15749         scope_die = get_context_die (containing_scope);
15750       else
15751         {
15752           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15753           if (scope_die == NULL)
15754             scope_die = comp_unit_die ();
15755         }
15756     }
15757   else
15758     scope_die = context_die;
15759
15760   return scope_die;
15761 }
15762
15763 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15764
15765 static inline int
15766 local_scope_p (dw_die_ref context_die)
15767 {
15768   for (; context_die; context_die = context_die->die_parent)
15769     if (context_die->die_tag == DW_TAG_inlined_subroutine
15770         || context_die->die_tag == DW_TAG_subprogram)
15771       return 1;
15772
15773   return 0;
15774 }
15775
15776 /* Returns nonzero if CONTEXT_DIE is a class.  */
15777
15778 static inline int
15779 class_scope_p (dw_die_ref context_die)
15780 {
15781   return (context_die
15782           && (context_die->die_tag == DW_TAG_structure_type
15783               || context_die->die_tag == DW_TAG_class_type
15784               || context_die->die_tag == DW_TAG_interface_type
15785               || context_die->die_tag == DW_TAG_union_type));
15786 }
15787
15788 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15789    whether or not to treat a DIE in this context as a declaration.  */
15790
15791 static inline int
15792 class_or_namespace_scope_p (dw_die_ref context_die)
15793 {
15794   return (class_scope_p (context_die)
15795           || (context_die && context_die->die_tag == DW_TAG_namespace));
15796 }
15797
15798 /* Many forms of DIEs require a "type description" attribute.  This
15799    routine locates the proper "type descriptor" die for the type given
15800    by 'type', and adds a DW_AT_type attribute below the given die.  */
15801
15802 static void
15803 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15804                     int decl_volatile, dw_die_ref context_die)
15805 {
15806   enum tree_code code  = TREE_CODE (type);
15807   dw_die_ref type_die  = NULL;
15808
15809   /* ??? If this type is an unnamed subrange type of an integral, floating-point
15810      or fixed-point type, use the inner type.  This is because we have no
15811      support for unnamed types in base_type_die.  This can happen if this is
15812      an Ada subrange type.  Correct solution is emit a subrange type die.  */
15813   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15814       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15815     type = TREE_TYPE (type), code = TREE_CODE (type);
15816
15817   if (code == ERROR_MARK
15818       /* Handle a special case.  For functions whose return type is void, we
15819          generate *no* type attribute.  (Note that no object may have type
15820          `void', so this only applies to function return types).  */
15821       || code == VOID_TYPE)
15822     return;
15823
15824   type_die = modified_type_die (type,
15825                                 decl_const || TYPE_READONLY (type),
15826                                 decl_volatile || TYPE_VOLATILE (type),
15827                                 context_die);
15828
15829   if (type_die != NULL)
15830     add_AT_die_ref (object_die, DW_AT_type, type_die);
15831 }
15832
15833 /* Given an object die, add the calling convention attribute for the
15834    function call type.  */
15835 static void
15836 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15837 {
15838   enum dwarf_calling_convention value = DW_CC_normal;
15839
15840   value = ((enum dwarf_calling_convention)
15841            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15842
15843   if (is_fortran ()
15844       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15845     {
15846       /* DWARF 2 doesn't provide a way to identify a program's source-level
15847         entry point.  DW_AT_calling_convention attributes are only meant
15848         to describe functions' calling conventions.  However, lacking a
15849         better way to signal the Fortran main program, we used this for 
15850         a long time, following existing custom.  Now, DWARF 4 has 
15851         DW_AT_main_subprogram, which we add below, but some tools still
15852         rely on the old way, which we thus keep.  */
15853       value = DW_CC_program;
15854
15855       if (dwarf_version >= 4 || !dwarf_strict)
15856         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15857     }
15858
15859   /* Only add the attribute if the backend requests it, and
15860      is not DW_CC_normal.  */
15861   if (value && (value != DW_CC_normal))
15862     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15863 }
15864
15865 /* Given a tree pointer to a struct, class, union, or enum type node, return
15866    a pointer to the (string) tag name for the given type, or zero if the type
15867    was declared without a tag.  */
15868
15869 static const char *
15870 type_tag (const_tree type)
15871 {
15872   const char *name = 0;
15873
15874   if (TYPE_NAME (type) != 0)
15875     {
15876       tree t = 0;
15877
15878       /* Find the IDENTIFIER_NODE for the type name.  */
15879       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15880           && !TYPE_NAMELESS (type))
15881         t = TYPE_NAME (type);
15882
15883       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15884          a TYPE_DECL node, regardless of whether or not a `typedef' was
15885          involved.  */
15886       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15887                && ! DECL_IGNORED_P (TYPE_NAME (type)))
15888         {
15889           /* We want to be extra verbose.  Don't call dwarf_name if
15890              DECL_NAME isn't set.  The default hook for decl_printable_name
15891              doesn't like that, and in this context it's correct to return
15892              0, instead of "<anonymous>" or the like.  */
15893           if (DECL_NAME (TYPE_NAME (type))
15894               && !DECL_NAMELESS (TYPE_NAME (type)))
15895             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15896         }
15897
15898       /* Now get the name as a string, or invent one.  */
15899       if (!name && t != 0)
15900         name = IDENTIFIER_POINTER (t);
15901     }
15902
15903   return (name == 0 || *name == '\0') ? 0 : name;
15904 }
15905
15906 /* Return the type associated with a data member, make a special check
15907    for bit field types.  */
15908
15909 static inline tree
15910 member_declared_type (const_tree member)
15911 {
15912   return (DECL_BIT_FIELD_TYPE (member)
15913           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15914 }
15915
15916 /* Get the decl's label, as described by its RTL. This may be different
15917    from the DECL_NAME name used in the source file.  */
15918
15919 #if 0
15920 static const char *
15921 decl_start_label (tree decl)
15922 {
15923   rtx x;
15924   const char *fnname;
15925
15926   x = DECL_RTL (decl);
15927   gcc_assert (MEM_P (x));
15928
15929   x = XEXP (x, 0);
15930   gcc_assert (GET_CODE (x) == SYMBOL_REF);
15931
15932   fnname = XSTR (x, 0);
15933   return fnname;
15934 }
15935 #endif
15936 \f
15937 /* These routines generate the internal representation of the DIE's for
15938    the compilation unit.  Debugging information is collected by walking
15939    the declaration trees passed in from dwarf2out_decl().  */
15940
15941 static void
15942 gen_array_type_die (tree type, dw_die_ref context_die)
15943 {
15944   dw_die_ref scope_die = scope_die_for (type, context_die);
15945   dw_die_ref array_die;
15946
15947   /* GNU compilers represent multidimensional array types as sequences of one
15948      dimensional array types whose element types are themselves array types.
15949      We sometimes squish that down to a single array_type DIE with multiple
15950      subscripts in the Dwarf debugging info.  The draft Dwarf specification
15951      say that we are allowed to do this kind of compression in C, because
15952      there is no difference between an array of arrays and a multidimensional
15953      array.  We don't do this for Ada to remain as close as possible to the
15954      actual representation, which is especially important against the language
15955      flexibilty wrt arrays of variable size.  */
15956
15957   bool collapse_nested_arrays = !is_ada ();
15958   tree element_type;
15959
15960   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15961      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
15962   if (TYPE_STRING_FLAG (type)
15963       && TREE_CODE (type) == ARRAY_TYPE
15964       && is_fortran ()
15965       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15966     {
15967       HOST_WIDE_INT size;
15968
15969       array_die = new_die (DW_TAG_string_type, scope_die, type);
15970       add_name_attribute (array_die, type_tag (type));
15971       equate_type_number_to_die (type, array_die);
15972       size = int_size_in_bytes (type);
15973       if (size >= 0)
15974         add_AT_unsigned (array_die, DW_AT_byte_size, size);
15975       else if (TYPE_DOMAIN (type) != NULL_TREE
15976                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15977                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15978         {
15979           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15980           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15981
15982           size = int_size_in_bytes (TREE_TYPE (szdecl));
15983           if (loc && size > 0)
15984             {
15985               add_AT_location_description (array_die, DW_AT_string_length, loc);
15986               if (size != DWARF2_ADDR_SIZE)
15987                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15988             }
15989         }
15990       return;
15991     }
15992
15993   array_die = new_die (DW_TAG_array_type, scope_die, type);
15994   add_name_attribute (array_die, type_tag (type));
15995   equate_type_number_to_die (type, array_die);
15996
15997   if (TREE_CODE (type) == VECTOR_TYPE)
15998     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15999
16000   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16001   if (is_fortran ()
16002       && TREE_CODE (type) == ARRAY_TYPE
16003       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16004       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16005     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16006
16007 #if 0
16008   /* We default the array ordering.  SDB will probably do
16009      the right things even if DW_AT_ordering is not present.  It's not even
16010      an issue until we start to get into multidimensional arrays anyway.  If
16011      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16012      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16013      and when we find out that we need to put these in, we will only do so
16014      for multidimensional arrays.  */
16015   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16016 #endif
16017
16018   if (TREE_CODE (type) == VECTOR_TYPE)
16019     {
16020       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16021       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16022       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16023       add_bound_info (subrange_die, DW_AT_upper_bound,
16024                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16025     }
16026   else
16027     add_subscript_info (array_die, type, collapse_nested_arrays);
16028
16029   /* Add representation of the type of the elements of this array type and
16030      emit the corresponding DIE if we haven't done it already.  */
16031   element_type = TREE_TYPE (type);
16032   if (collapse_nested_arrays)
16033     while (TREE_CODE (element_type) == ARRAY_TYPE)
16034       {
16035         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16036           break;
16037         element_type = TREE_TYPE (element_type);
16038       }
16039
16040   add_type_attribute (array_die, element_type, 0, 0, context_die);
16041
16042   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16043   if (TYPE_ARTIFICIAL (type))
16044     add_AT_flag (array_die, DW_AT_artificial, 1);
16045
16046   if (get_AT (array_die, DW_AT_name))
16047     add_pubtype (type, array_die);
16048 }
16049
16050 static dw_loc_descr_ref
16051 descr_info_loc (tree val, tree base_decl)
16052 {
16053   HOST_WIDE_INT size;
16054   dw_loc_descr_ref loc, loc2;
16055   enum dwarf_location_atom op;
16056
16057   if (val == base_decl)
16058     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16059
16060   switch (TREE_CODE (val))
16061     {
16062     CASE_CONVERT:
16063       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16064     case VAR_DECL:
16065       return loc_descriptor_from_tree (val, 0);
16066     case INTEGER_CST:
16067       if (host_integerp (val, 0))
16068         return int_loc_descriptor (tree_low_cst (val, 0));
16069       break;
16070     case INDIRECT_REF:
16071       size = int_size_in_bytes (TREE_TYPE (val));
16072       if (size < 0)
16073         break;
16074       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16075       if (!loc)
16076         break;
16077       if (size == DWARF2_ADDR_SIZE)
16078         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16079       else
16080         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16081       return loc;
16082     case POINTER_PLUS_EXPR:
16083     case PLUS_EXPR:
16084       if (host_integerp (TREE_OPERAND (val, 1), 1)
16085           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16086              < 16384)
16087         {
16088           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16089           if (!loc)
16090             break;
16091           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16092         }
16093       else
16094         {
16095           op = DW_OP_plus;
16096         do_binop:
16097           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16098           if (!loc)
16099             break;
16100           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16101           if (!loc2)
16102             break;
16103           add_loc_descr (&loc, loc2);
16104           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16105         }
16106       return loc;
16107     case MINUS_EXPR:
16108       op = DW_OP_minus;
16109       goto do_binop;
16110     case MULT_EXPR:
16111       op = DW_OP_mul;
16112       goto do_binop;
16113     case EQ_EXPR:
16114       op = DW_OP_eq;
16115       goto do_binop;
16116     case NE_EXPR:
16117       op = DW_OP_ne;
16118       goto do_binop;
16119     default:
16120       break;
16121     }
16122   return NULL;
16123 }
16124
16125 static void
16126 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16127                       tree val, tree base_decl)
16128 {
16129   dw_loc_descr_ref loc;
16130
16131   if (host_integerp (val, 0))
16132     {
16133       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16134       return;
16135     }
16136
16137   loc = descr_info_loc (val, base_decl);
16138   if (!loc)
16139     return;
16140
16141   add_AT_loc (die, attr, loc);
16142 }
16143
16144 /* This routine generates DIE for array with hidden descriptor, details
16145    are filled into *info by a langhook.  */
16146
16147 static void
16148 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16149                           dw_die_ref context_die)
16150 {
16151   dw_die_ref scope_die = scope_die_for (type, context_die);
16152   dw_die_ref array_die;
16153   int dim;
16154
16155   array_die = new_die (DW_TAG_array_type, scope_die, type);
16156   add_name_attribute (array_die, type_tag (type));
16157   equate_type_number_to_die (type, array_die);
16158
16159   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16160   if (is_fortran ()
16161       && info->ndimensions >= 2)
16162     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16163
16164   if (info->data_location)
16165     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16166                           info->base_decl);
16167   if (info->associated)
16168     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16169                           info->base_decl);
16170   if (info->allocated)
16171     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16172                           info->base_decl);
16173
16174   for (dim = 0; dim < info->ndimensions; dim++)
16175     {
16176       dw_die_ref subrange_die
16177         = new_die (DW_TAG_subrange_type, array_die, NULL);
16178
16179       if (info->dimen[dim].lower_bound)
16180         {
16181           /* If it is the default value, omit it.  */
16182           int dflt;
16183
16184           if (host_integerp (info->dimen[dim].lower_bound, 0)
16185               && (dflt = lower_bound_default ()) != -1
16186               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16187             ;
16188           else
16189             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16190                                   info->dimen[dim].lower_bound,
16191                                   info->base_decl);
16192         }
16193       if (info->dimen[dim].upper_bound)
16194         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16195                               info->dimen[dim].upper_bound,
16196                               info->base_decl);
16197       if (info->dimen[dim].stride)
16198         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16199                               info->dimen[dim].stride,
16200                               info->base_decl);
16201     }
16202
16203   gen_type_die (info->element_type, context_die);
16204   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16205
16206   if (get_AT (array_die, DW_AT_name))
16207     add_pubtype (type, array_die);
16208 }
16209
16210 #if 0
16211 static void
16212 gen_entry_point_die (tree decl, dw_die_ref context_die)
16213 {
16214   tree origin = decl_ultimate_origin (decl);
16215   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16216
16217   if (origin != NULL)
16218     add_abstract_origin_attribute (decl_die, origin);
16219   else
16220     {
16221       add_name_and_src_coords_attributes (decl_die, decl);
16222       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16223                           0, 0, context_die);
16224     }
16225
16226   if (DECL_ABSTRACT (decl))
16227     equate_decl_number_to_die (decl, decl_die);
16228   else
16229     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16230 }
16231 #endif
16232
16233 /* Walk through the list of incomplete types again, trying once more to
16234    emit full debugging info for them.  */
16235
16236 static void
16237 retry_incomplete_types (void)
16238 {
16239   int i;
16240
16241   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16242     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16243                                   DINFO_USAGE_DIR_USE))
16244       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16245 }
16246
16247 /* Determine what tag to use for a record type.  */
16248
16249 static enum dwarf_tag
16250 record_type_tag (tree type)
16251 {
16252   if (! lang_hooks.types.classify_record)
16253     return DW_TAG_structure_type;
16254
16255   switch (lang_hooks.types.classify_record (type))
16256     {
16257     case RECORD_IS_STRUCT:
16258       return DW_TAG_structure_type;
16259
16260     case RECORD_IS_CLASS:
16261       return DW_TAG_class_type;
16262
16263     case RECORD_IS_INTERFACE:
16264       if (dwarf_version >= 3 || !dwarf_strict)
16265         return DW_TAG_interface_type;
16266       return DW_TAG_structure_type;
16267
16268     default:
16269       gcc_unreachable ();
16270     }
16271 }
16272
16273 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16274    include all of the information about the enumeration values also. Each
16275    enumerated type name/value is listed as a child of the enumerated type
16276    DIE.  */
16277
16278 static dw_die_ref
16279 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16280 {
16281   dw_die_ref type_die = lookup_type_die (type);
16282
16283   if (type_die == NULL)
16284     {
16285       type_die = new_die (DW_TAG_enumeration_type,
16286                           scope_die_for (type, context_die), type);
16287       equate_type_number_to_die (type, type_die);
16288       add_name_attribute (type_die, type_tag (type));
16289       if (dwarf_version >= 4 || !dwarf_strict)
16290         {
16291           if (ENUM_IS_SCOPED (type))
16292             add_AT_flag (type_die, DW_AT_enum_class, 1);
16293           if (ENUM_IS_OPAQUE (type))
16294             add_AT_flag (type_die, DW_AT_declaration, 1);
16295         }
16296     }
16297   else if (! TYPE_SIZE (type))
16298     return type_die;
16299   else
16300     remove_AT (type_die, DW_AT_declaration);
16301
16302   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16303      given enum type is incomplete, do not generate the DW_AT_byte_size
16304      attribute or the DW_AT_element_list attribute.  */
16305   if (TYPE_SIZE (type))
16306     {
16307       tree link;
16308
16309       TREE_ASM_WRITTEN (type) = 1;
16310       add_byte_size_attribute (type_die, type);
16311       if (TYPE_STUB_DECL (type) != NULL_TREE)
16312         {
16313           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16314           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16315         }
16316
16317       /* If the first reference to this type was as the return type of an
16318          inline function, then it may not have a parent.  Fix this now.  */
16319       if (type_die->die_parent == NULL)
16320         add_child_die (scope_die_for (type, context_die), type_die);
16321
16322       for (link = TYPE_VALUES (type);
16323            link != NULL; link = TREE_CHAIN (link))
16324         {
16325           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16326           tree value = TREE_VALUE (link);
16327
16328           add_name_attribute (enum_die,
16329                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16330
16331           if (TREE_CODE (value) == CONST_DECL)
16332             value = DECL_INITIAL (value);
16333
16334           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16335             /* DWARF2 does not provide a way of indicating whether or
16336                not enumeration constants are signed or unsigned.  GDB
16337                always assumes the values are signed, so we output all
16338                values as if they were signed.  That means that
16339                enumeration constants with very large unsigned values
16340                will appear to have negative values in the debugger.  */
16341             add_AT_int (enum_die, DW_AT_const_value,
16342                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16343         }
16344
16345       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16346       if (TYPE_ARTIFICIAL (type))
16347         add_AT_flag (type_die, DW_AT_artificial, 1);
16348     }
16349   else
16350     add_AT_flag (type_die, DW_AT_declaration, 1);
16351
16352   add_pubtype (type, type_die);
16353
16354   return type_die;
16355 }
16356
16357 /* Generate a DIE to represent either a real live formal parameter decl or to
16358    represent just the type of some formal parameter position in some function
16359    type.
16360
16361    Note that this routine is a bit unusual because its argument may be a
16362    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16363    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16364    node.  If it's the former then this function is being called to output a
16365    DIE to represent a formal parameter object (or some inlining thereof).  If
16366    it's the latter, then this function is only being called to output a
16367    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16368    argument type of some subprogram type.
16369    If EMIT_NAME_P is true, name and source coordinate attributes
16370    are emitted.  */
16371
16372 static dw_die_ref
16373 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16374                           dw_die_ref context_die)
16375 {
16376   tree node_or_origin = node ? node : origin;
16377   tree ultimate_origin;
16378   dw_die_ref parm_die
16379     = new_die (DW_TAG_formal_parameter, context_die, node);
16380
16381   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16382     {
16383     case tcc_declaration:
16384       ultimate_origin = decl_ultimate_origin (node_or_origin);
16385       if (node || ultimate_origin)
16386         origin = ultimate_origin;
16387       if (origin != NULL)
16388         add_abstract_origin_attribute (parm_die, origin);
16389       else if (emit_name_p)
16390         add_name_and_src_coords_attributes (parm_die, node);
16391       if (origin == NULL
16392           || (! DECL_ABSTRACT (node_or_origin)
16393               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16394                                            decl_function_context
16395                                                             (node_or_origin))))
16396         {
16397           tree type = TREE_TYPE (node_or_origin);
16398           if (decl_by_reference_p (node_or_origin))
16399             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16400                                 context_die);
16401           else
16402             add_type_attribute (parm_die, type,
16403                                 TREE_READONLY (node_or_origin),
16404                                 TREE_THIS_VOLATILE (node_or_origin),
16405                                 context_die);
16406         }
16407       if (origin == NULL && DECL_ARTIFICIAL (node))
16408         add_AT_flag (parm_die, DW_AT_artificial, 1);
16409
16410       if (node && node != origin)
16411         equate_decl_number_to_die (node, parm_die);
16412       if (! DECL_ABSTRACT (node_or_origin))
16413         add_location_or_const_value_attribute (parm_die, node_or_origin,
16414                                                node == NULL, DW_AT_location);
16415
16416       break;
16417
16418     case tcc_type:
16419       /* We were called with some kind of a ..._TYPE node.  */
16420       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16421       break;
16422
16423     default:
16424       gcc_unreachable ();
16425     }
16426
16427   return parm_die;
16428 }
16429
16430 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16431    children DW_TAG_formal_parameter DIEs representing the arguments of the
16432    parameter pack.
16433
16434    PARM_PACK must be a function parameter pack.
16435    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16436    must point to the subsequent arguments of the function PACK_ARG belongs to.
16437    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16438    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16439    following the last one for which a DIE was generated.  */
16440
16441 static dw_die_ref
16442 gen_formal_parameter_pack_die  (tree parm_pack,
16443                                 tree pack_arg,
16444                                 dw_die_ref subr_die,
16445                                 tree *next_arg)
16446 {
16447   tree arg;
16448   dw_die_ref parm_pack_die;
16449
16450   gcc_assert (parm_pack
16451               && lang_hooks.function_parameter_pack_p (parm_pack)
16452               && subr_die);
16453
16454   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16455   add_src_coords_attributes (parm_pack_die, parm_pack);
16456
16457   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16458     {
16459       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16460                                                                  parm_pack))
16461         break;
16462       gen_formal_parameter_die (arg, NULL,
16463                                 false /* Don't emit name attribute.  */,
16464                                 parm_pack_die);
16465     }
16466   if (next_arg)
16467     *next_arg = arg;
16468   return parm_pack_die;
16469 }
16470
16471 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16472    at the end of an (ANSI prototyped) formal parameters list.  */
16473
16474 static void
16475 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16476 {
16477   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16478 }
16479
16480 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16481    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16482    parameters as specified in some function type specification (except for
16483    those which appear as part of a function *definition*).  */
16484
16485 static void
16486 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16487 {
16488   tree link;
16489   tree formal_type = NULL;
16490   tree first_parm_type;
16491   tree arg;
16492
16493   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16494     {
16495       arg = DECL_ARGUMENTS (function_or_method_type);
16496       function_or_method_type = TREE_TYPE (function_or_method_type);
16497     }
16498   else
16499     arg = NULL_TREE;
16500
16501   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16502
16503   /* Make our first pass over the list of formal parameter types and output a
16504      DW_TAG_formal_parameter DIE for each one.  */
16505   for (link = first_parm_type; link; )
16506     {
16507       dw_die_ref parm_die;
16508
16509       formal_type = TREE_VALUE (link);
16510       if (formal_type == void_type_node)
16511         break;
16512
16513       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16514       parm_die = gen_formal_parameter_die (formal_type, NULL,
16515                                            true /* Emit name attribute.  */,
16516                                            context_die);
16517       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16518           && link == first_parm_type)
16519         {
16520           add_AT_flag (parm_die, DW_AT_artificial, 1);
16521           if (dwarf_version >= 3 || !dwarf_strict)
16522             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16523         }
16524       else if (arg && DECL_ARTIFICIAL (arg))
16525         add_AT_flag (parm_die, DW_AT_artificial, 1);
16526
16527       link = TREE_CHAIN (link);
16528       if (arg)
16529         arg = DECL_CHAIN (arg);
16530     }
16531
16532   /* If this function type has an ellipsis, add a
16533      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16534   if (formal_type != void_type_node)
16535     gen_unspecified_parameters_die (function_or_method_type, context_die);
16536
16537   /* Make our second (and final) pass over the list of formal parameter types
16538      and output DIEs to represent those types (as necessary).  */
16539   for (link = TYPE_ARG_TYPES (function_or_method_type);
16540        link && TREE_VALUE (link);
16541        link = TREE_CHAIN (link))
16542     gen_type_die (TREE_VALUE (link), context_die);
16543 }
16544
16545 /* We want to generate the DIE for TYPE so that we can generate the
16546    die for MEMBER, which has been defined; we will need to refer back
16547    to the member declaration nested within TYPE.  If we're trying to
16548    generate minimal debug info for TYPE, processing TYPE won't do the
16549    trick; we need to attach the member declaration by hand.  */
16550
16551 static void
16552 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16553 {
16554   gen_type_die (type, context_die);
16555
16556   /* If we're trying to avoid duplicate debug info, we may not have
16557      emitted the member decl for this function.  Emit it now.  */
16558   if (TYPE_STUB_DECL (type)
16559       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16560       && ! lookup_decl_die (member))
16561     {
16562       dw_die_ref type_die;
16563       gcc_assert (!decl_ultimate_origin (member));
16564
16565       push_decl_scope (type);
16566       type_die = lookup_type_die_strip_naming_typedef (type);
16567       if (TREE_CODE (member) == FUNCTION_DECL)
16568         gen_subprogram_die (member, type_die);
16569       else if (TREE_CODE (member) == FIELD_DECL)
16570         {
16571           /* Ignore the nameless fields that are used to skip bits but handle
16572              C++ anonymous unions and structs.  */
16573           if (DECL_NAME (member) != NULL_TREE
16574               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16575               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16576             {
16577               gen_type_die (member_declared_type (member), type_die);
16578               gen_field_die (member, type_die);
16579             }
16580         }
16581       else
16582         gen_variable_die (member, NULL_TREE, type_die);
16583
16584       pop_decl_scope ();
16585     }
16586 }
16587 \f
16588 /* Forward declare these functions, because they are mutually recursive
16589   with their set_block_* pairing functions.  */
16590 static void set_decl_origin_self (tree);
16591 static void set_decl_abstract_flags (tree, int);
16592
16593 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16594    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16595    that it points to the node itself, thus indicating that the node is its
16596    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16597    the given node is NULL, recursively descend the decl/block tree which
16598    it is the root of, and for each other ..._DECL or BLOCK node contained
16599    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16600    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16601    values to point to themselves.  */
16602
16603 static void
16604 set_block_origin_self (tree stmt)
16605 {
16606   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16607     {
16608       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16609
16610       {
16611         tree local_decl;
16612
16613         for (local_decl = BLOCK_VARS (stmt);
16614              local_decl != NULL_TREE;
16615              local_decl = DECL_CHAIN (local_decl))
16616           if (! DECL_EXTERNAL (local_decl))
16617             set_decl_origin_self (local_decl);  /* Potential recursion.  */
16618       }
16619
16620       {
16621         tree subblock;
16622
16623         for (subblock = BLOCK_SUBBLOCKS (stmt);
16624              subblock != NULL_TREE;
16625              subblock = BLOCK_CHAIN (subblock))
16626           set_block_origin_self (subblock);     /* Recurse.  */
16627       }
16628     }
16629 }
16630
16631 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16632    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16633    node to so that it points to the node itself, thus indicating that the
16634    node represents its own (abstract) origin.  Additionally, if the
16635    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16636    the decl/block tree of which the given node is the root of, and for
16637    each other ..._DECL or BLOCK node contained therein whose
16638    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16639    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16640    point to themselves.  */
16641
16642 static void
16643 set_decl_origin_self (tree decl)
16644 {
16645   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16646     {
16647       DECL_ABSTRACT_ORIGIN (decl) = decl;
16648       if (TREE_CODE (decl) == FUNCTION_DECL)
16649         {
16650           tree arg;
16651
16652           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16653             DECL_ABSTRACT_ORIGIN (arg) = arg;
16654           if (DECL_INITIAL (decl) != NULL_TREE
16655               && DECL_INITIAL (decl) != error_mark_node)
16656             set_block_origin_self (DECL_INITIAL (decl));
16657         }
16658     }
16659 }
16660 \f
16661 /* Given a pointer to some BLOCK node, and a boolean value to set the
16662    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16663    the given block, and for all local decls and all local sub-blocks
16664    (recursively) which are contained therein.  */
16665
16666 static void
16667 set_block_abstract_flags (tree stmt, int setting)
16668 {
16669   tree local_decl;
16670   tree subblock;
16671   unsigned int i;
16672
16673   BLOCK_ABSTRACT (stmt) = setting;
16674
16675   for (local_decl = BLOCK_VARS (stmt);
16676        local_decl != NULL_TREE;
16677        local_decl = DECL_CHAIN (local_decl))
16678     if (! DECL_EXTERNAL (local_decl))
16679       set_decl_abstract_flags (local_decl, setting);
16680
16681   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16682     {
16683       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16684       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16685           || TREE_CODE (local_decl) == PARM_DECL)
16686         set_decl_abstract_flags (local_decl, setting);
16687     }
16688
16689   for (subblock = BLOCK_SUBBLOCKS (stmt);
16690        subblock != NULL_TREE;
16691        subblock = BLOCK_CHAIN (subblock))
16692     set_block_abstract_flags (subblock, setting);
16693 }
16694
16695 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16696    "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16697    given decl, and (in the case where the decl is a FUNCTION_DECL) also
16698    set the abstract flags for all of the parameters, local vars, local
16699    blocks and sub-blocks (recursively) to the same setting.  */
16700
16701 static void
16702 set_decl_abstract_flags (tree decl, int setting)
16703 {
16704   DECL_ABSTRACT (decl) = setting;
16705   if (TREE_CODE (decl) == FUNCTION_DECL)
16706     {
16707       tree arg;
16708
16709       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16710         DECL_ABSTRACT (arg) = setting;
16711       if (DECL_INITIAL (decl) != NULL_TREE
16712           && DECL_INITIAL (decl) != error_mark_node)
16713         set_block_abstract_flags (DECL_INITIAL (decl), setting);
16714     }
16715 }
16716
16717 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16718    may later generate inlined and/or out-of-line instances of.  */
16719
16720 static void
16721 dwarf2out_abstract_function (tree decl)
16722 {
16723   dw_die_ref old_die;
16724   tree save_fn;
16725   tree context;
16726   int was_abstract;
16727   htab_t old_decl_loc_table;
16728   htab_t old_cached_dw_loc_list_table;
16729   int old_call_site_count, old_tail_call_site_count;
16730   struct call_arg_loc_node *old_call_arg_locations;
16731
16732   /* Make sure we have the actual abstract inline, not a clone.  */
16733   decl = DECL_ORIGIN (decl);
16734
16735   old_die = lookup_decl_die (decl);
16736   if (old_die && get_AT (old_die, DW_AT_inline))
16737     /* We've already generated the abstract instance.  */
16738     return;
16739
16740   /* We can be called while recursively when seeing block defining inlined subroutine
16741      DIE.  Be sure to not clobber the outer location table nor use it or we would
16742      get locations in abstract instantces.  */
16743   old_decl_loc_table = decl_loc_table;
16744   decl_loc_table = NULL;
16745   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16746   cached_dw_loc_list_table = NULL;
16747   old_call_arg_locations = call_arg_locations;
16748   call_arg_locations = NULL;
16749   old_call_site_count = call_site_count;
16750   call_site_count = -1;
16751   old_tail_call_site_count = tail_call_site_count;
16752   tail_call_site_count = -1;
16753
16754   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16755      we don't get confused by DECL_ABSTRACT.  */
16756   if (debug_info_level > DINFO_LEVEL_TERSE)
16757     {
16758       context = decl_class_context (decl);
16759       if (context)
16760         gen_type_die_for_member
16761           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16762     }
16763
16764   /* Pretend we've just finished compiling this function.  */
16765   save_fn = current_function_decl;
16766   current_function_decl = decl;
16767
16768   was_abstract = DECL_ABSTRACT (decl);
16769   set_decl_abstract_flags (decl, 1);
16770   dwarf2out_decl (decl);
16771   if (! was_abstract)
16772     set_decl_abstract_flags (decl, 0);
16773
16774   current_function_decl = save_fn;
16775   decl_loc_table = old_decl_loc_table;
16776   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16777   call_arg_locations = old_call_arg_locations;
16778   call_site_count = old_call_site_count;
16779   tail_call_site_count = old_tail_call_site_count;
16780 }
16781
16782 /* Helper function of premark_used_types() which gets called through
16783    htab_traverse.
16784
16785    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16786    marked as unused by prune_unused_types.  */
16787
16788 static int
16789 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16790 {
16791   tree type;
16792   dw_die_ref die;
16793
16794   type = (tree) *slot;
16795   die = lookup_type_die (type);
16796   if (die != NULL)
16797     die->die_perennial_p = 1;
16798   return 1;
16799 }
16800
16801 /* Helper function of premark_types_used_by_global_vars which gets called
16802    through htab_traverse.
16803
16804    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16805    marked as unused by prune_unused_types. The DIE of the type is marked
16806    only if the global variable using the type will actually be emitted.  */
16807
16808 static int
16809 premark_types_used_by_global_vars_helper (void **slot,
16810                                           void *data ATTRIBUTE_UNUSED)
16811 {
16812   struct types_used_by_vars_entry *entry;
16813   dw_die_ref die;
16814
16815   entry = (struct types_used_by_vars_entry *) *slot;
16816   gcc_assert (entry->type != NULL
16817               && entry->var_decl != NULL);
16818   die = lookup_type_die (entry->type);
16819   if (die)
16820     {
16821       /* Ask cgraph if the global variable really is to be emitted.
16822          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16823       struct varpool_node *node = varpool_get_node (entry->var_decl);
16824       if (node && node->analyzed)
16825         {
16826           die->die_perennial_p = 1;
16827           /* Keep the parent DIEs as well.  */
16828           while ((die = die->die_parent) && die->die_perennial_p == 0)
16829             die->die_perennial_p = 1;
16830         }
16831     }
16832   return 1;
16833 }
16834
16835 /* Mark all members of used_types_hash as perennial.  */
16836
16837 static void
16838 premark_used_types (struct function *fun)
16839 {
16840   if (fun && fun->used_types_hash)
16841     htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
16842 }
16843
16844 /* Mark all members of types_used_by_vars_entry as perennial.  */
16845
16846 static void
16847 premark_types_used_by_global_vars (void)
16848 {
16849   if (types_used_by_vars_hash)
16850     htab_traverse (types_used_by_vars_hash,
16851                    premark_types_used_by_global_vars_helper, NULL);
16852 }
16853
16854 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16855    for CA_LOC call arg loc node.  */
16856
16857 static dw_die_ref
16858 gen_call_site_die (tree decl, dw_die_ref subr_die,
16859                    struct call_arg_loc_node *ca_loc)
16860 {
16861   dw_die_ref stmt_die = NULL, die;
16862   tree block = ca_loc->block;
16863
16864   while (block
16865          && block != DECL_INITIAL (decl)
16866          && TREE_CODE (block) == BLOCK)
16867     {
16868       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16869         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16870       if (stmt_die)
16871         break;
16872       block = BLOCK_SUPERCONTEXT (block);
16873     }
16874   if (stmt_die == NULL)
16875     stmt_die = subr_die;
16876   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16877   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16878   if (ca_loc->tail_call_p)
16879     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16880   if (ca_loc->symbol_ref)
16881     {
16882       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16883       if (tdie)
16884         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16885       else
16886         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16887     }
16888   return die;
16889 }
16890
16891 /* Generate a DIE to represent a declared function (either file-scope or
16892    block-local).  */
16893
16894 static void
16895 gen_subprogram_die (tree decl, dw_die_ref context_die)
16896 {
16897   tree origin = decl_ultimate_origin (decl);
16898   dw_die_ref subr_die;
16899   tree outer_scope;
16900   dw_die_ref old_die = lookup_decl_die (decl);
16901   int declaration = (current_function_decl != decl
16902                      || class_or_namespace_scope_p (context_die));
16903
16904   premark_used_types (DECL_STRUCT_FUNCTION (decl));
16905
16906   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16907      started to generate the abstract instance of an inline, decided to output
16908      its containing class, and proceeded to emit the declaration of the inline
16909      from the member list for the class.  If so, DECLARATION takes priority;
16910      we'll get back to the abstract instance when done with the class.  */
16911
16912   /* The class-scope declaration DIE must be the primary DIE.  */
16913   if (origin && declaration && class_or_namespace_scope_p (context_die))
16914     {
16915       origin = NULL;
16916       gcc_assert (!old_die);
16917     }
16918
16919   /* Now that the C++ front end lazily declares artificial member fns, we
16920      might need to retrofit the declaration into its class.  */
16921   if (!declaration && !origin && !old_die
16922       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16923       && !class_or_namespace_scope_p (context_die)
16924       && debug_info_level > DINFO_LEVEL_TERSE)
16925     old_die = force_decl_die (decl);
16926
16927   if (origin != NULL)
16928     {
16929       gcc_assert (!declaration || local_scope_p (context_die));
16930
16931       /* Fixup die_parent for the abstract instance of a nested
16932          inline function.  */
16933       if (old_die && old_die->die_parent == NULL)
16934         add_child_die (context_die, old_die);
16935
16936       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16937       add_abstract_origin_attribute (subr_die, origin);
16938       /*  This is where the actual code for a cloned function is.
16939           Let's emit linkage name attribute for it.  This helps
16940           debuggers to e.g, set breakpoints into
16941           constructors/destructors when the user asks "break
16942           K::K".  */
16943       add_linkage_name (subr_die, decl);
16944     }
16945   else if (old_die)
16946     {
16947       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16948       struct dwarf_file_data * file_index = lookup_filename (s.file);
16949
16950       if (!get_AT_flag (old_die, DW_AT_declaration)
16951           /* We can have a normal definition following an inline one in the
16952              case of redefinition of GNU C extern inlines.
16953              It seems reasonable to use AT_specification in this case.  */
16954           && !get_AT (old_die, DW_AT_inline))
16955         {
16956           /* Detect and ignore this case, where we are trying to output
16957              something we have already output.  */
16958           return;
16959         }
16960
16961       /* If the definition comes from the same place as the declaration,
16962          maybe use the old DIE.  We always want the DIE for this function
16963          that has the *_pc attributes to be under comp_unit_die so the
16964          debugger can find it.  We also need to do this for abstract
16965          instances of inlines, since the spec requires the out-of-line copy
16966          to have the same parent.  For local class methods, this doesn't
16967          apply; we just use the old DIE.  */
16968       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16969           && (DECL_ARTIFICIAL (decl)
16970               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16971                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
16972                       == (unsigned) s.line))))
16973         {
16974           subr_die = old_die;
16975
16976           /* Clear out the declaration attribute and the formal parameters.
16977              Do not remove all children, because it is possible that this
16978              declaration die was forced using force_decl_die(). In such
16979              cases die that forced declaration die (e.g. TAG_imported_module)
16980              is one of the children that we do not want to remove.  */
16981           remove_AT (subr_die, DW_AT_declaration);
16982           remove_AT (subr_die, DW_AT_object_pointer);
16983           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16984         }
16985       else
16986         {
16987           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16988           add_AT_specification (subr_die, old_die);
16989           add_pubname (decl, subr_die);
16990           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16991             add_AT_file (subr_die, DW_AT_decl_file, file_index);
16992           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16993             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
16994         }
16995     }
16996   else
16997     {
16998       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16999
17000       if (TREE_PUBLIC (decl))
17001         add_AT_flag (subr_die, DW_AT_external, 1);
17002
17003       add_name_and_src_coords_attributes (subr_die, decl);
17004       add_pubname (decl, subr_die);
17005       if (debug_info_level > DINFO_LEVEL_TERSE)
17006         {
17007           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17008           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17009                               0, 0, context_die);
17010         }
17011
17012       add_pure_or_virtual_attribute (subr_die, decl);
17013       if (DECL_ARTIFICIAL (decl))
17014         add_AT_flag (subr_die, DW_AT_artificial, 1);
17015
17016       add_accessibility_attribute (subr_die, decl);
17017     }
17018
17019   if (declaration)
17020     {
17021       if (!old_die || !get_AT (old_die, DW_AT_inline))
17022         {
17023           add_AT_flag (subr_die, DW_AT_declaration, 1);
17024
17025           /* If this is an explicit function declaration then generate
17026              a DW_AT_explicit attribute.  */
17027           if (lang_hooks.decls.function_decl_explicit_p (decl)
17028               && (dwarf_version >= 3 || !dwarf_strict))
17029             add_AT_flag (subr_die, DW_AT_explicit, 1);
17030
17031           /* The first time we see a member function, it is in the context of
17032              the class to which it belongs.  We make sure of this by emitting
17033              the class first.  The next time is the definition, which is
17034              handled above.  The two may come from the same source text.
17035
17036              Note that force_decl_die() forces function declaration die. It is
17037              later reused to represent definition.  */
17038           equate_decl_number_to_die (decl, subr_die);
17039         }
17040     }
17041   else if (DECL_ABSTRACT (decl))
17042     {
17043       if (DECL_DECLARED_INLINE_P (decl))
17044         {
17045           if (cgraph_function_possibly_inlined_p (decl))
17046             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17047           else
17048             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17049         }
17050       else
17051         {
17052           if (cgraph_function_possibly_inlined_p (decl))
17053             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17054           else
17055             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17056         }
17057
17058       if (DECL_DECLARED_INLINE_P (decl)
17059           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17060         add_AT_flag (subr_die, DW_AT_artificial, 1);
17061
17062       equate_decl_number_to_die (decl, subr_die);
17063     }
17064   else if (!DECL_EXTERNAL (decl))
17065     {
17066       HOST_WIDE_INT cfa_fb_offset;
17067       struct function *fun = DECL_STRUCT_FUNCTION (decl);
17068
17069       if (!old_die || !get_AT (old_die, DW_AT_inline))
17070         equate_decl_number_to_die (decl, subr_die);
17071
17072       gcc_checking_assert (fun);
17073       if (!flag_reorder_blocks_and_partition)
17074         {
17075           dw_fde_ref fde = fun->fde;
17076           if (fde->dw_fde_begin)
17077             {
17078               /* We have already generated the labels.  */
17079               add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17080             }
17081           else
17082             {
17083               /* Create start/end labels and add the range.  */
17084               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17085               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17086               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17087                                            current_function_funcdef_no);
17088               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17089                                            current_function_funcdef_no);
17090               add_AT_low_high_pc (subr_die, label_id_low, label_id_high);
17091             }
17092
17093 #if VMS_DEBUGGING_INFO
17094       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17095          Section 2.3 Prologue and Epilogue Attributes:
17096          When a breakpoint is set on entry to a function, it is generally
17097          desirable for execution to be suspended, not on the very first
17098          instruction of the function, but rather at a point after the
17099          function's frame has been set up, after any language defined local
17100          declaration processing has been completed, and before execution of
17101          the first statement of the function begins. Debuggers generally
17102          cannot properly determine where this point is.  Similarly for a
17103          breakpoint set on exit from a function. The prologue and epilogue
17104          attributes allow a compiler to communicate the location(s) to use.  */
17105
17106       {
17107         if (fde->dw_fde_vms_end_prologue)
17108           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17109             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17110
17111         if (fde->dw_fde_vms_begin_epilogue)
17112           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17113             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17114       }
17115 #endif
17116
17117         }
17118       else
17119         {
17120           /* Generate pubnames entries for the split function code ranges.  */
17121           dw_fde_ref fde = fun->fde;
17122
17123           if (fde->dw_fde_second_begin)
17124             {
17125               if (dwarf_version >= 3 || !dwarf_strict)
17126                 {
17127                   /* We should use ranges for non-contiguous code section 
17128                      addresses.  Use the actual code range for the initial
17129                      section, since the HOT/COLD labels might precede an 
17130                      alignment offset.  */
17131                   bool range_list_added = false;
17132                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17133                                         fde->dw_fde_end, &range_list_added);
17134                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17135                                         fde->dw_fde_second_end,
17136                                         &range_list_added);
17137                   if (range_list_added)
17138                     add_ranges (NULL);
17139                 }
17140               else
17141                 {
17142                   /* There is no real support in DW2 for this .. so we make
17143                      a work-around.  First, emit the pub name for the segment
17144                      containing the function label.  Then make and emit a
17145                      simplified subprogram DIE for the second segment with the
17146                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17147                      linkage name for the second die so that gdb will find both
17148                      sections when given "b foo".  */
17149                   const char *name = NULL;
17150                   tree decl_name = DECL_NAME (decl);
17151                   dw_die_ref seg_die;
17152
17153                   /* Do the 'primary' section.   */
17154                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17155                                       fde->dw_fde_end);
17156
17157                   /* Build a minimal DIE for the secondary section.  */
17158                   seg_die = new_die (DW_TAG_subprogram,
17159                                      subr_die->die_parent, decl);
17160
17161                   if (TREE_PUBLIC (decl))
17162                     add_AT_flag (seg_die, DW_AT_external, 1);
17163
17164                   if (decl_name != NULL 
17165                       && IDENTIFIER_POINTER (decl_name) != NULL)
17166                     {
17167                       name = dwarf2_name (decl, 1);
17168                       if (! DECL_ARTIFICIAL (decl))
17169                         add_src_coords_attributes (seg_die, decl);
17170
17171                       add_linkage_name (seg_die, decl);
17172                     }
17173                   gcc_assert (name != NULL);
17174                   add_pure_or_virtual_attribute (seg_die, decl);
17175                   if (DECL_ARTIFICIAL (decl))
17176                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17177
17178                   name = concat ("__second_sect_of_", name, NULL); 
17179                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17180                                       fde->dw_fde_second_end);
17181                   add_name_attribute (seg_die, name);
17182                   if (want_pubnames ())
17183                     add_pubname_string (name, seg_die);
17184                 }
17185             }
17186           else
17187             add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end);
17188         }
17189
17190       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17191
17192       /* We define the "frame base" as the function's CFA.  This is more
17193          convenient for several reasons: (1) It's stable across the prologue
17194          and epilogue, which makes it better than just a frame pointer,
17195          (2) With dwarf3, there exists a one-byte encoding that allows us
17196          to reference the .debug_frame data by proxy, but failing that,
17197          (3) We can at least reuse the code inspection and interpretation
17198          code that determines the CFA position at various points in the
17199          function.  */
17200       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17201         {
17202           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17203           add_AT_loc (subr_die, DW_AT_frame_base, op);
17204         }
17205       else
17206         {
17207           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17208           if (list->dw_loc_next)
17209             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17210           else
17211             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17212         }
17213
17214       /* Compute a displacement from the "steady-state frame pointer" to
17215          the CFA.  The former is what all stack slots and argument slots
17216          will reference in the rtl; the later is what we've told the
17217          debugger about.  We'll need to adjust all frame_base references
17218          by this displacement.  */
17219       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17220
17221       if (fun->static_chain_decl)
17222         add_AT_location_description (subr_die, DW_AT_static_link,
17223                  loc_list_from_tree (fun->static_chain_decl, 2));
17224     }
17225
17226   /* Generate child dies for template paramaters.  */
17227   if (debug_info_level > DINFO_LEVEL_TERSE)
17228     gen_generic_params_dies (decl);
17229
17230   /* Now output descriptions of the arguments for this function. This gets
17231      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17232      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17233      `...' at the end of the formal parameter list.  In order to find out if
17234      there was a trailing ellipsis or not, we must instead look at the type
17235      associated with the FUNCTION_DECL.  This will be a node of type
17236      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17237      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17238      an ellipsis at the end.  */
17239
17240   /* In the case where we are describing a mere function declaration, all we
17241      need to do here (and all we *can* do here) is to describe the *types* of
17242      its formal parameters.  */
17243   if (debug_info_level <= DINFO_LEVEL_TERSE)
17244     ;
17245   else if (declaration)
17246     gen_formal_types_die (decl, subr_die);
17247   else
17248     {
17249       /* Generate DIEs to represent all known formal parameters.  */
17250       tree parm = DECL_ARGUMENTS (decl);
17251       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17252       tree generic_decl_parm = generic_decl
17253                                 ? DECL_ARGUMENTS (generic_decl)
17254                                 : NULL;
17255
17256       /* Now we want to walk the list of parameters of the function and
17257          emit their relevant DIEs.
17258
17259          We consider the case of DECL being an instance of a generic function
17260          as well as it being a normal function.
17261
17262          If DECL is an instance of a generic function we walk the
17263          parameters of the generic function declaration _and_ the parameters of
17264          DECL itself. This is useful because we want to emit specific DIEs for
17265          function parameter packs and those are declared as part of the
17266          generic function declaration. In that particular case,
17267          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17268          That DIE has children DIEs representing the set of arguments
17269          of the pack. Note that the set of pack arguments can be empty.
17270          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17271          children DIE.
17272
17273          Otherwise, we just consider the parameters of DECL.  */
17274       while (generic_decl_parm || parm)
17275         {
17276           if (generic_decl_parm
17277               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17278             gen_formal_parameter_pack_die (generic_decl_parm,
17279                                            parm, subr_die,
17280                                            &parm);
17281           else if (parm)
17282             {
17283               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17284
17285               if (parm == DECL_ARGUMENTS (decl)
17286                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17287                   && parm_die
17288                   && (dwarf_version >= 3 || !dwarf_strict))
17289                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17290
17291               parm = DECL_CHAIN (parm);
17292             }
17293
17294           if (generic_decl_parm)
17295             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17296         }
17297
17298       /* Decide whether we need an unspecified_parameters DIE at the end.
17299          There are 2 more cases to do this for: 1) the ansi ... declaration -
17300          this is detectable when the end of the arg list is not a
17301          void_type_node 2) an unprototyped function declaration (not a
17302          definition).  This just means that we have no info about the
17303          parameters at all.  */
17304       if (prototype_p (TREE_TYPE (decl)))
17305         {
17306           /* This is the prototyped case, check for....  */
17307           if (stdarg_p (TREE_TYPE (decl)))
17308             gen_unspecified_parameters_die (decl, subr_die);
17309         }
17310       else if (DECL_INITIAL (decl) == NULL_TREE)
17311         gen_unspecified_parameters_die (decl, subr_die);
17312     }
17313
17314   /* Output Dwarf info for all of the stuff within the body of the function
17315      (if it has one - it may be just a declaration).  */
17316   outer_scope = DECL_INITIAL (decl);
17317
17318   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17319      a function.  This BLOCK actually represents the outermost binding contour
17320      for the function, i.e. the contour in which the function's formal
17321      parameters and labels get declared. Curiously, it appears that the front
17322      end doesn't actually put the PARM_DECL nodes for the current function onto
17323      the BLOCK_VARS list for this outer scope, but are strung off of the
17324      DECL_ARGUMENTS list for the function instead.
17325
17326      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17327      the LABEL_DECL nodes for the function however, and we output DWARF info
17328      for those in decls_for_scope.  Just within the `outer_scope' there will be
17329      a BLOCK node representing the function's outermost pair of curly braces,
17330      and any blocks used for the base and member initializers of a C++
17331      constructor function.  */
17332   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
17333     {
17334       int call_site_note_count = 0;
17335       int tail_call_site_note_count = 0;
17336
17337       /* Emit a DW_TAG_variable DIE for a named return value.  */
17338       if (DECL_NAME (DECL_RESULT (decl)))
17339         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17340
17341       current_function_has_inlines = 0;
17342       decls_for_scope (outer_scope, subr_die, 0);
17343
17344       if (call_arg_locations && !dwarf_strict)
17345         {
17346           struct call_arg_loc_node *ca_loc;
17347           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17348             {
17349               dw_die_ref die = NULL;
17350               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17351               rtx arg, next_arg;
17352
17353               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17354                    arg; arg = next_arg)
17355                 {
17356                   dw_loc_descr_ref reg, val;
17357                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17358                   dw_die_ref cdie, tdie = NULL;
17359
17360                   next_arg = XEXP (arg, 1);
17361                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17362                       && next_arg
17363                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17364                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17365                       && REGNO (XEXP (XEXP (arg, 0), 0))
17366                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17367                     next_arg = XEXP (next_arg, 1);
17368                   if (mode == VOIDmode)
17369                     {
17370                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17371                       if (mode == VOIDmode)
17372                         mode = GET_MODE (XEXP (arg, 0));
17373                     }
17374                   if (mode == VOIDmode || mode == BLKmode)
17375                     continue;
17376                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17377                     {
17378                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17379                       tloc = XEXP (XEXP (arg, 0), 1);
17380                       continue;
17381                     }
17382                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17383                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17384                     {
17385                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17386                       tlocc = XEXP (XEXP (arg, 0), 1);
17387                       continue;
17388                     }
17389                   reg = NULL;
17390                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17391                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17392                                               VAR_INIT_STATUS_INITIALIZED);
17393                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17394                     {
17395                       rtx mem = XEXP (XEXP (arg, 0), 0);
17396                       reg = mem_loc_descriptor (XEXP (mem, 0),
17397                                                 get_address_mode (mem),
17398                                                 GET_MODE (mem),
17399                                                 VAR_INIT_STATUS_INITIALIZED);
17400                     }
17401                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17402                            == DEBUG_PARAMETER_REF)
17403                     {
17404                       tree tdecl
17405                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17406                       tdie = lookup_decl_die (tdecl);
17407                       if (tdie == NULL)
17408                         continue;
17409                     }
17410                   else
17411                     continue;
17412                   if (reg == NULL
17413                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17414                          != DEBUG_PARAMETER_REF)
17415                     continue;
17416                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17417                                             VOIDmode,
17418                                             VAR_INIT_STATUS_INITIALIZED);
17419                   if (val == NULL)
17420                     continue;
17421                   if (die == NULL)
17422                     die = gen_call_site_die (decl, subr_die, ca_loc);
17423                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17424                                   NULL_TREE);
17425                   if (reg != NULL)
17426                     add_AT_loc (cdie, DW_AT_location, reg);
17427                   else if (tdie != NULL)
17428                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17429                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17430                   if (next_arg != XEXP (arg, 1))
17431                     {
17432                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17433                       if (mode == VOIDmode)
17434                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17435                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17436                                                             0), 1),
17437                                                 mode, VOIDmode,
17438                                                 VAR_INIT_STATUS_INITIALIZED);
17439                       if (val != NULL)
17440                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17441                     }
17442                 }
17443               if (die == NULL
17444                   && (ca_loc->symbol_ref || tloc))
17445                 die = gen_call_site_die (decl, subr_die, ca_loc);
17446               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17447                 {
17448                   dw_loc_descr_ref tval = NULL;
17449
17450                   if (tloc != NULL_RTX)
17451                     tval = mem_loc_descriptor (tloc,
17452                                                GET_MODE (tloc) == VOIDmode
17453                                                ? Pmode : GET_MODE (tloc),
17454                                                VOIDmode,
17455                                                VAR_INIT_STATUS_INITIALIZED);
17456                   if (tval)
17457                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17458                   else if (tlocc != NULL_RTX)
17459                     {
17460                       tval = mem_loc_descriptor (tlocc,
17461                                                  GET_MODE (tlocc) == VOIDmode
17462                                                  ? Pmode : GET_MODE (tlocc),
17463                                                  VOIDmode,
17464                                                  VAR_INIT_STATUS_INITIALIZED);
17465                       if (tval)
17466                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17467                                     tval);
17468                     }
17469                 }
17470               if (die != NULL)
17471                 {
17472                   call_site_note_count++;
17473                   if (ca_loc->tail_call_p)
17474                     tail_call_site_note_count++;
17475                 }
17476             }
17477         }
17478       call_arg_locations = NULL;
17479       call_arg_loc_last = NULL;
17480       if (tail_call_site_count >= 0
17481           && tail_call_site_count == tail_call_site_note_count
17482           && !dwarf_strict)
17483         {
17484           if (call_site_count >= 0
17485               && call_site_count == call_site_note_count)
17486             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17487           else
17488             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17489         }
17490       call_site_count = -1;
17491       tail_call_site_count = -1;
17492     }
17493   /* Add the calling convention attribute if requested.  */
17494   add_calling_convention_attribute (subr_die, decl);
17495
17496 }
17497
17498 /* Returns a hash value for X (which really is a die_struct).  */
17499
17500 static hashval_t
17501 common_block_die_table_hash (const void *x)
17502 {
17503   const_dw_die_ref d = (const_dw_die_ref) x;
17504   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17505 }
17506
17507 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17508    as decl_id and die_parent of die_struct Y.  */
17509
17510 static int
17511 common_block_die_table_eq (const void *x, const void *y)
17512 {
17513   const_dw_die_ref d = (const_dw_die_ref) x;
17514   const_dw_die_ref e = (const_dw_die_ref) y;
17515   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17516 }
17517
17518 /* Generate a DIE to represent a declared data object.
17519    Either DECL or ORIGIN must be non-null.  */
17520
17521 static void
17522 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17523 {
17524   HOST_WIDE_INT off = 0;
17525   tree com_decl;
17526   tree decl_or_origin = decl ? decl : origin;
17527   tree ultimate_origin;
17528   dw_die_ref var_die;
17529   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17530   dw_die_ref origin_die;
17531   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17532                       || class_or_namespace_scope_p (context_die));
17533   bool specialization_p = false;
17534
17535   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17536   if (decl || ultimate_origin)
17537     origin = ultimate_origin;
17538   com_decl = fortran_common (decl_or_origin, &off);
17539
17540   /* Symbol in common gets emitted as a child of the common block, in the form
17541      of a data member.  */
17542   if (com_decl)
17543     {
17544       dw_die_ref com_die;
17545       dw_loc_list_ref loc;
17546       die_node com_die_arg;
17547
17548       var_die = lookup_decl_die (decl_or_origin);
17549       if (var_die)
17550         {
17551           if (get_AT (var_die, DW_AT_location) == NULL)
17552             {
17553               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17554               if (loc)
17555                 {
17556                   if (off)
17557                     {
17558                       /* Optimize the common case.  */
17559                       if (single_element_loc_list_p (loc)
17560                           && loc->expr->dw_loc_opc == DW_OP_addr
17561                           && loc->expr->dw_loc_next == NULL
17562                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17563                              == SYMBOL_REF)
17564                         {
17565                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17566                           loc->expr->dw_loc_oprnd1.v.val_addr
17567                             = plus_constant (GET_MODE (x), x , off);
17568                         }
17569                       else
17570                         loc_list_plus_const (loc, off);
17571                     }
17572                   add_AT_location_description (var_die, DW_AT_location, loc);
17573                   remove_AT (var_die, DW_AT_declaration);
17574                 }
17575             }
17576           return;
17577         }
17578
17579       if (common_block_die_table == NULL)
17580         common_block_die_table
17581           = htab_create_ggc (10, common_block_die_table_hash,
17582                              common_block_die_table_eq, NULL);
17583
17584       com_die_arg.decl_id = DECL_UID (com_decl);
17585       com_die_arg.die_parent = context_die;
17586       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17587       loc = loc_list_from_tree (com_decl, 2);
17588       if (com_die == NULL)
17589         {
17590           const char *cnam
17591             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17592           void **slot;
17593
17594           com_die = new_die (DW_TAG_common_block, context_die, decl);
17595           add_name_and_src_coords_attributes (com_die, com_decl);
17596           if (loc)
17597             {
17598               add_AT_location_description (com_die, DW_AT_location, loc);
17599               /* Avoid sharing the same loc descriptor between
17600                  DW_TAG_common_block and DW_TAG_variable.  */
17601               loc = loc_list_from_tree (com_decl, 2);
17602             }
17603           else if (DECL_EXTERNAL (decl))
17604             add_AT_flag (com_die, DW_AT_declaration, 1);
17605           if (want_pubnames ())
17606             add_pubname_string (cnam, com_die); /* ??? needed? */
17607           com_die->decl_id = DECL_UID (com_decl);
17608           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17609           *slot = (void *) com_die;
17610         }
17611       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17612         {
17613           add_AT_location_description (com_die, DW_AT_location, loc);
17614           loc = loc_list_from_tree (com_decl, 2);
17615           remove_AT (com_die, DW_AT_declaration);
17616         }
17617       var_die = new_die (DW_TAG_variable, com_die, decl);
17618       add_name_and_src_coords_attributes (var_die, decl);
17619       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17620                           TREE_THIS_VOLATILE (decl), context_die);
17621       add_AT_flag (var_die, DW_AT_external, 1);
17622       if (loc)
17623         {
17624           if (off)
17625             {
17626               /* Optimize the common case.  */
17627               if (single_element_loc_list_p (loc)
17628                   && loc->expr->dw_loc_opc == DW_OP_addr
17629                   && loc->expr->dw_loc_next == NULL
17630                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17631                 {
17632                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17633                   loc->expr->dw_loc_oprnd1.v.val_addr
17634                     = plus_constant (GET_MODE (x), x, off);
17635                 }
17636               else
17637                 loc_list_plus_const (loc, off);
17638             }
17639           add_AT_location_description (var_die, DW_AT_location, loc);
17640         }
17641       else if (DECL_EXTERNAL (decl))
17642         add_AT_flag (var_die, DW_AT_declaration, 1);
17643       equate_decl_number_to_die (decl, var_die);
17644       return;
17645     }
17646
17647   /* If the compiler emitted a definition for the DECL declaration
17648      and if we already emitted a DIE for it, don't emit a second
17649      DIE for it again. Allow re-declarations of DECLs that are
17650      inside functions, though.  */
17651   if (old_die && declaration && !local_scope_p (context_die))
17652     return;
17653
17654   /* For static data members, the declaration in the class is supposed
17655      to have DW_TAG_member tag; the specification should still be
17656      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17657   if (declaration && class_scope_p (context_die))
17658     var_die = new_die (DW_TAG_member, context_die, decl);
17659   else
17660     var_die = new_die (DW_TAG_variable, context_die, decl);
17661
17662   origin_die = NULL;
17663   if (origin != NULL)
17664     origin_die = add_abstract_origin_attribute (var_die, origin);
17665
17666   /* Loop unrolling can create multiple blocks that refer to the same
17667      static variable, so we must test for the DW_AT_declaration flag.
17668
17669      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17670      copy decls and set the DECL_ABSTRACT flag on them instead of
17671      sharing them.
17672
17673      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17674
17675      ??? The declare_in_namespace support causes us to get two DIEs for one
17676      variable, both of which are declarations.  We want to avoid considering
17677      one to be a specification, so we must test that this DIE is not a
17678      declaration.  */
17679   else if (old_die && TREE_STATIC (decl) && ! declaration
17680            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17681     {
17682       /* This is a definition of a C++ class level static.  */
17683       add_AT_specification (var_die, old_die);
17684       specialization_p = true;
17685       if (DECL_NAME (decl))
17686         {
17687           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17688           struct dwarf_file_data * file_index = lookup_filename (s.file);
17689
17690           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17691             add_AT_file (var_die, DW_AT_decl_file, file_index);
17692
17693           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17694             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17695
17696           if (old_die->die_tag == DW_TAG_member)
17697             add_linkage_name (var_die, decl);
17698         }
17699     }
17700   else
17701     add_name_and_src_coords_attributes (var_die, decl);
17702
17703   if ((origin == NULL && !specialization_p)
17704       || (origin != NULL
17705           && !DECL_ABSTRACT (decl_or_origin)
17706           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17707                                        decl_function_context
17708                                                         (decl_or_origin))))
17709     {
17710       tree type = TREE_TYPE (decl_or_origin);
17711
17712       if (decl_by_reference_p (decl_or_origin))
17713         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17714       else
17715         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17716                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17717     }
17718
17719   if (origin == NULL && !specialization_p)
17720     {
17721       if (TREE_PUBLIC (decl))
17722         add_AT_flag (var_die, DW_AT_external, 1);
17723
17724       if (DECL_ARTIFICIAL (decl))
17725         add_AT_flag (var_die, DW_AT_artificial, 1);
17726
17727       add_accessibility_attribute (var_die, decl);
17728     }
17729
17730   if (declaration)
17731     add_AT_flag (var_die, DW_AT_declaration, 1);
17732
17733   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17734     equate_decl_number_to_die (decl, var_die);
17735
17736   if (! declaration
17737       && (! DECL_ABSTRACT (decl_or_origin)
17738           /* Local static vars are shared between all clones/inlines,
17739              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17740              already set.  */
17741           || (TREE_CODE (decl_or_origin) == VAR_DECL
17742               && TREE_STATIC (decl_or_origin)
17743               && DECL_RTL_SET_P (decl_or_origin)))
17744       /* When abstract origin already has DW_AT_location attribute, no need
17745          to add it again.  */
17746       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17747     {
17748       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17749           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17750         defer_location (decl_or_origin, var_die);
17751       else
17752         add_location_or_const_value_attribute (var_die, decl_or_origin,
17753                                                decl == NULL, DW_AT_location);
17754       add_pubname (decl_or_origin, var_die);
17755     }
17756   else
17757     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17758 }
17759
17760 /* Generate a DIE to represent a named constant.  */
17761
17762 static void
17763 gen_const_die (tree decl, dw_die_ref context_die)
17764 {
17765   dw_die_ref const_die;
17766   tree type = TREE_TYPE (decl);
17767
17768   const_die = new_die (DW_TAG_constant, context_die, decl);
17769   add_name_and_src_coords_attributes (const_die, decl);
17770   add_type_attribute (const_die, type, 1, 0, context_die);
17771   if (TREE_PUBLIC (decl))
17772     add_AT_flag (const_die, DW_AT_external, 1);
17773   if (DECL_ARTIFICIAL (decl))
17774     add_AT_flag (const_die, DW_AT_artificial, 1);
17775   tree_add_const_value_attribute_for_decl (const_die, decl);
17776 }
17777
17778 /* Generate a DIE to represent a label identifier.  */
17779
17780 static void
17781 gen_label_die (tree decl, dw_die_ref context_die)
17782 {
17783   tree origin = decl_ultimate_origin (decl);
17784   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17785   rtx insn;
17786   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17787
17788   if (origin != NULL)
17789     add_abstract_origin_attribute (lbl_die, origin);
17790   else
17791     add_name_and_src_coords_attributes (lbl_die, decl);
17792
17793   if (DECL_ABSTRACT (decl))
17794     equate_decl_number_to_die (decl, lbl_die);
17795   else
17796     {
17797       insn = DECL_RTL_IF_SET (decl);
17798
17799       /* Deleted labels are programmer specified labels which have been
17800          eliminated because of various optimizations.  We still emit them
17801          here so that it is possible to put breakpoints on them.  */
17802       if (insn
17803           && (LABEL_P (insn)
17804               || ((NOTE_P (insn)
17805                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17806         {
17807           /* When optimization is enabled (via -O) some parts of the compiler
17808              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17809              represent source-level labels which were explicitly declared by
17810              the user.  This really shouldn't be happening though, so catch
17811              it if it ever does happen.  */
17812           gcc_assert (!INSN_DELETED_P (insn));
17813
17814           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17815           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17816         }
17817       else if (insn
17818                && NOTE_P (insn)
17819                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17820                && CODE_LABEL_NUMBER (insn) != -1)
17821         {
17822           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17823           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17824         }
17825     }
17826 }
17827
17828 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17829    attributes to the DIE for a block STMT, to describe where the inlined
17830    function was called from.  This is similar to add_src_coords_attributes.  */
17831
17832 static inline void
17833 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17834 {
17835   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17836
17837   if (dwarf_version >= 3 || !dwarf_strict)
17838     {
17839       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17840       add_AT_unsigned (die, DW_AT_call_line, s.line);
17841     }
17842 }
17843
17844
17845 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17846    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17847
17848 static inline void
17849 add_high_low_attributes (tree stmt, dw_die_ref die)
17850 {
17851   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17852
17853   if (BLOCK_FRAGMENT_CHAIN (stmt)
17854       && (dwarf_version >= 3 || !dwarf_strict))
17855     {
17856       tree chain, superblock = NULL_TREE;
17857       dw_die_ref pdie;
17858       dw_attr_ref attr = NULL;
17859
17860       if (inlined_function_outer_scope_p (stmt))
17861         {
17862           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17863                                        BLOCK_NUMBER (stmt));
17864           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17865         }
17866
17867       /* Optimize duplicate .debug_ranges lists or even tails of
17868          lists.  If this BLOCK has same ranges as its supercontext,
17869          lookup DW_AT_ranges attribute in the supercontext (and
17870          recursively so), verify that the ranges_table contains the
17871          right values and use it instead of adding a new .debug_range.  */
17872       for (chain = stmt, pdie = die;
17873            BLOCK_SAME_RANGE (chain);
17874            chain = BLOCK_SUPERCONTEXT (chain))
17875         {
17876           dw_attr_ref new_attr;
17877
17878           pdie = pdie->die_parent;
17879           if (pdie == NULL)
17880             break;
17881           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17882             break;
17883           new_attr = get_AT (pdie, DW_AT_ranges);
17884           if (new_attr == NULL
17885               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17886             break;
17887           attr = new_attr;
17888           superblock = BLOCK_SUPERCONTEXT (chain);
17889         }
17890       if (attr != NULL
17891           && (ranges_table[attr->dw_attr_val.v.val_offset
17892                            / 2 / DWARF2_ADDR_SIZE].num
17893               == BLOCK_NUMBER (superblock))
17894           && BLOCK_FRAGMENT_CHAIN (superblock))
17895         {
17896           unsigned long off = attr->dw_attr_val.v.val_offset
17897                               / 2 / DWARF2_ADDR_SIZE;
17898           unsigned long supercnt = 0, thiscnt = 0;
17899           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17900                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17901             {
17902               ++supercnt;
17903               gcc_checking_assert (ranges_table[off + supercnt].num
17904                                    == BLOCK_NUMBER (chain));
17905             }
17906           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17907           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17908                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17909             ++thiscnt;
17910           gcc_assert (supercnt >= thiscnt);
17911           add_AT_range_list (die, DW_AT_ranges,
17912                              (off + supercnt - thiscnt)
17913                              * 2 * DWARF2_ADDR_SIZE);
17914           return;
17915         }
17916
17917       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17918
17919       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17920       do
17921         {
17922           add_ranges (chain);
17923           chain = BLOCK_FRAGMENT_CHAIN (chain);
17924         }
17925       while (chain);
17926       add_ranges (NULL);
17927     }
17928   else
17929     {
17930       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
17931       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17932                                    BLOCK_NUMBER (stmt));
17933       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
17934                                    BLOCK_NUMBER (stmt));
17935       add_AT_low_high_pc (die, label, label_high);
17936     }
17937 }
17938
17939 /* Generate a DIE for a lexical block.  */
17940
17941 static void
17942 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17943 {
17944   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17945
17946   if (call_arg_locations)
17947     {
17948       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17949         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17950                                BLOCK_NUMBER (stmt) + 1);
17951       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17952     }
17953
17954   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17955     add_high_low_attributes (stmt, stmt_die);
17956
17957   decls_for_scope (stmt, stmt_die, depth);
17958 }
17959
17960 /* Generate a DIE for an inlined subprogram.  */
17961
17962 static void
17963 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17964 {
17965   tree decl;
17966
17967   /* The instance of function that is effectively being inlined shall not
17968      be abstract.  */
17969   gcc_assert (! BLOCK_ABSTRACT (stmt));
17970
17971   decl = block_ultimate_origin (stmt);
17972
17973   /* Emit info for the abstract instance first, if we haven't yet.  We
17974      must emit this even if the block is abstract, otherwise when we
17975      emit the block below (or elsewhere), we may end up trying to emit
17976      a die whose origin die hasn't been emitted, and crashing.  */
17977   dwarf2out_abstract_function (decl);
17978
17979   if (! BLOCK_ABSTRACT (stmt))
17980     {
17981       dw_die_ref subr_die
17982         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17983
17984       if (call_arg_locations)
17985         {
17986           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17987             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17988                                    BLOCK_NUMBER (stmt) + 1);
17989           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17990         }
17991       add_abstract_origin_attribute (subr_die, decl);
17992       if (TREE_ASM_WRITTEN (stmt))
17993         add_high_low_attributes (stmt, subr_die);
17994       add_call_src_coords_attributes (stmt, subr_die);
17995
17996       decls_for_scope (stmt, subr_die, depth);
17997       current_function_has_inlines = 1;
17998     }
17999 }
18000
18001 /* Generate a DIE for a field in a record, or structure.  */
18002
18003 static void
18004 gen_field_die (tree decl, dw_die_ref context_die)
18005 {
18006   dw_die_ref decl_die;
18007
18008   if (TREE_TYPE (decl) == error_mark_node)
18009     return;
18010
18011   decl_die = new_die (DW_TAG_member, context_die, decl);
18012   add_name_and_src_coords_attributes (decl_die, decl);
18013   add_type_attribute (decl_die, member_declared_type (decl),
18014                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18015                       context_die);
18016
18017   if (DECL_BIT_FIELD_TYPE (decl))
18018     {
18019       add_byte_size_attribute (decl_die, decl);
18020       add_bit_size_attribute (decl_die, decl);
18021       add_bit_offset_attribute (decl_die, decl);
18022     }
18023
18024   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18025     add_data_member_location_attribute (decl_die, decl);
18026
18027   if (DECL_ARTIFICIAL (decl))
18028     add_AT_flag (decl_die, DW_AT_artificial, 1);
18029
18030   add_accessibility_attribute (decl_die, decl);
18031
18032   /* Equate decl number to die, so that we can look up this decl later on.  */
18033   equate_decl_number_to_die (decl, decl_die);
18034 }
18035
18036 #if 0
18037 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18038    Use modified_type_die instead.
18039    We keep this code here just in case these types of DIEs may be needed to
18040    represent certain things in other languages (e.g. Pascal) someday.  */
18041
18042 static void
18043 gen_pointer_type_die (tree type, dw_die_ref context_die)
18044 {
18045   dw_die_ref ptr_die
18046     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18047
18048   equate_type_number_to_die (type, ptr_die);
18049   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18050   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18051 }
18052
18053 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18054    Use modified_type_die instead.
18055    We keep this code here just in case these types of DIEs may be needed to
18056    represent certain things in other languages (e.g. Pascal) someday.  */
18057
18058 static void
18059 gen_reference_type_die (tree type, dw_die_ref context_die)
18060 {
18061   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18062
18063   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18064     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18065   else
18066     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18067
18068   equate_type_number_to_die (type, ref_die);
18069   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18070   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18071 }
18072 #endif
18073
18074 /* Generate a DIE for a pointer to a member type.  */
18075
18076 static void
18077 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18078 {
18079   dw_die_ref ptr_die
18080     = new_die (DW_TAG_ptr_to_member_type,
18081                scope_die_for (type, context_die), type);
18082
18083   equate_type_number_to_die (type, ptr_die);
18084   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18085                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18086   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18087 }
18088
18089 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18090 DEF_VEC_P(dchar_p);
18091 DEF_VEC_ALLOC_P(dchar_p,heap);
18092
18093 static char *producer_string;
18094
18095 /* Return a heap allocated producer string including command line options
18096    if -grecord-gcc-switches.  */
18097
18098 static char *
18099 gen_producer_string (void)
18100 {
18101   size_t j;
18102   VEC(dchar_p, heap) *switches = NULL;
18103   const char *language_string = lang_hooks.name;
18104   char *producer, *tail;
18105   const char *p;
18106   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18107   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18108
18109   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18110     switch (save_decoded_options[j].opt_index)
18111       {
18112       case OPT_o:
18113       case OPT_d:
18114       case OPT_dumpbase:
18115       case OPT_dumpdir:
18116       case OPT_auxbase:
18117       case OPT_auxbase_strip:
18118       case OPT_quiet:
18119       case OPT_version:
18120       case OPT_v:
18121       case OPT_w:
18122       case OPT_L:
18123       case OPT_D:
18124       case OPT_I:
18125       case OPT_U:
18126       case OPT_SPECIAL_unknown:
18127       case OPT_SPECIAL_ignore:
18128       case OPT_SPECIAL_program_name:
18129       case OPT_SPECIAL_input_file:
18130       case OPT_grecord_gcc_switches:
18131       case OPT_gno_record_gcc_switches:
18132       case OPT__output_pch_:
18133       case OPT_fdiagnostics_show_location_:
18134       case OPT_fdiagnostics_show_option:
18135       case OPT_fdiagnostics_show_caret:
18136       case OPT_fverbose_asm:
18137       case OPT____:
18138       case OPT__sysroot_:
18139       case OPT_nostdinc:
18140       case OPT_nostdinc__:
18141         /* Ignore these.  */
18142         continue;
18143       default:
18144         if (cl_options[save_decoded_options[j].opt_index].flags
18145             & CL_NO_DWARF_RECORD)
18146           continue;
18147         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18148                              == '-');
18149         switch (save_decoded_options[j].canonical_option[0][1])
18150           {
18151           case 'M':
18152           case 'i':
18153           case 'W':
18154             continue;
18155           case 'f':
18156             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18157                          "dump", 4) == 0)
18158               continue;
18159             break;
18160           default:
18161             break;
18162           }
18163         VEC_safe_push (dchar_p, heap, switches,
18164                        save_decoded_options[j].orig_option_with_args_text);
18165         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18166         break;
18167       }
18168
18169   producer = XNEWVEC (char, plen + 1 + len + 1);
18170   tail = producer;
18171   sprintf (tail, "%s %s", language_string, version_string);
18172   tail += plen;
18173
18174   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18175     {
18176       len = strlen (p);
18177       *tail = ' ';
18178       memcpy (tail + 1, p, len);
18179       tail += len + 1;
18180     }
18181
18182   *tail = '\0';
18183   VEC_free (dchar_p, heap, switches);
18184   return producer;
18185 }
18186
18187 /* Generate the DIE for the compilation unit.  */
18188
18189 static dw_die_ref
18190 gen_compile_unit_die (const char *filename)
18191 {
18192   dw_die_ref die;
18193   const char *language_string = lang_hooks.name;
18194   int language;
18195
18196   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18197
18198   if (filename)
18199     {
18200       add_name_attribute (die, filename);
18201       /* Don't add cwd for <built-in>.  */
18202       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18203         add_comp_dir_attribute (die);
18204     }
18205
18206   if (producer_string == NULL)
18207     producer_string = gen_producer_string ();
18208   add_AT_string (die, DW_AT_producer, producer_string);
18209
18210   /* If our producer is LTO try to figure out a common language to use
18211      from the global list of translation units.  */
18212   if (strcmp (language_string, "GNU GIMPLE") == 0)
18213     {
18214       unsigned i;
18215       tree t;
18216       const char *common_lang = NULL;
18217
18218       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18219         {
18220           if (!TRANSLATION_UNIT_LANGUAGE (t))
18221             continue;
18222           if (!common_lang)
18223             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18224           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18225             ;
18226           else if (strncmp (common_lang, "GNU C", 5) == 0
18227                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18228             /* Mixing C and C++ is ok, use C++ in that case.  */
18229             common_lang = "GNU C++";
18230           else
18231             {
18232               /* Fall back to C.  */
18233               common_lang = NULL;
18234               break;
18235             }
18236         }
18237
18238       if (common_lang)
18239         language_string = common_lang;
18240     }
18241
18242   language = DW_LANG_C89;
18243   if (strcmp (language_string, "GNU C++") == 0)
18244     language = DW_LANG_C_plus_plus;
18245   else if (strcmp (language_string, "GNU F77") == 0)
18246     language = DW_LANG_Fortran77;
18247   else if (strcmp (language_string, "GNU Pascal") == 0)
18248     language = DW_LANG_Pascal83;
18249   else if (dwarf_version >= 3 || !dwarf_strict)
18250     {
18251       if (strcmp (language_string, "GNU Ada") == 0)
18252         language = DW_LANG_Ada95;
18253       else if (strcmp (language_string, "GNU Fortran") == 0)
18254         language = DW_LANG_Fortran95;
18255       else if (strcmp (language_string, "GNU Java") == 0)
18256         language = DW_LANG_Java;
18257       else if (strcmp (language_string, "GNU Objective-C") == 0)
18258         language = DW_LANG_ObjC;
18259       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18260         language = DW_LANG_ObjC_plus_plus;
18261       else if (dwarf_version >= 5 || !dwarf_strict)
18262         {
18263           if (strcmp (language_string, "GNU Go") == 0)
18264             language = DW_LANG_Go;
18265         }
18266     }
18267   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
18268   else if (strcmp (language_string, "GNU Fortran") == 0)
18269     language = DW_LANG_Fortran90;
18270
18271   add_AT_unsigned (die, DW_AT_language, language);
18272
18273   switch (language)
18274     {
18275     case DW_LANG_Fortran77:
18276     case DW_LANG_Fortran90:
18277     case DW_LANG_Fortran95:
18278       /* Fortran has case insensitive identifiers and the front-end
18279          lowercases everything.  */
18280       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18281       break;
18282     default:
18283       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18284       break;
18285     }
18286   return die;
18287 }
18288
18289 /* Generate the DIE for a base class.  */
18290
18291 static void
18292 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18293 {
18294   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18295
18296   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18297   add_data_member_location_attribute (die, binfo);
18298
18299   if (BINFO_VIRTUAL_P (binfo))
18300     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18301
18302   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18303      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18304      the default has always been DW_ACCESS_private.  */
18305   if (access == access_public_node)
18306     {
18307       if (dwarf_version == 2
18308           || context_die->die_tag == DW_TAG_class_type)
18309       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18310     }
18311   else if (access == access_protected_node)
18312     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18313   else if (dwarf_version > 2
18314            && context_die->die_tag != DW_TAG_class_type)
18315     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18316 }
18317
18318 /* Generate a DIE for a class member.  */
18319
18320 static void
18321 gen_member_die (tree type, dw_die_ref context_die)
18322 {
18323   tree member;
18324   tree binfo = TYPE_BINFO (type);
18325   dw_die_ref child;
18326
18327   /* If this is not an incomplete type, output descriptions of each of its
18328      members. Note that as we output the DIEs necessary to represent the
18329      members of this record or union type, we will also be trying to output
18330      DIEs to represent the *types* of those members. However the `type'
18331      function (above) will specifically avoid generating type DIEs for member
18332      types *within* the list of member DIEs for this (containing) type except
18333      for those types (of members) which are explicitly marked as also being
18334      members of this (containing) type themselves.  The g++ front- end can
18335      force any given type to be treated as a member of some other (containing)
18336      type by setting the TYPE_CONTEXT of the given (member) type to point to
18337      the TREE node representing the appropriate (containing) type.  */
18338
18339   /* First output info about the base classes.  */
18340   if (binfo)
18341     {
18342       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18343       int i;
18344       tree base;
18345
18346       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18347         gen_inheritance_die (base,
18348                              (accesses ? VEC_index (tree, accesses, i)
18349                               : access_public_node), context_die);
18350     }
18351
18352   /* Now output info about the data members and type members.  */
18353   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18354     {
18355       /* If we thought we were generating minimal debug info for TYPE
18356          and then changed our minds, some of the member declarations
18357          may have already been defined.  Don't define them again, but
18358          do put them in the right order.  */
18359
18360       child = lookup_decl_die (member);
18361       if (child)
18362         splice_child_die (context_die, child);
18363       else
18364         gen_decl_die (member, NULL, context_die);
18365     }
18366
18367   /* Now output info about the function members (if any).  */
18368   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18369     {
18370       /* Don't include clones in the member list.  */
18371       if (DECL_ABSTRACT_ORIGIN (member))
18372         continue;
18373
18374       child = lookup_decl_die (member);
18375       if (child)
18376         splice_child_die (context_die, child);
18377       else
18378         gen_decl_die (member, NULL, context_die);
18379     }
18380 }
18381
18382 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18383    is set, we pretend that the type was never defined, so we only get the
18384    member DIEs needed by later specification DIEs.  */
18385
18386 static void
18387 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18388                                 enum debug_info_usage usage)
18389 {
18390   dw_die_ref type_die = lookup_type_die (type);
18391   dw_die_ref scope_die = 0;
18392   int nested = 0;
18393   int complete = (TYPE_SIZE (type)
18394                   && (! TYPE_STUB_DECL (type)
18395                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18396   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18397   complete = complete && should_emit_struct_debug (type, usage);
18398
18399   if (type_die && ! complete)
18400     return;
18401
18402   if (TYPE_CONTEXT (type) != NULL_TREE
18403       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18404           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18405     nested = 1;
18406
18407   scope_die = scope_die_for (type, context_die);
18408
18409   if (! type_die || (nested && is_cu_die (scope_die)))
18410     /* First occurrence of type or toplevel definition of nested class.  */
18411     {
18412       dw_die_ref old_die = type_die;
18413
18414       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18415                           ? record_type_tag (type) : DW_TAG_union_type,
18416                           scope_die, type);
18417       equate_type_number_to_die (type, type_die);
18418       if (old_die)
18419         add_AT_specification (type_die, old_die);
18420       else
18421         add_name_attribute (type_die, type_tag (type));
18422     }
18423   else
18424     remove_AT (type_die, DW_AT_declaration);
18425
18426   /* Generate child dies for template paramaters.  */
18427   if (debug_info_level > DINFO_LEVEL_TERSE
18428       && COMPLETE_TYPE_P (type))
18429     schedule_generic_params_dies_gen (type);
18430
18431   /* If this type has been completed, then give it a byte_size attribute and
18432      then give a list of members.  */
18433   if (complete && !ns_decl)
18434     {
18435       /* Prevent infinite recursion in cases where the type of some member of
18436          this type is expressed in terms of this type itself.  */
18437       TREE_ASM_WRITTEN (type) = 1;
18438       add_byte_size_attribute (type_die, type);
18439       if (TYPE_STUB_DECL (type) != NULL_TREE)
18440         {
18441           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18442           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18443         }
18444
18445       /* If the first reference to this type was as the return type of an
18446          inline function, then it may not have a parent.  Fix this now.  */
18447       if (type_die->die_parent == NULL)
18448         add_child_die (scope_die, type_die);
18449
18450       push_decl_scope (type);
18451       gen_member_die (type, type_die);
18452       pop_decl_scope ();
18453
18454       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18455       if (TYPE_ARTIFICIAL (type))
18456         add_AT_flag (type_die, DW_AT_artificial, 1);
18457
18458       /* GNU extension: Record what type our vtable lives in.  */
18459       if (TYPE_VFIELD (type))
18460         {
18461           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18462
18463           gen_type_die (vtype, context_die);
18464           add_AT_die_ref (type_die, DW_AT_containing_type,
18465                           lookup_type_die (vtype));
18466         }
18467     }
18468   else
18469     {
18470       add_AT_flag (type_die, DW_AT_declaration, 1);
18471
18472       /* We don't need to do this for function-local types.  */
18473       if (TYPE_STUB_DECL (type)
18474           && ! decl_function_context (TYPE_STUB_DECL (type)))
18475         VEC_safe_push (tree, gc, incomplete_types, type);
18476     }
18477
18478   if (get_AT (type_die, DW_AT_name))
18479     add_pubtype (type, type_die);
18480 }
18481
18482 /* Generate a DIE for a subroutine _type_.  */
18483
18484 static void
18485 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18486 {
18487   tree return_type = TREE_TYPE (type);
18488   dw_die_ref subr_die
18489     = new_die (DW_TAG_subroutine_type,
18490                scope_die_for (type, context_die), type);
18491
18492   equate_type_number_to_die (type, subr_die);
18493   add_prototyped_attribute (subr_die, type);
18494   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18495   gen_formal_types_die (type, subr_die);
18496
18497   if (get_AT (subr_die, DW_AT_name))
18498     add_pubtype (type, subr_die);
18499 }
18500
18501 /* Generate a DIE for a type definition.  */
18502
18503 static void
18504 gen_typedef_die (tree decl, dw_die_ref context_die)
18505 {
18506   dw_die_ref type_die;
18507   tree origin;
18508
18509   if (TREE_ASM_WRITTEN (decl))
18510     return;
18511
18512   TREE_ASM_WRITTEN (decl) = 1;
18513   type_die = new_die (DW_TAG_typedef, context_die, decl);
18514   origin = decl_ultimate_origin (decl);
18515   if (origin != NULL)
18516     add_abstract_origin_attribute (type_die, origin);
18517   else
18518     {
18519       tree type;
18520
18521       add_name_and_src_coords_attributes (type_die, decl);
18522       if (DECL_ORIGINAL_TYPE (decl))
18523         {
18524           type = DECL_ORIGINAL_TYPE (decl);
18525
18526           gcc_assert (type != TREE_TYPE (decl));
18527           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18528         }
18529       else
18530         {
18531           type = TREE_TYPE (decl);
18532
18533           if (is_naming_typedef_decl (TYPE_NAME (type)))
18534             {
18535               /* Here, we are in the case of decl being a typedef naming
18536                  an anonymous type, e.g:
18537                      typedef struct {...} foo;
18538                  In that case TREE_TYPE (decl) is not a typedef variant
18539                  type and TYPE_NAME of the anonymous type is set to the
18540                  TYPE_DECL of the typedef. This construct is emitted by
18541                  the C++ FE.
18542
18543                  TYPE is the anonymous struct named by the typedef
18544                  DECL. As we need the DW_AT_type attribute of the
18545                  DW_TAG_typedef to point to the DIE of TYPE, let's
18546                  generate that DIE right away. add_type_attribute
18547                  called below will then pick (via lookup_type_die) that
18548                  anonymous struct DIE.  */
18549               if (!TREE_ASM_WRITTEN (type))
18550                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18551
18552               /* This is a GNU Extension.  We are adding a
18553                  DW_AT_linkage_name attribute to the DIE of the
18554                  anonymous struct TYPE.  The value of that attribute
18555                  is the name of the typedef decl naming the anonymous
18556                  struct.  This greatly eases the work of consumers of
18557                  this debug info.  */
18558               add_linkage_attr (lookup_type_die (type), decl);
18559             }
18560         }
18561
18562       add_type_attribute (type_die, type, TREE_READONLY (decl),
18563                           TREE_THIS_VOLATILE (decl), context_die);
18564
18565       if (is_naming_typedef_decl (decl))
18566         /* We want that all subsequent calls to lookup_type_die with
18567            TYPE in argument yield the DW_TAG_typedef we have just
18568            created.  */
18569         equate_type_number_to_die (type, type_die);
18570
18571       add_accessibility_attribute (type_die, decl);
18572     }
18573
18574   if (DECL_ABSTRACT (decl))
18575     equate_decl_number_to_die (decl, type_die);
18576
18577   if (get_AT (type_die, DW_AT_name))
18578     add_pubtype (decl, type_die);
18579 }
18580
18581 /* Generate a DIE for a struct, class, enum or union type.  */
18582
18583 static void
18584 gen_tagged_type_die (tree type,
18585                      dw_die_ref context_die,
18586                      enum debug_info_usage usage)
18587 {
18588   int need_pop;
18589
18590   if (type == NULL_TREE
18591       || !is_tagged_type (type))
18592     return;
18593
18594   /* If this is a nested type whose containing class hasn't been written
18595      out yet, writing it out will cover this one, too.  This does not apply
18596      to instantiations of member class templates; they need to be added to
18597      the containing class as they are generated.  FIXME: This hurts the
18598      idea of combining type decls from multiple TUs, since we can't predict
18599      what set of template instantiations we'll get.  */
18600   if (TYPE_CONTEXT (type)
18601       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18602       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18603     {
18604       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18605
18606       if (TREE_ASM_WRITTEN (type))
18607         return;
18608
18609       /* If that failed, attach ourselves to the stub.  */
18610       push_decl_scope (TYPE_CONTEXT (type));
18611       context_die = lookup_type_die (TYPE_CONTEXT (type));
18612       need_pop = 1;
18613     }
18614   else if (TYPE_CONTEXT (type) != NULL_TREE
18615            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18616     {
18617       /* If this type is local to a function that hasn't been written
18618          out yet, use a NULL context for now; it will be fixed up in
18619          decls_for_scope.  */
18620       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18621       /* A declaration DIE doesn't count; nested types need to go in the
18622          specification.  */
18623       if (context_die && is_declaration_die (context_die))
18624         context_die = NULL;
18625       need_pop = 0;
18626     }
18627   else
18628     {
18629       context_die = declare_in_namespace (type, context_die);
18630       need_pop = 0;
18631     }
18632
18633   if (TREE_CODE (type) == ENUMERAL_TYPE)
18634     {
18635       /* This might have been written out by the call to
18636          declare_in_namespace.  */
18637       if (!TREE_ASM_WRITTEN (type))
18638         gen_enumeration_type_die (type, context_die);
18639     }
18640   else
18641     gen_struct_or_union_type_die (type, context_die, usage);
18642
18643   if (need_pop)
18644     pop_decl_scope ();
18645
18646   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18647      it up if it is ever completed.  gen_*_type_die will set it for us
18648      when appropriate.  */
18649 }
18650
18651 /* Generate a type description DIE.  */
18652
18653 static void
18654 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18655                          enum debug_info_usage usage)
18656 {
18657   struct array_descr_info info;
18658
18659   if (type == NULL_TREE || type == error_mark_node)
18660     return;
18661
18662   if (TYPE_NAME (type) != NULL_TREE
18663       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18664       && is_redundant_typedef (TYPE_NAME (type))
18665       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18666     /* The DECL of this type is a typedef we don't want to emit debug
18667        info for but we want debug info for its underlying typedef.
18668        This can happen for e.g, the injected-class-name of a C++
18669        type.  */
18670     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18671
18672   /* If TYPE is a typedef type variant, let's generate debug info
18673      for the parent typedef which TYPE is a type of.  */
18674   if (typedef_variant_p (type))
18675     {
18676       if (TREE_ASM_WRITTEN (type))
18677         return;
18678
18679       /* Prevent broken recursion; we can't hand off to the same type.  */
18680       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18681
18682       /* Give typedefs the right scope.  */
18683       context_die = scope_die_for (type, context_die);
18684
18685       TREE_ASM_WRITTEN (type) = 1;
18686
18687       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18688       return;
18689     }
18690
18691   /* If type is an anonymous tagged type named by a typedef, let's
18692      generate debug info for the typedef.  */
18693   if (is_naming_typedef_decl (TYPE_NAME (type)))
18694     {
18695       /* Use the DIE of the containing namespace as the parent DIE of
18696          the type description DIE we want to generate.  */
18697       if (DECL_CONTEXT (TYPE_NAME (type))
18698           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18699         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18700       
18701       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18702       return;
18703     }
18704
18705   /* If this is an array type with hidden descriptor, handle it first.  */
18706   if (!TREE_ASM_WRITTEN (type)
18707       && lang_hooks.types.get_array_descr_info
18708       && lang_hooks.types.get_array_descr_info (type, &info)
18709       && (dwarf_version >= 3 || !dwarf_strict))
18710     {
18711       gen_descr_array_type_die (type, &info, context_die);
18712       TREE_ASM_WRITTEN (type) = 1;
18713       return;
18714     }
18715
18716   /* We are going to output a DIE to represent the unqualified version
18717      of this type (i.e. without any const or volatile qualifiers) so
18718      get the main variant (i.e. the unqualified version) of this type
18719      now.  (Vectors are special because the debugging info is in the
18720      cloned type itself).  */
18721   if (TREE_CODE (type) != VECTOR_TYPE)
18722     type = type_main_variant (type);
18723
18724   if (TREE_ASM_WRITTEN (type))
18725     return;
18726
18727   switch (TREE_CODE (type))
18728     {
18729     case ERROR_MARK:
18730       break;
18731
18732     case POINTER_TYPE:
18733     case REFERENCE_TYPE:
18734       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18735          ensures that the gen_type_die recursion will terminate even if the
18736          type is recursive.  Recursive types are possible in Ada.  */
18737       /* ??? We could perhaps do this for all types before the switch
18738          statement.  */
18739       TREE_ASM_WRITTEN (type) = 1;
18740
18741       /* For these types, all that is required is that we output a DIE (or a
18742          set of DIEs) to represent the "basis" type.  */
18743       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18744                                 DINFO_USAGE_IND_USE);
18745       break;
18746
18747     case OFFSET_TYPE:
18748       /* This code is used for C++ pointer-to-data-member types.
18749          Output a description of the relevant class type.  */
18750       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18751                                         DINFO_USAGE_IND_USE);
18752
18753       /* Output a description of the type of the object pointed to.  */
18754       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18755                                         DINFO_USAGE_IND_USE);
18756
18757       /* Now output a DIE to represent this pointer-to-data-member type
18758          itself.  */
18759       gen_ptr_to_mbr_type_die (type, context_die);
18760       break;
18761
18762     case FUNCTION_TYPE:
18763       /* Force out return type (in case it wasn't forced out already).  */
18764       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18765                                         DINFO_USAGE_DIR_USE);
18766       gen_subroutine_type_die (type, context_die);
18767       break;
18768
18769     case METHOD_TYPE:
18770       /* Force out return type (in case it wasn't forced out already).  */
18771       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18772                                         DINFO_USAGE_DIR_USE);
18773       gen_subroutine_type_die (type, context_die);
18774       break;
18775
18776     case ARRAY_TYPE:
18777       gen_array_type_die (type, context_die);
18778       break;
18779
18780     case VECTOR_TYPE:
18781       gen_array_type_die (type, context_die);
18782       break;
18783
18784     case ENUMERAL_TYPE:
18785     case RECORD_TYPE:
18786     case UNION_TYPE:
18787     case QUAL_UNION_TYPE:
18788       gen_tagged_type_die (type, context_die, usage);
18789       return;
18790
18791     case VOID_TYPE:
18792     case INTEGER_TYPE:
18793     case REAL_TYPE:
18794     case FIXED_POINT_TYPE:
18795     case COMPLEX_TYPE:
18796     case BOOLEAN_TYPE:
18797       /* No DIEs needed for fundamental types.  */
18798       break;
18799
18800     case NULLPTR_TYPE:
18801     case LANG_TYPE:
18802       /* Just use DW_TAG_unspecified_type.  */
18803       {
18804         dw_die_ref type_die = lookup_type_die (type);
18805         if (type_die == NULL)
18806           {
18807             tree name = TYPE_NAME (type);
18808             if (TREE_CODE (name) == TYPE_DECL)
18809               name = DECL_NAME (name);
18810             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18811             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18812             equate_type_number_to_die (type, type_die);
18813           }
18814       }
18815       break;
18816
18817     default:
18818       gcc_unreachable ();
18819     }
18820
18821   TREE_ASM_WRITTEN (type) = 1;
18822 }
18823
18824 static void
18825 gen_type_die (tree type, dw_die_ref context_die)
18826 {
18827   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18828 }
18829
18830 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18831    things which are local to the given block.  */
18832
18833 static void
18834 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18835 {
18836   int must_output_die = 0;
18837   bool inlined_func;
18838
18839   /* Ignore blocks that are NULL.  */
18840   if (stmt == NULL_TREE)
18841     return;
18842
18843   inlined_func = inlined_function_outer_scope_p (stmt);
18844
18845   /* If the block is one fragment of a non-contiguous block, do not
18846      process the variables, since they will have been done by the
18847      origin block.  Do process subblocks.  */
18848   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18849     {
18850       tree sub;
18851
18852       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18853         gen_block_die (sub, context_die, depth + 1);
18854
18855       return;
18856     }
18857
18858   /* Determine if we need to output any Dwarf DIEs at all to represent this
18859      block.  */
18860   if (inlined_func)
18861     /* The outer scopes for inlinings *must* always be represented.  We
18862        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18863     must_output_die = 1;
18864   else
18865     {
18866       /* Determine if this block directly contains any "significant"
18867          local declarations which we will need to output DIEs for.  */
18868       if (debug_info_level > DINFO_LEVEL_TERSE)
18869         /* We are not in terse mode so *any* local declaration counts
18870            as being a "significant" one.  */
18871         must_output_die = ((BLOCK_VARS (stmt) != NULL
18872                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18873                            && (TREE_USED (stmt)
18874                                || TREE_ASM_WRITTEN (stmt)
18875                                || BLOCK_ABSTRACT (stmt)));
18876       else if ((TREE_USED (stmt)
18877                 || TREE_ASM_WRITTEN (stmt)
18878                 || BLOCK_ABSTRACT (stmt))
18879                && !dwarf2out_ignore_block (stmt))
18880         must_output_die = 1;
18881     }
18882
18883   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18884      DIE for any block which contains no significant local declarations at
18885      all.  Rather, in such cases we just call `decls_for_scope' so that any
18886      needed Dwarf info for any sub-blocks will get properly generated. Note
18887      that in terse mode, our definition of what constitutes a "significant"
18888      local declaration gets restricted to include only inlined function
18889      instances and local (nested) function definitions.  */
18890   if (must_output_die)
18891     {
18892       if (inlined_func)
18893         {
18894           /* If STMT block is abstract, that means we have been called
18895              indirectly from dwarf2out_abstract_function.
18896              That function rightfully marks the descendent blocks (of
18897              the abstract function it is dealing with) as being abstract,
18898              precisely to prevent us from emitting any
18899              DW_TAG_inlined_subroutine DIE as a descendent
18900              of an abstract function instance. So in that case, we should
18901              not call gen_inlined_subroutine_die.
18902
18903              Later though, when cgraph asks dwarf2out to emit info
18904              for the concrete instance of the function decl into which
18905              the concrete instance of STMT got inlined, the later will lead
18906              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18907           if (! BLOCK_ABSTRACT (stmt))
18908             gen_inlined_subroutine_die (stmt, context_die, depth);
18909         }
18910       else
18911         gen_lexical_block_die (stmt, context_die, depth);
18912     }
18913   else
18914     decls_for_scope (stmt, context_die, depth);
18915 }
18916
18917 /* Process variable DECL (or variable with origin ORIGIN) within
18918    block STMT and add it to CONTEXT_DIE.  */
18919 static void
18920 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18921 {
18922   dw_die_ref die;
18923   tree decl_or_origin = decl ? decl : origin;
18924
18925   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18926     die = lookup_decl_die (decl_or_origin);
18927   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18928            && TYPE_DECL_IS_STUB (decl_or_origin))
18929     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18930   else
18931     die = NULL;
18932
18933   if (die != NULL && die->die_parent == NULL)
18934     add_child_die (context_die, die);
18935   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18936     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18937                                          stmt, context_die);
18938   else
18939     gen_decl_die (decl, origin, context_die);
18940 }
18941
18942 /* Generate all of the decls declared within a given scope and (recursively)
18943    all of its sub-blocks.  */
18944
18945 static void
18946 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18947 {
18948   tree decl;
18949   unsigned int i;
18950   tree subblocks;
18951
18952   /* Ignore NULL blocks.  */
18953   if (stmt == NULL_TREE)
18954     return;
18955
18956   /* Output the DIEs to represent all of the data objects and typedefs
18957      declared directly within this block but not within any nested
18958      sub-blocks.  Also, nested function and tag DIEs have been
18959      generated with a parent of NULL; fix that up now.  */
18960   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18961     process_scope_var (stmt, decl, NULL_TREE, context_die);
18962   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18963     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18964                        context_die);
18965
18966   /* If we're at -g1, we're not interested in subblocks.  */
18967   if (debug_info_level <= DINFO_LEVEL_TERSE)
18968     return;
18969
18970   /* Output the DIEs to represent all sub-blocks (and the items declared
18971      therein) of this block.  */
18972   for (subblocks = BLOCK_SUBBLOCKS (stmt);
18973        subblocks != NULL;
18974        subblocks = BLOCK_CHAIN (subblocks))
18975     gen_block_die (subblocks, context_die, depth + 1);
18976 }
18977
18978 /* Is this a typedef we can avoid emitting?  */
18979
18980 static inline int
18981 is_redundant_typedef (const_tree decl)
18982 {
18983   if (TYPE_DECL_IS_STUB (decl))
18984     return 1;
18985
18986   if (DECL_ARTIFICIAL (decl)
18987       && DECL_CONTEXT (decl)
18988       && is_tagged_type (DECL_CONTEXT (decl))
18989       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18990       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18991     /* Also ignore the artificial member typedef for the class name.  */
18992     return 1;
18993
18994   return 0;
18995 }
18996
18997 /* Return TRUE if TYPE is a typedef that names a type for linkage
18998    purposes. This kind of typedefs is produced by the C++ FE for
18999    constructs like:
19000
19001    typedef struct {...} foo;
19002
19003    In that case, there is no typedef variant type produced for foo.
19004    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19005    struct type.  */
19006
19007 static bool
19008 is_naming_typedef_decl (const_tree decl)
19009 {
19010   if (decl == NULL_TREE
19011       || TREE_CODE (decl) != TYPE_DECL
19012       || !is_tagged_type (TREE_TYPE (decl))
19013       || DECL_IS_BUILTIN (decl)
19014       || is_redundant_typedef (decl)
19015       /* It looks like Ada produces TYPE_DECLs that are very similar
19016          to C++ naming typedefs but that have different
19017          semantics. Let's be specific to c++ for now.  */
19018       || !is_cxx ())
19019     return FALSE;
19020
19021   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19022           && TYPE_NAME (TREE_TYPE (decl)) == decl
19023           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19024               != TYPE_NAME (TREE_TYPE (decl))));
19025 }
19026
19027 /* Returns the DIE for a context.  */
19028
19029 static inline dw_die_ref
19030 get_context_die (tree context)
19031 {
19032   if (context)
19033     {
19034       /* Find die that represents this context.  */
19035       if (TYPE_P (context))
19036         {
19037           context = TYPE_MAIN_VARIANT (context);
19038           return strip_naming_typedef (context, force_type_die (context));
19039         }
19040       else
19041         return force_decl_die (context);
19042     }
19043   return comp_unit_die ();
19044 }
19045
19046 /* Returns the DIE for decl.  A DIE will always be returned.  */
19047
19048 static dw_die_ref
19049 force_decl_die (tree decl)
19050 {
19051   dw_die_ref decl_die;
19052   unsigned saved_external_flag;
19053   tree save_fn = NULL_TREE;
19054   decl_die = lookup_decl_die (decl);
19055   if (!decl_die)
19056     {
19057       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19058
19059       decl_die = lookup_decl_die (decl);
19060       if (decl_die)
19061         return decl_die;
19062
19063       switch (TREE_CODE (decl))
19064         {
19065         case FUNCTION_DECL:
19066           /* Clear current_function_decl, so that gen_subprogram_die thinks
19067              that this is a declaration. At this point, we just want to force
19068              declaration die.  */
19069           save_fn = current_function_decl;
19070           current_function_decl = NULL_TREE;
19071           gen_subprogram_die (decl, context_die);
19072           current_function_decl = save_fn;
19073           break;
19074
19075         case VAR_DECL:
19076           /* Set external flag to force declaration die. Restore it after
19077            gen_decl_die() call.  */
19078           saved_external_flag = DECL_EXTERNAL (decl);
19079           DECL_EXTERNAL (decl) = 1;
19080           gen_decl_die (decl, NULL, context_die);
19081           DECL_EXTERNAL (decl) = saved_external_flag;
19082           break;
19083
19084         case NAMESPACE_DECL:
19085           if (dwarf_version >= 3 || !dwarf_strict)
19086             dwarf2out_decl (decl);
19087           else
19088             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19089             decl_die = comp_unit_die ();
19090           break;
19091
19092         case TRANSLATION_UNIT_DECL:
19093           decl_die = comp_unit_die ();
19094           break;
19095
19096         default:
19097           gcc_unreachable ();
19098         }
19099
19100       /* We should be able to find the DIE now.  */
19101       if (!decl_die)
19102         decl_die = lookup_decl_die (decl);
19103       gcc_assert (decl_die);
19104     }
19105
19106   return decl_die;
19107 }
19108
19109 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19110    always returned.  */
19111
19112 static dw_die_ref
19113 force_type_die (tree type)
19114 {
19115   dw_die_ref type_die;
19116
19117   type_die = lookup_type_die (type);
19118   if (!type_die)
19119     {
19120       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19121
19122       type_die = modified_type_die (type, TYPE_READONLY (type),
19123                                     TYPE_VOLATILE (type), context_die);
19124       gcc_assert (type_die);
19125     }
19126   return type_die;
19127 }
19128
19129 /* Force out any required namespaces to be able to output DECL,
19130    and return the new context_die for it, if it's changed.  */
19131
19132 static dw_die_ref
19133 setup_namespace_context (tree thing, dw_die_ref context_die)
19134 {
19135   tree context = (DECL_P (thing)
19136                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19137   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19138     /* Force out the namespace.  */
19139     context_die = force_decl_die (context);
19140
19141   return context_die;
19142 }
19143
19144 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19145    type) within its namespace, if appropriate.
19146
19147    For compatibility with older debuggers, namespace DIEs only contain
19148    declarations; all definitions are emitted at CU scope.  */
19149
19150 static dw_die_ref
19151 declare_in_namespace (tree thing, dw_die_ref context_die)
19152 {
19153   dw_die_ref ns_context;
19154
19155   if (debug_info_level <= DINFO_LEVEL_TERSE)
19156     return context_die;
19157
19158   /* If this decl is from an inlined function, then don't try to emit it in its
19159      namespace, as we will get confused.  It would have already been emitted
19160      when the abstract instance of the inline function was emitted anyways.  */
19161   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19162     return context_die;
19163
19164   ns_context = setup_namespace_context (thing, context_die);
19165
19166   if (ns_context != context_die)
19167     {
19168       if (is_fortran ())
19169         return ns_context;
19170       if (DECL_P (thing))
19171         gen_decl_die (thing, NULL, ns_context);
19172       else
19173         gen_type_die (thing, ns_context);
19174     }
19175   return context_die;
19176 }
19177
19178 /* Generate a DIE for a namespace or namespace alias.  */
19179
19180 static void
19181 gen_namespace_die (tree decl, dw_die_ref context_die)
19182 {
19183   dw_die_ref namespace_die;
19184
19185   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19186      they are an alias of.  */
19187   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19188     {
19189       /* Output a real namespace or module.  */
19190       context_die = setup_namespace_context (decl, comp_unit_die ());
19191       namespace_die = new_die (is_fortran ()
19192                                ? DW_TAG_module : DW_TAG_namespace,
19193                                context_die, decl);
19194       /* For Fortran modules defined in different CU don't add src coords.  */
19195       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19196         {
19197           const char *name = dwarf2_name (decl, 0);
19198           if (name)
19199             add_name_attribute (namespace_die, name);
19200         }
19201       else
19202         add_name_and_src_coords_attributes (namespace_die, decl);
19203       if (DECL_EXTERNAL (decl))
19204         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19205       equate_decl_number_to_die (decl, namespace_die);
19206     }
19207   else
19208     {
19209       /* Output a namespace alias.  */
19210
19211       /* Force out the namespace we are an alias of, if necessary.  */
19212       dw_die_ref origin_die
19213         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19214
19215       if (DECL_FILE_SCOPE_P (decl)
19216           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19217         context_die = setup_namespace_context (decl, comp_unit_die ());
19218       /* Now create the namespace alias DIE.  */
19219       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19220       add_name_and_src_coords_attributes (namespace_die, decl);
19221       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19222       equate_decl_number_to_die (decl, namespace_die);
19223     }
19224   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
19225   if (want_pubnames ())
19226     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19227 }
19228
19229 /* Generate Dwarf debug information for a decl described by DECL.
19230    The return value is currently only meaningful for PARM_DECLs,
19231    for all other decls it returns NULL.  */
19232
19233 static dw_die_ref
19234 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19235 {
19236   tree decl_or_origin = decl ? decl : origin;
19237   tree class_origin = NULL, ultimate_origin;
19238
19239   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19240     return NULL;
19241
19242   switch (TREE_CODE (decl_or_origin))
19243     {
19244     case ERROR_MARK:
19245       break;
19246
19247     case CONST_DECL:
19248       if (!is_fortran () && !is_ada ())
19249         {
19250           /* The individual enumerators of an enum type get output when we output
19251              the Dwarf representation of the relevant enum type itself.  */
19252           break;
19253         }
19254
19255       /* Emit its type.  */
19256       gen_type_die (TREE_TYPE (decl), context_die);
19257
19258       /* And its containing namespace.  */
19259       context_die = declare_in_namespace (decl, context_die);
19260
19261       gen_const_die (decl, context_die);
19262       break;
19263
19264     case FUNCTION_DECL:
19265       /* Don't output any DIEs to represent mere function declarations,
19266          unless they are class members or explicit block externs.  */
19267       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19268           && DECL_FILE_SCOPE_P (decl_or_origin)
19269           && (current_function_decl == NULL_TREE
19270               || DECL_ARTIFICIAL (decl_or_origin)))
19271         break;
19272
19273 #if 0
19274       /* FIXME */
19275       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19276          on local redeclarations of global functions.  That seems broken.  */
19277       if (current_function_decl != decl)
19278         /* This is only a declaration.  */;
19279 #endif
19280
19281       /* If we're emitting a clone, emit info for the abstract instance.  */
19282       if (origin || DECL_ORIGIN (decl) != decl)
19283         dwarf2out_abstract_function (origin
19284                                      ? DECL_ORIGIN (origin)
19285                                      : DECL_ABSTRACT_ORIGIN (decl));
19286
19287       /* If we're emitting an out-of-line copy of an inline function,
19288          emit info for the abstract instance and set up to refer to it.  */
19289       else if (cgraph_function_possibly_inlined_p (decl)
19290                && ! DECL_ABSTRACT (decl)
19291                && ! class_or_namespace_scope_p (context_die)
19292                /* dwarf2out_abstract_function won't emit a die if this is just
19293                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19294                   that case, because that works only if we have a die.  */
19295                && DECL_INITIAL (decl) != NULL_TREE)
19296         {
19297           dwarf2out_abstract_function (decl);
19298           set_decl_origin_self (decl);
19299         }
19300
19301       /* Otherwise we're emitting the primary DIE for this decl.  */
19302       else if (debug_info_level > DINFO_LEVEL_TERSE)
19303         {
19304           /* Before we describe the FUNCTION_DECL itself, make sure that we
19305              have its containing type.  */
19306           if (!origin)
19307             origin = decl_class_context (decl);
19308           if (origin != NULL_TREE)
19309             gen_type_die (origin, context_die);
19310
19311           /* And its return type.  */
19312           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19313
19314           /* And its virtual context.  */
19315           if (DECL_VINDEX (decl) != NULL_TREE)
19316             gen_type_die (DECL_CONTEXT (decl), context_die);
19317
19318           /* Make sure we have a member DIE for decl.  */
19319           if (origin != NULL_TREE)
19320             gen_type_die_for_member (origin, decl, context_die);
19321
19322           /* And its containing namespace.  */
19323           context_die = declare_in_namespace (decl, context_die);
19324         }
19325
19326       /* Now output a DIE to represent the function itself.  */
19327       if (decl)
19328         gen_subprogram_die (decl, context_die);
19329       break;
19330
19331     case TYPE_DECL:
19332       /* If we are in terse mode, don't generate any DIEs to represent any
19333          actual typedefs.  */
19334       if (debug_info_level <= DINFO_LEVEL_TERSE)
19335         break;
19336
19337       /* In the special case of a TYPE_DECL node representing the declaration
19338          of some type tag, if the given TYPE_DECL is marked as having been
19339          instantiated from some other (original) TYPE_DECL node (e.g. one which
19340          was generated within the original definition of an inline function) we
19341          used to generate a special (abbreviated) DW_TAG_structure_type,
19342          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19343          should be actually referencing those DIEs, as variable DIEs with that
19344          type would be emitted already in the abstract origin, so it was always
19345          removed during unused type prunning.  Don't add anything in this
19346          case.  */
19347       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19348         break;
19349
19350       if (is_redundant_typedef (decl))
19351         gen_type_die (TREE_TYPE (decl), context_die);
19352       else
19353         /* Output a DIE to represent the typedef itself.  */
19354         gen_typedef_die (decl, context_die);
19355       break;
19356
19357     case LABEL_DECL:
19358       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19359         gen_label_die (decl, context_die);
19360       break;
19361
19362     case VAR_DECL:
19363     case RESULT_DECL:
19364       /* If we are in terse mode, don't generate any DIEs to represent any
19365          variable declarations or definitions.  */
19366       if (debug_info_level <= DINFO_LEVEL_TERSE)
19367         break;
19368
19369       /* Output any DIEs that are needed to specify the type of this data
19370          object.  */
19371       if (decl_by_reference_p (decl_or_origin))
19372         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19373       else
19374         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19375
19376       /* And its containing type.  */
19377       class_origin = decl_class_context (decl_or_origin);
19378       if (class_origin != NULL_TREE)
19379         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19380
19381       /* And its containing namespace.  */
19382       context_die = declare_in_namespace (decl_or_origin, context_die);
19383
19384       /* Now output the DIE to represent the data object itself.  This gets
19385          complicated because of the possibility that the VAR_DECL really
19386          represents an inlined instance of a formal parameter for an inline
19387          function.  */
19388       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19389       if (ultimate_origin != NULL_TREE
19390           && TREE_CODE (ultimate_origin) == PARM_DECL)
19391         gen_formal_parameter_die (decl, origin,
19392                                   true /* Emit name attribute.  */,
19393                                   context_die);
19394       else
19395         gen_variable_die (decl, origin, context_die);
19396       break;
19397
19398     case FIELD_DECL:
19399       /* Ignore the nameless fields that are used to skip bits but handle C++
19400          anonymous unions and structs.  */
19401       if (DECL_NAME (decl) != NULL_TREE
19402           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19403           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19404         {
19405           gen_type_die (member_declared_type (decl), context_die);
19406           gen_field_die (decl, context_die);
19407         }
19408       break;
19409
19410     case PARM_DECL:
19411       if (DECL_BY_REFERENCE (decl_or_origin))
19412         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19413       else
19414         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19415       return gen_formal_parameter_die (decl, origin,
19416                                        true /* Emit name attribute.  */,
19417                                        context_die);
19418
19419     case NAMESPACE_DECL:
19420     case IMPORTED_DECL:
19421       if (dwarf_version >= 3 || !dwarf_strict)
19422         gen_namespace_die (decl, context_die);
19423       break;
19424
19425     default:
19426       /* Probably some frontend-internal decl.  Assume we don't care.  */
19427       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19428       break;
19429     }
19430
19431   return NULL;
19432 }
19433 \f
19434 /* Output debug information for global decl DECL.  Called from toplev.c after
19435    compilation proper has finished.  */
19436
19437 static void
19438 dwarf2out_global_decl (tree decl)
19439 {
19440   /* Output DWARF2 information for file-scope tentative data object
19441      declarations, file-scope (extern) function declarations (which
19442      had no corresponding body) and file-scope tagged type declarations
19443      and definitions which have not yet been forced out.  */
19444   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19445     dwarf2out_decl (decl);
19446 }
19447
19448 /* Output debug information for type decl DECL.  Called from toplev.c
19449    and from language front ends (to record built-in types).  */
19450 static void
19451 dwarf2out_type_decl (tree decl, int local)
19452 {
19453   if (!local)
19454     dwarf2out_decl (decl);
19455 }
19456
19457 /* Output debug information for imported module or decl DECL.
19458    NAME is non-NULL name in the lexical block if the decl has been renamed.
19459    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19460    that DECL belongs to.
19461    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19462 static void
19463 dwarf2out_imported_module_or_decl_1 (tree decl,
19464                                      tree name,
19465                                      tree lexical_block,
19466                                      dw_die_ref lexical_block_die)
19467 {
19468   expanded_location xloc;
19469   dw_die_ref imported_die = NULL;
19470   dw_die_ref at_import_die;
19471
19472   if (TREE_CODE (decl) == IMPORTED_DECL)
19473     {
19474       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19475       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19476       gcc_assert (decl);
19477     }
19478   else
19479     xloc = expand_location (input_location);
19480
19481   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19482     {
19483       at_import_die = force_type_die (TREE_TYPE (decl));
19484       /* For namespace N { typedef void T; } using N::T; base_type_die
19485          returns NULL, but DW_TAG_imported_declaration requires
19486          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19487       if (!at_import_die)
19488         {
19489           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19490           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19491           at_import_die = lookup_type_die (TREE_TYPE (decl));
19492           gcc_assert (at_import_die);
19493         }
19494     }
19495   else
19496     {
19497       at_import_die = lookup_decl_die (decl);
19498       if (!at_import_die)
19499         {
19500           /* If we're trying to avoid duplicate debug info, we may not have
19501              emitted the member decl for this field.  Emit it now.  */
19502           if (TREE_CODE (decl) == FIELD_DECL)
19503             {
19504               tree type = DECL_CONTEXT (decl);
19505
19506               if (TYPE_CONTEXT (type)
19507                   && TYPE_P (TYPE_CONTEXT (type))
19508                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19509                                                 DINFO_USAGE_DIR_USE))
19510                 return;
19511               gen_type_die_for_member (type, decl,
19512                                        get_context_die (TYPE_CONTEXT (type)));
19513             }
19514           at_import_die = force_decl_die (decl);
19515         }
19516     }
19517
19518   if (TREE_CODE (decl) == NAMESPACE_DECL)
19519     {
19520       if (dwarf_version >= 3 || !dwarf_strict)
19521         imported_die = new_die (DW_TAG_imported_module,
19522                                 lexical_block_die,
19523                                 lexical_block);
19524       else
19525         return;
19526     }
19527   else
19528     imported_die = new_die (DW_TAG_imported_declaration,
19529                             lexical_block_die,
19530                             lexical_block);
19531
19532   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19533   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19534   if (name)
19535     add_AT_string (imported_die, DW_AT_name,
19536                    IDENTIFIER_POINTER (name));
19537   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19538 }
19539
19540 /* Output debug information for imported module or decl DECL.
19541    NAME is non-NULL name in context if the decl has been renamed.
19542    CHILD is true if decl is one of the renamed decls as part of
19543    importing whole module.  */
19544
19545 static void
19546 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19547                                    bool child)
19548 {
19549   /* dw_die_ref at_import_die;  */
19550   dw_die_ref scope_die;
19551
19552   if (debug_info_level <= DINFO_LEVEL_TERSE)
19553     return;
19554
19555   gcc_assert (decl);
19556
19557   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19558      We need decl DIE for reference and scope die. First, get DIE for the decl
19559      itself.  */
19560
19561   /* Get the scope die for decl context. Use comp_unit_die for global module
19562      or decl. If die is not found for non globals, force new die.  */
19563   if (context
19564       && TYPE_P (context)
19565       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19566     return;
19567
19568   if (!(dwarf_version >= 3 || !dwarf_strict))
19569     return;
19570
19571   scope_die = get_context_die (context);
19572
19573   if (child)
19574     {
19575       gcc_assert (scope_die->die_child);
19576       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19577       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19578       scope_die = scope_die->die_child;
19579     }
19580
19581   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19582   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19583
19584 }
19585
19586 /* Write the debugging output for DECL.  */
19587
19588 void
19589 dwarf2out_decl (tree decl)
19590 {
19591   dw_die_ref context_die = comp_unit_die ();
19592
19593   switch (TREE_CODE (decl))
19594     {
19595     case ERROR_MARK:
19596       return;
19597
19598     case FUNCTION_DECL:
19599       /* What we would really like to do here is to filter out all mere
19600          file-scope declarations of file-scope functions which are never
19601          referenced later within this translation unit (and keep all of ones
19602          that *are* referenced later on) but we aren't clairvoyant, so we have
19603          no idea which functions will be referenced in the future (i.e. later
19604          on within the current translation unit). So here we just ignore all
19605          file-scope function declarations which are not also definitions.  If
19606          and when the debugger needs to know something about these functions,
19607          it will have to hunt around and find the DWARF information associated
19608          with the definition of the function.
19609
19610          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19611          nodes represent definitions and which ones represent mere
19612          declarations.  We have to check DECL_INITIAL instead. That's because
19613          the C front-end supports some weird semantics for "extern inline"
19614          function definitions.  These can get inlined within the current
19615          translation unit (and thus, we need to generate Dwarf info for their
19616          abstract instances so that the Dwarf info for the concrete inlined
19617          instances can have something to refer to) but the compiler never
19618          generates any out-of-lines instances of such things (despite the fact
19619          that they *are* definitions).
19620
19621          The important point is that the C front-end marks these "extern
19622          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19623          them anyway. Note that the C++ front-end also plays some similar games
19624          for inline function definitions appearing within include files which
19625          also contain `#pragma interface' pragmas.
19626
19627          If we are called from dwarf2out_abstract_function output a DIE
19628          anyway.  We can end up here this way with early inlining and LTO
19629          where the inlined function is output in a different LTRANS unit
19630          or not at all.  */
19631       if (DECL_INITIAL (decl) == NULL_TREE
19632           && ! DECL_ABSTRACT (decl))
19633         return;
19634
19635       /* If we're a nested function, initially use a parent of NULL; if we're
19636          a plain function, this will be fixed up in decls_for_scope.  If
19637          we're a method, it will be ignored, since we already have a DIE.  */
19638       if (decl_function_context (decl)
19639           /* But if we're in terse mode, we don't care about scope.  */
19640           && debug_info_level > DINFO_LEVEL_TERSE)
19641         context_die = NULL;
19642       break;
19643
19644     case VAR_DECL:
19645       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19646          declaration and if the declaration was never even referenced from
19647          within this entire compilation unit.  We suppress these DIEs in
19648          order to save space in the .debug section (by eliminating entries
19649          which are probably useless).  Note that we must not suppress
19650          block-local extern declarations (whether used or not) because that
19651          would screw-up the debugger's name lookup mechanism and cause it to
19652          miss things which really ought to be in scope at a given point.  */
19653       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19654         return;
19655
19656       /* For local statics lookup proper context die.  */
19657       if (TREE_STATIC (decl)
19658           && DECL_CONTEXT (decl)
19659           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19660         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19661
19662       /* If we are in terse mode, don't generate any DIEs to represent any
19663          variable declarations or definitions.  */
19664       if (debug_info_level <= DINFO_LEVEL_TERSE)
19665         return;
19666       break;
19667
19668     case CONST_DECL:
19669       if (debug_info_level <= DINFO_LEVEL_TERSE)
19670         return;
19671       if (!is_fortran () && !is_ada ())
19672         return;
19673       if (TREE_STATIC (decl) && decl_function_context (decl))
19674         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19675       break;
19676
19677     case NAMESPACE_DECL:
19678     case IMPORTED_DECL:
19679       if (debug_info_level <= DINFO_LEVEL_TERSE)
19680         return;
19681       if (lookup_decl_die (decl) != NULL)
19682         return;
19683       break;
19684
19685     case TYPE_DECL:
19686       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19687       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19688         return;
19689
19690       /* Don't bother trying to generate any DIEs to represent any of the
19691          normal built-in types for the language we are compiling.  */
19692       if (DECL_IS_BUILTIN (decl))
19693         return;
19694
19695       /* If we are in terse mode, don't generate any DIEs for types.  */
19696       if (debug_info_level <= DINFO_LEVEL_TERSE)
19697         return;
19698
19699       /* If we're a function-scope tag, initially use a parent of NULL;
19700          this will be fixed up in decls_for_scope.  */
19701       if (decl_function_context (decl))
19702         context_die = NULL;
19703
19704       break;
19705
19706     default:
19707       return;
19708     }
19709
19710   gen_decl_die (decl, NULL, context_die);
19711 }
19712
19713 /* Write the debugging output for DECL.  */
19714
19715 static void
19716 dwarf2out_function_decl (tree decl)
19717 {
19718   dwarf2out_decl (decl);
19719   call_arg_locations = NULL;
19720   call_arg_loc_last = NULL;
19721   call_site_count = -1;
19722   tail_call_site_count = -1;
19723   VEC_free (dw_die_ref, heap, block_map);
19724   htab_empty (decl_loc_table);
19725   htab_empty (cached_dw_loc_list_table);
19726 }
19727
19728 /* Output a marker (i.e. a label) for the beginning of the generated code for
19729    a lexical block.  */
19730
19731 static void
19732 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19733                        unsigned int blocknum)
19734 {
19735   switch_to_section (current_function_section ());
19736   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19737 }
19738
19739 /* Output a marker (i.e. a label) for the end of the generated code for a
19740    lexical block.  */
19741
19742 static void
19743 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19744 {
19745   switch_to_section (current_function_section ());
19746   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19747 }
19748
19749 /* Returns nonzero if it is appropriate not to emit any debugging
19750    information for BLOCK, because it doesn't contain any instructions.
19751
19752    Don't allow this for blocks with nested functions or local classes
19753    as we would end up with orphans, and in the presence of scheduling
19754    we may end up calling them anyway.  */
19755
19756 static bool
19757 dwarf2out_ignore_block (const_tree block)
19758 {
19759   tree decl;
19760   unsigned int i;
19761
19762   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19763     if (TREE_CODE (decl) == FUNCTION_DECL
19764         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19765       return 0;
19766   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19767     {
19768       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19769       if (TREE_CODE (decl) == FUNCTION_DECL
19770           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19771       return 0;
19772     }
19773
19774   return 1;
19775 }
19776
19777 /* Hash table routines for file_hash.  */
19778
19779 static int
19780 file_table_eq (const void *p1_p, const void *p2_p)
19781 {
19782   const struct dwarf_file_data *const p1 =
19783     (const struct dwarf_file_data *) p1_p;
19784   const char *const p2 = (const char *) p2_p;
19785   return filename_cmp (p1->filename, p2) == 0;
19786 }
19787
19788 static hashval_t
19789 file_table_hash (const void *p_p)
19790 {
19791   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19792   return htab_hash_string (p->filename);
19793 }
19794
19795 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19796    dwarf2out.c) and return its "index".  The index of each (known) filename is
19797    just a unique number which is associated with only that one filename.  We
19798    need such numbers for the sake of generating labels (in the .debug_sfnames
19799    section) and references to those files numbers (in the .debug_srcinfo
19800    and.debug_macinfo sections).  If the filename given as an argument is not
19801    found in our current list, add it to the list and assign it the next
19802    available unique index number.  In order to speed up searches, we remember
19803    the index of the filename was looked up last.  This handles the majority of
19804    all searches.  */
19805
19806 static struct dwarf_file_data *
19807 lookup_filename (const char *file_name)
19808 {
19809   void ** slot;
19810   struct dwarf_file_data * created;
19811
19812   /* Check to see if the file name that was searched on the previous
19813      call matches this file name.  If so, return the index.  */
19814   if (file_table_last_lookup
19815       && (file_name == file_table_last_lookup->filename
19816           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19817     return file_table_last_lookup;
19818
19819   /* Didn't match the previous lookup, search the table.  */
19820   slot = htab_find_slot_with_hash (file_table, file_name,
19821                                    htab_hash_string (file_name), INSERT);
19822   if (*slot)
19823     return (struct dwarf_file_data *) *slot;
19824
19825   created = ggc_alloc_dwarf_file_data ();
19826   created->filename = file_name;
19827   created->emitted_number = 0;
19828   *slot = created;
19829   return created;
19830 }
19831
19832 /* If the assembler will construct the file table, then translate the compiler
19833    internal file table number into the assembler file table number, and emit
19834    a .file directive if we haven't already emitted one yet.  The file table
19835    numbers are different because we prune debug info for unused variables and
19836    types, which may include filenames.  */
19837
19838 static int
19839 maybe_emit_file (struct dwarf_file_data * fd)
19840 {
19841   if (! fd->emitted_number)
19842     {
19843       if (last_emitted_file)
19844         fd->emitted_number = last_emitted_file->emitted_number + 1;
19845       else
19846         fd->emitted_number = 1;
19847       last_emitted_file = fd;
19848
19849       if (DWARF2_ASM_LINE_DEBUG_INFO)
19850         {
19851           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19852           output_quoted_string (asm_out_file,
19853                                 remap_debug_filename (fd->filename));
19854           fputc ('\n', asm_out_file);
19855         }
19856     }
19857
19858   return fd->emitted_number;
19859 }
19860
19861 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19862    That generation should happen after function debug info has been
19863    generated. The value of the attribute is the constant value of ARG.  */
19864
19865 static void
19866 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19867 {
19868   die_arg_entry entry;
19869
19870   if (!die || !arg)
19871     return;
19872
19873   if (!tmpl_value_parm_die_table)
19874     tmpl_value_parm_die_table
19875       = VEC_alloc (die_arg_entry, gc, 32);
19876
19877   entry.die = die;
19878   entry.arg = arg;
19879   VEC_safe_push (die_arg_entry, gc,
19880                  tmpl_value_parm_die_table,
19881                  entry);
19882 }
19883
19884 /* Return TRUE if T is an instance of generic type, FALSE
19885    otherwise.  */
19886
19887 static bool
19888 generic_type_p (tree t)
19889 {
19890   if (t == NULL_TREE || !TYPE_P (t))
19891     return false;
19892   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19893 }
19894
19895 /* Schedule the generation of the generic parameter dies for the
19896   instance of generic type T. The proper generation itself is later
19897   done by gen_scheduled_generic_parms_dies. */
19898
19899 static void
19900 schedule_generic_params_dies_gen (tree t)
19901 {
19902   if (!generic_type_p (t))
19903     return;
19904
19905   if (generic_type_instances == NULL)
19906     generic_type_instances = VEC_alloc (tree, gc, 256);
19907
19908   VEC_safe_push (tree, gc, generic_type_instances, t);
19909 }
19910
19911 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19912    by append_entry_to_tmpl_value_parm_die_table. This function must
19913    be called after function DIEs have been generated.  */
19914
19915 static void
19916 gen_remaining_tmpl_value_param_die_attribute (void)
19917 {
19918   if (tmpl_value_parm_die_table)
19919     {
19920       unsigned i;
19921       die_arg_entry *e;
19922
19923       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19924         tree_add_const_value_attribute (e->die, e->arg);
19925     }
19926 }
19927
19928 /* Generate generic parameters DIEs for instances of generic types
19929    that have been previously scheduled by
19930    schedule_generic_params_dies_gen. This function must be called
19931    after all the types of the CU have been laid out.  */
19932
19933 static void
19934 gen_scheduled_generic_parms_dies (void)
19935 {
19936   unsigned i;
19937   tree t;
19938
19939   if (generic_type_instances == NULL)
19940     return;
19941   
19942   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19943     gen_generic_params_dies (t);
19944 }
19945
19946
19947 /* Replace DW_AT_name for the decl with name.  */
19948
19949 static void
19950 dwarf2out_set_name (tree decl, tree name)
19951 {
19952   dw_die_ref die;
19953   dw_attr_ref attr;
19954   const char *dname;
19955
19956   die = TYPE_SYMTAB_DIE (decl);
19957   if (!die)
19958     return;
19959
19960   dname = dwarf2_name (name, 0);
19961   if (!dname)
19962     return;
19963
19964   attr = get_AT (die, DW_AT_name);
19965   if (attr)
19966     {
19967       struct indirect_string_node *node;
19968
19969       node = find_AT_string (dname);
19970       /* replace the string.  */
19971       attr->dw_attr_val.v.val_str = node;
19972     }
19973
19974   else
19975     add_name_attribute (die, dname);
19976 }
19977
19978 /* Called by the final INSN scan whenever we see a var location.  We
19979    use it to drop labels in the right places, and throw the location in
19980    our lookup table.  */
19981
19982 static void
19983 dwarf2out_var_location (rtx loc_note)
19984 {
19985   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19986   struct var_loc_node *newloc;
19987   rtx next_real, next_note;
19988   static const char *last_label;
19989   static const char *last_postcall_label;
19990   static bool last_in_cold_section_p;
19991   static rtx expected_next_loc_note;
19992   tree decl;
19993   bool var_loc_p;
19994
19995   if (!NOTE_P (loc_note))
19996     {
19997       if (CALL_P (loc_note))
19998         {
19999           call_site_count++;
20000           if (SIBLING_CALL_P (loc_note))
20001             tail_call_site_count++;
20002         }
20003       return;
20004     }
20005
20006   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20007   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20008     return;
20009
20010   /* Optimize processing a large consecutive sequence of location
20011      notes so we don't spend too much time in next_real_insn.  If the
20012      next insn is another location note, remember the next_real_insn
20013      calculation for next time.  */
20014   next_real = cached_next_real_insn;
20015   if (next_real)
20016     {
20017       if (expected_next_loc_note != loc_note)
20018         next_real = NULL_RTX;
20019     }
20020
20021   next_note = NEXT_INSN (loc_note);
20022   if (! next_note
20023       || INSN_DELETED_P (next_note)
20024       || GET_CODE (next_note) != NOTE
20025       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20026           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20027     next_note = NULL_RTX;
20028
20029   if (! next_real)
20030     next_real = next_real_insn (loc_note);
20031
20032   if (next_note)
20033     {
20034       expected_next_loc_note = next_note;
20035       cached_next_real_insn = next_real;
20036     }
20037   else
20038     cached_next_real_insn = NULL_RTX;
20039
20040   /* If there are no instructions which would be affected by this note,
20041      don't do anything.  */
20042   if (var_loc_p
20043       && next_real == NULL_RTX
20044       && !NOTE_DURING_CALL_P (loc_note))
20045     return;
20046
20047   if (next_real == NULL_RTX)
20048     next_real = get_last_insn ();
20049
20050   /* If there were any real insns between note we processed last time
20051      and this note (or if it is the first note), clear
20052      last_{,postcall_}label so that they are not reused this time.  */
20053   if (last_var_location_insn == NULL_RTX
20054       || last_var_location_insn != next_real
20055       || last_in_cold_section_p != in_cold_section_p)
20056     {
20057       last_label = NULL;
20058       last_postcall_label = NULL;
20059     }
20060
20061   if (var_loc_p)
20062     {
20063       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20064       newloc = add_var_loc_to_decl (decl, loc_note,
20065                                     NOTE_DURING_CALL_P (loc_note)
20066                                     ? last_postcall_label : last_label);
20067       if (newloc == NULL)
20068         return;
20069     }
20070   else
20071     {
20072       decl = NULL_TREE;
20073       newloc = NULL;
20074     }
20075
20076   /* If there were no real insns between note we processed last time
20077      and this note, use the label we emitted last time.  Otherwise
20078      create a new label and emit it.  */
20079   if (last_label == NULL)
20080     {
20081       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20082       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20083       loclabel_num++;
20084       last_label = ggc_strdup (loclabel);
20085     }
20086
20087   if (!var_loc_p)
20088     {
20089       struct call_arg_loc_node *ca_loc
20090         = ggc_alloc_cleared_call_arg_loc_node ();
20091       rtx prev = prev_real_insn (loc_note), x;
20092       ca_loc->call_arg_loc_note = loc_note;
20093       ca_loc->next = NULL;
20094       ca_loc->label = last_label;
20095       gcc_assert (prev
20096                   && (CALL_P (prev)
20097                       || (NONJUMP_INSN_P (prev)
20098                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20099                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20100       if (!CALL_P (prev))
20101         prev = XVECEXP (PATTERN (prev), 0, 0);
20102       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20103       x = PATTERN (prev);
20104       if (GET_CODE (x) == PARALLEL)
20105         x = XVECEXP (x, 0, 0);
20106       if (GET_CODE (x) == SET)
20107         x = SET_SRC (x);
20108       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20109         {
20110           x = XEXP (XEXP (x, 0), 0);
20111           if (GET_CODE (x) == SYMBOL_REF
20112               && SYMBOL_REF_DECL (x)
20113               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20114             ca_loc->symbol_ref = x;
20115         }
20116       ca_loc->block = insn_scope (prev);
20117       if (call_arg_locations)
20118         call_arg_loc_last->next = ca_loc;
20119       else
20120         call_arg_locations = ca_loc;
20121       call_arg_loc_last = ca_loc;
20122     }
20123   else if (!NOTE_DURING_CALL_P (loc_note))
20124     newloc->label = last_label;
20125   else
20126     {
20127       if (!last_postcall_label)
20128         {
20129           sprintf (loclabel, "%s-1", last_label);
20130           last_postcall_label = ggc_strdup (loclabel);
20131         }
20132       newloc->label = last_postcall_label;
20133     }
20134
20135   last_var_location_insn = next_real;
20136   last_in_cold_section_p = in_cold_section_p;
20137 }
20138
20139 /* Note in one location list that text section has changed.  */
20140
20141 static int
20142 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20143 {
20144   var_loc_list *list = (var_loc_list *) *slot;
20145   if (list->first)
20146     list->last_before_switch
20147       = list->last->next ? list->last->next : list->last;
20148   return 1;
20149 }
20150
20151 /* Note in all location lists that text section has changed.  */
20152
20153 static void
20154 var_location_switch_text_section (void)
20155 {
20156   if (decl_loc_table == NULL)
20157     return;
20158
20159   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20160 }
20161
20162 /* Create a new line number table.  */
20163
20164 static dw_line_info_table *
20165 new_line_info_table (void)
20166 {
20167   dw_line_info_table *table;
20168
20169   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20170   table->file_num = 1;
20171   table->line_num = 1;
20172   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20173
20174   return table;
20175 }
20176
20177 /* Lookup the "current" table into which we emit line info, so
20178    that we don't have to do it for every source line.  */
20179
20180 static void
20181 set_cur_line_info_table (section *sec)
20182 {
20183   dw_line_info_table *table;
20184
20185   if (sec == text_section)
20186     table = text_section_line_info;
20187   else if (sec == cold_text_section)
20188     {
20189       table = cold_text_section_line_info;
20190       if (!table)
20191         {
20192           cold_text_section_line_info = table = new_line_info_table ();
20193           table->end_label = cold_end_label;
20194         }
20195     }
20196   else
20197     {
20198       const char *end_label;
20199
20200       if (flag_reorder_blocks_and_partition)
20201         {
20202           if (in_cold_section_p)
20203             end_label = crtl->subsections.cold_section_end_label;
20204           else
20205             end_label = crtl->subsections.hot_section_end_label;
20206         }
20207       else
20208         {
20209           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20210           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20211                                        current_function_funcdef_no);
20212           end_label = ggc_strdup (label);
20213         }
20214
20215       table = new_line_info_table ();
20216       table->end_label = end_label;
20217
20218       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20219     }
20220
20221   if (DWARF2_ASM_LINE_DEBUG_INFO)
20222     table->is_stmt = (cur_line_info_table
20223                       ? cur_line_info_table->is_stmt
20224                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20225   cur_line_info_table = table;
20226 }
20227
20228
20229 /* We need to reset the locations at the beginning of each
20230    function. We can't do this in the end_function hook, because the
20231    declarations that use the locations won't have been output when
20232    that hook is called.  Also compute have_multiple_function_sections here.  */
20233
20234 static void
20235 dwarf2out_begin_function (tree fun)
20236 {
20237   section *sec = function_section (fun);
20238
20239   if (sec != text_section)
20240     have_multiple_function_sections = true;
20241
20242   if (flag_reorder_blocks_and_partition && !cold_text_section)
20243     {
20244       gcc_assert (current_function_decl == fun);
20245       cold_text_section = unlikely_text_section ();
20246       switch_to_section (cold_text_section);
20247       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20248       switch_to_section (sec);
20249     }
20250
20251   dwarf2out_note_section_used ();
20252   call_site_count = 0;
20253   tail_call_site_count = 0;
20254
20255   set_cur_line_info_table (sec);
20256 }
20257
20258 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20259
20260 static void
20261 push_dw_line_info_entry (dw_line_info_table *table,
20262                          enum dw_line_info_opcode opcode, unsigned int val)
20263 {
20264   dw_line_info_entry e;
20265   e.opcode = opcode;
20266   e.val = val;
20267   VEC_safe_push (dw_line_info_entry, gc, table->entries, e);
20268 }
20269
20270 /* Output a label to mark the beginning of a source code line entry
20271    and record information relating to this source line, in
20272    'line_info_table' for later output of the .debug_line section.  */
20273 /* ??? The discriminator parameter ought to be unsigned.  */
20274
20275 static void
20276 dwarf2out_source_line (unsigned int line, const char *filename,
20277                        int discriminator, bool is_stmt)
20278 {
20279   unsigned int file_num;
20280   dw_line_info_table *table;
20281
20282   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20283     return;
20284
20285   /* The discriminator column was added in dwarf4.  Simplify the below
20286      by simply removing it if we're not supposed to output it.  */
20287   if (dwarf_version < 4 && dwarf_strict)
20288     discriminator = 0;
20289
20290   table = cur_line_info_table;
20291   file_num = maybe_emit_file (lookup_filename (filename));
20292
20293   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20294      the debugger has used the second (possibly duplicate) line number
20295      at the beginning of the function to mark the end of the prologue.
20296      We could eliminate any other duplicates within the function.  For
20297      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20298      that second line number entry.  */
20299   /* Recall that this end-of-prologue indication is *not* the same thing
20300      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20301      to which the hook corresponds, follows the last insn that was 
20302      emitted by gen_prologue.  What we need is to precede the first insn
20303      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20304      insn that corresponds to something the user wrote.  These may be
20305      very different locations once scheduling is enabled.  */
20306
20307   if (0 && file_num == table->file_num
20308       && line == table->line_num
20309       && discriminator == table->discrim_num
20310       && is_stmt == table->is_stmt)
20311     return;
20312
20313   switch_to_section (current_function_section ());
20314
20315   /* If requested, emit something human-readable.  */
20316   if (flag_debug_asm)
20317     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20318
20319   if (DWARF2_ASM_LINE_DEBUG_INFO)
20320     {
20321       /* Emit the .loc directive understood by GNU as.  */
20322       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20323          file_num, line, is_stmt, discriminator */
20324       fputs ("\t.loc ", asm_out_file);
20325       fprint_ul (asm_out_file, file_num);
20326       putc (' ', asm_out_file);
20327       fprint_ul (asm_out_file, line);
20328       putc (' ', asm_out_file);
20329       putc ('0', asm_out_file);
20330
20331       if (is_stmt != table->is_stmt)
20332         {
20333           fputs (" is_stmt ", asm_out_file);
20334           putc (is_stmt ? '1' : '0', asm_out_file);
20335         }
20336       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20337         {
20338           gcc_assert (discriminator > 0);
20339           fputs (" discriminator ", asm_out_file);
20340           fprint_ul (asm_out_file, (unsigned long) discriminator);
20341         }
20342       putc ('\n', asm_out_file);
20343     }
20344   else
20345     {
20346       unsigned int label_num = ++line_info_label_num;
20347
20348       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20349
20350       push_dw_line_info_entry (table, LI_set_address, label_num);
20351       if (file_num != table->file_num)
20352         push_dw_line_info_entry (table, LI_set_file, file_num);
20353       if (discriminator != table->discrim_num)
20354         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20355       if (is_stmt != table->is_stmt)
20356         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20357       push_dw_line_info_entry (table, LI_set_line, line);
20358     }
20359
20360   table->file_num = file_num;
20361   table->line_num = line;
20362   table->discrim_num = discriminator;
20363   table->is_stmt = is_stmt;
20364   table->in_use = true;
20365 }
20366
20367 /* Record the beginning of a new source file.  */
20368
20369 static void
20370 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20371 {
20372   if (flag_eliminate_dwarf2_dups)
20373     {
20374       /* Record the beginning of the file for break_out_includes.  */
20375       dw_die_ref bincl_die;
20376
20377       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20378       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20379     }
20380
20381   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20382     {
20383       macinfo_entry e;
20384       e.code = DW_MACINFO_start_file;
20385       e.lineno = lineno;
20386       e.info = ggc_strdup (filename);
20387       VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20388     }
20389 }
20390
20391 /* Record the end of a source file.  */
20392
20393 static void
20394 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20395 {
20396   if (flag_eliminate_dwarf2_dups)
20397     /* Record the end of the file for break_out_includes.  */
20398     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20399
20400   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20401     {
20402       macinfo_entry e;
20403       e.code = DW_MACINFO_end_file;
20404       e.lineno = lineno;
20405       e.info = NULL;
20406       VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20407     }
20408 }
20409
20410 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20411    the tail part of the directive line, i.e. the part which is past the
20412    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20413
20414 static void
20415 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20416                   const char *buffer ATTRIBUTE_UNUSED)
20417 {
20418   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20419     {
20420       macinfo_entry e;
20421       /* Insert a dummy first entry to be able to optimize the whole
20422          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20423       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20424         {
20425           e.code = 0;
20426           e.lineno = 0;
20427           e.info = NULL;
20428           VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20429         }
20430       e.code = DW_MACINFO_define;
20431       e.lineno = lineno;
20432       e.info = ggc_strdup (buffer);
20433       VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20434     }
20435 }
20436
20437 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20438    the tail part of the directive line, i.e. the part which is past the
20439    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20440
20441 static void
20442 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20443                  const char *buffer ATTRIBUTE_UNUSED)
20444 {
20445   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20446     {
20447       macinfo_entry e;
20448       /* Insert a dummy first entry to be able to optimize the whole
20449          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20450       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20451         {
20452           e.code = 0;
20453           e.lineno = 0;
20454           e.info = NULL;
20455           VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20456         }
20457       e.code = DW_MACINFO_undef;
20458       e.lineno = lineno;
20459       e.info = ggc_strdup (buffer);
20460       VEC_safe_push (macinfo_entry, gc, macinfo_table, e);
20461     }
20462 }
20463
20464 /* Routines to manipulate hash table of CUs.  */
20465
20466 static hashval_t
20467 htab_macinfo_hash (const void *of)
20468 {
20469   const macinfo_entry *const entry =
20470     (const macinfo_entry *) of;
20471
20472   return htab_hash_string (entry->info);
20473 }
20474
20475 static int
20476 htab_macinfo_eq (const void *of1, const void *of2)
20477 {
20478   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20479   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20480
20481   return !strcmp (entry1->info, entry2->info);
20482 }
20483
20484 /* Output a single .debug_macinfo entry.  */
20485
20486 static void
20487 output_macinfo_op (macinfo_entry *ref)
20488 {
20489   int file_num;
20490   size_t len;
20491   struct indirect_string_node *node;
20492   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20493   struct dwarf_file_data *fd;
20494
20495   switch (ref->code)
20496     {
20497     case DW_MACINFO_start_file:
20498       fd = lookup_filename (ref->info);
20499       file_num = maybe_emit_file (fd);
20500       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20501       dw2_asm_output_data_uleb128 (ref->lineno,
20502                                    "Included from line number %lu", 
20503                                    (unsigned long) ref->lineno);
20504       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20505       break;
20506     case DW_MACINFO_end_file:
20507       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20508       break;
20509     case DW_MACINFO_define:
20510     case DW_MACINFO_undef:
20511       len = strlen (ref->info) + 1;
20512       if (!dwarf_strict
20513           && len > DWARF_OFFSET_SIZE
20514           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20515           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20516         {
20517           ref->code = ref->code == DW_MACINFO_define
20518                       ? DW_MACRO_GNU_define_indirect
20519                       : DW_MACRO_GNU_undef_indirect;
20520           output_macinfo_op (ref);
20521           return;
20522         }
20523       dw2_asm_output_data (1, ref->code,
20524                            ref->code == DW_MACINFO_define
20525                            ? "Define macro" : "Undefine macro");
20526       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20527                                    (unsigned long) ref->lineno);
20528       dw2_asm_output_nstring (ref->info, -1, "The macro");
20529       break;
20530     case DW_MACRO_GNU_define_indirect:
20531     case DW_MACRO_GNU_undef_indirect:
20532       node = find_AT_string (ref->info);
20533       if (node->form != DW_FORM_strp)
20534         {
20535           char label[32];
20536           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20537           ++dw2_string_counter;
20538           node->label = xstrdup (label);
20539           node->form = DW_FORM_strp;
20540         }
20541       dw2_asm_output_data (1, ref->code,
20542                            ref->code == DW_MACRO_GNU_define_indirect
20543                            ? "Define macro indirect"
20544                            : "Undefine macro indirect");
20545       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20546                                    (unsigned long) ref->lineno);
20547       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20548                              debug_str_section, "The macro: \"%s\"",
20549                              ref->info);
20550       break;
20551     case DW_MACRO_GNU_transparent_include:
20552       dw2_asm_output_data (1, ref->code, "Transparent include");
20553       ASM_GENERATE_INTERNAL_LABEL (label,
20554                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20555       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20556       break;
20557     default:
20558       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20559                ASM_COMMENT_START, (unsigned long) ref->code);
20560       break;
20561     }
20562 }
20563
20564 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20565    other compilation unit .debug_macinfo sections.  IDX is the first
20566    index of a define/undef, return the number of ops that should be
20567    emitted in a comdat .debug_macinfo section and emit
20568    a DW_MACRO_GNU_transparent_include entry referencing it.
20569    If the define/undef entry should be emitted normally, return 0.  */
20570
20571 static unsigned
20572 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20573                         htab_t *macinfo_htab)
20574 {
20575   macinfo_entry *first, *second, *cur, *inc;
20576   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20577   unsigned char checksum[16];
20578   struct md5_ctx ctx;
20579   char *grp_name, *tail;
20580   const char *base;
20581   unsigned int i, count, encoded_filename_len, linebuf_len;
20582   void **slot;
20583
20584   first = &VEC_index (macinfo_entry, macinfo_table, idx);
20585   second = &VEC_index (macinfo_entry, macinfo_table, idx + 1);
20586
20587   /* Optimize only if there are at least two consecutive define/undef ops,
20588      and either all of them are before first DW_MACINFO_start_file
20589      with lineno {0,1} (i.e. predefined macro block), or all of them are
20590      in some included header file.  */
20591   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20592     return 0;
20593   if (VEC_empty (macinfo_entry, files))
20594     {
20595       if (first->lineno > 1 || second->lineno > 1)
20596         return 0;
20597     }
20598   else if (first->lineno == 0)
20599     return 0;
20600
20601   /* Find the last define/undef entry that can be grouped together
20602      with first and at the same time compute md5 checksum of their
20603      codes, linenumbers and strings.  */
20604   md5_init_ctx (&ctx);
20605   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20606     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20607       break;
20608     else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20609       break;
20610     else
20611       {
20612         unsigned char code = cur->code;
20613         md5_process_bytes (&code, 1, &ctx);
20614         checksum_uleb128 (cur->lineno, &ctx);
20615         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20616       }
20617   md5_finish_ctx (&ctx, checksum);
20618   count = i - idx;
20619
20620   /* From the containing include filename (if any) pick up just
20621      usable characters from its basename.  */
20622   if (VEC_empty (macinfo_entry, files))
20623     base = "";
20624   else
20625     base = lbasename (VEC_last (macinfo_entry, files).info);
20626   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20627     if (ISIDNUM (base[i]) || base[i] == '.')
20628       encoded_filename_len++;
20629   /* Count . at the end.  */
20630   if (encoded_filename_len)
20631     encoded_filename_len++;
20632
20633   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20634   linebuf_len = strlen (linebuf);
20635
20636   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20637   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20638                          + 16 * 2 + 1);
20639   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20640   tail = grp_name + 4;
20641   if (encoded_filename_len)
20642     {
20643       for (i = 0; base[i]; i++)
20644         if (ISIDNUM (base[i]) || base[i] == '.')
20645           *tail++ = base[i];
20646       *tail++ = '.';
20647     }
20648   memcpy (tail, linebuf, linebuf_len);
20649   tail += linebuf_len;
20650   *tail++ = '.';
20651   for (i = 0; i < 16; i++)
20652     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20653
20654   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20655      in the empty vector entry before the first define/undef.  */
20656   inc = &VEC_index (macinfo_entry, macinfo_table, idx - 1);
20657   inc->code = DW_MACRO_GNU_transparent_include;
20658   inc->lineno = 0;
20659   inc->info = ggc_strdup (grp_name);
20660   if (*macinfo_htab == NULL)
20661     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20662   /* Avoid emitting duplicates.  */
20663   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20664   if (*slot != NULL)
20665     {
20666       inc->code = 0;
20667       inc->info = NULL;
20668       /* If such an entry has been used before, just emit
20669          a DW_MACRO_GNU_transparent_include op.  */
20670       inc = (macinfo_entry *) *slot;
20671       output_macinfo_op (inc);
20672       /* And clear all macinfo_entry in the range to avoid emitting them
20673          in the second pass.  */
20674       for (i = idx;
20675            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20676            && i < idx + count;
20677            i++)
20678         {
20679           cur->code = 0;
20680           cur->info = NULL;
20681         }
20682     }
20683   else
20684     {
20685       *slot = inc;
20686       inc->lineno = htab_elements (*macinfo_htab);
20687       output_macinfo_op (inc);
20688     }
20689   return count;
20690 }
20691
20692 /* Output macinfo section(s).  */
20693
20694 static void
20695 output_macinfo (void)
20696 {
20697   unsigned i;
20698   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20699   macinfo_entry *ref;
20700   VEC (macinfo_entry, gc) *files = NULL;
20701   htab_t macinfo_htab = NULL;
20702
20703   if (! length)
20704     return;
20705
20706   /* output_macinfo* uses these interchangeably.  */
20707   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20708               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20709               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20710               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20711
20712   /* For .debug_macro emit the section header.  */
20713   if (!dwarf_strict)
20714     {
20715       dw2_asm_output_data (2, 4, "DWARF macro version number");
20716       if (DWARF_OFFSET_SIZE == 8)
20717         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20718       else
20719         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20720       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20721                              debug_line_section, NULL);
20722     }
20723
20724   /* In the first loop, it emits the primary .debug_macinfo section
20725      and after each emitted op the macinfo_entry is cleared.
20726      If a longer range of define/undef ops can be optimized using
20727      DW_MACRO_GNU_transparent_include, the
20728      DW_MACRO_GNU_transparent_include op is emitted and kept in
20729      the vector before the first define/undef in the range and the
20730      whole range of define/undef ops is not emitted and kept.  */
20731   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20732     {
20733       switch (ref->code)
20734         {
20735         case DW_MACINFO_start_file:
20736           VEC_safe_push (macinfo_entry, gc, files, *ref);
20737           break;
20738         case DW_MACINFO_end_file:
20739           if (!VEC_empty (macinfo_entry, files))
20740             VEC_pop (macinfo_entry, files);
20741           break;
20742         case DW_MACINFO_define:
20743         case DW_MACINFO_undef:
20744           if (!dwarf_strict
20745               && HAVE_COMDAT_GROUP
20746               && VEC_length (macinfo_entry, files) != 1
20747               && i > 0
20748               && i + 1 < length
20749               && VEC_index (macinfo_entry, macinfo_table, i - 1).code == 0)
20750             {
20751               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20752               if (count)
20753                 {
20754                   i += count - 1;
20755                   continue;
20756                 }
20757             }
20758           break;
20759         case 0:
20760           /* A dummy entry may be inserted at the beginning to be able
20761              to optimize the whole block of predefined macros.  */
20762           if (i == 0)
20763             continue;
20764         default:
20765           break;
20766         }
20767       output_macinfo_op (ref);
20768       ref->info = NULL;
20769       ref->code = 0;
20770     }
20771
20772   if (macinfo_htab == NULL)
20773     return;
20774
20775   htab_delete (macinfo_htab);
20776
20777   /* If any DW_MACRO_GNU_transparent_include were used, on those
20778      DW_MACRO_GNU_transparent_include entries terminate the
20779      current chain and switch to a new comdat .debug_macinfo
20780      section and emit the define/undef entries within it.  */
20781   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20782     switch (ref->code)
20783       {
20784       case 0:
20785         continue;
20786       case DW_MACRO_GNU_transparent_include:
20787         {
20788           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20789           tree comdat_key = get_identifier (ref->info);
20790           /* Terminate the previous .debug_macinfo section.  */
20791           dw2_asm_output_data (1, 0, "End compilation unit");
20792           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20793                                          SECTION_DEBUG
20794                                          | SECTION_LINKONCE,
20795                                          comdat_key);
20796           ASM_GENERATE_INTERNAL_LABEL (label,
20797                                        DEBUG_MACRO_SECTION_LABEL,
20798                                        ref->lineno);
20799           ASM_OUTPUT_LABEL (asm_out_file, label);
20800           ref->code = 0;
20801           ref->info = NULL;
20802           dw2_asm_output_data (2, 4, "DWARF macro version number");
20803           if (DWARF_OFFSET_SIZE == 8)
20804             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20805           else
20806             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20807         }
20808         break;
20809       case DW_MACINFO_define:
20810       case DW_MACINFO_undef:
20811         output_macinfo_op (ref);
20812         ref->code = 0;
20813         ref->info = NULL;
20814         break;
20815       default:
20816         gcc_unreachable ();
20817       }
20818 }
20819
20820 /* Set up for Dwarf output at the start of compilation.  */
20821
20822 static void
20823 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20824 {
20825   /* Allocate the file_table.  */
20826   file_table = htab_create_ggc (50, file_table_hash,
20827                                 file_table_eq, NULL);
20828
20829   /* Allocate the decl_die_table.  */
20830   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20831                                     decl_die_table_eq, NULL);
20832
20833   /* Allocate the decl_loc_table.  */
20834   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20835                                     decl_loc_table_eq, NULL);
20836
20837   /* Allocate the cached_dw_loc_list_table.  */
20838   cached_dw_loc_list_table
20839     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20840                        cached_dw_loc_list_table_eq, NULL);
20841
20842   /* Allocate the initial hunk of the decl_scope_table.  */
20843   decl_scope_table = VEC_alloc (tree, gc, 256);
20844
20845   /* Allocate the initial hunk of the abbrev_die_table.  */
20846   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20847     (ABBREV_DIE_TABLE_INCREMENT);
20848   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20849   /* Zero-th entry is allocated, but unused.  */
20850   abbrev_die_table_in_use = 1;
20851
20852   /* Allocate the pubtypes and pubnames vectors.  */
20853   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20854   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20855
20856   incomplete_types = VEC_alloc (tree, gc, 64);
20857
20858   used_rtx_array = VEC_alloc (rtx, gc, 32);
20859
20860   debug_info_section = get_section (DEBUG_INFO_SECTION,
20861                                     SECTION_DEBUG, NULL);
20862   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20863                                       SECTION_DEBUG, NULL);
20864   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20865                                        SECTION_DEBUG, NULL);
20866   debug_macinfo_section = get_section (dwarf_strict
20867                                        ? DEBUG_MACINFO_SECTION
20868                                        : DEBUG_MACRO_SECTION,
20869                                        SECTION_DEBUG, NULL);
20870   debug_line_section = get_section (DEBUG_LINE_SECTION,
20871                                     SECTION_DEBUG, NULL);
20872   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20873                                    SECTION_DEBUG, NULL);
20874   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20875                                         SECTION_DEBUG, NULL);
20876   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20877                                         SECTION_DEBUG, NULL);
20878   debug_str_section = get_section (DEBUG_STR_SECTION,
20879                                    DEBUG_STR_SECTION_FLAGS, NULL);
20880   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20881                                       SECTION_DEBUG, NULL);
20882   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20883                                      SECTION_DEBUG, NULL);
20884
20885   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20886   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20887                                DEBUG_ABBREV_SECTION_LABEL, 0);
20888   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20889   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20890                                COLD_TEXT_SECTION_LABEL, 0);
20891   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20892
20893   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20894                                DEBUG_INFO_SECTION_LABEL, 0);
20895   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20896                                DEBUG_LINE_SECTION_LABEL, 0);
20897   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20898                                DEBUG_RANGES_SECTION_LABEL, 0);
20899   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20900                                dwarf_strict
20901                                ? DEBUG_MACINFO_SECTION_LABEL
20902                                : DEBUG_MACRO_SECTION_LABEL, 0);
20903
20904   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20905     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20906
20907   switch_to_section (text_section);
20908   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20909
20910   /* Make sure the line number table for .text always exists.  */
20911   text_section_line_info = new_line_info_table ();
20912   text_section_line_info->end_label = text_end_label;
20913 }
20914
20915 /* Called before compile () starts outputtting functions, variables
20916    and toplevel asms into assembly.  */
20917
20918 static void
20919 dwarf2out_assembly_start (void)
20920 {
20921   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20922       && dwarf2out_do_cfi_asm ()
20923       && (!(flag_unwind_tables || flag_exceptions)
20924           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20925     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20926 }
20927
20928 /* A helper function for dwarf2out_finish called through
20929    htab_traverse.  Emit one queued .debug_str string.  */
20930
20931 static int
20932 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20933 {
20934   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20935
20936   if (node->form == DW_FORM_strp)
20937     {
20938       switch_to_section (debug_str_section);
20939       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20940       assemble_string (node->str, strlen (node->str) + 1);
20941     }
20942
20943   return 1;
20944 }
20945
20946 #if ENABLE_ASSERT_CHECKING
20947 /* Verify that all marks are clear.  */
20948
20949 static void
20950 verify_marks_clear (dw_die_ref die)
20951 {
20952   dw_die_ref c;
20953
20954   gcc_assert (! die->die_mark);
20955   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20956 }
20957 #endif /* ENABLE_ASSERT_CHECKING */
20958
20959 /* Clear the marks for a die and its children.
20960    Be cool if the mark isn't set.  */
20961
20962 static void
20963 prune_unmark_dies (dw_die_ref die)
20964 {
20965   dw_die_ref c;
20966
20967   if (die->die_mark)
20968     die->die_mark = 0;
20969   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20970 }
20971
20972 /* Given DIE that we're marking as used, find any other dies
20973    it references as attributes and mark them as used.  */
20974
20975 static void
20976 prune_unused_types_walk_attribs (dw_die_ref die)
20977 {
20978   dw_attr_ref a;
20979   unsigned ix;
20980
20981   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20982     {
20983       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20984         {
20985           /* A reference to another DIE.
20986              Make sure that it will get emitted.
20987              If it was broken out into a comdat group, don't follow it.  */
20988           if (! AT_ref (a)->comdat_type_p
20989               || a->dw_attr == DW_AT_specification)
20990             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20991         }
20992       /* Set the string's refcount to 0 so that prune_unused_types_mark
20993          accounts properly for it.  */
20994       if (AT_class (a) == dw_val_class_str)
20995         a->dw_attr_val.v.val_str->refcount = 0;
20996     }
20997 }
20998
20999 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21000
21001 static void
21002 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21003 {
21004   dw_die_ref c;
21005
21006   if (die == NULL || die->die_child == NULL)
21007     return;
21008   c = die->die_child;
21009   do
21010     {
21011       switch (c->die_tag)
21012         {
21013         case DW_TAG_template_type_param:
21014         case DW_TAG_template_value_param:
21015         case DW_TAG_GNU_template_template_param:
21016         case DW_TAG_GNU_template_parameter_pack:
21017           prune_unused_types_mark (c, 1);
21018           break;
21019         default:
21020           break;
21021         }
21022       c = c->die_sib;
21023     } while (c && c != die->die_child);
21024 }
21025
21026 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21027    to DIE's children.  */
21028
21029 static void
21030 prune_unused_types_mark (dw_die_ref die, int dokids)
21031 {
21032   dw_die_ref c;
21033
21034   if (die->die_mark == 0)
21035     {
21036       /* We haven't done this node yet.  Mark it as used.  */
21037       die->die_mark = 1;
21038       /* If this is the DIE of a generic type instantiation,
21039          mark the children DIEs that describe its generic parms and
21040          args.  */
21041       prune_unused_types_mark_generic_parms_dies (die);
21042
21043       /* We also have to mark its parents as used.
21044          (But we don't want to mark our parents' kids due to this.)  */
21045       if (die->die_parent)
21046         prune_unused_types_mark (die->die_parent, 0);
21047
21048       /* Mark any referenced nodes.  */
21049       prune_unused_types_walk_attribs (die);
21050
21051       /* If this node is a specification,
21052          also mark the definition, if it exists.  */
21053       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21054         prune_unused_types_mark (die->die_definition, 1);
21055     }
21056
21057   if (dokids && die->die_mark != 2)
21058     {
21059       /* We need to walk the children, but haven't done so yet.
21060          Remember that we've walked the kids.  */
21061       die->die_mark = 2;
21062
21063       /* If this is an array type, we need to make sure our
21064          kids get marked, even if they're types.  If we're
21065          breaking out types into comdat sections, do this
21066          for all type definitions.  */
21067       if (die->die_tag == DW_TAG_array_type
21068           || (use_debug_types
21069               && is_type_die (die) && ! is_declaration_die (die)))
21070         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21071       else
21072         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21073     }
21074 }
21075
21076 /* For local classes, look if any static member functions were emitted
21077    and if so, mark them.  */
21078
21079 static void
21080 prune_unused_types_walk_local_classes (dw_die_ref die)
21081 {
21082   dw_die_ref c;
21083
21084   if (die->die_mark == 2)
21085     return;
21086
21087   switch (die->die_tag)
21088     {
21089     case DW_TAG_structure_type:
21090     case DW_TAG_union_type:
21091     case DW_TAG_class_type:
21092       break;
21093
21094     case DW_TAG_subprogram:
21095       if (!get_AT_flag (die, DW_AT_declaration)
21096           || die->die_definition != NULL)
21097         prune_unused_types_mark (die, 1);
21098       return;
21099
21100     default:
21101       return;
21102     }
21103
21104   /* Mark children.  */
21105   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21106 }
21107
21108 /* Walk the tree DIE and mark types that we actually use.  */
21109
21110 static void
21111 prune_unused_types_walk (dw_die_ref die)
21112 {
21113   dw_die_ref c;
21114
21115   /* Don't do anything if this node is already marked and
21116      children have been marked as well.  */
21117   if (die->die_mark == 2)
21118     return;
21119
21120   switch (die->die_tag)
21121     {
21122     case DW_TAG_structure_type:
21123     case DW_TAG_union_type:
21124     case DW_TAG_class_type:
21125       if (die->die_perennial_p)
21126         break;
21127
21128       for (c = die->die_parent; c; c = c->die_parent)
21129         if (c->die_tag == DW_TAG_subprogram)
21130           break;
21131
21132       /* Finding used static member functions inside of classes
21133          is needed just for local classes, because for other classes
21134          static member function DIEs with DW_AT_specification
21135          are emitted outside of the DW_TAG_*_type.  If we ever change
21136          it, we'd need to call this even for non-local classes.  */
21137       if (c)
21138         prune_unused_types_walk_local_classes (die);
21139
21140       /* It's a type node --- don't mark it.  */
21141       return;
21142
21143     case DW_TAG_const_type:
21144     case DW_TAG_packed_type:
21145     case DW_TAG_pointer_type:
21146     case DW_TAG_reference_type:
21147     case DW_TAG_rvalue_reference_type:
21148     case DW_TAG_volatile_type:
21149     case DW_TAG_typedef:
21150     case DW_TAG_array_type:
21151     case DW_TAG_interface_type:
21152     case DW_TAG_friend:
21153     case DW_TAG_variant_part:
21154     case DW_TAG_enumeration_type:
21155     case DW_TAG_subroutine_type:
21156     case DW_TAG_string_type:
21157     case DW_TAG_set_type:
21158     case DW_TAG_subrange_type:
21159     case DW_TAG_ptr_to_member_type:
21160     case DW_TAG_file_type:
21161       if (die->die_perennial_p)
21162         break;
21163
21164       /* It's a type node --- don't mark it.  */
21165       return;
21166
21167     default:
21168       /* Mark everything else.  */
21169       break;
21170   }
21171
21172   if (die->die_mark == 0)
21173     {
21174       die->die_mark = 1;
21175
21176       /* Now, mark any dies referenced from here.  */
21177       prune_unused_types_walk_attribs (die);
21178     }
21179
21180   die->die_mark = 2;
21181
21182   /* Mark children.  */
21183   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21184 }
21185
21186 /* Increment the string counts on strings referred to from DIE's
21187    attributes.  */
21188
21189 static void
21190 prune_unused_types_update_strings (dw_die_ref die)
21191 {
21192   dw_attr_ref a;
21193   unsigned ix;
21194
21195   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21196     if (AT_class (a) == dw_val_class_str)
21197       {
21198         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21199         s->refcount++;
21200         /* Avoid unnecessarily putting strings that are used less than
21201            twice in the hash table.  */
21202         if (s->refcount
21203             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21204           {
21205             void ** slot;
21206             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21207                                              htab_hash_string (s->str),
21208                                              INSERT);
21209             gcc_assert (*slot == NULL);
21210             *slot = s;
21211           }
21212       }
21213 }
21214
21215 /* Remove from the tree DIE any dies that aren't marked.  */
21216
21217 static void
21218 prune_unused_types_prune (dw_die_ref die)
21219 {
21220   dw_die_ref c;
21221
21222   gcc_assert (die->die_mark);
21223   prune_unused_types_update_strings (die);
21224
21225   if (! die->die_child)
21226     return;
21227
21228   c = die->die_child;
21229   do {
21230     dw_die_ref prev = c;
21231     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21232       if (c == die->die_child)
21233         {
21234           /* No marked children between 'prev' and the end of the list.  */
21235           if (prev == c)
21236             /* No marked children at all.  */
21237             die->die_child = NULL;
21238           else
21239             {
21240               prev->die_sib = c->die_sib;
21241               die->die_child = prev;
21242             }
21243           return;
21244         }
21245
21246     if (c != prev->die_sib)
21247       prev->die_sib = c;
21248     prune_unused_types_prune (c);
21249   } while (c != die->die_child);
21250 }
21251
21252 /* Remove dies representing declarations that we never use.  */
21253
21254 static void
21255 prune_unused_types (void)
21256 {
21257   unsigned int i;
21258   limbo_die_node *node;
21259   comdat_type_node *ctnode;
21260   pubname_ref pub;
21261   dw_die_ref base_type;
21262
21263 #if ENABLE_ASSERT_CHECKING
21264   /* All the marks should already be clear.  */
21265   verify_marks_clear (comp_unit_die ());
21266   for (node = limbo_die_list; node; node = node->next)
21267     verify_marks_clear (node->die);
21268   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21269     verify_marks_clear (ctnode->root_die);
21270 #endif /* ENABLE_ASSERT_CHECKING */
21271
21272   /* Mark types that are used in global variables.  */
21273   premark_types_used_by_global_vars ();
21274
21275   /* Set the mark on nodes that are actually used.  */
21276   prune_unused_types_walk (comp_unit_die ());
21277   for (node = limbo_die_list; node; node = node->next)
21278     prune_unused_types_walk (node->die);
21279   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21280     {
21281       prune_unused_types_walk (ctnode->root_die);
21282       prune_unused_types_mark (ctnode->type_die, 1);
21283     }
21284
21285   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
21286      are unusual in that they are pubnames that are the children of pubtypes.
21287      They should only be marked via their parent DW_TAG_enumeration_type die,
21288      not as roots in themselves.  */
21289   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21290     if (pub->die->die_tag != DW_TAG_enumerator)
21291       prune_unused_types_mark (pub->die, 1);
21292   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21293     prune_unused_types_mark (base_type, 1);
21294
21295   if (debug_str_hash)
21296     htab_empty (debug_str_hash);
21297   prune_unused_types_prune (comp_unit_die ());
21298   for (node = limbo_die_list; node; node = node->next)
21299     prune_unused_types_prune (node->die);
21300   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21301     prune_unused_types_prune (ctnode->root_die);
21302
21303   /* Leave the marks clear.  */
21304   prune_unmark_dies (comp_unit_die ());
21305   for (node = limbo_die_list; node; node = node->next)
21306     prune_unmark_dies (node->die);
21307   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21308     prune_unmark_dies (ctnode->root_die);
21309 }
21310
21311 /* Set the parameter to true if there are any relative pathnames in
21312    the file table.  */
21313 static int
21314 file_table_relative_p (void ** slot, void *param)
21315 {
21316   bool *p = (bool *) param;
21317   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21318   if (!IS_ABSOLUTE_PATH (d->filename))
21319     {
21320       *p = true;
21321       return 0;
21322     }
21323   return 1;
21324 }
21325
21326 /* Routines to manipulate hash table of comdat type units.  */
21327
21328 static hashval_t
21329 htab_ct_hash (const void *of)
21330 {
21331   hashval_t h;
21332   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21333
21334   memcpy (&h, type_node->signature, sizeof (h));
21335   return h;
21336 }
21337
21338 static int
21339 htab_ct_eq (const void *of1, const void *of2)
21340 {
21341   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21342   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21343
21344   return (! memcmp (type_node_1->signature, type_node_2->signature,
21345                     DWARF_TYPE_SIGNATURE_SIZE));
21346 }
21347
21348 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21349    to the location it would have been added, should we know its
21350    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21351    probably improve compactness of debug info, removing equivalent
21352    abbrevs, and hide any differences caused by deferring the
21353    computation of the assembler name, triggered by e.g. PCH.  */
21354
21355 static inline void
21356 move_linkage_attr (dw_die_ref die)
21357 {
21358   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21359   dw_attr_node linkage = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21360
21361   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21362               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21363
21364   while (--ix > 0)
21365     {
21366       dw_attr_node *prev = &VEC_index (dw_attr_node, die->die_attr, ix - 1);
21367
21368       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21369         break;
21370     }
21371
21372   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21373     {
21374       VEC_pop (dw_attr_node, die->die_attr);
21375       VEC_quick_insert (dw_attr_node, die->die_attr, ix, linkage);
21376     }
21377 }
21378
21379 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21380    referenced from typed stack ops and count how often they are used.  */
21381
21382 static void
21383 mark_base_types (dw_loc_descr_ref loc)
21384 {
21385   dw_die_ref base_type = NULL;
21386
21387   for (; loc; loc = loc->dw_loc_next)
21388     {
21389       switch (loc->dw_loc_opc)
21390         {
21391         case DW_OP_GNU_regval_type:
21392         case DW_OP_GNU_deref_type:
21393           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21394           break;
21395         case DW_OP_GNU_convert:
21396         case DW_OP_GNU_reinterpret:
21397           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21398             continue;
21399           /* FALLTHRU */
21400         case DW_OP_GNU_const_type:
21401           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21402           break;
21403         case DW_OP_GNU_entry_value:
21404           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21405           continue;
21406         default:
21407           continue;
21408         }
21409       gcc_assert (base_type->die_parent == comp_unit_die ());
21410       if (base_type->die_mark)
21411         base_type->die_mark++;
21412       else
21413         {
21414           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21415           base_type->die_mark = 1;
21416         }
21417     }
21418 }
21419
21420 /* Comparison function for sorting marked base types.  */
21421
21422 static int
21423 base_type_cmp (const void *x, const void *y)
21424 {
21425   dw_die_ref dx = *(const dw_die_ref *) x;
21426   dw_die_ref dy = *(const dw_die_ref *) y;
21427   unsigned int byte_size1, byte_size2;
21428   unsigned int encoding1, encoding2;
21429   if (dx->die_mark > dy->die_mark)
21430     return -1;
21431   if (dx->die_mark < dy->die_mark)
21432     return 1;
21433   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21434   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21435   if (byte_size1 < byte_size2)
21436     return 1;
21437   if (byte_size1 > byte_size2)
21438     return -1;
21439   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21440   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21441   if (encoding1 < encoding2)
21442     return 1;
21443   if (encoding1 > encoding2)
21444     return -1;
21445   return 0;
21446 }
21447
21448 /* Move base types marked by mark_base_types as early as possible
21449    in the CU, sorted by decreasing usage count both to make the
21450    uleb128 references as small as possible and to make sure they
21451    will have die_offset already computed by calc_die_sizes when
21452    sizes of typed stack loc ops is computed.  */
21453
21454 static void
21455 move_marked_base_types (void)
21456 {
21457   unsigned int i;
21458   dw_die_ref base_type, die, c;
21459
21460   if (VEC_empty (dw_die_ref, base_types))
21461     return;
21462
21463   /* Sort by decreasing usage count, they will be added again in that
21464      order later on.  */
21465   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21466   die = comp_unit_die ();
21467   c = die->die_child;
21468   do
21469     {
21470       dw_die_ref prev = c;
21471       c = c->die_sib;
21472       while (c->die_mark)
21473         {
21474           remove_child_with_prev (c, prev);
21475           /* As base types got marked, there must be at least
21476              one node other than DW_TAG_base_type.  */
21477           gcc_assert (c != c->die_sib);
21478           c = c->die_sib;
21479         }
21480     }
21481   while (c != die->die_child);
21482   gcc_assert (die->die_child);
21483   c = die->die_child;
21484   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21485     {
21486       base_type->die_mark = 0;
21487       base_type->die_sib = c->die_sib;
21488       c->die_sib = base_type;
21489       c = base_type;
21490     }
21491 }
21492
21493 /* Helper function for resolve_addr, attempt to resolve
21494    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21495    SYMBOL_REFs refer to variables emitted in the current CU.  */
21496
21497 static int
21498 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21499 {
21500   rtx rtl = *addr;
21501
21502   if (GET_CODE (rtl) == CONST_STRING)
21503     {
21504       size_t len = strlen (XSTR (rtl, 0)) + 1;
21505       tree t = build_string (len, XSTR (rtl, 0));
21506       tree tlen = size_int (len - 1);
21507       TREE_TYPE (t)
21508         = build_array_type (char_type_node, build_index_type (tlen));
21509       rtl = lookup_constant_def (t);
21510       if (!rtl || !MEM_P (rtl))
21511         return 1;
21512       rtl = XEXP (rtl, 0);
21513       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21514       *addr = rtl;
21515       return 0;
21516     }
21517
21518   if (GET_CODE (rtl) == SYMBOL_REF
21519       && SYMBOL_REF_DECL (rtl))
21520     {
21521       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21522         {
21523           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21524             return 1;
21525         }
21526       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21527         return 1;
21528     }
21529
21530   if (GET_CODE (rtl) == CONST
21531       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21532     return 1;
21533
21534   return 0;
21535 }
21536
21537 /* Helper function for resolve_addr, handle one location
21538    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21539    the location list couldn't be resolved.  */
21540
21541 static bool
21542 resolve_addr_in_expr (dw_loc_descr_ref loc)
21543 {
21544   dw_loc_descr_ref keep = NULL;
21545   for (; loc; loc = loc->dw_loc_next)
21546     switch (loc->dw_loc_opc)
21547       {
21548       case DW_OP_addr:
21549         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21550           return false;
21551         break;
21552       case DW_OP_const4u:
21553       case DW_OP_const8u:
21554         if (loc->dtprel
21555             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21556           return false;
21557         break;
21558       case DW_OP_plus_uconst:
21559         if (size_of_loc_descr (loc)
21560             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21561               + 1
21562             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21563           {
21564             dw_loc_descr_ref repl
21565               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21566             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21567             add_loc_descr (&repl, loc->dw_loc_next);
21568             *loc = *repl;
21569           }
21570         break;
21571       case DW_OP_implicit_value:
21572         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21573             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21574           return false;
21575         break;
21576       case DW_OP_GNU_implicit_pointer:
21577       case DW_OP_GNU_parameter_ref:
21578         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21579           {
21580             dw_die_ref ref
21581               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21582             if (ref == NULL)
21583               return false;
21584             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21585             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21586             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21587           }
21588         break;
21589       case DW_OP_GNU_const_type:
21590       case DW_OP_GNU_regval_type:
21591       case DW_OP_GNU_deref_type:
21592       case DW_OP_GNU_convert:
21593       case DW_OP_GNU_reinterpret:
21594         while (loc->dw_loc_next
21595                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21596           {
21597             dw_die_ref base1, base2;
21598             unsigned enc1, enc2, size1, size2;
21599             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21600                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21601               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21602             else if (loc->dw_loc_oprnd1.val_class
21603                      == dw_val_class_unsigned_const)
21604               break;
21605             else
21606               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21607             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21608                 == dw_val_class_unsigned_const)
21609               break;
21610             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21611             gcc_assert (base1->die_tag == DW_TAG_base_type
21612                         && base2->die_tag == DW_TAG_base_type);
21613             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21614             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21615             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21616             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21617             if (size1 == size2
21618                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21619                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21620                      && loc != keep)
21621                     || enc1 == enc2))
21622               {
21623                 /* Optimize away next DW_OP_GNU_convert after
21624                    adjusting LOC's base type die reference.  */
21625                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21626                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21627                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21628                 else
21629                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21630                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21631                 continue;
21632               }
21633             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21634                point typed stack entry.  */
21635             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21636               keep = loc->dw_loc_next;
21637             break;
21638           }
21639         break;
21640       default:
21641         break;
21642       }
21643   return true;
21644 }
21645
21646 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21647    an address in .rodata section if the string literal is emitted there,
21648    or remove the containing location list or replace DW_AT_const_value
21649    with DW_AT_location and empty location expression, if it isn't found
21650    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21651    to something that has been emitted in the current CU.  */
21652
21653 static void
21654 resolve_addr (dw_die_ref die)
21655 {
21656   dw_die_ref c;
21657   dw_attr_ref a;
21658   dw_loc_list_ref *curr, *start, loc;
21659   unsigned ix;
21660
21661   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21662     switch (AT_class (a))
21663       {
21664       case dw_val_class_loc_list:
21665         start = curr = AT_loc_list_ptr (a);
21666         loc = *curr;
21667         gcc_assert (loc);
21668         /* The same list can be referenced more than once.  See if we have
21669            already recorded the result from a previous pass.  */
21670         if (loc->replaced)
21671           *curr = loc->dw_loc_next;
21672         else if (!loc->resolved_addr)
21673           {
21674             /* As things stand, we do not expect or allow one die to
21675                reference a suffix of another die's location list chain.
21676                References must be identical or completely separate.
21677                There is therefore no need to cache the result of this
21678                pass on any list other than the first; doing so
21679                would lead to unnecessary writes.  */
21680             while (*curr)
21681               {
21682                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21683                 if (!resolve_addr_in_expr ((*curr)->expr))
21684                   {
21685                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21686                     if (next && (*curr)->ll_symbol)
21687                       {
21688                         gcc_assert (!next->ll_symbol);
21689                         next->ll_symbol = (*curr)->ll_symbol;
21690                       }
21691                     *curr = next;
21692                   }
21693                 else
21694                   {
21695                     mark_base_types ((*curr)->expr);
21696                     curr = &(*curr)->dw_loc_next;
21697                   }
21698               }
21699             if (loc == *start)
21700               loc->resolved_addr = 1;
21701             else
21702               {
21703                 loc->replaced = 1;
21704                 loc->dw_loc_next = *start;
21705               }
21706           }
21707         if (!*start)
21708           {
21709             remove_AT (die, a->dw_attr);
21710             ix--;
21711           }
21712         break;
21713       case dw_val_class_loc:
21714         {
21715           dw_loc_descr_ref l = AT_loc (a);
21716           /* For -gdwarf-2 don't attempt to optimize
21717              DW_AT_data_member_location containing
21718              DW_OP_plus_uconst - older consumers might
21719              rely on it being that op instead of a more complex,
21720              but shorter, location description.  */
21721           if ((dwarf_version > 2
21722                || a->dw_attr != DW_AT_data_member_location
21723                || l == NULL
21724                || l->dw_loc_opc != DW_OP_plus_uconst
21725                || l->dw_loc_next != NULL)
21726               && !resolve_addr_in_expr (l))
21727             {
21728               remove_AT (die, a->dw_attr);
21729               ix--;
21730             }
21731           else
21732             mark_base_types (l);
21733         }
21734         break;
21735       case dw_val_class_addr:
21736         if (a->dw_attr == DW_AT_const_value
21737             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21738           {
21739             remove_AT (die, a->dw_attr);
21740             ix--;
21741           }
21742         if (die->die_tag == DW_TAG_GNU_call_site
21743             && a->dw_attr == DW_AT_abstract_origin)
21744           {
21745             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21746             dw_die_ref tdie = lookup_decl_die (tdecl);
21747             if (tdie == NULL
21748                 && DECL_EXTERNAL (tdecl)
21749                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21750               {
21751                 force_decl_die (tdecl);
21752                 tdie = lookup_decl_die (tdecl);
21753               }
21754             if (tdie)
21755               {
21756                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21757                 a->dw_attr_val.v.val_die_ref.die = tdie;
21758                 a->dw_attr_val.v.val_die_ref.external = 0;
21759               }
21760             else
21761               {
21762                 remove_AT (die, a->dw_attr);
21763                 ix--;
21764               }
21765           }
21766         break;
21767       default:
21768         break;
21769       }
21770
21771   FOR_EACH_CHILD (die, c, resolve_addr (c));
21772 }
21773 \f
21774 /* Helper routines for optimize_location_lists.
21775    This pass tries to share identical local lists in .debug_loc
21776    section.  */
21777
21778 /* Iteratively hash operands of LOC opcode.  */
21779
21780 static inline hashval_t
21781 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21782 {
21783   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21784   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21785
21786   switch (loc->dw_loc_opc)
21787     {
21788     case DW_OP_const4u:
21789     case DW_OP_const8u:
21790       if (loc->dtprel)
21791         goto hash_addr;
21792       /* FALLTHRU */
21793     case DW_OP_const1u:
21794     case DW_OP_const1s:
21795     case DW_OP_const2u:
21796     case DW_OP_const2s:
21797     case DW_OP_const4s:
21798     case DW_OP_const8s:
21799     case DW_OP_constu:
21800     case DW_OP_consts:
21801     case DW_OP_pick:
21802     case DW_OP_plus_uconst:
21803     case DW_OP_breg0:
21804     case DW_OP_breg1:
21805     case DW_OP_breg2:
21806     case DW_OP_breg3:
21807     case DW_OP_breg4:
21808     case DW_OP_breg5:
21809     case DW_OP_breg6:
21810     case DW_OP_breg7:
21811     case DW_OP_breg8:
21812     case DW_OP_breg9:
21813     case DW_OP_breg10:
21814     case DW_OP_breg11:
21815     case DW_OP_breg12:
21816     case DW_OP_breg13:
21817     case DW_OP_breg14:
21818     case DW_OP_breg15:
21819     case DW_OP_breg16:
21820     case DW_OP_breg17:
21821     case DW_OP_breg18:
21822     case DW_OP_breg19:
21823     case DW_OP_breg20:
21824     case DW_OP_breg21:
21825     case DW_OP_breg22:
21826     case DW_OP_breg23:
21827     case DW_OP_breg24:
21828     case DW_OP_breg25:
21829     case DW_OP_breg26:
21830     case DW_OP_breg27:
21831     case DW_OP_breg28:
21832     case DW_OP_breg29:
21833     case DW_OP_breg30:
21834     case DW_OP_breg31:
21835     case DW_OP_regx:
21836     case DW_OP_fbreg:
21837     case DW_OP_piece:
21838     case DW_OP_deref_size:
21839     case DW_OP_xderef_size:
21840       hash = iterative_hash_object (val1->v.val_int, hash);
21841       break;
21842     case DW_OP_skip:
21843     case DW_OP_bra:
21844       {
21845         int offset;
21846
21847         gcc_assert (val1->val_class == dw_val_class_loc);
21848         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21849         hash = iterative_hash_object (offset, hash);
21850       }
21851       break;
21852     case DW_OP_implicit_value:
21853       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21854       switch (val2->val_class)
21855         {
21856         case dw_val_class_const:
21857           hash = iterative_hash_object (val2->v.val_int, hash);
21858           break;
21859         case dw_val_class_vec:
21860           {
21861             unsigned int elt_size = val2->v.val_vec.elt_size;
21862             unsigned int len = val2->v.val_vec.length;
21863
21864             hash = iterative_hash_object (elt_size, hash);
21865             hash = iterative_hash_object (len, hash);
21866             hash = iterative_hash (val2->v.val_vec.array,
21867                                    len * elt_size, hash);
21868           }
21869           break;
21870         case dw_val_class_const_double:
21871           hash = iterative_hash_object (val2->v.val_double.low, hash);
21872           hash = iterative_hash_object (val2->v.val_double.high, hash);
21873           break;
21874         case dw_val_class_addr:
21875           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21876           break;
21877         default:
21878           gcc_unreachable ();
21879         }
21880       break;
21881     case DW_OP_bregx:
21882     case DW_OP_bit_piece:
21883       hash = iterative_hash_object (val1->v.val_int, hash);
21884       hash = iterative_hash_object (val2->v.val_int, hash);
21885       break;
21886     case DW_OP_addr:
21887     hash_addr:
21888       if (loc->dtprel)
21889         {
21890           unsigned char dtprel = 0xd1;
21891           hash = iterative_hash_object (dtprel, hash);
21892         }
21893       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21894       break;
21895     case DW_OP_GNU_implicit_pointer:
21896       hash = iterative_hash_object (val2->v.val_int, hash);
21897       break;
21898     case DW_OP_GNU_entry_value:
21899       hash = hash_loc_operands (val1->v.val_loc, hash);
21900       break;
21901     case DW_OP_GNU_regval_type:
21902     case DW_OP_GNU_deref_type:
21903       {
21904         unsigned int byte_size
21905           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21906         unsigned int encoding
21907           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21908         hash = iterative_hash_object (val1->v.val_int, hash);
21909         hash = iterative_hash_object (byte_size, hash);
21910         hash = iterative_hash_object (encoding, hash);
21911       }
21912       break;
21913     case DW_OP_GNU_convert:
21914     case DW_OP_GNU_reinterpret:
21915       if (val1->val_class == dw_val_class_unsigned_const)
21916         {
21917           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21918           break;
21919         }
21920       /* FALLTHRU */
21921     case DW_OP_GNU_const_type:
21922       {
21923         unsigned int byte_size
21924           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21925         unsigned int encoding
21926           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21927         hash = iterative_hash_object (byte_size, hash);
21928         hash = iterative_hash_object (encoding, hash);
21929         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21930           break;
21931         hash = iterative_hash_object (val2->val_class, hash);
21932         switch (val2->val_class)
21933           {
21934           case dw_val_class_const:
21935             hash = iterative_hash_object (val2->v.val_int, hash);
21936             break;
21937           case dw_val_class_vec:
21938             {
21939               unsigned int elt_size = val2->v.val_vec.elt_size;
21940               unsigned int len = val2->v.val_vec.length;
21941
21942               hash = iterative_hash_object (elt_size, hash);
21943               hash = iterative_hash_object (len, hash);
21944               hash = iterative_hash (val2->v.val_vec.array,
21945                                      len * elt_size, hash);
21946             }
21947             break;
21948           case dw_val_class_const_double:
21949             hash = iterative_hash_object (val2->v.val_double.low, hash);
21950             hash = iterative_hash_object (val2->v.val_double.high, hash);
21951             break;
21952           default:
21953             gcc_unreachable ();
21954           }
21955       }
21956       break;
21957
21958     default:
21959       /* Other codes have no operands.  */
21960       break;
21961     }
21962   return hash;
21963 }
21964
21965 /* Iteratively hash the whole DWARF location expression LOC.  */
21966
21967 static inline hashval_t
21968 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21969 {
21970   dw_loc_descr_ref l;
21971   bool sizes_computed = false;
21972   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21973   size_of_locs (loc);
21974
21975   for (l = loc; l != NULL; l = l->dw_loc_next)
21976     {
21977       enum dwarf_location_atom opc = l->dw_loc_opc;
21978       hash = iterative_hash_object (opc, hash);
21979       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21980         {
21981           size_of_locs (loc);
21982           sizes_computed = true;
21983         }
21984       hash = hash_loc_operands (l, hash);
21985     }
21986   return hash;
21987 }
21988
21989 /* Compute hash of the whole location list LIST_HEAD.  */
21990
21991 static inline void
21992 hash_loc_list (dw_loc_list_ref list_head)
21993 {
21994   dw_loc_list_ref curr = list_head;
21995   hashval_t hash = 0;
21996
21997   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21998     {
21999       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22000       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22001       if (curr->section)
22002         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22003                                hash);
22004       hash = hash_locs (curr->expr, hash);
22005     }
22006   list_head->hash = hash;
22007 }
22008
22009 /* Return true if X and Y opcodes have the same operands.  */
22010
22011 static inline bool
22012 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22013 {
22014   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22015   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22016   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22017   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22018
22019   switch (x->dw_loc_opc)
22020     {
22021     case DW_OP_const4u:
22022     case DW_OP_const8u:
22023       if (x->dtprel)
22024         goto hash_addr;
22025       /* FALLTHRU */
22026     case DW_OP_const1u:
22027     case DW_OP_const1s:
22028     case DW_OP_const2u:
22029     case DW_OP_const2s:
22030     case DW_OP_const4s:
22031     case DW_OP_const8s:
22032     case DW_OP_constu:
22033     case DW_OP_consts:
22034     case DW_OP_pick:
22035     case DW_OP_plus_uconst:
22036     case DW_OP_breg0:
22037     case DW_OP_breg1:
22038     case DW_OP_breg2:
22039     case DW_OP_breg3:
22040     case DW_OP_breg4:
22041     case DW_OP_breg5:
22042     case DW_OP_breg6:
22043     case DW_OP_breg7:
22044     case DW_OP_breg8:
22045     case DW_OP_breg9:
22046     case DW_OP_breg10:
22047     case DW_OP_breg11:
22048     case DW_OP_breg12:
22049     case DW_OP_breg13:
22050     case DW_OP_breg14:
22051     case DW_OP_breg15:
22052     case DW_OP_breg16:
22053     case DW_OP_breg17:
22054     case DW_OP_breg18:
22055     case DW_OP_breg19:
22056     case DW_OP_breg20:
22057     case DW_OP_breg21:
22058     case DW_OP_breg22:
22059     case DW_OP_breg23:
22060     case DW_OP_breg24:
22061     case DW_OP_breg25:
22062     case DW_OP_breg26:
22063     case DW_OP_breg27:
22064     case DW_OP_breg28:
22065     case DW_OP_breg29:
22066     case DW_OP_breg30:
22067     case DW_OP_breg31:
22068     case DW_OP_regx:
22069     case DW_OP_fbreg:
22070     case DW_OP_piece:
22071     case DW_OP_deref_size:
22072     case DW_OP_xderef_size:
22073       return valx1->v.val_int == valy1->v.val_int;
22074     case DW_OP_skip:
22075     case DW_OP_bra:
22076       gcc_assert (valx1->val_class == dw_val_class_loc
22077                   && valy1->val_class == dw_val_class_loc
22078                   && x->dw_loc_addr == y->dw_loc_addr);
22079       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22080     case DW_OP_implicit_value:
22081       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22082           || valx2->val_class != valy2->val_class)
22083         return false;
22084       switch (valx2->val_class)
22085         {
22086         case dw_val_class_const:
22087           return valx2->v.val_int == valy2->v.val_int;
22088         case dw_val_class_vec:
22089           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22090                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22091                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22092                             valx2->v.val_vec.elt_size
22093                             * valx2->v.val_vec.length) == 0;
22094         case dw_val_class_const_double:
22095           return valx2->v.val_double.low == valy2->v.val_double.low
22096                  && valx2->v.val_double.high == valy2->v.val_double.high;
22097         case dw_val_class_addr:
22098           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22099         default:
22100           gcc_unreachable ();
22101         }
22102     case DW_OP_bregx:
22103     case DW_OP_bit_piece:
22104       return valx1->v.val_int == valy1->v.val_int
22105              && valx2->v.val_int == valy2->v.val_int;
22106     case DW_OP_addr:
22107     hash_addr:
22108       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22109     case DW_OP_GNU_implicit_pointer:
22110       return valx1->val_class == dw_val_class_die_ref
22111              && valx1->val_class == valy1->val_class
22112              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22113              && valx2->v.val_int == valy2->v.val_int;
22114     case DW_OP_GNU_entry_value:
22115       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22116     case DW_OP_GNU_const_type:
22117       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22118           || valx2->val_class != valy2->val_class)
22119         return false;
22120       switch (valx2->val_class)
22121         {
22122         case dw_val_class_const:
22123           return valx2->v.val_int == valy2->v.val_int;
22124         case dw_val_class_vec:
22125           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22126                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22127                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22128                             valx2->v.val_vec.elt_size
22129                             * valx2->v.val_vec.length) == 0;
22130         case dw_val_class_const_double:
22131           return valx2->v.val_double.low == valy2->v.val_double.low
22132                  && valx2->v.val_double.high == valy2->v.val_double.high;
22133         default:
22134           gcc_unreachable ();
22135         }
22136     case DW_OP_GNU_regval_type:
22137     case DW_OP_GNU_deref_type:
22138       return valx1->v.val_int == valy1->v.val_int
22139              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22140     case DW_OP_GNU_convert:
22141     case DW_OP_GNU_reinterpret:
22142       if (valx1->val_class != valy1->val_class)
22143         return false;
22144       if (valx1->val_class == dw_val_class_unsigned_const)
22145         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22146       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22147     case DW_OP_GNU_parameter_ref:
22148       return valx1->val_class == dw_val_class_die_ref
22149              && valx1->val_class == valy1->val_class
22150              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22151     default:
22152       /* Other codes have no operands.  */
22153       return true;
22154     }
22155 }
22156
22157 /* Return true if DWARF location expressions X and Y are the same.  */
22158
22159 static inline bool
22160 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22161 {
22162   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22163     if (x->dw_loc_opc != y->dw_loc_opc
22164         || x->dtprel != y->dtprel
22165         || !compare_loc_operands (x, y))
22166       break;
22167   return x == NULL && y == NULL;
22168 }
22169
22170 /* Return precomputed hash of location list X.  */
22171
22172 static hashval_t
22173 loc_list_hash (const void *x)
22174 {
22175   return ((const struct dw_loc_list_struct *) x)->hash;
22176 }
22177
22178 /* Return 1 if location lists X and Y are the same.  */
22179
22180 static int
22181 loc_list_eq (const void *x, const void *y)
22182 {
22183   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22184   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22185   if (a == b)
22186     return 1;
22187   if (a->hash != b->hash)
22188     return 0;
22189   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22190     if (strcmp (a->begin, b->begin) != 0
22191         || strcmp (a->end, b->end) != 0
22192         || (a->section == NULL) != (b->section == NULL)
22193         || (a->section && strcmp (a->section, b->section) != 0)
22194         || !compare_locs (a->expr, b->expr))
22195       break;
22196   return a == NULL && b == NULL;
22197 }
22198
22199 /* Recursively optimize location lists referenced from DIE
22200    children and share them whenever possible.  */
22201
22202 static void
22203 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22204 {
22205   dw_die_ref c;
22206   dw_attr_ref a;
22207   unsigned ix;
22208   void **slot;
22209
22210   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22211     if (AT_class (a) == dw_val_class_loc_list)
22212       {
22213         dw_loc_list_ref list = AT_loc_list (a);
22214         /* TODO: perform some optimizations here, before hashing
22215            it and storing into the hash table.  */
22216         hash_loc_list (list);
22217         slot = htab_find_slot_with_hash (htab, list, list->hash,
22218                                          INSERT);
22219         if (*slot == NULL)
22220           *slot = (void *) list;
22221         else
22222           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22223       }
22224
22225   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22226 }
22227
22228 /* Optimize location lists referenced from DIE
22229    children and share them whenever possible.  */
22230
22231 static void
22232 optimize_location_lists (dw_die_ref die)
22233 {
22234   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22235   optimize_location_lists_1 (die, htab);
22236   htab_delete (htab);
22237 }
22238 \f
22239 /* Output stuff that dwarf requires at the end of every file,
22240    and generate the DWARF-2 debugging info.  */
22241
22242 static void
22243 dwarf2out_finish (const char *filename)
22244 {
22245   limbo_die_node *node, *next_node;
22246   comdat_type_node *ctnode;
22247   htab_t comdat_type_table;
22248   unsigned int i;
22249
22250   /* PCH might result in DW_AT_producer string being restored from the
22251      header compilation, fix it up if needed.  */
22252   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22253   if (strcmp (AT_string (producer), producer_string) != 0)
22254     {
22255       struct indirect_string_node *node = find_AT_string (producer_string);
22256       producer->dw_attr_val.v.val_str = node;
22257     }
22258
22259   gen_scheduled_generic_parms_dies ();
22260   gen_remaining_tmpl_value_param_die_attribute ();
22261
22262   /* Add the name for the main input file now.  We delayed this from
22263      dwarf2out_init to avoid complications with PCH.  */
22264   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22265   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22266     add_comp_dir_attribute (comp_unit_die ());
22267   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22268     {
22269       bool p = false;
22270       htab_traverse (file_table, file_table_relative_p, &p);
22271       if (p)
22272         add_comp_dir_attribute (comp_unit_die ());
22273     }
22274
22275   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22276     {
22277       add_location_or_const_value_attribute (
22278         VEC_index (deferred_locations, deferred_locations_list, i).die,
22279         VEC_index (deferred_locations, deferred_locations_list, i).variable,
22280         false,
22281         DW_AT_location);
22282     }
22283
22284   /* Traverse the limbo die list, and add parent/child links.  The only
22285      dies without parents that should be here are concrete instances of
22286      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22287      For concrete instances, we can get the parent die from the abstract
22288      instance.  */
22289   for (node = limbo_die_list; node; node = next_node)
22290     {
22291       dw_die_ref die = node->die;
22292       next_node = node->next;
22293
22294       if (die->die_parent == NULL)
22295         {
22296           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22297
22298           if (origin && origin->die_parent)
22299             add_child_die (origin->die_parent, die);
22300           else if (is_cu_die (die))
22301             ;
22302           else if (seen_error ())
22303             /* It's OK to be confused by errors in the input.  */
22304             add_child_die (comp_unit_die (), die);
22305           else
22306             {
22307               /* In certain situations, the lexical block containing a
22308                  nested function can be optimized away, which results
22309                  in the nested function die being orphaned.  Likewise
22310                  with the return type of that nested function.  Force
22311                  this to be a child of the containing function.
22312
22313                  It may happen that even the containing function got fully
22314                  inlined and optimized out.  In that case we are lost and
22315                  assign the empty child.  This should not be big issue as
22316                  the function is likely unreachable too.  */
22317               gcc_assert (node->created_for);
22318
22319               if (DECL_P (node->created_for))
22320                 origin = get_context_die (DECL_CONTEXT (node->created_for));
22321               else if (TYPE_P (node->created_for))
22322                 origin = scope_die_for (node->created_for, comp_unit_die ());
22323               else
22324                 origin = comp_unit_die ();
22325
22326               add_child_die (origin, die);
22327             }
22328         }
22329     }
22330
22331   limbo_die_list = NULL;
22332
22333 #if ENABLE_ASSERT_CHECKING
22334   {
22335     dw_die_ref die = comp_unit_die (), c;
22336     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22337   }
22338 #endif
22339   resolve_addr (comp_unit_die ());
22340   move_marked_base_types ();
22341
22342   for (node = deferred_asm_name; node; node = node->next)
22343     {
22344       tree decl = node->created_for;
22345       /* When generating LTO bytecode we can not generate new assembler
22346          names at this point and all important decls got theirs via
22347          free-lang-data.  */
22348       if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22349           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22350         {
22351           add_linkage_attr (node->die, decl);
22352           move_linkage_attr (node->die);
22353         }
22354     }
22355
22356   deferred_asm_name = NULL;
22357
22358   /* Walk through the list of incomplete types again, trying once more to
22359      emit full debugging info for them.  */
22360   retry_incomplete_types ();
22361
22362   if (flag_eliminate_unused_debug_types)
22363     prune_unused_types ();
22364
22365   /* Generate separate COMDAT sections for type DIEs. */
22366   if (use_debug_types)
22367     {
22368       break_out_comdat_types (comp_unit_die ());
22369
22370       /* Each new type_unit DIE was added to the limbo die list when created.
22371          Since these have all been added to comdat_type_list, clear the
22372          limbo die list.  */
22373       limbo_die_list = NULL;
22374
22375       /* For each new comdat type unit, copy declarations for incomplete
22376          types to make the new unit self-contained (i.e., no direct
22377          references to the main compile unit).  */
22378       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22379         copy_decls_for_unworthy_types (ctnode->root_die);
22380       copy_decls_for_unworthy_types (comp_unit_die ());
22381
22382       /* In the process of copying declarations from one unit to another,
22383          we may have left some declarations behind that are no longer
22384          referenced.  Prune them.  */
22385       prune_unused_types ();
22386     }
22387
22388   /* Generate separate CUs for each of the include files we've seen.
22389      They will go into limbo_die_list.  */
22390   if (flag_eliminate_dwarf2_dups)
22391     break_out_includes (comp_unit_die ());
22392
22393   /* Traverse the DIE's and add add sibling attributes to those DIE's
22394      that have children.  */
22395   add_sibling_attributes (comp_unit_die ());
22396   for (node = limbo_die_list; node; node = node->next)
22397     add_sibling_attributes (node->die);
22398   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22399     add_sibling_attributes (ctnode->root_die);
22400
22401   /* Output a terminator label for the .text section.  */
22402   switch_to_section (text_section);
22403   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22404   if (cold_text_section)
22405     {
22406       switch_to_section (cold_text_section);
22407       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22408     }
22409
22410   /* We can only use the low/high_pc attributes if all of the code was
22411      in .text.  */
22412   if (!have_multiple_function_sections 
22413       || (dwarf_version < 3 && dwarf_strict))
22414     {
22415       /* Don't add if the CU has no associated code.  */
22416       if (text_section_used)
22417         add_AT_low_high_pc (comp_unit_die (), text_section_label,
22418                             text_end_label);
22419     }
22420   else
22421     {
22422       unsigned fde_idx;
22423       dw_fde_ref fde;
22424       bool range_list_added = false;
22425
22426       if (text_section_used)
22427         add_ranges_by_labels (comp_unit_die (), text_section_label,
22428                               text_end_label, &range_list_added);
22429       if (cold_text_section_used)
22430         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22431                               cold_end_label, &range_list_added);
22432
22433       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22434         {
22435           if (DECL_IGNORED_P (fde->decl))
22436             continue;
22437           if (!fde->in_std_section)
22438             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22439                                   fde->dw_fde_end, &range_list_added);
22440           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22441             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22442                                   fde->dw_fde_second_end, &range_list_added);
22443         }
22444
22445       if (range_list_added)
22446         {
22447           /* We need to give .debug_loc and .debug_ranges an appropriate
22448              "base address".  Use zero so that these addresses become
22449              absolute.  Historically, we've emitted the unexpected
22450              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22451              Emit both to give time for other tools to adapt.  */
22452           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22453           if (! dwarf_strict && dwarf_version < 4)
22454             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22455
22456           add_ranges (NULL);
22457         }
22458     }
22459
22460   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22461     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22462                     debug_line_section_label);
22463
22464   if (have_macinfo)
22465     add_AT_macptr (comp_unit_die (),
22466                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22467                    macinfo_section_label);
22468
22469   if (have_location_lists)
22470     optimize_location_lists (comp_unit_die ());
22471
22472   /* Output all of the compilation units.  We put the main one last so that
22473      the offsets are available to output_pubnames.  */
22474   for (node = limbo_die_list; node; node = node->next)
22475     output_comp_unit (node->die, 0);
22476
22477   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22478   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22479     {
22480       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22481
22482       /* Don't output duplicate types.  */
22483       if (*slot != HTAB_EMPTY_ENTRY)
22484         continue;
22485
22486       /* Add a pointer to the line table for the main compilation unit
22487          so that the debugger can make sense of DW_AT_decl_file
22488          attributes.  */
22489       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22490         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22491                         debug_line_section_label);
22492
22493       output_comdat_type_unit (ctnode);
22494       *slot = ctnode;
22495     }
22496   htab_delete (comdat_type_table);
22497
22498   add_AT_pubnames (comp_unit_die ());
22499
22500   /* Output the main compilation unit if non-empty or if .debug_macinfo
22501      or .debug_macro will be emitted.  */
22502   output_comp_unit (comp_unit_die (), have_macinfo);
22503
22504   /* Output the abbreviation table.  */
22505   if (abbrev_die_table_in_use != 1)
22506     {
22507       switch_to_section (debug_abbrev_section);
22508       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22509       output_abbrev_section ();
22510     }
22511
22512   /* Output location list section if necessary.  */
22513   if (have_location_lists)
22514     {
22515       /* Output the location lists info.  */
22516       switch_to_section (debug_loc_section);
22517       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22518                                    DEBUG_LOC_SECTION_LABEL, 0);
22519       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22520       output_location_lists (comp_unit_die ());
22521     }
22522
22523   /* Output public names and types tables if necessary.  */
22524   output_pubnames (pubname_table);
22525   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22526      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22527      simply won't look for the section.  */
22528   output_pubnames (pubtype_table);
22529
22530   /* Output the address range information if a CU (.debug_info section)
22531      was emitted.  We output an empty table even if we had no functions
22532      to put in it.  This because the consumer has no way to tell the
22533      difference between an empty table that we omitted and failure to
22534      generate a table that would have contained data.  */
22535   if (info_section_emitted)
22536     {
22537       unsigned long aranges_length = size_of_aranges ();
22538
22539       switch_to_section (debug_aranges_section);
22540       output_aranges (aranges_length);
22541     }
22542
22543   /* Output ranges section if necessary.  */
22544   if (ranges_table_in_use)
22545     {
22546       switch_to_section (debug_ranges_section);
22547       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22548       output_ranges ();
22549     }
22550
22551   /* Have to end the macro section.  */
22552   if (have_macinfo)
22553     {
22554       switch_to_section (debug_macinfo_section);
22555       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22556       output_macinfo ();
22557       dw2_asm_output_data (1, 0, "End compilation unit");
22558     }
22559
22560   /* Output the source line correspondence table.  We must do this
22561      even if there is no line information.  Otherwise, on an empty
22562      translation unit, we will generate a present, but empty,
22563      .debug_info section.  IRIX 6.5 `nm' will then complain when
22564      examining the file.  This is done late so that any filenames
22565      used by the debug_info section are marked as 'used'.  */
22566   switch_to_section (debug_line_section);
22567   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22568   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22569     output_line_info ();
22570
22571   /* If we emitted any DW_FORM_strp form attribute, output the string
22572      table too.  */
22573   if (debug_str_hash)
22574     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22575 }
22576
22577 #include "gt-dwarf2out.h"