OSDN Git Service

64695060c3b0a9b18c1b81601bdde01dab47a43c
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically.  They've been
118    totally replaced in this file; make sure it stays that way.  */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 /* The size of the target's pointer type.  */
126 #ifndef PTR_SIZE
127 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
128 #endif
129
130 /* Array of RTXes referenced by the debugging information, which therefore
131    must be kept around forever.  */
132 static GTY(()) VEC(rtx,gc) *used_rtx_array;
133
134 /* A pointer to the base of a list of incomplete types which might be
135    completed at some later time.  incomplete_types_list needs to be a
136    VEC(tree,gc) because we want to tell the garbage collector about
137    it.  */
138 static GTY(()) VEC(tree,gc) *incomplete_types;
139
140 /* A pointer to the base of a table of references to declaration
141    scopes.  This table is a display which tracks the nesting
142    of declaration scopes at the current scope and containing
143    scopes.  This table is used to find the proper place to
144    define type declaration DIE's.  */
145 static GTY(()) VEC(tree,gc) *decl_scope_table;
146
147 /* Pointers to various DWARF2 sections.  */
148 static GTY(()) section *debug_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_aranges_section;
151 static GTY(()) section *debug_macinfo_section;
152 static GTY(()) section *debug_line_section;
153 static GTY(()) section *debug_loc_section;
154 static GTY(()) section *debug_pubnames_section;
155 static GTY(()) section *debug_pubtypes_section;
156 static GTY(()) section *debug_str_section;
157 static GTY(()) section *debug_ranges_section;
158 static GTY(()) section *debug_frame_section;
159
160 /* Maximum size (in bytes) of an artificially generated label.  */
161 #define MAX_ARTIFICIAL_LABEL_BYTES      30
162
163 /* According to the (draft) DWARF 3 specification, the initial length
164    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
165    bytes are 0xffffffff, followed by the length stored in the next 8
166    bytes.
167
168    However, the SGI/MIPS ABI uses an initial length which is equal to
169    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
170
171 #ifndef DWARF_INITIAL_LENGTH_SIZE
172 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
173 #endif
174
175 /* Round SIZE up to the nearest BOUNDARY.  */
176 #define DWARF_ROUND(SIZE,BOUNDARY) \
177   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
178
179 /* CIE identifier.  */
180 #if HOST_BITS_PER_WIDE_INT >= 64
181 #define DWARF_CIE_ID \
182   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
183 #else
184 #define DWARF_CIE_ID DW_CIE_ID
185 #endif
186
187 DEF_VEC_P (dw_fde_ref);
188 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
189
190 /* A vector for a table that contains frame description
191    information for each routine.  */
192 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
193
194 struct GTY(()) indirect_string_node {
195   const char *str;
196   unsigned int refcount;
197   enum dwarf_form form;
198   char *label;
199 };
200
201 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
202
203 static GTY(()) int dw2_string_counter;
204
205 /* True if the compilation unit places functions in more than one section.  */
206 static GTY(()) bool have_multiple_function_sections = false;
207
208 /* Whether the default text and cold text sections have been used at all.  */
209
210 static GTY(()) bool text_section_used = false;
211 static GTY(()) bool cold_text_section_used = false;
212
213 /* The default cold text section.  */
214 static GTY(()) section *cold_text_section;
215
216 /* Forward declarations for functions defined in this file.  */
217
218 static char *stripattributes (const char *);
219 static void output_call_frame_info (int);
220 static void dwarf2out_note_section_used (void);
221
222 /* Personality decl of current unit.  Used only when assembler does not support
223    personality CFI.  */
224 static GTY(()) rtx current_unit_personality;
225
226 /* Data and reference forms for relocatable data.  */
227 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
228 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
229
230 #ifndef DEBUG_FRAME_SECTION
231 #define DEBUG_FRAME_SECTION     ".debug_frame"
232 #endif
233
234 #ifndef FUNC_BEGIN_LABEL
235 #define FUNC_BEGIN_LABEL        "LFB"
236 #endif
237
238 #ifndef FUNC_END_LABEL
239 #define FUNC_END_LABEL          "LFE"
240 #endif
241
242 #ifndef PROLOGUE_END_LABEL
243 #define PROLOGUE_END_LABEL      "LPE"
244 #endif
245
246 #ifndef EPILOGUE_BEGIN_LABEL
247 #define EPILOGUE_BEGIN_LABEL    "LEB"
248 #endif
249
250 #ifndef FRAME_BEGIN_LABEL
251 #define FRAME_BEGIN_LABEL       "Lframe"
252 #endif
253 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
254 #define CIE_END_LABEL           "LECIE"
255 #define FDE_LABEL               "LSFDE"
256 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
257 #define FDE_END_LABEL           "LEFDE"
258 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
259 #define LINE_NUMBER_END_LABEL   "LELT"
260 #define LN_PROLOG_AS_LABEL      "LASLTP"
261 #define LN_PROLOG_END_LABEL     "LELTP"
262 #define DIE_LABEL_PREFIX        "DW"
263 \f
264 /* Match the base name of a file to the base name of a compilation unit. */
265
266 static int
267 matches_main_base (const char *path)
268 {
269   /* Cache the last query. */
270   static const char *last_path = NULL;
271   static int last_match = 0;
272   if (path != last_path)
273     {
274       const char *base;
275       int length = base_of_path (path, &base);
276       last_path = path;
277       last_match = (length == main_input_baselength
278                     && memcmp (base, main_input_basename, length) == 0);
279     }
280   return last_match;
281 }
282
283 #ifdef DEBUG_DEBUG_STRUCT
284
285 static int
286 dump_struct_debug (tree type, enum debug_info_usage usage,
287                    enum debug_struct_file criterion, int generic,
288                    int matches, int result)
289 {
290   /* Find the type name. */
291   tree type_decl = TYPE_STUB_DECL (type);
292   tree t = type_decl;
293   const char *name = 0;
294   if (TREE_CODE (t) == TYPE_DECL)
295     t = DECL_NAME (t);
296   if (t)
297     name = IDENTIFIER_POINTER (t);
298
299   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
300            criterion,
301            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
302            matches ? "bas" : "hdr",
303            generic ? "gen" : "ord",
304            usage == DINFO_USAGE_DFN ? ";" :
305              usage == DINFO_USAGE_DIR_USE ? "." : "*",
306            result,
307            (void*) type_decl, name);
308   return result;
309 }
310 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
311   dump_struct_debug (type, usage, criterion, generic, matches, result)
312
313 #else
314
315 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
316   (result)
317
318 #endif
319
320 static bool
321 should_emit_struct_debug (tree type, enum debug_info_usage usage)
322 {
323   enum debug_struct_file criterion;
324   tree type_decl;
325   bool generic = lang_hooks.types.generic_p (type);
326
327   if (generic)
328     criterion = debug_struct_generic[usage];
329   else
330     criterion = debug_struct_ordinary[usage];
331
332   if (criterion == DINFO_STRUCT_FILE_NONE)
333     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
334   if (criterion == DINFO_STRUCT_FILE_ANY)
335     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
336
337   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
338
339   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
340     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
341
342   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
343     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
344   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
345 }
346 \f
347 /* Return a pointer to a copy of the section string name S with all
348    attributes stripped off, and an asterisk prepended (for assemble_name).  */
349
350 static inline char *
351 stripattributes (const char *s)
352 {
353   char *stripped = XNEWVEC (char, strlen (s) + 2);
354   char *p = stripped;
355
356   *p++ = '*';
357
358   while (*s && *s != ',')
359     *p++ = *s++;
360
361   *p = '\0';
362   return stripped;
363 }
364
365 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
366    switch to the data section instead, and write out a synthetic start label
367    for collect2 the first time around.  */
368
369 static void
370 switch_to_eh_frame_section (bool back)
371 {
372   tree label;
373
374 #ifdef EH_FRAME_SECTION_NAME
375   if (eh_frame_section == 0)
376     {
377       int flags;
378
379       if (EH_TABLES_CAN_BE_READ_ONLY)
380         {
381           int fde_encoding;
382           int per_encoding;
383           int lsda_encoding;
384
385           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
386                                                        /*global=*/0);
387           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
388                                                        /*global=*/1);
389           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
390                                                         /*global=*/0);
391           flags = ((! flag_pic
392                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
393                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
394                         && (per_encoding & 0x70) != DW_EH_PE_absptr
395                         && (per_encoding & 0x70) != DW_EH_PE_aligned
396                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
397                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
398                    ? 0 : SECTION_WRITE);
399         }
400       else
401         flags = SECTION_WRITE;
402       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
403     }
404 #endif /* EH_FRAME_SECTION_NAME */
405
406   if (eh_frame_section)
407     switch_to_section (eh_frame_section);
408   else
409     {
410       /* We have no special eh_frame section.  Put the information in
411          the data section and emit special labels to guide collect2.  */
412       switch_to_section (data_section);
413
414       if (!back)
415         {
416           label = get_file_function_name ("F");
417           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
418           targetm.asm_out.globalize_label (asm_out_file,
419                                            IDENTIFIER_POINTER (label));
420           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
421         }
422     }
423 }
424
425 /* Switch [BACK] to the eh or debug frame table section, depending on
426    FOR_EH.  */
427
428 static void
429 switch_to_frame_table_section (int for_eh, bool back)
430 {
431   if (for_eh)
432     switch_to_eh_frame_section (back);
433   else
434     {
435       if (!debug_frame_section)
436         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
437                                            SECTION_DEBUG, NULL);
438       switch_to_section (debug_frame_section);
439     }
440 }
441
442 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
443
444 enum dw_cfi_oprnd_type
445 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
446 {
447   switch (cfi)
448     {
449     case DW_CFA_nop:
450     case DW_CFA_GNU_window_save:
451     case DW_CFA_remember_state:
452     case DW_CFA_restore_state:
453       return dw_cfi_oprnd_unused;
454
455     case DW_CFA_set_loc:
456     case DW_CFA_advance_loc1:
457     case DW_CFA_advance_loc2:
458     case DW_CFA_advance_loc4:
459     case DW_CFA_MIPS_advance_loc8:
460       return dw_cfi_oprnd_addr;
461
462     case DW_CFA_offset:
463     case DW_CFA_offset_extended:
464     case DW_CFA_def_cfa:
465     case DW_CFA_offset_extended_sf:
466     case DW_CFA_def_cfa_sf:
467     case DW_CFA_restore:
468     case DW_CFA_restore_extended:
469     case DW_CFA_undefined:
470     case DW_CFA_same_value:
471     case DW_CFA_def_cfa_register:
472     case DW_CFA_register:
473     case DW_CFA_expression:
474       return dw_cfi_oprnd_reg_num;
475
476     case DW_CFA_def_cfa_offset:
477     case DW_CFA_GNU_args_size:
478     case DW_CFA_def_cfa_offset_sf:
479       return dw_cfi_oprnd_offset;
480
481     case DW_CFA_def_cfa_expression:
482       return dw_cfi_oprnd_loc;
483
484     default:
485       gcc_unreachable ();
486     }
487 }
488
489 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
490
491 enum dw_cfi_oprnd_type
492 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
493 {
494   switch (cfi)
495     {
496     case DW_CFA_def_cfa:
497     case DW_CFA_def_cfa_sf:
498     case DW_CFA_offset:
499     case DW_CFA_offset_extended_sf:
500     case DW_CFA_offset_extended:
501       return dw_cfi_oprnd_offset;
502
503     case DW_CFA_register:
504       return dw_cfi_oprnd_reg_num;
505
506     case DW_CFA_expression:
507       return dw_cfi_oprnd_loc;
508
509     default:
510       return dw_cfi_oprnd_unused;
511     }
512 }
513
514 /* Output one FDE.  */
515
516 static void
517 output_fde (dw_fde_ref fde, bool for_eh, bool second,
518             char *section_start_label, int fde_encoding, char *augmentation,
519             bool any_lsda_needed, int lsda_encoding)
520 {
521   int ix;
522   const char *begin, *end;
523   static unsigned int j;
524   char l1[20], l2[20];
525   dw_cfi_ref cfi;
526
527   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
528                                      /* empty */ 0);
529   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
530                                   for_eh + j);
531   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
532   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
533   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
534     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
535                          " indicating 64-bit DWARF extension");
536   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
537                         "FDE Length");
538   ASM_OUTPUT_LABEL (asm_out_file, l1);
539
540   if (for_eh)
541     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
542   else
543     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
544                            debug_frame_section, "FDE CIE offset");
545
546   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
547   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
548
549   if (for_eh)
550     {
551       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
552       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
553       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
554                                        "FDE initial location");
555       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
556                             end, begin, "FDE address range");
557     }
558   else
559     {
560       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
561       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
562     }
563
564   if (augmentation[0])
565     {
566       if (any_lsda_needed)
567         {
568           int size = size_of_encoded_value (lsda_encoding);
569
570           if (lsda_encoding == DW_EH_PE_aligned)
571             {
572               int offset = (  4         /* Length */
573                             + 4         /* CIE offset */
574                             + 2 * size_of_encoded_value (fde_encoding)
575                             + 1         /* Augmentation size */ );
576               int pad = -offset & (PTR_SIZE - 1);
577
578               size += pad;
579               gcc_assert (size_of_uleb128 (size) == 1);
580             }
581
582           dw2_asm_output_data_uleb128 (size, "Augmentation size");
583
584           if (fde->uses_eh_lsda)
585             {
586               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
587                                            fde->funcdef_number);
588               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
589                                                gen_rtx_SYMBOL_REF (Pmode, l1),
590                                                false,
591                                                "Language Specific Data Area");
592             }
593           else
594             {
595               if (lsda_encoding == DW_EH_PE_aligned)
596                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
597               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
598                                    "Language Specific Data Area (none)");
599             }
600         }
601       else
602         dw2_asm_output_data_uleb128 (0, "Augmentation size");
603     }
604
605   /* Loop through the Call Frame Instructions associated with
606      this FDE.  */
607   fde->dw_fde_current_label = begin;
608   if (fde->dw_fde_second_begin == NULL)
609     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
610       output_cfi (cfi, fde, for_eh);
611   else if (!second)
612     {
613       if (fde->dw_fde_switch_cfi_index > 0)
614         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
615           {
616             if (ix == fde->dw_fde_switch_cfi_index)
617               break;
618             output_cfi (cfi, fde, for_eh);
619           }
620     }
621   else
622     {
623       int i, from = 0;
624       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
625
626       if (fde->dw_fde_switch_cfi_index > 0)
627         {
628           from = fde->dw_fde_switch_cfi_index;
629           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
630         }
631       for (i = from; i < until; i++)
632         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
633                     fde, for_eh);
634     }
635
636   /* If we are to emit a ref/link from function bodies to their frame tables,
637      do it now.  This is typically performed to make sure that tables
638      associated with functions are dragged with them and not discarded in
639      garbage collecting links. We need to do this on a per function basis to
640      cope with -ffunction-sections.  */
641
642 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
643   /* Switch to the function section, emit the ref to the tables, and
644      switch *back* into the table section.  */
645   switch_to_section (function_section (fde->decl));
646   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
647   switch_to_frame_table_section (for_eh, true);
648 #endif
649
650   /* Pad the FDE out to an address sized boundary.  */
651   ASM_OUTPUT_ALIGN (asm_out_file,
652                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
653   ASM_OUTPUT_LABEL (asm_out_file, l2);
654
655   j += 2;
656 }
657
658 /* Return true if frame description entry FDE is needed for EH.  */
659
660 static bool
661 fde_needed_for_eh_p (dw_fde_ref fde)
662 {
663   if (flag_asynchronous_unwind_tables)
664     return true;
665
666   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
667     return true;
668
669   if (fde->uses_eh_lsda)
670     return true;
671
672   /* If exceptions are enabled, we have collected nothrow info.  */
673   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
674     return false;
675
676   return true;
677 }
678
679 /* Output the call frame information used to record information
680    that relates to calculating the frame pointer, and records the
681    location of saved registers.  */
682
683 static void
684 output_call_frame_info (int for_eh)
685 {
686   unsigned int i;
687   dw_fde_ref fde;
688   dw_cfi_ref cfi;
689   char l1[20], l2[20], section_start_label[20];
690   bool any_lsda_needed = false;
691   char augmentation[6];
692   int augmentation_size;
693   int fde_encoding = DW_EH_PE_absptr;
694   int per_encoding = DW_EH_PE_absptr;
695   int lsda_encoding = DW_EH_PE_absptr;
696   int return_reg;
697   rtx personality = NULL;
698   int dw_cie_version;
699
700   /* Don't emit a CIE if there won't be any FDEs.  */
701   if (fde_vec == NULL)
702     return;
703
704   /* Nothing to do if the assembler's doing it all.  */
705   if (dwarf2out_do_cfi_asm ())
706     return;
707
708   /* If we don't have any functions we'll want to unwind out of, don't emit
709      any EH unwind information.  If we make FDEs linkonce, we may have to
710      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
711      want to avoid having an FDE kept around when the function it refers to
712      is discarded.  Example where this matters: a primary function template
713      in C++ requires EH information, an explicit specialization doesn't.  */
714   if (for_eh)
715     {
716       bool any_eh_needed = false;
717
718       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
719         {
720           if (fde->uses_eh_lsda)
721             any_eh_needed = any_lsda_needed = true;
722           else if (fde_needed_for_eh_p (fde))
723             any_eh_needed = true;
724           else if (TARGET_USES_WEAK_UNWIND_INFO)
725             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
726         }
727
728       if (!any_eh_needed)
729         return;
730     }
731
732   /* We're going to be generating comments, so turn on app.  */
733   if (flag_debug_asm)
734     app_enable ();
735
736   /* Switch to the proper frame section, first time.  */
737   switch_to_frame_table_section (for_eh, false);
738
739   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
740   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
741
742   /* Output the CIE.  */
743   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
744   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
745   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
746     dw2_asm_output_data (4, 0xffffffff,
747       "Initial length escape value indicating 64-bit DWARF extension");
748   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
749                         "Length of Common Information Entry");
750   ASM_OUTPUT_LABEL (asm_out_file, l1);
751
752   /* Now that the CIE pointer is PC-relative for EH,
753      use 0 to identify the CIE.  */
754   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
755                        (for_eh ? 0 : DWARF_CIE_ID),
756                        "CIE Identifier Tag");
757
758   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
759      use CIE version 1, unless that would produce incorrect results
760      due to overflowing the return register column.  */
761   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
762   dw_cie_version = 1;
763   if (return_reg >= 256 || dwarf_version > 2)
764     dw_cie_version = 3;
765   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
766
767   augmentation[0] = 0;
768   augmentation_size = 0;
769
770   personality = current_unit_personality;
771   if (for_eh)
772     {
773       char *p;
774
775       /* Augmentation:
776          z      Indicates that a uleb128 is present to size the
777                 augmentation section.
778          L      Indicates the encoding (and thus presence) of
779                 an LSDA pointer in the FDE augmentation.
780          R      Indicates a non-default pointer encoding for
781                 FDE code pointers.
782          P      Indicates the presence of an encoding + language
783                 personality routine in the CIE augmentation.  */
784
785       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
786       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
787       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
788
789       p = augmentation + 1;
790       if (personality)
791         {
792           *p++ = 'P';
793           augmentation_size += 1 + size_of_encoded_value (per_encoding);
794           assemble_external_libcall (personality);
795         }
796       if (any_lsda_needed)
797         {
798           *p++ = 'L';
799           augmentation_size += 1;
800         }
801       if (fde_encoding != DW_EH_PE_absptr)
802         {
803           *p++ = 'R';
804           augmentation_size += 1;
805         }
806       if (p > augmentation + 1)
807         {
808           augmentation[0] = 'z';
809           *p = '\0';
810         }
811
812       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
813       if (personality && per_encoding == DW_EH_PE_aligned)
814         {
815           int offset = (  4             /* Length */
816                         + 4             /* CIE Id */
817                         + 1             /* CIE version */
818                         + strlen (augmentation) + 1     /* Augmentation */
819                         + size_of_uleb128 (1)           /* Code alignment */
820                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
821                         + 1             /* RA column */
822                         + 1             /* Augmentation size */
823                         + 1             /* Personality encoding */ );
824           int pad = -offset & (PTR_SIZE - 1);
825
826           augmentation_size += pad;
827
828           /* Augmentations should be small, so there's scarce need to
829              iterate for a solution.  Die if we exceed one uleb128 byte.  */
830           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
831         }
832     }
833
834   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
835   if (dw_cie_version >= 4)
836     {
837       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
838       dw2_asm_output_data (1, 0, "CIE Segment Size");
839     }
840   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
841   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
842                                "CIE Data Alignment Factor");
843
844   if (dw_cie_version == 1)
845     dw2_asm_output_data (1, return_reg, "CIE RA Column");
846   else
847     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
848
849   if (augmentation[0])
850     {
851       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
852       if (personality)
853         {
854           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
855                                eh_data_format_name (per_encoding));
856           dw2_asm_output_encoded_addr_rtx (per_encoding,
857                                            personality,
858                                            true, NULL);
859         }
860
861       if (any_lsda_needed)
862         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
863                              eh_data_format_name (lsda_encoding));
864
865       if (fde_encoding != DW_EH_PE_absptr)
866         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
867                              eh_data_format_name (fde_encoding));
868     }
869
870   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
871     output_cfi (cfi, NULL, for_eh);
872
873   /* Pad the CIE out to an address sized boundary.  */
874   ASM_OUTPUT_ALIGN (asm_out_file,
875                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
876   ASM_OUTPUT_LABEL (asm_out_file, l2);
877
878   /* Loop through all of the FDE's.  */
879   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
880     {
881       unsigned int k;
882
883       /* Don't emit EH unwind info for leaf functions that don't need it.  */
884       if (for_eh && !fde_needed_for_eh_p (fde))
885         continue;
886
887       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
888         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
889                     augmentation, any_lsda_needed, lsda_encoding);
890     }
891
892   if (for_eh && targetm.terminate_dw2_eh_frame_info)
893     dw2_asm_output_data (4, 0, "End of Table");
894 #ifdef MIPS_DEBUGGING_INFO
895   /* Work around Irix 6 assembler bug whereby labels at the end of a section
896      get a value of 0.  Putting .align 0 after the label fixes it.  */
897   ASM_OUTPUT_ALIGN (asm_out_file, 0);
898 #endif
899
900   /* Turn off app to make assembly quicker.  */
901   if (flag_debug_asm)
902     app_disable ();
903 }
904
905 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
906
907 static void
908 dwarf2out_do_cfi_startproc (bool second)
909 {
910   int enc;
911   rtx ref;
912   rtx personality = get_personality_function (current_function_decl);
913
914   fprintf (asm_out_file, "\t.cfi_startproc\n");
915
916   if (personality)
917     {
918       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
919       ref = personality;
920
921       /* ??? The GAS support isn't entirely consistent.  We have to
922          handle indirect support ourselves, but PC-relative is done
923          in the assembler.  Further, the assembler can't handle any
924          of the weirder relocation types.  */
925       if (enc & DW_EH_PE_indirect)
926         ref = dw2_force_const_mem (ref, true);
927
928       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
929       output_addr_const (asm_out_file, ref);
930       fputc ('\n', asm_out_file);
931     }
932
933   if (crtl->uses_eh_lsda)
934     {
935       char lab[20];
936
937       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
938       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
939                                    current_function_funcdef_no);
940       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
941       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
942
943       if (enc & DW_EH_PE_indirect)
944         ref = dw2_force_const_mem (ref, true);
945
946       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
947       output_addr_const (asm_out_file, ref);
948       fputc ('\n', asm_out_file);
949     }
950 }
951
952 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
953    this allocation may be done before pass_final.  */
954
955 dw_fde_ref
956 dwarf2out_alloc_current_fde (void)
957 {
958   dw_fde_ref fde;
959
960   fde = ggc_alloc_cleared_dw_fde_node ();
961   fde->decl = current_function_decl;
962   fde->funcdef_number = current_function_funcdef_no;
963   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
964   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
965   fde->uses_eh_lsda = crtl->uses_eh_lsda;
966   fde->nothrow = crtl->nothrow;
967   fde->drap_reg = INVALID_REGNUM;
968   fde->vdrap_reg = INVALID_REGNUM;
969
970   /* Record the FDE associated with this function.  */
971   cfun->fde = fde;
972   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
973
974   return fde;
975 }
976
977 /* Output a marker (i.e. a label) for the beginning of a function, before
978    the prologue.  */
979
980 void
981 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
982                           const char *file ATTRIBUTE_UNUSED)
983 {
984   char label[MAX_ARTIFICIAL_LABEL_BYTES];
985   char * dup_label;
986   dw_fde_ref fde;
987   section *fnsec;
988   bool do_frame;
989
990   current_function_func_begin_label = NULL;
991
992   do_frame = dwarf2out_do_frame ();
993
994   /* ??? current_function_func_begin_label is also used by except.c for
995      call-site information.  We must emit this label if it might be used.  */
996   if (!do_frame
997       && (!flag_exceptions
998           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
999     return;
1000
1001   fnsec = function_section (current_function_decl);
1002   switch_to_section (fnsec);
1003   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1004                                current_function_funcdef_no);
1005   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1006                           current_function_funcdef_no);
1007   dup_label = xstrdup (label);
1008   current_function_func_begin_label = dup_label;
1009
1010   /* We can elide the fde allocation if we're not emitting debug info.  */
1011   if (!do_frame)
1012     return;
1013
1014   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1015      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1016      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1017      do so now.  */
1018   fde = cfun->fde;
1019   if (fde == NULL)
1020     fde = dwarf2out_alloc_current_fde ();
1021
1022   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1023   fde->dw_fde_begin = dup_label;
1024   fde->dw_fde_current_label = dup_label;
1025   fde->in_std_section = (fnsec == text_section
1026                          || (cold_text_section && fnsec == cold_text_section));
1027
1028   /* We only want to output line number information for the genuine dwarf2
1029      prologue case, not the eh frame case.  */
1030 #ifdef DWARF2_DEBUGGING_INFO
1031   if (file)
1032     dwarf2out_source_line (line, file, 0, true);
1033 #endif
1034
1035   if (dwarf2out_do_cfi_asm ())
1036     dwarf2out_do_cfi_startproc (false);
1037   else
1038     {
1039       rtx personality = get_personality_function (current_function_decl);
1040       if (!current_unit_personality)
1041         current_unit_personality = personality;
1042
1043       /* We cannot keep a current personality per function as without CFI
1044          asm, at the point where we emit the CFI data, there is no current
1045          function anymore.  */
1046       if (personality && current_unit_personality != personality)
1047         sorry ("multiple EH personalities are supported only with assemblers "
1048                "supporting .cfi_personality directive");
1049     }
1050 }
1051
1052 /* Output a marker (i.e. a label) for the end of the generated code
1053    for a function prologue.  This gets called *after* the prologue code has
1054    been generated.  */
1055
1056 void
1057 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1058                         const char *file ATTRIBUTE_UNUSED)
1059 {
1060   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1061
1062   /* Output a label to mark the endpoint of the code generated for this
1063      function.  */
1064   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1065                                current_function_funcdef_no);
1066   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1067                           current_function_funcdef_no);
1068   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1069 }
1070
1071 /* Output a marker (i.e. a label) for the beginning of the generated code
1072    for a function epilogue.  This gets called *before* the prologue code has
1073    been generated.  */
1074
1075 void
1076 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1077                           const char *file ATTRIBUTE_UNUSED)
1078 {
1079   dw_fde_ref fde = cfun->fde;
1080   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1081
1082   if (fde->dw_fde_vms_begin_epilogue)
1083     return;
1084
1085   /* Output a label to mark the endpoint of the code generated for this
1086      function.  */
1087   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1088                                current_function_funcdef_no);
1089   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1090                           current_function_funcdef_no);
1091   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1092 }
1093
1094 /* Output a marker (i.e. a label) for the absolute end of the generated code
1095    for a function definition.  This gets called *after* the epilogue code has
1096    been generated.  */
1097
1098 void
1099 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1100                         const char *file ATTRIBUTE_UNUSED)
1101 {
1102   dw_fde_ref fde;
1103   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1104
1105   last_var_location_insn = NULL_RTX;
1106
1107   if (dwarf2out_do_cfi_asm ())
1108     fprintf (asm_out_file, "\t.cfi_endproc\n");
1109
1110   /* Output a label to mark the endpoint of the code generated for this
1111      function.  */
1112   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1113                                current_function_funcdef_no);
1114   ASM_OUTPUT_LABEL (asm_out_file, label);
1115   fde = cfun->fde;
1116   gcc_assert (fde != NULL);
1117   if (fde->dw_fde_second_begin == NULL)
1118     fde->dw_fde_end = xstrdup (label);
1119 }
1120
1121 void
1122 dwarf2out_frame_finish (void)
1123 {
1124   /* Output call frame information.  */
1125   if (targetm.debug_unwind_info () == UI_DWARF2)
1126     output_call_frame_info (0);
1127
1128   /* Output another copy for the unwinder.  */
1129   if ((flag_unwind_tables || flag_exceptions)
1130       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1131     output_call_frame_info (1);
1132 }
1133
1134 /* Note that the current function section is being used for code.  */
1135
1136 static void
1137 dwarf2out_note_section_used (void)
1138 {
1139   section *sec = current_function_section ();
1140   if (sec == text_section)
1141     text_section_used = true;
1142   else if (sec == cold_text_section)
1143     cold_text_section_used = true;
1144 }
1145
1146 static void var_location_switch_text_section (void);
1147 static void set_cur_line_info_table (section *);
1148
1149 void
1150 dwarf2out_switch_text_section (void)
1151 {
1152   section *sect;
1153   dw_fde_ref fde = cfun->fde;
1154
1155   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1156
1157   if (!in_cold_section_p)
1158     {
1159       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1160       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1161       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1162     }
1163   else
1164     {
1165       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1166       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1167       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1168     }
1169   have_multiple_function_sections = true;
1170
1171   /* There is no need to mark used sections when not debugging.  */
1172   if (cold_text_section != NULL)
1173     dwarf2out_note_section_used ();
1174
1175   if (dwarf2out_do_cfi_asm ())
1176     fprintf (asm_out_file, "\t.cfi_endproc\n");
1177
1178   /* Now do the real section switch.  */
1179   sect = current_function_section ();
1180   switch_to_section (sect);
1181
1182   fde->second_in_std_section
1183     = (sect == text_section
1184        || (cold_text_section && sect == cold_text_section));
1185
1186   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1187
1188   if (dwarf2out_do_cfi_asm ())
1189     {
1190       dwarf2out_do_cfi_startproc (true);
1191       /* As this is a different FDE, insert all current CFI instructions
1192          again.  */
1193       output_cfis (fde->dw_fde_cfi, fde->dw_fde_switch_cfi_index,
1194                    true, fde, true);
1195     }
1196
1197   var_location_switch_text_section ();
1198
1199   set_cur_line_info_table (sect);
1200 }
1201 \f
1202 /* And now, the subset of the debugging information support code necessary
1203    for emitting location expressions.  */
1204
1205 /* Data about a single source file.  */
1206 struct GTY(()) dwarf_file_data {
1207   const char * filename;
1208   int emitted_number;
1209 };
1210
1211 typedef struct GTY(()) deferred_locations_struct
1212 {
1213   tree variable;
1214   dw_die_ref die;
1215 } deferred_locations;
1216
1217 DEF_VEC_O(deferred_locations);
1218 DEF_VEC_ALLOC_O(deferred_locations,gc);
1219
1220 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1221
1222 DEF_VEC_P(dw_die_ref);
1223 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1224
1225 /* Location lists are ranges + location descriptions for that range,
1226    so you can track variables that are in different places over
1227    their entire life.  */
1228 typedef struct GTY(()) dw_loc_list_struct {
1229   dw_loc_list_ref dw_loc_next;
1230   const char *begin; /* Label for begin address of range */
1231   const char *end;  /* Label for end address of range */
1232   char *ll_symbol; /* Label for beginning of location list.
1233                       Only on head of list */
1234   const char *section; /* Section this loclist is relative to */
1235   dw_loc_descr_ref expr;
1236   hashval_t hash;
1237   /* True if all addresses in this and subsequent lists are known to be
1238      resolved.  */
1239   bool resolved_addr;
1240   /* True if this list has been replaced by dw_loc_next.  */
1241   bool replaced;
1242   bool emitted;
1243   /* True if the range should be emitted even if begin and end
1244      are the same.  */
1245   bool force;
1246 } dw_loc_list_node;
1247
1248 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1249
1250 /* Convert a DWARF stack opcode into its string name.  */
1251
1252 static const char *
1253 dwarf_stack_op_name (unsigned int op)
1254 {
1255   switch (op)
1256     {
1257     case DW_OP_addr:
1258       return "DW_OP_addr";
1259     case DW_OP_deref:
1260       return "DW_OP_deref";
1261     case DW_OP_const1u:
1262       return "DW_OP_const1u";
1263     case DW_OP_const1s:
1264       return "DW_OP_const1s";
1265     case DW_OP_const2u:
1266       return "DW_OP_const2u";
1267     case DW_OP_const2s:
1268       return "DW_OP_const2s";
1269     case DW_OP_const4u:
1270       return "DW_OP_const4u";
1271     case DW_OP_const4s:
1272       return "DW_OP_const4s";
1273     case DW_OP_const8u:
1274       return "DW_OP_const8u";
1275     case DW_OP_const8s:
1276       return "DW_OP_const8s";
1277     case DW_OP_constu:
1278       return "DW_OP_constu";
1279     case DW_OP_consts:
1280       return "DW_OP_consts";
1281     case DW_OP_dup:
1282       return "DW_OP_dup";
1283     case DW_OP_drop:
1284       return "DW_OP_drop";
1285     case DW_OP_over:
1286       return "DW_OP_over";
1287     case DW_OP_pick:
1288       return "DW_OP_pick";
1289     case DW_OP_swap:
1290       return "DW_OP_swap";
1291     case DW_OP_rot:
1292       return "DW_OP_rot";
1293     case DW_OP_xderef:
1294       return "DW_OP_xderef";
1295     case DW_OP_abs:
1296       return "DW_OP_abs";
1297     case DW_OP_and:
1298       return "DW_OP_and";
1299     case DW_OP_div:
1300       return "DW_OP_div";
1301     case DW_OP_minus:
1302       return "DW_OP_minus";
1303     case DW_OP_mod:
1304       return "DW_OP_mod";
1305     case DW_OP_mul:
1306       return "DW_OP_mul";
1307     case DW_OP_neg:
1308       return "DW_OP_neg";
1309     case DW_OP_not:
1310       return "DW_OP_not";
1311     case DW_OP_or:
1312       return "DW_OP_or";
1313     case DW_OP_plus:
1314       return "DW_OP_plus";
1315     case DW_OP_plus_uconst:
1316       return "DW_OP_plus_uconst";
1317     case DW_OP_shl:
1318       return "DW_OP_shl";
1319     case DW_OP_shr:
1320       return "DW_OP_shr";
1321     case DW_OP_shra:
1322       return "DW_OP_shra";
1323     case DW_OP_xor:
1324       return "DW_OP_xor";
1325     case DW_OP_bra:
1326       return "DW_OP_bra";
1327     case DW_OP_eq:
1328       return "DW_OP_eq";
1329     case DW_OP_ge:
1330       return "DW_OP_ge";
1331     case DW_OP_gt:
1332       return "DW_OP_gt";
1333     case DW_OP_le:
1334       return "DW_OP_le";
1335     case DW_OP_lt:
1336       return "DW_OP_lt";
1337     case DW_OP_ne:
1338       return "DW_OP_ne";
1339     case DW_OP_skip:
1340       return "DW_OP_skip";
1341     case DW_OP_lit0:
1342       return "DW_OP_lit0";
1343     case DW_OP_lit1:
1344       return "DW_OP_lit1";
1345     case DW_OP_lit2:
1346       return "DW_OP_lit2";
1347     case DW_OP_lit3:
1348       return "DW_OP_lit3";
1349     case DW_OP_lit4:
1350       return "DW_OP_lit4";
1351     case DW_OP_lit5:
1352       return "DW_OP_lit5";
1353     case DW_OP_lit6:
1354       return "DW_OP_lit6";
1355     case DW_OP_lit7:
1356       return "DW_OP_lit7";
1357     case DW_OP_lit8:
1358       return "DW_OP_lit8";
1359     case DW_OP_lit9:
1360       return "DW_OP_lit9";
1361     case DW_OP_lit10:
1362       return "DW_OP_lit10";
1363     case DW_OP_lit11:
1364       return "DW_OP_lit11";
1365     case DW_OP_lit12:
1366       return "DW_OP_lit12";
1367     case DW_OP_lit13:
1368       return "DW_OP_lit13";
1369     case DW_OP_lit14:
1370       return "DW_OP_lit14";
1371     case DW_OP_lit15:
1372       return "DW_OP_lit15";
1373     case DW_OP_lit16:
1374       return "DW_OP_lit16";
1375     case DW_OP_lit17:
1376       return "DW_OP_lit17";
1377     case DW_OP_lit18:
1378       return "DW_OP_lit18";
1379     case DW_OP_lit19:
1380       return "DW_OP_lit19";
1381     case DW_OP_lit20:
1382       return "DW_OP_lit20";
1383     case DW_OP_lit21:
1384       return "DW_OP_lit21";
1385     case DW_OP_lit22:
1386       return "DW_OP_lit22";
1387     case DW_OP_lit23:
1388       return "DW_OP_lit23";
1389     case DW_OP_lit24:
1390       return "DW_OP_lit24";
1391     case DW_OP_lit25:
1392       return "DW_OP_lit25";
1393     case DW_OP_lit26:
1394       return "DW_OP_lit26";
1395     case DW_OP_lit27:
1396       return "DW_OP_lit27";
1397     case DW_OP_lit28:
1398       return "DW_OP_lit28";
1399     case DW_OP_lit29:
1400       return "DW_OP_lit29";
1401     case DW_OP_lit30:
1402       return "DW_OP_lit30";
1403     case DW_OP_lit31:
1404       return "DW_OP_lit31";
1405     case DW_OP_reg0:
1406       return "DW_OP_reg0";
1407     case DW_OP_reg1:
1408       return "DW_OP_reg1";
1409     case DW_OP_reg2:
1410       return "DW_OP_reg2";
1411     case DW_OP_reg3:
1412       return "DW_OP_reg3";
1413     case DW_OP_reg4:
1414       return "DW_OP_reg4";
1415     case DW_OP_reg5:
1416       return "DW_OP_reg5";
1417     case DW_OP_reg6:
1418       return "DW_OP_reg6";
1419     case DW_OP_reg7:
1420       return "DW_OP_reg7";
1421     case DW_OP_reg8:
1422       return "DW_OP_reg8";
1423     case DW_OP_reg9:
1424       return "DW_OP_reg9";
1425     case DW_OP_reg10:
1426       return "DW_OP_reg10";
1427     case DW_OP_reg11:
1428       return "DW_OP_reg11";
1429     case DW_OP_reg12:
1430       return "DW_OP_reg12";
1431     case DW_OP_reg13:
1432       return "DW_OP_reg13";
1433     case DW_OP_reg14:
1434       return "DW_OP_reg14";
1435     case DW_OP_reg15:
1436       return "DW_OP_reg15";
1437     case DW_OP_reg16:
1438       return "DW_OP_reg16";
1439     case DW_OP_reg17:
1440       return "DW_OP_reg17";
1441     case DW_OP_reg18:
1442       return "DW_OP_reg18";
1443     case DW_OP_reg19:
1444       return "DW_OP_reg19";
1445     case DW_OP_reg20:
1446       return "DW_OP_reg20";
1447     case DW_OP_reg21:
1448       return "DW_OP_reg21";
1449     case DW_OP_reg22:
1450       return "DW_OP_reg22";
1451     case DW_OP_reg23:
1452       return "DW_OP_reg23";
1453     case DW_OP_reg24:
1454       return "DW_OP_reg24";
1455     case DW_OP_reg25:
1456       return "DW_OP_reg25";
1457     case DW_OP_reg26:
1458       return "DW_OP_reg26";
1459     case DW_OP_reg27:
1460       return "DW_OP_reg27";
1461     case DW_OP_reg28:
1462       return "DW_OP_reg28";
1463     case DW_OP_reg29:
1464       return "DW_OP_reg29";
1465     case DW_OP_reg30:
1466       return "DW_OP_reg30";
1467     case DW_OP_reg31:
1468       return "DW_OP_reg31";
1469     case DW_OP_breg0:
1470       return "DW_OP_breg0";
1471     case DW_OP_breg1:
1472       return "DW_OP_breg1";
1473     case DW_OP_breg2:
1474       return "DW_OP_breg2";
1475     case DW_OP_breg3:
1476       return "DW_OP_breg3";
1477     case DW_OP_breg4:
1478       return "DW_OP_breg4";
1479     case DW_OP_breg5:
1480       return "DW_OP_breg5";
1481     case DW_OP_breg6:
1482       return "DW_OP_breg6";
1483     case DW_OP_breg7:
1484       return "DW_OP_breg7";
1485     case DW_OP_breg8:
1486       return "DW_OP_breg8";
1487     case DW_OP_breg9:
1488       return "DW_OP_breg9";
1489     case DW_OP_breg10:
1490       return "DW_OP_breg10";
1491     case DW_OP_breg11:
1492       return "DW_OP_breg11";
1493     case DW_OP_breg12:
1494       return "DW_OP_breg12";
1495     case DW_OP_breg13:
1496       return "DW_OP_breg13";
1497     case DW_OP_breg14:
1498       return "DW_OP_breg14";
1499     case DW_OP_breg15:
1500       return "DW_OP_breg15";
1501     case DW_OP_breg16:
1502       return "DW_OP_breg16";
1503     case DW_OP_breg17:
1504       return "DW_OP_breg17";
1505     case DW_OP_breg18:
1506       return "DW_OP_breg18";
1507     case DW_OP_breg19:
1508       return "DW_OP_breg19";
1509     case DW_OP_breg20:
1510       return "DW_OP_breg20";
1511     case DW_OP_breg21:
1512       return "DW_OP_breg21";
1513     case DW_OP_breg22:
1514       return "DW_OP_breg22";
1515     case DW_OP_breg23:
1516       return "DW_OP_breg23";
1517     case DW_OP_breg24:
1518       return "DW_OP_breg24";
1519     case DW_OP_breg25:
1520       return "DW_OP_breg25";
1521     case DW_OP_breg26:
1522       return "DW_OP_breg26";
1523     case DW_OP_breg27:
1524       return "DW_OP_breg27";
1525     case DW_OP_breg28:
1526       return "DW_OP_breg28";
1527     case DW_OP_breg29:
1528       return "DW_OP_breg29";
1529     case DW_OP_breg30:
1530       return "DW_OP_breg30";
1531     case DW_OP_breg31:
1532       return "DW_OP_breg31";
1533     case DW_OP_regx:
1534       return "DW_OP_regx";
1535     case DW_OP_fbreg:
1536       return "DW_OP_fbreg";
1537     case DW_OP_bregx:
1538       return "DW_OP_bregx";
1539     case DW_OP_piece:
1540       return "DW_OP_piece";
1541     case DW_OP_deref_size:
1542       return "DW_OP_deref_size";
1543     case DW_OP_xderef_size:
1544       return "DW_OP_xderef_size";
1545     case DW_OP_nop:
1546       return "DW_OP_nop";
1547
1548     case DW_OP_push_object_address:
1549       return "DW_OP_push_object_address";
1550     case DW_OP_call2:
1551       return "DW_OP_call2";
1552     case DW_OP_call4:
1553       return "DW_OP_call4";
1554     case DW_OP_call_ref:
1555       return "DW_OP_call_ref";
1556     case DW_OP_implicit_value:
1557       return "DW_OP_implicit_value";
1558     case DW_OP_stack_value:
1559       return "DW_OP_stack_value";
1560     case DW_OP_form_tls_address:
1561       return "DW_OP_form_tls_address";
1562     case DW_OP_call_frame_cfa:
1563       return "DW_OP_call_frame_cfa";
1564     case DW_OP_bit_piece:
1565       return "DW_OP_bit_piece";
1566
1567     case DW_OP_GNU_push_tls_address:
1568       return "DW_OP_GNU_push_tls_address";
1569     case DW_OP_GNU_uninit:
1570       return "DW_OP_GNU_uninit";
1571     case DW_OP_GNU_encoded_addr:
1572       return "DW_OP_GNU_encoded_addr";
1573     case DW_OP_GNU_implicit_pointer:
1574       return "DW_OP_GNU_implicit_pointer";
1575     case DW_OP_GNU_entry_value:
1576       return "DW_OP_GNU_entry_value";
1577     case DW_OP_GNU_const_type:
1578       return "DW_OP_GNU_const_type";
1579     case DW_OP_GNU_regval_type:
1580       return "DW_OP_GNU_regval_type";
1581     case DW_OP_GNU_deref_type:
1582       return "DW_OP_GNU_deref_type";
1583     case DW_OP_GNU_convert:
1584       return "DW_OP_GNU_convert";
1585     case DW_OP_GNU_reinterpret:
1586       return "DW_OP_GNU_reinterpret";
1587     case DW_OP_GNU_parameter_ref:
1588       return "DW_OP_GNU_parameter_ref";
1589
1590     default:
1591       return "OP_<unknown>";
1592     }
1593 }
1594
1595 /* Return a pointer to a newly allocated location description.  Location
1596    descriptions are simple expression terms that can be strung
1597    together to form more complicated location (address) descriptions.  */
1598
1599 static inline dw_loc_descr_ref
1600 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1601                unsigned HOST_WIDE_INT oprnd2)
1602 {
1603   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1604
1605   descr->dw_loc_opc = op;
1606   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1607   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1608   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1609   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1610
1611   return descr;
1612 }
1613
1614 /* Return a pointer to a newly allocated location description for
1615    REG and OFFSET.  */
1616
1617 static inline dw_loc_descr_ref
1618 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1619 {
1620   if (reg <= 31)
1621     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1622                           offset, 0);
1623   else
1624     return new_loc_descr (DW_OP_bregx, reg, offset);
1625 }
1626
1627 /* Add a location description term to a location description expression.  */
1628
1629 static inline void
1630 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1631 {
1632   dw_loc_descr_ref *d;
1633
1634   /* Find the end of the chain.  */
1635   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1636     ;
1637
1638   *d = descr;
1639 }
1640
1641 /* Add a constant OFFSET to a location expression.  */
1642
1643 static void
1644 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1645 {
1646   dw_loc_descr_ref loc;
1647   HOST_WIDE_INT *p;
1648
1649   gcc_assert (*list_head != NULL);
1650
1651   if (!offset)
1652     return;
1653
1654   /* Find the end of the chain.  */
1655   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1656     ;
1657
1658   p = NULL;
1659   if (loc->dw_loc_opc == DW_OP_fbreg
1660       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1661     p = &loc->dw_loc_oprnd1.v.val_int;
1662   else if (loc->dw_loc_opc == DW_OP_bregx)
1663     p = &loc->dw_loc_oprnd2.v.val_int;
1664
1665   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1666      offset.  Don't optimize if an signed integer overflow would happen.  */
1667   if (p != NULL
1668       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1669           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1670     *p += offset;
1671
1672   else if (offset > 0)
1673     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1674
1675   else
1676     {
1677       loc->dw_loc_next = int_loc_descriptor (-offset);
1678       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1679     }
1680 }
1681
1682 /* Add a constant OFFSET to a location list.  */
1683
1684 static void
1685 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1686 {
1687   dw_loc_list_ref d;
1688   for (d = list_head; d != NULL; d = d->dw_loc_next)
1689     loc_descr_plus_const (&d->expr, offset);
1690 }
1691
1692 #define DWARF_REF_SIZE  \
1693   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1694
1695 static unsigned long int get_base_type_offset (dw_die_ref);
1696
1697 /* Return the size of a location descriptor.  */
1698
1699 static unsigned long
1700 size_of_loc_descr (dw_loc_descr_ref loc)
1701 {
1702   unsigned long size = 1;
1703
1704   switch (loc->dw_loc_opc)
1705     {
1706     case DW_OP_addr:
1707       size += DWARF2_ADDR_SIZE;
1708       break;
1709     case DW_OP_const1u:
1710     case DW_OP_const1s:
1711       size += 1;
1712       break;
1713     case DW_OP_const2u:
1714     case DW_OP_const2s:
1715       size += 2;
1716       break;
1717     case DW_OP_const4u:
1718     case DW_OP_const4s:
1719       size += 4;
1720       break;
1721     case DW_OP_const8u:
1722     case DW_OP_const8s:
1723       size += 8;
1724       break;
1725     case DW_OP_constu:
1726       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727       break;
1728     case DW_OP_consts:
1729       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730       break;
1731     case DW_OP_pick:
1732       size += 1;
1733       break;
1734     case DW_OP_plus_uconst:
1735       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736       break;
1737     case DW_OP_skip:
1738     case DW_OP_bra:
1739       size += 2;
1740       break;
1741     case DW_OP_breg0:
1742     case DW_OP_breg1:
1743     case DW_OP_breg2:
1744     case DW_OP_breg3:
1745     case DW_OP_breg4:
1746     case DW_OP_breg5:
1747     case DW_OP_breg6:
1748     case DW_OP_breg7:
1749     case DW_OP_breg8:
1750     case DW_OP_breg9:
1751     case DW_OP_breg10:
1752     case DW_OP_breg11:
1753     case DW_OP_breg12:
1754     case DW_OP_breg13:
1755     case DW_OP_breg14:
1756     case DW_OP_breg15:
1757     case DW_OP_breg16:
1758     case DW_OP_breg17:
1759     case DW_OP_breg18:
1760     case DW_OP_breg19:
1761     case DW_OP_breg20:
1762     case DW_OP_breg21:
1763     case DW_OP_breg22:
1764     case DW_OP_breg23:
1765     case DW_OP_breg24:
1766     case DW_OP_breg25:
1767     case DW_OP_breg26:
1768     case DW_OP_breg27:
1769     case DW_OP_breg28:
1770     case DW_OP_breg29:
1771     case DW_OP_breg30:
1772     case DW_OP_breg31:
1773       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774       break;
1775     case DW_OP_regx:
1776       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777       break;
1778     case DW_OP_fbreg:
1779       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780       break;
1781     case DW_OP_bregx:
1782       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784       break;
1785     case DW_OP_piece:
1786       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787       break;
1788     case DW_OP_bit_piece:
1789       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791       break;
1792     case DW_OP_deref_size:
1793     case DW_OP_xderef_size:
1794       size += 1;
1795       break;
1796     case DW_OP_call2:
1797       size += 2;
1798       break;
1799     case DW_OP_call4:
1800       size += 4;
1801       break;
1802     case DW_OP_call_ref:
1803       size += DWARF_REF_SIZE;
1804       break;
1805     case DW_OP_implicit_value:
1806       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1807               + loc->dw_loc_oprnd1.v.val_unsigned;
1808       break;
1809     case DW_OP_GNU_implicit_pointer:
1810       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1811       break;
1812     case DW_OP_GNU_entry_value:
1813       {
1814         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1815         size += size_of_uleb128 (op_size) + op_size;
1816         break;
1817       }
1818     case DW_OP_GNU_const_type:
1819       {
1820         unsigned long o
1821           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1822         size += size_of_uleb128 (o) + 1;
1823         switch (loc->dw_loc_oprnd2.val_class)
1824           {
1825           case dw_val_class_vec:
1826             size += loc->dw_loc_oprnd2.v.val_vec.length
1827                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1828             break;
1829           case dw_val_class_const:
1830             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1831             break;
1832           case dw_val_class_const_double:
1833             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1834             break;
1835           default:
1836             gcc_unreachable ();
1837           }
1838         break;
1839       }
1840     case DW_OP_GNU_regval_type:
1841       {
1842         unsigned long o
1843           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1844         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1845                 + size_of_uleb128 (o);
1846       }
1847       break;
1848     case DW_OP_GNU_deref_type:
1849       {
1850         unsigned long o
1851           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1852         size += 1 + size_of_uleb128 (o);
1853       }
1854       break;
1855     case DW_OP_GNU_convert:
1856     case DW_OP_GNU_reinterpret:
1857       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1858         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859       else
1860         {
1861           unsigned long o
1862             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1863           size += size_of_uleb128 (o);
1864         }
1865       break;
1866     case DW_OP_GNU_parameter_ref:
1867       size += 4;
1868       break;
1869     default:
1870       break;
1871     }
1872
1873   return size;
1874 }
1875
1876 /* Return the size of a series of location descriptors.  */
1877
1878 unsigned long
1879 size_of_locs (dw_loc_descr_ref loc)
1880 {
1881   dw_loc_descr_ref l;
1882   unsigned long size;
1883
1884   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1885      field, to avoid writing to a PCH file.  */
1886   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1887     {
1888       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1889         break;
1890       size += size_of_loc_descr (l);
1891     }
1892   if (! l)
1893     return size;
1894
1895   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1896     {
1897       l->dw_loc_addr = size;
1898       size += size_of_loc_descr (l);
1899     }
1900
1901   return size;
1902 }
1903
1904 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1905 static void get_ref_die_offset_label (char *, dw_die_ref);
1906 static unsigned long int get_ref_die_offset (dw_die_ref);
1907
1908 /* Output location description stack opcode's operands (if any).
1909    The for_eh_or_skip parameter controls whether register numbers are
1910    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1911    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1912    info).  This should be suppressed for the cases that have not been converted
1913    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1914
1915 static void
1916 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1917 {
1918   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1919   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1920
1921   switch (loc->dw_loc_opc)
1922     {
1923 #ifdef DWARF2_DEBUGGING_INFO
1924     case DW_OP_const2u:
1925     case DW_OP_const2s:
1926       dw2_asm_output_data (2, val1->v.val_int, NULL);
1927       break;
1928     case DW_OP_const4u:
1929       if (loc->dtprel)
1930         {
1931           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1932           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1933                                                val1->v.val_addr);
1934           fputc ('\n', asm_out_file);
1935           break;
1936         }
1937       /* FALLTHRU */
1938     case DW_OP_const4s:
1939       dw2_asm_output_data (4, val1->v.val_int, NULL);
1940       break;
1941     case DW_OP_const8u:
1942       if (loc->dtprel)
1943         {
1944           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1945           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1946                                                val1->v.val_addr);
1947           fputc ('\n', asm_out_file);
1948           break;
1949         }
1950       /* FALLTHRU */
1951     case DW_OP_const8s:
1952       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1953       dw2_asm_output_data (8, val1->v.val_int, NULL);
1954       break;
1955     case DW_OP_skip:
1956     case DW_OP_bra:
1957       {
1958         int offset;
1959
1960         gcc_assert (val1->val_class == dw_val_class_loc);
1961         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1962
1963         dw2_asm_output_data (2, offset, NULL);
1964       }
1965       break;
1966     case DW_OP_implicit_value:
1967       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1968       switch (val2->val_class)
1969         {
1970         case dw_val_class_const:
1971           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1972           break;
1973         case dw_val_class_vec:
1974           {
1975             unsigned int elt_size = val2->v.val_vec.elt_size;
1976             unsigned int len = val2->v.val_vec.length;
1977             unsigned int i;
1978             unsigned char *p;
1979
1980             if (elt_size > sizeof (HOST_WIDE_INT))
1981               {
1982                 elt_size /= 2;
1983                 len *= 2;
1984               }
1985             for (i = 0, p = val2->v.val_vec.array;
1986                  i < len;
1987                  i++, p += elt_size)
1988               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1989                                    "fp or vector constant word %u", i);
1990           }
1991           break;
1992         case dw_val_class_const_double:
1993           {
1994             unsigned HOST_WIDE_INT first, second;
1995
1996             if (WORDS_BIG_ENDIAN)
1997               {
1998                 first = val2->v.val_double.high;
1999                 second = val2->v.val_double.low;
2000               }
2001             else
2002               {
2003                 first = val2->v.val_double.low;
2004                 second = val2->v.val_double.high;
2005               }
2006             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2007                                  first, NULL);
2008             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2009                                  second, NULL);
2010           }
2011           break;
2012         case dw_val_class_addr:
2013           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2014           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2015           break;
2016         default:
2017           gcc_unreachable ();
2018         }
2019       break;
2020 #else
2021     case DW_OP_const2u:
2022     case DW_OP_const2s:
2023     case DW_OP_const4u:
2024     case DW_OP_const4s:
2025     case DW_OP_const8u:
2026     case DW_OP_const8s:
2027     case DW_OP_skip:
2028     case DW_OP_bra:
2029     case DW_OP_implicit_value:
2030       /* We currently don't make any attempt to make sure these are
2031          aligned properly like we do for the main unwind info, so
2032          don't support emitting things larger than a byte if we're
2033          only doing unwinding.  */
2034       gcc_unreachable ();
2035 #endif
2036     case DW_OP_const1u:
2037     case DW_OP_const1s:
2038       dw2_asm_output_data (1, val1->v.val_int, NULL);
2039       break;
2040     case DW_OP_constu:
2041       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2042       break;
2043     case DW_OP_consts:
2044       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2045       break;
2046     case DW_OP_pick:
2047       dw2_asm_output_data (1, val1->v.val_int, NULL);
2048       break;
2049     case DW_OP_plus_uconst:
2050       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2051       break;
2052     case DW_OP_breg0:
2053     case DW_OP_breg1:
2054     case DW_OP_breg2:
2055     case DW_OP_breg3:
2056     case DW_OP_breg4:
2057     case DW_OP_breg5:
2058     case DW_OP_breg6:
2059     case DW_OP_breg7:
2060     case DW_OP_breg8:
2061     case DW_OP_breg9:
2062     case DW_OP_breg10:
2063     case DW_OP_breg11:
2064     case DW_OP_breg12:
2065     case DW_OP_breg13:
2066     case DW_OP_breg14:
2067     case DW_OP_breg15:
2068     case DW_OP_breg16:
2069     case DW_OP_breg17:
2070     case DW_OP_breg18:
2071     case DW_OP_breg19:
2072     case DW_OP_breg20:
2073     case DW_OP_breg21:
2074     case DW_OP_breg22:
2075     case DW_OP_breg23:
2076     case DW_OP_breg24:
2077     case DW_OP_breg25:
2078     case DW_OP_breg26:
2079     case DW_OP_breg27:
2080     case DW_OP_breg28:
2081     case DW_OP_breg29:
2082     case DW_OP_breg30:
2083     case DW_OP_breg31:
2084       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2085       break;
2086     case DW_OP_regx:
2087       {
2088         unsigned r = val1->v.val_unsigned;
2089         if (for_eh_or_skip >= 0)
2090           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2091         gcc_assert (size_of_uleb128 (r) 
2092                     == size_of_uleb128 (val1->v.val_unsigned));
2093         dw2_asm_output_data_uleb128 (r, NULL);  
2094       }
2095       break;
2096     case DW_OP_fbreg:
2097       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2098       break;
2099     case DW_OP_bregx:
2100       {
2101         unsigned r = val1->v.val_unsigned;
2102         if (for_eh_or_skip >= 0)
2103           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2104         gcc_assert (size_of_uleb128 (r) 
2105                     == size_of_uleb128 (val1->v.val_unsigned));
2106         dw2_asm_output_data_uleb128 (r, NULL);  
2107         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2108       }
2109       break;
2110     case DW_OP_piece:
2111       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2112       break;
2113     case DW_OP_bit_piece:
2114       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2116       break;
2117     case DW_OP_deref_size:
2118     case DW_OP_xderef_size:
2119       dw2_asm_output_data (1, val1->v.val_int, NULL);
2120       break;
2121
2122     case DW_OP_addr:
2123       if (loc->dtprel)
2124         {
2125           if (targetm.asm_out.output_dwarf_dtprel)
2126             {
2127               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2128                                                    DWARF2_ADDR_SIZE,
2129                                                    val1->v.val_addr);
2130               fputc ('\n', asm_out_file);
2131             }
2132           else
2133             gcc_unreachable ();
2134         }
2135       else
2136         {
2137 #ifdef DWARF2_DEBUGGING_INFO
2138           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2139 #else
2140           gcc_unreachable ();
2141 #endif
2142         }
2143       break;
2144
2145     case DW_OP_GNU_implicit_pointer:
2146       {
2147         char label[MAX_ARTIFICIAL_LABEL_BYTES
2148                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2149         gcc_assert (val1->val_class == dw_val_class_die_ref);
2150         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2151         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2152         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2153       }
2154       break;
2155
2156     case DW_OP_GNU_entry_value:
2157       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2158       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2159       break;
2160
2161     case DW_OP_GNU_const_type:
2162       {
2163         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2164         gcc_assert (o);
2165         dw2_asm_output_data_uleb128 (o, NULL);
2166         switch (val2->val_class)
2167           {
2168           case dw_val_class_const:
2169             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2170             dw2_asm_output_data (1, l, NULL);
2171             dw2_asm_output_data (l, val2->v.val_int, NULL);
2172             break;
2173           case dw_val_class_vec:
2174             {
2175               unsigned int elt_size = val2->v.val_vec.elt_size;
2176               unsigned int len = val2->v.val_vec.length;
2177               unsigned int i;
2178               unsigned char *p;
2179
2180               l = len * elt_size;
2181               dw2_asm_output_data (1, l, NULL);
2182               if (elt_size > sizeof (HOST_WIDE_INT))
2183                 {
2184                   elt_size /= 2;
2185                   len *= 2;
2186                 }
2187               for (i = 0, p = val2->v.val_vec.array;
2188                    i < len;
2189                    i++, p += elt_size)
2190                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2191                                      "fp or vector constant word %u", i);
2192             }
2193             break;
2194           case dw_val_class_const_double:
2195             {
2196               unsigned HOST_WIDE_INT first, second;
2197               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2198
2199               dw2_asm_output_data (1, 2 * l, NULL);
2200               if (WORDS_BIG_ENDIAN)
2201                 {
2202                   first = val2->v.val_double.high;
2203                   second = val2->v.val_double.low;
2204                 }
2205               else
2206                 {
2207                   first = val2->v.val_double.low;
2208                   second = val2->v.val_double.high;
2209                 }
2210               dw2_asm_output_data (l, first, NULL);
2211               dw2_asm_output_data (l, second, NULL);
2212             }
2213             break;
2214           default:
2215             gcc_unreachable ();
2216           }
2217       }
2218       break;
2219     case DW_OP_GNU_regval_type:
2220       {
2221         unsigned r = val1->v.val_unsigned;
2222         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2223         gcc_assert (o);
2224         if (for_eh_or_skip >= 0)
2225           {
2226             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2227             gcc_assert (size_of_uleb128 (r)
2228                         == size_of_uleb128 (val1->v.val_unsigned));
2229           }
2230         dw2_asm_output_data_uleb128 (r, NULL);
2231         dw2_asm_output_data_uleb128 (o, NULL);
2232       }
2233       break;
2234     case DW_OP_GNU_deref_type:
2235       {
2236         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2237         gcc_assert (o);
2238         dw2_asm_output_data (1, val1->v.val_int, NULL);
2239         dw2_asm_output_data_uleb128 (o, NULL);
2240       }
2241       break;
2242     case DW_OP_GNU_convert:
2243     case DW_OP_GNU_reinterpret:
2244       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2245         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2246       else
2247         {
2248           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2249           gcc_assert (o);
2250           dw2_asm_output_data_uleb128 (o, NULL);
2251         }
2252       break;
2253
2254     case DW_OP_GNU_parameter_ref:
2255       {
2256         unsigned long o;
2257         gcc_assert (val1->val_class == dw_val_class_die_ref);
2258         o = get_ref_die_offset (val1->v.val_die_ref.die);
2259         dw2_asm_output_data (4, o, NULL);
2260       }
2261       break;
2262
2263     default:
2264       /* Other codes have no operands.  */
2265       break;
2266     }
2267 }
2268
2269 /* Output a sequence of location operations.  
2270    The for_eh_or_skip parameter controls whether register numbers are
2271    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2272    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2273    info).  This should be suppressed for the cases that have not been converted
2274    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2275
2276 void
2277 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2278 {
2279   for (; loc != NULL; loc = loc->dw_loc_next)
2280     {
2281       enum dwarf_location_atom opc = loc->dw_loc_opc;
2282       /* Output the opcode.  */
2283       if (for_eh_or_skip >= 0 
2284           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2285         {
2286           unsigned r = (opc - DW_OP_breg0);
2287           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2288           gcc_assert (r <= 31);
2289           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2290         }
2291       else if (for_eh_or_skip >= 0 
2292                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2293         {
2294           unsigned r = (opc - DW_OP_reg0);
2295           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2296           gcc_assert (r <= 31);
2297           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2298         }
2299
2300       dw2_asm_output_data (1, opc,
2301                              "%s", dwarf_stack_op_name (opc));
2302
2303       /* Output the operand(s) (if any).  */
2304       output_loc_operands (loc, for_eh_or_skip);
2305     }
2306 }
2307
2308 /* Output location description stack opcode's operands (if any).
2309    The output is single bytes on a line, suitable for .cfi_escape.  */
2310
2311 static void
2312 output_loc_operands_raw (dw_loc_descr_ref loc)
2313 {
2314   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2315   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2316
2317   switch (loc->dw_loc_opc)
2318     {
2319     case DW_OP_addr:
2320     case DW_OP_implicit_value:
2321       /* We cannot output addresses in .cfi_escape, only bytes.  */
2322       gcc_unreachable ();
2323
2324     case DW_OP_const1u:
2325     case DW_OP_const1s:
2326     case DW_OP_pick:
2327     case DW_OP_deref_size:
2328     case DW_OP_xderef_size:
2329       fputc (',', asm_out_file);
2330       dw2_asm_output_data_raw (1, val1->v.val_int);
2331       break;
2332
2333     case DW_OP_const2u:
2334     case DW_OP_const2s:
2335       fputc (',', asm_out_file);
2336       dw2_asm_output_data_raw (2, val1->v.val_int);
2337       break;
2338
2339     case DW_OP_const4u:
2340     case DW_OP_const4s:
2341       fputc (',', asm_out_file);
2342       dw2_asm_output_data_raw (4, val1->v.val_int);
2343       break;
2344
2345     case DW_OP_const8u:
2346     case DW_OP_const8s:
2347       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2348       fputc (',', asm_out_file);
2349       dw2_asm_output_data_raw (8, val1->v.val_int);
2350       break;
2351
2352     case DW_OP_skip:
2353     case DW_OP_bra:
2354       {
2355         int offset;
2356
2357         gcc_assert (val1->val_class == dw_val_class_loc);
2358         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2359
2360         fputc (',', asm_out_file);
2361         dw2_asm_output_data_raw (2, offset);
2362       }
2363       break;
2364
2365     case DW_OP_regx:
2366       {
2367         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2368         gcc_assert (size_of_uleb128 (r) 
2369                     == size_of_uleb128 (val1->v.val_unsigned));
2370         fputc (',', asm_out_file);
2371         dw2_asm_output_data_uleb128_raw (r);
2372       }
2373       break;
2374       
2375     case DW_OP_constu:
2376     case DW_OP_plus_uconst:
2377     case DW_OP_piece:
2378       fputc (',', asm_out_file);
2379       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2380       break;
2381
2382     case DW_OP_bit_piece:
2383       fputc (',', asm_out_file);
2384       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2385       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2386       break;
2387
2388     case DW_OP_consts:
2389     case DW_OP_breg0:
2390     case DW_OP_breg1:
2391     case DW_OP_breg2:
2392     case DW_OP_breg3:
2393     case DW_OP_breg4:
2394     case DW_OP_breg5:
2395     case DW_OP_breg6:
2396     case DW_OP_breg7:
2397     case DW_OP_breg8:
2398     case DW_OP_breg9:
2399     case DW_OP_breg10:
2400     case DW_OP_breg11:
2401     case DW_OP_breg12:
2402     case DW_OP_breg13:
2403     case DW_OP_breg14:
2404     case DW_OP_breg15:
2405     case DW_OP_breg16:
2406     case DW_OP_breg17:
2407     case DW_OP_breg18:
2408     case DW_OP_breg19:
2409     case DW_OP_breg20:
2410     case DW_OP_breg21:
2411     case DW_OP_breg22:
2412     case DW_OP_breg23:
2413     case DW_OP_breg24:
2414     case DW_OP_breg25:
2415     case DW_OP_breg26:
2416     case DW_OP_breg27:
2417     case DW_OP_breg28:
2418     case DW_OP_breg29:
2419     case DW_OP_breg30:
2420     case DW_OP_breg31:
2421     case DW_OP_fbreg:
2422       fputc (',', asm_out_file);
2423       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2424       break;
2425
2426     case DW_OP_bregx:
2427       {
2428         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2429         gcc_assert (size_of_uleb128 (r) 
2430                     == size_of_uleb128 (val1->v.val_unsigned));
2431         fputc (',', asm_out_file);
2432         dw2_asm_output_data_uleb128_raw (r);
2433         fputc (',', asm_out_file);
2434         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2435       }
2436       break;
2437
2438     case DW_OP_GNU_implicit_pointer:
2439     case DW_OP_GNU_entry_value:
2440     case DW_OP_GNU_const_type:
2441     case DW_OP_GNU_regval_type:
2442     case DW_OP_GNU_deref_type:
2443     case DW_OP_GNU_convert:
2444     case DW_OP_GNU_reinterpret:
2445     case DW_OP_GNU_parameter_ref:
2446       gcc_unreachable ();
2447       break;
2448
2449     default:
2450       /* Other codes have no operands.  */
2451       break;
2452     }
2453 }
2454
2455 void
2456 output_loc_sequence_raw (dw_loc_descr_ref loc)
2457 {
2458   while (1)
2459     {
2460       enum dwarf_location_atom opc = loc->dw_loc_opc;
2461       /* Output the opcode.  */
2462       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2463         {
2464           unsigned r = (opc - DW_OP_breg0);
2465           r = DWARF2_FRAME_REG_OUT (r, 1);
2466           gcc_assert (r <= 31);
2467           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2468         }
2469       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2470         {
2471           unsigned r = (opc - DW_OP_reg0);
2472           r = DWARF2_FRAME_REG_OUT (r, 1);
2473           gcc_assert (r <= 31);
2474           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2475         }
2476       /* Output the opcode.  */
2477       fprintf (asm_out_file, "%#x", opc);
2478       output_loc_operands_raw (loc);
2479
2480       if (!loc->dw_loc_next)
2481         break;
2482       loc = loc->dw_loc_next;
2483
2484       fputc (',', asm_out_file);
2485     }
2486 }
2487
2488 /* This function builds a dwarf location descriptor sequence from a
2489    dw_cfa_location, adding the given OFFSET to the result of the
2490    expression.  */
2491
2492 struct dw_loc_descr_struct *
2493 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2494 {
2495   struct dw_loc_descr_struct *head, *tmp;
2496
2497   offset += cfa->offset;
2498
2499   if (cfa->indirect)
2500     {
2501       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2502       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2503       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2504       add_loc_descr (&head, tmp);
2505       if (offset != 0)
2506         {
2507           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2508           add_loc_descr (&head, tmp);
2509         }
2510     }
2511   else
2512     head = new_reg_loc_descr (cfa->reg, offset);
2513
2514   return head;
2515 }
2516
2517 /* This function builds a dwarf location descriptor sequence for
2518    the address at OFFSET from the CFA when stack is aligned to
2519    ALIGNMENT byte.  */
2520
2521 struct dw_loc_descr_struct *
2522 build_cfa_aligned_loc (dw_cfa_location *cfa,
2523                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2524 {
2525   struct dw_loc_descr_struct *head;
2526   unsigned int dwarf_fp
2527     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2528
2529   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2530   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2531     {
2532       head = new_reg_loc_descr (dwarf_fp, 0);
2533       add_loc_descr (&head, int_loc_descriptor (alignment));
2534       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2535       loc_descr_plus_const (&head, offset);
2536     }
2537   else
2538     head = new_reg_loc_descr (dwarf_fp, offset);
2539   return head;
2540 }
2541 \f
2542 /* And now, the support for symbolic debugging information.  */
2543
2544 /* .debug_str support.  */
2545 static int output_indirect_string (void **, void *);
2546
2547 static void dwarf2out_init (const char *);
2548 static void dwarf2out_finish (const char *);
2549 static void dwarf2out_assembly_start (void);
2550 static void dwarf2out_define (unsigned int, const char *);
2551 static void dwarf2out_undef (unsigned int, const char *);
2552 static void dwarf2out_start_source_file (unsigned, const char *);
2553 static void dwarf2out_end_source_file (unsigned);
2554 static void dwarf2out_function_decl (tree);
2555 static void dwarf2out_begin_block (unsigned, unsigned);
2556 static void dwarf2out_end_block (unsigned, unsigned);
2557 static bool dwarf2out_ignore_block (const_tree);
2558 static void dwarf2out_global_decl (tree);
2559 static void dwarf2out_type_decl (tree, int);
2560 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2561 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2562                                                  dw_die_ref);
2563 static void dwarf2out_abstract_function (tree);
2564 static void dwarf2out_var_location (rtx);
2565 static void dwarf2out_begin_function (tree);
2566 static void dwarf2out_set_name (tree, tree);
2567
2568 /* The debug hooks structure.  */
2569
2570 const struct gcc_debug_hooks dwarf2_debug_hooks =
2571 {
2572   dwarf2out_init,
2573   dwarf2out_finish,
2574   dwarf2out_assembly_start,
2575   dwarf2out_define,
2576   dwarf2out_undef,
2577   dwarf2out_start_source_file,
2578   dwarf2out_end_source_file,
2579   dwarf2out_begin_block,
2580   dwarf2out_end_block,
2581   dwarf2out_ignore_block,
2582   dwarf2out_source_line,
2583   dwarf2out_begin_prologue,
2584 #if VMS_DEBUGGING_INFO
2585   dwarf2out_vms_end_prologue,
2586   dwarf2out_vms_begin_epilogue,
2587 #else
2588   debug_nothing_int_charstar,
2589   debug_nothing_int_charstar,
2590 #endif
2591   dwarf2out_end_epilogue,
2592   dwarf2out_begin_function,
2593   debug_nothing_int,            /* end_function */
2594   dwarf2out_function_decl,      /* function_decl */
2595   dwarf2out_global_decl,
2596   dwarf2out_type_decl,          /* type_decl */
2597   dwarf2out_imported_module_or_decl,
2598   debug_nothing_tree,           /* deferred_inline_function */
2599   /* The DWARF 2 backend tries to reduce debugging bloat by not
2600      emitting the abstract description of inline functions until
2601      something tries to reference them.  */
2602   dwarf2out_abstract_function,  /* outlining_inline_function */
2603   debug_nothing_rtx,            /* label */
2604   debug_nothing_int,            /* handle_pch */
2605   dwarf2out_var_location,
2606   dwarf2out_switch_text_section,
2607   dwarf2out_set_name,
2608   1,                            /* start_end_main_source_file */
2609   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2610 };
2611 \f
2612 /* NOTE: In the comments in this file, many references are made to
2613    "Debugging Information Entries".  This term is abbreviated as `DIE'
2614    throughout the remainder of this file.  */
2615
2616 /* An internal representation of the DWARF output is built, and then
2617    walked to generate the DWARF debugging info.  The walk of the internal
2618    representation is done after the entire program has been compiled.
2619    The types below are used to describe the internal representation.  */
2620
2621 /* Whether to put type DIEs into their own section .debug_types instead
2622    of making them part of the .debug_info section.  Only supported for
2623    Dwarf V4 or higher and the user didn't disable them through
2624    -fno-debug-types-section.  It is more efficient to put them in a
2625    separate comdat sections since the linker will then be able to
2626    remove duplicates.  But not all tools support .debug_types sections
2627    yet.  */
2628
2629 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2630
2631 /* Various DIE's use offsets relative to the beginning of the
2632    .debug_info section to refer to each other.  */
2633
2634 typedef long int dw_offset;
2635
2636 /* Define typedefs here to avoid circular dependencies.  */
2637
2638 typedef struct dw_attr_struct *dw_attr_ref;
2639 typedef struct dw_line_info_struct *dw_line_info_ref;
2640 typedef struct pubname_struct *pubname_ref;
2641 typedef struct dw_ranges_struct *dw_ranges_ref;
2642 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2643 typedef struct comdat_type_struct *comdat_type_node_ref;
2644
2645 /* The entries in the line_info table more-or-less mirror the opcodes
2646    that are used in the real dwarf line table.  Arrays of these entries
2647    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2648    supported.  */
2649
2650 enum dw_line_info_opcode {
2651   /* Emit DW_LNE_set_address; the operand is the label index.  */
2652   LI_set_address,
2653
2654   /* Emit a row to the matrix with the given line.  This may be done
2655      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2656      special opcodes.  */
2657   LI_set_line,
2658
2659   /* Emit a DW_LNS_set_file.  */
2660   LI_set_file,
2661
2662   /* Emit a DW_LNS_set_column.  */
2663   LI_set_column,
2664
2665   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2666   LI_negate_stmt,
2667
2668   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2669   LI_set_prologue_end,
2670   LI_set_epilogue_begin,
2671
2672   /* Emit a DW_LNE_set_discriminator.  */
2673   LI_set_discriminator
2674 };
2675
2676 typedef struct GTY(()) dw_line_info_struct {
2677   enum dw_line_info_opcode opcode;
2678   unsigned int val;
2679 } dw_line_info_entry;
2680
2681 DEF_VEC_O(dw_line_info_entry);
2682 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2683
2684 typedef struct GTY(()) dw_line_info_table_struct {
2685   /* The label that marks the end of this section.  */
2686   const char *end_label;
2687
2688   /* The values for the last row of the matrix, as collected in the table.
2689      These are used to minimize the changes to the next row.  */
2690   unsigned int file_num;
2691   unsigned int line_num;
2692   unsigned int column_num;
2693   int discrim_num;
2694   bool is_stmt;
2695   bool in_use;
2696
2697   VEC(dw_line_info_entry, gc) *entries;
2698 } dw_line_info_table;
2699
2700 typedef dw_line_info_table *dw_line_info_table_p;
2701
2702 DEF_VEC_P(dw_line_info_table_p);
2703 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2704
2705 /* Each DIE attribute has a field specifying the attribute kind,
2706    a link to the next attribute in the chain, and an attribute value.
2707    Attributes are typically linked below the DIE they modify.  */
2708
2709 typedef struct GTY(()) dw_attr_struct {
2710   enum dwarf_attribute dw_attr;
2711   dw_val_node dw_attr_val;
2712 }
2713 dw_attr_node;
2714
2715 DEF_VEC_O(dw_attr_node);
2716 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2717
2718 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2719    The children of each node form a circular list linked by
2720    die_sib.  die_child points to the node *before* the "first" child node.  */
2721
2722 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2723   union die_symbol_or_type_node
2724     {
2725       char * GTY ((tag ("0"))) die_symbol;
2726       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2727     }
2728   GTY ((desc ("use_debug_types"))) die_id;
2729   VEC(dw_attr_node,gc) * die_attr;
2730   dw_die_ref die_parent;
2731   dw_die_ref die_child;
2732   dw_die_ref die_sib;
2733   dw_die_ref die_definition; /* ref from a specification to its definition */
2734   dw_offset die_offset;
2735   unsigned long die_abbrev;
2736   int die_mark;
2737   /* Die is used and must not be pruned as unused.  */
2738   int die_perennial_p;
2739   unsigned int decl_id;
2740   enum dwarf_tag die_tag;
2741 }
2742 die_node;
2743
2744 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2745 #define FOR_EACH_CHILD(die, c, expr) do {       \
2746   c = die->die_child;                           \
2747   if (c) do {                                   \
2748     c = c->die_sib;                             \
2749     expr;                                       \
2750   } while (c != die->die_child);                \
2751 } while (0)
2752
2753 /* The pubname structure */
2754
2755 typedef struct GTY(()) pubname_struct {
2756   dw_die_ref die;
2757   const char *name;
2758 }
2759 pubname_entry;
2760
2761 DEF_VEC_O(pubname_entry);
2762 DEF_VEC_ALLOC_O(pubname_entry, gc);
2763
2764 struct GTY(()) dw_ranges_struct {
2765   /* If this is positive, it's a block number, otherwise it's a
2766      bitwise-negated index into dw_ranges_by_label.  */
2767   int num;
2768 };
2769
2770 /* A structure to hold a macinfo entry.  */
2771
2772 typedef struct GTY(()) macinfo_struct {
2773   unsigned HOST_WIDE_INT code;
2774   unsigned HOST_WIDE_INT lineno;
2775   const char *info;
2776 }
2777 macinfo_entry;
2778
2779 DEF_VEC_O(macinfo_entry);
2780 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2781
2782 struct GTY(()) dw_ranges_by_label_struct {
2783   const char *begin;
2784   const char *end;
2785 };
2786
2787 /* The comdat type node structure.  */
2788 typedef struct GTY(()) comdat_type_struct
2789 {
2790   dw_die_ref root_die;
2791   dw_die_ref type_die;
2792   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2793   struct comdat_type_struct *next;
2794 }
2795 comdat_type_node;
2796
2797 /* The limbo die list structure.  */
2798 typedef struct GTY(()) limbo_die_struct {
2799   dw_die_ref die;
2800   tree created_for;
2801   struct limbo_die_struct *next;
2802 }
2803 limbo_die_node;
2804
2805 typedef struct skeleton_chain_struct
2806 {
2807   dw_die_ref old_die;
2808   dw_die_ref new_die;
2809   struct skeleton_chain_struct *parent;
2810 }
2811 skeleton_chain_node;
2812
2813 /* Define a macro which returns nonzero for a TYPE_DECL which was
2814    implicitly generated for a type.
2815
2816    Note that, unlike the C front-end (which generates a NULL named
2817    TYPE_DECL node for each complete tagged type, each array type,
2818    and each function type node created) the C++ front-end generates
2819    a _named_ TYPE_DECL node for each tagged type node created.
2820    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2821    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2822    front-end, but for each type, tagged or not.  */
2823
2824 #define TYPE_DECL_IS_STUB(decl)                         \
2825   (DECL_NAME (decl) == NULL_TREE                        \
2826    || (DECL_ARTIFICIAL (decl)                           \
2827        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2828            /* This is necessary for stub decls that     \
2829               appear in nested inline functions.  */    \
2830            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2831                && (decl_ultimate_origin (decl)          \
2832                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2833
2834 /* Information concerning the compilation unit's programming
2835    language, and compiler version.  */
2836
2837 /* Fixed size portion of the DWARF compilation unit header.  */
2838 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2839   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2840
2841 /* Fixed size portion of the DWARF comdat type unit header.  */
2842 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2843   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2844    + DWARF_OFFSET_SIZE)
2845
2846 /* Fixed size portion of public names info.  */
2847 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2848
2849 /* Fixed size portion of the address range info.  */
2850 #define DWARF_ARANGES_HEADER_SIZE                                       \
2851   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2852                 DWARF2_ADDR_SIZE * 2)                                   \
2853    - DWARF_INITIAL_LENGTH_SIZE)
2854
2855 /* Size of padding portion in the address range info.  It must be
2856    aligned to twice the pointer size.  */
2857 #define DWARF_ARANGES_PAD_SIZE \
2858   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2859                 DWARF2_ADDR_SIZE * 2)                              \
2860    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2861
2862 /* Use assembler line directives if available.  */
2863 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2864 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2865 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2866 #else
2867 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2868 #endif
2869 #endif
2870
2871 /* Minimum line offset in a special line info. opcode.
2872    This value was chosen to give a reasonable range of values.  */
2873 #define DWARF_LINE_BASE  -10
2874
2875 /* First special line opcode - leave room for the standard opcodes.  */
2876 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2877
2878 /* Range of line offsets in a special line info. opcode.  */
2879 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2880
2881 /* Flag that indicates the initial value of the is_stmt_start flag.
2882    In the present implementation, we do not mark any lines as
2883    the beginning of a source statement, because that information
2884    is not made available by the GCC front-end.  */
2885 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2886
2887 /* Maximum number of operations per instruction bundle.  */
2888 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2889 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2890 #endif
2891
2892 /* This location is used by calc_die_sizes() to keep track
2893    the offset of each DIE within the .debug_info section.  */
2894 static unsigned long next_die_offset;
2895
2896 /* Record the root of the DIE's built for the current compilation unit.  */
2897 static GTY(()) dw_die_ref single_comp_unit_die;
2898
2899 /* A list of type DIEs that have been separated into comdat sections.  */
2900 static GTY(()) comdat_type_node *comdat_type_list;
2901
2902 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2903 static GTY(()) limbo_die_node *limbo_die_list;
2904
2905 /* A list of DIEs for which we may have to generate
2906    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2907 static GTY(()) limbo_die_node *deferred_asm_name;
2908
2909 /* Filenames referenced by this compilation unit.  */
2910 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2911
2912 /* A hash table of references to DIE's that describe declarations.
2913    The key is a DECL_UID() which is a unique number identifying each decl.  */
2914 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2915
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917    The key is DECL_UID() ^ die_parent.  */
2918 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2919
2920 typedef struct GTY(()) die_arg_entry_struct {
2921     dw_die_ref die;
2922     tree arg;
2923 } die_arg_entry;
2924
2925 DEF_VEC_O(die_arg_entry);
2926 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2927
2928 /* Node of the variable location list.  */
2929 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2930   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2931      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2932      in mode of the EXPR_LIST node and first EXPR_LIST operand
2933      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2934      location or NULL for padding.  For larger bitsizes,
2935      mode is 0 and first operand is a CONCAT with bitsize
2936      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2937      NULL as second operand.  */
2938   rtx GTY (()) loc;
2939   const char * GTY (()) label;
2940   struct var_loc_node * GTY (()) next;
2941 };
2942
2943 /* Variable location list.  */
2944 struct GTY (()) var_loc_list_def {
2945   struct var_loc_node * GTY (()) first;
2946
2947   /* Pointer to the last but one or last element of the
2948      chained list.  If the list is empty, both first and
2949      last are NULL, if the list contains just one node
2950      or the last node certainly is not redundant, it points
2951      to the last node, otherwise points to the last but one.
2952      Do not mark it for GC because it is marked through the chain.  */
2953   struct var_loc_node * GTY ((skip ("%h"))) last;
2954
2955   /* Pointer to the last element before section switch,
2956      if NULL, either sections weren't switched or first
2957      is after section switch.  */
2958   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2959
2960   /* DECL_UID of the variable decl.  */
2961   unsigned int decl_id;
2962 };
2963 typedef struct var_loc_list_def var_loc_list;
2964
2965 /* Call argument location list.  */
2966 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2967   rtx GTY (()) call_arg_loc_note;
2968   const char * GTY (()) label;
2969   tree GTY (()) block;
2970   bool tail_call_p;
2971   rtx GTY (()) symbol_ref;
2972   struct call_arg_loc_node * GTY (()) next;
2973 };
2974
2975
2976 /* Table of decl location linked lists.  */
2977 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2978
2979 /* Head and tail of call_arg_loc chain.  */
2980 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2981 static struct call_arg_loc_node *call_arg_loc_last;
2982
2983 /* Number of call sites in the current function.  */
2984 static int call_site_count = -1;
2985 /* Number of tail call sites in the current function.  */
2986 static int tail_call_site_count = -1;
2987
2988 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2989    DIEs.  */
2990 static VEC (dw_die_ref, heap) *block_map;
2991
2992 /* A cached location list.  */
2993 struct GTY (()) cached_dw_loc_list_def {
2994   /* The DECL_UID of the decl that this entry describes.  */
2995   unsigned int decl_id;
2996
2997   /* The cached location list.  */
2998   dw_loc_list_ref loc_list;
2999 };
3000 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3001
3002 /* Table of cached location lists.  */
3003 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3004
3005 /* A pointer to the base of a list of references to DIE's that
3006    are uniquely identified by their tag, presence/absence of
3007    children DIE's, and list of attribute/value pairs.  */
3008 static GTY((length ("abbrev_die_table_allocated")))
3009   dw_die_ref *abbrev_die_table;
3010
3011 /* Number of elements currently allocated for abbrev_die_table.  */
3012 static GTY(()) unsigned abbrev_die_table_allocated;
3013
3014 /* Number of elements in type_die_table currently in use.  */
3015 static GTY(()) unsigned abbrev_die_table_in_use;
3016
3017 /* Size (in elements) of increments by which we may expand the
3018    abbrev_die_table.  */
3019 #define ABBREV_DIE_TABLE_INCREMENT 256
3020
3021 /* A global counter for generating labels for line number data.  */
3022 static unsigned int line_info_label_num;
3023
3024 /* The current table to which we should emit line number information
3025    for the current function.  This will be set up at the beginning of
3026    assembly for the function.  */
3027 static dw_line_info_table *cur_line_info_table;
3028
3029 /* The two default tables of line number info.  */
3030 static GTY(()) dw_line_info_table *text_section_line_info;
3031 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3032
3033 /* The set of all non-default tables of line number info.  */
3034 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3035
3036 /* A flag to tell pubnames/types export if there is an info section to
3037    refer to.  */
3038 static bool info_section_emitted;
3039
3040 /* A pointer to the base of a table that contains a list of publicly
3041    accessible names.  */
3042 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3043
3044 /* A pointer to the base of a table that contains a list of publicly
3045    accessible types.  */
3046 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3047
3048 /* A pointer to the base of a table that contains a list of macro
3049    defines/undefines (and file start/end markers).  */
3050 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3051
3052 /* Array of dies for which we should generate .debug_ranges info.  */
3053 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3054
3055 /* Number of elements currently allocated for ranges_table.  */
3056 static GTY(()) unsigned ranges_table_allocated;
3057
3058 /* Number of elements in ranges_table currently in use.  */
3059 static GTY(()) unsigned ranges_table_in_use;
3060
3061 /* Array of pairs of labels referenced in ranges_table.  */
3062 static GTY ((length ("ranges_by_label_allocated")))
3063      dw_ranges_by_label_ref ranges_by_label;
3064
3065 /* Number of elements currently allocated for ranges_by_label.  */
3066 static GTY(()) unsigned ranges_by_label_allocated;
3067
3068 /* Number of elements in ranges_by_label currently in use.  */
3069 static GTY(()) unsigned ranges_by_label_in_use;
3070
3071 /* Size (in elements) of increments by which we may expand the
3072    ranges_table.  */
3073 #define RANGES_TABLE_INCREMENT 64
3074
3075 /* Whether we have location lists that need outputting */
3076 static GTY(()) bool have_location_lists;
3077
3078 /* Unique label counter.  */
3079 static GTY(()) unsigned int loclabel_num;
3080
3081 /* Unique label counter for point-of-call tables.  */
3082 static GTY(()) unsigned int poc_label_num;
3083
3084 /* Record whether the function being analyzed contains inlined functions.  */
3085 static int current_function_has_inlines;
3086
3087 /* The last file entry emitted by maybe_emit_file().  */
3088 static GTY(()) struct dwarf_file_data * last_emitted_file;
3089
3090 /* Number of internal labels generated by gen_internal_sym().  */
3091 static GTY(()) int label_num;
3092
3093 /* Cached result of previous call to lookup_filename.  */
3094 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3095
3096 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3097
3098 /* Instances of generic types for which we need to generate debug
3099    info that describe their generic parameters and arguments. That
3100    generation needs to happen once all types are properly laid out so
3101    we do it at the end of compilation.  */
3102 static GTY(()) VEC(tree,gc) *generic_type_instances;
3103
3104 /* Offset from the "steady-state frame pointer" to the frame base,
3105    within the current function.  */
3106 static HOST_WIDE_INT frame_pointer_fb_offset;
3107 static bool frame_pointer_fb_offset_valid;
3108
3109 static VEC (dw_die_ref, heap) *base_types;
3110
3111 /* Forward declarations for functions defined in this file.  */
3112
3113 static int is_pseudo_reg (const_rtx);
3114 static tree type_main_variant (tree);
3115 static int is_tagged_type (const_tree);
3116 static const char *dwarf_tag_name (unsigned);
3117 static const char *dwarf_attr_name (unsigned);
3118 static const char *dwarf_form_name (unsigned);
3119 static tree decl_ultimate_origin (const_tree);
3120 static tree decl_class_context (tree);
3121 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3122 static inline enum dw_val_class AT_class (dw_attr_ref);
3123 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3124 static inline unsigned AT_flag (dw_attr_ref);
3125 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3126 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3127 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3128 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3129 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3130                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3131 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3132                                unsigned int, unsigned char *);
3133 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3134 static hashval_t debug_str_do_hash (const void *);
3135 static int debug_str_eq (const void *, const void *);
3136 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3137 static inline const char *AT_string (dw_attr_ref);
3138 static enum dwarf_form AT_string_form (dw_attr_ref);
3139 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3140 static void add_AT_specification (dw_die_ref, dw_die_ref);
3141 static inline dw_die_ref AT_ref (dw_attr_ref);
3142 static inline int AT_ref_external (dw_attr_ref);
3143 static inline void set_AT_ref_external (dw_attr_ref, int);
3144 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3145 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3146 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3147 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3148                              dw_loc_list_ref);
3149 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3150 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3151 static inline rtx AT_addr (dw_attr_ref);
3152 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3153 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3154 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3155 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3156                            unsigned HOST_WIDE_INT);
3157 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3158                                unsigned long);
3159 static inline const char *AT_lbl (dw_attr_ref);
3160 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3161 static const char *get_AT_low_pc (dw_die_ref);
3162 static const char *get_AT_hi_pc (dw_die_ref);
3163 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3164 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3165 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3166 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3167 static bool is_cxx (void);
3168 static bool is_fortran (void);
3169 static bool is_ada (void);
3170 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3171 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3172 static void add_child_die (dw_die_ref, dw_die_ref);
3173 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3174 static dw_die_ref lookup_type_die (tree);
3175 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3176 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3177 static void equate_type_number_to_die (tree, dw_die_ref);
3178 static hashval_t decl_die_table_hash (const void *);
3179 static int decl_die_table_eq (const void *, const void *);
3180 static dw_die_ref lookup_decl_die (tree);
3181 static hashval_t common_block_die_table_hash (const void *);
3182 static int common_block_die_table_eq (const void *, const void *);
3183 static hashval_t decl_loc_table_hash (const void *);
3184 static int decl_loc_table_eq (const void *, const void *);
3185 static var_loc_list *lookup_decl_loc (const_tree);
3186 static void equate_decl_number_to_die (tree, dw_die_ref);
3187 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3188 static void print_spaces (FILE *);
3189 static void print_die (dw_die_ref, FILE *);
3190 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3191 static dw_die_ref pop_compile_unit (dw_die_ref);
3192 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3193 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3194 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3195 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3196 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3197 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3198 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3199                                    struct md5_ctx *, int *);
3200 struct checksum_attributes;
3201 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3202 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3203 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3204 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3205 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3206 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3207 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3208 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3209 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3210 static void compute_section_prefix (dw_die_ref);
3211 static int is_type_die (dw_die_ref);
3212 static int is_comdat_die (dw_die_ref);
3213 static int is_symbol_die (dw_die_ref);
3214 static void assign_symbol_names (dw_die_ref);
3215 static void break_out_includes (dw_die_ref);
3216 static int is_declaration_die (dw_die_ref);
3217 static int should_move_die_to_comdat (dw_die_ref);
3218 static dw_die_ref clone_as_declaration (dw_die_ref);
3219 static dw_die_ref clone_die (dw_die_ref);
3220 static dw_die_ref clone_tree (dw_die_ref);
3221 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3222 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3223 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3224 static dw_die_ref generate_skeleton (dw_die_ref);
3225 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3226                                                          dw_die_ref);
3227 static void break_out_comdat_types (dw_die_ref);
3228 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3229 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3230 static void copy_decls_for_unworthy_types (dw_die_ref);
3231
3232 static hashval_t htab_cu_hash (const void *);
3233 static int htab_cu_eq (const void *, const void *);
3234 static void htab_cu_del (void *);
3235 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3236 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3237 static void add_sibling_attributes (dw_die_ref);
3238 static void build_abbrev_table (dw_die_ref);
3239 static void output_location_lists (dw_die_ref);
3240 static int constant_size (unsigned HOST_WIDE_INT);
3241 static unsigned long size_of_die (dw_die_ref);
3242 static void calc_die_sizes (dw_die_ref);
3243 static void calc_base_type_die_sizes (void);
3244 static void mark_dies (dw_die_ref);
3245 static void unmark_dies (dw_die_ref);
3246 static void unmark_all_dies (dw_die_ref);
3247 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3248 static unsigned long size_of_aranges (void);
3249 static enum dwarf_form value_format (dw_attr_ref);
3250 static void output_value_format (dw_attr_ref);
3251 static void output_abbrev_section (void);
3252 static void output_die_symbol (dw_die_ref);
3253 static void output_die (dw_die_ref);
3254 static void output_compilation_unit_header (void);
3255 static void output_comp_unit (dw_die_ref, int);
3256 static void output_comdat_type_unit (comdat_type_node *);
3257 static const char *dwarf2_name (tree, int);
3258 static void add_pubname (tree, dw_die_ref);
3259 static void add_pubname_string (const char *, dw_die_ref);
3260 static void add_pubtype (tree, dw_die_ref);
3261 static void output_pubnames (VEC (pubname_entry,gc) *);
3262 static void output_aranges (unsigned long);
3263 static unsigned int add_ranges_num (int);
3264 static unsigned int add_ranges (const_tree);
3265 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3266                                   bool *);
3267 static void output_ranges (void);
3268 static dw_line_info_table *new_line_info_table (void);
3269 static void output_line_info (void);
3270 static void output_file_names (void);
3271 static dw_die_ref base_type_die (tree);
3272 static int is_base_type (tree);
3273 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3274 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3275 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3276 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3277 static int type_is_enum (const_tree);
3278 static unsigned int dbx_reg_number (const_rtx);
3279 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3280 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3281 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3282                                                 enum var_init_status);
3283 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3284                                                      enum var_init_status);
3285 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3286                                          enum var_init_status);
3287 static int is_based_loc (const_rtx);
3288 static int resolve_one_addr (rtx *, void *);
3289 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3290                                                enum var_init_status);
3291 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3292                                         enum var_init_status);
3293 static dw_loc_list_ref loc_list_from_tree (tree, int);
3294 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3295 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3296 static tree field_type (const_tree);
3297 static unsigned int simple_type_align_in_bits (const_tree);
3298 static unsigned int simple_decl_align_in_bits (const_tree);
3299 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3300 static HOST_WIDE_INT field_byte_offset (const_tree);
3301 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3302                                          dw_loc_list_ref);
3303 static void add_data_member_location_attribute (dw_die_ref, tree);
3304 static bool add_const_value_attribute (dw_die_ref, rtx);
3305 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3306 static void insert_double (double_int, unsigned char *);
3307 static void insert_float (const_rtx, unsigned char *);
3308 static rtx rtl_for_decl_location (tree);
3309 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3310                                                    enum dwarf_attribute);
3311 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3312 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3313 static void add_name_attribute (dw_die_ref, const char *);
3314 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3315 static void add_comp_dir_attribute (dw_die_ref);
3316 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3317 static void add_subscript_info (dw_die_ref, tree, bool);
3318 static void add_byte_size_attribute (dw_die_ref, tree);
3319 static void add_bit_offset_attribute (dw_die_ref, tree);
3320 static void add_bit_size_attribute (dw_die_ref, tree);
3321 static void add_prototyped_attribute (dw_die_ref, tree);
3322 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3323 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3324 static void add_src_coords_attributes (dw_die_ref, tree);
3325 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3326 static void push_decl_scope (tree);
3327 static void pop_decl_scope (void);
3328 static dw_die_ref scope_die_for (tree, dw_die_ref);
3329 static inline int local_scope_p (dw_die_ref);
3330 static inline int class_scope_p (dw_die_ref);
3331 static inline int class_or_namespace_scope_p (dw_die_ref);
3332 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3333 static void add_calling_convention_attribute (dw_die_ref, tree);
3334 static const char *type_tag (const_tree);
3335 static tree member_declared_type (const_tree);
3336 #if 0
3337 static const char *decl_start_label (tree);
3338 #endif
3339 static void gen_array_type_die (tree, dw_die_ref);
3340 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3341 #if 0
3342 static void gen_entry_point_die (tree, dw_die_ref);
3343 #endif
3344 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3345 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3346 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3347 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3348 static void gen_formal_types_die (tree, dw_die_ref);
3349 static void gen_subprogram_die (tree, dw_die_ref);
3350 static void gen_variable_die (tree, tree, dw_die_ref);
3351 static void gen_const_die (tree, dw_die_ref);
3352 static void gen_label_die (tree, dw_die_ref);
3353 static void gen_lexical_block_die (tree, dw_die_ref, int);
3354 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3355 static void gen_field_die (tree, dw_die_ref);
3356 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3357 static dw_die_ref gen_compile_unit_die (const char *);
3358 static void gen_inheritance_die (tree, tree, dw_die_ref);
3359 static void gen_member_die (tree, dw_die_ref);
3360 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3361                                                 enum debug_info_usage);
3362 static void gen_subroutine_type_die (tree, dw_die_ref);
3363 static void gen_typedef_die (tree, dw_die_ref);
3364 static void gen_type_die (tree, dw_die_ref);
3365 static void gen_block_die (tree, dw_die_ref, int);
3366 static void decls_for_scope (tree, dw_die_ref, int);
3367 static inline int is_redundant_typedef (const_tree);
3368 static bool is_naming_typedef_decl (const_tree);
3369 static inline dw_die_ref get_context_die (tree);
3370 static void gen_namespace_die (tree, dw_die_ref);
3371 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3372 static dw_die_ref force_decl_die (tree);
3373 static dw_die_ref force_type_die (tree);
3374 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3375 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3376 static struct dwarf_file_data * lookup_filename (const char *);
3377 static void retry_incomplete_types (void);
3378 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3379 static void gen_generic_params_dies (tree);
3380 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3381 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3382 static void splice_child_die (dw_die_ref, dw_die_ref);
3383 static int file_info_cmp (const void *, const void *);
3384 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3385                                      const char *, const char *);
3386 static void output_loc_list (dw_loc_list_ref);
3387 static char *gen_internal_sym (const char *);
3388
3389 static void prune_unmark_dies (dw_die_ref);
3390 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3391 static void prune_unused_types_mark (dw_die_ref, int);
3392 static void prune_unused_types_walk (dw_die_ref);
3393 static void prune_unused_types_walk_attribs (dw_die_ref);
3394 static void prune_unused_types_prune (dw_die_ref);
3395 static void prune_unused_types (void);
3396 static int maybe_emit_file (struct dwarf_file_data *fd);
3397 static inline const char *AT_vms_delta1 (dw_attr_ref);
3398 static inline const char *AT_vms_delta2 (dw_attr_ref);
3399 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3400                                      const char *, const char *);
3401 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3402 static void gen_remaining_tmpl_value_param_die_attribute (void);
3403 static bool generic_type_p (tree);
3404 static void schedule_generic_params_dies_gen (tree t);
3405 static void gen_scheduled_generic_parms_dies (void);
3406
3407 /* Section names used to hold DWARF debugging information.  */
3408 #ifndef DEBUG_INFO_SECTION
3409 #define DEBUG_INFO_SECTION      ".debug_info"
3410 #endif
3411 #ifndef DEBUG_ABBREV_SECTION
3412 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3413 #endif
3414 #ifndef DEBUG_ARANGES_SECTION
3415 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3416 #endif
3417 #ifndef DEBUG_MACINFO_SECTION
3418 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3419 #endif
3420 #ifndef DEBUG_LINE_SECTION
3421 #define DEBUG_LINE_SECTION      ".debug_line"
3422 #endif
3423 #ifndef DEBUG_LOC_SECTION
3424 #define DEBUG_LOC_SECTION       ".debug_loc"
3425 #endif
3426 #ifndef DEBUG_PUBNAMES_SECTION
3427 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3428 #endif
3429 #ifndef DEBUG_PUBTYPES_SECTION
3430 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3431 #endif
3432 #ifndef DEBUG_STR_SECTION
3433 #define DEBUG_STR_SECTION       ".debug_str"
3434 #endif
3435 #ifndef DEBUG_RANGES_SECTION
3436 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3437 #endif
3438
3439 /* Standard ELF section names for compiled code and data.  */
3440 #ifndef TEXT_SECTION_NAME
3441 #define TEXT_SECTION_NAME       ".text"
3442 #endif
3443
3444 /* Section flags for .debug_str section.  */
3445 #define DEBUG_STR_SECTION_FLAGS \
3446   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3447    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3448    : SECTION_DEBUG)
3449
3450 /* Labels we insert at beginning sections we can reference instead of
3451    the section names themselves.  */
3452
3453 #ifndef TEXT_SECTION_LABEL
3454 #define TEXT_SECTION_LABEL              "Ltext"
3455 #endif
3456 #ifndef COLD_TEXT_SECTION_LABEL
3457 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3458 #endif
3459 #ifndef DEBUG_LINE_SECTION_LABEL
3460 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3461 #endif
3462 #ifndef DEBUG_INFO_SECTION_LABEL
3463 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3464 #endif
3465 #ifndef DEBUG_ABBREV_SECTION_LABEL
3466 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3467 #endif
3468 #ifndef DEBUG_LOC_SECTION_LABEL
3469 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3470 #endif
3471 #ifndef DEBUG_RANGES_SECTION_LABEL
3472 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3473 #endif
3474 #ifndef DEBUG_MACINFO_SECTION_LABEL
3475 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3476 #endif
3477
3478
3479 /* Definitions of defaults for formats and names of various special
3480    (artificial) labels which may be generated within this file (when the -g
3481    options is used and DWARF2_DEBUGGING_INFO is in effect.
3482    If necessary, these may be overridden from within the tm.h file, but
3483    typically, overriding these defaults is unnecessary.  */
3484
3485 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3495
3496 #ifndef TEXT_END_LABEL
3497 #define TEXT_END_LABEL          "Letext"
3498 #endif
3499 #ifndef COLD_END_LABEL
3500 #define COLD_END_LABEL          "Letext_cold"
3501 #endif
3502 #ifndef BLOCK_BEGIN_LABEL
3503 #define BLOCK_BEGIN_LABEL       "LBB"
3504 #endif
3505 #ifndef BLOCK_END_LABEL
3506 #define BLOCK_END_LABEL         "LBE"
3507 #endif
3508 #ifndef LINE_CODE_LABEL
3509 #define LINE_CODE_LABEL         "LM"
3510 #endif
3511
3512 \f
3513 /* Return the root of the DIE's built for the current compilation unit.  */
3514 static dw_die_ref
3515 comp_unit_die (void)
3516 {
3517   if (!single_comp_unit_die)
3518     single_comp_unit_die = gen_compile_unit_die (NULL);
3519   return single_comp_unit_die;
3520 }
3521
3522 /* We allow a language front-end to designate a function that is to be
3523    called to "demangle" any name before it is put into a DIE.  */
3524
3525 static const char *(*demangle_name_func) (const char *);
3526
3527 void
3528 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3529 {
3530   demangle_name_func = func;
3531 }
3532
3533 /* Test if rtl node points to a pseudo register.  */
3534
3535 static inline int
3536 is_pseudo_reg (const_rtx rtl)
3537 {
3538   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3539           || (GET_CODE (rtl) == SUBREG
3540               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3541 }
3542
3543 /* Return a reference to a type, with its const and volatile qualifiers
3544    removed.  */
3545
3546 static inline tree
3547 type_main_variant (tree type)
3548 {
3549   type = TYPE_MAIN_VARIANT (type);
3550
3551   /* ??? There really should be only one main variant among any group of
3552      variants of a given type (and all of the MAIN_VARIANT values for all
3553      members of the group should point to that one type) but sometimes the C
3554      front-end messes this up for array types, so we work around that bug
3555      here.  */
3556   if (TREE_CODE (type) == ARRAY_TYPE)
3557     while (type != TYPE_MAIN_VARIANT (type))
3558       type = TYPE_MAIN_VARIANT (type);
3559
3560   return type;
3561 }
3562
3563 /* Return nonzero if the given type node represents a tagged type.  */
3564
3565 static inline int
3566 is_tagged_type (const_tree type)
3567 {
3568   enum tree_code code = TREE_CODE (type);
3569
3570   return (code == RECORD_TYPE || code == UNION_TYPE
3571           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3572 }
3573
3574 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3575
3576 static void
3577 get_ref_die_offset_label (char *label, dw_die_ref ref)
3578 {
3579   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3580 }
3581
3582 /* Return die_offset of a DIE reference to a base type.  */
3583
3584 static unsigned long int
3585 get_base_type_offset (dw_die_ref ref)
3586 {
3587   if (ref->die_offset)
3588     return ref->die_offset;
3589   if (comp_unit_die ()->die_abbrev)
3590     {
3591       calc_base_type_die_sizes ();
3592       gcc_assert (ref->die_offset);
3593     }
3594   return ref->die_offset;
3595 }
3596
3597 /* Return die_offset of a DIE reference other than base type.  */
3598
3599 static unsigned long int
3600 get_ref_die_offset (dw_die_ref ref)
3601 {
3602   gcc_assert (ref->die_offset);
3603   return ref->die_offset;
3604 }
3605
3606 /* Convert a DIE tag into its string name.  */
3607
3608 static const char *
3609 dwarf_tag_name (unsigned int tag)
3610 {
3611   switch (tag)
3612     {
3613     case DW_TAG_padding:
3614       return "DW_TAG_padding";
3615     case DW_TAG_array_type:
3616       return "DW_TAG_array_type";
3617     case DW_TAG_class_type:
3618       return "DW_TAG_class_type";
3619     case DW_TAG_entry_point:
3620       return "DW_TAG_entry_point";
3621     case DW_TAG_enumeration_type:
3622       return "DW_TAG_enumeration_type";
3623     case DW_TAG_formal_parameter:
3624       return "DW_TAG_formal_parameter";
3625     case DW_TAG_imported_declaration:
3626       return "DW_TAG_imported_declaration";
3627     case DW_TAG_label:
3628       return "DW_TAG_label";
3629     case DW_TAG_lexical_block:
3630       return "DW_TAG_lexical_block";
3631     case DW_TAG_member:
3632       return "DW_TAG_member";
3633     case DW_TAG_pointer_type:
3634       return "DW_TAG_pointer_type";
3635     case DW_TAG_reference_type:
3636       return "DW_TAG_reference_type";
3637     case DW_TAG_compile_unit:
3638       return "DW_TAG_compile_unit";
3639     case DW_TAG_string_type:
3640       return "DW_TAG_string_type";
3641     case DW_TAG_structure_type:
3642       return "DW_TAG_structure_type";
3643     case DW_TAG_subroutine_type:
3644       return "DW_TAG_subroutine_type";
3645     case DW_TAG_typedef:
3646       return "DW_TAG_typedef";
3647     case DW_TAG_union_type:
3648       return "DW_TAG_union_type";
3649     case DW_TAG_unspecified_parameters:
3650       return "DW_TAG_unspecified_parameters";
3651     case DW_TAG_variant:
3652       return "DW_TAG_variant";
3653     case DW_TAG_common_block:
3654       return "DW_TAG_common_block";
3655     case DW_TAG_common_inclusion:
3656       return "DW_TAG_common_inclusion";
3657     case DW_TAG_inheritance:
3658       return "DW_TAG_inheritance";
3659     case DW_TAG_inlined_subroutine:
3660       return "DW_TAG_inlined_subroutine";
3661     case DW_TAG_module:
3662       return "DW_TAG_module";
3663     case DW_TAG_ptr_to_member_type:
3664       return "DW_TAG_ptr_to_member_type";
3665     case DW_TAG_set_type:
3666       return "DW_TAG_set_type";
3667     case DW_TAG_subrange_type:
3668       return "DW_TAG_subrange_type";
3669     case DW_TAG_with_stmt:
3670       return "DW_TAG_with_stmt";
3671     case DW_TAG_access_declaration:
3672       return "DW_TAG_access_declaration";
3673     case DW_TAG_base_type:
3674       return "DW_TAG_base_type";
3675     case DW_TAG_catch_block:
3676       return "DW_TAG_catch_block";
3677     case DW_TAG_const_type:
3678       return "DW_TAG_const_type";
3679     case DW_TAG_constant:
3680       return "DW_TAG_constant";
3681     case DW_TAG_enumerator:
3682       return "DW_TAG_enumerator";
3683     case DW_TAG_file_type:
3684       return "DW_TAG_file_type";
3685     case DW_TAG_friend:
3686       return "DW_TAG_friend";
3687     case DW_TAG_namelist:
3688       return "DW_TAG_namelist";
3689     case DW_TAG_namelist_item:
3690       return "DW_TAG_namelist_item";
3691     case DW_TAG_packed_type:
3692       return "DW_TAG_packed_type";
3693     case DW_TAG_subprogram:
3694       return "DW_TAG_subprogram";
3695     case DW_TAG_template_type_param:
3696       return "DW_TAG_template_type_param";
3697     case DW_TAG_template_value_param:
3698       return "DW_TAG_template_value_param";
3699     case DW_TAG_thrown_type:
3700       return "DW_TAG_thrown_type";
3701     case DW_TAG_try_block:
3702       return "DW_TAG_try_block";
3703     case DW_TAG_variant_part:
3704       return "DW_TAG_variant_part";
3705     case DW_TAG_variable:
3706       return "DW_TAG_variable";
3707     case DW_TAG_volatile_type:
3708       return "DW_TAG_volatile_type";
3709     case DW_TAG_dwarf_procedure:
3710       return "DW_TAG_dwarf_procedure";
3711     case DW_TAG_restrict_type:
3712       return "DW_TAG_restrict_type";
3713     case DW_TAG_interface_type:
3714       return "DW_TAG_interface_type";
3715     case DW_TAG_namespace:
3716       return "DW_TAG_namespace";
3717     case DW_TAG_imported_module:
3718       return "DW_TAG_imported_module";
3719     case DW_TAG_unspecified_type:
3720       return "DW_TAG_unspecified_type";
3721     case DW_TAG_partial_unit:
3722       return "DW_TAG_partial_unit";
3723     case DW_TAG_imported_unit:
3724       return "DW_TAG_imported_unit";
3725     case DW_TAG_condition:
3726       return "DW_TAG_condition";
3727     case DW_TAG_shared_type:
3728       return "DW_TAG_shared_type";
3729     case DW_TAG_type_unit:
3730       return "DW_TAG_type_unit";
3731     case DW_TAG_rvalue_reference_type:
3732       return "DW_TAG_rvalue_reference_type";
3733     case DW_TAG_template_alias:
3734       return "DW_TAG_template_alias";
3735     case DW_TAG_GNU_template_parameter_pack:
3736       return "DW_TAG_GNU_template_parameter_pack";
3737     case DW_TAG_GNU_formal_parameter_pack:
3738       return "DW_TAG_GNU_formal_parameter_pack";
3739     case DW_TAG_MIPS_loop:
3740       return "DW_TAG_MIPS_loop";
3741     case DW_TAG_format_label:
3742       return "DW_TAG_format_label";
3743     case DW_TAG_function_template:
3744       return "DW_TAG_function_template";
3745     case DW_TAG_class_template:
3746       return "DW_TAG_class_template";
3747     case DW_TAG_GNU_BINCL:
3748       return "DW_TAG_GNU_BINCL";
3749     case DW_TAG_GNU_EINCL:
3750       return "DW_TAG_GNU_EINCL";
3751     case DW_TAG_GNU_template_template_param:
3752       return "DW_TAG_GNU_template_template_param";
3753     case DW_TAG_GNU_call_site:
3754       return "DW_TAG_GNU_call_site";
3755     case DW_TAG_GNU_call_site_parameter:
3756       return "DW_TAG_GNU_call_site_parameter";
3757     default:
3758       return "DW_TAG_<unknown>";
3759     }
3760 }
3761
3762 /* Convert a DWARF attribute code into its string name.  */
3763
3764 static const char *
3765 dwarf_attr_name (unsigned int attr)
3766 {
3767   switch (attr)
3768     {
3769     case DW_AT_sibling:
3770       return "DW_AT_sibling";
3771     case DW_AT_location:
3772       return "DW_AT_location";
3773     case DW_AT_name:
3774       return "DW_AT_name";
3775     case DW_AT_ordering:
3776       return "DW_AT_ordering";
3777     case DW_AT_subscr_data:
3778       return "DW_AT_subscr_data";
3779     case DW_AT_byte_size:
3780       return "DW_AT_byte_size";
3781     case DW_AT_bit_offset:
3782       return "DW_AT_bit_offset";
3783     case DW_AT_bit_size:
3784       return "DW_AT_bit_size";
3785     case DW_AT_element_list:
3786       return "DW_AT_element_list";
3787     case DW_AT_stmt_list:
3788       return "DW_AT_stmt_list";
3789     case DW_AT_low_pc:
3790       return "DW_AT_low_pc";
3791     case DW_AT_high_pc:
3792       return "DW_AT_high_pc";
3793     case DW_AT_language:
3794       return "DW_AT_language";
3795     case DW_AT_member:
3796       return "DW_AT_member";
3797     case DW_AT_discr:
3798       return "DW_AT_discr";
3799     case DW_AT_discr_value:
3800       return "DW_AT_discr_value";
3801     case DW_AT_visibility:
3802       return "DW_AT_visibility";
3803     case DW_AT_import:
3804       return "DW_AT_import";
3805     case DW_AT_string_length:
3806       return "DW_AT_string_length";
3807     case DW_AT_common_reference:
3808       return "DW_AT_common_reference";
3809     case DW_AT_comp_dir:
3810       return "DW_AT_comp_dir";
3811     case DW_AT_const_value:
3812       return "DW_AT_const_value";
3813     case DW_AT_containing_type:
3814       return "DW_AT_containing_type";
3815     case DW_AT_default_value:
3816       return "DW_AT_default_value";
3817     case DW_AT_inline:
3818       return "DW_AT_inline";
3819     case DW_AT_is_optional:
3820       return "DW_AT_is_optional";
3821     case DW_AT_lower_bound:
3822       return "DW_AT_lower_bound";
3823     case DW_AT_producer:
3824       return "DW_AT_producer";
3825     case DW_AT_prototyped:
3826       return "DW_AT_prototyped";
3827     case DW_AT_return_addr:
3828       return "DW_AT_return_addr";
3829     case DW_AT_start_scope:
3830       return "DW_AT_start_scope";
3831     case DW_AT_bit_stride:
3832       return "DW_AT_bit_stride";
3833     case DW_AT_upper_bound:
3834       return "DW_AT_upper_bound";
3835     case DW_AT_abstract_origin:
3836       return "DW_AT_abstract_origin";
3837     case DW_AT_accessibility:
3838       return "DW_AT_accessibility";
3839     case DW_AT_address_class:
3840       return "DW_AT_address_class";
3841     case DW_AT_artificial:
3842       return "DW_AT_artificial";
3843     case DW_AT_base_types:
3844       return "DW_AT_base_types";
3845     case DW_AT_calling_convention:
3846       return "DW_AT_calling_convention";
3847     case DW_AT_count:
3848       return "DW_AT_count";
3849     case DW_AT_data_member_location:
3850       return "DW_AT_data_member_location";
3851     case DW_AT_decl_column:
3852       return "DW_AT_decl_column";
3853     case DW_AT_decl_file:
3854       return "DW_AT_decl_file";
3855     case DW_AT_decl_line:
3856       return "DW_AT_decl_line";
3857     case DW_AT_declaration:
3858       return "DW_AT_declaration";
3859     case DW_AT_discr_list:
3860       return "DW_AT_discr_list";
3861     case DW_AT_encoding:
3862       return "DW_AT_encoding";
3863     case DW_AT_external:
3864       return "DW_AT_external";
3865     case DW_AT_explicit:
3866       return "DW_AT_explicit";
3867     case DW_AT_frame_base:
3868       return "DW_AT_frame_base";
3869     case DW_AT_friend:
3870       return "DW_AT_friend";
3871     case DW_AT_identifier_case:
3872       return "DW_AT_identifier_case";
3873     case DW_AT_macro_info:
3874       return "DW_AT_macro_info";
3875     case DW_AT_namelist_items:
3876       return "DW_AT_namelist_items";
3877     case DW_AT_priority:
3878       return "DW_AT_priority";
3879     case DW_AT_segment:
3880       return "DW_AT_segment";
3881     case DW_AT_specification:
3882       return "DW_AT_specification";
3883     case DW_AT_static_link:
3884       return "DW_AT_static_link";
3885     case DW_AT_type:
3886       return "DW_AT_type";
3887     case DW_AT_use_location:
3888       return "DW_AT_use_location";
3889     case DW_AT_variable_parameter:
3890       return "DW_AT_variable_parameter";
3891     case DW_AT_virtuality:
3892       return "DW_AT_virtuality";
3893     case DW_AT_vtable_elem_location:
3894       return "DW_AT_vtable_elem_location";
3895
3896     case DW_AT_allocated:
3897       return "DW_AT_allocated";
3898     case DW_AT_associated:
3899       return "DW_AT_associated";
3900     case DW_AT_data_location:
3901       return "DW_AT_data_location";
3902     case DW_AT_byte_stride:
3903       return "DW_AT_byte_stride";
3904     case DW_AT_entry_pc:
3905       return "DW_AT_entry_pc";
3906     case DW_AT_use_UTF8:
3907       return "DW_AT_use_UTF8";
3908     case DW_AT_extension:
3909       return "DW_AT_extension";
3910     case DW_AT_ranges:
3911       return "DW_AT_ranges";
3912     case DW_AT_trampoline:
3913       return "DW_AT_trampoline";
3914     case DW_AT_call_column:
3915       return "DW_AT_call_column";
3916     case DW_AT_call_file:
3917       return "DW_AT_call_file";
3918     case DW_AT_call_line:
3919       return "DW_AT_call_line";
3920     case DW_AT_object_pointer:
3921       return "DW_AT_object_pointer";
3922
3923     case DW_AT_signature:
3924       return "DW_AT_signature";
3925     case DW_AT_main_subprogram:
3926       return "DW_AT_main_subprogram";
3927     case DW_AT_data_bit_offset:
3928       return "DW_AT_data_bit_offset";
3929     case DW_AT_const_expr:
3930       return "DW_AT_const_expr";
3931     case DW_AT_enum_class:
3932       return "DW_AT_enum_class";
3933     case DW_AT_linkage_name:
3934       return "DW_AT_linkage_name";
3935
3936     case DW_AT_MIPS_fde:
3937       return "DW_AT_MIPS_fde";
3938     case DW_AT_MIPS_loop_begin:
3939       return "DW_AT_MIPS_loop_begin";
3940     case DW_AT_MIPS_tail_loop_begin:
3941       return "DW_AT_MIPS_tail_loop_begin";
3942     case DW_AT_MIPS_epilog_begin:
3943       return "DW_AT_MIPS_epilog_begin";
3944 #if VMS_DEBUGGING_INFO
3945     case DW_AT_HP_prologue:
3946       return "DW_AT_HP_prologue";
3947 #else
3948     case DW_AT_MIPS_loop_unroll_factor:
3949       return "DW_AT_MIPS_loop_unroll_factor";
3950 #endif
3951     case DW_AT_MIPS_software_pipeline_depth:
3952       return "DW_AT_MIPS_software_pipeline_depth";
3953     case DW_AT_MIPS_linkage_name:
3954       return "DW_AT_MIPS_linkage_name";
3955 #if VMS_DEBUGGING_INFO
3956     case DW_AT_HP_epilogue:
3957       return "DW_AT_HP_epilogue";
3958 #else
3959     case DW_AT_MIPS_stride:
3960       return "DW_AT_MIPS_stride";
3961 #endif
3962     case DW_AT_MIPS_abstract_name:
3963       return "DW_AT_MIPS_abstract_name";
3964     case DW_AT_MIPS_clone_origin:
3965       return "DW_AT_MIPS_clone_origin";
3966     case DW_AT_MIPS_has_inlines:
3967       return "DW_AT_MIPS_has_inlines";
3968
3969     case DW_AT_sf_names:
3970       return "DW_AT_sf_names";
3971     case DW_AT_src_info:
3972       return "DW_AT_src_info";
3973     case DW_AT_mac_info:
3974       return "DW_AT_mac_info";
3975     case DW_AT_src_coords:
3976       return "DW_AT_src_coords";
3977     case DW_AT_body_begin:
3978       return "DW_AT_body_begin";
3979     case DW_AT_body_end:
3980       return "DW_AT_body_end";
3981
3982     case DW_AT_GNU_vector:
3983       return "DW_AT_GNU_vector";
3984     case DW_AT_GNU_guarded_by:
3985       return "DW_AT_GNU_guarded_by";
3986     case DW_AT_GNU_pt_guarded_by:
3987       return "DW_AT_GNU_pt_guarded_by";
3988     case DW_AT_GNU_guarded:
3989       return "DW_AT_GNU_guarded";
3990     case DW_AT_GNU_pt_guarded:
3991       return "DW_AT_GNU_pt_guarded";
3992     case DW_AT_GNU_locks_excluded:
3993       return "DW_AT_GNU_locks_excluded";
3994     case DW_AT_GNU_exclusive_locks_required:
3995       return "DW_AT_GNU_exclusive_locks_required";
3996     case DW_AT_GNU_shared_locks_required:
3997       return "DW_AT_GNU_shared_locks_required";
3998     case DW_AT_GNU_odr_signature:
3999       return "DW_AT_GNU_odr_signature";
4000     case DW_AT_GNU_template_name:
4001       return "DW_AT_GNU_template_name";
4002     case DW_AT_GNU_call_site_value:
4003       return "DW_AT_GNU_call_site_value";
4004     case DW_AT_GNU_call_site_data_value:
4005       return "DW_AT_GNU_call_site_data_value";
4006     case DW_AT_GNU_call_site_target:
4007       return "DW_AT_GNU_call_site_target";
4008     case DW_AT_GNU_call_site_target_clobbered:
4009       return "DW_AT_GNU_call_site_target_clobbered";
4010     case DW_AT_GNU_tail_call:
4011       return "DW_AT_GNU_tail_call";
4012     case DW_AT_GNU_all_tail_call_sites:
4013       return "DW_AT_GNU_all_tail_call_sites";
4014     case DW_AT_GNU_all_call_sites:
4015       return "DW_AT_GNU_all_call_sites";
4016     case DW_AT_GNU_all_source_call_sites:
4017       return "DW_AT_GNU_all_source_call_sites";
4018
4019     case DW_AT_GNAT_descriptive_type:
4020       return "DW_AT_GNAT_descriptive_type";
4021
4022     case DW_AT_VMS_rtnbeg_pd_address:
4023       return "DW_AT_VMS_rtnbeg_pd_address";
4024
4025     default:
4026       return "DW_AT_<unknown>";
4027     }
4028 }
4029
4030 /* Convert a DWARF value form code into its string name.  */
4031
4032 static const char *
4033 dwarf_form_name (unsigned int form)
4034 {
4035   switch (form)
4036     {
4037     case DW_FORM_addr:
4038       return "DW_FORM_addr";
4039     case DW_FORM_block2:
4040       return "DW_FORM_block2";
4041     case DW_FORM_block4:
4042       return "DW_FORM_block4";
4043     case DW_FORM_data2:
4044       return "DW_FORM_data2";
4045     case DW_FORM_data4:
4046       return "DW_FORM_data4";
4047     case DW_FORM_data8:
4048       return "DW_FORM_data8";
4049     case DW_FORM_string:
4050       return "DW_FORM_string";
4051     case DW_FORM_block:
4052       return "DW_FORM_block";
4053     case DW_FORM_block1:
4054       return "DW_FORM_block1";
4055     case DW_FORM_data1:
4056       return "DW_FORM_data1";
4057     case DW_FORM_flag:
4058       return "DW_FORM_flag";
4059     case DW_FORM_sdata:
4060       return "DW_FORM_sdata";
4061     case DW_FORM_strp:
4062       return "DW_FORM_strp";
4063     case DW_FORM_udata:
4064       return "DW_FORM_udata";
4065     case DW_FORM_ref_addr:
4066       return "DW_FORM_ref_addr";
4067     case DW_FORM_ref1:
4068       return "DW_FORM_ref1";
4069     case DW_FORM_ref2:
4070       return "DW_FORM_ref2";
4071     case DW_FORM_ref4:
4072       return "DW_FORM_ref4";
4073     case DW_FORM_ref8:
4074       return "DW_FORM_ref8";
4075     case DW_FORM_ref_udata:
4076       return "DW_FORM_ref_udata";
4077     case DW_FORM_indirect:
4078       return "DW_FORM_indirect";
4079     case DW_FORM_sec_offset:
4080       return "DW_FORM_sec_offset";
4081     case DW_FORM_exprloc:
4082       return "DW_FORM_exprloc";
4083     case DW_FORM_flag_present:
4084       return "DW_FORM_flag_present";
4085     case DW_FORM_ref_sig8:
4086       return "DW_FORM_ref_sig8";
4087     default:
4088       return "DW_FORM_<unknown>";
4089     }
4090 }
4091 \f
4092 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4093    instance of an inlined instance of a decl which is local to an inline
4094    function, so we have to trace all of the way back through the origin chain
4095    to find out what sort of node actually served as the original seed for the
4096    given block.  */
4097
4098 static tree
4099 decl_ultimate_origin (const_tree decl)
4100 {
4101   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4102     return NULL_TREE;
4103
4104   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4105      nodes in the function to point to themselves; ignore that if
4106      we're trying to output the abstract instance of this function.  */
4107   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4108     return NULL_TREE;
4109
4110   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4111      most distant ancestor, this should never happen.  */
4112   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4113
4114   return DECL_ABSTRACT_ORIGIN (decl);
4115 }
4116
4117 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4118    of a virtual function may refer to a base class, so we check the 'this'
4119    parameter.  */
4120
4121 static tree
4122 decl_class_context (tree decl)
4123 {
4124   tree context = NULL_TREE;
4125
4126   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4127     context = DECL_CONTEXT (decl);
4128   else
4129     context = TYPE_MAIN_VARIANT
4130       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4131
4132   if (context && !TYPE_P (context))
4133     context = NULL_TREE;
4134
4135   return context;
4136 }
4137 \f
4138 /* Add an attribute/value pair to a DIE.  */
4139
4140 static inline void
4141 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4142 {
4143   /* Maybe this should be an assert?  */
4144   if (die == NULL)
4145     return;
4146
4147   if (die->die_attr == NULL)
4148     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4149   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4150 }
4151
4152 static inline enum dw_val_class
4153 AT_class (dw_attr_ref a)
4154 {
4155   return a->dw_attr_val.val_class;
4156 }
4157
4158 /* Add a flag value attribute to a DIE.  */
4159
4160 static inline void
4161 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4162 {
4163   dw_attr_node attr;
4164
4165   attr.dw_attr = attr_kind;
4166   attr.dw_attr_val.val_class = dw_val_class_flag;
4167   attr.dw_attr_val.v.val_flag = flag;
4168   add_dwarf_attr (die, &attr);
4169 }
4170
4171 static inline unsigned
4172 AT_flag (dw_attr_ref a)
4173 {
4174   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4175   return a->dw_attr_val.v.val_flag;
4176 }
4177
4178 /* Add a signed integer attribute value to a DIE.  */
4179
4180 static inline void
4181 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4182 {
4183   dw_attr_node attr;
4184
4185   attr.dw_attr = attr_kind;
4186   attr.dw_attr_val.val_class = dw_val_class_const;
4187   attr.dw_attr_val.v.val_int = int_val;
4188   add_dwarf_attr (die, &attr);
4189 }
4190
4191 static inline HOST_WIDE_INT
4192 AT_int (dw_attr_ref a)
4193 {
4194   gcc_assert (a && AT_class (a) == dw_val_class_const);
4195   return a->dw_attr_val.v.val_int;
4196 }
4197
4198 /* Add an unsigned integer attribute value to a DIE.  */
4199
4200 static inline void
4201 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4202                  unsigned HOST_WIDE_INT unsigned_val)
4203 {
4204   dw_attr_node attr;
4205
4206   attr.dw_attr = attr_kind;
4207   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4208   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4209   add_dwarf_attr (die, &attr);
4210 }
4211
4212 static inline unsigned HOST_WIDE_INT
4213 AT_unsigned (dw_attr_ref a)
4214 {
4215   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4216   return a->dw_attr_val.v.val_unsigned;
4217 }
4218
4219 /* Add an unsigned double integer attribute value to a DIE.  */
4220
4221 static inline void
4222 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4223                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4224 {
4225   dw_attr_node attr;
4226
4227   attr.dw_attr = attr_kind;
4228   attr.dw_attr_val.val_class = dw_val_class_const_double;
4229   attr.dw_attr_val.v.val_double.high = high;
4230   attr.dw_attr_val.v.val_double.low = low;
4231   add_dwarf_attr (die, &attr);
4232 }
4233
4234 /* Add a floating point attribute value to a DIE and return it.  */
4235
4236 static inline void
4237 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4238             unsigned int length, unsigned int elt_size, unsigned char *array)
4239 {
4240   dw_attr_node attr;
4241
4242   attr.dw_attr = attr_kind;
4243   attr.dw_attr_val.val_class = dw_val_class_vec;
4244   attr.dw_attr_val.v.val_vec.length = length;
4245   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4246   attr.dw_attr_val.v.val_vec.array = array;
4247   add_dwarf_attr (die, &attr);
4248 }
4249
4250 /* Add an 8-byte data attribute value to a DIE.  */
4251
4252 static inline void
4253 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4254               unsigned char data8[8])
4255 {
4256   dw_attr_node attr;
4257
4258   attr.dw_attr = attr_kind;
4259   attr.dw_attr_val.val_class = dw_val_class_data8;
4260   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4261   add_dwarf_attr (die, &attr);
4262 }
4263
4264 /* Hash and equality functions for debug_str_hash.  */
4265
4266 static hashval_t
4267 debug_str_do_hash (const void *x)
4268 {
4269   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4270 }
4271
4272 static int
4273 debug_str_eq (const void *x1, const void *x2)
4274 {
4275   return strcmp ((((const struct indirect_string_node *)x1)->str),
4276                  (const char *)x2) == 0;
4277 }
4278
4279 /* Add STR to the indirect string hash table.  */
4280
4281 static struct indirect_string_node *
4282 find_AT_string (const char *str)
4283 {
4284   struct indirect_string_node *node;
4285   void **slot;
4286
4287   if (! debug_str_hash)
4288     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4289                                       debug_str_eq, NULL);
4290
4291   slot = htab_find_slot_with_hash (debug_str_hash, str,
4292                                    htab_hash_string (str), INSERT);
4293   if (*slot == NULL)
4294     {
4295       node = ggc_alloc_cleared_indirect_string_node ();
4296       node->str = ggc_strdup (str);
4297       *slot = node;
4298     }
4299   else
4300     node = (struct indirect_string_node *) *slot;
4301
4302   node->refcount++;
4303   return node;
4304 }
4305
4306 /* Add a string attribute value to a DIE.  */
4307
4308 static inline void
4309 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4310 {
4311   dw_attr_node attr;
4312   struct indirect_string_node *node;
4313
4314   node = find_AT_string (str);
4315
4316   attr.dw_attr = attr_kind;
4317   attr.dw_attr_val.val_class = dw_val_class_str;
4318   attr.dw_attr_val.v.val_str = node;
4319   add_dwarf_attr (die, &attr);
4320 }
4321
4322 static inline const char *
4323 AT_string (dw_attr_ref a)
4324 {
4325   gcc_assert (a && AT_class (a) == dw_val_class_str);
4326   return a->dw_attr_val.v.val_str->str;
4327 }
4328
4329 /* Find out whether a string should be output inline in DIE
4330    or out-of-line in .debug_str section.  */
4331
4332 static enum dwarf_form
4333 AT_string_form (dw_attr_ref a)
4334 {
4335   struct indirect_string_node *node;
4336   unsigned int len;
4337   char label[32];
4338
4339   gcc_assert (a && AT_class (a) == dw_val_class_str);
4340
4341   node = a->dw_attr_val.v.val_str;
4342   if (node->form)
4343     return node->form;
4344
4345   len = strlen (node->str) + 1;
4346
4347   /* If the string is shorter or equal to the size of the reference, it is
4348      always better to put it inline.  */
4349   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4350     return node->form = DW_FORM_string;
4351
4352   /* If we cannot expect the linker to merge strings in .debug_str
4353      section, only put it into .debug_str if it is worth even in this
4354      single module.  */
4355   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4356       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4357       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4358     return node->form = DW_FORM_string;
4359
4360   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4361   ++dw2_string_counter;
4362   node->label = xstrdup (label);
4363
4364   return node->form = DW_FORM_strp;
4365 }
4366
4367 /* Add a DIE reference attribute value to a DIE.  */
4368
4369 static inline void
4370 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4371 {
4372   dw_attr_node attr;
4373
4374 #ifdef ENABLE_CHECKING
4375   gcc_assert (targ_die != NULL);
4376 #else
4377   /* With LTO we can end up trying to reference something we didn't create
4378      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4379   if (targ_die == NULL)
4380     return;
4381 #endif
4382
4383   attr.dw_attr = attr_kind;
4384   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4385   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4386   attr.dw_attr_val.v.val_die_ref.external = 0;
4387   add_dwarf_attr (die, &attr);
4388 }
4389
4390 /* Add an AT_specification attribute to a DIE, and also make the back
4391    pointer from the specification to the definition.  */
4392
4393 static inline void
4394 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4395 {
4396   add_AT_die_ref (die, DW_AT_specification, targ_die);
4397   gcc_assert (!targ_die->die_definition);
4398   targ_die->die_definition = die;
4399 }
4400
4401 static inline dw_die_ref
4402 AT_ref (dw_attr_ref a)
4403 {
4404   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4405   return a->dw_attr_val.v.val_die_ref.die;
4406 }
4407
4408 static inline int
4409 AT_ref_external (dw_attr_ref a)
4410 {
4411   if (a && AT_class (a) == dw_val_class_die_ref)
4412     return a->dw_attr_val.v.val_die_ref.external;
4413
4414   return 0;
4415 }
4416
4417 static inline void
4418 set_AT_ref_external (dw_attr_ref a, int i)
4419 {
4420   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4421   a->dw_attr_val.v.val_die_ref.external = i;
4422 }
4423
4424 /* Add an FDE reference attribute value to a DIE.  */
4425
4426 static inline void
4427 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4428 {
4429   dw_attr_node attr;
4430
4431   attr.dw_attr = attr_kind;
4432   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4433   attr.dw_attr_val.v.val_fde_index = targ_fde;
4434   add_dwarf_attr (die, &attr);
4435 }
4436
4437 /* Add a location description attribute value to a DIE.  */
4438
4439 static inline void
4440 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4441 {
4442   dw_attr_node attr;
4443
4444   attr.dw_attr = attr_kind;
4445   attr.dw_attr_val.val_class = dw_val_class_loc;
4446   attr.dw_attr_val.v.val_loc = loc;
4447   add_dwarf_attr (die, &attr);
4448 }
4449
4450 static inline dw_loc_descr_ref
4451 AT_loc (dw_attr_ref a)
4452 {
4453   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4454   return a->dw_attr_val.v.val_loc;
4455 }
4456
4457 static inline void
4458 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4459 {
4460   dw_attr_node attr;
4461
4462   attr.dw_attr = attr_kind;
4463   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4464   attr.dw_attr_val.v.val_loc_list = loc_list;
4465   add_dwarf_attr (die, &attr);
4466   have_location_lists = true;
4467 }
4468
4469 static inline dw_loc_list_ref
4470 AT_loc_list (dw_attr_ref a)
4471 {
4472   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4473   return a->dw_attr_val.v.val_loc_list;
4474 }
4475
4476 static inline dw_loc_list_ref *
4477 AT_loc_list_ptr (dw_attr_ref a)
4478 {
4479   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4480   return &a->dw_attr_val.v.val_loc_list;
4481 }
4482
4483 /* Add an address constant attribute value to a DIE.  */
4484
4485 static inline void
4486 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4487 {
4488   dw_attr_node attr;
4489
4490   attr.dw_attr = attr_kind;
4491   attr.dw_attr_val.val_class = dw_val_class_addr;
4492   attr.dw_attr_val.v.val_addr = addr;
4493   add_dwarf_attr (die, &attr);
4494 }
4495
4496 /* Get the RTX from to an address DIE attribute.  */
4497
4498 static inline rtx
4499 AT_addr (dw_attr_ref a)
4500 {
4501   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4502   return a->dw_attr_val.v.val_addr;
4503 }
4504
4505 /* Add a file attribute value to a DIE.  */
4506
4507 static inline void
4508 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4509              struct dwarf_file_data *fd)
4510 {
4511   dw_attr_node attr;
4512
4513   attr.dw_attr = attr_kind;
4514   attr.dw_attr_val.val_class = dw_val_class_file;
4515   attr.dw_attr_val.v.val_file = fd;
4516   add_dwarf_attr (die, &attr);
4517 }
4518
4519 /* Get the dwarf_file_data from a file DIE attribute.  */
4520
4521 static inline struct dwarf_file_data *
4522 AT_file (dw_attr_ref a)
4523 {
4524   gcc_assert (a && AT_class (a) == dw_val_class_file);
4525   return a->dw_attr_val.v.val_file;
4526 }
4527
4528 /* Add a vms delta attribute value to a DIE.  */
4529
4530 static inline void
4531 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4532                   const char *lbl1, const char *lbl2)
4533 {
4534   dw_attr_node attr;
4535
4536   attr.dw_attr = attr_kind;
4537   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4538   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4539   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4540   add_dwarf_attr (die, &attr);
4541 }
4542
4543 /* Add a label identifier attribute value to a DIE.  */
4544
4545 static inline void
4546 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4547 {
4548   dw_attr_node attr;
4549
4550   attr.dw_attr = attr_kind;
4551   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4552   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4553   add_dwarf_attr (die, &attr);
4554 }
4555
4556 /* Add a section offset attribute value to a DIE, an offset into the
4557    debug_line section.  */
4558
4559 static inline void
4560 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4561                 const char *label)
4562 {
4563   dw_attr_node attr;
4564
4565   attr.dw_attr = attr_kind;
4566   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4567   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4568   add_dwarf_attr (die, &attr);
4569 }
4570
4571 /* Add a section offset attribute value to a DIE, an offset into the
4572    debug_macinfo section.  */
4573
4574 static inline void
4575 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4576                const char *label)
4577 {
4578   dw_attr_node attr;
4579
4580   attr.dw_attr = attr_kind;
4581   attr.dw_attr_val.val_class = dw_val_class_macptr;
4582   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4583   add_dwarf_attr (die, &attr);
4584 }
4585
4586 /* Add an offset attribute value to a DIE.  */
4587
4588 static inline void
4589 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4590                unsigned HOST_WIDE_INT offset)
4591 {
4592   dw_attr_node attr;
4593
4594   attr.dw_attr = attr_kind;
4595   attr.dw_attr_val.val_class = dw_val_class_offset;
4596   attr.dw_attr_val.v.val_offset = offset;
4597   add_dwarf_attr (die, &attr);
4598 }
4599
4600 /* Add an range_list attribute value to a DIE.  */
4601
4602 static void
4603 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4604                    long unsigned int offset)
4605 {
4606   dw_attr_node attr;
4607
4608   attr.dw_attr = attr_kind;
4609   attr.dw_attr_val.val_class = dw_val_class_range_list;
4610   attr.dw_attr_val.v.val_offset = offset;
4611   add_dwarf_attr (die, &attr);
4612 }
4613
4614 /* Return the start label of a delta attribute.  */
4615
4616 static inline const char *
4617 AT_vms_delta1 (dw_attr_ref a)
4618 {
4619   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4620   return a->dw_attr_val.v.val_vms_delta.lbl1;
4621 }
4622
4623 /* Return the end label of a delta attribute.  */
4624
4625 static inline const char *
4626 AT_vms_delta2 (dw_attr_ref a)
4627 {
4628   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4629   return a->dw_attr_val.v.val_vms_delta.lbl2;
4630 }
4631
4632 static inline const char *
4633 AT_lbl (dw_attr_ref a)
4634 {
4635   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4636                     || AT_class (a) == dw_val_class_lineptr
4637                     || AT_class (a) == dw_val_class_macptr));
4638   return a->dw_attr_val.v.val_lbl_id;
4639 }
4640
4641 /* Get the attribute of type attr_kind.  */
4642
4643 static dw_attr_ref
4644 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4645 {
4646   dw_attr_ref a;
4647   unsigned ix;
4648   dw_die_ref spec = NULL;
4649
4650   if (! die)
4651     return NULL;
4652
4653   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4654     if (a->dw_attr == attr_kind)
4655       return a;
4656     else if (a->dw_attr == DW_AT_specification
4657              || a->dw_attr == DW_AT_abstract_origin)
4658       spec = AT_ref (a);
4659
4660   if (spec)
4661     return get_AT (spec, attr_kind);
4662
4663   return NULL;
4664 }
4665
4666 /* Return the "low pc" attribute value, typically associated with a subprogram
4667    DIE.  Return null if the "low pc" attribute is either not present, or if it
4668    cannot be represented as an assembler label identifier.  */
4669
4670 static inline const char *
4671 get_AT_low_pc (dw_die_ref die)
4672 {
4673   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4674
4675   return a ? AT_lbl (a) : NULL;
4676 }
4677
4678 /* Return the "high pc" attribute value, typically associated with a subprogram
4679    DIE.  Return null if the "high pc" attribute is either not present, or if it
4680    cannot be represented as an assembler label identifier.  */
4681
4682 static inline const char *
4683 get_AT_hi_pc (dw_die_ref die)
4684 {
4685   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4686
4687   return a ? AT_lbl (a) : NULL;
4688 }
4689
4690 /* Return the value of the string attribute designated by ATTR_KIND, or
4691    NULL if it is not present.  */
4692
4693 static inline const char *
4694 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4695 {
4696   dw_attr_ref a = get_AT (die, attr_kind);
4697
4698   return a ? AT_string (a) : NULL;
4699 }
4700
4701 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4702    if it is not present.  */
4703
4704 static inline int
4705 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4706 {
4707   dw_attr_ref a = get_AT (die, attr_kind);
4708
4709   return a ? AT_flag (a) : 0;
4710 }
4711
4712 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4713    if it is not present.  */
4714
4715 static inline unsigned
4716 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4717 {
4718   dw_attr_ref a = get_AT (die, attr_kind);
4719
4720   return a ? AT_unsigned (a) : 0;
4721 }
4722
4723 static inline dw_die_ref
4724 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4725 {
4726   dw_attr_ref a = get_AT (die, attr_kind);
4727
4728   return a ? AT_ref (a) : NULL;
4729 }
4730
4731 static inline struct dwarf_file_data *
4732 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4733 {
4734   dw_attr_ref a = get_AT (die, attr_kind);
4735
4736   return a ? AT_file (a) : NULL;
4737 }
4738
4739 /* Return TRUE if the language is C++.  */
4740
4741 static inline bool
4742 is_cxx (void)
4743 {
4744   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4745
4746   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4747 }
4748
4749 /* Return TRUE if the language is Fortran.  */
4750
4751 static inline bool
4752 is_fortran (void)
4753 {
4754   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4755
4756   return (lang == DW_LANG_Fortran77
4757           || lang == DW_LANG_Fortran90
4758           || lang == DW_LANG_Fortran95);
4759 }
4760
4761 /* Return TRUE if the language is Ada.  */
4762
4763 static inline bool
4764 is_ada (void)
4765 {
4766   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4767
4768   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4769 }
4770
4771 /* Remove the specified attribute if present.  */
4772
4773 static void
4774 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4775 {
4776   dw_attr_ref a;
4777   unsigned ix;
4778
4779   if (! die)
4780     return;
4781
4782   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4783     if (a->dw_attr == attr_kind)
4784       {
4785         if (AT_class (a) == dw_val_class_str)
4786           if (a->dw_attr_val.v.val_str->refcount)
4787             a->dw_attr_val.v.val_str->refcount--;
4788
4789         /* VEC_ordered_remove should help reduce the number of abbrevs
4790            that are needed.  */
4791         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4792         return;
4793       }
4794 }
4795
4796 /* Remove CHILD from its parent.  PREV must have the property that
4797    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4798
4799 static void
4800 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4801 {
4802   gcc_assert (child->die_parent == prev->die_parent);
4803   gcc_assert (prev->die_sib == child);
4804   if (prev == child)
4805     {
4806       gcc_assert (child->die_parent->die_child == child);
4807       prev = NULL;
4808     }
4809   else
4810     prev->die_sib = child->die_sib;
4811   if (child->die_parent->die_child == child)
4812     child->die_parent->die_child = prev;
4813 }
4814
4815 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4816    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4817
4818 static void
4819 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4820 {
4821   dw_die_ref parent = old_child->die_parent;
4822
4823   gcc_assert (parent == prev->die_parent);
4824   gcc_assert (prev->die_sib == old_child);
4825
4826   new_child->die_parent = parent;
4827   if (prev == old_child)
4828     {
4829       gcc_assert (parent->die_child == old_child);
4830       new_child->die_sib = new_child;
4831     }
4832   else
4833     {
4834       prev->die_sib = new_child;
4835       new_child->die_sib = old_child->die_sib;
4836     }
4837   if (old_child->die_parent->die_child == old_child)
4838     old_child->die_parent->die_child = new_child;
4839 }
4840
4841 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4842
4843 static void
4844 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4845 {
4846   dw_die_ref c;
4847   new_parent->die_child = old_parent->die_child;
4848   old_parent->die_child = NULL;
4849   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4850 }
4851
4852 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4853    matches TAG.  */
4854
4855 static void
4856 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4857 {
4858   dw_die_ref c;
4859
4860   c = die->die_child;
4861   if (c) do {
4862     dw_die_ref prev = c;
4863     c = c->die_sib;
4864     while (c->die_tag == tag)
4865       {
4866         remove_child_with_prev (c, prev);
4867         /* Might have removed every child.  */
4868         if (c == c->die_sib)
4869           return;
4870         c = c->die_sib;
4871       }
4872   } while (c != die->die_child);
4873 }
4874
4875 /* Add a CHILD_DIE as the last child of DIE.  */
4876
4877 static void
4878 add_child_die (dw_die_ref die, dw_die_ref child_die)
4879 {
4880   /* FIXME this should probably be an assert.  */
4881   if (! die || ! child_die)
4882     return;
4883   gcc_assert (die != child_die);
4884
4885   child_die->die_parent = die;
4886   if (die->die_child)
4887     {
4888       child_die->die_sib = die->die_child->die_sib;
4889       die->die_child->die_sib = child_die;
4890     }
4891   else
4892     child_die->die_sib = child_die;
4893   die->die_child = child_die;
4894 }
4895
4896 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4897    is the specification, to the end of PARENT's list of children.
4898    This is done by removing and re-adding it.  */
4899
4900 static void
4901 splice_child_die (dw_die_ref parent, dw_die_ref child)
4902 {
4903   dw_die_ref p;
4904
4905   /* We want the declaration DIE from inside the class, not the
4906      specification DIE at toplevel.  */
4907   if (child->die_parent != parent)
4908     {
4909       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4910
4911       if (tmp)
4912         child = tmp;
4913     }
4914
4915   gcc_assert (child->die_parent == parent
4916               || (child->die_parent
4917                   == get_AT_ref (parent, DW_AT_specification)));
4918
4919   for (p = child->die_parent->die_child; ; p = p->die_sib)
4920     if (p->die_sib == child)
4921       {
4922         remove_child_with_prev (child, p);
4923         break;
4924       }
4925
4926   add_child_die (parent, child);
4927 }
4928
4929 /* Return a pointer to a newly created DIE node.  */
4930
4931 static inline dw_die_ref
4932 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4933 {
4934   dw_die_ref die = ggc_alloc_cleared_die_node ();
4935
4936   die->die_tag = tag_value;
4937
4938   if (parent_die != NULL)
4939     add_child_die (parent_die, die);
4940   else
4941     {
4942       limbo_die_node *limbo_node;
4943
4944       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4945       limbo_node->die = die;
4946       limbo_node->created_for = t;
4947       limbo_node->next = limbo_die_list;
4948       limbo_die_list = limbo_node;
4949     }
4950
4951   return die;
4952 }
4953
4954 /* Return the DIE associated with the given type specifier.  */
4955
4956 static inline dw_die_ref
4957 lookup_type_die (tree type)
4958 {
4959   return TYPE_SYMTAB_DIE (type);
4960 }
4961
4962 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4963    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4964    anonymous type instead the one of the naming typedef.  */
4965
4966 static inline dw_die_ref
4967 strip_naming_typedef (tree type, dw_die_ref type_die)
4968 {
4969   if (type
4970       && TREE_CODE (type) == RECORD_TYPE
4971       && type_die
4972       && type_die->die_tag == DW_TAG_typedef
4973       && is_naming_typedef_decl (TYPE_NAME (type)))
4974     type_die = get_AT_ref (type_die, DW_AT_type);
4975   return type_die;
4976 }
4977
4978 /* Like lookup_type_die, but if type is an anonymous type named by a
4979    typedef[1], return the DIE of the anonymous type instead the one of
4980    the naming typedef.  This is because in gen_typedef_die, we did
4981    equate the anonymous struct named by the typedef with the DIE of
4982    the naming typedef. So by default, lookup_type_die on an anonymous
4983    struct yields the DIE of the naming typedef.
4984
4985    [1]: Read the comment of is_naming_typedef_decl to learn about what
4986    a naming typedef is.  */
4987
4988 static inline dw_die_ref
4989 lookup_type_die_strip_naming_typedef (tree type)
4990 {
4991   dw_die_ref die = lookup_type_die (type);
4992   return strip_naming_typedef (type, die);
4993 }
4994
4995 /* Equate a DIE to a given type specifier.  */
4996
4997 static inline void
4998 equate_type_number_to_die (tree type, dw_die_ref type_die)
4999 {
5000   TYPE_SYMTAB_DIE (type) = type_die;
5001 }
5002
5003 /* Returns a hash value for X (which really is a die_struct).  */
5004
5005 static hashval_t
5006 decl_die_table_hash (const void *x)
5007 {
5008   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5009 }
5010
5011 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5012
5013 static int
5014 decl_die_table_eq (const void *x, const void *y)
5015 {
5016   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5017 }
5018
5019 /* Return the DIE associated with a given declaration.  */
5020
5021 static inline dw_die_ref
5022 lookup_decl_die (tree decl)
5023 {
5024   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5025 }
5026
5027 /* Returns a hash value for X (which really is a var_loc_list).  */
5028
5029 static hashval_t
5030 decl_loc_table_hash (const void *x)
5031 {
5032   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5033 }
5034
5035 /* Return nonzero if decl_id of var_loc_list X is the same as
5036    UID of decl *Y.  */
5037
5038 static int
5039 decl_loc_table_eq (const void *x, const void *y)
5040 {
5041   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5042 }
5043
5044 /* Return the var_loc list associated with a given declaration.  */
5045
5046 static inline var_loc_list *
5047 lookup_decl_loc (const_tree decl)
5048 {
5049   if (!decl_loc_table)
5050     return NULL;
5051   return (var_loc_list *)
5052     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5053 }
5054
5055 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5056
5057 static hashval_t
5058 cached_dw_loc_list_table_hash (const void *x)
5059 {
5060   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5061 }
5062
5063 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5064    UID of decl *Y.  */
5065
5066 static int
5067 cached_dw_loc_list_table_eq (const void *x, const void *y)
5068 {
5069   return (((const cached_dw_loc_list *) x)->decl_id
5070           == DECL_UID ((const_tree) y));
5071 }
5072
5073 /* Equate a DIE to a particular declaration.  */
5074
5075 static void
5076 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5077 {
5078   unsigned int decl_id = DECL_UID (decl);
5079   void **slot;
5080
5081   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5082   *slot = decl_die;
5083   decl_die->decl_id = decl_id;
5084 }
5085
5086 /* Return how many bits covers PIECE EXPR_LIST.  */
5087
5088 static int
5089 decl_piece_bitsize (rtx piece)
5090 {
5091   int ret = (int) GET_MODE (piece);
5092   if (ret)
5093     return ret;
5094   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5095               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5096   return INTVAL (XEXP (XEXP (piece, 0), 0));
5097 }
5098
5099 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5100
5101 static rtx *
5102 decl_piece_varloc_ptr (rtx piece)
5103 {
5104   if ((int) GET_MODE (piece))
5105     return &XEXP (piece, 0);
5106   else
5107     return &XEXP (XEXP (piece, 0), 1);
5108 }
5109
5110 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5111    Next is the chain of following piece nodes.  */
5112
5113 static rtx
5114 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5115 {
5116   if (bitsize <= (int) MAX_MACHINE_MODE)
5117     return alloc_EXPR_LIST (bitsize, loc_note, next);
5118   else
5119     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5120                                                GEN_INT (bitsize),
5121                                                loc_note), next);
5122 }
5123
5124 /* Return rtx that should be stored into loc field for
5125    LOC_NOTE and BITPOS/BITSIZE.  */
5126
5127 static rtx
5128 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5129                       HOST_WIDE_INT bitsize)
5130 {
5131   if (bitsize != -1)
5132     {
5133       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5134       if (bitpos != 0)
5135         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5136     }
5137   return loc_note;
5138 }
5139
5140 /* This function either modifies location piece list *DEST in
5141    place (if SRC and INNER is NULL), or copies location piece list
5142    *SRC to *DEST while modifying it.  Location BITPOS is modified
5143    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5144    not copied and if needed some padding around it is added.
5145    When modifying in place, DEST should point to EXPR_LIST where
5146    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5147    to the start of the whole list and INNER points to the EXPR_LIST
5148    where earlier pieces cover PIECE_BITPOS bits.  */
5149
5150 static void
5151 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5152                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5153                    HOST_WIDE_INT bitsize, rtx loc_note)
5154 {
5155   int diff;
5156   bool copy = inner != NULL;
5157
5158   if (copy)
5159     {
5160       /* First copy all nodes preceeding the current bitpos.  */
5161       while (src != inner)
5162         {
5163           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5164                                    decl_piece_bitsize (*src), NULL_RTX);
5165           dest = &XEXP (*dest, 1);
5166           src = &XEXP (*src, 1);
5167         }
5168     }
5169   /* Add padding if needed.  */
5170   if (bitpos != piece_bitpos)
5171     {
5172       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5173                                copy ? NULL_RTX : *dest);
5174       dest = &XEXP (*dest, 1);
5175     }
5176   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5177     {
5178       gcc_assert (!copy);
5179       /* A piece with correct bitpos and bitsize already exist,
5180          just update the location for it and return.  */
5181       *decl_piece_varloc_ptr (*dest) = loc_note;
5182       return;
5183     }
5184   /* Add the piece that changed.  */
5185   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5186   dest = &XEXP (*dest, 1);
5187   /* Skip over pieces that overlap it.  */
5188   diff = bitpos - piece_bitpos + bitsize;
5189   if (!copy)
5190     src = dest;
5191   while (diff > 0 && *src)
5192     {
5193       rtx piece = *src;
5194       diff -= decl_piece_bitsize (piece);
5195       if (copy)
5196         src = &XEXP (piece, 1);
5197       else
5198         {
5199           *src = XEXP (piece, 1);
5200           free_EXPR_LIST_node (piece);
5201         }
5202     }
5203   /* Add padding if needed.  */
5204   if (diff < 0 && *src)
5205     {
5206       if (!copy)
5207         dest = src;
5208       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5209       dest = &XEXP (*dest, 1);
5210     }
5211   if (!copy)
5212     return;
5213   /* Finally copy all nodes following it.  */
5214   while (*src)
5215     {
5216       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5217                                decl_piece_bitsize (*src), NULL_RTX);
5218       dest = &XEXP (*dest, 1);
5219       src = &XEXP (*src, 1);
5220     }
5221 }
5222
5223 /* Add a variable location node to the linked list for DECL.  */
5224
5225 static struct var_loc_node *
5226 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5227 {
5228   unsigned int decl_id;
5229   var_loc_list *temp;
5230   void **slot;
5231   struct var_loc_node *loc = NULL;
5232   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5233
5234   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5235     {
5236       tree realdecl = DECL_DEBUG_EXPR (decl);
5237       if (realdecl && handled_component_p (realdecl))
5238         {
5239           HOST_WIDE_INT maxsize;
5240           tree innerdecl;
5241           innerdecl
5242             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5243           if (!DECL_P (innerdecl)
5244               || DECL_IGNORED_P (innerdecl)
5245               || TREE_STATIC (innerdecl)
5246               || bitsize <= 0
5247               || bitpos + bitsize > 256
5248               || bitsize != maxsize)
5249             return NULL;
5250           decl = innerdecl;
5251         }
5252     }
5253
5254   decl_id = DECL_UID (decl);
5255   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5256   if (*slot == NULL)
5257     {
5258       temp = ggc_alloc_cleared_var_loc_list ();
5259       temp->decl_id = decl_id;
5260       *slot = temp;
5261     }
5262   else
5263     temp = (var_loc_list *) *slot;
5264
5265   /* For PARM_DECLs try to keep around the original incoming value,
5266      even if that means we'll emit a zero-range .debug_loc entry.  */
5267   if (temp->last
5268       && temp->first == temp->last
5269       && TREE_CODE (decl) == PARM_DECL
5270       && GET_CODE (temp->first->loc) == NOTE
5271       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5272       && DECL_INCOMING_RTL (decl)
5273       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5274       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5275          == GET_CODE (DECL_INCOMING_RTL (decl))
5276       && prev_real_insn (temp->first->loc) == NULL_RTX
5277       && (bitsize != -1
5278           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5279                            NOTE_VAR_LOCATION_LOC (loc_note))
5280           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5281               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5282     {
5283       loc = ggc_alloc_cleared_var_loc_node ();
5284       temp->first->next = loc;
5285       temp->last = loc;
5286       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5287     }
5288   else if (temp->last)
5289     {
5290       struct var_loc_node *last = temp->last, *unused = NULL;
5291       rtx *piece_loc = NULL, last_loc_note;
5292       int piece_bitpos = 0;
5293       if (last->next)
5294         {
5295           last = last->next;
5296           gcc_assert (last->next == NULL);
5297         }
5298       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5299         {
5300           piece_loc = &last->loc;
5301           do
5302             {
5303               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5304               if (piece_bitpos + cur_bitsize > bitpos)
5305                 break;
5306               piece_bitpos += cur_bitsize;
5307               piece_loc = &XEXP (*piece_loc, 1);
5308             }
5309           while (*piece_loc);
5310         }
5311       /* TEMP->LAST here is either pointer to the last but one or
5312          last element in the chained list, LAST is pointer to the
5313          last element.  */
5314       if (label && strcmp (last->label, label) == 0)
5315         {
5316           /* For SRA optimized variables if there weren't any real
5317              insns since last note, just modify the last node.  */
5318           if (piece_loc != NULL)
5319             {
5320               adjust_piece_list (piece_loc, NULL, NULL,
5321                                  bitpos, piece_bitpos, bitsize, loc_note);
5322               return NULL;
5323             }
5324           /* If the last note doesn't cover any instructions, remove it.  */
5325           if (temp->last != last)
5326             {
5327               temp->last->next = NULL;
5328               unused = last;
5329               last = temp->last;
5330               gcc_assert (strcmp (last->label, label) != 0);
5331             }
5332           else
5333             {
5334               gcc_assert (temp->first == temp->last
5335                           || (temp->first->next == temp->last
5336                               && TREE_CODE (decl) == PARM_DECL));
5337               memset (temp->last, '\0', sizeof (*temp->last));
5338               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5339               return temp->last;
5340             }
5341         }
5342       if (bitsize == -1 && NOTE_P (last->loc))
5343         last_loc_note = last->loc;
5344       else if (piece_loc != NULL
5345                && *piece_loc != NULL_RTX
5346                && piece_bitpos == bitpos
5347                && decl_piece_bitsize (*piece_loc) == bitsize)
5348         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5349       else
5350         last_loc_note = NULL_RTX;
5351       /* If the current location is the same as the end of the list,
5352          and either both or neither of the locations is uninitialized,
5353          we have nothing to do.  */
5354       if (last_loc_note == NULL_RTX
5355           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5356                             NOTE_VAR_LOCATION_LOC (loc_note)))
5357           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5358                != NOTE_VAR_LOCATION_STATUS (loc_note))
5359               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5360                    == VAR_INIT_STATUS_UNINITIALIZED)
5361                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5362                       == VAR_INIT_STATUS_UNINITIALIZED))))
5363         {
5364           /* Add LOC to the end of list and update LAST.  If the last
5365              element of the list has been removed above, reuse its
5366              memory for the new node, otherwise allocate a new one.  */
5367           if (unused)
5368             {
5369               loc = unused;
5370               memset (loc, '\0', sizeof (*loc));
5371             }
5372           else
5373             loc = ggc_alloc_cleared_var_loc_node ();
5374           if (bitsize == -1 || piece_loc == NULL)
5375             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5376           else
5377             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5378                                bitpos, piece_bitpos, bitsize, loc_note);
5379           last->next = loc;
5380           /* Ensure TEMP->LAST will point either to the new last but one
5381              element of the chain, or to the last element in it.  */
5382           if (last != temp->last)
5383             temp->last = last;
5384         }
5385       else if (unused)
5386         ggc_free (unused);
5387     }
5388   else
5389     {
5390       loc = ggc_alloc_cleared_var_loc_node ();
5391       temp->first = loc;
5392       temp->last = loc;
5393       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5394     }
5395   return loc;
5396 }
5397 \f
5398 /* Keep track of the number of spaces used to indent the
5399    output of the debugging routines that print the structure of
5400    the DIE internal representation.  */
5401 static int print_indent;
5402
5403 /* Indent the line the number of spaces given by print_indent.  */
5404
5405 static inline void
5406 print_spaces (FILE *outfile)
5407 {
5408   fprintf (outfile, "%*s", print_indent, "");
5409 }
5410
5411 /* Print a type signature in hex.  */
5412
5413 static inline void
5414 print_signature (FILE *outfile, char *sig)
5415 {
5416   int i;
5417
5418   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5419     fprintf (outfile, "%02x", sig[i] & 0xff);
5420 }
5421
5422 /* Print the information associated with a given DIE, and its children.
5423    This routine is a debugging aid only.  */
5424
5425 static void
5426 print_die (dw_die_ref die, FILE *outfile)
5427 {
5428   dw_attr_ref a;
5429   dw_die_ref c;
5430   unsigned ix;
5431
5432   print_spaces (outfile);
5433   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5434            die->die_offset, dwarf_tag_name (die->die_tag),
5435            (void*) die);
5436   print_spaces (outfile);
5437   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5438   fprintf (outfile, " offset: %ld", die->die_offset);
5439   fprintf (outfile, " mark: %d\n", die->die_mark);
5440
5441   if (use_debug_types && die->die_id.die_type_node)
5442     {
5443       print_spaces (outfile);
5444       fprintf (outfile, "  signature: ");
5445       print_signature (outfile, die->die_id.die_type_node->signature);
5446       fprintf (outfile, "\n");
5447     }
5448
5449   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5450     {
5451       print_spaces (outfile);
5452       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5453
5454       switch (AT_class (a))
5455         {
5456         case dw_val_class_addr:
5457           fprintf (outfile, "address");
5458           break;
5459         case dw_val_class_offset:
5460           fprintf (outfile, "offset");
5461           break;
5462         case dw_val_class_loc:
5463           fprintf (outfile, "location descriptor");
5464           break;
5465         case dw_val_class_loc_list:
5466           fprintf (outfile, "location list -> label:%s",
5467                    AT_loc_list (a)->ll_symbol);
5468           break;
5469         case dw_val_class_range_list:
5470           fprintf (outfile, "range list");
5471           break;
5472         case dw_val_class_const:
5473           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5474           break;
5475         case dw_val_class_unsigned_const:
5476           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5477           break;
5478         case dw_val_class_const_double:
5479           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5480                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5481                    a->dw_attr_val.v.val_double.high,
5482                    a->dw_attr_val.v.val_double.low);
5483           break;
5484         case dw_val_class_vec:
5485           fprintf (outfile, "floating-point or vector constant");
5486           break;
5487         case dw_val_class_flag:
5488           fprintf (outfile, "%u", AT_flag (a));
5489           break;
5490         case dw_val_class_die_ref:
5491           if (AT_ref (a) != NULL)
5492             {
5493               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5494                 {
5495                   fprintf (outfile, "die -> signature: ");
5496                   print_signature (outfile,
5497                                    AT_ref (a)->die_id.die_type_node->signature);
5498                 }
5499               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5500                 fprintf (outfile, "die -> label: %s",
5501                          AT_ref (a)->die_id.die_symbol);
5502               else
5503                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5504               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5505             }
5506           else
5507             fprintf (outfile, "die -> <null>");
5508           break;
5509         case dw_val_class_vms_delta:
5510           fprintf (outfile, "delta: @slotcount(%s-%s)",
5511                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5512           break;
5513         case dw_val_class_lbl_id:
5514         case dw_val_class_lineptr:
5515         case dw_val_class_macptr:
5516           fprintf (outfile, "label: %s", AT_lbl (a));
5517           break;
5518         case dw_val_class_str:
5519           if (AT_string (a) != NULL)
5520             fprintf (outfile, "\"%s\"", AT_string (a));
5521           else
5522             fprintf (outfile, "<null>");
5523           break;
5524         case dw_val_class_file:
5525           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5526                    AT_file (a)->emitted_number);
5527           break;
5528         case dw_val_class_data8:
5529           {
5530             int i;
5531
5532             for (i = 0; i < 8; i++)
5533               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5534             break;
5535           }
5536         default:
5537           break;
5538         }
5539
5540       fprintf (outfile, "\n");
5541     }
5542
5543   if (die->die_child != NULL)
5544     {
5545       print_indent += 4;
5546       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5547       print_indent -= 4;
5548     }
5549   if (print_indent == 0)
5550     fprintf (outfile, "\n");
5551 }
5552
5553 /* Print the information collected for a given DIE.  */
5554
5555 DEBUG_FUNCTION void
5556 debug_dwarf_die (dw_die_ref die)
5557 {
5558   print_die (die, stderr);
5559 }
5560
5561 /* Print all DWARF information collected for the compilation unit.
5562    This routine is a debugging aid only.  */
5563
5564 DEBUG_FUNCTION void
5565 debug_dwarf (void)
5566 {
5567   print_indent = 0;
5568   print_die (comp_unit_die (), stderr);
5569 }
5570 \f
5571 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5572    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5573    DIE that marks the start of the DIEs for this include file.  */
5574
5575 static dw_die_ref
5576 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5577 {
5578   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5579   dw_die_ref new_unit = gen_compile_unit_die (filename);
5580
5581   new_unit->die_sib = old_unit;
5582   return new_unit;
5583 }
5584
5585 /* Close an include-file CU and reopen the enclosing one.  */
5586
5587 static dw_die_ref
5588 pop_compile_unit (dw_die_ref old_unit)
5589 {
5590   dw_die_ref new_unit = old_unit->die_sib;
5591
5592   old_unit->die_sib = NULL;
5593   return new_unit;
5594 }
5595
5596 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5597 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5598
5599 /* Calculate the checksum of a location expression.  */
5600
5601 static inline void
5602 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5603 {
5604   int tem;
5605
5606   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5607   CHECKSUM (tem);
5608   CHECKSUM (loc->dw_loc_oprnd1);
5609   CHECKSUM (loc->dw_loc_oprnd2);
5610 }
5611
5612 /* Calculate the checksum of an attribute.  */
5613
5614 static void
5615 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5616 {
5617   dw_loc_descr_ref loc;
5618   rtx r;
5619
5620   CHECKSUM (at->dw_attr);
5621
5622   /* We don't care that this was compiled with a different compiler
5623      snapshot; if the output is the same, that's what matters.  */
5624   if (at->dw_attr == DW_AT_producer)
5625     return;
5626
5627   switch (AT_class (at))
5628     {
5629     case dw_val_class_const:
5630       CHECKSUM (at->dw_attr_val.v.val_int);
5631       break;
5632     case dw_val_class_unsigned_const:
5633       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5634       break;
5635     case dw_val_class_const_double:
5636       CHECKSUM (at->dw_attr_val.v.val_double);
5637       break;
5638     case dw_val_class_vec:
5639       CHECKSUM (at->dw_attr_val.v.val_vec);
5640       break;
5641     case dw_val_class_flag:
5642       CHECKSUM (at->dw_attr_val.v.val_flag);
5643       break;
5644     case dw_val_class_str:
5645       CHECKSUM_STRING (AT_string (at));
5646       break;
5647
5648     case dw_val_class_addr:
5649       r = AT_addr (at);
5650       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5651       CHECKSUM_STRING (XSTR (r, 0));
5652       break;
5653
5654     case dw_val_class_offset:
5655       CHECKSUM (at->dw_attr_val.v.val_offset);
5656       break;
5657
5658     case dw_val_class_loc:
5659       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5660         loc_checksum (loc, ctx);
5661       break;
5662
5663     case dw_val_class_die_ref:
5664       die_checksum (AT_ref (at), ctx, mark);
5665       break;
5666
5667     case dw_val_class_fde_ref:
5668     case dw_val_class_vms_delta:
5669     case dw_val_class_lbl_id:
5670     case dw_val_class_lineptr:
5671     case dw_val_class_macptr:
5672       break;
5673
5674     case dw_val_class_file:
5675       CHECKSUM_STRING (AT_file (at)->filename);
5676       break;
5677
5678     case dw_val_class_data8:
5679       CHECKSUM (at->dw_attr_val.v.val_data8);
5680       break;
5681
5682     default:
5683       break;
5684     }
5685 }
5686
5687 /* Calculate the checksum of a DIE.  */
5688
5689 static void
5690 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5691 {
5692   dw_die_ref c;
5693   dw_attr_ref a;
5694   unsigned ix;
5695
5696   /* To avoid infinite recursion.  */
5697   if (die->die_mark)
5698     {
5699       CHECKSUM (die->die_mark);
5700       return;
5701     }
5702   die->die_mark = ++(*mark);
5703
5704   CHECKSUM (die->die_tag);
5705
5706   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5707     attr_checksum (a, ctx, mark);
5708
5709   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5710 }
5711
5712 #undef CHECKSUM
5713 #undef CHECKSUM_STRING
5714
5715 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5716 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5717 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5718 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5719 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5720 #define CHECKSUM_ATTR(FOO) \
5721   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5722
5723 /* Calculate the checksum of a number in signed LEB128 format.  */
5724
5725 static void
5726 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5727 {
5728   unsigned char byte;
5729   bool more;
5730
5731   while (1)
5732     {
5733       byte = (value & 0x7f);
5734       value >>= 7;
5735       more = !((value == 0 && (byte & 0x40) == 0)
5736                 || (value == -1 && (byte & 0x40) != 0));
5737       if (more)
5738         byte |= 0x80;
5739       CHECKSUM (byte);
5740       if (!more)
5741         break;
5742     }
5743 }
5744
5745 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5746
5747 static void
5748 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5749 {
5750   while (1)
5751     {
5752       unsigned char byte = (value & 0x7f);
5753       value >>= 7;
5754       if (value != 0)
5755         /* More bytes to follow.  */
5756         byte |= 0x80;
5757       CHECKSUM (byte);
5758       if (value == 0)
5759         break;
5760     }
5761 }
5762
5763 /* Checksum the context of the DIE.  This adds the names of any
5764    surrounding namespaces or structures to the checksum.  */
5765
5766 static void
5767 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5768 {
5769   const char *name;
5770   dw_die_ref spec;
5771   int tag = die->die_tag;
5772
5773   if (tag != DW_TAG_namespace
5774       && tag != DW_TAG_structure_type
5775       && tag != DW_TAG_class_type)
5776     return;
5777
5778   name = get_AT_string (die, DW_AT_name);
5779
5780   spec = get_AT_ref (die, DW_AT_specification);
5781   if (spec != NULL)
5782     die = spec;
5783
5784   if (die->die_parent != NULL)
5785     checksum_die_context (die->die_parent, ctx);
5786
5787   CHECKSUM_ULEB128 ('C');
5788   CHECKSUM_ULEB128 (tag);
5789   if (name != NULL)
5790     CHECKSUM_STRING (name);
5791 }
5792
5793 /* Calculate the checksum of a location expression.  */
5794
5795 static inline void
5796 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5797 {
5798   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5799      were emitted as a DW_FORM_sdata instead of a location expression.  */
5800   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5801     {
5802       CHECKSUM_ULEB128 (DW_FORM_sdata);
5803       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5804       return;
5805     }
5806
5807   /* Otherwise, just checksum the raw location expression.  */
5808   while (loc != NULL)
5809     {
5810       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5811       CHECKSUM (loc->dw_loc_oprnd1);
5812       CHECKSUM (loc->dw_loc_oprnd2);
5813       loc = loc->dw_loc_next;
5814     }
5815 }
5816
5817 /* Calculate the checksum of an attribute.  */
5818
5819 static void
5820 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5821                        struct md5_ctx *ctx, int *mark)
5822 {
5823   dw_loc_descr_ref loc;
5824   rtx r;
5825
5826   if (AT_class (at) == dw_val_class_die_ref)
5827     {
5828       dw_die_ref target_die = AT_ref (at);
5829
5830       /* For pointer and reference types, we checksum only the (qualified)
5831          name of the target type (if there is a name).  For friend entries,
5832          we checksum only the (qualified) name of the target type or function.
5833          This allows the checksum to remain the same whether the target type
5834          is complete or not.  */
5835       if ((at->dw_attr == DW_AT_type
5836            && (tag == DW_TAG_pointer_type
5837                || tag == DW_TAG_reference_type
5838                || tag == DW_TAG_rvalue_reference_type
5839                || tag == DW_TAG_ptr_to_member_type))
5840           || (at->dw_attr == DW_AT_friend
5841               && tag == DW_TAG_friend))
5842         {
5843           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5844
5845           if (name_attr != NULL)
5846             {
5847               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5848
5849               if (decl == NULL)
5850                 decl = target_die;
5851               CHECKSUM_ULEB128 ('N');
5852               CHECKSUM_ULEB128 (at->dw_attr);
5853               if (decl->die_parent != NULL)
5854                 checksum_die_context (decl->die_parent, ctx);
5855               CHECKSUM_ULEB128 ('E');
5856               CHECKSUM_STRING (AT_string (name_attr));
5857               return;
5858             }
5859         }
5860
5861       /* For all other references to another DIE, we check to see if the
5862          target DIE has already been visited.  If it has, we emit a
5863          backward reference; if not, we descend recursively.  */
5864       if (target_die->die_mark > 0)
5865         {
5866           CHECKSUM_ULEB128 ('R');
5867           CHECKSUM_ULEB128 (at->dw_attr);
5868           CHECKSUM_ULEB128 (target_die->die_mark);
5869         }
5870       else
5871         {
5872           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5873
5874           if (decl == NULL)
5875             decl = target_die;
5876           target_die->die_mark = ++(*mark);
5877           CHECKSUM_ULEB128 ('T');
5878           CHECKSUM_ULEB128 (at->dw_attr);
5879           if (decl->die_parent != NULL)
5880             checksum_die_context (decl->die_parent, ctx);
5881           die_checksum_ordered (target_die, ctx, mark);
5882         }
5883       return;
5884     }
5885
5886   CHECKSUM_ULEB128 ('A');
5887   CHECKSUM_ULEB128 (at->dw_attr);
5888
5889   switch (AT_class (at))
5890     {
5891     case dw_val_class_const:
5892       CHECKSUM_ULEB128 (DW_FORM_sdata);
5893       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5894       break;
5895
5896     case dw_val_class_unsigned_const:
5897       CHECKSUM_ULEB128 (DW_FORM_sdata);
5898       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5899       break;
5900
5901     case dw_val_class_const_double:
5902       CHECKSUM_ULEB128 (DW_FORM_block);
5903       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5904       CHECKSUM (at->dw_attr_val.v.val_double);
5905       break;
5906
5907     case dw_val_class_vec:
5908       CHECKSUM_ULEB128 (DW_FORM_block);
5909       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5910       CHECKSUM (at->dw_attr_val.v.val_vec);
5911       break;
5912
5913     case dw_val_class_flag:
5914       CHECKSUM_ULEB128 (DW_FORM_flag);
5915       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5916       break;
5917
5918     case dw_val_class_str:
5919       CHECKSUM_ULEB128 (DW_FORM_string);
5920       CHECKSUM_STRING (AT_string (at));
5921       break;
5922
5923     case dw_val_class_addr:
5924       r = AT_addr (at);
5925       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5926       CHECKSUM_ULEB128 (DW_FORM_string);
5927       CHECKSUM_STRING (XSTR (r, 0));
5928       break;
5929
5930     case dw_val_class_offset:
5931       CHECKSUM_ULEB128 (DW_FORM_sdata);
5932       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5933       break;
5934
5935     case dw_val_class_loc:
5936       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5937         loc_checksum_ordered (loc, ctx);
5938       break;
5939
5940     case dw_val_class_fde_ref:
5941     case dw_val_class_lbl_id:
5942     case dw_val_class_lineptr:
5943     case dw_val_class_macptr:
5944       break;
5945
5946     case dw_val_class_file:
5947       CHECKSUM_ULEB128 (DW_FORM_string);
5948       CHECKSUM_STRING (AT_file (at)->filename);
5949       break;
5950
5951     case dw_val_class_data8:
5952       CHECKSUM (at->dw_attr_val.v.val_data8);
5953       break;
5954
5955     default:
5956       break;
5957     }
5958 }
5959
5960 struct checksum_attributes
5961 {
5962   dw_attr_ref at_name;
5963   dw_attr_ref at_type;
5964   dw_attr_ref at_friend;
5965   dw_attr_ref at_accessibility;
5966   dw_attr_ref at_address_class;
5967   dw_attr_ref at_allocated;
5968   dw_attr_ref at_artificial;
5969   dw_attr_ref at_associated;
5970   dw_attr_ref at_binary_scale;
5971   dw_attr_ref at_bit_offset;
5972   dw_attr_ref at_bit_size;
5973   dw_attr_ref at_bit_stride;
5974   dw_attr_ref at_byte_size;
5975   dw_attr_ref at_byte_stride;
5976   dw_attr_ref at_const_value;
5977   dw_attr_ref at_containing_type;
5978   dw_attr_ref at_count;
5979   dw_attr_ref at_data_location;
5980   dw_attr_ref at_data_member_location;
5981   dw_attr_ref at_decimal_scale;
5982   dw_attr_ref at_decimal_sign;
5983   dw_attr_ref at_default_value;
5984   dw_attr_ref at_digit_count;
5985   dw_attr_ref at_discr;
5986   dw_attr_ref at_discr_list;
5987   dw_attr_ref at_discr_value;
5988   dw_attr_ref at_encoding;
5989   dw_attr_ref at_endianity;
5990   dw_attr_ref at_explicit;
5991   dw_attr_ref at_is_optional;
5992   dw_attr_ref at_location;
5993   dw_attr_ref at_lower_bound;
5994   dw_attr_ref at_mutable;
5995   dw_attr_ref at_ordering;
5996   dw_attr_ref at_picture_string;
5997   dw_attr_ref at_prototyped;
5998   dw_attr_ref at_small;
5999   dw_attr_ref at_segment;
6000   dw_attr_ref at_string_length;
6001   dw_attr_ref at_threads_scaled;
6002   dw_attr_ref at_upper_bound;
6003   dw_attr_ref at_use_location;
6004   dw_attr_ref at_use_UTF8;
6005   dw_attr_ref at_variable_parameter;
6006   dw_attr_ref at_virtuality;
6007   dw_attr_ref at_visibility;
6008   dw_attr_ref at_vtable_elem_location;
6009 };
6010
6011 /* Collect the attributes that we will want to use for the checksum.  */
6012
6013 static void
6014 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6015 {
6016   dw_attr_ref a;
6017   unsigned ix;
6018
6019   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6020     {
6021       switch (a->dw_attr)
6022         {
6023         case DW_AT_name:
6024           attrs->at_name = a;
6025           break;
6026         case DW_AT_type:
6027           attrs->at_type = a;
6028           break;
6029         case DW_AT_friend:
6030           attrs->at_friend = a;
6031           break;
6032         case DW_AT_accessibility:
6033           attrs->at_accessibility = a;
6034           break;
6035         case DW_AT_address_class:
6036           attrs->at_address_class = a;
6037           break;
6038         case DW_AT_allocated:
6039           attrs->at_allocated = a;
6040           break;
6041         case DW_AT_artificial:
6042           attrs->at_artificial = a;
6043           break;
6044         case DW_AT_associated:
6045           attrs->at_associated = a;
6046           break;
6047         case DW_AT_binary_scale:
6048           attrs->at_binary_scale = a;
6049           break;
6050         case DW_AT_bit_offset:
6051           attrs->at_bit_offset = a;
6052           break;
6053         case DW_AT_bit_size:
6054           attrs->at_bit_size = a;
6055           break;
6056         case DW_AT_bit_stride:
6057           attrs->at_bit_stride = a;
6058           break;
6059         case DW_AT_byte_size:
6060           attrs->at_byte_size = a;
6061           break;
6062         case DW_AT_byte_stride:
6063           attrs->at_byte_stride = a;
6064           break;
6065         case DW_AT_const_value:
6066           attrs->at_const_value = a;
6067           break;
6068         case DW_AT_containing_type:
6069           attrs->at_containing_type = a;
6070           break;
6071         case DW_AT_count:
6072           attrs->at_count = a;
6073           break;
6074         case DW_AT_data_location:
6075           attrs->at_data_location = a;
6076           break;
6077         case DW_AT_data_member_location:
6078           attrs->at_data_member_location = a;
6079           break;
6080         case DW_AT_decimal_scale:
6081           attrs->at_decimal_scale = a;
6082           break;
6083         case DW_AT_decimal_sign:
6084           attrs->at_decimal_sign = a;
6085           break;
6086         case DW_AT_default_value:
6087           attrs->at_default_value = a;
6088           break;
6089         case DW_AT_digit_count:
6090           attrs->at_digit_count = a;
6091           break;
6092         case DW_AT_discr:
6093           attrs->at_discr = a;
6094           break;
6095         case DW_AT_discr_list:
6096           attrs->at_discr_list = a;
6097           break;
6098         case DW_AT_discr_value:
6099           attrs->at_discr_value = a;
6100           break;
6101         case DW_AT_encoding:
6102           attrs->at_encoding = a;
6103           break;
6104         case DW_AT_endianity:
6105           attrs->at_endianity = a;
6106           break;
6107         case DW_AT_explicit:
6108           attrs->at_explicit = a;
6109           break;
6110         case DW_AT_is_optional:
6111           attrs->at_is_optional = a;
6112           break;
6113         case DW_AT_location:
6114           attrs->at_location = a;
6115           break;
6116         case DW_AT_lower_bound:
6117           attrs->at_lower_bound = a;
6118           break;
6119         case DW_AT_mutable:
6120           attrs->at_mutable = a;
6121           break;
6122         case DW_AT_ordering:
6123           attrs->at_ordering = a;
6124           break;
6125         case DW_AT_picture_string:
6126           attrs->at_picture_string = a;
6127           break;
6128         case DW_AT_prototyped:
6129           attrs->at_prototyped = a;
6130           break;
6131         case DW_AT_small:
6132           attrs->at_small = a;
6133           break;
6134         case DW_AT_segment:
6135           attrs->at_segment = a;
6136           break;
6137         case DW_AT_string_length:
6138           attrs->at_string_length = a;
6139           break;
6140         case DW_AT_threads_scaled:
6141           attrs->at_threads_scaled = a;
6142           break;
6143         case DW_AT_upper_bound:
6144           attrs->at_upper_bound = a;
6145           break;
6146         case DW_AT_use_location:
6147           attrs->at_use_location = a;
6148           break;
6149         case DW_AT_use_UTF8:
6150           attrs->at_use_UTF8 = a;
6151           break;
6152         case DW_AT_variable_parameter:
6153           attrs->at_variable_parameter = a;
6154           break;
6155         case DW_AT_virtuality:
6156           attrs->at_virtuality = a;
6157           break;
6158         case DW_AT_visibility:
6159           attrs->at_visibility = a;
6160           break;
6161         case DW_AT_vtable_elem_location:
6162           attrs->at_vtable_elem_location = a;
6163           break;
6164         default:
6165           break;
6166         }
6167     }
6168 }
6169
6170 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6171
6172 static void
6173 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6174 {
6175   dw_die_ref c;
6176   dw_die_ref decl;
6177   struct checksum_attributes attrs;
6178
6179   CHECKSUM_ULEB128 ('D');
6180   CHECKSUM_ULEB128 (die->die_tag);
6181
6182   memset (&attrs, 0, sizeof (attrs));
6183
6184   decl = get_AT_ref (die, DW_AT_specification);
6185   if (decl != NULL)
6186     collect_checksum_attributes (&attrs, decl);
6187   collect_checksum_attributes (&attrs, die);
6188
6189   CHECKSUM_ATTR (attrs.at_name);
6190   CHECKSUM_ATTR (attrs.at_accessibility);
6191   CHECKSUM_ATTR (attrs.at_address_class);
6192   CHECKSUM_ATTR (attrs.at_allocated);
6193   CHECKSUM_ATTR (attrs.at_artificial);
6194   CHECKSUM_ATTR (attrs.at_associated);
6195   CHECKSUM_ATTR (attrs.at_binary_scale);
6196   CHECKSUM_ATTR (attrs.at_bit_offset);
6197   CHECKSUM_ATTR (attrs.at_bit_size);
6198   CHECKSUM_ATTR (attrs.at_bit_stride);
6199   CHECKSUM_ATTR (attrs.at_byte_size);
6200   CHECKSUM_ATTR (attrs.at_byte_stride);
6201   CHECKSUM_ATTR (attrs.at_const_value);
6202   CHECKSUM_ATTR (attrs.at_containing_type);
6203   CHECKSUM_ATTR (attrs.at_count);
6204   CHECKSUM_ATTR (attrs.at_data_location);
6205   CHECKSUM_ATTR (attrs.at_data_member_location);
6206   CHECKSUM_ATTR (attrs.at_decimal_scale);
6207   CHECKSUM_ATTR (attrs.at_decimal_sign);
6208   CHECKSUM_ATTR (attrs.at_default_value);
6209   CHECKSUM_ATTR (attrs.at_digit_count);
6210   CHECKSUM_ATTR (attrs.at_discr);
6211   CHECKSUM_ATTR (attrs.at_discr_list);
6212   CHECKSUM_ATTR (attrs.at_discr_value);
6213   CHECKSUM_ATTR (attrs.at_encoding);
6214   CHECKSUM_ATTR (attrs.at_endianity);
6215   CHECKSUM_ATTR (attrs.at_explicit);
6216   CHECKSUM_ATTR (attrs.at_is_optional);
6217   CHECKSUM_ATTR (attrs.at_location);
6218   CHECKSUM_ATTR (attrs.at_lower_bound);
6219   CHECKSUM_ATTR (attrs.at_mutable);
6220   CHECKSUM_ATTR (attrs.at_ordering);
6221   CHECKSUM_ATTR (attrs.at_picture_string);
6222   CHECKSUM_ATTR (attrs.at_prototyped);
6223   CHECKSUM_ATTR (attrs.at_small);
6224   CHECKSUM_ATTR (attrs.at_segment);
6225   CHECKSUM_ATTR (attrs.at_string_length);
6226   CHECKSUM_ATTR (attrs.at_threads_scaled);
6227   CHECKSUM_ATTR (attrs.at_upper_bound);
6228   CHECKSUM_ATTR (attrs.at_use_location);
6229   CHECKSUM_ATTR (attrs.at_use_UTF8);
6230   CHECKSUM_ATTR (attrs.at_variable_parameter);
6231   CHECKSUM_ATTR (attrs.at_virtuality);
6232   CHECKSUM_ATTR (attrs.at_visibility);
6233   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6234   CHECKSUM_ATTR (attrs.at_type);
6235   CHECKSUM_ATTR (attrs.at_friend);
6236
6237   /* Checksum the child DIEs, except for nested types and member functions.  */
6238   c = die->die_child;
6239   if (c) do {
6240     dw_attr_ref name_attr;
6241
6242     c = c->die_sib;
6243     name_attr = get_AT (c, DW_AT_name);
6244     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6245         && name_attr != NULL)
6246       {
6247         CHECKSUM_ULEB128 ('S');
6248         CHECKSUM_ULEB128 (c->die_tag);
6249         CHECKSUM_STRING (AT_string (name_attr));
6250       }
6251     else
6252       {
6253         /* Mark this DIE so it gets processed when unmarking.  */
6254         if (c->die_mark == 0)
6255           c->die_mark = -1;
6256         die_checksum_ordered (c, ctx, mark);
6257       }
6258   } while (c != die->die_child);
6259
6260   CHECKSUM_ULEB128 (0);
6261 }
6262
6263 #undef CHECKSUM
6264 #undef CHECKSUM_STRING
6265 #undef CHECKSUM_ATTR
6266 #undef CHECKSUM_LEB128
6267 #undef CHECKSUM_ULEB128
6268
6269 /* Generate the type signature for DIE.  This is computed by generating an
6270    MD5 checksum over the DIE's tag, its relevant attributes, and its
6271    children.  Attributes that are references to other DIEs are processed
6272    by recursion, using the MARK field to prevent infinite recursion.
6273    If the DIE is nested inside a namespace or another type, we also
6274    need to include that context in the signature.  The lower 64 bits
6275    of the resulting MD5 checksum comprise the signature.  */
6276
6277 static void
6278 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6279 {
6280   int mark;
6281   const char *name;
6282   unsigned char checksum[16];
6283   struct md5_ctx ctx;
6284   dw_die_ref decl;
6285
6286   name = get_AT_string (die, DW_AT_name);
6287   decl = get_AT_ref (die, DW_AT_specification);
6288
6289   /* First, compute a signature for just the type name (and its surrounding
6290      context, if any.  This is stored in the type unit DIE for link-time
6291      ODR (one-definition rule) checking.  */
6292
6293   if (is_cxx() && name != NULL)
6294     {
6295       md5_init_ctx (&ctx);
6296
6297       /* Checksum the names of surrounding namespaces and structures.  */
6298       if (decl != NULL && decl->die_parent != NULL)
6299         checksum_die_context (decl->die_parent, &ctx);
6300
6301       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6302       md5_process_bytes (name, strlen (name) + 1, &ctx);
6303       md5_finish_ctx (&ctx, checksum);
6304
6305       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6306     }
6307
6308   /* Next, compute the complete type signature.  */
6309
6310   md5_init_ctx (&ctx);
6311   mark = 1;
6312   die->die_mark = mark;
6313
6314   /* Checksum the names of surrounding namespaces and structures.  */
6315   if (decl != NULL && decl->die_parent != NULL)
6316     checksum_die_context (decl->die_parent, &ctx);
6317
6318   /* Checksum the DIE and its children.  */
6319   die_checksum_ordered (die, &ctx, &mark);
6320   unmark_all_dies (die);
6321   md5_finish_ctx (&ctx, checksum);
6322
6323   /* Store the signature in the type node and link the type DIE and the
6324      type node together.  */
6325   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6326           DWARF_TYPE_SIGNATURE_SIZE);
6327   die->die_id.die_type_node = type_node;
6328   type_node->type_die = die;
6329
6330   /* If the DIE is a specification, link its declaration to the type node
6331      as well.  */
6332   if (decl != NULL)
6333     decl->die_id.die_type_node = type_node;
6334 }
6335
6336 /* Do the location expressions look same?  */
6337 static inline int
6338 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6339 {
6340   return loc1->dw_loc_opc == loc2->dw_loc_opc
6341          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6342          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6343 }
6344
6345 /* Do the values look the same?  */
6346 static int
6347 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6348 {
6349   dw_loc_descr_ref loc1, loc2;
6350   rtx r1, r2;
6351
6352   if (v1->val_class != v2->val_class)
6353     return 0;
6354
6355   switch (v1->val_class)
6356     {
6357     case dw_val_class_const:
6358       return v1->v.val_int == v2->v.val_int;
6359     case dw_val_class_unsigned_const:
6360       return v1->v.val_unsigned == v2->v.val_unsigned;
6361     case dw_val_class_const_double:
6362       return v1->v.val_double.high == v2->v.val_double.high
6363              && v1->v.val_double.low == v2->v.val_double.low;
6364     case dw_val_class_vec:
6365       if (v1->v.val_vec.length != v2->v.val_vec.length
6366           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6367         return 0;
6368       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6369                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6370         return 0;
6371       return 1;
6372     case dw_val_class_flag:
6373       return v1->v.val_flag == v2->v.val_flag;
6374     case dw_val_class_str:
6375       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6376
6377     case dw_val_class_addr:
6378       r1 = v1->v.val_addr;
6379       r2 = v2->v.val_addr;
6380       if (GET_CODE (r1) != GET_CODE (r2))
6381         return 0;
6382       return !rtx_equal_p (r1, r2);
6383
6384     case dw_val_class_offset:
6385       return v1->v.val_offset == v2->v.val_offset;
6386
6387     case dw_val_class_loc:
6388       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6389            loc1 && loc2;
6390            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6391         if (!same_loc_p (loc1, loc2, mark))
6392           return 0;
6393       return !loc1 && !loc2;
6394
6395     case dw_val_class_die_ref:
6396       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6397
6398     case dw_val_class_fde_ref:
6399     case dw_val_class_vms_delta:
6400     case dw_val_class_lbl_id:
6401     case dw_val_class_lineptr:
6402     case dw_val_class_macptr:
6403       return 1;
6404
6405     case dw_val_class_file:
6406       return v1->v.val_file == v2->v.val_file;
6407
6408     case dw_val_class_data8:
6409       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6410
6411     default:
6412       return 1;
6413     }
6414 }
6415
6416 /* Do the attributes look the same?  */
6417
6418 static int
6419 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6420 {
6421   if (at1->dw_attr != at2->dw_attr)
6422     return 0;
6423
6424   /* We don't care that this was compiled with a different compiler
6425      snapshot; if the output is the same, that's what matters. */
6426   if (at1->dw_attr == DW_AT_producer)
6427     return 1;
6428
6429   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6430 }
6431
6432 /* Do the dies look the same?  */
6433
6434 static int
6435 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6436 {
6437   dw_die_ref c1, c2;
6438   dw_attr_ref a1;
6439   unsigned ix;
6440
6441   /* To avoid infinite recursion.  */
6442   if (die1->die_mark)
6443     return die1->die_mark == die2->die_mark;
6444   die1->die_mark = die2->die_mark = ++(*mark);
6445
6446   if (die1->die_tag != die2->die_tag)
6447     return 0;
6448
6449   if (VEC_length (dw_attr_node, die1->die_attr)
6450       != VEC_length (dw_attr_node, die2->die_attr))
6451     return 0;
6452
6453   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6454     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6455       return 0;
6456
6457   c1 = die1->die_child;
6458   c2 = die2->die_child;
6459   if (! c1)
6460     {
6461       if (c2)
6462         return 0;
6463     }
6464   else
6465     for (;;)
6466       {
6467         if (!same_die_p (c1, c2, mark))
6468           return 0;
6469         c1 = c1->die_sib;
6470         c2 = c2->die_sib;
6471         if (c1 == die1->die_child)
6472           {
6473             if (c2 == die2->die_child)
6474               break;
6475             else
6476               return 0;
6477           }
6478     }
6479
6480   return 1;
6481 }
6482
6483 /* Do the dies look the same?  Wrapper around same_die_p.  */
6484
6485 static int
6486 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6487 {
6488   int mark = 0;
6489   int ret = same_die_p (die1, die2, &mark);
6490
6491   unmark_all_dies (die1);
6492   unmark_all_dies (die2);
6493
6494   return ret;
6495 }
6496
6497 /* The prefix to attach to symbols on DIEs in the current comdat debug
6498    info section.  */
6499 static char *comdat_symbol_id;
6500
6501 /* The index of the current symbol within the current comdat CU.  */
6502 static unsigned int comdat_symbol_number;
6503
6504 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6505    children, and set comdat_symbol_id accordingly.  */
6506
6507 static void
6508 compute_section_prefix (dw_die_ref unit_die)
6509 {
6510   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6511   const char *base = die_name ? lbasename (die_name) : "anonymous";
6512   char *name = XALLOCAVEC (char, strlen (base) + 64);
6513   char *p;
6514   int i, mark;
6515   unsigned char checksum[16];
6516   struct md5_ctx ctx;
6517
6518   /* Compute the checksum of the DIE, then append part of it as hex digits to
6519      the name filename of the unit.  */
6520
6521   md5_init_ctx (&ctx);
6522   mark = 0;
6523   die_checksum (unit_die, &ctx, &mark);
6524   unmark_all_dies (unit_die);
6525   md5_finish_ctx (&ctx, checksum);
6526
6527   sprintf (name, "%s.", base);
6528   clean_symbol_name (name);
6529
6530   p = name + strlen (name);
6531   for (i = 0; i < 4; i++)
6532     {
6533       sprintf (p, "%.2x", checksum[i]);
6534       p += 2;
6535     }
6536
6537   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6538   comdat_symbol_number = 0;
6539 }
6540
6541 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6542
6543 static int
6544 is_type_die (dw_die_ref die)
6545 {
6546   switch (die->die_tag)
6547     {
6548     case DW_TAG_array_type:
6549     case DW_TAG_class_type:
6550     case DW_TAG_interface_type:
6551     case DW_TAG_enumeration_type:
6552     case DW_TAG_pointer_type:
6553     case DW_TAG_reference_type:
6554     case DW_TAG_rvalue_reference_type:
6555     case DW_TAG_string_type:
6556     case DW_TAG_structure_type:
6557     case DW_TAG_subroutine_type:
6558     case DW_TAG_union_type:
6559     case DW_TAG_ptr_to_member_type:
6560     case DW_TAG_set_type:
6561     case DW_TAG_subrange_type:
6562     case DW_TAG_base_type:
6563     case DW_TAG_const_type:
6564     case DW_TAG_file_type:
6565     case DW_TAG_packed_type:
6566     case DW_TAG_volatile_type:
6567     case DW_TAG_typedef:
6568       return 1;
6569     default:
6570       return 0;
6571     }
6572 }
6573
6574 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6575    Basically, we want to choose the bits that are likely to be shared between
6576    compilations (types) and leave out the bits that are specific to individual
6577    compilations (functions).  */
6578
6579 static int
6580 is_comdat_die (dw_die_ref c)
6581 {
6582   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6583      we do for stabs.  The advantage is a greater likelihood of sharing between
6584      objects that don't include headers in the same order (and therefore would
6585      put the base types in a different comdat).  jason 8/28/00 */
6586
6587   if (c->die_tag == DW_TAG_base_type)
6588     return 0;
6589
6590   if (c->die_tag == DW_TAG_pointer_type
6591       || c->die_tag == DW_TAG_reference_type
6592       || c->die_tag == DW_TAG_rvalue_reference_type
6593       || c->die_tag == DW_TAG_const_type
6594       || c->die_tag == DW_TAG_volatile_type)
6595     {
6596       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6597
6598       return t ? is_comdat_die (t) : 0;
6599     }
6600
6601   return is_type_die (c);
6602 }
6603
6604 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6605    compilation unit.  */
6606
6607 static int
6608 is_symbol_die (dw_die_ref c)
6609 {
6610   return (is_type_die (c)
6611           || is_declaration_die (c)
6612           || c->die_tag == DW_TAG_namespace
6613           || c->die_tag == DW_TAG_module);
6614 }
6615
6616 /* Returns true iff C is a compile-unit DIE.  */
6617
6618 static inline bool
6619 is_cu_die (dw_die_ref c)
6620 {
6621   return c && c->die_tag == DW_TAG_compile_unit;
6622 }
6623
6624 static char *
6625 gen_internal_sym (const char *prefix)
6626 {
6627   char buf[256];
6628
6629   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6630   return xstrdup (buf);
6631 }
6632
6633 /* Assign symbols to all worthy DIEs under DIE.  */
6634
6635 static void
6636 assign_symbol_names (dw_die_ref die)
6637 {
6638   dw_die_ref c;
6639
6640   if (is_symbol_die (die))
6641     {
6642       if (comdat_symbol_id)
6643         {
6644           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6645
6646           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6647                    comdat_symbol_id, comdat_symbol_number++);
6648           die->die_id.die_symbol = xstrdup (p);
6649         }
6650       else
6651         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6652     }
6653
6654   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6655 }
6656
6657 struct cu_hash_table_entry
6658 {
6659   dw_die_ref cu;
6660   unsigned min_comdat_num, max_comdat_num;
6661   struct cu_hash_table_entry *next;
6662 };
6663
6664 /* Routines to manipulate hash table of CUs.  */
6665 static hashval_t
6666 htab_cu_hash (const void *of)
6667 {
6668   const struct cu_hash_table_entry *const entry =
6669     (const struct cu_hash_table_entry *) of;
6670
6671   return htab_hash_string (entry->cu->die_id.die_symbol);
6672 }
6673
6674 static int
6675 htab_cu_eq (const void *of1, const void *of2)
6676 {
6677   const struct cu_hash_table_entry *const entry1 =
6678     (const struct cu_hash_table_entry *) of1;
6679   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6680
6681   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6682 }
6683
6684 static void
6685 htab_cu_del (void *what)
6686 {
6687   struct cu_hash_table_entry *next,
6688     *entry = (struct cu_hash_table_entry *) what;
6689
6690   while (entry)
6691     {
6692       next = entry->next;
6693       free (entry);
6694       entry = next;
6695     }
6696 }
6697
6698 /* Check whether we have already seen this CU and set up SYM_NUM
6699    accordingly.  */
6700 static int
6701 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6702 {
6703   struct cu_hash_table_entry dummy;
6704   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6705
6706   dummy.max_comdat_num = 0;
6707
6708   slot = (struct cu_hash_table_entry **)
6709     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6710         INSERT);
6711   entry = *slot;
6712
6713   for (; entry; last = entry, entry = entry->next)
6714     {
6715       if (same_die_p_wrap (cu, entry->cu))
6716         break;
6717     }
6718
6719   if (entry)
6720     {
6721       *sym_num = entry->min_comdat_num;
6722       return 1;
6723     }
6724
6725   entry = XCNEW (struct cu_hash_table_entry);
6726   entry->cu = cu;
6727   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6728   entry->next = *slot;
6729   *slot = entry;
6730
6731   return 0;
6732 }
6733
6734 /* Record SYM_NUM to record of CU in HTABLE.  */
6735 static void
6736 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6737 {
6738   struct cu_hash_table_entry **slot, *entry;
6739
6740   slot = (struct cu_hash_table_entry **)
6741     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6742         NO_INSERT);
6743   entry = *slot;
6744
6745   entry->max_comdat_num = sym_num;
6746 }
6747
6748 /* Traverse the DIE (which is always comp_unit_die), and set up
6749    additional compilation units for each of the include files we see
6750    bracketed by BINCL/EINCL.  */
6751
6752 static void
6753 break_out_includes (dw_die_ref die)
6754 {
6755   dw_die_ref c;
6756   dw_die_ref unit = NULL;
6757   limbo_die_node *node, **pnode;
6758   htab_t cu_hash_table;
6759
6760   c = die->die_child;
6761   if (c) do {
6762     dw_die_ref prev = c;
6763     c = c->die_sib;
6764     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6765            || (unit && is_comdat_die (c)))
6766       {
6767         dw_die_ref next = c->die_sib;
6768
6769         /* This DIE is for a secondary CU; remove it from the main one.  */
6770         remove_child_with_prev (c, prev);
6771
6772         if (c->die_tag == DW_TAG_GNU_BINCL)
6773           unit = push_new_compile_unit (unit, c);
6774         else if (c->die_tag == DW_TAG_GNU_EINCL)
6775           unit = pop_compile_unit (unit);
6776         else
6777           add_child_die (unit, c);
6778         c = next;
6779         if (c == die->die_child)
6780           break;
6781       }
6782   } while (c != die->die_child);
6783
6784 #if 0
6785   /* We can only use this in debugging, since the frontend doesn't check
6786      to make sure that we leave every include file we enter.  */
6787   gcc_assert (!unit);
6788 #endif
6789
6790   assign_symbol_names (die);
6791   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6792   for (node = limbo_die_list, pnode = &limbo_die_list;
6793        node;
6794        node = node->next)
6795     {
6796       int is_dupl;
6797
6798       compute_section_prefix (node->die);
6799       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6800                         &comdat_symbol_number);
6801       assign_symbol_names (node->die);
6802       if (is_dupl)
6803         *pnode = node->next;
6804       else
6805         {
6806           pnode = &node->next;
6807           record_comdat_symbol_number (node->die, cu_hash_table,
6808                 comdat_symbol_number);
6809         }
6810     }
6811   htab_delete (cu_hash_table);
6812 }
6813
6814 /* Return non-zero if this DIE is a declaration.  */
6815
6816 static int
6817 is_declaration_die (dw_die_ref die)
6818 {
6819   dw_attr_ref a;
6820   unsigned ix;
6821
6822   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6823     if (a->dw_attr == DW_AT_declaration)
6824       return 1;
6825
6826   return 0;
6827 }
6828
6829 /* Return non-zero if this DIE is nested inside a subprogram.  */
6830
6831 static int
6832 is_nested_in_subprogram (dw_die_ref die)
6833 {
6834   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6835
6836   if (decl == NULL)
6837     decl = die;
6838   return local_scope_p (decl);
6839 }
6840
6841 /* Return non-zero if this DIE contains a defining declaration of a
6842    subprogram.  */
6843
6844 static int
6845 contains_subprogram_definition (dw_die_ref die)
6846 {
6847   dw_die_ref c;
6848
6849   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6850     return 1;
6851   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6852   return 0;
6853 }
6854
6855 /* Return non-zero if this is a type DIE that should be moved to a
6856    COMDAT .debug_types section.  */
6857
6858 static int
6859 should_move_die_to_comdat (dw_die_ref die)
6860 {
6861   switch (die->die_tag)
6862     {
6863     case DW_TAG_class_type:
6864     case DW_TAG_structure_type:
6865     case DW_TAG_enumeration_type:
6866     case DW_TAG_union_type:
6867       /* Don't move declarations, inlined instances, or types nested in a
6868          subprogram.  */
6869       if (is_declaration_die (die)
6870           || get_AT (die, DW_AT_abstract_origin)
6871           || is_nested_in_subprogram (die))
6872         return 0;
6873       /* A type definition should never contain a subprogram definition.  */
6874       gcc_assert (!contains_subprogram_definition (die));
6875       return 1;
6876     case DW_TAG_array_type:
6877     case DW_TAG_interface_type:
6878     case DW_TAG_pointer_type:
6879     case DW_TAG_reference_type:
6880     case DW_TAG_rvalue_reference_type:
6881     case DW_TAG_string_type:
6882     case DW_TAG_subroutine_type:
6883     case DW_TAG_ptr_to_member_type:
6884     case DW_TAG_set_type:
6885     case DW_TAG_subrange_type:
6886     case DW_TAG_base_type:
6887     case DW_TAG_const_type:
6888     case DW_TAG_file_type:
6889     case DW_TAG_packed_type:
6890     case DW_TAG_volatile_type:
6891     case DW_TAG_typedef:
6892     default:
6893       return 0;
6894     }
6895 }
6896
6897 /* Make a clone of DIE.  */
6898
6899 static dw_die_ref
6900 clone_die (dw_die_ref die)
6901 {
6902   dw_die_ref clone;
6903   dw_attr_ref a;
6904   unsigned ix;
6905
6906   clone = ggc_alloc_cleared_die_node ();
6907   clone->die_tag = die->die_tag;
6908
6909   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6910     add_dwarf_attr (clone, a);
6911
6912   return clone;
6913 }
6914
6915 /* Make a clone of the tree rooted at DIE.  */
6916
6917 static dw_die_ref
6918 clone_tree (dw_die_ref die)
6919 {
6920   dw_die_ref c;
6921   dw_die_ref clone = clone_die (die);
6922
6923   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6924
6925   return clone;
6926 }
6927
6928 /* Make a clone of DIE as a declaration.  */
6929
6930 static dw_die_ref
6931 clone_as_declaration (dw_die_ref die)
6932 {
6933   dw_die_ref clone;
6934   dw_die_ref decl;
6935   dw_attr_ref a;
6936   unsigned ix;
6937
6938   /* If the DIE is already a declaration, just clone it.  */
6939   if (is_declaration_die (die))
6940     return clone_die (die);
6941
6942   /* If the DIE is a specification, just clone its declaration DIE.  */
6943   decl = get_AT_ref (die, DW_AT_specification);
6944   if (decl != NULL)
6945     return clone_die (decl);
6946
6947   clone = ggc_alloc_cleared_die_node ();
6948   clone->die_tag = die->die_tag;
6949
6950   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6951     {
6952       /* We don't want to copy over all attributes.
6953          For example we don't want DW_AT_byte_size because otherwise we will no
6954          longer have a declaration and GDB will treat it as a definition.  */
6955
6956       switch (a->dw_attr)
6957         {
6958         case DW_AT_artificial:
6959         case DW_AT_containing_type:
6960         case DW_AT_external:
6961         case DW_AT_name:
6962         case DW_AT_type:
6963         case DW_AT_virtuality:
6964         case DW_AT_linkage_name:
6965         case DW_AT_MIPS_linkage_name:
6966           add_dwarf_attr (clone, a);
6967           break;
6968         case DW_AT_byte_size:
6969         default:
6970           break;
6971         }
6972     }
6973
6974   if (die->die_id.die_type_node)
6975     add_AT_die_ref (clone, DW_AT_signature, die);
6976
6977   add_AT_flag (clone, DW_AT_declaration, 1);
6978   return clone;
6979 }
6980
6981 /* Copy the declaration context to the new compile unit DIE.  This includes
6982    any surrounding namespace or type declarations.  If the DIE has an
6983    AT_specification attribute, it also includes attributes and children
6984    attached to the specification.  */
6985
6986 static void
6987 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6988 {
6989   dw_die_ref decl;
6990   dw_die_ref new_decl;
6991
6992   decl = get_AT_ref (die, DW_AT_specification);
6993   if (decl == NULL)
6994     decl = die;
6995   else
6996     {
6997       unsigned ix;
6998       dw_die_ref c;
6999       dw_attr_ref a;
7000
7001       /* Copy the type node pointer from the new DIE to the original
7002          declaration DIE so we can forward references later.  */
7003       decl->die_id.die_type_node = die->die_id.die_type_node;
7004
7005       remove_AT (die, DW_AT_specification);
7006
7007       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7008         {
7009           if (a->dw_attr != DW_AT_name
7010               && a->dw_attr != DW_AT_declaration
7011               && a->dw_attr != DW_AT_external)
7012             add_dwarf_attr (die, a);
7013         }
7014
7015       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7016     }
7017
7018   if (decl->die_parent != NULL
7019       && decl->die_parent->die_tag != DW_TAG_compile_unit
7020       && decl->die_parent->die_tag != DW_TAG_type_unit)
7021     {
7022       new_decl = copy_ancestor_tree (unit, decl, NULL);
7023       if (new_decl != NULL)
7024         {
7025           remove_AT (new_decl, DW_AT_signature);
7026           add_AT_specification (die, new_decl);
7027         }
7028     }
7029 }
7030
7031 /* Generate the skeleton ancestor tree for the given NODE, then clone
7032    the DIE and add the clone into the tree.  */
7033
7034 static void
7035 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7036 {
7037   if (node->new_die != NULL)
7038     return;
7039
7040   node->new_die = clone_as_declaration (node->old_die);
7041
7042   if (node->parent != NULL)
7043     {
7044       generate_skeleton_ancestor_tree (node->parent);
7045       add_child_die (node->parent->new_die, node->new_die);
7046     }
7047 }
7048
7049 /* Generate a skeleton tree of DIEs containing any declarations that are
7050    found in the original tree.  We traverse the tree looking for declaration
7051    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7052
7053 static void
7054 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7055 {
7056   skeleton_chain_node node;
7057   dw_die_ref c;
7058   dw_die_ref first;
7059   dw_die_ref prev = NULL;
7060   dw_die_ref next = NULL;
7061
7062   node.parent = parent;
7063
7064   first = c = parent->old_die->die_child;
7065   if (c)
7066     next = c->die_sib;
7067   if (c) do {
7068     if (prev == NULL || prev->die_sib == c)
7069       prev = c;
7070     c = next;
7071     next = (c == first ? NULL : c->die_sib);
7072     node.old_die = c;
7073     node.new_die = NULL;
7074     if (is_declaration_die (c))
7075       {
7076         /* Clone the existing DIE, move the original to the skeleton
7077            tree (which is in the main CU), and put the clone, with
7078            all the original's children, where the original came from.  */
7079         dw_die_ref clone = clone_die (c);
7080         move_all_children (c, clone);
7081
7082         replace_child (c, clone, prev);
7083         generate_skeleton_ancestor_tree (parent);
7084         add_child_die (parent->new_die, c);
7085         node.new_die = c;
7086         c = clone;
7087       }
7088     generate_skeleton_bottom_up (&node);
7089   } while (next != NULL);
7090 }
7091
7092 /* Wrapper function for generate_skeleton_bottom_up.  */
7093
7094 static dw_die_ref
7095 generate_skeleton (dw_die_ref die)
7096 {
7097   skeleton_chain_node node;
7098
7099   node.old_die = die;
7100   node.new_die = NULL;
7101   node.parent = NULL;
7102
7103   /* If this type definition is nested inside another type,
7104      always leave at least a declaration in its place.  */
7105   if (die->die_parent != NULL && is_type_die (die->die_parent))
7106     node.new_die = clone_as_declaration (die);
7107
7108   generate_skeleton_bottom_up (&node);
7109   return node.new_die;
7110 }
7111
7112 /* Remove the DIE from its parent, possibly replacing it with a cloned
7113    declaration.  The original DIE will be moved to a new compile unit
7114    so that existing references to it follow it to the new location.  If
7115    any of the original DIE's descendants is a declaration, we need to
7116    replace the original DIE with a skeleton tree and move the
7117    declarations back into the skeleton tree.  */
7118
7119 static dw_die_ref
7120 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7121 {
7122   dw_die_ref skeleton;
7123
7124   skeleton = generate_skeleton (child);
7125   if (skeleton == NULL)
7126     remove_child_with_prev (child, prev);
7127   else
7128     {
7129       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7130       replace_child (child, skeleton, prev);
7131     }
7132
7133   return skeleton;
7134 }
7135
7136 /* Traverse the DIE and set up additional .debug_types sections for each
7137    type worthy of being placed in a COMDAT section.  */
7138
7139 static void
7140 break_out_comdat_types (dw_die_ref die)
7141 {
7142   dw_die_ref c;
7143   dw_die_ref first;
7144   dw_die_ref prev = NULL;
7145   dw_die_ref next = NULL;
7146   dw_die_ref unit = NULL;
7147
7148   first = c = die->die_child;
7149   if (c)
7150     next = c->die_sib;
7151   if (c) do {
7152     if (prev == NULL || prev->die_sib == c)
7153       prev = c;
7154     c = next;
7155     next = (c == first ? NULL : c->die_sib);
7156     if (should_move_die_to_comdat (c))
7157       {
7158         dw_die_ref replacement;
7159         comdat_type_node_ref type_node;
7160
7161         /* Create a new type unit DIE as the root for the new tree, and
7162            add it to the list of comdat types.  */
7163         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7164         add_AT_unsigned (unit, DW_AT_language,
7165                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7166         type_node = ggc_alloc_cleared_comdat_type_node ();
7167         type_node->root_die = unit;
7168         type_node->next = comdat_type_list;
7169         comdat_type_list = type_node;
7170
7171         /* Generate the type signature.  */
7172         generate_type_signature (c, type_node);
7173
7174         /* Copy the declaration context, attributes, and children of the
7175            declaration into the new compile unit DIE.  */
7176         copy_declaration_context (unit, c);
7177
7178         /* Remove this DIE from the main CU.  */
7179         replacement = remove_child_or_replace_with_skeleton (c, prev);
7180
7181         /* Break out nested types into their own type units.  */
7182         break_out_comdat_types (c);
7183
7184         /* Add the DIE to the new compunit.  */
7185         add_child_die (unit, c);
7186
7187         if (replacement != NULL)
7188           c = replacement;
7189       }
7190     else if (c->die_tag == DW_TAG_namespace
7191              || c->die_tag == DW_TAG_class_type
7192              || c->die_tag == DW_TAG_structure_type
7193              || c->die_tag == DW_TAG_union_type)
7194       {
7195         /* Look for nested types that can be broken out.  */
7196         break_out_comdat_types (c);
7197       }
7198   } while (next != NULL);
7199 }
7200
7201 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7202
7203 struct decl_table_entry
7204 {
7205   dw_die_ref orig;
7206   dw_die_ref copy;
7207 };
7208
7209 /* Routines to manipulate hash table of copied declarations.  */
7210
7211 static hashval_t
7212 htab_decl_hash (const void *of)
7213 {
7214   const struct decl_table_entry *const entry =
7215     (const struct decl_table_entry *) of;
7216
7217   return htab_hash_pointer (entry->orig);
7218 }
7219
7220 static int
7221 htab_decl_eq (const void *of1, const void *of2)
7222 {
7223   const struct decl_table_entry *const entry1 =
7224     (const struct decl_table_entry *) of1;
7225   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7226
7227   return entry1->orig == entry2;
7228 }
7229
7230 static void
7231 htab_decl_del (void *what)
7232 {
7233   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7234
7235   free (entry);
7236 }
7237
7238 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7239    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7240    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7241    to check if the ancestor has already been copied into UNIT.  */
7242
7243 static dw_die_ref
7244 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7245 {
7246   dw_die_ref parent = die->die_parent;
7247   dw_die_ref new_parent = unit;
7248   dw_die_ref copy;
7249   void **slot = NULL;
7250   struct decl_table_entry *entry = NULL;
7251
7252   if (decl_table)
7253     {
7254       /* Check if the entry has already been copied to UNIT.  */
7255       slot = htab_find_slot_with_hash (decl_table, die,
7256                                        htab_hash_pointer (die), INSERT);
7257       if (*slot != HTAB_EMPTY_ENTRY)
7258         {
7259           entry = (struct decl_table_entry *) *slot;
7260           return entry->copy;
7261         }
7262
7263       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7264       entry = XCNEW (struct decl_table_entry);
7265       entry->orig = die;
7266       entry->copy = NULL;
7267       *slot = entry;
7268     }
7269
7270   if (parent != NULL)
7271     {
7272       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7273       if (spec != NULL)
7274         parent = spec;
7275       if (parent->die_tag != DW_TAG_compile_unit
7276           && parent->die_tag != DW_TAG_type_unit)
7277         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7278     }
7279
7280   copy = clone_as_declaration (die);
7281   add_child_die (new_parent, copy);
7282
7283   if (decl_table != NULL)
7284     {
7285       /* Record the pointer to the copy.  */
7286       entry->copy = copy;
7287     }
7288
7289   return copy;
7290 }
7291
7292 /* Walk the DIE and its children, looking for references to incomplete
7293    or trivial types that are unmarked (i.e., that are not in the current
7294    type_unit).  */
7295
7296 static void
7297 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7298 {
7299   dw_die_ref c;
7300   dw_attr_ref a;
7301   unsigned ix;
7302
7303   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7304     {
7305       if (AT_class (a) == dw_val_class_die_ref)
7306         {
7307           dw_die_ref targ = AT_ref (a);
7308           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7309           void **slot;
7310           struct decl_table_entry *entry;
7311
7312           if (targ->die_mark != 0 || type_node != NULL)
7313             continue;
7314
7315           slot = htab_find_slot_with_hash (decl_table, targ,
7316                                            htab_hash_pointer (targ), INSERT);
7317
7318           if (*slot != HTAB_EMPTY_ENTRY)
7319             {
7320               /* TARG has already been copied, so we just need to
7321                  modify the reference to point to the copy.  */
7322               entry = (struct decl_table_entry *) *slot;
7323               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7324             }
7325           else
7326             {
7327               dw_die_ref parent = unit;
7328               dw_die_ref copy = clone_tree (targ);
7329
7330               /* Make sure the cloned tree is marked as part of the
7331                  type unit.  */
7332               mark_dies (copy);
7333
7334               /* Record in DECL_TABLE that TARG has been copied.
7335                  Need to do this now, before the recursive call,
7336                  because DECL_TABLE may be expanded and SLOT
7337                  would no longer be a valid pointer.  */
7338               entry = XCNEW (struct decl_table_entry);
7339               entry->orig = targ;
7340               entry->copy = copy;
7341               *slot = entry;
7342
7343               /* If TARG has surrounding context, copy its ancestor tree
7344                  into the new type unit.  */
7345               if (targ->die_parent != NULL
7346                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7347                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7348                 parent = copy_ancestor_tree (unit, targ->die_parent,
7349                                              decl_table);
7350
7351               add_child_die (parent, copy);
7352               a->dw_attr_val.v.val_die_ref.die = copy;
7353
7354               /* Make sure the newly-copied DIE is walked.  If it was
7355                  installed in a previously-added context, it won't
7356                  get visited otherwise.  */
7357               if (parent != unit)
7358                 {
7359                   /* Find the highest point of the newly-added tree,
7360                      mark each node along the way, and walk from there.  */
7361                   parent->die_mark = 1;
7362                   while (parent->die_parent
7363                          && parent->die_parent->die_mark == 0)
7364                     {
7365                       parent = parent->die_parent;
7366                       parent->die_mark = 1;
7367                     }
7368                   copy_decls_walk (unit, parent, decl_table);
7369                 }
7370             }
7371         }
7372     }
7373
7374   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7375 }
7376
7377 /* Copy declarations for "unworthy" types into the new comdat section.
7378    Incomplete types, modified types, and certain other types aren't broken
7379    out into comdat sections of their own, so they don't have a signature,
7380    and we need to copy the declaration into the same section so that we
7381    don't have an external reference.  */
7382
7383 static void
7384 copy_decls_for_unworthy_types (dw_die_ref unit)
7385 {
7386   htab_t decl_table;
7387
7388   mark_dies (unit);
7389   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7390   copy_decls_walk (unit, unit, decl_table);
7391   htab_delete (decl_table);
7392   unmark_dies (unit);
7393 }
7394
7395 /* Traverse the DIE and add a sibling attribute if it may have the
7396    effect of speeding up access to siblings.  To save some space,
7397    avoid generating sibling attributes for DIE's without children.  */
7398
7399 static void
7400 add_sibling_attributes (dw_die_ref die)
7401 {
7402   dw_die_ref c;
7403
7404   if (! die->die_child)
7405     return;
7406
7407   if (die->die_parent && die != die->die_parent->die_child)
7408     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7409
7410   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7411 }
7412
7413 /* Output all location lists for the DIE and its children.  */
7414
7415 static void
7416 output_location_lists (dw_die_ref die)
7417 {
7418   dw_die_ref c;
7419   dw_attr_ref a;
7420   unsigned ix;
7421
7422   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7423     if (AT_class (a) == dw_val_class_loc_list)
7424       output_loc_list (AT_loc_list (a));
7425
7426   FOR_EACH_CHILD (die, c, output_location_lists (c));
7427 }
7428
7429 /* The format of each DIE (and its attribute value pairs) is encoded in an
7430    abbreviation table.  This routine builds the abbreviation table and assigns
7431    a unique abbreviation id for each abbreviation entry.  The children of each
7432    die are visited recursively.  */
7433
7434 static void
7435 build_abbrev_table (dw_die_ref die)
7436 {
7437   unsigned long abbrev_id;
7438   unsigned int n_alloc;
7439   dw_die_ref c;
7440   dw_attr_ref a;
7441   unsigned ix;
7442
7443   /* Scan the DIE references, and mark as external any that refer to
7444      DIEs from other CUs (i.e. those which are not marked).  */
7445   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7446     if (AT_class (a) == dw_val_class_die_ref
7447         && AT_ref (a)->die_mark == 0)
7448       {
7449         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7450         set_AT_ref_external (a, 1);
7451       }
7452
7453   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7454     {
7455       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7456       dw_attr_ref die_a, abbrev_a;
7457       unsigned ix;
7458       bool ok = true;
7459
7460       if (abbrev->die_tag != die->die_tag)
7461         continue;
7462       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7463         continue;
7464
7465       if (VEC_length (dw_attr_node, abbrev->die_attr)
7466           != VEC_length (dw_attr_node, die->die_attr))
7467         continue;
7468
7469       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7470         {
7471           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7472           if ((abbrev_a->dw_attr != die_a->dw_attr)
7473               || (value_format (abbrev_a) != value_format (die_a)))
7474             {
7475               ok = false;
7476               break;
7477             }
7478         }
7479       if (ok)
7480         break;
7481     }
7482
7483   if (abbrev_id >= abbrev_die_table_in_use)
7484     {
7485       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7486         {
7487           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7488           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7489                                             n_alloc);
7490
7491           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7492                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7493           abbrev_die_table_allocated = n_alloc;
7494         }
7495
7496       ++abbrev_die_table_in_use;
7497       abbrev_die_table[abbrev_id] = die;
7498     }
7499
7500   die->die_abbrev = abbrev_id;
7501   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7502 }
7503 \f
7504 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7505
7506 static int
7507 constant_size (unsigned HOST_WIDE_INT value)
7508 {
7509   int log;
7510
7511   if (value == 0)
7512     log = 0;
7513   else
7514     log = floor_log2 (value);
7515
7516   log = log / 8;
7517   log = 1 << (floor_log2 (log) + 1);
7518
7519   return log;
7520 }
7521
7522 /* Return the size of a DIE as it is represented in the
7523    .debug_info section.  */
7524
7525 static unsigned long
7526 size_of_die (dw_die_ref die)
7527 {
7528   unsigned long size = 0;
7529   dw_attr_ref a;
7530   unsigned ix;
7531
7532   size += size_of_uleb128 (die->die_abbrev);
7533   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7534     {
7535       switch (AT_class (a))
7536         {
7537         case dw_val_class_addr:
7538           size += DWARF2_ADDR_SIZE;
7539           break;
7540         case dw_val_class_offset:
7541           size += DWARF_OFFSET_SIZE;
7542           break;
7543         case dw_val_class_loc:
7544           {
7545             unsigned long lsize = size_of_locs (AT_loc (a));
7546
7547             /* Block length.  */
7548             if (dwarf_version >= 4)
7549               size += size_of_uleb128 (lsize);
7550             else
7551               size += constant_size (lsize);
7552             size += lsize;
7553           }
7554           break;
7555         case dw_val_class_loc_list:
7556           size += DWARF_OFFSET_SIZE;
7557           break;
7558         case dw_val_class_range_list:
7559           size += DWARF_OFFSET_SIZE;
7560           break;
7561         case dw_val_class_const:
7562           size += size_of_sleb128 (AT_int (a));
7563           break;
7564         case dw_val_class_unsigned_const:
7565           size += constant_size (AT_unsigned (a));
7566           break;
7567         case dw_val_class_const_double:
7568           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7569           if (HOST_BITS_PER_WIDE_INT >= 64)
7570             size++; /* block */
7571           break;
7572         case dw_val_class_vec:
7573           size += constant_size (a->dw_attr_val.v.val_vec.length
7574                                  * a->dw_attr_val.v.val_vec.elt_size)
7575                   + a->dw_attr_val.v.val_vec.length
7576                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7577           break;
7578         case dw_val_class_flag:
7579           if (dwarf_version >= 4)
7580             /* Currently all add_AT_flag calls pass in 1 as last argument,
7581                so DW_FORM_flag_present can be used.  If that ever changes,
7582                we'll need to use DW_FORM_flag and have some optimization
7583                in build_abbrev_table that will change those to
7584                DW_FORM_flag_present if it is set to 1 in all DIEs using
7585                the same abbrev entry.  */
7586             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7587           else
7588             size += 1;
7589           break;
7590         case dw_val_class_die_ref:
7591           if (AT_ref_external (a))
7592             {
7593               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7594                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7595                  is sized by target address length, whereas in DWARF3
7596                  it's always sized as an offset.  */
7597               if (use_debug_types)
7598                 size += DWARF_TYPE_SIGNATURE_SIZE;
7599               else if (dwarf_version == 2)
7600                 size += DWARF2_ADDR_SIZE;
7601               else
7602                 size += DWARF_OFFSET_SIZE;
7603             }
7604           else
7605             size += DWARF_OFFSET_SIZE;
7606           break;
7607         case dw_val_class_fde_ref:
7608           size += DWARF_OFFSET_SIZE;
7609           break;
7610         case dw_val_class_lbl_id:
7611           size += DWARF2_ADDR_SIZE;
7612           break;
7613         case dw_val_class_lineptr:
7614         case dw_val_class_macptr:
7615           size += DWARF_OFFSET_SIZE;
7616           break;
7617         case dw_val_class_str:
7618           if (AT_string_form (a) == DW_FORM_strp)
7619             size += DWARF_OFFSET_SIZE;
7620           else
7621             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7622           break;
7623         case dw_val_class_file:
7624           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7625           break;
7626         case dw_val_class_data8:
7627           size += 8;
7628           break;
7629         case dw_val_class_vms_delta:
7630           size += DWARF_OFFSET_SIZE;
7631           break;
7632         default:
7633           gcc_unreachable ();
7634         }
7635     }
7636
7637   return size;
7638 }
7639
7640 /* Size the debugging information associated with a given DIE.  Visits the
7641    DIE's children recursively.  Updates the global variable next_die_offset, on
7642    each time through.  Uses the current value of next_die_offset to update the
7643    die_offset field in each DIE.  */
7644
7645 static void
7646 calc_die_sizes (dw_die_ref die)
7647 {
7648   dw_die_ref c;
7649
7650   gcc_assert (die->die_offset == 0
7651               || (unsigned long int) die->die_offset == next_die_offset);
7652   die->die_offset = next_die_offset;
7653   next_die_offset += size_of_die (die);
7654
7655   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7656
7657   if (die->die_child != NULL)
7658     /* Count the null byte used to terminate sibling lists.  */
7659     next_die_offset += 1;
7660 }
7661
7662 /* Size just the base type children at the start of the CU.
7663    This is needed because build_abbrev needs to size locs
7664    and sizing of type based stack ops needs to know die_offset
7665    values for the base types.  */
7666
7667 static void
7668 calc_base_type_die_sizes (void)
7669 {
7670   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7671   unsigned int i;
7672   dw_die_ref base_type;
7673 #if ENABLE_ASSERT_CHECKING
7674   dw_die_ref prev = comp_unit_die ()->die_child;
7675 #endif
7676
7677   die_offset += size_of_die (comp_unit_die ());
7678   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7679     {
7680 #if ENABLE_ASSERT_CHECKING
7681       gcc_assert (base_type->die_offset == 0
7682                   && prev->die_sib == base_type
7683                   && base_type->die_child == NULL
7684                   && base_type->die_abbrev);
7685       prev = base_type;
7686 #endif
7687       base_type->die_offset = die_offset;
7688       die_offset += size_of_die (base_type);
7689     }
7690 }
7691
7692 /* Set the marks for a die and its children.  We do this so
7693    that we know whether or not a reference needs to use FORM_ref_addr; only
7694    DIEs in the same CU will be marked.  We used to clear out the offset
7695    and use that as the flag, but ran into ordering problems.  */
7696
7697 static void
7698 mark_dies (dw_die_ref die)
7699 {
7700   dw_die_ref c;
7701
7702   gcc_assert (!die->die_mark);
7703
7704   die->die_mark = 1;
7705   FOR_EACH_CHILD (die, c, mark_dies (c));
7706 }
7707
7708 /* Clear the marks for a die and its children.  */
7709
7710 static void
7711 unmark_dies (dw_die_ref die)
7712 {
7713   dw_die_ref c;
7714
7715   if (! use_debug_types)
7716     gcc_assert (die->die_mark);
7717
7718   die->die_mark = 0;
7719   FOR_EACH_CHILD (die, c, unmark_dies (c));
7720 }
7721
7722 /* Clear the marks for a die, its children and referred dies.  */
7723
7724 static void
7725 unmark_all_dies (dw_die_ref die)
7726 {
7727   dw_die_ref c;
7728   dw_attr_ref a;
7729   unsigned ix;
7730
7731   if (!die->die_mark)
7732     return;
7733   die->die_mark = 0;
7734
7735   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7736
7737   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7738     if (AT_class (a) == dw_val_class_die_ref)
7739       unmark_all_dies (AT_ref (a));
7740 }
7741
7742 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7743    generated for the compilation unit.  */
7744
7745 static unsigned long
7746 size_of_pubnames (VEC (pubname_entry, gc) * names)
7747 {
7748   unsigned long size;
7749   unsigned i;
7750   pubname_ref p;
7751
7752   size = DWARF_PUBNAMES_HEADER_SIZE;
7753   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7754     if (names != pubtype_table
7755         || p->die->die_offset != 0
7756         || !flag_eliminate_unused_debug_types)
7757       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7758
7759   size += DWARF_OFFSET_SIZE;
7760   return size;
7761 }
7762
7763 /* Return the size of the information in the .debug_aranges section.  */
7764
7765 static unsigned long
7766 size_of_aranges (void)
7767 {
7768   unsigned long size;
7769
7770   size = DWARF_ARANGES_HEADER_SIZE;
7771
7772   /* Count the address/length pair for this compilation unit.  */
7773   if (text_section_used)
7774     size += 2 * DWARF2_ADDR_SIZE;
7775   if (cold_text_section_used)
7776     size += 2 * DWARF2_ADDR_SIZE;
7777   if (have_multiple_function_sections)
7778     {
7779       unsigned fde_idx;
7780       dw_fde_ref fde;
7781
7782       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7783         {
7784           if (!fde->in_std_section)
7785             size += 2 * DWARF2_ADDR_SIZE;
7786           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7787             size += 2 * DWARF2_ADDR_SIZE;
7788         }
7789     }
7790
7791   /* Count the two zero words used to terminated the address range table.  */
7792   size += 2 * DWARF2_ADDR_SIZE;
7793   return size;
7794 }
7795 \f
7796 /* Select the encoding of an attribute value.  */
7797
7798 static enum dwarf_form
7799 value_format (dw_attr_ref a)
7800 {
7801   switch (a->dw_attr_val.val_class)
7802     {
7803     case dw_val_class_addr:
7804       /* Only very few attributes allow DW_FORM_addr.  */
7805       switch (a->dw_attr)
7806         {
7807         case DW_AT_low_pc:
7808         case DW_AT_high_pc:
7809         case DW_AT_entry_pc:
7810         case DW_AT_trampoline:
7811           return DW_FORM_addr;
7812         default:
7813           break;
7814         }
7815       switch (DWARF2_ADDR_SIZE)
7816         {
7817         case 1:
7818           return DW_FORM_data1;
7819         case 2:
7820           return DW_FORM_data2;
7821         case 4:
7822           return DW_FORM_data4;
7823         case 8:
7824           return DW_FORM_data8;
7825         default:
7826           gcc_unreachable ();
7827         }
7828     case dw_val_class_range_list:
7829     case dw_val_class_loc_list:
7830       if (dwarf_version >= 4)
7831         return DW_FORM_sec_offset;
7832       /* FALLTHRU */
7833     case dw_val_class_vms_delta:
7834     case dw_val_class_offset:
7835       switch (DWARF_OFFSET_SIZE)
7836         {
7837         case 4:
7838           return DW_FORM_data4;
7839         case 8:
7840           return DW_FORM_data8;
7841         default:
7842           gcc_unreachable ();
7843         }
7844     case dw_val_class_loc:
7845       if (dwarf_version >= 4)
7846         return DW_FORM_exprloc;
7847       switch (constant_size (size_of_locs (AT_loc (a))))
7848         {
7849         case 1:
7850           return DW_FORM_block1;
7851         case 2:
7852           return DW_FORM_block2;
7853         default:
7854           gcc_unreachable ();
7855         }
7856     case dw_val_class_const:
7857       return DW_FORM_sdata;
7858     case dw_val_class_unsigned_const:
7859       switch (constant_size (AT_unsigned (a)))
7860         {
7861         case 1:
7862           return DW_FORM_data1;
7863         case 2:
7864           return DW_FORM_data2;
7865         case 4:
7866           return DW_FORM_data4;
7867         case 8:
7868           return DW_FORM_data8;
7869         default:
7870           gcc_unreachable ();
7871         }
7872     case dw_val_class_const_double:
7873       switch (HOST_BITS_PER_WIDE_INT)
7874         {
7875         case 8:
7876           return DW_FORM_data2;
7877         case 16:
7878           return DW_FORM_data4;
7879         case 32:
7880           return DW_FORM_data8;
7881         case 64:
7882         default:
7883           return DW_FORM_block1;
7884         }
7885     case dw_val_class_vec:
7886       switch (constant_size (a->dw_attr_val.v.val_vec.length
7887                              * a->dw_attr_val.v.val_vec.elt_size))
7888         {
7889         case 1:
7890           return DW_FORM_block1;
7891         case 2:
7892           return DW_FORM_block2;
7893         case 4:
7894           return DW_FORM_block4;
7895         default:
7896           gcc_unreachable ();
7897         }
7898     case dw_val_class_flag:
7899       if (dwarf_version >= 4)
7900         {
7901           /* Currently all add_AT_flag calls pass in 1 as last argument,
7902              so DW_FORM_flag_present can be used.  If that ever changes,
7903              we'll need to use DW_FORM_flag and have some optimization
7904              in build_abbrev_table that will change those to
7905              DW_FORM_flag_present if it is set to 1 in all DIEs using
7906              the same abbrev entry.  */
7907           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7908           return DW_FORM_flag_present;
7909         }
7910       return DW_FORM_flag;
7911     case dw_val_class_die_ref:
7912       if (AT_ref_external (a))
7913         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7914       else
7915         return DW_FORM_ref;
7916     case dw_val_class_fde_ref:
7917       return DW_FORM_data;
7918     case dw_val_class_lbl_id:
7919       return DW_FORM_addr;
7920     case dw_val_class_lineptr:
7921     case dw_val_class_macptr:
7922       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7923     case dw_val_class_str:
7924       return AT_string_form (a);
7925     case dw_val_class_file:
7926       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7927         {
7928         case 1:
7929           return DW_FORM_data1;
7930         case 2:
7931           return DW_FORM_data2;
7932         case 4:
7933           return DW_FORM_data4;
7934         default:
7935           gcc_unreachable ();
7936         }
7937
7938     case dw_val_class_data8:
7939       return DW_FORM_data8;
7940
7941     default:
7942       gcc_unreachable ();
7943     }
7944 }
7945
7946 /* Output the encoding of an attribute value.  */
7947
7948 static void
7949 output_value_format (dw_attr_ref a)
7950 {
7951   enum dwarf_form form = value_format (a);
7952
7953   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7954 }
7955
7956 /* Output the .debug_abbrev section which defines the DIE abbreviation
7957    table.  */
7958
7959 static void
7960 output_abbrev_section (void)
7961 {
7962   unsigned long abbrev_id;
7963
7964   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7965     {
7966       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7967       unsigned ix;
7968       dw_attr_ref a_attr;
7969
7970       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7971       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7972                                    dwarf_tag_name (abbrev->die_tag));
7973
7974       if (abbrev->die_child != NULL)
7975         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7976       else
7977         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7978
7979       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7980            ix++)
7981         {
7982           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7983                                        dwarf_attr_name (a_attr->dw_attr));
7984           output_value_format (a_attr);
7985         }
7986
7987       dw2_asm_output_data (1, 0, NULL);
7988       dw2_asm_output_data (1, 0, NULL);
7989     }
7990
7991   /* Terminate the table.  */
7992   dw2_asm_output_data (1, 0, NULL);
7993 }
7994
7995 /* Output a symbol we can use to refer to this DIE from another CU.  */
7996
7997 static inline void
7998 output_die_symbol (dw_die_ref die)
7999 {
8000   char *sym = die->die_id.die_symbol;
8001
8002   if (sym == 0)
8003     return;
8004
8005   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8006     /* We make these global, not weak; if the target doesn't support
8007        .linkonce, it doesn't support combining the sections, so debugging
8008        will break.  */
8009     targetm.asm_out.globalize_label (asm_out_file, sym);
8010
8011   ASM_OUTPUT_LABEL (asm_out_file, sym);
8012 }
8013
8014 /* Return a new location list, given the begin and end range, and the
8015    expression.  */
8016
8017 static inline dw_loc_list_ref
8018 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8019               const char *section)
8020 {
8021   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8022
8023   retlist->begin = begin;
8024   retlist->end = end;
8025   retlist->expr = expr;
8026   retlist->section = section;
8027
8028   return retlist;
8029 }
8030
8031 /* Generate a new internal symbol for this location list node, if it
8032    hasn't got one yet.  */
8033
8034 static inline void
8035 gen_llsym (dw_loc_list_ref list)
8036 {
8037   gcc_assert (!list->ll_symbol);
8038   list->ll_symbol = gen_internal_sym ("LLST");
8039 }
8040
8041 /* Output the location list given to us.  */
8042
8043 static void
8044 output_loc_list (dw_loc_list_ref list_head)
8045 {
8046   dw_loc_list_ref curr = list_head;
8047
8048   if (list_head->emitted)
8049     return;
8050   list_head->emitted = true;
8051
8052   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8053
8054   /* Walk the location list, and output each range + expression.  */
8055   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8056     {
8057       unsigned long size;
8058       /* Don't output an entry that starts and ends at the same address.  */
8059       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8060         continue;
8061       if (!have_multiple_function_sections)
8062         {
8063           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8064                                 "Location list begin address (%s)",
8065                                 list_head->ll_symbol);
8066           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8067                                 "Location list end address (%s)",
8068                                 list_head->ll_symbol);
8069         }
8070       else
8071         {
8072           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8073                                "Location list begin address (%s)",
8074                                list_head->ll_symbol);
8075           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8076                                "Location list end address (%s)",
8077                                list_head->ll_symbol);
8078         }
8079       size = size_of_locs (curr->expr);
8080
8081       /* Output the block length for this list of location operations.  */
8082       gcc_assert (size <= 0xffff);
8083       dw2_asm_output_data (2, size, "%s", "Location expression size");
8084
8085       output_loc_sequence (curr->expr, -1);
8086     }
8087
8088   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8089                        "Location list terminator begin (%s)",
8090                        list_head->ll_symbol);
8091   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8092                        "Location list terminator end (%s)",
8093                        list_head->ll_symbol);
8094 }
8095
8096 /* Output a type signature.  */
8097
8098 static inline void
8099 output_signature (const char *sig, const char *name)
8100 {
8101   int i;
8102
8103   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8104     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8105 }
8106
8107 /* Output the DIE and its attributes.  Called recursively to generate
8108    the definitions of each child DIE.  */
8109
8110 static void
8111 output_die (dw_die_ref die)
8112 {
8113   dw_attr_ref a;
8114   dw_die_ref c;
8115   unsigned long size;
8116   unsigned ix;
8117
8118   /* If someone in another CU might refer to us, set up a symbol for
8119      them to point to.  */
8120   if (! use_debug_types && die->die_id.die_symbol)
8121     output_die_symbol (die);
8122
8123   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8124                                (unsigned long)die->die_offset,
8125                                dwarf_tag_name (die->die_tag));
8126
8127   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8128     {
8129       const char *name = dwarf_attr_name (a->dw_attr);
8130
8131       switch (AT_class (a))
8132         {
8133         case dw_val_class_addr:
8134           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8135           break;
8136
8137         case dw_val_class_offset:
8138           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8139                                "%s", name);
8140           break;
8141
8142         case dw_val_class_range_list:
8143           {
8144             char *p = strchr (ranges_section_label, '\0');
8145
8146             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8147                      a->dw_attr_val.v.val_offset);
8148             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8149                                    debug_ranges_section, "%s", name);
8150             *p = '\0';
8151           }
8152           break;
8153
8154         case dw_val_class_loc:
8155           size = size_of_locs (AT_loc (a));
8156
8157           /* Output the block length for this list of location operations.  */
8158           if (dwarf_version >= 4)
8159             dw2_asm_output_data_uleb128 (size, "%s", name);
8160           else
8161             dw2_asm_output_data (constant_size (size), size, "%s", name);
8162
8163           output_loc_sequence (AT_loc (a), -1);
8164           break;
8165
8166         case dw_val_class_const:
8167           /* ??? It would be slightly more efficient to use a scheme like is
8168              used for unsigned constants below, but gdb 4.x does not sign
8169              extend.  Gdb 5.x does sign extend.  */
8170           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8171           break;
8172
8173         case dw_val_class_unsigned_const:
8174           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8175                                AT_unsigned (a), "%s", name);
8176           break;
8177
8178         case dw_val_class_const_double:
8179           {
8180             unsigned HOST_WIDE_INT first, second;
8181
8182             if (HOST_BITS_PER_WIDE_INT >= 64)
8183               dw2_asm_output_data (1,
8184                                    2 * HOST_BITS_PER_WIDE_INT
8185                                    / HOST_BITS_PER_CHAR,
8186                                    NULL);
8187
8188             if (WORDS_BIG_ENDIAN)
8189               {
8190                 first = a->dw_attr_val.v.val_double.high;
8191                 second = a->dw_attr_val.v.val_double.low;
8192               }
8193             else
8194               {
8195                 first = a->dw_attr_val.v.val_double.low;
8196                 second = a->dw_attr_val.v.val_double.high;
8197               }
8198
8199             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8200                                  first, name);
8201             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8202                                  second, NULL);
8203           }
8204           break;
8205
8206         case dw_val_class_vec:
8207           {
8208             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8209             unsigned int len = a->dw_attr_val.v.val_vec.length;
8210             unsigned int i;
8211             unsigned char *p;
8212
8213             dw2_asm_output_data (constant_size (len * elt_size),
8214                                  len * elt_size, "%s", name);
8215             if (elt_size > sizeof (HOST_WIDE_INT))
8216               {
8217                 elt_size /= 2;
8218                 len *= 2;
8219               }
8220             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8221                  i < len;
8222                  i++, p += elt_size)
8223               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8224                                    "fp or vector constant word %u", i);
8225             break;
8226           }
8227
8228         case dw_val_class_flag:
8229           if (dwarf_version >= 4)
8230             {
8231               /* Currently all add_AT_flag calls pass in 1 as last argument,
8232                  so DW_FORM_flag_present can be used.  If that ever changes,
8233                  we'll need to use DW_FORM_flag and have some optimization
8234                  in build_abbrev_table that will change those to
8235                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8236                  the same abbrev entry.  */
8237               gcc_assert (AT_flag (a) == 1);
8238               if (flag_debug_asm)
8239                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8240                          ASM_COMMENT_START, name);
8241               break;
8242             }
8243           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8244           break;
8245
8246         case dw_val_class_loc_list:
8247           {
8248             char *sym = AT_loc_list (a)->ll_symbol;
8249
8250             gcc_assert (sym);
8251             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8252                                    "%s", name);
8253           }
8254           break;
8255
8256         case dw_val_class_die_ref:
8257           if (AT_ref_external (a))
8258             {
8259               if (use_debug_types)
8260                 {
8261                   comdat_type_node_ref type_node =
8262                     AT_ref (a)->die_id.die_type_node;
8263
8264                   gcc_assert (type_node);
8265                   output_signature (type_node->signature, name);
8266                 }
8267               else
8268                 {
8269                   char *sym = AT_ref (a)->die_id.die_symbol;
8270                   int size;
8271
8272                   gcc_assert (sym);
8273                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8274                      length, whereas in DWARF3 it's always sized as an
8275                      offset.  */
8276                   if (dwarf_version == 2)
8277                     size = DWARF2_ADDR_SIZE;
8278                   else
8279                     size = DWARF_OFFSET_SIZE;
8280                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8281                                          name);
8282                 }
8283             }
8284           else
8285             {
8286               gcc_assert (AT_ref (a)->die_offset);
8287               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8288                                    "%s", name);
8289             }
8290           break;
8291
8292         case dw_val_class_fde_ref:
8293           {
8294             char l1[20];
8295
8296             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8297                                          a->dw_attr_val.v.val_fde_index * 2);
8298             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8299                                    "%s", name);
8300           }
8301           break;
8302
8303         case dw_val_class_vms_delta:
8304           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8305                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8306                                     "%s", name);
8307           break;
8308
8309         case dw_val_class_lbl_id:
8310           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8311           break;
8312
8313         case dw_val_class_lineptr:
8314           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8315                                  debug_line_section, "%s", name);
8316           break;
8317
8318         case dw_val_class_macptr:
8319           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8320                                  debug_macinfo_section, "%s", name);
8321           break;
8322
8323         case dw_val_class_str:
8324           if (AT_string_form (a) == DW_FORM_strp)
8325             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8326                                    a->dw_attr_val.v.val_str->label,
8327                                    debug_str_section,
8328                                    "%s: \"%s\"", name, AT_string (a));
8329           else
8330             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8331           break;
8332
8333         case dw_val_class_file:
8334           {
8335             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8336
8337             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8338                                  a->dw_attr_val.v.val_file->filename);
8339             break;
8340           }
8341
8342         case dw_val_class_data8:
8343           {
8344             int i;
8345
8346             for (i = 0; i < 8; i++)
8347               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8348                                    i == 0 ? "%s" : NULL, name);
8349             break;
8350           }
8351
8352         default:
8353           gcc_unreachable ();
8354         }
8355     }
8356
8357   FOR_EACH_CHILD (die, c, output_die (c));
8358
8359   /* Add null byte to terminate sibling list.  */
8360   if (die->die_child != NULL)
8361     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8362                          (unsigned long) die->die_offset);
8363 }
8364
8365 /* Output the compilation unit that appears at the beginning of the
8366    .debug_info section, and precedes the DIE descriptions.  */
8367
8368 static void
8369 output_compilation_unit_header (void)
8370 {
8371   int ver = dwarf_version;
8372
8373   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8374     dw2_asm_output_data (4, 0xffffffff,
8375       "Initial length escape value indicating 64-bit DWARF extension");
8376   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8377                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8378                        "Length of Compilation Unit Info");
8379   dw2_asm_output_data (2, ver, "DWARF version number");
8380   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8381                          debug_abbrev_section,
8382                          "Offset Into Abbrev. Section");
8383   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8384 }
8385
8386 /* Output the compilation unit DIE and its children.  */
8387
8388 static void
8389 output_comp_unit (dw_die_ref die, int output_if_empty)
8390 {
8391   const char *secname;
8392   char *oldsym, *tmp;
8393
8394   /* Unless we are outputting main CU, we may throw away empty ones.  */
8395   if (!output_if_empty && die->die_child == NULL)
8396     return;
8397
8398   /* Even if there are no children of this DIE, we must output the information
8399      about the compilation unit.  Otherwise, on an empty translation unit, we
8400      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8401      will then complain when examining the file.  First mark all the DIEs in
8402      this CU so we know which get local refs.  */
8403   mark_dies (die);
8404
8405   build_abbrev_table (die);
8406
8407   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8408   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8409   calc_die_sizes (die);
8410
8411   oldsym = die->die_id.die_symbol;
8412   if (oldsym)
8413     {
8414       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8415
8416       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8417       secname = tmp;
8418       die->die_id.die_symbol = NULL;
8419       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8420     }
8421   else
8422     {
8423       switch_to_section (debug_info_section);
8424       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8425       info_section_emitted = true;
8426     }
8427
8428   /* Output debugging information.  */
8429   output_compilation_unit_header ();
8430   output_die (die);
8431
8432   /* Leave the marks on the main CU, so we can check them in
8433      output_pubnames.  */
8434   if (oldsym)
8435     {
8436       unmark_dies (die);
8437       die->die_id.die_symbol = oldsym;
8438     }
8439 }
8440
8441 /* Output a comdat type unit DIE and its children.  */
8442
8443 static void
8444 output_comdat_type_unit (comdat_type_node *node)
8445 {
8446   const char *secname;
8447   char *tmp;
8448   int i;
8449 #if defined (OBJECT_FORMAT_ELF)
8450   tree comdat_key;
8451 #endif
8452
8453   /* First mark all the DIEs in this CU so we know which get local refs.  */
8454   mark_dies (node->root_die);
8455
8456   build_abbrev_table (node->root_die);
8457
8458   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8459   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8460   calc_die_sizes (node->root_die);
8461
8462 #if defined (OBJECT_FORMAT_ELF)
8463   secname = ".debug_types";
8464   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8465   sprintf (tmp, "wt.");
8466   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8467     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8468   comdat_key = get_identifier (tmp);
8469   targetm.asm_out.named_section (secname,
8470                                  SECTION_DEBUG | SECTION_LINKONCE,
8471                                  comdat_key);
8472 #else
8473   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8474   sprintf (tmp, ".gnu.linkonce.wt.");
8475   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8476     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8477   secname = tmp;
8478   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8479 #endif
8480
8481   /* Output debugging information.  */
8482   output_compilation_unit_header ();
8483   output_signature (node->signature, "Type Signature");
8484   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8485                        "Offset to Type DIE");
8486   output_die (node->root_die);
8487
8488   unmark_dies (node->root_die);
8489 }
8490
8491 /* Return the DWARF2/3 pubname associated with a decl.  */
8492
8493 static const char *
8494 dwarf2_name (tree decl, int scope)
8495 {
8496   if (DECL_NAMELESS (decl))
8497     return NULL;
8498   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8499 }
8500
8501 /* Add a new entry to .debug_pubnames if appropriate.  */
8502
8503 static void
8504 add_pubname_string (const char *str, dw_die_ref die)
8505 {
8506   if (targetm.want_debug_pub_sections)
8507     {
8508       pubname_entry e;
8509
8510       e.die = die;
8511       e.name = xstrdup (str);
8512       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8513     }
8514 }
8515
8516 static void
8517 add_pubname (tree decl, dw_die_ref die)
8518 {
8519   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8520     {
8521       const char *name = dwarf2_name (decl, 1);
8522       if (name)
8523         add_pubname_string (name, die);
8524     }
8525 }
8526
8527 /* Add a new entry to .debug_pubtypes if appropriate.  */
8528
8529 static void
8530 add_pubtype (tree decl, dw_die_ref die)
8531 {
8532   pubname_entry e;
8533
8534   if (!targetm.want_debug_pub_sections)
8535     return;
8536
8537   e.name = NULL;
8538   if ((TREE_PUBLIC (decl)
8539        || is_cu_die (die->die_parent))
8540       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8541     {
8542       e.die = die;
8543       if (TYPE_P (decl))
8544         {
8545           if (TYPE_NAME (decl))
8546             {
8547               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8548                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8549               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8550                        && DECL_NAME (TYPE_NAME (decl)))
8551                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8552               else
8553                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8554             }
8555         }
8556       else
8557         {
8558           e.name = dwarf2_name (decl, 1);
8559           if (e.name)
8560             e.name = xstrdup (e.name);
8561         }
8562
8563       /* If we don't have a name for the type, there's no point in adding
8564          it to the table.  */
8565       if (e.name && e.name[0] != '\0')
8566         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8567     }
8568 }
8569
8570 /* Output the public names table used to speed up access to externally
8571    visible names; or the public types table used to find type definitions.  */
8572
8573 static void
8574 output_pubnames (VEC (pubname_entry, gc) * names)
8575 {
8576   unsigned i;
8577   unsigned long pubnames_length = size_of_pubnames (names);
8578   pubname_ref pub;
8579
8580   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8581     dw2_asm_output_data (4, 0xffffffff,
8582       "Initial length escape value indicating 64-bit DWARF extension");
8583   if (names == pubname_table)
8584     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8585                          "Length of Public Names Info");
8586   else
8587     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8588                          "Length of Public Type Names Info");
8589   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8590   dw2_asm_output_data (2, 2, "DWARF Version");
8591   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8592                          debug_info_section,
8593                          "Offset of Compilation Unit Info");
8594   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8595                        "Compilation Unit Length");
8596
8597   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8598     {
8599       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8600       if (names == pubname_table)
8601         gcc_assert (pub->die->die_mark);
8602
8603       if (names != pubtype_table
8604           || pub->die->die_offset != 0
8605           || !flag_eliminate_unused_debug_types)
8606         {
8607           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8608                                "DIE offset");
8609
8610           dw2_asm_output_nstring (pub->name, -1, "external name");
8611         }
8612     }
8613
8614   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8615 }
8616
8617 /* Output the information that goes into the .debug_aranges table.
8618    Namely, define the beginning and ending address range of the
8619    text section generated for this compilation unit.  */
8620
8621 static void
8622 output_aranges (unsigned long aranges_length)
8623 {
8624   unsigned i;
8625
8626   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8627     dw2_asm_output_data (4, 0xffffffff,
8628       "Initial length escape value indicating 64-bit DWARF extension");
8629   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8630                        "Length of Address Ranges Info");
8631   /* Version number for aranges is still 2, even in DWARF3.  */
8632   dw2_asm_output_data (2, 2, "DWARF Version");
8633   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8634                          debug_info_section,
8635                          "Offset of Compilation Unit Info");
8636   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8637   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8638
8639   /* We need to align to twice the pointer size here.  */
8640   if (DWARF_ARANGES_PAD_SIZE)
8641     {
8642       /* Pad using a 2 byte words so that padding is correct for any
8643          pointer size.  */
8644       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8645                            2 * DWARF2_ADDR_SIZE);
8646       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8647         dw2_asm_output_data (2, 0, NULL);
8648     }
8649
8650   /* It is necessary not to output these entries if the sections were
8651      not used; if the sections were not used, the length will be 0 and
8652      the address may end up as 0 if the section is discarded by ld
8653      --gc-sections, leaving an invalid (0, 0) entry that can be
8654      confused with the terminator.  */
8655   if (text_section_used)
8656     {
8657       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8658       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8659                             text_section_label, "Length");
8660     }
8661   if (cold_text_section_used)
8662     {
8663       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8664                            "Address");
8665       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8666                             cold_text_section_label, "Length");
8667     }
8668
8669   if (have_multiple_function_sections)
8670     {
8671       unsigned fde_idx;
8672       dw_fde_ref fde;
8673
8674       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8675         {
8676           if (!fde->in_std_section)
8677             {
8678               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8679                                    "Address");
8680               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8681                                     fde->dw_fde_begin, "Length");
8682             }
8683           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8684             {
8685               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8686                                    "Address");
8687               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8688                                     fde->dw_fde_second_begin, "Length");
8689             }
8690         }
8691     }
8692
8693   /* Output the terminator words.  */
8694   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8695   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8696 }
8697
8698 /* Add a new entry to .debug_ranges.  Return the offset at which it
8699    was placed.  */
8700
8701 static unsigned int
8702 add_ranges_num (int num)
8703 {
8704   unsigned int in_use = ranges_table_in_use;
8705
8706   if (in_use == ranges_table_allocated)
8707     {
8708       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8709       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8710                                     ranges_table_allocated);
8711       memset (ranges_table + ranges_table_in_use, 0,
8712               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8713     }
8714
8715   ranges_table[in_use].num = num;
8716   ranges_table_in_use = in_use + 1;
8717
8718   return in_use * 2 * DWARF2_ADDR_SIZE;
8719 }
8720
8721 /* Add a new entry to .debug_ranges corresponding to a block, or a
8722    range terminator if BLOCK is NULL.  */
8723
8724 static unsigned int
8725 add_ranges (const_tree block)
8726 {
8727   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8728 }
8729
8730 /* Add a new entry to .debug_ranges corresponding to a pair of
8731    labels.  */
8732
8733 static void
8734 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8735                       bool *added)
8736 {
8737   unsigned int in_use = ranges_by_label_in_use;
8738   unsigned int offset;
8739
8740   if (in_use == ranges_by_label_allocated)
8741     {
8742       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8743       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8744                                        ranges_by_label,
8745                                        ranges_by_label_allocated);
8746       memset (ranges_by_label + ranges_by_label_in_use, 0,
8747               RANGES_TABLE_INCREMENT
8748               * sizeof (struct dw_ranges_by_label_struct));
8749     }
8750
8751   ranges_by_label[in_use].begin = begin;
8752   ranges_by_label[in_use].end = end;
8753   ranges_by_label_in_use = in_use + 1;
8754
8755   offset = add_ranges_num (-(int)in_use - 1);
8756   if (!*added)
8757     {
8758       add_AT_range_list (die, DW_AT_ranges, offset);
8759       *added = true;
8760     }
8761 }
8762
8763 static void
8764 output_ranges (void)
8765 {
8766   unsigned i;
8767   static const char *const start_fmt = "Offset %#x";
8768   const char *fmt = start_fmt;
8769
8770   for (i = 0; i < ranges_table_in_use; i++)
8771     {
8772       int block_num = ranges_table[i].num;
8773
8774       if (block_num > 0)
8775         {
8776           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8777           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8778
8779           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8780           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8781
8782           /* If all code is in the text section, then the compilation
8783              unit base address defaults to DW_AT_low_pc, which is the
8784              base of the text section.  */
8785           if (!have_multiple_function_sections)
8786             {
8787               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8788                                     text_section_label,
8789                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8790               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8791                                     text_section_label, NULL);
8792             }
8793
8794           /* Otherwise, the compilation unit base address is zero,
8795              which allows us to use absolute addresses, and not worry
8796              about whether the target supports cross-section
8797              arithmetic.  */
8798           else
8799             {
8800               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8801                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8802               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8803             }
8804
8805           fmt = NULL;
8806         }
8807
8808       /* Negative block_num stands for an index into ranges_by_label.  */
8809       else if (block_num < 0)
8810         {
8811           int lab_idx = - block_num - 1;
8812
8813           if (!have_multiple_function_sections)
8814             {
8815               gcc_unreachable ();
8816 #if 0
8817               /* If we ever use add_ranges_by_labels () for a single
8818                  function section, all we have to do is to take out
8819                  the #if 0 above.  */
8820               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8821                                     ranges_by_label[lab_idx].begin,
8822                                     text_section_label,
8823                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8824               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8825                                     ranges_by_label[lab_idx].end,
8826                                     text_section_label, NULL);
8827 #endif
8828             }
8829           else
8830             {
8831               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8832                                    ranges_by_label[lab_idx].begin,
8833                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8834               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8835                                    ranges_by_label[lab_idx].end,
8836                                    NULL);
8837             }
8838         }
8839       else
8840         {
8841           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8842           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8843           fmt = start_fmt;
8844         }
8845     }
8846 }
8847
8848 /* Data structure containing information about input files.  */
8849 struct file_info
8850 {
8851   const char *path;     /* Complete file name.  */
8852   const char *fname;    /* File name part.  */
8853   int length;           /* Length of entire string.  */
8854   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8855   int dir_idx;          /* Index in directory table.  */
8856 };
8857
8858 /* Data structure containing information about directories with source
8859    files.  */
8860 struct dir_info
8861 {
8862   const char *path;     /* Path including directory name.  */
8863   int length;           /* Path length.  */
8864   int prefix;           /* Index of directory entry which is a prefix.  */
8865   int count;            /* Number of files in this directory.  */
8866   int dir_idx;          /* Index of directory used as base.  */
8867 };
8868
8869 /* Callback function for file_info comparison.  We sort by looking at
8870    the directories in the path.  */
8871
8872 static int
8873 file_info_cmp (const void *p1, const void *p2)
8874 {
8875   const struct file_info *const s1 = (const struct file_info *) p1;
8876   const struct file_info *const s2 = (const struct file_info *) p2;
8877   const unsigned char *cp1;
8878   const unsigned char *cp2;
8879
8880   /* Take care of file names without directories.  We need to make sure that
8881      we return consistent values to qsort since some will get confused if
8882      we return the same value when identical operands are passed in opposite
8883      orders.  So if neither has a directory, return 0 and otherwise return
8884      1 or -1 depending on which one has the directory.  */
8885   if ((s1->path == s1->fname || s2->path == s2->fname))
8886     return (s2->path == s2->fname) - (s1->path == s1->fname);
8887
8888   cp1 = (const unsigned char *) s1->path;
8889   cp2 = (const unsigned char *) s2->path;
8890
8891   while (1)
8892     {
8893       ++cp1;
8894       ++cp2;
8895       /* Reached the end of the first path?  If so, handle like above.  */
8896       if ((cp1 == (const unsigned char *) s1->fname)
8897           || (cp2 == (const unsigned char *) s2->fname))
8898         return ((cp2 == (const unsigned char *) s2->fname)
8899                 - (cp1 == (const unsigned char *) s1->fname));
8900
8901       /* Character of current path component the same?  */
8902       else if (*cp1 != *cp2)
8903         return *cp1 - *cp2;
8904     }
8905 }
8906
8907 struct file_name_acquire_data
8908 {
8909   struct file_info *files;
8910   int used_files;
8911   int max_files;
8912 };
8913
8914 /* Traversal function for the hash table.  */
8915
8916 static int
8917 file_name_acquire (void ** slot, void *data)
8918 {
8919   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8920   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8921   struct file_info *fi;
8922   const char *f;
8923
8924   gcc_assert (fnad->max_files >= d->emitted_number);
8925
8926   if (! d->emitted_number)
8927     return 1;
8928
8929   gcc_assert (fnad->max_files != fnad->used_files);
8930
8931   fi = fnad->files + fnad->used_files++;
8932
8933   /* Skip all leading "./".  */
8934   f = d->filename;
8935   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8936     f += 2;
8937
8938   /* Create a new array entry.  */
8939   fi->path = f;
8940   fi->length = strlen (f);
8941   fi->file_idx = d;
8942
8943   /* Search for the file name part.  */
8944   f = strrchr (f, DIR_SEPARATOR);
8945 #if defined (DIR_SEPARATOR_2)
8946   {
8947     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8948
8949     if (g != NULL)
8950       {
8951         if (f == NULL || f < g)
8952           f = g;
8953       }
8954   }
8955 #endif
8956
8957   fi->fname = f == NULL ? fi->path : f + 1;
8958   return 1;
8959 }
8960
8961 /* Output the directory table and the file name table.  We try to minimize
8962    the total amount of memory needed.  A heuristic is used to avoid large
8963    slowdowns with many input files.  */
8964
8965 static void
8966 output_file_names (void)
8967 {
8968   struct file_name_acquire_data fnad;
8969   int numfiles;
8970   struct file_info *files;
8971   struct dir_info *dirs;
8972   int *saved;
8973   int *savehere;
8974   int *backmap;
8975   int ndirs;
8976   int idx_offset;
8977   int i;
8978
8979   if (!last_emitted_file)
8980     {
8981       dw2_asm_output_data (1, 0, "End directory table");
8982       dw2_asm_output_data (1, 0, "End file name table");
8983       return;
8984     }
8985
8986   numfiles = last_emitted_file->emitted_number;
8987
8988   /* Allocate the various arrays we need.  */
8989   files = XALLOCAVEC (struct file_info, numfiles);
8990   dirs = XALLOCAVEC (struct dir_info, numfiles);
8991
8992   fnad.files = files;
8993   fnad.used_files = 0;
8994   fnad.max_files = numfiles;
8995   htab_traverse (file_table, file_name_acquire, &fnad);
8996   gcc_assert (fnad.used_files == fnad.max_files);
8997
8998   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8999
9000   /* Find all the different directories used.  */
9001   dirs[0].path = files[0].path;
9002   dirs[0].length = files[0].fname - files[0].path;
9003   dirs[0].prefix = -1;
9004   dirs[0].count = 1;
9005   dirs[0].dir_idx = 0;
9006   files[0].dir_idx = 0;
9007   ndirs = 1;
9008
9009   for (i = 1; i < numfiles; i++)
9010     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9011         && memcmp (dirs[ndirs - 1].path, files[i].path,
9012                    dirs[ndirs - 1].length) == 0)
9013       {
9014         /* Same directory as last entry.  */
9015         files[i].dir_idx = ndirs - 1;
9016         ++dirs[ndirs - 1].count;
9017       }
9018     else
9019       {
9020         int j;
9021
9022         /* This is a new directory.  */
9023         dirs[ndirs].path = files[i].path;
9024         dirs[ndirs].length = files[i].fname - files[i].path;
9025         dirs[ndirs].count = 1;
9026         dirs[ndirs].dir_idx = ndirs;
9027         files[i].dir_idx = ndirs;
9028
9029         /* Search for a prefix.  */
9030         dirs[ndirs].prefix = -1;
9031         for (j = 0; j < ndirs; j++)
9032           if (dirs[j].length < dirs[ndirs].length
9033               && dirs[j].length > 1
9034               && (dirs[ndirs].prefix == -1
9035                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9036               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9037             dirs[ndirs].prefix = j;
9038
9039         ++ndirs;
9040       }
9041
9042   /* Now to the actual work.  We have to find a subset of the directories which
9043      allow expressing the file name using references to the directory table
9044      with the least amount of characters.  We do not do an exhaustive search
9045      where we would have to check out every combination of every single
9046      possible prefix.  Instead we use a heuristic which provides nearly optimal
9047      results in most cases and never is much off.  */
9048   saved = XALLOCAVEC (int, ndirs);
9049   savehere = XALLOCAVEC (int, ndirs);
9050
9051   memset (saved, '\0', ndirs * sizeof (saved[0]));
9052   for (i = 0; i < ndirs; i++)
9053     {
9054       int j;
9055       int total;
9056
9057       /* We can always save some space for the current directory.  But this
9058          does not mean it will be enough to justify adding the directory.  */
9059       savehere[i] = dirs[i].length;
9060       total = (savehere[i] - saved[i]) * dirs[i].count;
9061
9062       for (j = i + 1; j < ndirs; j++)
9063         {
9064           savehere[j] = 0;
9065           if (saved[j] < dirs[i].length)
9066             {
9067               /* Determine whether the dirs[i] path is a prefix of the
9068                  dirs[j] path.  */
9069               int k;
9070
9071               k = dirs[j].prefix;
9072               while (k != -1 && k != (int) i)
9073                 k = dirs[k].prefix;
9074
9075               if (k == (int) i)
9076                 {
9077                   /* Yes it is.  We can possibly save some memory by
9078                      writing the filenames in dirs[j] relative to
9079                      dirs[i].  */
9080                   savehere[j] = dirs[i].length;
9081                   total += (savehere[j] - saved[j]) * dirs[j].count;
9082                 }
9083             }
9084         }
9085
9086       /* Check whether we can save enough to justify adding the dirs[i]
9087          directory.  */
9088       if (total > dirs[i].length + 1)
9089         {
9090           /* It's worthwhile adding.  */
9091           for (j = i; j < ndirs; j++)
9092             if (savehere[j] > 0)
9093               {
9094                 /* Remember how much we saved for this directory so far.  */
9095                 saved[j] = savehere[j];
9096
9097                 /* Remember the prefix directory.  */
9098                 dirs[j].dir_idx = i;
9099               }
9100         }
9101     }
9102
9103   /* Emit the directory name table.  */
9104   idx_offset = dirs[0].length > 0 ? 1 : 0;
9105   for (i = 1 - idx_offset; i < ndirs; i++)
9106     dw2_asm_output_nstring (dirs[i].path,
9107                             dirs[i].length
9108                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9109                             "Directory Entry: %#x", i + idx_offset);
9110
9111   dw2_asm_output_data (1, 0, "End directory table");
9112
9113   /* We have to emit them in the order of emitted_number since that's
9114      used in the debug info generation.  To do this efficiently we
9115      generate a back-mapping of the indices first.  */
9116   backmap = XALLOCAVEC (int, numfiles);
9117   for (i = 0; i < numfiles; i++)
9118     backmap[files[i].file_idx->emitted_number - 1] = i;
9119
9120   /* Now write all the file names.  */
9121   for (i = 0; i < numfiles; i++)
9122     {
9123       int file_idx = backmap[i];
9124       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9125
9126 #ifdef VMS_DEBUGGING_INFO
9127 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9128
9129       /* Setting these fields can lead to debugger miscomparisons,
9130          but VMS Debug requires them to be set correctly.  */
9131
9132       int ver;
9133       long long cdt;
9134       long siz;
9135       int maxfilelen = strlen (files[file_idx].path)
9136                                + dirs[dir_idx].length
9137                                + MAX_VMS_VERSION_LEN + 1;
9138       char *filebuf = XALLOCAVEC (char, maxfilelen);
9139
9140       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9141       snprintf (filebuf, maxfilelen, "%s;%d",
9142                 files[file_idx].path + dirs[dir_idx].length, ver);
9143
9144       dw2_asm_output_nstring
9145         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9146
9147       /* Include directory index.  */
9148       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9149
9150       /* Modification time.  */
9151       dw2_asm_output_data_uleb128
9152         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9153           ? cdt : 0,
9154          NULL);
9155
9156       /* File length in bytes.  */
9157       dw2_asm_output_data_uleb128
9158         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9159           ? siz : 0,
9160          NULL);
9161 #else
9162       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9163                               "File Entry: %#x", (unsigned) i + 1);
9164
9165       /* Include directory index.  */
9166       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9167
9168       /* Modification time.  */
9169       dw2_asm_output_data_uleb128 (0, NULL);
9170
9171       /* File length in bytes.  */
9172       dw2_asm_output_data_uleb128 (0, NULL);
9173 #endif /* VMS_DEBUGGING_INFO */
9174     }
9175
9176   dw2_asm_output_data (1, 0, "End file name table");
9177 }
9178
9179
9180 /* Output one line number table into the .debug_line section.  */
9181
9182 static void
9183 output_one_line_info_table (dw_line_info_table *table)
9184 {
9185   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9186   unsigned int current_line = 1;
9187   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9188   dw_line_info_entry *ent;
9189   size_t i;
9190
9191   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9192     {
9193       switch (ent->opcode)
9194         {
9195         case LI_set_address:
9196           /* ??? Unfortunately, we have little choice here currently, and
9197              must always use the most general form.  GCC does not know the
9198              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9199              ports do have length attributes which will give an upper bound
9200              on the address range.  We could perhaps use length attributes
9201              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9202           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9203
9204           /* This can handle any delta.  This takes
9205              4+DWARF2_ADDR_SIZE bytes.  */
9206           dw2_asm_output_data (1, 0, "set address %s", line_label);
9207           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9208           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9209           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9210           break;
9211
9212         case LI_set_line:
9213           if (ent->val == current_line)
9214             {
9215               /* We still need to start a new row, so output a copy insn.  */
9216               dw2_asm_output_data (1, DW_LNS_copy,
9217                                    "copy line %u", current_line);
9218             }
9219           else
9220             {
9221               int line_offset = ent->val - current_line;
9222               int line_delta = line_offset - DWARF_LINE_BASE;
9223
9224               current_line = ent->val;
9225               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9226                 {
9227                   /* This can handle deltas from -10 to 234, using the current
9228                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9229                      This takes 1 byte.  */
9230                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9231                                        "line %u", current_line);
9232                 }
9233               else
9234                 {
9235                   /* This can handle any delta.  This takes at least 4 bytes,
9236                      depending on the value being encoded.  */
9237                   dw2_asm_output_data (1, DW_LNS_advance_line,
9238                                        "advance to line %u", current_line);
9239                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9240                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9241                 }
9242             }
9243           break;
9244
9245         case LI_set_file:
9246           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9247           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9248           break;
9249
9250         case LI_set_column:
9251           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9252           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9253           break;
9254
9255         case LI_negate_stmt:
9256           current_is_stmt = !current_is_stmt;
9257           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9258                                "is_stmt %d", current_is_stmt);
9259           break;
9260
9261         case LI_set_prologue_end:
9262           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9263                                "set prologue end");
9264           break;
9265           
9266         case LI_set_epilogue_begin:
9267           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9268                                "set epilogue begin");
9269           break;
9270
9271         case LI_set_discriminator:
9272           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9273           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9274           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9275           dw2_asm_output_data_uleb128 (ent->val, NULL);
9276           break;
9277         }
9278     }
9279
9280   /* Emit debug info for the address of the end of the table.  */
9281   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9282   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9283   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9284   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9285
9286   dw2_asm_output_data (1, 0, "end sequence");
9287   dw2_asm_output_data_uleb128 (1, NULL);
9288   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9289 }
9290
9291 /* Output the source line number correspondence information.  This
9292    information goes into the .debug_line section.  */
9293
9294 static void
9295 output_line_info (void)
9296 {
9297   char l1[20], l2[20], p1[20], p2[20];
9298   int ver = dwarf_version;
9299   bool saw_one = false;
9300   int opc;
9301
9302   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9303   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9304   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9305   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9306
9307   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9308     dw2_asm_output_data (4, 0xffffffff,
9309       "Initial length escape value indicating 64-bit DWARF extension");
9310   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9311                         "Length of Source Line Info");
9312   ASM_OUTPUT_LABEL (asm_out_file, l1);
9313
9314   dw2_asm_output_data (2, ver, "DWARF Version");
9315   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9316   ASM_OUTPUT_LABEL (asm_out_file, p1);
9317
9318   /* Define the architecture-dependent minimum instruction length (in bytes).
9319      In this implementation of DWARF, this field is used for information
9320      purposes only.  Since GCC generates assembly language, we have no
9321      a priori knowledge of how many instruction bytes are generated for each
9322      source line, and therefore can use only the DW_LNE_set_address and
9323      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9324      this as '1', which is "correct enough" for all architectures,
9325      and don't let the target override.  */
9326   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9327
9328   if (ver >= 4)
9329     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9330                          "Maximum Operations Per Instruction");
9331   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9332                        "Default is_stmt_start flag");
9333   dw2_asm_output_data (1, DWARF_LINE_BASE,
9334                        "Line Base Value (Special Opcodes)");
9335   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9336                        "Line Range Value (Special Opcodes)");
9337   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9338                        "Special Opcode Base");
9339
9340   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9341     {
9342       int n_op_args;
9343       switch (opc)
9344         {
9345         case DW_LNS_advance_pc:
9346         case DW_LNS_advance_line:
9347         case DW_LNS_set_file:
9348         case DW_LNS_set_column:
9349         case DW_LNS_fixed_advance_pc:
9350         case DW_LNS_set_isa:
9351           n_op_args = 1;
9352           break;
9353         default:
9354           n_op_args = 0;
9355           break;
9356         }
9357
9358       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9359                            opc, n_op_args);
9360     }
9361
9362   /* Write out the information about the files we use.  */
9363   output_file_names ();
9364   ASM_OUTPUT_LABEL (asm_out_file, p2);
9365
9366   if (separate_line_info)
9367     {
9368       dw_line_info_table *table;
9369       size_t i;
9370
9371       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9372         if (table->in_use)
9373           {
9374             output_one_line_info_table (table);
9375             saw_one = true;
9376           }
9377     }
9378   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9379     {
9380       output_one_line_info_table (cold_text_section_line_info);
9381       saw_one = true;
9382     }
9383
9384   /* ??? Some Darwin linkers crash on a .debug_line section with no
9385      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9386      sufficient -- the address column must also be initialized.
9387      Make sure to output at least one set_address/end_sequence pair,
9388      choosing .text since that section is always present.  */
9389   if (text_section_line_info->in_use || !saw_one)
9390     output_one_line_info_table (text_section_line_info);
9391
9392   /* Output the marker for the end of the line number info.  */
9393   ASM_OUTPUT_LABEL (asm_out_file, l2);
9394 }
9395 \f
9396 /* Given a pointer to a tree node for some base type, return a pointer to
9397    a DIE that describes the given type.
9398
9399    This routine must only be called for GCC type nodes that correspond to
9400    Dwarf base (fundamental) types.  */
9401
9402 static dw_die_ref
9403 base_type_die (tree type)
9404 {
9405   dw_die_ref base_type_result;
9406   enum dwarf_type encoding;
9407
9408   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9409     return 0;
9410
9411   /* If this is a subtype that should not be emitted as a subrange type,
9412      use the base type.  See subrange_type_for_debug_p.  */
9413   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9414     type = TREE_TYPE (type);
9415
9416   switch (TREE_CODE (type))
9417     {
9418     case INTEGER_TYPE:
9419       if ((dwarf_version >= 4 || !dwarf_strict)
9420           && TYPE_NAME (type)
9421           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9422           && DECL_IS_BUILTIN (TYPE_NAME (type))
9423           && DECL_NAME (TYPE_NAME (type)))
9424         {
9425           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9426           if (strcmp (name, "char16_t") == 0
9427               || strcmp (name, "char32_t") == 0)
9428             {
9429               encoding = DW_ATE_UTF;
9430               break;
9431             }
9432         }
9433       if (TYPE_STRING_FLAG (type))
9434         {
9435           if (TYPE_UNSIGNED (type))
9436             encoding = DW_ATE_unsigned_char;
9437           else
9438             encoding = DW_ATE_signed_char;
9439         }
9440       else if (TYPE_UNSIGNED (type))
9441         encoding = DW_ATE_unsigned;
9442       else
9443         encoding = DW_ATE_signed;
9444       break;
9445
9446     case REAL_TYPE:
9447       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9448         {
9449           if (dwarf_version >= 3 || !dwarf_strict)
9450             encoding = DW_ATE_decimal_float;
9451           else
9452             encoding = DW_ATE_lo_user;
9453         }
9454       else
9455         encoding = DW_ATE_float;
9456       break;
9457
9458     case FIXED_POINT_TYPE:
9459       if (!(dwarf_version >= 3 || !dwarf_strict))
9460         encoding = DW_ATE_lo_user;
9461       else if (TYPE_UNSIGNED (type))
9462         encoding = DW_ATE_unsigned_fixed;
9463       else
9464         encoding = DW_ATE_signed_fixed;
9465       break;
9466
9467       /* Dwarf2 doesn't know anything about complex ints, so use
9468          a user defined type for it.  */
9469     case COMPLEX_TYPE:
9470       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9471         encoding = DW_ATE_complex_float;
9472       else
9473         encoding = DW_ATE_lo_user;
9474       break;
9475
9476     case BOOLEAN_TYPE:
9477       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9478       encoding = DW_ATE_boolean;
9479       break;
9480
9481     default:
9482       /* No other TREE_CODEs are Dwarf fundamental types.  */
9483       gcc_unreachable ();
9484     }
9485
9486   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9487
9488   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9489                    int_size_in_bytes (type));
9490   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9491
9492   return base_type_result;
9493 }
9494
9495 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9496    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9497
9498 static inline int
9499 is_base_type (tree type)
9500 {
9501   switch (TREE_CODE (type))
9502     {
9503     case ERROR_MARK:
9504     case VOID_TYPE:
9505     case INTEGER_TYPE:
9506     case REAL_TYPE:
9507     case FIXED_POINT_TYPE:
9508     case COMPLEX_TYPE:
9509     case BOOLEAN_TYPE:
9510       return 1;
9511
9512     case ARRAY_TYPE:
9513     case RECORD_TYPE:
9514     case UNION_TYPE:
9515     case QUAL_UNION_TYPE:
9516     case ENUMERAL_TYPE:
9517     case FUNCTION_TYPE:
9518     case METHOD_TYPE:
9519     case POINTER_TYPE:
9520     case REFERENCE_TYPE:
9521     case NULLPTR_TYPE:
9522     case OFFSET_TYPE:
9523     case LANG_TYPE:
9524     case VECTOR_TYPE:
9525       return 0;
9526
9527     default:
9528       gcc_unreachable ();
9529     }
9530
9531   return 0;
9532 }
9533
9534 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9535    node, return the size in bits for the type if it is a constant, or else
9536    return the alignment for the type if the type's size is not constant, or
9537    else return BITS_PER_WORD if the type actually turns out to be an
9538    ERROR_MARK node.  */
9539
9540 static inline unsigned HOST_WIDE_INT
9541 simple_type_size_in_bits (const_tree type)
9542 {
9543   if (TREE_CODE (type) == ERROR_MARK)
9544     return BITS_PER_WORD;
9545   else if (TYPE_SIZE (type) == NULL_TREE)
9546     return 0;
9547   else if (host_integerp (TYPE_SIZE (type), 1))
9548     return tree_low_cst (TYPE_SIZE (type), 1);
9549   else
9550     return TYPE_ALIGN (type);
9551 }
9552
9553 /* Similarly, but return a double_int instead of UHWI.  */
9554
9555 static inline double_int
9556 double_int_type_size_in_bits (const_tree type)
9557 {
9558   if (TREE_CODE (type) == ERROR_MARK)
9559     return uhwi_to_double_int (BITS_PER_WORD);
9560   else if (TYPE_SIZE (type) == NULL_TREE)
9561     return double_int_zero;
9562   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9563     return tree_to_double_int (TYPE_SIZE (type));
9564   else
9565     return uhwi_to_double_int (TYPE_ALIGN (type));
9566 }
9567
9568 /*  Given a pointer to a tree node for a subrange type, return a pointer
9569     to a DIE that describes the given type.  */
9570
9571 static dw_die_ref
9572 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9573 {
9574   dw_die_ref subrange_die;
9575   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9576
9577   if (context_die == NULL)
9578     context_die = comp_unit_die ();
9579
9580   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9581
9582   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9583     {
9584       /* The size of the subrange type and its base type do not match,
9585          so we need to generate a size attribute for the subrange type.  */
9586       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9587     }
9588
9589   if (low)
9590     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9591   if (high)
9592     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9593
9594   return subrange_die;
9595 }
9596
9597 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9598    entry that chains various modifiers in front of the given type.  */
9599
9600 static dw_die_ref
9601 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9602                    dw_die_ref context_die)
9603 {
9604   enum tree_code code = TREE_CODE (type);
9605   dw_die_ref mod_type_die;
9606   dw_die_ref sub_die = NULL;
9607   tree item_type = NULL;
9608   tree qualified_type;
9609   tree name, low, high;
9610
9611   if (code == ERROR_MARK)
9612     return NULL;
9613
9614   /* See if we already have the appropriately qualified variant of
9615      this type.  */
9616   qualified_type
9617     = get_qualified_type (type,
9618                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9619                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9620
9621   if (qualified_type == sizetype
9622       && TYPE_NAME (qualified_type)
9623       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9624     {
9625       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9626
9627       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9628                            && TYPE_PRECISION (t)
9629                            == TYPE_PRECISION (qualified_type)
9630                            && TYPE_UNSIGNED (t)
9631                            == TYPE_UNSIGNED (qualified_type));
9632       qualified_type = t;
9633     }
9634
9635   /* If we do, then we can just use its DIE, if it exists.  */
9636   if (qualified_type)
9637     {
9638       mod_type_die = lookup_type_die (qualified_type);
9639       if (mod_type_die)
9640         return mod_type_die;
9641     }
9642
9643   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9644
9645   /* Handle C typedef types.  */
9646   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9647       && !DECL_ARTIFICIAL (name))
9648     {
9649       tree dtype = TREE_TYPE (name);
9650
9651       if (qualified_type == dtype)
9652         {
9653           /* For a named type, use the typedef.  */
9654           gen_type_die (qualified_type, context_die);
9655           return lookup_type_die (qualified_type);
9656         }
9657       else if (is_const_type < TYPE_READONLY (dtype)
9658                || is_volatile_type < TYPE_VOLATILE (dtype)
9659                || (is_const_type <= TYPE_READONLY (dtype)
9660                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9661                    && DECL_ORIGINAL_TYPE (name) != type))
9662         /* cv-unqualified version of named type.  Just use the unnamed
9663            type to which it refers.  */
9664         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9665                                   is_const_type, is_volatile_type,
9666                                   context_die);
9667       /* Else cv-qualified version of named type; fall through.  */
9668     }
9669
9670   if (is_const_type
9671       /* If both is_const_type and is_volatile_type, prefer the path
9672          which leads to a qualified type.  */
9673       && (!is_volatile_type
9674           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9675           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9676     {
9677       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9678       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9679     }
9680   else if (is_volatile_type)
9681     {
9682       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9683       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9684     }
9685   else if (code == POINTER_TYPE)
9686     {
9687       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9688       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9689                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9690       item_type = TREE_TYPE (type);
9691       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9692         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9693                          TYPE_ADDR_SPACE (item_type));
9694     }
9695   else if (code == REFERENCE_TYPE)
9696     {
9697       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9698         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9699                                 type);
9700       else
9701         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9702       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9703                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9704       item_type = TREE_TYPE (type);
9705       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9706         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9707                          TYPE_ADDR_SPACE (item_type));
9708     }
9709   else if (code == INTEGER_TYPE
9710            && TREE_TYPE (type) != NULL_TREE
9711            && subrange_type_for_debug_p (type, &low, &high))
9712     {
9713       mod_type_die = subrange_type_die (type, low, high, context_die);
9714       item_type = TREE_TYPE (type);
9715     }
9716   else if (is_base_type (type))
9717     mod_type_die = base_type_die (type);
9718   else
9719     {
9720       gen_type_die (type, context_die);
9721
9722       /* We have to get the type_main_variant here (and pass that to the
9723          `lookup_type_die' routine) because the ..._TYPE node we have
9724          might simply be a *copy* of some original type node (where the
9725          copy was created to help us keep track of typedef names) and
9726          that copy might have a different TYPE_UID from the original
9727          ..._TYPE node.  */
9728       if (TREE_CODE (type) != VECTOR_TYPE)
9729         return lookup_type_die (type_main_variant (type));
9730       else
9731         /* Vectors have the debugging information in the type,
9732            not the main variant.  */
9733         return lookup_type_die (type);
9734     }
9735
9736   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9737      don't output a DW_TAG_typedef, since there isn't one in the
9738      user's program; just attach a DW_AT_name to the type.
9739      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9740      if the base type already has the same name.  */
9741   if (name
9742       && ((TREE_CODE (name) != TYPE_DECL
9743            && (qualified_type == TYPE_MAIN_VARIANT (type)
9744                || (!is_const_type && !is_volatile_type)))
9745           || (TREE_CODE (name) == TYPE_DECL
9746               && TREE_TYPE (name) == qualified_type
9747               && DECL_NAME (name))))
9748     {
9749       if (TREE_CODE (name) == TYPE_DECL)
9750         /* Could just call add_name_and_src_coords_attributes here,
9751            but since this is a builtin type it doesn't have any
9752            useful source coordinates anyway.  */
9753         name = DECL_NAME (name);
9754       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9755       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9756       if (TYPE_ARTIFICIAL (type))
9757         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9758     }
9759   /* This probably indicates a bug.  */
9760   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9761     add_name_attribute (mod_type_die, "__unknown__");
9762
9763   if (qualified_type)
9764     equate_type_number_to_die (qualified_type, mod_type_die);
9765
9766   if (item_type)
9767     /* We must do this after the equate_type_number_to_die call, in case
9768        this is a recursive type.  This ensures that the modified_type_die
9769        recursion will terminate even if the type is recursive.  Recursive
9770        types are possible in Ada.  */
9771     sub_die = modified_type_die (item_type,
9772                                  TYPE_READONLY (item_type),
9773                                  TYPE_VOLATILE (item_type),
9774                                  context_die);
9775
9776   if (sub_die != NULL)
9777     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9778
9779   return mod_type_die;
9780 }
9781
9782 /* Generate DIEs for the generic parameters of T.
9783    T must be either a generic type or a generic function.
9784    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9785
9786 static void
9787 gen_generic_params_dies (tree t)
9788 {
9789   tree parms, args;
9790   int parms_num, i;
9791   dw_die_ref die = NULL;
9792
9793   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9794     return;
9795
9796   if (TYPE_P (t))
9797     die = lookup_type_die (t);
9798   else if (DECL_P (t))
9799     die = lookup_decl_die (t);
9800
9801   gcc_assert (die);
9802
9803   parms = lang_hooks.get_innermost_generic_parms (t);
9804   if (!parms)
9805     /* T has no generic parameter. It means T is neither a generic type
9806        or function. End of story.  */
9807     return;
9808
9809   parms_num = TREE_VEC_LENGTH (parms);
9810   args = lang_hooks.get_innermost_generic_args (t);
9811   for (i = 0; i < parms_num; i++)
9812     {
9813       tree parm, arg, arg_pack_elems;
9814
9815       parm = TREE_VEC_ELT (parms, i);
9816       arg = TREE_VEC_ELT (args, i);
9817       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9818       gcc_assert (parm && TREE_VALUE (parm) && arg);
9819
9820       if (parm && TREE_VALUE (parm) && arg)
9821         {
9822           /* If PARM represents a template parameter pack,
9823              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9824              by DW_TAG_template_*_parameter DIEs for the argument
9825              pack elements of ARG. Note that ARG would then be
9826              an argument pack.  */
9827           if (arg_pack_elems)
9828             template_parameter_pack_die (TREE_VALUE (parm),
9829                                          arg_pack_elems,
9830                                          die);
9831           else
9832             generic_parameter_die (TREE_VALUE (parm), arg,
9833                                    true /* Emit DW_AT_name */, die);
9834         }
9835     }
9836 }
9837
9838 /* Create and return a DIE for PARM which should be
9839    the representation of a generic type parameter.
9840    For instance, in the C++ front end, PARM would be a template parameter.
9841    ARG is the argument to PARM.
9842    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9843    name of the PARM.
9844    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9845    as a child node.  */
9846
9847 static dw_die_ref
9848 generic_parameter_die (tree parm, tree arg,
9849                        bool emit_name_p,
9850                        dw_die_ref parent_die)
9851 {
9852   dw_die_ref tmpl_die = NULL;
9853   const char *name = NULL;
9854
9855   if (!parm || !DECL_NAME (parm) || !arg)
9856     return NULL;
9857
9858   /* We support non-type generic parameters and arguments,
9859      type generic parameters and arguments, as well as
9860      generic generic parameters (a.k.a. template template parameters in C++)
9861      and arguments.  */
9862   if (TREE_CODE (parm) == PARM_DECL)
9863     /* PARM is a nontype generic parameter  */
9864     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9865   else if (TREE_CODE (parm) == TYPE_DECL)
9866     /* PARM is a type generic parameter.  */
9867     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9868   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9869     /* PARM is a generic generic parameter.
9870        Its DIE is a GNU extension. It shall have a
9871        DW_AT_name attribute to represent the name of the template template
9872        parameter, and a DW_AT_GNU_template_name attribute to represent the
9873        name of the template template argument.  */
9874     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9875                         parent_die, parm);
9876   else
9877     gcc_unreachable ();
9878
9879   if (tmpl_die)
9880     {
9881       tree tmpl_type;
9882
9883       /* If PARM is a generic parameter pack, it means we are
9884          emitting debug info for a template argument pack element.
9885          In other terms, ARG is a template argument pack element.
9886          In that case, we don't emit any DW_AT_name attribute for
9887          the die.  */
9888       if (emit_name_p)
9889         {
9890           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9891           gcc_assert (name);
9892           add_AT_string (tmpl_die, DW_AT_name, name);
9893         }
9894
9895       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9896         {
9897           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9898              TMPL_DIE should have a child DW_AT_type attribute that is set
9899              to the type of the argument to PARM, which is ARG.
9900              If PARM is a type generic parameter, TMPL_DIE should have a
9901              child DW_AT_type that is set to ARG.  */
9902           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9903           add_type_attribute (tmpl_die, tmpl_type, 0,
9904                               TREE_THIS_VOLATILE (tmpl_type),
9905                               parent_die);
9906         }
9907       else
9908         {
9909           /* So TMPL_DIE is a DIE representing a
9910              a generic generic template parameter, a.k.a template template
9911              parameter in C++ and arg is a template.  */
9912
9913           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9914              to the name of the argument.  */
9915           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9916           if (name)
9917             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9918         }
9919
9920       if (TREE_CODE (parm) == PARM_DECL)
9921         /* So PARM is a non-type generic parameter.
9922            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9923            attribute of TMPL_DIE which value represents the value
9924            of ARG.
9925            We must be careful here:
9926            The value of ARG might reference some function decls.
9927            We might currently be emitting debug info for a generic
9928            type and types are emitted before function decls, we don't
9929            know if the function decls referenced by ARG will actually be
9930            emitted after cgraph computations.
9931            So must defer the generation of the DW_AT_const_value to
9932            after cgraph is ready.  */
9933         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9934     }
9935
9936   return tmpl_die;
9937 }
9938
9939 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9940    PARM_PACK must be a template parameter pack. The returned DIE
9941    will be child DIE of PARENT_DIE.  */
9942
9943 static dw_die_ref
9944 template_parameter_pack_die (tree parm_pack,
9945                              tree parm_pack_args,
9946                              dw_die_ref parent_die)
9947 {
9948   dw_die_ref die;
9949   int j;
9950
9951   gcc_assert (parent_die && parm_pack);
9952
9953   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9954   add_name_and_src_coords_attributes (die, parm_pack);
9955   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9956     generic_parameter_die (parm_pack,
9957                            TREE_VEC_ELT (parm_pack_args, j),
9958                            false /* Don't emit DW_AT_name */,
9959                            die);
9960   return die;
9961 }
9962
9963 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9964    an enumerated type.  */
9965
9966 static inline int
9967 type_is_enum (const_tree type)
9968 {
9969   return TREE_CODE (type) == ENUMERAL_TYPE;
9970 }
9971
9972 /* Return the DBX register number described by a given RTL node.  */
9973
9974 static unsigned int
9975 dbx_reg_number (const_rtx rtl)
9976 {
9977   unsigned regno = REGNO (rtl);
9978
9979   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9980
9981 #ifdef LEAF_REG_REMAP
9982   if (current_function_uses_only_leaf_regs)
9983     {
9984       int leaf_reg = LEAF_REG_REMAP (regno);
9985       if (leaf_reg != -1)
9986         regno = (unsigned) leaf_reg;
9987     }
9988 #endif
9989
9990   return DBX_REGISTER_NUMBER (regno);
9991 }
9992
9993 /* Optionally add a DW_OP_piece term to a location description expression.
9994    DW_OP_piece is only added if the location description expression already
9995    doesn't end with DW_OP_piece.  */
9996
9997 static void
9998 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9999 {
10000   dw_loc_descr_ref loc;
10001
10002   if (*list_head != NULL)
10003     {
10004       /* Find the end of the chain.  */
10005       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10006         ;
10007
10008       if (loc->dw_loc_opc != DW_OP_piece)
10009         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10010     }
10011 }
10012
10013 /* Return a location descriptor that designates a machine register or
10014    zero if there is none.  */
10015
10016 static dw_loc_descr_ref
10017 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10018 {
10019   rtx regs;
10020
10021   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10022     return 0;
10023
10024   /* We only use "frame base" when we're sure we're talking about the
10025      post-prologue local stack frame.  We do this by *not* running
10026      register elimination until this point, and recognizing the special
10027      argument pointer and soft frame pointer rtx's.
10028      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10029   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10030       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10031     {
10032       dw_loc_descr_ref result = NULL;
10033
10034       if (dwarf_version >= 4 || !dwarf_strict)
10035         {
10036           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10037                                        initialized);
10038           if (result)
10039             add_loc_descr (&result,
10040                            new_loc_descr (DW_OP_stack_value, 0, 0));
10041         }
10042       return result;
10043     }
10044
10045   regs = targetm.dwarf_register_span (rtl);
10046
10047   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10048     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10049   else
10050     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10051 }
10052
10053 /* Return a location descriptor that designates a machine register for
10054    a given hard register number.  */
10055
10056 static dw_loc_descr_ref
10057 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10058 {
10059   dw_loc_descr_ref reg_loc_descr;
10060
10061   if (regno <= 31)
10062     reg_loc_descr
10063       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10064   else
10065     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10066
10067   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10068     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10069
10070   return reg_loc_descr;
10071 }
10072
10073 /* Given an RTL of a register, return a location descriptor that
10074    designates a value that spans more than one register.  */
10075
10076 static dw_loc_descr_ref
10077 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10078                              enum var_init_status initialized)
10079 {
10080   int nregs, size, i;
10081   unsigned reg;
10082   dw_loc_descr_ref loc_result = NULL;
10083
10084   reg = REGNO (rtl);
10085 #ifdef LEAF_REG_REMAP
10086   if (current_function_uses_only_leaf_regs)
10087     {
10088       int leaf_reg = LEAF_REG_REMAP (reg);
10089       if (leaf_reg != -1)
10090         reg = (unsigned) leaf_reg;
10091     }
10092 #endif
10093   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10094   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10095
10096   /* Simple, contiguous registers.  */
10097   if (regs == NULL_RTX)
10098     {
10099       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10100
10101       loc_result = NULL;
10102       while (nregs--)
10103         {
10104           dw_loc_descr_ref t;
10105
10106           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10107                                       VAR_INIT_STATUS_INITIALIZED);
10108           add_loc_descr (&loc_result, t);
10109           add_loc_descr_op_piece (&loc_result, size);
10110           ++reg;
10111         }
10112       return loc_result;
10113     }
10114
10115   /* Now onto stupid register sets in non contiguous locations.  */
10116
10117   gcc_assert (GET_CODE (regs) == PARALLEL);
10118
10119   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10120   loc_result = NULL;
10121
10122   for (i = 0; i < XVECLEN (regs, 0); ++i)
10123     {
10124       dw_loc_descr_ref t;
10125
10126       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10127                                   VAR_INIT_STATUS_INITIALIZED);
10128       add_loc_descr (&loc_result, t);
10129       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10130       add_loc_descr_op_piece (&loc_result, size);
10131     }
10132
10133   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10134     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10135   return loc_result;
10136 }
10137
10138 /* Return a location descriptor that designates a constant.  */
10139
10140 static dw_loc_descr_ref
10141 int_loc_descriptor (HOST_WIDE_INT i)
10142 {
10143   enum dwarf_location_atom op;
10144
10145   /* Pick the smallest representation of a constant, rather than just
10146      defaulting to the LEB encoding.  */
10147   if (i >= 0)
10148     {
10149       if (i <= 31)
10150         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10151       else if (i <= 0xff)
10152         op = DW_OP_const1u;
10153       else if (i <= 0xffff)
10154         op = DW_OP_const2u;
10155       else if (HOST_BITS_PER_WIDE_INT == 32
10156                || i <= 0xffffffff)
10157         op = DW_OP_const4u;
10158       else
10159         op = DW_OP_constu;
10160     }
10161   else
10162     {
10163       if (i >= -0x80)
10164         op = DW_OP_const1s;
10165       else if (i >= -0x8000)
10166         op = DW_OP_const2s;
10167       else if (HOST_BITS_PER_WIDE_INT == 32
10168                || i >= -0x80000000)
10169         op = DW_OP_const4s;
10170       else
10171         op = DW_OP_consts;
10172     }
10173
10174   return new_loc_descr (op, i, 0);
10175 }
10176
10177 /* Return size_of_locs (int_loc_descriptor (i)) without
10178    actually allocating it.  */
10179
10180 static unsigned long
10181 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10182 {
10183   if (i >= 0)
10184     {
10185       if (i <= 31)
10186         return 1;
10187       else if (i <= 0xff)
10188         return 2;
10189       else if (i <= 0xffff)
10190         return 3;
10191       else if (HOST_BITS_PER_WIDE_INT == 32
10192                || i <= 0xffffffff)
10193         return 5;
10194       else
10195         return 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10196     }
10197   else
10198     {
10199       if (i >= -0x80)
10200         return 2;
10201       else if (i >= -0x8000)
10202         return 3;
10203       else if (HOST_BITS_PER_WIDE_INT == 32
10204                || i >= -0x80000000)
10205         return 5;
10206       else
10207         return 1 + size_of_sleb128 (i);
10208     }
10209 }
10210
10211 /* Return loc description representing "address" of integer value.
10212    This can appear only as toplevel expression.  */
10213
10214 static dw_loc_descr_ref
10215 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10216 {
10217   int litsize;
10218   dw_loc_descr_ref loc_result = NULL;
10219
10220   if (!(dwarf_version >= 4 || !dwarf_strict))
10221     return NULL;
10222
10223   litsize = size_of_int_loc_descriptor (i);
10224   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10225      is more compact.  For DW_OP_stack_value we need:
10226      litsize + 1 (DW_OP_stack_value)
10227      and for DW_OP_implicit_value:
10228      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10229   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10230     {
10231       loc_result = int_loc_descriptor (i);
10232       add_loc_descr (&loc_result,
10233                      new_loc_descr (DW_OP_stack_value, 0, 0));
10234       return loc_result;
10235     }
10236
10237   loc_result = new_loc_descr (DW_OP_implicit_value,
10238                               size, 0);
10239   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10240   loc_result->dw_loc_oprnd2.v.val_int = i;
10241   return loc_result;
10242 }
10243
10244 /* Return a location descriptor that designates a base+offset location.  */
10245
10246 static dw_loc_descr_ref
10247 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10248                  enum var_init_status initialized)
10249 {
10250   unsigned int regno;
10251   dw_loc_descr_ref result;
10252   dw_fde_ref fde = cfun->fde;
10253
10254   /* We only use "frame base" when we're sure we're talking about the
10255      post-prologue local stack frame.  We do this by *not* running
10256      register elimination until this point, and recognizing the special
10257      argument pointer and soft frame pointer rtx's.  */
10258   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10259     {
10260       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10261
10262       if (elim != reg)
10263         {
10264           if (GET_CODE (elim) == PLUS)
10265             {
10266               offset += INTVAL (XEXP (elim, 1));
10267               elim = XEXP (elim, 0);
10268             }
10269           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10270                        && (elim == hard_frame_pointer_rtx
10271                            || elim == stack_pointer_rtx))
10272                       || elim == (frame_pointer_needed
10273                                   ? hard_frame_pointer_rtx
10274                                   : stack_pointer_rtx));
10275
10276           /* If drap register is used to align stack, use frame
10277              pointer + offset to access stack variables.  If stack
10278              is aligned without drap, use stack pointer + offset to
10279              access stack variables.  */
10280           if (crtl->stack_realign_tried
10281               && reg == frame_pointer_rtx)
10282             {
10283               int base_reg
10284                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10285                                       ? HARD_FRAME_POINTER_REGNUM
10286                                       : REGNO (elim));
10287               return new_reg_loc_descr (base_reg, offset);
10288             }
10289
10290           gcc_assert (frame_pointer_fb_offset_valid);
10291           offset += frame_pointer_fb_offset;
10292           return new_loc_descr (DW_OP_fbreg, offset, 0);
10293         }
10294     }
10295   else if (!optimize
10296            && fde
10297            && (fde->drap_reg == REGNO (reg)
10298                || fde->vdrap_reg == REGNO (reg)))
10299     {
10300       /* Use cfa+offset to represent the location of arguments passed
10301          on the stack when drap is used to align stack.
10302          Only do this when not optimizing, for optimized code var-tracking
10303          is supposed to track where the arguments live and the register
10304          used as vdrap or drap in some spot might be used for something
10305          else in other part of the routine.  */
10306       return new_loc_descr (DW_OP_fbreg, offset, 0);
10307     }
10308
10309   regno = dbx_reg_number (reg);
10310   if (regno <= 31)
10311     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10312                             offset, 0);
10313   else
10314     result = new_loc_descr (DW_OP_bregx, regno, offset);
10315
10316   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10317     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10318
10319   return result;
10320 }
10321
10322 /* Return true if this RTL expression describes a base+offset calculation.  */
10323
10324 static inline int
10325 is_based_loc (const_rtx rtl)
10326 {
10327   return (GET_CODE (rtl) == PLUS
10328           && ((REG_P (XEXP (rtl, 0))
10329                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10330                && CONST_INT_P (XEXP (rtl, 1)))));
10331 }
10332
10333 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10334    failed.  */
10335
10336 static dw_loc_descr_ref
10337 tls_mem_loc_descriptor (rtx mem)
10338 {
10339   tree base;
10340   dw_loc_descr_ref loc_result;
10341
10342   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10343     return NULL;
10344
10345   base = get_base_address (MEM_EXPR (mem));
10346   if (base == NULL
10347       || TREE_CODE (base) != VAR_DECL
10348       || !DECL_THREAD_LOCAL_P (base))
10349     return NULL;
10350
10351   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10352   if (loc_result == NULL)
10353     return NULL;
10354
10355   if (INTVAL (MEM_OFFSET (mem)))
10356     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10357
10358   return loc_result;
10359 }
10360
10361 /* Output debug info about reason why we failed to expand expression as dwarf
10362    expression.  */
10363
10364 static void
10365 expansion_failed (tree expr, rtx rtl, char const *reason)
10366 {
10367   if (dump_file && (dump_flags & TDF_DETAILS))
10368     {
10369       fprintf (dump_file, "Failed to expand as dwarf: ");
10370       if (expr)
10371         print_generic_expr (dump_file, expr, dump_flags);
10372       if (rtl)
10373         {
10374           fprintf (dump_file, "\n");
10375           print_rtl (dump_file, rtl);
10376         }
10377       fprintf (dump_file, "\nReason: %s\n", reason);
10378     }
10379 }
10380
10381 /* Helper function for const_ok_for_output, called either directly
10382    or via for_each_rtx.  */
10383
10384 static int
10385 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10386 {
10387   rtx rtl = *rtlp;
10388
10389   if (GET_CODE (rtl) == UNSPEC)
10390     {
10391       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10392          we can't express it in the debug info.  */
10393 #ifdef ENABLE_CHECKING
10394       /* Don't complain about TLS UNSPECs, those are just too hard to
10395          delegitimize.  */
10396       if (XVECLEN (rtl, 0) != 1
10397           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10398           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10399           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10400           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10401         inform (current_function_decl
10402                 ? DECL_SOURCE_LOCATION (current_function_decl)
10403                 : UNKNOWN_LOCATION,
10404 #if NUM_UNSPEC_VALUES > 0
10405                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10406                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10407                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10408                 XINT (rtl, 1));
10409 #else
10410                 "non-delegitimized UNSPEC %d found in variable location",
10411                 XINT (rtl, 1));
10412 #endif
10413 #endif
10414       expansion_failed (NULL_TREE, rtl,
10415                         "UNSPEC hasn't been delegitimized.\n");
10416       return 1;
10417     }
10418
10419   if (GET_CODE (rtl) != SYMBOL_REF)
10420     return 0;
10421
10422   if (CONSTANT_POOL_ADDRESS_P (rtl))
10423     {
10424       bool marked;
10425       get_pool_constant_mark (rtl, &marked);
10426       /* If all references to this pool constant were optimized away,
10427          it was not output and thus we can't represent it.  */
10428       if (!marked)
10429         {
10430           expansion_failed (NULL_TREE, rtl,
10431                             "Constant was removed from constant pool.\n");
10432           return 1;
10433         }
10434     }
10435
10436   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10437     return 1;
10438
10439   /* Avoid references to external symbols in debug info, on several targets
10440      the linker might even refuse to link when linking a shared library,
10441      and in many other cases the relocations for .debug_info/.debug_loc are
10442      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10443      to be defined within the same shared library or executable are fine.  */
10444   if (SYMBOL_REF_EXTERNAL_P (rtl))
10445     {
10446       tree decl = SYMBOL_REF_DECL (rtl);
10447
10448       if (decl == NULL || !targetm.binds_local_p (decl))
10449         {
10450           expansion_failed (NULL_TREE, rtl,
10451                             "Symbol not defined in current TU.\n");
10452           return 1;
10453         }
10454     }
10455
10456   return 0;
10457 }
10458
10459 /* Return true if constant RTL can be emitted in DW_OP_addr or
10460    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10461    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10462
10463 static bool
10464 const_ok_for_output (rtx rtl)
10465 {
10466   if (GET_CODE (rtl) == SYMBOL_REF)
10467     return const_ok_for_output_1 (&rtl, NULL) == 0;
10468
10469   if (GET_CODE (rtl) == CONST)
10470     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10471
10472   return true;
10473 }
10474
10475 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10476    if possible, NULL otherwise.  */
10477
10478 static dw_die_ref
10479 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10480 {
10481   dw_die_ref type_die;
10482   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10483
10484   if (type == NULL)
10485     return NULL;
10486   switch (TREE_CODE (type))
10487     {
10488     case INTEGER_TYPE:
10489     case REAL_TYPE:
10490       break;
10491     default:
10492       return NULL;
10493     }
10494   type_die = lookup_type_die (type);
10495   if (!type_die)
10496     type_die = modified_type_die (type, false, false, comp_unit_die ());
10497   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10498     return NULL;
10499   return type_die;
10500 }
10501
10502 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10503    type matching MODE, or, if MODE is narrower than or as wide as
10504    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10505    possible.  */
10506
10507 static dw_loc_descr_ref
10508 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10509 {
10510   enum machine_mode outer_mode = mode;
10511   dw_die_ref type_die;
10512   dw_loc_descr_ref cvt;
10513
10514   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10515     {
10516       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10517       return op;
10518     }
10519   type_die = base_type_for_mode (outer_mode, 1);
10520   if (type_die == NULL)
10521     return NULL;
10522   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10523   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10524   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10525   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10526   add_loc_descr (&op, cvt);
10527   return op;
10528 }
10529
10530 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10531
10532 static dw_loc_descr_ref
10533 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10534                         dw_loc_descr_ref op1)
10535 {
10536   dw_loc_descr_ref ret = op0;
10537   add_loc_descr (&ret, op1);
10538   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10539   if (STORE_FLAG_VALUE != 1)
10540     {
10541       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10542       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10543     }
10544   return ret;
10545 }
10546
10547 /* Return location descriptor for signed comparison OP RTL.  */
10548
10549 static dw_loc_descr_ref
10550 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10551                          enum machine_mode mem_mode)
10552 {
10553   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10554   dw_loc_descr_ref op0, op1;
10555   int shift;
10556
10557   if (op_mode == VOIDmode)
10558     op_mode = GET_MODE (XEXP (rtl, 1));
10559   if (op_mode == VOIDmode)
10560     return NULL;
10561
10562   if (dwarf_strict
10563       && (GET_MODE_CLASS (op_mode) != MODE_INT
10564           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10565     return NULL;
10566
10567   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10568                             VAR_INIT_STATUS_INITIALIZED);
10569   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10570                             VAR_INIT_STATUS_INITIALIZED);
10571
10572   if (op0 == NULL || op1 == NULL)
10573     return NULL;
10574
10575   if (GET_MODE_CLASS (op_mode) != MODE_INT
10576       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10577     return compare_loc_descriptor (op, op0, op1);
10578
10579   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10580     {
10581       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10582       dw_loc_descr_ref cvt;
10583
10584       if (type_die == NULL)
10585         return NULL;
10586       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10587       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10588       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10589       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10590       add_loc_descr (&op0, cvt);
10591       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10592       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10593       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10594       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10595       add_loc_descr (&op1, cvt);
10596       return compare_loc_descriptor (op, op0, op1);
10597     }
10598
10599   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10600   /* For eq/ne, if the operands are known to be zero-extended,
10601      there is no need to do the fancy shifting up.  */
10602   if (op == DW_OP_eq || op == DW_OP_ne)
10603     {
10604       dw_loc_descr_ref last0, last1;
10605       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10606         ;
10607       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10608         ;
10609       /* deref_size zero extends, and for constants we can check
10610          whether they are zero extended or not.  */
10611       if (((last0->dw_loc_opc == DW_OP_deref_size
10612             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10613            || (CONST_INT_P (XEXP (rtl, 0))
10614                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10615                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10616           && ((last1->dw_loc_opc == DW_OP_deref_size
10617                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10618               || (CONST_INT_P (XEXP (rtl, 1))
10619                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10620                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10621         return compare_loc_descriptor (op, op0, op1);
10622
10623       /* EQ/NE comparison against constant in narrower type than
10624          DWARF2_ADDR_SIZE can be performed either as
10625          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10626          DW_OP_{eq,ne}
10627          or
10628          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10629          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10630       if (CONST_INT_P (XEXP (rtl, 1))
10631           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10632           && (size_of_int_loc_descriptor (shift) + 1
10633               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10634               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10635                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10636                                                & GET_MODE_MASK (op_mode))))
10637         {
10638           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10639           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10640           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10641                                     & GET_MODE_MASK (op_mode));
10642           return compare_loc_descriptor (op, op0, op1);
10643         }
10644     }
10645   add_loc_descr (&op0, int_loc_descriptor (shift));
10646   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10647   if (CONST_INT_P (XEXP (rtl, 1)))
10648     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10649   else
10650     {
10651       add_loc_descr (&op1, int_loc_descriptor (shift));
10652       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10653     }
10654   return compare_loc_descriptor (op, op0, op1);
10655 }
10656
10657 /* Return location descriptor for unsigned comparison OP RTL.  */
10658
10659 static dw_loc_descr_ref
10660 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10661                          enum machine_mode mem_mode)
10662 {
10663   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10664   dw_loc_descr_ref op0, op1;
10665
10666   if (op_mode == VOIDmode)
10667     op_mode = GET_MODE (XEXP (rtl, 1));
10668   if (op_mode == VOIDmode)
10669     return NULL;
10670   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10671     return NULL;
10672
10673   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10674     return NULL;
10675
10676   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10677                             VAR_INIT_STATUS_INITIALIZED);
10678   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10679                             VAR_INIT_STATUS_INITIALIZED);
10680
10681   if (op0 == NULL || op1 == NULL)
10682     return NULL;
10683
10684   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10685     {
10686       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10687       dw_loc_descr_ref last0, last1;
10688       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10689         ;
10690       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10691         ;
10692       if (CONST_INT_P (XEXP (rtl, 0)))
10693         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10694       /* deref_size zero extends, so no need to mask it again.  */
10695       else if (last0->dw_loc_opc != DW_OP_deref_size
10696                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10697         {
10698           add_loc_descr (&op0, int_loc_descriptor (mask));
10699           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10700         }
10701       if (CONST_INT_P (XEXP (rtl, 1)))
10702         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10703       /* deref_size zero extends, so no need to mask it again.  */
10704       else if (last1->dw_loc_opc != DW_OP_deref_size
10705                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10706         {
10707           add_loc_descr (&op1, int_loc_descriptor (mask));
10708           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10709         }
10710     }
10711   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10712     {
10713       HOST_WIDE_INT bias = 1;
10714       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10715       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10716       if (CONST_INT_P (XEXP (rtl, 1)))
10717         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10718                                   + INTVAL (XEXP (rtl, 1)));
10719       else
10720         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10721                                             bias, 0));
10722     }
10723   return compare_loc_descriptor (op, op0, op1);
10724 }
10725
10726 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10727
10728 static dw_loc_descr_ref
10729 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10730                        enum machine_mode mem_mode)
10731 {
10732   enum dwarf_location_atom op;
10733   dw_loc_descr_ref op0, op1, ret;
10734   dw_loc_descr_ref bra_node, drop_node;
10735
10736   if (dwarf_strict
10737       && (GET_MODE_CLASS (mode) != MODE_INT
10738           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10739     return NULL;
10740
10741   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10742                             VAR_INIT_STATUS_INITIALIZED);
10743   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10744                             VAR_INIT_STATUS_INITIALIZED);
10745
10746   if (op0 == NULL || op1 == NULL)
10747     return NULL;
10748
10749   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10750   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10751   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10752   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10753     {
10754       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10755         {
10756           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10757           add_loc_descr (&op0, int_loc_descriptor (mask));
10758           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10759           add_loc_descr (&op1, int_loc_descriptor (mask));
10760           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10761         }
10762       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10763         {
10764           HOST_WIDE_INT bias = 1;
10765           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10766           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10767           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10768         }
10769     }
10770   else if (GET_MODE_CLASS (mode) == MODE_INT
10771            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10772     {
10773       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10774       add_loc_descr (&op0, int_loc_descriptor (shift));
10775       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10776       add_loc_descr (&op1, int_loc_descriptor (shift));
10777       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10778     }
10779   else if (GET_MODE_CLASS (mode) == MODE_INT
10780            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10781     {
10782       dw_die_ref type_die = base_type_for_mode (mode, 0);
10783       dw_loc_descr_ref cvt;
10784       if (type_die == NULL)
10785         return NULL;
10786       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10787       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10788       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10789       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10790       add_loc_descr (&op0, cvt);
10791       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10792       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10793       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10794       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10795       add_loc_descr (&op1, cvt);
10796     }
10797
10798   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10799     op = DW_OP_lt;
10800   else
10801     op = DW_OP_gt;
10802   ret = op0;
10803   add_loc_descr (&ret, op1);
10804   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10805   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10806   add_loc_descr (&ret, bra_node);
10807   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10808   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10809   add_loc_descr (&ret, drop_node);
10810   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10811   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10812   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10813       && GET_MODE_CLASS (mode) == MODE_INT
10814       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10815     ret = convert_descriptor_to_mode (mode, ret);
10816   return ret;
10817 }
10818
10819 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10820    but after converting arguments to type_die, afterwards
10821    convert back to unsigned.  */
10822
10823 static dw_loc_descr_ref
10824 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10825              enum machine_mode mode, enum machine_mode mem_mode)
10826 {
10827   dw_loc_descr_ref cvt, op0, op1;
10828
10829   if (type_die == NULL)
10830     return NULL;
10831   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10832                             VAR_INIT_STATUS_INITIALIZED);
10833   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10834                             VAR_INIT_STATUS_INITIALIZED);
10835   if (op0 == NULL || op1 == NULL)
10836     return NULL;
10837   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10838   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10839   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10840   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10841   add_loc_descr (&op0, cvt);
10842   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10843   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10844   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10845   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10846   add_loc_descr (&op1, cvt);
10847   add_loc_descr (&op0, op1);
10848   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10849   return convert_descriptor_to_mode (mode, op0);
10850 }
10851
10852 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10853    const0 is DW_OP_lit0 or corresponding typed constant,
10854    const1 is DW_OP_lit1 or corresponding typed constant
10855    and constMSB is constant with just the MSB bit set
10856    for the mode):
10857        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10858    L1: const0 DW_OP_swap
10859    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10860        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10861    L3: DW_OP_drop
10862    L4: DW_OP_nop
10863
10864    CTZ is similar:
10865        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10866    L1: const0 DW_OP_swap
10867    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10868        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10869    L3: DW_OP_drop
10870    L4: DW_OP_nop
10871
10872    FFS is similar:
10873        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10874    L1: const1 DW_OP_swap
10875    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10876        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10877    L3: DW_OP_drop
10878    L4: DW_OP_nop  */
10879
10880 static dw_loc_descr_ref
10881 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10882                     enum machine_mode mem_mode)
10883 {
10884   dw_loc_descr_ref op0, ret, tmp;
10885   HOST_WIDE_INT valv;
10886   dw_loc_descr_ref l1jump, l1label;
10887   dw_loc_descr_ref l2jump, l2label;
10888   dw_loc_descr_ref l3jump, l3label;
10889   dw_loc_descr_ref l4jump, l4label;
10890   rtx msb;
10891
10892   if (GET_MODE_CLASS (mode) != MODE_INT
10893       || GET_MODE (XEXP (rtl, 0)) != mode
10894       || (GET_CODE (rtl) == CLZ
10895           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
10896     return NULL;
10897
10898   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10899                             VAR_INIT_STATUS_INITIALIZED);
10900   if (op0 == NULL)
10901     return NULL;
10902   ret = op0;
10903   if (GET_CODE (rtl) == CLZ)
10904     {
10905       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10906         valv = GET_MODE_BITSIZE (mode);
10907     }
10908   else if (GET_CODE (rtl) == FFS)
10909     valv = 0;
10910   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10911     valv = GET_MODE_BITSIZE (mode);
10912   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10913   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10914   add_loc_descr (&ret, l1jump);
10915   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10916   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10917                             VAR_INIT_STATUS_INITIALIZED);
10918   if (tmp == NULL)
10919     return NULL;
10920   add_loc_descr (&ret, tmp);
10921   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10922   add_loc_descr (&ret, l4jump);
10923   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10924                                 ? const1_rtx : const0_rtx,
10925                                 mode, mem_mode,
10926                                 VAR_INIT_STATUS_INITIALIZED);
10927   if (l1label == NULL)
10928     return NULL;
10929   add_loc_descr (&ret, l1label);
10930   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10931   l2label = new_loc_descr (DW_OP_dup, 0, 0);
10932   add_loc_descr (&ret, l2label);
10933   if (GET_CODE (rtl) != CLZ)
10934     msb = const1_rtx;
10935   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10936     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10937                    << (GET_MODE_BITSIZE (mode) - 1));
10938   else
10939     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10940                                   << (GET_MODE_BITSIZE (mode)
10941                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
10942   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10943     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10944                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10945                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10946   else
10947     tmp = mem_loc_descriptor (msb, mode, mem_mode,
10948                               VAR_INIT_STATUS_INITIALIZED);
10949   if (tmp == NULL)
10950     return NULL;
10951   add_loc_descr (&ret, tmp);
10952   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10953   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10954   add_loc_descr (&ret, l3jump);
10955   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10956                             VAR_INIT_STATUS_INITIALIZED);
10957   if (tmp == NULL)
10958     return NULL;
10959   add_loc_descr (&ret, tmp);
10960   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10961                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
10962   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10963   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10964   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10965   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10966   add_loc_descr (&ret, l2jump);
10967   l3label = new_loc_descr (DW_OP_drop, 0, 0);
10968   add_loc_descr (&ret, l3label);
10969   l4label = new_loc_descr (DW_OP_nop, 0, 0);
10970   add_loc_descr (&ret, l4label);
10971   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10972   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10973   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10974   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10975   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10976   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10977   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10978   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10979   return ret;
10980 }
10981
10982 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10983    const1 is DW_OP_lit1 or corresponding typed constant):
10984        const0 DW_OP_swap
10985    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10986        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10987    L2: DW_OP_drop
10988
10989    PARITY is similar:
10990    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10991        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10992    L2: DW_OP_drop  */
10993
10994 static dw_loc_descr_ref
10995 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10996                          enum machine_mode mem_mode)
10997 {
10998   dw_loc_descr_ref op0, ret, tmp;
10999   dw_loc_descr_ref l1jump, l1label;
11000   dw_loc_descr_ref l2jump, l2label;
11001
11002   if (GET_MODE_CLASS (mode) != MODE_INT
11003       || GET_MODE (XEXP (rtl, 0)) != mode)
11004     return NULL;
11005
11006   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11007                             VAR_INIT_STATUS_INITIALIZED);
11008   if (op0 == NULL)
11009     return NULL;
11010   ret = op0;
11011   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11012                             VAR_INIT_STATUS_INITIALIZED);
11013   if (tmp == NULL)
11014     return NULL;
11015   add_loc_descr (&ret, tmp);
11016   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11017   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11018   add_loc_descr (&ret, l1label);
11019   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11020   add_loc_descr (&ret, l2jump);
11021   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11022   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11023   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11024                             VAR_INIT_STATUS_INITIALIZED);
11025   if (tmp == NULL)
11026     return NULL;
11027   add_loc_descr (&ret, tmp);
11028   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11029   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11030                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11031   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11032   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11033                             VAR_INIT_STATUS_INITIALIZED);
11034   add_loc_descr (&ret, tmp);
11035   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11036   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11037   add_loc_descr (&ret, l1jump);
11038   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11039   add_loc_descr (&ret, l2label);
11040   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11041   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11042   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11043   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11044   return ret;
11045 }
11046
11047 /* BSWAP (constS is initial shift count, either 56 or 24):
11048        constS const0
11049    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11050        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11051        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11052        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11053    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11054
11055 static dw_loc_descr_ref
11056 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11057                       enum machine_mode mem_mode)
11058 {
11059   dw_loc_descr_ref op0, ret, tmp;
11060   dw_loc_descr_ref l1jump, l1label;
11061   dw_loc_descr_ref l2jump, l2label;
11062
11063   if (GET_MODE_CLASS (mode) != MODE_INT
11064       || BITS_PER_UNIT != 8
11065       || (GET_MODE_BITSIZE (mode) != 32
11066           &&  GET_MODE_BITSIZE (mode) != 64))
11067     return NULL;
11068
11069   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11070                             VAR_INIT_STATUS_INITIALIZED);
11071   if (op0 == NULL)
11072     return NULL;
11073
11074   ret = op0;
11075   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11076                             mode, mem_mode,
11077                             VAR_INIT_STATUS_INITIALIZED);
11078   if (tmp == NULL)
11079     return NULL;
11080   add_loc_descr (&ret, tmp);
11081   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11082                             VAR_INIT_STATUS_INITIALIZED);
11083   if (tmp == NULL)
11084     return NULL;
11085   add_loc_descr (&ret, tmp);
11086   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11087   add_loc_descr (&ret, l1label);
11088   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11089                             mode, mem_mode,
11090                             VAR_INIT_STATUS_INITIALIZED);
11091   add_loc_descr (&ret, tmp);
11092   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11093   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11094   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11095   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11096                             VAR_INIT_STATUS_INITIALIZED);
11097   if (tmp == NULL)
11098     return NULL;
11099   add_loc_descr (&ret, tmp);
11100   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11101   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11102   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11103   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11104   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11105   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11106   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11107                             VAR_INIT_STATUS_INITIALIZED);
11108   add_loc_descr (&ret, tmp);
11109   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11110   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11111   add_loc_descr (&ret, l2jump);
11112   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11113                             VAR_INIT_STATUS_INITIALIZED);
11114   add_loc_descr (&ret, tmp);
11115   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11116   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11117   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11118   add_loc_descr (&ret, l1jump);
11119   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11120   add_loc_descr (&ret, l2label);
11121   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11122   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11123   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11124   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11125   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11126   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11127   return ret;
11128 }
11129
11130 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11131    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11132    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11133    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11134
11135    ROTATERT is similar:
11136    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11137    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11138    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11139
11140 static dw_loc_descr_ref
11141 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11142                        enum machine_mode mem_mode)
11143 {
11144   rtx rtlop1 = XEXP (rtl, 1);
11145   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11146   int i;
11147
11148   if (GET_MODE_CLASS (mode) != MODE_INT)
11149     return NULL;
11150
11151   if (GET_MODE (rtlop1) != VOIDmode
11152       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11153     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11154   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11155                             VAR_INIT_STATUS_INITIALIZED);
11156   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11157                             VAR_INIT_STATUS_INITIALIZED);
11158   if (op0 == NULL || op1 == NULL)
11159     return NULL;
11160   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11161     for (i = 0; i < 2; i++)
11162       {
11163         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11164           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11165                                         mode, mem_mode,
11166                                         VAR_INIT_STATUS_INITIALIZED);
11167         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11168           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11169                                    ? DW_OP_const4u
11170                                    : HOST_BITS_PER_WIDE_INT == 64
11171                                    ? DW_OP_const8u : DW_OP_constu,
11172                                    GET_MODE_MASK (mode), 0);
11173         else
11174           mask[i] = NULL;
11175         if (mask[i] == NULL)
11176           return NULL;
11177         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11178       }
11179   ret = op0;
11180   add_loc_descr (&ret, op1);
11181   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11182   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11183   if (GET_CODE (rtl) == ROTATERT)
11184     {
11185       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11186       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11187                                           GET_MODE_BITSIZE (mode), 0));
11188     }
11189   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11190   if (mask[0] != NULL)
11191     add_loc_descr (&ret, mask[0]);
11192   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11193   if (mask[1] != NULL)
11194     {
11195       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11196       add_loc_descr (&ret, mask[1]);
11197       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11198     }
11199   if (GET_CODE (rtl) == ROTATE)
11200     {
11201       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11202       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11203                                           GET_MODE_BITSIZE (mode), 0));
11204     }
11205   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11206   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11207   return ret;
11208 }
11209
11210 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11211    for DEBUG_PARAMETER_REF RTL.  */
11212
11213 static dw_loc_descr_ref
11214 parameter_ref_descriptor (rtx rtl)
11215 {
11216   dw_loc_descr_ref ret;
11217   dw_die_ref ref;
11218
11219   if (dwarf_strict)
11220     return NULL;
11221   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11222   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11223   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11224   if (ref)
11225     {
11226       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11227       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11228       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11229     }
11230   else
11231     {
11232       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11233       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11234     }
11235   return ret;
11236 }
11237
11238 /* Helper function to get mode of MEM's address.  */
11239
11240 enum machine_mode
11241 get_address_mode (rtx mem)
11242 {
11243   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11244   if (mode != VOIDmode)
11245     return mode;
11246   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11247 }
11248
11249 /* The following routine converts the RTL for a variable or parameter
11250    (resident in memory) into an equivalent Dwarf representation of a
11251    mechanism for getting the address of that same variable onto the top of a
11252    hypothetical "address evaluation" stack.
11253
11254    When creating memory location descriptors, we are effectively transforming
11255    the RTL for a memory-resident object into its Dwarf postfix expression
11256    equivalent.  This routine recursively descends an RTL tree, turning
11257    it into Dwarf postfix code as it goes.
11258
11259    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11260
11261    MEM_MODE is the mode of the memory reference, needed to handle some
11262    autoincrement addressing modes.
11263
11264    Return 0 if we can't represent the location.  */
11265
11266 dw_loc_descr_ref
11267 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11268                     enum machine_mode mem_mode,
11269                     enum var_init_status initialized)
11270 {
11271   dw_loc_descr_ref mem_loc_result = NULL;
11272   enum dwarf_location_atom op;
11273   dw_loc_descr_ref op0, op1;
11274
11275   if (mode == VOIDmode)
11276     mode = GET_MODE (rtl);
11277
11278   /* Note that for a dynamically sized array, the location we will generate a
11279      description of here will be the lowest numbered location which is
11280      actually within the array.  That's *not* necessarily the same as the
11281      zeroth element of the array.  */
11282
11283   rtl = targetm.delegitimize_address (rtl);
11284
11285   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11286     return NULL;
11287
11288   switch (GET_CODE (rtl))
11289     {
11290     case POST_INC:
11291     case POST_DEC:
11292     case POST_MODIFY:
11293       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11294
11295     case SUBREG:
11296       /* The case of a subreg may arise when we have a local (register)
11297          variable or a formal (register) parameter which doesn't quite fill
11298          up an entire register.  For now, just assume that it is
11299          legitimate to make the Dwarf info refer to the whole register which
11300          contains the given subreg.  */
11301       if (!subreg_lowpart_p (rtl))
11302         break;
11303       if (GET_MODE_CLASS (mode) == MODE_INT
11304           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11305           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11306 #ifdef POINTERS_EXTEND_UNSIGNED
11307               || (mode == Pmode && mem_mode != VOIDmode)
11308 #endif
11309              )
11310           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11311         {
11312           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11313                                                GET_MODE (SUBREG_REG (rtl)),
11314                                                mem_mode, initialized);
11315           break;
11316         }
11317       if (dwarf_strict)
11318         break;
11319       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11320         break;
11321       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11322           && (GET_MODE_CLASS (mode) != MODE_INT
11323               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11324         break;
11325       else
11326         {
11327           dw_die_ref type_die;
11328           dw_loc_descr_ref cvt;
11329
11330           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11331                                                GET_MODE (SUBREG_REG (rtl)),
11332                                                mem_mode, initialized);
11333           if (mem_loc_result == NULL)
11334             break;
11335           type_die = base_type_for_mode (mode,
11336                                          GET_MODE_CLASS (mode) == MODE_INT);
11337           if (type_die == NULL)
11338             {
11339               mem_loc_result = NULL;
11340               break;
11341             }
11342           if (GET_MODE_SIZE (mode)
11343               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11344             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11345           else
11346             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11347           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11348           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11349           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11350           add_loc_descr (&mem_loc_result, cvt);
11351         }
11352       break;
11353
11354     case REG:
11355       if (GET_MODE_CLASS (mode) != MODE_INT
11356           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11357 #ifdef POINTERS_EXTEND_UNSIGNED
11358               && (mode != Pmode || mem_mode == VOIDmode)
11359 #endif
11360               ))
11361         {
11362           dw_die_ref type_die;
11363
11364           if (dwarf_strict)
11365             break;
11366           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11367             break;
11368           type_die = base_type_for_mode (mode,
11369                                          GET_MODE_CLASS (mode) == MODE_INT);
11370           if (type_die == NULL)
11371             break;
11372           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11373                                           dbx_reg_number (rtl), 0);
11374           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11375           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11376           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11377           break;
11378         }
11379       /* Whenever a register number forms a part of the description of the
11380          method for calculating the (dynamic) address of a memory resident
11381          object, DWARF rules require the register number be referred to as
11382          a "base register".  This distinction is not based in any way upon
11383          what category of register the hardware believes the given register
11384          belongs to.  This is strictly DWARF terminology we're dealing with
11385          here. Note that in cases where the location of a memory-resident
11386          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11387          OP_CONST (0)) the actual DWARF location descriptor that we generate
11388          may just be OP_BASEREG (basereg).  This may look deceptively like
11389          the object in question was allocated to a register (rather than in
11390          memory) so DWARF consumers need to be aware of the subtle
11391          distinction between OP_REG and OP_BASEREG.  */
11392       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11393         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11394       else if (stack_realign_drap
11395                && crtl->drap_reg
11396                && crtl->args.internal_arg_pointer == rtl
11397                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11398         {
11399           /* If RTL is internal_arg_pointer, which has been optimized
11400              out, use DRAP instead.  */
11401           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11402                                             VAR_INIT_STATUS_INITIALIZED);
11403         }
11404       break;
11405
11406     case SIGN_EXTEND:
11407     case ZERO_EXTEND:
11408       if (GET_MODE_CLASS (mode) != MODE_INT)
11409         break;
11410       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11411                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11412       if (op0 == 0)
11413         break;
11414       else if (GET_CODE (rtl) == ZERO_EXTEND
11415                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11416                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11417                   < HOST_BITS_PER_WIDE_INT
11418                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11419                   to expand zero extend as two shifts instead of
11420                   masking.  */
11421                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11422         {
11423           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11424           mem_loc_result = op0;
11425           add_loc_descr (&mem_loc_result,
11426                          int_loc_descriptor (GET_MODE_MASK (imode)));
11427           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11428         }
11429       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11430         {
11431           int shift = DWARF2_ADDR_SIZE
11432                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11433           shift *= BITS_PER_UNIT;
11434           if (GET_CODE (rtl) == SIGN_EXTEND)
11435             op = DW_OP_shra;
11436           else
11437             op = DW_OP_shr;
11438           mem_loc_result = op0;
11439           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11440           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11441           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11442           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11443         }
11444       else if (!dwarf_strict)
11445         {
11446           dw_die_ref type_die1, type_die2;
11447           dw_loc_descr_ref cvt;
11448
11449           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11450                                           GET_CODE (rtl) == ZERO_EXTEND);
11451           if (type_die1 == NULL)
11452             break;
11453           type_die2 = base_type_for_mode (mode, 1);
11454           if (type_die2 == NULL)
11455             break;
11456           mem_loc_result = op0;
11457           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11458           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11459           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11460           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11461           add_loc_descr (&mem_loc_result, cvt);
11462           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11463           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11464           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11465           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11466           add_loc_descr (&mem_loc_result, cvt);
11467         }
11468       break;
11469
11470     case MEM:
11471       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11472                                            get_address_mode (rtl), mode,
11473                                            VAR_INIT_STATUS_INITIALIZED);
11474       if (mem_loc_result == NULL)
11475         mem_loc_result = tls_mem_loc_descriptor (rtl);
11476       if (mem_loc_result != 0)
11477         {
11478           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11479               || GET_MODE_CLASS (mode) != MODE_INT)
11480             {
11481               dw_die_ref type_die;
11482               dw_loc_descr_ref deref;
11483
11484               if (dwarf_strict)
11485                 return NULL;
11486               type_die
11487                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11488               if (type_die == NULL)
11489                 return NULL;
11490               deref = new_loc_descr (DW_OP_GNU_deref_type,
11491                                      GET_MODE_SIZE (mode), 0);
11492               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11493               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11494               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11495               add_loc_descr (&mem_loc_result, deref);
11496             }
11497           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11498             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11499           else
11500             add_loc_descr (&mem_loc_result,
11501                            new_loc_descr (DW_OP_deref_size,
11502                                           GET_MODE_SIZE (mode), 0));
11503         }
11504       else
11505         {
11506           rtx new_rtl = avoid_constant_pool_reference (rtl);
11507           if (new_rtl != rtl)
11508             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
11509         }
11510       break;
11511
11512     case LO_SUM:
11513       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11514
11515     case LABEL_REF:
11516       /* Some ports can transform a symbol ref into a label ref, because
11517          the symbol ref is too far away and has to be dumped into a constant
11518          pool.  */
11519     case CONST:
11520     case SYMBOL_REF:
11521       if (GET_MODE_CLASS (mode) != MODE_INT
11522           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11523 #ifdef POINTERS_EXTEND_UNSIGNED
11524               && (mode != Pmode || mem_mode == VOIDmode)
11525 #endif
11526               ))
11527         break;
11528       if (GET_CODE (rtl) == SYMBOL_REF
11529           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11530         {
11531           dw_loc_descr_ref temp;
11532
11533           /* If this is not defined, we have no way to emit the data.  */
11534           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11535             break;
11536
11537           /* We used to emit DW_OP_addr here, but that's wrong, since
11538              DW_OP_addr should be relocated by the debug info consumer,
11539              while DW_OP_GNU_push_tls_address operand should not.  */
11540           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11541                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11542           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11543           temp->dw_loc_oprnd1.v.val_addr = rtl;
11544           temp->dtprel = true;
11545
11546           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11547           add_loc_descr (&mem_loc_result, temp);
11548
11549           break;
11550         }
11551
11552       if (!const_ok_for_output (rtl))
11553         break;
11554
11555     symref:
11556       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11557       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11558       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11559       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11560       break;
11561
11562     case CONCAT:
11563     case CONCATN:
11564     case VAR_LOCATION:
11565     case DEBUG_IMPLICIT_PTR:
11566       expansion_failed (NULL_TREE, rtl,
11567                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11568       return 0;
11569
11570     case ENTRY_VALUE:
11571       if (dwarf_strict)
11572         return NULL;
11573       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11574         {
11575           if (GET_MODE_CLASS (mode) != MODE_INT
11576               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11577             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11578                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11579           else
11580             op0
11581               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11582                                         VAR_INIT_STATUS_INITIALIZED);
11583         }
11584       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11585                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11586         {
11587           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11588                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11589           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11590             return NULL;
11591         }
11592       else
11593         gcc_unreachable ();
11594       if (op0 == NULL)
11595         return NULL;
11596       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11597       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11598       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11599       break;
11600
11601     case DEBUG_PARAMETER_REF:
11602       mem_loc_result = parameter_ref_descriptor (rtl);
11603       break;
11604
11605     case PRE_MODIFY:
11606       /* Extract the PLUS expression nested inside and fall into
11607          PLUS code below.  */
11608       rtl = XEXP (rtl, 1);
11609       goto plus;
11610
11611     case PRE_INC:
11612     case PRE_DEC:
11613       /* Turn these into a PLUS expression and fall into the PLUS code
11614          below.  */
11615       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11616                           GEN_INT (GET_CODE (rtl) == PRE_INC
11617                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11618                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11619
11620       /* ... fall through ...  */
11621
11622     case PLUS:
11623     plus:
11624       if (is_based_loc (rtl)
11625           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11626           && GET_MODE_CLASS (mode) == MODE_INT)
11627         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11628                                           INTVAL (XEXP (rtl, 1)),
11629                                           VAR_INIT_STATUS_INITIALIZED);
11630       else
11631         {
11632           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11633                                                VAR_INIT_STATUS_INITIALIZED);
11634           if (mem_loc_result == 0)
11635             break;
11636
11637           if (CONST_INT_P (XEXP (rtl, 1))
11638               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11639             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11640           else
11641             {
11642               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11643                                         VAR_INIT_STATUS_INITIALIZED);
11644               if (op1 == 0)
11645                 break;
11646               add_loc_descr (&mem_loc_result, op1);
11647               add_loc_descr (&mem_loc_result,
11648                              new_loc_descr (DW_OP_plus, 0, 0));
11649             }
11650         }
11651       break;
11652
11653     /* If a pseudo-reg is optimized away, it is possible for it to
11654        be replaced with a MEM containing a multiply or shift.  */
11655     case MINUS:
11656       op = DW_OP_minus;
11657       goto do_binop;
11658
11659     case MULT:
11660       op = DW_OP_mul;
11661       goto do_binop;
11662
11663     case DIV:
11664       if (!dwarf_strict
11665           && GET_MODE_CLASS (mode) == MODE_INT
11666           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11667         {
11668           mem_loc_result = typed_binop (DW_OP_div, rtl,
11669                                         base_type_for_mode (mode, 0),
11670                                         mode, mem_mode);
11671           break;
11672         }
11673       op = DW_OP_div;
11674       goto do_binop;
11675
11676     case UMOD:
11677       op = DW_OP_mod;
11678       goto do_binop;
11679
11680     case ASHIFT:
11681       op = DW_OP_shl;
11682       goto do_shift;
11683
11684     case ASHIFTRT:
11685       op = DW_OP_shra;
11686       goto do_shift;
11687
11688     case LSHIFTRT:
11689       op = DW_OP_shr;
11690       goto do_shift;
11691
11692     do_shift:
11693       if (GET_MODE_CLASS (mode) != MODE_INT)
11694         break;
11695       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11696                                 VAR_INIT_STATUS_INITIALIZED);
11697       {
11698         rtx rtlop1 = XEXP (rtl, 1);
11699         if (GET_MODE (rtlop1) != VOIDmode
11700             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11701                < GET_MODE_BITSIZE (mode))
11702           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11703         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11704                                   VAR_INIT_STATUS_INITIALIZED);
11705       }
11706
11707       if (op0 == 0 || op1 == 0)
11708         break;
11709
11710       mem_loc_result = op0;
11711       add_loc_descr (&mem_loc_result, op1);
11712       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11713       break;
11714
11715     case AND:
11716       op = DW_OP_and;
11717       goto do_binop;
11718
11719     case IOR:
11720       op = DW_OP_or;
11721       goto do_binop;
11722
11723     case XOR:
11724       op = DW_OP_xor;
11725       goto do_binop;
11726
11727     do_binop:
11728       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11729                                 VAR_INIT_STATUS_INITIALIZED);
11730       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11731                                 VAR_INIT_STATUS_INITIALIZED);
11732
11733       if (op0 == 0 || op1 == 0)
11734         break;
11735
11736       mem_loc_result = op0;
11737       add_loc_descr (&mem_loc_result, op1);
11738       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11739       break;
11740
11741     case MOD:
11742       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11743         {
11744           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11745                                         base_type_for_mode (mode, 0),
11746                                         mode, mem_mode);
11747           break;
11748         }
11749
11750       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11751                                 VAR_INIT_STATUS_INITIALIZED);
11752       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11753                                 VAR_INIT_STATUS_INITIALIZED);
11754
11755       if (op0 == 0 || op1 == 0)
11756         break;
11757
11758       mem_loc_result = op0;
11759       add_loc_descr (&mem_loc_result, op1);
11760       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11761       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11762       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11763       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11764       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11765       break;
11766
11767     case UDIV:
11768       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11769         {
11770           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11771             {
11772               op = DW_OP_div;
11773               goto do_binop;
11774             }
11775           mem_loc_result = typed_binop (DW_OP_div, rtl,
11776                                         base_type_for_mode (mode, 1),
11777                                         mode, mem_mode);
11778         }
11779       break;
11780
11781     case NOT:
11782       op = DW_OP_not;
11783       goto do_unop;
11784
11785     case ABS:
11786       op = DW_OP_abs;
11787       goto do_unop;
11788
11789     case NEG:
11790       op = DW_OP_neg;
11791       goto do_unop;
11792
11793     do_unop:
11794       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11795                                 VAR_INIT_STATUS_INITIALIZED);
11796
11797       if (op0 == 0)
11798         break;
11799
11800       mem_loc_result = op0;
11801       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11802       break;
11803
11804     case CONST_INT:
11805       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11806 #ifdef POINTERS_EXTEND_UNSIGNED
11807           || (mode == Pmode
11808               && mem_mode != VOIDmode
11809               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11810 #endif
11811           )
11812         {
11813           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11814           break;
11815         }
11816       if (!dwarf_strict
11817           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11818               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
11819         {
11820           dw_die_ref type_die = base_type_for_mode (mode, 1);
11821           if (type_die == NULL)
11822             return NULL;
11823           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11824                                           INTVAL (rtl));
11825           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11826           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11827           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11828           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11829             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11830           else
11831             {
11832               mem_loc_result->dw_loc_oprnd2.val_class
11833                 = dw_val_class_const_double;
11834               mem_loc_result->dw_loc_oprnd2.v.val_double
11835                 = shwi_to_double_int (INTVAL (rtl));
11836             }
11837         }
11838       break;
11839
11840     case CONST_DOUBLE:
11841       if (!dwarf_strict)
11842         {
11843           dw_die_ref type_die;
11844
11845           /* Note that a CONST_DOUBLE rtx could represent either an integer
11846              or a floating-point constant.  A CONST_DOUBLE is used whenever
11847              the constant requires more than one word in order to be
11848              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11849           if (mode == VOIDmode
11850               || (GET_MODE (rtl) == VOIDmode
11851                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
11852             break;
11853           type_die = base_type_for_mode (mode,
11854                                          GET_MODE_CLASS (mode) == MODE_INT);
11855           if (type_die == NULL)
11856             return NULL;
11857           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11858           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11859           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11860           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11861           if (SCALAR_FLOAT_MODE_P (mode))
11862             {
11863               unsigned int length = GET_MODE_SIZE (mode);
11864               unsigned char *array
11865                   = (unsigned char*) ggc_alloc_atomic (length);
11866
11867               insert_float (rtl, array);
11868               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11869               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11870               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11871               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11872             }
11873           else
11874             {
11875               mem_loc_result->dw_loc_oprnd2.val_class
11876                 = dw_val_class_const_double;
11877               mem_loc_result->dw_loc_oprnd2.v.val_double
11878                 = rtx_to_double_int (rtl);
11879             }
11880         }
11881       break;
11882
11883     case EQ:
11884       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11885       break;
11886
11887     case GE:
11888       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11889       break;
11890
11891     case GT:
11892       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11893       break;
11894
11895     case LE:
11896       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11897       break;
11898
11899     case LT:
11900       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11901       break;
11902
11903     case NE:
11904       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11905       break;
11906
11907     case GEU:
11908       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11909       break;
11910
11911     case GTU:
11912       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11913       break;
11914
11915     case LEU:
11916       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11917       break;
11918
11919     case LTU:
11920       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11921       break;
11922
11923     case UMIN:
11924     case UMAX:
11925       if (GET_MODE_CLASS (mode) != MODE_INT)
11926         break;
11927       /* FALLTHRU */
11928     case SMIN:
11929     case SMAX:
11930       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11931       break;
11932
11933     case ZERO_EXTRACT:
11934     case SIGN_EXTRACT:
11935       if (CONST_INT_P (XEXP (rtl, 1))
11936           && CONST_INT_P (XEXP (rtl, 2))
11937           && ((unsigned) INTVAL (XEXP (rtl, 1))
11938               + (unsigned) INTVAL (XEXP (rtl, 2))
11939               <= GET_MODE_BITSIZE (mode))
11940           && GET_MODE_CLASS (mode) == MODE_INT
11941           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11942           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11943         {
11944           int shift, size;
11945           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11946                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
11947           if (op0 == 0)
11948             break;
11949           if (GET_CODE (rtl) == SIGN_EXTRACT)
11950             op = DW_OP_shra;
11951           else
11952             op = DW_OP_shr;
11953           mem_loc_result = op0;
11954           size = INTVAL (XEXP (rtl, 1));
11955           shift = INTVAL (XEXP (rtl, 2));
11956           if (BITS_BIG_ENDIAN)
11957             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11958                     - shift - size;
11959           if (shift + size != (int) DWARF2_ADDR_SIZE)
11960             {
11961               add_loc_descr (&mem_loc_result,
11962                              int_loc_descriptor (DWARF2_ADDR_SIZE
11963                                                  - shift - size));
11964               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11965             }
11966           if (size != (int) DWARF2_ADDR_SIZE)
11967             {
11968               add_loc_descr (&mem_loc_result,
11969                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11970               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11971             }
11972         }
11973       break;
11974
11975     case IF_THEN_ELSE:
11976       {
11977         dw_loc_descr_ref op2, bra_node, drop_node;
11978         op0 = mem_loc_descriptor (XEXP (rtl, 0),
11979                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
11980                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
11981                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
11982         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11983                                   VAR_INIT_STATUS_INITIALIZED);
11984         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11985                                   VAR_INIT_STATUS_INITIALIZED);
11986         if (op0 == NULL || op1 == NULL || op2 == NULL)
11987           break;
11988
11989         mem_loc_result = op1;
11990         add_loc_descr (&mem_loc_result, op2);
11991         add_loc_descr (&mem_loc_result, op0);
11992         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11993         add_loc_descr (&mem_loc_result, bra_node);
11994         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11995         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11996         add_loc_descr (&mem_loc_result, drop_node);
11997         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11998         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11999       }
12000       break;
12001
12002     case FLOAT_EXTEND:
12003     case FLOAT_TRUNCATE:
12004     case FLOAT:
12005     case UNSIGNED_FLOAT:
12006     case FIX:
12007     case UNSIGNED_FIX:
12008       if (!dwarf_strict)
12009         {
12010           dw_die_ref type_die;
12011           dw_loc_descr_ref cvt;
12012
12013           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12014                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12015           if (op0 == NULL)
12016             break;
12017           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12018               && (GET_CODE (rtl) == FLOAT
12019                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12020                      <= DWARF2_ADDR_SIZE))
12021             {
12022               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12023                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12024               if (type_die == NULL)
12025                 break;
12026               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12027               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12028               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12029               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12030               add_loc_descr (&op0, cvt);
12031             }
12032           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12033           if (type_die == NULL)
12034             break;
12035           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12036           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12037           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12038           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12039           add_loc_descr (&op0, cvt);
12040           if (GET_MODE_CLASS (mode) == MODE_INT
12041               && (GET_CODE (rtl) == FIX
12042                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12043             {
12044               op0 = convert_descriptor_to_mode (mode, op0);
12045               if (op0 == NULL)
12046                 break;
12047             }
12048           mem_loc_result = op0;
12049         }
12050       break;
12051
12052     case CLZ:
12053     case CTZ:
12054     case FFS:
12055       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12056       break;
12057
12058     case POPCOUNT:
12059     case PARITY:
12060       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12061       break;
12062
12063     case BSWAP:
12064       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12065       break;
12066
12067     case ROTATE:
12068     case ROTATERT:
12069       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12070       break;
12071
12072     case COMPARE:
12073     case TRUNCATE:
12074       /* In theory, we could implement the above.  */
12075       /* DWARF cannot represent the unsigned compare operations
12076          natively.  */
12077     case SS_MULT:
12078     case US_MULT:
12079     case SS_DIV:
12080     case US_DIV:
12081     case SS_PLUS:
12082     case US_PLUS:
12083     case SS_MINUS:
12084     case US_MINUS:
12085     case SS_NEG:
12086     case US_NEG:
12087     case SS_ABS:
12088     case SS_ASHIFT:
12089     case US_ASHIFT:
12090     case SS_TRUNCATE:
12091     case US_TRUNCATE:
12092     case UNORDERED:
12093     case ORDERED:
12094     case UNEQ:
12095     case UNGE:
12096     case UNGT:
12097     case UNLE:
12098     case UNLT:
12099     case LTGT:
12100     case FRACT_CONVERT:
12101     case UNSIGNED_FRACT_CONVERT:
12102     case SAT_FRACT:
12103     case UNSIGNED_SAT_FRACT:
12104     case SQRT:
12105     case ASM_OPERANDS:
12106     case VEC_MERGE:
12107     case VEC_SELECT:
12108     case VEC_CONCAT:
12109     case VEC_DUPLICATE:
12110     case UNSPEC:
12111     case HIGH:
12112     case FMA:
12113     case STRICT_LOW_PART:
12114     case CONST_VECTOR:
12115     case CONST_FIXED:
12116     case CLRSB:
12117       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12118          can't express it in the debug info.  This can happen e.g. with some
12119          TLS UNSPECs.  */
12120       break;
12121
12122     case CONST_STRING:
12123       resolve_one_addr (&rtl, NULL);
12124       goto symref;
12125
12126     default:
12127 #ifdef ENABLE_CHECKING
12128       print_rtl (stderr, rtl);
12129       gcc_unreachable ();
12130 #else
12131       break;
12132 #endif
12133     }
12134
12135   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12136     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12137
12138   return mem_loc_result;
12139 }
12140
12141 /* Return a descriptor that describes the concatenation of two locations.
12142    This is typically a complex variable.  */
12143
12144 static dw_loc_descr_ref
12145 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12146 {
12147   dw_loc_descr_ref cc_loc_result = NULL;
12148   dw_loc_descr_ref x0_ref
12149     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12150   dw_loc_descr_ref x1_ref
12151     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12152
12153   if (x0_ref == 0 || x1_ref == 0)
12154     return 0;
12155
12156   cc_loc_result = x0_ref;
12157   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12158
12159   add_loc_descr (&cc_loc_result, x1_ref);
12160   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12161
12162   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12163     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12164
12165   return cc_loc_result;
12166 }
12167
12168 /* Return a descriptor that describes the concatenation of N
12169    locations.  */
12170
12171 static dw_loc_descr_ref
12172 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12173 {
12174   unsigned int i;
12175   dw_loc_descr_ref cc_loc_result = NULL;
12176   unsigned int n = XVECLEN (concatn, 0);
12177
12178   for (i = 0; i < n; ++i)
12179     {
12180       dw_loc_descr_ref ref;
12181       rtx x = XVECEXP (concatn, 0, i);
12182
12183       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12184       if (ref == NULL)
12185         return NULL;
12186
12187       add_loc_descr (&cc_loc_result, ref);
12188       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12189     }
12190
12191   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12192     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12193
12194   return cc_loc_result;
12195 }
12196
12197 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12198    for DEBUG_IMPLICIT_PTR RTL.  */
12199
12200 static dw_loc_descr_ref
12201 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12202 {
12203   dw_loc_descr_ref ret;
12204   dw_die_ref ref;
12205
12206   if (dwarf_strict)
12207     return NULL;
12208   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12209               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12210               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12211   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12212   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12213   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12214   if (ref)
12215     {
12216       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12217       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12218       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12219     }
12220   else
12221     {
12222       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12223       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12224     }
12225   return ret;
12226 }
12227
12228 /* Output a proper Dwarf location descriptor for a variable or parameter
12229    which is either allocated in a register or in a memory location.  For a
12230    register, we just generate an OP_REG and the register number.  For a
12231    memory location we provide a Dwarf postfix expression describing how to
12232    generate the (dynamic) address of the object onto the address stack.
12233
12234    MODE is mode of the decl if this loc_descriptor is going to be used in
12235    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12236    allowed, VOIDmode otherwise.
12237
12238    If we don't know how to describe it, return 0.  */
12239
12240 static dw_loc_descr_ref
12241 loc_descriptor (rtx rtl, enum machine_mode mode,
12242                 enum var_init_status initialized)
12243 {
12244   dw_loc_descr_ref loc_result = NULL;
12245
12246   switch (GET_CODE (rtl))
12247     {
12248     case SUBREG:
12249       /* The case of a subreg may arise when we have a local (register)
12250          variable or a formal (register) parameter which doesn't quite fill
12251          up an entire register.  For now, just assume that it is
12252          legitimate to make the Dwarf info refer to the whole register which
12253          contains the given subreg.  */
12254       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12255         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12256       else
12257         goto do_default;
12258       break;
12259
12260     case REG:
12261       loc_result = reg_loc_descriptor (rtl, initialized);
12262       break;
12263
12264     case MEM:
12265       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12266                                        GET_MODE (rtl), initialized);
12267       if (loc_result == NULL)
12268         loc_result = tls_mem_loc_descriptor (rtl);
12269       if (loc_result == NULL)
12270         {
12271           rtx new_rtl = avoid_constant_pool_reference (rtl);
12272           if (new_rtl != rtl)
12273             loc_result = loc_descriptor (new_rtl, mode, initialized);
12274         }
12275       break;
12276
12277     case CONCAT:
12278       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12279                                           initialized);
12280       break;
12281
12282     case CONCATN:
12283       loc_result = concatn_loc_descriptor (rtl, initialized);
12284       break;
12285
12286     case VAR_LOCATION:
12287       /* Single part.  */
12288       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12289         {
12290           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12291           if (GET_CODE (loc) == EXPR_LIST)
12292             loc = XEXP (loc, 0);
12293           loc_result = loc_descriptor (loc, mode, initialized);
12294           break;
12295         }
12296
12297       rtl = XEXP (rtl, 1);
12298       /* FALLTHRU */
12299
12300     case PARALLEL:
12301       {
12302         rtvec par_elems = XVEC (rtl, 0);
12303         int num_elem = GET_NUM_ELEM (par_elems);
12304         enum machine_mode mode;
12305         int i;
12306
12307         /* Create the first one, so we have something to add to.  */
12308         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12309                                      VOIDmode, initialized);
12310         if (loc_result == NULL)
12311           return NULL;
12312         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12313         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12314         for (i = 1; i < num_elem; i++)
12315           {
12316             dw_loc_descr_ref temp;
12317
12318             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12319                                    VOIDmode, initialized);
12320             if (temp == NULL)
12321               return NULL;
12322             add_loc_descr (&loc_result, temp);
12323             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12324             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12325           }
12326       }
12327       break;
12328
12329     case CONST_INT:
12330       if (mode != VOIDmode && mode != BLKmode)
12331         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12332                                                     INTVAL (rtl));
12333       break;
12334
12335     case CONST_DOUBLE:
12336       if (mode == VOIDmode)
12337         mode = GET_MODE (rtl);
12338
12339       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12340         {
12341           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12342
12343           /* Note that a CONST_DOUBLE rtx could represent either an integer
12344              or a floating-point constant.  A CONST_DOUBLE is used whenever
12345              the constant requires more than one word in order to be
12346              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12347           loc_result = new_loc_descr (DW_OP_implicit_value,
12348                                       GET_MODE_SIZE (mode), 0);
12349           if (SCALAR_FLOAT_MODE_P (mode))
12350             {
12351               unsigned int length = GET_MODE_SIZE (mode);
12352               unsigned char *array
12353                   = (unsigned char*) ggc_alloc_atomic (length);
12354
12355               insert_float (rtl, array);
12356               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12357               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12358               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12359               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12360             }
12361           else
12362             {
12363               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12364               loc_result->dw_loc_oprnd2.v.val_double
12365                 = rtx_to_double_int (rtl);
12366             }
12367         }
12368       break;
12369
12370     case CONST_VECTOR:
12371       if (mode == VOIDmode)
12372         mode = GET_MODE (rtl);
12373
12374       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12375         {
12376           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12377           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12378           unsigned char *array = (unsigned char *)
12379             ggc_alloc_atomic (length * elt_size);
12380           unsigned int i;
12381           unsigned char *p;
12382
12383           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12384           switch (GET_MODE_CLASS (mode))
12385             {
12386             case MODE_VECTOR_INT:
12387               for (i = 0, p = array; i < length; i++, p += elt_size)
12388                 {
12389                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12390                   double_int val = rtx_to_double_int (elt);
12391
12392                   if (elt_size <= sizeof (HOST_WIDE_INT))
12393                     insert_int (double_int_to_shwi (val), elt_size, p);
12394                   else
12395                     {
12396                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12397                       insert_double (val, p);
12398                     }
12399                 }
12400               break;
12401
12402             case MODE_VECTOR_FLOAT:
12403               for (i = 0, p = array; i < length; i++, p += elt_size)
12404                 {
12405                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12406                   insert_float (elt, p);
12407                 }
12408               break;
12409
12410             default:
12411               gcc_unreachable ();
12412             }
12413
12414           loc_result = new_loc_descr (DW_OP_implicit_value,
12415                                       length * elt_size, 0);
12416           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12417           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12418           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12419           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12420         }
12421       break;
12422
12423     case CONST:
12424       if (mode == VOIDmode
12425           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12426           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12427           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12428         {
12429           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12430           break;
12431         }
12432       /* FALLTHROUGH */
12433     case SYMBOL_REF:
12434       if (!const_ok_for_output (rtl))
12435         break;
12436     case LABEL_REF:
12437       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12438           && (dwarf_version >= 4 || !dwarf_strict))
12439         {
12440           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12441           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12442           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12443           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12444           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12445         }
12446       break;
12447
12448     case DEBUG_IMPLICIT_PTR:
12449       loc_result = implicit_ptr_descriptor (rtl, 0);
12450       break;
12451
12452     case PLUS:
12453       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12454           && CONST_INT_P (XEXP (rtl, 1)))
12455         {
12456           loc_result
12457             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12458           break;
12459         }
12460       /* FALLTHRU */
12461     do_default:
12462     default:
12463       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12464            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12465            && dwarf_version >= 4)
12466           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12467         {
12468           /* Value expression.  */
12469           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12470           if (loc_result)
12471             add_loc_descr (&loc_result,
12472                            new_loc_descr (DW_OP_stack_value, 0, 0));
12473         }
12474       break;
12475     }
12476
12477   return loc_result;
12478 }
12479
12480 /* We need to figure out what section we should use as the base for the
12481    address ranges where a given location is valid.
12482    1. If this particular DECL has a section associated with it, use that.
12483    2. If this function has a section associated with it, use that.
12484    3. Otherwise, use the text section.
12485    XXX: If you split a variable across multiple sections, we won't notice.  */
12486
12487 static const char *
12488 secname_for_decl (const_tree decl)
12489 {
12490   const char *secname;
12491
12492   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12493     {
12494       tree sectree = DECL_SECTION_NAME (decl);
12495       secname = TREE_STRING_POINTER (sectree);
12496     }
12497   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12498     {
12499       tree sectree = DECL_SECTION_NAME (current_function_decl);
12500       secname = TREE_STRING_POINTER (sectree);
12501     }
12502   else if (cfun && in_cold_section_p)
12503     secname = crtl->subsections.cold_section_label;
12504   else
12505     secname = text_section_label;
12506
12507   return secname;
12508 }
12509
12510 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12511
12512 static bool
12513 decl_by_reference_p (tree decl)
12514 {
12515   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12516            || TREE_CODE (decl) == VAR_DECL)
12517           && DECL_BY_REFERENCE (decl));
12518 }
12519
12520 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12521    for VARLOC.  */
12522
12523 static dw_loc_descr_ref
12524 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12525                enum var_init_status initialized)
12526 {
12527   int have_address = 0;
12528   dw_loc_descr_ref descr;
12529   enum machine_mode mode;
12530
12531   if (want_address != 2)
12532     {
12533       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12534       /* Single part.  */
12535       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12536         {
12537           varloc = PAT_VAR_LOCATION_LOC (varloc);
12538           if (GET_CODE (varloc) == EXPR_LIST)
12539             varloc = XEXP (varloc, 0);
12540           mode = GET_MODE (varloc);
12541           if (MEM_P (varloc))
12542             {
12543               rtx addr = XEXP (varloc, 0);
12544               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12545                                           mode, initialized);
12546               if (descr)
12547                 have_address = 1;
12548               else
12549                 {
12550                   rtx x = avoid_constant_pool_reference (varloc);
12551                   if (x != varloc)
12552                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12553                                                 initialized);
12554                 }
12555             }
12556           else
12557             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12558         }
12559       else
12560         return 0;
12561     }
12562   else
12563     {
12564       if (GET_CODE (varloc) == VAR_LOCATION)
12565         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12566       else
12567         mode = DECL_MODE (loc);
12568       descr = loc_descriptor (varloc, mode, initialized);
12569       have_address = 1;
12570     }
12571
12572   if (!descr)
12573     return 0;
12574
12575   if (want_address == 2 && !have_address
12576       && (dwarf_version >= 4 || !dwarf_strict))
12577     {
12578       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12579         {
12580           expansion_failed (loc, NULL_RTX,
12581                             "DWARF address size mismatch");
12582           return 0;
12583         }
12584       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12585       have_address = 1;
12586     }
12587   /* Show if we can't fill the request for an address.  */
12588   if (want_address && !have_address)
12589     {
12590       expansion_failed (loc, NULL_RTX,
12591                         "Want address and only have value");
12592       return 0;
12593     }
12594
12595   /* If we've got an address and don't want one, dereference.  */
12596   if (!want_address && have_address)
12597     {
12598       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12599       enum dwarf_location_atom op;
12600
12601       if (size > DWARF2_ADDR_SIZE || size == -1)
12602         {
12603           expansion_failed (loc, NULL_RTX,
12604                             "DWARF address size mismatch");
12605           return 0;
12606         }
12607       else if (size == DWARF2_ADDR_SIZE)
12608         op = DW_OP_deref;
12609       else
12610         op = DW_OP_deref_size;
12611
12612       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12613     }
12614
12615   return descr;
12616 }
12617
12618 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12619    if it is not possible.  */
12620
12621 static dw_loc_descr_ref
12622 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12623 {
12624   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12625     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12626   else if (dwarf_version >= 3 || !dwarf_strict)
12627     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12628   else
12629     return NULL;
12630 }
12631
12632 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12633    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12634
12635 static dw_loc_descr_ref
12636 dw_sra_loc_expr (tree decl, rtx loc)
12637 {
12638   rtx p;
12639   unsigned int padsize = 0;
12640   dw_loc_descr_ref descr, *descr_tail;
12641   unsigned HOST_WIDE_INT decl_size;
12642   rtx varloc;
12643   enum var_init_status initialized;
12644
12645   if (DECL_SIZE (decl) == NULL
12646       || !host_integerp (DECL_SIZE (decl), 1))
12647     return NULL;
12648
12649   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12650   descr = NULL;
12651   descr_tail = &descr;
12652
12653   for (p = loc; p; p = XEXP (p, 1))
12654     {
12655       unsigned int bitsize = decl_piece_bitsize (p);
12656       rtx loc_note = *decl_piece_varloc_ptr (p);
12657       dw_loc_descr_ref cur_descr;
12658       dw_loc_descr_ref *tail, last = NULL;
12659       unsigned int opsize = 0;
12660
12661       if (loc_note == NULL_RTX
12662           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12663         {
12664           padsize += bitsize;
12665           continue;
12666         }
12667       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12668       varloc = NOTE_VAR_LOCATION (loc_note);
12669       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12670       if (cur_descr == NULL)
12671         {
12672           padsize += bitsize;
12673           continue;
12674         }
12675
12676       /* Check that cur_descr either doesn't use
12677          DW_OP_*piece operations, or their sum is equal
12678          to bitsize.  Otherwise we can't embed it.  */
12679       for (tail = &cur_descr; *tail != NULL;
12680            tail = &(*tail)->dw_loc_next)
12681         if ((*tail)->dw_loc_opc == DW_OP_piece)
12682           {
12683             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12684                       * BITS_PER_UNIT;
12685             last = *tail;
12686           }
12687         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12688           {
12689             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12690             last = *tail;
12691           }
12692
12693       if (last != NULL && opsize != bitsize)
12694         {
12695           padsize += bitsize;
12696           continue;
12697         }
12698
12699       /* If there is a hole, add DW_OP_*piece after empty DWARF
12700          expression, which means that those bits are optimized out.  */
12701       if (padsize)
12702         {
12703           if (padsize > decl_size)
12704             return NULL;
12705           decl_size -= padsize;
12706           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12707           if (*descr_tail == NULL)
12708             return NULL;
12709           descr_tail = &(*descr_tail)->dw_loc_next;
12710           padsize = 0;
12711         }
12712       *descr_tail = cur_descr;
12713       descr_tail = tail;
12714       if (bitsize > decl_size)
12715         return NULL;
12716       decl_size -= bitsize;
12717       if (last == NULL)
12718         {
12719           HOST_WIDE_INT offset = 0;
12720           if (GET_CODE (varloc) == VAR_LOCATION
12721               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12722             {
12723               varloc = PAT_VAR_LOCATION_LOC (varloc);
12724               if (GET_CODE (varloc) == EXPR_LIST)
12725                 varloc = XEXP (varloc, 0);
12726             }
12727           do 
12728             {
12729               if (GET_CODE (varloc) == CONST
12730                   || GET_CODE (varloc) == SIGN_EXTEND
12731                   || GET_CODE (varloc) == ZERO_EXTEND)
12732                 varloc = XEXP (varloc, 0);
12733               else if (GET_CODE (varloc) == SUBREG)
12734                 varloc = SUBREG_REG (varloc);
12735               else
12736                 break;
12737             }
12738           while (1);
12739           /* DW_OP_bit_size offset should be zero for register
12740              or implicit location descriptions and empty location
12741              descriptions, but for memory addresses needs big endian
12742              adjustment.  */
12743           if (MEM_P (varloc))
12744             {
12745               unsigned HOST_WIDE_INT memsize
12746                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
12747               if (memsize != bitsize)
12748                 {
12749                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12750                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12751                     return NULL;
12752                   if (memsize < bitsize)
12753                     return NULL;
12754                   if (BITS_BIG_ENDIAN)
12755                     offset = memsize - bitsize;
12756                 }
12757             }
12758
12759           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12760           if (*descr_tail == NULL)
12761             return NULL;
12762           descr_tail = &(*descr_tail)->dw_loc_next;
12763         }
12764     }
12765
12766   /* If there were any non-empty expressions, add padding till the end of
12767      the decl.  */
12768   if (descr != NULL && decl_size != 0)
12769     {
12770       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12771       if (*descr_tail == NULL)
12772         return NULL;
12773     }
12774   return descr;
12775 }
12776
12777 /* Return the dwarf representation of the location list LOC_LIST of
12778    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12779    function.  */
12780
12781 static dw_loc_list_ref
12782 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12783 {
12784   const char *endname, *secname;
12785   rtx varloc;
12786   enum var_init_status initialized;
12787   struct var_loc_node *node;
12788   dw_loc_descr_ref descr;
12789   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12790   dw_loc_list_ref list = NULL;
12791   dw_loc_list_ref *listp = &list;
12792
12793   /* Now that we know what section we are using for a base,
12794      actually construct the list of locations.
12795      The first location information is what is passed to the
12796      function that creates the location list, and the remaining
12797      locations just get added on to that list.
12798      Note that we only know the start address for a location
12799      (IE location changes), so to build the range, we use
12800      the range [current location start, next location start].
12801      This means we have to special case the last node, and generate
12802      a range of [last location start, end of function label].  */
12803
12804   secname = secname_for_decl (decl);
12805
12806   for (node = loc_list->first; node; node = node->next)
12807     if (GET_CODE (node->loc) == EXPR_LIST
12808         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12809       {
12810         if (GET_CODE (node->loc) == EXPR_LIST)
12811           {
12812             /* This requires DW_OP_{,bit_}piece, which is not usable
12813                inside DWARF expressions.  */
12814             if (want_address != 2)
12815               continue;
12816             descr = dw_sra_loc_expr (decl, node->loc);
12817             if (descr == NULL)
12818               continue;
12819           }
12820         else
12821           {
12822             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12823             varloc = NOTE_VAR_LOCATION (node->loc);
12824             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12825           }
12826         if (descr)
12827           {
12828             bool range_across_switch = false;
12829             /* If section switch happens in between node->label
12830                and node->next->label (or end of function) and
12831                we can't emit it as a single entry list,
12832                emit two ranges, first one ending at the end
12833                of first partition and second one starting at the
12834                beginning of second partition.  */
12835             if (node == loc_list->last_before_switch
12836                 && (node != loc_list->first || loc_list->first->next)
12837                 && current_function_decl)
12838               {
12839                 endname = cfun->fde->dw_fde_end;
12840                 range_across_switch = true;
12841               }
12842             /* The variable has a location between NODE->LABEL and
12843                NODE->NEXT->LABEL.  */
12844             else if (node->next)
12845               endname = node->next->label;
12846             /* If the variable has a location at the last label
12847                it keeps its location until the end of function.  */
12848             else if (!current_function_decl)
12849               endname = text_end_label;
12850             else
12851               {
12852                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12853                                              current_function_funcdef_no);
12854                 endname = ggc_strdup (label_id);
12855               }
12856
12857             *listp = new_loc_list (descr, node->label, endname, secname);
12858             if (TREE_CODE (decl) == PARM_DECL
12859                 && node == loc_list->first
12860                 && GET_CODE (node->loc) == NOTE
12861                 && strcmp (node->label, endname) == 0)
12862               (*listp)->force = true;
12863             listp = &(*listp)->dw_loc_next;
12864
12865             if (range_across_switch)
12866               {
12867                 if (GET_CODE (node->loc) == EXPR_LIST)
12868                   descr = dw_sra_loc_expr (decl, node->loc);
12869                 else
12870                   {
12871                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12872                     varloc = NOTE_VAR_LOCATION (node->loc);
12873                     descr = dw_loc_list_1 (decl, varloc, want_address,
12874                                            initialized);
12875                   }
12876                 gcc_assert (descr);
12877                 /* The variable has a location between NODE->LABEL and
12878                    NODE->NEXT->LABEL.  */
12879                 if (node->next)
12880                   endname = node->next->label;
12881                 else
12882                   endname = cfun->fde->dw_fde_second_end;
12883                 *listp = new_loc_list (descr,
12884                                        cfun->fde->dw_fde_second_begin,
12885                                        endname, secname);
12886                 listp = &(*listp)->dw_loc_next;
12887               }
12888           }
12889       }
12890
12891   /* Try to avoid the overhead of a location list emitting a location
12892      expression instead, but only if we didn't have more than one
12893      location entry in the first place.  If some entries were not
12894      representable, we don't want to pretend a single entry that was
12895      applies to the entire scope in which the variable is
12896      available.  */
12897   if (list && loc_list->first->next)
12898     gen_llsym (list);
12899
12900   return list;
12901 }
12902
12903 /* Return if the loc_list has only single element and thus can be represented
12904    as location description.   */
12905
12906 static bool
12907 single_element_loc_list_p (dw_loc_list_ref list)
12908 {
12909   gcc_assert (!list->dw_loc_next || list->ll_symbol);
12910   return !list->ll_symbol;
12911 }
12912
12913 /* To each location in list LIST add loc descr REF.  */
12914
12915 static void
12916 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12917 {
12918   dw_loc_descr_ref copy;
12919   add_loc_descr (&list->expr, ref);
12920   list = list->dw_loc_next;
12921   while (list)
12922     {
12923       copy = ggc_alloc_dw_loc_descr_node ();
12924       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12925       add_loc_descr (&list->expr, copy);
12926       while (copy->dw_loc_next)
12927         {
12928           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12929           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12930           copy->dw_loc_next = new_copy;
12931           copy = new_copy;
12932         }
12933       list = list->dw_loc_next;
12934     }
12935 }
12936
12937 /* Given two lists RET and LIST
12938    produce location list that is result of adding expression in LIST
12939    to expression in RET on each possition in program.
12940    Might be destructive on both RET and LIST.
12941
12942    TODO: We handle only simple cases of RET or LIST having at most one
12943    element. General case would inolve sorting the lists in program order
12944    and merging them that will need some additional work.
12945    Adding that will improve quality of debug info especially for SRA-ed
12946    structures.  */
12947
12948 static void
12949 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12950 {
12951   if (!list)
12952     return;
12953   if (!*ret)
12954     {
12955       *ret = list;
12956       return;
12957     }
12958   if (!list->dw_loc_next)
12959     {
12960       add_loc_descr_to_each (*ret, list->expr);
12961       return;
12962     }
12963   if (!(*ret)->dw_loc_next)
12964     {
12965       add_loc_descr_to_each (list, (*ret)->expr);
12966       *ret = list;
12967       return;
12968     }
12969   expansion_failed (NULL_TREE, NULL_RTX,
12970                     "Don't know how to merge two non-trivial"
12971                     " location lists.\n");
12972   *ret = NULL;
12973   return;
12974 }
12975
12976 /* LOC is constant expression.  Try a luck, look it up in constant
12977    pool and return its loc_descr of its address.  */
12978
12979 static dw_loc_descr_ref
12980 cst_pool_loc_descr (tree loc)
12981 {
12982   /* Get an RTL for this, if something has been emitted.  */
12983   rtx rtl = lookup_constant_def (loc);
12984
12985   if (!rtl || !MEM_P (rtl))
12986     {
12987       gcc_assert (!rtl);
12988       return 0;
12989     }
12990   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12991
12992   /* TODO: We might get more coverage if we was actually delaying expansion
12993      of all expressions till end of compilation when constant pools are fully
12994      populated.  */
12995   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12996     {
12997       expansion_failed (loc, NULL_RTX,
12998                         "CST value in contant pool but not marked.");
12999       return 0;
13000     }
13001   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13002                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13003 }
13004
13005 /* Return dw_loc_list representing address of addr_expr LOC
13006    by looking for innder INDIRECT_REF expression and turing it
13007    into simple arithmetics.  */
13008
13009 static dw_loc_list_ref
13010 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13011 {
13012   tree obj, offset;
13013   HOST_WIDE_INT bitsize, bitpos, bytepos;
13014   enum machine_mode mode;
13015   int volatilep;
13016   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13017   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13018
13019   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13020                              &bitsize, &bitpos, &offset, &mode,
13021                              &unsignedp, &volatilep, false);
13022   STRIP_NOPS (obj);
13023   if (bitpos % BITS_PER_UNIT)
13024     {
13025       expansion_failed (loc, NULL_RTX, "bitfield access");
13026       return 0;
13027     }
13028   if (!INDIRECT_REF_P (obj))
13029     {
13030       expansion_failed (obj,
13031                         NULL_RTX, "no indirect ref in inner refrence");
13032       return 0;
13033     }
13034   if (!offset && !bitpos)
13035     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13036   else if (toplev
13037            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13038            && (dwarf_version >= 4 || !dwarf_strict))
13039     {
13040       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13041       if (!list_ret)
13042         return 0;
13043       if (offset)
13044         {
13045           /* Variable offset.  */
13046           list_ret1 = loc_list_from_tree (offset, 0);
13047           if (list_ret1 == 0)
13048             return 0;
13049           add_loc_list (&list_ret, list_ret1);
13050           if (!list_ret)
13051             return 0;
13052           add_loc_descr_to_each (list_ret,
13053                                  new_loc_descr (DW_OP_plus, 0, 0));
13054         }
13055       bytepos = bitpos / BITS_PER_UNIT;
13056       if (bytepos > 0)
13057         add_loc_descr_to_each (list_ret,
13058                                new_loc_descr (DW_OP_plus_uconst,
13059                                               bytepos, 0));
13060       else if (bytepos < 0)
13061         loc_list_plus_const (list_ret, bytepos);
13062       add_loc_descr_to_each (list_ret,
13063                              new_loc_descr (DW_OP_stack_value, 0, 0));
13064     }
13065   return list_ret;
13066 }
13067
13068
13069 /* Generate Dwarf location list representing LOC.
13070    If WANT_ADDRESS is false, expression computing LOC will be computed
13071    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13072    if WANT_ADDRESS is 2, expression computing address useable in location
13073      will be returned (i.e. DW_OP_reg can be used
13074      to refer to register values).  */
13075
13076 static dw_loc_list_ref
13077 loc_list_from_tree (tree loc, int want_address)
13078 {
13079   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13080   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13081   int have_address = 0;
13082   enum dwarf_location_atom op;
13083
13084   /* ??? Most of the time we do not take proper care for sign/zero
13085      extending the values properly.  Hopefully this won't be a real
13086      problem...  */
13087
13088   switch (TREE_CODE (loc))
13089     {
13090     case ERROR_MARK:
13091       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13092       return 0;
13093
13094     case PLACEHOLDER_EXPR:
13095       /* This case involves extracting fields from an object to determine the
13096          position of other fields.  We don't try to encode this here.  The
13097          only user of this is Ada, which encodes the needed information using
13098          the names of types.  */
13099       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13100       return 0;
13101
13102     case CALL_EXPR:
13103       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13104       /* There are no opcodes for these operations.  */
13105       return 0;
13106
13107     case PREINCREMENT_EXPR:
13108     case PREDECREMENT_EXPR:
13109     case POSTINCREMENT_EXPR:
13110     case POSTDECREMENT_EXPR:
13111       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13112       /* There are no opcodes for these operations.  */
13113       return 0;
13114
13115     case ADDR_EXPR:
13116       /* If we already want an address, see if there is INDIRECT_REF inside
13117          e.g. for &this->field.  */
13118       if (want_address)
13119         {
13120           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13121                        (loc, want_address == 2);
13122           if (list_ret)
13123             have_address = 1;
13124           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13125                    && (ret = cst_pool_loc_descr (loc)))
13126             have_address = 1;
13127         }
13128         /* Otherwise, process the argument and look for the address.  */
13129       if (!list_ret && !ret)
13130         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13131       else
13132         {
13133           if (want_address)
13134             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13135           return NULL;
13136         }
13137       break;
13138
13139     case VAR_DECL:
13140       if (DECL_THREAD_LOCAL_P (loc))
13141         {
13142           rtx rtl;
13143           enum dwarf_location_atom first_op;
13144           enum dwarf_location_atom second_op;
13145           bool dtprel = false;
13146
13147           if (targetm.have_tls)
13148             {
13149               /* If this is not defined, we have no way to emit the
13150                  data.  */
13151               if (!targetm.asm_out.output_dwarf_dtprel)
13152                 return 0;
13153
13154                /* The way DW_OP_GNU_push_tls_address is specified, we
13155                   can only look up addresses of objects in the current
13156                   module.  We used DW_OP_addr as first op, but that's
13157                   wrong, because DW_OP_addr is relocated by the debug
13158                   info consumer, while DW_OP_GNU_push_tls_address
13159                   operand shouldn't be.  */
13160               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13161                 return 0;
13162               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13163               dtprel = true;
13164               second_op = DW_OP_GNU_push_tls_address;
13165             }
13166           else
13167             {
13168               if (!targetm.emutls.debug_form_tls_address
13169                   || !(dwarf_version >= 3 || !dwarf_strict))
13170                 return 0;
13171               /* We stuffed the control variable into the DECL_VALUE_EXPR
13172                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13173                  no longer appear in gimple code.  We used the control
13174                  variable in specific so that we could pick it up here.  */
13175               loc = DECL_VALUE_EXPR (loc);
13176               first_op = DW_OP_addr;
13177               second_op = DW_OP_form_tls_address;
13178             }
13179
13180           rtl = rtl_for_decl_location (loc);
13181           if (rtl == NULL_RTX)
13182             return 0;
13183
13184           if (!MEM_P (rtl))
13185             return 0;
13186           rtl = XEXP (rtl, 0);
13187           if (! CONSTANT_P (rtl))
13188             return 0;
13189
13190           ret = new_loc_descr (first_op, 0, 0);
13191           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13192           ret->dw_loc_oprnd1.v.val_addr = rtl;
13193           ret->dtprel = dtprel;
13194
13195           ret1 = new_loc_descr (second_op, 0, 0);
13196           add_loc_descr (&ret, ret1);
13197
13198           have_address = 1;
13199           break;
13200         }
13201       /* FALLTHRU */
13202
13203     case PARM_DECL:
13204     case RESULT_DECL:
13205       if (DECL_HAS_VALUE_EXPR_P (loc))
13206         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13207                                    want_address);
13208       /* FALLTHRU */
13209
13210     case FUNCTION_DECL:
13211       {
13212         rtx rtl;
13213         var_loc_list *loc_list = lookup_decl_loc (loc);
13214
13215         if (loc_list && loc_list->first)
13216           {
13217             list_ret = dw_loc_list (loc_list, loc, want_address);
13218             have_address = want_address != 0;
13219             break;
13220           }
13221         rtl = rtl_for_decl_location (loc);
13222         if (rtl == NULL_RTX)
13223           {
13224             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13225             return 0;
13226           }
13227         else if (CONST_INT_P (rtl))
13228           {
13229             HOST_WIDE_INT val = INTVAL (rtl);
13230             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13231               val &= GET_MODE_MASK (DECL_MODE (loc));
13232             ret = int_loc_descriptor (val);
13233           }
13234         else if (GET_CODE (rtl) == CONST_STRING)
13235           {
13236             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13237             return 0;
13238           }
13239         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13240           {
13241             ret = new_loc_descr (DW_OP_addr, 0, 0);
13242             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13243             ret->dw_loc_oprnd1.v.val_addr = rtl;
13244           }
13245         else
13246           {
13247             enum machine_mode mode, mem_mode;
13248
13249             /* Certain constructs can only be represented at top-level.  */
13250             if (want_address == 2)
13251               {
13252                 ret = loc_descriptor (rtl, VOIDmode,
13253                                       VAR_INIT_STATUS_INITIALIZED);
13254                 have_address = 1;
13255               }
13256             else
13257               {
13258                 mode = GET_MODE (rtl);
13259                 mem_mode = VOIDmode;
13260                 if (MEM_P (rtl))
13261                   {
13262                     mem_mode = mode;
13263                     mode = get_address_mode (rtl);
13264                     rtl = XEXP (rtl, 0);
13265                     have_address = 1;
13266                   }
13267                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13268                                           VAR_INIT_STATUS_INITIALIZED);
13269               }
13270             if (!ret)
13271               expansion_failed (loc, rtl,
13272                                 "failed to produce loc descriptor for rtl");
13273           }
13274       }
13275       break;
13276
13277     case MEM_REF:
13278       /* ??? FIXME.  */
13279       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13280         return 0;
13281       /* Fallthru.  */
13282     case INDIRECT_REF:
13283       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13284       have_address = 1;
13285       break;
13286
13287     case COMPOUND_EXPR:
13288       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13289
13290     CASE_CONVERT:
13291     case VIEW_CONVERT_EXPR:
13292     case SAVE_EXPR:
13293     case MODIFY_EXPR:
13294       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13295
13296     case COMPONENT_REF:
13297     case BIT_FIELD_REF:
13298     case ARRAY_REF:
13299     case ARRAY_RANGE_REF:
13300     case REALPART_EXPR:
13301     case IMAGPART_EXPR:
13302       {
13303         tree obj, offset;
13304         HOST_WIDE_INT bitsize, bitpos, bytepos;
13305         enum machine_mode mode;
13306         int volatilep;
13307         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13308
13309         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13310                                    &unsignedp, &volatilep, false);
13311
13312         gcc_assert (obj != loc);
13313
13314         list_ret = loc_list_from_tree (obj,
13315                                        want_address == 2
13316                                        && !bitpos && !offset ? 2 : 1);
13317         /* TODO: We can extract value of the small expression via shifting even
13318            for nonzero bitpos.  */
13319         if (list_ret == 0)
13320           return 0;
13321         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13322           {
13323             expansion_failed (loc, NULL_RTX,
13324                               "bitfield access");
13325             return 0;
13326           }
13327
13328         if (offset != NULL_TREE)
13329           {
13330             /* Variable offset.  */
13331             list_ret1 = loc_list_from_tree (offset, 0);
13332             if (list_ret1 == 0)
13333               return 0;
13334             add_loc_list (&list_ret, list_ret1);
13335             if (!list_ret)
13336               return 0;
13337             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13338           }
13339
13340         bytepos = bitpos / BITS_PER_UNIT;
13341         if (bytepos > 0)
13342           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13343         else if (bytepos < 0)
13344           loc_list_plus_const (list_ret, bytepos);
13345
13346         have_address = 1;
13347         break;
13348       }
13349
13350     case INTEGER_CST:
13351       if ((want_address || !host_integerp (loc, 0))
13352           && (ret = cst_pool_loc_descr (loc)))
13353         have_address = 1;
13354       else if (want_address == 2
13355                && host_integerp (loc, 0)
13356                && (ret = address_of_int_loc_descriptor
13357                            (int_size_in_bytes (TREE_TYPE (loc)),
13358                             tree_low_cst (loc, 0))))
13359         have_address = 1;
13360       else if (host_integerp (loc, 0))
13361         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13362       else
13363         {
13364           expansion_failed (loc, NULL_RTX,
13365                             "Integer operand is not host integer");
13366           return 0;
13367         }
13368       break;
13369
13370     case CONSTRUCTOR:
13371     case REAL_CST:
13372     case STRING_CST:
13373     case COMPLEX_CST:
13374       if ((ret = cst_pool_loc_descr (loc)))
13375         have_address = 1;
13376       else
13377       /* We can construct small constants here using int_loc_descriptor.  */
13378         expansion_failed (loc, NULL_RTX,
13379                           "constructor or constant not in constant pool");
13380       break;
13381
13382     case TRUTH_AND_EXPR:
13383     case TRUTH_ANDIF_EXPR:
13384     case BIT_AND_EXPR:
13385       op = DW_OP_and;
13386       goto do_binop;
13387
13388     case TRUTH_XOR_EXPR:
13389     case BIT_XOR_EXPR:
13390       op = DW_OP_xor;
13391       goto do_binop;
13392
13393     case TRUTH_OR_EXPR:
13394     case TRUTH_ORIF_EXPR:
13395     case BIT_IOR_EXPR:
13396       op = DW_OP_or;
13397       goto do_binop;
13398
13399     case FLOOR_DIV_EXPR:
13400     case CEIL_DIV_EXPR:
13401     case ROUND_DIV_EXPR:
13402     case TRUNC_DIV_EXPR:
13403       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13404         return 0;
13405       op = DW_OP_div;
13406       goto do_binop;
13407
13408     case MINUS_EXPR:
13409       op = DW_OP_minus;
13410       goto do_binop;
13411
13412     case FLOOR_MOD_EXPR:
13413     case CEIL_MOD_EXPR:
13414     case ROUND_MOD_EXPR:
13415     case TRUNC_MOD_EXPR:
13416       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13417         {
13418           op = DW_OP_mod;
13419           goto do_binop;
13420         }
13421       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13422       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13423       if (list_ret == 0 || list_ret1 == 0)
13424         return 0;
13425
13426       add_loc_list (&list_ret, list_ret1);
13427       if (list_ret == 0)
13428         return 0;
13429       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13430       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13431       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13432       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13433       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13434       break;
13435
13436     case MULT_EXPR:
13437       op = DW_OP_mul;
13438       goto do_binop;
13439
13440     case LSHIFT_EXPR:
13441       op = DW_OP_shl;
13442       goto do_binop;
13443
13444     case RSHIFT_EXPR:
13445       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13446       goto do_binop;
13447
13448     case POINTER_PLUS_EXPR:
13449     case PLUS_EXPR:
13450       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13451         {
13452           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13453           if (list_ret == 0)
13454             return 0;
13455
13456           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13457           break;
13458         }
13459
13460       op = DW_OP_plus;
13461       goto do_binop;
13462
13463     case LE_EXPR:
13464       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13465         return 0;
13466
13467       op = DW_OP_le;
13468       goto do_binop;
13469
13470     case GE_EXPR:
13471       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13472         return 0;
13473
13474       op = DW_OP_ge;
13475       goto do_binop;
13476
13477     case LT_EXPR:
13478       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13479         return 0;
13480
13481       op = DW_OP_lt;
13482       goto do_binop;
13483
13484     case GT_EXPR:
13485       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13486         return 0;
13487
13488       op = DW_OP_gt;
13489       goto do_binop;
13490
13491     case EQ_EXPR:
13492       op = DW_OP_eq;
13493       goto do_binop;
13494
13495     case NE_EXPR:
13496       op = DW_OP_ne;
13497       goto do_binop;
13498
13499     do_binop:
13500       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13501       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13502       if (list_ret == 0 || list_ret1 == 0)
13503         return 0;
13504
13505       add_loc_list (&list_ret, list_ret1);
13506       if (list_ret == 0)
13507         return 0;
13508       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13509       break;
13510
13511     case TRUTH_NOT_EXPR:
13512     case BIT_NOT_EXPR:
13513       op = DW_OP_not;
13514       goto do_unop;
13515
13516     case ABS_EXPR:
13517       op = DW_OP_abs;
13518       goto do_unop;
13519
13520     case NEGATE_EXPR:
13521       op = DW_OP_neg;
13522       goto do_unop;
13523
13524     do_unop:
13525       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13526       if (list_ret == 0)
13527         return 0;
13528
13529       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13530       break;
13531
13532     case MIN_EXPR:
13533     case MAX_EXPR:
13534       {
13535         const enum tree_code code =
13536           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13537
13538         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13539                       build2 (code, integer_type_node,
13540                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13541                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13542       }
13543
13544       /* ... fall through ...  */
13545
13546     case COND_EXPR:
13547       {
13548         dw_loc_descr_ref lhs
13549           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13550         dw_loc_list_ref rhs
13551           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13552         dw_loc_descr_ref bra_node, jump_node, tmp;
13553
13554         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13555         if (list_ret == 0 || lhs == 0 || rhs == 0)
13556           return 0;
13557
13558         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13559         add_loc_descr_to_each (list_ret, bra_node);
13560
13561         add_loc_list (&list_ret, rhs);
13562         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13563         add_loc_descr_to_each (list_ret, jump_node);
13564
13565         add_loc_descr_to_each (list_ret, lhs);
13566         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13567         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13568
13569         /* ??? Need a node to point the skip at.  Use a nop.  */
13570         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13571         add_loc_descr_to_each (list_ret, tmp);
13572         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13573         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13574       }
13575       break;
13576
13577     case FIX_TRUNC_EXPR:
13578       return 0;
13579
13580     default:
13581       /* Leave front-end specific codes as simply unknown.  This comes
13582          up, for instance, with the C STMT_EXPR.  */
13583       if ((unsigned int) TREE_CODE (loc)
13584           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13585         {
13586           expansion_failed (loc, NULL_RTX,
13587                             "language specific tree node");
13588           return 0;
13589         }
13590
13591 #ifdef ENABLE_CHECKING
13592       /* Otherwise this is a generic code; we should just lists all of
13593          these explicitly.  We forgot one.  */
13594       gcc_unreachable ();
13595 #else
13596       /* In a release build, we want to degrade gracefully: better to
13597          generate incomplete debugging information than to crash.  */
13598       return NULL;
13599 #endif
13600     }
13601
13602   if (!ret && !list_ret)
13603     return 0;
13604
13605   if (want_address == 2 && !have_address
13606       && (dwarf_version >= 4 || !dwarf_strict))
13607     {
13608       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13609         {
13610           expansion_failed (loc, NULL_RTX,
13611                             "DWARF address size mismatch");
13612           return 0;
13613         }
13614       if (ret)
13615         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13616       else
13617         add_loc_descr_to_each (list_ret,
13618                                new_loc_descr (DW_OP_stack_value, 0, 0));
13619       have_address = 1;
13620     }
13621   /* Show if we can't fill the request for an address.  */
13622   if (want_address && !have_address)
13623     {
13624       expansion_failed (loc, NULL_RTX,
13625                         "Want address and only have value");
13626       return 0;
13627     }
13628
13629   gcc_assert (!ret || !list_ret);
13630
13631   /* If we've got an address and don't want one, dereference.  */
13632   if (!want_address && have_address)
13633     {
13634       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13635
13636       if (size > DWARF2_ADDR_SIZE || size == -1)
13637         {
13638           expansion_failed (loc, NULL_RTX,
13639                             "DWARF address size mismatch");
13640           return 0;
13641         }
13642       else if (size == DWARF2_ADDR_SIZE)
13643         op = DW_OP_deref;
13644       else
13645         op = DW_OP_deref_size;
13646
13647       if (ret)
13648         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13649       else
13650         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13651     }
13652   if (ret)
13653     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13654
13655   return list_ret;
13656 }
13657
13658 /* Same as above but return only single location expression.  */
13659 static dw_loc_descr_ref
13660 loc_descriptor_from_tree (tree loc, int want_address)
13661 {
13662   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13663   if (!ret)
13664     return NULL;
13665   if (ret->dw_loc_next)
13666     {
13667       expansion_failed (loc, NULL_RTX,
13668                         "Location list where only loc descriptor needed");
13669       return NULL;
13670     }
13671   return ret->expr;
13672 }
13673
13674 /* Given a value, round it up to the lowest multiple of `boundary'
13675    which is not less than the value itself.  */
13676
13677 static inline HOST_WIDE_INT
13678 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13679 {
13680   return (((value + boundary - 1) / boundary) * boundary);
13681 }
13682
13683 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13684    pointer to the declared type for the relevant field variable, or return
13685    `integer_type_node' if the given node turns out to be an
13686    ERROR_MARK node.  */
13687
13688 static inline tree
13689 field_type (const_tree decl)
13690 {
13691   tree type;
13692
13693   if (TREE_CODE (decl) == ERROR_MARK)
13694     return integer_type_node;
13695
13696   type = DECL_BIT_FIELD_TYPE (decl);
13697   if (type == NULL_TREE)
13698     type = TREE_TYPE (decl);
13699
13700   return type;
13701 }
13702
13703 /* Given a pointer to a tree node, return the alignment in bits for
13704    it, or else return BITS_PER_WORD if the node actually turns out to
13705    be an ERROR_MARK node.  */
13706
13707 static inline unsigned
13708 simple_type_align_in_bits (const_tree type)
13709 {
13710   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13711 }
13712
13713 static inline unsigned
13714 simple_decl_align_in_bits (const_tree decl)
13715 {
13716   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13717 }
13718
13719 /* Return the result of rounding T up to ALIGN.  */
13720
13721 static inline double_int
13722 round_up_to_align (double_int t, unsigned int align)
13723 {
13724   double_int alignd = uhwi_to_double_int (align);
13725   t = double_int_add (t, alignd);
13726   t = double_int_add (t, double_int_minus_one);
13727   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13728   t = double_int_mul (t, alignd);
13729   return t;
13730 }
13731
13732 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13733    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13734    or return 0 if we are unable to determine what that offset is, either
13735    because the argument turns out to be a pointer to an ERROR_MARK node, or
13736    because the offset is actually variable.  (We can't handle the latter case
13737    just yet).  */
13738
13739 static HOST_WIDE_INT
13740 field_byte_offset (const_tree decl)
13741 {
13742   double_int object_offset_in_bits;
13743   double_int object_offset_in_bytes;
13744   double_int bitpos_int;
13745
13746   if (TREE_CODE (decl) == ERROR_MARK)
13747     return 0;
13748
13749   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13750
13751   /* We cannot yet cope with fields whose positions are variable, so
13752      for now, when we see such things, we simply return 0.  Someday, we may
13753      be able to handle such cases, but it will be damn difficult.  */
13754   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13755     return 0;
13756
13757   bitpos_int = tree_to_double_int (bit_position (decl));
13758
13759 #ifdef PCC_BITFIELD_TYPE_MATTERS
13760   if (PCC_BITFIELD_TYPE_MATTERS)
13761     {
13762       tree type;
13763       tree field_size_tree;
13764       double_int deepest_bitpos;
13765       double_int field_size_in_bits;
13766       unsigned int type_align_in_bits;
13767       unsigned int decl_align_in_bits;
13768       double_int type_size_in_bits;
13769
13770       type = field_type (decl);
13771       type_size_in_bits = double_int_type_size_in_bits (type);
13772       type_align_in_bits = simple_type_align_in_bits (type);
13773
13774       field_size_tree = DECL_SIZE (decl);
13775
13776       /* The size could be unspecified if there was an error, or for
13777          a flexible array member.  */
13778       if (!field_size_tree)
13779         field_size_tree = bitsize_zero_node;
13780
13781       /* If the size of the field is not constant, use the type size.  */
13782       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13783         field_size_in_bits = tree_to_double_int (field_size_tree);
13784       else
13785         field_size_in_bits = type_size_in_bits;
13786
13787       decl_align_in_bits = simple_decl_align_in_bits (decl);
13788
13789       /* The GCC front-end doesn't make any attempt to keep track of the
13790          starting bit offset (relative to the start of the containing
13791          structure type) of the hypothetical "containing object" for a
13792          bit-field.  Thus, when computing the byte offset value for the
13793          start of the "containing object" of a bit-field, we must deduce
13794          this information on our own. This can be rather tricky to do in
13795          some cases.  For example, handling the following structure type
13796          definition when compiling for an i386/i486 target (which only
13797          aligns long long's to 32-bit boundaries) can be very tricky:
13798
13799          struct S { int field1; long long field2:31; };
13800
13801          Fortunately, there is a simple rule-of-thumb which can be used
13802          in such cases.  When compiling for an i386/i486, GCC will
13803          allocate 8 bytes for the structure shown above.  It decides to
13804          do this based upon one simple rule for bit-field allocation.
13805          GCC allocates each "containing object" for each bit-field at
13806          the first (i.e. lowest addressed) legitimate alignment boundary
13807          (based upon the required minimum alignment for the declared
13808          type of the field) which it can possibly use, subject to the
13809          condition that there is still enough available space remaining
13810          in the containing object (when allocated at the selected point)
13811          to fully accommodate all of the bits of the bit-field itself.
13812
13813          This simple rule makes it obvious why GCC allocates 8 bytes for
13814          each object of the structure type shown above.  When looking
13815          for a place to allocate the "containing object" for `field2',
13816          the compiler simply tries to allocate a 64-bit "containing
13817          object" at each successive 32-bit boundary (starting at zero)
13818          until it finds a place to allocate that 64- bit field such that
13819          at least 31 contiguous (and previously unallocated) bits remain
13820          within that selected 64 bit field.  (As it turns out, for the
13821          example above, the compiler finds it is OK to allocate the
13822          "containing object" 64-bit field at bit-offset zero within the
13823          structure type.)
13824
13825          Here we attempt to work backwards from the limited set of facts
13826          we're given, and we try to deduce from those facts, where GCC
13827          must have believed that the containing object started (within
13828          the structure type). The value we deduce is then used (by the
13829          callers of this routine) to generate DW_AT_location and
13830          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13831          the case of DW_AT_location, regular fields as well).  */
13832
13833       /* Figure out the bit-distance from the start of the structure to
13834          the "deepest" bit of the bit-field.  */
13835       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
13836
13837       /* This is the tricky part.  Use some fancy footwork to deduce
13838          where the lowest addressed bit of the containing object must
13839          be.  */
13840       object_offset_in_bits
13841         = double_int_sub (deepest_bitpos, type_size_in_bits);
13842
13843       /* Round up to type_align by default.  This works best for
13844          bitfields.  */
13845       object_offset_in_bits
13846         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13847
13848       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13849         {
13850           object_offset_in_bits
13851             = double_int_sub (deepest_bitpos, type_size_in_bits);
13852
13853           /* Round up to decl_align instead.  */
13854           object_offset_in_bits
13855             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13856         }
13857     }
13858   else
13859 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13860     object_offset_in_bits = bitpos_int;
13861
13862   object_offset_in_bytes
13863     = double_int_div (object_offset_in_bits,
13864                       uhwi_to_double_int (BITS_PER_UNIT), true,
13865                       TRUNC_DIV_EXPR);
13866   return double_int_to_shwi (object_offset_in_bytes);
13867 }
13868 \f
13869 /* The following routines define various Dwarf attributes and any data
13870    associated with them.  */
13871
13872 /* Add a location description attribute value to a DIE.
13873
13874    This emits location attributes suitable for whole variables and
13875    whole parameters.  Note that the location attributes for struct fields are
13876    generated by the routine `data_member_location_attribute' below.  */
13877
13878 static inline void
13879 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13880                              dw_loc_list_ref descr)
13881 {
13882   if (descr == 0)
13883     return;
13884   if (single_element_loc_list_p (descr))
13885     add_AT_loc (die, attr_kind, descr->expr);
13886   else
13887     add_AT_loc_list (die, attr_kind, descr);
13888 }
13889
13890 /* Add DW_AT_accessibility attribute to DIE if needed.  */
13891
13892 static void
13893 add_accessibility_attribute (dw_die_ref die, tree decl)
13894 {
13895   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13896      children, otherwise the default is DW_ACCESS_public.  In DWARF2
13897      the default has always been DW_ACCESS_public.  */
13898   if (TREE_PROTECTED (decl))
13899     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13900   else if (TREE_PRIVATE (decl))
13901     {
13902       if (dwarf_version == 2
13903           || die->die_parent == NULL
13904           || die->die_parent->die_tag != DW_TAG_class_type)
13905         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13906     }
13907   else if (dwarf_version > 2
13908            && die->die_parent
13909            && die->die_parent->die_tag == DW_TAG_class_type)
13910     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13911 }
13912
13913 /* Attach the specialized form of location attribute used for data members of
13914    struct and union types.  In the special case of a FIELD_DECL node which
13915    represents a bit-field, the "offset" part of this special location
13916    descriptor must indicate the distance in bytes from the lowest-addressed
13917    byte of the containing struct or union type to the lowest-addressed byte of
13918    the "containing object" for the bit-field.  (See the `field_byte_offset'
13919    function above).
13920
13921    For any given bit-field, the "containing object" is a hypothetical object
13922    (of some integral or enum type) within which the given bit-field lives.  The
13923    type of this hypothetical "containing object" is always the same as the
13924    declared type of the individual bit-field itself (for GCC anyway... the
13925    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13926    bytes) of the hypothetical "containing object" which will be given in the
13927    DW_AT_byte_size attribute for this bit-field.  (See the
13928    `byte_size_attribute' function below.)  It is also used when calculating the
13929    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13930    function below.)  */
13931
13932 static void
13933 add_data_member_location_attribute (dw_die_ref die, tree decl)
13934 {
13935   HOST_WIDE_INT offset;
13936   dw_loc_descr_ref loc_descr = 0;
13937
13938   if (TREE_CODE (decl) == TREE_BINFO)
13939     {
13940       /* We're working on the TAG_inheritance for a base class.  */
13941       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13942         {
13943           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13944              aren't at a fixed offset from all (sub)objects of the same
13945              type.  We need to extract the appropriate offset from our
13946              vtable.  The following dwarf expression means
13947
13948                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13949
13950              This is specific to the V3 ABI, of course.  */
13951
13952           dw_loc_descr_ref tmp;
13953
13954           /* Make a copy of the object address.  */
13955           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13956           add_loc_descr (&loc_descr, tmp);
13957
13958           /* Extract the vtable address.  */
13959           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13960           add_loc_descr (&loc_descr, tmp);
13961
13962           /* Calculate the address of the offset.  */
13963           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13964           gcc_assert (offset < 0);
13965
13966           tmp = int_loc_descriptor (-offset);
13967           add_loc_descr (&loc_descr, tmp);
13968           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13969           add_loc_descr (&loc_descr, tmp);
13970
13971           /* Extract the offset.  */
13972           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13973           add_loc_descr (&loc_descr, tmp);
13974
13975           /* Add it to the object address.  */
13976           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13977           add_loc_descr (&loc_descr, tmp);
13978         }
13979       else
13980         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13981     }
13982   else
13983     offset = field_byte_offset (decl);
13984
13985   if (! loc_descr)
13986     {
13987       if (dwarf_version > 2)
13988         {
13989           /* Don't need to output a location expression, just the constant. */
13990           if (offset < 0)
13991             add_AT_int (die, DW_AT_data_member_location, offset);
13992           else
13993             add_AT_unsigned (die, DW_AT_data_member_location, offset);
13994           return;
13995         }
13996       else
13997         {
13998           enum dwarf_location_atom op;
13999
14000           /* The DWARF2 standard says that we should assume that the structure
14001              address is already on the stack, so we can specify a structure
14002              field address by using DW_OP_plus_uconst.  */
14003
14004 #ifdef MIPS_DEBUGGING_INFO
14005           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14006              operator correctly.  It works only if we leave the offset on the
14007              stack.  */
14008           op = DW_OP_constu;
14009 #else
14010           op = DW_OP_plus_uconst;
14011 #endif
14012
14013           loc_descr = new_loc_descr (op, offset, 0);
14014         }
14015     }
14016
14017   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14018 }
14019
14020 /* Writes integer values to dw_vec_const array.  */
14021
14022 static void
14023 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14024 {
14025   while (size != 0)
14026     {
14027       *dest++ = val & 0xff;
14028       val >>= 8;
14029       --size;
14030     }
14031 }
14032
14033 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14034
14035 static HOST_WIDE_INT
14036 extract_int (const unsigned char *src, unsigned int size)
14037 {
14038   HOST_WIDE_INT val = 0;
14039
14040   src += size;
14041   while (size != 0)
14042     {
14043       val <<= 8;
14044       val |= *--src & 0xff;
14045       --size;
14046     }
14047   return val;
14048 }
14049
14050 /* Writes double_int values to dw_vec_const array.  */
14051
14052 static void
14053 insert_double (double_int val, unsigned char *dest)
14054 {
14055   unsigned char *p0 = dest;
14056   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14057
14058   if (WORDS_BIG_ENDIAN)
14059     {
14060       p0 = p1;
14061       p1 = dest;
14062     }
14063
14064   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14065   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14066 }
14067
14068 /* Writes floating point values to dw_vec_const array.  */
14069
14070 static void
14071 insert_float (const_rtx rtl, unsigned char *array)
14072 {
14073   REAL_VALUE_TYPE rv;
14074   long val[4];
14075   int i;
14076
14077   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14078   real_to_target (val, &rv, GET_MODE (rtl));
14079
14080   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14081   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14082     {
14083       insert_int (val[i], 4, array);
14084       array += 4;
14085     }
14086 }
14087
14088 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14089    does not have a "location" either in memory or in a register.  These
14090    things can arise in GNU C when a constant is passed as an actual parameter
14091    to an inlined function.  They can also arise in C++ where declared
14092    constants do not necessarily get memory "homes".  */
14093
14094 static bool
14095 add_const_value_attribute (dw_die_ref die, rtx rtl)
14096 {
14097   switch (GET_CODE (rtl))
14098     {
14099     case CONST_INT:
14100       {
14101         HOST_WIDE_INT val = INTVAL (rtl);
14102
14103         if (val < 0)
14104           add_AT_int (die, DW_AT_const_value, val);
14105         else
14106           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14107       }
14108       return true;
14109
14110     case CONST_DOUBLE:
14111       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14112          floating-point constant.  A CONST_DOUBLE is used whenever the
14113          constant requires more than one word in order to be adequately
14114          represented.  */
14115       {
14116         enum machine_mode mode = GET_MODE (rtl);
14117
14118         if (SCALAR_FLOAT_MODE_P (mode))
14119           {
14120             unsigned int length = GET_MODE_SIZE (mode);
14121             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14122
14123             insert_float (rtl, array);
14124             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14125           }
14126         else
14127           add_AT_double (die, DW_AT_const_value,
14128                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14129       }
14130       return true;
14131
14132     case CONST_VECTOR:
14133       {
14134         enum machine_mode mode = GET_MODE (rtl);
14135         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14136         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14137         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14138           (length * elt_size);
14139         unsigned int i;
14140         unsigned char *p;
14141
14142         switch (GET_MODE_CLASS (mode))
14143           {
14144           case MODE_VECTOR_INT:
14145             for (i = 0, p = array; i < length; i++, p += elt_size)
14146               {
14147                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14148                 double_int val = rtx_to_double_int (elt);
14149
14150                 if (elt_size <= sizeof (HOST_WIDE_INT))
14151                   insert_int (double_int_to_shwi (val), elt_size, p);
14152                 else
14153                   {
14154                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14155                     insert_double (val, p);
14156                   }
14157               }
14158             break;
14159
14160           case MODE_VECTOR_FLOAT:
14161             for (i = 0, p = array; i < length; i++, p += elt_size)
14162               {
14163                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14164                 insert_float (elt, p);
14165               }
14166             break;
14167
14168           default:
14169             gcc_unreachable ();
14170           }
14171
14172         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14173       }
14174       return true;
14175
14176     case CONST_STRING:
14177       if (dwarf_version >= 4 || !dwarf_strict)
14178         {
14179           dw_loc_descr_ref loc_result;
14180           resolve_one_addr (&rtl, NULL);
14181         rtl_addr:
14182           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14183           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14184           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14185           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14186           add_AT_loc (die, DW_AT_location, loc_result);
14187           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14188           return true;
14189         }
14190       return false;
14191
14192     case CONST:
14193       if (CONSTANT_P (XEXP (rtl, 0)))
14194         return add_const_value_attribute (die, XEXP (rtl, 0));
14195       /* FALLTHROUGH */
14196     case SYMBOL_REF:
14197       if (!const_ok_for_output (rtl))
14198         return false;
14199     case LABEL_REF:
14200       if (dwarf_version >= 4 || !dwarf_strict)
14201         goto rtl_addr;
14202       return false;
14203
14204     case PLUS:
14205       /* In cases where an inlined instance of an inline function is passed
14206          the address of an `auto' variable (which is local to the caller) we
14207          can get a situation where the DECL_RTL of the artificial local
14208          variable (for the inlining) which acts as a stand-in for the
14209          corresponding formal parameter (of the inline function) will look
14210          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14211          exactly a compile-time constant expression, but it isn't the address
14212          of the (artificial) local variable either.  Rather, it represents the
14213          *value* which the artificial local variable always has during its
14214          lifetime.  We currently have no way to represent such quasi-constant
14215          values in Dwarf, so for now we just punt and generate nothing.  */
14216       return false;
14217
14218     case HIGH:
14219     case CONST_FIXED:
14220       return false;
14221
14222     case MEM:
14223       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14224           && MEM_READONLY_P (rtl)
14225           && GET_MODE (rtl) == BLKmode)
14226         {
14227           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14228           return true;
14229         }
14230       return false;
14231
14232     default:
14233       /* No other kinds of rtx should be possible here.  */
14234       gcc_unreachable ();
14235     }
14236   return false;
14237 }
14238
14239 /* Determine whether the evaluation of EXPR references any variables
14240    or functions which aren't otherwise used (and therefore may not be
14241    output).  */
14242 static tree
14243 reference_to_unused (tree * tp, int * walk_subtrees,
14244                      void * data ATTRIBUTE_UNUSED)
14245 {
14246   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14247     *walk_subtrees = 0;
14248
14249   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14250       && ! TREE_ASM_WRITTEN (*tp))
14251     return *tp;
14252   /* ???  The C++ FE emits debug information for using decls, so
14253      putting gcc_unreachable here falls over.  See PR31899.  For now
14254      be conservative.  */
14255   else if (!cgraph_global_info_ready
14256            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14257     return *tp;
14258   else if (TREE_CODE (*tp) == VAR_DECL)
14259     {
14260       struct varpool_node *node = varpool_get_node (*tp);
14261       if (!node || !node->needed)
14262         return *tp;
14263     }
14264   else if (TREE_CODE (*tp) == FUNCTION_DECL
14265            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14266     {
14267       /* The call graph machinery must have finished analyzing,
14268          optimizing and gimplifying the CU by now.
14269          So if *TP has no call graph node associated
14270          to it, it means *TP will not be emitted.  */
14271       if (!cgraph_get_node (*tp))
14272         return *tp;
14273     }
14274   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14275     return *tp;
14276
14277   return NULL_TREE;
14278 }
14279
14280 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14281    for use in a later add_const_value_attribute call.  */
14282
14283 static rtx
14284 rtl_for_decl_init (tree init, tree type)
14285 {
14286   rtx rtl = NULL_RTX;
14287
14288   STRIP_NOPS (init);
14289
14290   /* If a variable is initialized with a string constant without embedded
14291      zeros, build CONST_STRING.  */
14292   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14293     {
14294       tree enttype = TREE_TYPE (type);
14295       tree domain = TYPE_DOMAIN (type);
14296       enum machine_mode mode = TYPE_MODE (enttype);
14297
14298       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14299           && domain
14300           && integer_zerop (TYPE_MIN_VALUE (domain))
14301           && compare_tree_int (TYPE_MAX_VALUE (domain),
14302                                TREE_STRING_LENGTH (init) - 1) == 0
14303           && ((size_t) TREE_STRING_LENGTH (init)
14304               == strlen (TREE_STRING_POINTER (init)) + 1))
14305         {
14306           rtl = gen_rtx_CONST_STRING (VOIDmode,
14307                                       ggc_strdup (TREE_STRING_POINTER (init)));
14308           rtl = gen_rtx_MEM (BLKmode, rtl);
14309           MEM_READONLY_P (rtl) = 1;
14310         }
14311     }
14312   /* Other aggregates, and complex values, could be represented using
14313      CONCAT: FIXME!  */
14314   else if (AGGREGATE_TYPE_P (type)
14315            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14316                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14317            || TREE_CODE (type) == COMPLEX_TYPE)
14318     ;
14319   /* Vectors only work if their mode is supported by the target.
14320      FIXME: generic vectors ought to work too.  */
14321   else if (TREE_CODE (type) == VECTOR_TYPE
14322            && !VECTOR_MODE_P (TYPE_MODE (type)))
14323     ;
14324   /* If the initializer is something that we know will expand into an
14325      immediate RTL constant, expand it now.  We must be careful not to
14326      reference variables which won't be output.  */
14327   else if (initializer_constant_valid_p (init, type)
14328            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14329     {
14330       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14331          possible.  */
14332       if (TREE_CODE (type) == VECTOR_TYPE)
14333         switch (TREE_CODE (init))
14334           {
14335           case VECTOR_CST:
14336             break;
14337           case CONSTRUCTOR:
14338             if (TREE_CONSTANT (init))
14339               {
14340                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14341                 bool constant_p = true;
14342                 tree value;
14343                 unsigned HOST_WIDE_INT ix;
14344
14345                 /* Even when ctor is constant, it might contain non-*_CST
14346                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14347                    belong into VECTOR_CST nodes.  */
14348                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14349                   if (!CONSTANT_CLASS_P (value))
14350                     {
14351                       constant_p = false;
14352                       break;
14353                     }
14354
14355                 if (constant_p)
14356                   {
14357                     init = build_vector_from_ctor (type, elts);
14358                     break;
14359                   }
14360               }
14361             /* FALLTHRU */
14362
14363           default:
14364             return NULL;
14365           }
14366
14367       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14368
14369       /* If expand_expr returns a MEM, it wasn't immediate.  */
14370       gcc_assert (!rtl || !MEM_P (rtl));
14371     }
14372
14373   return rtl;
14374 }
14375
14376 /* Generate RTL for the variable DECL to represent its location.  */
14377
14378 static rtx
14379 rtl_for_decl_location (tree decl)
14380 {
14381   rtx rtl;
14382
14383   /* Here we have to decide where we are going to say the parameter "lives"
14384      (as far as the debugger is concerned).  We only have a couple of
14385      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14386
14387      DECL_RTL normally indicates where the parameter lives during most of the
14388      activation of the function.  If optimization is enabled however, this
14389      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14390      that the parameter doesn't really live anywhere (as far as the code
14391      generation parts of GCC are concerned) during most of the function's
14392      activation.  That will happen (for example) if the parameter is never
14393      referenced within the function.
14394
14395      We could just generate a location descriptor here for all non-NULL
14396      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14397      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14398      where DECL_RTL is NULL or is a pseudo-reg.
14399
14400      Note however that we can only get away with using DECL_INCOMING_RTL as
14401      a backup substitute for DECL_RTL in certain limited cases.  In cases
14402      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14403      we can be sure that the parameter was passed using the same type as it is
14404      declared to have within the function, and that its DECL_INCOMING_RTL
14405      points us to a place where a value of that type is passed.
14406
14407      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14408      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14409      because in these cases DECL_INCOMING_RTL points us to a value of some
14410      type which is *different* from the type of the parameter itself.  Thus,
14411      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14412      such cases, the debugger would end up (for example) trying to fetch a
14413      `float' from a place which actually contains the first part of a
14414      `double'.  That would lead to really incorrect and confusing
14415      output at debug-time.
14416
14417      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14418      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14419      are a couple of exceptions however.  On little-endian machines we can
14420      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14421      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14422      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14423      when (on a little-endian machine) a non-prototyped function has a
14424      parameter declared to be of type `short' or `char'.  In such cases,
14425      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14426      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14427      passed `int' value.  If the debugger then uses that address to fetch
14428      a `short' or a `char' (on a little-endian machine) the result will be
14429      the correct data, so we allow for such exceptional cases below.
14430
14431      Note that our goal here is to describe the place where the given formal
14432      parameter lives during most of the function's activation (i.e. between the
14433      end of the prologue and the start of the epilogue).  We'll do that as best
14434      as we can. Note however that if the given formal parameter is modified
14435      sometime during the execution of the function, then a stack backtrace (at
14436      debug-time) will show the function as having been called with the *new*
14437      value rather than the value which was originally passed in.  This happens
14438      rarely enough that it is not a major problem, but it *is* a problem, and
14439      I'd like to fix it.
14440
14441      A future version of dwarf2out.c may generate two additional attributes for
14442      any given DW_TAG_formal_parameter DIE which will describe the "passed
14443      type" and the "passed location" for the given formal parameter in addition
14444      to the attributes we now generate to indicate the "declared type" and the
14445      "active location" for each parameter.  This additional set of attributes
14446      could be used by debuggers for stack backtraces. Separately, note that
14447      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14448      This happens (for example) for inlined-instances of inline function formal
14449      parameters which are never referenced.  This really shouldn't be
14450      happening.  All PARM_DECL nodes should get valid non-NULL
14451      DECL_INCOMING_RTL values.  FIXME.  */
14452
14453   /* Use DECL_RTL as the "location" unless we find something better.  */
14454   rtl = DECL_RTL_IF_SET (decl);
14455
14456   /* When generating abstract instances, ignore everything except
14457      constants, symbols living in memory, and symbols living in
14458      fixed registers.  */
14459   if (! reload_completed)
14460     {
14461       if (rtl
14462           && (CONSTANT_P (rtl)
14463               || (MEM_P (rtl)
14464                   && CONSTANT_P (XEXP (rtl, 0)))
14465               || (REG_P (rtl)
14466                   && TREE_CODE (decl) == VAR_DECL
14467                   && TREE_STATIC (decl))))
14468         {
14469           rtl = targetm.delegitimize_address (rtl);
14470           return rtl;
14471         }
14472       rtl = NULL_RTX;
14473     }
14474   else if (TREE_CODE (decl) == PARM_DECL)
14475     {
14476       if (rtl == NULL_RTX
14477           || is_pseudo_reg (rtl)
14478           || (MEM_P (rtl)
14479               && is_pseudo_reg (XEXP (rtl, 0))
14480               && DECL_INCOMING_RTL (decl)
14481               && MEM_P (DECL_INCOMING_RTL (decl))
14482               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14483         {
14484           tree declared_type = TREE_TYPE (decl);
14485           tree passed_type = DECL_ARG_TYPE (decl);
14486           enum machine_mode dmode = TYPE_MODE (declared_type);
14487           enum machine_mode pmode = TYPE_MODE (passed_type);
14488
14489           /* This decl represents a formal parameter which was optimized out.
14490              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14491              all cases where (rtl == NULL_RTX) just below.  */
14492           if (dmode == pmode)
14493             rtl = DECL_INCOMING_RTL (decl);
14494           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14495                    && SCALAR_INT_MODE_P (dmode)
14496                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14497                    && DECL_INCOMING_RTL (decl))
14498             {
14499               rtx inc = DECL_INCOMING_RTL (decl);
14500               if (REG_P (inc))
14501                 rtl = inc;
14502               else if (MEM_P (inc))
14503                 {
14504                   if (BYTES_BIG_ENDIAN)
14505                     rtl = adjust_address_nv (inc, dmode,
14506                                              GET_MODE_SIZE (pmode)
14507                                              - GET_MODE_SIZE (dmode));
14508                   else
14509                     rtl = inc;
14510                 }
14511             }
14512         }
14513
14514       /* If the parm was passed in registers, but lives on the stack, then
14515          make a big endian correction if the mode of the type of the
14516          parameter is not the same as the mode of the rtl.  */
14517       /* ??? This is the same series of checks that are made in dbxout.c before
14518          we reach the big endian correction code there.  It isn't clear if all
14519          of these checks are necessary here, but keeping them all is the safe
14520          thing to do.  */
14521       else if (MEM_P (rtl)
14522                && XEXP (rtl, 0) != const0_rtx
14523                && ! CONSTANT_P (XEXP (rtl, 0))
14524                /* Not passed in memory.  */
14525                && !MEM_P (DECL_INCOMING_RTL (decl))
14526                /* Not passed by invisible reference.  */
14527                && (!REG_P (XEXP (rtl, 0))
14528                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14529                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14530 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14531                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14532 #endif
14533                      )
14534                /* Big endian correction check.  */
14535                && BYTES_BIG_ENDIAN
14536                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14537                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14538                    < UNITS_PER_WORD))
14539         {
14540           int offset = (UNITS_PER_WORD
14541                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14542
14543           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14544                              plus_constant (XEXP (rtl, 0), offset));
14545         }
14546     }
14547   else if (TREE_CODE (decl) == VAR_DECL
14548            && rtl
14549            && MEM_P (rtl)
14550            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14551            && BYTES_BIG_ENDIAN)
14552     {
14553       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14554       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14555
14556       /* If a variable is declared "register" yet is smaller than
14557          a register, then if we store the variable to memory, it
14558          looks like we're storing a register-sized value, when in
14559          fact we are not.  We need to adjust the offset of the
14560          storage location to reflect the actual value's bytes,
14561          else gdb will not be able to display it.  */
14562       if (rsize > dsize)
14563         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14564                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14565     }
14566
14567   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14568      and will have been substituted directly into all expressions that use it.
14569      C does not have such a concept, but C++ and other languages do.  */
14570   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14571     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14572
14573   if (rtl)
14574     rtl = targetm.delegitimize_address (rtl);
14575
14576   /* If we don't look past the constant pool, we risk emitting a
14577      reference to a constant pool entry that isn't referenced from
14578      code, and thus is not emitted.  */
14579   if (rtl)
14580     rtl = avoid_constant_pool_reference (rtl);
14581
14582   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14583      in the current CU, resolve_addr will remove the expression referencing
14584      it.  */
14585   if (rtl == NULL_RTX
14586       && TREE_CODE (decl) == VAR_DECL
14587       && !DECL_EXTERNAL (decl)
14588       && TREE_STATIC (decl)
14589       && DECL_NAME (decl)
14590       && !DECL_HARD_REGISTER (decl)
14591       && DECL_MODE (decl) != VOIDmode)
14592     {
14593       rtl = make_decl_rtl_for_debug (decl);
14594       if (!MEM_P (rtl)
14595           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14596           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14597         rtl = NULL_RTX;
14598     }
14599
14600   return rtl;
14601 }
14602
14603 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14604    returned.  If so, the decl for the COMMON block is returned, and the
14605    value is the offset into the common block for the symbol.  */
14606
14607 static tree
14608 fortran_common (tree decl, HOST_WIDE_INT *value)
14609 {
14610   tree val_expr, cvar;
14611   enum machine_mode mode;
14612   HOST_WIDE_INT bitsize, bitpos;
14613   tree offset;
14614   int volatilep = 0, unsignedp = 0;
14615
14616   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14617      it does not have a value (the offset into the common area), or if it
14618      is thread local (as opposed to global) then it isn't common, and shouldn't
14619      be handled as such.  */
14620   if (TREE_CODE (decl) != VAR_DECL
14621       || !TREE_STATIC (decl)
14622       || !DECL_HAS_VALUE_EXPR_P (decl)
14623       || !is_fortran ())
14624     return NULL_TREE;
14625
14626   val_expr = DECL_VALUE_EXPR (decl);
14627   if (TREE_CODE (val_expr) != COMPONENT_REF)
14628     return NULL_TREE;
14629
14630   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14631                               &mode, &unsignedp, &volatilep, true);
14632
14633   if (cvar == NULL_TREE
14634       || TREE_CODE (cvar) != VAR_DECL
14635       || DECL_ARTIFICIAL (cvar)
14636       || !TREE_PUBLIC (cvar))
14637     return NULL_TREE;
14638
14639   *value = 0;
14640   if (offset != NULL)
14641     {
14642       if (!host_integerp (offset, 0))
14643         return NULL_TREE;
14644       *value = tree_low_cst (offset, 0);
14645     }
14646   if (bitpos != 0)
14647     *value += bitpos / BITS_PER_UNIT;
14648
14649   return cvar;
14650 }
14651
14652 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14653    data attribute for a variable or a parameter.  We generate the
14654    DW_AT_const_value attribute only in those cases where the given variable
14655    or parameter does not have a true "location" either in memory or in a
14656    register.  This can happen (for example) when a constant is passed as an
14657    actual argument in a call to an inline function.  (It's possible that
14658    these things can crop up in other ways also.)  Note that one type of
14659    constant value which can be passed into an inlined function is a constant
14660    pointer.  This can happen for example if an actual argument in an inlined
14661    function call evaluates to a compile-time constant address.
14662
14663    CACHE_P is true if it is worth caching the location list for DECL,
14664    so that future calls can reuse it rather than regenerate it from scratch.
14665    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14666    since we will need to refer to them each time the function is inlined.  */
14667
14668 static bool
14669 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14670                                        enum dwarf_attribute attr)
14671 {
14672   rtx rtl;
14673   dw_loc_list_ref list;
14674   var_loc_list *loc_list;
14675   cached_dw_loc_list *cache;
14676   void **slot;
14677
14678   if (TREE_CODE (decl) == ERROR_MARK)
14679     return false;
14680
14681   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14682               || TREE_CODE (decl) == RESULT_DECL);
14683
14684   /* Try to get some constant RTL for this decl, and use that as the value of
14685      the location.  */
14686
14687   rtl = rtl_for_decl_location (decl);
14688   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14689       && add_const_value_attribute (die, rtl))
14690     return true;
14691
14692   /* See if we have single element location list that is equivalent to
14693      a constant value.  That way we are better to use add_const_value_attribute
14694      rather than expanding constant value equivalent.  */
14695   loc_list = lookup_decl_loc (decl);
14696   if (loc_list
14697       && loc_list->first
14698       && loc_list->first->next == NULL
14699       && NOTE_P (loc_list->first->loc)
14700       && NOTE_VAR_LOCATION (loc_list->first->loc)
14701       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14702     {
14703       struct var_loc_node *node;
14704
14705       node = loc_list->first;
14706       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14707       if (GET_CODE (rtl) == EXPR_LIST)
14708         rtl = XEXP (rtl, 0);
14709       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14710           && add_const_value_attribute (die, rtl))
14711          return true;
14712     }
14713   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14714      list several times.  See if we've already cached the contents.  */
14715   list = NULL;
14716   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14717     cache_p = false;
14718   if (cache_p)
14719     {
14720       cache = (cached_dw_loc_list *)
14721         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14722       if (cache)
14723         list = cache->loc_list;
14724     }
14725   if (list == NULL)
14726     {
14727       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14728       /* It is usually worth caching this result if the decl is from
14729          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14730       if (cache_p && list && list->dw_loc_next)
14731         {
14732           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14733                                            DECL_UID (decl), INSERT);
14734           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14735           cache->decl_id = DECL_UID (decl);
14736           cache->loc_list = list;
14737           *slot = cache;
14738         }
14739     }
14740   if (list)
14741     {
14742       add_AT_location_description (die, attr, list);
14743       return true;
14744     }
14745   /* None of that worked, so it must not really have a location;
14746      try adding a constant value attribute from the DECL_INITIAL.  */
14747   return tree_add_const_value_attribute_for_decl (die, decl);
14748 }
14749
14750 /* Add VARIABLE and DIE into deferred locations list.  */
14751
14752 static void
14753 defer_location (tree variable, dw_die_ref die)
14754 {
14755   deferred_locations entry;
14756   entry.variable = variable;
14757   entry.die = die;
14758   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14759 }
14760
14761 /* Helper function for tree_add_const_value_attribute.  Natively encode
14762    initializer INIT into an array.  Return true if successful.  */
14763
14764 static bool
14765 native_encode_initializer (tree init, unsigned char *array, int size)
14766 {
14767   tree type;
14768
14769   if (init == NULL_TREE)
14770     return false;
14771
14772   STRIP_NOPS (init);
14773   switch (TREE_CODE (init))
14774     {
14775     case STRING_CST:
14776       type = TREE_TYPE (init);
14777       if (TREE_CODE (type) == ARRAY_TYPE)
14778         {
14779           tree enttype = TREE_TYPE (type);
14780           enum machine_mode mode = TYPE_MODE (enttype);
14781
14782           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14783             return false;
14784           if (int_size_in_bytes (type) != size)
14785             return false;
14786           if (size > TREE_STRING_LENGTH (init))
14787             {
14788               memcpy (array, TREE_STRING_POINTER (init),
14789                       TREE_STRING_LENGTH (init));
14790               memset (array + TREE_STRING_LENGTH (init),
14791                       '\0', size - TREE_STRING_LENGTH (init));
14792             }
14793           else
14794             memcpy (array, TREE_STRING_POINTER (init), size);
14795           return true;
14796         }
14797       return false;
14798     case CONSTRUCTOR:
14799       type = TREE_TYPE (init);
14800       if (int_size_in_bytes (type) != size)
14801         return false;
14802       if (TREE_CODE (type) == ARRAY_TYPE)
14803         {
14804           HOST_WIDE_INT min_index;
14805           unsigned HOST_WIDE_INT cnt;
14806           int curpos = 0, fieldsize;
14807           constructor_elt *ce;
14808
14809           if (TYPE_DOMAIN (type) == NULL_TREE
14810               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14811             return false;
14812
14813           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14814           if (fieldsize <= 0)
14815             return false;
14816
14817           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14818           memset (array, '\0', size);
14819           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14820             {
14821               tree val = ce->value;
14822               tree index = ce->index;
14823               int pos = curpos;
14824               if (index && TREE_CODE (index) == RANGE_EXPR)
14825                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14826                       * fieldsize;
14827               else if (index)
14828                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14829
14830               if (val)
14831                 {
14832                   STRIP_NOPS (val);
14833                   if (!native_encode_initializer (val, array + pos, fieldsize))
14834                     return false;
14835                 }
14836               curpos = pos + fieldsize;
14837               if (index && TREE_CODE (index) == RANGE_EXPR)
14838                 {
14839                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14840                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14841                   while (count-- > 0)
14842                     {
14843                       if (val)
14844                         memcpy (array + curpos, array + pos, fieldsize);
14845                       curpos += fieldsize;
14846                     }
14847                 }
14848               gcc_assert (curpos <= size);
14849             }
14850           return true;
14851         }
14852       else if (TREE_CODE (type) == RECORD_TYPE
14853                || TREE_CODE (type) == UNION_TYPE)
14854         {
14855           tree field = NULL_TREE;
14856           unsigned HOST_WIDE_INT cnt;
14857           constructor_elt *ce;
14858
14859           if (int_size_in_bytes (type) != size)
14860             return false;
14861
14862           if (TREE_CODE (type) == RECORD_TYPE)
14863             field = TYPE_FIELDS (type);
14864
14865           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14866             {
14867               tree val = ce->value;
14868               int pos, fieldsize;
14869
14870               if (ce->index != 0)
14871                 field = ce->index;
14872
14873               if (val)
14874                 STRIP_NOPS (val);
14875
14876               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14877                 return false;
14878
14879               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14880                   && TYPE_DOMAIN (TREE_TYPE (field))
14881                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14882                 return false;
14883               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14884                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14885                 return false;
14886               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14887               pos = int_byte_position (field);
14888               gcc_assert (pos + fieldsize <= size);
14889               if (val
14890                   && !native_encode_initializer (val, array + pos, fieldsize))
14891                 return false;
14892             }
14893           return true;
14894         }
14895       return false;
14896     case VIEW_CONVERT_EXPR:
14897     case NON_LVALUE_EXPR:
14898       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14899     default:
14900       return native_encode_expr (init, array, size) == size;
14901     }
14902 }
14903
14904 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14905    attribute is the const value T.  */
14906
14907 static bool
14908 tree_add_const_value_attribute (dw_die_ref die, tree t)
14909 {
14910   tree init;
14911   tree type = TREE_TYPE (t);
14912   rtx rtl;
14913
14914   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14915     return false;
14916
14917   init = t;
14918   gcc_assert (!DECL_P (init));
14919
14920   rtl = rtl_for_decl_init (init, type);
14921   if (rtl)
14922     return add_const_value_attribute (die, rtl);
14923   /* If the host and target are sane, try harder.  */
14924   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14925            && initializer_constant_valid_p (init, type))
14926     {
14927       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14928       if (size > 0 && (int) size == size)
14929         {
14930           unsigned char *array = (unsigned char *)
14931             ggc_alloc_cleared_atomic (size);
14932
14933           if (native_encode_initializer (init, array, size))
14934             {
14935               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14936               return true;
14937             }
14938         }
14939     }
14940   return false;
14941 }
14942
14943 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14944    attribute is the const value of T, where T is an integral constant
14945    variable with static storage duration
14946    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14947
14948 static bool
14949 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14950 {
14951
14952   if (!decl
14953       || (TREE_CODE (decl) != VAR_DECL
14954           && TREE_CODE (decl) != CONST_DECL)
14955       || (TREE_CODE (decl) == VAR_DECL
14956           && !TREE_STATIC (decl)))
14957     return false;
14958
14959     if (TREE_READONLY (decl)
14960         && ! TREE_THIS_VOLATILE (decl)
14961         && DECL_INITIAL (decl))
14962       /* OK */;
14963     else
14964       return false;
14965
14966   /* Don't add DW_AT_const_value if abstract origin already has one.  */
14967   if (get_AT (var_die, DW_AT_const_value))
14968     return false;
14969
14970   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14971 }
14972
14973 /* Convert the CFI instructions for the current function into a
14974    location list.  This is used for DW_AT_frame_base when we targeting
14975    a dwarf2 consumer that does not support the dwarf3
14976    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14977    expressions.  */
14978
14979 static dw_loc_list_ref
14980 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14981 {
14982   int ix;
14983   dw_fde_ref fde;
14984   dw_loc_list_ref list, *list_tail;
14985   dw_cfi_ref cfi;
14986   dw_cfa_location last_cfa, next_cfa;
14987   const char *start_label, *last_label, *section;
14988   dw_cfa_location remember;
14989
14990   fde = cfun->fde;
14991   gcc_assert (fde != NULL);
14992
14993   section = secname_for_decl (current_function_decl);
14994   list_tail = &list;
14995   list = NULL;
14996
14997   memset (&next_cfa, 0, sizeof (next_cfa));
14998   next_cfa.reg = INVALID_REGNUM;
14999   remember = next_cfa;
15000
15001   start_label = fde->dw_fde_begin;
15002
15003   /* ??? Bald assumption that the CIE opcode list does not contain
15004      advance opcodes.  */
15005   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15006     lookup_cfa_1 (cfi, &next_cfa, &remember);
15007
15008   last_cfa = next_cfa;
15009   last_label = start_label;
15010
15011   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15012     {
15013       /* If the first partition contained no CFI adjustments, the
15014          CIE opcodes apply to the whole first partition.  */
15015       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15016                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15017       list_tail =&(*list_tail)->dw_loc_next;
15018       start_label = last_label = fde->dw_fde_second_begin;
15019     }
15020
15021   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15022     {
15023       switch (cfi->dw_cfi_opc)
15024         {
15025         case DW_CFA_set_loc:
15026         case DW_CFA_advance_loc1:
15027         case DW_CFA_advance_loc2:
15028         case DW_CFA_advance_loc4:
15029           if (!cfa_equal_p (&last_cfa, &next_cfa))
15030             {
15031               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15032                                          start_label, last_label, section);
15033
15034               list_tail = &(*list_tail)->dw_loc_next;
15035               last_cfa = next_cfa;
15036               start_label = last_label;
15037             }
15038           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15039           break;
15040
15041         case DW_CFA_advance_loc:
15042           /* The encoding is complex enough that we should never emit this.  */
15043           gcc_unreachable ();
15044
15045         default:
15046           lookup_cfa_1 (cfi, &next_cfa, &remember);
15047           break;
15048         }
15049       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15050         {
15051           if (!cfa_equal_p (&last_cfa, &next_cfa))
15052             {
15053               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15054                                          start_label, last_label, section);
15055
15056               list_tail = &(*list_tail)->dw_loc_next;
15057               last_cfa = next_cfa;
15058               start_label = last_label;
15059             }
15060           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15061                                      start_label, fde->dw_fde_end, section);
15062           list_tail = &(*list_tail)->dw_loc_next;
15063           start_label = last_label = fde->dw_fde_second_begin;
15064         }
15065     }
15066
15067   if (!cfa_equal_p (&last_cfa, &next_cfa))
15068     {
15069       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15070                                  start_label, last_label, section);
15071       list_tail = &(*list_tail)->dw_loc_next;
15072       start_label = last_label;
15073     }
15074
15075   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15076                              start_label,
15077                              fde->dw_fde_second_begin
15078                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15079                              section);
15080
15081   if (list && list->dw_loc_next)
15082     gen_llsym (list);
15083
15084   return list;
15085 }
15086
15087 /* Compute a displacement from the "steady-state frame pointer" to the
15088    frame base (often the same as the CFA), and store it in
15089    frame_pointer_fb_offset.  OFFSET is added to the displacement
15090    before the latter is negated.  */
15091
15092 static void
15093 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15094 {
15095   rtx reg, elim;
15096
15097 #ifdef FRAME_POINTER_CFA_OFFSET
15098   reg = frame_pointer_rtx;
15099   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15100 #else
15101   reg = arg_pointer_rtx;
15102   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15103 #endif
15104
15105   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15106   if (GET_CODE (elim) == PLUS)
15107     {
15108       offset += INTVAL (XEXP (elim, 1));
15109       elim = XEXP (elim, 0);
15110     }
15111
15112   frame_pointer_fb_offset = -offset;
15113
15114   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15115      in which to eliminate.  This is because it's stack pointer isn't 
15116      directly accessible as a register within the ISA.  To work around
15117      this, assume that while we cannot provide a proper value for
15118      frame_pointer_fb_offset, we won't need one either.  */
15119   frame_pointer_fb_offset_valid
15120     = ((SUPPORTS_STACK_ALIGNMENT
15121         && (elim == hard_frame_pointer_rtx
15122             || elim == stack_pointer_rtx))
15123        || elim == (frame_pointer_needed
15124                    ? hard_frame_pointer_rtx
15125                    : stack_pointer_rtx));
15126 }
15127
15128 /* Generate a DW_AT_name attribute given some string value to be included as
15129    the value of the attribute.  */
15130
15131 static void
15132 add_name_attribute (dw_die_ref die, const char *name_string)
15133 {
15134   if (name_string != NULL && *name_string != 0)
15135     {
15136       if (demangle_name_func)
15137         name_string = (*demangle_name_func) (name_string);
15138
15139       add_AT_string (die, DW_AT_name, name_string);
15140     }
15141 }
15142
15143 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15144    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15145    of TYPE accordingly.
15146
15147    ??? This is a temporary measure until after we're able to generate
15148    regular DWARF for the complex Ada type system.  */
15149
15150 static void 
15151 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15152                                      dw_die_ref context_die)
15153 {
15154   tree dtype;
15155   dw_die_ref dtype_die;
15156
15157   if (!lang_hooks.types.descriptive_type)
15158     return;
15159
15160   dtype = lang_hooks.types.descriptive_type (type);
15161   if (!dtype)
15162     return;
15163
15164   dtype_die = lookup_type_die (dtype);
15165   if (!dtype_die)
15166     {
15167       gen_type_die (dtype, context_die);
15168       dtype_die = lookup_type_die (dtype);
15169       gcc_assert (dtype_die);
15170     }
15171
15172   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15173 }
15174
15175 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15176
15177 static void
15178 add_comp_dir_attribute (dw_die_ref die)
15179 {
15180   const char *wd = get_src_pwd ();
15181   char *wd1;
15182
15183   if (wd == NULL)
15184     return;
15185
15186   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15187     {
15188       int wdlen;
15189
15190       wdlen = strlen (wd);
15191       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15192       strcpy (wd1, wd);
15193       wd1 [wdlen] = DIR_SEPARATOR;
15194       wd1 [wdlen + 1] = 0;
15195       wd = wd1;
15196     }
15197
15198     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15199 }
15200
15201 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15202    default.  */
15203
15204 static int
15205 lower_bound_default (void)
15206 {
15207   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15208     {
15209     case DW_LANG_C:
15210     case DW_LANG_C89:
15211     case DW_LANG_C99:
15212     case DW_LANG_C_plus_plus:
15213     case DW_LANG_ObjC:
15214     case DW_LANG_ObjC_plus_plus:
15215     case DW_LANG_Java:
15216       return 0;
15217     case DW_LANG_Fortran77:
15218     case DW_LANG_Fortran90:
15219     case DW_LANG_Fortran95:
15220       return 1;
15221     case DW_LANG_UPC:
15222     case DW_LANG_D:
15223     case DW_LANG_Python:
15224       return dwarf_version >= 4 ? 0 : -1;
15225     case DW_LANG_Ada95:
15226     case DW_LANG_Ada83:
15227     case DW_LANG_Cobol74:
15228     case DW_LANG_Cobol85:
15229     case DW_LANG_Pascal83:
15230     case DW_LANG_Modula2:
15231     case DW_LANG_PLI:
15232       return dwarf_version >= 4 ? 1 : -1;
15233     default:
15234       return -1;
15235     }
15236 }
15237
15238 /* Given a tree node describing an array bound (either lower or upper) output
15239    a representation for that bound.  */
15240
15241 static void
15242 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15243 {
15244   switch (TREE_CODE (bound))
15245     {
15246     case ERROR_MARK:
15247       return;
15248
15249     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15250     case INTEGER_CST:
15251       {
15252         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15253         int dflt;
15254
15255         /* Use the default if possible.  */
15256         if (bound_attr == DW_AT_lower_bound
15257             && host_integerp (bound, 0)
15258             && (dflt = lower_bound_default ()) != -1
15259             && tree_low_cst (bound, 0) == dflt)
15260           ;
15261
15262         /* Otherwise represent the bound as an unsigned value with the
15263            precision of its type.  The precision and signedness of the
15264            type will be necessary to re-interpret it unambiguously.  */
15265         else if (prec < HOST_BITS_PER_WIDE_INT)
15266           {
15267             unsigned HOST_WIDE_INT mask
15268               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15269             add_AT_unsigned (subrange_die, bound_attr,
15270                              TREE_INT_CST_LOW (bound) & mask);
15271           }
15272         else if (prec == HOST_BITS_PER_WIDE_INT
15273                  || TREE_INT_CST_HIGH (bound) == 0)
15274           add_AT_unsigned (subrange_die, bound_attr,
15275                            TREE_INT_CST_LOW (bound));
15276         else
15277           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15278                          TREE_INT_CST_LOW (bound));
15279       }
15280       break;
15281
15282     CASE_CONVERT:
15283     case VIEW_CONVERT_EXPR:
15284       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15285       break;
15286
15287     case SAVE_EXPR:
15288       break;
15289
15290     case VAR_DECL:
15291     case PARM_DECL:
15292     case RESULT_DECL:
15293       {
15294         dw_die_ref decl_die = lookup_decl_die (bound);
15295
15296         /* ??? Can this happen, or should the variable have been bound
15297            first?  Probably it can, since I imagine that we try to create
15298            the types of parameters in the order in which they exist in
15299            the list, and won't have created a forward reference to a
15300            later parameter.  */
15301         if (decl_die != NULL)
15302           {
15303             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15304             break;
15305           }
15306       }
15307       /* FALLTHRU */
15308
15309     default:
15310       {
15311         /* Otherwise try to create a stack operation procedure to
15312            evaluate the value of the array bound.  */
15313
15314         dw_die_ref ctx, decl_die;
15315         dw_loc_list_ref list;
15316
15317         list = loc_list_from_tree (bound, 2);
15318         if (list == NULL || single_element_loc_list_p (list))
15319           {
15320             /* If DW_AT_*bound is not a reference nor constant, it is
15321                a DWARF expression rather than location description.
15322                For that loc_list_from_tree (bound, 0) is needed.
15323                If that fails to give a single element list,
15324                fall back to outputting this as a reference anyway.  */
15325             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15326             if (list2 && single_element_loc_list_p (list2))
15327               {
15328                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15329                 break;
15330               }
15331           }
15332         if (list == NULL)
15333           break;
15334
15335         if (current_function_decl == 0)
15336           ctx = comp_unit_die ();
15337         else
15338           ctx = lookup_decl_die (current_function_decl);
15339
15340         decl_die = new_die (DW_TAG_variable, ctx, bound);
15341         add_AT_flag (decl_die, DW_AT_artificial, 1);
15342         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15343         add_AT_location_description (decl_die, DW_AT_location, list);
15344         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15345         break;
15346       }
15347     }
15348 }
15349
15350 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15351    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15352    Note that the block of subscript information for an array type also
15353    includes information about the element type of the given array type.  */
15354
15355 static void
15356 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15357 {
15358   unsigned dimension_number;
15359   tree lower, upper;
15360   dw_die_ref subrange_die;
15361
15362   for (dimension_number = 0;
15363        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15364        type = TREE_TYPE (type), dimension_number++)
15365     {
15366       tree domain = TYPE_DOMAIN (type);
15367
15368       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15369         break;
15370
15371       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15372          and (in GNU C only) variable bounds.  Handle all three forms
15373          here.  */
15374       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15375       if (domain)
15376         {
15377           /* We have an array type with specified bounds.  */
15378           lower = TYPE_MIN_VALUE (domain);
15379           upper = TYPE_MAX_VALUE (domain);
15380
15381           /* Define the index type.  */
15382           if (TREE_TYPE (domain))
15383             {
15384               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15385                  TREE_TYPE field.  We can't emit debug info for this
15386                  because it is an unnamed integral type.  */
15387               if (TREE_CODE (domain) == INTEGER_TYPE
15388                   && TYPE_NAME (domain) == NULL_TREE
15389                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15390                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15391                 ;
15392               else
15393                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15394                                     type_die);
15395             }
15396
15397           /* ??? If upper is NULL, the array has unspecified length,
15398              but it does have a lower bound.  This happens with Fortran
15399                dimension arr(N:*)
15400              Since the debugger is definitely going to need to know N
15401              to produce useful results, go ahead and output the lower
15402              bound solo, and hope the debugger can cope.  */
15403
15404           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15405           if (upper)
15406             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15407         }
15408
15409       /* Otherwise we have an array type with an unspecified length.  The
15410          DWARF-2 spec does not say how to handle this; let's just leave out the
15411          bounds.  */
15412     }
15413 }
15414
15415 static void
15416 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15417 {
15418   unsigned size;
15419
15420   switch (TREE_CODE (tree_node))
15421     {
15422     case ERROR_MARK:
15423       size = 0;
15424       break;
15425     case ENUMERAL_TYPE:
15426     case RECORD_TYPE:
15427     case UNION_TYPE:
15428     case QUAL_UNION_TYPE:
15429       size = int_size_in_bytes (tree_node);
15430       break;
15431     case FIELD_DECL:
15432       /* For a data member of a struct or union, the DW_AT_byte_size is
15433          generally given as the number of bytes normally allocated for an
15434          object of the *declared* type of the member itself.  This is true
15435          even for bit-fields.  */
15436       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15437       break;
15438     default:
15439       gcc_unreachable ();
15440     }
15441
15442   /* Note that `size' might be -1 when we get to this point.  If it is, that
15443      indicates that the byte size of the entity in question is variable.  We
15444      have no good way of expressing this fact in Dwarf at the present time,
15445      so just let the -1 pass on through.  */
15446   add_AT_unsigned (die, DW_AT_byte_size, size);
15447 }
15448
15449 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15450    which specifies the distance in bits from the highest order bit of the
15451    "containing object" for the bit-field to the highest order bit of the
15452    bit-field itself.
15453
15454    For any given bit-field, the "containing object" is a hypothetical object
15455    (of some integral or enum type) within which the given bit-field lives.  The
15456    type of this hypothetical "containing object" is always the same as the
15457    declared type of the individual bit-field itself.  The determination of the
15458    exact location of the "containing object" for a bit-field is rather
15459    complicated.  It's handled by the `field_byte_offset' function (above).
15460
15461    Note that it is the size (in bytes) of the hypothetical "containing object"
15462    which will be given in the DW_AT_byte_size attribute for this bit-field.
15463    (See `byte_size_attribute' above).  */
15464
15465 static inline void
15466 add_bit_offset_attribute (dw_die_ref die, tree decl)
15467 {
15468   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15469   tree type = DECL_BIT_FIELD_TYPE (decl);
15470   HOST_WIDE_INT bitpos_int;
15471   HOST_WIDE_INT highest_order_object_bit_offset;
15472   HOST_WIDE_INT highest_order_field_bit_offset;
15473   HOST_WIDE_INT bit_offset;
15474
15475   /* Must be a field and a bit field.  */
15476   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15477
15478   /* We can't yet handle bit-fields whose offsets are variable, so if we
15479      encounter such things, just return without generating any attribute
15480      whatsoever.  Likewise for variable or too large size.  */
15481   if (! host_integerp (bit_position (decl), 0)
15482       || ! host_integerp (DECL_SIZE (decl), 1))
15483     return;
15484
15485   bitpos_int = int_bit_position (decl);
15486
15487   /* Note that the bit offset is always the distance (in bits) from the
15488      highest-order bit of the "containing object" to the highest-order bit of
15489      the bit-field itself.  Since the "high-order end" of any object or field
15490      is different on big-endian and little-endian machines, the computation
15491      below must take account of these differences.  */
15492   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15493   highest_order_field_bit_offset = bitpos_int;
15494
15495   if (! BYTES_BIG_ENDIAN)
15496     {
15497       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15498       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15499     }
15500
15501   bit_offset
15502     = (! BYTES_BIG_ENDIAN
15503        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15504        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15505
15506   if (bit_offset < 0)
15507     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15508   else
15509     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15510 }
15511
15512 /* For a FIELD_DECL node which represents a bit field, output an attribute
15513    which specifies the length in bits of the given field.  */
15514
15515 static inline void
15516 add_bit_size_attribute (dw_die_ref die, tree decl)
15517 {
15518   /* Must be a field and a bit field.  */
15519   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15520               && DECL_BIT_FIELD_TYPE (decl));
15521
15522   if (host_integerp (DECL_SIZE (decl), 1))
15523     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15524 }
15525
15526 /* If the compiled language is ANSI C, then add a 'prototyped'
15527    attribute, if arg types are given for the parameters of a function.  */
15528
15529 static inline void
15530 add_prototyped_attribute (dw_die_ref die, tree func_type)
15531 {
15532   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15533       && prototype_p (func_type))
15534     add_AT_flag (die, DW_AT_prototyped, 1);
15535 }
15536
15537 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15538    by looking in either the type declaration or object declaration
15539    equate table.  */
15540
15541 static inline dw_die_ref
15542 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15543 {
15544   dw_die_ref origin_die = NULL;
15545
15546   if (TREE_CODE (origin) != FUNCTION_DECL)
15547     {
15548       /* We may have gotten separated from the block for the inlined
15549          function, if we're in an exception handler or some such; make
15550          sure that the abstract function has been written out.
15551
15552          Doing this for nested functions is wrong, however; functions are
15553          distinct units, and our context might not even be inline.  */
15554       tree fn = origin;
15555
15556       if (TYPE_P (fn))
15557         fn = TYPE_STUB_DECL (fn);
15558
15559       fn = decl_function_context (fn);
15560       if (fn)
15561         dwarf2out_abstract_function (fn);
15562     }
15563
15564   if (DECL_P (origin))
15565     origin_die = lookup_decl_die (origin);
15566   else if (TYPE_P (origin))
15567     origin_die = lookup_type_die (origin);
15568
15569   /* XXX: Functions that are never lowered don't always have correct block
15570      trees (in the case of java, they simply have no block tree, in some other
15571      languages).  For these functions, there is nothing we can really do to
15572      output correct debug info for inlined functions in all cases.  Rather
15573      than die, we'll just produce deficient debug info now, in that we will
15574      have variables without a proper abstract origin.  In the future, when all
15575      functions are lowered, we should re-add a gcc_assert (origin_die)
15576      here.  */
15577
15578   if (origin_die)
15579     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15580   return origin_die;
15581 }
15582
15583 /* We do not currently support the pure_virtual attribute.  */
15584
15585 static inline void
15586 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15587 {
15588   if (DECL_VINDEX (func_decl))
15589     {
15590       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15591
15592       if (host_integerp (DECL_VINDEX (func_decl), 0))
15593         add_AT_loc (die, DW_AT_vtable_elem_location,
15594                     new_loc_descr (DW_OP_constu,
15595                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15596                                    0));
15597
15598       /* GNU extension: Record what type this method came from originally.  */
15599       if (debug_info_level > DINFO_LEVEL_TERSE
15600           && DECL_CONTEXT (func_decl))
15601         add_AT_die_ref (die, DW_AT_containing_type,
15602                         lookup_type_die (DECL_CONTEXT (func_decl)));
15603     }
15604 }
15605 \f
15606 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15607    given decl.  This used to be a vendor extension until after DWARF 4
15608    standardized it.  */
15609
15610 static void
15611 add_linkage_attr (dw_die_ref die, tree decl)
15612 {
15613   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15614
15615   /* Mimic what assemble_name_raw does with a leading '*'.  */
15616   if (name[0] == '*')
15617     name = &name[1];
15618
15619   if (dwarf_version >= 4)
15620     add_AT_string (die, DW_AT_linkage_name, name);
15621   else
15622     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15623 }
15624
15625 /* Add source coordinate attributes for the given decl.  */
15626
15627 static void
15628 add_src_coords_attributes (dw_die_ref die, tree decl)
15629 {
15630   expanded_location s;
15631
15632   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15633     return;
15634   s = expand_location (DECL_SOURCE_LOCATION (decl));
15635   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15636   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15637 }
15638
15639 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15640
15641 static void
15642 add_linkage_name (dw_die_ref die, tree decl)
15643 {
15644   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15645        && TREE_PUBLIC (decl)
15646        && !DECL_ABSTRACT (decl)
15647        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15648        && die->die_tag != DW_TAG_member)
15649     {
15650       /* Defer until we have an assembler name set.  */
15651       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15652         {
15653           limbo_die_node *asm_name;
15654
15655           asm_name = ggc_alloc_cleared_limbo_die_node ();
15656           asm_name->die = die;
15657           asm_name->created_for = decl;
15658           asm_name->next = deferred_asm_name;
15659           deferred_asm_name = asm_name;
15660         }
15661       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15662         add_linkage_attr (die, decl);
15663     }
15664 }
15665
15666 /* Add a DW_AT_name attribute and source coordinate attribute for the
15667    given decl, but only if it actually has a name.  */
15668
15669 static void
15670 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15671 {
15672   tree decl_name;
15673
15674   decl_name = DECL_NAME (decl);
15675   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15676     {
15677       const char *name = dwarf2_name (decl, 0);
15678       if (name)
15679         add_name_attribute (die, name);
15680       if (! DECL_ARTIFICIAL (decl))
15681         add_src_coords_attributes (die, decl);
15682
15683       add_linkage_name (die, decl);
15684     }
15685
15686 #ifdef VMS_DEBUGGING_INFO
15687   /* Get the function's name, as described by its RTL.  This may be different
15688      from the DECL_NAME name used in the source file.  */
15689   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15690     {
15691       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15692                    XEXP (DECL_RTL (decl), 0));
15693       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15694     }
15695 #endif /* VMS_DEBUGGING_INFO */
15696 }
15697
15698 #ifdef VMS_DEBUGGING_INFO
15699 /* Output the debug main pointer die for VMS */
15700
15701 void
15702 dwarf2out_vms_debug_main_pointer (void)
15703 {
15704   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15705   dw_die_ref die;
15706
15707   /* Allocate the VMS debug main subprogram die.  */
15708   die = ggc_alloc_cleared_die_node ();
15709   die->die_tag = DW_TAG_subprogram;
15710   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15711   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15712                                current_function_funcdef_no);
15713   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15714
15715   /* Make it the first child of comp_unit_die ().  */
15716   die->die_parent = comp_unit_die ();
15717   if (comp_unit_die ()->die_child)
15718     {
15719       die->die_sib = comp_unit_die ()->die_child->die_sib;
15720       comp_unit_die ()->die_child->die_sib = die;
15721     }
15722   else
15723     {
15724       die->die_sib = die;
15725       comp_unit_die ()->die_child = die;
15726     }
15727 }
15728 #endif /* VMS_DEBUGGING_INFO */
15729
15730 /* Push a new declaration scope.  */
15731
15732 static void
15733 push_decl_scope (tree scope)
15734 {
15735   VEC_safe_push (tree, gc, decl_scope_table, scope);
15736 }
15737
15738 /* Pop a declaration scope.  */
15739
15740 static inline void
15741 pop_decl_scope (void)
15742 {
15743   VEC_pop (tree, decl_scope_table);
15744 }
15745
15746 /* Return the DIE for the scope that immediately contains this type.
15747    Non-named types get global scope.  Named types nested in other
15748    types get their containing scope if it's open, or global scope
15749    otherwise.  All other types (i.e. function-local named types) get
15750    the current active scope.  */
15751
15752 static dw_die_ref
15753 scope_die_for (tree t, dw_die_ref context_die)
15754 {
15755   dw_die_ref scope_die = NULL;
15756   tree containing_scope;
15757   int i;
15758
15759   /* Non-types always go in the current scope.  */
15760   gcc_assert (TYPE_P (t));
15761
15762   containing_scope = TYPE_CONTEXT (t);
15763
15764   /* Use the containing namespace if it was passed in (for a declaration).  */
15765   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15766     {
15767       if (context_die == lookup_decl_die (containing_scope))
15768         /* OK */;
15769       else
15770         containing_scope = NULL_TREE;
15771     }
15772
15773   /* Ignore function type "scopes" from the C frontend.  They mean that
15774      a tagged type is local to a parmlist of a function declarator, but
15775      that isn't useful to DWARF.  */
15776   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15777     containing_scope = NULL_TREE;
15778
15779   if (SCOPE_FILE_SCOPE_P (containing_scope))
15780     scope_die = comp_unit_die ();
15781   else if (TYPE_P (containing_scope))
15782     {
15783       /* For types, we can just look up the appropriate DIE.  But
15784          first we check to see if we're in the middle of emitting it
15785          so we know where the new DIE should go.  */
15786       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
15787         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
15788           break;
15789
15790       if (i < 0)
15791         {
15792           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
15793                       || TREE_ASM_WRITTEN (containing_scope));
15794           /*We are not in the middle of emitting the type
15795             CONTAINING_SCOPE. Let's see if it's emitted already.  */
15796           scope_die = lookup_type_die (containing_scope);
15797
15798           /* If none of the current dies are suitable, we get file scope.  */
15799           if (scope_die == NULL)
15800             scope_die = comp_unit_die ();
15801         }
15802       else
15803         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15804     }
15805   else
15806     scope_die = context_die;
15807
15808   return scope_die;
15809 }
15810
15811 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15812
15813 static inline int
15814 local_scope_p (dw_die_ref context_die)
15815 {
15816   for (; context_die; context_die = context_die->die_parent)
15817     if (context_die->die_tag == DW_TAG_inlined_subroutine
15818         || context_die->die_tag == DW_TAG_subprogram)
15819       return 1;
15820
15821   return 0;
15822 }
15823
15824 /* Returns nonzero if CONTEXT_DIE is a class.  */
15825
15826 static inline int
15827 class_scope_p (dw_die_ref context_die)
15828 {
15829   return (context_die
15830           && (context_die->die_tag == DW_TAG_structure_type
15831               || context_die->die_tag == DW_TAG_class_type
15832               || context_die->die_tag == DW_TAG_interface_type
15833               || context_die->die_tag == DW_TAG_union_type));
15834 }
15835
15836 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15837    whether or not to treat a DIE in this context as a declaration.  */
15838
15839 static inline int
15840 class_or_namespace_scope_p (dw_die_ref context_die)
15841 {
15842   return (class_scope_p (context_die)
15843           || (context_die && context_die->die_tag == DW_TAG_namespace));
15844 }
15845
15846 /* Many forms of DIEs require a "type description" attribute.  This
15847    routine locates the proper "type descriptor" die for the type given
15848    by 'type', and adds a DW_AT_type attribute below the given die.  */
15849
15850 static void
15851 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15852                     int decl_volatile, dw_die_ref context_die)
15853 {
15854   enum tree_code code  = TREE_CODE (type);
15855   dw_die_ref type_die  = NULL;
15856
15857   /* ??? If this type is an unnamed subrange type of an integral, floating-point
15858      or fixed-point type, use the inner type.  This is because we have no
15859      support for unnamed types in base_type_die.  This can happen if this is
15860      an Ada subrange type.  Correct solution is emit a subrange type die.  */
15861   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15862       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15863     type = TREE_TYPE (type), code = TREE_CODE (type);
15864
15865   if (code == ERROR_MARK
15866       /* Handle a special case.  For functions whose return type is void, we
15867          generate *no* type attribute.  (Note that no object may have type
15868          `void', so this only applies to function return types).  */
15869       || code == VOID_TYPE)
15870     return;
15871
15872   type_die = modified_type_die (type,
15873                                 decl_const || TYPE_READONLY (type),
15874                                 decl_volatile || TYPE_VOLATILE (type),
15875                                 context_die);
15876
15877   if (type_die != NULL)
15878     add_AT_die_ref (object_die, DW_AT_type, type_die);
15879 }
15880
15881 /* Given an object die, add the calling convention attribute for the
15882    function call type.  */
15883 static void
15884 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15885 {
15886   enum dwarf_calling_convention value = DW_CC_normal;
15887
15888   value = ((enum dwarf_calling_convention)
15889            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15890
15891   if (is_fortran ()
15892       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15893     {
15894       /* DWARF 2 doesn't provide a way to identify a program's source-level
15895         entry point.  DW_AT_calling_convention attributes are only meant
15896         to describe functions' calling conventions.  However, lacking a
15897         better way to signal the Fortran main program, we used this for 
15898         a long time, following existing custom.  Now, DWARF 4 has 
15899         DW_AT_main_subprogram, which we add below, but some tools still
15900         rely on the old way, which we thus keep.  */
15901       value = DW_CC_program;
15902
15903       if (dwarf_version >= 4 || !dwarf_strict)
15904         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15905     }
15906
15907   /* Only add the attribute if the backend requests it, and
15908      is not DW_CC_normal.  */
15909   if (value && (value != DW_CC_normal))
15910     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15911 }
15912
15913 /* Given a tree pointer to a struct, class, union, or enum type node, return
15914    a pointer to the (string) tag name for the given type, or zero if the type
15915    was declared without a tag.  */
15916
15917 static const char *
15918 type_tag (const_tree type)
15919 {
15920   const char *name = 0;
15921
15922   if (TYPE_NAME (type) != 0)
15923     {
15924       tree t = 0;
15925
15926       /* Find the IDENTIFIER_NODE for the type name.  */
15927       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15928           && !TYPE_NAMELESS (type))
15929         t = TYPE_NAME (type);
15930
15931       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15932          a TYPE_DECL node, regardless of whether or not a `typedef' was
15933          involved.  */
15934       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15935                && ! DECL_IGNORED_P (TYPE_NAME (type)))
15936         {
15937           /* We want to be extra verbose.  Don't call dwarf_name if
15938              DECL_NAME isn't set.  The default hook for decl_printable_name
15939              doesn't like that, and in this context it's correct to return
15940              0, instead of "<anonymous>" or the like.  */
15941           if (DECL_NAME (TYPE_NAME (type))
15942               && !DECL_NAMELESS (TYPE_NAME (type)))
15943             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15944         }
15945
15946       /* Now get the name as a string, or invent one.  */
15947       if (!name && t != 0)
15948         name = IDENTIFIER_POINTER (t);
15949     }
15950
15951   return (name == 0 || *name == '\0') ? 0 : name;
15952 }
15953
15954 /* Return the type associated with a data member, make a special check
15955    for bit field types.  */
15956
15957 static inline tree
15958 member_declared_type (const_tree member)
15959 {
15960   return (DECL_BIT_FIELD_TYPE (member)
15961           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15962 }
15963
15964 /* Get the decl's label, as described by its RTL. This may be different
15965    from the DECL_NAME name used in the source file.  */
15966
15967 #if 0
15968 static const char *
15969 decl_start_label (tree decl)
15970 {
15971   rtx x;
15972   const char *fnname;
15973
15974   x = DECL_RTL (decl);
15975   gcc_assert (MEM_P (x));
15976
15977   x = XEXP (x, 0);
15978   gcc_assert (GET_CODE (x) == SYMBOL_REF);
15979
15980   fnname = XSTR (x, 0);
15981   return fnname;
15982 }
15983 #endif
15984 \f
15985 /* These routines generate the internal representation of the DIE's for
15986    the compilation unit.  Debugging information is collected by walking
15987    the declaration trees passed in from dwarf2out_decl().  */
15988
15989 static void
15990 gen_array_type_die (tree type, dw_die_ref context_die)
15991 {
15992   dw_die_ref scope_die = scope_die_for (type, context_die);
15993   dw_die_ref array_die;
15994
15995   /* GNU compilers represent multidimensional array types as sequences of one
15996      dimensional array types whose element types are themselves array types.
15997      We sometimes squish that down to a single array_type DIE with multiple
15998      subscripts in the Dwarf debugging info.  The draft Dwarf specification
15999      say that we are allowed to do this kind of compression in C, because
16000      there is no difference between an array of arrays and a multidimensional
16001      array.  We don't do this for Ada to remain as close as possible to the
16002      actual representation, which is especially important against the language
16003      flexibilty wrt arrays of variable size.  */
16004
16005   bool collapse_nested_arrays = !is_ada ();
16006   tree element_type;
16007
16008   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16009      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16010   if (TYPE_STRING_FLAG (type)
16011       && TREE_CODE (type) == ARRAY_TYPE
16012       && is_fortran ()
16013       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16014     {
16015       HOST_WIDE_INT size;
16016
16017       array_die = new_die (DW_TAG_string_type, scope_die, type);
16018       add_name_attribute (array_die, type_tag (type));
16019       equate_type_number_to_die (type, array_die);
16020       size = int_size_in_bytes (type);
16021       if (size >= 0)
16022         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16023       else if (TYPE_DOMAIN (type) != NULL_TREE
16024                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16025                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16026         {
16027           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16028           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16029
16030           size = int_size_in_bytes (TREE_TYPE (szdecl));
16031           if (loc && size > 0)
16032             {
16033               add_AT_location_description (array_die, DW_AT_string_length, loc);
16034               if (size != DWARF2_ADDR_SIZE)
16035                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16036             }
16037         }
16038       return;
16039     }
16040
16041   /* ??? The SGI dwarf reader fails for array of array of enum types
16042      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16043      array type comes before the outer array type.  We thus call gen_type_die
16044      before we new_die and must prevent nested array types collapsing for this
16045      target.  */
16046
16047 #ifdef MIPS_DEBUGGING_INFO
16048   gen_type_die (TREE_TYPE (type), context_die);
16049   collapse_nested_arrays = false;
16050 #endif
16051
16052   array_die = new_die (DW_TAG_array_type, scope_die, type);
16053   add_name_attribute (array_die, type_tag (type));
16054   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16055   if (TYPE_ARTIFICIAL (type))
16056     add_AT_flag (array_die, DW_AT_artificial, 1);
16057   equate_type_number_to_die (type, array_die);
16058
16059   if (TREE_CODE (type) == VECTOR_TYPE)
16060     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16061
16062   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16063   if (is_fortran ()
16064       && TREE_CODE (type) == ARRAY_TYPE
16065       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16066       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16067     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16068
16069 #if 0
16070   /* We default the array ordering.  SDB will probably do
16071      the right things even if DW_AT_ordering is not present.  It's not even
16072      an issue until we start to get into multidimensional arrays anyway.  If
16073      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16074      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16075      and when we find out that we need to put these in, we will only do so
16076      for multidimensional arrays.  */
16077   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16078 #endif
16079
16080 #ifdef MIPS_DEBUGGING_INFO
16081   /* The SGI compilers handle arrays of unknown bound by setting
16082      AT_declaration and not emitting any subrange DIEs.  */
16083   if (TREE_CODE (type) == ARRAY_TYPE
16084       && ! TYPE_DOMAIN (type))
16085     add_AT_flag (array_die, DW_AT_declaration, 1);
16086   else
16087 #endif
16088   if (TREE_CODE (type) == VECTOR_TYPE)
16089     {
16090       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16091       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16092       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16093       add_bound_info (subrange_die, DW_AT_upper_bound,
16094                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16095     }
16096   else
16097     add_subscript_info (array_die, type, collapse_nested_arrays);
16098
16099   /* Add representation of the type of the elements of this array type and
16100      emit the corresponding DIE if we haven't done it already.  */
16101   element_type = TREE_TYPE (type);
16102   if (collapse_nested_arrays)
16103     while (TREE_CODE (element_type) == ARRAY_TYPE)
16104       {
16105         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16106           break;
16107         element_type = TREE_TYPE (element_type);
16108       }
16109
16110 #ifndef MIPS_DEBUGGING_INFO
16111   gen_type_die (element_type, context_die);
16112 #endif
16113
16114   add_type_attribute (array_die, element_type, 0, 0, context_die);
16115
16116   if (get_AT (array_die, DW_AT_name))
16117     add_pubtype (type, array_die);
16118 }
16119
16120 static dw_loc_descr_ref
16121 descr_info_loc (tree val, tree base_decl)
16122 {
16123   HOST_WIDE_INT size;
16124   dw_loc_descr_ref loc, loc2;
16125   enum dwarf_location_atom op;
16126
16127   if (val == base_decl)
16128     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16129
16130   switch (TREE_CODE (val))
16131     {
16132     CASE_CONVERT:
16133       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16134     case VAR_DECL:
16135       return loc_descriptor_from_tree (val, 0);
16136     case INTEGER_CST:
16137       if (host_integerp (val, 0))
16138         return int_loc_descriptor (tree_low_cst (val, 0));
16139       break;
16140     case INDIRECT_REF:
16141       size = int_size_in_bytes (TREE_TYPE (val));
16142       if (size < 0)
16143         break;
16144       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16145       if (!loc)
16146         break;
16147       if (size == DWARF2_ADDR_SIZE)
16148         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16149       else
16150         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16151       return loc;
16152     case POINTER_PLUS_EXPR:
16153     case PLUS_EXPR:
16154       if (host_integerp (TREE_OPERAND (val, 1), 1)
16155           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16156              < 16384)
16157         {
16158           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16159           if (!loc)
16160             break;
16161           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16162         }
16163       else
16164         {
16165           op = DW_OP_plus;
16166         do_binop:
16167           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16168           if (!loc)
16169             break;
16170           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16171           if (!loc2)
16172             break;
16173           add_loc_descr (&loc, loc2);
16174           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16175         }
16176       return loc;
16177     case MINUS_EXPR:
16178       op = DW_OP_minus;
16179       goto do_binop;
16180     case MULT_EXPR:
16181       op = DW_OP_mul;
16182       goto do_binop;
16183     case EQ_EXPR:
16184       op = DW_OP_eq;
16185       goto do_binop;
16186     case NE_EXPR:
16187       op = DW_OP_ne;
16188       goto do_binop;
16189     default:
16190       break;
16191     }
16192   return NULL;
16193 }
16194
16195 static void
16196 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16197                       tree val, tree base_decl)
16198 {
16199   dw_loc_descr_ref loc;
16200
16201   if (host_integerp (val, 0))
16202     {
16203       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16204       return;
16205     }
16206
16207   loc = descr_info_loc (val, base_decl);
16208   if (!loc)
16209     return;
16210
16211   add_AT_loc (die, attr, loc);
16212 }
16213
16214 /* This routine generates DIE for array with hidden descriptor, details
16215    are filled into *info by a langhook.  */
16216
16217 static void
16218 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16219                           dw_die_ref context_die)
16220 {
16221   dw_die_ref scope_die = scope_die_for (type, context_die);
16222   dw_die_ref array_die;
16223   int dim;
16224
16225   array_die = new_die (DW_TAG_array_type, scope_die, type);
16226   add_name_attribute (array_die, type_tag (type));
16227   equate_type_number_to_die (type, array_die);
16228
16229   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16230   if (is_fortran ()
16231       && info->ndimensions >= 2)
16232     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16233
16234   if (info->data_location)
16235     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16236                           info->base_decl);
16237   if (info->associated)
16238     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16239                           info->base_decl);
16240   if (info->allocated)
16241     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16242                           info->base_decl);
16243
16244   for (dim = 0; dim < info->ndimensions; dim++)
16245     {
16246       dw_die_ref subrange_die
16247         = new_die (DW_TAG_subrange_type, array_die, NULL);
16248
16249       if (info->dimen[dim].lower_bound)
16250         {
16251           /* If it is the default value, omit it.  */
16252           int dflt;
16253
16254           if (host_integerp (info->dimen[dim].lower_bound, 0)
16255               && (dflt = lower_bound_default ()) != -1
16256               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16257             ;
16258           else
16259             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16260                                   info->dimen[dim].lower_bound,
16261                                   info->base_decl);
16262         }
16263       if (info->dimen[dim].upper_bound)
16264         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16265                               info->dimen[dim].upper_bound,
16266                               info->base_decl);
16267       if (info->dimen[dim].stride)
16268         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16269                               info->dimen[dim].stride,
16270                               info->base_decl);
16271     }
16272
16273   gen_type_die (info->element_type, context_die);
16274   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16275
16276   if (get_AT (array_die, DW_AT_name))
16277     add_pubtype (type, array_die);
16278 }
16279
16280 #if 0
16281 static void
16282 gen_entry_point_die (tree decl, dw_die_ref context_die)
16283 {
16284   tree origin = decl_ultimate_origin (decl);
16285   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16286
16287   if (origin != NULL)
16288     add_abstract_origin_attribute (decl_die, origin);
16289   else
16290     {
16291       add_name_and_src_coords_attributes (decl_die, decl);
16292       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16293                           0, 0, context_die);
16294     }
16295
16296   if (DECL_ABSTRACT (decl))
16297     equate_decl_number_to_die (decl, decl_die);
16298   else
16299     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16300 }
16301 #endif
16302
16303 /* Walk through the list of incomplete types again, trying once more to
16304    emit full debugging info for them.  */
16305
16306 static void
16307 retry_incomplete_types (void)
16308 {
16309   int i;
16310
16311   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16312     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16313                                   DINFO_USAGE_DIR_USE))
16314       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16315 }
16316
16317 /* Determine what tag to use for a record type.  */
16318
16319 static enum dwarf_tag
16320 record_type_tag (tree type)
16321 {
16322   if (! lang_hooks.types.classify_record)
16323     return DW_TAG_structure_type;
16324
16325   switch (lang_hooks.types.classify_record (type))
16326     {
16327     case RECORD_IS_STRUCT:
16328       return DW_TAG_structure_type;
16329
16330     case RECORD_IS_CLASS:
16331       return DW_TAG_class_type;
16332
16333     case RECORD_IS_INTERFACE:
16334       if (dwarf_version >= 3 || !dwarf_strict)
16335         return DW_TAG_interface_type;
16336       return DW_TAG_structure_type;
16337
16338     default:
16339       gcc_unreachable ();
16340     }
16341 }
16342
16343 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16344    include all of the information about the enumeration values also. Each
16345    enumerated type name/value is listed as a child of the enumerated type
16346    DIE.  */
16347
16348 static dw_die_ref
16349 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16350 {
16351   dw_die_ref type_die = lookup_type_die (type);
16352
16353   if (type_die == NULL)
16354     {
16355       type_die = new_die (DW_TAG_enumeration_type,
16356                           scope_die_for (type, context_die), type);
16357       equate_type_number_to_die (type, type_die);
16358       add_name_attribute (type_die, type_tag (type));
16359       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16360       if (TYPE_ARTIFICIAL (type))
16361         add_AT_flag (type_die, DW_AT_artificial, 1);
16362       if (dwarf_version >= 4 || !dwarf_strict)
16363         {
16364           if (ENUM_IS_SCOPED (type))
16365             add_AT_flag (type_die, DW_AT_enum_class, 1);
16366           if (ENUM_IS_OPAQUE (type))
16367             add_AT_flag (type_die, DW_AT_declaration, 1);
16368         }
16369     }
16370   else if (! TYPE_SIZE (type))
16371     return type_die;
16372   else
16373     remove_AT (type_die, DW_AT_declaration);
16374
16375   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16376      given enum type is incomplete, do not generate the DW_AT_byte_size
16377      attribute or the DW_AT_element_list attribute.  */
16378   if (TYPE_SIZE (type))
16379     {
16380       tree link;
16381
16382       TREE_ASM_WRITTEN (type) = 1;
16383       add_byte_size_attribute (type_die, type);
16384       if (TYPE_STUB_DECL (type) != NULL_TREE)
16385         {
16386           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16387           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16388         }
16389
16390       /* If the first reference to this type was as the return type of an
16391          inline function, then it may not have a parent.  Fix this now.  */
16392       if (type_die->die_parent == NULL)
16393         add_child_die (scope_die_for (type, context_die), type_die);
16394
16395       for (link = TYPE_VALUES (type);
16396            link != NULL; link = TREE_CHAIN (link))
16397         {
16398           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16399           tree value = TREE_VALUE (link);
16400
16401           add_name_attribute (enum_die,
16402                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16403
16404           if (TREE_CODE (value) == CONST_DECL)
16405             value = DECL_INITIAL (value);
16406
16407           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16408             /* DWARF2 does not provide a way of indicating whether or
16409                not enumeration constants are signed or unsigned.  GDB
16410                always assumes the values are signed, so we output all
16411                values as if they were signed.  That means that
16412                enumeration constants with very large unsigned values
16413                will appear to have negative values in the debugger.  */
16414             add_AT_int (enum_die, DW_AT_const_value,
16415                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16416         }
16417     }
16418   else
16419     add_AT_flag (type_die, DW_AT_declaration, 1);
16420
16421   if (get_AT (type_die, DW_AT_name))
16422     add_pubtype (type, type_die);
16423
16424   return type_die;
16425 }
16426
16427 /* Generate a DIE to represent either a real live formal parameter decl or to
16428    represent just the type of some formal parameter position in some function
16429    type.
16430
16431    Note that this routine is a bit unusual because its argument may be a
16432    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16433    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16434    node.  If it's the former then this function is being called to output a
16435    DIE to represent a formal parameter object (or some inlining thereof).  If
16436    it's the latter, then this function is only being called to output a
16437    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16438    argument type of some subprogram type.
16439    If EMIT_NAME_P is true, name and source coordinate attributes
16440    are emitted.  */
16441
16442 static dw_die_ref
16443 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16444                           dw_die_ref context_die)
16445 {
16446   tree node_or_origin = node ? node : origin;
16447   tree ultimate_origin;
16448   dw_die_ref parm_die
16449     = new_die (DW_TAG_formal_parameter, context_die, node);
16450
16451   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16452     {
16453     case tcc_declaration:
16454       ultimate_origin = decl_ultimate_origin (node_or_origin);
16455       if (node || ultimate_origin)
16456         origin = ultimate_origin;
16457       if (origin != NULL)
16458         add_abstract_origin_attribute (parm_die, origin);
16459       else if (emit_name_p)
16460         add_name_and_src_coords_attributes (parm_die, node);
16461       if (origin == NULL
16462           || (! DECL_ABSTRACT (node_or_origin)
16463               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16464                                            decl_function_context
16465                                                             (node_or_origin))))
16466         {
16467           tree type = TREE_TYPE (node_or_origin);
16468           if (decl_by_reference_p (node_or_origin))
16469             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16470                                 context_die);
16471           else
16472             add_type_attribute (parm_die, type,
16473                                 TREE_READONLY (node_or_origin),
16474                                 TREE_THIS_VOLATILE (node_or_origin),
16475                                 context_die);
16476         }
16477       if (origin == NULL && DECL_ARTIFICIAL (node))
16478         add_AT_flag (parm_die, DW_AT_artificial, 1);
16479
16480       if (node && node != origin)
16481         equate_decl_number_to_die (node, parm_die);
16482       if (! DECL_ABSTRACT (node_or_origin))
16483         add_location_or_const_value_attribute (parm_die, node_or_origin,
16484                                                node == NULL, DW_AT_location);
16485
16486       break;
16487
16488     case tcc_type:
16489       /* We were called with some kind of a ..._TYPE node.  */
16490       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16491       break;
16492
16493     default:
16494       gcc_unreachable ();
16495     }
16496
16497   return parm_die;
16498 }
16499
16500 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16501    children DW_TAG_formal_parameter DIEs representing the arguments of the
16502    parameter pack.
16503
16504    PARM_PACK must be a function parameter pack.
16505    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16506    must point to the subsequent arguments of the function PACK_ARG belongs to.
16507    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16508    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16509    following the last one for which a DIE was generated.  */
16510
16511 static dw_die_ref
16512 gen_formal_parameter_pack_die  (tree parm_pack,
16513                                 tree pack_arg,
16514                                 dw_die_ref subr_die,
16515                                 tree *next_arg)
16516 {
16517   tree arg;
16518   dw_die_ref parm_pack_die;
16519
16520   gcc_assert (parm_pack
16521               && lang_hooks.function_parameter_pack_p (parm_pack)
16522               && subr_die);
16523
16524   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16525   add_src_coords_attributes (parm_pack_die, parm_pack);
16526
16527   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16528     {
16529       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16530                                                                  parm_pack))
16531         break;
16532       gen_formal_parameter_die (arg, NULL,
16533                                 false /* Don't emit name attribute.  */,
16534                                 parm_pack_die);
16535     }
16536   if (next_arg)
16537     *next_arg = arg;
16538   return parm_pack_die;
16539 }
16540
16541 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16542    at the end of an (ANSI prototyped) formal parameters list.  */
16543
16544 static void
16545 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16546 {
16547   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16548 }
16549
16550 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16551    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16552    parameters as specified in some function type specification (except for
16553    those which appear as part of a function *definition*).  */
16554
16555 static void
16556 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16557 {
16558   tree link;
16559   tree formal_type = NULL;
16560   tree first_parm_type;
16561   tree arg;
16562
16563   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16564     {
16565       arg = DECL_ARGUMENTS (function_or_method_type);
16566       function_or_method_type = TREE_TYPE (function_or_method_type);
16567     }
16568   else
16569     arg = NULL_TREE;
16570
16571   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16572
16573   /* Make our first pass over the list of formal parameter types and output a
16574      DW_TAG_formal_parameter DIE for each one.  */
16575   for (link = first_parm_type; link; )
16576     {
16577       dw_die_ref parm_die;
16578
16579       formal_type = TREE_VALUE (link);
16580       if (formal_type == void_type_node)
16581         break;
16582
16583       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16584       parm_die = gen_formal_parameter_die (formal_type, NULL,
16585                                            true /* Emit name attribute.  */,
16586                                            context_die);
16587       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16588           && link == first_parm_type)
16589         {
16590           add_AT_flag (parm_die, DW_AT_artificial, 1);
16591           if (dwarf_version >= 3 || !dwarf_strict)
16592             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16593         }
16594       else if (arg && DECL_ARTIFICIAL (arg))
16595         add_AT_flag (parm_die, DW_AT_artificial, 1);
16596
16597       link = TREE_CHAIN (link);
16598       if (arg)
16599         arg = DECL_CHAIN (arg);
16600     }
16601
16602   /* If this function type has an ellipsis, add a
16603      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16604   if (formal_type != void_type_node)
16605     gen_unspecified_parameters_die (function_or_method_type, context_die);
16606
16607   /* Make our second (and final) pass over the list of formal parameter types
16608      and output DIEs to represent those types (as necessary).  */
16609   for (link = TYPE_ARG_TYPES (function_or_method_type);
16610        link && TREE_VALUE (link);
16611        link = TREE_CHAIN (link))
16612     gen_type_die (TREE_VALUE (link), context_die);
16613 }
16614
16615 /* We want to generate the DIE for TYPE so that we can generate the
16616    die for MEMBER, which has been defined; we will need to refer back
16617    to the member declaration nested within TYPE.  If we're trying to
16618    generate minimal debug info for TYPE, processing TYPE won't do the
16619    trick; we need to attach the member declaration by hand.  */
16620
16621 static void
16622 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16623 {
16624   gen_type_die (type, context_die);
16625
16626   /* If we're trying to avoid duplicate debug info, we may not have
16627      emitted the member decl for this function.  Emit it now.  */
16628   if (TYPE_STUB_DECL (type)
16629       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16630       && ! lookup_decl_die (member))
16631     {
16632       dw_die_ref type_die;
16633       gcc_assert (!decl_ultimate_origin (member));
16634
16635       push_decl_scope (type);
16636       type_die = lookup_type_die_strip_naming_typedef (type);
16637       if (TREE_CODE (member) == FUNCTION_DECL)
16638         gen_subprogram_die (member, type_die);
16639       else if (TREE_CODE (member) == FIELD_DECL)
16640         {
16641           /* Ignore the nameless fields that are used to skip bits but handle
16642              C++ anonymous unions and structs.  */
16643           if (DECL_NAME (member) != NULL_TREE
16644               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16645               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16646             {
16647               gen_type_die (member_declared_type (member), type_die);
16648               gen_field_die (member, type_die);
16649             }
16650         }
16651       else
16652         gen_variable_die (member, NULL_TREE, type_die);
16653
16654       pop_decl_scope ();
16655     }
16656 }
16657
16658 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16659    may later generate inlined and/or out-of-line instances of.  */
16660
16661 static void
16662 dwarf2out_abstract_function (tree decl)
16663 {
16664   dw_die_ref old_die;
16665   tree save_fn;
16666   tree context;
16667   int was_abstract;
16668   htab_t old_decl_loc_table;
16669   htab_t old_cached_dw_loc_list_table;
16670   int old_call_site_count, old_tail_call_site_count;
16671   struct call_arg_loc_node *old_call_arg_locations;
16672
16673   /* Make sure we have the actual abstract inline, not a clone.  */
16674   decl = DECL_ORIGIN (decl);
16675
16676   old_die = lookup_decl_die (decl);
16677   if (old_die && get_AT (old_die, DW_AT_inline))
16678     /* We've already generated the abstract instance.  */
16679     return;
16680
16681   /* We can be called while recursively when seeing block defining inlined subroutine
16682      DIE.  Be sure to not clobber the outer location table nor use it or we would
16683      get locations in abstract instantces.  */
16684   old_decl_loc_table = decl_loc_table;
16685   decl_loc_table = NULL;
16686   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16687   cached_dw_loc_list_table = NULL;
16688   old_call_arg_locations = call_arg_locations;
16689   call_arg_locations = NULL;
16690   old_call_site_count = call_site_count;
16691   call_site_count = -1;
16692   old_tail_call_site_count = tail_call_site_count;
16693   tail_call_site_count = -1;
16694
16695   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16696      we don't get confused by DECL_ABSTRACT.  */
16697   if (debug_info_level > DINFO_LEVEL_TERSE)
16698     {
16699       context = decl_class_context (decl);
16700       if (context)
16701         gen_type_die_for_member
16702           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16703     }
16704
16705   /* Pretend we've just finished compiling this function.  */
16706   save_fn = current_function_decl;
16707   current_function_decl = decl;
16708   push_cfun (DECL_STRUCT_FUNCTION (decl));
16709
16710   was_abstract = DECL_ABSTRACT (decl);
16711   set_decl_abstract_flags (decl, 1);
16712   dwarf2out_decl (decl);
16713   if (! was_abstract)
16714     set_decl_abstract_flags (decl, 0);
16715
16716   current_function_decl = save_fn;
16717   decl_loc_table = old_decl_loc_table;
16718   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16719   call_arg_locations = old_call_arg_locations;
16720   call_site_count = old_call_site_count;
16721   tail_call_site_count = old_tail_call_site_count;
16722   pop_cfun ();
16723 }
16724
16725 /* Helper function of premark_used_types() which gets called through
16726    htab_traverse.
16727
16728    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16729    marked as unused by prune_unused_types.  */
16730
16731 static int
16732 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16733 {
16734   tree type;
16735   dw_die_ref die;
16736
16737   type = (tree) *slot;
16738   die = lookup_type_die (type);
16739   if (die != NULL)
16740     die->die_perennial_p = 1;
16741   return 1;
16742 }
16743
16744 /* Helper function of premark_types_used_by_global_vars which gets called
16745    through htab_traverse.
16746
16747    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16748    marked as unused by prune_unused_types. The DIE of the type is marked
16749    only if the global variable using the type will actually be emitted.  */
16750
16751 static int
16752 premark_types_used_by_global_vars_helper (void **slot,
16753                                           void *data ATTRIBUTE_UNUSED)
16754 {
16755   struct types_used_by_vars_entry *entry;
16756   dw_die_ref die;
16757
16758   entry = (struct types_used_by_vars_entry *) *slot;
16759   gcc_assert (entry->type != NULL
16760               && entry->var_decl != NULL);
16761   die = lookup_type_die (entry->type);
16762   if (die)
16763     {
16764       /* Ask cgraph if the global variable really is to be emitted.
16765          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16766       struct varpool_node *node = varpool_get_node (entry->var_decl);
16767       if (node && node->needed)
16768         {
16769           die->die_perennial_p = 1;
16770           /* Keep the parent DIEs as well.  */
16771           while ((die = die->die_parent) && die->die_perennial_p == 0)
16772             die->die_perennial_p = 1;
16773         }
16774     }
16775   return 1;
16776 }
16777
16778 /* Mark all members of used_types_hash as perennial.  */
16779
16780 static void
16781 premark_used_types (void)
16782 {
16783   if (cfun && cfun->used_types_hash)
16784     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16785 }
16786
16787 /* Mark all members of types_used_by_vars_entry as perennial.  */
16788
16789 static void
16790 premark_types_used_by_global_vars (void)
16791 {
16792   if (types_used_by_vars_hash)
16793     htab_traverse (types_used_by_vars_hash,
16794                    premark_types_used_by_global_vars_helper, NULL);
16795 }
16796
16797 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16798    for CA_LOC call arg loc node.  */
16799
16800 static dw_die_ref
16801 gen_call_site_die (tree decl, dw_die_ref subr_die,
16802                    struct call_arg_loc_node *ca_loc)
16803 {
16804   dw_die_ref stmt_die = NULL, die;
16805   tree block = ca_loc->block;
16806
16807   while (block
16808          && block != DECL_INITIAL (decl)
16809          && TREE_CODE (block) == BLOCK)
16810     {
16811       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16812         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16813       if (stmt_die)
16814         break;
16815       block = BLOCK_SUPERCONTEXT (block);
16816     }
16817   if (stmt_die == NULL)
16818     stmt_die = subr_die;
16819   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16820   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16821   if (ca_loc->tail_call_p)
16822     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16823   if (ca_loc->symbol_ref)
16824     {
16825       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16826       if (tdie)
16827         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16828       else
16829         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16830     }
16831   return die;
16832 }
16833
16834 /* Generate a DIE to represent a declared function (either file-scope or
16835    block-local).  */
16836
16837 static void
16838 gen_subprogram_die (tree decl, dw_die_ref context_die)
16839 {
16840   tree origin = decl_ultimate_origin (decl);
16841   dw_die_ref subr_die;
16842   tree outer_scope;
16843   dw_die_ref old_die = lookup_decl_die (decl);
16844   int declaration = (current_function_decl != decl
16845                      || class_or_namespace_scope_p (context_die));
16846
16847   premark_used_types ();
16848
16849   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16850      started to generate the abstract instance of an inline, decided to output
16851      its containing class, and proceeded to emit the declaration of the inline
16852      from the member list for the class.  If so, DECLARATION takes priority;
16853      we'll get back to the abstract instance when done with the class.  */
16854
16855   /* The class-scope declaration DIE must be the primary DIE.  */
16856   if (origin && declaration && class_or_namespace_scope_p (context_die))
16857     {
16858       origin = NULL;
16859       gcc_assert (!old_die);
16860     }
16861
16862   /* Now that the C++ front end lazily declares artificial member fns, we
16863      might need to retrofit the declaration into its class.  */
16864   if (!declaration && !origin && !old_die
16865       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16866       && !class_or_namespace_scope_p (context_die)
16867       && debug_info_level > DINFO_LEVEL_TERSE)
16868     old_die = force_decl_die (decl);
16869
16870   if (origin != NULL)
16871     {
16872       gcc_assert (!declaration || local_scope_p (context_die));
16873
16874       /* Fixup die_parent for the abstract instance of a nested
16875          inline function.  */
16876       if (old_die && old_die->die_parent == NULL)
16877         add_child_die (context_die, old_die);
16878
16879       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16880       add_abstract_origin_attribute (subr_die, origin);
16881       /*  This is where the actual code for a cloned function is.
16882           Let's emit linkage name attribute for it.  This helps
16883           debuggers to e.g, set breakpoints into
16884           constructors/destructors when the user asks "break
16885           K::K".  */
16886       add_linkage_name (subr_die, decl);
16887     }
16888   else if (old_die)
16889     {
16890       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16891       struct dwarf_file_data * file_index = lookup_filename (s.file);
16892
16893       if (!get_AT_flag (old_die, DW_AT_declaration)
16894           /* We can have a normal definition following an inline one in the
16895              case of redefinition of GNU C extern inlines.
16896              It seems reasonable to use AT_specification in this case.  */
16897           && !get_AT (old_die, DW_AT_inline))
16898         {
16899           /* Detect and ignore this case, where we are trying to output
16900              something we have already output.  */
16901           return;
16902         }
16903
16904       /* If the definition comes from the same place as the declaration,
16905          maybe use the old DIE.  We always want the DIE for this function
16906          that has the *_pc attributes to be under comp_unit_die so the
16907          debugger can find it.  We also need to do this for abstract
16908          instances of inlines, since the spec requires the out-of-line copy
16909          to have the same parent.  For local class methods, this doesn't
16910          apply; we just use the old DIE.  */
16911       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16912           && (DECL_ARTIFICIAL (decl)
16913               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16914                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
16915                       == (unsigned) s.line))))
16916         {
16917           subr_die = old_die;
16918
16919           /* Clear out the declaration attribute and the formal parameters.
16920              Do not remove all children, because it is possible that this
16921              declaration die was forced using force_decl_die(). In such
16922              cases die that forced declaration die (e.g. TAG_imported_module)
16923              is one of the children that we do not want to remove.  */
16924           remove_AT (subr_die, DW_AT_declaration);
16925           remove_AT (subr_die, DW_AT_object_pointer);
16926           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16927         }
16928       else
16929         {
16930           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16931           add_AT_specification (subr_die, old_die);
16932           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16933             add_AT_file (subr_die, DW_AT_decl_file, file_index);
16934           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16935             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
16936         }
16937     }
16938   else
16939     {
16940       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16941
16942       if (TREE_PUBLIC (decl))
16943         add_AT_flag (subr_die, DW_AT_external, 1);
16944
16945       add_name_and_src_coords_attributes (subr_die, decl);
16946       if (debug_info_level > DINFO_LEVEL_TERSE)
16947         {
16948           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
16949           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
16950                               0, 0, context_die);
16951         }
16952
16953       add_pure_or_virtual_attribute (subr_die, decl);
16954       if (DECL_ARTIFICIAL (decl))
16955         add_AT_flag (subr_die, DW_AT_artificial, 1);
16956
16957       add_accessibility_attribute (subr_die, decl);
16958     }
16959
16960   if (declaration)
16961     {
16962       if (!old_die || !get_AT (old_die, DW_AT_inline))
16963         {
16964           add_AT_flag (subr_die, DW_AT_declaration, 1);
16965
16966           /* If this is an explicit function declaration then generate
16967              a DW_AT_explicit attribute.  */
16968           if (lang_hooks.decls.function_decl_explicit_p (decl)
16969               && (dwarf_version >= 3 || !dwarf_strict))
16970             add_AT_flag (subr_die, DW_AT_explicit, 1);
16971
16972           /* The first time we see a member function, it is in the context of
16973              the class to which it belongs.  We make sure of this by emitting
16974              the class first.  The next time is the definition, which is
16975              handled above.  The two may come from the same source text.
16976
16977              Note that force_decl_die() forces function declaration die. It is
16978              later reused to represent definition.  */
16979           equate_decl_number_to_die (decl, subr_die);
16980         }
16981     }
16982   else if (DECL_ABSTRACT (decl))
16983     {
16984       if (DECL_DECLARED_INLINE_P (decl))
16985         {
16986           if (cgraph_function_possibly_inlined_p (decl))
16987             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
16988           else
16989             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
16990         }
16991       else
16992         {
16993           if (cgraph_function_possibly_inlined_p (decl))
16994             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
16995           else
16996             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
16997         }
16998
16999       if (DECL_DECLARED_INLINE_P (decl)
17000           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17001         add_AT_flag (subr_die, DW_AT_artificial, 1);
17002
17003       equate_decl_number_to_die (decl, subr_die);
17004     }
17005   else if (!DECL_EXTERNAL (decl))
17006     {
17007       HOST_WIDE_INT cfa_fb_offset;
17008
17009       if (!old_die || !get_AT (old_die, DW_AT_inline))
17010         equate_decl_number_to_die (decl, subr_die);
17011
17012       if (!flag_reorder_blocks_and_partition)
17013         {
17014           dw_fde_ref fde = cfun->fde;
17015           if (fde->dw_fde_begin)
17016             {
17017               /* We have already generated the labels.  */
17018               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17019               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17020             }
17021           else
17022             {
17023               /* Create start/end labels and add the range.  */
17024               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17025               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17026                                            current_function_funcdef_no);
17027               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17028               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17029                                            current_function_funcdef_no);
17030               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17031             }
17032
17033 #if VMS_DEBUGGING_INFO
17034       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17035          Section 2.3 Prologue and Epilogue Attributes:
17036          When a breakpoint is set on entry to a function, it is generally
17037          desirable for execution to be suspended, not on the very first
17038          instruction of the function, but rather at a point after the
17039          function's frame has been set up, after any language defined local
17040          declaration processing has been completed, and before execution of
17041          the first statement of the function begins. Debuggers generally
17042          cannot properly determine where this point is.  Similarly for a
17043          breakpoint set on exit from a function. The prologue and epilogue
17044          attributes allow a compiler to communicate the location(s) to use.  */
17045
17046       {
17047         if (fde->dw_fde_vms_end_prologue)
17048           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17049             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17050
17051         if (fde->dw_fde_vms_begin_epilogue)
17052           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17053             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17054       }
17055 #endif
17056
17057           add_pubname (decl, subr_die);
17058         }
17059       else
17060         {
17061           /* Generate pubnames entries for the split function code ranges.  */
17062           dw_fde_ref fde = cfun->fde;
17063
17064           if (fde->dw_fde_second_begin)
17065             {
17066               if (dwarf_version >= 3 || !dwarf_strict)
17067                 {
17068                   /* We should use ranges for non-contiguous code section 
17069                      addresses.  Use the actual code range for the initial
17070                      section, since the HOT/COLD labels might precede an 
17071                      alignment offset.  */
17072                   bool range_list_added = false;
17073                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17074                                         fde->dw_fde_end, &range_list_added);
17075                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17076                                         fde->dw_fde_second_end,
17077                                         &range_list_added);
17078                   add_pubname (decl, subr_die);
17079                   if (range_list_added)
17080                     add_ranges (NULL);
17081                 }
17082               else
17083                 {
17084                   /* There is no real support in DW2 for this .. so we make
17085                      a work-around.  First, emit the pub name for the segment
17086                      containing the function label.  Then make and emit a
17087                      simplified subprogram DIE for the second segment with the
17088                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17089                      linkage name for the second die so that gdb will find both
17090                      sections when given "b foo".  */
17091                   const char *name = NULL;
17092                   tree decl_name = DECL_NAME (decl);
17093                   dw_die_ref seg_die;
17094
17095                   /* Do the 'primary' section.   */
17096                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17097                                  fde->dw_fde_begin);
17098                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17099                                  fde->dw_fde_end);
17100                   /* Add it.   */
17101                   add_pubname (decl, subr_die);
17102
17103                   /* Build a minimal DIE for the secondary section.  */
17104                   seg_die = new_die (DW_TAG_subprogram,
17105                                      subr_die->die_parent, decl);
17106
17107                   if (TREE_PUBLIC (decl))
17108                     add_AT_flag (seg_die, DW_AT_external, 1);
17109
17110                   if (decl_name != NULL 
17111                       && IDENTIFIER_POINTER (decl_name) != NULL)
17112                     {
17113                       name = dwarf2_name (decl, 1);
17114                       if (! DECL_ARTIFICIAL (decl))
17115                         add_src_coords_attributes (seg_die, decl);
17116
17117                       add_linkage_name (seg_die, decl);
17118                     }
17119                   gcc_assert (name != NULL);
17120                   add_pure_or_virtual_attribute (seg_die, decl);
17121                   if (DECL_ARTIFICIAL (decl))
17122                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17123
17124                   name = concat ("__second_sect_of_", name, NULL); 
17125                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17126                                  fde->dw_fde_second_begin);
17127                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17128                                  fde->dw_fde_second_end);
17129                   add_name_attribute (seg_die, name);
17130                   add_pubname_string (name, seg_die);
17131                 }
17132             }
17133           else
17134             {
17135               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17136               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17137               add_pubname (decl, subr_die);
17138             }
17139         }
17140
17141 #ifdef MIPS_DEBUGGING_INFO
17142       /* Add a reference to the FDE for this routine.  */
17143       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17144 #endif
17145
17146       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17147
17148       /* We define the "frame base" as the function's CFA.  This is more
17149          convenient for several reasons: (1) It's stable across the prologue
17150          and epilogue, which makes it better than just a frame pointer,
17151          (2) With dwarf3, there exists a one-byte encoding that allows us
17152          to reference the .debug_frame data by proxy, but failing that,
17153          (3) We can at least reuse the code inspection and interpretation
17154          code that determines the CFA position at various points in the
17155          function.  */
17156       if (dwarf_version >= 3)
17157         {
17158           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17159           add_AT_loc (subr_die, DW_AT_frame_base, op);
17160         }
17161       else
17162         {
17163           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17164           if (list->dw_loc_next)
17165             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17166           else
17167             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17168         }
17169
17170       /* Compute a displacement from the "steady-state frame pointer" to
17171          the CFA.  The former is what all stack slots and argument slots
17172          will reference in the rtl; the later is what we've told the
17173          debugger about.  We'll need to adjust all frame_base references
17174          by this displacement.  */
17175       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17176
17177       if (cfun->static_chain_decl)
17178         add_AT_location_description (subr_die, DW_AT_static_link,
17179                  loc_list_from_tree (cfun->static_chain_decl, 2));
17180     }
17181
17182   /* Generate child dies for template paramaters.  */
17183   if (debug_info_level > DINFO_LEVEL_TERSE)
17184     gen_generic_params_dies (decl);
17185
17186   /* Now output descriptions of the arguments for this function. This gets
17187      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17188      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17189      `...' at the end of the formal parameter list.  In order to find out if
17190      there was a trailing ellipsis or not, we must instead look at the type
17191      associated with the FUNCTION_DECL.  This will be a node of type
17192      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17193      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17194      an ellipsis at the end.  */
17195
17196   /* In the case where we are describing a mere function declaration, all we
17197      need to do here (and all we *can* do here) is to describe the *types* of
17198      its formal parameters.  */
17199   if (debug_info_level <= DINFO_LEVEL_TERSE)
17200     ;
17201   else if (declaration)
17202     gen_formal_types_die (decl, subr_die);
17203   else
17204     {
17205       /* Generate DIEs to represent all known formal parameters.  */
17206       tree parm = DECL_ARGUMENTS (decl);
17207       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17208       tree generic_decl_parm = generic_decl
17209                                 ? DECL_ARGUMENTS (generic_decl)
17210                                 : NULL;
17211
17212       /* Now we want to walk the list of parameters of the function and
17213          emit their relevant DIEs.
17214
17215          We consider the case of DECL being an instance of a generic function
17216          as well as it being a normal function.
17217
17218          If DECL is an instance of a generic function we walk the
17219          parameters of the generic function declaration _and_ the parameters of
17220          DECL itself. This is useful because we want to emit specific DIEs for
17221          function parameter packs and those are declared as part of the
17222          generic function declaration. In that particular case,
17223          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17224          That DIE has children DIEs representing the set of arguments
17225          of the pack. Note that the set of pack arguments can be empty.
17226          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17227          children DIE.
17228
17229          Otherwise, we just consider the parameters of DECL.  */
17230       while (generic_decl_parm || parm)
17231         {
17232           if (generic_decl_parm
17233               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17234             gen_formal_parameter_pack_die (generic_decl_parm,
17235                                            parm, subr_die,
17236                                            &parm);
17237           else if (parm)
17238             {
17239               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17240
17241               if (parm == DECL_ARGUMENTS (decl)
17242                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17243                   && parm_die
17244                   && (dwarf_version >= 3 || !dwarf_strict))
17245                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17246
17247               parm = DECL_CHAIN (parm);
17248             }
17249
17250           if (generic_decl_parm)
17251             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17252         }
17253
17254       /* Decide whether we need an unspecified_parameters DIE at the end.
17255          There are 2 more cases to do this for: 1) the ansi ... declaration -
17256          this is detectable when the end of the arg list is not a
17257          void_type_node 2) an unprototyped function declaration (not a
17258          definition).  This just means that we have no info about the
17259          parameters at all.  */
17260       if (prototype_p (TREE_TYPE (decl)))
17261         {
17262           /* This is the prototyped case, check for....  */
17263           if (stdarg_p (TREE_TYPE (decl)))
17264             gen_unspecified_parameters_die (decl, subr_die);
17265         }
17266       else if (DECL_INITIAL (decl) == NULL_TREE)
17267         gen_unspecified_parameters_die (decl, subr_die);
17268     }
17269
17270   /* Output Dwarf info for all of the stuff within the body of the function
17271      (if it has one - it may be just a declaration).  */
17272   outer_scope = DECL_INITIAL (decl);
17273
17274   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17275      a function.  This BLOCK actually represents the outermost binding contour
17276      for the function, i.e. the contour in which the function's formal
17277      parameters and labels get declared. Curiously, it appears that the front
17278      end doesn't actually put the PARM_DECL nodes for the current function onto
17279      the BLOCK_VARS list for this outer scope, but are strung off of the
17280      DECL_ARGUMENTS list for the function instead.
17281
17282      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17283      the LABEL_DECL nodes for the function however, and we output DWARF info
17284      for those in decls_for_scope.  Just within the `outer_scope' there will be
17285      a BLOCK node representing the function's outermost pair of curly braces,
17286      and any blocks used for the base and member initializers of a C++
17287      constructor function.  */
17288   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17289     {
17290       int call_site_note_count = 0;
17291       int tail_call_site_note_count = 0;
17292
17293       /* Emit a DW_TAG_variable DIE for a named return value.  */
17294       if (DECL_NAME (DECL_RESULT (decl)))
17295         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17296
17297       current_function_has_inlines = 0;
17298       decls_for_scope (outer_scope, subr_die, 0);
17299
17300       if (call_arg_locations && !dwarf_strict)
17301         {
17302           struct call_arg_loc_node *ca_loc;
17303           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17304             {
17305               dw_die_ref die = NULL;
17306               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17307               rtx arg, next_arg;
17308
17309               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17310                    arg; arg = next_arg)
17311                 {
17312                   dw_loc_descr_ref reg, val;
17313                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17314                   dw_die_ref cdie, tdie = NULL;
17315
17316                   next_arg = XEXP (arg, 1);
17317                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17318                       && next_arg
17319                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17320                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17321                       && REGNO (XEXP (XEXP (arg, 0), 0))
17322                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17323                     next_arg = XEXP (next_arg, 1);
17324                   if (mode == VOIDmode)
17325                     {
17326                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17327                       if (mode == VOIDmode)
17328                         mode = GET_MODE (XEXP (arg, 0));
17329                     }
17330                   if (mode == VOIDmode || mode == BLKmode)
17331                     continue;
17332                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17333                     {
17334                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17335                       tloc = XEXP (XEXP (arg, 0), 1);
17336                       continue;
17337                     }
17338                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17339                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17340                     {
17341                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17342                       tlocc = XEXP (XEXP (arg, 0), 1);
17343                       continue;
17344                     }
17345                   reg = NULL;
17346                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17347                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17348                                               VAR_INIT_STATUS_INITIALIZED);
17349                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17350                     {
17351                       rtx mem = XEXP (XEXP (arg, 0), 0);
17352                       reg = mem_loc_descriptor (XEXP (mem, 0),
17353                                                 get_address_mode (mem),
17354                                                 GET_MODE (mem),
17355                                                 VAR_INIT_STATUS_INITIALIZED);
17356                     }
17357                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17358                            == DEBUG_PARAMETER_REF)
17359                     {
17360                       tree tdecl
17361                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17362                       tdie = lookup_decl_die (tdecl);
17363                       if (tdie == NULL)
17364                         continue;
17365                     }
17366                   else
17367                     continue;
17368                   if (reg == NULL
17369                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17370                          != DEBUG_PARAMETER_REF)
17371                     continue;
17372                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17373                                             VOIDmode,
17374                                             VAR_INIT_STATUS_INITIALIZED);
17375                   if (val == NULL)
17376                     continue;
17377                   if (die == NULL)
17378                     die = gen_call_site_die (decl, subr_die, ca_loc);
17379                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17380                                   NULL_TREE);
17381                   if (reg != NULL)
17382                     add_AT_loc (cdie, DW_AT_location, reg);
17383                   else if (tdie != NULL)
17384                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17385                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17386                   if (next_arg != XEXP (arg, 1))
17387                     {
17388                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17389                       if (mode == VOIDmode)
17390                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17391                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17392                                                             0), 1),
17393                                                 mode, VOIDmode,
17394                                                 VAR_INIT_STATUS_INITIALIZED);
17395                       if (val != NULL)
17396                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17397                     }
17398                 }
17399               if (die == NULL
17400                   && (ca_loc->symbol_ref || tloc))
17401                 die = gen_call_site_die (decl, subr_die, ca_loc);
17402               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17403                 {
17404                   dw_loc_descr_ref tval = NULL;
17405
17406                   if (tloc != NULL_RTX)
17407                     tval = mem_loc_descriptor (tloc,
17408                                                GET_MODE (tloc) == VOIDmode
17409                                                ? Pmode : GET_MODE (tloc),
17410                                                VOIDmode,
17411                                                VAR_INIT_STATUS_INITIALIZED);
17412                   if (tval)
17413                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17414                   else if (tlocc != NULL_RTX)
17415                     {
17416                       tval = mem_loc_descriptor (tlocc,
17417                                                  GET_MODE (tlocc) == VOIDmode
17418                                                  ? Pmode : GET_MODE (tlocc),
17419                                                  VOIDmode,
17420                                                  VAR_INIT_STATUS_INITIALIZED);
17421                       if (tval)
17422                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17423                                     tval);
17424                     }
17425                 }
17426               if (die != NULL)
17427                 {
17428                   call_site_note_count++;
17429                   if (ca_loc->tail_call_p)
17430                     tail_call_site_note_count++;
17431                 }
17432             }
17433         }
17434       call_arg_locations = NULL;
17435       call_arg_loc_last = NULL;
17436       if (tail_call_site_count >= 0
17437           && tail_call_site_count == tail_call_site_note_count
17438           && !dwarf_strict)
17439         {
17440           if (call_site_count >= 0
17441               && call_site_count == call_site_note_count)
17442             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17443           else
17444             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17445         }
17446       call_site_count = -1;
17447       tail_call_site_count = -1;
17448     }
17449   /* Add the calling convention attribute if requested.  */
17450   add_calling_convention_attribute (subr_die, decl);
17451
17452 }
17453
17454 /* Returns a hash value for X (which really is a die_struct).  */
17455
17456 static hashval_t
17457 common_block_die_table_hash (const void *x)
17458 {
17459   const_dw_die_ref d = (const_dw_die_ref) x;
17460   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17461 }
17462
17463 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17464    as decl_id and die_parent of die_struct Y.  */
17465
17466 static int
17467 common_block_die_table_eq (const void *x, const void *y)
17468 {
17469   const_dw_die_ref d = (const_dw_die_ref) x;
17470   const_dw_die_ref e = (const_dw_die_ref) y;
17471   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17472 }
17473
17474 /* Generate a DIE to represent a declared data object.
17475    Either DECL or ORIGIN must be non-null.  */
17476
17477 static void
17478 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17479 {
17480   HOST_WIDE_INT off;
17481   tree com_decl;
17482   tree decl_or_origin = decl ? decl : origin;
17483   tree ultimate_origin;
17484   dw_die_ref var_die;
17485   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17486   dw_die_ref origin_die;
17487   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17488                       || class_or_namespace_scope_p (context_die));
17489   bool specialization_p = false;
17490
17491   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17492   if (decl || ultimate_origin)
17493     origin = ultimate_origin;
17494   com_decl = fortran_common (decl_or_origin, &off);
17495
17496   /* Symbol in common gets emitted as a child of the common block, in the form
17497      of a data member.  */
17498   if (com_decl)
17499     {
17500       dw_die_ref com_die;
17501       dw_loc_list_ref loc;
17502       die_node com_die_arg;
17503
17504       var_die = lookup_decl_die (decl_or_origin);
17505       if (var_die)
17506         {
17507           if (get_AT (var_die, DW_AT_location) == NULL)
17508             {
17509               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17510               if (loc)
17511                 {
17512                   if (off)
17513                     {
17514                       /* Optimize the common case.  */
17515                       if (single_element_loc_list_p (loc)
17516                           && loc->expr->dw_loc_opc == DW_OP_addr
17517                           && loc->expr->dw_loc_next == NULL
17518                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17519                              == SYMBOL_REF)
17520                         loc->expr->dw_loc_oprnd1.v.val_addr
17521                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17522                         else
17523                           loc_list_plus_const (loc, off);
17524                     }
17525                   add_AT_location_description (var_die, DW_AT_location, loc);
17526                   remove_AT (var_die, DW_AT_declaration);
17527                 }
17528             }
17529           return;
17530         }
17531
17532       if (common_block_die_table == NULL)
17533         common_block_die_table
17534           = htab_create_ggc (10, common_block_die_table_hash,
17535                              common_block_die_table_eq, NULL);
17536
17537       com_die_arg.decl_id = DECL_UID (com_decl);
17538       com_die_arg.die_parent = context_die;
17539       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17540       loc = loc_list_from_tree (com_decl, 2);
17541       if (com_die == NULL)
17542         {
17543           const char *cnam
17544             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17545           void **slot;
17546
17547           com_die = new_die (DW_TAG_common_block, context_die, decl);
17548           add_name_and_src_coords_attributes (com_die, com_decl);
17549           if (loc)
17550             {
17551               add_AT_location_description (com_die, DW_AT_location, loc);
17552               /* Avoid sharing the same loc descriptor between
17553                  DW_TAG_common_block and DW_TAG_variable.  */
17554               loc = loc_list_from_tree (com_decl, 2);
17555             }
17556           else if (DECL_EXTERNAL (decl))
17557             add_AT_flag (com_die, DW_AT_declaration, 1);
17558           add_pubname_string (cnam, com_die); /* ??? needed? */
17559           com_die->decl_id = DECL_UID (com_decl);
17560           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17561           *slot = (void *) com_die;
17562         }
17563       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17564         {
17565           add_AT_location_description (com_die, DW_AT_location, loc);
17566           loc = loc_list_from_tree (com_decl, 2);
17567           remove_AT (com_die, DW_AT_declaration);
17568         }
17569       var_die = new_die (DW_TAG_variable, com_die, decl);
17570       add_name_and_src_coords_attributes (var_die, decl);
17571       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17572                           TREE_THIS_VOLATILE (decl), context_die);
17573       add_AT_flag (var_die, DW_AT_external, 1);
17574       if (loc)
17575         {
17576           if (off)
17577             {
17578               /* Optimize the common case.  */
17579               if (single_element_loc_list_p (loc)
17580                   && loc->expr->dw_loc_opc == DW_OP_addr
17581                   && loc->expr->dw_loc_next == NULL
17582                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17583                 loc->expr->dw_loc_oprnd1.v.val_addr
17584                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17585               else
17586                 loc_list_plus_const (loc, off);
17587             }
17588           add_AT_location_description (var_die, DW_AT_location, loc);
17589         }
17590       else if (DECL_EXTERNAL (decl))
17591         add_AT_flag (var_die, DW_AT_declaration, 1);
17592       equate_decl_number_to_die (decl, var_die);
17593       return;
17594     }
17595
17596   /* If the compiler emitted a definition for the DECL declaration
17597      and if we already emitted a DIE for it, don't emit a second
17598      DIE for it again. Allow re-declarations of DECLs that are
17599      inside functions, though.  */
17600   if (old_die && declaration && !local_scope_p (context_die))
17601     return;
17602
17603   /* For static data members, the declaration in the class is supposed
17604      to have DW_TAG_member tag; the specification should still be
17605      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17606   if (declaration && class_scope_p (context_die))
17607     var_die = new_die (DW_TAG_member, context_die, decl);
17608   else
17609     var_die = new_die (DW_TAG_variable, context_die, decl);
17610
17611   origin_die = NULL;
17612   if (origin != NULL)
17613     origin_die = add_abstract_origin_attribute (var_die, origin);
17614
17615   /* Loop unrolling can create multiple blocks that refer to the same
17616      static variable, so we must test for the DW_AT_declaration flag.
17617
17618      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17619      copy decls and set the DECL_ABSTRACT flag on them instead of
17620      sharing them.
17621
17622      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17623
17624      ??? The declare_in_namespace support causes us to get two DIEs for one
17625      variable, both of which are declarations.  We want to avoid considering
17626      one to be a specification, so we must test that this DIE is not a
17627      declaration.  */
17628   else if (old_die && TREE_STATIC (decl) && ! declaration
17629            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17630     {
17631       /* This is a definition of a C++ class level static.  */
17632       add_AT_specification (var_die, old_die);
17633       specialization_p = true;
17634       if (DECL_NAME (decl))
17635         {
17636           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17637           struct dwarf_file_data * file_index = lookup_filename (s.file);
17638
17639           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17640             add_AT_file (var_die, DW_AT_decl_file, file_index);
17641
17642           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17643             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17644
17645           if (old_die->die_tag == DW_TAG_member)
17646             add_linkage_name (var_die, decl);
17647         }
17648     }
17649   else
17650     add_name_and_src_coords_attributes (var_die, decl);
17651
17652   if ((origin == NULL && !specialization_p)
17653       || (origin != NULL
17654           && !DECL_ABSTRACT (decl_or_origin)
17655           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17656                                        decl_function_context
17657                                                         (decl_or_origin))))
17658     {
17659       tree type = TREE_TYPE (decl_or_origin);
17660
17661       if (decl_by_reference_p (decl_or_origin))
17662         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17663       else
17664         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17665                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17666     }
17667
17668   if (origin == NULL && !specialization_p)
17669     {
17670       if (TREE_PUBLIC (decl))
17671         add_AT_flag (var_die, DW_AT_external, 1);
17672
17673       if (DECL_ARTIFICIAL (decl))
17674         add_AT_flag (var_die, DW_AT_artificial, 1);
17675
17676       add_accessibility_attribute (var_die, decl);
17677     }
17678
17679   if (declaration)
17680     add_AT_flag (var_die, DW_AT_declaration, 1);
17681
17682   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17683     equate_decl_number_to_die (decl, var_die);
17684
17685   if (! declaration
17686       && (! DECL_ABSTRACT (decl_or_origin)
17687           /* Local static vars are shared between all clones/inlines,
17688              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17689              already set.  */
17690           || (TREE_CODE (decl_or_origin) == VAR_DECL
17691               && TREE_STATIC (decl_or_origin)
17692               && DECL_RTL_SET_P (decl_or_origin)))
17693       /* When abstract origin already has DW_AT_location attribute, no need
17694          to add it again.  */
17695       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17696     {
17697       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17698           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17699         defer_location (decl_or_origin, var_die);
17700       else
17701         add_location_or_const_value_attribute (var_die, decl_or_origin,
17702                                                decl == NULL, DW_AT_location);
17703       add_pubname (decl_or_origin, var_die);
17704     }
17705   else
17706     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17707 }
17708
17709 /* Generate a DIE to represent a named constant.  */
17710
17711 static void
17712 gen_const_die (tree decl, dw_die_ref context_die)
17713 {
17714   dw_die_ref const_die;
17715   tree type = TREE_TYPE (decl);
17716
17717   const_die = new_die (DW_TAG_constant, context_die, decl);
17718   add_name_and_src_coords_attributes (const_die, decl);
17719   add_type_attribute (const_die, type, 1, 0, context_die);
17720   if (TREE_PUBLIC (decl))
17721     add_AT_flag (const_die, DW_AT_external, 1);
17722   if (DECL_ARTIFICIAL (decl))
17723     add_AT_flag (const_die, DW_AT_artificial, 1);
17724   tree_add_const_value_attribute_for_decl (const_die, decl);
17725 }
17726
17727 /* Generate a DIE to represent a label identifier.  */
17728
17729 static void
17730 gen_label_die (tree decl, dw_die_ref context_die)
17731 {
17732   tree origin = decl_ultimate_origin (decl);
17733   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17734   rtx insn;
17735   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17736
17737   if (origin != NULL)
17738     add_abstract_origin_attribute (lbl_die, origin);
17739   else
17740     add_name_and_src_coords_attributes (lbl_die, decl);
17741
17742   if (DECL_ABSTRACT (decl))
17743     equate_decl_number_to_die (decl, lbl_die);
17744   else
17745     {
17746       insn = DECL_RTL_IF_SET (decl);
17747
17748       /* Deleted labels are programmer specified labels which have been
17749          eliminated because of various optimizations.  We still emit them
17750          here so that it is possible to put breakpoints on them.  */
17751       if (insn
17752           && (LABEL_P (insn)
17753               || ((NOTE_P (insn)
17754                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17755         {
17756           /* When optimization is enabled (via -O) some parts of the compiler
17757              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17758              represent source-level labels which were explicitly declared by
17759              the user.  This really shouldn't be happening though, so catch
17760              it if it ever does happen.  */
17761           gcc_assert (!INSN_DELETED_P (insn));
17762
17763           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17764           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17765         }
17766     }
17767 }
17768
17769 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17770    attributes to the DIE for a block STMT, to describe where the inlined
17771    function was called from.  This is similar to add_src_coords_attributes.  */
17772
17773 static inline void
17774 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17775 {
17776   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17777
17778   if (dwarf_version >= 3 || !dwarf_strict)
17779     {
17780       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17781       add_AT_unsigned (die, DW_AT_call_line, s.line);
17782     }
17783 }
17784
17785
17786 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17787    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17788
17789 static inline void
17790 add_high_low_attributes (tree stmt, dw_die_ref die)
17791 {
17792   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17793
17794   if (BLOCK_FRAGMENT_CHAIN (stmt)
17795       && (dwarf_version >= 3 || !dwarf_strict))
17796     {
17797       tree chain;
17798
17799       if (inlined_function_outer_scope_p (stmt))
17800         {
17801           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17802                                        BLOCK_NUMBER (stmt));
17803           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17804         }
17805
17806       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17807
17808       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17809       do
17810         {
17811           add_ranges (chain);
17812           chain = BLOCK_FRAGMENT_CHAIN (chain);
17813         }
17814       while (chain);
17815       add_ranges (NULL);
17816     }
17817   else
17818     {
17819       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17820                                    BLOCK_NUMBER (stmt));
17821       add_AT_lbl_id (die, DW_AT_low_pc, label);
17822       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
17823                                    BLOCK_NUMBER (stmt));
17824       add_AT_lbl_id (die, DW_AT_high_pc, label);
17825     }
17826 }
17827
17828 /* Generate a DIE for a lexical block.  */
17829
17830 static void
17831 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17832 {
17833   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17834
17835   if (call_arg_locations)
17836     {
17837       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17838         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17839                                BLOCK_NUMBER (stmt) + 1);
17840       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17841     }
17842
17843   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17844     add_high_low_attributes (stmt, stmt_die);
17845
17846   decls_for_scope (stmt, stmt_die, depth);
17847 }
17848
17849 /* Generate a DIE for an inlined subprogram.  */
17850
17851 static void
17852 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17853 {
17854   tree decl;
17855
17856   /* The instance of function that is effectively being inlined shall not
17857      be abstract.  */
17858   gcc_assert (! BLOCK_ABSTRACT (stmt));
17859
17860   decl = block_ultimate_origin (stmt);
17861
17862   /* Emit info for the abstract instance first, if we haven't yet.  We
17863      must emit this even if the block is abstract, otherwise when we
17864      emit the block below (or elsewhere), we may end up trying to emit
17865      a die whose origin die hasn't been emitted, and crashing.  */
17866   dwarf2out_abstract_function (decl);
17867
17868   if (! BLOCK_ABSTRACT (stmt))
17869     {
17870       dw_die_ref subr_die
17871         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17872
17873       if (call_arg_locations)
17874         {
17875           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17876             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17877                                    BLOCK_NUMBER (stmt) + 1);
17878           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17879         }
17880       add_abstract_origin_attribute (subr_die, decl);
17881       if (TREE_ASM_WRITTEN (stmt))
17882         add_high_low_attributes (stmt, subr_die);
17883       add_call_src_coords_attributes (stmt, subr_die);
17884
17885       decls_for_scope (stmt, subr_die, depth);
17886       current_function_has_inlines = 1;
17887     }
17888 }
17889
17890 /* Generate a DIE for a field in a record, or structure.  */
17891
17892 static void
17893 gen_field_die (tree decl, dw_die_ref context_die)
17894 {
17895   dw_die_ref decl_die;
17896
17897   if (TREE_TYPE (decl) == error_mark_node)
17898     return;
17899
17900   decl_die = new_die (DW_TAG_member, context_die, decl);
17901   add_name_and_src_coords_attributes (decl_die, decl);
17902   add_type_attribute (decl_die, member_declared_type (decl),
17903                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
17904                       context_die);
17905
17906   if (DECL_BIT_FIELD_TYPE (decl))
17907     {
17908       add_byte_size_attribute (decl_die, decl);
17909       add_bit_size_attribute (decl_die, decl);
17910       add_bit_offset_attribute (decl_die, decl);
17911     }
17912
17913   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
17914     add_data_member_location_attribute (decl_die, decl);
17915
17916   if (DECL_ARTIFICIAL (decl))
17917     add_AT_flag (decl_die, DW_AT_artificial, 1);
17918
17919   add_accessibility_attribute (decl_die, decl);
17920
17921   /* Equate decl number to die, so that we can look up this decl later on.  */
17922   equate_decl_number_to_die (decl, decl_die);
17923 }
17924
17925 #if 0
17926 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
17927    Use modified_type_die instead.
17928    We keep this code here just in case these types of DIEs may be needed to
17929    represent certain things in other languages (e.g. Pascal) someday.  */
17930
17931 static void
17932 gen_pointer_type_die (tree type, dw_die_ref context_die)
17933 {
17934   dw_die_ref ptr_die
17935     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
17936
17937   equate_type_number_to_die (type, ptr_die);
17938   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
17939   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
17940 }
17941
17942 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
17943    Use modified_type_die instead.
17944    We keep this code here just in case these types of DIEs may be needed to
17945    represent certain things in other languages (e.g. Pascal) someday.  */
17946
17947 static void
17948 gen_reference_type_die (tree type, dw_die_ref context_die)
17949 {
17950   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
17951
17952   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
17953     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
17954   else
17955     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
17956
17957   equate_type_number_to_die (type, ref_die);
17958   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
17959   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
17960 }
17961 #endif
17962
17963 /* Generate a DIE for a pointer to a member type.  */
17964
17965 static void
17966 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
17967 {
17968   dw_die_ref ptr_die
17969     = new_die (DW_TAG_ptr_to_member_type,
17970                scope_die_for (type, context_die), type);
17971
17972   equate_type_number_to_die (type, ptr_die);
17973   add_AT_die_ref (ptr_die, DW_AT_containing_type,
17974                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
17975   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
17976 }
17977
17978 /* Generate the DIE for the compilation unit.  */
17979
17980 static dw_die_ref
17981 gen_compile_unit_die (const char *filename)
17982 {
17983   dw_die_ref die;
17984   char producer[250];
17985   const char *language_string = lang_hooks.name;
17986   int language;
17987
17988   die = new_die (DW_TAG_compile_unit, NULL, NULL);
17989
17990   if (filename)
17991     {
17992       add_name_attribute (die, filename);
17993       /* Don't add cwd for <built-in>.  */
17994       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
17995         add_comp_dir_attribute (die);
17996     }
17997
17998   sprintf (producer, "%s %s", language_string, version_string);
17999
18000 #ifdef MIPS_DEBUGGING_INFO
18001   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18002      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18003      not appear in the producer string, the debugger reaches the conclusion
18004      that the object file is stripped and has no debugging information.
18005      To get the MIPS/SGI debugger to believe that there is debugging
18006      information in the object file, we add a -g to the producer string.  */
18007   if (debug_info_level > DINFO_LEVEL_TERSE)
18008     strcat (producer, " -g");
18009 #endif
18010
18011   add_AT_string (die, DW_AT_producer, producer);
18012
18013   /* If our producer is LTO try to figure out a common language to use
18014      from the global list of translation units.  */
18015   if (strcmp (language_string, "GNU GIMPLE") == 0)
18016     {
18017       unsigned i;
18018       tree t;
18019       const char *common_lang = NULL;
18020
18021       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18022         {
18023           if (!TRANSLATION_UNIT_LANGUAGE (t))
18024             continue;
18025           if (!common_lang)
18026             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18027           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18028             ;
18029           else if (strncmp (common_lang, "GNU C", 5) == 0
18030                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18031             /* Mixing C and C++ is ok, use C++ in that case.  */
18032             common_lang = "GNU C++";
18033           else
18034             {
18035               /* Fall back to C.  */
18036               common_lang = NULL;
18037               break;
18038             }
18039         }
18040
18041       if (common_lang)
18042         language_string = common_lang;
18043     }
18044
18045   language = DW_LANG_C89;
18046   if (strcmp (language_string, "GNU C++") == 0)
18047     language = DW_LANG_C_plus_plus;
18048   else if (strcmp (language_string, "GNU F77") == 0)
18049     language = DW_LANG_Fortran77;
18050   else if (strcmp (language_string, "GNU Pascal") == 0)
18051     language = DW_LANG_Pascal83;
18052   else if (dwarf_version >= 3 || !dwarf_strict)
18053     {
18054       if (strcmp (language_string, "GNU Ada") == 0)
18055         language = DW_LANG_Ada95;
18056       else if (strcmp (language_string, "GNU Fortran") == 0)
18057         language = DW_LANG_Fortran95;
18058       else if (strcmp (language_string, "GNU Java") == 0)
18059         language = DW_LANG_Java;
18060       else if (strcmp (language_string, "GNU Objective-C") == 0)
18061         language = DW_LANG_ObjC;
18062       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18063         language = DW_LANG_ObjC_plus_plus;
18064     }
18065
18066   add_AT_unsigned (die, DW_AT_language, language);
18067
18068   switch (language)
18069     {
18070     case DW_LANG_Fortran77:
18071     case DW_LANG_Fortran90:
18072     case DW_LANG_Fortran95:
18073       /* Fortran has case insensitive identifiers and the front-end
18074          lowercases everything.  */
18075       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18076       break;
18077     default:
18078       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18079       break;
18080     }
18081   return die;
18082 }
18083
18084 /* Generate the DIE for a base class.  */
18085
18086 static void
18087 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18088 {
18089   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18090
18091   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18092   add_data_member_location_attribute (die, binfo);
18093
18094   if (BINFO_VIRTUAL_P (binfo))
18095     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18096
18097   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18098      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18099      the default has always been DW_ACCESS_private.  */
18100   if (access == access_public_node)
18101     {
18102       if (dwarf_version == 2
18103           || context_die->die_tag == DW_TAG_class_type)
18104       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18105     }
18106   else if (access == access_protected_node)
18107     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18108   else if (dwarf_version > 2
18109            && context_die->die_tag != DW_TAG_class_type)
18110     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18111 }
18112
18113 /* Generate a DIE for a class member.  */
18114
18115 static void
18116 gen_member_die (tree type, dw_die_ref context_die)
18117 {
18118   tree member;
18119   tree binfo = TYPE_BINFO (type);
18120   dw_die_ref child;
18121
18122   /* If this is not an incomplete type, output descriptions of each of its
18123      members. Note that as we output the DIEs necessary to represent the
18124      members of this record or union type, we will also be trying to output
18125      DIEs to represent the *types* of those members. However the `type'
18126      function (above) will specifically avoid generating type DIEs for member
18127      types *within* the list of member DIEs for this (containing) type except
18128      for those types (of members) which are explicitly marked as also being
18129      members of this (containing) type themselves.  The g++ front- end can
18130      force any given type to be treated as a member of some other (containing)
18131      type by setting the TYPE_CONTEXT of the given (member) type to point to
18132      the TREE node representing the appropriate (containing) type.  */
18133
18134   /* First output info about the base classes.  */
18135   if (binfo)
18136     {
18137       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18138       int i;
18139       tree base;
18140
18141       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18142         gen_inheritance_die (base,
18143                              (accesses ? VEC_index (tree, accesses, i)
18144                               : access_public_node), context_die);
18145     }
18146
18147   /* Now output info about the data members and type members.  */
18148   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18149     {
18150       /* If we thought we were generating minimal debug info for TYPE
18151          and then changed our minds, some of the member declarations
18152          may have already been defined.  Don't define them again, but
18153          do put them in the right order.  */
18154
18155       child = lookup_decl_die (member);
18156       if (child)
18157         splice_child_die (context_die, child);
18158       else
18159         gen_decl_die (member, NULL, context_die);
18160     }
18161
18162   /* Now output info about the function members (if any).  */
18163   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18164     {
18165       /* Don't include clones in the member list.  */
18166       if (DECL_ABSTRACT_ORIGIN (member))
18167         continue;
18168
18169       child = lookup_decl_die (member);
18170       if (child)
18171         splice_child_die (context_die, child);
18172       else
18173         gen_decl_die (member, NULL, context_die);
18174     }
18175 }
18176
18177 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18178    is set, we pretend that the type was never defined, so we only get the
18179    member DIEs needed by later specification DIEs.  */
18180
18181 static void
18182 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18183                                 enum debug_info_usage usage)
18184 {
18185   dw_die_ref type_die = lookup_type_die (type);
18186   dw_die_ref scope_die = 0;
18187   int nested = 0;
18188   int complete = (TYPE_SIZE (type)
18189                   && (! TYPE_STUB_DECL (type)
18190                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18191   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18192   complete = complete && should_emit_struct_debug (type, usage);
18193
18194   if (type_die && ! complete)
18195     return;
18196
18197   if (TYPE_CONTEXT (type) != NULL_TREE
18198       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18199           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18200     nested = 1;
18201
18202   scope_die = scope_die_for (type, context_die);
18203
18204   if (! type_die || (nested && is_cu_die (scope_die)))
18205     /* First occurrence of type or toplevel definition of nested class.  */
18206     {
18207       dw_die_ref old_die = type_die;
18208
18209       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18210                           ? record_type_tag (type) : DW_TAG_union_type,
18211                           scope_die, type);
18212       equate_type_number_to_die (type, type_die);
18213       if (old_die)
18214         add_AT_specification (type_die, old_die);
18215       else
18216         {
18217           add_name_attribute (type_die, type_tag (type));
18218           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18219           if (TYPE_ARTIFICIAL (type))
18220             add_AT_flag (type_die, DW_AT_artificial, 1);
18221         }
18222     }
18223   else
18224     remove_AT (type_die, DW_AT_declaration);
18225
18226   /* Generate child dies for template paramaters.  */
18227   if (debug_info_level > DINFO_LEVEL_TERSE
18228       && COMPLETE_TYPE_P (type))
18229     schedule_generic_params_dies_gen (type);
18230
18231   /* If this type has been completed, then give it a byte_size attribute and
18232      then give a list of members.  */
18233   if (complete && !ns_decl)
18234     {
18235       /* Prevent infinite recursion in cases where the type of some member of
18236          this type is expressed in terms of this type itself.  */
18237       TREE_ASM_WRITTEN (type) = 1;
18238       add_byte_size_attribute (type_die, type);
18239       if (TYPE_STUB_DECL (type) != NULL_TREE)
18240         {
18241           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18242           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18243         }
18244
18245       /* If the first reference to this type was as the return type of an
18246          inline function, then it may not have a parent.  Fix this now.  */
18247       if (type_die->die_parent == NULL)
18248         add_child_die (scope_die, type_die);
18249
18250       push_decl_scope (type);
18251       gen_member_die (type, type_die);
18252       pop_decl_scope ();
18253
18254       /* GNU extension: Record what type our vtable lives in.  */
18255       if (TYPE_VFIELD (type))
18256         {
18257           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18258
18259           gen_type_die (vtype, context_die);
18260           add_AT_die_ref (type_die, DW_AT_containing_type,
18261                           lookup_type_die (vtype));
18262         }
18263     }
18264   else
18265     {
18266       add_AT_flag (type_die, DW_AT_declaration, 1);
18267
18268       /* We don't need to do this for function-local types.  */
18269       if (TYPE_STUB_DECL (type)
18270           && ! decl_function_context (TYPE_STUB_DECL (type)))
18271         VEC_safe_push (tree, gc, incomplete_types, type);
18272     }
18273
18274   if (get_AT (type_die, DW_AT_name))
18275     add_pubtype (type, type_die);
18276 }
18277
18278 /* Generate a DIE for a subroutine _type_.  */
18279
18280 static void
18281 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18282 {
18283   tree return_type = TREE_TYPE (type);
18284   dw_die_ref subr_die
18285     = new_die (DW_TAG_subroutine_type,
18286                scope_die_for (type, context_die), type);
18287
18288   equate_type_number_to_die (type, subr_die);
18289   add_prototyped_attribute (subr_die, type);
18290   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18291   gen_formal_types_die (type, subr_die);
18292
18293   if (get_AT (subr_die, DW_AT_name))
18294     add_pubtype (type, subr_die);
18295 }
18296
18297 /* Generate a DIE for a type definition.  */
18298
18299 static void
18300 gen_typedef_die (tree decl, dw_die_ref context_die)
18301 {
18302   dw_die_ref type_die;
18303   tree origin;
18304
18305   if (TREE_ASM_WRITTEN (decl))
18306     return;
18307
18308   TREE_ASM_WRITTEN (decl) = 1;
18309   type_die = new_die (DW_TAG_typedef, context_die, decl);
18310   origin = decl_ultimate_origin (decl);
18311   if (origin != NULL)
18312     add_abstract_origin_attribute (type_die, origin);
18313   else
18314     {
18315       tree type;
18316
18317       add_name_and_src_coords_attributes (type_die, decl);
18318       if (DECL_ORIGINAL_TYPE (decl))
18319         {
18320           type = DECL_ORIGINAL_TYPE (decl);
18321
18322           gcc_assert (type != TREE_TYPE (decl));
18323           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18324         }
18325       else
18326         {
18327           type = TREE_TYPE (decl);
18328
18329           if (is_naming_typedef_decl (TYPE_NAME (type)))
18330             {
18331               /* Here, we are in the case of decl being a typedef naming
18332                  an anonymous type, e.g:
18333                      typedef struct {...} foo;
18334                  In that case TREE_TYPE (decl) is not a typedef variant
18335                  type and TYPE_NAME of the anonymous type is set to the
18336                  TYPE_DECL of the typedef. This construct is emitted by
18337                  the C++ FE.
18338
18339                  TYPE is the anonymous struct named by the typedef
18340                  DECL. As we need the DW_AT_type attribute of the
18341                  DW_TAG_typedef to point to the DIE of TYPE, let's
18342                  generate that DIE right away. add_type_attribute
18343                  called below will then pick (via lookup_type_die) that
18344                  anonymous struct DIE.  */
18345               if (!TREE_ASM_WRITTEN (type))
18346                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18347
18348               /* This is a GNU Extension.  We are adding a
18349                  DW_AT_linkage_name attribute to the DIE of the
18350                  anonymous struct TYPE.  The value of that attribute
18351                  is the name of the typedef decl naming the anonymous
18352                  struct.  This greatly eases the work of consumers of
18353                  this debug info.  */
18354               add_linkage_attr (lookup_type_die (type), decl);
18355             }
18356         }
18357
18358       add_type_attribute (type_die, type, TREE_READONLY (decl),
18359                           TREE_THIS_VOLATILE (decl), context_die);
18360
18361       if (is_naming_typedef_decl (decl))
18362         /* We want that all subsequent calls to lookup_type_die with
18363            TYPE in argument yield the DW_TAG_typedef we have just
18364            created.  */
18365         equate_type_number_to_die (type, type_die);
18366
18367       add_accessibility_attribute (type_die, decl);
18368     }
18369
18370   if (DECL_ABSTRACT (decl))
18371     equate_decl_number_to_die (decl, type_die);
18372
18373   if (get_AT (type_die, DW_AT_name))
18374     add_pubtype (decl, type_die);
18375 }
18376
18377 /* Generate a DIE for a struct, class, enum or union type.  */
18378
18379 static void
18380 gen_tagged_type_die (tree type,
18381                      dw_die_ref context_die,
18382                      enum debug_info_usage usage)
18383 {
18384   int need_pop;
18385
18386   if (type == NULL_TREE
18387       || !is_tagged_type (type))
18388     return;
18389
18390   /* If this is a nested type whose containing class hasn't been written
18391      out yet, writing it out will cover this one, too.  This does not apply
18392      to instantiations of member class templates; they need to be added to
18393      the containing class as they are generated.  FIXME: This hurts the
18394      idea of combining type decls from multiple TUs, since we can't predict
18395      what set of template instantiations we'll get.  */
18396   if (TYPE_CONTEXT (type)
18397       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18398       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18399     {
18400       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18401
18402       if (TREE_ASM_WRITTEN (type))
18403         return;
18404
18405       /* If that failed, attach ourselves to the stub.  */
18406       push_decl_scope (TYPE_CONTEXT (type));
18407       context_die = lookup_type_die (TYPE_CONTEXT (type));
18408       need_pop = 1;
18409     }
18410   else if (TYPE_CONTEXT (type) != NULL_TREE
18411            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18412     {
18413       /* If this type is local to a function that hasn't been written
18414          out yet, use a NULL context for now; it will be fixed up in
18415          decls_for_scope.  */
18416       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18417       /* A declaration DIE doesn't count; nested types need to go in the
18418          specification.  */
18419       if (context_die && is_declaration_die (context_die))
18420         context_die = NULL;
18421       need_pop = 0;
18422     }
18423   else
18424     {
18425       context_die = declare_in_namespace (type, context_die);
18426       need_pop = 0;
18427     }
18428
18429   if (TREE_CODE (type) == ENUMERAL_TYPE)
18430     {
18431       /* This might have been written out by the call to
18432          declare_in_namespace.  */
18433       if (!TREE_ASM_WRITTEN (type))
18434         gen_enumeration_type_die (type, context_die);
18435     }
18436   else
18437     gen_struct_or_union_type_die (type, context_die, usage);
18438
18439   if (need_pop)
18440     pop_decl_scope ();
18441
18442   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18443      it up if it is ever completed.  gen_*_type_die will set it for us
18444      when appropriate.  */
18445 }
18446
18447 /* Generate a type description DIE.  */
18448
18449 static void
18450 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18451                          enum debug_info_usage usage)
18452 {
18453   struct array_descr_info info;
18454
18455   if (type == NULL_TREE || type == error_mark_node)
18456     return;
18457
18458   if (TYPE_NAME (type) != NULL_TREE
18459       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18460       && is_redundant_typedef (TYPE_NAME (type))
18461       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18462     /* The DECL of this type is a typedef we don't want to emit debug
18463        info for but we want debug info for its underlying typedef.
18464        This can happen for e.g, the injected-class-name of a C++
18465        type.  */
18466     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18467
18468   /* If TYPE is a typedef type variant, let's generate debug info
18469      for the parent typedef which TYPE is a type of.  */
18470   if (typedef_variant_p (type))
18471     {
18472       if (TREE_ASM_WRITTEN (type))
18473         return;
18474
18475       /* Prevent broken recursion; we can't hand off to the same type.  */
18476       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18477
18478       /* Use the DIE of the containing namespace as the parent DIE of
18479          the type description DIE we want to generate.  */
18480       if (DECL_CONTEXT (TYPE_NAME (type))
18481           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18482         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18483
18484       TREE_ASM_WRITTEN (type) = 1;
18485
18486       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18487       return;
18488     }
18489
18490   /* If type is an anonymous tagged type named by a typedef, let's
18491      generate debug info for the typedef.  */
18492   if (is_naming_typedef_decl (TYPE_NAME (type)))
18493     {
18494       /* Use the DIE of the containing namespace as the parent DIE of
18495          the type description DIE we want to generate.  */
18496       if (DECL_CONTEXT (TYPE_NAME (type))
18497           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18498         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18499       
18500       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18501       return;
18502     }
18503
18504   /* If this is an array type with hidden descriptor, handle it first.  */
18505   if (!TREE_ASM_WRITTEN (type)
18506       && lang_hooks.types.get_array_descr_info
18507       && lang_hooks.types.get_array_descr_info (type, &info)
18508       && (dwarf_version >= 3 || !dwarf_strict))
18509     {
18510       gen_descr_array_type_die (type, &info, context_die);
18511       TREE_ASM_WRITTEN (type) = 1;
18512       return;
18513     }
18514
18515   /* We are going to output a DIE to represent the unqualified version
18516      of this type (i.e. without any const or volatile qualifiers) so
18517      get the main variant (i.e. the unqualified version) of this type
18518      now.  (Vectors are special because the debugging info is in the
18519      cloned type itself).  */
18520   if (TREE_CODE (type) != VECTOR_TYPE)
18521     type = type_main_variant (type);
18522
18523   if (TREE_ASM_WRITTEN (type))
18524     return;
18525
18526   switch (TREE_CODE (type))
18527     {
18528     case ERROR_MARK:
18529       break;
18530
18531     case POINTER_TYPE:
18532     case REFERENCE_TYPE:
18533       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18534          ensures that the gen_type_die recursion will terminate even if the
18535          type is recursive.  Recursive types are possible in Ada.  */
18536       /* ??? We could perhaps do this for all types before the switch
18537          statement.  */
18538       TREE_ASM_WRITTEN (type) = 1;
18539
18540       /* For these types, all that is required is that we output a DIE (or a
18541          set of DIEs) to represent the "basis" type.  */
18542       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18543                                 DINFO_USAGE_IND_USE);
18544       break;
18545
18546     case OFFSET_TYPE:
18547       /* This code is used for C++ pointer-to-data-member types.
18548          Output a description of the relevant class type.  */
18549       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18550                                         DINFO_USAGE_IND_USE);
18551
18552       /* Output a description of the type of the object pointed to.  */
18553       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18554                                         DINFO_USAGE_IND_USE);
18555
18556       /* Now output a DIE to represent this pointer-to-data-member type
18557          itself.  */
18558       gen_ptr_to_mbr_type_die (type, context_die);
18559       break;
18560
18561     case FUNCTION_TYPE:
18562       /* Force out return type (in case it wasn't forced out already).  */
18563       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18564                                         DINFO_USAGE_DIR_USE);
18565       gen_subroutine_type_die (type, context_die);
18566       break;
18567
18568     case METHOD_TYPE:
18569       /* Force out return type (in case it wasn't forced out already).  */
18570       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18571                                         DINFO_USAGE_DIR_USE);
18572       gen_subroutine_type_die (type, context_die);
18573       break;
18574
18575     case ARRAY_TYPE:
18576       gen_array_type_die (type, context_die);
18577       break;
18578
18579     case VECTOR_TYPE:
18580       gen_array_type_die (type, context_die);
18581       break;
18582
18583     case ENUMERAL_TYPE:
18584     case RECORD_TYPE:
18585     case UNION_TYPE:
18586     case QUAL_UNION_TYPE:
18587       gen_tagged_type_die (type, context_die, usage);
18588       return;
18589
18590     case VOID_TYPE:
18591     case INTEGER_TYPE:
18592     case REAL_TYPE:
18593     case FIXED_POINT_TYPE:
18594     case COMPLEX_TYPE:
18595     case BOOLEAN_TYPE:
18596       /* No DIEs needed for fundamental types.  */
18597       break;
18598
18599     case NULLPTR_TYPE:
18600     case LANG_TYPE:
18601       /* Just use DW_TAG_unspecified_type.  */
18602       {
18603         dw_die_ref type_die = lookup_type_die (type);
18604         if (type_die == NULL)
18605           {
18606             tree name = TYPE_NAME (type);
18607             if (TREE_CODE (name) == TYPE_DECL)
18608               name = DECL_NAME (name);
18609             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18610             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18611             equate_type_number_to_die (type, type_die);
18612           }
18613       }
18614       break;
18615
18616     default:
18617       gcc_unreachable ();
18618     }
18619
18620   TREE_ASM_WRITTEN (type) = 1;
18621 }
18622
18623 static void
18624 gen_type_die (tree type, dw_die_ref context_die)
18625 {
18626   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18627 }
18628
18629 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18630    things which are local to the given block.  */
18631
18632 static void
18633 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18634 {
18635   int must_output_die = 0;
18636   bool inlined_func;
18637
18638   /* Ignore blocks that are NULL.  */
18639   if (stmt == NULL_TREE)
18640     return;
18641
18642   inlined_func = inlined_function_outer_scope_p (stmt);
18643
18644   /* If the block is one fragment of a non-contiguous block, do not
18645      process the variables, since they will have been done by the
18646      origin block.  Do process subblocks.  */
18647   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18648     {
18649       tree sub;
18650
18651       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18652         gen_block_die (sub, context_die, depth + 1);
18653
18654       return;
18655     }
18656
18657   /* Determine if we need to output any Dwarf DIEs at all to represent this
18658      block.  */
18659   if (inlined_func)
18660     /* The outer scopes for inlinings *must* always be represented.  We
18661        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18662     must_output_die = 1;
18663   else
18664     {
18665       /* Determine if this block directly contains any "significant"
18666          local declarations which we will need to output DIEs for.  */
18667       if (debug_info_level > DINFO_LEVEL_TERSE)
18668         /* We are not in terse mode so *any* local declaration counts
18669            as being a "significant" one.  */
18670         must_output_die = ((BLOCK_VARS (stmt) != NULL
18671                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18672                            && (TREE_USED (stmt)
18673                                || TREE_ASM_WRITTEN (stmt)
18674                                || BLOCK_ABSTRACT (stmt)));
18675       else if ((TREE_USED (stmt)
18676                 || TREE_ASM_WRITTEN (stmt)
18677                 || BLOCK_ABSTRACT (stmt))
18678                && !dwarf2out_ignore_block (stmt))
18679         must_output_die = 1;
18680     }
18681
18682   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18683      DIE for any block which contains no significant local declarations at
18684      all.  Rather, in such cases we just call `decls_for_scope' so that any
18685      needed Dwarf info for any sub-blocks will get properly generated. Note
18686      that in terse mode, our definition of what constitutes a "significant"
18687      local declaration gets restricted to include only inlined function
18688      instances and local (nested) function definitions.  */
18689   if (must_output_die)
18690     {
18691       if (inlined_func)
18692         {
18693           /* If STMT block is abstract, that means we have been called
18694              indirectly from dwarf2out_abstract_function.
18695              That function rightfully marks the descendent blocks (of
18696              the abstract function it is dealing with) as being abstract,
18697              precisely to prevent us from emitting any
18698              DW_TAG_inlined_subroutine DIE as a descendent
18699              of an abstract function instance. So in that case, we should
18700              not call gen_inlined_subroutine_die.
18701
18702              Later though, when cgraph asks dwarf2out to emit info
18703              for the concrete instance of the function decl into which
18704              the concrete instance of STMT got inlined, the later will lead
18705              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18706           if (! BLOCK_ABSTRACT (stmt))
18707             gen_inlined_subroutine_die (stmt, context_die, depth);
18708         }
18709       else
18710         gen_lexical_block_die (stmt, context_die, depth);
18711     }
18712   else
18713     decls_for_scope (stmt, context_die, depth);
18714 }
18715
18716 /* Process variable DECL (or variable with origin ORIGIN) within
18717    block STMT and add it to CONTEXT_DIE.  */
18718 static void
18719 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18720 {
18721   dw_die_ref die;
18722   tree decl_or_origin = decl ? decl : origin;
18723
18724   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18725     die = lookup_decl_die (decl_or_origin);
18726   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18727            && TYPE_DECL_IS_STUB (decl_or_origin))
18728     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18729   else
18730     die = NULL;
18731
18732   if (die != NULL && die->die_parent == NULL)
18733     add_child_die (context_die, die);
18734   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18735     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18736                                          stmt, context_die);
18737   else
18738     gen_decl_die (decl, origin, context_die);
18739 }
18740
18741 /* Generate all of the decls declared within a given scope and (recursively)
18742    all of its sub-blocks.  */
18743
18744 static void
18745 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18746 {
18747   tree decl;
18748   unsigned int i;
18749   tree subblocks;
18750
18751   /* Ignore NULL blocks.  */
18752   if (stmt == NULL_TREE)
18753     return;
18754
18755   /* Output the DIEs to represent all of the data objects and typedefs
18756      declared directly within this block but not within any nested
18757      sub-blocks.  Also, nested function and tag DIEs have been
18758      generated with a parent of NULL; fix that up now.  */
18759   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18760     process_scope_var (stmt, decl, NULL_TREE, context_die);
18761   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18762     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18763                        context_die);
18764
18765   /* If we're at -g1, we're not interested in subblocks.  */
18766   if (debug_info_level <= DINFO_LEVEL_TERSE)
18767     return;
18768
18769   /* Output the DIEs to represent all sub-blocks (and the items declared
18770      therein) of this block.  */
18771   for (subblocks = BLOCK_SUBBLOCKS (stmt);
18772        subblocks != NULL;
18773        subblocks = BLOCK_CHAIN (subblocks))
18774     gen_block_die (subblocks, context_die, depth + 1);
18775 }
18776
18777 /* Is this a typedef we can avoid emitting?  */
18778
18779 static inline int
18780 is_redundant_typedef (const_tree decl)
18781 {
18782   if (TYPE_DECL_IS_STUB (decl))
18783     return 1;
18784
18785   if (DECL_ARTIFICIAL (decl)
18786       && DECL_CONTEXT (decl)
18787       && is_tagged_type (DECL_CONTEXT (decl))
18788       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18789       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18790     /* Also ignore the artificial member typedef for the class name.  */
18791     return 1;
18792
18793   return 0;
18794 }
18795
18796 /* Return TRUE if TYPE is a typedef that names a type for linkage
18797    purposes. This kind of typedefs is produced by the C++ FE for
18798    constructs like:
18799
18800    typedef struct {...} foo;
18801
18802    In that case, there is no typedef variant type produced for foo.
18803    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
18804    struct type.  */
18805
18806 static bool
18807 is_naming_typedef_decl (const_tree decl)
18808 {
18809   if (decl == NULL_TREE
18810       || TREE_CODE (decl) != TYPE_DECL
18811       || !is_tagged_type (TREE_TYPE (decl))
18812       || DECL_IS_BUILTIN (decl)
18813       || is_redundant_typedef (decl)
18814       /* It looks like Ada produces TYPE_DECLs that are very similar
18815          to C++ naming typedefs but that have different
18816          semantics. Let's be specific to c++ for now.  */
18817       || !is_cxx ())
18818     return FALSE;
18819
18820   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
18821           && TYPE_NAME (TREE_TYPE (decl)) == decl
18822           && (TYPE_STUB_DECL (TREE_TYPE (decl))
18823               != TYPE_NAME (TREE_TYPE (decl))));
18824 }
18825
18826 /* Returns the DIE for a context.  */
18827
18828 static inline dw_die_ref
18829 get_context_die (tree context)
18830 {
18831   if (context)
18832     {
18833       /* Find die that represents this context.  */
18834       if (TYPE_P (context))
18835         {
18836           context = TYPE_MAIN_VARIANT (context);
18837           return strip_naming_typedef (context, force_type_die (context));
18838         }
18839       else
18840         return force_decl_die (context);
18841     }
18842   return comp_unit_die ();
18843 }
18844
18845 /* Returns the DIE for decl.  A DIE will always be returned.  */
18846
18847 static dw_die_ref
18848 force_decl_die (tree decl)
18849 {
18850   dw_die_ref decl_die;
18851   unsigned saved_external_flag;
18852   tree save_fn = NULL_TREE;
18853   decl_die = lookup_decl_die (decl);
18854   if (!decl_die)
18855     {
18856       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
18857
18858       decl_die = lookup_decl_die (decl);
18859       if (decl_die)
18860         return decl_die;
18861
18862       switch (TREE_CODE (decl))
18863         {
18864         case FUNCTION_DECL:
18865           /* Clear current_function_decl, so that gen_subprogram_die thinks
18866              that this is a declaration. At this point, we just want to force
18867              declaration die.  */
18868           save_fn = current_function_decl;
18869           current_function_decl = NULL_TREE;
18870           gen_subprogram_die (decl, context_die);
18871           current_function_decl = save_fn;
18872           break;
18873
18874         case VAR_DECL:
18875           /* Set external flag to force declaration die. Restore it after
18876            gen_decl_die() call.  */
18877           saved_external_flag = DECL_EXTERNAL (decl);
18878           DECL_EXTERNAL (decl) = 1;
18879           gen_decl_die (decl, NULL, context_die);
18880           DECL_EXTERNAL (decl) = saved_external_flag;
18881           break;
18882
18883         case NAMESPACE_DECL:
18884           if (dwarf_version >= 3 || !dwarf_strict)
18885             dwarf2out_decl (decl);
18886           else
18887             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
18888             decl_die = comp_unit_die ();
18889           break;
18890
18891         case TRANSLATION_UNIT_DECL:
18892           decl_die = comp_unit_die ();
18893           break;
18894
18895         default:
18896           gcc_unreachable ();
18897         }
18898
18899       /* We should be able to find the DIE now.  */
18900       if (!decl_die)
18901         decl_die = lookup_decl_die (decl);
18902       gcc_assert (decl_die);
18903     }
18904
18905   return decl_die;
18906 }
18907
18908 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
18909    always returned.  */
18910
18911 static dw_die_ref
18912 force_type_die (tree type)
18913 {
18914   dw_die_ref type_die;
18915
18916   type_die = lookup_type_die (type);
18917   if (!type_die)
18918     {
18919       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
18920
18921       type_die = modified_type_die (type, TYPE_READONLY (type),
18922                                     TYPE_VOLATILE (type), context_die);
18923       gcc_assert (type_die);
18924     }
18925   return type_die;
18926 }
18927
18928 /* Force out any required namespaces to be able to output DECL,
18929    and return the new context_die for it, if it's changed.  */
18930
18931 static dw_die_ref
18932 setup_namespace_context (tree thing, dw_die_ref context_die)
18933 {
18934   tree context = (DECL_P (thing)
18935                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
18936   if (context && TREE_CODE (context) == NAMESPACE_DECL)
18937     /* Force out the namespace.  */
18938     context_die = force_decl_die (context);
18939
18940   return context_die;
18941 }
18942
18943 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
18944    type) within its namespace, if appropriate.
18945
18946    For compatibility with older debuggers, namespace DIEs only contain
18947    declarations; all definitions are emitted at CU scope.  */
18948
18949 static dw_die_ref
18950 declare_in_namespace (tree thing, dw_die_ref context_die)
18951 {
18952   dw_die_ref ns_context;
18953
18954   if (debug_info_level <= DINFO_LEVEL_TERSE)
18955     return context_die;
18956
18957   /* If this decl is from an inlined function, then don't try to emit it in its
18958      namespace, as we will get confused.  It would have already been emitted
18959      when the abstract instance of the inline function was emitted anyways.  */
18960   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
18961     return context_die;
18962
18963   ns_context = setup_namespace_context (thing, context_die);
18964
18965   if (ns_context != context_die)
18966     {
18967       if (is_fortran ())
18968         return ns_context;
18969       if (DECL_P (thing))
18970         gen_decl_die (thing, NULL, ns_context);
18971       else
18972         gen_type_die (thing, ns_context);
18973     }
18974   return context_die;
18975 }
18976
18977 /* Generate a DIE for a namespace or namespace alias.  */
18978
18979 static void
18980 gen_namespace_die (tree decl, dw_die_ref context_die)
18981 {
18982   dw_die_ref namespace_die;
18983
18984   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
18985      they are an alias of.  */
18986   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
18987     {
18988       /* Output a real namespace or module.  */
18989       context_die = setup_namespace_context (decl, comp_unit_die ());
18990       namespace_die = new_die (is_fortran ()
18991                                ? DW_TAG_module : DW_TAG_namespace,
18992                                context_die, decl);
18993       /* For Fortran modules defined in different CU don't add src coords.  */
18994       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
18995         {
18996           const char *name = dwarf2_name (decl, 0);
18997           if (name)
18998             add_name_attribute (namespace_die, name);
18999         }
19000       else
19001         add_name_and_src_coords_attributes (namespace_die, decl);
19002       if (DECL_EXTERNAL (decl))
19003         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19004       equate_decl_number_to_die (decl, namespace_die);
19005     }
19006   else
19007     {
19008       /* Output a namespace alias.  */
19009
19010       /* Force out the namespace we are an alias of, if necessary.  */
19011       dw_die_ref origin_die
19012         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19013
19014       if (DECL_FILE_SCOPE_P (decl)
19015           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19016         context_die = setup_namespace_context (decl, comp_unit_die ());
19017       /* Now create the namespace alias DIE.  */
19018       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19019       add_name_and_src_coords_attributes (namespace_die, decl);
19020       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19021       equate_decl_number_to_die (decl, namespace_die);
19022     }
19023 }
19024
19025 /* Generate Dwarf debug information for a decl described by DECL.
19026    The return value is currently only meaningful for PARM_DECLs,
19027    for all other decls it returns NULL.  */
19028
19029 static dw_die_ref
19030 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19031 {
19032   tree decl_or_origin = decl ? decl : origin;
19033   tree class_origin = NULL, ultimate_origin;
19034
19035   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19036     return NULL;
19037
19038   switch (TREE_CODE (decl_or_origin))
19039     {
19040     case ERROR_MARK:
19041       break;
19042
19043     case CONST_DECL:
19044       if (!is_fortran () && !is_ada ())
19045         {
19046           /* The individual enumerators of an enum type get output when we output
19047              the Dwarf representation of the relevant enum type itself.  */
19048           break;
19049         }
19050
19051       /* Emit its type.  */
19052       gen_type_die (TREE_TYPE (decl), context_die);
19053
19054       /* And its containing namespace.  */
19055       context_die = declare_in_namespace (decl, context_die);
19056
19057       gen_const_die (decl, context_die);
19058       break;
19059
19060     case FUNCTION_DECL:
19061       /* Don't output any DIEs to represent mere function declarations,
19062          unless they are class members or explicit block externs.  */
19063       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19064           && DECL_FILE_SCOPE_P (decl_or_origin)
19065           && (current_function_decl == NULL_TREE
19066               || DECL_ARTIFICIAL (decl_or_origin)))
19067         break;
19068
19069 #if 0
19070       /* FIXME */
19071       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19072          on local redeclarations of global functions.  That seems broken.  */
19073       if (current_function_decl != decl)
19074         /* This is only a declaration.  */;
19075 #endif
19076
19077       /* If we're emitting a clone, emit info for the abstract instance.  */
19078       if (origin || DECL_ORIGIN (decl) != decl)
19079         dwarf2out_abstract_function (origin
19080                                      ? DECL_ORIGIN (origin)
19081                                      : DECL_ABSTRACT_ORIGIN (decl));
19082
19083       /* If we're emitting an out-of-line copy of an inline function,
19084          emit info for the abstract instance and set up to refer to it.  */
19085       else if (cgraph_function_possibly_inlined_p (decl)
19086                && ! DECL_ABSTRACT (decl)
19087                && ! class_or_namespace_scope_p (context_die)
19088                /* dwarf2out_abstract_function won't emit a die if this is just
19089                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19090                   that case, because that works only if we have a die.  */
19091                && DECL_INITIAL (decl) != NULL_TREE)
19092         {
19093           dwarf2out_abstract_function (decl);
19094           set_decl_origin_self (decl);
19095         }
19096
19097       /* Otherwise we're emitting the primary DIE for this decl.  */
19098       else if (debug_info_level > DINFO_LEVEL_TERSE)
19099         {
19100           /* Before we describe the FUNCTION_DECL itself, make sure that we
19101              have its containing type.  */
19102           if (!origin)
19103             origin = decl_class_context (decl);
19104           if (origin != NULL_TREE)
19105             gen_type_die (origin, context_die);
19106
19107           /* And its return type.  */
19108           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19109
19110           /* And its virtual context.  */
19111           if (DECL_VINDEX (decl) != NULL_TREE)
19112             gen_type_die (DECL_CONTEXT (decl), context_die);
19113
19114           /* Make sure we have a member DIE for decl.  */
19115           if (origin != NULL_TREE)
19116             gen_type_die_for_member (origin, decl, context_die);
19117
19118           /* And its containing namespace.  */
19119           context_die = declare_in_namespace (decl, context_die);
19120         }
19121
19122       /* Now output a DIE to represent the function itself.  */
19123       if (decl)
19124         gen_subprogram_die (decl, context_die);
19125       break;
19126
19127     case TYPE_DECL:
19128       /* If we are in terse mode, don't generate any DIEs to represent any
19129          actual typedefs.  */
19130       if (debug_info_level <= DINFO_LEVEL_TERSE)
19131         break;
19132
19133       /* In the special case of a TYPE_DECL node representing the declaration
19134          of some type tag, if the given TYPE_DECL is marked as having been
19135          instantiated from some other (original) TYPE_DECL node (e.g. one which
19136          was generated within the original definition of an inline function) we
19137          used to generate a special (abbreviated) DW_TAG_structure_type,
19138          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19139          should be actually referencing those DIEs, as variable DIEs with that
19140          type would be emitted already in the abstract origin, so it was always
19141          removed during unused type prunning.  Don't add anything in this
19142          case.  */
19143       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19144         break;
19145
19146       if (is_redundant_typedef (decl))
19147         gen_type_die (TREE_TYPE (decl), context_die);
19148       else
19149         /* Output a DIE to represent the typedef itself.  */
19150         gen_typedef_die (decl, context_die);
19151       break;
19152
19153     case LABEL_DECL:
19154       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19155         gen_label_die (decl, context_die);
19156       break;
19157
19158     case VAR_DECL:
19159     case RESULT_DECL:
19160       /* If we are in terse mode, don't generate any DIEs to represent any
19161          variable declarations or definitions.  */
19162       if (debug_info_level <= DINFO_LEVEL_TERSE)
19163         break;
19164
19165       /* Output any DIEs that are needed to specify the type of this data
19166          object.  */
19167       if (decl_by_reference_p (decl_or_origin))
19168         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19169       else
19170         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19171
19172       /* And its containing type.  */
19173       class_origin = decl_class_context (decl_or_origin);
19174       if (class_origin != NULL_TREE)
19175         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19176
19177       /* And its containing namespace.  */
19178       context_die = declare_in_namespace (decl_or_origin, context_die);
19179
19180       /* Now output the DIE to represent the data object itself.  This gets
19181          complicated because of the possibility that the VAR_DECL really
19182          represents an inlined instance of a formal parameter for an inline
19183          function.  */
19184       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19185       if (ultimate_origin != NULL_TREE
19186           && TREE_CODE (ultimate_origin) == PARM_DECL)
19187         gen_formal_parameter_die (decl, origin,
19188                                   true /* Emit name attribute.  */,
19189                                   context_die);
19190       else
19191         gen_variable_die (decl, origin, context_die);
19192       break;
19193
19194     case FIELD_DECL:
19195       /* Ignore the nameless fields that are used to skip bits but handle C++
19196          anonymous unions and structs.  */
19197       if (DECL_NAME (decl) != NULL_TREE
19198           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19199           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19200         {
19201           gen_type_die (member_declared_type (decl), context_die);
19202           gen_field_die (decl, context_die);
19203         }
19204       break;
19205
19206     case PARM_DECL:
19207       if (DECL_BY_REFERENCE (decl_or_origin))
19208         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19209       else
19210         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19211       return gen_formal_parameter_die (decl, origin,
19212                                        true /* Emit name attribute.  */,
19213                                        context_die);
19214
19215     case NAMESPACE_DECL:
19216     case IMPORTED_DECL:
19217       if (dwarf_version >= 3 || !dwarf_strict)
19218         gen_namespace_die (decl, context_die);
19219       break;
19220
19221     default:
19222       /* Probably some frontend-internal decl.  Assume we don't care.  */
19223       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19224       break;
19225     }
19226
19227   return NULL;
19228 }
19229 \f
19230 /* Output debug information for global decl DECL.  Called from toplev.c after
19231    compilation proper has finished.  */
19232
19233 static void
19234 dwarf2out_global_decl (tree decl)
19235 {
19236   /* Output DWARF2 information for file-scope tentative data object
19237      declarations, file-scope (extern) function declarations (which
19238      had no corresponding body) and file-scope tagged type declarations
19239      and definitions which have not yet been forced out.  */
19240   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19241     dwarf2out_decl (decl);
19242 }
19243
19244 /* Output debug information for type decl DECL.  Called from toplev.c
19245    and from language front ends (to record built-in types).  */
19246 static void
19247 dwarf2out_type_decl (tree decl, int local)
19248 {
19249   if (!local)
19250     dwarf2out_decl (decl);
19251 }
19252
19253 /* Output debug information for imported module or decl DECL.
19254    NAME is non-NULL name in the lexical block if the decl has been renamed.
19255    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19256    that DECL belongs to.
19257    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19258 static void
19259 dwarf2out_imported_module_or_decl_1 (tree decl,
19260                                      tree name,
19261                                      tree lexical_block,
19262                                      dw_die_ref lexical_block_die)
19263 {
19264   expanded_location xloc;
19265   dw_die_ref imported_die = NULL;
19266   dw_die_ref at_import_die;
19267
19268   if (TREE_CODE (decl) == IMPORTED_DECL)
19269     {
19270       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19271       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19272       gcc_assert (decl);
19273     }
19274   else
19275     xloc = expand_location (input_location);
19276
19277   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19278     {
19279       at_import_die = force_type_die (TREE_TYPE (decl));
19280       /* For namespace N { typedef void T; } using N::T; base_type_die
19281          returns NULL, but DW_TAG_imported_declaration requires
19282          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19283       if (!at_import_die)
19284         {
19285           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19286           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19287           at_import_die = lookup_type_die (TREE_TYPE (decl));
19288           gcc_assert (at_import_die);
19289         }
19290     }
19291   else
19292     {
19293       at_import_die = lookup_decl_die (decl);
19294       if (!at_import_die)
19295         {
19296           /* If we're trying to avoid duplicate debug info, we may not have
19297              emitted the member decl for this field.  Emit it now.  */
19298           if (TREE_CODE (decl) == FIELD_DECL)
19299             {
19300               tree type = DECL_CONTEXT (decl);
19301
19302               if (TYPE_CONTEXT (type)
19303                   && TYPE_P (TYPE_CONTEXT (type))
19304                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19305                                                 DINFO_USAGE_DIR_USE))
19306                 return;
19307               gen_type_die_for_member (type, decl,
19308                                        get_context_die (TYPE_CONTEXT (type)));
19309             }
19310           at_import_die = force_decl_die (decl);
19311         }
19312     }
19313
19314   if (TREE_CODE (decl) == NAMESPACE_DECL)
19315     {
19316       if (dwarf_version >= 3 || !dwarf_strict)
19317         imported_die = new_die (DW_TAG_imported_module,
19318                                 lexical_block_die,
19319                                 lexical_block);
19320       else
19321         return;
19322     }
19323   else
19324     imported_die = new_die (DW_TAG_imported_declaration,
19325                             lexical_block_die,
19326                             lexical_block);
19327
19328   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19329   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19330   if (name)
19331     add_AT_string (imported_die, DW_AT_name,
19332                    IDENTIFIER_POINTER (name));
19333   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19334 }
19335
19336 /* Output debug information for imported module or decl DECL.
19337    NAME is non-NULL name in context if the decl has been renamed.
19338    CHILD is true if decl is one of the renamed decls as part of
19339    importing whole module.  */
19340
19341 static void
19342 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19343                                    bool child)
19344 {
19345   /* dw_die_ref at_import_die;  */
19346   dw_die_ref scope_die;
19347
19348   if (debug_info_level <= DINFO_LEVEL_TERSE)
19349     return;
19350
19351   gcc_assert (decl);
19352
19353   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19354      We need decl DIE for reference and scope die. First, get DIE for the decl
19355      itself.  */
19356
19357   /* Get the scope die for decl context. Use comp_unit_die for global module
19358      or decl. If die is not found for non globals, force new die.  */
19359   if (context
19360       && TYPE_P (context)
19361       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19362     return;
19363
19364   if (!(dwarf_version >= 3 || !dwarf_strict))
19365     return;
19366
19367   scope_die = get_context_die (context);
19368
19369   if (child)
19370     {
19371       gcc_assert (scope_die->die_child);
19372       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19373       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19374       scope_die = scope_die->die_child;
19375     }
19376
19377   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19378   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19379
19380 }
19381
19382 /* Write the debugging output for DECL.  */
19383
19384 void
19385 dwarf2out_decl (tree decl)
19386 {
19387   dw_die_ref context_die = comp_unit_die ();
19388
19389   switch (TREE_CODE (decl))
19390     {
19391     case ERROR_MARK:
19392       return;
19393
19394     case FUNCTION_DECL:
19395       /* What we would really like to do here is to filter out all mere
19396          file-scope declarations of file-scope functions which are never
19397          referenced later within this translation unit (and keep all of ones
19398          that *are* referenced later on) but we aren't clairvoyant, so we have
19399          no idea which functions will be referenced in the future (i.e. later
19400          on within the current translation unit). So here we just ignore all
19401          file-scope function declarations which are not also definitions.  If
19402          and when the debugger needs to know something about these functions,
19403          it will have to hunt around and find the DWARF information associated
19404          with the definition of the function.
19405
19406          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19407          nodes represent definitions and which ones represent mere
19408          declarations.  We have to check DECL_INITIAL instead. That's because
19409          the C front-end supports some weird semantics for "extern inline"
19410          function definitions.  These can get inlined within the current
19411          translation unit (and thus, we need to generate Dwarf info for their
19412          abstract instances so that the Dwarf info for the concrete inlined
19413          instances can have something to refer to) but the compiler never
19414          generates any out-of-lines instances of such things (despite the fact
19415          that they *are* definitions).
19416
19417          The important point is that the C front-end marks these "extern
19418          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19419          them anyway. Note that the C++ front-end also plays some similar games
19420          for inline function definitions appearing within include files which
19421          also contain `#pragma interface' pragmas.  */
19422       if (DECL_INITIAL (decl) == NULL_TREE)
19423         return;
19424
19425       /* If we're a nested function, initially use a parent of NULL; if we're
19426          a plain function, this will be fixed up in decls_for_scope.  If
19427          we're a method, it will be ignored, since we already have a DIE.  */
19428       if (decl_function_context (decl)
19429           /* But if we're in terse mode, we don't care about scope.  */
19430           && debug_info_level > DINFO_LEVEL_TERSE)
19431         context_die = NULL;
19432       break;
19433
19434     case VAR_DECL:
19435       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19436          declaration and if the declaration was never even referenced from
19437          within this entire compilation unit.  We suppress these DIEs in
19438          order to save space in the .debug section (by eliminating entries
19439          which are probably useless).  Note that we must not suppress
19440          block-local extern declarations (whether used or not) because that
19441          would screw-up the debugger's name lookup mechanism and cause it to
19442          miss things which really ought to be in scope at a given point.  */
19443       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19444         return;
19445
19446       /* For local statics lookup proper context die.  */
19447       if (TREE_STATIC (decl) && decl_function_context (decl))
19448         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19449
19450       /* If we are in terse mode, don't generate any DIEs to represent any
19451          variable declarations or definitions.  */
19452       if (debug_info_level <= DINFO_LEVEL_TERSE)
19453         return;
19454       break;
19455
19456     case CONST_DECL:
19457       if (debug_info_level <= DINFO_LEVEL_TERSE)
19458         return;
19459       if (!is_fortran () && !is_ada ())
19460         return;
19461       if (TREE_STATIC (decl) && decl_function_context (decl))
19462         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19463       break;
19464
19465     case NAMESPACE_DECL:
19466     case IMPORTED_DECL:
19467       if (debug_info_level <= DINFO_LEVEL_TERSE)
19468         return;
19469       if (lookup_decl_die (decl) != NULL)
19470         return;
19471       break;
19472
19473     case TYPE_DECL:
19474       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19475       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19476         return;
19477
19478       /* Don't bother trying to generate any DIEs to represent any of the
19479          normal built-in types for the language we are compiling.  */
19480       if (DECL_IS_BUILTIN (decl))
19481         return;
19482
19483       /* If we are in terse mode, don't generate any DIEs for types.  */
19484       if (debug_info_level <= DINFO_LEVEL_TERSE)
19485         return;
19486
19487       /* If we're a function-scope tag, initially use a parent of NULL;
19488          this will be fixed up in decls_for_scope.  */
19489       if (decl_function_context (decl))
19490         context_die = NULL;
19491
19492       break;
19493
19494     default:
19495       return;
19496     }
19497
19498   gen_decl_die (decl, NULL, context_die);
19499 }
19500
19501 /* Write the debugging output for DECL.  */
19502
19503 static void
19504 dwarf2out_function_decl (tree decl)
19505 {
19506   dwarf2out_decl (decl);
19507   call_arg_locations = NULL;
19508   call_arg_loc_last = NULL;
19509   call_site_count = -1;
19510   tail_call_site_count = -1;
19511   VEC_free (dw_die_ref, heap, block_map);
19512   htab_empty (decl_loc_table);
19513   htab_empty (cached_dw_loc_list_table);
19514 }
19515
19516 /* Output a marker (i.e. a label) for the beginning of the generated code for
19517    a lexical block.  */
19518
19519 static void
19520 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19521                        unsigned int blocknum)
19522 {
19523   switch_to_section (current_function_section ());
19524   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19525 }
19526
19527 /* Output a marker (i.e. a label) for the end of the generated code for a
19528    lexical block.  */
19529
19530 static void
19531 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19532 {
19533   switch_to_section (current_function_section ());
19534   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19535 }
19536
19537 /* Returns nonzero if it is appropriate not to emit any debugging
19538    information for BLOCK, because it doesn't contain any instructions.
19539
19540    Don't allow this for blocks with nested functions or local classes
19541    as we would end up with orphans, and in the presence of scheduling
19542    we may end up calling them anyway.  */
19543
19544 static bool
19545 dwarf2out_ignore_block (const_tree block)
19546 {
19547   tree decl;
19548   unsigned int i;
19549
19550   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19551     if (TREE_CODE (decl) == FUNCTION_DECL
19552         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19553       return 0;
19554   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19555     {
19556       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19557       if (TREE_CODE (decl) == FUNCTION_DECL
19558           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19559       return 0;
19560     }
19561
19562   return 1;
19563 }
19564
19565 /* Hash table routines for file_hash.  */
19566
19567 static int
19568 file_table_eq (const void *p1_p, const void *p2_p)
19569 {
19570   const struct dwarf_file_data *const p1 =
19571     (const struct dwarf_file_data *) p1_p;
19572   const char *const p2 = (const char *) p2_p;
19573   return filename_cmp (p1->filename, p2) == 0;
19574 }
19575
19576 static hashval_t
19577 file_table_hash (const void *p_p)
19578 {
19579   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19580   return htab_hash_string (p->filename);
19581 }
19582
19583 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19584    dwarf2out.c) and return its "index".  The index of each (known) filename is
19585    just a unique number which is associated with only that one filename.  We
19586    need such numbers for the sake of generating labels (in the .debug_sfnames
19587    section) and references to those files numbers (in the .debug_srcinfo
19588    and.debug_macinfo sections).  If the filename given as an argument is not
19589    found in our current list, add it to the list and assign it the next
19590    available unique index number.  In order to speed up searches, we remember
19591    the index of the filename was looked up last.  This handles the majority of
19592    all searches.  */
19593
19594 static struct dwarf_file_data *
19595 lookup_filename (const char *file_name)
19596 {
19597   void ** slot;
19598   struct dwarf_file_data * created;
19599
19600   /* Check to see if the file name that was searched on the previous
19601      call matches this file name.  If so, return the index.  */
19602   if (file_table_last_lookup
19603       && (file_name == file_table_last_lookup->filename
19604           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19605     return file_table_last_lookup;
19606
19607   /* Didn't match the previous lookup, search the table.  */
19608   slot = htab_find_slot_with_hash (file_table, file_name,
19609                                    htab_hash_string (file_name), INSERT);
19610   if (*slot)
19611     return (struct dwarf_file_data *) *slot;
19612
19613   created = ggc_alloc_dwarf_file_data ();
19614   created->filename = file_name;
19615   created->emitted_number = 0;
19616   *slot = created;
19617   return created;
19618 }
19619
19620 /* If the assembler will construct the file table, then translate the compiler
19621    internal file table number into the assembler file table number, and emit
19622    a .file directive if we haven't already emitted one yet.  The file table
19623    numbers are different because we prune debug info for unused variables and
19624    types, which may include filenames.  */
19625
19626 static int
19627 maybe_emit_file (struct dwarf_file_data * fd)
19628 {
19629   if (! fd->emitted_number)
19630     {
19631       if (last_emitted_file)
19632         fd->emitted_number = last_emitted_file->emitted_number + 1;
19633       else
19634         fd->emitted_number = 1;
19635       last_emitted_file = fd;
19636
19637       if (DWARF2_ASM_LINE_DEBUG_INFO)
19638         {
19639           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19640           output_quoted_string (asm_out_file,
19641                                 remap_debug_filename (fd->filename));
19642           fputc ('\n', asm_out_file);
19643         }
19644     }
19645
19646   return fd->emitted_number;
19647 }
19648
19649 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19650    That generation should happen after function debug info has been
19651    generated. The value of the attribute is the constant value of ARG.  */
19652
19653 static void
19654 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19655 {
19656   die_arg_entry entry;
19657
19658   if (!die || !arg)
19659     return;
19660
19661   if (!tmpl_value_parm_die_table)
19662     tmpl_value_parm_die_table
19663       = VEC_alloc (die_arg_entry, gc, 32);
19664
19665   entry.die = die;
19666   entry.arg = arg;
19667   VEC_safe_push (die_arg_entry, gc,
19668                  tmpl_value_parm_die_table,
19669                  &entry);
19670 }
19671
19672 /* Return TRUE if T is an instance of generic type, FALSE
19673    otherwise.  */
19674
19675 static bool
19676 generic_type_p (tree t)
19677 {
19678   if (t == NULL_TREE || !TYPE_P (t))
19679     return false;
19680   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19681 }
19682
19683 /* Schedule the generation of the generic parameter dies for the
19684   instance of generic type T. The proper generation itself is later
19685   done by gen_scheduled_generic_parms_dies. */
19686
19687 static void
19688 schedule_generic_params_dies_gen (tree t)
19689 {
19690   if (!generic_type_p (t))
19691     return;
19692
19693   if (generic_type_instances == NULL)
19694     generic_type_instances = VEC_alloc (tree, gc, 256);
19695
19696   VEC_safe_push (tree, gc, generic_type_instances, t);
19697 }
19698
19699 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19700    by append_entry_to_tmpl_value_parm_die_table. This function must
19701    be called after function DIEs have been generated.  */
19702
19703 static void
19704 gen_remaining_tmpl_value_param_die_attribute (void)
19705 {
19706   if (tmpl_value_parm_die_table)
19707     {
19708       unsigned i;
19709       die_arg_entry *e;
19710
19711       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19712         tree_add_const_value_attribute (e->die, e->arg);
19713     }
19714 }
19715
19716 /* Generate generic parameters DIEs for instances of generic types
19717    that have been previously scheduled by
19718    schedule_generic_params_dies_gen. This function must be called
19719    after all the types of the CU have been laid out.  */
19720
19721 static void
19722 gen_scheduled_generic_parms_dies (void)
19723 {
19724   unsigned i;
19725   tree t;
19726
19727   if (generic_type_instances == NULL)
19728     return;
19729   
19730   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19731     gen_generic_params_dies (t);
19732 }
19733
19734
19735 /* Replace DW_AT_name for the decl with name.  */
19736
19737 static void
19738 dwarf2out_set_name (tree decl, tree name)
19739 {
19740   dw_die_ref die;
19741   dw_attr_ref attr;
19742   const char *dname;
19743
19744   die = TYPE_SYMTAB_DIE (decl);
19745   if (!die)
19746     return;
19747
19748   dname = dwarf2_name (name, 0);
19749   if (!dname)
19750     return;
19751
19752   attr = get_AT (die, DW_AT_name);
19753   if (attr)
19754     {
19755       struct indirect_string_node *node;
19756
19757       node = find_AT_string (dname);
19758       /* replace the string.  */
19759       attr->dw_attr_val.v.val_str = node;
19760     }
19761
19762   else
19763     add_name_attribute (die, dname);
19764 }
19765
19766 /* Called by the final INSN scan whenever we see a var location.  We
19767    use it to drop labels in the right places, and throw the location in
19768    our lookup table.  */
19769
19770 static void
19771 dwarf2out_var_location (rtx loc_note)
19772 {
19773   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19774   struct var_loc_node *newloc;
19775   rtx next_real;
19776   static const char *last_label;
19777   static const char *last_postcall_label;
19778   static bool last_in_cold_section_p;
19779   tree decl;
19780   bool var_loc_p;
19781
19782   if (!NOTE_P (loc_note))
19783     {
19784       if (CALL_P (loc_note))
19785         {
19786           call_site_count++;
19787           if (SIBLING_CALL_P (loc_note))
19788             tail_call_site_count++;
19789         }
19790       return;
19791     }
19792
19793   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
19794   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
19795     return;
19796
19797   next_real = next_real_insn (loc_note);
19798
19799   /* If there are no instructions which would be affected by this note,
19800      don't do anything.  */
19801   if (var_loc_p
19802       && next_real == NULL_RTX
19803       && !NOTE_DURING_CALL_P (loc_note))
19804     return;
19805
19806   if (next_real == NULL_RTX)
19807     next_real = get_last_insn ();
19808
19809   /* If there were any real insns between note we processed last time
19810      and this note (or if it is the first note), clear
19811      last_{,postcall_}label so that they are not reused this time.  */
19812   if (last_var_location_insn == NULL_RTX
19813       || last_var_location_insn != next_real
19814       || last_in_cold_section_p != in_cold_section_p)
19815     {
19816       last_label = NULL;
19817       last_postcall_label = NULL;
19818     }
19819
19820   if (var_loc_p)
19821     {
19822       decl = NOTE_VAR_LOCATION_DECL (loc_note);
19823       newloc = add_var_loc_to_decl (decl, loc_note,
19824                                     NOTE_DURING_CALL_P (loc_note)
19825                                     ? last_postcall_label : last_label);
19826       if (newloc == NULL)
19827         return;
19828     }
19829   else
19830     {
19831       decl = NULL_TREE;
19832       newloc = NULL;
19833     }
19834
19835   /* If there were no real insns between note we processed last time
19836      and this note, use the label we emitted last time.  Otherwise
19837      create a new label and emit it.  */
19838   if (last_label == NULL)
19839     {
19840       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
19841       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
19842       loclabel_num++;
19843       last_label = ggc_strdup (loclabel);
19844     }
19845
19846   if (!var_loc_p)
19847     {
19848       struct call_arg_loc_node *ca_loc
19849         = ggc_alloc_cleared_call_arg_loc_node ();
19850       rtx prev = prev_real_insn (loc_note), x;
19851       ca_loc->call_arg_loc_note = loc_note;
19852       ca_loc->next = NULL;
19853       ca_loc->label = last_label;
19854       gcc_assert (prev
19855                   && (CALL_P (prev)
19856                       || (NONJUMP_INSN_P (prev)
19857                           && GET_CODE (PATTERN (prev)) == SEQUENCE
19858                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
19859       if (!CALL_P (prev))
19860         prev = XVECEXP (PATTERN (prev), 0, 0);
19861       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
19862       x = PATTERN (prev);
19863       if (GET_CODE (x) == PARALLEL)
19864         x = XVECEXP (x, 0, 0);
19865       if (GET_CODE (x) == SET)
19866         x = SET_SRC (x);
19867       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
19868         {
19869           x = XEXP (XEXP (x, 0), 0);
19870           if (GET_CODE (x) == SYMBOL_REF
19871               && SYMBOL_REF_DECL (x)
19872               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
19873             ca_loc->symbol_ref = x;
19874         }
19875       ca_loc->block = insn_scope (prev);
19876       if (call_arg_locations)
19877         call_arg_loc_last->next = ca_loc;
19878       else
19879         call_arg_locations = ca_loc;
19880       call_arg_loc_last = ca_loc;
19881     }
19882   else if (!NOTE_DURING_CALL_P (loc_note))
19883     newloc->label = last_label;
19884   else
19885     {
19886       if (!last_postcall_label)
19887         {
19888           sprintf (loclabel, "%s-1", last_label);
19889           last_postcall_label = ggc_strdup (loclabel);
19890         }
19891       newloc->label = last_postcall_label;
19892     }
19893
19894   last_var_location_insn = next_real;
19895   last_in_cold_section_p = in_cold_section_p;
19896 }
19897
19898 /* Note in one location list that text section has changed.  */
19899
19900 static int
19901 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
19902 {
19903   var_loc_list *list = (var_loc_list *) *slot;
19904   if (list->first)
19905     list->last_before_switch
19906       = list->last->next ? list->last->next : list->last;
19907   return 1;
19908 }
19909
19910 /* Note in all location lists that text section has changed.  */
19911
19912 static void
19913 var_location_switch_text_section (void)
19914 {
19915   if (decl_loc_table == NULL)
19916     return;
19917
19918   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
19919 }
19920
19921 /* Create a new line number table.  */
19922
19923 static dw_line_info_table *
19924 new_line_info_table (void)
19925 {
19926   dw_line_info_table *table;
19927
19928   table = ggc_alloc_cleared_dw_line_info_table_struct ();
19929   table->file_num = 1;
19930   table->line_num = 1;
19931   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
19932
19933   return table;
19934 }
19935
19936 /* Lookup the "current" table into which we emit line info, so
19937    that we don't have to do it for every source line.  */
19938
19939 static void
19940 set_cur_line_info_table (section *sec)
19941 {
19942   dw_line_info_table *table;
19943
19944   if (sec == text_section)
19945     table = text_section_line_info;
19946   else if (sec == cold_text_section)
19947     {
19948       table = cold_text_section_line_info;
19949       if (!table)
19950         {
19951           cold_text_section_line_info = table = new_line_info_table ();
19952           table->end_label = cold_end_label;
19953         }
19954     }
19955   else
19956     {
19957       const char *end_label;
19958
19959       if (flag_reorder_blocks_and_partition)
19960         {
19961           if (in_cold_section_p)
19962             end_label = crtl->subsections.cold_section_end_label;
19963           else
19964             end_label = crtl->subsections.hot_section_end_label;
19965         }
19966       else
19967         {
19968           char label[MAX_ARTIFICIAL_LABEL_BYTES];
19969           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
19970                                        current_function_funcdef_no);
19971           end_label = ggc_strdup (label);
19972         }
19973
19974       table = new_line_info_table ();
19975       table->end_label = end_label;
19976
19977       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
19978     }
19979
19980   cur_line_info_table = table;
19981 }
19982
19983
19984 /* We need to reset the locations at the beginning of each
19985    function. We can't do this in the end_function hook, because the
19986    declarations that use the locations won't have been output when
19987    that hook is called.  Also compute have_multiple_function_sections here.  */
19988
19989 static void
19990 dwarf2out_begin_function (tree fun)
19991 {
19992   section *sec = function_section (fun);
19993
19994   if (sec != text_section)
19995     have_multiple_function_sections = true;
19996
19997   if (flag_reorder_blocks_and_partition && !cold_text_section)
19998     {
19999       gcc_assert (current_function_decl == fun);
20000       cold_text_section = unlikely_text_section ();
20001       switch_to_section (cold_text_section);
20002       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20003       switch_to_section (sec);
20004     }
20005
20006   dwarf2out_note_section_used ();
20007   call_site_count = 0;
20008   tail_call_site_count = 0;
20009
20010   set_cur_line_info_table (sec);
20011 }
20012
20013 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20014
20015 static void
20016 push_dw_line_info_entry (dw_line_info_table *table,
20017                          enum dw_line_info_opcode opcode, unsigned int val)
20018 {
20019   dw_line_info_entry e;
20020   e.opcode = opcode;
20021   e.val = val;
20022   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20023 }
20024
20025 /* Output a label to mark the beginning of a source code line entry
20026    and record information relating to this source line, in
20027    'line_info_table' for later output of the .debug_line section.  */
20028 /* ??? The discriminator parameter ought to be unsigned.  */
20029
20030 static void
20031 dwarf2out_source_line (unsigned int line, const char *filename,
20032                        int discriminator, bool is_stmt)
20033 {
20034   unsigned int file_num;
20035   dw_line_info_table *table;
20036
20037   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20038     return;
20039
20040   /* The discriminator column was added in dwarf4.  Simplify the below
20041      by simply removing it if we're not supposed to output it.  */
20042   if (dwarf_version < 4 && dwarf_strict)
20043     discriminator = 0;
20044
20045   table = cur_line_info_table;
20046   file_num = maybe_emit_file (lookup_filename (filename));
20047
20048   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20049      the debugger has used the second (possibly duplicate) line number
20050      at the beginning of the function to mark the end of the prologue.
20051      We could eliminate any other duplicates within the function.  For
20052      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20053      that second line number entry.  */
20054   /* Recall that this end-of-prologue indication is *not* the same thing
20055      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20056      to which the hook corresponds, follows the last insn that was 
20057      emitted by gen_prologue.  What we need is to preceed the first insn
20058      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20059      insn that corresponds to something the user wrote.  These may be
20060      very different locations once scheduling is enabled.  */
20061
20062   if (0 && file_num == table->file_num
20063       && line == table->line_num
20064       && discriminator == table->discrim_num
20065       && is_stmt == table->is_stmt)
20066     return;
20067
20068   switch_to_section (current_function_section ());
20069
20070   /* If requested, emit something human-readable.  */
20071   if (flag_debug_asm)
20072     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20073
20074   if (DWARF2_ASM_LINE_DEBUG_INFO)
20075     {
20076       /* Emit the .loc directive understood by GNU as.  */
20077       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20078       if (is_stmt != table->is_stmt)
20079         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20080       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20081         fprintf (asm_out_file, " discriminator %d", discriminator);
20082       fputc ('\n', asm_out_file);
20083     }
20084   else
20085     {
20086       unsigned int label_num = ++line_info_label_num;
20087
20088       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20089
20090       push_dw_line_info_entry (table, LI_set_address, label_num);
20091       if (file_num != table->file_num)
20092         push_dw_line_info_entry (table, LI_set_file, file_num);
20093       if (discriminator != table->discrim_num)
20094         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20095       if (is_stmt != table->is_stmt)
20096         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20097       push_dw_line_info_entry (table, LI_set_line, line);
20098     }
20099
20100   table->file_num = file_num;
20101   table->line_num = line;
20102   table->discrim_num = discriminator;
20103   table->is_stmt = is_stmt;
20104   table->in_use = true;
20105 }
20106
20107 /* Record the beginning of a new source file.  */
20108
20109 static void
20110 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20111 {
20112   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20113     {
20114       /* Record the beginning of the file for break_out_includes.  */
20115       dw_die_ref bincl_die;
20116
20117       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20118       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20119     }
20120
20121   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20122     {
20123       macinfo_entry e;
20124       e.code = DW_MACINFO_start_file;
20125       e.lineno = lineno;
20126       e.info = xstrdup (filename);
20127       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20128     }
20129 }
20130
20131 /* Record the end of a source file.  */
20132
20133 static void
20134 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20135 {
20136   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20137     /* Record the end of the file for break_out_includes.  */
20138     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20139
20140   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20141     {
20142       macinfo_entry e;
20143       e.code = DW_MACINFO_end_file;
20144       e.lineno = lineno;
20145       e.info = NULL;
20146       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20147     }
20148 }
20149
20150 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20151    the tail part of the directive line, i.e. the part which is past the
20152    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20153
20154 static void
20155 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20156                   const char *buffer ATTRIBUTE_UNUSED)
20157 {
20158   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20159     {
20160       macinfo_entry e;
20161       e.code = DW_MACINFO_define;
20162       e.lineno = lineno;
20163       e.info = xstrdup (buffer);;
20164       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20165     }
20166 }
20167
20168 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20169    the tail part of the directive line, i.e. the part which is past the
20170    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20171
20172 static void
20173 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20174                  const char *buffer ATTRIBUTE_UNUSED)
20175 {
20176   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20177     {
20178       macinfo_entry e;
20179       e.code = DW_MACINFO_undef;
20180       e.lineno = lineno;
20181       e.info = xstrdup (buffer);;
20182       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20183     }
20184 }
20185
20186 static void
20187 output_macinfo (void)
20188 {
20189   unsigned i;
20190   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20191   macinfo_entry *ref;
20192
20193   if (! length)
20194     return;
20195
20196   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20197     {
20198       switch (ref->code)
20199         {
20200           case DW_MACINFO_start_file:
20201             {
20202               int file_num = maybe_emit_file (lookup_filename (ref->info));
20203               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20204               dw2_asm_output_data_uleb128 
20205                         (ref->lineno, "Included from line number %lu", 
20206                                                 (unsigned long)ref->lineno);
20207               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20208             }
20209             break;
20210           case DW_MACINFO_end_file:
20211             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20212             break;
20213           case DW_MACINFO_define:
20214             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20215             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20216                                                 (unsigned long)ref->lineno);
20217             dw2_asm_output_nstring (ref->info, -1, "The macro");
20218             break;
20219           case DW_MACINFO_undef:
20220             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20221             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20222                                                 (unsigned long)ref->lineno);
20223             dw2_asm_output_nstring (ref->info, -1, "The macro");
20224             break;
20225           default:
20226            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20227              ASM_COMMENT_START, (unsigned long)ref->code);
20228           break;
20229         }
20230     }
20231 }
20232
20233 /* Set up for Dwarf output at the start of compilation.  */
20234
20235 static void
20236 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20237 {
20238   /* Allocate the file_table.  */
20239   file_table = htab_create_ggc (50, file_table_hash,
20240                                 file_table_eq, NULL);
20241
20242   /* Allocate the decl_die_table.  */
20243   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20244                                     decl_die_table_eq, NULL);
20245
20246   /* Allocate the decl_loc_table.  */
20247   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20248                                     decl_loc_table_eq, NULL);
20249
20250   /* Allocate the cached_dw_loc_list_table.  */
20251   cached_dw_loc_list_table
20252     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20253                        cached_dw_loc_list_table_eq, NULL);
20254
20255   /* Allocate the initial hunk of the decl_scope_table.  */
20256   decl_scope_table = VEC_alloc (tree, gc, 256);
20257
20258   /* Allocate the initial hunk of the abbrev_die_table.  */
20259   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20260     (ABBREV_DIE_TABLE_INCREMENT);
20261   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20262   /* Zero-th entry is allocated, but unused.  */
20263   abbrev_die_table_in_use = 1;
20264
20265   /* Allocate the pubtypes and pubnames vectors.  */
20266   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20267   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20268
20269   incomplete_types = VEC_alloc (tree, gc, 64);
20270
20271   used_rtx_array = VEC_alloc (rtx, gc, 32);
20272
20273   debug_info_section = get_section (DEBUG_INFO_SECTION,
20274                                     SECTION_DEBUG, NULL);
20275   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20276                                       SECTION_DEBUG, NULL);
20277   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20278                                        SECTION_DEBUG, NULL);
20279   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20280                                        SECTION_DEBUG, NULL);
20281   debug_line_section = get_section (DEBUG_LINE_SECTION,
20282                                     SECTION_DEBUG, NULL);
20283   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20284                                    SECTION_DEBUG, NULL);
20285   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20286                                         SECTION_DEBUG, NULL);
20287   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20288                                         SECTION_DEBUG, NULL);
20289   debug_str_section = get_section (DEBUG_STR_SECTION,
20290                                    DEBUG_STR_SECTION_FLAGS, NULL);
20291   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20292                                       SECTION_DEBUG, NULL);
20293   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20294                                      SECTION_DEBUG, NULL);
20295
20296   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20297   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20298                                DEBUG_ABBREV_SECTION_LABEL, 0);
20299   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20300   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20301                                COLD_TEXT_SECTION_LABEL, 0);
20302   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20303
20304   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20305                                DEBUG_INFO_SECTION_LABEL, 0);
20306   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20307                                DEBUG_LINE_SECTION_LABEL, 0);
20308   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20309                                DEBUG_RANGES_SECTION_LABEL, 0);
20310   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20311                                DEBUG_MACINFO_SECTION_LABEL, 0);
20312
20313   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20314     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20315
20316   switch_to_section (text_section);
20317   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20318
20319   /* Make sure the line number table for .text always exists.  */
20320   text_section_line_info = new_line_info_table ();
20321   text_section_line_info->end_label = text_end_label;
20322 }
20323
20324 /* Called before cgraph_optimize starts outputtting functions, variables
20325    and toplevel asms into assembly.  */
20326
20327 static void
20328 dwarf2out_assembly_start (void)
20329 {
20330   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20331       && dwarf2out_do_cfi_asm ()
20332       && (!(flag_unwind_tables || flag_exceptions)
20333           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20334     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20335 }
20336
20337 /* A helper function for dwarf2out_finish called through
20338    htab_traverse.  Emit one queued .debug_str string.  */
20339
20340 static int
20341 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20342 {
20343   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20344
20345   if (node->form == DW_FORM_strp)
20346     {
20347       switch_to_section (debug_str_section);
20348       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20349       assemble_string (node->str, strlen (node->str) + 1);
20350     }
20351
20352   return 1;
20353 }
20354
20355 #if ENABLE_ASSERT_CHECKING
20356 /* Verify that all marks are clear.  */
20357
20358 static void
20359 verify_marks_clear (dw_die_ref die)
20360 {
20361   dw_die_ref c;
20362
20363   gcc_assert (! die->die_mark);
20364   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20365 }
20366 #endif /* ENABLE_ASSERT_CHECKING */
20367
20368 /* Clear the marks for a die and its children.
20369    Be cool if the mark isn't set.  */
20370
20371 static void
20372 prune_unmark_dies (dw_die_ref die)
20373 {
20374   dw_die_ref c;
20375
20376   if (die->die_mark)
20377     die->die_mark = 0;
20378   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20379 }
20380
20381 /* Given DIE that we're marking as used, find any other dies
20382    it references as attributes and mark them as used.  */
20383
20384 static void
20385 prune_unused_types_walk_attribs (dw_die_ref die)
20386 {
20387   dw_attr_ref a;
20388   unsigned ix;
20389
20390   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20391     {
20392       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20393         {
20394           /* A reference to another DIE.
20395              Make sure that it will get emitted.
20396              If it was broken out into a comdat group, don't follow it.  */
20397           if (! use_debug_types
20398               || a->dw_attr == DW_AT_specification
20399               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20400             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20401         }
20402       /* Set the string's refcount to 0 so that prune_unused_types_mark
20403          accounts properly for it.  */
20404       if (AT_class (a) == dw_val_class_str)
20405         a->dw_attr_val.v.val_str->refcount = 0;
20406     }
20407 }
20408
20409 /* Mark the generic parameters and arguments children DIEs of DIE.  */
20410
20411 static void
20412 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20413 {
20414   dw_die_ref c;
20415
20416   if (die == NULL || die->die_child == NULL)
20417     return;
20418   c = die->die_child;
20419   do
20420     {
20421       switch (c->die_tag)
20422         {
20423         case DW_TAG_template_type_param:
20424         case DW_TAG_template_value_param:
20425         case DW_TAG_GNU_template_template_param:
20426         case DW_TAG_GNU_template_parameter_pack:
20427           prune_unused_types_mark (c, 1);
20428           break;
20429         default:
20430           break;
20431         }
20432       c = c->die_sib;
20433     } while (c && c != die->die_child);
20434 }
20435
20436 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20437    to DIE's children.  */
20438
20439 static void
20440 prune_unused_types_mark (dw_die_ref die, int dokids)
20441 {
20442   dw_die_ref c;
20443
20444   if (die->die_mark == 0)
20445     {
20446       /* We haven't done this node yet.  Mark it as used.  */
20447       die->die_mark = 1;
20448       /* If this is the DIE of a generic type instantiation,
20449          mark the children DIEs that describe its generic parms and
20450          args.  */
20451       prune_unused_types_mark_generic_parms_dies (die);
20452
20453       /* We also have to mark its parents as used.
20454          (But we don't want to mark our parents' kids due to this.)  */
20455       if (die->die_parent)
20456         prune_unused_types_mark (die->die_parent, 0);
20457
20458       /* Mark any referenced nodes.  */
20459       prune_unused_types_walk_attribs (die);
20460
20461       /* If this node is a specification,
20462          also mark the definition, if it exists.  */
20463       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20464         prune_unused_types_mark (die->die_definition, 1);
20465     }
20466
20467   if (dokids && die->die_mark != 2)
20468     {
20469       /* We need to walk the children, but haven't done so yet.
20470          Remember that we've walked the kids.  */
20471       die->die_mark = 2;
20472
20473       /* If this is an array type, we need to make sure our
20474          kids get marked, even if they're types.  If we're
20475          breaking out types into comdat sections, do this
20476          for all type definitions.  */
20477       if (die->die_tag == DW_TAG_array_type
20478           || (use_debug_types
20479               && is_type_die (die) && ! is_declaration_die (die)))
20480         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20481       else
20482         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20483     }
20484 }
20485
20486 /* For local classes, look if any static member functions were emitted
20487    and if so, mark them.  */
20488
20489 static void
20490 prune_unused_types_walk_local_classes (dw_die_ref die)
20491 {
20492   dw_die_ref c;
20493
20494   if (die->die_mark == 2)
20495     return;
20496
20497   switch (die->die_tag)
20498     {
20499     case DW_TAG_structure_type:
20500     case DW_TAG_union_type:
20501     case DW_TAG_class_type:
20502       break;
20503
20504     case DW_TAG_subprogram:
20505       if (!get_AT_flag (die, DW_AT_declaration)
20506           || die->die_definition != NULL)
20507         prune_unused_types_mark (die, 1);
20508       return;
20509
20510     default:
20511       return;
20512     }
20513
20514   /* Mark children.  */
20515   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20516 }
20517
20518 /* Walk the tree DIE and mark types that we actually use.  */
20519
20520 static void
20521 prune_unused_types_walk (dw_die_ref die)
20522 {
20523   dw_die_ref c;
20524
20525   /* Don't do anything if this node is already marked and
20526      children have been marked as well.  */
20527   if (die->die_mark == 2)
20528     return;
20529
20530   switch (die->die_tag)
20531     {
20532     case DW_TAG_structure_type:
20533     case DW_TAG_union_type:
20534     case DW_TAG_class_type:
20535       if (die->die_perennial_p)
20536         break;
20537
20538       for (c = die->die_parent; c; c = c->die_parent)
20539         if (c->die_tag == DW_TAG_subprogram)
20540           break;
20541
20542       /* Finding used static member functions inside of classes
20543          is needed just for local classes, because for other classes
20544          static member function DIEs with DW_AT_specification
20545          are emitted outside of the DW_TAG_*_type.  If we ever change
20546          it, we'd need to call this even for non-local classes.  */
20547       if (c)
20548         prune_unused_types_walk_local_classes (die);
20549
20550       /* It's a type node --- don't mark it.  */
20551       return;
20552
20553     case DW_TAG_const_type:
20554     case DW_TAG_packed_type:
20555     case DW_TAG_pointer_type:
20556     case DW_TAG_reference_type:
20557     case DW_TAG_rvalue_reference_type:
20558     case DW_TAG_volatile_type:
20559     case DW_TAG_typedef:
20560     case DW_TAG_array_type:
20561     case DW_TAG_interface_type:
20562     case DW_TAG_friend:
20563     case DW_TAG_variant_part:
20564     case DW_TAG_enumeration_type:
20565     case DW_TAG_subroutine_type:
20566     case DW_TAG_string_type:
20567     case DW_TAG_set_type:
20568     case DW_TAG_subrange_type:
20569     case DW_TAG_ptr_to_member_type:
20570     case DW_TAG_file_type:
20571       if (die->die_perennial_p)
20572         break;
20573
20574       /* It's a type node --- don't mark it.  */
20575       return;
20576
20577     default:
20578       /* Mark everything else.  */
20579       break;
20580   }
20581
20582   if (die->die_mark == 0)
20583     {
20584       die->die_mark = 1;
20585
20586       /* Now, mark any dies referenced from here.  */
20587       prune_unused_types_walk_attribs (die);
20588     }
20589
20590   die->die_mark = 2;
20591
20592   /* Mark children.  */
20593   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20594 }
20595
20596 /* Increment the string counts on strings referred to from DIE's
20597    attributes.  */
20598
20599 static void
20600 prune_unused_types_update_strings (dw_die_ref die)
20601 {
20602   dw_attr_ref a;
20603   unsigned ix;
20604
20605   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20606     if (AT_class (a) == dw_val_class_str)
20607       {
20608         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20609         s->refcount++;
20610         /* Avoid unnecessarily putting strings that are used less than
20611            twice in the hash table.  */
20612         if (s->refcount
20613             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20614           {
20615             void ** slot;
20616             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20617                                              htab_hash_string (s->str),
20618                                              INSERT);
20619             gcc_assert (*slot == NULL);
20620             *slot = s;
20621           }
20622       }
20623 }
20624
20625 /* Remove from the tree DIE any dies that aren't marked.  */
20626
20627 static void
20628 prune_unused_types_prune (dw_die_ref die)
20629 {
20630   dw_die_ref c;
20631
20632   gcc_assert (die->die_mark);
20633   prune_unused_types_update_strings (die);
20634
20635   if (! die->die_child)
20636     return;
20637
20638   c = die->die_child;
20639   do {
20640     dw_die_ref prev = c;
20641     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20642       if (c == die->die_child)
20643         {
20644           /* No marked children between 'prev' and the end of the list.  */
20645           if (prev == c)
20646             /* No marked children at all.  */
20647             die->die_child = NULL;
20648           else
20649             {
20650               prev->die_sib = c->die_sib;
20651               die->die_child = prev;
20652             }
20653           return;
20654         }
20655
20656     if (c != prev->die_sib)
20657       prev->die_sib = c;
20658     prune_unused_types_prune (c);
20659   } while (c != die->die_child);
20660 }
20661
20662 /* Remove dies representing declarations that we never use.  */
20663
20664 static void
20665 prune_unused_types (void)
20666 {
20667   unsigned int i;
20668   limbo_die_node *node;
20669   comdat_type_node *ctnode;
20670   pubname_ref pub;
20671   dw_die_ref base_type;
20672
20673 #if ENABLE_ASSERT_CHECKING
20674   /* All the marks should already be clear.  */
20675   verify_marks_clear (comp_unit_die ());
20676   for (node = limbo_die_list; node; node = node->next)
20677     verify_marks_clear (node->die);
20678   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20679     verify_marks_clear (ctnode->root_die);
20680 #endif /* ENABLE_ASSERT_CHECKING */
20681
20682   /* Mark types that are used in global variables.  */
20683   premark_types_used_by_global_vars ();
20684
20685   /* Set the mark on nodes that are actually used.  */
20686   prune_unused_types_walk (comp_unit_die ());
20687   for (node = limbo_die_list; node; node = node->next)
20688     prune_unused_types_walk (node->die);
20689   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20690     {
20691       prune_unused_types_walk (ctnode->root_die);
20692       prune_unused_types_mark (ctnode->type_die, 1);
20693     }
20694
20695   /* Also set the mark on nodes referenced from the
20696      pubname_table.  */
20697   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
20698     prune_unused_types_mark (pub->die, 1);
20699   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
20700     prune_unused_types_mark (base_type, 1);
20701
20702   if (debug_str_hash)
20703     htab_empty (debug_str_hash);
20704   prune_unused_types_prune (comp_unit_die ());
20705   for (node = limbo_die_list; node; node = node->next)
20706     prune_unused_types_prune (node->die);
20707   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20708     prune_unused_types_prune (ctnode->root_die);
20709
20710   /* Leave the marks clear.  */
20711   prune_unmark_dies (comp_unit_die ());
20712   for (node = limbo_die_list; node; node = node->next)
20713     prune_unmark_dies (node->die);
20714   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20715     prune_unmark_dies (ctnode->root_die);
20716 }
20717
20718 /* Set the parameter to true if there are any relative pathnames in
20719    the file table.  */
20720 static int
20721 file_table_relative_p (void ** slot, void *param)
20722 {
20723   bool *p = (bool *) param;
20724   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20725   if (!IS_ABSOLUTE_PATH (d->filename))
20726     {
20727       *p = true;
20728       return 0;
20729     }
20730   return 1;
20731 }
20732
20733 /* Routines to manipulate hash table of comdat type units.  */
20734
20735 static hashval_t
20736 htab_ct_hash (const void *of)
20737 {
20738   hashval_t h;
20739   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20740
20741   memcpy (&h, type_node->signature, sizeof (h));
20742   return h;
20743 }
20744
20745 static int
20746 htab_ct_eq (const void *of1, const void *of2)
20747 {
20748   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20749   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20750
20751   return (! memcmp (type_node_1->signature, type_node_2->signature,
20752                     DWARF_TYPE_SIGNATURE_SIZE));
20753 }
20754
20755 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
20756    to the location it would have been added, should we know its
20757    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20758    probably improve compactness of debug info, removing equivalent
20759    abbrevs, and hide any differences caused by deferring the
20760    computation of the assembler name, triggered by e.g. PCH.  */
20761
20762 static inline void
20763 move_linkage_attr (dw_die_ref die)
20764 {
20765   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20766   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
20767
20768   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
20769               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
20770
20771   while (--ix > 0)
20772     {
20773       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
20774
20775       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
20776         break;
20777     }
20778
20779   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
20780     {
20781       VEC_pop (dw_attr_node, die->die_attr);
20782       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
20783     }
20784 }
20785
20786 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
20787    referenced from typed stack ops and count how often they are used.  */
20788
20789 static void
20790 mark_base_types (dw_loc_descr_ref loc)
20791 {
20792   dw_die_ref base_type = NULL;
20793
20794   for (; loc; loc = loc->dw_loc_next)
20795     {
20796       switch (loc->dw_loc_opc)
20797         {
20798         case DW_OP_GNU_regval_type:
20799         case DW_OP_GNU_deref_type:
20800           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
20801           break;
20802         case DW_OP_GNU_convert:
20803         case DW_OP_GNU_reinterpret:
20804           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
20805             continue;
20806           /* FALLTHRU */
20807         case DW_OP_GNU_const_type:
20808           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
20809           break;
20810         case DW_OP_GNU_entry_value:
20811           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
20812           continue;
20813         default:
20814           continue;
20815         }
20816       gcc_assert (base_type->die_parent == comp_unit_die ());
20817       if (base_type->die_mark)
20818         base_type->die_mark++;
20819       else
20820         {
20821           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
20822           base_type->die_mark = 1;
20823         }
20824     }
20825 }
20826
20827 /* Comparison function for sorting marked base types.  */
20828
20829 static int
20830 base_type_cmp (const void *x, const void *y)
20831 {
20832   dw_die_ref dx = *(const dw_die_ref *) x;
20833   dw_die_ref dy = *(const dw_die_ref *) y;
20834   unsigned int byte_size1, byte_size2;
20835   unsigned int encoding1, encoding2;
20836   if (dx->die_mark > dy->die_mark)
20837     return -1;
20838   if (dx->die_mark < dy->die_mark)
20839     return 1;
20840   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
20841   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
20842   if (byte_size1 < byte_size2)
20843     return 1;
20844   if (byte_size1 > byte_size2)
20845     return -1;
20846   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
20847   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
20848   if (encoding1 < encoding2)
20849     return 1;
20850   if (encoding1 > encoding2)
20851     return -1;
20852   return 0;
20853 }
20854
20855 /* Move base types marked by mark_base_types as early as possible
20856    in the CU, sorted by decreasing usage count both to make the
20857    uleb128 references as small as possible and to make sure they
20858    will have die_offset already computed by calc_die_sizes when
20859    sizes of typed stack loc ops is computed.  */
20860
20861 static void
20862 move_marked_base_types (void)
20863 {
20864   unsigned int i;
20865   dw_die_ref base_type, die, c;
20866
20867   if (VEC_empty (dw_die_ref, base_types))
20868     return;
20869
20870   /* Sort by decreasing usage count, they will be added again in that
20871      order later on.  */
20872   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
20873   die = comp_unit_die ();
20874   c = die->die_child;
20875   do
20876     {
20877       dw_die_ref prev = c;
20878       c = c->die_sib;
20879       while (c->die_mark)
20880         {
20881           remove_child_with_prev (c, prev);
20882           /* As base types got marked, there must be at least
20883              one node other than DW_TAG_base_type.  */
20884           gcc_assert (c != c->die_sib);
20885           c = c->die_sib;
20886         }
20887     }
20888   while (c != die->die_child);
20889   gcc_assert (die->die_child);
20890   c = die->die_child;
20891   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
20892     {
20893       base_type->die_mark = 0;
20894       base_type->die_sib = c->die_sib;
20895       c->die_sib = base_type;
20896       c = base_type;
20897     }
20898 }
20899
20900 /* Helper function for resolve_addr, attempt to resolve
20901    one CONST_STRING, return non-zero if not successful.  Similarly verify that
20902    SYMBOL_REFs refer to variables emitted in the current CU.  */
20903
20904 static int
20905 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
20906 {
20907   rtx rtl = *addr;
20908
20909   if (GET_CODE (rtl) == CONST_STRING)
20910     {
20911       size_t len = strlen (XSTR (rtl, 0)) + 1;
20912       tree t = build_string (len, XSTR (rtl, 0));
20913       tree tlen = size_int (len - 1);
20914       TREE_TYPE (t)
20915         = build_array_type (char_type_node, build_index_type (tlen));
20916       rtl = lookup_constant_def (t);
20917       if (!rtl || !MEM_P (rtl))
20918         return 1;
20919       rtl = XEXP (rtl, 0);
20920       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
20921       *addr = rtl;
20922       return 0;
20923     }
20924
20925   if (GET_CODE (rtl) == SYMBOL_REF
20926       && SYMBOL_REF_DECL (rtl))
20927     {
20928       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
20929         {
20930           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
20931             return 1;
20932         }
20933       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
20934         return 1;
20935     }
20936
20937   if (GET_CODE (rtl) == CONST
20938       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
20939     return 1;
20940
20941   return 0;
20942 }
20943
20944 /* Helper function for resolve_addr, handle one location
20945    expression, return false if at least one CONST_STRING or SYMBOL_REF in
20946    the location list couldn't be resolved.  */
20947
20948 static bool
20949 resolve_addr_in_expr (dw_loc_descr_ref loc)
20950 {
20951   dw_loc_descr_ref keep = NULL;
20952   for (; loc; loc = loc->dw_loc_next)
20953     switch (loc->dw_loc_opc)
20954       {
20955       case DW_OP_addr:
20956         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
20957           return false;
20958         break;
20959       case DW_OP_const4u:
20960       case DW_OP_const8u:
20961         if (loc->dtprel
20962             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
20963           return false;
20964         break;
20965       case DW_OP_implicit_value:
20966         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
20967             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
20968           return false;
20969         break;
20970       case DW_OP_GNU_implicit_pointer:
20971       case DW_OP_GNU_parameter_ref:
20972         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
20973           {
20974             dw_die_ref ref
20975               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
20976             if (ref == NULL)
20977               return false;
20978             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
20979             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
20980             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
20981           }
20982         break;
20983       case DW_OP_GNU_const_type:
20984       case DW_OP_GNU_regval_type:
20985       case DW_OP_GNU_deref_type:
20986       case DW_OP_GNU_convert:
20987       case DW_OP_GNU_reinterpret:
20988         while (loc->dw_loc_next
20989                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
20990           {
20991             dw_die_ref base1, base2;
20992             unsigned enc1, enc2, size1, size2;
20993             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
20994                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
20995               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
20996             else if (loc->dw_loc_oprnd1.val_class
20997                      == dw_val_class_unsigned_const)
20998               break;
20999             else
21000               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21001             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21002                 == dw_val_class_unsigned_const)
21003               break;
21004             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21005             gcc_assert (base1->die_tag == DW_TAG_base_type
21006                         && base2->die_tag == DW_TAG_base_type);
21007             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21008             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21009             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21010             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21011             if (size1 == size2
21012                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21013                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21014                      && loc != keep)
21015                     || enc1 == enc2))
21016               {
21017                 /* Optimize away next DW_OP_GNU_convert after
21018                    adjusting LOC's base type die reference.  */
21019                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21020                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21021                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21022                 else
21023                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21024                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21025                 continue;
21026               }
21027             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21028                point typed stack entry.  */
21029             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21030               keep = loc->dw_loc_next;
21031             break;
21032           }
21033         break;
21034       default:
21035         break;
21036       }
21037   return true;
21038 }
21039
21040 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21041    an address in .rodata section if the string literal is emitted there,
21042    or remove the containing location list or replace DW_AT_const_value
21043    with DW_AT_location and empty location expression, if it isn't found
21044    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21045    to something that has been emitted in the current CU.  */
21046
21047 static void
21048 resolve_addr (dw_die_ref die)
21049 {
21050   dw_die_ref c;
21051   dw_attr_ref a;
21052   dw_loc_list_ref *curr, *start, loc;
21053   unsigned ix;
21054
21055   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21056     switch (AT_class (a))
21057       {
21058       case dw_val_class_loc_list:
21059         start = curr = AT_loc_list_ptr (a);
21060         loc = *curr;
21061         gcc_assert (loc);
21062         /* The same list can be referenced more than once.  See if we have
21063            already recorded the result from a previous pass.  */
21064         if (loc->replaced)
21065           *curr = loc->dw_loc_next;
21066         else if (!loc->resolved_addr)
21067           {
21068             /* As things stand, we do not expect or allow one die to
21069                reference a suffix of another die's location list chain.
21070                References must be identical or completely separate.
21071                There is therefore no need to cache the result of this
21072                pass on any list other than the first; doing so
21073                would lead to unnecessary writes.  */
21074             while (*curr)
21075               {
21076                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21077                 if (!resolve_addr_in_expr ((*curr)->expr))
21078                   {
21079                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21080                     if (next && (*curr)->ll_symbol)
21081                       {
21082                         gcc_assert (!next->ll_symbol);
21083                         next->ll_symbol = (*curr)->ll_symbol;
21084                       }
21085                     *curr = next;
21086                   }
21087                 else
21088                   {
21089                     mark_base_types ((*curr)->expr);
21090                     curr = &(*curr)->dw_loc_next;
21091                   }
21092               }
21093             if (loc == *start)
21094               loc->resolved_addr = 1;
21095             else
21096               {
21097                 loc->replaced = 1;
21098                 loc->dw_loc_next = *start;
21099               }
21100           }
21101         if (!*start)
21102           {
21103             remove_AT (die, a->dw_attr);
21104             ix--;
21105           }
21106         break;
21107       case dw_val_class_loc:
21108         if (!resolve_addr_in_expr (AT_loc (a)))
21109           {
21110             remove_AT (die, a->dw_attr);
21111             ix--;
21112           }
21113         else
21114           mark_base_types (AT_loc (a));
21115         break;
21116       case dw_val_class_addr:
21117         if (a->dw_attr == DW_AT_const_value
21118             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21119           {
21120             remove_AT (die, a->dw_attr);
21121             ix--;
21122           }
21123         if (die->die_tag == DW_TAG_GNU_call_site
21124             && a->dw_attr == DW_AT_abstract_origin)
21125           {
21126             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21127             dw_die_ref tdie = lookup_decl_die (tdecl);
21128             if (tdie == NULL
21129                 && DECL_EXTERNAL (tdecl)
21130                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21131               {
21132                 force_decl_die (tdecl);
21133                 tdie = lookup_decl_die (tdecl);
21134               }
21135             if (tdie)
21136               {
21137                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21138                 a->dw_attr_val.v.val_die_ref.die = tdie;
21139                 a->dw_attr_val.v.val_die_ref.external = 0;
21140               }
21141             else
21142               {
21143                 remove_AT (die, a->dw_attr);
21144                 ix--;
21145               }
21146           }
21147         break;
21148       default:
21149         break;
21150       }
21151
21152   FOR_EACH_CHILD (die, c, resolve_addr (c));
21153 }
21154 \f
21155 /* Helper routines for optimize_location_lists.
21156    This pass tries to share identical local lists in .debug_loc
21157    section.  */
21158
21159 /* Iteratively hash operands of LOC opcode.  */
21160
21161 static inline hashval_t
21162 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21163 {
21164   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21165   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21166
21167   switch (loc->dw_loc_opc)
21168     {
21169     case DW_OP_const4u:
21170     case DW_OP_const8u:
21171       if (loc->dtprel)
21172         goto hash_addr;
21173       /* FALLTHRU */
21174     case DW_OP_const1u:
21175     case DW_OP_const1s:
21176     case DW_OP_const2u:
21177     case DW_OP_const2s:
21178     case DW_OP_const4s:
21179     case DW_OP_const8s:
21180     case DW_OP_constu:
21181     case DW_OP_consts:
21182     case DW_OP_pick:
21183     case DW_OP_plus_uconst:
21184     case DW_OP_breg0:
21185     case DW_OP_breg1:
21186     case DW_OP_breg2:
21187     case DW_OP_breg3:
21188     case DW_OP_breg4:
21189     case DW_OP_breg5:
21190     case DW_OP_breg6:
21191     case DW_OP_breg7:
21192     case DW_OP_breg8:
21193     case DW_OP_breg9:
21194     case DW_OP_breg10:
21195     case DW_OP_breg11:
21196     case DW_OP_breg12:
21197     case DW_OP_breg13:
21198     case DW_OP_breg14:
21199     case DW_OP_breg15:
21200     case DW_OP_breg16:
21201     case DW_OP_breg17:
21202     case DW_OP_breg18:
21203     case DW_OP_breg19:
21204     case DW_OP_breg20:
21205     case DW_OP_breg21:
21206     case DW_OP_breg22:
21207     case DW_OP_breg23:
21208     case DW_OP_breg24:
21209     case DW_OP_breg25:
21210     case DW_OP_breg26:
21211     case DW_OP_breg27:
21212     case DW_OP_breg28:
21213     case DW_OP_breg29:
21214     case DW_OP_breg30:
21215     case DW_OP_breg31:
21216     case DW_OP_regx:
21217     case DW_OP_fbreg:
21218     case DW_OP_piece:
21219     case DW_OP_deref_size:
21220     case DW_OP_xderef_size:
21221       hash = iterative_hash_object (val1->v.val_int, hash);
21222       break;
21223     case DW_OP_skip:
21224     case DW_OP_bra:
21225       {
21226         int offset;
21227
21228         gcc_assert (val1->val_class == dw_val_class_loc);
21229         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21230         hash = iterative_hash_object (offset, hash);
21231       }
21232       break;
21233     case DW_OP_implicit_value:
21234       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21235       switch (val2->val_class)
21236         {
21237         case dw_val_class_const:
21238           hash = iterative_hash_object (val2->v.val_int, hash);
21239           break;
21240         case dw_val_class_vec:
21241           {
21242             unsigned int elt_size = val2->v.val_vec.elt_size;
21243             unsigned int len = val2->v.val_vec.length;
21244
21245             hash = iterative_hash_object (elt_size, hash);
21246             hash = iterative_hash_object (len, hash);
21247             hash = iterative_hash (val2->v.val_vec.array,
21248                                    len * elt_size, hash);
21249           }
21250           break;
21251         case dw_val_class_const_double:
21252           hash = iterative_hash_object (val2->v.val_double.low, hash);
21253           hash = iterative_hash_object (val2->v.val_double.high, hash);
21254           break;
21255         case dw_val_class_addr:
21256           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21257           break;
21258         default:
21259           gcc_unreachable ();
21260         }
21261       break;
21262     case DW_OP_bregx:
21263     case DW_OP_bit_piece:
21264       hash = iterative_hash_object (val1->v.val_int, hash);
21265       hash = iterative_hash_object (val2->v.val_int, hash);
21266       break;
21267     case DW_OP_addr:
21268     hash_addr:
21269       if (loc->dtprel)
21270         {
21271           unsigned char dtprel = 0xd1;
21272           hash = iterative_hash_object (dtprel, hash);
21273         }
21274       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21275       break;
21276     case DW_OP_GNU_implicit_pointer:
21277       hash = iterative_hash_object (val2->v.val_int, hash);
21278       break;
21279     case DW_OP_GNU_entry_value:
21280       hash = hash_loc_operands (val1->v.val_loc, hash);
21281       break;
21282     case DW_OP_GNU_regval_type:
21283     case DW_OP_GNU_deref_type:
21284       {
21285         unsigned int byte_size
21286           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21287         unsigned int encoding
21288           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21289         hash = iterative_hash_object (val1->v.val_int, hash);
21290         hash = iterative_hash_object (byte_size, hash);
21291         hash = iterative_hash_object (encoding, hash);
21292       }
21293       break;
21294     case DW_OP_GNU_convert:
21295     case DW_OP_GNU_reinterpret:
21296       if (val1->val_class == dw_val_class_unsigned_const)
21297         {
21298           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21299           break;
21300         }
21301       /* FALLTHRU */
21302     case DW_OP_GNU_const_type:
21303       {
21304         unsigned int byte_size
21305           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21306         unsigned int encoding
21307           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21308         hash = iterative_hash_object (byte_size, hash);
21309         hash = iterative_hash_object (encoding, hash);
21310         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21311           break;
21312         hash = iterative_hash_object (val2->val_class, hash);
21313         switch (val2->val_class)
21314           {
21315           case dw_val_class_const:
21316             hash = iterative_hash_object (val2->v.val_int, hash);
21317             break;
21318           case dw_val_class_vec:
21319             {
21320               unsigned int elt_size = val2->v.val_vec.elt_size;
21321               unsigned int len = val2->v.val_vec.length;
21322
21323               hash = iterative_hash_object (elt_size, hash);
21324               hash = iterative_hash_object (len, hash);
21325               hash = iterative_hash (val2->v.val_vec.array,
21326                                      len * elt_size, hash);
21327             }
21328             break;
21329           case dw_val_class_const_double:
21330             hash = iterative_hash_object (val2->v.val_double.low, hash);
21331             hash = iterative_hash_object (val2->v.val_double.high, hash);
21332             break;
21333           default:
21334             gcc_unreachable ();
21335           }
21336       }
21337       break;
21338
21339     default:
21340       /* Other codes have no operands.  */
21341       break;
21342     }
21343   return hash;
21344 }
21345
21346 /* Iteratively hash the whole DWARF location expression LOC.  */
21347
21348 static inline hashval_t
21349 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21350 {
21351   dw_loc_descr_ref l;
21352   bool sizes_computed = false;
21353   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21354   size_of_locs (loc);
21355
21356   for (l = loc; l != NULL; l = l->dw_loc_next)
21357     {
21358       enum dwarf_location_atom opc = l->dw_loc_opc;
21359       hash = iterative_hash_object (opc, hash);
21360       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21361         {
21362           size_of_locs (loc);
21363           sizes_computed = true;
21364         }
21365       hash = hash_loc_operands (l, hash);
21366     }
21367   return hash;
21368 }
21369
21370 /* Compute hash of the whole location list LIST_HEAD.  */
21371
21372 static inline void
21373 hash_loc_list (dw_loc_list_ref list_head)
21374 {
21375   dw_loc_list_ref curr = list_head;
21376   hashval_t hash = 0;
21377
21378   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21379     {
21380       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21381       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21382       if (curr->section)
21383         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21384                                hash);
21385       hash = hash_locs (curr->expr, hash);
21386     }
21387   list_head->hash = hash;
21388 }
21389
21390 /* Return true if X and Y opcodes have the same operands.  */
21391
21392 static inline bool
21393 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
21394 {
21395   dw_val_ref valx1 = &x->dw_loc_oprnd1;
21396   dw_val_ref valx2 = &x->dw_loc_oprnd2;
21397   dw_val_ref valy1 = &y->dw_loc_oprnd1;
21398   dw_val_ref valy2 = &y->dw_loc_oprnd2;
21399
21400   switch (x->dw_loc_opc)
21401     {
21402     case DW_OP_const4u:
21403     case DW_OP_const8u:
21404       if (x->dtprel)
21405         goto hash_addr;
21406       /* FALLTHRU */
21407     case DW_OP_const1u:
21408     case DW_OP_const1s:
21409     case DW_OP_const2u:
21410     case DW_OP_const2s:
21411     case DW_OP_const4s:
21412     case DW_OP_const8s:
21413     case DW_OP_constu:
21414     case DW_OP_consts:
21415     case DW_OP_pick:
21416     case DW_OP_plus_uconst:
21417     case DW_OP_breg0:
21418     case DW_OP_breg1:
21419     case DW_OP_breg2:
21420     case DW_OP_breg3:
21421     case DW_OP_breg4:
21422     case DW_OP_breg5:
21423     case DW_OP_breg6:
21424     case DW_OP_breg7:
21425     case DW_OP_breg8:
21426     case DW_OP_breg9:
21427     case DW_OP_breg10:
21428     case DW_OP_breg11:
21429     case DW_OP_breg12:
21430     case DW_OP_breg13:
21431     case DW_OP_breg14:
21432     case DW_OP_breg15:
21433     case DW_OP_breg16:
21434     case DW_OP_breg17:
21435     case DW_OP_breg18:
21436     case DW_OP_breg19:
21437     case DW_OP_breg20:
21438     case DW_OP_breg21:
21439     case DW_OP_breg22:
21440     case DW_OP_breg23:
21441     case DW_OP_breg24:
21442     case DW_OP_breg25:
21443     case DW_OP_breg26:
21444     case DW_OP_breg27:
21445     case DW_OP_breg28:
21446     case DW_OP_breg29:
21447     case DW_OP_breg30:
21448     case DW_OP_breg31:
21449     case DW_OP_regx:
21450     case DW_OP_fbreg:
21451     case DW_OP_piece:
21452     case DW_OP_deref_size:
21453     case DW_OP_xderef_size:
21454       return valx1->v.val_int == valy1->v.val_int;
21455     case DW_OP_skip:
21456     case DW_OP_bra:
21457       gcc_assert (valx1->val_class == dw_val_class_loc
21458                   && valy1->val_class == dw_val_class_loc
21459                   && x->dw_loc_addr == y->dw_loc_addr);
21460       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
21461     case DW_OP_implicit_value:
21462       if (valx1->v.val_unsigned != valy1->v.val_unsigned
21463           || valx2->val_class != valy2->val_class)
21464         return false;
21465       switch (valx2->val_class)
21466         {
21467         case dw_val_class_const:
21468           return valx2->v.val_int == valy2->v.val_int;
21469         case dw_val_class_vec:
21470           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21471                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21472                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21473                             valx2->v.val_vec.elt_size
21474                             * valx2->v.val_vec.length) == 0;
21475         case dw_val_class_const_double:
21476           return valx2->v.val_double.low == valy2->v.val_double.low
21477                  && valx2->v.val_double.high == valy2->v.val_double.high;
21478         case dw_val_class_addr:
21479           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
21480         default:
21481           gcc_unreachable ();
21482         }
21483     case DW_OP_bregx:
21484     case DW_OP_bit_piece:
21485       return valx1->v.val_int == valy1->v.val_int
21486              && valx2->v.val_int == valy2->v.val_int;
21487     case DW_OP_addr:
21488     hash_addr:
21489       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
21490     case DW_OP_GNU_implicit_pointer:
21491       return valx1->val_class == dw_val_class_die_ref
21492              && valx1->val_class == valy1->val_class
21493              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
21494              && valx2->v.val_int == valy2->v.val_int;
21495     case DW_OP_GNU_entry_value:
21496       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
21497     case DW_OP_GNU_const_type:
21498       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
21499           || valx2->val_class != valy2->val_class)
21500         return false;
21501       switch (valx2->val_class)
21502         {
21503         case dw_val_class_const:
21504           return valx2->v.val_int == valy2->v.val_int;
21505         case dw_val_class_vec:
21506           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21507                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21508                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21509                             valx2->v.val_vec.elt_size
21510                             * valx2->v.val_vec.length) == 0;
21511         case dw_val_class_const_double:
21512           return valx2->v.val_double.low == valy2->v.val_double.low
21513                  && valx2->v.val_double.high == valy2->v.val_double.high;
21514         default:
21515           gcc_unreachable ();
21516         }
21517     case DW_OP_GNU_regval_type:
21518     case DW_OP_GNU_deref_type:
21519       return valx1->v.val_int == valy1->v.val_int
21520              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
21521     case DW_OP_GNU_convert:
21522     case DW_OP_GNU_reinterpret:
21523       if (valx1->val_class != valy1->val_class)
21524         return false;
21525       if (valx1->val_class == dw_val_class_unsigned_const)
21526         return valx1->v.val_unsigned == valy1->v.val_unsigned;
21527       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21528     case DW_OP_GNU_parameter_ref:
21529       return valx1->val_class == dw_val_class_die_ref
21530              && valx1->val_class == valy1->val_class
21531              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21532     default:
21533       /* Other codes have no operands.  */
21534       return true;
21535     }
21536 }
21537
21538 /* Return true if DWARF location expressions X and Y are the same.  */
21539
21540 static inline bool
21541 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
21542 {
21543   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
21544     if (x->dw_loc_opc != y->dw_loc_opc
21545         || x->dtprel != y->dtprel
21546         || !compare_loc_operands (x, y))
21547       break;
21548   return x == NULL && y == NULL;
21549 }
21550
21551 /* Return precomputed hash of location list X.  */
21552
21553 static hashval_t
21554 loc_list_hash (const void *x)
21555 {
21556   return ((const struct dw_loc_list_struct *) x)->hash;
21557 }
21558
21559 /* Return 1 if location lists X and Y are the same.  */
21560
21561 static int
21562 loc_list_eq (const void *x, const void *y)
21563 {
21564   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
21565   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
21566   if (a == b)
21567     return 1;
21568   if (a->hash != b->hash)
21569     return 0;
21570   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
21571     if (strcmp (a->begin, b->begin) != 0
21572         || strcmp (a->end, b->end) != 0
21573         || (a->section == NULL) != (b->section == NULL)
21574         || (a->section && strcmp (a->section, b->section) != 0)
21575         || !compare_locs (a->expr, b->expr))
21576       break;
21577   return a == NULL && b == NULL;
21578 }
21579
21580 /* Recursively optimize location lists referenced from DIE
21581    children and share them whenever possible.  */
21582
21583 static void
21584 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
21585 {
21586   dw_die_ref c;
21587   dw_attr_ref a;
21588   unsigned ix;
21589   void **slot;
21590
21591   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21592     if (AT_class (a) == dw_val_class_loc_list)
21593       {
21594         dw_loc_list_ref list = AT_loc_list (a);
21595         /* TODO: perform some optimizations here, before hashing
21596            it and storing into the hash table.  */
21597         hash_loc_list (list);
21598         slot = htab_find_slot_with_hash (htab, list, list->hash,
21599                                          INSERT);
21600         if (*slot == NULL)
21601           *slot = (void *) list;
21602         else
21603           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
21604       }
21605
21606   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
21607 }
21608
21609 /* Optimize location lists referenced from DIE
21610    children and share them whenever possible.  */
21611
21612 static void
21613 optimize_location_lists (dw_die_ref die)
21614 {
21615   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
21616   optimize_location_lists_1 (die, htab);
21617   htab_delete (htab);
21618 }
21619 \f
21620 /* Output stuff that dwarf requires at the end of every file,
21621    and generate the DWARF-2 debugging info.  */
21622
21623 static void
21624 dwarf2out_finish (const char *filename)
21625 {
21626   limbo_die_node *node, *next_node;
21627   comdat_type_node *ctnode;
21628   htab_t comdat_type_table;
21629   unsigned int i;
21630
21631   gen_scheduled_generic_parms_dies ();
21632   gen_remaining_tmpl_value_param_die_attribute ();
21633
21634   /* Add the name for the main input file now.  We delayed this from
21635      dwarf2out_init to avoid complications with PCH.  */
21636   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
21637   if (!IS_ABSOLUTE_PATH (filename))
21638     add_comp_dir_attribute (comp_unit_die ());
21639   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
21640     {
21641       bool p = false;
21642       htab_traverse (file_table, file_table_relative_p, &p);
21643       if (p)
21644         add_comp_dir_attribute (comp_unit_die ());
21645     }
21646
21647   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21648     {
21649       add_location_or_const_value_attribute (
21650         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21651         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21652         false,
21653         DW_AT_location);
21654     }
21655
21656   /* Traverse the limbo die list, and add parent/child links.  The only
21657      dies without parents that should be here are concrete instances of
21658      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21659      For concrete instances, we can get the parent die from the abstract
21660      instance.  */
21661   for (node = limbo_die_list; node; node = next_node)
21662     {
21663       dw_die_ref die = node->die;
21664       next_node = node->next;
21665
21666       if (die->die_parent == NULL)
21667         {
21668           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21669
21670           if (origin && origin->die_parent)
21671             add_child_die (origin->die_parent, die);
21672           else if (is_cu_die (die))
21673             ;
21674           else if (seen_error ())
21675             /* It's OK to be confused by errors in the input.  */
21676             add_child_die (comp_unit_die (), die);
21677           else
21678             {
21679               /* In certain situations, the lexical block containing a
21680                  nested function can be optimized away, which results
21681                  in the nested function die being orphaned.  Likewise
21682                  with the return type of that nested function.  Force
21683                  this to be a child of the containing function.
21684
21685                  It may happen that even the containing function got fully
21686                  inlined and optimized out.  In that case we are lost and
21687                  assign the empty child.  This should not be big issue as
21688                  the function is likely unreachable too.  */
21689               tree context = NULL_TREE;
21690
21691               gcc_assert (node->created_for);
21692
21693               if (DECL_P (node->created_for))
21694                 context = DECL_CONTEXT (node->created_for);
21695               else if (TYPE_P (node->created_for))
21696                 context = TYPE_CONTEXT (node->created_for);
21697
21698               gcc_assert (context
21699                           && (TREE_CODE (context) == FUNCTION_DECL
21700                               || TREE_CODE (context) == NAMESPACE_DECL));
21701
21702               origin = lookup_decl_die (context);
21703               if (origin)
21704                 add_child_die (origin, die);
21705               else
21706                 add_child_die (comp_unit_die (), die);
21707             }
21708         }
21709     }
21710
21711   limbo_die_list = NULL;
21712
21713 #if ENABLE_ASSERT_CHECKING
21714   {
21715     dw_die_ref die = comp_unit_die (), c;
21716     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
21717   }
21718 #endif
21719   resolve_addr (comp_unit_die ());
21720   move_marked_base_types ();
21721
21722   for (node = deferred_asm_name; node; node = node->next)
21723     {
21724       tree decl = node->created_for;
21725       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21726         {
21727           add_linkage_attr (node->die, decl);
21728           move_linkage_attr (node->die);
21729         }
21730     }
21731
21732   deferred_asm_name = NULL;
21733
21734   /* Walk through the list of incomplete types again, trying once more to
21735      emit full debugging info for them.  */
21736   retry_incomplete_types ();
21737
21738   if (flag_eliminate_unused_debug_types)
21739     prune_unused_types ();
21740
21741   /* Generate separate CUs for each of the include files we've seen.
21742      They will go into limbo_die_list.  */
21743   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
21744     break_out_includes (comp_unit_die ());
21745
21746   /* Generate separate COMDAT sections for type DIEs. */
21747   if (use_debug_types)
21748     {
21749       break_out_comdat_types (comp_unit_die ());
21750
21751       /* Each new type_unit DIE was added to the limbo die list when created.
21752          Since these have all been added to comdat_type_list, clear the
21753          limbo die list.  */
21754       limbo_die_list = NULL;
21755
21756       /* For each new comdat type unit, copy declarations for incomplete
21757          types to make the new unit self-contained (i.e., no direct
21758          references to the main compile unit).  */
21759       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21760         copy_decls_for_unworthy_types (ctnode->root_die);
21761       copy_decls_for_unworthy_types (comp_unit_die ());
21762
21763       /* In the process of copying declarations from one unit to another,
21764          we may have left some declarations behind that are no longer
21765          referenced.  Prune them.  */
21766       prune_unused_types ();
21767     }
21768
21769   /* Traverse the DIE's and add add sibling attributes to those DIE's
21770      that have children.  */
21771   add_sibling_attributes (comp_unit_die ());
21772   for (node = limbo_die_list; node; node = node->next)
21773     add_sibling_attributes (node->die);
21774   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21775     add_sibling_attributes (ctnode->root_die);
21776
21777   /* Output a terminator label for the .text section.  */
21778   switch_to_section (text_section);
21779   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
21780   if (cold_text_section)
21781     {
21782       switch_to_section (cold_text_section);
21783       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
21784     }
21785
21786   /* We can only use the low/high_pc attributes if all of the code was
21787      in .text.  */
21788   if (!have_multiple_function_sections 
21789       || (dwarf_version < 3 && dwarf_strict))
21790     {
21791       /* Don't add if the CU has no associated code.  */
21792       if (text_section_used)
21793         {
21794           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
21795           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
21796         }
21797     }
21798   else
21799     {
21800       unsigned fde_idx;
21801       dw_fde_ref fde;
21802       bool range_list_added = false;
21803
21804       if (text_section_used)
21805         add_ranges_by_labels (comp_unit_die (), text_section_label,
21806                               text_end_label, &range_list_added);
21807       if (cold_text_section_used)
21808         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
21809                               cold_end_label, &range_list_added);
21810
21811       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
21812         {
21813           if (!fde->in_std_section)
21814             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
21815                                   fde->dw_fde_end, &range_list_added);
21816           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
21817             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
21818                                   fde->dw_fde_second_end, &range_list_added);
21819         }
21820
21821       if (range_list_added)
21822         {
21823           /* We need to give .debug_loc and .debug_ranges an appropriate
21824              "base address".  Use zero so that these addresses become
21825              absolute.  Historically, we've emitted the unexpected
21826              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
21827              Emit both to give time for other tools to adapt.  */
21828           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
21829           if (! dwarf_strict && dwarf_version < 4)
21830             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
21831
21832           add_ranges (NULL);
21833         }
21834     }
21835
21836   if (debug_info_level >= DINFO_LEVEL_NORMAL)
21837     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
21838                     debug_line_section_label);
21839
21840   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21841     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
21842
21843   if (have_location_lists)
21844     optimize_location_lists (comp_unit_die ());
21845
21846   /* Output all of the compilation units.  We put the main one last so that
21847      the offsets are available to output_pubnames.  */
21848   for (node = limbo_die_list; node; node = node->next)
21849     output_comp_unit (node->die, 0);
21850
21851   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
21852   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21853     {
21854       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
21855
21856       /* Don't output duplicate types.  */
21857       if (*slot != HTAB_EMPTY_ENTRY)
21858         continue;
21859
21860       /* Add a pointer to the line table for the main compilation unit
21861          so that the debugger can make sense of DW_AT_decl_file
21862          attributes.  */
21863       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21864         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
21865                         debug_line_section_label);
21866
21867       output_comdat_type_unit (ctnode);
21868       *slot = ctnode;
21869     }
21870   htab_delete (comdat_type_table);
21871
21872   /* Output the main compilation unit if non-empty or if .debug_macinfo
21873      will be emitted.  */
21874   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
21875
21876   /* Output the abbreviation table.  */
21877   if (abbrev_die_table_in_use != 1)
21878     {
21879       switch_to_section (debug_abbrev_section);
21880       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21881       output_abbrev_section ();
21882     }
21883
21884   /* Output location list section if necessary.  */
21885   if (have_location_lists)
21886     {
21887       /* Output the location lists info.  */
21888       switch_to_section (debug_loc_section);
21889       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
21890                                    DEBUG_LOC_SECTION_LABEL, 0);
21891       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
21892       output_location_lists (comp_unit_die ());
21893     }
21894
21895   /* Output public names table if necessary.  */
21896   if (!VEC_empty (pubname_entry, pubname_table))
21897     {
21898       gcc_assert (info_section_emitted);
21899       switch_to_section (debug_pubnames_section);
21900       output_pubnames (pubname_table);
21901     }
21902
21903   /* Output public types table if necessary.  */
21904   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
21905      It shouldn't hurt to emit it always, since pure DWARF2 consumers
21906      simply won't look for the section.  */
21907   if (!VEC_empty (pubname_entry, pubtype_table))
21908     {
21909       bool empty = false;
21910       
21911       if (flag_eliminate_unused_debug_types)
21912         {
21913           /* The pubtypes table might be emptied by pruning unused items.  */
21914           unsigned i;
21915           pubname_ref p;
21916           empty = true;
21917           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
21918             if (p->die->die_offset != 0)
21919               {
21920                 empty = false;
21921                 break;
21922               }
21923         }
21924       if (!empty)
21925         {
21926           gcc_assert (info_section_emitted);
21927           switch_to_section (debug_pubtypes_section);
21928           output_pubnames (pubtype_table);
21929         }
21930     }
21931
21932   /* Output the address range information if a CU (.debug_info section)
21933      was emitted.  We output an empty table even if we had no functions
21934      to put in it.  This because the consumer has no way to tell the
21935      difference between an empty table that we omitted and failure to
21936      generate a table that would have contained data.  */
21937   if (info_section_emitted)
21938     {
21939       unsigned long aranges_length = size_of_aranges ();
21940
21941       switch_to_section (debug_aranges_section);
21942       output_aranges (aranges_length);
21943     }
21944
21945   /* Output ranges section if necessary.  */
21946   if (ranges_table_in_use)
21947     {
21948       switch_to_section (debug_ranges_section);
21949       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
21950       output_ranges ();
21951     }
21952
21953   /* Output the source line correspondence table.  We must do this
21954      even if there is no line information.  Otherwise, on an empty
21955      translation unit, we will generate a present, but empty,
21956      .debug_info section.  IRIX 6.5 `nm' will then complain when
21957      examining the file.  This is done late so that any filenames
21958      used by the debug_info section are marked as 'used'.  */
21959   switch_to_section (debug_line_section);
21960   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21961   if (! DWARF2_ASM_LINE_DEBUG_INFO)
21962     output_line_info ();
21963
21964   /* Have to end the macro section.  */
21965   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21966     {
21967       switch_to_section (debug_macinfo_section);
21968       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21969       if (!VEC_empty (macinfo_entry, macinfo_table))
21970         output_macinfo ();
21971       dw2_asm_output_data (1, 0, "End compilation unit");
21972     }
21973
21974   /* If we emitted any DW_FORM_strp form attribute, output the string
21975      table too.  */
21976   if (debug_str_hash)
21977     htab_traverse (debug_str_hash, output_indirect_string, NULL);
21978 }
21979
21980 #include "gt-dwarf2out.h"