OSDN Git Service

PR debug/52027
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "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 #include "opts.h"
98
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_insn;
101 static rtx cached_next_real_insn;
102
103 #ifdef VMS_DEBUGGING_INFO
104 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105
106 /* Define this macro to be a nonzero value if the directory specifications
107     which are output in the debug info should end with a separator.  */
108 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
109 /* Define this macro to evaluate to a nonzero value if GCC should refrain
110    from generating indirect strings in DWARF2 debug information, for instance
111    if your target is stuck with an old version of GDB that is unable to
112    process them properly or uses VMS Debug.  */
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
114 #else
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
116 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
117 #endif
118
119 /* ??? Poison these here until it can be done generically.  They've been
120    totally replaced in this file; make sure it stays that way.  */
121 #undef DWARF2_UNWIND_INFO
122 #undef DWARF2_FRAME_INFO
123 #if (GCC_VERSION >= 3000)
124  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
125 #endif
126
127 /* The size of the target's pointer type.  */
128 #ifndef PTR_SIZE
129 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
130 #endif
131
132 /* Array of RTXes referenced by the debugging information, which therefore
133    must be kept around forever.  */
134 static GTY(()) VEC(rtx,gc) *used_rtx_array;
135
136 /* A pointer to the base of a list of incomplete types which might be
137    completed at some later time.  incomplete_types_list needs to be a
138    VEC(tree,gc) because we want to tell the garbage collector about
139    it.  */
140 static GTY(()) VEC(tree,gc) *incomplete_types;
141
142 /* A pointer to the base of a table of references to declaration
143    scopes.  This table is a display which tracks the nesting
144    of declaration scopes at the current scope and containing
145    scopes.  This table is used to find the proper place to
146    define type declaration DIE's.  */
147 static GTY(()) VEC(tree,gc) *decl_scope_table;
148
149 /* Pointers to various DWARF2 sections.  */
150 static GTY(()) section *debug_info_section;
151 static GTY(()) section *debug_abbrev_section;
152 static GTY(()) section *debug_aranges_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_loc_section;
156 static GTY(()) section *debug_pubnames_section;
157 static GTY(()) section *debug_pubtypes_section;
158 static GTY(()) section *debug_str_section;
159 static GTY(()) section *debug_ranges_section;
160 static GTY(()) section *debug_frame_section;
161
162 /* Maximum size (in bytes) of an artificially generated label.  */
163 #define MAX_ARTIFICIAL_LABEL_BYTES      30
164
165 /* According to the (draft) DWARF 3 specification, the initial length
166    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
167    bytes are 0xffffffff, followed by the length stored in the next 8
168    bytes.
169
170    However, the SGI/MIPS ABI uses an initial length which is equal to
171    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
172
173 #ifndef DWARF_INITIAL_LENGTH_SIZE
174 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
175 #endif
176
177 /* Round SIZE up to the nearest BOUNDARY.  */
178 #define DWARF_ROUND(SIZE,BOUNDARY) \
179   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
180
181 /* CIE identifier.  */
182 #if HOST_BITS_PER_WIDE_INT >= 64
183 #define DWARF_CIE_ID \
184   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
185 #else
186 #define DWARF_CIE_ID DW_CIE_ID
187 #endif
188
189 DEF_VEC_P (dw_fde_ref);
190 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
191
192 /* A vector for a table that contains frame description
193    information for each routine.  */
194 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
195
196 struct GTY(()) indirect_string_node {
197   const char *str;
198   unsigned int refcount;
199   enum dwarf_form form;
200   char *label;
201 };
202
203 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
204
205 static GTY(()) int dw2_string_counter;
206
207 /* True if the compilation unit places functions in more than one section.  */
208 static GTY(()) bool have_multiple_function_sections = false;
209
210 /* Whether the default text and cold text sections have been used at all.  */
211
212 static GTY(()) bool text_section_used = false;
213 static GTY(()) bool cold_text_section_used = false;
214
215 /* The default cold text section.  */
216 static GTY(()) section *cold_text_section;
217
218 /* Forward declarations for functions defined in this file.  */
219
220 static char *stripattributes (const char *);
221 static void output_call_frame_info (int);
222 static void dwarf2out_note_section_used (void);
223
224 /* Personality decl of current unit.  Used only when assembler does not support
225    personality CFI.  */
226 static GTY(()) rtx current_unit_personality;
227
228 /* Data and reference forms for relocatable data.  */
229 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
230 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
231
232 #ifndef DEBUG_FRAME_SECTION
233 #define DEBUG_FRAME_SECTION     ".debug_frame"
234 #endif
235
236 #ifndef FUNC_BEGIN_LABEL
237 #define FUNC_BEGIN_LABEL        "LFB"
238 #endif
239
240 #ifndef FUNC_END_LABEL
241 #define FUNC_END_LABEL          "LFE"
242 #endif
243
244 #ifndef PROLOGUE_END_LABEL
245 #define PROLOGUE_END_LABEL      "LPE"
246 #endif
247
248 #ifndef EPILOGUE_BEGIN_LABEL
249 #define EPILOGUE_BEGIN_LABEL    "LEB"
250 #endif
251
252 #ifndef FRAME_BEGIN_LABEL
253 #define FRAME_BEGIN_LABEL       "Lframe"
254 #endif
255 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
256 #define CIE_END_LABEL           "LECIE"
257 #define FDE_LABEL               "LSFDE"
258 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
259 #define FDE_END_LABEL           "LEFDE"
260 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
261 #define LINE_NUMBER_END_LABEL   "LELT"
262 #define LN_PROLOG_AS_LABEL      "LASLTP"
263 #define LN_PROLOG_END_LABEL     "LELTP"
264 #define DIE_LABEL_PREFIX        "DW"
265 \f
266 /* Match the base name of a file to the base name of a compilation unit. */
267
268 static int
269 matches_main_base (const char *path)
270 {
271   /* Cache the last query. */
272   static const char *last_path = NULL;
273   static int last_match = 0;
274   if (path != last_path)
275     {
276       const char *base;
277       int length = base_of_path (path, &base);
278       last_path = path;
279       last_match = (length == main_input_baselength
280                     && memcmp (base, main_input_basename, length) == 0);
281     }
282   return last_match;
283 }
284
285 #ifdef DEBUG_DEBUG_STRUCT
286
287 static int
288 dump_struct_debug (tree type, enum debug_info_usage usage,
289                    enum debug_struct_file criterion, int generic,
290                    int matches, int result)
291 {
292   /* Find the type name. */
293   tree type_decl = TYPE_STUB_DECL (type);
294   tree t = type_decl;
295   const char *name = 0;
296   if (TREE_CODE (t) == TYPE_DECL)
297     t = DECL_NAME (t);
298   if (t)
299     name = IDENTIFIER_POINTER (t);
300
301   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
302            criterion,
303            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
304            matches ? "bas" : "hdr",
305            generic ? "gen" : "ord",
306            usage == DINFO_USAGE_DFN ? ";" :
307              usage == DINFO_USAGE_DIR_USE ? "." : "*",
308            result,
309            (void*) type_decl, name);
310   return result;
311 }
312 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
313   dump_struct_debug (type, usage, criterion, generic, matches, result)
314
315 #else
316
317 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
318   (result)
319
320 #endif
321
322 static bool
323 should_emit_struct_debug (tree type, enum debug_info_usage usage)
324 {
325   enum debug_struct_file criterion;
326   tree type_decl;
327   bool generic = lang_hooks.types.generic_p (type);
328
329   if (generic)
330     criterion = debug_struct_generic[usage];
331   else
332     criterion = debug_struct_ordinary[usage];
333
334   if (criterion == DINFO_STRUCT_FILE_NONE)
335     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
336   if (criterion == DINFO_STRUCT_FILE_ANY)
337     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
338
339   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
340
341   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
342     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343
344   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
345     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
346   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
347 }
348 \f
349 /* Return a pointer to a copy of the section string name S with all
350    attributes stripped off, and an asterisk prepended (for assemble_name).  */
351
352 static inline char *
353 stripattributes (const char *s)
354 {
355   char *stripped = XNEWVEC (char, strlen (s) + 2);
356   char *p = stripped;
357
358   *p++ = '*';
359
360   while (*s && *s != ',')
361     *p++ = *s++;
362
363   *p = '\0';
364   return stripped;
365 }
366
367 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
368    switch to the data section instead, and write out a synthetic start label
369    for collect2 the first time around.  */
370
371 static void
372 switch_to_eh_frame_section (bool back)
373 {
374   tree label;
375
376 #ifdef EH_FRAME_SECTION_NAME
377   if (eh_frame_section == 0)
378     {
379       int flags;
380
381       if (EH_TABLES_CAN_BE_READ_ONLY)
382         {
383           int fde_encoding;
384           int per_encoding;
385           int lsda_encoding;
386
387           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
388                                                        /*global=*/0);
389           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
390                                                        /*global=*/1);
391           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
392                                                         /*global=*/0);
393           flags = ((! flag_pic
394                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
395                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
396                         && (per_encoding & 0x70) != DW_EH_PE_absptr
397                         && (per_encoding & 0x70) != DW_EH_PE_aligned
398                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
399                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
400                    ? 0 : SECTION_WRITE);
401         }
402       else
403         flags = SECTION_WRITE;
404       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
405     }
406 #endif /* EH_FRAME_SECTION_NAME */
407
408   if (eh_frame_section)
409     switch_to_section (eh_frame_section);
410   else
411     {
412       /* We have no special eh_frame section.  Put the information in
413          the data section and emit special labels to guide collect2.  */
414       switch_to_section (data_section);
415
416       if (!back)
417         {
418           label = get_file_function_name ("F");
419           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
420           targetm.asm_out.globalize_label (asm_out_file,
421                                            IDENTIFIER_POINTER (label));
422           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
423         }
424     }
425 }
426
427 /* Switch [BACK] to the eh or debug frame table section, depending on
428    FOR_EH.  */
429
430 static void
431 switch_to_frame_table_section (int for_eh, bool back)
432 {
433   if (for_eh)
434     switch_to_eh_frame_section (back);
435   else
436     {
437       if (!debug_frame_section)
438         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
439                                            SECTION_DEBUG, NULL);
440       switch_to_section (debug_frame_section);
441     }
442 }
443
444 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
445
446 enum dw_cfi_oprnd_type
447 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
448 {
449   switch (cfi)
450     {
451     case DW_CFA_nop:
452     case DW_CFA_GNU_window_save:
453     case DW_CFA_remember_state:
454     case DW_CFA_restore_state:
455       return dw_cfi_oprnd_unused;
456
457     case DW_CFA_set_loc:
458     case DW_CFA_advance_loc1:
459     case DW_CFA_advance_loc2:
460     case DW_CFA_advance_loc4:
461     case DW_CFA_MIPS_advance_loc8:
462       return dw_cfi_oprnd_addr;
463
464     case DW_CFA_offset:
465     case DW_CFA_offset_extended:
466     case DW_CFA_def_cfa:
467     case DW_CFA_offset_extended_sf:
468     case DW_CFA_def_cfa_sf:
469     case DW_CFA_restore:
470     case DW_CFA_restore_extended:
471     case DW_CFA_undefined:
472     case DW_CFA_same_value:
473     case DW_CFA_def_cfa_register:
474     case DW_CFA_register:
475     case DW_CFA_expression:
476       return dw_cfi_oprnd_reg_num;
477
478     case DW_CFA_def_cfa_offset:
479     case DW_CFA_GNU_args_size:
480     case DW_CFA_def_cfa_offset_sf:
481       return dw_cfi_oprnd_offset;
482
483     case DW_CFA_def_cfa_expression:
484       return dw_cfi_oprnd_loc;
485
486     default:
487       gcc_unreachable ();
488     }
489 }
490
491 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
492
493 enum dw_cfi_oprnd_type
494 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
495 {
496   switch (cfi)
497     {
498     case DW_CFA_def_cfa:
499     case DW_CFA_def_cfa_sf:
500     case DW_CFA_offset:
501     case DW_CFA_offset_extended_sf:
502     case DW_CFA_offset_extended:
503       return dw_cfi_oprnd_offset;
504
505     case DW_CFA_register:
506       return dw_cfi_oprnd_reg_num;
507
508     case DW_CFA_expression:
509       return dw_cfi_oprnd_loc;
510
511     default:
512       return dw_cfi_oprnd_unused;
513     }
514 }
515
516 /* Output one FDE.  */
517
518 static void
519 output_fde (dw_fde_ref fde, bool for_eh, bool second,
520             char *section_start_label, int fde_encoding, char *augmentation,
521             bool any_lsda_needed, int lsda_encoding)
522 {
523   const char *begin, *end;
524   static unsigned int j;
525   char l1[20], l2[20];
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 this FDE.  */
606   fde->dw_fde_current_label = begin;
607   {
608     size_t from, until, i;
609
610     from = 0;
611     until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
612
613     if (fde->dw_fde_second_begin == NULL)
614       ;
615     else if (!second)
616       until = fde->dw_fde_switch_cfi_index;
617     else
618       from = fde->dw_fde_switch_cfi_index;
619
620     for (i = from; i < until; i++)
621       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
622   }
623
624   /* If we are to emit a ref/link from function bodies to their frame tables,
625      do it now.  This is typically performed to make sure that tables
626      associated with functions are dragged with them and not discarded in
627      garbage collecting links. We need to do this on a per function basis to
628      cope with -ffunction-sections.  */
629
630 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
631   /* Switch to the function section, emit the ref to the tables, and
632      switch *back* into the table section.  */
633   switch_to_section (function_section (fde->decl));
634   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
635   switch_to_frame_table_section (for_eh, true);
636 #endif
637
638   /* Pad the FDE out to an address sized boundary.  */
639   ASM_OUTPUT_ALIGN (asm_out_file,
640                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
641   ASM_OUTPUT_LABEL (asm_out_file, l2);
642
643   j += 2;
644 }
645
646 /* Return true if frame description entry FDE is needed for EH.  */
647
648 static bool
649 fde_needed_for_eh_p (dw_fde_ref fde)
650 {
651   if (flag_asynchronous_unwind_tables)
652     return true;
653
654   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
655     return true;
656
657   if (fde->uses_eh_lsda)
658     return true;
659
660   /* If exceptions are enabled, we have collected nothrow info.  */
661   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
662     return false;
663
664   return true;
665 }
666
667 /* Output the call frame information used to record information
668    that relates to calculating the frame pointer, and records the
669    location of saved registers.  */
670
671 static void
672 output_call_frame_info (int for_eh)
673 {
674   unsigned int i;
675   dw_fde_ref fde;
676   dw_cfi_ref cfi;
677   char l1[20], l2[20], section_start_label[20];
678   bool any_lsda_needed = false;
679   char augmentation[6];
680   int augmentation_size;
681   int fde_encoding = DW_EH_PE_absptr;
682   int per_encoding = DW_EH_PE_absptr;
683   int lsda_encoding = DW_EH_PE_absptr;
684   int return_reg;
685   rtx personality = NULL;
686   int dw_cie_version;
687
688   /* Don't emit a CIE if there won't be any FDEs.  */
689   if (fde_vec == NULL)
690     return;
691
692   /* Nothing to do if the assembler's doing it all.  */
693   if (dwarf2out_do_cfi_asm ())
694     return;
695
696   /* If we don't have any functions we'll want to unwind out of, don't emit
697      any EH unwind information.  If we make FDEs linkonce, we may have to
698      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
699      want to avoid having an FDE kept around when the function it refers to
700      is discarded.  Example where this matters: a primary function template
701      in C++ requires EH information, an explicit specialization doesn't.  */
702   if (for_eh)
703     {
704       bool any_eh_needed = false;
705
706       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
707         {
708           if (fde->uses_eh_lsda)
709             any_eh_needed = any_lsda_needed = true;
710           else if (fde_needed_for_eh_p (fde))
711             any_eh_needed = true;
712           else if (TARGET_USES_WEAK_UNWIND_INFO)
713             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
714         }
715
716       if (!any_eh_needed)
717         return;
718     }
719
720   /* We're going to be generating comments, so turn on app.  */
721   if (flag_debug_asm)
722     app_enable ();
723
724   /* Switch to the proper frame section, first time.  */
725   switch_to_frame_table_section (for_eh, false);
726
727   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
728   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
729
730   /* Output the CIE.  */
731   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
732   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
733   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
734     dw2_asm_output_data (4, 0xffffffff,
735       "Initial length escape value indicating 64-bit DWARF extension");
736   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
737                         "Length of Common Information Entry");
738   ASM_OUTPUT_LABEL (asm_out_file, l1);
739
740   /* Now that the CIE pointer is PC-relative for EH,
741      use 0 to identify the CIE.  */
742   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
743                        (for_eh ? 0 : DWARF_CIE_ID),
744                        "CIE Identifier Tag");
745
746   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
747      use CIE version 1, unless that would produce incorrect results
748      due to overflowing the return register column.  */
749   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
750   dw_cie_version = 1;
751   if (return_reg >= 256 || dwarf_version > 2)
752     dw_cie_version = 3;
753   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
754
755   augmentation[0] = 0;
756   augmentation_size = 0;
757
758   personality = current_unit_personality;
759   if (for_eh)
760     {
761       char *p;
762
763       /* Augmentation:
764          z      Indicates that a uleb128 is present to size the
765                 augmentation section.
766          L      Indicates the encoding (and thus presence) of
767                 an LSDA pointer in the FDE augmentation.
768          R      Indicates a non-default pointer encoding for
769                 FDE code pointers.
770          P      Indicates the presence of an encoding + language
771                 personality routine in the CIE augmentation.  */
772
773       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
774       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
775       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
776
777       p = augmentation + 1;
778       if (personality)
779         {
780           *p++ = 'P';
781           augmentation_size += 1 + size_of_encoded_value (per_encoding);
782           assemble_external_libcall (personality);
783         }
784       if (any_lsda_needed)
785         {
786           *p++ = 'L';
787           augmentation_size += 1;
788         }
789       if (fde_encoding != DW_EH_PE_absptr)
790         {
791           *p++ = 'R';
792           augmentation_size += 1;
793         }
794       if (p > augmentation + 1)
795         {
796           augmentation[0] = 'z';
797           *p = '\0';
798         }
799
800       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
801       if (personality && per_encoding == DW_EH_PE_aligned)
802         {
803           int offset = (  4             /* Length */
804                         + 4             /* CIE Id */
805                         + 1             /* CIE version */
806                         + strlen (augmentation) + 1     /* Augmentation */
807                         + size_of_uleb128 (1)           /* Code alignment */
808                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
809                         + 1             /* RA column */
810                         + 1             /* Augmentation size */
811                         + 1             /* Personality encoding */ );
812           int pad = -offset & (PTR_SIZE - 1);
813
814           augmentation_size += pad;
815
816           /* Augmentations should be small, so there's scarce need to
817              iterate for a solution.  Die if we exceed one uleb128 byte.  */
818           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
819         }
820     }
821
822   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
823   if (dw_cie_version >= 4)
824     {
825       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
826       dw2_asm_output_data (1, 0, "CIE Segment Size");
827     }
828   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
829   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
830                                "CIE Data Alignment Factor");
831
832   if (dw_cie_version == 1)
833     dw2_asm_output_data (1, return_reg, "CIE RA Column");
834   else
835     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
836
837   if (augmentation[0])
838     {
839       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
840       if (personality)
841         {
842           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
843                                eh_data_format_name (per_encoding));
844           dw2_asm_output_encoded_addr_rtx (per_encoding,
845                                            personality,
846                                            true, NULL);
847         }
848
849       if (any_lsda_needed)
850         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
851                              eh_data_format_name (lsda_encoding));
852
853       if (fde_encoding != DW_EH_PE_absptr)
854         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
855                              eh_data_format_name (fde_encoding));
856     }
857
858   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
859     output_cfi (cfi, NULL, for_eh);
860
861   /* Pad the CIE out to an address sized boundary.  */
862   ASM_OUTPUT_ALIGN (asm_out_file,
863                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
864   ASM_OUTPUT_LABEL (asm_out_file, l2);
865
866   /* Loop through all of the FDE's.  */
867   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
868     {
869       unsigned int k;
870
871       /* Don't emit EH unwind info for leaf functions that don't need it.  */
872       if (for_eh && !fde_needed_for_eh_p (fde))
873         continue;
874
875       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
876         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
877                     augmentation, any_lsda_needed, lsda_encoding);
878     }
879
880   if (for_eh && targetm.terminate_dw2_eh_frame_info)
881     dw2_asm_output_data (4, 0, "End of Table");
882 #ifdef MIPS_DEBUGGING_INFO
883   /* Work around Irix 6 assembler bug whereby labels at the end of a section
884      get a value of 0.  Putting .align 0 after the label fixes it.  */
885   ASM_OUTPUT_ALIGN (asm_out_file, 0);
886 #endif
887
888   /* Turn off app to make assembly quicker.  */
889   if (flag_debug_asm)
890     app_disable ();
891 }
892
893 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
894
895 static void
896 dwarf2out_do_cfi_startproc (bool second)
897 {
898   int enc;
899   rtx ref;
900   rtx personality = get_personality_function (current_function_decl);
901
902   fprintf (asm_out_file, "\t.cfi_startproc\n");
903
904   if (personality)
905     {
906       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
907       ref = personality;
908
909       /* ??? The GAS support isn't entirely consistent.  We have to
910          handle indirect support ourselves, but PC-relative is done
911          in the assembler.  Further, the assembler can't handle any
912          of the weirder relocation types.  */
913       if (enc & DW_EH_PE_indirect)
914         ref = dw2_force_const_mem (ref, true);
915
916       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
917       output_addr_const (asm_out_file, ref);
918       fputc ('\n', asm_out_file);
919     }
920
921   if (crtl->uses_eh_lsda)
922     {
923       char lab[20];
924
925       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
926       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
927                                    current_function_funcdef_no);
928       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
929       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930
931       if (enc & DW_EH_PE_indirect)
932         ref = dw2_force_const_mem (ref, true);
933
934       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
935       output_addr_const (asm_out_file, ref);
936       fputc ('\n', asm_out_file);
937     }
938 }
939
940 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
941    this allocation may be done before pass_final.  */
942
943 dw_fde_ref
944 dwarf2out_alloc_current_fde (void)
945 {
946   dw_fde_ref fde;
947
948   fde = ggc_alloc_cleared_dw_fde_node ();
949   fde->decl = current_function_decl;
950   fde->funcdef_number = current_function_funcdef_no;
951   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
952   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
953   fde->uses_eh_lsda = crtl->uses_eh_lsda;
954   fde->nothrow = crtl->nothrow;
955   fde->drap_reg = INVALID_REGNUM;
956   fde->vdrap_reg = INVALID_REGNUM;
957
958   /* Record the FDE associated with this function.  */
959   cfun->fde = fde;
960   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
961
962   return fde;
963 }
964
965 /* Output a marker (i.e. a label) for the beginning of a function, before
966    the prologue.  */
967
968 void
969 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
970                           const char *file ATTRIBUTE_UNUSED)
971 {
972   char label[MAX_ARTIFICIAL_LABEL_BYTES];
973   char * dup_label;
974   dw_fde_ref fde;
975   section *fnsec;
976   bool do_frame;
977
978   current_function_func_begin_label = NULL;
979
980   do_frame = dwarf2out_do_frame ();
981
982   /* ??? current_function_func_begin_label is also used by except.c for
983      call-site information.  We must emit this label if it might be used.  */
984   if (!do_frame
985       && (!flag_exceptions
986           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
987     return;
988
989   fnsec = function_section (current_function_decl);
990   switch_to_section (fnsec);
991   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
992                                current_function_funcdef_no);
993   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
994                           current_function_funcdef_no);
995   dup_label = xstrdup (label);
996   current_function_func_begin_label = dup_label;
997
998   /* We can elide the fde allocation if we're not emitting debug info.  */
999   if (!do_frame)
1000     return;
1001
1002   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1003      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1004      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1005      do so now.  */
1006   fde = cfun->fde;
1007   if (fde == NULL)
1008     fde = dwarf2out_alloc_current_fde ();
1009
1010   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1011   fde->dw_fde_begin = dup_label;
1012   fde->dw_fde_current_label = dup_label;
1013   fde->in_std_section = (fnsec == text_section
1014                          || (cold_text_section && fnsec == cold_text_section));
1015
1016   /* We only want to output line number information for the genuine dwarf2
1017      prologue case, not the eh frame case.  */
1018 #ifdef DWARF2_DEBUGGING_INFO
1019   if (file)
1020     dwarf2out_source_line (line, file, 0, true);
1021 #endif
1022
1023   if (dwarf2out_do_cfi_asm ())
1024     dwarf2out_do_cfi_startproc (false);
1025   else
1026     {
1027       rtx personality = get_personality_function (current_function_decl);
1028       if (!current_unit_personality)
1029         current_unit_personality = personality;
1030
1031       /* We cannot keep a current personality per function as without CFI
1032          asm, at the point where we emit the CFI data, there is no current
1033          function anymore.  */
1034       if (personality && current_unit_personality != personality)
1035         sorry ("multiple EH personalities are supported only with assemblers "
1036                "supporting .cfi_personality directive");
1037     }
1038 }
1039
1040 /* Output a marker (i.e. a label) for the end of the generated code
1041    for a function prologue.  This gets called *after* the prologue code has
1042    been generated.  */
1043
1044 void
1045 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1046                         const char *file ATTRIBUTE_UNUSED)
1047 {
1048   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049
1050   /* Output a label to mark the endpoint of the code generated for this
1051      function.  */
1052   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1053                                current_function_funcdef_no);
1054   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1055                           current_function_funcdef_no);
1056   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1057 }
1058
1059 /* Output a marker (i.e. a label) for the beginning of the generated code
1060    for a function epilogue.  This gets called *before* the prologue code has
1061    been generated.  */
1062
1063 void
1064 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1065                           const char *file ATTRIBUTE_UNUSED)
1066 {
1067   dw_fde_ref fde = cfun->fde;
1068   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069
1070   if (fde->dw_fde_vms_begin_epilogue)
1071     return;
1072
1073   /* Output a label to mark the endpoint of the code generated for this
1074      function.  */
1075   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1076                                current_function_funcdef_no);
1077   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1078                           current_function_funcdef_no);
1079   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1080 }
1081
1082 /* Output a marker (i.e. a label) for the absolute end of the generated code
1083    for a function definition.  This gets called *after* the epilogue code has
1084    been generated.  */
1085
1086 void
1087 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1088                         const char *file ATTRIBUTE_UNUSED)
1089 {
1090   dw_fde_ref fde;
1091   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092
1093   last_var_location_insn = NULL_RTX;
1094   cached_next_real_insn = NULL_RTX;
1095
1096   if (dwarf2out_do_cfi_asm ())
1097     fprintf (asm_out_file, "\t.cfi_endproc\n");
1098
1099   /* Output a label to mark the endpoint of the code generated for this
1100      function.  */
1101   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1102                                current_function_funcdef_no);
1103   ASM_OUTPUT_LABEL (asm_out_file, label);
1104   fde = cfun->fde;
1105   gcc_assert (fde != NULL);
1106   if (fde->dw_fde_second_begin == NULL)
1107     fde->dw_fde_end = xstrdup (label);
1108 }
1109
1110 void
1111 dwarf2out_frame_finish (void)
1112 {
1113   /* Output call frame information.  */
1114   if (targetm.debug_unwind_info () == UI_DWARF2)
1115     output_call_frame_info (0);
1116
1117   /* Output another copy for the unwinder.  */
1118   if ((flag_unwind_tables || flag_exceptions)
1119       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1120     output_call_frame_info (1);
1121 }
1122
1123 /* Note that the current function section is being used for code.  */
1124
1125 static void
1126 dwarf2out_note_section_used (void)
1127 {
1128   section *sec = current_function_section ();
1129   if (sec == text_section)
1130     text_section_used = true;
1131   else if (sec == cold_text_section)
1132     cold_text_section_used = true;
1133 }
1134
1135 static void var_location_switch_text_section (void);
1136 static void set_cur_line_info_table (section *);
1137
1138 void
1139 dwarf2out_switch_text_section (void)
1140 {
1141   section *sect;
1142   dw_fde_ref fde = cfun->fde;
1143
1144   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1145
1146   if (!in_cold_section_p)
1147     {
1148       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1149       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1150       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1151     }
1152   else
1153     {
1154       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1155       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1156       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1157     }
1158   have_multiple_function_sections = true;
1159
1160   /* There is no need to mark used sections when not debugging.  */
1161   if (cold_text_section != NULL)
1162     dwarf2out_note_section_used ();
1163
1164   if (dwarf2out_do_cfi_asm ())
1165     fprintf (asm_out_file, "\t.cfi_endproc\n");
1166
1167   /* Now do the real section switch.  */
1168   sect = current_function_section ();
1169   switch_to_section (sect);
1170
1171   fde->second_in_std_section
1172     = (sect == text_section
1173        || (cold_text_section && sect == cold_text_section));
1174
1175   if (dwarf2out_do_cfi_asm ())
1176     dwarf2out_do_cfi_startproc (true);
1177
1178   var_location_switch_text_section ();
1179
1180   if (cold_text_section != NULL)
1181     set_cur_line_info_table (sect);
1182 }
1183 \f
1184 /* And now, the subset of the debugging information support code necessary
1185    for emitting location expressions.  */
1186
1187 /* Data about a single source file.  */
1188 struct GTY(()) dwarf_file_data {
1189   const char * filename;
1190   int emitted_number;
1191 };
1192
1193 typedef struct GTY(()) deferred_locations_struct
1194 {
1195   tree variable;
1196   dw_die_ref die;
1197 } deferred_locations;
1198
1199 DEF_VEC_O(deferred_locations);
1200 DEF_VEC_ALLOC_O(deferred_locations,gc);
1201
1202 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1203
1204 DEF_VEC_P(dw_die_ref);
1205 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1206
1207 /* Location lists are ranges + location descriptions for that range,
1208    so you can track variables that are in different places over
1209    their entire life.  */
1210 typedef struct GTY(()) dw_loc_list_struct {
1211   dw_loc_list_ref dw_loc_next;
1212   const char *begin; /* Label for begin address of range */
1213   const char *end;  /* Label for end address of range */
1214   char *ll_symbol; /* Label for beginning of location list.
1215                       Only on head of list */
1216   const char *section; /* Section this loclist is relative to */
1217   dw_loc_descr_ref expr;
1218   hashval_t hash;
1219   /* True if all addresses in this and subsequent lists are known to be
1220      resolved.  */
1221   bool resolved_addr;
1222   /* True if this list has been replaced by dw_loc_next.  */
1223   bool replaced;
1224   bool emitted;
1225   /* True if the range should be emitted even if begin and end
1226      are the same.  */
1227   bool force;
1228 } dw_loc_list_node;
1229
1230 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1231
1232 /* Convert a DWARF stack opcode into its string name.  */
1233
1234 static const char *
1235 dwarf_stack_op_name (unsigned int op)
1236 {
1237   switch (op)
1238     {
1239     case DW_OP_addr:
1240       return "DW_OP_addr";
1241     case DW_OP_deref:
1242       return "DW_OP_deref";
1243     case DW_OP_const1u:
1244       return "DW_OP_const1u";
1245     case DW_OP_const1s:
1246       return "DW_OP_const1s";
1247     case DW_OP_const2u:
1248       return "DW_OP_const2u";
1249     case DW_OP_const2s:
1250       return "DW_OP_const2s";
1251     case DW_OP_const4u:
1252       return "DW_OP_const4u";
1253     case DW_OP_const4s:
1254       return "DW_OP_const4s";
1255     case DW_OP_const8u:
1256       return "DW_OP_const8u";
1257     case DW_OP_const8s:
1258       return "DW_OP_const8s";
1259     case DW_OP_constu:
1260       return "DW_OP_constu";
1261     case DW_OP_consts:
1262       return "DW_OP_consts";
1263     case DW_OP_dup:
1264       return "DW_OP_dup";
1265     case DW_OP_drop:
1266       return "DW_OP_drop";
1267     case DW_OP_over:
1268       return "DW_OP_over";
1269     case DW_OP_pick:
1270       return "DW_OP_pick";
1271     case DW_OP_swap:
1272       return "DW_OP_swap";
1273     case DW_OP_rot:
1274       return "DW_OP_rot";
1275     case DW_OP_xderef:
1276       return "DW_OP_xderef";
1277     case DW_OP_abs:
1278       return "DW_OP_abs";
1279     case DW_OP_and:
1280       return "DW_OP_and";
1281     case DW_OP_div:
1282       return "DW_OP_div";
1283     case DW_OP_minus:
1284       return "DW_OP_minus";
1285     case DW_OP_mod:
1286       return "DW_OP_mod";
1287     case DW_OP_mul:
1288       return "DW_OP_mul";
1289     case DW_OP_neg:
1290       return "DW_OP_neg";
1291     case DW_OP_not:
1292       return "DW_OP_not";
1293     case DW_OP_or:
1294       return "DW_OP_or";
1295     case DW_OP_plus:
1296       return "DW_OP_plus";
1297     case DW_OP_plus_uconst:
1298       return "DW_OP_plus_uconst";
1299     case DW_OP_shl:
1300       return "DW_OP_shl";
1301     case DW_OP_shr:
1302       return "DW_OP_shr";
1303     case DW_OP_shra:
1304       return "DW_OP_shra";
1305     case DW_OP_xor:
1306       return "DW_OP_xor";
1307     case DW_OP_bra:
1308       return "DW_OP_bra";
1309     case DW_OP_eq:
1310       return "DW_OP_eq";
1311     case DW_OP_ge:
1312       return "DW_OP_ge";
1313     case DW_OP_gt:
1314       return "DW_OP_gt";
1315     case DW_OP_le:
1316       return "DW_OP_le";
1317     case DW_OP_lt:
1318       return "DW_OP_lt";
1319     case DW_OP_ne:
1320       return "DW_OP_ne";
1321     case DW_OP_skip:
1322       return "DW_OP_skip";
1323     case DW_OP_lit0:
1324       return "DW_OP_lit0";
1325     case DW_OP_lit1:
1326       return "DW_OP_lit1";
1327     case DW_OP_lit2:
1328       return "DW_OP_lit2";
1329     case DW_OP_lit3:
1330       return "DW_OP_lit3";
1331     case DW_OP_lit4:
1332       return "DW_OP_lit4";
1333     case DW_OP_lit5:
1334       return "DW_OP_lit5";
1335     case DW_OP_lit6:
1336       return "DW_OP_lit6";
1337     case DW_OP_lit7:
1338       return "DW_OP_lit7";
1339     case DW_OP_lit8:
1340       return "DW_OP_lit8";
1341     case DW_OP_lit9:
1342       return "DW_OP_lit9";
1343     case DW_OP_lit10:
1344       return "DW_OP_lit10";
1345     case DW_OP_lit11:
1346       return "DW_OP_lit11";
1347     case DW_OP_lit12:
1348       return "DW_OP_lit12";
1349     case DW_OP_lit13:
1350       return "DW_OP_lit13";
1351     case DW_OP_lit14:
1352       return "DW_OP_lit14";
1353     case DW_OP_lit15:
1354       return "DW_OP_lit15";
1355     case DW_OP_lit16:
1356       return "DW_OP_lit16";
1357     case DW_OP_lit17:
1358       return "DW_OP_lit17";
1359     case DW_OP_lit18:
1360       return "DW_OP_lit18";
1361     case DW_OP_lit19:
1362       return "DW_OP_lit19";
1363     case DW_OP_lit20:
1364       return "DW_OP_lit20";
1365     case DW_OP_lit21:
1366       return "DW_OP_lit21";
1367     case DW_OP_lit22:
1368       return "DW_OP_lit22";
1369     case DW_OP_lit23:
1370       return "DW_OP_lit23";
1371     case DW_OP_lit24:
1372       return "DW_OP_lit24";
1373     case DW_OP_lit25:
1374       return "DW_OP_lit25";
1375     case DW_OP_lit26:
1376       return "DW_OP_lit26";
1377     case DW_OP_lit27:
1378       return "DW_OP_lit27";
1379     case DW_OP_lit28:
1380       return "DW_OP_lit28";
1381     case DW_OP_lit29:
1382       return "DW_OP_lit29";
1383     case DW_OP_lit30:
1384       return "DW_OP_lit30";
1385     case DW_OP_lit31:
1386       return "DW_OP_lit31";
1387     case DW_OP_reg0:
1388       return "DW_OP_reg0";
1389     case DW_OP_reg1:
1390       return "DW_OP_reg1";
1391     case DW_OP_reg2:
1392       return "DW_OP_reg2";
1393     case DW_OP_reg3:
1394       return "DW_OP_reg3";
1395     case DW_OP_reg4:
1396       return "DW_OP_reg4";
1397     case DW_OP_reg5:
1398       return "DW_OP_reg5";
1399     case DW_OP_reg6:
1400       return "DW_OP_reg6";
1401     case DW_OP_reg7:
1402       return "DW_OP_reg7";
1403     case DW_OP_reg8:
1404       return "DW_OP_reg8";
1405     case DW_OP_reg9:
1406       return "DW_OP_reg9";
1407     case DW_OP_reg10:
1408       return "DW_OP_reg10";
1409     case DW_OP_reg11:
1410       return "DW_OP_reg11";
1411     case DW_OP_reg12:
1412       return "DW_OP_reg12";
1413     case DW_OP_reg13:
1414       return "DW_OP_reg13";
1415     case DW_OP_reg14:
1416       return "DW_OP_reg14";
1417     case DW_OP_reg15:
1418       return "DW_OP_reg15";
1419     case DW_OP_reg16:
1420       return "DW_OP_reg16";
1421     case DW_OP_reg17:
1422       return "DW_OP_reg17";
1423     case DW_OP_reg18:
1424       return "DW_OP_reg18";
1425     case DW_OP_reg19:
1426       return "DW_OP_reg19";
1427     case DW_OP_reg20:
1428       return "DW_OP_reg20";
1429     case DW_OP_reg21:
1430       return "DW_OP_reg21";
1431     case DW_OP_reg22:
1432       return "DW_OP_reg22";
1433     case DW_OP_reg23:
1434       return "DW_OP_reg23";
1435     case DW_OP_reg24:
1436       return "DW_OP_reg24";
1437     case DW_OP_reg25:
1438       return "DW_OP_reg25";
1439     case DW_OP_reg26:
1440       return "DW_OP_reg26";
1441     case DW_OP_reg27:
1442       return "DW_OP_reg27";
1443     case DW_OP_reg28:
1444       return "DW_OP_reg28";
1445     case DW_OP_reg29:
1446       return "DW_OP_reg29";
1447     case DW_OP_reg30:
1448       return "DW_OP_reg30";
1449     case DW_OP_reg31:
1450       return "DW_OP_reg31";
1451     case DW_OP_breg0:
1452       return "DW_OP_breg0";
1453     case DW_OP_breg1:
1454       return "DW_OP_breg1";
1455     case DW_OP_breg2:
1456       return "DW_OP_breg2";
1457     case DW_OP_breg3:
1458       return "DW_OP_breg3";
1459     case DW_OP_breg4:
1460       return "DW_OP_breg4";
1461     case DW_OP_breg5:
1462       return "DW_OP_breg5";
1463     case DW_OP_breg6:
1464       return "DW_OP_breg6";
1465     case DW_OP_breg7:
1466       return "DW_OP_breg7";
1467     case DW_OP_breg8:
1468       return "DW_OP_breg8";
1469     case DW_OP_breg9:
1470       return "DW_OP_breg9";
1471     case DW_OP_breg10:
1472       return "DW_OP_breg10";
1473     case DW_OP_breg11:
1474       return "DW_OP_breg11";
1475     case DW_OP_breg12:
1476       return "DW_OP_breg12";
1477     case DW_OP_breg13:
1478       return "DW_OP_breg13";
1479     case DW_OP_breg14:
1480       return "DW_OP_breg14";
1481     case DW_OP_breg15:
1482       return "DW_OP_breg15";
1483     case DW_OP_breg16:
1484       return "DW_OP_breg16";
1485     case DW_OP_breg17:
1486       return "DW_OP_breg17";
1487     case DW_OP_breg18:
1488       return "DW_OP_breg18";
1489     case DW_OP_breg19:
1490       return "DW_OP_breg19";
1491     case DW_OP_breg20:
1492       return "DW_OP_breg20";
1493     case DW_OP_breg21:
1494       return "DW_OP_breg21";
1495     case DW_OP_breg22:
1496       return "DW_OP_breg22";
1497     case DW_OP_breg23:
1498       return "DW_OP_breg23";
1499     case DW_OP_breg24:
1500       return "DW_OP_breg24";
1501     case DW_OP_breg25:
1502       return "DW_OP_breg25";
1503     case DW_OP_breg26:
1504       return "DW_OP_breg26";
1505     case DW_OP_breg27:
1506       return "DW_OP_breg27";
1507     case DW_OP_breg28:
1508       return "DW_OP_breg28";
1509     case DW_OP_breg29:
1510       return "DW_OP_breg29";
1511     case DW_OP_breg30:
1512       return "DW_OP_breg30";
1513     case DW_OP_breg31:
1514       return "DW_OP_breg31";
1515     case DW_OP_regx:
1516       return "DW_OP_regx";
1517     case DW_OP_fbreg:
1518       return "DW_OP_fbreg";
1519     case DW_OP_bregx:
1520       return "DW_OP_bregx";
1521     case DW_OP_piece:
1522       return "DW_OP_piece";
1523     case DW_OP_deref_size:
1524       return "DW_OP_deref_size";
1525     case DW_OP_xderef_size:
1526       return "DW_OP_xderef_size";
1527     case DW_OP_nop:
1528       return "DW_OP_nop";
1529
1530     case DW_OP_push_object_address:
1531       return "DW_OP_push_object_address";
1532     case DW_OP_call2:
1533       return "DW_OP_call2";
1534     case DW_OP_call4:
1535       return "DW_OP_call4";
1536     case DW_OP_call_ref:
1537       return "DW_OP_call_ref";
1538     case DW_OP_implicit_value:
1539       return "DW_OP_implicit_value";
1540     case DW_OP_stack_value:
1541       return "DW_OP_stack_value";
1542     case DW_OP_form_tls_address:
1543       return "DW_OP_form_tls_address";
1544     case DW_OP_call_frame_cfa:
1545       return "DW_OP_call_frame_cfa";
1546     case DW_OP_bit_piece:
1547       return "DW_OP_bit_piece";
1548
1549     case DW_OP_GNU_push_tls_address:
1550       return "DW_OP_GNU_push_tls_address";
1551     case DW_OP_GNU_uninit:
1552       return "DW_OP_GNU_uninit";
1553     case DW_OP_GNU_encoded_addr:
1554       return "DW_OP_GNU_encoded_addr";
1555     case DW_OP_GNU_implicit_pointer:
1556       return "DW_OP_GNU_implicit_pointer";
1557     case DW_OP_GNU_entry_value:
1558       return "DW_OP_GNU_entry_value";
1559     case DW_OP_GNU_const_type:
1560       return "DW_OP_GNU_const_type";
1561     case DW_OP_GNU_regval_type:
1562       return "DW_OP_GNU_regval_type";
1563     case DW_OP_GNU_deref_type:
1564       return "DW_OP_GNU_deref_type";
1565     case DW_OP_GNU_convert:
1566       return "DW_OP_GNU_convert";
1567     case DW_OP_GNU_reinterpret:
1568       return "DW_OP_GNU_reinterpret";
1569     case DW_OP_GNU_parameter_ref:
1570       return "DW_OP_GNU_parameter_ref";
1571
1572     default:
1573       return "OP_<unknown>";
1574     }
1575 }
1576
1577 /* Return a pointer to a newly allocated location description.  Location
1578    descriptions are simple expression terms that can be strung
1579    together to form more complicated location (address) descriptions.  */
1580
1581 static inline dw_loc_descr_ref
1582 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1583                unsigned HOST_WIDE_INT oprnd2)
1584 {
1585   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1586
1587   descr->dw_loc_opc = op;
1588   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1589   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1590   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1591   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1592
1593   return descr;
1594 }
1595
1596 /* Return a pointer to a newly allocated location description for
1597    REG and OFFSET.  */
1598
1599 static inline dw_loc_descr_ref
1600 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1601 {
1602   if (reg <= 31)
1603     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1604                           offset, 0);
1605   else
1606     return new_loc_descr (DW_OP_bregx, reg, offset);
1607 }
1608
1609 /* Add a location description term to a location description expression.  */
1610
1611 static inline void
1612 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1613 {
1614   dw_loc_descr_ref *d;
1615
1616   /* Find the end of the chain.  */
1617   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1618     ;
1619
1620   *d = descr;
1621 }
1622
1623 /* Compare two location operands for exact equality.  */
1624
1625 static bool
1626 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1627 {
1628   if (a->val_class != b->val_class)
1629     return false;
1630   switch (a->val_class)
1631     {
1632     case dw_val_class_none:
1633       return true;
1634     case dw_val_class_addr:
1635       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1636
1637     case dw_val_class_offset:
1638     case dw_val_class_unsigned_const:
1639     case dw_val_class_const:
1640     case dw_val_class_range_list:
1641     case dw_val_class_lineptr:
1642     case dw_val_class_macptr:
1643       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1644       return a->v.val_unsigned == b->v.val_unsigned;
1645
1646     case dw_val_class_loc:
1647       return a->v.val_loc == b->v.val_loc;
1648     case dw_val_class_loc_list:
1649       return a->v.val_loc_list == b->v.val_loc_list;
1650     case dw_val_class_die_ref:
1651       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1652     case dw_val_class_fde_ref:
1653       return a->v.val_fde_index == b->v.val_fde_index;
1654     case dw_val_class_lbl_id:
1655       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1656     case dw_val_class_str:
1657       return a->v.val_str == b->v.val_str;
1658     case dw_val_class_flag:
1659       return a->v.val_flag == b->v.val_flag;
1660     case dw_val_class_file:
1661       return a->v.val_file == b->v.val_file;
1662     case dw_val_class_decl_ref:
1663       return a->v.val_decl_ref == b->v.val_decl_ref;
1664     
1665     case dw_val_class_const_double:
1666       return (a->v.val_double.high == b->v.val_double.high
1667               && a->v.val_double.low == b->v.val_double.low);
1668
1669     case dw_val_class_vec:
1670       {
1671         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1672         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1673
1674         return (a_len == b_len
1675                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1676       }
1677
1678     case dw_val_class_data8:
1679       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1680
1681     case dw_val_class_vms_delta:
1682       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1683               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1684     }
1685   gcc_unreachable ();
1686 }
1687
1688 /* Compare two location atoms for exact equality.  */
1689
1690 static bool
1691 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1692 {
1693   if (a->dw_loc_opc != b->dw_loc_opc)
1694     return false;
1695
1696   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1697      address size, but since we always allocate cleared storage it
1698      should be zero for other types of locations.  */
1699   if (a->dtprel != b->dtprel)
1700     return false;
1701
1702   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1703           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1704 }
1705
1706 /* Compare two complete location expressions for exact equality.  */
1707
1708 bool
1709 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1710 {
1711   while (1)
1712     {
1713       if (a == b)
1714         return true;
1715       if (a == NULL || b == NULL)
1716         return false;
1717       if (!loc_descr_equal_p_1 (a, b))
1718         return false;
1719
1720       a = a->dw_loc_next;
1721       b = b->dw_loc_next;
1722     }
1723 }
1724
1725
1726 /* Add a constant OFFSET to a location expression.  */
1727
1728 static void
1729 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1730 {
1731   dw_loc_descr_ref loc;
1732   HOST_WIDE_INT *p;
1733
1734   gcc_assert (*list_head != NULL);
1735
1736   if (!offset)
1737     return;
1738
1739   /* Find the end of the chain.  */
1740   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1741     ;
1742
1743   p = NULL;
1744   if (loc->dw_loc_opc == DW_OP_fbreg
1745       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1746     p = &loc->dw_loc_oprnd1.v.val_int;
1747   else if (loc->dw_loc_opc == DW_OP_bregx)
1748     p = &loc->dw_loc_oprnd2.v.val_int;
1749
1750   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1751      offset.  Don't optimize if an signed integer overflow would happen.  */
1752   if (p != NULL
1753       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1754           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1755     *p += offset;
1756
1757   else if (offset > 0)
1758     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1759
1760   else
1761     {
1762       loc->dw_loc_next = int_loc_descriptor (-offset);
1763       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1764     }
1765 }
1766
1767 /* Add a constant OFFSET to a location list.  */
1768
1769 static void
1770 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1771 {
1772   dw_loc_list_ref d;
1773   for (d = list_head; d != NULL; d = d->dw_loc_next)
1774     loc_descr_plus_const (&d->expr, offset);
1775 }
1776
1777 #define DWARF_REF_SIZE  \
1778   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1779
1780 static unsigned long int get_base_type_offset (dw_die_ref);
1781
1782 /* Return the size of a location descriptor.  */
1783
1784 static unsigned long
1785 size_of_loc_descr (dw_loc_descr_ref loc)
1786 {
1787   unsigned long size = 1;
1788
1789   switch (loc->dw_loc_opc)
1790     {
1791     case DW_OP_addr:
1792       size += DWARF2_ADDR_SIZE;
1793       break;
1794     case DW_OP_const1u:
1795     case DW_OP_const1s:
1796       size += 1;
1797       break;
1798     case DW_OP_const2u:
1799     case DW_OP_const2s:
1800       size += 2;
1801       break;
1802     case DW_OP_const4u:
1803     case DW_OP_const4s:
1804       size += 4;
1805       break;
1806     case DW_OP_const8u:
1807     case DW_OP_const8s:
1808       size += 8;
1809       break;
1810     case DW_OP_constu:
1811       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1812       break;
1813     case DW_OP_consts:
1814       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1815       break;
1816     case DW_OP_pick:
1817       size += 1;
1818       break;
1819     case DW_OP_plus_uconst:
1820       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1821       break;
1822     case DW_OP_skip:
1823     case DW_OP_bra:
1824       size += 2;
1825       break;
1826     case DW_OP_breg0:
1827     case DW_OP_breg1:
1828     case DW_OP_breg2:
1829     case DW_OP_breg3:
1830     case DW_OP_breg4:
1831     case DW_OP_breg5:
1832     case DW_OP_breg6:
1833     case DW_OP_breg7:
1834     case DW_OP_breg8:
1835     case DW_OP_breg9:
1836     case DW_OP_breg10:
1837     case DW_OP_breg11:
1838     case DW_OP_breg12:
1839     case DW_OP_breg13:
1840     case DW_OP_breg14:
1841     case DW_OP_breg15:
1842     case DW_OP_breg16:
1843     case DW_OP_breg17:
1844     case DW_OP_breg18:
1845     case DW_OP_breg19:
1846     case DW_OP_breg20:
1847     case DW_OP_breg21:
1848     case DW_OP_breg22:
1849     case DW_OP_breg23:
1850     case DW_OP_breg24:
1851     case DW_OP_breg25:
1852     case DW_OP_breg26:
1853     case DW_OP_breg27:
1854     case DW_OP_breg28:
1855     case DW_OP_breg29:
1856     case DW_OP_breg30:
1857     case DW_OP_breg31:
1858       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1859       break;
1860     case DW_OP_regx:
1861       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1862       break;
1863     case DW_OP_fbreg:
1864       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1865       break;
1866     case DW_OP_bregx:
1867       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1868       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1869       break;
1870     case DW_OP_piece:
1871       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1872       break;
1873     case DW_OP_bit_piece:
1874       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1875       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1876       break;
1877     case DW_OP_deref_size:
1878     case DW_OP_xderef_size:
1879       size += 1;
1880       break;
1881     case DW_OP_call2:
1882       size += 2;
1883       break;
1884     case DW_OP_call4:
1885       size += 4;
1886       break;
1887     case DW_OP_call_ref:
1888       size += DWARF_REF_SIZE;
1889       break;
1890     case DW_OP_implicit_value:
1891       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1892               + loc->dw_loc_oprnd1.v.val_unsigned;
1893       break;
1894     case DW_OP_GNU_implicit_pointer:
1895       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1896       break;
1897     case DW_OP_GNU_entry_value:
1898       {
1899         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1900         size += size_of_uleb128 (op_size) + op_size;
1901         break;
1902       }
1903     case DW_OP_GNU_const_type:
1904       {
1905         unsigned long o
1906           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1907         size += size_of_uleb128 (o) + 1;
1908         switch (loc->dw_loc_oprnd2.val_class)
1909           {
1910           case dw_val_class_vec:
1911             size += loc->dw_loc_oprnd2.v.val_vec.length
1912                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1913             break;
1914           case dw_val_class_const:
1915             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1916             break;
1917           case dw_val_class_const_double:
1918             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1919             break;
1920           default:
1921             gcc_unreachable ();
1922           }
1923         break;
1924       }
1925     case DW_OP_GNU_regval_type:
1926       {
1927         unsigned long o
1928           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1929         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1930                 + size_of_uleb128 (o);
1931       }
1932       break;
1933     case DW_OP_GNU_deref_type:
1934       {
1935         unsigned long o
1936           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1937         size += 1 + size_of_uleb128 (o);
1938       }
1939       break;
1940     case DW_OP_GNU_convert:
1941     case DW_OP_GNU_reinterpret:
1942       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1943         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944       else
1945         {
1946           unsigned long o
1947             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1948           size += size_of_uleb128 (o);
1949         }
1950       break;
1951     case DW_OP_GNU_parameter_ref:
1952       size += 4;
1953       break;
1954     default:
1955       break;
1956     }
1957
1958   return size;
1959 }
1960
1961 /* Return the size of a series of location descriptors.  */
1962
1963 unsigned long
1964 size_of_locs (dw_loc_descr_ref loc)
1965 {
1966   dw_loc_descr_ref l;
1967   unsigned long size;
1968
1969   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1970      field, to avoid writing to a PCH file.  */
1971   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1972     {
1973       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1974         break;
1975       size += size_of_loc_descr (l);
1976     }
1977   if (! l)
1978     return size;
1979
1980   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1981     {
1982       l->dw_loc_addr = size;
1983       size += size_of_loc_descr (l);
1984     }
1985
1986   return size;
1987 }
1988
1989 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1990 static void get_ref_die_offset_label (char *, dw_die_ref);
1991 static unsigned long int get_ref_die_offset (dw_die_ref);
1992
1993 /* Output location description stack opcode's operands (if any).
1994    The for_eh_or_skip parameter controls whether register numbers are
1995    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1996    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1997    info).  This should be suppressed for the cases that have not been converted
1998    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1999
2000 static void
2001 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2002 {
2003   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2004   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2005
2006   switch (loc->dw_loc_opc)
2007     {
2008 #ifdef DWARF2_DEBUGGING_INFO
2009     case DW_OP_const2u:
2010     case DW_OP_const2s:
2011       dw2_asm_output_data (2, val1->v.val_int, NULL);
2012       break;
2013     case DW_OP_const4u:
2014       if (loc->dtprel)
2015         {
2016           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2017           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2018                                                val1->v.val_addr);
2019           fputc ('\n', asm_out_file);
2020           break;
2021         }
2022       /* FALLTHRU */
2023     case DW_OP_const4s:
2024       dw2_asm_output_data (4, val1->v.val_int, NULL);
2025       break;
2026     case DW_OP_const8u:
2027       if (loc->dtprel)
2028         {
2029           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2030           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2031                                                val1->v.val_addr);
2032           fputc ('\n', asm_out_file);
2033           break;
2034         }
2035       /* FALLTHRU */
2036     case DW_OP_const8s:
2037       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2038       dw2_asm_output_data (8, val1->v.val_int, NULL);
2039       break;
2040     case DW_OP_skip:
2041     case DW_OP_bra:
2042       {
2043         int offset;
2044
2045         gcc_assert (val1->val_class == dw_val_class_loc);
2046         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2047
2048         dw2_asm_output_data (2, offset, NULL);
2049       }
2050       break;
2051     case DW_OP_implicit_value:
2052       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2053       switch (val2->val_class)
2054         {
2055         case dw_val_class_const:
2056           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2057           break;
2058         case dw_val_class_vec:
2059           {
2060             unsigned int elt_size = val2->v.val_vec.elt_size;
2061             unsigned int len = val2->v.val_vec.length;
2062             unsigned int i;
2063             unsigned char *p;
2064
2065             if (elt_size > sizeof (HOST_WIDE_INT))
2066               {
2067                 elt_size /= 2;
2068                 len *= 2;
2069               }
2070             for (i = 0, p = val2->v.val_vec.array;
2071                  i < len;
2072                  i++, p += elt_size)
2073               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2074                                    "fp or vector constant word %u", i);
2075           }
2076           break;
2077         case dw_val_class_const_double:
2078           {
2079             unsigned HOST_WIDE_INT first, second;
2080
2081             if (WORDS_BIG_ENDIAN)
2082               {
2083                 first = val2->v.val_double.high;
2084                 second = val2->v.val_double.low;
2085               }
2086             else
2087               {
2088                 first = val2->v.val_double.low;
2089                 second = val2->v.val_double.high;
2090               }
2091             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2092                                  first, NULL);
2093             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2094                                  second, NULL);
2095           }
2096           break;
2097         case dw_val_class_addr:
2098           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2099           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2100           break;
2101         default:
2102           gcc_unreachable ();
2103         }
2104       break;
2105 #else
2106     case DW_OP_const2u:
2107     case DW_OP_const2s:
2108     case DW_OP_const4u:
2109     case DW_OP_const4s:
2110     case DW_OP_const8u:
2111     case DW_OP_const8s:
2112     case DW_OP_skip:
2113     case DW_OP_bra:
2114     case DW_OP_implicit_value:
2115       /* We currently don't make any attempt to make sure these are
2116          aligned properly like we do for the main unwind info, so
2117          don't support emitting things larger than a byte if we're
2118          only doing unwinding.  */
2119       gcc_unreachable ();
2120 #endif
2121     case DW_OP_const1u:
2122     case DW_OP_const1s:
2123       dw2_asm_output_data (1, val1->v.val_int, NULL);
2124       break;
2125     case DW_OP_constu:
2126       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2127       break;
2128     case DW_OP_consts:
2129       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2130       break;
2131     case DW_OP_pick:
2132       dw2_asm_output_data (1, val1->v.val_int, NULL);
2133       break;
2134     case DW_OP_plus_uconst:
2135       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2136       break;
2137     case DW_OP_breg0:
2138     case DW_OP_breg1:
2139     case DW_OP_breg2:
2140     case DW_OP_breg3:
2141     case DW_OP_breg4:
2142     case DW_OP_breg5:
2143     case DW_OP_breg6:
2144     case DW_OP_breg7:
2145     case DW_OP_breg8:
2146     case DW_OP_breg9:
2147     case DW_OP_breg10:
2148     case DW_OP_breg11:
2149     case DW_OP_breg12:
2150     case DW_OP_breg13:
2151     case DW_OP_breg14:
2152     case DW_OP_breg15:
2153     case DW_OP_breg16:
2154     case DW_OP_breg17:
2155     case DW_OP_breg18:
2156     case DW_OP_breg19:
2157     case DW_OP_breg20:
2158     case DW_OP_breg21:
2159     case DW_OP_breg22:
2160     case DW_OP_breg23:
2161     case DW_OP_breg24:
2162     case DW_OP_breg25:
2163     case DW_OP_breg26:
2164     case DW_OP_breg27:
2165     case DW_OP_breg28:
2166     case DW_OP_breg29:
2167     case DW_OP_breg30:
2168     case DW_OP_breg31:
2169       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2170       break;
2171     case DW_OP_regx:
2172       {
2173         unsigned r = val1->v.val_unsigned;
2174         if (for_eh_or_skip >= 0)
2175           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2176         gcc_assert (size_of_uleb128 (r) 
2177                     == size_of_uleb128 (val1->v.val_unsigned));
2178         dw2_asm_output_data_uleb128 (r, NULL);  
2179       }
2180       break;
2181     case DW_OP_fbreg:
2182       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2183       break;
2184     case DW_OP_bregx:
2185       {
2186         unsigned r = val1->v.val_unsigned;
2187         if (for_eh_or_skip >= 0)
2188           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2189         gcc_assert (size_of_uleb128 (r) 
2190                     == size_of_uleb128 (val1->v.val_unsigned));
2191         dw2_asm_output_data_uleb128 (r, NULL);  
2192         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2193       }
2194       break;
2195     case DW_OP_piece:
2196       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2197       break;
2198     case DW_OP_bit_piece:
2199       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2200       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2201       break;
2202     case DW_OP_deref_size:
2203     case DW_OP_xderef_size:
2204       dw2_asm_output_data (1, val1->v.val_int, NULL);
2205       break;
2206
2207     case DW_OP_addr:
2208       if (loc->dtprel)
2209         {
2210           if (targetm.asm_out.output_dwarf_dtprel)
2211             {
2212               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2213                                                    DWARF2_ADDR_SIZE,
2214                                                    val1->v.val_addr);
2215               fputc ('\n', asm_out_file);
2216             }
2217           else
2218             gcc_unreachable ();
2219         }
2220       else
2221         {
2222 #ifdef DWARF2_DEBUGGING_INFO
2223           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2224 #else
2225           gcc_unreachable ();
2226 #endif
2227         }
2228       break;
2229
2230     case DW_OP_GNU_implicit_pointer:
2231       {
2232         char label[MAX_ARTIFICIAL_LABEL_BYTES
2233                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2234         gcc_assert (val1->val_class == dw_val_class_die_ref);
2235         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2236         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2237         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2238       }
2239       break;
2240
2241     case DW_OP_GNU_entry_value:
2242       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2243       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2244       break;
2245
2246     case DW_OP_GNU_const_type:
2247       {
2248         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2249         gcc_assert (o);
2250         dw2_asm_output_data_uleb128 (o, NULL);
2251         switch (val2->val_class)
2252           {
2253           case dw_val_class_const:
2254             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2255             dw2_asm_output_data (1, l, NULL);
2256             dw2_asm_output_data (l, val2->v.val_int, NULL);
2257             break;
2258           case dw_val_class_vec:
2259             {
2260               unsigned int elt_size = val2->v.val_vec.elt_size;
2261               unsigned int len = val2->v.val_vec.length;
2262               unsigned int i;
2263               unsigned char *p;
2264
2265               l = len * elt_size;
2266               dw2_asm_output_data (1, l, NULL);
2267               if (elt_size > sizeof (HOST_WIDE_INT))
2268                 {
2269                   elt_size /= 2;
2270                   len *= 2;
2271                 }
2272               for (i = 0, p = val2->v.val_vec.array;
2273                    i < len;
2274                    i++, p += elt_size)
2275                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2276                                      "fp or vector constant word %u", i);
2277             }
2278             break;
2279           case dw_val_class_const_double:
2280             {
2281               unsigned HOST_WIDE_INT first, second;
2282               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2283
2284               dw2_asm_output_data (1, 2 * l, NULL);
2285               if (WORDS_BIG_ENDIAN)
2286                 {
2287                   first = val2->v.val_double.high;
2288                   second = val2->v.val_double.low;
2289                 }
2290               else
2291                 {
2292                   first = val2->v.val_double.low;
2293                   second = val2->v.val_double.high;
2294                 }
2295               dw2_asm_output_data (l, first, NULL);
2296               dw2_asm_output_data (l, second, NULL);
2297             }
2298             break;
2299           default:
2300             gcc_unreachable ();
2301           }
2302       }
2303       break;
2304     case DW_OP_GNU_regval_type:
2305       {
2306         unsigned r = val1->v.val_unsigned;
2307         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2308         gcc_assert (o);
2309         if (for_eh_or_skip >= 0)
2310           {
2311             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2312             gcc_assert (size_of_uleb128 (r)
2313                         == size_of_uleb128 (val1->v.val_unsigned));
2314           }
2315         dw2_asm_output_data_uleb128 (r, NULL);
2316         dw2_asm_output_data_uleb128 (o, NULL);
2317       }
2318       break;
2319     case DW_OP_GNU_deref_type:
2320       {
2321         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2322         gcc_assert (o);
2323         dw2_asm_output_data (1, val1->v.val_int, NULL);
2324         dw2_asm_output_data_uleb128 (o, NULL);
2325       }
2326       break;
2327     case DW_OP_GNU_convert:
2328     case DW_OP_GNU_reinterpret:
2329       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2330         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2331       else
2332         {
2333           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2334           gcc_assert (o);
2335           dw2_asm_output_data_uleb128 (o, NULL);
2336         }
2337       break;
2338
2339     case DW_OP_GNU_parameter_ref:
2340       {
2341         unsigned long o;
2342         gcc_assert (val1->val_class == dw_val_class_die_ref);
2343         o = get_ref_die_offset (val1->v.val_die_ref.die);
2344         dw2_asm_output_data (4, o, NULL);
2345       }
2346       break;
2347
2348     default:
2349       /* Other codes have no operands.  */
2350       break;
2351     }
2352 }
2353
2354 /* Output a sequence of location operations.  
2355    The for_eh_or_skip parameter controls whether register numbers are
2356    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2357    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2358    info).  This should be suppressed for the cases that have not been converted
2359    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2360
2361 void
2362 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2363 {
2364   for (; loc != NULL; loc = loc->dw_loc_next)
2365     {
2366       enum dwarf_location_atom opc = loc->dw_loc_opc;
2367       /* Output the opcode.  */
2368       if (for_eh_or_skip >= 0 
2369           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2370         {
2371           unsigned r = (opc - DW_OP_breg0);
2372           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2373           gcc_assert (r <= 31);
2374           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2375         }
2376       else if (for_eh_or_skip >= 0 
2377                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2378         {
2379           unsigned r = (opc - DW_OP_reg0);
2380           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2381           gcc_assert (r <= 31);
2382           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2383         }
2384
2385       dw2_asm_output_data (1, opc,
2386                              "%s", dwarf_stack_op_name (opc));
2387
2388       /* Output the operand(s) (if any).  */
2389       output_loc_operands (loc, for_eh_or_skip);
2390     }
2391 }
2392
2393 /* Output location description stack opcode's operands (if any).
2394    The output is single bytes on a line, suitable for .cfi_escape.  */
2395
2396 static void
2397 output_loc_operands_raw (dw_loc_descr_ref loc)
2398 {
2399   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2400   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2401
2402   switch (loc->dw_loc_opc)
2403     {
2404     case DW_OP_addr:
2405     case DW_OP_implicit_value:
2406       /* We cannot output addresses in .cfi_escape, only bytes.  */
2407       gcc_unreachable ();
2408
2409     case DW_OP_const1u:
2410     case DW_OP_const1s:
2411     case DW_OP_pick:
2412     case DW_OP_deref_size:
2413     case DW_OP_xderef_size:
2414       fputc (',', asm_out_file);
2415       dw2_asm_output_data_raw (1, val1->v.val_int);
2416       break;
2417
2418     case DW_OP_const2u:
2419     case DW_OP_const2s:
2420       fputc (',', asm_out_file);
2421       dw2_asm_output_data_raw (2, val1->v.val_int);
2422       break;
2423
2424     case DW_OP_const4u:
2425     case DW_OP_const4s:
2426       fputc (',', asm_out_file);
2427       dw2_asm_output_data_raw (4, val1->v.val_int);
2428       break;
2429
2430     case DW_OP_const8u:
2431     case DW_OP_const8s:
2432       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2433       fputc (',', asm_out_file);
2434       dw2_asm_output_data_raw (8, val1->v.val_int);
2435       break;
2436
2437     case DW_OP_skip:
2438     case DW_OP_bra:
2439       {
2440         int offset;
2441
2442         gcc_assert (val1->val_class == dw_val_class_loc);
2443         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2444
2445         fputc (',', asm_out_file);
2446         dw2_asm_output_data_raw (2, offset);
2447       }
2448       break;
2449
2450     case DW_OP_regx:
2451       {
2452         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2453         gcc_assert (size_of_uleb128 (r) 
2454                     == size_of_uleb128 (val1->v.val_unsigned));
2455         fputc (',', asm_out_file);
2456         dw2_asm_output_data_uleb128_raw (r);
2457       }
2458       break;
2459       
2460     case DW_OP_constu:
2461     case DW_OP_plus_uconst:
2462     case DW_OP_piece:
2463       fputc (',', asm_out_file);
2464       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2465       break;
2466
2467     case DW_OP_bit_piece:
2468       fputc (',', asm_out_file);
2469       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2470       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2471       break;
2472
2473     case DW_OP_consts:
2474     case DW_OP_breg0:
2475     case DW_OP_breg1:
2476     case DW_OP_breg2:
2477     case DW_OP_breg3:
2478     case DW_OP_breg4:
2479     case DW_OP_breg5:
2480     case DW_OP_breg6:
2481     case DW_OP_breg7:
2482     case DW_OP_breg8:
2483     case DW_OP_breg9:
2484     case DW_OP_breg10:
2485     case DW_OP_breg11:
2486     case DW_OP_breg12:
2487     case DW_OP_breg13:
2488     case DW_OP_breg14:
2489     case DW_OP_breg15:
2490     case DW_OP_breg16:
2491     case DW_OP_breg17:
2492     case DW_OP_breg18:
2493     case DW_OP_breg19:
2494     case DW_OP_breg20:
2495     case DW_OP_breg21:
2496     case DW_OP_breg22:
2497     case DW_OP_breg23:
2498     case DW_OP_breg24:
2499     case DW_OP_breg25:
2500     case DW_OP_breg26:
2501     case DW_OP_breg27:
2502     case DW_OP_breg28:
2503     case DW_OP_breg29:
2504     case DW_OP_breg30:
2505     case DW_OP_breg31:
2506     case DW_OP_fbreg:
2507       fputc (',', asm_out_file);
2508       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2509       break;
2510
2511     case DW_OP_bregx:
2512       {
2513         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2514         gcc_assert (size_of_uleb128 (r) 
2515                     == size_of_uleb128 (val1->v.val_unsigned));
2516         fputc (',', asm_out_file);
2517         dw2_asm_output_data_uleb128_raw (r);
2518         fputc (',', asm_out_file);
2519         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2520       }
2521       break;
2522
2523     case DW_OP_GNU_implicit_pointer:
2524     case DW_OP_GNU_entry_value:
2525     case DW_OP_GNU_const_type:
2526     case DW_OP_GNU_regval_type:
2527     case DW_OP_GNU_deref_type:
2528     case DW_OP_GNU_convert:
2529     case DW_OP_GNU_reinterpret:
2530     case DW_OP_GNU_parameter_ref:
2531       gcc_unreachable ();
2532       break;
2533
2534     default:
2535       /* Other codes have no operands.  */
2536       break;
2537     }
2538 }
2539
2540 void
2541 output_loc_sequence_raw (dw_loc_descr_ref loc)
2542 {
2543   while (1)
2544     {
2545       enum dwarf_location_atom opc = loc->dw_loc_opc;
2546       /* Output the opcode.  */
2547       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2548         {
2549           unsigned r = (opc - DW_OP_breg0);
2550           r = DWARF2_FRAME_REG_OUT (r, 1);
2551           gcc_assert (r <= 31);
2552           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2553         }
2554       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2555         {
2556           unsigned r = (opc - DW_OP_reg0);
2557           r = DWARF2_FRAME_REG_OUT (r, 1);
2558           gcc_assert (r <= 31);
2559           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2560         }
2561       /* Output the opcode.  */
2562       fprintf (asm_out_file, "%#x", opc);
2563       output_loc_operands_raw (loc);
2564
2565       if (!loc->dw_loc_next)
2566         break;
2567       loc = loc->dw_loc_next;
2568
2569       fputc (',', asm_out_file);
2570     }
2571 }
2572
2573 /* This function builds a dwarf location descriptor sequence from a
2574    dw_cfa_location, adding the given OFFSET to the result of the
2575    expression.  */
2576
2577 struct dw_loc_descr_struct *
2578 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2579 {
2580   struct dw_loc_descr_struct *head, *tmp;
2581
2582   offset += cfa->offset;
2583
2584   if (cfa->indirect)
2585     {
2586       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2587       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2588       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2589       add_loc_descr (&head, tmp);
2590       if (offset != 0)
2591         {
2592           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2593           add_loc_descr (&head, tmp);
2594         }
2595     }
2596   else
2597     head = new_reg_loc_descr (cfa->reg, offset);
2598
2599   return head;
2600 }
2601
2602 /* This function builds a dwarf location descriptor sequence for
2603    the address at OFFSET from the CFA when stack is aligned to
2604    ALIGNMENT byte.  */
2605
2606 struct dw_loc_descr_struct *
2607 build_cfa_aligned_loc (dw_cfa_location *cfa,
2608                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2609 {
2610   struct dw_loc_descr_struct *head;
2611   unsigned int dwarf_fp
2612     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2613
2614   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2615   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2616     {
2617       head = new_reg_loc_descr (dwarf_fp, 0);
2618       add_loc_descr (&head, int_loc_descriptor (alignment));
2619       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2620       loc_descr_plus_const (&head, offset);
2621     }
2622   else
2623     head = new_reg_loc_descr (dwarf_fp, offset);
2624   return head;
2625 }
2626 \f
2627 /* And now, the support for symbolic debugging information.  */
2628
2629 /* .debug_str support.  */
2630 static int output_indirect_string (void **, void *);
2631
2632 static void dwarf2out_init (const char *);
2633 static void dwarf2out_finish (const char *);
2634 static void dwarf2out_assembly_start (void);
2635 static void dwarf2out_define (unsigned int, const char *);
2636 static void dwarf2out_undef (unsigned int, const char *);
2637 static void dwarf2out_start_source_file (unsigned, const char *);
2638 static void dwarf2out_end_source_file (unsigned);
2639 static void dwarf2out_function_decl (tree);
2640 static void dwarf2out_begin_block (unsigned, unsigned);
2641 static void dwarf2out_end_block (unsigned, unsigned);
2642 static bool dwarf2out_ignore_block (const_tree);
2643 static void dwarf2out_global_decl (tree);
2644 static void dwarf2out_type_decl (tree, int);
2645 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2646 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2647                                                  dw_die_ref);
2648 static void dwarf2out_abstract_function (tree);
2649 static void dwarf2out_var_location (rtx);
2650 static void dwarf2out_begin_function (tree);
2651 static void dwarf2out_set_name (tree, tree);
2652
2653 /* The debug hooks structure.  */
2654
2655 const struct gcc_debug_hooks dwarf2_debug_hooks =
2656 {
2657   dwarf2out_init,
2658   dwarf2out_finish,
2659   dwarf2out_assembly_start,
2660   dwarf2out_define,
2661   dwarf2out_undef,
2662   dwarf2out_start_source_file,
2663   dwarf2out_end_source_file,
2664   dwarf2out_begin_block,
2665   dwarf2out_end_block,
2666   dwarf2out_ignore_block,
2667   dwarf2out_source_line,
2668   dwarf2out_begin_prologue,
2669 #if VMS_DEBUGGING_INFO
2670   dwarf2out_vms_end_prologue,
2671   dwarf2out_vms_begin_epilogue,
2672 #else
2673   debug_nothing_int_charstar,
2674   debug_nothing_int_charstar,
2675 #endif
2676   dwarf2out_end_epilogue,
2677   dwarf2out_begin_function,
2678   debug_nothing_int,            /* end_function */
2679   dwarf2out_function_decl,      /* function_decl */
2680   dwarf2out_global_decl,
2681   dwarf2out_type_decl,          /* type_decl */
2682   dwarf2out_imported_module_or_decl,
2683   debug_nothing_tree,           /* deferred_inline_function */
2684   /* The DWARF 2 backend tries to reduce debugging bloat by not
2685      emitting the abstract description of inline functions until
2686      something tries to reference them.  */
2687   dwarf2out_abstract_function,  /* outlining_inline_function */
2688   debug_nothing_rtx,            /* label */
2689   debug_nothing_int,            /* handle_pch */
2690   dwarf2out_var_location,
2691   dwarf2out_switch_text_section,
2692   dwarf2out_set_name,
2693   1,                            /* start_end_main_source_file */
2694   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2695 };
2696 \f
2697 /* NOTE: In the comments in this file, many references are made to
2698    "Debugging Information Entries".  This term is abbreviated as `DIE'
2699    throughout the remainder of this file.  */
2700
2701 /* An internal representation of the DWARF output is built, and then
2702    walked to generate the DWARF debugging info.  The walk of the internal
2703    representation is done after the entire program has been compiled.
2704    The types below are used to describe the internal representation.  */
2705
2706 /* Whether to put type DIEs into their own section .debug_types instead
2707    of making them part of the .debug_info section.  Only supported for
2708    Dwarf V4 or higher and the user didn't disable them through
2709    -fno-debug-types-section.  It is more efficient to put them in a
2710    separate comdat sections since the linker will then be able to
2711    remove duplicates.  But not all tools support .debug_types sections
2712    yet.  */
2713
2714 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2715
2716 /* Various DIE's use offsets relative to the beginning of the
2717    .debug_info section to refer to each other.  */
2718
2719 typedef long int dw_offset;
2720
2721 /* Define typedefs here to avoid circular dependencies.  */
2722
2723 typedef struct dw_attr_struct *dw_attr_ref;
2724 typedef struct dw_line_info_struct *dw_line_info_ref;
2725 typedef struct pubname_struct *pubname_ref;
2726 typedef struct dw_ranges_struct *dw_ranges_ref;
2727 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2728 typedef struct comdat_type_struct *comdat_type_node_ref;
2729
2730 /* The entries in the line_info table more-or-less mirror the opcodes
2731    that are used in the real dwarf line table.  Arrays of these entries
2732    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2733    supported.  */
2734
2735 enum dw_line_info_opcode {
2736   /* Emit DW_LNE_set_address; the operand is the label index.  */
2737   LI_set_address,
2738
2739   /* Emit a row to the matrix with the given line.  This may be done
2740      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2741      special opcodes.  */
2742   LI_set_line,
2743
2744   /* Emit a DW_LNS_set_file.  */
2745   LI_set_file,
2746
2747   /* Emit a DW_LNS_set_column.  */
2748   LI_set_column,
2749
2750   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2751   LI_negate_stmt,
2752
2753   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2754   LI_set_prologue_end,
2755   LI_set_epilogue_begin,
2756
2757   /* Emit a DW_LNE_set_discriminator.  */
2758   LI_set_discriminator
2759 };
2760
2761 typedef struct GTY(()) dw_line_info_struct {
2762   enum dw_line_info_opcode opcode;
2763   unsigned int val;
2764 } dw_line_info_entry;
2765
2766 DEF_VEC_O(dw_line_info_entry);
2767 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2768
2769 typedef struct GTY(()) dw_line_info_table_struct {
2770   /* The label that marks the end of this section.  */
2771   const char *end_label;
2772
2773   /* The values for the last row of the matrix, as collected in the table.
2774      These are used to minimize the changes to the next row.  */
2775   unsigned int file_num;
2776   unsigned int line_num;
2777   unsigned int column_num;
2778   int discrim_num;
2779   bool is_stmt;
2780   bool in_use;
2781
2782   VEC(dw_line_info_entry, gc) *entries;
2783 } dw_line_info_table;
2784
2785 typedef dw_line_info_table *dw_line_info_table_p;
2786
2787 DEF_VEC_P(dw_line_info_table_p);
2788 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2789
2790 /* Each DIE attribute has a field specifying the attribute kind,
2791    a link to the next attribute in the chain, and an attribute value.
2792    Attributes are typically linked below the DIE they modify.  */
2793
2794 typedef struct GTY(()) dw_attr_struct {
2795   enum dwarf_attribute dw_attr;
2796   dw_val_node dw_attr_val;
2797 }
2798 dw_attr_node;
2799
2800 DEF_VEC_O(dw_attr_node);
2801 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2802
2803 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2804    The children of each node form a circular list linked by
2805    die_sib.  die_child points to the node *before* the "first" child node.  */
2806
2807 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2808   union die_symbol_or_type_node
2809     {
2810       char * GTY ((tag ("0"))) die_symbol;
2811       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2812     }
2813   GTY ((desc ("use_debug_types"))) die_id;
2814   VEC(dw_attr_node,gc) * die_attr;
2815   dw_die_ref die_parent;
2816   dw_die_ref die_child;
2817   dw_die_ref die_sib;
2818   dw_die_ref die_definition; /* ref from a specification to its definition */
2819   dw_offset die_offset;
2820   unsigned long die_abbrev;
2821   int die_mark;
2822   /* Die is used and must not be pruned as unused.  */
2823   int die_perennial_p;
2824   unsigned int decl_id;
2825   enum dwarf_tag die_tag;
2826 }
2827 die_node;
2828
2829 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2830 #define FOR_EACH_CHILD(die, c, expr) do {       \
2831   c = die->die_child;                           \
2832   if (c) do {                                   \
2833     c = c->die_sib;                             \
2834     expr;                                       \
2835   } while (c != die->die_child);                \
2836 } while (0)
2837
2838 /* The pubname structure */
2839
2840 typedef struct GTY(()) pubname_struct {
2841   dw_die_ref die;
2842   const char *name;
2843 }
2844 pubname_entry;
2845
2846 DEF_VEC_O(pubname_entry);
2847 DEF_VEC_ALLOC_O(pubname_entry, gc);
2848
2849 struct GTY(()) dw_ranges_struct {
2850   /* If this is positive, it's a block number, otherwise it's a
2851      bitwise-negated index into dw_ranges_by_label.  */
2852   int num;
2853 };
2854
2855 /* A structure to hold a macinfo entry.  */
2856
2857 typedef struct GTY(()) macinfo_struct {
2858   unsigned char code;
2859   unsigned HOST_WIDE_INT lineno;
2860   const char *info;
2861 }
2862 macinfo_entry;
2863
2864 DEF_VEC_O(macinfo_entry);
2865 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2866
2867 struct GTY(()) dw_ranges_by_label_struct {
2868   const char *begin;
2869   const char *end;
2870 };
2871
2872 /* The comdat type node structure.  */
2873 typedef struct GTY(()) comdat_type_struct
2874 {
2875   dw_die_ref root_die;
2876   dw_die_ref type_die;
2877   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2878   struct comdat_type_struct *next;
2879 }
2880 comdat_type_node;
2881
2882 /* The limbo die list structure.  */
2883 typedef struct GTY(()) limbo_die_struct {
2884   dw_die_ref die;
2885   tree created_for;
2886   struct limbo_die_struct *next;
2887 }
2888 limbo_die_node;
2889
2890 typedef struct skeleton_chain_struct
2891 {
2892   dw_die_ref old_die;
2893   dw_die_ref new_die;
2894   struct skeleton_chain_struct *parent;
2895 }
2896 skeleton_chain_node;
2897
2898 /* Define a macro which returns nonzero for a TYPE_DECL which was
2899    implicitly generated for a type.
2900
2901    Note that, unlike the C front-end (which generates a NULL named
2902    TYPE_DECL node for each complete tagged type, each array type,
2903    and each function type node created) the C++ front-end generates
2904    a _named_ TYPE_DECL node for each tagged type node created.
2905    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2906    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2907    front-end, but for each type, tagged or not.  */
2908
2909 #define TYPE_DECL_IS_STUB(decl)                         \
2910   (DECL_NAME (decl) == NULL_TREE                        \
2911    || (DECL_ARTIFICIAL (decl)                           \
2912        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2913            /* This is necessary for stub decls that     \
2914               appear in nested inline functions.  */    \
2915            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2916                && (decl_ultimate_origin (decl)          \
2917                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2918
2919 /* Information concerning the compilation unit's programming
2920    language, and compiler version.  */
2921
2922 /* Fixed size portion of the DWARF compilation unit header.  */
2923 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2924   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2925
2926 /* Fixed size portion of the DWARF comdat type unit header.  */
2927 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2928   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2929    + DWARF_OFFSET_SIZE)
2930
2931 /* Fixed size portion of public names info.  */
2932 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2933
2934 /* Fixed size portion of the address range info.  */
2935 #define DWARF_ARANGES_HEADER_SIZE                                       \
2936   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2937                 DWARF2_ADDR_SIZE * 2)                                   \
2938    - DWARF_INITIAL_LENGTH_SIZE)
2939
2940 /* Size of padding portion in the address range info.  It must be
2941    aligned to twice the pointer size.  */
2942 #define DWARF_ARANGES_PAD_SIZE \
2943   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2944                 DWARF2_ADDR_SIZE * 2)                              \
2945    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2946
2947 /* Use assembler line directives if available.  */
2948 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2949 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2950 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2951 #else
2952 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2953 #endif
2954 #endif
2955
2956 /* Minimum line offset in a special line info. opcode.
2957    This value was chosen to give a reasonable range of values.  */
2958 #define DWARF_LINE_BASE  -10
2959
2960 /* First special line opcode - leave room for the standard opcodes.  */
2961 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2962
2963 /* Range of line offsets in a special line info. opcode.  */
2964 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2965
2966 /* Flag that indicates the initial value of the is_stmt_start flag.
2967    In the present implementation, we do not mark any lines as
2968    the beginning of a source statement, because that information
2969    is not made available by the GCC front-end.  */
2970 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2971
2972 /* Maximum number of operations per instruction bundle.  */
2973 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2974 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2975 #endif
2976
2977 /* This location is used by calc_die_sizes() to keep track
2978    the offset of each DIE within the .debug_info section.  */
2979 static unsigned long next_die_offset;
2980
2981 /* Record the root of the DIE's built for the current compilation unit.  */
2982 static GTY(()) dw_die_ref single_comp_unit_die;
2983
2984 /* A list of type DIEs that have been separated into comdat sections.  */
2985 static GTY(()) comdat_type_node *comdat_type_list;
2986
2987 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2988 static GTY(()) limbo_die_node *limbo_die_list;
2989
2990 /* A list of DIEs for which we may have to generate
2991    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2992 static GTY(()) limbo_die_node *deferred_asm_name;
2993
2994 /* Filenames referenced by this compilation unit.  */
2995 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2996
2997 /* A hash table of references to DIE's that describe declarations.
2998    The key is a DECL_UID() which is a unique number identifying each decl.  */
2999 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3000
3001 /* A hash table of references to DIE's that describe COMMON blocks.
3002    The key is DECL_UID() ^ die_parent.  */
3003 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3004
3005 typedef struct GTY(()) die_arg_entry_struct {
3006     dw_die_ref die;
3007     tree arg;
3008 } die_arg_entry;
3009
3010 DEF_VEC_O(die_arg_entry);
3011 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3012
3013 /* Node of the variable location list.  */
3014 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3015   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3016      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3017      in mode of the EXPR_LIST node and first EXPR_LIST operand
3018      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3019      location or NULL for padding.  For larger bitsizes,
3020      mode is 0 and first operand is a CONCAT with bitsize
3021      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3022      NULL as second operand.  */
3023   rtx GTY (()) loc;
3024   const char * GTY (()) label;
3025   struct var_loc_node * GTY (()) next;
3026 };
3027
3028 /* Variable location list.  */
3029 struct GTY (()) var_loc_list_def {
3030   struct var_loc_node * GTY (()) first;
3031
3032   /* Pointer to the last but one or last element of the
3033      chained list.  If the list is empty, both first and
3034      last are NULL, if the list contains just one node
3035      or the last node certainly is not redundant, it points
3036      to the last node, otherwise points to the last but one.
3037      Do not mark it for GC because it is marked through the chain.  */
3038   struct var_loc_node * GTY ((skip ("%h"))) last;
3039
3040   /* Pointer to the last element before section switch,
3041      if NULL, either sections weren't switched or first
3042      is after section switch.  */
3043   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3044
3045   /* DECL_UID of the variable decl.  */
3046   unsigned int decl_id;
3047 };
3048 typedef struct var_loc_list_def var_loc_list;
3049
3050 /* Call argument location list.  */
3051 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3052   rtx GTY (()) call_arg_loc_note;
3053   const char * GTY (()) label;
3054   tree GTY (()) block;
3055   bool tail_call_p;
3056   rtx GTY (()) symbol_ref;
3057   struct call_arg_loc_node * GTY (()) next;
3058 };
3059
3060
3061 /* Table of decl location linked lists.  */
3062 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3063
3064 /* Head and tail of call_arg_loc chain.  */
3065 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3066 static struct call_arg_loc_node *call_arg_loc_last;
3067
3068 /* Number of call sites in the current function.  */
3069 static int call_site_count = -1;
3070 /* Number of tail call sites in the current function.  */
3071 static int tail_call_site_count = -1;
3072
3073 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3074    DIEs.  */
3075 static VEC (dw_die_ref, heap) *block_map;
3076
3077 /* A cached location list.  */
3078 struct GTY (()) cached_dw_loc_list_def {
3079   /* The DECL_UID of the decl that this entry describes.  */
3080   unsigned int decl_id;
3081
3082   /* The cached location list.  */
3083   dw_loc_list_ref loc_list;
3084 };
3085 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3086
3087 /* Table of cached location lists.  */
3088 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3089
3090 /* A pointer to the base of a list of references to DIE's that
3091    are uniquely identified by their tag, presence/absence of
3092    children DIE's, and list of attribute/value pairs.  */
3093 static GTY((length ("abbrev_die_table_allocated")))
3094   dw_die_ref *abbrev_die_table;
3095
3096 /* Number of elements currently allocated for abbrev_die_table.  */
3097 static GTY(()) unsigned abbrev_die_table_allocated;
3098
3099 /* Number of elements in type_die_table currently in use.  */
3100 static GTY(()) unsigned abbrev_die_table_in_use;
3101
3102 /* Size (in elements) of increments by which we may expand the
3103    abbrev_die_table.  */
3104 #define ABBREV_DIE_TABLE_INCREMENT 256
3105
3106 /* A global counter for generating labels for line number data.  */
3107 static unsigned int line_info_label_num;
3108
3109 /* The current table to which we should emit line number information
3110    for the current function.  This will be set up at the beginning of
3111    assembly for the function.  */
3112 static dw_line_info_table *cur_line_info_table;
3113
3114 /* The two default tables of line number info.  */
3115 static GTY(()) dw_line_info_table *text_section_line_info;
3116 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3117
3118 /* The set of all non-default tables of line number info.  */
3119 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3120
3121 /* A flag to tell pubnames/types export if there is an info section to
3122    refer to.  */
3123 static bool info_section_emitted;
3124
3125 /* A pointer to the base of a table that contains a list of publicly
3126    accessible names.  */
3127 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3128
3129 /* A pointer to the base of a table that contains a list of publicly
3130    accessible types.  */
3131 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3132
3133 /* A pointer to the base of a table that contains a list of macro
3134    defines/undefines (and file start/end markers).  */
3135 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3136
3137 /* Array of dies for which we should generate .debug_ranges info.  */
3138 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3139
3140 /* Number of elements currently allocated for ranges_table.  */
3141 static GTY(()) unsigned ranges_table_allocated;
3142
3143 /* Number of elements in ranges_table currently in use.  */
3144 static GTY(()) unsigned ranges_table_in_use;
3145
3146 /* Array of pairs of labels referenced in ranges_table.  */
3147 static GTY ((length ("ranges_by_label_allocated")))
3148      dw_ranges_by_label_ref ranges_by_label;
3149
3150 /* Number of elements currently allocated for ranges_by_label.  */
3151 static GTY(()) unsigned ranges_by_label_allocated;
3152
3153 /* Number of elements in ranges_by_label currently in use.  */
3154 static GTY(()) unsigned ranges_by_label_in_use;
3155
3156 /* Size (in elements) of increments by which we may expand the
3157    ranges_table.  */
3158 #define RANGES_TABLE_INCREMENT 64
3159
3160 /* Whether we have location lists that need outputting */
3161 static GTY(()) bool have_location_lists;
3162
3163 /* Unique label counter.  */
3164 static GTY(()) unsigned int loclabel_num;
3165
3166 /* Unique label counter for point-of-call tables.  */
3167 static GTY(()) unsigned int poc_label_num;
3168
3169 /* Record whether the function being analyzed contains inlined functions.  */
3170 static int current_function_has_inlines;
3171
3172 /* The last file entry emitted by maybe_emit_file().  */
3173 static GTY(()) struct dwarf_file_data * last_emitted_file;
3174
3175 /* Number of internal labels generated by gen_internal_sym().  */
3176 static GTY(()) int label_num;
3177
3178 /* Cached result of previous call to lookup_filename.  */
3179 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3180
3181 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3182
3183 /* Instances of generic types for which we need to generate debug
3184    info that describe their generic parameters and arguments. That
3185    generation needs to happen once all types are properly laid out so
3186    we do it at the end of compilation.  */
3187 static GTY(()) VEC(tree,gc) *generic_type_instances;
3188
3189 /* Offset from the "steady-state frame pointer" to the frame base,
3190    within the current function.  */
3191 static HOST_WIDE_INT frame_pointer_fb_offset;
3192 static bool frame_pointer_fb_offset_valid;
3193
3194 static VEC (dw_die_ref, heap) *base_types;
3195
3196 /* Forward declarations for functions defined in this file.  */
3197
3198 static int is_pseudo_reg (const_rtx);
3199 static tree type_main_variant (tree);
3200 static int is_tagged_type (const_tree);
3201 static const char *dwarf_tag_name (unsigned);
3202 static const char *dwarf_attr_name (unsigned);
3203 static const char *dwarf_form_name (unsigned);
3204 static tree decl_ultimate_origin (const_tree);
3205 static tree decl_class_context (tree);
3206 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3207 static inline enum dw_val_class AT_class (dw_attr_ref);
3208 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3209 static inline unsigned AT_flag (dw_attr_ref);
3210 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3211 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3212 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3213 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3214 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3215                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3216 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3217                                unsigned int, unsigned char *);
3218 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3219 static hashval_t debug_str_do_hash (const void *);
3220 static int debug_str_eq (const void *, const void *);
3221 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3222 static inline const char *AT_string (dw_attr_ref);
3223 static enum dwarf_form AT_string_form (dw_attr_ref);
3224 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3225 static void add_AT_specification (dw_die_ref, dw_die_ref);
3226 static inline dw_die_ref AT_ref (dw_attr_ref);
3227 static inline int AT_ref_external (dw_attr_ref);
3228 static inline void set_AT_ref_external (dw_attr_ref, int);
3229 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3230 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3231 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3232 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3233                              dw_loc_list_ref);
3234 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3235 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3236 static inline rtx AT_addr (dw_attr_ref);
3237 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3238 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3239 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3240 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3241                            unsigned HOST_WIDE_INT);
3242 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3243                                unsigned long);
3244 static inline const char *AT_lbl (dw_attr_ref);
3245 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3246 static const char *get_AT_low_pc (dw_die_ref);
3247 static const char *get_AT_hi_pc (dw_die_ref);
3248 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3249 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3250 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3251 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3252 static bool is_cxx (void);
3253 static bool is_fortran (void);
3254 static bool is_ada (void);
3255 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3256 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3257 static void add_child_die (dw_die_ref, dw_die_ref);
3258 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3259 static dw_die_ref lookup_type_die (tree);
3260 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3261 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3262 static void equate_type_number_to_die (tree, dw_die_ref);
3263 static hashval_t decl_die_table_hash (const void *);
3264 static int decl_die_table_eq (const void *, const void *);
3265 static dw_die_ref lookup_decl_die (tree);
3266 static hashval_t common_block_die_table_hash (const void *);
3267 static int common_block_die_table_eq (const void *, const void *);
3268 static hashval_t decl_loc_table_hash (const void *);
3269 static int decl_loc_table_eq (const void *, const void *);
3270 static var_loc_list *lookup_decl_loc (const_tree);
3271 static void equate_decl_number_to_die (tree, dw_die_ref);
3272 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3273 static void print_spaces (FILE *);
3274 static void print_die (dw_die_ref, FILE *);
3275 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3276 static dw_die_ref pop_compile_unit (dw_die_ref);
3277 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3278 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3279 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3280 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3281 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3282 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3283 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3284                                    struct md5_ctx *, int *);
3285 struct checksum_attributes;
3286 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3287 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3288 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3289 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3290 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3291 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3292 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3293 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3294 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3295 static void compute_section_prefix (dw_die_ref);
3296 static int is_type_die (dw_die_ref);
3297 static int is_comdat_die (dw_die_ref);
3298 static int is_symbol_die (dw_die_ref);
3299 static void assign_symbol_names (dw_die_ref);
3300 static void break_out_includes (dw_die_ref);
3301 static int is_declaration_die (dw_die_ref);
3302 static int should_move_die_to_comdat (dw_die_ref);
3303 static dw_die_ref clone_as_declaration (dw_die_ref);
3304 static dw_die_ref clone_die (dw_die_ref);
3305 static dw_die_ref clone_tree (dw_die_ref);
3306 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3307 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3308 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3309 static dw_die_ref generate_skeleton (dw_die_ref);
3310 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3311                                                          dw_die_ref,
3312                                                          dw_die_ref);
3313 static void break_out_comdat_types (dw_die_ref);
3314 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3315 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3316 static void copy_decls_for_unworthy_types (dw_die_ref);
3317
3318 static hashval_t htab_cu_hash (const void *);
3319 static int htab_cu_eq (const void *, const void *);
3320 static void htab_cu_del (void *);
3321 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3322 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3323 static void add_sibling_attributes (dw_die_ref);
3324 static void build_abbrev_table (dw_die_ref);
3325 static void output_location_lists (dw_die_ref);
3326 static int constant_size (unsigned HOST_WIDE_INT);
3327 static unsigned long size_of_die (dw_die_ref);
3328 static void calc_die_sizes (dw_die_ref);
3329 static void calc_base_type_die_sizes (void);
3330 static void mark_dies (dw_die_ref);
3331 static void unmark_dies (dw_die_ref);
3332 static void unmark_all_dies (dw_die_ref);
3333 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3334 static unsigned long size_of_aranges (void);
3335 static enum dwarf_form value_format (dw_attr_ref);
3336 static void output_value_format (dw_attr_ref);
3337 static void output_abbrev_section (void);
3338 static void output_die_symbol (dw_die_ref);
3339 static void output_die (dw_die_ref);
3340 static void output_compilation_unit_header (void);
3341 static void output_comp_unit (dw_die_ref, int);
3342 static void output_comdat_type_unit (comdat_type_node *);
3343 static const char *dwarf2_name (tree, int);
3344 static void add_pubname (tree, dw_die_ref);
3345 static void add_pubname_string (const char *, dw_die_ref);
3346 static void add_pubtype (tree, dw_die_ref);
3347 static void output_pubnames (VEC (pubname_entry,gc) *);
3348 static void output_aranges (unsigned long);
3349 static unsigned int add_ranges_num (int);
3350 static unsigned int add_ranges (const_tree);
3351 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3352                                   bool *);
3353 static void output_ranges (void);
3354 static dw_line_info_table *new_line_info_table (void);
3355 static void output_line_info (void);
3356 static void output_file_names (void);
3357 static dw_die_ref base_type_die (tree);
3358 static int is_base_type (tree);
3359 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3360 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3361 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3362 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3363 static int type_is_enum (const_tree);
3364 static unsigned int dbx_reg_number (const_rtx);
3365 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3366 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3367 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3368                                                 enum var_init_status);
3369 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3370                                                      enum var_init_status);
3371 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3372                                          enum var_init_status);
3373 static int is_based_loc (const_rtx);
3374 static int resolve_one_addr (rtx *, void *);
3375 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3376                                                enum var_init_status);
3377 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3378                                         enum var_init_status);
3379 static dw_loc_list_ref loc_list_from_tree (tree, int);
3380 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3381 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3382 static tree field_type (const_tree);
3383 static unsigned int simple_type_align_in_bits (const_tree);
3384 static unsigned int simple_decl_align_in_bits (const_tree);
3385 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3386 static HOST_WIDE_INT field_byte_offset (const_tree);
3387 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3388                                          dw_loc_list_ref);
3389 static void add_data_member_location_attribute (dw_die_ref, tree);
3390 static bool add_const_value_attribute (dw_die_ref, rtx);
3391 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3392 static void insert_double (double_int, unsigned char *);
3393 static void insert_float (const_rtx, unsigned char *);
3394 static rtx rtl_for_decl_location (tree);
3395 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3396                                                    enum dwarf_attribute);
3397 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3398 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3399 static void add_name_attribute (dw_die_ref, const char *);
3400 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3401 static void add_comp_dir_attribute (dw_die_ref);
3402 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3403 static void add_subscript_info (dw_die_ref, tree, bool);
3404 static void add_byte_size_attribute (dw_die_ref, tree);
3405 static void add_bit_offset_attribute (dw_die_ref, tree);
3406 static void add_bit_size_attribute (dw_die_ref, tree);
3407 static void add_prototyped_attribute (dw_die_ref, tree);
3408 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3409 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3410 static void add_src_coords_attributes (dw_die_ref, tree);
3411 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3412 static void push_decl_scope (tree);
3413 static void pop_decl_scope (void);
3414 static dw_die_ref scope_die_for (tree, dw_die_ref);
3415 static inline int local_scope_p (dw_die_ref);
3416 static inline int class_scope_p (dw_die_ref);
3417 static inline int class_or_namespace_scope_p (dw_die_ref);
3418 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3419 static void add_calling_convention_attribute (dw_die_ref, tree);
3420 static const char *type_tag (const_tree);
3421 static tree member_declared_type (const_tree);
3422 #if 0
3423 static const char *decl_start_label (tree);
3424 #endif
3425 static void gen_array_type_die (tree, dw_die_ref);
3426 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3427 #if 0
3428 static void gen_entry_point_die (tree, dw_die_ref);
3429 #endif
3430 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3431 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3432 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3433 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3434 static void gen_formal_types_die (tree, dw_die_ref);
3435 static void gen_subprogram_die (tree, dw_die_ref);
3436 static void gen_variable_die (tree, tree, dw_die_ref);
3437 static void gen_const_die (tree, dw_die_ref);
3438 static void gen_label_die (tree, dw_die_ref);
3439 static void gen_lexical_block_die (tree, dw_die_ref, int);
3440 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3441 static void gen_field_die (tree, dw_die_ref);
3442 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3443 static dw_die_ref gen_compile_unit_die (const char *);
3444 static void gen_inheritance_die (tree, tree, dw_die_ref);
3445 static void gen_member_die (tree, dw_die_ref);
3446 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3447                                                 enum debug_info_usage);
3448 static void gen_subroutine_type_die (tree, dw_die_ref);
3449 static void gen_typedef_die (tree, dw_die_ref);
3450 static void gen_type_die (tree, dw_die_ref);
3451 static void gen_block_die (tree, dw_die_ref, int);
3452 static void decls_for_scope (tree, dw_die_ref, int);
3453 static inline int is_redundant_typedef (const_tree);
3454 static bool is_naming_typedef_decl (const_tree);
3455 static inline dw_die_ref get_context_die (tree);
3456 static void gen_namespace_die (tree, dw_die_ref);
3457 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3458 static dw_die_ref force_decl_die (tree);
3459 static dw_die_ref force_type_die (tree);
3460 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3461 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3462 static struct dwarf_file_data * lookup_filename (const char *);
3463 static void retry_incomplete_types (void);
3464 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3465 static void gen_generic_params_dies (tree);
3466 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3467 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3468 static void splice_child_die (dw_die_ref, dw_die_ref);
3469 static int file_info_cmp (const void *, const void *);
3470 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3471                                      const char *, const char *);
3472 static void output_loc_list (dw_loc_list_ref);
3473 static char *gen_internal_sym (const char *);
3474
3475 static void prune_unmark_dies (dw_die_ref);
3476 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3477 static void prune_unused_types_mark (dw_die_ref, int);
3478 static void prune_unused_types_walk (dw_die_ref);
3479 static void prune_unused_types_walk_attribs (dw_die_ref);
3480 static void prune_unused_types_prune (dw_die_ref);
3481 static void prune_unused_types (void);
3482 static int maybe_emit_file (struct dwarf_file_data *fd);
3483 static inline const char *AT_vms_delta1 (dw_attr_ref);
3484 static inline const char *AT_vms_delta2 (dw_attr_ref);
3485 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3486                                      const char *, const char *);
3487 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3488 static void gen_remaining_tmpl_value_param_die_attribute (void);
3489 static bool generic_type_p (tree);
3490 static void schedule_generic_params_dies_gen (tree t);
3491 static void gen_scheduled_generic_parms_dies (void);
3492
3493 /* Section names used to hold DWARF debugging information.  */
3494 #ifndef DEBUG_INFO_SECTION
3495 #define DEBUG_INFO_SECTION      ".debug_info"
3496 #endif
3497 #ifndef DEBUG_ABBREV_SECTION
3498 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3499 #endif
3500 #ifndef DEBUG_ARANGES_SECTION
3501 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3502 #endif
3503 #ifndef DEBUG_MACINFO_SECTION
3504 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3505 #endif
3506 #ifndef DEBUG_MACRO_SECTION
3507 #define DEBUG_MACRO_SECTION     ".debug_macro"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION
3510 #define DEBUG_LINE_SECTION      ".debug_line"
3511 #endif
3512 #ifndef DEBUG_LOC_SECTION
3513 #define DEBUG_LOC_SECTION       ".debug_loc"
3514 #endif
3515 #ifndef DEBUG_PUBNAMES_SECTION
3516 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3517 #endif
3518 #ifndef DEBUG_PUBTYPES_SECTION
3519 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3520 #endif
3521 #ifndef DEBUG_STR_SECTION
3522 #define DEBUG_STR_SECTION       ".debug_str"
3523 #endif
3524 #ifndef DEBUG_RANGES_SECTION
3525 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3526 #endif
3527
3528 /* Standard ELF section names for compiled code and data.  */
3529 #ifndef TEXT_SECTION_NAME
3530 #define TEXT_SECTION_NAME       ".text"
3531 #endif
3532
3533 /* Section flags for .debug_str section.  */
3534 #define DEBUG_STR_SECTION_FLAGS \
3535   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3536    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3537    : SECTION_DEBUG)
3538
3539 /* Labels we insert at beginning sections we can reference instead of
3540    the section names themselves.  */
3541
3542 #ifndef TEXT_SECTION_LABEL
3543 #define TEXT_SECTION_LABEL              "Ltext"
3544 #endif
3545 #ifndef COLD_TEXT_SECTION_LABEL
3546 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3547 #endif
3548 #ifndef DEBUG_LINE_SECTION_LABEL
3549 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3550 #endif
3551 #ifndef DEBUG_INFO_SECTION_LABEL
3552 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3553 #endif
3554 #ifndef DEBUG_ABBREV_SECTION_LABEL
3555 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3556 #endif
3557 #ifndef DEBUG_LOC_SECTION_LABEL
3558 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3559 #endif
3560 #ifndef DEBUG_RANGES_SECTION_LABEL
3561 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3562 #endif
3563 #ifndef DEBUG_MACINFO_SECTION_LABEL
3564 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3565 #endif
3566 #ifndef DEBUG_MACRO_SECTION_LABEL
3567 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3568 #endif
3569
3570
3571 /* Definitions of defaults for formats and names of various special
3572    (artificial) labels which may be generated within this file (when the -g
3573    options is used and DWARF2_DEBUGGING_INFO is in effect.
3574    If necessary, these may be overridden from within the tm.h file, but
3575    typically, overriding these defaults is unnecessary.  */
3576
3577 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3585 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3586 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3587
3588 #ifndef TEXT_END_LABEL
3589 #define TEXT_END_LABEL          "Letext"
3590 #endif
3591 #ifndef COLD_END_LABEL
3592 #define COLD_END_LABEL          "Letext_cold"
3593 #endif
3594 #ifndef BLOCK_BEGIN_LABEL
3595 #define BLOCK_BEGIN_LABEL       "LBB"
3596 #endif
3597 #ifndef BLOCK_END_LABEL
3598 #define BLOCK_END_LABEL         "LBE"
3599 #endif
3600 #ifndef LINE_CODE_LABEL
3601 #define LINE_CODE_LABEL         "LM"
3602 #endif
3603
3604 \f
3605 /* Return the root of the DIE's built for the current compilation unit.  */
3606 static dw_die_ref
3607 comp_unit_die (void)
3608 {
3609   if (!single_comp_unit_die)
3610     single_comp_unit_die = gen_compile_unit_die (NULL);
3611   return single_comp_unit_die;
3612 }
3613
3614 /* We allow a language front-end to designate a function that is to be
3615    called to "demangle" any name before it is put into a DIE.  */
3616
3617 static const char *(*demangle_name_func) (const char *);
3618
3619 void
3620 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3621 {
3622   demangle_name_func = func;
3623 }
3624
3625 /* Test if rtl node points to a pseudo register.  */
3626
3627 static inline int
3628 is_pseudo_reg (const_rtx rtl)
3629 {
3630   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3631           || (GET_CODE (rtl) == SUBREG
3632               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3633 }
3634
3635 /* Return a reference to a type, with its const and volatile qualifiers
3636    removed.  */
3637
3638 static inline tree
3639 type_main_variant (tree type)
3640 {
3641   type = TYPE_MAIN_VARIANT (type);
3642
3643   /* ??? There really should be only one main variant among any group of
3644      variants of a given type (and all of the MAIN_VARIANT values for all
3645      members of the group should point to that one type) but sometimes the C
3646      front-end messes this up for array types, so we work around that bug
3647      here.  */
3648   if (TREE_CODE (type) == ARRAY_TYPE)
3649     while (type != TYPE_MAIN_VARIANT (type))
3650       type = TYPE_MAIN_VARIANT (type);
3651
3652   return type;
3653 }
3654
3655 /* Return nonzero if the given type node represents a tagged type.  */
3656
3657 static inline int
3658 is_tagged_type (const_tree type)
3659 {
3660   enum tree_code code = TREE_CODE (type);
3661
3662   return (code == RECORD_TYPE || code == UNION_TYPE
3663           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3664 }
3665
3666 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3667
3668 static void
3669 get_ref_die_offset_label (char *label, dw_die_ref ref)
3670 {
3671   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3672 }
3673
3674 /* Return die_offset of a DIE reference to a base type.  */
3675
3676 static unsigned long int
3677 get_base_type_offset (dw_die_ref ref)
3678 {
3679   if (ref->die_offset)
3680     return ref->die_offset;
3681   if (comp_unit_die ()->die_abbrev)
3682     {
3683       calc_base_type_die_sizes ();
3684       gcc_assert (ref->die_offset);
3685     }
3686   return ref->die_offset;
3687 }
3688
3689 /* Return die_offset of a DIE reference other than base type.  */
3690
3691 static unsigned long int
3692 get_ref_die_offset (dw_die_ref ref)
3693 {
3694   gcc_assert (ref->die_offset);
3695   return ref->die_offset;
3696 }
3697
3698 /* Convert a DIE tag into its string name.  */
3699
3700 static const char *
3701 dwarf_tag_name (unsigned int tag)
3702 {
3703   switch (tag)
3704     {
3705     case DW_TAG_padding:
3706       return "DW_TAG_padding";
3707     case DW_TAG_array_type:
3708       return "DW_TAG_array_type";
3709     case DW_TAG_class_type:
3710       return "DW_TAG_class_type";
3711     case DW_TAG_entry_point:
3712       return "DW_TAG_entry_point";
3713     case DW_TAG_enumeration_type:
3714       return "DW_TAG_enumeration_type";
3715     case DW_TAG_formal_parameter:
3716       return "DW_TAG_formal_parameter";
3717     case DW_TAG_imported_declaration:
3718       return "DW_TAG_imported_declaration";
3719     case DW_TAG_label:
3720       return "DW_TAG_label";
3721     case DW_TAG_lexical_block:
3722       return "DW_TAG_lexical_block";
3723     case DW_TAG_member:
3724       return "DW_TAG_member";
3725     case DW_TAG_pointer_type:
3726       return "DW_TAG_pointer_type";
3727     case DW_TAG_reference_type:
3728       return "DW_TAG_reference_type";
3729     case DW_TAG_compile_unit:
3730       return "DW_TAG_compile_unit";
3731     case DW_TAG_string_type:
3732       return "DW_TAG_string_type";
3733     case DW_TAG_structure_type:
3734       return "DW_TAG_structure_type";
3735     case DW_TAG_subroutine_type:
3736       return "DW_TAG_subroutine_type";
3737     case DW_TAG_typedef:
3738       return "DW_TAG_typedef";
3739     case DW_TAG_union_type:
3740       return "DW_TAG_union_type";
3741     case DW_TAG_unspecified_parameters:
3742       return "DW_TAG_unspecified_parameters";
3743     case DW_TAG_variant:
3744       return "DW_TAG_variant";
3745     case DW_TAG_common_block:
3746       return "DW_TAG_common_block";
3747     case DW_TAG_common_inclusion:
3748       return "DW_TAG_common_inclusion";
3749     case DW_TAG_inheritance:
3750       return "DW_TAG_inheritance";
3751     case DW_TAG_inlined_subroutine:
3752       return "DW_TAG_inlined_subroutine";
3753     case DW_TAG_module:
3754       return "DW_TAG_module";
3755     case DW_TAG_ptr_to_member_type:
3756       return "DW_TAG_ptr_to_member_type";
3757     case DW_TAG_set_type:
3758       return "DW_TAG_set_type";
3759     case DW_TAG_subrange_type:
3760       return "DW_TAG_subrange_type";
3761     case DW_TAG_with_stmt:
3762       return "DW_TAG_with_stmt";
3763     case DW_TAG_access_declaration:
3764       return "DW_TAG_access_declaration";
3765     case DW_TAG_base_type:
3766       return "DW_TAG_base_type";
3767     case DW_TAG_catch_block:
3768       return "DW_TAG_catch_block";
3769     case DW_TAG_const_type:
3770       return "DW_TAG_const_type";
3771     case DW_TAG_constant:
3772       return "DW_TAG_constant";
3773     case DW_TAG_enumerator:
3774       return "DW_TAG_enumerator";
3775     case DW_TAG_file_type:
3776       return "DW_TAG_file_type";
3777     case DW_TAG_friend:
3778       return "DW_TAG_friend";
3779     case DW_TAG_namelist:
3780       return "DW_TAG_namelist";
3781     case DW_TAG_namelist_item:
3782       return "DW_TAG_namelist_item";
3783     case DW_TAG_packed_type:
3784       return "DW_TAG_packed_type";
3785     case DW_TAG_subprogram:
3786       return "DW_TAG_subprogram";
3787     case DW_TAG_template_type_param:
3788       return "DW_TAG_template_type_param";
3789     case DW_TAG_template_value_param:
3790       return "DW_TAG_template_value_param";
3791     case DW_TAG_thrown_type:
3792       return "DW_TAG_thrown_type";
3793     case DW_TAG_try_block:
3794       return "DW_TAG_try_block";
3795     case DW_TAG_variant_part:
3796       return "DW_TAG_variant_part";
3797     case DW_TAG_variable:
3798       return "DW_TAG_variable";
3799     case DW_TAG_volatile_type:
3800       return "DW_TAG_volatile_type";
3801     case DW_TAG_dwarf_procedure:
3802       return "DW_TAG_dwarf_procedure";
3803     case DW_TAG_restrict_type:
3804       return "DW_TAG_restrict_type";
3805     case DW_TAG_interface_type:
3806       return "DW_TAG_interface_type";
3807     case DW_TAG_namespace:
3808       return "DW_TAG_namespace";
3809     case DW_TAG_imported_module:
3810       return "DW_TAG_imported_module";
3811     case DW_TAG_unspecified_type:
3812       return "DW_TAG_unspecified_type";
3813     case DW_TAG_partial_unit:
3814       return "DW_TAG_partial_unit";
3815     case DW_TAG_imported_unit:
3816       return "DW_TAG_imported_unit";
3817     case DW_TAG_condition:
3818       return "DW_TAG_condition";
3819     case DW_TAG_shared_type:
3820       return "DW_TAG_shared_type";
3821     case DW_TAG_type_unit:
3822       return "DW_TAG_type_unit";
3823     case DW_TAG_rvalue_reference_type:
3824       return "DW_TAG_rvalue_reference_type";
3825     case DW_TAG_template_alias:
3826       return "DW_TAG_template_alias";
3827     case DW_TAG_GNU_template_parameter_pack:
3828       return "DW_TAG_GNU_template_parameter_pack";
3829     case DW_TAG_GNU_formal_parameter_pack:
3830       return "DW_TAG_GNU_formal_parameter_pack";
3831     case DW_TAG_MIPS_loop:
3832       return "DW_TAG_MIPS_loop";
3833     case DW_TAG_format_label:
3834       return "DW_TAG_format_label";
3835     case DW_TAG_function_template:
3836       return "DW_TAG_function_template";
3837     case DW_TAG_class_template:
3838       return "DW_TAG_class_template";
3839     case DW_TAG_GNU_BINCL:
3840       return "DW_TAG_GNU_BINCL";
3841     case DW_TAG_GNU_EINCL:
3842       return "DW_TAG_GNU_EINCL";
3843     case DW_TAG_GNU_template_template_param:
3844       return "DW_TAG_GNU_template_template_param";
3845     case DW_TAG_GNU_call_site:
3846       return "DW_TAG_GNU_call_site";
3847     case DW_TAG_GNU_call_site_parameter:
3848       return "DW_TAG_GNU_call_site_parameter";
3849     default:
3850       return "DW_TAG_<unknown>";
3851     }
3852 }
3853
3854 /* Convert a DWARF attribute code into its string name.  */
3855
3856 static const char *
3857 dwarf_attr_name (unsigned int attr)
3858 {
3859   switch (attr)
3860     {
3861     case DW_AT_sibling:
3862       return "DW_AT_sibling";
3863     case DW_AT_location:
3864       return "DW_AT_location";
3865     case DW_AT_name:
3866       return "DW_AT_name";
3867     case DW_AT_ordering:
3868       return "DW_AT_ordering";
3869     case DW_AT_subscr_data:
3870       return "DW_AT_subscr_data";
3871     case DW_AT_byte_size:
3872       return "DW_AT_byte_size";
3873     case DW_AT_bit_offset:
3874       return "DW_AT_bit_offset";
3875     case DW_AT_bit_size:
3876       return "DW_AT_bit_size";
3877     case DW_AT_element_list:
3878       return "DW_AT_element_list";
3879     case DW_AT_stmt_list:
3880       return "DW_AT_stmt_list";
3881     case DW_AT_low_pc:
3882       return "DW_AT_low_pc";
3883     case DW_AT_high_pc:
3884       return "DW_AT_high_pc";
3885     case DW_AT_language:
3886       return "DW_AT_language";
3887     case DW_AT_member:
3888       return "DW_AT_member";
3889     case DW_AT_discr:
3890       return "DW_AT_discr";
3891     case DW_AT_discr_value:
3892       return "DW_AT_discr_value";
3893     case DW_AT_visibility:
3894       return "DW_AT_visibility";
3895     case DW_AT_import:
3896       return "DW_AT_import";
3897     case DW_AT_string_length:
3898       return "DW_AT_string_length";
3899     case DW_AT_common_reference:
3900       return "DW_AT_common_reference";
3901     case DW_AT_comp_dir:
3902       return "DW_AT_comp_dir";
3903     case DW_AT_const_value:
3904       return "DW_AT_const_value";
3905     case DW_AT_containing_type:
3906       return "DW_AT_containing_type";
3907     case DW_AT_default_value:
3908       return "DW_AT_default_value";
3909     case DW_AT_inline:
3910       return "DW_AT_inline";
3911     case DW_AT_is_optional:
3912       return "DW_AT_is_optional";
3913     case DW_AT_lower_bound:
3914       return "DW_AT_lower_bound";
3915     case DW_AT_producer:
3916       return "DW_AT_producer";
3917     case DW_AT_prototyped:
3918       return "DW_AT_prototyped";
3919     case DW_AT_return_addr:
3920       return "DW_AT_return_addr";
3921     case DW_AT_start_scope:
3922       return "DW_AT_start_scope";
3923     case DW_AT_bit_stride:
3924       return "DW_AT_bit_stride";
3925     case DW_AT_upper_bound:
3926       return "DW_AT_upper_bound";
3927     case DW_AT_abstract_origin:
3928       return "DW_AT_abstract_origin";
3929     case DW_AT_accessibility:
3930       return "DW_AT_accessibility";
3931     case DW_AT_address_class:
3932       return "DW_AT_address_class";
3933     case DW_AT_artificial:
3934       return "DW_AT_artificial";
3935     case DW_AT_base_types:
3936       return "DW_AT_base_types";
3937     case DW_AT_calling_convention:
3938       return "DW_AT_calling_convention";
3939     case DW_AT_count:
3940       return "DW_AT_count";
3941     case DW_AT_data_member_location:
3942       return "DW_AT_data_member_location";
3943     case DW_AT_decl_column:
3944       return "DW_AT_decl_column";
3945     case DW_AT_decl_file:
3946       return "DW_AT_decl_file";
3947     case DW_AT_decl_line:
3948       return "DW_AT_decl_line";
3949     case DW_AT_declaration:
3950       return "DW_AT_declaration";
3951     case DW_AT_discr_list:
3952       return "DW_AT_discr_list";
3953     case DW_AT_encoding:
3954       return "DW_AT_encoding";
3955     case DW_AT_external:
3956       return "DW_AT_external";
3957     case DW_AT_explicit:
3958       return "DW_AT_explicit";
3959     case DW_AT_frame_base:
3960       return "DW_AT_frame_base";
3961     case DW_AT_friend:
3962       return "DW_AT_friend";
3963     case DW_AT_identifier_case:
3964       return "DW_AT_identifier_case";
3965     case DW_AT_macro_info:
3966       return "DW_AT_macro_info";
3967     case DW_AT_namelist_items:
3968       return "DW_AT_namelist_items";
3969     case DW_AT_priority:
3970       return "DW_AT_priority";
3971     case DW_AT_segment:
3972       return "DW_AT_segment";
3973     case DW_AT_specification:
3974       return "DW_AT_specification";
3975     case DW_AT_static_link:
3976       return "DW_AT_static_link";
3977     case DW_AT_type:
3978       return "DW_AT_type";
3979     case DW_AT_use_location:
3980       return "DW_AT_use_location";
3981     case DW_AT_variable_parameter:
3982       return "DW_AT_variable_parameter";
3983     case DW_AT_virtuality:
3984       return "DW_AT_virtuality";
3985     case DW_AT_vtable_elem_location:
3986       return "DW_AT_vtable_elem_location";
3987
3988     case DW_AT_allocated:
3989       return "DW_AT_allocated";
3990     case DW_AT_associated:
3991       return "DW_AT_associated";
3992     case DW_AT_data_location:
3993       return "DW_AT_data_location";
3994     case DW_AT_byte_stride:
3995       return "DW_AT_byte_stride";
3996     case DW_AT_entry_pc:
3997       return "DW_AT_entry_pc";
3998     case DW_AT_use_UTF8:
3999       return "DW_AT_use_UTF8";
4000     case DW_AT_extension:
4001       return "DW_AT_extension";
4002     case DW_AT_ranges:
4003       return "DW_AT_ranges";
4004     case DW_AT_trampoline:
4005       return "DW_AT_trampoline";
4006     case DW_AT_call_column:
4007       return "DW_AT_call_column";
4008     case DW_AT_call_file:
4009       return "DW_AT_call_file";
4010     case DW_AT_call_line:
4011       return "DW_AT_call_line";
4012     case DW_AT_object_pointer:
4013       return "DW_AT_object_pointer";
4014
4015     case DW_AT_signature:
4016       return "DW_AT_signature";
4017     case DW_AT_main_subprogram:
4018       return "DW_AT_main_subprogram";
4019     case DW_AT_data_bit_offset:
4020       return "DW_AT_data_bit_offset";
4021     case DW_AT_const_expr:
4022       return "DW_AT_const_expr";
4023     case DW_AT_enum_class:
4024       return "DW_AT_enum_class";
4025     case DW_AT_linkage_name:
4026       return "DW_AT_linkage_name";
4027
4028     case DW_AT_MIPS_fde:
4029       return "DW_AT_MIPS_fde";
4030     case DW_AT_MIPS_loop_begin:
4031       return "DW_AT_MIPS_loop_begin";
4032     case DW_AT_MIPS_tail_loop_begin:
4033       return "DW_AT_MIPS_tail_loop_begin";
4034     case DW_AT_MIPS_epilog_begin:
4035       return "DW_AT_MIPS_epilog_begin";
4036 #if VMS_DEBUGGING_INFO
4037     case DW_AT_HP_prologue:
4038       return "DW_AT_HP_prologue";
4039 #else
4040     case DW_AT_MIPS_loop_unroll_factor:
4041       return "DW_AT_MIPS_loop_unroll_factor";
4042 #endif
4043     case DW_AT_MIPS_software_pipeline_depth:
4044       return "DW_AT_MIPS_software_pipeline_depth";
4045     case DW_AT_MIPS_linkage_name:
4046       return "DW_AT_MIPS_linkage_name";
4047 #if VMS_DEBUGGING_INFO
4048     case DW_AT_HP_epilogue:
4049       return "DW_AT_HP_epilogue";
4050 #else
4051     case DW_AT_MIPS_stride:
4052       return "DW_AT_MIPS_stride";
4053 #endif
4054     case DW_AT_MIPS_abstract_name:
4055       return "DW_AT_MIPS_abstract_name";
4056     case DW_AT_MIPS_clone_origin:
4057       return "DW_AT_MIPS_clone_origin";
4058     case DW_AT_MIPS_has_inlines:
4059       return "DW_AT_MIPS_has_inlines";
4060
4061     case DW_AT_sf_names:
4062       return "DW_AT_sf_names";
4063     case DW_AT_src_info:
4064       return "DW_AT_src_info";
4065     case DW_AT_mac_info:
4066       return "DW_AT_mac_info";
4067     case DW_AT_src_coords:
4068       return "DW_AT_src_coords";
4069     case DW_AT_body_begin:
4070       return "DW_AT_body_begin";
4071     case DW_AT_body_end:
4072       return "DW_AT_body_end";
4073
4074     case DW_AT_GNU_vector:
4075       return "DW_AT_GNU_vector";
4076     case DW_AT_GNU_guarded_by:
4077       return "DW_AT_GNU_guarded_by";
4078     case DW_AT_GNU_pt_guarded_by:
4079       return "DW_AT_GNU_pt_guarded_by";
4080     case DW_AT_GNU_guarded:
4081       return "DW_AT_GNU_guarded";
4082     case DW_AT_GNU_pt_guarded:
4083       return "DW_AT_GNU_pt_guarded";
4084     case DW_AT_GNU_locks_excluded:
4085       return "DW_AT_GNU_locks_excluded";
4086     case DW_AT_GNU_exclusive_locks_required:
4087       return "DW_AT_GNU_exclusive_locks_required";
4088     case DW_AT_GNU_shared_locks_required:
4089       return "DW_AT_GNU_shared_locks_required";
4090     case DW_AT_GNU_odr_signature:
4091       return "DW_AT_GNU_odr_signature";
4092     case DW_AT_GNU_template_name:
4093       return "DW_AT_GNU_template_name";
4094     case DW_AT_GNU_call_site_value:
4095       return "DW_AT_GNU_call_site_value";
4096     case DW_AT_GNU_call_site_data_value:
4097       return "DW_AT_GNU_call_site_data_value";
4098     case DW_AT_GNU_call_site_target:
4099       return "DW_AT_GNU_call_site_target";
4100     case DW_AT_GNU_call_site_target_clobbered:
4101       return "DW_AT_GNU_call_site_target_clobbered";
4102     case DW_AT_GNU_tail_call:
4103       return "DW_AT_GNU_tail_call";
4104     case DW_AT_GNU_all_tail_call_sites:
4105       return "DW_AT_GNU_all_tail_call_sites";
4106     case DW_AT_GNU_all_call_sites:
4107       return "DW_AT_GNU_all_call_sites";
4108     case DW_AT_GNU_all_source_call_sites:
4109       return "DW_AT_GNU_all_source_call_sites";
4110     case DW_AT_GNU_macros:
4111       return "DW_AT_GNU_macros";
4112
4113     case DW_AT_GNAT_descriptive_type:
4114       return "DW_AT_GNAT_descriptive_type";
4115
4116     case DW_AT_VMS_rtnbeg_pd_address:
4117       return "DW_AT_VMS_rtnbeg_pd_address";
4118
4119     default:
4120       return "DW_AT_<unknown>";
4121     }
4122 }
4123
4124 /* Convert a DWARF value form code into its string name.  */
4125
4126 static const char *
4127 dwarf_form_name (unsigned int form)
4128 {
4129   switch (form)
4130     {
4131     case DW_FORM_addr:
4132       return "DW_FORM_addr";
4133     case DW_FORM_block2:
4134       return "DW_FORM_block2";
4135     case DW_FORM_block4:
4136       return "DW_FORM_block4";
4137     case DW_FORM_data2:
4138       return "DW_FORM_data2";
4139     case DW_FORM_data4:
4140       return "DW_FORM_data4";
4141     case DW_FORM_data8:
4142       return "DW_FORM_data8";
4143     case DW_FORM_string:
4144       return "DW_FORM_string";
4145     case DW_FORM_block:
4146       return "DW_FORM_block";
4147     case DW_FORM_block1:
4148       return "DW_FORM_block1";
4149     case DW_FORM_data1:
4150       return "DW_FORM_data1";
4151     case DW_FORM_flag:
4152       return "DW_FORM_flag";
4153     case DW_FORM_sdata:
4154       return "DW_FORM_sdata";
4155     case DW_FORM_strp:
4156       return "DW_FORM_strp";
4157     case DW_FORM_udata:
4158       return "DW_FORM_udata";
4159     case DW_FORM_ref_addr:
4160       return "DW_FORM_ref_addr";
4161     case DW_FORM_ref1:
4162       return "DW_FORM_ref1";
4163     case DW_FORM_ref2:
4164       return "DW_FORM_ref2";
4165     case DW_FORM_ref4:
4166       return "DW_FORM_ref4";
4167     case DW_FORM_ref8:
4168       return "DW_FORM_ref8";
4169     case DW_FORM_ref_udata:
4170       return "DW_FORM_ref_udata";
4171     case DW_FORM_indirect:
4172       return "DW_FORM_indirect";
4173     case DW_FORM_sec_offset:
4174       return "DW_FORM_sec_offset";
4175     case DW_FORM_exprloc:
4176       return "DW_FORM_exprloc";
4177     case DW_FORM_flag_present:
4178       return "DW_FORM_flag_present";
4179     case DW_FORM_ref_sig8:
4180       return "DW_FORM_ref_sig8";
4181     default:
4182       return "DW_FORM_<unknown>";
4183     }
4184 }
4185 \f
4186 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4187    instance of an inlined instance of a decl which is local to an inline
4188    function, so we have to trace all of the way back through the origin chain
4189    to find out what sort of node actually served as the original seed for the
4190    given block.  */
4191
4192 static tree
4193 decl_ultimate_origin (const_tree decl)
4194 {
4195   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4196     return NULL_TREE;
4197
4198   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4199      nodes in the function to point to themselves; ignore that if
4200      we're trying to output the abstract instance of this function.  */
4201   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4202     return NULL_TREE;
4203
4204   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4205      most distant ancestor, this should never happen.  */
4206   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4207
4208   return DECL_ABSTRACT_ORIGIN (decl);
4209 }
4210
4211 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4212    of a virtual function may refer to a base class, so we check the 'this'
4213    parameter.  */
4214
4215 static tree
4216 decl_class_context (tree decl)
4217 {
4218   tree context = NULL_TREE;
4219
4220   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4221     context = DECL_CONTEXT (decl);
4222   else
4223     context = TYPE_MAIN_VARIANT
4224       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4225
4226   if (context && !TYPE_P (context))
4227     context = NULL_TREE;
4228
4229   return context;
4230 }
4231 \f
4232 /* Add an attribute/value pair to a DIE.  */
4233
4234 static inline void
4235 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4236 {
4237   /* Maybe this should be an assert?  */
4238   if (die == NULL)
4239     return;
4240
4241   if (die->die_attr == NULL)
4242     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4243   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4244 }
4245
4246 static inline enum dw_val_class
4247 AT_class (dw_attr_ref a)
4248 {
4249   return a->dw_attr_val.val_class;
4250 }
4251
4252 /* Add a flag value attribute to a DIE.  */
4253
4254 static inline void
4255 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4256 {
4257   dw_attr_node attr;
4258
4259   attr.dw_attr = attr_kind;
4260   attr.dw_attr_val.val_class = dw_val_class_flag;
4261   attr.dw_attr_val.v.val_flag = flag;
4262   add_dwarf_attr (die, &attr);
4263 }
4264
4265 static inline unsigned
4266 AT_flag (dw_attr_ref a)
4267 {
4268   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4269   return a->dw_attr_val.v.val_flag;
4270 }
4271
4272 /* Add a signed integer attribute value to a DIE.  */
4273
4274 static inline void
4275 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4276 {
4277   dw_attr_node attr;
4278
4279   attr.dw_attr = attr_kind;
4280   attr.dw_attr_val.val_class = dw_val_class_const;
4281   attr.dw_attr_val.v.val_int = int_val;
4282   add_dwarf_attr (die, &attr);
4283 }
4284
4285 static inline HOST_WIDE_INT
4286 AT_int (dw_attr_ref a)
4287 {
4288   gcc_assert (a && AT_class (a) == dw_val_class_const);
4289   return a->dw_attr_val.v.val_int;
4290 }
4291
4292 /* Add an unsigned integer attribute value to a DIE.  */
4293
4294 static inline void
4295 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4296                  unsigned HOST_WIDE_INT unsigned_val)
4297 {
4298   dw_attr_node attr;
4299
4300   attr.dw_attr = attr_kind;
4301   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4302   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4303   add_dwarf_attr (die, &attr);
4304 }
4305
4306 static inline unsigned HOST_WIDE_INT
4307 AT_unsigned (dw_attr_ref a)
4308 {
4309   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4310   return a->dw_attr_val.v.val_unsigned;
4311 }
4312
4313 /* Add an unsigned double integer attribute value to a DIE.  */
4314
4315 static inline void
4316 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4317                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4318 {
4319   dw_attr_node attr;
4320
4321   attr.dw_attr = attr_kind;
4322   attr.dw_attr_val.val_class = dw_val_class_const_double;
4323   attr.dw_attr_val.v.val_double.high = high;
4324   attr.dw_attr_val.v.val_double.low = low;
4325   add_dwarf_attr (die, &attr);
4326 }
4327
4328 /* Add a floating point attribute value to a DIE and return it.  */
4329
4330 static inline void
4331 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4332             unsigned int length, unsigned int elt_size, unsigned char *array)
4333 {
4334   dw_attr_node attr;
4335
4336   attr.dw_attr = attr_kind;
4337   attr.dw_attr_val.val_class = dw_val_class_vec;
4338   attr.dw_attr_val.v.val_vec.length = length;
4339   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4340   attr.dw_attr_val.v.val_vec.array = array;
4341   add_dwarf_attr (die, &attr);
4342 }
4343
4344 /* Add an 8-byte data attribute value to a DIE.  */
4345
4346 static inline void
4347 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4348               unsigned char data8[8])
4349 {
4350   dw_attr_node attr;
4351
4352   attr.dw_attr = attr_kind;
4353   attr.dw_attr_val.val_class = dw_val_class_data8;
4354   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4355   add_dwarf_attr (die, &attr);
4356 }
4357
4358 /* Hash and equality functions for debug_str_hash.  */
4359
4360 static hashval_t
4361 debug_str_do_hash (const void *x)
4362 {
4363   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4364 }
4365
4366 static int
4367 debug_str_eq (const void *x1, const void *x2)
4368 {
4369   return strcmp ((((const struct indirect_string_node *)x1)->str),
4370                  (const char *)x2) == 0;
4371 }
4372
4373 /* Add STR to the indirect string hash table.  */
4374
4375 static struct indirect_string_node *
4376 find_AT_string (const char *str)
4377 {
4378   struct indirect_string_node *node;
4379   void **slot;
4380
4381   if (! debug_str_hash)
4382     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4383                                       debug_str_eq, NULL);
4384
4385   slot = htab_find_slot_with_hash (debug_str_hash, str,
4386                                    htab_hash_string (str), INSERT);
4387   if (*slot == NULL)
4388     {
4389       node = ggc_alloc_cleared_indirect_string_node ();
4390       node->str = ggc_strdup (str);
4391       *slot = node;
4392     }
4393   else
4394     node = (struct indirect_string_node *) *slot;
4395
4396   node->refcount++;
4397   return node;
4398 }
4399
4400 /* Add a string attribute value to a DIE.  */
4401
4402 static inline void
4403 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4404 {
4405   dw_attr_node attr;
4406   struct indirect_string_node *node;
4407
4408   node = find_AT_string (str);
4409
4410   attr.dw_attr = attr_kind;
4411   attr.dw_attr_val.val_class = dw_val_class_str;
4412   attr.dw_attr_val.v.val_str = node;
4413   add_dwarf_attr (die, &attr);
4414 }
4415
4416 static inline const char *
4417 AT_string (dw_attr_ref a)
4418 {
4419   gcc_assert (a && AT_class (a) == dw_val_class_str);
4420   return a->dw_attr_val.v.val_str->str;
4421 }
4422
4423 /* Find out whether a string should be output inline in DIE
4424    or out-of-line in .debug_str section.  */
4425
4426 static enum dwarf_form
4427 AT_string_form (dw_attr_ref a)
4428 {
4429   struct indirect_string_node *node;
4430   unsigned int len;
4431   char label[32];
4432
4433   gcc_assert (a && AT_class (a) == dw_val_class_str);
4434
4435   node = a->dw_attr_val.v.val_str;
4436   if (node->form)
4437     return node->form;
4438
4439   len = strlen (node->str) + 1;
4440
4441   /* If the string is shorter or equal to the size of the reference, it is
4442      always better to put it inline.  */
4443   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4444     return node->form = DW_FORM_string;
4445
4446   /* If we cannot expect the linker to merge strings in .debug_str
4447      section, only put it into .debug_str if it is worth even in this
4448      single module.  */
4449   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4450       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4451       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4452     return node->form = DW_FORM_string;
4453
4454   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4455   ++dw2_string_counter;
4456   node->label = xstrdup (label);
4457
4458   return node->form = DW_FORM_strp;
4459 }
4460
4461 /* Add a DIE reference attribute value to a DIE.  */
4462
4463 static inline void
4464 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4465 {
4466   dw_attr_node attr;
4467
4468 #ifdef ENABLE_CHECKING
4469   gcc_assert (targ_die != NULL);
4470 #else
4471   /* With LTO we can end up trying to reference something we didn't create
4472      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4473   if (targ_die == NULL)
4474     return;
4475 #endif
4476
4477   attr.dw_attr = attr_kind;
4478   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4479   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4480   attr.dw_attr_val.v.val_die_ref.external = 0;
4481   add_dwarf_attr (die, &attr);
4482 }
4483
4484 /* Add an AT_specification attribute to a DIE, and also make the back
4485    pointer from the specification to the definition.  */
4486
4487 static inline void
4488 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4489 {
4490   add_AT_die_ref (die, DW_AT_specification, targ_die);
4491   gcc_assert (!targ_die->die_definition);
4492   targ_die->die_definition = die;
4493 }
4494
4495 static inline dw_die_ref
4496 AT_ref (dw_attr_ref a)
4497 {
4498   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4499   return a->dw_attr_val.v.val_die_ref.die;
4500 }
4501
4502 static inline int
4503 AT_ref_external (dw_attr_ref a)
4504 {
4505   if (a && AT_class (a) == dw_val_class_die_ref)
4506     return a->dw_attr_val.v.val_die_ref.external;
4507
4508   return 0;
4509 }
4510
4511 static inline void
4512 set_AT_ref_external (dw_attr_ref a, int i)
4513 {
4514   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4515   a->dw_attr_val.v.val_die_ref.external = i;
4516 }
4517
4518 /* Add an FDE reference attribute value to a DIE.  */
4519
4520 static inline void
4521 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4522 {
4523   dw_attr_node attr;
4524
4525   attr.dw_attr = attr_kind;
4526   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4527   attr.dw_attr_val.v.val_fde_index = targ_fde;
4528   add_dwarf_attr (die, &attr);
4529 }
4530
4531 /* Add a location description attribute value to a DIE.  */
4532
4533 static inline void
4534 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4535 {
4536   dw_attr_node attr;
4537
4538   attr.dw_attr = attr_kind;
4539   attr.dw_attr_val.val_class = dw_val_class_loc;
4540   attr.dw_attr_val.v.val_loc = loc;
4541   add_dwarf_attr (die, &attr);
4542 }
4543
4544 static inline dw_loc_descr_ref
4545 AT_loc (dw_attr_ref a)
4546 {
4547   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4548   return a->dw_attr_val.v.val_loc;
4549 }
4550
4551 static inline void
4552 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4553 {
4554   dw_attr_node attr;
4555
4556   attr.dw_attr = attr_kind;
4557   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4558   attr.dw_attr_val.v.val_loc_list = loc_list;
4559   add_dwarf_attr (die, &attr);
4560   have_location_lists = true;
4561 }
4562
4563 static inline dw_loc_list_ref
4564 AT_loc_list (dw_attr_ref a)
4565 {
4566   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4567   return a->dw_attr_val.v.val_loc_list;
4568 }
4569
4570 static inline dw_loc_list_ref *
4571 AT_loc_list_ptr (dw_attr_ref a)
4572 {
4573   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4574   return &a->dw_attr_val.v.val_loc_list;
4575 }
4576
4577 /* Add an address constant attribute value to a DIE.  */
4578
4579 static inline void
4580 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4581 {
4582   dw_attr_node attr;
4583
4584   attr.dw_attr = attr_kind;
4585   attr.dw_attr_val.val_class = dw_val_class_addr;
4586   attr.dw_attr_val.v.val_addr = addr;
4587   add_dwarf_attr (die, &attr);
4588 }
4589
4590 /* Get the RTX from to an address DIE attribute.  */
4591
4592 static inline rtx
4593 AT_addr (dw_attr_ref a)
4594 {
4595   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4596   return a->dw_attr_val.v.val_addr;
4597 }
4598
4599 /* Add a file attribute value to a DIE.  */
4600
4601 static inline void
4602 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4603              struct dwarf_file_data *fd)
4604 {
4605   dw_attr_node attr;
4606
4607   attr.dw_attr = attr_kind;
4608   attr.dw_attr_val.val_class = dw_val_class_file;
4609   attr.dw_attr_val.v.val_file = fd;
4610   add_dwarf_attr (die, &attr);
4611 }
4612
4613 /* Get the dwarf_file_data from a file DIE attribute.  */
4614
4615 static inline struct dwarf_file_data *
4616 AT_file (dw_attr_ref a)
4617 {
4618   gcc_assert (a && AT_class (a) == dw_val_class_file);
4619   return a->dw_attr_val.v.val_file;
4620 }
4621
4622 /* Add a vms delta attribute value to a DIE.  */
4623
4624 static inline void
4625 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4626                   const char *lbl1, const char *lbl2)
4627 {
4628   dw_attr_node attr;
4629
4630   attr.dw_attr = attr_kind;
4631   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4632   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4633   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4634   add_dwarf_attr (die, &attr);
4635 }
4636
4637 /* Add a label identifier attribute value to a DIE.  */
4638
4639 static inline void
4640 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4641 {
4642   dw_attr_node attr;
4643
4644   attr.dw_attr = attr_kind;
4645   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4646   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4647   add_dwarf_attr (die, &attr);
4648 }
4649
4650 /* Add a section offset attribute value to a DIE, an offset into the
4651    debug_line section.  */
4652
4653 static inline void
4654 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4655                 const char *label)
4656 {
4657   dw_attr_node attr;
4658
4659   attr.dw_attr = attr_kind;
4660   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4661   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4662   add_dwarf_attr (die, &attr);
4663 }
4664
4665 /* Add a section offset attribute value to a DIE, an offset into the
4666    debug_macinfo section.  */
4667
4668 static inline void
4669 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4670                const char *label)
4671 {
4672   dw_attr_node attr;
4673
4674   attr.dw_attr = attr_kind;
4675   attr.dw_attr_val.val_class = dw_val_class_macptr;
4676   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4677   add_dwarf_attr (die, &attr);
4678 }
4679
4680 /* Add an offset attribute value to a DIE.  */
4681
4682 static inline void
4683 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4684                unsigned HOST_WIDE_INT offset)
4685 {
4686   dw_attr_node attr;
4687
4688   attr.dw_attr = attr_kind;
4689   attr.dw_attr_val.val_class = dw_val_class_offset;
4690   attr.dw_attr_val.v.val_offset = offset;
4691   add_dwarf_attr (die, &attr);
4692 }
4693
4694 /* Add an range_list attribute value to a DIE.  */
4695
4696 static void
4697 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4698                    long unsigned int offset)
4699 {
4700   dw_attr_node attr;
4701
4702   attr.dw_attr = attr_kind;
4703   attr.dw_attr_val.val_class = dw_val_class_range_list;
4704   attr.dw_attr_val.v.val_offset = offset;
4705   add_dwarf_attr (die, &attr);
4706 }
4707
4708 /* Return the start label of a delta attribute.  */
4709
4710 static inline const char *
4711 AT_vms_delta1 (dw_attr_ref a)
4712 {
4713   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4714   return a->dw_attr_val.v.val_vms_delta.lbl1;
4715 }
4716
4717 /* Return the end label of a delta attribute.  */
4718
4719 static inline const char *
4720 AT_vms_delta2 (dw_attr_ref a)
4721 {
4722   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4723   return a->dw_attr_val.v.val_vms_delta.lbl2;
4724 }
4725
4726 static inline const char *
4727 AT_lbl (dw_attr_ref a)
4728 {
4729   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4730                     || AT_class (a) == dw_val_class_lineptr
4731                     || AT_class (a) == dw_val_class_macptr));
4732   return a->dw_attr_val.v.val_lbl_id;
4733 }
4734
4735 /* Get the attribute of type attr_kind.  */
4736
4737 static dw_attr_ref
4738 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4739 {
4740   dw_attr_ref a;
4741   unsigned ix;
4742   dw_die_ref spec = NULL;
4743
4744   if (! die)
4745     return NULL;
4746
4747   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4748     if (a->dw_attr == attr_kind)
4749       return a;
4750     else if (a->dw_attr == DW_AT_specification
4751              || a->dw_attr == DW_AT_abstract_origin)
4752       spec = AT_ref (a);
4753
4754   if (spec)
4755     return get_AT (spec, attr_kind);
4756
4757   return NULL;
4758 }
4759
4760 /* Return the "low pc" attribute value, typically associated with a subprogram
4761    DIE.  Return null if the "low pc" attribute is either not present, or if it
4762    cannot be represented as an assembler label identifier.  */
4763
4764 static inline const char *
4765 get_AT_low_pc (dw_die_ref die)
4766 {
4767   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4768
4769   return a ? AT_lbl (a) : NULL;
4770 }
4771
4772 /* Return the "high pc" attribute value, typically associated with a subprogram
4773    DIE.  Return null if the "high pc" attribute is either not present, or if it
4774    cannot be represented as an assembler label identifier.  */
4775
4776 static inline const char *
4777 get_AT_hi_pc (dw_die_ref die)
4778 {
4779   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4780
4781   return a ? AT_lbl (a) : NULL;
4782 }
4783
4784 /* Return the value of the string attribute designated by ATTR_KIND, or
4785    NULL if it is not present.  */
4786
4787 static inline const char *
4788 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4789 {
4790   dw_attr_ref a = get_AT (die, attr_kind);
4791
4792   return a ? AT_string (a) : NULL;
4793 }
4794
4795 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4796    if it is not present.  */
4797
4798 static inline int
4799 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4800 {
4801   dw_attr_ref a = get_AT (die, attr_kind);
4802
4803   return a ? AT_flag (a) : 0;
4804 }
4805
4806 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4807    if it is not present.  */
4808
4809 static inline unsigned
4810 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4811 {
4812   dw_attr_ref a = get_AT (die, attr_kind);
4813
4814   return a ? AT_unsigned (a) : 0;
4815 }
4816
4817 static inline dw_die_ref
4818 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4819 {
4820   dw_attr_ref a = get_AT (die, attr_kind);
4821
4822   return a ? AT_ref (a) : NULL;
4823 }
4824
4825 static inline struct dwarf_file_data *
4826 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4827 {
4828   dw_attr_ref a = get_AT (die, attr_kind);
4829
4830   return a ? AT_file (a) : NULL;
4831 }
4832
4833 /* Return TRUE if the language is C++.  */
4834
4835 static inline bool
4836 is_cxx (void)
4837 {
4838   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4839
4840   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4841 }
4842
4843 /* Return TRUE if the language is Fortran.  */
4844
4845 static inline bool
4846 is_fortran (void)
4847 {
4848   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4849
4850   return (lang == DW_LANG_Fortran77
4851           || lang == DW_LANG_Fortran90
4852           || lang == DW_LANG_Fortran95);
4853 }
4854
4855 /* Return TRUE if the language is Ada.  */
4856
4857 static inline bool
4858 is_ada (void)
4859 {
4860   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4861
4862   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4863 }
4864
4865 /* Remove the specified attribute if present.  */
4866
4867 static void
4868 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4869 {
4870   dw_attr_ref a;
4871   unsigned ix;
4872
4873   if (! die)
4874     return;
4875
4876   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4877     if (a->dw_attr == attr_kind)
4878       {
4879         if (AT_class (a) == dw_val_class_str)
4880           if (a->dw_attr_val.v.val_str->refcount)
4881             a->dw_attr_val.v.val_str->refcount--;
4882
4883         /* VEC_ordered_remove should help reduce the number of abbrevs
4884            that are needed.  */
4885         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4886         return;
4887       }
4888 }
4889
4890 /* Remove CHILD from its parent.  PREV must have the property that
4891    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4892
4893 static void
4894 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4895 {
4896   gcc_assert (child->die_parent == prev->die_parent);
4897   gcc_assert (prev->die_sib == child);
4898   if (prev == child)
4899     {
4900       gcc_assert (child->die_parent->die_child == child);
4901       prev = NULL;
4902     }
4903   else
4904     prev->die_sib = child->die_sib;
4905   if (child->die_parent->die_child == child)
4906     child->die_parent->die_child = prev;
4907 }
4908
4909 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4910    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4911
4912 static void
4913 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4914 {
4915   dw_die_ref parent = old_child->die_parent;
4916
4917   gcc_assert (parent == prev->die_parent);
4918   gcc_assert (prev->die_sib == old_child);
4919
4920   new_child->die_parent = parent;
4921   if (prev == old_child)
4922     {
4923       gcc_assert (parent->die_child == old_child);
4924       new_child->die_sib = new_child;
4925     }
4926   else
4927     {
4928       prev->die_sib = new_child;
4929       new_child->die_sib = old_child->die_sib;
4930     }
4931   if (old_child->die_parent->die_child == old_child)
4932     old_child->die_parent->die_child = new_child;
4933 }
4934
4935 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4936
4937 static void
4938 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4939 {
4940   dw_die_ref c;
4941   new_parent->die_child = old_parent->die_child;
4942   old_parent->die_child = NULL;
4943   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4944 }
4945
4946 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4947    matches TAG.  */
4948
4949 static void
4950 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4951 {
4952   dw_die_ref c;
4953
4954   c = die->die_child;
4955   if (c) do {
4956     dw_die_ref prev = c;
4957     c = c->die_sib;
4958     while (c->die_tag == tag)
4959       {
4960         remove_child_with_prev (c, prev);
4961         /* Might have removed every child.  */
4962         if (c == c->die_sib)
4963           return;
4964         c = c->die_sib;
4965       }
4966   } while (c != die->die_child);
4967 }
4968
4969 /* Add a CHILD_DIE as the last child of DIE.  */
4970
4971 static void
4972 add_child_die (dw_die_ref die, dw_die_ref child_die)
4973 {
4974   /* FIXME this should probably be an assert.  */
4975   if (! die || ! child_die)
4976     return;
4977   gcc_assert (die != child_die);
4978
4979   child_die->die_parent = die;
4980   if (die->die_child)
4981     {
4982       child_die->die_sib = die->die_child->die_sib;
4983       die->die_child->die_sib = child_die;
4984     }
4985   else
4986     child_die->die_sib = child_die;
4987   die->die_child = child_die;
4988 }
4989
4990 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4991    is the specification, to the end of PARENT's list of children.
4992    This is done by removing and re-adding it.  */
4993
4994 static void
4995 splice_child_die (dw_die_ref parent, dw_die_ref child)
4996 {
4997   dw_die_ref p;
4998
4999   /* We want the declaration DIE from inside the class, not the
5000      specification DIE at toplevel.  */
5001   if (child->die_parent != parent)
5002     {
5003       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5004
5005       if (tmp)
5006         child = tmp;
5007     }
5008
5009   gcc_assert (child->die_parent == parent
5010               || (child->die_parent
5011                   == get_AT_ref (parent, DW_AT_specification)));
5012
5013   for (p = child->die_parent->die_child; ; p = p->die_sib)
5014     if (p->die_sib == child)
5015       {
5016         remove_child_with_prev (child, p);
5017         break;
5018       }
5019
5020   add_child_die (parent, child);
5021 }
5022
5023 /* Return a pointer to a newly created DIE node.  */
5024
5025 static inline dw_die_ref
5026 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5027 {
5028   dw_die_ref die = ggc_alloc_cleared_die_node ();
5029
5030   die->die_tag = tag_value;
5031
5032   if (parent_die != NULL)
5033     add_child_die (parent_die, die);
5034   else
5035     {
5036       limbo_die_node *limbo_node;
5037
5038       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5039       limbo_node->die = die;
5040       limbo_node->created_for = t;
5041       limbo_node->next = limbo_die_list;
5042       limbo_die_list = limbo_node;
5043     }
5044
5045   return die;
5046 }
5047
5048 /* Return the DIE associated with the given type specifier.  */
5049
5050 static inline dw_die_ref
5051 lookup_type_die (tree type)
5052 {
5053   return TYPE_SYMTAB_DIE (type);
5054 }
5055
5056 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5057    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5058    anonymous type instead the one of the naming typedef.  */
5059
5060 static inline dw_die_ref
5061 strip_naming_typedef (tree type, dw_die_ref type_die)
5062 {
5063   if (type
5064       && TREE_CODE (type) == RECORD_TYPE
5065       && type_die
5066       && type_die->die_tag == DW_TAG_typedef
5067       && is_naming_typedef_decl (TYPE_NAME (type)))
5068     type_die = get_AT_ref (type_die, DW_AT_type);
5069   return type_die;
5070 }
5071
5072 /* Like lookup_type_die, but if type is an anonymous type named by a
5073    typedef[1], return the DIE of the anonymous type instead the one of
5074    the naming typedef.  This is because in gen_typedef_die, we did
5075    equate the anonymous struct named by the typedef with the DIE of
5076    the naming typedef. So by default, lookup_type_die on an anonymous
5077    struct yields the DIE of the naming typedef.
5078
5079    [1]: Read the comment of is_naming_typedef_decl to learn about what
5080    a naming typedef is.  */
5081
5082 static inline dw_die_ref
5083 lookup_type_die_strip_naming_typedef (tree type)
5084 {
5085   dw_die_ref die = lookup_type_die (type);
5086   return strip_naming_typedef (type, die);
5087 }
5088
5089 /* Equate a DIE to a given type specifier.  */
5090
5091 static inline void
5092 equate_type_number_to_die (tree type, dw_die_ref type_die)
5093 {
5094   TYPE_SYMTAB_DIE (type) = type_die;
5095 }
5096
5097 /* Returns a hash value for X (which really is a die_struct).  */
5098
5099 static hashval_t
5100 decl_die_table_hash (const void *x)
5101 {
5102   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5103 }
5104
5105 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5106
5107 static int
5108 decl_die_table_eq (const void *x, const void *y)
5109 {
5110   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5111 }
5112
5113 /* Return the DIE associated with a given declaration.  */
5114
5115 static inline dw_die_ref
5116 lookup_decl_die (tree decl)
5117 {
5118   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5119 }
5120
5121 /* Returns a hash value for X (which really is a var_loc_list).  */
5122
5123 static hashval_t
5124 decl_loc_table_hash (const void *x)
5125 {
5126   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5127 }
5128
5129 /* Return nonzero if decl_id of var_loc_list X is the same as
5130    UID of decl *Y.  */
5131
5132 static int
5133 decl_loc_table_eq (const void *x, const void *y)
5134 {
5135   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5136 }
5137
5138 /* Return the var_loc list associated with a given declaration.  */
5139
5140 static inline var_loc_list *
5141 lookup_decl_loc (const_tree decl)
5142 {
5143   if (!decl_loc_table)
5144     return NULL;
5145   return (var_loc_list *)
5146     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5147 }
5148
5149 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5150
5151 static hashval_t
5152 cached_dw_loc_list_table_hash (const void *x)
5153 {
5154   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5155 }
5156
5157 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5158    UID of decl *Y.  */
5159
5160 static int
5161 cached_dw_loc_list_table_eq (const void *x, const void *y)
5162 {
5163   return (((const cached_dw_loc_list *) x)->decl_id
5164           == DECL_UID ((const_tree) y));
5165 }
5166
5167 /* Equate a DIE to a particular declaration.  */
5168
5169 static void
5170 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5171 {
5172   unsigned int decl_id = DECL_UID (decl);
5173   void **slot;
5174
5175   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5176   *slot = decl_die;
5177   decl_die->decl_id = decl_id;
5178 }
5179
5180 /* Return how many bits covers PIECE EXPR_LIST.  */
5181
5182 static int
5183 decl_piece_bitsize (rtx piece)
5184 {
5185   int ret = (int) GET_MODE (piece);
5186   if (ret)
5187     return ret;
5188   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5189               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5190   return INTVAL (XEXP (XEXP (piece, 0), 0));
5191 }
5192
5193 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5194
5195 static rtx *
5196 decl_piece_varloc_ptr (rtx piece)
5197 {
5198   if ((int) GET_MODE (piece))
5199     return &XEXP (piece, 0);
5200   else
5201     return &XEXP (XEXP (piece, 0), 1);
5202 }
5203
5204 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5205    Next is the chain of following piece nodes.  */
5206
5207 static rtx
5208 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5209 {
5210   if (bitsize <= (int) MAX_MACHINE_MODE)
5211     return alloc_EXPR_LIST (bitsize, loc_note, next);
5212   else
5213     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5214                                                GEN_INT (bitsize),
5215                                                loc_note), next);
5216 }
5217
5218 /* Return rtx that should be stored into loc field for
5219    LOC_NOTE and BITPOS/BITSIZE.  */
5220
5221 static rtx
5222 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5223                       HOST_WIDE_INT bitsize)
5224 {
5225   if (bitsize != -1)
5226     {
5227       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5228       if (bitpos != 0)
5229         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5230     }
5231   return loc_note;
5232 }
5233
5234 /* This function either modifies location piece list *DEST in
5235    place (if SRC and INNER is NULL), or copies location piece list
5236    *SRC to *DEST while modifying it.  Location BITPOS is modified
5237    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5238    not copied and if needed some padding around it is added.
5239    When modifying in place, DEST should point to EXPR_LIST where
5240    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5241    to the start of the whole list and INNER points to the EXPR_LIST
5242    where earlier pieces cover PIECE_BITPOS bits.  */
5243
5244 static void
5245 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5246                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5247                    HOST_WIDE_INT bitsize, rtx loc_note)
5248 {
5249   int diff;
5250   bool copy = inner != NULL;
5251
5252   if (copy)
5253     {
5254       /* First copy all nodes preceeding the current bitpos.  */
5255       while (src != inner)
5256         {
5257           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5258                                    decl_piece_bitsize (*src), NULL_RTX);
5259           dest = &XEXP (*dest, 1);
5260           src = &XEXP (*src, 1);
5261         }
5262     }
5263   /* Add padding if needed.  */
5264   if (bitpos != piece_bitpos)
5265     {
5266       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5267                                copy ? NULL_RTX : *dest);
5268       dest = &XEXP (*dest, 1);
5269     }
5270   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5271     {
5272       gcc_assert (!copy);
5273       /* A piece with correct bitpos and bitsize already exist,
5274          just update the location for it and return.  */
5275       *decl_piece_varloc_ptr (*dest) = loc_note;
5276       return;
5277     }
5278   /* Add the piece that changed.  */
5279   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5280   dest = &XEXP (*dest, 1);
5281   /* Skip over pieces that overlap it.  */
5282   diff = bitpos - piece_bitpos + bitsize;
5283   if (!copy)
5284     src = dest;
5285   while (diff > 0 && *src)
5286     {
5287       rtx piece = *src;
5288       diff -= decl_piece_bitsize (piece);
5289       if (copy)
5290         src = &XEXP (piece, 1);
5291       else
5292         {
5293           *src = XEXP (piece, 1);
5294           free_EXPR_LIST_node (piece);
5295         }
5296     }
5297   /* Add padding if needed.  */
5298   if (diff < 0 && *src)
5299     {
5300       if (!copy)
5301         dest = src;
5302       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5303       dest = &XEXP (*dest, 1);
5304     }
5305   if (!copy)
5306     return;
5307   /* Finally copy all nodes following it.  */
5308   while (*src)
5309     {
5310       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5311                                decl_piece_bitsize (*src), NULL_RTX);
5312       dest = &XEXP (*dest, 1);
5313       src = &XEXP (*src, 1);
5314     }
5315 }
5316
5317 /* Add a variable location node to the linked list for DECL.  */
5318
5319 static struct var_loc_node *
5320 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5321 {
5322   unsigned int decl_id;
5323   var_loc_list *temp;
5324   void **slot;
5325   struct var_loc_node *loc = NULL;
5326   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5327
5328   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5329     {
5330       tree realdecl = DECL_DEBUG_EXPR (decl);
5331       if (realdecl && handled_component_p (realdecl))
5332         {
5333           HOST_WIDE_INT maxsize;
5334           tree innerdecl;
5335           innerdecl
5336             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5337           if (!DECL_P (innerdecl)
5338               || DECL_IGNORED_P (innerdecl)
5339               || TREE_STATIC (innerdecl)
5340               || bitsize <= 0
5341               || bitpos + bitsize > 256
5342               || bitsize != maxsize)
5343             return NULL;
5344           decl = innerdecl;
5345         }
5346     }
5347
5348   decl_id = DECL_UID (decl);
5349   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5350   if (*slot == NULL)
5351     {
5352       temp = ggc_alloc_cleared_var_loc_list ();
5353       temp->decl_id = decl_id;
5354       *slot = temp;
5355     }
5356   else
5357     temp = (var_loc_list *) *slot;
5358
5359   /* For PARM_DECLs try to keep around the original incoming value,
5360      even if that means we'll emit a zero-range .debug_loc entry.  */
5361   if (temp->last
5362       && temp->first == temp->last
5363       && TREE_CODE (decl) == PARM_DECL
5364       && GET_CODE (temp->first->loc) == NOTE
5365       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5366       && DECL_INCOMING_RTL (decl)
5367       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5368       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5369          == GET_CODE (DECL_INCOMING_RTL (decl))
5370       && prev_real_insn (temp->first->loc) == NULL_RTX
5371       && (bitsize != -1
5372           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5373                            NOTE_VAR_LOCATION_LOC (loc_note))
5374           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5375               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5376     {
5377       loc = ggc_alloc_cleared_var_loc_node ();
5378       temp->first->next = loc;
5379       temp->last = loc;
5380       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5381     }
5382   else if (temp->last)
5383     {
5384       struct var_loc_node *last = temp->last, *unused = NULL;
5385       rtx *piece_loc = NULL, last_loc_note;
5386       int piece_bitpos = 0;
5387       if (last->next)
5388         {
5389           last = last->next;
5390           gcc_assert (last->next == NULL);
5391         }
5392       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5393         {
5394           piece_loc = &last->loc;
5395           do
5396             {
5397               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5398               if (piece_bitpos + cur_bitsize > bitpos)
5399                 break;
5400               piece_bitpos += cur_bitsize;
5401               piece_loc = &XEXP (*piece_loc, 1);
5402             }
5403           while (*piece_loc);
5404         }
5405       /* TEMP->LAST here is either pointer to the last but one or
5406          last element in the chained list, LAST is pointer to the
5407          last element.  */
5408       if (label && strcmp (last->label, label) == 0)
5409         {
5410           /* For SRA optimized variables if there weren't any real
5411              insns since last note, just modify the last node.  */
5412           if (piece_loc != NULL)
5413             {
5414               adjust_piece_list (piece_loc, NULL, NULL,
5415                                  bitpos, piece_bitpos, bitsize, loc_note);
5416               return NULL;
5417             }
5418           /* If the last note doesn't cover any instructions, remove it.  */
5419           if (temp->last != last)
5420             {
5421               temp->last->next = NULL;
5422               unused = last;
5423               last = temp->last;
5424               gcc_assert (strcmp (last->label, label) != 0);
5425             }
5426           else
5427             {
5428               gcc_assert (temp->first == temp->last
5429                           || (temp->first->next == temp->last
5430                               && TREE_CODE (decl) == PARM_DECL));
5431               memset (temp->last, '\0', sizeof (*temp->last));
5432               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5433               return temp->last;
5434             }
5435         }
5436       if (bitsize == -1 && NOTE_P (last->loc))
5437         last_loc_note = last->loc;
5438       else if (piece_loc != NULL
5439                && *piece_loc != NULL_RTX
5440                && piece_bitpos == bitpos
5441                && decl_piece_bitsize (*piece_loc) == bitsize)
5442         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5443       else
5444         last_loc_note = NULL_RTX;
5445       /* If the current location is the same as the end of the list,
5446          and either both or neither of the locations is uninitialized,
5447          we have nothing to do.  */
5448       if (last_loc_note == NULL_RTX
5449           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5450                             NOTE_VAR_LOCATION_LOC (loc_note)))
5451           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5452                != NOTE_VAR_LOCATION_STATUS (loc_note))
5453               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5454                    == VAR_INIT_STATUS_UNINITIALIZED)
5455                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5456                       == VAR_INIT_STATUS_UNINITIALIZED))))
5457         {
5458           /* Add LOC to the end of list and update LAST.  If the last
5459              element of the list has been removed above, reuse its
5460              memory for the new node, otherwise allocate a new one.  */
5461           if (unused)
5462             {
5463               loc = unused;
5464               memset (loc, '\0', sizeof (*loc));
5465             }
5466           else
5467             loc = ggc_alloc_cleared_var_loc_node ();
5468           if (bitsize == -1 || piece_loc == NULL)
5469             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5470           else
5471             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5472                                bitpos, piece_bitpos, bitsize, loc_note);
5473           last->next = loc;
5474           /* Ensure TEMP->LAST will point either to the new last but one
5475              element of the chain, or to the last element in it.  */
5476           if (last != temp->last)
5477             temp->last = last;
5478         }
5479       else if (unused)
5480         ggc_free (unused);
5481     }
5482   else
5483     {
5484       loc = ggc_alloc_cleared_var_loc_node ();
5485       temp->first = loc;
5486       temp->last = loc;
5487       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5488     }
5489   return loc;
5490 }
5491 \f
5492 /* Keep track of the number of spaces used to indent the
5493    output of the debugging routines that print the structure of
5494    the DIE internal representation.  */
5495 static int print_indent;
5496
5497 /* Indent the line the number of spaces given by print_indent.  */
5498
5499 static inline void
5500 print_spaces (FILE *outfile)
5501 {
5502   fprintf (outfile, "%*s", print_indent, "");
5503 }
5504
5505 /* Print a type signature in hex.  */
5506
5507 static inline void
5508 print_signature (FILE *outfile, char *sig)
5509 {
5510   int i;
5511
5512   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5513     fprintf (outfile, "%02x", sig[i] & 0xff);
5514 }
5515
5516 /* Print the information associated with a given DIE, and its children.
5517    This routine is a debugging aid only.  */
5518
5519 static void
5520 print_die (dw_die_ref die, FILE *outfile)
5521 {
5522   dw_attr_ref a;
5523   dw_die_ref c;
5524   unsigned ix;
5525
5526   print_spaces (outfile);
5527   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5528            die->die_offset, dwarf_tag_name (die->die_tag),
5529            (void*) die);
5530   print_spaces (outfile);
5531   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5532   fprintf (outfile, " offset: %ld", die->die_offset);
5533   fprintf (outfile, " mark: %d\n", die->die_mark);
5534
5535   if (use_debug_types && die->die_id.die_type_node)
5536     {
5537       print_spaces (outfile);
5538       fprintf (outfile, "  signature: ");
5539       print_signature (outfile, die->die_id.die_type_node->signature);
5540       fprintf (outfile, "\n");
5541     }
5542
5543   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5544     {
5545       print_spaces (outfile);
5546       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5547
5548       switch (AT_class (a))
5549         {
5550         case dw_val_class_addr:
5551           fprintf (outfile, "address");
5552           break;
5553         case dw_val_class_offset:
5554           fprintf (outfile, "offset");
5555           break;
5556         case dw_val_class_loc:
5557           fprintf (outfile, "location descriptor");
5558           break;
5559         case dw_val_class_loc_list:
5560           fprintf (outfile, "location list -> label:%s",
5561                    AT_loc_list (a)->ll_symbol);
5562           break;
5563         case dw_val_class_range_list:
5564           fprintf (outfile, "range list");
5565           break;
5566         case dw_val_class_const:
5567           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5568           break;
5569         case dw_val_class_unsigned_const:
5570           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5571           break;
5572         case dw_val_class_const_double:
5573           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5574                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5575                    a->dw_attr_val.v.val_double.high,
5576                    a->dw_attr_val.v.val_double.low);
5577           break;
5578         case dw_val_class_vec:
5579           fprintf (outfile, "floating-point or vector constant");
5580           break;
5581         case dw_val_class_flag:
5582           fprintf (outfile, "%u", AT_flag (a));
5583           break;
5584         case dw_val_class_die_ref:
5585           if (AT_ref (a) != NULL)
5586             {
5587               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5588                 {
5589                   fprintf (outfile, "die -> signature: ");
5590                   print_signature (outfile,
5591                                    AT_ref (a)->die_id.die_type_node->signature);
5592                 }
5593               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5594                 fprintf (outfile, "die -> label: %s",
5595                          AT_ref (a)->die_id.die_symbol);
5596               else
5597                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5598               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5599             }
5600           else
5601             fprintf (outfile, "die -> <null>");
5602           break;
5603         case dw_val_class_vms_delta:
5604           fprintf (outfile, "delta: @slotcount(%s-%s)",
5605                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5606           break;
5607         case dw_val_class_lbl_id:
5608         case dw_val_class_lineptr:
5609         case dw_val_class_macptr:
5610           fprintf (outfile, "label: %s", AT_lbl (a));
5611           break;
5612         case dw_val_class_str:
5613           if (AT_string (a) != NULL)
5614             fprintf (outfile, "\"%s\"", AT_string (a));
5615           else
5616             fprintf (outfile, "<null>");
5617           break;
5618         case dw_val_class_file:
5619           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5620                    AT_file (a)->emitted_number);
5621           break;
5622         case dw_val_class_data8:
5623           {
5624             int i;
5625
5626             for (i = 0; i < 8; i++)
5627               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5628             break;
5629           }
5630         default:
5631           break;
5632         }
5633
5634       fprintf (outfile, "\n");
5635     }
5636
5637   if (die->die_child != NULL)
5638     {
5639       print_indent += 4;
5640       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5641       print_indent -= 4;
5642     }
5643   if (print_indent == 0)
5644     fprintf (outfile, "\n");
5645 }
5646
5647 /* Print the information collected for a given DIE.  */
5648
5649 DEBUG_FUNCTION void
5650 debug_dwarf_die (dw_die_ref die)
5651 {
5652   print_die (die, stderr);
5653 }
5654
5655 /* Print all DWARF information collected for the compilation unit.
5656    This routine is a debugging aid only.  */
5657
5658 DEBUG_FUNCTION void
5659 debug_dwarf (void)
5660 {
5661   print_indent = 0;
5662   print_die (comp_unit_die (), stderr);
5663 }
5664 \f
5665 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5666    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5667    DIE that marks the start of the DIEs for this include file.  */
5668
5669 static dw_die_ref
5670 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5671 {
5672   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5673   dw_die_ref new_unit = gen_compile_unit_die (filename);
5674
5675   new_unit->die_sib = old_unit;
5676   return new_unit;
5677 }
5678
5679 /* Close an include-file CU and reopen the enclosing one.  */
5680
5681 static dw_die_ref
5682 pop_compile_unit (dw_die_ref old_unit)
5683 {
5684   dw_die_ref new_unit = old_unit->die_sib;
5685
5686   old_unit->die_sib = NULL;
5687   return new_unit;
5688 }
5689
5690 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5691 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5692
5693 /* Calculate the checksum of a location expression.  */
5694
5695 static inline void
5696 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5697 {
5698   int tem;
5699
5700   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5701   CHECKSUM (tem);
5702   CHECKSUM (loc->dw_loc_oprnd1);
5703   CHECKSUM (loc->dw_loc_oprnd2);
5704 }
5705
5706 /* Calculate the checksum of an attribute.  */
5707
5708 static void
5709 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5710 {
5711   dw_loc_descr_ref loc;
5712   rtx r;
5713
5714   CHECKSUM (at->dw_attr);
5715
5716   /* We don't care that this was compiled with a different compiler
5717      snapshot; if the output is the same, that's what matters.  */
5718   if (at->dw_attr == DW_AT_producer)
5719     return;
5720
5721   switch (AT_class (at))
5722     {
5723     case dw_val_class_const:
5724       CHECKSUM (at->dw_attr_val.v.val_int);
5725       break;
5726     case dw_val_class_unsigned_const:
5727       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5728       break;
5729     case dw_val_class_const_double:
5730       CHECKSUM (at->dw_attr_val.v.val_double);
5731       break;
5732     case dw_val_class_vec:
5733       CHECKSUM (at->dw_attr_val.v.val_vec);
5734       break;
5735     case dw_val_class_flag:
5736       CHECKSUM (at->dw_attr_val.v.val_flag);
5737       break;
5738     case dw_val_class_str:
5739       CHECKSUM_STRING (AT_string (at));
5740       break;
5741
5742     case dw_val_class_addr:
5743       r = AT_addr (at);
5744       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5745       CHECKSUM_STRING (XSTR (r, 0));
5746       break;
5747
5748     case dw_val_class_offset:
5749       CHECKSUM (at->dw_attr_val.v.val_offset);
5750       break;
5751
5752     case dw_val_class_loc:
5753       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5754         loc_checksum (loc, ctx);
5755       break;
5756
5757     case dw_val_class_die_ref:
5758       die_checksum (AT_ref (at), ctx, mark);
5759       break;
5760
5761     case dw_val_class_fde_ref:
5762     case dw_val_class_vms_delta:
5763     case dw_val_class_lbl_id:
5764     case dw_val_class_lineptr:
5765     case dw_val_class_macptr:
5766       break;
5767
5768     case dw_val_class_file:
5769       CHECKSUM_STRING (AT_file (at)->filename);
5770       break;
5771
5772     case dw_val_class_data8:
5773       CHECKSUM (at->dw_attr_val.v.val_data8);
5774       break;
5775
5776     default:
5777       break;
5778     }
5779 }
5780
5781 /* Calculate the checksum of a DIE.  */
5782
5783 static void
5784 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5785 {
5786   dw_die_ref c;
5787   dw_attr_ref a;
5788   unsigned ix;
5789
5790   /* To avoid infinite recursion.  */
5791   if (die->die_mark)
5792     {
5793       CHECKSUM (die->die_mark);
5794       return;
5795     }
5796   die->die_mark = ++(*mark);
5797
5798   CHECKSUM (die->die_tag);
5799
5800   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5801     attr_checksum (a, ctx, mark);
5802
5803   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5804 }
5805
5806 #undef CHECKSUM
5807 #undef CHECKSUM_STRING
5808
5809 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5810 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5811 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5812 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5813 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5814 #define CHECKSUM_ATTR(FOO) \
5815   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5816
5817 /* Calculate the checksum of a number in signed LEB128 format.  */
5818
5819 static void
5820 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5821 {
5822   unsigned char byte;
5823   bool more;
5824
5825   while (1)
5826     {
5827       byte = (value & 0x7f);
5828       value >>= 7;
5829       more = !((value == 0 && (byte & 0x40) == 0)
5830                 || (value == -1 && (byte & 0x40) != 0));
5831       if (more)
5832         byte |= 0x80;
5833       CHECKSUM (byte);
5834       if (!more)
5835         break;
5836     }
5837 }
5838
5839 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5840
5841 static void
5842 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5843 {
5844   while (1)
5845     {
5846       unsigned char byte = (value & 0x7f);
5847       value >>= 7;
5848       if (value != 0)
5849         /* More bytes to follow.  */
5850         byte |= 0x80;
5851       CHECKSUM (byte);
5852       if (value == 0)
5853         break;
5854     }
5855 }
5856
5857 /* Checksum the context of the DIE.  This adds the names of any
5858    surrounding namespaces or structures to the checksum.  */
5859
5860 static void
5861 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5862 {
5863   const char *name;
5864   dw_die_ref spec;
5865   int tag = die->die_tag;
5866
5867   if (tag != DW_TAG_namespace
5868       && tag != DW_TAG_structure_type
5869       && tag != DW_TAG_class_type)
5870     return;
5871
5872   name = get_AT_string (die, DW_AT_name);
5873
5874   spec = get_AT_ref (die, DW_AT_specification);
5875   if (spec != NULL)
5876     die = spec;
5877
5878   if (die->die_parent != NULL)
5879     checksum_die_context (die->die_parent, ctx);
5880
5881   CHECKSUM_ULEB128 ('C');
5882   CHECKSUM_ULEB128 (tag);
5883   if (name != NULL)
5884     CHECKSUM_STRING (name);
5885 }
5886
5887 /* Calculate the checksum of a location expression.  */
5888
5889 static inline void
5890 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5891 {
5892   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5893      were emitted as a DW_FORM_sdata instead of a location expression.  */
5894   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5895     {
5896       CHECKSUM_ULEB128 (DW_FORM_sdata);
5897       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5898       return;
5899     }
5900
5901   /* Otherwise, just checksum the raw location expression.  */
5902   while (loc != NULL)
5903     {
5904       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5905       CHECKSUM (loc->dw_loc_oprnd1);
5906       CHECKSUM (loc->dw_loc_oprnd2);
5907       loc = loc->dw_loc_next;
5908     }
5909 }
5910
5911 /* Calculate the checksum of an attribute.  */
5912
5913 static void
5914 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5915                        struct md5_ctx *ctx, int *mark)
5916 {
5917   dw_loc_descr_ref loc;
5918   rtx r;
5919
5920   if (AT_class (at) == dw_val_class_die_ref)
5921     {
5922       dw_die_ref target_die = AT_ref (at);
5923
5924       /* For pointer and reference types, we checksum only the (qualified)
5925          name of the target type (if there is a name).  For friend entries,
5926          we checksum only the (qualified) name of the target type or function.
5927          This allows the checksum to remain the same whether the target type
5928          is complete or not.  */
5929       if ((at->dw_attr == DW_AT_type
5930            && (tag == DW_TAG_pointer_type
5931                || tag == DW_TAG_reference_type
5932                || tag == DW_TAG_rvalue_reference_type
5933                || tag == DW_TAG_ptr_to_member_type))
5934           || (at->dw_attr == DW_AT_friend
5935               && tag == DW_TAG_friend))
5936         {
5937           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5938
5939           if (name_attr != NULL)
5940             {
5941               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5942
5943               if (decl == NULL)
5944                 decl = target_die;
5945               CHECKSUM_ULEB128 ('N');
5946               CHECKSUM_ULEB128 (at->dw_attr);
5947               if (decl->die_parent != NULL)
5948                 checksum_die_context (decl->die_parent, ctx);
5949               CHECKSUM_ULEB128 ('E');
5950               CHECKSUM_STRING (AT_string (name_attr));
5951               return;
5952             }
5953         }
5954
5955       /* For all other references to another DIE, we check to see if the
5956          target DIE has already been visited.  If it has, we emit a
5957          backward reference; if not, we descend recursively.  */
5958       if (target_die->die_mark > 0)
5959         {
5960           CHECKSUM_ULEB128 ('R');
5961           CHECKSUM_ULEB128 (at->dw_attr);
5962           CHECKSUM_ULEB128 (target_die->die_mark);
5963         }
5964       else
5965         {
5966           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5967
5968           if (decl == NULL)
5969             decl = target_die;
5970           target_die->die_mark = ++(*mark);
5971           CHECKSUM_ULEB128 ('T');
5972           CHECKSUM_ULEB128 (at->dw_attr);
5973           if (decl->die_parent != NULL)
5974             checksum_die_context (decl->die_parent, ctx);
5975           die_checksum_ordered (target_die, ctx, mark);
5976         }
5977       return;
5978     }
5979
5980   CHECKSUM_ULEB128 ('A');
5981   CHECKSUM_ULEB128 (at->dw_attr);
5982
5983   switch (AT_class (at))
5984     {
5985     case dw_val_class_const:
5986       CHECKSUM_ULEB128 (DW_FORM_sdata);
5987       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5988       break;
5989
5990     case dw_val_class_unsigned_const:
5991       CHECKSUM_ULEB128 (DW_FORM_sdata);
5992       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5993       break;
5994
5995     case dw_val_class_const_double:
5996       CHECKSUM_ULEB128 (DW_FORM_block);
5997       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5998       CHECKSUM (at->dw_attr_val.v.val_double);
5999       break;
6000
6001     case dw_val_class_vec:
6002       CHECKSUM_ULEB128 (DW_FORM_block);
6003       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6004       CHECKSUM (at->dw_attr_val.v.val_vec);
6005       break;
6006
6007     case dw_val_class_flag:
6008       CHECKSUM_ULEB128 (DW_FORM_flag);
6009       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6010       break;
6011
6012     case dw_val_class_str:
6013       CHECKSUM_ULEB128 (DW_FORM_string);
6014       CHECKSUM_STRING (AT_string (at));
6015       break;
6016
6017     case dw_val_class_addr:
6018       r = AT_addr (at);
6019       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6020       CHECKSUM_ULEB128 (DW_FORM_string);
6021       CHECKSUM_STRING (XSTR (r, 0));
6022       break;
6023
6024     case dw_val_class_offset:
6025       CHECKSUM_ULEB128 (DW_FORM_sdata);
6026       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6027       break;
6028
6029     case dw_val_class_loc:
6030       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6031         loc_checksum_ordered (loc, ctx);
6032       break;
6033
6034     case dw_val_class_fde_ref:
6035     case dw_val_class_lbl_id:
6036     case dw_val_class_lineptr:
6037     case dw_val_class_macptr:
6038       break;
6039
6040     case dw_val_class_file:
6041       CHECKSUM_ULEB128 (DW_FORM_string);
6042       CHECKSUM_STRING (AT_file (at)->filename);
6043       break;
6044
6045     case dw_val_class_data8:
6046       CHECKSUM (at->dw_attr_val.v.val_data8);
6047       break;
6048
6049     default:
6050       break;
6051     }
6052 }
6053
6054 struct checksum_attributes
6055 {
6056   dw_attr_ref at_name;
6057   dw_attr_ref at_type;
6058   dw_attr_ref at_friend;
6059   dw_attr_ref at_accessibility;
6060   dw_attr_ref at_address_class;
6061   dw_attr_ref at_allocated;
6062   dw_attr_ref at_artificial;
6063   dw_attr_ref at_associated;
6064   dw_attr_ref at_binary_scale;
6065   dw_attr_ref at_bit_offset;
6066   dw_attr_ref at_bit_size;
6067   dw_attr_ref at_bit_stride;
6068   dw_attr_ref at_byte_size;
6069   dw_attr_ref at_byte_stride;
6070   dw_attr_ref at_const_value;
6071   dw_attr_ref at_containing_type;
6072   dw_attr_ref at_count;
6073   dw_attr_ref at_data_location;
6074   dw_attr_ref at_data_member_location;
6075   dw_attr_ref at_decimal_scale;
6076   dw_attr_ref at_decimal_sign;
6077   dw_attr_ref at_default_value;
6078   dw_attr_ref at_digit_count;
6079   dw_attr_ref at_discr;
6080   dw_attr_ref at_discr_list;
6081   dw_attr_ref at_discr_value;
6082   dw_attr_ref at_encoding;
6083   dw_attr_ref at_endianity;
6084   dw_attr_ref at_explicit;
6085   dw_attr_ref at_is_optional;
6086   dw_attr_ref at_location;
6087   dw_attr_ref at_lower_bound;
6088   dw_attr_ref at_mutable;
6089   dw_attr_ref at_ordering;
6090   dw_attr_ref at_picture_string;
6091   dw_attr_ref at_prototyped;
6092   dw_attr_ref at_small;
6093   dw_attr_ref at_segment;
6094   dw_attr_ref at_string_length;
6095   dw_attr_ref at_threads_scaled;
6096   dw_attr_ref at_upper_bound;
6097   dw_attr_ref at_use_location;
6098   dw_attr_ref at_use_UTF8;
6099   dw_attr_ref at_variable_parameter;
6100   dw_attr_ref at_virtuality;
6101   dw_attr_ref at_visibility;
6102   dw_attr_ref at_vtable_elem_location;
6103 };
6104
6105 /* Collect the attributes that we will want to use for the checksum.  */
6106
6107 static void
6108 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6109 {
6110   dw_attr_ref a;
6111   unsigned ix;
6112
6113   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6114     {
6115       switch (a->dw_attr)
6116         {
6117         case DW_AT_name:
6118           attrs->at_name = a;
6119           break;
6120         case DW_AT_type:
6121           attrs->at_type = a;
6122           break;
6123         case DW_AT_friend:
6124           attrs->at_friend = a;
6125           break;
6126         case DW_AT_accessibility:
6127           attrs->at_accessibility = a;
6128           break;
6129         case DW_AT_address_class:
6130           attrs->at_address_class = a;
6131           break;
6132         case DW_AT_allocated:
6133           attrs->at_allocated = a;
6134           break;
6135         case DW_AT_artificial:
6136           attrs->at_artificial = a;
6137           break;
6138         case DW_AT_associated:
6139           attrs->at_associated = a;
6140           break;
6141         case DW_AT_binary_scale:
6142           attrs->at_binary_scale = a;
6143           break;
6144         case DW_AT_bit_offset:
6145           attrs->at_bit_offset = a;
6146           break;
6147         case DW_AT_bit_size:
6148           attrs->at_bit_size = a;
6149           break;
6150         case DW_AT_bit_stride:
6151           attrs->at_bit_stride = a;
6152           break;
6153         case DW_AT_byte_size:
6154           attrs->at_byte_size = a;
6155           break;
6156         case DW_AT_byte_stride:
6157           attrs->at_byte_stride = a;
6158           break;
6159         case DW_AT_const_value:
6160           attrs->at_const_value = a;
6161           break;
6162         case DW_AT_containing_type:
6163           attrs->at_containing_type = a;
6164           break;
6165         case DW_AT_count:
6166           attrs->at_count = a;
6167           break;
6168         case DW_AT_data_location:
6169           attrs->at_data_location = a;
6170           break;
6171         case DW_AT_data_member_location:
6172           attrs->at_data_member_location = a;
6173           break;
6174         case DW_AT_decimal_scale:
6175           attrs->at_decimal_scale = a;
6176           break;
6177         case DW_AT_decimal_sign:
6178           attrs->at_decimal_sign = a;
6179           break;
6180         case DW_AT_default_value:
6181           attrs->at_default_value = a;
6182           break;
6183         case DW_AT_digit_count:
6184           attrs->at_digit_count = a;
6185           break;
6186         case DW_AT_discr:
6187           attrs->at_discr = a;
6188           break;
6189         case DW_AT_discr_list:
6190           attrs->at_discr_list = a;
6191           break;
6192         case DW_AT_discr_value:
6193           attrs->at_discr_value = a;
6194           break;
6195         case DW_AT_encoding:
6196           attrs->at_encoding = a;
6197           break;
6198         case DW_AT_endianity:
6199           attrs->at_endianity = a;
6200           break;
6201         case DW_AT_explicit:
6202           attrs->at_explicit = a;
6203           break;
6204         case DW_AT_is_optional:
6205           attrs->at_is_optional = a;
6206           break;
6207         case DW_AT_location:
6208           attrs->at_location = a;
6209           break;
6210         case DW_AT_lower_bound:
6211           attrs->at_lower_bound = a;
6212           break;
6213         case DW_AT_mutable:
6214           attrs->at_mutable = a;
6215           break;
6216         case DW_AT_ordering:
6217           attrs->at_ordering = a;
6218           break;
6219         case DW_AT_picture_string:
6220           attrs->at_picture_string = a;
6221           break;
6222         case DW_AT_prototyped:
6223           attrs->at_prototyped = a;
6224           break;
6225         case DW_AT_small:
6226           attrs->at_small = a;
6227           break;
6228         case DW_AT_segment:
6229           attrs->at_segment = a;
6230           break;
6231         case DW_AT_string_length:
6232           attrs->at_string_length = a;
6233           break;
6234         case DW_AT_threads_scaled:
6235           attrs->at_threads_scaled = a;
6236           break;
6237         case DW_AT_upper_bound:
6238           attrs->at_upper_bound = a;
6239           break;
6240         case DW_AT_use_location:
6241           attrs->at_use_location = a;
6242           break;
6243         case DW_AT_use_UTF8:
6244           attrs->at_use_UTF8 = a;
6245           break;
6246         case DW_AT_variable_parameter:
6247           attrs->at_variable_parameter = a;
6248           break;
6249         case DW_AT_virtuality:
6250           attrs->at_virtuality = a;
6251           break;
6252         case DW_AT_visibility:
6253           attrs->at_visibility = a;
6254           break;
6255         case DW_AT_vtable_elem_location:
6256           attrs->at_vtable_elem_location = a;
6257           break;
6258         default:
6259           break;
6260         }
6261     }
6262 }
6263
6264 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6265
6266 static void
6267 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6268 {
6269   dw_die_ref c;
6270   dw_die_ref decl;
6271   struct checksum_attributes attrs;
6272
6273   CHECKSUM_ULEB128 ('D');
6274   CHECKSUM_ULEB128 (die->die_tag);
6275
6276   memset (&attrs, 0, sizeof (attrs));
6277
6278   decl = get_AT_ref (die, DW_AT_specification);
6279   if (decl != NULL)
6280     collect_checksum_attributes (&attrs, decl);
6281   collect_checksum_attributes (&attrs, die);
6282
6283   CHECKSUM_ATTR (attrs.at_name);
6284   CHECKSUM_ATTR (attrs.at_accessibility);
6285   CHECKSUM_ATTR (attrs.at_address_class);
6286   CHECKSUM_ATTR (attrs.at_allocated);
6287   CHECKSUM_ATTR (attrs.at_artificial);
6288   CHECKSUM_ATTR (attrs.at_associated);
6289   CHECKSUM_ATTR (attrs.at_binary_scale);
6290   CHECKSUM_ATTR (attrs.at_bit_offset);
6291   CHECKSUM_ATTR (attrs.at_bit_size);
6292   CHECKSUM_ATTR (attrs.at_bit_stride);
6293   CHECKSUM_ATTR (attrs.at_byte_size);
6294   CHECKSUM_ATTR (attrs.at_byte_stride);
6295   CHECKSUM_ATTR (attrs.at_const_value);
6296   CHECKSUM_ATTR (attrs.at_containing_type);
6297   CHECKSUM_ATTR (attrs.at_count);
6298   CHECKSUM_ATTR (attrs.at_data_location);
6299   CHECKSUM_ATTR (attrs.at_data_member_location);
6300   CHECKSUM_ATTR (attrs.at_decimal_scale);
6301   CHECKSUM_ATTR (attrs.at_decimal_sign);
6302   CHECKSUM_ATTR (attrs.at_default_value);
6303   CHECKSUM_ATTR (attrs.at_digit_count);
6304   CHECKSUM_ATTR (attrs.at_discr);
6305   CHECKSUM_ATTR (attrs.at_discr_list);
6306   CHECKSUM_ATTR (attrs.at_discr_value);
6307   CHECKSUM_ATTR (attrs.at_encoding);
6308   CHECKSUM_ATTR (attrs.at_endianity);
6309   CHECKSUM_ATTR (attrs.at_explicit);
6310   CHECKSUM_ATTR (attrs.at_is_optional);
6311   CHECKSUM_ATTR (attrs.at_location);
6312   CHECKSUM_ATTR (attrs.at_lower_bound);
6313   CHECKSUM_ATTR (attrs.at_mutable);
6314   CHECKSUM_ATTR (attrs.at_ordering);
6315   CHECKSUM_ATTR (attrs.at_picture_string);
6316   CHECKSUM_ATTR (attrs.at_prototyped);
6317   CHECKSUM_ATTR (attrs.at_small);
6318   CHECKSUM_ATTR (attrs.at_segment);
6319   CHECKSUM_ATTR (attrs.at_string_length);
6320   CHECKSUM_ATTR (attrs.at_threads_scaled);
6321   CHECKSUM_ATTR (attrs.at_upper_bound);
6322   CHECKSUM_ATTR (attrs.at_use_location);
6323   CHECKSUM_ATTR (attrs.at_use_UTF8);
6324   CHECKSUM_ATTR (attrs.at_variable_parameter);
6325   CHECKSUM_ATTR (attrs.at_virtuality);
6326   CHECKSUM_ATTR (attrs.at_visibility);
6327   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6328   CHECKSUM_ATTR (attrs.at_type);
6329   CHECKSUM_ATTR (attrs.at_friend);
6330
6331   /* Checksum the child DIEs, except for nested types and member functions.  */
6332   c = die->die_child;
6333   if (c) do {
6334     dw_attr_ref name_attr;
6335
6336     c = c->die_sib;
6337     name_attr = get_AT (c, DW_AT_name);
6338     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6339         && name_attr != NULL)
6340       {
6341         CHECKSUM_ULEB128 ('S');
6342         CHECKSUM_ULEB128 (c->die_tag);
6343         CHECKSUM_STRING (AT_string (name_attr));
6344       }
6345     else
6346       {
6347         /* Mark this DIE so it gets processed when unmarking.  */
6348         if (c->die_mark == 0)
6349           c->die_mark = -1;
6350         die_checksum_ordered (c, ctx, mark);
6351       }
6352   } while (c != die->die_child);
6353
6354   CHECKSUM_ULEB128 (0);
6355 }
6356
6357 #undef CHECKSUM
6358 #undef CHECKSUM_STRING
6359 #undef CHECKSUM_ATTR
6360 #undef CHECKSUM_LEB128
6361 #undef CHECKSUM_ULEB128
6362
6363 /* Generate the type signature for DIE.  This is computed by generating an
6364    MD5 checksum over the DIE's tag, its relevant attributes, and its
6365    children.  Attributes that are references to other DIEs are processed
6366    by recursion, using the MARK field to prevent infinite recursion.
6367    If the DIE is nested inside a namespace or another type, we also
6368    need to include that context in the signature.  The lower 64 bits
6369    of the resulting MD5 checksum comprise the signature.  */
6370
6371 static void
6372 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6373 {
6374   int mark;
6375   const char *name;
6376   unsigned char checksum[16];
6377   struct md5_ctx ctx;
6378   dw_die_ref decl;
6379
6380   name = get_AT_string (die, DW_AT_name);
6381   decl = get_AT_ref (die, DW_AT_specification);
6382
6383   /* First, compute a signature for just the type name (and its surrounding
6384      context, if any.  This is stored in the type unit DIE for link-time
6385      ODR (one-definition rule) checking.  */
6386
6387   if (is_cxx() && name != NULL)
6388     {
6389       md5_init_ctx (&ctx);
6390
6391       /* Checksum the names of surrounding namespaces and structures.  */
6392       if (decl != NULL && decl->die_parent != NULL)
6393         checksum_die_context (decl->die_parent, &ctx);
6394
6395       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6396       md5_process_bytes (name, strlen (name) + 1, &ctx);
6397       md5_finish_ctx (&ctx, checksum);
6398
6399       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6400     }
6401
6402   /* Next, compute the complete type signature.  */
6403
6404   md5_init_ctx (&ctx);
6405   mark = 1;
6406   die->die_mark = mark;
6407
6408   /* Checksum the names of surrounding namespaces and structures.  */
6409   if (decl != NULL && decl->die_parent != NULL)
6410     checksum_die_context (decl->die_parent, &ctx);
6411
6412   /* Checksum the DIE and its children.  */
6413   die_checksum_ordered (die, &ctx, &mark);
6414   unmark_all_dies (die);
6415   md5_finish_ctx (&ctx, checksum);
6416
6417   /* Store the signature in the type node and link the type DIE and the
6418      type node together.  */
6419   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6420           DWARF_TYPE_SIGNATURE_SIZE);
6421   die->die_id.die_type_node = type_node;
6422   type_node->type_die = die;
6423
6424   /* If the DIE is a specification, link its declaration to the type node
6425      as well.  */
6426   if (decl != NULL)
6427     decl->die_id.die_type_node = type_node;
6428 }
6429
6430 /* Do the location expressions look same?  */
6431 static inline int
6432 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6433 {
6434   return loc1->dw_loc_opc == loc2->dw_loc_opc
6435          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6436          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6437 }
6438
6439 /* Do the values look the same?  */
6440 static int
6441 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6442 {
6443   dw_loc_descr_ref loc1, loc2;
6444   rtx r1, r2;
6445
6446   if (v1->val_class != v2->val_class)
6447     return 0;
6448
6449   switch (v1->val_class)
6450     {
6451     case dw_val_class_const:
6452       return v1->v.val_int == v2->v.val_int;
6453     case dw_val_class_unsigned_const:
6454       return v1->v.val_unsigned == v2->v.val_unsigned;
6455     case dw_val_class_const_double:
6456       return v1->v.val_double.high == v2->v.val_double.high
6457              && v1->v.val_double.low == v2->v.val_double.low;
6458     case dw_val_class_vec:
6459       if (v1->v.val_vec.length != v2->v.val_vec.length
6460           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6461         return 0;
6462       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6463                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6464         return 0;
6465       return 1;
6466     case dw_val_class_flag:
6467       return v1->v.val_flag == v2->v.val_flag;
6468     case dw_val_class_str:
6469       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6470
6471     case dw_val_class_addr:
6472       r1 = v1->v.val_addr;
6473       r2 = v2->v.val_addr;
6474       if (GET_CODE (r1) != GET_CODE (r2))
6475         return 0;
6476       return !rtx_equal_p (r1, r2);
6477
6478     case dw_val_class_offset:
6479       return v1->v.val_offset == v2->v.val_offset;
6480
6481     case dw_val_class_loc:
6482       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6483            loc1 && loc2;
6484            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6485         if (!same_loc_p (loc1, loc2, mark))
6486           return 0;
6487       return !loc1 && !loc2;
6488
6489     case dw_val_class_die_ref:
6490       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6491
6492     case dw_val_class_fde_ref:
6493     case dw_val_class_vms_delta:
6494     case dw_val_class_lbl_id:
6495     case dw_val_class_lineptr:
6496     case dw_val_class_macptr:
6497       return 1;
6498
6499     case dw_val_class_file:
6500       return v1->v.val_file == v2->v.val_file;
6501
6502     case dw_val_class_data8:
6503       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6504
6505     default:
6506       return 1;
6507     }
6508 }
6509
6510 /* Do the attributes look the same?  */
6511
6512 static int
6513 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6514 {
6515   if (at1->dw_attr != at2->dw_attr)
6516     return 0;
6517
6518   /* We don't care that this was compiled with a different compiler
6519      snapshot; if the output is the same, that's what matters. */
6520   if (at1->dw_attr == DW_AT_producer)
6521     return 1;
6522
6523   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6524 }
6525
6526 /* Do the dies look the same?  */
6527
6528 static int
6529 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6530 {
6531   dw_die_ref c1, c2;
6532   dw_attr_ref a1;
6533   unsigned ix;
6534
6535   /* To avoid infinite recursion.  */
6536   if (die1->die_mark)
6537     return die1->die_mark == die2->die_mark;
6538   die1->die_mark = die2->die_mark = ++(*mark);
6539
6540   if (die1->die_tag != die2->die_tag)
6541     return 0;
6542
6543   if (VEC_length (dw_attr_node, die1->die_attr)
6544       != VEC_length (dw_attr_node, die2->die_attr))
6545     return 0;
6546
6547   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6548     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6549       return 0;
6550
6551   c1 = die1->die_child;
6552   c2 = die2->die_child;
6553   if (! c1)
6554     {
6555       if (c2)
6556         return 0;
6557     }
6558   else
6559     for (;;)
6560       {
6561         if (!same_die_p (c1, c2, mark))
6562           return 0;
6563         c1 = c1->die_sib;
6564         c2 = c2->die_sib;
6565         if (c1 == die1->die_child)
6566           {
6567             if (c2 == die2->die_child)
6568               break;
6569             else
6570               return 0;
6571           }
6572     }
6573
6574   return 1;
6575 }
6576
6577 /* Do the dies look the same?  Wrapper around same_die_p.  */
6578
6579 static int
6580 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6581 {
6582   int mark = 0;
6583   int ret = same_die_p (die1, die2, &mark);
6584
6585   unmark_all_dies (die1);
6586   unmark_all_dies (die2);
6587
6588   return ret;
6589 }
6590
6591 /* The prefix to attach to symbols on DIEs in the current comdat debug
6592    info section.  */
6593 static char *comdat_symbol_id;
6594
6595 /* The index of the current symbol within the current comdat CU.  */
6596 static unsigned int comdat_symbol_number;
6597
6598 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6599    children, and set comdat_symbol_id accordingly.  */
6600
6601 static void
6602 compute_section_prefix (dw_die_ref unit_die)
6603 {
6604   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6605   const char *base = die_name ? lbasename (die_name) : "anonymous";
6606   char *name = XALLOCAVEC (char, strlen (base) + 64);
6607   char *p;
6608   int i, mark;
6609   unsigned char checksum[16];
6610   struct md5_ctx ctx;
6611
6612   /* Compute the checksum of the DIE, then append part of it as hex digits to
6613      the name filename of the unit.  */
6614
6615   md5_init_ctx (&ctx);
6616   mark = 0;
6617   die_checksum (unit_die, &ctx, &mark);
6618   unmark_all_dies (unit_die);
6619   md5_finish_ctx (&ctx, checksum);
6620
6621   sprintf (name, "%s.", base);
6622   clean_symbol_name (name);
6623
6624   p = name + strlen (name);
6625   for (i = 0; i < 4; i++)
6626     {
6627       sprintf (p, "%.2x", checksum[i]);
6628       p += 2;
6629     }
6630
6631   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6632   comdat_symbol_number = 0;
6633 }
6634
6635 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6636
6637 static int
6638 is_type_die (dw_die_ref die)
6639 {
6640   switch (die->die_tag)
6641     {
6642     case DW_TAG_array_type:
6643     case DW_TAG_class_type:
6644     case DW_TAG_interface_type:
6645     case DW_TAG_enumeration_type:
6646     case DW_TAG_pointer_type:
6647     case DW_TAG_reference_type:
6648     case DW_TAG_rvalue_reference_type:
6649     case DW_TAG_string_type:
6650     case DW_TAG_structure_type:
6651     case DW_TAG_subroutine_type:
6652     case DW_TAG_union_type:
6653     case DW_TAG_ptr_to_member_type:
6654     case DW_TAG_set_type:
6655     case DW_TAG_subrange_type:
6656     case DW_TAG_base_type:
6657     case DW_TAG_const_type:
6658     case DW_TAG_file_type:
6659     case DW_TAG_packed_type:
6660     case DW_TAG_volatile_type:
6661     case DW_TAG_typedef:
6662       return 1;
6663     default:
6664       return 0;
6665     }
6666 }
6667
6668 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6669    Basically, we want to choose the bits that are likely to be shared between
6670    compilations (types) and leave out the bits that are specific to individual
6671    compilations (functions).  */
6672
6673 static int
6674 is_comdat_die (dw_die_ref c)
6675 {
6676   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6677      we do for stabs.  The advantage is a greater likelihood of sharing between
6678      objects that don't include headers in the same order (and therefore would
6679      put the base types in a different comdat).  jason 8/28/00 */
6680
6681   if (c->die_tag == DW_TAG_base_type)
6682     return 0;
6683
6684   if (c->die_tag == DW_TAG_pointer_type
6685       || c->die_tag == DW_TAG_reference_type
6686       || c->die_tag == DW_TAG_rvalue_reference_type
6687       || c->die_tag == DW_TAG_const_type
6688       || c->die_tag == DW_TAG_volatile_type)
6689     {
6690       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6691
6692       return t ? is_comdat_die (t) : 0;
6693     }
6694
6695   return is_type_die (c);
6696 }
6697
6698 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6699    compilation unit.  */
6700
6701 static int
6702 is_symbol_die (dw_die_ref c)
6703 {
6704   return (is_type_die (c)
6705           || is_declaration_die (c)
6706           || c->die_tag == DW_TAG_namespace
6707           || c->die_tag == DW_TAG_module);
6708 }
6709
6710 /* Returns true iff C is a compile-unit DIE.  */
6711
6712 static inline bool
6713 is_cu_die (dw_die_ref c)
6714 {
6715   return c && c->die_tag == DW_TAG_compile_unit;
6716 }
6717
6718 static char *
6719 gen_internal_sym (const char *prefix)
6720 {
6721   char buf[256];
6722
6723   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6724   return xstrdup (buf);
6725 }
6726
6727 /* Assign symbols to all worthy DIEs under DIE.  */
6728
6729 static void
6730 assign_symbol_names (dw_die_ref die)
6731 {
6732   dw_die_ref c;
6733
6734   if (is_symbol_die (die))
6735     {
6736       if (comdat_symbol_id)
6737         {
6738           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6739
6740           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6741                    comdat_symbol_id, comdat_symbol_number++);
6742           die->die_id.die_symbol = xstrdup (p);
6743         }
6744       else
6745         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6746     }
6747
6748   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6749 }
6750
6751 struct cu_hash_table_entry
6752 {
6753   dw_die_ref cu;
6754   unsigned min_comdat_num, max_comdat_num;
6755   struct cu_hash_table_entry *next;
6756 };
6757
6758 /* Routines to manipulate hash table of CUs.  */
6759 static hashval_t
6760 htab_cu_hash (const void *of)
6761 {
6762   const struct cu_hash_table_entry *const entry =
6763     (const struct cu_hash_table_entry *) of;
6764
6765   return htab_hash_string (entry->cu->die_id.die_symbol);
6766 }
6767
6768 static int
6769 htab_cu_eq (const void *of1, const void *of2)
6770 {
6771   const struct cu_hash_table_entry *const entry1 =
6772     (const struct cu_hash_table_entry *) of1;
6773   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6774
6775   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6776 }
6777
6778 static void
6779 htab_cu_del (void *what)
6780 {
6781   struct cu_hash_table_entry *next,
6782     *entry = (struct cu_hash_table_entry *) what;
6783
6784   while (entry)
6785     {
6786       next = entry->next;
6787       free (entry);
6788       entry = next;
6789     }
6790 }
6791
6792 /* Check whether we have already seen this CU and set up SYM_NUM
6793    accordingly.  */
6794 static int
6795 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6796 {
6797   struct cu_hash_table_entry dummy;
6798   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6799
6800   dummy.max_comdat_num = 0;
6801
6802   slot = (struct cu_hash_table_entry **)
6803     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6804         INSERT);
6805   entry = *slot;
6806
6807   for (; entry; last = entry, entry = entry->next)
6808     {
6809       if (same_die_p_wrap (cu, entry->cu))
6810         break;
6811     }
6812
6813   if (entry)
6814     {
6815       *sym_num = entry->min_comdat_num;
6816       return 1;
6817     }
6818
6819   entry = XCNEW (struct cu_hash_table_entry);
6820   entry->cu = cu;
6821   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6822   entry->next = *slot;
6823   *slot = entry;
6824
6825   return 0;
6826 }
6827
6828 /* Record SYM_NUM to record of CU in HTABLE.  */
6829 static void
6830 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6831 {
6832   struct cu_hash_table_entry **slot, *entry;
6833
6834   slot = (struct cu_hash_table_entry **)
6835     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6836         NO_INSERT);
6837   entry = *slot;
6838
6839   entry->max_comdat_num = sym_num;
6840 }
6841
6842 /* Traverse the DIE (which is always comp_unit_die), and set up
6843    additional compilation units for each of the include files we see
6844    bracketed by BINCL/EINCL.  */
6845
6846 static void
6847 break_out_includes (dw_die_ref die)
6848 {
6849   dw_die_ref c;
6850   dw_die_ref unit = NULL;
6851   limbo_die_node *node, **pnode;
6852   htab_t cu_hash_table;
6853
6854   c = die->die_child;
6855   if (c) do {
6856     dw_die_ref prev = c;
6857     c = c->die_sib;
6858     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6859            || (unit && is_comdat_die (c)))
6860       {
6861         dw_die_ref next = c->die_sib;
6862
6863         /* This DIE is for a secondary CU; remove it from the main one.  */
6864         remove_child_with_prev (c, prev);
6865
6866         if (c->die_tag == DW_TAG_GNU_BINCL)
6867           unit = push_new_compile_unit (unit, c);
6868         else if (c->die_tag == DW_TAG_GNU_EINCL)
6869           unit = pop_compile_unit (unit);
6870         else
6871           add_child_die (unit, c);
6872         c = next;
6873         if (c == die->die_child)
6874           break;
6875       }
6876   } while (c != die->die_child);
6877
6878 #if 0
6879   /* We can only use this in debugging, since the frontend doesn't check
6880      to make sure that we leave every include file we enter.  */
6881   gcc_assert (!unit);
6882 #endif
6883
6884   assign_symbol_names (die);
6885   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6886   for (node = limbo_die_list, pnode = &limbo_die_list;
6887        node;
6888        node = node->next)
6889     {
6890       int is_dupl;
6891
6892       compute_section_prefix (node->die);
6893       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6894                         &comdat_symbol_number);
6895       assign_symbol_names (node->die);
6896       if (is_dupl)
6897         *pnode = node->next;
6898       else
6899         {
6900           pnode = &node->next;
6901           record_comdat_symbol_number (node->die, cu_hash_table,
6902                 comdat_symbol_number);
6903         }
6904     }
6905   htab_delete (cu_hash_table);
6906 }
6907
6908 /* Return non-zero if this DIE is a declaration.  */
6909
6910 static int
6911 is_declaration_die (dw_die_ref die)
6912 {
6913   dw_attr_ref a;
6914   unsigned ix;
6915
6916   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6917     if (a->dw_attr == DW_AT_declaration)
6918       return 1;
6919
6920   return 0;
6921 }
6922
6923 /* Return non-zero if this DIE is nested inside a subprogram.  */
6924
6925 static int
6926 is_nested_in_subprogram (dw_die_ref die)
6927 {
6928   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6929
6930   if (decl == NULL)
6931     decl = die;
6932   return local_scope_p (decl);
6933 }
6934
6935 /* Return non-zero if this DIE contains a defining declaration of a
6936    subprogram.  */
6937
6938 static int
6939 contains_subprogram_definition (dw_die_ref die)
6940 {
6941   dw_die_ref c;
6942
6943   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6944     return 1;
6945   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6946   return 0;
6947 }
6948
6949 /* Return non-zero if this is a type DIE that should be moved to a
6950    COMDAT .debug_types section.  */
6951
6952 static int
6953 should_move_die_to_comdat (dw_die_ref die)
6954 {
6955   switch (die->die_tag)
6956     {
6957     case DW_TAG_class_type:
6958     case DW_TAG_structure_type:
6959     case DW_TAG_enumeration_type:
6960     case DW_TAG_union_type:
6961       /* Don't move declarations, inlined instances, or types nested in a
6962          subprogram.  */
6963       if (is_declaration_die (die)
6964           || get_AT (die, DW_AT_abstract_origin)
6965           || is_nested_in_subprogram (die))
6966         return 0;
6967       /* A type definition should never contain a subprogram definition.  */
6968       gcc_assert (!contains_subprogram_definition (die));
6969       return 1;
6970     case DW_TAG_array_type:
6971     case DW_TAG_interface_type:
6972     case DW_TAG_pointer_type:
6973     case DW_TAG_reference_type:
6974     case DW_TAG_rvalue_reference_type:
6975     case DW_TAG_string_type:
6976     case DW_TAG_subroutine_type:
6977     case DW_TAG_ptr_to_member_type:
6978     case DW_TAG_set_type:
6979     case DW_TAG_subrange_type:
6980     case DW_TAG_base_type:
6981     case DW_TAG_const_type:
6982     case DW_TAG_file_type:
6983     case DW_TAG_packed_type:
6984     case DW_TAG_volatile_type:
6985     case DW_TAG_typedef:
6986     default:
6987       return 0;
6988     }
6989 }
6990
6991 /* Make a clone of DIE.  */
6992
6993 static dw_die_ref
6994 clone_die (dw_die_ref die)
6995 {
6996   dw_die_ref clone;
6997   dw_attr_ref a;
6998   unsigned ix;
6999
7000   clone = ggc_alloc_cleared_die_node ();
7001   clone->die_tag = die->die_tag;
7002
7003   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7004     add_dwarf_attr (clone, a);
7005
7006   return clone;
7007 }
7008
7009 /* Make a clone of the tree rooted at DIE.  */
7010
7011 static dw_die_ref
7012 clone_tree (dw_die_ref die)
7013 {
7014   dw_die_ref c;
7015   dw_die_ref clone = clone_die (die);
7016
7017   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7018
7019   return clone;
7020 }
7021
7022 /* Make a clone of DIE as a declaration.  */
7023
7024 static dw_die_ref
7025 clone_as_declaration (dw_die_ref die)
7026 {
7027   dw_die_ref clone;
7028   dw_die_ref decl;
7029   dw_attr_ref a;
7030   unsigned ix;
7031
7032   /* If the DIE is already a declaration, just clone it.  */
7033   if (is_declaration_die (die))
7034     return clone_die (die);
7035
7036   /* If the DIE is a specification, just clone its declaration DIE.  */
7037   decl = get_AT_ref (die, DW_AT_specification);
7038   if (decl != NULL)
7039     return clone_die (decl);
7040
7041   clone = ggc_alloc_cleared_die_node ();
7042   clone->die_tag = die->die_tag;
7043
7044   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7045     {
7046       /* We don't want to copy over all attributes.
7047          For example we don't want DW_AT_byte_size because otherwise we will no
7048          longer have a declaration and GDB will treat it as a definition.  */
7049
7050       switch (a->dw_attr)
7051         {
7052         case DW_AT_artificial:
7053         case DW_AT_containing_type:
7054         case DW_AT_external:
7055         case DW_AT_name:
7056         case DW_AT_type:
7057         case DW_AT_virtuality:
7058         case DW_AT_linkage_name:
7059         case DW_AT_MIPS_linkage_name:
7060           add_dwarf_attr (clone, a);
7061           break;
7062         case DW_AT_byte_size:
7063         default:
7064           break;
7065         }
7066     }
7067
7068   if (die->die_id.die_type_node)
7069     add_AT_die_ref (clone, DW_AT_signature, die);
7070
7071   add_AT_flag (clone, DW_AT_declaration, 1);
7072   return clone;
7073 }
7074
7075 /* Copy the declaration context to the new type unit DIE.  This includes
7076    any surrounding namespace or type declarations.  If the DIE has an
7077    AT_specification attribute, it also includes attributes and children
7078    attached to the specification, and returns a pointer to the original
7079    parent of the declaration DIE.  Returns NULL otherwise.  */
7080
7081 static dw_die_ref
7082 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7083 {
7084   dw_die_ref decl;
7085   dw_die_ref new_decl;
7086   dw_die_ref orig_parent = NULL;
7087
7088   decl = get_AT_ref (die, DW_AT_specification);
7089   if (decl == NULL)
7090     decl = die;
7091   else
7092     {
7093       unsigned ix;
7094       dw_die_ref c;
7095       dw_attr_ref a;
7096
7097       /* The original DIE will be changed to a declaration, and must
7098          be moved to be a child of the original declaration DIE.  */
7099       orig_parent = decl->die_parent;
7100
7101       /* Copy the type node pointer from the new DIE to the original
7102          declaration DIE so we can forward references later.  */
7103       decl->die_id.die_type_node = die->die_id.die_type_node;
7104
7105       remove_AT (die, DW_AT_specification);
7106
7107       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7108         {
7109           if (a->dw_attr != DW_AT_name
7110               && a->dw_attr != DW_AT_declaration
7111               && a->dw_attr != DW_AT_external)
7112             add_dwarf_attr (die, a);
7113         }
7114
7115       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7116     }
7117
7118   if (decl->die_parent != NULL
7119       && decl->die_parent->die_tag != DW_TAG_compile_unit
7120       && decl->die_parent->die_tag != DW_TAG_type_unit)
7121     {
7122       new_decl = copy_ancestor_tree (unit, decl, NULL);
7123       if (new_decl != NULL)
7124         {
7125           remove_AT (new_decl, DW_AT_signature);
7126           add_AT_specification (die, new_decl);
7127         }
7128     }
7129
7130   return orig_parent;
7131 }
7132
7133 /* Generate the skeleton ancestor tree for the given NODE, then clone
7134    the DIE and add the clone into the tree.  */
7135
7136 static void
7137 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7138 {
7139   if (node->new_die != NULL)
7140     return;
7141
7142   node->new_die = clone_as_declaration (node->old_die);
7143
7144   if (node->parent != NULL)
7145     {
7146       generate_skeleton_ancestor_tree (node->parent);
7147       add_child_die (node->parent->new_die, node->new_die);
7148     }
7149 }
7150
7151 /* Generate a skeleton tree of DIEs containing any declarations that are
7152    found in the original tree.  We traverse the tree looking for declaration
7153    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7154
7155 static void
7156 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7157 {
7158   skeleton_chain_node node;
7159   dw_die_ref c;
7160   dw_die_ref first;
7161   dw_die_ref prev = NULL;
7162   dw_die_ref next = NULL;
7163
7164   node.parent = parent;
7165
7166   first = c = parent->old_die->die_child;
7167   if (c)
7168     next = c->die_sib;
7169   if (c) do {
7170     if (prev == NULL || prev->die_sib == c)
7171       prev = c;
7172     c = next;
7173     next = (c == first ? NULL : c->die_sib);
7174     node.old_die = c;
7175     node.new_die = NULL;
7176     if (is_declaration_die (c))
7177       {
7178         /* Clone the existing DIE, move the original to the skeleton
7179            tree (which is in the main CU), and put the clone, with
7180            all the original's children, where the original came from.  */
7181         dw_die_ref clone = clone_die (c);
7182         move_all_children (c, clone);
7183
7184         replace_child (c, clone, prev);
7185         generate_skeleton_ancestor_tree (parent);
7186         add_child_die (parent->new_die, c);
7187         node.new_die = c;
7188         c = clone;
7189       }
7190     generate_skeleton_bottom_up (&node);
7191   } while (next != NULL);
7192 }
7193
7194 /* Wrapper function for generate_skeleton_bottom_up.  */
7195
7196 static dw_die_ref
7197 generate_skeleton (dw_die_ref die)
7198 {
7199   skeleton_chain_node node;
7200
7201   node.old_die = die;
7202   node.new_die = NULL;
7203   node.parent = NULL;
7204
7205   /* If this type definition is nested inside another type,
7206      always leave at least a declaration in its place.  */
7207   if (die->die_parent != NULL && is_type_die (die->die_parent))
7208     node.new_die = clone_as_declaration (die);
7209
7210   generate_skeleton_bottom_up (&node);
7211   return node.new_die;
7212 }
7213
7214 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7215    declaration.  The original DIE is moved to a new compile unit so that
7216    existing references to it follow it to the new location.  If any of the
7217    original DIE's descendants is a declaration, we need to replace the
7218    original DIE with a skeleton tree and move the declarations back into the
7219    skeleton tree.  */
7220
7221 static dw_die_ref
7222 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7223                                        dw_die_ref prev)
7224 {
7225   dw_die_ref skeleton, orig_parent;
7226
7227   /* Copy the declaration context to the type unit DIE.  If the returned
7228      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7229      that DIE.  */
7230   orig_parent = copy_declaration_context (unit, child);
7231
7232   skeleton = generate_skeleton (child);
7233   if (skeleton == NULL)
7234     remove_child_with_prev (child, prev);
7235   else
7236     {
7237       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7238
7239       /* If the original DIE was a specification, we need to put
7240          the skeleton under the parent DIE of the declaration.
7241          This leaves the original declaration in the tree, but
7242          it will be pruned later since there are no longer any
7243          references to it.  */
7244       if (orig_parent != NULL)
7245         {
7246           remove_child_with_prev (child, prev);
7247           add_child_die (orig_parent, skeleton);
7248         }
7249       else
7250         replace_child (child, skeleton, prev);
7251     }
7252
7253   return skeleton;
7254 }
7255
7256 /* Traverse the DIE and set up additional .debug_types sections for each
7257    type worthy of being placed in a COMDAT section.  */
7258
7259 static void
7260 break_out_comdat_types (dw_die_ref die)
7261 {
7262   dw_die_ref c;
7263   dw_die_ref first;
7264   dw_die_ref prev = NULL;
7265   dw_die_ref next = NULL;
7266   dw_die_ref unit = NULL;
7267
7268   first = c = die->die_child;
7269   if (c)
7270     next = c->die_sib;
7271   if (c) do {
7272     if (prev == NULL || prev->die_sib == c)
7273       prev = c;
7274     c = next;
7275     next = (c == first ? NULL : c->die_sib);
7276     if (should_move_die_to_comdat (c))
7277       {
7278         dw_die_ref replacement;
7279         comdat_type_node_ref type_node;
7280
7281         /* Create a new type unit DIE as the root for the new tree, and
7282            add it to the list of comdat types.  */
7283         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7284         add_AT_unsigned (unit, DW_AT_language,
7285                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7286         type_node = ggc_alloc_cleared_comdat_type_node ();
7287         type_node->root_die = unit;
7288         type_node->next = comdat_type_list;
7289         comdat_type_list = type_node;
7290
7291         /* Generate the type signature.  */
7292         generate_type_signature (c, type_node);
7293
7294         /* Copy the declaration context, attributes, and children of the
7295            declaration into the new type unit DIE, then remove this DIE
7296            from the main CU (or replace it with a skeleton if necessary).  */
7297         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7298
7299         /* Break out nested types into their own type units.  */
7300         break_out_comdat_types (c);
7301
7302         /* Add the DIE to the new compunit.  */
7303         add_child_die (unit, c);
7304
7305         if (replacement != NULL)
7306           c = replacement;
7307       }
7308     else if (c->die_tag == DW_TAG_namespace
7309              || c->die_tag == DW_TAG_class_type
7310              || c->die_tag == DW_TAG_structure_type
7311              || c->die_tag == DW_TAG_union_type)
7312       {
7313         /* Look for nested types that can be broken out.  */
7314         break_out_comdat_types (c);
7315       }
7316   } while (next != NULL);
7317 }
7318
7319 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7320
7321 struct decl_table_entry
7322 {
7323   dw_die_ref orig;
7324   dw_die_ref copy;
7325 };
7326
7327 /* Routines to manipulate hash table of copied declarations.  */
7328
7329 static hashval_t
7330 htab_decl_hash (const void *of)
7331 {
7332   const struct decl_table_entry *const entry =
7333     (const struct decl_table_entry *) of;
7334
7335   return htab_hash_pointer (entry->orig);
7336 }
7337
7338 static int
7339 htab_decl_eq (const void *of1, const void *of2)
7340 {
7341   const struct decl_table_entry *const entry1 =
7342     (const struct decl_table_entry *) of1;
7343   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7344
7345   return entry1->orig == entry2;
7346 }
7347
7348 static void
7349 htab_decl_del (void *what)
7350 {
7351   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7352
7353   free (entry);
7354 }
7355
7356 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7357    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7358    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7359    to check if the ancestor has already been copied into UNIT.  */
7360
7361 static dw_die_ref
7362 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7363 {
7364   dw_die_ref parent = die->die_parent;
7365   dw_die_ref new_parent = unit;
7366   dw_die_ref copy;
7367   void **slot = NULL;
7368   struct decl_table_entry *entry = NULL;
7369
7370   if (decl_table)
7371     {
7372       /* Check if the entry has already been copied to UNIT.  */
7373       slot = htab_find_slot_with_hash (decl_table, die,
7374                                        htab_hash_pointer (die), INSERT);
7375       if (*slot != HTAB_EMPTY_ENTRY)
7376         {
7377           entry = (struct decl_table_entry *) *slot;
7378           return entry->copy;
7379         }
7380
7381       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7382       entry = XCNEW (struct decl_table_entry);
7383       entry->orig = die;
7384       entry->copy = NULL;
7385       *slot = entry;
7386     }
7387
7388   if (parent != NULL)
7389     {
7390       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7391       if (spec != NULL)
7392         parent = spec;
7393       if (parent->die_tag != DW_TAG_compile_unit
7394           && parent->die_tag != DW_TAG_type_unit)
7395         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7396     }
7397
7398   copy = clone_as_declaration (die);
7399   add_child_die (new_parent, copy);
7400
7401   if (decl_table != NULL)
7402     {
7403       /* Record the pointer to the copy.  */
7404       entry->copy = copy;
7405     }
7406
7407   return copy;
7408 }
7409
7410 /* Walk the DIE and its children, looking for references to incomplete
7411    or trivial types that are unmarked (i.e., that are not in the current
7412    type_unit).  */
7413
7414 static void
7415 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7416 {
7417   dw_die_ref c;
7418   dw_attr_ref a;
7419   unsigned ix;
7420
7421   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7422     {
7423       if (AT_class (a) == dw_val_class_die_ref)
7424         {
7425           dw_die_ref targ = AT_ref (a);
7426           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7427           void **slot;
7428           struct decl_table_entry *entry;
7429
7430           if (targ->die_mark != 0 || type_node != NULL)
7431             continue;
7432
7433           slot = htab_find_slot_with_hash (decl_table, targ,
7434                                            htab_hash_pointer (targ), INSERT);
7435
7436           if (*slot != HTAB_EMPTY_ENTRY)
7437             {
7438               /* TARG has already been copied, so we just need to
7439                  modify the reference to point to the copy.  */
7440               entry = (struct decl_table_entry *) *slot;
7441               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7442             }
7443           else
7444             {
7445               dw_die_ref parent = unit;
7446               dw_die_ref copy = clone_tree (targ);
7447
7448               /* Make sure the cloned tree is marked as part of the
7449                  type unit.  */
7450               mark_dies (copy);
7451
7452               /* Record in DECL_TABLE that TARG has been copied.
7453                  Need to do this now, before the recursive call,
7454                  because DECL_TABLE may be expanded and SLOT
7455                  would no longer be a valid pointer.  */
7456               entry = XCNEW (struct decl_table_entry);
7457               entry->orig = targ;
7458               entry->copy = copy;
7459               *slot = entry;
7460
7461               /* If TARG has surrounding context, copy its ancestor tree
7462                  into the new type unit.  */
7463               if (targ->die_parent != NULL
7464                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7465                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7466                 parent = copy_ancestor_tree (unit, targ->die_parent,
7467                                              decl_table);
7468
7469               add_child_die (parent, copy);
7470               a->dw_attr_val.v.val_die_ref.die = copy;
7471
7472               /* Make sure the newly-copied DIE is walked.  If it was
7473                  installed in a previously-added context, it won't
7474                  get visited otherwise.  */
7475               if (parent != unit)
7476                 {
7477                   /* Find the highest point of the newly-added tree,
7478                      mark each node along the way, and walk from there.  */
7479                   parent->die_mark = 1;
7480                   while (parent->die_parent
7481                          && parent->die_parent->die_mark == 0)
7482                     {
7483                       parent = parent->die_parent;
7484                       parent->die_mark = 1;
7485                     }
7486                   copy_decls_walk (unit, parent, decl_table);
7487                 }
7488             }
7489         }
7490     }
7491
7492   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7493 }
7494
7495 /* Copy declarations for "unworthy" types into the new comdat section.
7496    Incomplete types, modified types, and certain other types aren't broken
7497    out into comdat sections of their own, so they don't have a signature,
7498    and we need to copy the declaration into the same section so that we
7499    don't have an external reference.  */
7500
7501 static void
7502 copy_decls_for_unworthy_types (dw_die_ref unit)
7503 {
7504   htab_t decl_table;
7505
7506   mark_dies (unit);
7507   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7508   copy_decls_walk (unit, unit, decl_table);
7509   htab_delete (decl_table);
7510   unmark_dies (unit);
7511 }
7512
7513 /* Traverse the DIE and add a sibling attribute if it may have the
7514    effect of speeding up access to siblings.  To save some space,
7515    avoid generating sibling attributes for DIE's without children.  */
7516
7517 static void
7518 add_sibling_attributes (dw_die_ref die)
7519 {
7520   dw_die_ref c;
7521
7522   if (! die->die_child)
7523     return;
7524
7525   if (die->die_parent && die != die->die_parent->die_child)
7526     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7527
7528   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7529 }
7530
7531 /* Output all location lists for the DIE and its children.  */
7532
7533 static void
7534 output_location_lists (dw_die_ref die)
7535 {
7536   dw_die_ref c;
7537   dw_attr_ref a;
7538   unsigned ix;
7539
7540   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7541     if (AT_class (a) == dw_val_class_loc_list)
7542       output_loc_list (AT_loc_list (a));
7543
7544   FOR_EACH_CHILD (die, c, output_location_lists (c));
7545 }
7546
7547 /* The format of each DIE (and its attribute value pairs) is encoded in an
7548    abbreviation table.  This routine builds the abbreviation table and assigns
7549    a unique abbreviation id for each abbreviation entry.  The children of each
7550    die are visited recursively.  */
7551
7552 static void
7553 build_abbrev_table (dw_die_ref die)
7554 {
7555   unsigned long abbrev_id;
7556   unsigned int n_alloc;
7557   dw_die_ref c;
7558   dw_attr_ref a;
7559   unsigned ix;
7560
7561   /* Scan the DIE references, and mark as external any that refer to
7562      DIEs from other CUs (i.e. those which are not marked).  */
7563   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7564     if (AT_class (a) == dw_val_class_die_ref
7565         && AT_ref (a)->die_mark == 0)
7566       {
7567         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7568         set_AT_ref_external (a, 1);
7569       }
7570
7571   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7572     {
7573       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7574       dw_attr_ref die_a, abbrev_a;
7575       unsigned ix;
7576       bool ok = true;
7577
7578       if (abbrev->die_tag != die->die_tag)
7579         continue;
7580       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7581         continue;
7582
7583       if (VEC_length (dw_attr_node, abbrev->die_attr)
7584           != VEC_length (dw_attr_node, die->die_attr))
7585         continue;
7586
7587       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7588         {
7589           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7590           if ((abbrev_a->dw_attr != die_a->dw_attr)
7591               || (value_format (abbrev_a) != value_format (die_a)))
7592             {
7593               ok = false;
7594               break;
7595             }
7596         }
7597       if (ok)
7598         break;
7599     }
7600
7601   if (abbrev_id >= abbrev_die_table_in_use)
7602     {
7603       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7604         {
7605           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7606           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7607                                             n_alloc);
7608
7609           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7610                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7611           abbrev_die_table_allocated = n_alloc;
7612         }
7613
7614       ++abbrev_die_table_in_use;
7615       abbrev_die_table[abbrev_id] = die;
7616     }
7617
7618   die->die_abbrev = abbrev_id;
7619   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7620 }
7621 \f
7622 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7623
7624 static int
7625 constant_size (unsigned HOST_WIDE_INT value)
7626 {
7627   int log;
7628
7629   if (value == 0)
7630     log = 0;
7631   else
7632     log = floor_log2 (value);
7633
7634   log = log / 8;
7635   log = 1 << (floor_log2 (log) + 1);
7636
7637   return log;
7638 }
7639
7640 /* Return the size of a DIE as it is represented in the
7641    .debug_info section.  */
7642
7643 static unsigned long
7644 size_of_die (dw_die_ref die)
7645 {
7646   unsigned long size = 0;
7647   dw_attr_ref a;
7648   unsigned ix;
7649
7650   size += size_of_uleb128 (die->die_abbrev);
7651   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7652     {
7653       switch (AT_class (a))
7654         {
7655         case dw_val_class_addr:
7656           size += DWARF2_ADDR_SIZE;
7657           break;
7658         case dw_val_class_offset:
7659           size += DWARF_OFFSET_SIZE;
7660           break;
7661         case dw_val_class_loc:
7662           {
7663             unsigned long lsize = size_of_locs (AT_loc (a));
7664
7665             /* Block length.  */
7666             if (dwarf_version >= 4)
7667               size += size_of_uleb128 (lsize);
7668             else
7669               size += constant_size (lsize);
7670             size += lsize;
7671           }
7672           break;
7673         case dw_val_class_loc_list:
7674           size += DWARF_OFFSET_SIZE;
7675           break;
7676         case dw_val_class_range_list:
7677           size += DWARF_OFFSET_SIZE;
7678           break;
7679         case dw_val_class_const:
7680           size += size_of_sleb128 (AT_int (a));
7681           break;
7682         case dw_val_class_unsigned_const:
7683           {
7684             int csize = constant_size (AT_unsigned (a));
7685             if (dwarf_version == 3
7686                 && a->dw_attr == DW_AT_data_member_location
7687                 && csize >= 4)
7688               size += size_of_uleb128 (AT_unsigned (a));
7689             else
7690               size += csize;
7691           }
7692           break;
7693         case dw_val_class_const_double:
7694           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7695           if (HOST_BITS_PER_WIDE_INT >= 64)
7696             size++; /* block */
7697           break;
7698         case dw_val_class_vec:
7699           size += constant_size (a->dw_attr_val.v.val_vec.length
7700                                  * a->dw_attr_val.v.val_vec.elt_size)
7701                   + a->dw_attr_val.v.val_vec.length
7702                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7703           break;
7704         case dw_val_class_flag:
7705           if (dwarf_version >= 4)
7706             /* Currently all add_AT_flag calls pass in 1 as last argument,
7707                so DW_FORM_flag_present can be used.  If that ever changes,
7708                we'll need to use DW_FORM_flag and have some optimization
7709                in build_abbrev_table that will change those to
7710                DW_FORM_flag_present if it is set to 1 in all DIEs using
7711                the same abbrev entry.  */
7712             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7713           else
7714             size += 1;
7715           break;
7716         case dw_val_class_die_ref:
7717           if (AT_ref_external (a))
7718             {
7719               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7720                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7721                  is sized by target address length, whereas in DWARF3
7722                  it's always sized as an offset.  */
7723               if (use_debug_types)
7724                 size += DWARF_TYPE_SIGNATURE_SIZE;
7725               else if (dwarf_version == 2)
7726                 size += DWARF2_ADDR_SIZE;
7727               else
7728                 size += DWARF_OFFSET_SIZE;
7729             }
7730           else
7731             size += DWARF_OFFSET_SIZE;
7732           break;
7733         case dw_val_class_fde_ref:
7734           size += DWARF_OFFSET_SIZE;
7735           break;
7736         case dw_val_class_lbl_id:
7737           size += DWARF2_ADDR_SIZE;
7738           break;
7739         case dw_val_class_lineptr:
7740         case dw_val_class_macptr:
7741           size += DWARF_OFFSET_SIZE;
7742           break;
7743         case dw_val_class_str:
7744           if (AT_string_form (a) == DW_FORM_strp)
7745             size += DWARF_OFFSET_SIZE;
7746           else
7747             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7748           break;
7749         case dw_val_class_file:
7750           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7751           break;
7752         case dw_val_class_data8:
7753           size += 8;
7754           break;
7755         case dw_val_class_vms_delta:
7756           size += DWARF_OFFSET_SIZE;
7757           break;
7758         default:
7759           gcc_unreachable ();
7760         }
7761     }
7762
7763   return size;
7764 }
7765
7766 /* Size the debugging information associated with a given DIE.  Visits the
7767    DIE's children recursively.  Updates the global variable next_die_offset, on
7768    each time through.  Uses the current value of next_die_offset to update the
7769    die_offset field in each DIE.  */
7770
7771 static void
7772 calc_die_sizes (dw_die_ref die)
7773 {
7774   dw_die_ref c;
7775
7776   gcc_assert (die->die_offset == 0
7777               || (unsigned long int) die->die_offset == next_die_offset);
7778   die->die_offset = next_die_offset;
7779   next_die_offset += size_of_die (die);
7780
7781   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7782
7783   if (die->die_child != NULL)
7784     /* Count the null byte used to terminate sibling lists.  */
7785     next_die_offset += 1;
7786 }
7787
7788 /* Size just the base type children at the start of the CU.
7789    This is needed because build_abbrev needs to size locs
7790    and sizing of type based stack ops needs to know die_offset
7791    values for the base types.  */
7792
7793 static void
7794 calc_base_type_die_sizes (void)
7795 {
7796   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7797   unsigned int i;
7798   dw_die_ref base_type;
7799 #if ENABLE_ASSERT_CHECKING
7800   dw_die_ref prev = comp_unit_die ()->die_child;
7801 #endif
7802
7803   die_offset += size_of_die (comp_unit_die ());
7804   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7805     {
7806 #if ENABLE_ASSERT_CHECKING
7807       gcc_assert (base_type->die_offset == 0
7808                   && prev->die_sib == base_type
7809                   && base_type->die_child == NULL
7810                   && base_type->die_abbrev);
7811       prev = base_type;
7812 #endif
7813       base_type->die_offset = die_offset;
7814       die_offset += size_of_die (base_type);
7815     }
7816 }
7817
7818 /* Set the marks for a die and its children.  We do this so
7819    that we know whether or not a reference needs to use FORM_ref_addr; only
7820    DIEs in the same CU will be marked.  We used to clear out the offset
7821    and use that as the flag, but ran into ordering problems.  */
7822
7823 static void
7824 mark_dies (dw_die_ref die)
7825 {
7826   dw_die_ref c;
7827
7828   gcc_assert (!die->die_mark);
7829
7830   die->die_mark = 1;
7831   FOR_EACH_CHILD (die, c, mark_dies (c));
7832 }
7833
7834 /* Clear the marks for a die and its children.  */
7835
7836 static void
7837 unmark_dies (dw_die_ref die)
7838 {
7839   dw_die_ref c;
7840
7841   if (! use_debug_types)
7842     gcc_assert (die->die_mark);
7843
7844   die->die_mark = 0;
7845   FOR_EACH_CHILD (die, c, unmark_dies (c));
7846 }
7847
7848 /* Clear the marks for a die, its children and referred dies.  */
7849
7850 static void
7851 unmark_all_dies (dw_die_ref die)
7852 {
7853   dw_die_ref c;
7854   dw_attr_ref a;
7855   unsigned ix;
7856
7857   if (!die->die_mark)
7858     return;
7859   die->die_mark = 0;
7860
7861   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7862
7863   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7864     if (AT_class (a) == dw_val_class_die_ref)
7865       unmark_all_dies (AT_ref (a));
7866 }
7867
7868 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7869    generated for the compilation unit.  */
7870
7871 static unsigned long
7872 size_of_pubnames (VEC (pubname_entry, gc) * names)
7873 {
7874   unsigned long size;
7875   unsigned i;
7876   pubname_ref p;
7877
7878   size = DWARF_PUBNAMES_HEADER_SIZE;
7879   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7880     if (names != pubtype_table
7881         || p->die->die_offset != 0
7882         || !flag_eliminate_unused_debug_types)
7883       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7884
7885   size += DWARF_OFFSET_SIZE;
7886   return size;
7887 }
7888
7889 /* Return the size of the information in the .debug_aranges section.  */
7890
7891 static unsigned long
7892 size_of_aranges (void)
7893 {
7894   unsigned long size;
7895
7896   size = DWARF_ARANGES_HEADER_SIZE;
7897
7898   /* Count the address/length pair for this compilation unit.  */
7899   if (text_section_used)
7900     size += 2 * DWARF2_ADDR_SIZE;
7901   if (cold_text_section_used)
7902     size += 2 * DWARF2_ADDR_SIZE;
7903   if (have_multiple_function_sections)
7904     {
7905       unsigned fde_idx;
7906       dw_fde_ref fde;
7907
7908       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7909         {
7910           if (!fde->in_std_section)
7911             size += 2 * DWARF2_ADDR_SIZE;
7912           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7913             size += 2 * DWARF2_ADDR_SIZE;
7914         }
7915     }
7916
7917   /* Count the two zero words used to terminated the address range table.  */
7918   size += 2 * DWARF2_ADDR_SIZE;
7919   return size;
7920 }
7921 \f
7922 /* Select the encoding of an attribute value.  */
7923
7924 static enum dwarf_form
7925 value_format (dw_attr_ref a)
7926 {
7927   switch (a->dw_attr_val.val_class)
7928     {
7929     case dw_val_class_addr:
7930       /* Only very few attributes allow DW_FORM_addr.  */
7931       switch (a->dw_attr)
7932         {
7933         case DW_AT_low_pc:
7934         case DW_AT_high_pc:
7935         case DW_AT_entry_pc:
7936         case DW_AT_trampoline:
7937           return DW_FORM_addr;
7938         default:
7939           break;
7940         }
7941       switch (DWARF2_ADDR_SIZE)
7942         {
7943         case 1:
7944           return DW_FORM_data1;
7945         case 2:
7946           return DW_FORM_data2;
7947         case 4:
7948           return DW_FORM_data4;
7949         case 8:
7950           return DW_FORM_data8;
7951         default:
7952           gcc_unreachable ();
7953         }
7954     case dw_val_class_range_list:
7955     case dw_val_class_loc_list:
7956       if (dwarf_version >= 4)
7957         return DW_FORM_sec_offset;
7958       /* FALLTHRU */
7959     case dw_val_class_vms_delta:
7960     case dw_val_class_offset:
7961       switch (DWARF_OFFSET_SIZE)
7962         {
7963         case 4:
7964           return DW_FORM_data4;
7965         case 8:
7966           return DW_FORM_data8;
7967         default:
7968           gcc_unreachable ();
7969         }
7970     case dw_val_class_loc:
7971       if (dwarf_version >= 4)
7972         return DW_FORM_exprloc;
7973       switch (constant_size (size_of_locs (AT_loc (a))))
7974         {
7975         case 1:
7976           return DW_FORM_block1;
7977         case 2:
7978           return DW_FORM_block2;
7979         default:
7980           gcc_unreachable ();
7981         }
7982     case dw_val_class_const:
7983       return DW_FORM_sdata;
7984     case dw_val_class_unsigned_const:
7985       switch (constant_size (AT_unsigned (a)))
7986         {
7987         case 1:
7988           return DW_FORM_data1;
7989         case 2:
7990           return DW_FORM_data2;
7991         case 4:
7992           /* In DWARF3 DW_AT_data_member_location with
7993              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7994              constant, so we need to use DW_FORM_udata if we need
7995              a large constant.  */
7996           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7997             return DW_FORM_udata;
7998           return DW_FORM_data4;
7999         case 8:
8000           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8001             return DW_FORM_udata;
8002           return DW_FORM_data8;
8003         default:
8004           gcc_unreachable ();
8005         }
8006     case dw_val_class_const_double:
8007       switch (HOST_BITS_PER_WIDE_INT)
8008         {
8009         case 8:
8010           return DW_FORM_data2;
8011         case 16:
8012           return DW_FORM_data4;
8013         case 32:
8014           return DW_FORM_data8;
8015         case 64:
8016         default:
8017           return DW_FORM_block1;
8018         }
8019     case dw_val_class_vec:
8020       switch (constant_size (a->dw_attr_val.v.val_vec.length
8021                              * a->dw_attr_val.v.val_vec.elt_size))
8022         {
8023         case 1:
8024           return DW_FORM_block1;
8025         case 2:
8026           return DW_FORM_block2;
8027         case 4:
8028           return DW_FORM_block4;
8029         default:
8030           gcc_unreachable ();
8031         }
8032     case dw_val_class_flag:
8033       if (dwarf_version >= 4)
8034         {
8035           /* Currently all add_AT_flag calls pass in 1 as last argument,
8036              so DW_FORM_flag_present can be used.  If that ever changes,
8037              we'll need to use DW_FORM_flag and have some optimization
8038              in build_abbrev_table that will change those to
8039              DW_FORM_flag_present if it is set to 1 in all DIEs using
8040              the same abbrev entry.  */
8041           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8042           return DW_FORM_flag_present;
8043         }
8044       return DW_FORM_flag;
8045     case dw_val_class_die_ref:
8046       if (AT_ref_external (a))
8047         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8048       else
8049         return DW_FORM_ref;
8050     case dw_val_class_fde_ref:
8051       return DW_FORM_data;
8052     case dw_val_class_lbl_id:
8053       return DW_FORM_addr;
8054     case dw_val_class_lineptr:
8055     case dw_val_class_macptr:
8056       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8057     case dw_val_class_str:
8058       return AT_string_form (a);
8059     case dw_val_class_file:
8060       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8061         {
8062         case 1:
8063           return DW_FORM_data1;
8064         case 2:
8065           return DW_FORM_data2;
8066         case 4:
8067           return DW_FORM_data4;
8068         default:
8069           gcc_unreachable ();
8070         }
8071
8072     case dw_val_class_data8:
8073       return DW_FORM_data8;
8074
8075     default:
8076       gcc_unreachable ();
8077     }
8078 }
8079
8080 /* Output the encoding of an attribute value.  */
8081
8082 static void
8083 output_value_format (dw_attr_ref a)
8084 {
8085   enum dwarf_form form = value_format (a);
8086
8087   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8088 }
8089
8090 /* Output the .debug_abbrev section which defines the DIE abbreviation
8091    table.  */
8092
8093 static void
8094 output_abbrev_section (void)
8095 {
8096   unsigned long abbrev_id;
8097
8098   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8099     {
8100       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8101       unsigned ix;
8102       dw_attr_ref a_attr;
8103
8104       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8105       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8106                                    dwarf_tag_name (abbrev->die_tag));
8107
8108       if (abbrev->die_child != NULL)
8109         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8110       else
8111         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8112
8113       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8114            ix++)
8115         {
8116           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8117                                        dwarf_attr_name (a_attr->dw_attr));
8118           output_value_format (a_attr);
8119         }
8120
8121       dw2_asm_output_data (1, 0, NULL);
8122       dw2_asm_output_data (1, 0, NULL);
8123     }
8124
8125   /* Terminate the table.  */
8126   dw2_asm_output_data (1, 0, NULL);
8127 }
8128
8129 /* Output a symbol we can use to refer to this DIE from another CU.  */
8130
8131 static inline void
8132 output_die_symbol (dw_die_ref die)
8133 {
8134   char *sym = die->die_id.die_symbol;
8135
8136   if (sym == 0)
8137     return;
8138
8139   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8140     /* We make these global, not weak; if the target doesn't support
8141        .linkonce, it doesn't support combining the sections, so debugging
8142        will break.  */
8143     targetm.asm_out.globalize_label (asm_out_file, sym);
8144
8145   ASM_OUTPUT_LABEL (asm_out_file, sym);
8146 }
8147
8148 /* Return a new location list, given the begin and end range, and the
8149    expression.  */
8150
8151 static inline dw_loc_list_ref
8152 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8153               const char *section)
8154 {
8155   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8156
8157   retlist->begin = begin;
8158   retlist->end = end;
8159   retlist->expr = expr;
8160   retlist->section = section;
8161
8162   return retlist;
8163 }
8164
8165 /* Generate a new internal symbol for this location list node, if it
8166    hasn't got one yet.  */
8167
8168 static inline void
8169 gen_llsym (dw_loc_list_ref list)
8170 {
8171   gcc_assert (!list->ll_symbol);
8172   list->ll_symbol = gen_internal_sym ("LLST");
8173 }
8174
8175 /* Output the location list given to us.  */
8176
8177 static void
8178 output_loc_list (dw_loc_list_ref list_head)
8179 {
8180   dw_loc_list_ref curr = list_head;
8181
8182   if (list_head->emitted)
8183     return;
8184   list_head->emitted = true;
8185
8186   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8187
8188   /* Walk the location list, and output each range + expression.  */
8189   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8190     {
8191       unsigned long size;
8192       /* Don't output an entry that starts and ends at the same address.  */
8193       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8194         continue;
8195       size = size_of_locs (curr->expr);
8196       /* If the expression is too large, drop it on the floor.  We could
8197          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8198          in the expression, but >= 64KB expressions for a single value
8199          in a single range are unlikely very useful.  */
8200       if (size > 0xffff)
8201         continue;
8202       if (!have_multiple_function_sections)
8203         {
8204           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8205                                 "Location list begin address (%s)",
8206                                 list_head->ll_symbol);
8207           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8208                                 "Location list end address (%s)",
8209                                 list_head->ll_symbol);
8210         }
8211       else
8212         {
8213           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8214                                "Location list begin address (%s)",
8215                                list_head->ll_symbol);
8216           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8217                                "Location list end address (%s)",
8218                                list_head->ll_symbol);
8219         }
8220
8221       /* Output the block length for this list of location operations.  */
8222       gcc_assert (size <= 0xffff);
8223       dw2_asm_output_data (2, size, "%s", "Location expression size");
8224
8225       output_loc_sequence (curr->expr, -1);
8226     }
8227
8228   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8229                        "Location list terminator begin (%s)",
8230                        list_head->ll_symbol);
8231   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8232                        "Location list terminator end (%s)",
8233                        list_head->ll_symbol);
8234 }
8235
8236 /* Output a type signature.  */
8237
8238 static inline void
8239 output_signature (const char *sig, const char *name)
8240 {
8241   int i;
8242
8243   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8244     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8245 }
8246
8247 /* Output the DIE and its attributes.  Called recursively to generate
8248    the definitions of each child DIE.  */
8249
8250 static void
8251 output_die (dw_die_ref die)
8252 {
8253   dw_attr_ref a;
8254   dw_die_ref c;
8255   unsigned long size;
8256   unsigned ix;
8257
8258   /* If someone in another CU might refer to us, set up a symbol for
8259      them to point to.  */
8260   if (! use_debug_types && die->die_id.die_symbol)
8261     output_die_symbol (die);
8262
8263   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8264                                (unsigned long)die->die_offset,
8265                                dwarf_tag_name (die->die_tag));
8266
8267   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8268     {
8269       const char *name = dwarf_attr_name (a->dw_attr);
8270
8271       switch (AT_class (a))
8272         {
8273         case dw_val_class_addr:
8274           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8275           break;
8276
8277         case dw_val_class_offset:
8278           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8279                                "%s", name);
8280           break;
8281
8282         case dw_val_class_range_list:
8283           {
8284             char *p = strchr (ranges_section_label, '\0');
8285
8286             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8287                      a->dw_attr_val.v.val_offset);
8288             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8289                                    debug_ranges_section, "%s", name);
8290             *p = '\0';
8291           }
8292           break;
8293
8294         case dw_val_class_loc:
8295           size = size_of_locs (AT_loc (a));
8296
8297           /* Output the block length for this list of location operations.  */
8298           if (dwarf_version >= 4)
8299             dw2_asm_output_data_uleb128 (size, "%s", name);
8300           else
8301             dw2_asm_output_data (constant_size (size), size, "%s", name);
8302
8303           output_loc_sequence (AT_loc (a), -1);
8304           break;
8305
8306         case dw_val_class_const:
8307           /* ??? It would be slightly more efficient to use a scheme like is
8308              used for unsigned constants below, but gdb 4.x does not sign
8309              extend.  Gdb 5.x does sign extend.  */
8310           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8311           break;
8312
8313         case dw_val_class_unsigned_const:
8314           {
8315             int csize = constant_size (AT_unsigned (a));
8316             if (dwarf_version == 3
8317                 && a->dw_attr == DW_AT_data_member_location
8318                 && csize >= 4)
8319               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8320             else
8321               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8322           }
8323           break;
8324
8325         case dw_val_class_const_double:
8326           {
8327             unsigned HOST_WIDE_INT first, second;
8328
8329             if (HOST_BITS_PER_WIDE_INT >= 64)
8330               dw2_asm_output_data (1,
8331                                    2 * HOST_BITS_PER_WIDE_INT
8332                                    / HOST_BITS_PER_CHAR,
8333                                    NULL);
8334
8335             if (WORDS_BIG_ENDIAN)
8336               {
8337                 first = a->dw_attr_val.v.val_double.high;
8338                 second = a->dw_attr_val.v.val_double.low;
8339               }
8340             else
8341               {
8342                 first = a->dw_attr_val.v.val_double.low;
8343                 second = a->dw_attr_val.v.val_double.high;
8344               }
8345
8346             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8347                                  first, name);
8348             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8349                                  second, NULL);
8350           }
8351           break;
8352
8353         case dw_val_class_vec:
8354           {
8355             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8356             unsigned int len = a->dw_attr_val.v.val_vec.length;
8357             unsigned int i;
8358             unsigned char *p;
8359
8360             dw2_asm_output_data (constant_size (len * elt_size),
8361                                  len * elt_size, "%s", name);
8362             if (elt_size > sizeof (HOST_WIDE_INT))
8363               {
8364                 elt_size /= 2;
8365                 len *= 2;
8366               }
8367             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8368                  i < len;
8369                  i++, p += elt_size)
8370               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8371                                    "fp or vector constant word %u", i);
8372             break;
8373           }
8374
8375         case dw_val_class_flag:
8376           if (dwarf_version >= 4)
8377             {
8378               /* Currently all add_AT_flag calls pass in 1 as last argument,
8379                  so DW_FORM_flag_present can be used.  If that ever changes,
8380                  we'll need to use DW_FORM_flag and have some optimization
8381                  in build_abbrev_table that will change those to
8382                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8383                  the same abbrev entry.  */
8384               gcc_assert (AT_flag (a) == 1);
8385               if (flag_debug_asm)
8386                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8387                          ASM_COMMENT_START, name);
8388               break;
8389             }
8390           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8391           break;
8392
8393         case dw_val_class_loc_list:
8394           {
8395             char *sym = AT_loc_list (a)->ll_symbol;
8396
8397             gcc_assert (sym);
8398             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8399                                    "%s", name);
8400           }
8401           break;
8402
8403         case dw_val_class_die_ref:
8404           if (AT_ref_external (a))
8405             {
8406               if (use_debug_types)
8407                 {
8408                   comdat_type_node_ref type_node =
8409                     AT_ref (a)->die_id.die_type_node;
8410
8411                   gcc_assert (type_node);
8412                   output_signature (type_node->signature, name);
8413                 }
8414               else
8415                 {
8416                   char *sym = AT_ref (a)->die_id.die_symbol;
8417                   int size;
8418
8419                   gcc_assert (sym);
8420                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8421                      length, whereas in DWARF3 it's always sized as an
8422                      offset.  */
8423                   if (dwarf_version == 2)
8424                     size = DWARF2_ADDR_SIZE;
8425                   else
8426                     size = DWARF_OFFSET_SIZE;
8427                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8428                                          name);
8429                 }
8430             }
8431           else
8432             {
8433               gcc_assert (AT_ref (a)->die_offset);
8434               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8435                                    "%s", name);
8436             }
8437           break;
8438
8439         case dw_val_class_fde_ref:
8440           {
8441             char l1[20];
8442
8443             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8444                                          a->dw_attr_val.v.val_fde_index * 2);
8445             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8446                                    "%s", name);
8447           }
8448           break;
8449
8450         case dw_val_class_vms_delta:
8451           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8452                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8453                                     "%s", name);
8454           break;
8455
8456         case dw_val_class_lbl_id:
8457           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8458           break;
8459
8460         case dw_val_class_lineptr:
8461           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8462                                  debug_line_section, "%s", name);
8463           break;
8464
8465         case dw_val_class_macptr:
8466           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8467                                  debug_macinfo_section, "%s", name);
8468           break;
8469
8470         case dw_val_class_str:
8471           if (AT_string_form (a) == DW_FORM_strp)
8472             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8473                                    a->dw_attr_val.v.val_str->label,
8474                                    debug_str_section,
8475                                    "%s: \"%s\"", name, AT_string (a));
8476           else
8477             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8478           break;
8479
8480         case dw_val_class_file:
8481           {
8482             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8483
8484             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8485                                  a->dw_attr_val.v.val_file->filename);
8486             break;
8487           }
8488
8489         case dw_val_class_data8:
8490           {
8491             int i;
8492
8493             for (i = 0; i < 8; i++)
8494               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8495                                    i == 0 ? "%s" : NULL, name);
8496             break;
8497           }
8498
8499         default:
8500           gcc_unreachable ();
8501         }
8502     }
8503
8504   FOR_EACH_CHILD (die, c, output_die (c));
8505
8506   /* Add null byte to terminate sibling list.  */
8507   if (die->die_child != NULL)
8508     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8509                          (unsigned long) die->die_offset);
8510 }
8511
8512 /* Output the compilation unit that appears at the beginning of the
8513    .debug_info section, and precedes the DIE descriptions.  */
8514
8515 static void
8516 output_compilation_unit_header (void)
8517 {
8518   int ver = dwarf_version;
8519
8520   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8521     dw2_asm_output_data (4, 0xffffffff,
8522       "Initial length escape value indicating 64-bit DWARF extension");
8523   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8524                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8525                        "Length of Compilation Unit Info");
8526   dw2_asm_output_data (2, ver, "DWARF version number");
8527   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8528                          debug_abbrev_section,
8529                          "Offset Into Abbrev. Section");
8530   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8531 }
8532
8533 /* Output the compilation unit DIE and its children.  */
8534
8535 static void
8536 output_comp_unit (dw_die_ref die, int output_if_empty)
8537 {
8538   const char *secname;
8539   char *oldsym, *tmp;
8540
8541   /* Unless we are outputting main CU, we may throw away empty ones.  */
8542   if (!output_if_empty && die->die_child == NULL)
8543     return;
8544
8545   /* Even if there are no children of this DIE, we must output the information
8546      about the compilation unit.  Otherwise, on an empty translation unit, we
8547      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8548      will then complain when examining the file.  First mark all the DIEs in
8549      this CU so we know which get local refs.  */
8550   mark_dies (die);
8551
8552   build_abbrev_table (die);
8553
8554   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8555   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8556   calc_die_sizes (die);
8557
8558   oldsym = die->die_id.die_symbol;
8559   if (oldsym)
8560     {
8561       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8562
8563       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8564       secname = tmp;
8565       die->die_id.die_symbol = NULL;
8566       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8567     }
8568   else
8569     {
8570       switch_to_section (debug_info_section);
8571       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8572       info_section_emitted = true;
8573     }
8574
8575   /* Output debugging information.  */
8576   output_compilation_unit_header ();
8577   output_die (die);
8578
8579   /* Leave the marks on the main CU, so we can check them in
8580      output_pubnames.  */
8581   if (oldsym)
8582     {
8583       unmark_dies (die);
8584       die->die_id.die_symbol = oldsym;
8585     }
8586 }
8587
8588 /* Output a comdat type unit DIE and its children.  */
8589
8590 static void
8591 output_comdat_type_unit (comdat_type_node *node)
8592 {
8593   const char *secname;
8594   char *tmp;
8595   int i;
8596 #if defined (OBJECT_FORMAT_ELF)
8597   tree comdat_key;
8598 #endif
8599
8600   /* First mark all the DIEs in this CU so we know which get local refs.  */
8601   mark_dies (node->root_die);
8602
8603   build_abbrev_table (node->root_die);
8604
8605   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8606   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8607   calc_die_sizes (node->root_die);
8608
8609 #if defined (OBJECT_FORMAT_ELF)
8610   secname = ".debug_types";
8611   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8612   sprintf (tmp, "wt.");
8613   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8614     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8615   comdat_key = get_identifier (tmp);
8616   targetm.asm_out.named_section (secname,
8617                                  SECTION_DEBUG | SECTION_LINKONCE,
8618                                  comdat_key);
8619 #else
8620   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8621   sprintf (tmp, ".gnu.linkonce.wt.");
8622   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8623     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8624   secname = tmp;
8625   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8626 #endif
8627
8628   /* Output debugging information.  */
8629   output_compilation_unit_header ();
8630   output_signature (node->signature, "Type Signature");
8631   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8632                        "Offset to Type DIE");
8633   output_die (node->root_die);
8634
8635   unmark_dies (node->root_die);
8636 }
8637
8638 /* Return the DWARF2/3 pubname associated with a decl.  */
8639
8640 static const char *
8641 dwarf2_name (tree decl, int scope)
8642 {
8643   if (DECL_NAMELESS (decl))
8644     return NULL;
8645   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8646 }
8647
8648 /* Add a new entry to .debug_pubnames if appropriate.  */
8649
8650 static void
8651 add_pubname_string (const char *str, dw_die_ref die)
8652 {
8653   if (targetm.want_debug_pub_sections)
8654     {
8655       pubname_entry e;
8656
8657       e.die = die;
8658       e.name = xstrdup (str);
8659       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8660     }
8661 }
8662
8663 static void
8664 add_pubname (tree decl, dw_die_ref die)
8665 {
8666   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8667     {
8668       const char *name = dwarf2_name (decl, 1);
8669       if (name)
8670         add_pubname_string (name, die);
8671     }
8672 }
8673
8674 /* Add a new entry to .debug_pubtypes if appropriate.  */
8675
8676 static void
8677 add_pubtype (tree decl, dw_die_ref die)
8678 {
8679   pubname_entry e;
8680
8681   if (!targetm.want_debug_pub_sections)
8682     return;
8683
8684   e.name = NULL;
8685   if ((TREE_PUBLIC (decl)
8686        || is_cu_die (die->die_parent))
8687       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8688     {
8689       e.die = die;
8690       if (TYPE_P (decl))
8691         {
8692           if (TYPE_NAME (decl))
8693             {
8694               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8695                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8696               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8697                        && DECL_NAME (TYPE_NAME (decl)))
8698                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8699               else
8700                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8701             }
8702         }
8703       else
8704         {
8705           e.name = dwarf2_name (decl, 1);
8706           if (e.name)
8707             e.name = xstrdup (e.name);
8708         }
8709
8710       /* If we don't have a name for the type, there's no point in adding
8711          it to the table.  */
8712       if (e.name && e.name[0] != '\0')
8713         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8714     }
8715 }
8716
8717 /* Output the public names table used to speed up access to externally
8718    visible names; or the public types table used to find type definitions.  */
8719
8720 static void
8721 output_pubnames (VEC (pubname_entry, gc) * names)
8722 {
8723   unsigned i;
8724   unsigned long pubnames_length = size_of_pubnames (names);
8725   pubname_ref pub;
8726
8727   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8728     dw2_asm_output_data (4, 0xffffffff,
8729       "Initial length escape value indicating 64-bit DWARF extension");
8730   if (names == pubname_table)
8731     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8732                          "Length of Public Names Info");
8733   else
8734     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8735                          "Length of Public Type Names Info");
8736   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8737   dw2_asm_output_data (2, 2, "DWARF Version");
8738   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8739                          debug_info_section,
8740                          "Offset of Compilation Unit Info");
8741   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8742                        "Compilation Unit Length");
8743
8744   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8745     {
8746       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8747       if (names == pubname_table)
8748         gcc_assert (pub->die->die_mark);
8749
8750       if (names != pubtype_table
8751           || pub->die->die_offset != 0
8752           || !flag_eliminate_unused_debug_types)
8753         {
8754           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8755                                "DIE offset");
8756
8757           dw2_asm_output_nstring (pub->name, -1, "external name");
8758         }
8759     }
8760
8761   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8762 }
8763
8764 /* Output the information that goes into the .debug_aranges table.
8765    Namely, define the beginning and ending address range of the
8766    text section generated for this compilation unit.  */
8767
8768 static void
8769 output_aranges (unsigned long aranges_length)
8770 {
8771   unsigned i;
8772
8773   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8774     dw2_asm_output_data (4, 0xffffffff,
8775       "Initial length escape value indicating 64-bit DWARF extension");
8776   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8777                        "Length of Address Ranges Info");
8778   /* Version number for aranges is still 2, even in DWARF3.  */
8779   dw2_asm_output_data (2, 2, "DWARF Version");
8780   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8781                          debug_info_section,
8782                          "Offset of Compilation Unit Info");
8783   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8784   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8785
8786   /* We need to align to twice the pointer size here.  */
8787   if (DWARF_ARANGES_PAD_SIZE)
8788     {
8789       /* Pad using a 2 byte words so that padding is correct for any
8790          pointer size.  */
8791       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8792                            2 * DWARF2_ADDR_SIZE);
8793       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8794         dw2_asm_output_data (2, 0, NULL);
8795     }
8796
8797   /* It is necessary not to output these entries if the sections were
8798      not used; if the sections were not used, the length will be 0 and
8799      the address may end up as 0 if the section is discarded by ld
8800      --gc-sections, leaving an invalid (0, 0) entry that can be
8801      confused with the terminator.  */
8802   if (text_section_used)
8803     {
8804       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8805       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8806                             text_section_label, "Length");
8807     }
8808   if (cold_text_section_used)
8809     {
8810       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8811                            "Address");
8812       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8813                             cold_text_section_label, "Length");
8814     }
8815
8816   if (have_multiple_function_sections)
8817     {
8818       unsigned fde_idx;
8819       dw_fde_ref fde;
8820
8821       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8822         {
8823           if (!fde->in_std_section)
8824             {
8825               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8826                                    "Address");
8827               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8828                                     fde->dw_fde_begin, "Length");
8829             }
8830           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8831             {
8832               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8833                                    "Address");
8834               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8835                                     fde->dw_fde_second_begin, "Length");
8836             }
8837         }
8838     }
8839
8840   /* Output the terminator words.  */
8841   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8842   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8843 }
8844
8845 /* Add a new entry to .debug_ranges.  Return the offset at which it
8846    was placed.  */
8847
8848 static unsigned int
8849 add_ranges_num (int num)
8850 {
8851   unsigned int in_use = ranges_table_in_use;
8852
8853   if (in_use == ranges_table_allocated)
8854     {
8855       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8856       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8857                                     ranges_table_allocated);
8858       memset (ranges_table + ranges_table_in_use, 0,
8859               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8860     }
8861
8862   ranges_table[in_use].num = num;
8863   ranges_table_in_use = in_use + 1;
8864
8865   return in_use * 2 * DWARF2_ADDR_SIZE;
8866 }
8867
8868 /* Add a new entry to .debug_ranges corresponding to a block, or a
8869    range terminator if BLOCK is NULL.  */
8870
8871 static unsigned int
8872 add_ranges (const_tree block)
8873 {
8874   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8875 }
8876
8877 /* Add a new entry to .debug_ranges corresponding to a pair of
8878    labels.  */
8879
8880 static void
8881 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8882                       bool *added)
8883 {
8884   unsigned int in_use = ranges_by_label_in_use;
8885   unsigned int offset;
8886
8887   if (in_use == ranges_by_label_allocated)
8888     {
8889       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8890       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8891                                        ranges_by_label,
8892                                        ranges_by_label_allocated);
8893       memset (ranges_by_label + ranges_by_label_in_use, 0,
8894               RANGES_TABLE_INCREMENT
8895               * sizeof (struct dw_ranges_by_label_struct));
8896     }
8897
8898   ranges_by_label[in_use].begin = begin;
8899   ranges_by_label[in_use].end = end;
8900   ranges_by_label_in_use = in_use + 1;
8901
8902   offset = add_ranges_num (-(int)in_use - 1);
8903   if (!*added)
8904     {
8905       add_AT_range_list (die, DW_AT_ranges, offset);
8906       *added = true;
8907     }
8908 }
8909
8910 static void
8911 output_ranges (void)
8912 {
8913   unsigned i;
8914   static const char *const start_fmt = "Offset %#x";
8915   const char *fmt = start_fmt;
8916
8917   for (i = 0; i < ranges_table_in_use; i++)
8918     {
8919       int block_num = ranges_table[i].num;
8920
8921       if (block_num > 0)
8922         {
8923           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8924           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8925
8926           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8927           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8928
8929           /* If all code is in the text section, then the compilation
8930              unit base address defaults to DW_AT_low_pc, which is the
8931              base of the text section.  */
8932           if (!have_multiple_function_sections)
8933             {
8934               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8935                                     text_section_label,
8936                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8937               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8938                                     text_section_label, NULL);
8939             }
8940
8941           /* Otherwise, the compilation unit base address is zero,
8942              which allows us to use absolute addresses, and not worry
8943              about whether the target supports cross-section
8944              arithmetic.  */
8945           else
8946             {
8947               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8948                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8949               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8950             }
8951
8952           fmt = NULL;
8953         }
8954
8955       /* Negative block_num stands for an index into ranges_by_label.  */
8956       else if (block_num < 0)
8957         {
8958           int lab_idx = - block_num - 1;
8959
8960           if (!have_multiple_function_sections)
8961             {
8962               gcc_unreachable ();
8963 #if 0
8964               /* If we ever use add_ranges_by_labels () for a single
8965                  function section, all we have to do is to take out
8966                  the #if 0 above.  */
8967               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8968                                     ranges_by_label[lab_idx].begin,
8969                                     text_section_label,
8970                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8971               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8972                                     ranges_by_label[lab_idx].end,
8973                                     text_section_label, NULL);
8974 #endif
8975             }
8976           else
8977             {
8978               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8979                                    ranges_by_label[lab_idx].begin,
8980                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8981               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8982                                    ranges_by_label[lab_idx].end,
8983                                    NULL);
8984             }
8985         }
8986       else
8987         {
8988           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8989           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8990           fmt = start_fmt;
8991         }
8992     }
8993 }
8994
8995 /* Data structure containing information about input files.  */
8996 struct file_info
8997 {
8998   const char *path;     /* Complete file name.  */
8999   const char *fname;    /* File name part.  */
9000   int length;           /* Length of entire string.  */
9001   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9002   int dir_idx;          /* Index in directory table.  */
9003 };
9004
9005 /* Data structure containing information about directories with source
9006    files.  */
9007 struct dir_info
9008 {
9009   const char *path;     /* Path including directory name.  */
9010   int length;           /* Path length.  */
9011   int prefix;           /* Index of directory entry which is a prefix.  */
9012   int count;            /* Number of files in this directory.  */
9013   int dir_idx;          /* Index of directory used as base.  */
9014 };
9015
9016 /* Callback function for file_info comparison.  We sort by looking at
9017    the directories in the path.  */
9018
9019 static int
9020 file_info_cmp (const void *p1, const void *p2)
9021 {
9022   const struct file_info *const s1 = (const struct file_info *) p1;
9023   const struct file_info *const s2 = (const struct file_info *) p2;
9024   const unsigned char *cp1;
9025   const unsigned char *cp2;
9026
9027   /* Take care of file names without directories.  We need to make sure that
9028      we return consistent values to qsort since some will get confused if
9029      we return the same value when identical operands are passed in opposite
9030      orders.  So if neither has a directory, return 0 and otherwise return
9031      1 or -1 depending on which one has the directory.  */
9032   if ((s1->path == s1->fname || s2->path == s2->fname))
9033     return (s2->path == s2->fname) - (s1->path == s1->fname);
9034
9035   cp1 = (const unsigned char *) s1->path;
9036   cp2 = (const unsigned char *) s2->path;
9037
9038   while (1)
9039     {
9040       ++cp1;
9041       ++cp2;
9042       /* Reached the end of the first path?  If so, handle like above.  */
9043       if ((cp1 == (const unsigned char *) s1->fname)
9044           || (cp2 == (const unsigned char *) s2->fname))
9045         return ((cp2 == (const unsigned char *) s2->fname)
9046                 - (cp1 == (const unsigned char *) s1->fname));
9047
9048       /* Character of current path component the same?  */
9049       else if (*cp1 != *cp2)
9050         return *cp1 - *cp2;
9051     }
9052 }
9053
9054 struct file_name_acquire_data
9055 {
9056   struct file_info *files;
9057   int used_files;
9058   int max_files;
9059 };
9060
9061 /* Traversal function for the hash table.  */
9062
9063 static int
9064 file_name_acquire (void ** slot, void *data)
9065 {
9066   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9067   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9068   struct file_info *fi;
9069   const char *f;
9070
9071   gcc_assert (fnad->max_files >= d->emitted_number);
9072
9073   if (! d->emitted_number)
9074     return 1;
9075
9076   gcc_assert (fnad->max_files != fnad->used_files);
9077
9078   fi = fnad->files + fnad->used_files++;
9079
9080   /* Skip all leading "./".  */
9081   f = d->filename;
9082   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9083     f += 2;
9084
9085   /* Create a new array entry.  */
9086   fi->path = f;
9087   fi->length = strlen (f);
9088   fi->file_idx = d;
9089
9090   /* Search for the file name part.  */
9091   f = strrchr (f, DIR_SEPARATOR);
9092 #if defined (DIR_SEPARATOR_2)
9093   {
9094     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9095
9096     if (g != NULL)
9097       {
9098         if (f == NULL || f < g)
9099           f = g;
9100       }
9101   }
9102 #endif
9103
9104   fi->fname = f == NULL ? fi->path : f + 1;
9105   return 1;
9106 }
9107
9108 /* Output the directory table and the file name table.  We try to minimize
9109    the total amount of memory needed.  A heuristic is used to avoid large
9110    slowdowns with many input files.  */
9111
9112 static void
9113 output_file_names (void)
9114 {
9115   struct file_name_acquire_data fnad;
9116   int numfiles;
9117   struct file_info *files;
9118   struct dir_info *dirs;
9119   int *saved;
9120   int *savehere;
9121   int *backmap;
9122   int ndirs;
9123   int idx_offset;
9124   int i;
9125
9126   if (!last_emitted_file)
9127     {
9128       dw2_asm_output_data (1, 0, "End directory table");
9129       dw2_asm_output_data (1, 0, "End file name table");
9130       return;
9131     }
9132
9133   numfiles = last_emitted_file->emitted_number;
9134
9135   /* Allocate the various arrays we need.  */
9136   files = XALLOCAVEC (struct file_info, numfiles);
9137   dirs = XALLOCAVEC (struct dir_info, numfiles);
9138
9139   fnad.files = files;
9140   fnad.used_files = 0;
9141   fnad.max_files = numfiles;
9142   htab_traverse (file_table, file_name_acquire, &fnad);
9143   gcc_assert (fnad.used_files == fnad.max_files);
9144
9145   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9146
9147   /* Find all the different directories used.  */
9148   dirs[0].path = files[0].path;
9149   dirs[0].length = files[0].fname - files[0].path;
9150   dirs[0].prefix = -1;
9151   dirs[0].count = 1;
9152   dirs[0].dir_idx = 0;
9153   files[0].dir_idx = 0;
9154   ndirs = 1;
9155
9156   for (i = 1; i < numfiles; i++)
9157     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9158         && memcmp (dirs[ndirs - 1].path, files[i].path,
9159                    dirs[ndirs - 1].length) == 0)
9160       {
9161         /* Same directory as last entry.  */
9162         files[i].dir_idx = ndirs - 1;
9163         ++dirs[ndirs - 1].count;
9164       }
9165     else
9166       {
9167         int j;
9168
9169         /* This is a new directory.  */
9170         dirs[ndirs].path = files[i].path;
9171         dirs[ndirs].length = files[i].fname - files[i].path;
9172         dirs[ndirs].count = 1;
9173         dirs[ndirs].dir_idx = ndirs;
9174         files[i].dir_idx = ndirs;
9175
9176         /* Search for a prefix.  */
9177         dirs[ndirs].prefix = -1;
9178         for (j = 0; j < ndirs; j++)
9179           if (dirs[j].length < dirs[ndirs].length
9180               && dirs[j].length > 1
9181               && (dirs[ndirs].prefix == -1
9182                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9183               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9184             dirs[ndirs].prefix = j;
9185
9186         ++ndirs;
9187       }
9188
9189   /* Now to the actual work.  We have to find a subset of the directories which
9190      allow expressing the file name using references to the directory table
9191      with the least amount of characters.  We do not do an exhaustive search
9192      where we would have to check out every combination of every single
9193      possible prefix.  Instead we use a heuristic which provides nearly optimal
9194      results in most cases and never is much off.  */
9195   saved = XALLOCAVEC (int, ndirs);
9196   savehere = XALLOCAVEC (int, ndirs);
9197
9198   memset (saved, '\0', ndirs * sizeof (saved[0]));
9199   for (i = 0; i < ndirs; i++)
9200     {
9201       int j;
9202       int total;
9203
9204       /* We can always save some space for the current directory.  But this
9205          does not mean it will be enough to justify adding the directory.  */
9206       savehere[i] = dirs[i].length;
9207       total = (savehere[i] - saved[i]) * dirs[i].count;
9208
9209       for (j = i + 1; j < ndirs; j++)
9210         {
9211           savehere[j] = 0;
9212           if (saved[j] < dirs[i].length)
9213             {
9214               /* Determine whether the dirs[i] path is a prefix of the
9215                  dirs[j] path.  */
9216               int k;
9217
9218               k = dirs[j].prefix;
9219               while (k != -1 && k != (int) i)
9220                 k = dirs[k].prefix;
9221
9222               if (k == (int) i)
9223                 {
9224                   /* Yes it is.  We can possibly save some memory by
9225                      writing the filenames in dirs[j] relative to
9226                      dirs[i].  */
9227                   savehere[j] = dirs[i].length;
9228                   total += (savehere[j] - saved[j]) * dirs[j].count;
9229                 }
9230             }
9231         }
9232
9233       /* Check whether we can save enough to justify adding the dirs[i]
9234          directory.  */
9235       if (total > dirs[i].length + 1)
9236         {
9237           /* It's worthwhile adding.  */
9238           for (j = i; j < ndirs; j++)
9239             if (savehere[j] > 0)
9240               {
9241                 /* Remember how much we saved for this directory so far.  */
9242                 saved[j] = savehere[j];
9243
9244                 /* Remember the prefix directory.  */
9245                 dirs[j].dir_idx = i;
9246               }
9247         }
9248     }
9249
9250   /* Emit the directory name table.  */
9251   idx_offset = dirs[0].length > 0 ? 1 : 0;
9252   for (i = 1 - idx_offset; i < ndirs; i++)
9253     dw2_asm_output_nstring (dirs[i].path,
9254                             dirs[i].length
9255                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9256                             "Directory Entry: %#x", i + idx_offset);
9257
9258   dw2_asm_output_data (1, 0, "End directory table");
9259
9260   /* We have to emit them in the order of emitted_number since that's
9261      used in the debug info generation.  To do this efficiently we
9262      generate a back-mapping of the indices first.  */
9263   backmap = XALLOCAVEC (int, numfiles);
9264   for (i = 0; i < numfiles; i++)
9265     backmap[files[i].file_idx->emitted_number - 1] = i;
9266
9267   /* Now write all the file names.  */
9268   for (i = 0; i < numfiles; i++)
9269     {
9270       int file_idx = backmap[i];
9271       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9272
9273 #ifdef VMS_DEBUGGING_INFO
9274 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9275
9276       /* Setting these fields can lead to debugger miscomparisons,
9277          but VMS Debug requires them to be set correctly.  */
9278
9279       int ver;
9280       long long cdt;
9281       long siz;
9282       int maxfilelen = strlen (files[file_idx].path)
9283                                + dirs[dir_idx].length
9284                                + MAX_VMS_VERSION_LEN + 1;
9285       char *filebuf = XALLOCAVEC (char, maxfilelen);
9286
9287       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9288       snprintf (filebuf, maxfilelen, "%s;%d",
9289                 files[file_idx].path + dirs[dir_idx].length, ver);
9290
9291       dw2_asm_output_nstring
9292         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9293
9294       /* Include directory index.  */
9295       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9296
9297       /* Modification time.  */
9298       dw2_asm_output_data_uleb128
9299         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9300           ? cdt : 0,
9301          NULL);
9302
9303       /* File length in bytes.  */
9304       dw2_asm_output_data_uleb128
9305         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9306           ? siz : 0,
9307          NULL);
9308 #else
9309       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9310                               "File Entry: %#x", (unsigned) i + 1);
9311
9312       /* Include directory index.  */
9313       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9314
9315       /* Modification time.  */
9316       dw2_asm_output_data_uleb128 (0, NULL);
9317
9318       /* File length in bytes.  */
9319       dw2_asm_output_data_uleb128 (0, NULL);
9320 #endif /* VMS_DEBUGGING_INFO */
9321     }
9322
9323   dw2_asm_output_data (1, 0, "End file name table");
9324 }
9325
9326
9327 /* Output one line number table into the .debug_line section.  */
9328
9329 static void
9330 output_one_line_info_table (dw_line_info_table *table)
9331 {
9332   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9333   unsigned int current_line = 1;
9334   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9335   dw_line_info_entry *ent;
9336   size_t i;
9337
9338   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9339     {
9340       switch (ent->opcode)
9341         {
9342         case LI_set_address:
9343           /* ??? Unfortunately, we have little choice here currently, and
9344              must always use the most general form.  GCC does not know the
9345              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9346              ports do have length attributes which will give an upper bound
9347              on the address range.  We could perhaps use length attributes
9348              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9349           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9350
9351           /* This can handle any delta.  This takes
9352              4+DWARF2_ADDR_SIZE bytes.  */
9353           dw2_asm_output_data (1, 0, "set address %s", line_label);
9354           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9355           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9356           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9357           break;
9358
9359         case LI_set_line:
9360           if (ent->val == current_line)
9361             {
9362               /* We still need to start a new row, so output a copy insn.  */
9363               dw2_asm_output_data (1, DW_LNS_copy,
9364                                    "copy line %u", current_line);
9365             }
9366           else
9367             {
9368               int line_offset = ent->val - current_line;
9369               int line_delta = line_offset - DWARF_LINE_BASE;
9370
9371               current_line = ent->val;
9372               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9373                 {
9374                   /* This can handle deltas from -10 to 234, using the current
9375                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9376                      This takes 1 byte.  */
9377                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9378                                        "line %u", current_line);
9379                 }
9380               else
9381                 {
9382                   /* This can handle any delta.  This takes at least 4 bytes,
9383                      depending on the value being encoded.  */
9384                   dw2_asm_output_data (1, DW_LNS_advance_line,
9385                                        "advance to line %u", current_line);
9386                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9387                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9388                 }
9389             }
9390           break;
9391
9392         case LI_set_file:
9393           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9394           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9395           break;
9396
9397         case LI_set_column:
9398           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9399           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9400           break;
9401
9402         case LI_negate_stmt:
9403           current_is_stmt = !current_is_stmt;
9404           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9405                                "is_stmt %d", current_is_stmt);
9406           break;
9407
9408         case LI_set_prologue_end:
9409           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9410                                "set prologue end");
9411           break;
9412           
9413         case LI_set_epilogue_begin:
9414           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9415                                "set epilogue begin");
9416           break;
9417
9418         case LI_set_discriminator:
9419           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9420           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9421           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9422           dw2_asm_output_data_uleb128 (ent->val, NULL);
9423           break;
9424         }
9425     }
9426
9427   /* Emit debug info for the address of the end of the table.  */
9428   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9429   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9430   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9431   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9432
9433   dw2_asm_output_data (1, 0, "end sequence");
9434   dw2_asm_output_data_uleb128 (1, NULL);
9435   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9436 }
9437
9438 /* Output the source line number correspondence information.  This
9439    information goes into the .debug_line section.  */
9440
9441 static void
9442 output_line_info (void)
9443 {
9444   char l1[20], l2[20], p1[20], p2[20];
9445   int ver = dwarf_version;
9446   bool saw_one = false;
9447   int opc;
9448
9449   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9450   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9451   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9452   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9453
9454   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9455     dw2_asm_output_data (4, 0xffffffff,
9456       "Initial length escape value indicating 64-bit DWARF extension");
9457   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9458                         "Length of Source Line Info");
9459   ASM_OUTPUT_LABEL (asm_out_file, l1);
9460
9461   dw2_asm_output_data (2, ver, "DWARF Version");
9462   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9463   ASM_OUTPUT_LABEL (asm_out_file, p1);
9464
9465   /* Define the architecture-dependent minimum instruction length (in bytes).
9466      In this implementation of DWARF, this field is used for information
9467      purposes only.  Since GCC generates assembly language, we have no
9468      a priori knowledge of how many instruction bytes are generated for each
9469      source line, and therefore can use only the DW_LNE_set_address and
9470      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9471      this as '1', which is "correct enough" for all architectures,
9472      and don't let the target override.  */
9473   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9474
9475   if (ver >= 4)
9476     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9477                          "Maximum Operations Per Instruction");
9478   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9479                        "Default is_stmt_start flag");
9480   dw2_asm_output_data (1, DWARF_LINE_BASE,
9481                        "Line Base Value (Special Opcodes)");
9482   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9483                        "Line Range Value (Special Opcodes)");
9484   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9485                        "Special Opcode Base");
9486
9487   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9488     {
9489       int n_op_args;
9490       switch (opc)
9491         {
9492         case DW_LNS_advance_pc:
9493         case DW_LNS_advance_line:
9494         case DW_LNS_set_file:
9495         case DW_LNS_set_column:
9496         case DW_LNS_fixed_advance_pc:
9497         case DW_LNS_set_isa:
9498           n_op_args = 1;
9499           break;
9500         default:
9501           n_op_args = 0;
9502           break;
9503         }
9504
9505       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9506                            opc, n_op_args);
9507     }
9508
9509   /* Write out the information about the files we use.  */
9510   output_file_names ();
9511   ASM_OUTPUT_LABEL (asm_out_file, p2);
9512
9513   if (separate_line_info)
9514     {
9515       dw_line_info_table *table;
9516       size_t i;
9517
9518       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9519         if (table->in_use)
9520           {
9521             output_one_line_info_table (table);
9522             saw_one = true;
9523           }
9524     }
9525   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9526     {
9527       output_one_line_info_table (cold_text_section_line_info);
9528       saw_one = true;
9529     }
9530
9531   /* ??? Some Darwin linkers crash on a .debug_line section with no
9532      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9533      sufficient -- the address column must also be initialized.
9534      Make sure to output at least one set_address/end_sequence pair,
9535      choosing .text since that section is always present.  */
9536   if (text_section_line_info->in_use || !saw_one)
9537     output_one_line_info_table (text_section_line_info);
9538
9539   /* Output the marker for the end of the line number info.  */
9540   ASM_OUTPUT_LABEL (asm_out_file, l2);
9541 }
9542 \f
9543 /* Given a pointer to a tree node for some base type, return a pointer to
9544    a DIE that describes the given type.
9545
9546    This routine must only be called for GCC type nodes that correspond to
9547    Dwarf base (fundamental) types.  */
9548
9549 static dw_die_ref
9550 base_type_die (tree type)
9551 {
9552   dw_die_ref base_type_result;
9553   enum dwarf_type encoding;
9554
9555   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9556     return 0;
9557
9558   /* If this is a subtype that should not be emitted as a subrange type,
9559      use the base type.  See subrange_type_for_debug_p.  */
9560   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9561     type = TREE_TYPE (type);
9562
9563   switch (TREE_CODE (type))
9564     {
9565     case INTEGER_TYPE:
9566       if ((dwarf_version >= 4 || !dwarf_strict)
9567           && TYPE_NAME (type)
9568           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9569           && DECL_IS_BUILTIN (TYPE_NAME (type))
9570           && DECL_NAME (TYPE_NAME (type)))
9571         {
9572           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9573           if (strcmp (name, "char16_t") == 0
9574               || strcmp (name, "char32_t") == 0)
9575             {
9576               encoding = DW_ATE_UTF;
9577               break;
9578             }
9579         }
9580       if (TYPE_STRING_FLAG (type))
9581         {
9582           if (TYPE_UNSIGNED (type))
9583             encoding = DW_ATE_unsigned_char;
9584           else
9585             encoding = DW_ATE_signed_char;
9586         }
9587       else if (TYPE_UNSIGNED (type))
9588         encoding = DW_ATE_unsigned;
9589       else
9590         encoding = DW_ATE_signed;
9591       break;
9592
9593     case REAL_TYPE:
9594       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9595         {
9596           if (dwarf_version >= 3 || !dwarf_strict)
9597             encoding = DW_ATE_decimal_float;
9598           else
9599             encoding = DW_ATE_lo_user;
9600         }
9601       else
9602         encoding = DW_ATE_float;
9603       break;
9604
9605     case FIXED_POINT_TYPE:
9606       if (!(dwarf_version >= 3 || !dwarf_strict))
9607         encoding = DW_ATE_lo_user;
9608       else if (TYPE_UNSIGNED (type))
9609         encoding = DW_ATE_unsigned_fixed;
9610       else
9611         encoding = DW_ATE_signed_fixed;
9612       break;
9613
9614       /* Dwarf2 doesn't know anything about complex ints, so use
9615          a user defined type for it.  */
9616     case COMPLEX_TYPE:
9617       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9618         encoding = DW_ATE_complex_float;
9619       else
9620         encoding = DW_ATE_lo_user;
9621       break;
9622
9623     case BOOLEAN_TYPE:
9624       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9625       encoding = DW_ATE_boolean;
9626       break;
9627
9628     default:
9629       /* No other TREE_CODEs are Dwarf fundamental types.  */
9630       gcc_unreachable ();
9631     }
9632
9633   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9634
9635   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9636                    int_size_in_bytes (type));
9637   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9638
9639   return base_type_result;
9640 }
9641
9642 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9643    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9644
9645 static inline int
9646 is_base_type (tree type)
9647 {
9648   switch (TREE_CODE (type))
9649     {
9650     case ERROR_MARK:
9651     case VOID_TYPE:
9652     case INTEGER_TYPE:
9653     case REAL_TYPE:
9654     case FIXED_POINT_TYPE:
9655     case COMPLEX_TYPE:
9656     case BOOLEAN_TYPE:
9657       return 1;
9658
9659     case ARRAY_TYPE:
9660     case RECORD_TYPE:
9661     case UNION_TYPE:
9662     case QUAL_UNION_TYPE:
9663     case ENUMERAL_TYPE:
9664     case FUNCTION_TYPE:
9665     case METHOD_TYPE:
9666     case POINTER_TYPE:
9667     case REFERENCE_TYPE:
9668     case NULLPTR_TYPE:
9669     case OFFSET_TYPE:
9670     case LANG_TYPE:
9671     case VECTOR_TYPE:
9672       return 0;
9673
9674     default:
9675       gcc_unreachable ();
9676     }
9677
9678   return 0;
9679 }
9680
9681 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9682    node, return the size in bits for the type if it is a constant, or else
9683    return the alignment for the type if the type's size is not constant, or
9684    else return BITS_PER_WORD if the type actually turns out to be an
9685    ERROR_MARK node.  */
9686
9687 static inline unsigned HOST_WIDE_INT
9688 simple_type_size_in_bits (const_tree type)
9689 {
9690   if (TREE_CODE (type) == ERROR_MARK)
9691     return BITS_PER_WORD;
9692   else if (TYPE_SIZE (type) == NULL_TREE)
9693     return 0;
9694   else if (host_integerp (TYPE_SIZE (type), 1))
9695     return tree_low_cst (TYPE_SIZE (type), 1);
9696   else
9697     return TYPE_ALIGN (type);
9698 }
9699
9700 /* Similarly, but return a double_int instead of UHWI.  */
9701
9702 static inline double_int
9703 double_int_type_size_in_bits (const_tree type)
9704 {
9705   if (TREE_CODE (type) == ERROR_MARK)
9706     return uhwi_to_double_int (BITS_PER_WORD);
9707   else if (TYPE_SIZE (type) == NULL_TREE)
9708     return double_int_zero;
9709   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9710     return tree_to_double_int (TYPE_SIZE (type));
9711   else
9712     return uhwi_to_double_int (TYPE_ALIGN (type));
9713 }
9714
9715 /*  Given a pointer to a tree node for a subrange type, return a pointer
9716     to a DIE that describes the given type.  */
9717
9718 static dw_die_ref
9719 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9720 {
9721   dw_die_ref subrange_die;
9722   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9723
9724   if (context_die == NULL)
9725     context_die = comp_unit_die ();
9726
9727   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9728
9729   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9730     {
9731       /* The size of the subrange type and its base type do not match,
9732          so we need to generate a size attribute for the subrange type.  */
9733       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9734     }
9735
9736   if (low)
9737     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9738   if (high)
9739     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9740
9741   return subrange_die;
9742 }
9743
9744 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9745    entry that chains various modifiers in front of the given type.  */
9746
9747 static dw_die_ref
9748 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9749                    dw_die_ref context_die)
9750 {
9751   enum tree_code code = TREE_CODE (type);
9752   dw_die_ref mod_type_die;
9753   dw_die_ref sub_die = NULL;
9754   tree item_type = NULL;
9755   tree qualified_type;
9756   tree name, low, high;
9757
9758   if (code == ERROR_MARK)
9759     return NULL;
9760
9761   /* See if we already have the appropriately qualified variant of
9762      this type.  */
9763   qualified_type
9764     = get_qualified_type (type,
9765                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9766                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9767
9768   if (qualified_type == sizetype
9769       && TYPE_NAME (qualified_type)
9770       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9771     {
9772       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9773
9774       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9775                            && TYPE_PRECISION (t)
9776                            == TYPE_PRECISION (qualified_type)
9777                            && TYPE_UNSIGNED (t)
9778                            == TYPE_UNSIGNED (qualified_type));
9779       qualified_type = t;
9780     }
9781
9782   /* If we do, then we can just use its DIE, if it exists.  */
9783   if (qualified_type)
9784     {
9785       mod_type_die = lookup_type_die (qualified_type);
9786       if (mod_type_die)
9787         return mod_type_die;
9788     }
9789
9790   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9791
9792   /* Handle C typedef types.  */
9793   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9794       && !DECL_ARTIFICIAL (name))
9795     {
9796       tree dtype = TREE_TYPE (name);
9797
9798       if (qualified_type == dtype)
9799         {
9800           /* For a named type, use the typedef.  */
9801           gen_type_die (qualified_type, context_die);
9802           return lookup_type_die (qualified_type);
9803         }
9804       else if (is_const_type < TYPE_READONLY (dtype)
9805                || is_volatile_type < TYPE_VOLATILE (dtype)
9806                || (is_const_type <= TYPE_READONLY (dtype)
9807                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9808                    && DECL_ORIGINAL_TYPE (name) != type))
9809         /* cv-unqualified version of named type.  Just use the unnamed
9810            type to which it refers.  */
9811         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9812                                   is_const_type, is_volatile_type,
9813                                   context_die);
9814       /* Else cv-qualified version of named type; fall through.  */
9815     }
9816
9817   if (is_const_type
9818       /* If both is_const_type and is_volatile_type, prefer the path
9819          which leads to a qualified type.  */
9820       && (!is_volatile_type
9821           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9822           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9823     {
9824       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9825       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9826     }
9827   else if (is_volatile_type)
9828     {
9829       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9830       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9831     }
9832   else if (code == POINTER_TYPE)
9833     {
9834       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9835       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9836                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9837       item_type = TREE_TYPE (type);
9838       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9839         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9840                          TYPE_ADDR_SPACE (item_type));
9841     }
9842   else if (code == REFERENCE_TYPE)
9843     {
9844       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9845         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9846                                 type);
9847       else
9848         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9849       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9850                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9851       item_type = TREE_TYPE (type);
9852       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9853         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9854                          TYPE_ADDR_SPACE (item_type));
9855     }
9856   else if (code == INTEGER_TYPE
9857            && TREE_TYPE (type) != NULL_TREE
9858            && subrange_type_for_debug_p (type, &low, &high))
9859     {
9860       mod_type_die = subrange_type_die (type, low, high, context_die);
9861       item_type = TREE_TYPE (type);
9862     }
9863   else if (is_base_type (type))
9864     mod_type_die = base_type_die (type);
9865   else
9866     {
9867       gen_type_die (type, context_die);
9868
9869       /* We have to get the type_main_variant here (and pass that to the
9870          `lookup_type_die' routine) because the ..._TYPE node we have
9871          might simply be a *copy* of some original type node (where the
9872          copy was created to help us keep track of typedef names) and
9873          that copy might have a different TYPE_UID from the original
9874          ..._TYPE node.  */
9875       if (TREE_CODE (type) != VECTOR_TYPE)
9876         return lookup_type_die (type_main_variant (type));
9877       else
9878         /* Vectors have the debugging information in the type,
9879            not the main variant.  */
9880         return lookup_type_die (type);
9881     }
9882
9883   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9884      don't output a DW_TAG_typedef, since there isn't one in the
9885      user's program; just attach a DW_AT_name to the type.
9886      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9887      if the base type already has the same name.  */
9888   if (name
9889       && ((TREE_CODE (name) != TYPE_DECL
9890            && (qualified_type == TYPE_MAIN_VARIANT (type)
9891                || (!is_const_type && !is_volatile_type)))
9892           || (TREE_CODE (name) == TYPE_DECL
9893               && TREE_TYPE (name) == qualified_type
9894               && DECL_NAME (name))))
9895     {
9896       if (TREE_CODE (name) == TYPE_DECL)
9897         /* Could just call add_name_and_src_coords_attributes here,
9898            but since this is a builtin type it doesn't have any
9899            useful source coordinates anyway.  */
9900         name = DECL_NAME (name);
9901       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9902       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9903       if (TYPE_ARTIFICIAL (type))
9904         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9905     }
9906   /* This probably indicates a bug.  */
9907   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9908     {
9909       name = TYPE_NAME (type);
9910       if (name
9911           && TREE_CODE (name) == TYPE_DECL)
9912         name = DECL_NAME (name);
9913       add_name_attribute (mod_type_die,
9914                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9915     }
9916
9917   if (qualified_type)
9918     equate_type_number_to_die (qualified_type, mod_type_die);
9919
9920   if (item_type)
9921     /* We must do this after the equate_type_number_to_die call, in case
9922        this is a recursive type.  This ensures that the modified_type_die
9923        recursion will terminate even if the type is recursive.  Recursive
9924        types are possible in Ada.  */
9925     sub_die = modified_type_die (item_type,
9926                                  TYPE_READONLY (item_type),
9927                                  TYPE_VOLATILE (item_type),
9928                                  context_die);
9929
9930   if (sub_die != NULL)
9931     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9932
9933   return mod_type_die;
9934 }
9935
9936 /* Generate DIEs for the generic parameters of T.
9937    T must be either a generic type or a generic function.
9938    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9939
9940 static void
9941 gen_generic_params_dies (tree t)
9942 {
9943   tree parms, args;
9944   int parms_num, i;
9945   dw_die_ref die = NULL;
9946
9947   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9948     return;
9949
9950   if (TYPE_P (t))
9951     die = lookup_type_die (t);
9952   else if (DECL_P (t))
9953     die = lookup_decl_die (t);
9954
9955   gcc_assert (die);
9956
9957   parms = lang_hooks.get_innermost_generic_parms (t);
9958   if (!parms)
9959     /* T has no generic parameter. It means T is neither a generic type
9960        or function. End of story.  */
9961     return;
9962
9963   parms_num = TREE_VEC_LENGTH (parms);
9964   args = lang_hooks.get_innermost_generic_args (t);
9965   for (i = 0; i < parms_num; i++)
9966     {
9967       tree parm, arg, arg_pack_elems;
9968
9969       parm = TREE_VEC_ELT (parms, i);
9970       arg = TREE_VEC_ELT (args, i);
9971       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9972       gcc_assert (parm && TREE_VALUE (parm) && arg);
9973
9974       if (parm && TREE_VALUE (parm) && arg)
9975         {
9976           /* If PARM represents a template parameter pack,
9977              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9978              by DW_TAG_template_*_parameter DIEs for the argument
9979              pack elements of ARG. Note that ARG would then be
9980              an argument pack.  */
9981           if (arg_pack_elems)
9982             template_parameter_pack_die (TREE_VALUE (parm),
9983                                          arg_pack_elems,
9984                                          die);
9985           else
9986             generic_parameter_die (TREE_VALUE (parm), arg,
9987                                    true /* Emit DW_AT_name */, die);
9988         }
9989     }
9990 }
9991
9992 /* Create and return a DIE for PARM which should be
9993    the representation of a generic type parameter.
9994    For instance, in the C++ front end, PARM would be a template parameter.
9995    ARG is the argument to PARM.
9996    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9997    name of the PARM.
9998    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9999    as a child node.  */
10000
10001 static dw_die_ref
10002 generic_parameter_die (tree parm, tree arg,
10003                        bool emit_name_p,
10004                        dw_die_ref parent_die)
10005 {
10006   dw_die_ref tmpl_die = NULL;
10007   const char *name = NULL;
10008
10009   if (!parm || !DECL_NAME (parm) || !arg)
10010     return NULL;
10011
10012   /* We support non-type generic parameters and arguments,
10013      type generic parameters and arguments, as well as
10014      generic generic parameters (a.k.a. template template parameters in C++)
10015      and arguments.  */
10016   if (TREE_CODE (parm) == PARM_DECL)
10017     /* PARM is a nontype generic parameter  */
10018     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10019   else if (TREE_CODE (parm) == TYPE_DECL)
10020     /* PARM is a type generic parameter.  */
10021     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10022   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10023     /* PARM is a generic generic parameter.
10024        Its DIE is a GNU extension. It shall have a
10025        DW_AT_name attribute to represent the name of the template template
10026        parameter, and a DW_AT_GNU_template_name attribute to represent the
10027        name of the template template argument.  */
10028     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10029                         parent_die, parm);
10030   else
10031     gcc_unreachable ();
10032
10033   if (tmpl_die)
10034     {
10035       tree tmpl_type;
10036
10037       /* If PARM is a generic parameter pack, it means we are
10038          emitting debug info for a template argument pack element.
10039          In other terms, ARG is a template argument pack element.
10040          In that case, we don't emit any DW_AT_name attribute for
10041          the die.  */
10042       if (emit_name_p)
10043         {
10044           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10045           gcc_assert (name);
10046           add_AT_string (tmpl_die, DW_AT_name, name);
10047         }
10048
10049       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10050         {
10051           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10052              TMPL_DIE should have a child DW_AT_type attribute that is set
10053              to the type of the argument to PARM, which is ARG.
10054              If PARM is a type generic parameter, TMPL_DIE should have a
10055              child DW_AT_type that is set to ARG.  */
10056           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10057           add_type_attribute (tmpl_die, tmpl_type, 0,
10058                               TREE_THIS_VOLATILE (tmpl_type),
10059                               parent_die);
10060         }
10061       else
10062         {
10063           /* So TMPL_DIE is a DIE representing a
10064              a generic generic template parameter, a.k.a template template
10065              parameter in C++ and arg is a template.  */
10066
10067           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10068              to the name of the argument.  */
10069           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10070           if (name)
10071             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10072         }
10073
10074       if (TREE_CODE (parm) == PARM_DECL)
10075         /* So PARM is a non-type generic parameter.
10076            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10077            attribute of TMPL_DIE which value represents the value
10078            of ARG.
10079            We must be careful here:
10080            The value of ARG might reference some function decls.
10081            We might currently be emitting debug info for a generic
10082            type and types are emitted before function decls, we don't
10083            know if the function decls referenced by ARG will actually be
10084            emitted after cgraph computations.
10085            So must defer the generation of the DW_AT_const_value to
10086            after cgraph is ready.  */
10087         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10088     }
10089
10090   return tmpl_die;
10091 }
10092
10093 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10094    PARM_PACK must be a template parameter pack. The returned DIE
10095    will be child DIE of PARENT_DIE.  */
10096
10097 static dw_die_ref
10098 template_parameter_pack_die (tree parm_pack,
10099                              tree parm_pack_args,
10100                              dw_die_ref parent_die)
10101 {
10102   dw_die_ref die;
10103   int j;
10104
10105   gcc_assert (parent_die && parm_pack);
10106
10107   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10108   add_name_and_src_coords_attributes (die, parm_pack);
10109   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10110     generic_parameter_die (parm_pack,
10111                            TREE_VEC_ELT (parm_pack_args, j),
10112                            false /* Don't emit DW_AT_name */,
10113                            die);
10114   return die;
10115 }
10116
10117 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10118    an enumerated type.  */
10119
10120 static inline int
10121 type_is_enum (const_tree type)
10122 {
10123   return TREE_CODE (type) == ENUMERAL_TYPE;
10124 }
10125
10126 /* Return the DBX register number described by a given RTL node.  */
10127
10128 static unsigned int
10129 dbx_reg_number (const_rtx rtl)
10130 {
10131   unsigned regno = REGNO (rtl);
10132
10133   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10134
10135 #ifdef LEAF_REG_REMAP
10136   if (current_function_uses_only_leaf_regs)
10137     {
10138       int leaf_reg = LEAF_REG_REMAP (regno);
10139       if (leaf_reg != -1)
10140         regno = (unsigned) leaf_reg;
10141     }
10142 #endif
10143
10144   return DBX_REGISTER_NUMBER (regno);
10145 }
10146
10147 /* Optionally add a DW_OP_piece term to a location description expression.
10148    DW_OP_piece is only added if the location description expression already
10149    doesn't end with DW_OP_piece.  */
10150
10151 static void
10152 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10153 {
10154   dw_loc_descr_ref loc;
10155
10156   if (*list_head != NULL)
10157     {
10158       /* Find the end of the chain.  */
10159       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10160         ;
10161
10162       if (loc->dw_loc_opc != DW_OP_piece)
10163         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10164     }
10165 }
10166
10167 /* Return a location descriptor that designates a machine register or
10168    zero if there is none.  */
10169
10170 static dw_loc_descr_ref
10171 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10172 {
10173   rtx regs;
10174
10175   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10176     return 0;
10177
10178   /* We only use "frame base" when we're sure we're talking about the
10179      post-prologue local stack frame.  We do this by *not* running
10180      register elimination until this point, and recognizing the special
10181      argument pointer and soft frame pointer rtx's.
10182      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10183   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10184       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10185     {
10186       dw_loc_descr_ref result = NULL;
10187
10188       if (dwarf_version >= 4 || !dwarf_strict)
10189         {
10190           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10191                                        initialized);
10192           if (result)
10193             add_loc_descr (&result,
10194                            new_loc_descr (DW_OP_stack_value, 0, 0));
10195         }
10196       return result;
10197     }
10198
10199   regs = targetm.dwarf_register_span (rtl);
10200
10201   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10202     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10203   else
10204     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10205 }
10206
10207 /* Return a location descriptor that designates a machine register for
10208    a given hard register number.  */
10209
10210 static dw_loc_descr_ref
10211 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10212 {
10213   dw_loc_descr_ref reg_loc_descr;
10214
10215   if (regno <= 31)
10216     reg_loc_descr
10217       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10218   else
10219     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10220
10221   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10222     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10223
10224   return reg_loc_descr;
10225 }
10226
10227 /* Given an RTL of a register, return a location descriptor that
10228    designates a value that spans more than one register.  */
10229
10230 static dw_loc_descr_ref
10231 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10232                              enum var_init_status initialized)
10233 {
10234   int nregs, size, i;
10235   unsigned reg;
10236   dw_loc_descr_ref loc_result = NULL;
10237
10238   reg = REGNO (rtl);
10239 #ifdef LEAF_REG_REMAP
10240   if (current_function_uses_only_leaf_regs)
10241     {
10242       int leaf_reg = LEAF_REG_REMAP (reg);
10243       if (leaf_reg != -1)
10244         reg = (unsigned) leaf_reg;
10245     }
10246 #endif
10247   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10248   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10249
10250   /* Simple, contiguous registers.  */
10251   if (regs == NULL_RTX)
10252     {
10253       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10254
10255       loc_result = NULL;
10256       while (nregs--)
10257         {
10258           dw_loc_descr_ref t;
10259
10260           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10261                                       VAR_INIT_STATUS_INITIALIZED);
10262           add_loc_descr (&loc_result, t);
10263           add_loc_descr_op_piece (&loc_result, size);
10264           ++reg;
10265         }
10266       return loc_result;
10267     }
10268
10269   /* Now onto stupid register sets in non contiguous locations.  */
10270
10271   gcc_assert (GET_CODE (regs) == PARALLEL);
10272
10273   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10274   loc_result = NULL;
10275
10276   for (i = 0; i < XVECLEN (regs, 0); ++i)
10277     {
10278       dw_loc_descr_ref t;
10279
10280       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10281                                   VAR_INIT_STATUS_INITIALIZED);
10282       add_loc_descr (&loc_result, t);
10283       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10284       add_loc_descr_op_piece (&loc_result, size);
10285     }
10286
10287   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10288     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10289   return loc_result;
10290 }
10291
10292 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10293
10294 /* Return a location descriptor that designates a constant i,
10295    as a compound operation from constant (i >> shift), constant shift
10296    and DW_OP_shl.  */
10297
10298 static dw_loc_descr_ref
10299 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10300 {
10301   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10302   add_loc_descr (&ret, int_loc_descriptor (shift));
10303   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10304   return ret;
10305 }
10306
10307 /* Return a location descriptor that designates a constant.  */
10308
10309 static dw_loc_descr_ref
10310 int_loc_descriptor (HOST_WIDE_INT i)
10311 {
10312   enum dwarf_location_atom op;
10313
10314   /* Pick the smallest representation of a constant, rather than just
10315      defaulting to the LEB encoding.  */
10316   if (i >= 0)
10317     {
10318       int clz = clz_hwi (i);
10319       int ctz = ctz_hwi (i);
10320       if (i <= 31)
10321         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10322       else if (i <= 0xff)
10323         op = DW_OP_const1u;
10324       else if (i <= 0xffff)
10325         op = DW_OP_const2u;
10326       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10327                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10328         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10329            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10330            while DW_OP_const4u is 5 bytes.  */
10331         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10332       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10333                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10334         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10335            while DW_OP_const4u is 5 bytes.  */
10336         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10337       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10338         op = DW_OP_const4u;
10339       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10340                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10341         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10342            while DW_OP_constu of constant >= 0x100000000 takes at least
10343            6 bytes.  */
10344         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10345       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10346                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10347                   >= HOST_BITS_PER_WIDE_INT)
10348         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10349            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10350            while DW_OP_constu takes in this case at least 6 bytes.  */
10351         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10352       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10353                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10354                && size_of_uleb128 (i) > 6)
10355         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10356         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10357       else
10358         op = DW_OP_constu;
10359     }
10360   else
10361     {
10362       if (i >= -0x80)
10363         op = DW_OP_const1s;
10364       else if (i >= -0x8000)
10365         op = DW_OP_const2s;
10366       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10367         {
10368           if (size_of_int_loc_descriptor (i) < 5)
10369             {
10370               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10371               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10372               return ret;
10373             }
10374           op = DW_OP_const4s;
10375         }
10376       else
10377         {
10378           if (size_of_int_loc_descriptor (i)
10379               < (unsigned long) 1 + size_of_sleb128 (i))
10380             {
10381               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10382               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10383               return ret;
10384             }
10385           op = DW_OP_consts;
10386         }
10387     }
10388
10389   return new_loc_descr (op, i, 0);
10390 }
10391
10392 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10393    without actually allocating it.  */
10394
10395 static unsigned long
10396 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10397 {
10398   return size_of_int_loc_descriptor (i >> shift)
10399          + size_of_int_loc_descriptor (shift)
10400          + 1;
10401 }
10402
10403 /* Return size_of_locs (int_loc_descriptor (i)) without
10404    actually allocating it.  */
10405
10406 static unsigned long
10407 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10408 {
10409   unsigned long s;
10410
10411   if (i >= 0)
10412     {
10413       int clz, ctz;
10414       if (i <= 31)
10415         return 1;
10416       else if (i <= 0xff)
10417         return 2;
10418       else if (i <= 0xffff)
10419         return 3;
10420       clz = clz_hwi (i);
10421       ctz = ctz_hwi (i);
10422       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10423           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10424         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10425                                                     - clz - 5);
10426       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10427                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10428         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10429                                                     - clz - 8);
10430       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10431         return 5;
10432       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10433       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10434           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10435         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10436                                                     - clz - 8);
10437       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10438                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10439         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10440                                                     - clz - 16);
10441       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10442                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10443                && s > 6)
10444         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10445                                                     - clz - 32);
10446       else
10447         return 1 + s;
10448     }
10449   else
10450     {
10451       if (i >= -0x80)
10452         return 2;
10453       else if (i >= -0x8000)
10454         return 3;
10455       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10456         {
10457           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10458             {
10459               s = size_of_int_loc_descriptor (-i) + 1;
10460               if (s < 5)
10461                 return s;
10462             }
10463           return 5;
10464         }
10465       else
10466         {
10467           unsigned long r = 1 + size_of_sleb128 (i);
10468           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10469             {
10470               s = size_of_int_loc_descriptor (-i) + 1;
10471               if (s < r)
10472                 return s;
10473             }
10474           return r;
10475         }
10476     }
10477 }
10478
10479 /* Return loc description representing "address" of integer value.
10480    This can appear only as toplevel expression.  */
10481
10482 static dw_loc_descr_ref
10483 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10484 {
10485   int litsize;
10486   dw_loc_descr_ref loc_result = NULL;
10487
10488   if (!(dwarf_version >= 4 || !dwarf_strict))
10489     return NULL;
10490
10491   litsize = size_of_int_loc_descriptor (i);
10492   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10493      is more compact.  For DW_OP_stack_value we need:
10494      litsize + 1 (DW_OP_stack_value)
10495      and for DW_OP_implicit_value:
10496      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10497   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10498     {
10499       loc_result = int_loc_descriptor (i);
10500       add_loc_descr (&loc_result,
10501                      new_loc_descr (DW_OP_stack_value, 0, 0));
10502       return loc_result;
10503     }
10504
10505   loc_result = new_loc_descr (DW_OP_implicit_value,
10506                               size, 0);
10507   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10508   loc_result->dw_loc_oprnd2.v.val_int = i;
10509   return loc_result;
10510 }
10511
10512 /* Return a location descriptor that designates a base+offset location.  */
10513
10514 static dw_loc_descr_ref
10515 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10516                  enum var_init_status initialized)
10517 {
10518   unsigned int regno;
10519   dw_loc_descr_ref result;
10520   dw_fde_ref fde = cfun->fde;
10521
10522   /* We only use "frame base" when we're sure we're talking about the
10523      post-prologue local stack frame.  We do this by *not* running
10524      register elimination until this point, and recognizing the special
10525      argument pointer and soft frame pointer rtx's.  */
10526   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10527     {
10528       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10529
10530       if (elim != reg)
10531         {
10532           if (GET_CODE (elim) == PLUS)
10533             {
10534               offset += INTVAL (XEXP (elim, 1));
10535               elim = XEXP (elim, 0);
10536             }
10537           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10538                        && (elim == hard_frame_pointer_rtx
10539                            || elim == stack_pointer_rtx))
10540                       || elim == (frame_pointer_needed
10541                                   ? hard_frame_pointer_rtx
10542                                   : stack_pointer_rtx));
10543
10544           /* If drap register is used to align stack, use frame
10545              pointer + offset to access stack variables.  If stack
10546              is aligned without drap, use stack pointer + offset to
10547              access stack variables.  */
10548           if (crtl->stack_realign_tried
10549               && reg == frame_pointer_rtx)
10550             {
10551               int base_reg
10552                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10553                                       ? HARD_FRAME_POINTER_REGNUM
10554                                       : REGNO (elim));
10555               return new_reg_loc_descr (base_reg, offset);
10556             }
10557
10558           gcc_assert (frame_pointer_fb_offset_valid);
10559           offset += frame_pointer_fb_offset;
10560           return new_loc_descr (DW_OP_fbreg, offset, 0);
10561         }
10562     }
10563
10564   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10565
10566   if (!optimize && fde
10567       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10568     {
10569       /* Use cfa+offset to represent the location of arguments passed
10570          on the stack when drap is used to align stack.
10571          Only do this when not optimizing, for optimized code var-tracking
10572          is supposed to track where the arguments live and the register
10573          used as vdrap or drap in some spot might be used for something
10574          else in other part of the routine.  */
10575       return new_loc_descr (DW_OP_fbreg, offset, 0);
10576     }
10577
10578   if (regno <= 31)
10579     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10580                             offset, 0);
10581   else
10582     result = new_loc_descr (DW_OP_bregx, regno, offset);
10583
10584   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10585     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10586
10587   return result;
10588 }
10589
10590 /* Return true if this RTL expression describes a base+offset calculation.  */
10591
10592 static inline int
10593 is_based_loc (const_rtx rtl)
10594 {
10595   return (GET_CODE (rtl) == PLUS
10596           && ((REG_P (XEXP (rtl, 0))
10597                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10598                && CONST_INT_P (XEXP (rtl, 1)))));
10599 }
10600
10601 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10602    failed.  */
10603
10604 static dw_loc_descr_ref
10605 tls_mem_loc_descriptor (rtx mem)
10606 {
10607   tree base;
10608   dw_loc_descr_ref loc_result;
10609
10610   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10611     return NULL;
10612
10613   base = get_base_address (MEM_EXPR (mem));
10614   if (base == NULL
10615       || TREE_CODE (base) != VAR_DECL
10616       || !DECL_THREAD_LOCAL_P (base))
10617     return NULL;
10618
10619   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10620   if (loc_result == NULL)
10621     return NULL;
10622
10623   if (MEM_OFFSET (mem))
10624     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10625
10626   return loc_result;
10627 }
10628
10629 /* Output debug info about reason why we failed to expand expression as dwarf
10630    expression.  */
10631
10632 static void
10633 expansion_failed (tree expr, rtx rtl, char const *reason)
10634 {
10635   if (dump_file && (dump_flags & TDF_DETAILS))
10636     {
10637       fprintf (dump_file, "Failed to expand as dwarf: ");
10638       if (expr)
10639         print_generic_expr (dump_file, expr, dump_flags);
10640       if (rtl)
10641         {
10642           fprintf (dump_file, "\n");
10643           print_rtl (dump_file, rtl);
10644         }
10645       fprintf (dump_file, "\nReason: %s\n", reason);
10646     }
10647 }
10648
10649 /* Helper function for const_ok_for_output, called either directly
10650    or via for_each_rtx.  */
10651
10652 static int
10653 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10654 {
10655   rtx rtl = *rtlp;
10656
10657   if (GET_CODE (rtl) == UNSPEC)
10658     {
10659       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10660          we can't express it in the debug info.  */
10661 #ifdef ENABLE_CHECKING
10662       /* Don't complain about TLS UNSPECs, those are just too hard to
10663          delegitimize.  */
10664       if (XVECLEN (rtl, 0) != 1
10665           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10666           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10667           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10668           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10669         inform (current_function_decl
10670                 ? DECL_SOURCE_LOCATION (current_function_decl)
10671                 : UNKNOWN_LOCATION,
10672 #if NUM_UNSPEC_VALUES > 0
10673                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10674                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10675                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10676                 XINT (rtl, 1));
10677 #else
10678                 "non-delegitimized UNSPEC %d found in variable location",
10679                 XINT (rtl, 1));
10680 #endif
10681 #endif
10682       expansion_failed (NULL_TREE, rtl,
10683                         "UNSPEC hasn't been delegitimized.\n");
10684       return 1;
10685     }
10686
10687   if (targetm.const_not_ok_for_debug_p (rtl))
10688     {
10689       expansion_failed (NULL_TREE, rtl,
10690                         "Expression rejected for debug by the backend.\n");
10691       return 1;
10692     }
10693
10694   if (GET_CODE (rtl) != SYMBOL_REF)
10695     return 0;
10696
10697   if (CONSTANT_POOL_ADDRESS_P (rtl))
10698     {
10699       bool marked;
10700       get_pool_constant_mark (rtl, &marked);
10701       /* If all references to this pool constant were optimized away,
10702          it was not output and thus we can't represent it.  */
10703       if (!marked)
10704         {
10705           expansion_failed (NULL_TREE, rtl,
10706                             "Constant was removed from constant pool.\n");
10707           return 1;
10708         }
10709     }
10710
10711   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10712     return 1;
10713
10714   /* Avoid references to external symbols in debug info, on several targets
10715      the linker might even refuse to link when linking a shared library,
10716      and in many other cases the relocations for .debug_info/.debug_loc are
10717      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10718      to be defined within the same shared library or executable are fine.  */
10719   if (SYMBOL_REF_EXTERNAL_P (rtl))
10720     {
10721       tree decl = SYMBOL_REF_DECL (rtl);
10722
10723       if (decl == NULL || !targetm.binds_local_p (decl))
10724         {
10725           expansion_failed (NULL_TREE, rtl,
10726                             "Symbol not defined in current TU.\n");
10727           return 1;
10728         }
10729     }
10730
10731   return 0;
10732 }
10733
10734 /* Return true if constant RTL can be emitted in DW_OP_addr or
10735    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10736    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10737
10738 static bool
10739 const_ok_for_output (rtx rtl)
10740 {
10741   if (GET_CODE (rtl) == SYMBOL_REF)
10742     return const_ok_for_output_1 (&rtl, NULL) == 0;
10743
10744   if (GET_CODE (rtl) == CONST)
10745     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10746
10747   return true;
10748 }
10749
10750 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10751    if possible, NULL otherwise.  */
10752
10753 static dw_die_ref
10754 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10755 {
10756   dw_die_ref type_die;
10757   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10758
10759   if (type == NULL)
10760     return NULL;
10761   switch (TREE_CODE (type))
10762     {
10763     case INTEGER_TYPE:
10764     case REAL_TYPE:
10765       break;
10766     default:
10767       return NULL;
10768     }
10769   type_die = lookup_type_die (type);
10770   if (!type_die)
10771     type_die = modified_type_die (type, false, false, comp_unit_die ());
10772   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10773     return NULL;
10774   return type_die;
10775 }
10776
10777 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10778    type matching MODE, or, if MODE is narrower than or as wide as
10779    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10780    possible.  */
10781
10782 static dw_loc_descr_ref
10783 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10784 {
10785   enum machine_mode outer_mode = mode;
10786   dw_die_ref type_die;
10787   dw_loc_descr_ref cvt;
10788
10789   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10790     {
10791       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10792       return op;
10793     }
10794   type_die = base_type_for_mode (outer_mode, 1);
10795   if (type_die == NULL)
10796     return NULL;
10797   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10798   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10799   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10800   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10801   add_loc_descr (&op, cvt);
10802   return op;
10803 }
10804
10805 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10806
10807 static dw_loc_descr_ref
10808 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10809                         dw_loc_descr_ref op1)
10810 {
10811   dw_loc_descr_ref ret = op0;
10812   add_loc_descr (&ret, op1);
10813   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10814   if (STORE_FLAG_VALUE != 1)
10815     {
10816       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10817       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10818     }
10819   return ret;
10820 }
10821
10822 /* Return location descriptor for signed comparison OP RTL.  */
10823
10824 static dw_loc_descr_ref
10825 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10826                          enum machine_mode mem_mode)
10827 {
10828   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10829   dw_loc_descr_ref op0, op1;
10830   int shift;
10831
10832   if (op_mode == VOIDmode)
10833     op_mode = GET_MODE (XEXP (rtl, 1));
10834   if (op_mode == VOIDmode)
10835     return NULL;
10836
10837   if (dwarf_strict
10838       && (GET_MODE_CLASS (op_mode) != MODE_INT
10839           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10840     return NULL;
10841
10842   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10843                             VAR_INIT_STATUS_INITIALIZED);
10844   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10845                             VAR_INIT_STATUS_INITIALIZED);
10846
10847   if (op0 == NULL || op1 == NULL)
10848     return NULL;
10849
10850   if (GET_MODE_CLASS (op_mode) != MODE_INT
10851       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10852     return compare_loc_descriptor (op, op0, op1);
10853
10854   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10855     {
10856       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10857       dw_loc_descr_ref cvt;
10858
10859       if (type_die == NULL)
10860         return NULL;
10861       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10862       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10863       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10864       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10865       add_loc_descr (&op0, cvt);
10866       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10867       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10868       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10869       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10870       add_loc_descr (&op1, cvt);
10871       return compare_loc_descriptor (op, op0, op1);
10872     }
10873
10874   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10875   /* For eq/ne, if the operands are known to be zero-extended,
10876      there is no need to do the fancy shifting up.  */
10877   if (op == DW_OP_eq || op == DW_OP_ne)
10878     {
10879       dw_loc_descr_ref last0, last1;
10880       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10881         ;
10882       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10883         ;
10884       /* deref_size zero extends, and for constants we can check
10885          whether they are zero extended or not.  */
10886       if (((last0->dw_loc_opc == DW_OP_deref_size
10887             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10888            || (CONST_INT_P (XEXP (rtl, 0))
10889                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10890                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10891           && ((last1->dw_loc_opc == DW_OP_deref_size
10892                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10893               || (CONST_INT_P (XEXP (rtl, 1))
10894                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10895                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10896         return compare_loc_descriptor (op, op0, op1);
10897
10898       /* EQ/NE comparison against constant in narrower type than
10899          DWARF2_ADDR_SIZE can be performed either as
10900          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10901          DW_OP_{eq,ne}
10902          or
10903          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10904          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10905       if (CONST_INT_P (XEXP (rtl, 1))
10906           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10907           && (size_of_int_loc_descriptor (shift) + 1
10908               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10909               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10910                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10911                                                & GET_MODE_MASK (op_mode))))
10912         {
10913           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10914           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10915           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10916                                     & GET_MODE_MASK (op_mode));
10917           return compare_loc_descriptor (op, op0, op1);
10918         }
10919     }
10920   add_loc_descr (&op0, int_loc_descriptor (shift));
10921   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10922   if (CONST_INT_P (XEXP (rtl, 1)))
10923     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10924   else
10925     {
10926       add_loc_descr (&op1, int_loc_descriptor (shift));
10927       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10928     }
10929   return compare_loc_descriptor (op, op0, op1);
10930 }
10931
10932 /* Return location descriptor for unsigned comparison OP RTL.  */
10933
10934 static dw_loc_descr_ref
10935 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10936                          enum machine_mode mem_mode)
10937 {
10938   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10939   dw_loc_descr_ref op0, op1;
10940
10941   if (op_mode == VOIDmode)
10942     op_mode = GET_MODE (XEXP (rtl, 1));
10943   if (op_mode == VOIDmode)
10944     return NULL;
10945   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10946     return NULL;
10947
10948   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10949     return NULL;
10950
10951   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10952                             VAR_INIT_STATUS_INITIALIZED);
10953   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10954                             VAR_INIT_STATUS_INITIALIZED);
10955
10956   if (op0 == NULL || op1 == NULL)
10957     return NULL;
10958
10959   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10960     {
10961       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10962       dw_loc_descr_ref last0, last1;
10963       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10964         ;
10965       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10966         ;
10967       if (CONST_INT_P (XEXP (rtl, 0)))
10968         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10969       /* deref_size zero extends, so no need to mask it again.  */
10970       else if (last0->dw_loc_opc != DW_OP_deref_size
10971                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10972         {
10973           add_loc_descr (&op0, int_loc_descriptor (mask));
10974           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10975         }
10976       if (CONST_INT_P (XEXP (rtl, 1)))
10977         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10978       /* deref_size zero extends, so no need to mask it again.  */
10979       else if (last1->dw_loc_opc != DW_OP_deref_size
10980                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10981         {
10982           add_loc_descr (&op1, int_loc_descriptor (mask));
10983           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10984         }
10985     }
10986   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10987     {
10988       HOST_WIDE_INT bias = 1;
10989       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10990       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10991       if (CONST_INT_P (XEXP (rtl, 1)))
10992         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10993                                   + INTVAL (XEXP (rtl, 1)));
10994       else
10995         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10996                                             bias, 0));
10997     }
10998   return compare_loc_descriptor (op, op0, op1);
10999 }
11000
11001 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11002
11003 static dw_loc_descr_ref
11004 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11005                        enum machine_mode mem_mode)
11006 {
11007   enum dwarf_location_atom op;
11008   dw_loc_descr_ref op0, op1, ret;
11009   dw_loc_descr_ref bra_node, drop_node;
11010
11011   if (dwarf_strict
11012       && (GET_MODE_CLASS (mode) != MODE_INT
11013           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11014     return NULL;
11015
11016   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11017                             VAR_INIT_STATUS_INITIALIZED);
11018   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11019                             VAR_INIT_STATUS_INITIALIZED);
11020
11021   if (op0 == NULL || op1 == NULL)
11022     return NULL;
11023
11024   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11025   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11026   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11027   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11028     {
11029       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11030         {
11031           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11032           add_loc_descr (&op0, int_loc_descriptor (mask));
11033           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11034           add_loc_descr (&op1, int_loc_descriptor (mask));
11035           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11036         }
11037       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11038         {
11039           HOST_WIDE_INT bias = 1;
11040           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11041           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11042           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11043         }
11044     }
11045   else if (GET_MODE_CLASS (mode) == MODE_INT
11046            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11047     {
11048       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11049       add_loc_descr (&op0, int_loc_descriptor (shift));
11050       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11051       add_loc_descr (&op1, int_loc_descriptor (shift));
11052       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11053     }
11054   else if (GET_MODE_CLASS (mode) == MODE_INT
11055            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11056     {
11057       dw_die_ref type_die = base_type_for_mode (mode, 0);
11058       dw_loc_descr_ref cvt;
11059       if (type_die == NULL)
11060         return NULL;
11061       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11062       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11063       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11064       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11065       add_loc_descr (&op0, cvt);
11066       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11067       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11068       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11069       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11070       add_loc_descr (&op1, cvt);
11071     }
11072
11073   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11074     op = DW_OP_lt;
11075   else
11076     op = DW_OP_gt;
11077   ret = op0;
11078   add_loc_descr (&ret, op1);
11079   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11080   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11081   add_loc_descr (&ret, bra_node);
11082   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11083   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11084   add_loc_descr (&ret, drop_node);
11085   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11086   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11087   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11088       && GET_MODE_CLASS (mode) == MODE_INT
11089       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11090     ret = convert_descriptor_to_mode (mode, ret);
11091   return ret;
11092 }
11093
11094 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11095    but after converting arguments to type_die, afterwards
11096    convert back to unsigned.  */
11097
11098 static dw_loc_descr_ref
11099 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11100              enum machine_mode mode, enum machine_mode mem_mode)
11101 {
11102   dw_loc_descr_ref cvt, op0, op1;
11103
11104   if (type_die == NULL)
11105     return NULL;
11106   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11107                             VAR_INIT_STATUS_INITIALIZED);
11108   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11109                             VAR_INIT_STATUS_INITIALIZED);
11110   if (op0 == NULL || op1 == NULL)
11111     return NULL;
11112   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11113   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11114   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11115   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11116   add_loc_descr (&op0, cvt);
11117   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11118   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11119   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11120   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11121   add_loc_descr (&op1, cvt);
11122   add_loc_descr (&op0, op1);
11123   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11124   return convert_descriptor_to_mode (mode, op0);
11125 }
11126
11127 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11128    const0 is DW_OP_lit0 or corresponding typed constant,
11129    const1 is DW_OP_lit1 or corresponding typed constant
11130    and constMSB is constant with just the MSB bit set
11131    for the mode):
11132        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11133    L1: const0 DW_OP_swap
11134    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11135        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11136    L3: DW_OP_drop
11137    L4: DW_OP_nop
11138
11139    CTZ is similar:
11140        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11141    L1: const0 DW_OP_swap
11142    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11143        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11144    L3: DW_OP_drop
11145    L4: DW_OP_nop
11146
11147    FFS is similar:
11148        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11149    L1: const1 DW_OP_swap
11150    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11151        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11152    L3: DW_OP_drop
11153    L4: DW_OP_nop  */
11154
11155 static dw_loc_descr_ref
11156 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11157                     enum machine_mode mem_mode)
11158 {
11159   dw_loc_descr_ref op0, ret, tmp;
11160   HOST_WIDE_INT valv;
11161   dw_loc_descr_ref l1jump, l1label;
11162   dw_loc_descr_ref l2jump, l2label;
11163   dw_loc_descr_ref l3jump, l3label;
11164   dw_loc_descr_ref l4jump, l4label;
11165   rtx msb;
11166
11167   if (GET_MODE_CLASS (mode) != MODE_INT
11168       || GET_MODE (XEXP (rtl, 0)) != mode
11169       || (GET_CODE (rtl) == CLZ
11170           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11171     return NULL;
11172
11173   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11174                             VAR_INIT_STATUS_INITIALIZED);
11175   if (op0 == NULL)
11176     return NULL;
11177   ret = op0;
11178   if (GET_CODE (rtl) == CLZ)
11179     {
11180       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11181         valv = GET_MODE_BITSIZE (mode);
11182     }
11183   else if (GET_CODE (rtl) == FFS)
11184     valv = 0;
11185   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11186     valv = GET_MODE_BITSIZE (mode);
11187   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11188   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11189   add_loc_descr (&ret, l1jump);
11190   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11191   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11192                             VAR_INIT_STATUS_INITIALIZED);
11193   if (tmp == NULL)
11194     return NULL;
11195   add_loc_descr (&ret, tmp);
11196   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11197   add_loc_descr (&ret, l4jump);
11198   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11199                                 ? const1_rtx : const0_rtx,
11200                                 mode, mem_mode,
11201                                 VAR_INIT_STATUS_INITIALIZED);
11202   if (l1label == NULL)
11203     return NULL;
11204   add_loc_descr (&ret, l1label);
11205   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11206   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11207   add_loc_descr (&ret, l2label);
11208   if (GET_CODE (rtl) != CLZ)
11209     msb = const1_rtx;
11210   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11211     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11212                    << (GET_MODE_BITSIZE (mode) - 1));
11213   else
11214     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11215                                   << (GET_MODE_BITSIZE (mode)
11216                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11217   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11218     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11219                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11220                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11221   else
11222     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11223                               VAR_INIT_STATUS_INITIALIZED);
11224   if (tmp == NULL)
11225     return NULL;
11226   add_loc_descr (&ret, tmp);
11227   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11228   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11229   add_loc_descr (&ret, l3jump);
11230   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11231                             VAR_INIT_STATUS_INITIALIZED);
11232   if (tmp == NULL)
11233     return NULL;
11234   add_loc_descr (&ret, tmp);
11235   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11236                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11237   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11238   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11239   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11240   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11241   add_loc_descr (&ret, l2jump);
11242   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11243   add_loc_descr (&ret, l3label);
11244   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11245   add_loc_descr (&ret, l4label);
11246   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11247   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11248   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11249   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11250   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11251   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11252   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11253   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11254   return ret;
11255 }
11256
11257 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11258    const1 is DW_OP_lit1 or corresponding typed constant):
11259        const0 DW_OP_swap
11260    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11261        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11262    L2: DW_OP_drop
11263
11264    PARITY is similar:
11265    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11266        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11267    L2: DW_OP_drop  */
11268
11269 static dw_loc_descr_ref
11270 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11271                          enum machine_mode mem_mode)
11272 {
11273   dw_loc_descr_ref op0, ret, tmp;
11274   dw_loc_descr_ref l1jump, l1label;
11275   dw_loc_descr_ref l2jump, l2label;
11276
11277   if (GET_MODE_CLASS (mode) != MODE_INT
11278       || GET_MODE (XEXP (rtl, 0)) != mode)
11279     return NULL;
11280
11281   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11282                             VAR_INIT_STATUS_INITIALIZED);
11283   if (op0 == NULL)
11284     return NULL;
11285   ret = op0;
11286   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11287                             VAR_INIT_STATUS_INITIALIZED);
11288   if (tmp == NULL)
11289     return NULL;
11290   add_loc_descr (&ret, tmp);
11291   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11292   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11293   add_loc_descr (&ret, l1label);
11294   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11295   add_loc_descr (&ret, l2jump);
11296   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11297   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11298   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11299                             VAR_INIT_STATUS_INITIALIZED);
11300   if (tmp == NULL)
11301     return NULL;
11302   add_loc_descr (&ret, tmp);
11303   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11304   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11305                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11306   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11307   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11308                             VAR_INIT_STATUS_INITIALIZED);
11309   add_loc_descr (&ret, tmp);
11310   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11311   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11312   add_loc_descr (&ret, l1jump);
11313   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11314   add_loc_descr (&ret, l2label);
11315   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11316   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11317   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11318   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11319   return ret;
11320 }
11321
11322 /* BSWAP (constS is initial shift count, either 56 or 24):
11323        constS const0
11324    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11325        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11326        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11327        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11328    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11329
11330 static dw_loc_descr_ref
11331 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11332                       enum machine_mode mem_mode)
11333 {
11334   dw_loc_descr_ref op0, ret, tmp;
11335   dw_loc_descr_ref l1jump, l1label;
11336   dw_loc_descr_ref l2jump, l2label;
11337
11338   if (GET_MODE_CLASS (mode) != MODE_INT
11339       || BITS_PER_UNIT != 8
11340       || (GET_MODE_BITSIZE (mode) != 32
11341           &&  GET_MODE_BITSIZE (mode) != 64))
11342     return NULL;
11343
11344   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11345                             VAR_INIT_STATUS_INITIALIZED);
11346   if (op0 == NULL)
11347     return NULL;
11348
11349   ret = op0;
11350   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11351                             mode, mem_mode,
11352                             VAR_INIT_STATUS_INITIALIZED);
11353   if (tmp == NULL)
11354     return NULL;
11355   add_loc_descr (&ret, tmp);
11356   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11357                             VAR_INIT_STATUS_INITIALIZED);
11358   if (tmp == NULL)
11359     return NULL;
11360   add_loc_descr (&ret, tmp);
11361   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11362   add_loc_descr (&ret, l1label);
11363   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11364                             mode, mem_mode,
11365                             VAR_INIT_STATUS_INITIALIZED);
11366   add_loc_descr (&ret, tmp);
11367   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11368   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11369   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11370   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11371                             VAR_INIT_STATUS_INITIALIZED);
11372   if (tmp == NULL)
11373     return NULL;
11374   add_loc_descr (&ret, tmp);
11375   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11376   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11377   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11378   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11379   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11380   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11381   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11382                             VAR_INIT_STATUS_INITIALIZED);
11383   add_loc_descr (&ret, tmp);
11384   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11385   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11386   add_loc_descr (&ret, l2jump);
11387   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11388                             VAR_INIT_STATUS_INITIALIZED);
11389   add_loc_descr (&ret, tmp);
11390   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11391   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11392   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11393   add_loc_descr (&ret, l1jump);
11394   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11395   add_loc_descr (&ret, l2label);
11396   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11397   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11398   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11399   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11400   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11401   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11402   return ret;
11403 }
11404
11405 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11406    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11407    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11408    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11409
11410    ROTATERT is similar:
11411    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11412    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11413    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11414
11415 static dw_loc_descr_ref
11416 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11417                        enum machine_mode mem_mode)
11418 {
11419   rtx rtlop1 = XEXP (rtl, 1);
11420   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11421   int i;
11422
11423   if (GET_MODE_CLASS (mode) != MODE_INT)
11424     return NULL;
11425
11426   if (GET_MODE (rtlop1) != VOIDmode
11427       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11428     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11429   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11430                             VAR_INIT_STATUS_INITIALIZED);
11431   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11432                             VAR_INIT_STATUS_INITIALIZED);
11433   if (op0 == NULL || op1 == NULL)
11434     return NULL;
11435   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11436     for (i = 0; i < 2; i++)
11437       {
11438         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11439           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11440                                         mode, mem_mode,
11441                                         VAR_INIT_STATUS_INITIALIZED);
11442         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11443           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11444                                    ? DW_OP_const4u
11445                                    : HOST_BITS_PER_WIDE_INT == 64
11446                                    ? DW_OP_const8u : DW_OP_constu,
11447                                    GET_MODE_MASK (mode), 0);
11448         else
11449           mask[i] = NULL;
11450         if (mask[i] == NULL)
11451           return NULL;
11452         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11453       }
11454   ret = op0;
11455   add_loc_descr (&ret, op1);
11456   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11457   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11458   if (GET_CODE (rtl) == ROTATERT)
11459     {
11460       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11461       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11462                                           GET_MODE_BITSIZE (mode), 0));
11463     }
11464   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11465   if (mask[0] != NULL)
11466     add_loc_descr (&ret, mask[0]);
11467   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11468   if (mask[1] != NULL)
11469     {
11470       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11471       add_loc_descr (&ret, mask[1]);
11472       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11473     }
11474   if (GET_CODE (rtl) == ROTATE)
11475     {
11476       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11477       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11478                                           GET_MODE_BITSIZE (mode), 0));
11479     }
11480   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11481   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11482   return ret;
11483 }
11484
11485 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11486    for DEBUG_PARAMETER_REF RTL.  */
11487
11488 static dw_loc_descr_ref
11489 parameter_ref_descriptor (rtx rtl)
11490 {
11491   dw_loc_descr_ref ret;
11492   dw_die_ref ref;
11493
11494   if (dwarf_strict)
11495     return NULL;
11496   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11497   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11498   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11499   if (ref)
11500     {
11501       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11502       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11503       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11504     }
11505   else
11506     {
11507       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11508       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11509     }
11510   return ret;
11511 }
11512
11513 /* Helper function to get mode of MEM's address.  */
11514
11515 enum machine_mode
11516 get_address_mode (rtx mem)
11517 {
11518   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11519   if (mode != VOIDmode)
11520     return mode;
11521   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11522 }
11523
11524 /* The following routine converts the RTL for a variable or parameter
11525    (resident in memory) into an equivalent Dwarf representation of a
11526    mechanism for getting the address of that same variable onto the top of a
11527    hypothetical "address evaluation" stack.
11528
11529    When creating memory location descriptors, we are effectively transforming
11530    the RTL for a memory-resident object into its Dwarf postfix expression
11531    equivalent.  This routine recursively descends an RTL tree, turning
11532    it into Dwarf postfix code as it goes.
11533
11534    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11535
11536    MEM_MODE is the mode of the memory reference, needed to handle some
11537    autoincrement addressing modes.
11538
11539    Return 0 if we can't represent the location.  */
11540
11541 dw_loc_descr_ref
11542 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11543                     enum machine_mode mem_mode,
11544                     enum var_init_status initialized)
11545 {
11546   dw_loc_descr_ref mem_loc_result = NULL;
11547   enum dwarf_location_atom op;
11548   dw_loc_descr_ref op0, op1;
11549
11550   if (mode == VOIDmode)
11551     mode = GET_MODE (rtl);
11552
11553   /* Note that for a dynamically sized array, the location we will generate a
11554      description of here will be the lowest numbered location which is
11555      actually within the array.  That's *not* necessarily the same as the
11556      zeroth element of the array.  */
11557
11558   rtl = targetm.delegitimize_address (rtl);
11559
11560   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11561     return NULL;
11562
11563   switch (GET_CODE (rtl))
11564     {
11565     case POST_INC:
11566     case POST_DEC:
11567     case POST_MODIFY:
11568       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11569
11570     case SUBREG:
11571       /* The case of a subreg may arise when we have a local (register)
11572          variable or a formal (register) parameter which doesn't quite fill
11573          up an entire register.  For now, just assume that it is
11574          legitimate to make the Dwarf info refer to the whole register which
11575          contains the given subreg.  */
11576       if (!subreg_lowpart_p (rtl))
11577         break;
11578       if (GET_MODE_CLASS (mode) == MODE_INT
11579           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11580           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11581 #ifdef POINTERS_EXTEND_UNSIGNED
11582               || (mode == Pmode && mem_mode != VOIDmode)
11583 #endif
11584              )
11585           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11586         {
11587           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11588                                                GET_MODE (SUBREG_REG (rtl)),
11589                                                mem_mode, initialized);
11590           break;
11591         }
11592       if (dwarf_strict)
11593         break;
11594       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11595         break;
11596       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11597           && (GET_MODE_CLASS (mode) != MODE_INT
11598               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11599         break;
11600       else
11601         {
11602           dw_die_ref type_die;
11603           dw_loc_descr_ref cvt;
11604
11605           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11606                                                GET_MODE (SUBREG_REG (rtl)),
11607                                                mem_mode, initialized);
11608           if (mem_loc_result == NULL)
11609             break;
11610           type_die = base_type_for_mode (mode,
11611                                          GET_MODE_CLASS (mode) == MODE_INT);
11612           if (type_die == NULL)
11613             {
11614               mem_loc_result = NULL;
11615               break;
11616             }
11617           if (GET_MODE_SIZE (mode)
11618               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11619             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11620           else
11621             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11622           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11623           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11624           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11625           add_loc_descr (&mem_loc_result, cvt);
11626         }
11627       break;
11628
11629     case REG:
11630       if (GET_MODE_CLASS (mode) != MODE_INT
11631           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11632 #ifdef POINTERS_EXTEND_UNSIGNED
11633               && (mode != Pmode || mem_mode == VOIDmode)
11634 #endif
11635               ))
11636         {
11637           dw_die_ref type_die;
11638
11639           if (dwarf_strict)
11640             break;
11641           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11642             break;
11643           type_die = base_type_for_mode (mode,
11644                                          GET_MODE_CLASS (mode) == MODE_INT);
11645           if (type_die == NULL)
11646             break;
11647           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11648                                           dbx_reg_number (rtl), 0);
11649           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11650           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11651           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11652           break;
11653         }
11654       /* Whenever a register number forms a part of the description of the
11655          method for calculating the (dynamic) address of a memory resident
11656          object, DWARF rules require the register number be referred to as
11657          a "base register".  This distinction is not based in any way upon
11658          what category of register the hardware believes the given register
11659          belongs to.  This is strictly DWARF terminology we're dealing with
11660          here. Note that in cases where the location of a memory-resident
11661          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11662          OP_CONST (0)) the actual DWARF location descriptor that we generate
11663          may just be OP_BASEREG (basereg).  This may look deceptively like
11664          the object in question was allocated to a register (rather than in
11665          memory) so DWARF consumers need to be aware of the subtle
11666          distinction between OP_REG and OP_BASEREG.  */
11667       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11668         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11669       else if (stack_realign_drap
11670                && crtl->drap_reg
11671                && crtl->args.internal_arg_pointer == rtl
11672                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11673         {
11674           /* If RTL is internal_arg_pointer, which has been optimized
11675              out, use DRAP instead.  */
11676           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11677                                             VAR_INIT_STATUS_INITIALIZED);
11678         }
11679       break;
11680
11681     case SIGN_EXTEND:
11682     case ZERO_EXTEND:
11683       if (GET_MODE_CLASS (mode) != MODE_INT)
11684         break;
11685       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11686                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11687       if (op0 == 0)
11688         break;
11689       else if (GET_CODE (rtl) == ZERO_EXTEND
11690                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11691                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11692                   < HOST_BITS_PER_WIDE_INT
11693                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11694                   to expand zero extend as two shifts instead of
11695                   masking.  */
11696                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11697         {
11698           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11699           mem_loc_result = op0;
11700           add_loc_descr (&mem_loc_result,
11701                          int_loc_descriptor (GET_MODE_MASK (imode)));
11702           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11703         }
11704       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11705         {
11706           int shift = DWARF2_ADDR_SIZE
11707                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11708           shift *= BITS_PER_UNIT;
11709           if (GET_CODE (rtl) == SIGN_EXTEND)
11710             op = DW_OP_shra;
11711           else
11712             op = DW_OP_shr;
11713           mem_loc_result = op0;
11714           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11715           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11716           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11717           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11718         }
11719       else if (!dwarf_strict)
11720         {
11721           dw_die_ref type_die1, type_die2;
11722           dw_loc_descr_ref cvt;
11723
11724           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11725                                           GET_CODE (rtl) == ZERO_EXTEND);
11726           if (type_die1 == NULL)
11727             break;
11728           type_die2 = base_type_for_mode (mode, 1);
11729           if (type_die2 == NULL)
11730             break;
11731           mem_loc_result = op0;
11732           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11733           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11734           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11735           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11736           add_loc_descr (&mem_loc_result, cvt);
11737           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11738           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11739           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11740           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11741           add_loc_descr (&mem_loc_result, cvt);
11742         }
11743       break;
11744
11745     case MEM:
11746       {
11747         rtx new_rtl = avoid_constant_pool_reference (rtl);
11748         if (new_rtl != rtl)
11749           {
11750             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11751                                                  initialized);
11752             if (mem_loc_result != NULL)
11753               return mem_loc_result;
11754           }
11755       }
11756       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11757                                            get_address_mode (rtl), mode,
11758                                            VAR_INIT_STATUS_INITIALIZED);
11759       if (mem_loc_result == NULL)
11760         mem_loc_result = tls_mem_loc_descriptor (rtl);
11761       if (mem_loc_result != NULL)
11762         {
11763           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11764               || GET_MODE_CLASS (mode) != MODE_INT)
11765             {
11766               dw_die_ref type_die;
11767               dw_loc_descr_ref deref;
11768
11769               if (dwarf_strict)
11770                 return NULL;
11771               type_die
11772                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11773               if (type_die == NULL)
11774                 return NULL;
11775               deref = new_loc_descr (DW_OP_GNU_deref_type,
11776                                      GET_MODE_SIZE (mode), 0);
11777               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11778               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11779               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11780               add_loc_descr (&mem_loc_result, deref);
11781             }
11782           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11783             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11784           else
11785             add_loc_descr (&mem_loc_result,
11786                            new_loc_descr (DW_OP_deref_size,
11787                                           GET_MODE_SIZE (mode), 0));
11788         }
11789       break;
11790
11791     case LO_SUM:
11792       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11793
11794     case LABEL_REF:
11795       /* Some ports can transform a symbol ref into a label ref, because
11796          the symbol ref is too far away and has to be dumped into a constant
11797          pool.  */
11798     case CONST:
11799     case SYMBOL_REF:
11800       if (GET_MODE_CLASS (mode) != MODE_INT
11801           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11802 #ifdef POINTERS_EXTEND_UNSIGNED
11803               && (mode != Pmode || mem_mode == VOIDmode)
11804 #endif
11805               ))
11806         break;
11807       if (GET_CODE (rtl) == SYMBOL_REF
11808           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11809         {
11810           dw_loc_descr_ref temp;
11811
11812           /* If this is not defined, we have no way to emit the data.  */
11813           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11814             break;
11815
11816           /* We used to emit DW_OP_addr here, but that's wrong, since
11817              DW_OP_addr should be relocated by the debug info consumer,
11818              while DW_OP_GNU_push_tls_address operand should not.  */
11819           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11820                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11821           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11822           temp->dw_loc_oprnd1.v.val_addr = rtl;
11823           temp->dtprel = true;
11824
11825           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11826           add_loc_descr (&mem_loc_result, temp);
11827
11828           break;
11829         }
11830
11831       if (!const_ok_for_output (rtl))
11832         break;
11833
11834     symref:
11835       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11836       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11837       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11838       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11839       break;
11840
11841     case CONCAT:
11842     case CONCATN:
11843     case VAR_LOCATION:
11844     case DEBUG_IMPLICIT_PTR:
11845       expansion_failed (NULL_TREE, rtl,
11846                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11847       return 0;
11848
11849     case ENTRY_VALUE:
11850       if (dwarf_strict)
11851         return NULL;
11852       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11853         {
11854           if (GET_MODE_CLASS (mode) != MODE_INT
11855               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11856             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11857                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11858           else
11859             op0
11860               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11861                                         VAR_INIT_STATUS_INITIALIZED);
11862         }
11863       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11864                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11865         {
11866           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11867                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11868           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11869             return NULL;
11870         }
11871       else
11872         gcc_unreachable ();
11873       if (op0 == NULL)
11874         return NULL;
11875       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11876       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11877       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11878       break;
11879
11880     case DEBUG_PARAMETER_REF:
11881       mem_loc_result = parameter_ref_descriptor (rtl);
11882       break;
11883
11884     case PRE_MODIFY:
11885       /* Extract the PLUS expression nested inside and fall into
11886          PLUS code below.  */
11887       rtl = XEXP (rtl, 1);
11888       goto plus;
11889
11890     case PRE_INC:
11891     case PRE_DEC:
11892       /* Turn these into a PLUS expression and fall into the PLUS code
11893          below.  */
11894       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11895                           GEN_INT (GET_CODE (rtl) == PRE_INC
11896                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11897                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11898
11899       /* ... fall through ...  */
11900
11901     case PLUS:
11902     plus:
11903       if (is_based_loc (rtl)
11904           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11905           && GET_MODE_CLASS (mode) == MODE_INT)
11906         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11907                                           INTVAL (XEXP (rtl, 1)),
11908                                           VAR_INIT_STATUS_INITIALIZED);
11909       else
11910         {
11911           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11912                                                VAR_INIT_STATUS_INITIALIZED);
11913           if (mem_loc_result == 0)
11914             break;
11915
11916           if (CONST_INT_P (XEXP (rtl, 1))
11917               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11918             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11919           else
11920             {
11921               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11922                                         VAR_INIT_STATUS_INITIALIZED);
11923               if (op1 == 0)
11924                 break;
11925               add_loc_descr (&mem_loc_result, op1);
11926               add_loc_descr (&mem_loc_result,
11927                              new_loc_descr (DW_OP_plus, 0, 0));
11928             }
11929         }
11930       break;
11931
11932     /* If a pseudo-reg is optimized away, it is possible for it to
11933        be replaced with a MEM containing a multiply or shift.  */
11934     case MINUS:
11935       op = DW_OP_minus;
11936       goto do_binop;
11937
11938     case MULT:
11939       op = DW_OP_mul;
11940       goto do_binop;
11941
11942     case DIV:
11943       if (!dwarf_strict
11944           && GET_MODE_CLASS (mode) == MODE_INT
11945           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11946         {
11947           mem_loc_result = typed_binop (DW_OP_div, rtl,
11948                                         base_type_for_mode (mode, 0),
11949                                         mode, mem_mode);
11950           break;
11951         }
11952       op = DW_OP_div;
11953       goto do_binop;
11954
11955     case UMOD:
11956       op = DW_OP_mod;
11957       goto do_binop;
11958
11959     case ASHIFT:
11960       op = DW_OP_shl;
11961       goto do_shift;
11962
11963     case ASHIFTRT:
11964       op = DW_OP_shra;
11965       goto do_shift;
11966
11967     case LSHIFTRT:
11968       op = DW_OP_shr;
11969       goto do_shift;
11970
11971     do_shift:
11972       if (GET_MODE_CLASS (mode) != MODE_INT)
11973         break;
11974       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11975                                 VAR_INIT_STATUS_INITIALIZED);
11976       {
11977         rtx rtlop1 = XEXP (rtl, 1);
11978         if (GET_MODE (rtlop1) != VOIDmode
11979             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11980                < GET_MODE_BITSIZE (mode))
11981           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11982         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11983                                   VAR_INIT_STATUS_INITIALIZED);
11984       }
11985
11986       if (op0 == 0 || op1 == 0)
11987         break;
11988
11989       mem_loc_result = op0;
11990       add_loc_descr (&mem_loc_result, op1);
11991       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11992       break;
11993
11994     case AND:
11995       op = DW_OP_and;
11996       goto do_binop;
11997
11998     case IOR:
11999       op = DW_OP_or;
12000       goto do_binop;
12001
12002     case XOR:
12003       op = DW_OP_xor;
12004       goto do_binop;
12005
12006     do_binop:
12007       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12008                                 VAR_INIT_STATUS_INITIALIZED);
12009       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12010                                 VAR_INIT_STATUS_INITIALIZED);
12011
12012       if (op0 == 0 || op1 == 0)
12013         break;
12014
12015       mem_loc_result = op0;
12016       add_loc_descr (&mem_loc_result, op1);
12017       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12018       break;
12019
12020     case MOD:
12021       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12022         {
12023           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12024                                         base_type_for_mode (mode, 0),
12025                                         mode, mem_mode);
12026           break;
12027         }
12028
12029       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12030                                 VAR_INIT_STATUS_INITIALIZED);
12031       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12032                                 VAR_INIT_STATUS_INITIALIZED);
12033
12034       if (op0 == 0 || op1 == 0)
12035         break;
12036
12037       mem_loc_result = op0;
12038       add_loc_descr (&mem_loc_result, op1);
12039       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12040       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12041       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12042       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12043       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12044       break;
12045
12046     case UDIV:
12047       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12048         {
12049           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12050             {
12051               op = DW_OP_div;
12052               goto do_binop;
12053             }
12054           mem_loc_result = typed_binop (DW_OP_div, rtl,
12055                                         base_type_for_mode (mode, 1),
12056                                         mode, mem_mode);
12057         }
12058       break;
12059
12060     case NOT:
12061       op = DW_OP_not;
12062       goto do_unop;
12063
12064     case ABS:
12065       op = DW_OP_abs;
12066       goto do_unop;
12067
12068     case NEG:
12069       op = DW_OP_neg;
12070       goto do_unop;
12071
12072     do_unop:
12073       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12074                                 VAR_INIT_STATUS_INITIALIZED);
12075
12076       if (op0 == 0)
12077         break;
12078
12079       mem_loc_result = op0;
12080       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12081       break;
12082
12083     case CONST_INT:
12084       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12085 #ifdef POINTERS_EXTEND_UNSIGNED
12086           || (mode == Pmode
12087               && mem_mode != VOIDmode
12088               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12089 #endif
12090           )
12091         {
12092           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12093           break;
12094         }
12095       if (!dwarf_strict
12096           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12097               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12098         {
12099           dw_die_ref type_die = base_type_for_mode (mode, 1);
12100           enum machine_mode amode;
12101           if (type_die == NULL)
12102             return NULL;
12103           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12104                                  MODE_INT, 0);
12105           if (INTVAL (rtl) >= 0
12106               && amode != BLKmode
12107               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12108               /* const DW_OP_GNU_convert <XXX> vs.
12109                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12110               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12111                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12112             {
12113               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12114               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12115               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12116               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12117               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12118               add_loc_descr (&mem_loc_result, op0);
12119               return mem_loc_result;
12120             }
12121           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12122                                           INTVAL (rtl));
12123           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12124           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12125           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12126           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12127             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12128           else
12129             {
12130               mem_loc_result->dw_loc_oprnd2.val_class
12131                 = dw_val_class_const_double;
12132               mem_loc_result->dw_loc_oprnd2.v.val_double
12133                 = shwi_to_double_int (INTVAL (rtl));
12134             }
12135         }
12136       break;
12137
12138     case CONST_DOUBLE:
12139       if (!dwarf_strict)
12140         {
12141           dw_die_ref type_die;
12142
12143           /* Note that a CONST_DOUBLE rtx could represent either an integer
12144              or a floating-point constant.  A CONST_DOUBLE is used whenever
12145              the constant requires more than one word in order to be
12146              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12147           if (mode == VOIDmode
12148               || (GET_MODE (rtl) == VOIDmode
12149                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12150             break;
12151           type_die = base_type_for_mode (mode,
12152                                          GET_MODE_CLASS (mode) == MODE_INT);
12153           if (type_die == NULL)
12154             return NULL;
12155           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12156           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12157           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12158           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12159           if (SCALAR_FLOAT_MODE_P (mode))
12160             {
12161               unsigned int length = GET_MODE_SIZE (mode);
12162               unsigned char *array
12163                   = (unsigned char*) ggc_alloc_atomic (length);
12164
12165               insert_float (rtl, array);
12166               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12167               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12168               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12169               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12170             }
12171           else
12172             {
12173               mem_loc_result->dw_loc_oprnd2.val_class
12174                 = dw_val_class_const_double;
12175               mem_loc_result->dw_loc_oprnd2.v.val_double
12176                 = rtx_to_double_int (rtl);
12177             }
12178         }
12179       break;
12180
12181     case EQ:
12182       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12183       break;
12184
12185     case GE:
12186       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12187       break;
12188
12189     case GT:
12190       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12191       break;
12192
12193     case LE:
12194       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12195       break;
12196
12197     case LT:
12198       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12199       break;
12200
12201     case NE:
12202       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12203       break;
12204
12205     case GEU:
12206       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12207       break;
12208
12209     case GTU:
12210       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12211       break;
12212
12213     case LEU:
12214       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12215       break;
12216
12217     case LTU:
12218       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12219       break;
12220
12221     case UMIN:
12222     case UMAX:
12223       if (GET_MODE_CLASS (mode) != MODE_INT)
12224         break;
12225       /* FALLTHRU */
12226     case SMIN:
12227     case SMAX:
12228       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12229       break;
12230
12231     case ZERO_EXTRACT:
12232     case SIGN_EXTRACT:
12233       if (CONST_INT_P (XEXP (rtl, 1))
12234           && CONST_INT_P (XEXP (rtl, 2))
12235           && ((unsigned) INTVAL (XEXP (rtl, 1))
12236               + (unsigned) INTVAL (XEXP (rtl, 2))
12237               <= GET_MODE_BITSIZE (mode))
12238           && GET_MODE_CLASS (mode) == MODE_INT
12239           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12240           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12241         {
12242           int shift, size;
12243           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12244                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12245           if (op0 == 0)
12246             break;
12247           if (GET_CODE (rtl) == SIGN_EXTRACT)
12248             op = DW_OP_shra;
12249           else
12250             op = DW_OP_shr;
12251           mem_loc_result = op0;
12252           size = INTVAL (XEXP (rtl, 1));
12253           shift = INTVAL (XEXP (rtl, 2));
12254           if (BITS_BIG_ENDIAN)
12255             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12256                     - shift - size;
12257           if (shift + size != (int) DWARF2_ADDR_SIZE)
12258             {
12259               add_loc_descr (&mem_loc_result,
12260                              int_loc_descriptor (DWARF2_ADDR_SIZE
12261                                                  - shift - size));
12262               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12263             }
12264           if (size != (int) DWARF2_ADDR_SIZE)
12265             {
12266               add_loc_descr (&mem_loc_result,
12267                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12268               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12269             }
12270         }
12271       break;
12272
12273     case IF_THEN_ELSE:
12274       {
12275         dw_loc_descr_ref op2, bra_node, drop_node;
12276         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12277                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12278                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12279                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12280         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12281                                   VAR_INIT_STATUS_INITIALIZED);
12282         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12283                                   VAR_INIT_STATUS_INITIALIZED);
12284         if (op0 == NULL || op1 == NULL || op2 == NULL)
12285           break;
12286
12287         mem_loc_result = op1;
12288         add_loc_descr (&mem_loc_result, op2);
12289         add_loc_descr (&mem_loc_result, op0);
12290         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12291         add_loc_descr (&mem_loc_result, bra_node);
12292         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12293         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12294         add_loc_descr (&mem_loc_result, drop_node);
12295         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12296         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12297       }
12298       break;
12299
12300     case FLOAT_EXTEND:
12301     case FLOAT_TRUNCATE:
12302     case FLOAT:
12303     case UNSIGNED_FLOAT:
12304     case FIX:
12305     case UNSIGNED_FIX:
12306       if (!dwarf_strict)
12307         {
12308           dw_die_ref type_die;
12309           dw_loc_descr_ref cvt;
12310
12311           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12312                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12313           if (op0 == NULL)
12314             break;
12315           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12316               && (GET_CODE (rtl) == FLOAT
12317                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12318                      <= DWARF2_ADDR_SIZE))
12319             {
12320               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12321                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12322               if (type_die == NULL)
12323                 break;
12324               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12325               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12326               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12327               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12328               add_loc_descr (&op0, cvt);
12329             }
12330           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12331           if (type_die == NULL)
12332             break;
12333           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12334           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12335           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12336           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12337           add_loc_descr (&op0, cvt);
12338           if (GET_MODE_CLASS (mode) == MODE_INT
12339               && (GET_CODE (rtl) == FIX
12340                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12341             {
12342               op0 = convert_descriptor_to_mode (mode, op0);
12343               if (op0 == NULL)
12344                 break;
12345             }
12346           mem_loc_result = op0;
12347         }
12348       break;
12349
12350     case CLZ:
12351     case CTZ:
12352     case FFS:
12353       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12354       break;
12355
12356     case POPCOUNT:
12357     case PARITY:
12358       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12359       break;
12360
12361     case BSWAP:
12362       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12363       break;
12364
12365     case ROTATE:
12366     case ROTATERT:
12367       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12368       break;
12369
12370     case COMPARE:
12371     case TRUNCATE:
12372       /* In theory, we could implement the above.  */
12373       /* DWARF cannot represent the unsigned compare operations
12374          natively.  */
12375     case SS_MULT:
12376     case US_MULT:
12377     case SS_DIV:
12378     case US_DIV:
12379     case SS_PLUS:
12380     case US_PLUS:
12381     case SS_MINUS:
12382     case US_MINUS:
12383     case SS_NEG:
12384     case US_NEG:
12385     case SS_ABS:
12386     case SS_ASHIFT:
12387     case US_ASHIFT:
12388     case SS_TRUNCATE:
12389     case US_TRUNCATE:
12390     case UNORDERED:
12391     case ORDERED:
12392     case UNEQ:
12393     case UNGE:
12394     case UNGT:
12395     case UNLE:
12396     case UNLT:
12397     case LTGT:
12398     case FRACT_CONVERT:
12399     case UNSIGNED_FRACT_CONVERT:
12400     case SAT_FRACT:
12401     case UNSIGNED_SAT_FRACT:
12402     case SQRT:
12403     case ASM_OPERANDS:
12404     case VEC_MERGE:
12405     case VEC_SELECT:
12406     case VEC_CONCAT:
12407     case VEC_DUPLICATE:
12408     case UNSPEC:
12409     case HIGH:
12410     case FMA:
12411     case STRICT_LOW_PART:
12412     case CONST_VECTOR:
12413     case CONST_FIXED:
12414     case CLRSB:
12415       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12416          can't express it in the debug info.  This can happen e.g. with some
12417          TLS UNSPECs.  */
12418       break;
12419
12420     case CONST_STRING:
12421       resolve_one_addr (&rtl, NULL);
12422       goto symref;
12423
12424     default:
12425 #ifdef ENABLE_CHECKING
12426       print_rtl (stderr, rtl);
12427       gcc_unreachable ();
12428 #else
12429       break;
12430 #endif
12431     }
12432
12433   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12434     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12435
12436   return mem_loc_result;
12437 }
12438
12439 /* Return a descriptor that describes the concatenation of two locations.
12440    This is typically a complex variable.  */
12441
12442 static dw_loc_descr_ref
12443 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12444 {
12445   dw_loc_descr_ref cc_loc_result = NULL;
12446   dw_loc_descr_ref x0_ref
12447     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12448   dw_loc_descr_ref x1_ref
12449     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12450
12451   if (x0_ref == 0 || x1_ref == 0)
12452     return 0;
12453
12454   cc_loc_result = x0_ref;
12455   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12456
12457   add_loc_descr (&cc_loc_result, x1_ref);
12458   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12459
12460   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12461     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12462
12463   return cc_loc_result;
12464 }
12465
12466 /* Return a descriptor that describes the concatenation of N
12467    locations.  */
12468
12469 static dw_loc_descr_ref
12470 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12471 {
12472   unsigned int i;
12473   dw_loc_descr_ref cc_loc_result = NULL;
12474   unsigned int n = XVECLEN (concatn, 0);
12475
12476   for (i = 0; i < n; ++i)
12477     {
12478       dw_loc_descr_ref ref;
12479       rtx x = XVECEXP (concatn, 0, i);
12480
12481       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12482       if (ref == NULL)
12483         return NULL;
12484
12485       add_loc_descr (&cc_loc_result, ref);
12486       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12487     }
12488
12489   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12490     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12491
12492   return cc_loc_result;
12493 }
12494
12495 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12496    for DEBUG_IMPLICIT_PTR RTL.  */
12497
12498 static dw_loc_descr_ref
12499 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12500 {
12501   dw_loc_descr_ref ret;
12502   dw_die_ref ref;
12503
12504   if (dwarf_strict)
12505     return NULL;
12506   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12507               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12508               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12509   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12510   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12511   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12512   if (ref)
12513     {
12514       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12515       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12516       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12517     }
12518   else
12519     {
12520       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12521       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12522     }
12523   return ret;
12524 }
12525
12526 /* Output a proper Dwarf location descriptor for a variable or parameter
12527    which is either allocated in a register or in a memory location.  For a
12528    register, we just generate an OP_REG and the register number.  For a
12529    memory location we provide a Dwarf postfix expression describing how to
12530    generate the (dynamic) address of the object onto the address stack.
12531
12532    MODE is mode of the decl if this loc_descriptor is going to be used in
12533    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12534    allowed, VOIDmode otherwise.
12535
12536    If we don't know how to describe it, return 0.  */
12537
12538 static dw_loc_descr_ref
12539 loc_descriptor (rtx rtl, enum machine_mode mode,
12540                 enum var_init_status initialized)
12541 {
12542   dw_loc_descr_ref loc_result = NULL;
12543
12544   switch (GET_CODE (rtl))
12545     {
12546     case SUBREG:
12547       /* The case of a subreg may arise when we have a local (register)
12548          variable or a formal (register) parameter which doesn't quite fill
12549          up an entire register.  For now, just assume that it is
12550          legitimate to make the Dwarf info refer to the whole register which
12551          contains the given subreg.  */
12552       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12553         loc_result = loc_descriptor (SUBREG_REG (rtl),
12554                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12555       else
12556         goto do_default;
12557       break;
12558
12559     case REG:
12560       loc_result = reg_loc_descriptor (rtl, initialized);
12561       break;
12562
12563     case MEM:
12564       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12565                                        GET_MODE (rtl), initialized);
12566       if (loc_result == NULL)
12567         loc_result = tls_mem_loc_descriptor (rtl);
12568       if (loc_result == NULL)
12569         {
12570           rtx new_rtl = avoid_constant_pool_reference (rtl);
12571           if (new_rtl != rtl)
12572             loc_result = loc_descriptor (new_rtl, mode, initialized);
12573         }
12574       break;
12575
12576     case CONCAT:
12577       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12578                                           initialized);
12579       break;
12580
12581     case CONCATN:
12582       loc_result = concatn_loc_descriptor (rtl, initialized);
12583       break;
12584
12585     case VAR_LOCATION:
12586       /* Single part.  */
12587       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12588         {
12589           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12590           if (GET_CODE (loc) == EXPR_LIST)
12591             loc = XEXP (loc, 0);
12592           loc_result = loc_descriptor (loc, mode, initialized);
12593           break;
12594         }
12595
12596       rtl = XEXP (rtl, 1);
12597       /* FALLTHRU */
12598
12599     case PARALLEL:
12600       {
12601         rtvec par_elems = XVEC (rtl, 0);
12602         int num_elem = GET_NUM_ELEM (par_elems);
12603         enum machine_mode mode;
12604         int i;
12605
12606         /* Create the first one, so we have something to add to.  */
12607         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12608                                      VOIDmode, initialized);
12609         if (loc_result == NULL)
12610           return NULL;
12611         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12612         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12613         for (i = 1; i < num_elem; i++)
12614           {
12615             dw_loc_descr_ref temp;
12616
12617             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12618                                    VOIDmode, initialized);
12619             if (temp == NULL)
12620               return NULL;
12621             add_loc_descr (&loc_result, temp);
12622             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12623             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12624           }
12625       }
12626       break;
12627
12628     case CONST_INT:
12629       if (mode != VOIDmode && mode != BLKmode)
12630         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12631                                                     INTVAL (rtl));
12632       break;
12633
12634     case CONST_DOUBLE:
12635       if (mode == VOIDmode)
12636         mode = GET_MODE (rtl);
12637
12638       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12639         {
12640           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12641
12642           /* Note that a CONST_DOUBLE rtx could represent either an integer
12643              or a floating-point constant.  A CONST_DOUBLE is used whenever
12644              the constant requires more than one word in order to be
12645              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12646           loc_result = new_loc_descr (DW_OP_implicit_value,
12647                                       GET_MODE_SIZE (mode), 0);
12648           if (SCALAR_FLOAT_MODE_P (mode))
12649             {
12650               unsigned int length = GET_MODE_SIZE (mode);
12651               unsigned char *array
12652                   = (unsigned char*) ggc_alloc_atomic (length);
12653
12654               insert_float (rtl, array);
12655               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12656               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12657               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12658               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12659             }
12660           else
12661             {
12662               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12663               loc_result->dw_loc_oprnd2.v.val_double
12664                 = rtx_to_double_int (rtl);
12665             }
12666         }
12667       break;
12668
12669     case CONST_VECTOR:
12670       if (mode == VOIDmode)
12671         mode = GET_MODE (rtl);
12672
12673       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12674         {
12675           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12676           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12677           unsigned char *array = (unsigned char *)
12678             ggc_alloc_atomic (length * elt_size);
12679           unsigned int i;
12680           unsigned char *p;
12681
12682           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12683           switch (GET_MODE_CLASS (mode))
12684             {
12685             case MODE_VECTOR_INT:
12686               for (i = 0, p = array; i < length; i++, p += elt_size)
12687                 {
12688                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12689                   double_int val = rtx_to_double_int (elt);
12690
12691                   if (elt_size <= sizeof (HOST_WIDE_INT))
12692                     insert_int (double_int_to_shwi (val), elt_size, p);
12693                   else
12694                     {
12695                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12696                       insert_double (val, p);
12697                     }
12698                 }
12699               break;
12700
12701             case MODE_VECTOR_FLOAT:
12702               for (i = 0, p = array; i < length; i++, p += elt_size)
12703                 {
12704                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12705                   insert_float (elt, p);
12706                 }
12707               break;
12708
12709             default:
12710               gcc_unreachable ();
12711             }
12712
12713           loc_result = new_loc_descr (DW_OP_implicit_value,
12714                                       length * elt_size, 0);
12715           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12716           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12717           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12718           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12719         }
12720       break;
12721
12722     case CONST:
12723       if (mode == VOIDmode
12724           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12725           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12726           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12727         {
12728           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12729           break;
12730         }
12731       /* FALLTHROUGH */
12732     case SYMBOL_REF:
12733       if (!const_ok_for_output (rtl))
12734         break;
12735     case LABEL_REF:
12736       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12737           && (dwarf_version >= 4 || !dwarf_strict))
12738         {
12739           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12740           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12741           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12742           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12743           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12744         }
12745       break;
12746
12747     case DEBUG_IMPLICIT_PTR:
12748       loc_result = implicit_ptr_descriptor (rtl, 0);
12749       break;
12750
12751     case PLUS:
12752       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12753           && CONST_INT_P (XEXP (rtl, 1)))
12754         {
12755           loc_result
12756             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12757           break;
12758         }
12759       /* FALLTHRU */
12760     do_default:
12761     default:
12762       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12763            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12764            && dwarf_version >= 4)
12765           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12766         {
12767           /* Value expression.  */
12768           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12769           if (loc_result)
12770             add_loc_descr (&loc_result,
12771                            new_loc_descr (DW_OP_stack_value, 0, 0));
12772         }
12773       break;
12774     }
12775
12776   return loc_result;
12777 }
12778
12779 /* We need to figure out what section we should use as the base for the
12780    address ranges where a given location is valid.
12781    1. If this particular DECL has a section associated with it, use that.
12782    2. If this function has a section associated with it, use that.
12783    3. Otherwise, use the text section.
12784    XXX: If you split a variable across multiple sections, we won't notice.  */
12785
12786 static const char *
12787 secname_for_decl (const_tree decl)
12788 {
12789   const char *secname;
12790
12791   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12792     {
12793       tree sectree = DECL_SECTION_NAME (decl);
12794       secname = TREE_STRING_POINTER (sectree);
12795     }
12796   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12797     {
12798       tree sectree = DECL_SECTION_NAME (current_function_decl);
12799       secname = TREE_STRING_POINTER (sectree);
12800     }
12801   else if (cfun && in_cold_section_p)
12802     secname = crtl->subsections.cold_section_label;
12803   else
12804     secname = text_section_label;
12805
12806   return secname;
12807 }
12808
12809 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12810
12811 static bool
12812 decl_by_reference_p (tree decl)
12813 {
12814   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12815            || TREE_CODE (decl) == VAR_DECL)
12816           && DECL_BY_REFERENCE (decl));
12817 }
12818
12819 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12820    for VARLOC.  */
12821
12822 static dw_loc_descr_ref
12823 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12824                enum var_init_status initialized)
12825 {
12826   int have_address = 0;
12827   dw_loc_descr_ref descr;
12828   enum machine_mode mode;
12829
12830   if (want_address != 2)
12831     {
12832       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12833       /* Single part.  */
12834       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12835         {
12836           varloc = PAT_VAR_LOCATION_LOC (varloc);
12837           if (GET_CODE (varloc) == EXPR_LIST)
12838             varloc = XEXP (varloc, 0);
12839           mode = GET_MODE (varloc);
12840           if (MEM_P (varloc))
12841             {
12842               rtx addr = XEXP (varloc, 0);
12843               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12844                                           mode, initialized);
12845               if (descr)
12846                 have_address = 1;
12847               else
12848                 {
12849                   rtx x = avoid_constant_pool_reference (varloc);
12850                   if (x != varloc)
12851                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12852                                                 initialized);
12853                 }
12854             }
12855           else
12856             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12857         }
12858       else
12859         return 0;
12860     }
12861   else
12862     {
12863       if (GET_CODE (varloc) == VAR_LOCATION)
12864         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12865       else
12866         mode = DECL_MODE (loc);
12867       descr = loc_descriptor (varloc, mode, initialized);
12868       have_address = 1;
12869     }
12870
12871   if (!descr)
12872     return 0;
12873
12874   if (want_address == 2 && !have_address
12875       && (dwarf_version >= 4 || !dwarf_strict))
12876     {
12877       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12878         {
12879           expansion_failed (loc, NULL_RTX,
12880                             "DWARF address size mismatch");
12881           return 0;
12882         }
12883       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12884       have_address = 1;
12885     }
12886   /* Show if we can't fill the request for an address.  */
12887   if (want_address && !have_address)
12888     {
12889       expansion_failed (loc, NULL_RTX,
12890                         "Want address and only have value");
12891       return 0;
12892     }
12893
12894   /* If we've got an address and don't want one, dereference.  */
12895   if (!want_address && have_address)
12896     {
12897       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12898       enum dwarf_location_atom op;
12899
12900       if (size > DWARF2_ADDR_SIZE || size == -1)
12901         {
12902           expansion_failed (loc, NULL_RTX,
12903                             "DWARF address size mismatch");
12904           return 0;
12905         }
12906       else if (size == DWARF2_ADDR_SIZE)
12907         op = DW_OP_deref;
12908       else
12909         op = DW_OP_deref_size;
12910
12911       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12912     }
12913
12914   return descr;
12915 }
12916
12917 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12918    if it is not possible.  */
12919
12920 static dw_loc_descr_ref
12921 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12922 {
12923   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12924     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12925   else if (dwarf_version >= 3 || !dwarf_strict)
12926     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12927   else
12928     return NULL;
12929 }
12930
12931 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12932    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12933
12934 static dw_loc_descr_ref
12935 dw_sra_loc_expr (tree decl, rtx loc)
12936 {
12937   rtx p;
12938   unsigned int padsize = 0;
12939   dw_loc_descr_ref descr, *descr_tail;
12940   unsigned HOST_WIDE_INT decl_size;
12941   rtx varloc;
12942   enum var_init_status initialized;
12943
12944   if (DECL_SIZE (decl) == NULL
12945       || !host_integerp (DECL_SIZE (decl), 1))
12946     return NULL;
12947
12948   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12949   descr = NULL;
12950   descr_tail = &descr;
12951
12952   for (p = loc; p; p = XEXP (p, 1))
12953     {
12954       unsigned int bitsize = decl_piece_bitsize (p);
12955       rtx loc_note = *decl_piece_varloc_ptr (p);
12956       dw_loc_descr_ref cur_descr;
12957       dw_loc_descr_ref *tail, last = NULL;
12958       unsigned int opsize = 0;
12959
12960       if (loc_note == NULL_RTX
12961           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12962         {
12963           padsize += bitsize;
12964           continue;
12965         }
12966       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12967       varloc = NOTE_VAR_LOCATION (loc_note);
12968       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12969       if (cur_descr == NULL)
12970         {
12971           padsize += bitsize;
12972           continue;
12973         }
12974
12975       /* Check that cur_descr either doesn't use
12976          DW_OP_*piece operations, or their sum is equal
12977          to bitsize.  Otherwise we can't embed it.  */
12978       for (tail = &cur_descr; *tail != NULL;
12979            tail = &(*tail)->dw_loc_next)
12980         if ((*tail)->dw_loc_opc == DW_OP_piece)
12981           {
12982             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12983                       * BITS_PER_UNIT;
12984             last = *tail;
12985           }
12986         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12987           {
12988             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12989             last = *tail;
12990           }
12991
12992       if (last != NULL && opsize != bitsize)
12993         {
12994           padsize += bitsize;
12995           continue;
12996         }
12997
12998       /* If there is a hole, add DW_OP_*piece after empty DWARF
12999          expression, which means that those bits are optimized out.  */
13000       if (padsize)
13001         {
13002           if (padsize > decl_size)
13003             return NULL;
13004           decl_size -= padsize;
13005           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13006           if (*descr_tail == NULL)
13007             return NULL;
13008           descr_tail = &(*descr_tail)->dw_loc_next;
13009           padsize = 0;
13010         }
13011       *descr_tail = cur_descr;
13012       descr_tail = tail;
13013       if (bitsize > decl_size)
13014         return NULL;
13015       decl_size -= bitsize;
13016       if (last == NULL)
13017         {
13018           HOST_WIDE_INT offset = 0;
13019           if (GET_CODE (varloc) == VAR_LOCATION
13020               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13021             {
13022               varloc = PAT_VAR_LOCATION_LOC (varloc);
13023               if (GET_CODE (varloc) == EXPR_LIST)
13024                 varloc = XEXP (varloc, 0);
13025             }
13026           do 
13027             {
13028               if (GET_CODE (varloc) == CONST
13029                   || GET_CODE (varloc) == SIGN_EXTEND
13030                   || GET_CODE (varloc) == ZERO_EXTEND)
13031                 varloc = XEXP (varloc, 0);
13032               else if (GET_CODE (varloc) == SUBREG)
13033                 varloc = SUBREG_REG (varloc);
13034               else
13035                 break;
13036             }
13037           while (1);
13038           /* DW_OP_bit_size offset should be zero for register
13039              or implicit location descriptions and empty location
13040              descriptions, but for memory addresses needs big endian
13041              adjustment.  */
13042           if (MEM_P (varloc))
13043             {
13044               unsigned HOST_WIDE_INT memsize
13045                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13046               if (memsize != bitsize)
13047                 {
13048                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13049                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13050                     return NULL;
13051                   if (memsize < bitsize)
13052                     return NULL;
13053                   if (BITS_BIG_ENDIAN)
13054                     offset = memsize - bitsize;
13055                 }
13056             }
13057
13058           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13059           if (*descr_tail == NULL)
13060             return NULL;
13061           descr_tail = &(*descr_tail)->dw_loc_next;
13062         }
13063     }
13064
13065   /* If there were any non-empty expressions, add padding till the end of
13066      the decl.  */
13067   if (descr != NULL && decl_size != 0)
13068     {
13069       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13070       if (*descr_tail == NULL)
13071         return NULL;
13072     }
13073   return descr;
13074 }
13075
13076 /* Return the dwarf representation of the location list LOC_LIST of
13077    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13078    function.  */
13079
13080 static dw_loc_list_ref
13081 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13082 {
13083   const char *endname, *secname;
13084   rtx varloc;
13085   enum var_init_status initialized;
13086   struct var_loc_node *node;
13087   dw_loc_descr_ref descr;
13088   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13089   dw_loc_list_ref list = NULL;
13090   dw_loc_list_ref *listp = &list;
13091
13092   /* Now that we know what section we are using for a base,
13093      actually construct the list of locations.
13094      The first location information is what is passed to the
13095      function that creates the location list, and the remaining
13096      locations just get added on to that list.
13097      Note that we only know the start address for a location
13098      (IE location changes), so to build the range, we use
13099      the range [current location start, next location start].
13100      This means we have to special case the last node, and generate
13101      a range of [last location start, end of function label].  */
13102
13103   secname = secname_for_decl (decl);
13104
13105   for (node = loc_list->first; node; node = node->next)
13106     if (GET_CODE (node->loc) == EXPR_LIST
13107         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13108       {
13109         if (GET_CODE (node->loc) == EXPR_LIST)
13110           {
13111             /* This requires DW_OP_{,bit_}piece, which is not usable
13112                inside DWARF expressions.  */
13113             if (want_address != 2)
13114               continue;
13115             descr = dw_sra_loc_expr (decl, node->loc);
13116             if (descr == NULL)
13117               continue;
13118           }
13119         else
13120           {
13121             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13122             varloc = NOTE_VAR_LOCATION (node->loc);
13123             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13124           }
13125         if (descr)
13126           {
13127             bool range_across_switch = false;
13128             /* If section switch happens in between node->label
13129                and node->next->label (or end of function) and
13130                we can't emit it as a single entry list,
13131                emit two ranges, first one ending at the end
13132                of first partition and second one starting at the
13133                beginning of second partition.  */
13134             if (node == loc_list->last_before_switch
13135                 && (node != loc_list->first || loc_list->first->next)
13136                 && current_function_decl)
13137               {
13138                 endname = cfun->fde->dw_fde_end;
13139                 range_across_switch = true;
13140               }
13141             /* The variable has a location between NODE->LABEL and
13142                NODE->NEXT->LABEL.  */
13143             else if (node->next)
13144               endname = node->next->label;
13145             /* If the variable has a location at the last label
13146                it keeps its location until the end of function.  */
13147             else if (!current_function_decl)
13148               endname = text_end_label;
13149             else
13150               {
13151                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13152                                              current_function_funcdef_no);
13153                 endname = ggc_strdup (label_id);
13154               }
13155
13156             *listp = new_loc_list (descr, node->label, endname, secname);
13157             if (TREE_CODE (decl) == PARM_DECL
13158                 && node == loc_list->first
13159                 && GET_CODE (node->loc) == NOTE
13160                 && strcmp (node->label, endname) == 0)
13161               (*listp)->force = true;
13162             listp = &(*listp)->dw_loc_next;
13163
13164             if (range_across_switch)
13165               {
13166                 if (GET_CODE (node->loc) == EXPR_LIST)
13167                   descr = dw_sra_loc_expr (decl, node->loc);
13168                 else
13169                   {
13170                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13171                     varloc = NOTE_VAR_LOCATION (node->loc);
13172                     descr = dw_loc_list_1 (decl, varloc, want_address,
13173                                            initialized);
13174                   }
13175                 gcc_assert (descr);
13176                 /* The variable has a location between NODE->LABEL and
13177                    NODE->NEXT->LABEL.  */
13178                 if (node->next)
13179                   endname = node->next->label;
13180                 else
13181                   endname = cfun->fde->dw_fde_second_end;
13182                 *listp = new_loc_list (descr,
13183                                        cfun->fde->dw_fde_second_begin,
13184                                        endname, secname);
13185                 listp = &(*listp)->dw_loc_next;
13186               }
13187           }
13188       }
13189
13190   /* Try to avoid the overhead of a location list emitting a location
13191      expression instead, but only if we didn't have more than one
13192      location entry in the first place.  If some entries were not
13193      representable, we don't want to pretend a single entry that was
13194      applies to the entire scope in which the variable is
13195      available.  */
13196   if (list && loc_list->first->next)
13197     gen_llsym (list);
13198
13199   return list;
13200 }
13201
13202 /* Return if the loc_list has only single element and thus can be represented
13203    as location description.   */
13204
13205 static bool
13206 single_element_loc_list_p (dw_loc_list_ref list)
13207 {
13208   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13209   return !list->ll_symbol;
13210 }
13211
13212 /* To each location in list LIST add loc descr REF.  */
13213
13214 static void
13215 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13216 {
13217   dw_loc_descr_ref copy;
13218   add_loc_descr (&list->expr, ref);
13219   list = list->dw_loc_next;
13220   while (list)
13221     {
13222       copy = ggc_alloc_dw_loc_descr_node ();
13223       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13224       add_loc_descr (&list->expr, copy);
13225       while (copy->dw_loc_next)
13226         {
13227           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13228           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13229           copy->dw_loc_next = new_copy;
13230           copy = new_copy;
13231         }
13232       list = list->dw_loc_next;
13233     }
13234 }
13235
13236 /* Given two lists RET and LIST
13237    produce location list that is result of adding expression in LIST
13238    to expression in RET on each possition in program.
13239    Might be destructive on both RET and LIST.
13240
13241    TODO: We handle only simple cases of RET or LIST having at most one
13242    element. General case would inolve sorting the lists in program order
13243    and merging them that will need some additional work.
13244    Adding that will improve quality of debug info especially for SRA-ed
13245    structures.  */
13246
13247 static void
13248 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13249 {
13250   if (!list)
13251     return;
13252   if (!*ret)
13253     {
13254       *ret = list;
13255       return;
13256     }
13257   if (!list->dw_loc_next)
13258     {
13259       add_loc_descr_to_each (*ret, list->expr);
13260       return;
13261     }
13262   if (!(*ret)->dw_loc_next)
13263     {
13264       add_loc_descr_to_each (list, (*ret)->expr);
13265       *ret = list;
13266       return;
13267     }
13268   expansion_failed (NULL_TREE, NULL_RTX,
13269                     "Don't know how to merge two non-trivial"
13270                     " location lists.\n");
13271   *ret = NULL;
13272   return;
13273 }
13274
13275 /* LOC is constant expression.  Try a luck, look it up in constant
13276    pool and return its loc_descr of its address.  */
13277
13278 static dw_loc_descr_ref
13279 cst_pool_loc_descr (tree loc)
13280 {
13281   /* Get an RTL for this, if something has been emitted.  */
13282   rtx rtl = lookup_constant_def (loc);
13283
13284   if (!rtl || !MEM_P (rtl))
13285     {
13286       gcc_assert (!rtl);
13287       return 0;
13288     }
13289   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13290
13291   /* TODO: We might get more coverage if we was actually delaying expansion
13292      of all expressions till end of compilation when constant pools are fully
13293      populated.  */
13294   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13295     {
13296       expansion_failed (loc, NULL_RTX,
13297                         "CST value in contant pool but not marked.");
13298       return 0;
13299     }
13300   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13301                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13302 }
13303
13304 /* Return dw_loc_list representing address of addr_expr LOC
13305    by looking for innder INDIRECT_REF expression and turing it
13306    into simple arithmetics.  */
13307
13308 static dw_loc_list_ref
13309 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13310 {
13311   tree obj, offset;
13312   HOST_WIDE_INT bitsize, bitpos, bytepos;
13313   enum machine_mode mode;
13314   int volatilep;
13315   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13316   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13317
13318   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13319                              &bitsize, &bitpos, &offset, &mode,
13320                              &unsignedp, &volatilep, false);
13321   STRIP_NOPS (obj);
13322   if (bitpos % BITS_PER_UNIT)
13323     {
13324       expansion_failed (loc, NULL_RTX, "bitfield access");
13325       return 0;
13326     }
13327   if (!INDIRECT_REF_P (obj))
13328     {
13329       expansion_failed (obj,
13330                         NULL_RTX, "no indirect ref in inner refrence");
13331       return 0;
13332     }
13333   if (!offset && !bitpos)
13334     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13335   else if (toplev
13336            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13337            && (dwarf_version >= 4 || !dwarf_strict))
13338     {
13339       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13340       if (!list_ret)
13341         return 0;
13342       if (offset)
13343         {
13344           /* Variable offset.  */
13345           list_ret1 = loc_list_from_tree (offset, 0);
13346           if (list_ret1 == 0)
13347             return 0;
13348           add_loc_list (&list_ret, list_ret1);
13349           if (!list_ret)
13350             return 0;
13351           add_loc_descr_to_each (list_ret,
13352                                  new_loc_descr (DW_OP_plus, 0, 0));
13353         }
13354       bytepos = bitpos / BITS_PER_UNIT;
13355       if (bytepos > 0)
13356         add_loc_descr_to_each (list_ret,
13357                                new_loc_descr (DW_OP_plus_uconst,
13358                                               bytepos, 0));
13359       else if (bytepos < 0)
13360         loc_list_plus_const (list_ret, bytepos);
13361       add_loc_descr_to_each (list_ret,
13362                              new_loc_descr (DW_OP_stack_value, 0, 0));
13363     }
13364   return list_ret;
13365 }
13366
13367
13368 /* Generate Dwarf location list representing LOC.
13369    If WANT_ADDRESS is false, expression computing LOC will be computed
13370    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13371    if WANT_ADDRESS is 2, expression computing address useable in location
13372      will be returned (i.e. DW_OP_reg can be used
13373      to refer to register values).  */
13374
13375 static dw_loc_list_ref
13376 loc_list_from_tree (tree loc, int want_address)
13377 {
13378   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13379   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13380   int have_address = 0;
13381   enum dwarf_location_atom op;
13382
13383   /* ??? Most of the time we do not take proper care for sign/zero
13384      extending the values properly.  Hopefully this won't be a real
13385      problem...  */
13386
13387   switch (TREE_CODE (loc))
13388     {
13389     case ERROR_MARK:
13390       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13391       return 0;
13392
13393     case PLACEHOLDER_EXPR:
13394       /* This case involves extracting fields from an object to determine the
13395          position of other fields.  We don't try to encode this here.  The
13396          only user of this is Ada, which encodes the needed information using
13397          the names of types.  */
13398       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13399       return 0;
13400
13401     case CALL_EXPR:
13402       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13403       /* There are no opcodes for these operations.  */
13404       return 0;
13405
13406     case PREINCREMENT_EXPR:
13407     case PREDECREMENT_EXPR:
13408     case POSTINCREMENT_EXPR:
13409     case POSTDECREMENT_EXPR:
13410       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13411       /* There are no opcodes for these operations.  */
13412       return 0;
13413
13414     case ADDR_EXPR:
13415       /* If we already want an address, see if there is INDIRECT_REF inside
13416          e.g. for &this->field.  */
13417       if (want_address)
13418         {
13419           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13420                        (loc, want_address == 2);
13421           if (list_ret)
13422             have_address = 1;
13423           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13424                    && (ret = cst_pool_loc_descr (loc)))
13425             have_address = 1;
13426         }
13427         /* Otherwise, process the argument and look for the address.  */
13428       if (!list_ret && !ret)
13429         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13430       else
13431         {
13432           if (want_address)
13433             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13434           return NULL;
13435         }
13436       break;
13437
13438     case VAR_DECL:
13439       if (DECL_THREAD_LOCAL_P (loc))
13440         {
13441           rtx rtl;
13442           enum dwarf_location_atom first_op;
13443           enum dwarf_location_atom second_op;
13444           bool dtprel = false;
13445
13446           if (targetm.have_tls)
13447             {
13448               /* If this is not defined, we have no way to emit the
13449                  data.  */
13450               if (!targetm.asm_out.output_dwarf_dtprel)
13451                 return 0;
13452
13453                /* The way DW_OP_GNU_push_tls_address is specified, we
13454                   can only look up addresses of objects in the current
13455                   module.  We used DW_OP_addr as first op, but that's
13456                   wrong, because DW_OP_addr is relocated by the debug
13457                   info consumer, while DW_OP_GNU_push_tls_address
13458                   operand shouldn't be.  */
13459               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13460                 return 0;
13461               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13462               dtprel = true;
13463               second_op = DW_OP_GNU_push_tls_address;
13464             }
13465           else
13466             {
13467               if (!targetm.emutls.debug_form_tls_address
13468                   || !(dwarf_version >= 3 || !dwarf_strict))
13469                 return 0;
13470               /* We stuffed the control variable into the DECL_VALUE_EXPR
13471                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13472                  no longer appear in gimple code.  We used the control
13473                  variable in specific so that we could pick it up here.  */
13474               loc = DECL_VALUE_EXPR (loc);
13475               first_op = DW_OP_addr;
13476               second_op = DW_OP_form_tls_address;
13477             }
13478
13479           rtl = rtl_for_decl_location (loc);
13480           if (rtl == NULL_RTX)
13481             return 0;
13482
13483           if (!MEM_P (rtl))
13484             return 0;
13485           rtl = XEXP (rtl, 0);
13486           if (! CONSTANT_P (rtl))
13487             return 0;
13488
13489           ret = new_loc_descr (first_op, 0, 0);
13490           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13491           ret->dw_loc_oprnd1.v.val_addr = rtl;
13492           ret->dtprel = dtprel;
13493
13494           ret1 = new_loc_descr (second_op, 0, 0);
13495           add_loc_descr (&ret, ret1);
13496
13497           have_address = 1;
13498           break;
13499         }
13500       /* FALLTHRU */
13501
13502     case PARM_DECL:
13503     case RESULT_DECL:
13504       if (DECL_HAS_VALUE_EXPR_P (loc))
13505         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13506                                    want_address);
13507       /* FALLTHRU */
13508
13509     case FUNCTION_DECL:
13510       {
13511         rtx rtl;
13512         var_loc_list *loc_list = lookup_decl_loc (loc);
13513
13514         if (loc_list && loc_list->first)
13515           {
13516             list_ret = dw_loc_list (loc_list, loc, want_address);
13517             have_address = want_address != 0;
13518             break;
13519           }
13520         rtl = rtl_for_decl_location (loc);
13521         if (rtl == NULL_RTX)
13522           {
13523             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13524             return 0;
13525           }
13526         else if (CONST_INT_P (rtl))
13527           {
13528             HOST_WIDE_INT val = INTVAL (rtl);
13529             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13530               val &= GET_MODE_MASK (DECL_MODE (loc));
13531             ret = int_loc_descriptor (val);
13532           }
13533         else if (GET_CODE (rtl) == CONST_STRING)
13534           {
13535             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13536             return 0;
13537           }
13538         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13539           {
13540             ret = new_loc_descr (DW_OP_addr, 0, 0);
13541             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13542             ret->dw_loc_oprnd1.v.val_addr = rtl;
13543           }
13544         else
13545           {
13546             enum machine_mode mode, mem_mode;
13547
13548             /* Certain constructs can only be represented at top-level.  */
13549             if (want_address == 2)
13550               {
13551                 ret = loc_descriptor (rtl, VOIDmode,
13552                                       VAR_INIT_STATUS_INITIALIZED);
13553                 have_address = 1;
13554               }
13555             else
13556               {
13557                 mode = GET_MODE (rtl);
13558                 mem_mode = VOIDmode;
13559                 if (MEM_P (rtl))
13560                   {
13561                     mem_mode = mode;
13562                     mode = get_address_mode (rtl);
13563                     rtl = XEXP (rtl, 0);
13564                     have_address = 1;
13565                   }
13566                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13567                                           VAR_INIT_STATUS_INITIALIZED);
13568               }
13569             if (!ret)
13570               expansion_failed (loc, rtl,
13571                                 "failed to produce loc descriptor for rtl");
13572           }
13573       }
13574       break;
13575
13576     case MEM_REF:
13577       /* ??? FIXME.  */
13578       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13579         return 0;
13580       /* Fallthru.  */
13581     case INDIRECT_REF:
13582       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13583       have_address = 1;
13584       break;
13585
13586     case COMPOUND_EXPR:
13587       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13588
13589     CASE_CONVERT:
13590     case VIEW_CONVERT_EXPR:
13591     case SAVE_EXPR:
13592     case MODIFY_EXPR:
13593       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13594
13595     case COMPONENT_REF:
13596     case BIT_FIELD_REF:
13597     case ARRAY_REF:
13598     case ARRAY_RANGE_REF:
13599     case REALPART_EXPR:
13600     case IMAGPART_EXPR:
13601       {
13602         tree obj, offset;
13603         HOST_WIDE_INT bitsize, bitpos, bytepos;
13604         enum machine_mode mode;
13605         int volatilep;
13606         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13607
13608         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13609                                    &unsignedp, &volatilep, false);
13610
13611         gcc_assert (obj != loc);
13612
13613         list_ret = loc_list_from_tree (obj,
13614                                        want_address == 2
13615                                        && !bitpos && !offset ? 2 : 1);
13616         /* TODO: We can extract value of the small expression via shifting even
13617            for nonzero bitpos.  */
13618         if (list_ret == 0)
13619           return 0;
13620         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13621           {
13622             expansion_failed (loc, NULL_RTX,
13623                               "bitfield access");
13624             return 0;
13625           }
13626
13627         if (offset != NULL_TREE)
13628           {
13629             /* Variable offset.  */
13630             list_ret1 = loc_list_from_tree (offset, 0);
13631             if (list_ret1 == 0)
13632               return 0;
13633             add_loc_list (&list_ret, list_ret1);
13634             if (!list_ret)
13635               return 0;
13636             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13637           }
13638
13639         bytepos = bitpos / BITS_PER_UNIT;
13640         if (bytepos > 0)
13641           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13642         else if (bytepos < 0)
13643           loc_list_plus_const (list_ret, bytepos);
13644
13645         have_address = 1;
13646         break;
13647       }
13648
13649     case INTEGER_CST:
13650       if ((want_address || !host_integerp (loc, 0))
13651           && (ret = cst_pool_loc_descr (loc)))
13652         have_address = 1;
13653       else if (want_address == 2
13654                && host_integerp (loc, 0)
13655                && (ret = address_of_int_loc_descriptor
13656                            (int_size_in_bytes (TREE_TYPE (loc)),
13657                             tree_low_cst (loc, 0))))
13658         have_address = 1;
13659       else if (host_integerp (loc, 0))
13660         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13661       else
13662         {
13663           expansion_failed (loc, NULL_RTX,
13664                             "Integer operand is not host integer");
13665           return 0;
13666         }
13667       break;
13668
13669     case CONSTRUCTOR:
13670     case REAL_CST:
13671     case STRING_CST:
13672     case COMPLEX_CST:
13673       if ((ret = cst_pool_loc_descr (loc)))
13674         have_address = 1;
13675       else
13676       /* We can construct small constants here using int_loc_descriptor.  */
13677         expansion_failed (loc, NULL_RTX,
13678                           "constructor or constant not in constant pool");
13679       break;
13680
13681     case TRUTH_AND_EXPR:
13682     case TRUTH_ANDIF_EXPR:
13683     case BIT_AND_EXPR:
13684       op = DW_OP_and;
13685       goto do_binop;
13686
13687     case TRUTH_XOR_EXPR:
13688     case BIT_XOR_EXPR:
13689       op = DW_OP_xor;
13690       goto do_binop;
13691
13692     case TRUTH_OR_EXPR:
13693     case TRUTH_ORIF_EXPR:
13694     case BIT_IOR_EXPR:
13695       op = DW_OP_or;
13696       goto do_binop;
13697
13698     case FLOOR_DIV_EXPR:
13699     case CEIL_DIV_EXPR:
13700     case ROUND_DIV_EXPR:
13701     case TRUNC_DIV_EXPR:
13702       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13703         return 0;
13704       op = DW_OP_div;
13705       goto do_binop;
13706
13707     case MINUS_EXPR:
13708       op = DW_OP_minus;
13709       goto do_binop;
13710
13711     case FLOOR_MOD_EXPR:
13712     case CEIL_MOD_EXPR:
13713     case ROUND_MOD_EXPR:
13714     case TRUNC_MOD_EXPR:
13715       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13716         {
13717           op = DW_OP_mod;
13718           goto do_binop;
13719         }
13720       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13721       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13722       if (list_ret == 0 || list_ret1 == 0)
13723         return 0;
13724
13725       add_loc_list (&list_ret, list_ret1);
13726       if (list_ret == 0)
13727         return 0;
13728       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13729       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13730       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13731       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13732       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13733       break;
13734
13735     case MULT_EXPR:
13736       op = DW_OP_mul;
13737       goto do_binop;
13738
13739     case LSHIFT_EXPR:
13740       op = DW_OP_shl;
13741       goto do_binop;
13742
13743     case RSHIFT_EXPR:
13744       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13745       goto do_binop;
13746
13747     case POINTER_PLUS_EXPR:
13748     case PLUS_EXPR:
13749       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13750         {
13751           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13752           if (list_ret == 0)
13753             return 0;
13754
13755           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13756           break;
13757         }
13758
13759       op = DW_OP_plus;
13760       goto do_binop;
13761
13762     case LE_EXPR:
13763       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13764         return 0;
13765
13766       op = DW_OP_le;
13767       goto do_binop;
13768
13769     case GE_EXPR:
13770       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13771         return 0;
13772
13773       op = DW_OP_ge;
13774       goto do_binop;
13775
13776     case LT_EXPR:
13777       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13778         return 0;
13779
13780       op = DW_OP_lt;
13781       goto do_binop;
13782
13783     case GT_EXPR:
13784       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13785         return 0;
13786
13787       op = DW_OP_gt;
13788       goto do_binop;
13789
13790     case EQ_EXPR:
13791       op = DW_OP_eq;
13792       goto do_binop;
13793
13794     case NE_EXPR:
13795       op = DW_OP_ne;
13796       goto do_binop;
13797
13798     do_binop:
13799       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13800       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13801       if (list_ret == 0 || list_ret1 == 0)
13802         return 0;
13803
13804       add_loc_list (&list_ret, list_ret1);
13805       if (list_ret == 0)
13806         return 0;
13807       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13808       break;
13809
13810     case TRUTH_NOT_EXPR:
13811     case BIT_NOT_EXPR:
13812       op = DW_OP_not;
13813       goto do_unop;
13814
13815     case ABS_EXPR:
13816       op = DW_OP_abs;
13817       goto do_unop;
13818
13819     case NEGATE_EXPR:
13820       op = DW_OP_neg;
13821       goto do_unop;
13822
13823     do_unop:
13824       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13825       if (list_ret == 0)
13826         return 0;
13827
13828       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13829       break;
13830
13831     case MIN_EXPR:
13832     case MAX_EXPR:
13833       {
13834         const enum tree_code code =
13835           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13836
13837         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13838                       build2 (code, integer_type_node,
13839                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13840                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13841       }
13842
13843       /* ... fall through ...  */
13844
13845     case COND_EXPR:
13846       {
13847         dw_loc_descr_ref lhs
13848           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13849         dw_loc_list_ref rhs
13850           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13851         dw_loc_descr_ref bra_node, jump_node, tmp;
13852
13853         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13854         if (list_ret == 0 || lhs == 0 || rhs == 0)
13855           return 0;
13856
13857         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13858         add_loc_descr_to_each (list_ret, bra_node);
13859
13860         add_loc_list (&list_ret, rhs);
13861         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13862         add_loc_descr_to_each (list_ret, jump_node);
13863
13864         add_loc_descr_to_each (list_ret, lhs);
13865         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13866         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13867
13868         /* ??? Need a node to point the skip at.  Use a nop.  */
13869         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13870         add_loc_descr_to_each (list_ret, tmp);
13871         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13872         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13873       }
13874       break;
13875
13876     case FIX_TRUNC_EXPR:
13877       return 0;
13878
13879     default:
13880       /* Leave front-end specific codes as simply unknown.  This comes
13881          up, for instance, with the C STMT_EXPR.  */
13882       if ((unsigned int) TREE_CODE (loc)
13883           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13884         {
13885           expansion_failed (loc, NULL_RTX,
13886                             "language specific tree node");
13887           return 0;
13888         }
13889
13890 #ifdef ENABLE_CHECKING
13891       /* Otherwise this is a generic code; we should just lists all of
13892          these explicitly.  We forgot one.  */
13893       gcc_unreachable ();
13894 #else
13895       /* In a release build, we want to degrade gracefully: better to
13896          generate incomplete debugging information than to crash.  */
13897       return NULL;
13898 #endif
13899     }
13900
13901   if (!ret && !list_ret)
13902     return 0;
13903
13904   if (want_address == 2 && !have_address
13905       && (dwarf_version >= 4 || !dwarf_strict))
13906     {
13907       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13908         {
13909           expansion_failed (loc, NULL_RTX,
13910                             "DWARF address size mismatch");
13911           return 0;
13912         }
13913       if (ret)
13914         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13915       else
13916         add_loc_descr_to_each (list_ret,
13917                                new_loc_descr (DW_OP_stack_value, 0, 0));
13918       have_address = 1;
13919     }
13920   /* Show if we can't fill the request for an address.  */
13921   if (want_address && !have_address)
13922     {
13923       expansion_failed (loc, NULL_RTX,
13924                         "Want address and only have value");
13925       return 0;
13926     }
13927
13928   gcc_assert (!ret || !list_ret);
13929
13930   /* If we've got an address and don't want one, dereference.  */
13931   if (!want_address && have_address)
13932     {
13933       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13934
13935       if (size > DWARF2_ADDR_SIZE || size == -1)
13936         {
13937           expansion_failed (loc, NULL_RTX,
13938                             "DWARF address size mismatch");
13939           return 0;
13940         }
13941       else if (size == DWARF2_ADDR_SIZE)
13942         op = DW_OP_deref;
13943       else
13944         op = DW_OP_deref_size;
13945
13946       if (ret)
13947         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13948       else
13949         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13950     }
13951   if (ret)
13952     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13953
13954   return list_ret;
13955 }
13956
13957 /* Same as above but return only single location expression.  */
13958 static dw_loc_descr_ref
13959 loc_descriptor_from_tree (tree loc, int want_address)
13960 {
13961   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13962   if (!ret)
13963     return NULL;
13964   if (ret->dw_loc_next)
13965     {
13966       expansion_failed (loc, NULL_RTX,
13967                         "Location list where only loc descriptor needed");
13968       return NULL;
13969     }
13970   return ret->expr;
13971 }
13972
13973 /* Given a value, round it up to the lowest multiple of `boundary'
13974    which is not less than the value itself.  */
13975
13976 static inline HOST_WIDE_INT
13977 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13978 {
13979   return (((value + boundary - 1) / boundary) * boundary);
13980 }
13981
13982 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13983    pointer to the declared type for the relevant field variable, or return
13984    `integer_type_node' if the given node turns out to be an
13985    ERROR_MARK node.  */
13986
13987 static inline tree
13988 field_type (const_tree decl)
13989 {
13990   tree type;
13991
13992   if (TREE_CODE (decl) == ERROR_MARK)
13993     return integer_type_node;
13994
13995   type = DECL_BIT_FIELD_TYPE (decl);
13996   if (type == NULL_TREE)
13997     type = TREE_TYPE (decl);
13998
13999   return type;
14000 }
14001
14002 /* Given a pointer to a tree node, return the alignment in bits for
14003    it, or else return BITS_PER_WORD if the node actually turns out to
14004    be an ERROR_MARK node.  */
14005
14006 static inline unsigned
14007 simple_type_align_in_bits (const_tree type)
14008 {
14009   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14010 }
14011
14012 static inline unsigned
14013 simple_decl_align_in_bits (const_tree decl)
14014 {
14015   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14016 }
14017
14018 /* Return the result of rounding T up to ALIGN.  */
14019
14020 static inline double_int
14021 round_up_to_align (double_int t, unsigned int align)
14022 {
14023   double_int alignd = uhwi_to_double_int (align);
14024   t = double_int_add (t, alignd);
14025   t = double_int_add (t, double_int_minus_one);
14026   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14027   t = double_int_mul (t, alignd);
14028   return t;
14029 }
14030
14031 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14032    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14033    or return 0 if we are unable to determine what that offset is, either
14034    because the argument turns out to be a pointer to an ERROR_MARK node, or
14035    because the offset is actually variable.  (We can't handle the latter case
14036    just yet).  */
14037
14038 static HOST_WIDE_INT
14039 field_byte_offset (const_tree decl)
14040 {
14041   double_int object_offset_in_bits;
14042   double_int object_offset_in_bytes;
14043   double_int bitpos_int;
14044
14045   if (TREE_CODE (decl) == ERROR_MARK)
14046     return 0;
14047
14048   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14049
14050   /* We cannot yet cope with fields whose positions are variable, so
14051      for now, when we see such things, we simply return 0.  Someday, we may
14052      be able to handle such cases, but it will be damn difficult.  */
14053   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14054     return 0;
14055
14056   bitpos_int = tree_to_double_int (bit_position (decl));
14057
14058 #ifdef PCC_BITFIELD_TYPE_MATTERS
14059   if (PCC_BITFIELD_TYPE_MATTERS)
14060     {
14061       tree type;
14062       tree field_size_tree;
14063       double_int deepest_bitpos;
14064       double_int field_size_in_bits;
14065       unsigned int type_align_in_bits;
14066       unsigned int decl_align_in_bits;
14067       double_int type_size_in_bits;
14068
14069       type = field_type (decl);
14070       type_size_in_bits = double_int_type_size_in_bits (type);
14071       type_align_in_bits = simple_type_align_in_bits (type);
14072
14073       field_size_tree = DECL_SIZE (decl);
14074
14075       /* The size could be unspecified if there was an error, or for
14076          a flexible array member.  */
14077       if (!field_size_tree)
14078         field_size_tree = bitsize_zero_node;
14079
14080       /* If the size of the field is not constant, use the type size.  */
14081       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14082         field_size_in_bits = tree_to_double_int (field_size_tree);
14083       else
14084         field_size_in_bits = type_size_in_bits;
14085
14086       decl_align_in_bits = simple_decl_align_in_bits (decl);
14087
14088       /* The GCC front-end doesn't make any attempt to keep track of the
14089          starting bit offset (relative to the start of the containing
14090          structure type) of the hypothetical "containing object" for a
14091          bit-field.  Thus, when computing the byte offset value for the
14092          start of the "containing object" of a bit-field, we must deduce
14093          this information on our own. This can be rather tricky to do in
14094          some cases.  For example, handling the following structure type
14095          definition when compiling for an i386/i486 target (which only
14096          aligns long long's to 32-bit boundaries) can be very tricky:
14097
14098          struct S { int field1; long long field2:31; };
14099
14100          Fortunately, there is a simple rule-of-thumb which can be used
14101          in such cases.  When compiling for an i386/i486, GCC will
14102          allocate 8 bytes for the structure shown above.  It decides to
14103          do this based upon one simple rule for bit-field allocation.
14104          GCC allocates each "containing object" for each bit-field at
14105          the first (i.e. lowest addressed) legitimate alignment boundary
14106          (based upon the required minimum alignment for the declared
14107          type of the field) which it can possibly use, subject to the
14108          condition that there is still enough available space remaining
14109          in the containing object (when allocated at the selected point)
14110          to fully accommodate all of the bits of the bit-field itself.
14111
14112          This simple rule makes it obvious why GCC allocates 8 bytes for
14113          each object of the structure type shown above.  When looking
14114          for a place to allocate the "containing object" for `field2',
14115          the compiler simply tries to allocate a 64-bit "containing
14116          object" at each successive 32-bit boundary (starting at zero)
14117          until it finds a place to allocate that 64- bit field such that
14118          at least 31 contiguous (and previously unallocated) bits remain
14119          within that selected 64 bit field.  (As it turns out, for the
14120          example above, the compiler finds it is OK to allocate the
14121          "containing object" 64-bit field at bit-offset zero within the
14122          structure type.)
14123
14124          Here we attempt to work backwards from the limited set of facts
14125          we're given, and we try to deduce from those facts, where GCC
14126          must have believed that the containing object started (within
14127          the structure type). The value we deduce is then used (by the
14128          callers of this routine) to generate DW_AT_location and
14129          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14130          the case of DW_AT_location, regular fields as well).  */
14131
14132       /* Figure out the bit-distance from the start of the structure to
14133          the "deepest" bit of the bit-field.  */
14134       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14135
14136       /* This is the tricky part.  Use some fancy footwork to deduce
14137          where the lowest addressed bit of the containing object must
14138          be.  */
14139       object_offset_in_bits
14140         = double_int_sub (deepest_bitpos, type_size_in_bits);
14141
14142       /* Round up to type_align by default.  This works best for
14143          bitfields.  */
14144       object_offset_in_bits
14145         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14146
14147       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14148         {
14149           object_offset_in_bits
14150             = double_int_sub (deepest_bitpos, type_size_in_bits);
14151
14152           /* Round up to decl_align instead.  */
14153           object_offset_in_bits
14154             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14155         }
14156     }
14157   else
14158 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14159     object_offset_in_bits = bitpos_int;
14160
14161   object_offset_in_bytes
14162     = double_int_div (object_offset_in_bits,
14163                       uhwi_to_double_int (BITS_PER_UNIT), true,
14164                       TRUNC_DIV_EXPR);
14165   return double_int_to_shwi (object_offset_in_bytes);
14166 }
14167 \f
14168 /* The following routines define various Dwarf attributes and any data
14169    associated with them.  */
14170
14171 /* Add a location description attribute value to a DIE.
14172
14173    This emits location attributes suitable for whole variables and
14174    whole parameters.  Note that the location attributes for struct fields are
14175    generated by the routine `data_member_location_attribute' below.  */
14176
14177 static inline void
14178 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14179                              dw_loc_list_ref descr)
14180 {
14181   if (descr == 0)
14182     return;
14183   if (single_element_loc_list_p (descr))
14184     add_AT_loc (die, attr_kind, descr->expr);
14185   else
14186     add_AT_loc_list (die, attr_kind, descr);
14187 }
14188
14189 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14190
14191 static void
14192 add_accessibility_attribute (dw_die_ref die, tree decl)
14193 {
14194   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14195      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14196      the default has always been DW_ACCESS_public.  */
14197   if (TREE_PROTECTED (decl))
14198     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14199   else if (TREE_PRIVATE (decl))
14200     {
14201       if (dwarf_version == 2
14202           || die->die_parent == NULL
14203           || die->die_parent->die_tag != DW_TAG_class_type)
14204         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14205     }
14206   else if (dwarf_version > 2
14207            && die->die_parent
14208            && die->die_parent->die_tag == DW_TAG_class_type)
14209     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14210 }
14211
14212 /* Attach the specialized form of location attribute used for data members of
14213    struct and union types.  In the special case of a FIELD_DECL node which
14214    represents a bit-field, the "offset" part of this special location
14215    descriptor must indicate the distance in bytes from the lowest-addressed
14216    byte of the containing struct or union type to the lowest-addressed byte of
14217    the "containing object" for the bit-field.  (See the `field_byte_offset'
14218    function above).
14219
14220    For any given bit-field, the "containing object" is a hypothetical object
14221    (of some integral or enum type) within which the given bit-field lives.  The
14222    type of this hypothetical "containing object" is always the same as the
14223    declared type of the individual bit-field itself (for GCC anyway... the
14224    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14225    bytes) of the hypothetical "containing object" which will be given in the
14226    DW_AT_byte_size attribute for this bit-field.  (See the
14227    `byte_size_attribute' function below.)  It is also used when calculating the
14228    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14229    function below.)  */
14230
14231 static void
14232 add_data_member_location_attribute (dw_die_ref die, tree decl)
14233 {
14234   HOST_WIDE_INT offset;
14235   dw_loc_descr_ref loc_descr = 0;
14236
14237   if (TREE_CODE (decl) == TREE_BINFO)
14238     {
14239       /* We're working on the TAG_inheritance for a base class.  */
14240       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14241         {
14242           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14243              aren't at a fixed offset from all (sub)objects of the same
14244              type.  We need to extract the appropriate offset from our
14245              vtable.  The following dwarf expression means
14246
14247                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14248
14249              This is specific to the V3 ABI, of course.  */
14250
14251           dw_loc_descr_ref tmp;
14252
14253           /* Make a copy of the object address.  */
14254           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14255           add_loc_descr (&loc_descr, tmp);
14256
14257           /* Extract the vtable address.  */
14258           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14259           add_loc_descr (&loc_descr, tmp);
14260
14261           /* Calculate the address of the offset.  */
14262           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14263           gcc_assert (offset < 0);
14264
14265           tmp = int_loc_descriptor (-offset);
14266           add_loc_descr (&loc_descr, tmp);
14267           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14268           add_loc_descr (&loc_descr, tmp);
14269
14270           /* Extract the offset.  */
14271           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14272           add_loc_descr (&loc_descr, tmp);
14273
14274           /* Add it to the object address.  */
14275           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14276           add_loc_descr (&loc_descr, tmp);
14277         }
14278       else
14279         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14280     }
14281   else
14282     offset = field_byte_offset (decl);
14283
14284   if (! loc_descr)
14285     {
14286       if (dwarf_version > 2)
14287         {
14288           /* Don't need to output a location expression, just the constant. */
14289           if (offset < 0)
14290             add_AT_int (die, DW_AT_data_member_location, offset);
14291           else
14292             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14293           return;
14294         }
14295       else
14296         {
14297           enum dwarf_location_atom op;
14298
14299           /* The DWARF2 standard says that we should assume that the structure
14300              address is already on the stack, so we can specify a structure
14301              field address by using DW_OP_plus_uconst.  */
14302
14303 #ifdef MIPS_DEBUGGING_INFO
14304           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14305              operator correctly.  It works only if we leave the offset on the
14306              stack.  */
14307           op = DW_OP_constu;
14308 #else
14309           op = DW_OP_plus_uconst;
14310 #endif
14311
14312           loc_descr = new_loc_descr (op, offset, 0);
14313         }
14314     }
14315
14316   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14317 }
14318
14319 /* Writes integer values to dw_vec_const array.  */
14320
14321 static void
14322 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14323 {
14324   while (size != 0)
14325     {
14326       *dest++ = val & 0xff;
14327       val >>= 8;
14328       --size;
14329     }
14330 }
14331
14332 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14333
14334 static HOST_WIDE_INT
14335 extract_int (const unsigned char *src, unsigned int size)
14336 {
14337   HOST_WIDE_INT val = 0;
14338
14339   src += size;
14340   while (size != 0)
14341     {
14342       val <<= 8;
14343       val |= *--src & 0xff;
14344       --size;
14345     }
14346   return val;
14347 }
14348
14349 /* Writes double_int values to dw_vec_const array.  */
14350
14351 static void
14352 insert_double (double_int val, unsigned char *dest)
14353 {
14354   unsigned char *p0 = dest;
14355   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14356
14357   if (WORDS_BIG_ENDIAN)
14358     {
14359       p0 = p1;
14360       p1 = dest;
14361     }
14362
14363   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14364   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14365 }
14366
14367 /* Writes floating point values to dw_vec_const array.  */
14368
14369 static void
14370 insert_float (const_rtx rtl, unsigned char *array)
14371 {
14372   REAL_VALUE_TYPE rv;
14373   long val[4];
14374   int i;
14375
14376   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14377   real_to_target (val, &rv, GET_MODE (rtl));
14378
14379   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14380   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14381     {
14382       insert_int (val[i], 4, array);
14383       array += 4;
14384     }
14385 }
14386
14387 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14388    does not have a "location" either in memory or in a register.  These
14389    things can arise in GNU C when a constant is passed as an actual parameter
14390    to an inlined function.  They can also arise in C++ where declared
14391    constants do not necessarily get memory "homes".  */
14392
14393 static bool
14394 add_const_value_attribute (dw_die_ref die, rtx rtl)
14395 {
14396   switch (GET_CODE (rtl))
14397     {
14398     case CONST_INT:
14399       {
14400         HOST_WIDE_INT val = INTVAL (rtl);
14401
14402         if (val < 0)
14403           add_AT_int (die, DW_AT_const_value, val);
14404         else
14405           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14406       }
14407       return true;
14408
14409     case CONST_DOUBLE:
14410       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14411          floating-point constant.  A CONST_DOUBLE is used whenever the
14412          constant requires more than one word in order to be adequately
14413          represented.  */
14414       {
14415         enum machine_mode mode = GET_MODE (rtl);
14416
14417         if (SCALAR_FLOAT_MODE_P (mode))
14418           {
14419             unsigned int length = GET_MODE_SIZE (mode);
14420             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14421
14422             insert_float (rtl, array);
14423             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14424           }
14425         else
14426           add_AT_double (die, DW_AT_const_value,
14427                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14428       }
14429       return true;
14430
14431     case CONST_VECTOR:
14432       {
14433         enum machine_mode mode = GET_MODE (rtl);
14434         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14435         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14436         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14437           (length * elt_size);
14438         unsigned int i;
14439         unsigned char *p;
14440
14441         switch (GET_MODE_CLASS (mode))
14442           {
14443           case MODE_VECTOR_INT:
14444             for (i = 0, p = array; i < length; i++, p += elt_size)
14445               {
14446                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14447                 double_int val = rtx_to_double_int (elt);
14448
14449                 if (elt_size <= sizeof (HOST_WIDE_INT))
14450                   insert_int (double_int_to_shwi (val), elt_size, p);
14451                 else
14452                   {
14453                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14454                     insert_double (val, p);
14455                   }
14456               }
14457             break;
14458
14459           case MODE_VECTOR_FLOAT:
14460             for (i = 0, p = array; i < length; i++, p += elt_size)
14461               {
14462                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14463                 insert_float (elt, p);
14464               }
14465             break;
14466
14467           default:
14468             gcc_unreachable ();
14469           }
14470
14471         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14472       }
14473       return true;
14474
14475     case CONST_STRING:
14476       if (dwarf_version >= 4 || !dwarf_strict)
14477         {
14478           dw_loc_descr_ref loc_result;
14479           resolve_one_addr (&rtl, NULL);
14480         rtl_addr:
14481           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14482           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14483           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14484           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14485           add_AT_loc (die, DW_AT_location, loc_result);
14486           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14487           return true;
14488         }
14489       return false;
14490
14491     case CONST:
14492       if (CONSTANT_P (XEXP (rtl, 0)))
14493         return add_const_value_attribute (die, XEXP (rtl, 0));
14494       /* FALLTHROUGH */
14495     case SYMBOL_REF:
14496       if (!const_ok_for_output (rtl))
14497         return false;
14498     case LABEL_REF:
14499       if (dwarf_version >= 4 || !dwarf_strict)
14500         goto rtl_addr;
14501       return false;
14502
14503     case PLUS:
14504       /* In cases where an inlined instance of an inline function is passed
14505          the address of an `auto' variable (which is local to the caller) we
14506          can get a situation where the DECL_RTL of the artificial local
14507          variable (for the inlining) which acts as a stand-in for the
14508          corresponding formal parameter (of the inline function) will look
14509          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14510          exactly a compile-time constant expression, but it isn't the address
14511          of the (artificial) local variable either.  Rather, it represents the
14512          *value* which the artificial local variable always has during its
14513          lifetime.  We currently have no way to represent such quasi-constant
14514          values in Dwarf, so for now we just punt and generate nothing.  */
14515       return false;
14516
14517     case HIGH:
14518     case CONST_FIXED:
14519       return false;
14520
14521     case MEM:
14522       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14523           && MEM_READONLY_P (rtl)
14524           && GET_MODE (rtl) == BLKmode)
14525         {
14526           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14527           return true;
14528         }
14529       return false;
14530
14531     default:
14532       /* No other kinds of rtx should be possible here.  */
14533       gcc_unreachable ();
14534     }
14535   return false;
14536 }
14537
14538 /* Determine whether the evaluation of EXPR references any variables
14539    or functions which aren't otherwise used (and therefore may not be
14540    output).  */
14541 static tree
14542 reference_to_unused (tree * tp, int * walk_subtrees,
14543                      void * data ATTRIBUTE_UNUSED)
14544 {
14545   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14546     *walk_subtrees = 0;
14547
14548   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14549       && ! TREE_ASM_WRITTEN (*tp))
14550     return *tp;
14551   /* ???  The C++ FE emits debug information for using decls, so
14552      putting gcc_unreachable here falls over.  See PR31899.  For now
14553      be conservative.  */
14554   else if (!cgraph_global_info_ready
14555            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14556     return *tp;
14557   else if (TREE_CODE (*tp) == VAR_DECL)
14558     {
14559       struct varpool_node *node = varpool_get_node (*tp);
14560       if (!node || !node->needed)
14561         return *tp;
14562     }
14563   else if (TREE_CODE (*tp) == FUNCTION_DECL
14564            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14565     {
14566       /* The call graph machinery must have finished analyzing,
14567          optimizing and gimplifying the CU by now.
14568          So if *TP has no call graph node associated
14569          to it, it means *TP will not be emitted.  */
14570       if (!cgraph_get_node (*tp))
14571         return *tp;
14572     }
14573   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14574     return *tp;
14575
14576   return NULL_TREE;
14577 }
14578
14579 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14580    for use in a later add_const_value_attribute call.  */
14581
14582 static rtx
14583 rtl_for_decl_init (tree init, tree type)
14584 {
14585   rtx rtl = NULL_RTX;
14586
14587   STRIP_NOPS (init);
14588
14589   /* If a variable is initialized with a string constant without embedded
14590      zeros, build CONST_STRING.  */
14591   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14592     {
14593       tree enttype = TREE_TYPE (type);
14594       tree domain = TYPE_DOMAIN (type);
14595       enum machine_mode mode = TYPE_MODE (enttype);
14596
14597       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14598           && domain
14599           && integer_zerop (TYPE_MIN_VALUE (domain))
14600           && compare_tree_int (TYPE_MAX_VALUE (domain),
14601                                TREE_STRING_LENGTH (init) - 1) == 0
14602           && ((size_t) TREE_STRING_LENGTH (init)
14603               == strlen (TREE_STRING_POINTER (init)) + 1))
14604         {
14605           rtl = gen_rtx_CONST_STRING (VOIDmode,
14606                                       ggc_strdup (TREE_STRING_POINTER (init)));
14607           rtl = gen_rtx_MEM (BLKmode, rtl);
14608           MEM_READONLY_P (rtl) = 1;
14609         }
14610     }
14611   /* Other aggregates, and complex values, could be represented using
14612      CONCAT: FIXME!  */
14613   else if (AGGREGATE_TYPE_P (type)
14614            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14615                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14616            || TREE_CODE (type) == COMPLEX_TYPE)
14617     ;
14618   /* Vectors only work if their mode is supported by the target.
14619      FIXME: generic vectors ought to work too.  */
14620   else if (TREE_CODE (type) == VECTOR_TYPE
14621            && !VECTOR_MODE_P (TYPE_MODE (type)))
14622     ;
14623   /* If the initializer is something that we know will expand into an
14624      immediate RTL constant, expand it now.  We must be careful not to
14625      reference variables which won't be output.  */
14626   else if (initializer_constant_valid_p (init, type)
14627            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14628     {
14629       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14630          possible.  */
14631       if (TREE_CODE (type) == VECTOR_TYPE)
14632         switch (TREE_CODE (init))
14633           {
14634           case VECTOR_CST:
14635             break;
14636           case CONSTRUCTOR:
14637             if (TREE_CONSTANT (init))
14638               {
14639                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14640                 bool constant_p = true;
14641                 tree value;
14642                 unsigned HOST_WIDE_INT ix;
14643
14644                 /* Even when ctor is constant, it might contain non-*_CST
14645                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14646                    belong into VECTOR_CST nodes.  */
14647                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14648                   if (!CONSTANT_CLASS_P (value))
14649                     {
14650                       constant_p = false;
14651                       break;
14652                     }
14653
14654                 if (constant_p)
14655                   {
14656                     init = build_vector_from_ctor (type, elts);
14657                     break;
14658                   }
14659               }
14660             /* FALLTHRU */
14661
14662           default:
14663             return NULL;
14664           }
14665
14666       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14667
14668       /* If expand_expr returns a MEM, it wasn't immediate.  */
14669       gcc_assert (!rtl || !MEM_P (rtl));
14670     }
14671
14672   return rtl;
14673 }
14674
14675 /* Generate RTL for the variable DECL to represent its location.  */
14676
14677 static rtx
14678 rtl_for_decl_location (tree decl)
14679 {
14680   rtx rtl;
14681
14682   /* Here we have to decide where we are going to say the parameter "lives"
14683      (as far as the debugger is concerned).  We only have a couple of
14684      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14685
14686      DECL_RTL normally indicates where the parameter lives during most of the
14687      activation of the function.  If optimization is enabled however, this
14688      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14689      that the parameter doesn't really live anywhere (as far as the code
14690      generation parts of GCC are concerned) during most of the function's
14691      activation.  That will happen (for example) if the parameter is never
14692      referenced within the function.
14693
14694      We could just generate a location descriptor here for all non-NULL
14695      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14696      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14697      where DECL_RTL is NULL or is a pseudo-reg.
14698
14699      Note however that we can only get away with using DECL_INCOMING_RTL as
14700      a backup substitute for DECL_RTL in certain limited cases.  In cases
14701      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14702      we can be sure that the parameter was passed using the same type as it is
14703      declared to have within the function, and that its DECL_INCOMING_RTL
14704      points us to a place where a value of that type is passed.
14705
14706      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14707      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14708      because in these cases DECL_INCOMING_RTL points us to a value of some
14709      type which is *different* from the type of the parameter itself.  Thus,
14710      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14711      such cases, the debugger would end up (for example) trying to fetch a
14712      `float' from a place which actually contains the first part of a
14713      `double'.  That would lead to really incorrect and confusing
14714      output at debug-time.
14715
14716      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14717      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14718      are a couple of exceptions however.  On little-endian machines we can
14719      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14720      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14721      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14722      when (on a little-endian machine) a non-prototyped function has a
14723      parameter declared to be of type `short' or `char'.  In such cases,
14724      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14725      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14726      passed `int' value.  If the debugger then uses that address to fetch
14727      a `short' or a `char' (on a little-endian machine) the result will be
14728      the correct data, so we allow for such exceptional cases below.
14729
14730      Note that our goal here is to describe the place where the given formal
14731      parameter lives during most of the function's activation (i.e. between the
14732      end of the prologue and the start of the epilogue).  We'll do that as best
14733      as we can. Note however that if the given formal parameter is modified
14734      sometime during the execution of the function, then a stack backtrace (at
14735      debug-time) will show the function as having been called with the *new*
14736      value rather than the value which was originally passed in.  This happens
14737      rarely enough that it is not a major problem, but it *is* a problem, and
14738      I'd like to fix it.
14739
14740      A future version of dwarf2out.c may generate two additional attributes for
14741      any given DW_TAG_formal_parameter DIE which will describe the "passed
14742      type" and the "passed location" for the given formal parameter in addition
14743      to the attributes we now generate to indicate the "declared type" and the
14744      "active location" for each parameter.  This additional set of attributes
14745      could be used by debuggers for stack backtraces. Separately, note that
14746      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14747      This happens (for example) for inlined-instances of inline function formal
14748      parameters which are never referenced.  This really shouldn't be
14749      happening.  All PARM_DECL nodes should get valid non-NULL
14750      DECL_INCOMING_RTL values.  FIXME.  */
14751
14752   /* Use DECL_RTL as the "location" unless we find something better.  */
14753   rtl = DECL_RTL_IF_SET (decl);
14754
14755   /* When generating abstract instances, ignore everything except
14756      constants, symbols living in memory, and symbols living in
14757      fixed registers.  */
14758   if (! reload_completed)
14759     {
14760       if (rtl
14761           && (CONSTANT_P (rtl)
14762               || (MEM_P (rtl)
14763                   && CONSTANT_P (XEXP (rtl, 0)))
14764               || (REG_P (rtl)
14765                   && TREE_CODE (decl) == VAR_DECL
14766                   && TREE_STATIC (decl))))
14767         {
14768           rtl = targetm.delegitimize_address (rtl);
14769           return rtl;
14770         }
14771       rtl = NULL_RTX;
14772     }
14773   else if (TREE_CODE (decl) == PARM_DECL)
14774     {
14775       if (rtl == NULL_RTX
14776           || is_pseudo_reg (rtl)
14777           || (MEM_P (rtl)
14778               && is_pseudo_reg (XEXP (rtl, 0))
14779               && DECL_INCOMING_RTL (decl)
14780               && MEM_P (DECL_INCOMING_RTL (decl))
14781               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14782         {
14783           tree declared_type = TREE_TYPE (decl);
14784           tree passed_type = DECL_ARG_TYPE (decl);
14785           enum machine_mode dmode = TYPE_MODE (declared_type);
14786           enum machine_mode pmode = TYPE_MODE (passed_type);
14787
14788           /* This decl represents a formal parameter which was optimized out.
14789              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14790              all cases where (rtl == NULL_RTX) just below.  */
14791           if (dmode == pmode)
14792             rtl = DECL_INCOMING_RTL (decl);
14793           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14794                    && SCALAR_INT_MODE_P (dmode)
14795                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14796                    && DECL_INCOMING_RTL (decl))
14797             {
14798               rtx inc = DECL_INCOMING_RTL (decl);
14799               if (REG_P (inc))
14800                 rtl = inc;
14801               else if (MEM_P (inc))
14802                 {
14803                   if (BYTES_BIG_ENDIAN)
14804                     rtl = adjust_address_nv (inc, dmode,
14805                                              GET_MODE_SIZE (pmode)
14806                                              - GET_MODE_SIZE (dmode));
14807                   else
14808                     rtl = inc;
14809                 }
14810             }
14811         }
14812
14813       /* If the parm was passed in registers, but lives on the stack, then
14814          make a big endian correction if the mode of the type of the
14815          parameter is not the same as the mode of the rtl.  */
14816       /* ??? This is the same series of checks that are made in dbxout.c before
14817          we reach the big endian correction code there.  It isn't clear if all
14818          of these checks are necessary here, but keeping them all is the safe
14819          thing to do.  */
14820       else if (MEM_P (rtl)
14821                && XEXP (rtl, 0) != const0_rtx
14822                && ! CONSTANT_P (XEXP (rtl, 0))
14823                /* Not passed in memory.  */
14824                && !MEM_P (DECL_INCOMING_RTL (decl))
14825                /* Not passed by invisible reference.  */
14826                && (!REG_P (XEXP (rtl, 0))
14827                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14828                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14829 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14830                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14831 #endif
14832                      )
14833                /* Big endian correction check.  */
14834                && BYTES_BIG_ENDIAN
14835                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14836                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14837                    < UNITS_PER_WORD))
14838         {
14839           int offset = (UNITS_PER_WORD
14840                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14841
14842           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14843                              plus_constant (XEXP (rtl, 0), offset));
14844         }
14845     }
14846   else if (TREE_CODE (decl) == VAR_DECL
14847            && rtl
14848            && MEM_P (rtl)
14849            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14850            && BYTES_BIG_ENDIAN)
14851     {
14852       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14853       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14854
14855       /* If a variable is declared "register" yet is smaller than
14856          a register, then if we store the variable to memory, it
14857          looks like we're storing a register-sized value, when in
14858          fact we are not.  We need to adjust the offset of the
14859          storage location to reflect the actual value's bytes,
14860          else gdb will not be able to display it.  */
14861       if (rsize > dsize)
14862         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14863                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14864     }
14865
14866   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14867      and will have been substituted directly into all expressions that use it.
14868      C does not have such a concept, but C++ and other languages do.  */
14869   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14870     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14871
14872   if (rtl)
14873     rtl = targetm.delegitimize_address (rtl);
14874
14875   /* If we don't look past the constant pool, we risk emitting a
14876      reference to a constant pool entry that isn't referenced from
14877      code, and thus is not emitted.  */
14878   if (rtl)
14879     rtl = avoid_constant_pool_reference (rtl);
14880
14881   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14882      in the current CU, resolve_addr will remove the expression referencing
14883      it.  */
14884   if (rtl == NULL_RTX
14885       && TREE_CODE (decl) == VAR_DECL
14886       && !DECL_EXTERNAL (decl)
14887       && TREE_STATIC (decl)
14888       && DECL_NAME (decl)
14889       && !DECL_HARD_REGISTER (decl)
14890       && DECL_MODE (decl) != VOIDmode)
14891     {
14892       rtl = make_decl_rtl_for_debug (decl);
14893       if (!MEM_P (rtl)
14894           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14895           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14896         rtl = NULL_RTX;
14897     }
14898
14899   return rtl;
14900 }
14901
14902 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14903    returned.  If so, the decl for the COMMON block is returned, and the
14904    value is the offset into the common block for the symbol.  */
14905
14906 static tree
14907 fortran_common (tree decl, HOST_WIDE_INT *value)
14908 {
14909   tree val_expr, cvar;
14910   enum machine_mode mode;
14911   HOST_WIDE_INT bitsize, bitpos;
14912   tree offset;
14913   int volatilep = 0, unsignedp = 0;
14914
14915   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14916      it does not have a value (the offset into the common area), or if it
14917      is thread local (as opposed to global) then it isn't common, and shouldn't
14918      be handled as such.  */
14919   if (TREE_CODE (decl) != VAR_DECL
14920       || !TREE_STATIC (decl)
14921       || !DECL_HAS_VALUE_EXPR_P (decl)
14922       || !is_fortran ())
14923     return NULL_TREE;
14924
14925   val_expr = DECL_VALUE_EXPR (decl);
14926   if (TREE_CODE (val_expr) != COMPONENT_REF)
14927     return NULL_TREE;
14928
14929   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14930                               &mode, &unsignedp, &volatilep, true);
14931
14932   if (cvar == NULL_TREE
14933       || TREE_CODE (cvar) != VAR_DECL
14934       || DECL_ARTIFICIAL (cvar)
14935       || !TREE_PUBLIC (cvar))
14936     return NULL_TREE;
14937
14938   *value = 0;
14939   if (offset != NULL)
14940     {
14941       if (!host_integerp (offset, 0))
14942         return NULL_TREE;
14943       *value = tree_low_cst (offset, 0);
14944     }
14945   if (bitpos != 0)
14946     *value += bitpos / BITS_PER_UNIT;
14947
14948   return cvar;
14949 }
14950
14951 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14952    data attribute for a variable or a parameter.  We generate the
14953    DW_AT_const_value attribute only in those cases where the given variable
14954    or parameter does not have a true "location" either in memory or in a
14955    register.  This can happen (for example) when a constant is passed as an
14956    actual argument in a call to an inline function.  (It's possible that
14957    these things can crop up in other ways also.)  Note that one type of
14958    constant value which can be passed into an inlined function is a constant
14959    pointer.  This can happen for example if an actual argument in an inlined
14960    function call evaluates to a compile-time constant address.
14961
14962    CACHE_P is true if it is worth caching the location list for DECL,
14963    so that future calls can reuse it rather than regenerate it from scratch.
14964    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14965    since we will need to refer to them each time the function is inlined.  */
14966
14967 static bool
14968 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14969                                        enum dwarf_attribute attr)
14970 {
14971   rtx rtl;
14972   dw_loc_list_ref list;
14973   var_loc_list *loc_list;
14974   cached_dw_loc_list *cache;
14975   void **slot;
14976
14977   if (TREE_CODE (decl) == ERROR_MARK)
14978     return false;
14979
14980   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14981               || TREE_CODE (decl) == RESULT_DECL);
14982
14983   /* Try to get some constant RTL for this decl, and use that as the value of
14984      the location.  */
14985
14986   rtl = rtl_for_decl_location (decl);
14987   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14988       && add_const_value_attribute (die, rtl))
14989     return true;
14990
14991   /* See if we have single element location list that is equivalent to
14992      a constant value.  That way we are better to use add_const_value_attribute
14993      rather than expanding constant value equivalent.  */
14994   loc_list = lookup_decl_loc (decl);
14995   if (loc_list
14996       && loc_list->first
14997       && loc_list->first->next == NULL
14998       && NOTE_P (loc_list->first->loc)
14999       && NOTE_VAR_LOCATION (loc_list->first->loc)
15000       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15001     {
15002       struct var_loc_node *node;
15003
15004       node = loc_list->first;
15005       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15006       if (GET_CODE (rtl) == EXPR_LIST)
15007         rtl = XEXP (rtl, 0);
15008       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15009           && add_const_value_attribute (die, rtl))
15010          return true;
15011     }
15012   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15013      list several times.  See if we've already cached the contents.  */
15014   list = NULL;
15015   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15016     cache_p = false;
15017   if (cache_p)
15018     {
15019       cache = (cached_dw_loc_list *)
15020         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15021       if (cache)
15022         list = cache->loc_list;
15023     }
15024   if (list == NULL)
15025     {
15026       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15027       /* It is usually worth caching this result if the decl is from
15028          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15029       if (cache_p && list && list->dw_loc_next)
15030         {
15031           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15032                                            DECL_UID (decl), INSERT);
15033           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15034           cache->decl_id = DECL_UID (decl);
15035           cache->loc_list = list;
15036           *slot = cache;
15037         }
15038     }
15039   if (list)
15040     {
15041       add_AT_location_description (die, attr, list);
15042       return true;
15043     }
15044   /* None of that worked, so it must not really have a location;
15045      try adding a constant value attribute from the DECL_INITIAL.  */
15046   return tree_add_const_value_attribute_for_decl (die, decl);
15047 }
15048
15049 /* Add VARIABLE and DIE into deferred locations list.  */
15050
15051 static void
15052 defer_location (tree variable, dw_die_ref die)
15053 {
15054   deferred_locations entry;
15055   entry.variable = variable;
15056   entry.die = die;
15057   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15058 }
15059
15060 /* Helper function for tree_add_const_value_attribute.  Natively encode
15061    initializer INIT into an array.  Return true if successful.  */
15062
15063 static bool
15064 native_encode_initializer (tree init, unsigned char *array, int size)
15065 {
15066   tree type;
15067
15068   if (init == NULL_TREE)
15069     return false;
15070
15071   STRIP_NOPS (init);
15072   switch (TREE_CODE (init))
15073     {
15074     case STRING_CST:
15075       type = TREE_TYPE (init);
15076       if (TREE_CODE (type) == ARRAY_TYPE)
15077         {
15078           tree enttype = TREE_TYPE (type);
15079           enum machine_mode mode = TYPE_MODE (enttype);
15080
15081           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15082             return false;
15083           if (int_size_in_bytes (type) != size)
15084             return false;
15085           if (size > TREE_STRING_LENGTH (init))
15086             {
15087               memcpy (array, TREE_STRING_POINTER (init),
15088                       TREE_STRING_LENGTH (init));
15089               memset (array + TREE_STRING_LENGTH (init),
15090                       '\0', size - TREE_STRING_LENGTH (init));
15091             }
15092           else
15093             memcpy (array, TREE_STRING_POINTER (init), size);
15094           return true;
15095         }
15096       return false;
15097     case CONSTRUCTOR:
15098       type = TREE_TYPE (init);
15099       if (int_size_in_bytes (type) != size)
15100         return false;
15101       if (TREE_CODE (type) == ARRAY_TYPE)
15102         {
15103           HOST_WIDE_INT min_index;
15104           unsigned HOST_WIDE_INT cnt;
15105           int curpos = 0, fieldsize;
15106           constructor_elt *ce;
15107
15108           if (TYPE_DOMAIN (type) == NULL_TREE
15109               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15110             return false;
15111
15112           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15113           if (fieldsize <= 0)
15114             return false;
15115
15116           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15117           memset (array, '\0', size);
15118           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15119             {
15120               tree val = ce->value;
15121               tree index = ce->index;
15122               int pos = curpos;
15123               if (index && TREE_CODE (index) == RANGE_EXPR)
15124                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15125                       * fieldsize;
15126               else if (index)
15127                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15128
15129               if (val)
15130                 {
15131                   STRIP_NOPS (val);
15132                   if (!native_encode_initializer (val, array + pos, fieldsize))
15133                     return false;
15134                 }
15135               curpos = pos + fieldsize;
15136               if (index && TREE_CODE (index) == RANGE_EXPR)
15137                 {
15138                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15139                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15140                   while (count-- > 0)
15141                     {
15142                       if (val)
15143                         memcpy (array + curpos, array + pos, fieldsize);
15144                       curpos += fieldsize;
15145                     }
15146                 }
15147               gcc_assert (curpos <= size);
15148             }
15149           return true;
15150         }
15151       else if (TREE_CODE (type) == RECORD_TYPE
15152                || TREE_CODE (type) == UNION_TYPE)
15153         {
15154           tree field = NULL_TREE;
15155           unsigned HOST_WIDE_INT cnt;
15156           constructor_elt *ce;
15157
15158           if (int_size_in_bytes (type) != size)
15159             return false;
15160
15161           if (TREE_CODE (type) == RECORD_TYPE)
15162             field = TYPE_FIELDS (type);
15163
15164           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15165             {
15166               tree val = ce->value;
15167               int pos, fieldsize;
15168
15169               if (ce->index != 0)
15170                 field = ce->index;
15171
15172               if (val)
15173                 STRIP_NOPS (val);
15174
15175               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15176                 return false;
15177
15178               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15179                   && TYPE_DOMAIN (TREE_TYPE (field))
15180                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15181                 return false;
15182               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15183                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15184                 return false;
15185               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15186               pos = int_byte_position (field);
15187               gcc_assert (pos + fieldsize <= size);
15188               if (val
15189                   && !native_encode_initializer (val, array + pos, fieldsize))
15190                 return false;
15191             }
15192           return true;
15193         }
15194       return false;
15195     case VIEW_CONVERT_EXPR:
15196     case NON_LVALUE_EXPR:
15197       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15198     default:
15199       return native_encode_expr (init, array, size) == size;
15200     }
15201 }
15202
15203 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15204    attribute is the const value T.  */
15205
15206 static bool
15207 tree_add_const_value_attribute (dw_die_ref die, tree t)
15208 {
15209   tree init;
15210   tree type = TREE_TYPE (t);
15211   rtx rtl;
15212
15213   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15214     return false;
15215
15216   init = t;
15217   gcc_assert (!DECL_P (init));
15218
15219   rtl = rtl_for_decl_init (init, type);
15220   if (rtl)
15221     return add_const_value_attribute (die, rtl);
15222   /* If the host and target are sane, try harder.  */
15223   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15224            && initializer_constant_valid_p (init, type))
15225     {
15226       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15227       if (size > 0 && (int) size == size)
15228         {
15229           unsigned char *array = (unsigned char *)
15230             ggc_alloc_cleared_atomic (size);
15231
15232           if (native_encode_initializer (init, array, size))
15233             {
15234               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15235               return true;
15236             }
15237         }
15238     }
15239   return false;
15240 }
15241
15242 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15243    attribute is the const value of T, where T is an integral constant
15244    variable with static storage duration
15245    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15246
15247 static bool
15248 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15249 {
15250
15251   if (!decl
15252       || (TREE_CODE (decl) != VAR_DECL
15253           && TREE_CODE (decl) != CONST_DECL)
15254       || (TREE_CODE (decl) == VAR_DECL
15255           && !TREE_STATIC (decl)))
15256     return false;
15257
15258     if (TREE_READONLY (decl)
15259         && ! TREE_THIS_VOLATILE (decl)
15260         && DECL_INITIAL (decl))
15261       /* OK */;
15262     else
15263       return false;
15264
15265   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15266   if (get_AT (var_die, DW_AT_const_value))
15267     return false;
15268
15269   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15270 }
15271
15272 /* Convert the CFI instructions for the current function into a
15273    location list.  This is used for DW_AT_frame_base when we targeting
15274    a dwarf2 consumer that does not support the dwarf3
15275    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15276    expressions.  */
15277
15278 static dw_loc_list_ref
15279 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15280 {
15281   int ix;
15282   dw_fde_ref fde;
15283   dw_loc_list_ref list, *list_tail;
15284   dw_cfi_ref cfi;
15285   dw_cfa_location last_cfa, next_cfa;
15286   const char *start_label, *last_label, *section;
15287   dw_cfa_location remember;
15288
15289   fde = cfun->fde;
15290   gcc_assert (fde != NULL);
15291
15292   section = secname_for_decl (current_function_decl);
15293   list_tail = &list;
15294   list = NULL;
15295
15296   memset (&next_cfa, 0, sizeof (next_cfa));
15297   next_cfa.reg = INVALID_REGNUM;
15298   remember = next_cfa;
15299
15300   start_label = fde->dw_fde_begin;
15301
15302   /* ??? Bald assumption that the CIE opcode list does not contain
15303      advance opcodes.  */
15304   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15305     lookup_cfa_1 (cfi, &next_cfa, &remember);
15306
15307   last_cfa = next_cfa;
15308   last_label = start_label;
15309
15310   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15311     {
15312       /* If the first partition contained no CFI adjustments, the
15313          CIE opcodes apply to the whole first partition.  */
15314       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15315                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15316       list_tail =&(*list_tail)->dw_loc_next;
15317       start_label = last_label = fde->dw_fde_second_begin;
15318     }
15319
15320   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15321     {
15322       switch (cfi->dw_cfi_opc)
15323         {
15324         case DW_CFA_set_loc:
15325         case DW_CFA_advance_loc1:
15326         case DW_CFA_advance_loc2:
15327         case DW_CFA_advance_loc4:
15328           if (!cfa_equal_p (&last_cfa, &next_cfa))
15329             {
15330               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15331                                          start_label, last_label, section);
15332
15333               list_tail = &(*list_tail)->dw_loc_next;
15334               last_cfa = next_cfa;
15335               start_label = last_label;
15336             }
15337           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15338           break;
15339
15340         case DW_CFA_advance_loc:
15341           /* The encoding is complex enough that we should never emit this.  */
15342           gcc_unreachable ();
15343
15344         default:
15345           lookup_cfa_1 (cfi, &next_cfa, &remember);
15346           break;
15347         }
15348       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15349         {
15350           if (!cfa_equal_p (&last_cfa, &next_cfa))
15351             {
15352               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15353                                          start_label, last_label, section);
15354
15355               list_tail = &(*list_tail)->dw_loc_next;
15356               last_cfa = next_cfa;
15357               start_label = last_label;
15358             }
15359           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15360                                      start_label, fde->dw_fde_end, section);
15361           list_tail = &(*list_tail)->dw_loc_next;
15362           start_label = last_label = fde->dw_fde_second_begin;
15363         }
15364     }
15365
15366   if (!cfa_equal_p (&last_cfa, &next_cfa))
15367     {
15368       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15369                                  start_label, last_label, section);
15370       list_tail = &(*list_tail)->dw_loc_next;
15371       start_label = last_label;
15372     }
15373
15374   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15375                              start_label,
15376                              fde->dw_fde_second_begin
15377                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15378                              section);
15379
15380   if (list && list->dw_loc_next)
15381     gen_llsym (list);
15382
15383   return list;
15384 }
15385
15386 /* Compute a displacement from the "steady-state frame pointer" to the
15387    frame base (often the same as the CFA), and store it in
15388    frame_pointer_fb_offset.  OFFSET is added to the displacement
15389    before the latter is negated.  */
15390
15391 static void
15392 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15393 {
15394   rtx reg, elim;
15395
15396 #ifdef FRAME_POINTER_CFA_OFFSET
15397   reg = frame_pointer_rtx;
15398   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15399 #else
15400   reg = arg_pointer_rtx;
15401   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15402 #endif
15403
15404   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15405   if (GET_CODE (elim) == PLUS)
15406     {
15407       offset += INTVAL (XEXP (elim, 1));
15408       elim = XEXP (elim, 0);
15409     }
15410
15411   frame_pointer_fb_offset = -offset;
15412
15413   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15414      in which to eliminate.  This is because it's stack pointer isn't 
15415      directly accessible as a register within the ISA.  To work around
15416      this, assume that while we cannot provide a proper value for
15417      frame_pointer_fb_offset, we won't need one either.  */
15418   frame_pointer_fb_offset_valid
15419     = ((SUPPORTS_STACK_ALIGNMENT
15420         && (elim == hard_frame_pointer_rtx
15421             || elim == stack_pointer_rtx))
15422        || elim == (frame_pointer_needed
15423                    ? hard_frame_pointer_rtx
15424                    : stack_pointer_rtx));
15425 }
15426
15427 /* Generate a DW_AT_name attribute given some string value to be included as
15428    the value of the attribute.  */
15429
15430 static void
15431 add_name_attribute (dw_die_ref die, const char *name_string)
15432 {
15433   if (name_string != NULL && *name_string != 0)
15434     {
15435       if (demangle_name_func)
15436         name_string = (*demangle_name_func) (name_string);
15437
15438       add_AT_string (die, DW_AT_name, name_string);
15439     }
15440 }
15441
15442 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15443    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15444    of TYPE accordingly.
15445
15446    ??? This is a temporary measure until after we're able to generate
15447    regular DWARF for the complex Ada type system.  */
15448
15449 static void 
15450 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15451                                      dw_die_ref context_die)
15452 {
15453   tree dtype;
15454   dw_die_ref dtype_die;
15455
15456   if (!lang_hooks.types.descriptive_type)
15457     return;
15458
15459   dtype = lang_hooks.types.descriptive_type (type);
15460   if (!dtype)
15461     return;
15462
15463   dtype_die = lookup_type_die (dtype);
15464   if (!dtype_die)
15465     {
15466       /* The descriptive type indirectly references TYPE if this is also the
15467          case for TYPE itself.  Do not deal with the circularity here.  */
15468       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
15469       gen_type_die (dtype, context_die);
15470       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 0;
15471       dtype_die = lookup_type_die (dtype);
15472       gcc_assert (dtype_die);
15473     }
15474
15475   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15476 }
15477
15478 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15479
15480 static void
15481 add_comp_dir_attribute (dw_die_ref die)
15482 {
15483   const char *wd = get_src_pwd ();
15484   char *wd1;
15485
15486   if (wd == NULL)
15487     return;
15488
15489   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15490     {
15491       int wdlen;
15492
15493       wdlen = strlen (wd);
15494       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15495       strcpy (wd1, wd);
15496       wd1 [wdlen] = DIR_SEPARATOR;
15497       wd1 [wdlen + 1] = 0;
15498       wd = wd1;
15499     }
15500
15501     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15502 }
15503
15504 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15505    default.  */
15506
15507 static int
15508 lower_bound_default (void)
15509 {
15510   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15511     {
15512     case DW_LANG_C:
15513     case DW_LANG_C89:
15514     case DW_LANG_C99:
15515     case DW_LANG_C_plus_plus:
15516     case DW_LANG_ObjC:
15517     case DW_LANG_ObjC_plus_plus:
15518     case DW_LANG_Java:
15519       return 0;
15520     case DW_LANG_Fortran77:
15521     case DW_LANG_Fortran90:
15522     case DW_LANG_Fortran95:
15523       return 1;
15524     case DW_LANG_UPC:
15525     case DW_LANG_D:
15526     case DW_LANG_Python:
15527       return dwarf_version >= 4 ? 0 : -1;
15528     case DW_LANG_Ada95:
15529     case DW_LANG_Ada83:
15530     case DW_LANG_Cobol74:
15531     case DW_LANG_Cobol85:
15532     case DW_LANG_Pascal83:
15533     case DW_LANG_Modula2:
15534     case DW_LANG_PLI:
15535       return dwarf_version >= 4 ? 1 : -1;
15536     default:
15537       return -1;
15538     }
15539 }
15540
15541 /* Given a tree node describing an array bound (either lower or upper) output
15542    a representation for that bound.  */
15543
15544 static void
15545 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15546 {
15547   switch (TREE_CODE (bound))
15548     {
15549     case ERROR_MARK:
15550       return;
15551
15552     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15553     case INTEGER_CST:
15554       {
15555         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15556         int dflt;
15557
15558         /* Use the default if possible.  */
15559         if (bound_attr == DW_AT_lower_bound
15560             && host_integerp (bound, 0)
15561             && (dflt = lower_bound_default ()) != -1
15562             && tree_low_cst (bound, 0) == dflt)
15563           ;
15564
15565         /* Otherwise represent the bound as an unsigned value with the
15566            precision of its type.  The precision and signedness of the
15567            type will be necessary to re-interpret it unambiguously.  */
15568         else if (prec < HOST_BITS_PER_WIDE_INT)
15569           {
15570             unsigned HOST_WIDE_INT mask
15571               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15572             add_AT_unsigned (subrange_die, bound_attr,
15573                              TREE_INT_CST_LOW (bound) & mask);
15574           }
15575         else if (prec == HOST_BITS_PER_WIDE_INT
15576                  || TREE_INT_CST_HIGH (bound) == 0)
15577           add_AT_unsigned (subrange_die, bound_attr,
15578                            TREE_INT_CST_LOW (bound));
15579         else
15580           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15581                          TREE_INT_CST_LOW (bound));
15582       }
15583       break;
15584
15585     CASE_CONVERT:
15586     case VIEW_CONVERT_EXPR:
15587       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15588       break;
15589
15590     case SAVE_EXPR:
15591       break;
15592
15593     case VAR_DECL:
15594     case PARM_DECL:
15595     case RESULT_DECL:
15596       {
15597         dw_die_ref decl_die = lookup_decl_die (bound);
15598
15599         /* ??? Can this happen, or should the variable have been bound
15600            first?  Probably it can, since I imagine that we try to create
15601            the types of parameters in the order in which they exist in
15602            the list, and won't have created a forward reference to a
15603            later parameter.  */
15604         if (decl_die != NULL)
15605           {
15606             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15607             break;
15608           }
15609       }
15610       /* FALLTHRU */
15611
15612     default:
15613       {
15614         /* Otherwise try to create a stack operation procedure to
15615            evaluate the value of the array bound.  */
15616
15617         dw_die_ref ctx, decl_die;
15618         dw_loc_list_ref list;
15619
15620         list = loc_list_from_tree (bound, 2);
15621         if (list == NULL || single_element_loc_list_p (list))
15622           {
15623             /* If DW_AT_*bound is not a reference nor constant, it is
15624                a DWARF expression rather than location description.
15625                For that loc_list_from_tree (bound, 0) is needed.
15626                If that fails to give a single element list,
15627                fall back to outputting this as a reference anyway.  */
15628             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15629             if (list2 && single_element_loc_list_p (list2))
15630               {
15631                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15632                 break;
15633               }
15634           }
15635         if (list == NULL)
15636           break;
15637
15638         if (current_function_decl == 0)
15639           ctx = comp_unit_die ();
15640         else
15641           ctx = lookup_decl_die (current_function_decl);
15642
15643         decl_die = new_die (DW_TAG_variable, ctx, bound);
15644         add_AT_flag (decl_die, DW_AT_artificial, 1);
15645         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15646         add_AT_location_description (decl_die, DW_AT_location, list);
15647         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15648         break;
15649       }
15650     }
15651 }
15652
15653 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15654    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15655    Note that the block of subscript information for an array type also
15656    includes information about the element type of the given array type.  */
15657
15658 static void
15659 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15660 {
15661   unsigned dimension_number;
15662   tree lower, upper;
15663   dw_die_ref subrange_die;
15664
15665   for (dimension_number = 0;
15666        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15667        type = TREE_TYPE (type), dimension_number++)
15668     {
15669       tree domain = TYPE_DOMAIN (type);
15670
15671       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15672         break;
15673
15674       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15675          and (in GNU C only) variable bounds.  Handle all three forms
15676          here.  */
15677       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15678       if (domain)
15679         {
15680           /* We have an array type with specified bounds.  */
15681           lower = TYPE_MIN_VALUE (domain);
15682           upper = TYPE_MAX_VALUE (domain);
15683
15684           /* Define the index type.  */
15685           if (TREE_TYPE (domain))
15686             {
15687               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15688                  TREE_TYPE field.  We can't emit debug info for this
15689                  because it is an unnamed integral type.  */
15690               if (TREE_CODE (domain) == INTEGER_TYPE
15691                   && TYPE_NAME (domain) == NULL_TREE
15692                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15693                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15694                 ;
15695               else
15696                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15697                                     type_die);
15698             }
15699
15700           /* ??? If upper is NULL, the array has unspecified length,
15701              but it does have a lower bound.  This happens with Fortran
15702                dimension arr(N:*)
15703              Since the debugger is definitely going to need to know N
15704              to produce useful results, go ahead and output the lower
15705              bound solo, and hope the debugger can cope.  */
15706
15707           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15708           if (upper)
15709             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15710         }
15711
15712       /* Otherwise we have an array type with an unspecified length.  The
15713          DWARF-2 spec does not say how to handle this; let's just leave out the
15714          bounds.  */
15715     }
15716 }
15717
15718 static void
15719 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15720 {
15721   unsigned size;
15722
15723   switch (TREE_CODE (tree_node))
15724     {
15725     case ERROR_MARK:
15726       size = 0;
15727       break;
15728     case ENUMERAL_TYPE:
15729     case RECORD_TYPE:
15730     case UNION_TYPE:
15731     case QUAL_UNION_TYPE:
15732       size = int_size_in_bytes (tree_node);
15733       break;
15734     case FIELD_DECL:
15735       /* For a data member of a struct or union, the DW_AT_byte_size is
15736          generally given as the number of bytes normally allocated for an
15737          object of the *declared* type of the member itself.  This is true
15738          even for bit-fields.  */
15739       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15740       break;
15741     default:
15742       gcc_unreachable ();
15743     }
15744
15745   /* Note that `size' might be -1 when we get to this point.  If it is, that
15746      indicates that the byte size of the entity in question is variable.  We
15747      have no good way of expressing this fact in Dwarf at the present time,
15748      so just let the -1 pass on through.  */
15749   add_AT_unsigned (die, DW_AT_byte_size, size);
15750 }
15751
15752 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15753    which specifies the distance in bits from the highest order bit of the
15754    "containing object" for the bit-field to the highest order bit of the
15755    bit-field itself.
15756
15757    For any given bit-field, the "containing object" is a hypothetical object
15758    (of some integral or enum type) within which the given bit-field lives.  The
15759    type of this hypothetical "containing object" is always the same as the
15760    declared type of the individual bit-field itself.  The determination of the
15761    exact location of the "containing object" for a bit-field is rather
15762    complicated.  It's handled by the `field_byte_offset' function (above).
15763
15764    Note that it is the size (in bytes) of the hypothetical "containing object"
15765    which will be given in the DW_AT_byte_size attribute for this bit-field.
15766    (See `byte_size_attribute' above).  */
15767
15768 static inline void
15769 add_bit_offset_attribute (dw_die_ref die, tree decl)
15770 {
15771   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15772   tree type = DECL_BIT_FIELD_TYPE (decl);
15773   HOST_WIDE_INT bitpos_int;
15774   HOST_WIDE_INT highest_order_object_bit_offset;
15775   HOST_WIDE_INT highest_order_field_bit_offset;
15776   HOST_WIDE_INT bit_offset;
15777
15778   /* Must be a field and a bit field.  */
15779   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15780
15781   /* We can't yet handle bit-fields whose offsets are variable, so if we
15782      encounter such things, just return without generating any attribute
15783      whatsoever.  Likewise for variable or too large size.  */
15784   if (! host_integerp (bit_position (decl), 0)
15785       || ! host_integerp (DECL_SIZE (decl), 1))
15786     return;
15787
15788   bitpos_int = int_bit_position (decl);
15789
15790   /* Note that the bit offset is always the distance (in bits) from the
15791      highest-order bit of the "containing object" to the highest-order bit of
15792      the bit-field itself.  Since the "high-order end" of any object or field
15793      is different on big-endian and little-endian machines, the computation
15794      below must take account of these differences.  */
15795   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15796   highest_order_field_bit_offset = bitpos_int;
15797
15798   if (! BYTES_BIG_ENDIAN)
15799     {
15800       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15801       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15802     }
15803
15804   bit_offset
15805     = (! BYTES_BIG_ENDIAN
15806        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15807        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15808
15809   if (bit_offset < 0)
15810     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15811   else
15812     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15813 }
15814
15815 /* For a FIELD_DECL node which represents a bit field, output an attribute
15816    which specifies the length in bits of the given field.  */
15817
15818 static inline void
15819 add_bit_size_attribute (dw_die_ref die, tree decl)
15820 {
15821   /* Must be a field and a bit field.  */
15822   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15823               && DECL_BIT_FIELD_TYPE (decl));
15824
15825   if (host_integerp (DECL_SIZE (decl), 1))
15826     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15827 }
15828
15829 /* If the compiled language is ANSI C, then add a 'prototyped'
15830    attribute, if arg types are given for the parameters of a function.  */
15831
15832 static inline void
15833 add_prototyped_attribute (dw_die_ref die, tree func_type)
15834 {
15835   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15836       && prototype_p (func_type))
15837     add_AT_flag (die, DW_AT_prototyped, 1);
15838 }
15839
15840 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15841    by looking in either the type declaration or object declaration
15842    equate table.  */
15843
15844 static inline dw_die_ref
15845 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15846 {
15847   dw_die_ref origin_die = NULL;
15848
15849   if (TREE_CODE (origin) != FUNCTION_DECL)
15850     {
15851       /* We may have gotten separated from the block for the inlined
15852          function, if we're in an exception handler or some such; make
15853          sure that the abstract function has been written out.
15854
15855          Doing this for nested functions is wrong, however; functions are
15856          distinct units, and our context might not even be inline.  */
15857       tree fn = origin;
15858
15859       if (TYPE_P (fn))
15860         fn = TYPE_STUB_DECL (fn);
15861
15862       fn = decl_function_context (fn);
15863       if (fn)
15864         dwarf2out_abstract_function (fn);
15865     }
15866
15867   if (DECL_P (origin))
15868     origin_die = lookup_decl_die (origin);
15869   else if (TYPE_P (origin))
15870     origin_die = lookup_type_die (origin);
15871
15872   /* XXX: Functions that are never lowered don't always have correct block
15873      trees (in the case of java, they simply have no block tree, in some other
15874      languages).  For these functions, there is nothing we can really do to
15875      output correct debug info for inlined functions in all cases.  Rather
15876      than die, we'll just produce deficient debug info now, in that we will
15877      have variables without a proper abstract origin.  In the future, when all
15878      functions are lowered, we should re-add a gcc_assert (origin_die)
15879      here.  */
15880
15881   if (origin_die)
15882     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15883   return origin_die;
15884 }
15885
15886 /* We do not currently support the pure_virtual attribute.  */
15887
15888 static inline void
15889 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15890 {
15891   if (DECL_VINDEX (func_decl))
15892     {
15893       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15894
15895       if (host_integerp (DECL_VINDEX (func_decl), 0))
15896         add_AT_loc (die, DW_AT_vtable_elem_location,
15897                     new_loc_descr (DW_OP_constu,
15898                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15899                                    0));
15900
15901       /* GNU extension: Record what type this method came from originally.  */
15902       if (debug_info_level > DINFO_LEVEL_TERSE
15903           && DECL_CONTEXT (func_decl))
15904         add_AT_die_ref (die, DW_AT_containing_type,
15905                         lookup_type_die (DECL_CONTEXT (func_decl)));
15906     }
15907 }
15908 \f
15909 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15910    given decl.  This used to be a vendor extension until after DWARF 4
15911    standardized it.  */
15912
15913 static void
15914 add_linkage_attr (dw_die_ref die, tree decl)
15915 {
15916   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15917
15918   /* Mimic what assemble_name_raw does with a leading '*'.  */
15919   if (name[0] == '*')
15920     name = &name[1];
15921
15922   if (dwarf_version >= 4)
15923     add_AT_string (die, DW_AT_linkage_name, name);
15924   else
15925     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15926 }
15927
15928 /* Add source coordinate attributes for the given decl.  */
15929
15930 static void
15931 add_src_coords_attributes (dw_die_ref die, tree decl)
15932 {
15933   expanded_location s;
15934
15935   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15936     return;
15937   s = expand_location (DECL_SOURCE_LOCATION (decl));
15938   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15939   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15940 }
15941
15942 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15943
15944 static void
15945 add_linkage_name (dw_die_ref die, tree decl)
15946 {
15947   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15948        && TREE_PUBLIC (decl)
15949        && !DECL_ABSTRACT (decl)
15950        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15951        && die->die_tag != DW_TAG_member)
15952     {
15953       /* Defer until we have an assembler name set.  */
15954       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15955         {
15956           limbo_die_node *asm_name;
15957
15958           asm_name = ggc_alloc_cleared_limbo_die_node ();
15959           asm_name->die = die;
15960           asm_name->created_for = decl;
15961           asm_name->next = deferred_asm_name;
15962           deferred_asm_name = asm_name;
15963         }
15964       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15965         add_linkage_attr (die, decl);
15966     }
15967 }
15968
15969 /* Add a DW_AT_name attribute and source coordinate attribute for the
15970    given decl, but only if it actually has a name.  */
15971
15972 static void
15973 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15974 {
15975   tree decl_name;
15976
15977   decl_name = DECL_NAME (decl);
15978   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15979     {
15980       const char *name = dwarf2_name (decl, 0);
15981       if (name)
15982         add_name_attribute (die, name);
15983       if (! DECL_ARTIFICIAL (decl))
15984         add_src_coords_attributes (die, decl);
15985
15986       add_linkage_name (die, decl);
15987     }
15988
15989 #ifdef VMS_DEBUGGING_INFO
15990   /* Get the function's name, as described by its RTL.  This may be different
15991      from the DECL_NAME name used in the source file.  */
15992   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15993     {
15994       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15995                    XEXP (DECL_RTL (decl), 0));
15996       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15997     }
15998 #endif /* VMS_DEBUGGING_INFO */
15999 }
16000
16001 #ifdef VMS_DEBUGGING_INFO
16002 /* Output the debug main pointer die for VMS */
16003
16004 void
16005 dwarf2out_vms_debug_main_pointer (void)
16006 {
16007   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16008   dw_die_ref die;
16009
16010   /* Allocate the VMS debug main subprogram die.  */
16011   die = ggc_alloc_cleared_die_node ();
16012   die->die_tag = DW_TAG_subprogram;
16013   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16014   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16015                                current_function_funcdef_no);
16016   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16017
16018   /* Make it the first child of comp_unit_die ().  */
16019   die->die_parent = comp_unit_die ();
16020   if (comp_unit_die ()->die_child)
16021     {
16022       die->die_sib = comp_unit_die ()->die_child->die_sib;
16023       comp_unit_die ()->die_child->die_sib = die;
16024     }
16025   else
16026     {
16027       die->die_sib = die;
16028       comp_unit_die ()->die_child = die;
16029     }
16030 }
16031 #endif /* VMS_DEBUGGING_INFO */
16032
16033 /* Push a new declaration scope.  */
16034
16035 static void
16036 push_decl_scope (tree scope)
16037 {
16038   VEC_safe_push (tree, gc, decl_scope_table, scope);
16039 }
16040
16041 /* Pop a declaration scope.  */
16042
16043 static inline void
16044 pop_decl_scope (void)
16045 {
16046   VEC_pop (tree, decl_scope_table);
16047 }
16048
16049 /* Return the DIE for the scope that immediately contains this type.
16050    Non-named types get global scope.  Named types nested in other
16051    types get their containing scope if it's open, or global scope
16052    otherwise.  All other types (i.e. function-local named types) get
16053    the current active scope.  */
16054
16055 static dw_die_ref
16056 scope_die_for (tree t, dw_die_ref context_die)
16057 {
16058   dw_die_ref scope_die = NULL;
16059   tree containing_scope;
16060   int i;
16061
16062   /* Non-types always go in the current scope.  */
16063   gcc_assert (TYPE_P (t));
16064
16065   containing_scope = TYPE_CONTEXT (t);
16066
16067   /* Use the containing namespace if it was passed in (for a declaration).  */
16068   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16069     {
16070       if (context_die == lookup_decl_die (containing_scope))
16071         /* OK */;
16072       else
16073         containing_scope = NULL_TREE;
16074     }
16075
16076   /* Ignore function type "scopes" from the C frontend.  They mean that
16077      a tagged type is local to a parmlist of a function declarator, but
16078      that isn't useful to DWARF.  */
16079   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16080     containing_scope = NULL_TREE;
16081
16082   if (SCOPE_FILE_SCOPE_P (containing_scope))
16083     scope_die = comp_unit_die ();
16084   else if (TYPE_P (containing_scope))
16085     {
16086       /* For types, we can just look up the appropriate DIE.  But
16087          first we check to see if we're in the middle of emitting it
16088          so we know where the new DIE should go.  */
16089       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16090         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16091           break;
16092
16093       if (i < 0)
16094         {
16095           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16096                       || TREE_ASM_WRITTEN (containing_scope));
16097           /*We are not in the middle of emitting the type
16098             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16099           scope_die = lookup_type_die (containing_scope);
16100
16101           /* If none of the current dies are suitable, we get file scope.  */
16102           if (scope_die == NULL)
16103             scope_die = comp_unit_die ();
16104         }
16105       else
16106         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16107     }
16108   else
16109     scope_die = context_die;
16110
16111   return scope_die;
16112 }
16113
16114 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16115
16116 static inline int
16117 local_scope_p (dw_die_ref context_die)
16118 {
16119   for (; context_die; context_die = context_die->die_parent)
16120     if (context_die->die_tag == DW_TAG_inlined_subroutine
16121         || context_die->die_tag == DW_TAG_subprogram)
16122       return 1;
16123
16124   return 0;
16125 }
16126
16127 /* Returns nonzero if CONTEXT_DIE is a class.  */
16128
16129 static inline int
16130 class_scope_p (dw_die_ref context_die)
16131 {
16132   return (context_die
16133           && (context_die->die_tag == DW_TAG_structure_type
16134               || context_die->die_tag == DW_TAG_class_type
16135               || context_die->die_tag == DW_TAG_interface_type
16136               || context_die->die_tag == DW_TAG_union_type));
16137 }
16138
16139 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16140    whether or not to treat a DIE in this context as a declaration.  */
16141
16142 static inline int
16143 class_or_namespace_scope_p (dw_die_ref context_die)
16144 {
16145   return (class_scope_p (context_die)
16146           || (context_die && context_die->die_tag == DW_TAG_namespace));
16147 }
16148
16149 /* Many forms of DIEs require a "type description" attribute.  This
16150    routine locates the proper "type descriptor" die for the type given
16151    by 'type', and adds a DW_AT_type attribute below the given die.  */
16152
16153 static void
16154 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16155                     int decl_volatile, dw_die_ref context_die)
16156 {
16157   enum tree_code code  = TREE_CODE (type);
16158   dw_die_ref type_die  = NULL;
16159
16160   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16161      or fixed-point type, use the inner type.  This is because we have no
16162      support for unnamed types in base_type_die.  This can happen if this is
16163      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16164   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16165       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16166     type = TREE_TYPE (type), code = TREE_CODE (type);
16167
16168   if (code == ERROR_MARK
16169       /* Handle a special case.  For functions whose return type is void, we
16170          generate *no* type attribute.  (Note that no object may have type
16171          `void', so this only applies to function return types).  */
16172       || code == VOID_TYPE)
16173     return;
16174
16175   type_die = modified_type_die (type,
16176                                 decl_const || TYPE_READONLY (type),
16177                                 decl_volatile || TYPE_VOLATILE (type),
16178                                 context_die);
16179
16180   if (type_die != NULL)
16181     add_AT_die_ref (object_die, DW_AT_type, type_die);
16182 }
16183
16184 /* Given an object die, add the calling convention attribute for the
16185    function call type.  */
16186 static void
16187 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16188 {
16189   enum dwarf_calling_convention value = DW_CC_normal;
16190
16191   value = ((enum dwarf_calling_convention)
16192            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16193
16194   if (is_fortran ()
16195       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16196     {
16197       /* DWARF 2 doesn't provide a way to identify a program's source-level
16198         entry point.  DW_AT_calling_convention attributes are only meant
16199         to describe functions' calling conventions.  However, lacking a
16200         better way to signal the Fortran main program, we used this for 
16201         a long time, following existing custom.  Now, DWARF 4 has 
16202         DW_AT_main_subprogram, which we add below, but some tools still
16203         rely on the old way, which we thus keep.  */
16204       value = DW_CC_program;
16205
16206       if (dwarf_version >= 4 || !dwarf_strict)
16207         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16208     }
16209
16210   /* Only add the attribute if the backend requests it, and
16211      is not DW_CC_normal.  */
16212   if (value && (value != DW_CC_normal))
16213     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16214 }
16215
16216 /* Given a tree pointer to a struct, class, union, or enum type node, return
16217    a pointer to the (string) tag name for the given type, or zero if the type
16218    was declared without a tag.  */
16219
16220 static const char *
16221 type_tag (const_tree type)
16222 {
16223   const char *name = 0;
16224
16225   if (TYPE_NAME (type) != 0)
16226     {
16227       tree t = 0;
16228
16229       /* Find the IDENTIFIER_NODE for the type name.  */
16230       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16231           && !TYPE_NAMELESS (type))
16232         t = TYPE_NAME (type);
16233
16234       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16235          a TYPE_DECL node, regardless of whether or not a `typedef' was
16236          involved.  */
16237       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16238                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16239         {
16240           /* We want to be extra verbose.  Don't call dwarf_name if
16241              DECL_NAME isn't set.  The default hook for decl_printable_name
16242              doesn't like that, and in this context it's correct to return
16243              0, instead of "<anonymous>" or the like.  */
16244           if (DECL_NAME (TYPE_NAME (type))
16245               && !DECL_NAMELESS (TYPE_NAME (type)))
16246             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16247         }
16248
16249       /* Now get the name as a string, or invent one.  */
16250       if (!name && t != 0)
16251         name = IDENTIFIER_POINTER (t);
16252     }
16253
16254   return (name == 0 || *name == '\0') ? 0 : name;
16255 }
16256
16257 /* Return the type associated with a data member, make a special check
16258    for bit field types.  */
16259
16260 static inline tree
16261 member_declared_type (const_tree member)
16262 {
16263   return (DECL_BIT_FIELD_TYPE (member)
16264           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16265 }
16266
16267 /* Get the decl's label, as described by its RTL. This may be different
16268    from the DECL_NAME name used in the source file.  */
16269
16270 #if 0
16271 static const char *
16272 decl_start_label (tree decl)
16273 {
16274   rtx x;
16275   const char *fnname;
16276
16277   x = DECL_RTL (decl);
16278   gcc_assert (MEM_P (x));
16279
16280   x = XEXP (x, 0);
16281   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16282
16283   fnname = XSTR (x, 0);
16284   return fnname;
16285 }
16286 #endif
16287 \f
16288 /* These routines generate the internal representation of the DIE's for
16289    the compilation unit.  Debugging information is collected by walking
16290    the declaration trees passed in from dwarf2out_decl().  */
16291
16292 static void
16293 gen_array_type_die (tree type, dw_die_ref context_die)
16294 {
16295   dw_die_ref scope_die = scope_die_for (type, context_die);
16296   dw_die_ref array_die;
16297
16298   /* GNU compilers represent multidimensional array types as sequences of one
16299      dimensional array types whose element types are themselves array types.
16300      We sometimes squish that down to a single array_type DIE with multiple
16301      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16302      say that we are allowed to do this kind of compression in C, because
16303      there is no difference between an array of arrays and a multidimensional
16304      array.  We don't do this for Ada to remain as close as possible to the
16305      actual representation, which is especially important against the language
16306      flexibilty wrt arrays of variable size.  */
16307
16308   bool collapse_nested_arrays = !is_ada ();
16309   tree element_type;
16310
16311   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16312      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16313   if (TYPE_STRING_FLAG (type)
16314       && TREE_CODE (type) == ARRAY_TYPE
16315       && is_fortran ()
16316       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16317     {
16318       HOST_WIDE_INT size;
16319
16320       array_die = new_die (DW_TAG_string_type, scope_die, type);
16321       add_name_attribute (array_die, type_tag (type));
16322       equate_type_number_to_die (type, array_die);
16323       size = int_size_in_bytes (type);
16324       if (size >= 0)
16325         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16326       else if (TYPE_DOMAIN (type) != NULL_TREE
16327                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16328                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16329         {
16330           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16331           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16332
16333           size = int_size_in_bytes (TREE_TYPE (szdecl));
16334           if (loc && size > 0)
16335             {
16336               add_AT_location_description (array_die, DW_AT_string_length, loc);
16337               if (size != DWARF2_ADDR_SIZE)
16338                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16339             }
16340         }
16341       return;
16342     }
16343
16344   /* ??? The SGI dwarf reader fails for array of array of enum types
16345      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16346      array type comes before the outer array type.  We thus call gen_type_die
16347      before we new_die and must prevent nested array types collapsing for this
16348      target.  */
16349
16350 #ifdef MIPS_DEBUGGING_INFO
16351   gen_type_die (TREE_TYPE (type), context_die);
16352   collapse_nested_arrays = false;
16353 #endif
16354
16355   array_die = new_die (DW_TAG_array_type, scope_die, type);
16356   add_name_attribute (array_die, type_tag (type));
16357   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16358   if (TYPE_ARTIFICIAL (type))
16359     add_AT_flag (array_die, DW_AT_artificial, 1);
16360   equate_type_number_to_die (type, array_die);
16361
16362   if (TREE_CODE (type) == VECTOR_TYPE)
16363     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16364
16365   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16366   if (is_fortran ()
16367       && TREE_CODE (type) == ARRAY_TYPE
16368       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16369       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16370     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16371
16372 #if 0
16373   /* We default the array ordering.  SDB will probably do
16374      the right things even if DW_AT_ordering is not present.  It's not even
16375      an issue until we start to get into multidimensional arrays anyway.  If
16376      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16377      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16378      and when we find out that we need to put these in, we will only do so
16379      for multidimensional arrays.  */
16380   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16381 #endif
16382
16383 #ifdef MIPS_DEBUGGING_INFO
16384   /* The SGI compilers handle arrays of unknown bound by setting
16385      AT_declaration and not emitting any subrange DIEs.  */
16386   if (TREE_CODE (type) == ARRAY_TYPE
16387       && ! TYPE_DOMAIN (type))
16388     add_AT_flag (array_die, DW_AT_declaration, 1);
16389   else
16390 #endif
16391   if (TREE_CODE (type) == VECTOR_TYPE)
16392     {
16393       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16394       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16395       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16396       add_bound_info (subrange_die, DW_AT_upper_bound,
16397                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16398     }
16399   else
16400     add_subscript_info (array_die, type, collapse_nested_arrays);
16401
16402   /* Add representation of the type of the elements of this array type and
16403      emit the corresponding DIE if we haven't done it already.  */
16404   element_type = TREE_TYPE (type);
16405   if (collapse_nested_arrays)
16406     while (TREE_CODE (element_type) == ARRAY_TYPE)
16407       {
16408         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16409           break;
16410         element_type = TREE_TYPE (element_type);
16411       }
16412
16413 #ifndef MIPS_DEBUGGING_INFO
16414   gen_type_die (element_type, context_die);
16415 #endif
16416
16417   add_type_attribute (array_die, element_type, 0, 0, context_die);
16418
16419   if (get_AT (array_die, DW_AT_name))
16420     add_pubtype (type, array_die);
16421 }
16422
16423 static dw_loc_descr_ref
16424 descr_info_loc (tree val, tree base_decl)
16425 {
16426   HOST_WIDE_INT size;
16427   dw_loc_descr_ref loc, loc2;
16428   enum dwarf_location_atom op;
16429
16430   if (val == base_decl)
16431     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16432
16433   switch (TREE_CODE (val))
16434     {
16435     CASE_CONVERT:
16436       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16437     case VAR_DECL:
16438       return loc_descriptor_from_tree (val, 0);
16439     case INTEGER_CST:
16440       if (host_integerp (val, 0))
16441         return int_loc_descriptor (tree_low_cst (val, 0));
16442       break;
16443     case INDIRECT_REF:
16444       size = int_size_in_bytes (TREE_TYPE (val));
16445       if (size < 0)
16446         break;
16447       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16448       if (!loc)
16449         break;
16450       if (size == DWARF2_ADDR_SIZE)
16451         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16452       else
16453         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16454       return loc;
16455     case POINTER_PLUS_EXPR:
16456     case PLUS_EXPR:
16457       if (host_integerp (TREE_OPERAND (val, 1), 1)
16458           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16459              < 16384)
16460         {
16461           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16462           if (!loc)
16463             break;
16464           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16465         }
16466       else
16467         {
16468           op = DW_OP_plus;
16469         do_binop:
16470           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16471           if (!loc)
16472             break;
16473           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16474           if (!loc2)
16475             break;
16476           add_loc_descr (&loc, loc2);
16477           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16478         }
16479       return loc;
16480     case MINUS_EXPR:
16481       op = DW_OP_minus;
16482       goto do_binop;
16483     case MULT_EXPR:
16484       op = DW_OP_mul;
16485       goto do_binop;
16486     case EQ_EXPR:
16487       op = DW_OP_eq;
16488       goto do_binop;
16489     case NE_EXPR:
16490       op = DW_OP_ne;
16491       goto do_binop;
16492     default:
16493       break;
16494     }
16495   return NULL;
16496 }
16497
16498 static void
16499 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16500                       tree val, tree base_decl)
16501 {
16502   dw_loc_descr_ref loc;
16503
16504   if (host_integerp (val, 0))
16505     {
16506       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16507       return;
16508     }
16509
16510   loc = descr_info_loc (val, base_decl);
16511   if (!loc)
16512     return;
16513
16514   add_AT_loc (die, attr, loc);
16515 }
16516
16517 /* This routine generates DIE for array with hidden descriptor, details
16518    are filled into *info by a langhook.  */
16519
16520 static void
16521 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16522                           dw_die_ref context_die)
16523 {
16524   dw_die_ref scope_die = scope_die_for (type, context_die);
16525   dw_die_ref array_die;
16526   int dim;
16527
16528   array_die = new_die (DW_TAG_array_type, scope_die, type);
16529   add_name_attribute (array_die, type_tag (type));
16530   equate_type_number_to_die (type, array_die);
16531
16532   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16533   if (is_fortran ()
16534       && info->ndimensions >= 2)
16535     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16536
16537   if (info->data_location)
16538     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16539                           info->base_decl);
16540   if (info->associated)
16541     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16542                           info->base_decl);
16543   if (info->allocated)
16544     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16545                           info->base_decl);
16546
16547   for (dim = 0; dim < info->ndimensions; dim++)
16548     {
16549       dw_die_ref subrange_die
16550         = new_die (DW_TAG_subrange_type, array_die, NULL);
16551
16552       if (info->dimen[dim].lower_bound)
16553         {
16554           /* If it is the default value, omit it.  */
16555           int dflt;
16556
16557           if (host_integerp (info->dimen[dim].lower_bound, 0)
16558               && (dflt = lower_bound_default ()) != -1
16559               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16560             ;
16561           else
16562             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16563                                   info->dimen[dim].lower_bound,
16564                                   info->base_decl);
16565         }
16566       if (info->dimen[dim].upper_bound)
16567         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16568                               info->dimen[dim].upper_bound,
16569                               info->base_decl);
16570       if (info->dimen[dim].stride)
16571         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16572                               info->dimen[dim].stride,
16573                               info->base_decl);
16574     }
16575
16576   gen_type_die (info->element_type, context_die);
16577   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16578
16579   if (get_AT (array_die, DW_AT_name))
16580     add_pubtype (type, array_die);
16581 }
16582
16583 #if 0
16584 static void
16585 gen_entry_point_die (tree decl, dw_die_ref context_die)
16586 {
16587   tree origin = decl_ultimate_origin (decl);
16588   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16589
16590   if (origin != NULL)
16591     add_abstract_origin_attribute (decl_die, origin);
16592   else
16593     {
16594       add_name_and_src_coords_attributes (decl_die, decl);
16595       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16596                           0, 0, context_die);
16597     }
16598
16599   if (DECL_ABSTRACT (decl))
16600     equate_decl_number_to_die (decl, decl_die);
16601   else
16602     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16603 }
16604 #endif
16605
16606 /* Walk through the list of incomplete types again, trying once more to
16607    emit full debugging info for them.  */
16608
16609 static void
16610 retry_incomplete_types (void)
16611 {
16612   int i;
16613
16614   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16615     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16616                                   DINFO_USAGE_DIR_USE))
16617       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16618 }
16619
16620 /* Determine what tag to use for a record type.  */
16621
16622 static enum dwarf_tag
16623 record_type_tag (tree type)
16624 {
16625   if (! lang_hooks.types.classify_record)
16626     return DW_TAG_structure_type;
16627
16628   switch (lang_hooks.types.classify_record (type))
16629     {
16630     case RECORD_IS_STRUCT:
16631       return DW_TAG_structure_type;
16632
16633     case RECORD_IS_CLASS:
16634       return DW_TAG_class_type;
16635
16636     case RECORD_IS_INTERFACE:
16637       if (dwarf_version >= 3 || !dwarf_strict)
16638         return DW_TAG_interface_type;
16639       return DW_TAG_structure_type;
16640
16641     default:
16642       gcc_unreachable ();
16643     }
16644 }
16645
16646 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16647    include all of the information about the enumeration values also. Each
16648    enumerated type name/value is listed as a child of the enumerated type
16649    DIE.  */
16650
16651 static dw_die_ref
16652 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16653 {
16654   dw_die_ref type_die = lookup_type_die (type);
16655
16656   if (type_die == NULL)
16657     {
16658       type_die = new_die (DW_TAG_enumeration_type,
16659                           scope_die_for (type, context_die), type);
16660       equate_type_number_to_die (type, type_die);
16661       add_name_attribute (type_die, type_tag (type));
16662       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16663       if (TYPE_ARTIFICIAL (type))
16664         add_AT_flag (type_die, DW_AT_artificial, 1);
16665       if (dwarf_version >= 4 || !dwarf_strict)
16666         {
16667           if (ENUM_IS_SCOPED (type))
16668             add_AT_flag (type_die, DW_AT_enum_class, 1);
16669           if (ENUM_IS_OPAQUE (type))
16670             add_AT_flag (type_die, DW_AT_declaration, 1);
16671         }
16672     }
16673   else if (! TYPE_SIZE (type))
16674     return type_die;
16675   else
16676     remove_AT (type_die, DW_AT_declaration);
16677
16678   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16679      given enum type is incomplete, do not generate the DW_AT_byte_size
16680      attribute or the DW_AT_element_list attribute.  */
16681   if (TYPE_SIZE (type))
16682     {
16683       tree link;
16684
16685       TREE_ASM_WRITTEN (type) = 1;
16686       add_byte_size_attribute (type_die, type);
16687       if (TYPE_STUB_DECL (type) != NULL_TREE)
16688         {
16689           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16690           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16691         }
16692
16693       /* If the first reference to this type was as the return type of an
16694          inline function, then it may not have a parent.  Fix this now.  */
16695       if (type_die->die_parent == NULL)
16696         add_child_die (scope_die_for (type, context_die), type_die);
16697
16698       for (link = TYPE_VALUES (type);
16699            link != NULL; link = TREE_CHAIN (link))
16700         {
16701           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16702           tree value = TREE_VALUE (link);
16703
16704           add_name_attribute (enum_die,
16705                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16706
16707           if (TREE_CODE (value) == CONST_DECL)
16708             value = DECL_INITIAL (value);
16709
16710           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16711             /* DWARF2 does not provide a way of indicating whether or
16712                not enumeration constants are signed or unsigned.  GDB
16713                always assumes the values are signed, so we output all
16714                values as if they were signed.  That means that
16715                enumeration constants with very large unsigned values
16716                will appear to have negative values in the debugger.  */
16717             add_AT_int (enum_die, DW_AT_const_value,
16718                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16719         }
16720     }
16721   else
16722     add_AT_flag (type_die, DW_AT_declaration, 1);
16723
16724   if (get_AT (type_die, DW_AT_name))
16725     add_pubtype (type, type_die);
16726
16727   return type_die;
16728 }
16729
16730 /* Generate a DIE to represent either a real live formal parameter decl or to
16731    represent just the type of some formal parameter position in some function
16732    type.
16733
16734    Note that this routine is a bit unusual because its argument may be a
16735    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16736    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16737    node.  If it's the former then this function is being called to output a
16738    DIE to represent a formal parameter object (or some inlining thereof).  If
16739    it's the latter, then this function is only being called to output a
16740    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16741    argument type of some subprogram type.
16742    If EMIT_NAME_P is true, name and source coordinate attributes
16743    are emitted.  */
16744
16745 static dw_die_ref
16746 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16747                           dw_die_ref context_die)
16748 {
16749   tree node_or_origin = node ? node : origin;
16750   tree ultimate_origin;
16751   dw_die_ref parm_die
16752     = new_die (DW_TAG_formal_parameter, context_die, node);
16753
16754   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16755     {
16756     case tcc_declaration:
16757       ultimate_origin = decl_ultimate_origin (node_or_origin);
16758       if (node || ultimate_origin)
16759         origin = ultimate_origin;
16760       if (origin != NULL)
16761         add_abstract_origin_attribute (parm_die, origin);
16762       else if (emit_name_p)
16763         add_name_and_src_coords_attributes (parm_die, node);
16764       if (origin == NULL
16765           || (! DECL_ABSTRACT (node_or_origin)
16766               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16767                                            decl_function_context
16768                                                             (node_or_origin))))
16769         {
16770           tree type = TREE_TYPE (node_or_origin);
16771           if (decl_by_reference_p (node_or_origin))
16772             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16773                                 context_die);
16774           else
16775             add_type_attribute (parm_die, type,
16776                                 TREE_READONLY (node_or_origin),
16777                                 TREE_THIS_VOLATILE (node_or_origin),
16778                                 context_die);
16779         }
16780       if (origin == NULL && DECL_ARTIFICIAL (node))
16781         add_AT_flag (parm_die, DW_AT_artificial, 1);
16782
16783       if (node && node != origin)
16784         equate_decl_number_to_die (node, parm_die);
16785       if (! DECL_ABSTRACT (node_or_origin))
16786         add_location_or_const_value_attribute (parm_die, node_or_origin,
16787                                                node == NULL, DW_AT_location);
16788
16789       break;
16790
16791     case tcc_type:
16792       /* We were called with some kind of a ..._TYPE node.  */
16793       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16794       break;
16795
16796     default:
16797       gcc_unreachable ();
16798     }
16799
16800   return parm_die;
16801 }
16802
16803 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16804    children DW_TAG_formal_parameter DIEs representing the arguments of the
16805    parameter pack.
16806
16807    PARM_PACK must be a function parameter pack.
16808    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16809    must point to the subsequent arguments of the function PACK_ARG belongs to.
16810    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16811    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16812    following the last one for which a DIE was generated.  */
16813
16814 static dw_die_ref
16815 gen_formal_parameter_pack_die  (tree parm_pack,
16816                                 tree pack_arg,
16817                                 dw_die_ref subr_die,
16818                                 tree *next_arg)
16819 {
16820   tree arg;
16821   dw_die_ref parm_pack_die;
16822
16823   gcc_assert (parm_pack
16824               && lang_hooks.function_parameter_pack_p (parm_pack)
16825               && subr_die);
16826
16827   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16828   add_src_coords_attributes (parm_pack_die, parm_pack);
16829
16830   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16831     {
16832       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16833                                                                  parm_pack))
16834         break;
16835       gen_formal_parameter_die (arg, NULL,
16836                                 false /* Don't emit name attribute.  */,
16837                                 parm_pack_die);
16838     }
16839   if (next_arg)
16840     *next_arg = arg;
16841   return parm_pack_die;
16842 }
16843
16844 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16845    at the end of an (ANSI prototyped) formal parameters list.  */
16846
16847 static void
16848 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16849 {
16850   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16851 }
16852
16853 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16854    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16855    parameters as specified in some function type specification (except for
16856    those which appear as part of a function *definition*).  */
16857
16858 static void
16859 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16860 {
16861   tree link;
16862   tree formal_type = NULL;
16863   tree first_parm_type;
16864   tree arg;
16865
16866   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16867     {
16868       arg = DECL_ARGUMENTS (function_or_method_type);
16869       function_or_method_type = TREE_TYPE (function_or_method_type);
16870     }
16871   else
16872     arg = NULL_TREE;
16873
16874   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16875
16876   /* Make our first pass over the list of formal parameter types and output a
16877      DW_TAG_formal_parameter DIE for each one.  */
16878   for (link = first_parm_type; link; )
16879     {
16880       dw_die_ref parm_die;
16881
16882       formal_type = TREE_VALUE (link);
16883       if (formal_type == void_type_node)
16884         break;
16885
16886       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16887       parm_die = gen_formal_parameter_die (formal_type, NULL,
16888                                            true /* Emit name attribute.  */,
16889                                            context_die);
16890       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16891           && link == first_parm_type)
16892         {
16893           add_AT_flag (parm_die, DW_AT_artificial, 1);
16894           if (dwarf_version >= 3 || !dwarf_strict)
16895             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16896         }
16897       else if (arg && DECL_ARTIFICIAL (arg))
16898         add_AT_flag (parm_die, DW_AT_artificial, 1);
16899
16900       link = TREE_CHAIN (link);
16901       if (arg)
16902         arg = DECL_CHAIN (arg);
16903     }
16904
16905   /* If this function type has an ellipsis, add a
16906      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16907   if (formal_type != void_type_node)
16908     gen_unspecified_parameters_die (function_or_method_type, context_die);
16909
16910   /* Make our second (and final) pass over the list of formal parameter types
16911      and output DIEs to represent those types (as necessary).  */
16912   for (link = TYPE_ARG_TYPES (function_or_method_type);
16913        link && TREE_VALUE (link);
16914        link = TREE_CHAIN (link))
16915     gen_type_die (TREE_VALUE (link), context_die);
16916 }
16917
16918 /* We want to generate the DIE for TYPE so that we can generate the
16919    die for MEMBER, which has been defined; we will need to refer back
16920    to the member declaration nested within TYPE.  If we're trying to
16921    generate minimal debug info for TYPE, processing TYPE won't do the
16922    trick; we need to attach the member declaration by hand.  */
16923
16924 static void
16925 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16926 {
16927   gen_type_die (type, context_die);
16928
16929   /* If we're trying to avoid duplicate debug info, we may not have
16930      emitted the member decl for this function.  Emit it now.  */
16931   if (TYPE_STUB_DECL (type)
16932       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16933       && ! lookup_decl_die (member))
16934     {
16935       dw_die_ref type_die;
16936       gcc_assert (!decl_ultimate_origin (member));
16937
16938       push_decl_scope (type);
16939       type_die = lookup_type_die_strip_naming_typedef (type);
16940       if (TREE_CODE (member) == FUNCTION_DECL)
16941         gen_subprogram_die (member, type_die);
16942       else if (TREE_CODE (member) == FIELD_DECL)
16943         {
16944           /* Ignore the nameless fields that are used to skip bits but handle
16945              C++ anonymous unions and structs.  */
16946           if (DECL_NAME (member) != NULL_TREE
16947               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16948               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16949             {
16950               gen_type_die (member_declared_type (member), type_die);
16951               gen_field_die (member, type_die);
16952             }
16953         }
16954       else
16955         gen_variable_die (member, NULL_TREE, type_die);
16956
16957       pop_decl_scope ();
16958     }
16959 }
16960
16961 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16962    may later generate inlined and/or out-of-line instances of.  */
16963
16964 static void
16965 dwarf2out_abstract_function (tree decl)
16966 {
16967   dw_die_ref old_die;
16968   tree save_fn;
16969   tree context;
16970   int was_abstract;
16971   htab_t old_decl_loc_table;
16972   htab_t old_cached_dw_loc_list_table;
16973   int old_call_site_count, old_tail_call_site_count;
16974   struct call_arg_loc_node *old_call_arg_locations;
16975
16976   /* Make sure we have the actual abstract inline, not a clone.  */
16977   decl = DECL_ORIGIN (decl);
16978
16979   old_die = lookup_decl_die (decl);
16980   if (old_die && get_AT (old_die, DW_AT_inline))
16981     /* We've already generated the abstract instance.  */
16982     return;
16983
16984   /* We can be called while recursively when seeing block defining inlined subroutine
16985      DIE.  Be sure to not clobber the outer location table nor use it or we would
16986      get locations in abstract instantces.  */
16987   old_decl_loc_table = decl_loc_table;
16988   decl_loc_table = NULL;
16989   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16990   cached_dw_loc_list_table = NULL;
16991   old_call_arg_locations = call_arg_locations;
16992   call_arg_locations = NULL;
16993   old_call_site_count = call_site_count;
16994   call_site_count = -1;
16995   old_tail_call_site_count = tail_call_site_count;
16996   tail_call_site_count = -1;
16997
16998   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16999      we don't get confused by DECL_ABSTRACT.  */
17000   if (debug_info_level > DINFO_LEVEL_TERSE)
17001     {
17002       context = decl_class_context (decl);
17003       if (context)
17004         gen_type_die_for_member
17005           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17006     }
17007
17008   /* Pretend we've just finished compiling this function.  */
17009   save_fn = current_function_decl;
17010   current_function_decl = decl;
17011   push_cfun (DECL_STRUCT_FUNCTION (decl));
17012
17013   was_abstract = DECL_ABSTRACT (decl);
17014   set_decl_abstract_flags (decl, 1);
17015   dwarf2out_decl (decl);
17016   if (! was_abstract)
17017     set_decl_abstract_flags (decl, 0);
17018
17019   current_function_decl = save_fn;
17020   decl_loc_table = old_decl_loc_table;
17021   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17022   call_arg_locations = old_call_arg_locations;
17023   call_site_count = old_call_site_count;
17024   tail_call_site_count = old_tail_call_site_count;
17025   pop_cfun ();
17026 }
17027
17028 /* Helper function of premark_used_types() which gets called through
17029    htab_traverse.
17030
17031    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17032    marked as unused by prune_unused_types.  */
17033
17034 static int
17035 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17036 {
17037   tree type;
17038   dw_die_ref die;
17039
17040   type = (tree) *slot;
17041   die = lookup_type_die (type);
17042   if (die != NULL)
17043     die->die_perennial_p = 1;
17044   return 1;
17045 }
17046
17047 /* Helper function of premark_types_used_by_global_vars which gets called
17048    through htab_traverse.
17049
17050    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17051    marked as unused by prune_unused_types. The DIE of the type is marked
17052    only if the global variable using the type will actually be emitted.  */
17053
17054 static int
17055 premark_types_used_by_global_vars_helper (void **slot,
17056                                           void *data ATTRIBUTE_UNUSED)
17057 {
17058   struct types_used_by_vars_entry *entry;
17059   dw_die_ref die;
17060
17061   entry = (struct types_used_by_vars_entry *) *slot;
17062   gcc_assert (entry->type != NULL
17063               && entry->var_decl != NULL);
17064   die = lookup_type_die (entry->type);
17065   if (die)
17066     {
17067       /* Ask cgraph if the global variable really is to be emitted.
17068          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17069       struct varpool_node *node = varpool_get_node (entry->var_decl);
17070       if (node && node->needed)
17071         {
17072           die->die_perennial_p = 1;
17073           /* Keep the parent DIEs as well.  */
17074           while ((die = die->die_parent) && die->die_perennial_p == 0)
17075             die->die_perennial_p = 1;
17076         }
17077     }
17078   return 1;
17079 }
17080
17081 /* Mark all members of used_types_hash as perennial.  */
17082
17083 static void
17084 premark_used_types (void)
17085 {
17086   if (cfun && cfun->used_types_hash)
17087     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17088 }
17089
17090 /* Mark all members of types_used_by_vars_entry as perennial.  */
17091
17092 static void
17093 premark_types_used_by_global_vars (void)
17094 {
17095   if (types_used_by_vars_hash)
17096     htab_traverse (types_used_by_vars_hash,
17097                    premark_types_used_by_global_vars_helper, NULL);
17098 }
17099
17100 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17101    for CA_LOC call arg loc node.  */
17102
17103 static dw_die_ref
17104 gen_call_site_die (tree decl, dw_die_ref subr_die,
17105                    struct call_arg_loc_node *ca_loc)
17106 {
17107   dw_die_ref stmt_die = NULL, die;
17108   tree block = ca_loc->block;
17109
17110   while (block
17111          && block != DECL_INITIAL (decl)
17112          && TREE_CODE (block) == BLOCK)
17113     {
17114       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17115         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17116       if (stmt_die)
17117         break;
17118       block = BLOCK_SUPERCONTEXT (block);
17119     }
17120   if (stmt_die == NULL)
17121     stmt_die = subr_die;
17122   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17123   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17124   if (ca_loc->tail_call_p)
17125     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17126   if (ca_loc->symbol_ref)
17127     {
17128       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17129       if (tdie)
17130         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17131       else
17132         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17133     }
17134   return die;
17135 }
17136
17137 /* Generate a DIE to represent a declared function (either file-scope or
17138    block-local).  */
17139
17140 static void
17141 gen_subprogram_die (tree decl, dw_die_ref context_die)
17142 {
17143   tree origin = decl_ultimate_origin (decl);
17144   dw_die_ref subr_die;
17145   tree outer_scope;
17146   dw_die_ref old_die = lookup_decl_die (decl);
17147   int declaration = (current_function_decl != decl
17148                      || class_or_namespace_scope_p (context_die));
17149
17150   premark_used_types ();
17151
17152   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17153      started to generate the abstract instance of an inline, decided to output
17154      its containing class, and proceeded to emit the declaration of the inline
17155      from the member list for the class.  If so, DECLARATION takes priority;
17156      we'll get back to the abstract instance when done with the class.  */
17157
17158   /* The class-scope declaration DIE must be the primary DIE.  */
17159   if (origin && declaration && class_or_namespace_scope_p (context_die))
17160     {
17161       origin = NULL;
17162       gcc_assert (!old_die);
17163     }
17164
17165   /* Now that the C++ front end lazily declares artificial member fns, we
17166      might need to retrofit the declaration into its class.  */
17167   if (!declaration && !origin && !old_die
17168       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17169       && !class_or_namespace_scope_p (context_die)
17170       && debug_info_level > DINFO_LEVEL_TERSE)
17171     old_die = force_decl_die (decl);
17172
17173   if (origin != NULL)
17174     {
17175       gcc_assert (!declaration || local_scope_p (context_die));
17176
17177       /* Fixup die_parent for the abstract instance of a nested
17178          inline function.  */
17179       if (old_die && old_die->die_parent == NULL)
17180         add_child_die (context_die, old_die);
17181
17182       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17183       add_abstract_origin_attribute (subr_die, origin);
17184       /*  This is where the actual code for a cloned function is.
17185           Let's emit linkage name attribute for it.  This helps
17186           debuggers to e.g, set breakpoints into
17187           constructors/destructors when the user asks "break
17188           K::K".  */
17189       add_linkage_name (subr_die, decl);
17190     }
17191   else if (old_die)
17192     {
17193       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17194       struct dwarf_file_data * file_index = lookup_filename (s.file);
17195
17196       if (!get_AT_flag (old_die, DW_AT_declaration)
17197           /* We can have a normal definition following an inline one in the
17198              case of redefinition of GNU C extern inlines.
17199              It seems reasonable to use AT_specification in this case.  */
17200           && !get_AT (old_die, DW_AT_inline))
17201         {
17202           /* Detect and ignore this case, where we are trying to output
17203              something we have already output.  */
17204           return;
17205         }
17206
17207       /* If the definition comes from the same place as the declaration,
17208          maybe use the old DIE.  We always want the DIE for this function
17209          that has the *_pc attributes to be under comp_unit_die so the
17210          debugger can find it.  We also need to do this for abstract
17211          instances of inlines, since the spec requires the out-of-line copy
17212          to have the same parent.  For local class methods, this doesn't
17213          apply; we just use the old DIE.  */
17214       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17215           && (DECL_ARTIFICIAL (decl)
17216               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17217                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17218                       == (unsigned) s.line))))
17219         {
17220           subr_die = old_die;
17221
17222           /* Clear out the declaration attribute and the formal parameters.
17223              Do not remove all children, because it is possible that this
17224              declaration die was forced using force_decl_die(). In such
17225              cases die that forced declaration die (e.g. TAG_imported_module)
17226              is one of the children that we do not want to remove.  */
17227           remove_AT (subr_die, DW_AT_declaration);
17228           remove_AT (subr_die, DW_AT_object_pointer);
17229           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17230         }
17231       else
17232         {
17233           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17234           add_AT_specification (subr_die, old_die);
17235           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17236             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17237           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17238             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17239         }
17240     }
17241   else
17242     {
17243       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17244
17245       if (TREE_PUBLIC (decl))
17246         add_AT_flag (subr_die, DW_AT_external, 1);
17247
17248       add_name_and_src_coords_attributes (subr_die, decl);
17249       if (debug_info_level > DINFO_LEVEL_TERSE)
17250         {
17251           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17252           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17253                               0, 0, context_die);
17254         }
17255
17256       add_pure_or_virtual_attribute (subr_die, decl);
17257       if (DECL_ARTIFICIAL (decl))
17258         add_AT_flag (subr_die, DW_AT_artificial, 1);
17259
17260       add_accessibility_attribute (subr_die, decl);
17261     }
17262
17263   if (declaration)
17264     {
17265       if (!old_die || !get_AT (old_die, DW_AT_inline))
17266         {
17267           add_AT_flag (subr_die, DW_AT_declaration, 1);
17268
17269           /* If this is an explicit function declaration then generate
17270              a DW_AT_explicit attribute.  */
17271           if (lang_hooks.decls.function_decl_explicit_p (decl)
17272               && (dwarf_version >= 3 || !dwarf_strict))
17273             add_AT_flag (subr_die, DW_AT_explicit, 1);
17274
17275           /* The first time we see a member function, it is in the context of
17276              the class to which it belongs.  We make sure of this by emitting
17277              the class first.  The next time is the definition, which is
17278              handled above.  The two may come from the same source text.
17279
17280              Note that force_decl_die() forces function declaration die. It is
17281              later reused to represent definition.  */
17282           equate_decl_number_to_die (decl, subr_die);
17283         }
17284     }
17285   else if (DECL_ABSTRACT (decl))
17286     {
17287       if (DECL_DECLARED_INLINE_P (decl))
17288         {
17289           if (cgraph_function_possibly_inlined_p (decl))
17290             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17291           else
17292             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17293         }
17294       else
17295         {
17296           if (cgraph_function_possibly_inlined_p (decl))
17297             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17298           else
17299             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17300         }
17301
17302       if (DECL_DECLARED_INLINE_P (decl)
17303           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17304         add_AT_flag (subr_die, DW_AT_artificial, 1);
17305
17306       equate_decl_number_to_die (decl, subr_die);
17307     }
17308   else if (!DECL_EXTERNAL (decl))
17309     {
17310       HOST_WIDE_INT cfa_fb_offset;
17311
17312       if (!old_die || !get_AT (old_die, DW_AT_inline))
17313         equate_decl_number_to_die (decl, subr_die);
17314
17315       if (!flag_reorder_blocks_and_partition)
17316         {
17317           dw_fde_ref fde = cfun->fde;
17318           if (fde->dw_fde_begin)
17319             {
17320               /* We have already generated the labels.  */
17321               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17322               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17323             }
17324           else
17325             {
17326               /* Create start/end labels and add the range.  */
17327               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17328               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17329                                            current_function_funcdef_no);
17330               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17331               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17332                                            current_function_funcdef_no);
17333               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17334             }
17335
17336 #if VMS_DEBUGGING_INFO
17337       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17338          Section 2.3 Prologue and Epilogue Attributes:
17339          When a breakpoint is set on entry to a function, it is generally
17340          desirable for execution to be suspended, not on the very first
17341          instruction of the function, but rather at a point after the
17342          function's frame has been set up, after any language defined local
17343          declaration processing has been completed, and before execution of
17344          the first statement of the function begins. Debuggers generally
17345          cannot properly determine where this point is.  Similarly for a
17346          breakpoint set on exit from a function. The prologue and epilogue
17347          attributes allow a compiler to communicate the location(s) to use.  */
17348
17349       {
17350         if (fde->dw_fde_vms_end_prologue)
17351           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17352             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17353
17354         if (fde->dw_fde_vms_begin_epilogue)
17355           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17356             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17357       }
17358 #endif
17359
17360           add_pubname (decl, subr_die);
17361         }
17362       else
17363         {
17364           /* Generate pubnames entries for the split function code ranges.  */
17365           dw_fde_ref fde = cfun->fde;
17366
17367           if (fde->dw_fde_second_begin)
17368             {
17369               if (dwarf_version >= 3 || !dwarf_strict)
17370                 {
17371                   /* We should use ranges for non-contiguous code section 
17372                      addresses.  Use the actual code range for the initial
17373                      section, since the HOT/COLD labels might precede an 
17374                      alignment offset.  */
17375                   bool range_list_added = false;
17376                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17377                                         fde->dw_fde_end, &range_list_added);
17378                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17379                                         fde->dw_fde_second_end,
17380                                         &range_list_added);
17381                   add_pubname (decl, subr_die);
17382                   if (range_list_added)
17383                     add_ranges (NULL);
17384                 }
17385               else
17386                 {
17387                   /* There is no real support in DW2 for this .. so we make
17388                      a work-around.  First, emit the pub name for the segment
17389                      containing the function label.  Then make and emit a
17390                      simplified subprogram DIE for the second segment with the
17391                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17392                      linkage name for the second die so that gdb will find both
17393                      sections when given "b foo".  */
17394                   const char *name = NULL;
17395                   tree decl_name = DECL_NAME (decl);
17396                   dw_die_ref seg_die;
17397
17398                   /* Do the 'primary' section.   */
17399                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17400                                  fde->dw_fde_begin);
17401                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17402                                  fde->dw_fde_end);
17403                   /* Add it.   */
17404                   add_pubname (decl, subr_die);
17405
17406                   /* Build a minimal DIE for the secondary section.  */
17407                   seg_die = new_die (DW_TAG_subprogram,
17408                                      subr_die->die_parent, decl);
17409
17410                   if (TREE_PUBLIC (decl))
17411                     add_AT_flag (seg_die, DW_AT_external, 1);
17412
17413                   if (decl_name != NULL 
17414                       && IDENTIFIER_POINTER (decl_name) != NULL)
17415                     {
17416                       name = dwarf2_name (decl, 1);
17417                       if (! DECL_ARTIFICIAL (decl))
17418                         add_src_coords_attributes (seg_die, decl);
17419
17420                       add_linkage_name (seg_die, decl);
17421                     }
17422                   gcc_assert (name != NULL);
17423                   add_pure_or_virtual_attribute (seg_die, decl);
17424                   if (DECL_ARTIFICIAL (decl))
17425                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17426
17427                   name = concat ("__second_sect_of_", name, NULL); 
17428                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17429                                  fde->dw_fde_second_begin);
17430                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17431                                  fde->dw_fde_second_end);
17432                   add_name_attribute (seg_die, name);
17433                   add_pubname_string (name, seg_die);
17434                 }
17435             }
17436           else
17437             {
17438               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17439               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17440               add_pubname (decl, subr_die);
17441             }
17442         }
17443
17444 #ifdef MIPS_DEBUGGING_INFO
17445       /* Add a reference to the FDE for this routine.  */
17446       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17447 #endif
17448
17449       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17450
17451       /* We define the "frame base" as the function's CFA.  This is more
17452          convenient for several reasons: (1) It's stable across the prologue
17453          and epilogue, which makes it better than just a frame pointer,
17454          (2) With dwarf3, there exists a one-byte encoding that allows us
17455          to reference the .debug_frame data by proxy, but failing that,
17456          (3) We can at least reuse the code inspection and interpretation
17457          code that determines the CFA position at various points in the
17458          function.  */
17459       if (dwarf_version >= 3)
17460         {
17461           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17462           add_AT_loc (subr_die, DW_AT_frame_base, op);
17463         }
17464       else
17465         {
17466           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17467           if (list->dw_loc_next)
17468             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17469           else
17470             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17471         }
17472
17473       /* Compute a displacement from the "steady-state frame pointer" to
17474          the CFA.  The former is what all stack slots and argument slots
17475          will reference in the rtl; the later is what we've told the
17476          debugger about.  We'll need to adjust all frame_base references
17477          by this displacement.  */
17478       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17479
17480       if (cfun->static_chain_decl)
17481         add_AT_location_description (subr_die, DW_AT_static_link,
17482                  loc_list_from_tree (cfun->static_chain_decl, 2));
17483     }
17484
17485   /* Generate child dies for template paramaters.  */
17486   if (debug_info_level > DINFO_LEVEL_TERSE)
17487     gen_generic_params_dies (decl);
17488
17489   /* Now output descriptions of the arguments for this function. This gets
17490      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17491      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17492      `...' at the end of the formal parameter list.  In order to find out if
17493      there was a trailing ellipsis or not, we must instead look at the type
17494      associated with the FUNCTION_DECL.  This will be a node of type
17495      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17496      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17497      an ellipsis at the end.  */
17498
17499   /* In the case where we are describing a mere function declaration, all we
17500      need to do here (and all we *can* do here) is to describe the *types* of
17501      its formal parameters.  */
17502   if (debug_info_level <= DINFO_LEVEL_TERSE)
17503     ;
17504   else if (declaration)
17505     gen_formal_types_die (decl, subr_die);
17506   else
17507     {
17508       /* Generate DIEs to represent all known formal parameters.  */
17509       tree parm = DECL_ARGUMENTS (decl);
17510       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17511       tree generic_decl_parm = generic_decl
17512                                 ? DECL_ARGUMENTS (generic_decl)
17513                                 : NULL;
17514
17515       /* Now we want to walk the list of parameters of the function and
17516          emit their relevant DIEs.
17517
17518          We consider the case of DECL being an instance of a generic function
17519          as well as it being a normal function.
17520
17521          If DECL is an instance of a generic function we walk the
17522          parameters of the generic function declaration _and_ the parameters of
17523          DECL itself. This is useful because we want to emit specific DIEs for
17524          function parameter packs and those are declared as part of the
17525          generic function declaration. In that particular case,
17526          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17527          That DIE has children DIEs representing the set of arguments
17528          of the pack. Note that the set of pack arguments can be empty.
17529          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17530          children DIE.
17531
17532          Otherwise, we just consider the parameters of DECL.  */
17533       while (generic_decl_parm || parm)
17534         {
17535           if (generic_decl_parm
17536               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17537             gen_formal_parameter_pack_die (generic_decl_parm,
17538                                            parm, subr_die,
17539                                            &parm);
17540           else if (parm)
17541             {
17542               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17543
17544               if (parm == DECL_ARGUMENTS (decl)
17545                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17546                   && parm_die
17547                   && (dwarf_version >= 3 || !dwarf_strict))
17548                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17549
17550               parm = DECL_CHAIN (parm);
17551             }
17552
17553           if (generic_decl_parm)
17554             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17555         }
17556
17557       /* Decide whether we need an unspecified_parameters DIE at the end.
17558          There are 2 more cases to do this for: 1) the ansi ... declaration -
17559          this is detectable when the end of the arg list is not a
17560          void_type_node 2) an unprototyped function declaration (not a
17561          definition).  This just means that we have no info about the
17562          parameters at all.  */
17563       if (prototype_p (TREE_TYPE (decl)))
17564         {
17565           /* This is the prototyped case, check for....  */
17566           if (stdarg_p (TREE_TYPE (decl)))
17567             gen_unspecified_parameters_die (decl, subr_die);
17568         }
17569       else if (DECL_INITIAL (decl) == NULL_TREE)
17570         gen_unspecified_parameters_die (decl, subr_die);
17571     }
17572
17573   /* Output Dwarf info for all of the stuff within the body of the function
17574      (if it has one - it may be just a declaration).  */
17575   outer_scope = DECL_INITIAL (decl);
17576
17577   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17578      a function.  This BLOCK actually represents the outermost binding contour
17579      for the function, i.e. the contour in which the function's formal
17580      parameters and labels get declared. Curiously, it appears that the front
17581      end doesn't actually put the PARM_DECL nodes for the current function onto
17582      the BLOCK_VARS list for this outer scope, but are strung off of the
17583      DECL_ARGUMENTS list for the function instead.
17584
17585      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17586      the LABEL_DECL nodes for the function however, and we output DWARF info
17587      for those in decls_for_scope.  Just within the `outer_scope' there will be
17588      a BLOCK node representing the function's outermost pair of curly braces,
17589      and any blocks used for the base and member initializers of a C++
17590      constructor function.  */
17591   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17592     {
17593       int call_site_note_count = 0;
17594       int tail_call_site_note_count = 0;
17595
17596       /* Emit a DW_TAG_variable DIE for a named return value.  */
17597       if (DECL_NAME (DECL_RESULT (decl)))
17598         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17599
17600       current_function_has_inlines = 0;
17601       decls_for_scope (outer_scope, subr_die, 0);
17602
17603       if (call_arg_locations && !dwarf_strict)
17604         {
17605           struct call_arg_loc_node *ca_loc;
17606           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17607             {
17608               dw_die_ref die = NULL;
17609               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17610               rtx arg, next_arg;
17611
17612               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17613                    arg; arg = next_arg)
17614                 {
17615                   dw_loc_descr_ref reg, val;
17616                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17617                   dw_die_ref cdie, tdie = NULL;
17618
17619                   next_arg = XEXP (arg, 1);
17620                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17621                       && next_arg
17622                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17623                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17624                       && REGNO (XEXP (XEXP (arg, 0), 0))
17625                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17626                     next_arg = XEXP (next_arg, 1);
17627                   if (mode == VOIDmode)
17628                     {
17629                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17630                       if (mode == VOIDmode)
17631                         mode = GET_MODE (XEXP (arg, 0));
17632                     }
17633                   if (mode == VOIDmode || mode == BLKmode)
17634                     continue;
17635                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17636                     {
17637                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17638                       tloc = XEXP (XEXP (arg, 0), 1);
17639                       continue;
17640                     }
17641                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17642                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17643                     {
17644                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17645                       tlocc = XEXP (XEXP (arg, 0), 1);
17646                       continue;
17647                     }
17648                   reg = NULL;
17649                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17650                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17651                                               VAR_INIT_STATUS_INITIALIZED);
17652                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17653                     {
17654                       rtx mem = XEXP (XEXP (arg, 0), 0);
17655                       reg = mem_loc_descriptor (XEXP (mem, 0),
17656                                                 get_address_mode (mem),
17657                                                 GET_MODE (mem),
17658                                                 VAR_INIT_STATUS_INITIALIZED);
17659                     }
17660                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17661                            == DEBUG_PARAMETER_REF)
17662                     {
17663                       tree tdecl
17664                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17665                       tdie = lookup_decl_die (tdecl);
17666                       if (tdie == NULL)
17667                         continue;
17668                     }
17669                   else
17670                     continue;
17671                   if (reg == NULL
17672                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17673                          != DEBUG_PARAMETER_REF)
17674                     continue;
17675                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17676                                             VOIDmode,
17677                                             VAR_INIT_STATUS_INITIALIZED);
17678                   if (val == NULL)
17679                     continue;
17680                   if (die == NULL)
17681                     die = gen_call_site_die (decl, subr_die, ca_loc);
17682                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17683                                   NULL_TREE);
17684                   if (reg != NULL)
17685                     add_AT_loc (cdie, DW_AT_location, reg);
17686                   else if (tdie != NULL)
17687                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17688                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17689                   if (next_arg != XEXP (arg, 1))
17690                     {
17691                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17692                       if (mode == VOIDmode)
17693                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17694                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17695                                                             0), 1),
17696                                                 mode, VOIDmode,
17697                                                 VAR_INIT_STATUS_INITIALIZED);
17698                       if (val != NULL)
17699                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17700                     }
17701                 }
17702               if (die == NULL
17703                   && (ca_loc->symbol_ref || tloc))
17704                 die = gen_call_site_die (decl, subr_die, ca_loc);
17705               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17706                 {
17707                   dw_loc_descr_ref tval = NULL;
17708
17709                   if (tloc != NULL_RTX)
17710                     tval = mem_loc_descriptor (tloc,
17711                                                GET_MODE (tloc) == VOIDmode
17712                                                ? Pmode : GET_MODE (tloc),
17713                                                VOIDmode,
17714                                                VAR_INIT_STATUS_INITIALIZED);
17715                   if (tval)
17716                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17717                   else if (tlocc != NULL_RTX)
17718                     {
17719                       tval = mem_loc_descriptor (tlocc,
17720                                                  GET_MODE (tlocc) == VOIDmode
17721                                                  ? Pmode : GET_MODE (tlocc),
17722                                                  VOIDmode,
17723                                                  VAR_INIT_STATUS_INITIALIZED);
17724                       if (tval)
17725                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17726                                     tval);
17727                     }
17728                 }
17729               if (die != NULL)
17730                 {
17731                   call_site_note_count++;
17732                   if (ca_loc->tail_call_p)
17733                     tail_call_site_note_count++;
17734                 }
17735             }
17736         }
17737       call_arg_locations = NULL;
17738       call_arg_loc_last = NULL;
17739       if (tail_call_site_count >= 0
17740           && tail_call_site_count == tail_call_site_note_count
17741           && !dwarf_strict)
17742         {
17743           if (call_site_count >= 0
17744               && call_site_count == call_site_note_count)
17745             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17746           else
17747             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17748         }
17749       call_site_count = -1;
17750       tail_call_site_count = -1;
17751     }
17752   /* Add the calling convention attribute if requested.  */
17753   add_calling_convention_attribute (subr_die, decl);
17754
17755 }
17756
17757 /* Returns a hash value for X (which really is a die_struct).  */
17758
17759 static hashval_t
17760 common_block_die_table_hash (const void *x)
17761 {
17762   const_dw_die_ref d = (const_dw_die_ref) x;
17763   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17764 }
17765
17766 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17767    as decl_id and die_parent of die_struct Y.  */
17768
17769 static int
17770 common_block_die_table_eq (const void *x, const void *y)
17771 {
17772   const_dw_die_ref d = (const_dw_die_ref) x;
17773   const_dw_die_ref e = (const_dw_die_ref) y;
17774   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17775 }
17776
17777 /* Generate a DIE to represent a declared data object.
17778    Either DECL or ORIGIN must be non-null.  */
17779
17780 static void
17781 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17782 {
17783   HOST_WIDE_INT off;
17784   tree com_decl;
17785   tree decl_or_origin = decl ? decl : origin;
17786   tree ultimate_origin;
17787   dw_die_ref var_die;
17788   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17789   dw_die_ref origin_die;
17790   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17791                       || class_or_namespace_scope_p (context_die));
17792   bool specialization_p = false;
17793
17794   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17795   if (decl || ultimate_origin)
17796     origin = ultimate_origin;
17797   com_decl = fortran_common (decl_or_origin, &off);
17798
17799   /* Symbol in common gets emitted as a child of the common block, in the form
17800      of a data member.  */
17801   if (com_decl)
17802     {
17803       dw_die_ref com_die;
17804       dw_loc_list_ref loc;
17805       die_node com_die_arg;
17806
17807       var_die = lookup_decl_die (decl_or_origin);
17808       if (var_die)
17809         {
17810           if (get_AT (var_die, DW_AT_location) == NULL)
17811             {
17812               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17813               if (loc)
17814                 {
17815                   if (off)
17816                     {
17817                       /* Optimize the common case.  */
17818                       if (single_element_loc_list_p (loc)
17819                           && loc->expr->dw_loc_opc == DW_OP_addr
17820                           && loc->expr->dw_loc_next == NULL
17821                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17822                              == SYMBOL_REF)
17823                         loc->expr->dw_loc_oprnd1.v.val_addr
17824                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17825                         else
17826                           loc_list_plus_const (loc, off);
17827                     }
17828                   add_AT_location_description (var_die, DW_AT_location, loc);
17829                   remove_AT (var_die, DW_AT_declaration);
17830                 }
17831             }
17832           return;
17833         }
17834
17835       if (common_block_die_table == NULL)
17836         common_block_die_table
17837           = htab_create_ggc (10, common_block_die_table_hash,
17838                              common_block_die_table_eq, NULL);
17839
17840       com_die_arg.decl_id = DECL_UID (com_decl);
17841       com_die_arg.die_parent = context_die;
17842       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17843       loc = loc_list_from_tree (com_decl, 2);
17844       if (com_die == NULL)
17845         {
17846           const char *cnam
17847             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17848           void **slot;
17849
17850           com_die = new_die (DW_TAG_common_block, context_die, decl);
17851           add_name_and_src_coords_attributes (com_die, com_decl);
17852           if (loc)
17853             {
17854               add_AT_location_description (com_die, DW_AT_location, loc);
17855               /* Avoid sharing the same loc descriptor between
17856                  DW_TAG_common_block and DW_TAG_variable.  */
17857               loc = loc_list_from_tree (com_decl, 2);
17858             }
17859           else if (DECL_EXTERNAL (decl))
17860             add_AT_flag (com_die, DW_AT_declaration, 1);
17861           add_pubname_string (cnam, com_die); /* ??? needed? */
17862           com_die->decl_id = DECL_UID (com_decl);
17863           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17864           *slot = (void *) com_die;
17865         }
17866       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17867         {
17868           add_AT_location_description (com_die, DW_AT_location, loc);
17869           loc = loc_list_from_tree (com_decl, 2);
17870           remove_AT (com_die, DW_AT_declaration);
17871         }
17872       var_die = new_die (DW_TAG_variable, com_die, decl);
17873       add_name_and_src_coords_attributes (var_die, decl);
17874       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17875                           TREE_THIS_VOLATILE (decl), context_die);
17876       add_AT_flag (var_die, DW_AT_external, 1);
17877       if (loc)
17878         {
17879           if (off)
17880             {
17881               /* Optimize the common case.  */
17882               if (single_element_loc_list_p (loc)
17883                   && loc->expr->dw_loc_opc == DW_OP_addr
17884                   && loc->expr->dw_loc_next == NULL
17885                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17886                 loc->expr->dw_loc_oprnd1.v.val_addr
17887                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17888               else
17889                 loc_list_plus_const (loc, off);
17890             }
17891           add_AT_location_description (var_die, DW_AT_location, loc);
17892         }
17893       else if (DECL_EXTERNAL (decl))
17894         add_AT_flag (var_die, DW_AT_declaration, 1);
17895       equate_decl_number_to_die (decl, var_die);
17896       return;
17897     }
17898
17899   /* If the compiler emitted a definition for the DECL declaration
17900      and if we already emitted a DIE for it, don't emit a second
17901      DIE for it again. Allow re-declarations of DECLs that are
17902      inside functions, though.  */
17903   if (old_die && declaration && !local_scope_p (context_die))
17904     return;
17905
17906   /* For static data members, the declaration in the class is supposed
17907      to have DW_TAG_member tag; the specification should still be
17908      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17909   if (declaration && class_scope_p (context_die))
17910     var_die = new_die (DW_TAG_member, context_die, decl);
17911   else
17912     var_die = new_die (DW_TAG_variable, context_die, decl);
17913
17914   origin_die = NULL;
17915   if (origin != NULL)
17916     origin_die = add_abstract_origin_attribute (var_die, origin);
17917
17918   /* Loop unrolling can create multiple blocks that refer to the same
17919      static variable, so we must test for the DW_AT_declaration flag.
17920
17921      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17922      copy decls and set the DECL_ABSTRACT flag on them instead of
17923      sharing them.
17924
17925      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17926
17927      ??? The declare_in_namespace support causes us to get two DIEs for one
17928      variable, both of which are declarations.  We want to avoid considering
17929      one to be a specification, so we must test that this DIE is not a
17930      declaration.  */
17931   else if (old_die && TREE_STATIC (decl) && ! declaration
17932            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17933     {
17934       /* This is a definition of a C++ class level static.  */
17935       add_AT_specification (var_die, old_die);
17936       specialization_p = true;
17937       if (DECL_NAME (decl))
17938         {
17939           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17940           struct dwarf_file_data * file_index = lookup_filename (s.file);
17941
17942           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17943             add_AT_file (var_die, DW_AT_decl_file, file_index);
17944
17945           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17946             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17947
17948           if (old_die->die_tag == DW_TAG_member)
17949             add_linkage_name (var_die, decl);
17950         }
17951     }
17952   else
17953     add_name_and_src_coords_attributes (var_die, decl);
17954
17955   if ((origin == NULL && !specialization_p)
17956       || (origin != NULL
17957           && !DECL_ABSTRACT (decl_or_origin)
17958           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17959                                        decl_function_context
17960                                                         (decl_or_origin))))
17961     {
17962       tree type = TREE_TYPE (decl_or_origin);
17963
17964       if (decl_by_reference_p (decl_or_origin))
17965         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17966       else
17967         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17968                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17969     }
17970
17971   if (origin == NULL && !specialization_p)
17972     {
17973       if (TREE_PUBLIC (decl))
17974         add_AT_flag (var_die, DW_AT_external, 1);
17975
17976       if (DECL_ARTIFICIAL (decl))
17977         add_AT_flag (var_die, DW_AT_artificial, 1);
17978
17979       add_accessibility_attribute (var_die, decl);
17980     }
17981
17982   if (declaration)
17983     add_AT_flag (var_die, DW_AT_declaration, 1);
17984
17985   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17986     equate_decl_number_to_die (decl, var_die);
17987
17988   if (! declaration
17989       && (! DECL_ABSTRACT (decl_or_origin)
17990           /* Local static vars are shared between all clones/inlines,
17991              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17992              already set.  */
17993           || (TREE_CODE (decl_or_origin) == VAR_DECL
17994               && TREE_STATIC (decl_or_origin)
17995               && DECL_RTL_SET_P (decl_or_origin)))
17996       /* When abstract origin already has DW_AT_location attribute, no need
17997          to add it again.  */
17998       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17999     {
18000       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18001           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18002         defer_location (decl_or_origin, var_die);
18003       else
18004         add_location_or_const_value_attribute (var_die, decl_or_origin,
18005                                                decl == NULL, DW_AT_location);
18006       add_pubname (decl_or_origin, var_die);
18007     }
18008   else
18009     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18010 }
18011
18012 /* Generate a DIE to represent a named constant.  */
18013
18014 static void
18015 gen_const_die (tree decl, dw_die_ref context_die)
18016 {
18017   dw_die_ref const_die;
18018   tree type = TREE_TYPE (decl);
18019
18020   const_die = new_die (DW_TAG_constant, context_die, decl);
18021   add_name_and_src_coords_attributes (const_die, decl);
18022   add_type_attribute (const_die, type, 1, 0, context_die);
18023   if (TREE_PUBLIC (decl))
18024     add_AT_flag (const_die, DW_AT_external, 1);
18025   if (DECL_ARTIFICIAL (decl))
18026     add_AT_flag (const_die, DW_AT_artificial, 1);
18027   tree_add_const_value_attribute_for_decl (const_die, decl);
18028 }
18029
18030 /* Generate a DIE to represent a label identifier.  */
18031
18032 static void
18033 gen_label_die (tree decl, dw_die_ref context_die)
18034 {
18035   tree origin = decl_ultimate_origin (decl);
18036   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18037   rtx insn;
18038   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18039
18040   if (origin != NULL)
18041     add_abstract_origin_attribute (lbl_die, origin);
18042   else
18043     add_name_and_src_coords_attributes (lbl_die, decl);
18044
18045   if (DECL_ABSTRACT (decl))
18046     equate_decl_number_to_die (decl, lbl_die);
18047   else
18048     {
18049       insn = DECL_RTL_IF_SET (decl);
18050
18051       /* Deleted labels are programmer specified labels which have been
18052          eliminated because of various optimizations.  We still emit them
18053          here so that it is possible to put breakpoints on them.  */
18054       if (insn
18055           && (LABEL_P (insn)
18056               || ((NOTE_P (insn)
18057                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18058         {
18059           /* When optimization is enabled (via -O) some parts of the compiler
18060              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18061              represent source-level labels which were explicitly declared by
18062              the user.  This really shouldn't be happening though, so catch
18063              it if it ever does happen.  */
18064           gcc_assert (!INSN_DELETED_P (insn));
18065
18066           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18067           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18068         }
18069       else if (insn
18070                && NOTE_P (insn)
18071                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18072                && CODE_LABEL_NUMBER (insn) != -1)
18073         {
18074           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18075           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18076         }
18077     }
18078 }
18079
18080 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18081    attributes to the DIE for a block STMT, to describe where the inlined
18082    function was called from.  This is similar to add_src_coords_attributes.  */
18083
18084 static inline void
18085 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18086 {
18087   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18088
18089   if (dwarf_version >= 3 || !dwarf_strict)
18090     {
18091       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18092       add_AT_unsigned (die, DW_AT_call_line, s.line);
18093     }
18094 }
18095
18096
18097 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18098    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18099
18100 static inline void
18101 add_high_low_attributes (tree stmt, dw_die_ref die)
18102 {
18103   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18104
18105   if (BLOCK_FRAGMENT_CHAIN (stmt)
18106       && (dwarf_version >= 3 || !dwarf_strict))
18107     {
18108       tree chain;
18109
18110       if (inlined_function_outer_scope_p (stmt))
18111         {
18112           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18113                                        BLOCK_NUMBER (stmt));
18114           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18115         }
18116
18117       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18118
18119       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18120       do
18121         {
18122           add_ranges (chain);
18123           chain = BLOCK_FRAGMENT_CHAIN (chain);
18124         }
18125       while (chain);
18126       add_ranges (NULL);
18127     }
18128   else
18129     {
18130       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18131                                    BLOCK_NUMBER (stmt));
18132       add_AT_lbl_id (die, DW_AT_low_pc, label);
18133       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18134                                    BLOCK_NUMBER (stmt));
18135       add_AT_lbl_id (die, DW_AT_high_pc, label);
18136     }
18137 }
18138
18139 /* Generate a DIE for a lexical block.  */
18140
18141 static void
18142 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18143 {
18144   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18145
18146   if (call_arg_locations)
18147     {
18148       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18149         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18150                                BLOCK_NUMBER (stmt) + 1);
18151       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18152     }
18153
18154   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18155     add_high_low_attributes (stmt, stmt_die);
18156
18157   decls_for_scope (stmt, stmt_die, depth);
18158 }
18159
18160 /* Generate a DIE for an inlined subprogram.  */
18161
18162 static void
18163 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18164 {
18165   tree decl;
18166
18167   /* The instance of function that is effectively being inlined shall not
18168      be abstract.  */
18169   gcc_assert (! BLOCK_ABSTRACT (stmt));
18170
18171   decl = block_ultimate_origin (stmt);
18172
18173   /* Emit info for the abstract instance first, if we haven't yet.  We
18174      must emit this even if the block is abstract, otherwise when we
18175      emit the block below (or elsewhere), we may end up trying to emit
18176      a die whose origin die hasn't been emitted, and crashing.  */
18177   dwarf2out_abstract_function (decl);
18178
18179   if (! BLOCK_ABSTRACT (stmt))
18180     {
18181       dw_die_ref subr_die
18182         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18183
18184       if (call_arg_locations)
18185         {
18186           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18187             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18188                                    BLOCK_NUMBER (stmt) + 1);
18189           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18190         }
18191       add_abstract_origin_attribute (subr_die, decl);
18192       if (TREE_ASM_WRITTEN (stmt))
18193         add_high_low_attributes (stmt, subr_die);
18194       add_call_src_coords_attributes (stmt, subr_die);
18195
18196       decls_for_scope (stmt, subr_die, depth);
18197       current_function_has_inlines = 1;
18198     }
18199 }
18200
18201 /* Generate a DIE for a field in a record, or structure.  */
18202
18203 static void
18204 gen_field_die (tree decl, dw_die_ref context_die)
18205 {
18206   dw_die_ref decl_die;
18207
18208   if (TREE_TYPE (decl) == error_mark_node)
18209     return;
18210
18211   decl_die = new_die (DW_TAG_member, context_die, decl);
18212   add_name_and_src_coords_attributes (decl_die, decl);
18213   add_type_attribute (decl_die, member_declared_type (decl),
18214                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18215                       context_die);
18216
18217   if (DECL_BIT_FIELD_TYPE (decl))
18218     {
18219       add_byte_size_attribute (decl_die, decl);
18220       add_bit_size_attribute (decl_die, decl);
18221       add_bit_offset_attribute (decl_die, decl);
18222     }
18223
18224   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18225     add_data_member_location_attribute (decl_die, decl);
18226
18227   if (DECL_ARTIFICIAL (decl))
18228     add_AT_flag (decl_die, DW_AT_artificial, 1);
18229
18230   add_accessibility_attribute (decl_die, decl);
18231
18232   /* Equate decl number to die, so that we can look up this decl later on.  */
18233   equate_decl_number_to_die (decl, decl_die);
18234 }
18235
18236 #if 0
18237 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18238    Use modified_type_die instead.
18239    We keep this code here just in case these types of DIEs may be needed to
18240    represent certain things in other languages (e.g. Pascal) someday.  */
18241
18242 static void
18243 gen_pointer_type_die (tree type, dw_die_ref context_die)
18244 {
18245   dw_die_ref ptr_die
18246     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18247
18248   equate_type_number_to_die (type, ptr_die);
18249   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18250   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18251 }
18252
18253 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18254    Use modified_type_die instead.
18255    We keep this code here just in case these types of DIEs may be needed to
18256    represent certain things in other languages (e.g. Pascal) someday.  */
18257
18258 static void
18259 gen_reference_type_die (tree type, dw_die_ref context_die)
18260 {
18261   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18262
18263   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18264     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18265   else
18266     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18267
18268   equate_type_number_to_die (type, ref_die);
18269   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18270   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18271 }
18272 #endif
18273
18274 /* Generate a DIE for a pointer to a member type.  */
18275
18276 static void
18277 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18278 {
18279   dw_die_ref ptr_die
18280     = new_die (DW_TAG_ptr_to_member_type,
18281                scope_die_for (type, context_die), type);
18282
18283   equate_type_number_to_die (type, ptr_die);
18284   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18285                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18286   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18287 }
18288
18289 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18290 DEF_VEC_P(dchar_p);
18291 DEF_VEC_ALLOC_P(dchar_p,heap);
18292
18293 static char *producer_string;
18294
18295 /* Return a heap allocated producer string including command line options
18296    if -grecord-gcc-switches.  */
18297
18298 static char *
18299 gen_producer_string (void)
18300 {
18301   size_t j;
18302   VEC(dchar_p, heap) *switches = NULL;
18303   const char *language_string = lang_hooks.name;
18304   char *producer, *tail;
18305   const char *p;
18306   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18307   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18308
18309   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18310     switch (save_decoded_options[j].opt_index)
18311       {
18312       case OPT_o:
18313       case OPT_d:
18314       case OPT_dumpbase:
18315       case OPT_dumpdir:
18316       case OPT_auxbase:
18317       case OPT_auxbase_strip:
18318       case OPT_quiet:
18319       case OPT_version:
18320       case OPT_v:
18321       case OPT_w:
18322       case OPT_L:
18323       case OPT_D:
18324       case OPT_I:
18325       case OPT_U:
18326       case OPT_SPECIAL_unknown:
18327       case OPT_SPECIAL_ignore:
18328       case OPT_SPECIAL_program_name:
18329       case OPT_SPECIAL_input_file:
18330       case OPT_grecord_gcc_switches:
18331       case OPT_gno_record_gcc_switches:
18332       case OPT__output_pch_:
18333       case OPT_fdiagnostics_show_location_:
18334       case OPT_fdiagnostics_show_option:
18335       case OPT_fverbose_asm:
18336       case OPT____:
18337       case OPT__sysroot_:
18338       case OPT_nostdinc:
18339       case OPT_nostdinc__:
18340         /* Ignore these.  */
18341         continue;
18342       default:
18343         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18344                              == '-');
18345         switch (save_decoded_options[j].canonical_option[0][1])
18346           {
18347           case 'M':
18348           case 'i':
18349           case 'W':
18350             continue;
18351           case 'f':
18352             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18353                          "dump", 4) == 0)
18354               continue;
18355             break;
18356           default:
18357             break;
18358           }
18359         VEC_safe_push (dchar_p, heap, switches,
18360                        save_decoded_options[j].orig_option_with_args_text);
18361         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18362         break;
18363       }
18364
18365   producer = XNEWVEC (char, plen + 1 + len + 1);
18366   tail = producer;
18367   sprintf (tail, "%s %s", language_string, version_string);
18368   tail += plen;
18369
18370   if (!dwarf_record_gcc_switches)
18371     {
18372 #ifdef MIPS_DEBUGGING_INFO
18373       /* The MIPS/SGI compilers place the 'cc' command line options in the
18374          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18375          if they do not appear in the producer string, the debugger reaches
18376          the conclusion that the object file is stripped and has no debugging
18377          information.  To get the MIPS/SGI debugger to believe that there is
18378          debugging information in the object file, we add a -g to the producer
18379          string.  */
18380       if (debug_info_level > DINFO_LEVEL_TERSE)
18381         {
18382           memcpy (tail, " -g", 3);
18383           tail += 3;
18384         }
18385 #endif
18386     }
18387
18388   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18389     {
18390       len = strlen (p);
18391       *tail = ' ';
18392       memcpy (tail + 1, p, len);
18393       tail += len + 1;
18394     }
18395
18396   *tail = '\0';
18397   VEC_free (dchar_p, heap, switches);
18398   return producer;
18399 }
18400
18401 /* Generate the DIE for the compilation unit.  */
18402
18403 static dw_die_ref
18404 gen_compile_unit_die (const char *filename)
18405 {
18406   dw_die_ref die;
18407   const char *language_string = lang_hooks.name;
18408   int language;
18409
18410   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18411
18412   if (filename)
18413     {
18414       add_name_attribute (die, filename);
18415       /* Don't add cwd for <built-in>.  */
18416       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18417         add_comp_dir_attribute (die);
18418     }
18419
18420   if (producer_string == NULL)
18421     producer_string = gen_producer_string ();
18422   add_AT_string (die, DW_AT_producer, producer_string);
18423
18424   /* If our producer is LTO try to figure out a common language to use
18425      from the global list of translation units.  */
18426   if (strcmp (language_string, "GNU GIMPLE") == 0)
18427     {
18428       unsigned i;
18429       tree t;
18430       const char *common_lang = NULL;
18431
18432       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18433         {
18434           if (!TRANSLATION_UNIT_LANGUAGE (t))
18435             continue;
18436           if (!common_lang)
18437             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18438           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18439             ;
18440           else if (strncmp (common_lang, "GNU C", 5) == 0
18441                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18442             /* Mixing C and C++ is ok, use C++ in that case.  */
18443             common_lang = "GNU C++";
18444           else
18445             {
18446               /* Fall back to C.  */
18447               common_lang = NULL;
18448               break;
18449             }
18450         }
18451
18452       if (common_lang)
18453         language_string = common_lang;
18454     }
18455
18456   language = DW_LANG_C89;
18457   if (strcmp (language_string, "GNU C++") == 0)
18458     language = DW_LANG_C_plus_plus;
18459   else if (strcmp (language_string, "GNU F77") == 0)
18460     language = DW_LANG_Fortran77;
18461   else if (strcmp (language_string, "GNU Pascal") == 0)
18462     language = DW_LANG_Pascal83;
18463   else if (dwarf_version >= 3 || !dwarf_strict)
18464     {
18465       if (strcmp (language_string, "GNU Ada") == 0)
18466         language = DW_LANG_Ada95;
18467       else if (strcmp (language_string, "GNU Fortran") == 0)
18468         language = DW_LANG_Fortran95;
18469       else if (strcmp (language_string, "GNU Java") == 0)
18470         language = DW_LANG_Java;
18471       else if (strcmp (language_string, "GNU Objective-C") == 0)
18472         language = DW_LANG_ObjC;
18473       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18474         language = DW_LANG_ObjC_plus_plus;
18475       else if (dwarf_version >= 5 || !dwarf_strict)
18476         {
18477           if (strcmp (language_string, "GNU Go") == 0)
18478             language = DW_LANG_Go;
18479         }
18480     }
18481
18482   add_AT_unsigned (die, DW_AT_language, language);
18483
18484   switch (language)
18485     {
18486     case DW_LANG_Fortran77:
18487     case DW_LANG_Fortran90:
18488     case DW_LANG_Fortran95:
18489       /* Fortran has case insensitive identifiers and the front-end
18490          lowercases everything.  */
18491       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18492       break;
18493     default:
18494       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18495       break;
18496     }
18497   return die;
18498 }
18499
18500 /* Generate the DIE for a base class.  */
18501
18502 static void
18503 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18504 {
18505   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18506
18507   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18508   add_data_member_location_attribute (die, binfo);
18509
18510   if (BINFO_VIRTUAL_P (binfo))
18511     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18512
18513   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18514      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18515      the default has always been DW_ACCESS_private.  */
18516   if (access == access_public_node)
18517     {
18518       if (dwarf_version == 2
18519           || context_die->die_tag == DW_TAG_class_type)
18520       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18521     }
18522   else if (access == access_protected_node)
18523     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18524   else if (dwarf_version > 2
18525            && context_die->die_tag != DW_TAG_class_type)
18526     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18527 }
18528
18529 /* Generate a DIE for a class member.  */
18530
18531 static void
18532 gen_member_die (tree type, dw_die_ref context_die)
18533 {
18534   tree member;
18535   tree binfo = TYPE_BINFO (type);
18536   dw_die_ref child;
18537
18538   /* If this is not an incomplete type, output descriptions of each of its
18539      members. Note that as we output the DIEs necessary to represent the
18540      members of this record or union type, we will also be trying to output
18541      DIEs to represent the *types* of those members. However the `type'
18542      function (above) will specifically avoid generating type DIEs for member
18543      types *within* the list of member DIEs for this (containing) type except
18544      for those types (of members) which are explicitly marked as also being
18545      members of this (containing) type themselves.  The g++ front- end can
18546      force any given type to be treated as a member of some other (containing)
18547      type by setting the TYPE_CONTEXT of the given (member) type to point to
18548      the TREE node representing the appropriate (containing) type.  */
18549
18550   /* First output info about the base classes.  */
18551   if (binfo)
18552     {
18553       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18554       int i;
18555       tree base;
18556
18557       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18558         gen_inheritance_die (base,
18559                              (accesses ? VEC_index (tree, accesses, i)
18560                               : access_public_node), context_die);
18561     }
18562
18563   /* Now output info about the data members and type members.  */
18564   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18565     {
18566       /* If we thought we were generating minimal debug info for TYPE
18567          and then changed our minds, some of the member declarations
18568          may have already been defined.  Don't define them again, but
18569          do put them in the right order.  */
18570
18571       child = lookup_decl_die (member);
18572       if (child)
18573         splice_child_die (context_die, child);
18574       else
18575         gen_decl_die (member, NULL, context_die);
18576     }
18577
18578   /* Now output info about the function members (if any).  */
18579   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18580     {
18581       /* Don't include clones in the member list.  */
18582       if (DECL_ABSTRACT_ORIGIN (member))
18583         continue;
18584
18585       child = lookup_decl_die (member);
18586       if (child)
18587         splice_child_die (context_die, child);
18588       else
18589         gen_decl_die (member, NULL, context_die);
18590     }
18591 }
18592
18593 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18594    is set, we pretend that the type was never defined, so we only get the
18595    member DIEs needed by later specification DIEs.  */
18596
18597 static void
18598 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18599                                 enum debug_info_usage usage)
18600 {
18601   dw_die_ref type_die = lookup_type_die (type);
18602   dw_die_ref scope_die = 0;
18603   int nested = 0;
18604   int complete = (TYPE_SIZE (type)
18605                   && (! TYPE_STUB_DECL (type)
18606                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18607   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18608   complete = complete && should_emit_struct_debug (type, usage);
18609
18610   if (type_die && ! complete)
18611     return;
18612
18613   if (TYPE_CONTEXT (type) != NULL_TREE
18614       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18615           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18616     nested = 1;
18617
18618   scope_die = scope_die_for (type, context_die);
18619
18620   if (! type_die || (nested && is_cu_die (scope_die)))
18621     /* First occurrence of type or toplevel definition of nested class.  */
18622     {
18623       dw_die_ref old_die = type_die;
18624
18625       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18626                           ? record_type_tag (type) : DW_TAG_union_type,
18627                           scope_die, type);
18628       equate_type_number_to_die (type, type_die);
18629       if (old_die)
18630         add_AT_specification (type_die, old_die);
18631       else
18632         {
18633           add_name_attribute (type_die, type_tag (type));
18634           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18635           if (TYPE_ARTIFICIAL (type))
18636             add_AT_flag (type_die, DW_AT_artificial, 1);
18637         }
18638     }
18639   else
18640     remove_AT (type_die, DW_AT_declaration);
18641
18642   /* Generate child dies for template paramaters.  */
18643   if (debug_info_level > DINFO_LEVEL_TERSE
18644       && COMPLETE_TYPE_P (type))
18645     schedule_generic_params_dies_gen (type);
18646
18647   /* If this type has been completed, then give it a byte_size attribute and
18648      then give a list of members.  */
18649   if (complete && !ns_decl)
18650     {
18651       /* Prevent infinite recursion in cases where the type of some member of
18652          this type is expressed in terms of this type itself.  */
18653       TREE_ASM_WRITTEN (type) = 1;
18654       add_byte_size_attribute (type_die, type);
18655       if (TYPE_STUB_DECL (type) != NULL_TREE)
18656         {
18657           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18658           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18659         }
18660
18661       /* If the first reference to this type was as the return type of an
18662          inline function, then it may not have a parent.  Fix this now.  */
18663       if (type_die->die_parent == NULL)
18664         add_child_die (scope_die, type_die);
18665
18666       push_decl_scope (type);
18667       gen_member_die (type, type_die);
18668       pop_decl_scope ();
18669
18670       /* GNU extension: Record what type our vtable lives in.  */
18671       if (TYPE_VFIELD (type))
18672         {
18673           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18674
18675           gen_type_die (vtype, context_die);
18676           add_AT_die_ref (type_die, DW_AT_containing_type,
18677                           lookup_type_die (vtype));
18678         }
18679     }
18680   else
18681     {
18682       add_AT_flag (type_die, DW_AT_declaration, 1);
18683
18684       /* We don't need to do this for function-local types.  */
18685       if (TYPE_STUB_DECL (type)
18686           && ! decl_function_context (TYPE_STUB_DECL (type)))
18687         VEC_safe_push (tree, gc, incomplete_types, type);
18688     }
18689
18690   if (get_AT (type_die, DW_AT_name))
18691     add_pubtype (type, type_die);
18692 }
18693
18694 /* Generate a DIE for a subroutine _type_.  */
18695
18696 static void
18697 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18698 {
18699   tree return_type = TREE_TYPE (type);
18700   dw_die_ref subr_die
18701     = new_die (DW_TAG_subroutine_type,
18702                scope_die_for (type, context_die), type);
18703
18704   equate_type_number_to_die (type, subr_die);
18705   add_prototyped_attribute (subr_die, type);
18706   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18707   gen_formal_types_die (type, subr_die);
18708
18709   if (get_AT (subr_die, DW_AT_name))
18710     add_pubtype (type, subr_die);
18711 }
18712
18713 /* Generate a DIE for a type definition.  */
18714
18715 static void
18716 gen_typedef_die (tree decl, dw_die_ref context_die)
18717 {
18718   dw_die_ref type_die;
18719   tree origin;
18720
18721   if (TREE_ASM_WRITTEN (decl))
18722     return;
18723
18724   TREE_ASM_WRITTEN (decl) = 1;
18725   type_die = new_die (DW_TAG_typedef, context_die, decl);
18726   origin = decl_ultimate_origin (decl);
18727   if (origin != NULL)
18728     add_abstract_origin_attribute (type_die, origin);
18729   else
18730     {
18731       tree type;
18732
18733       add_name_and_src_coords_attributes (type_die, decl);
18734       if (DECL_ORIGINAL_TYPE (decl))
18735         {
18736           type = DECL_ORIGINAL_TYPE (decl);
18737
18738           gcc_assert (type != TREE_TYPE (decl));
18739           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18740         }
18741       else
18742         {
18743           type = TREE_TYPE (decl);
18744
18745           if (is_naming_typedef_decl (TYPE_NAME (type)))
18746             {
18747               /* Here, we are in the case of decl being a typedef naming
18748                  an anonymous type, e.g:
18749                      typedef struct {...} foo;
18750                  In that case TREE_TYPE (decl) is not a typedef variant
18751                  type and TYPE_NAME of the anonymous type is set to the
18752                  TYPE_DECL of the typedef. This construct is emitted by
18753                  the C++ FE.
18754
18755                  TYPE is the anonymous struct named by the typedef
18756                  DECL. As we need the DW_AT_type attribute of the
18757                  DW_TAG_typedef to point to the DIE of TYPE, let's
18758                  generate that DIE right away. add_type_attribute
18759                  called below will then pick (via lookup_type_die) that
18760                  anonymous struct DIE.  */
18761               if (!TREE_ASM_WRITTEN (type))
18762                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18763
18764               /* This is a GNU Extension.  We are adding a
18765                  DW_AT_linkage_name attribute to the DIE of the
18766                  anonymous struct TYPE.  The value of that attribute
18767                  is the name of the typedef decl naming the anonymous
18768                  struct.  This greatly eases the work of consumers of
18769                  this debug info.  */
18770               add_linkage_attr (lookup_type_die (type), decl);
18771             }
18772         }
18773
18774       add_type_attribute (type_die, type, TREE_READONLY (decl),
18775                           TREE_THIS_VOLATILE (decl), context_die);
18776
18777       if (is_naming_typedef_decl (decl))
18778         /* We want that all subsequent calls to lookup_type_die with
18779            TYPE in argument yield the DW_TAG_typedef we have just
18780            created.  */
18781         equate_type_number_to_die (type, type_die);
18782
18783       add_accessibility_attribute (type_die, decl);
18784     }
18785
18786   if (DECL_ABSTRACT (decl))
18787     equate_decl_number_to_die (decl, type_die);
18788
18789   if (get_AT (type_die, DW_AT_name))
18790     add_pubtype (decl, type_die);
18791 }
18792
18793 /* Generate a DIE for a struct, class, enum or union type.  */
18794
18795 static void
18796 gen_tagged_type_die (tree type,
18797                      dw_die_ref context_die,
18798                      enum debug_info_usage usage)
18799 {
18800   int need_pop;
18801
18802   if (type == NULL_TREE
18803       || !is_tagged_type (type))
18804     return;
18805
18806   /* If this is a nested type whose containing class hasn't been written
18807      out yet, writing it out will cover this one, too.  This does not apply
18808      to instantiations of member class templates; they need to be added to
18809      the containing class as they are generated.  FIXME: This hurts the
18810      idea of combining type decls from multiple TUs, since we can't predict
18811      what set of template instantiations we'll get.  */
18812   if (TYPE_CONTEXT (type)
18813       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18814       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18815     {
18816       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18817
18818       if (TREE_ASM_WRITTEN (type))
18819         return;
18820
18821       /* If that failed, attach ourselves to the stub.  */
18822       push_decl_scope (TYPE_CONTEXT (type));
18823       context_die = lookup_type_die (TYPE_CONTEXT (type));
18824       need_pop = 1;
18825     }
18826   else if (TYPE_CONTEXT (type) != NULL_TREE
18827            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18828     {
18829       /* If this type is local to a function that hasn't been written
18830          out yet, use a NULL context for now; it will be fixed up in
18831          decls_for_scope.  */
18832       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18833       /* A declaration DIE doesn't count; nested types need to go in the
18834          specification.  */
18835       if (context_die && is_declaration_die (context_die))
18836         context_die = NULL;
18837       need_pop = 0;
18838     }
18839   else
18840     {
18841       context_die = declare_in_namespace (type, context_die);
18842       need_pop = 0;
18843     }
18844
18845   if (TREE_CODE (type) == ENUMERAL_TYPE)
18846     {
18847       /* This might have been written out by the call to
18848          declare_in_namespace.  */
18849       if (!TREE_ASM_WRITTEN (type))
18850         gen_enumeration_type_die (type, context_die);
18851     }
18852   else
18853     gen_struct_or_union_type_die (type, context_die, usage);
18854
18855   if (need_pop)
18856     pop_decl_scope ();
18857
18858   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18859      it up if it is ever completed.  gen_*_type_die will set it for us
18860      when appropriate.  */
18861 }
18862
18863 /* Generate a type description DIE.  */
18864
18865 static void
18866 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18867                          enum debug_info_usage usage)
18868 {
18869   struct array_descr_info info;
18870
18871   if (type == NULL_TREE || type == error_mark_node)
18872     return;
18873
18874   if (TYPE_NAME (type) != NULL_TREE
18875       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18876       && is_redundant_typedef (TYPE_NAME (type))
18877       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18878     /* The DECL of this type is a typedef we don't want to emit debug
18879        info for but we want debug info for its underlying typedef.
18880        This can happen for e.g, the injected-class-name of a C++
18881        type.  */
18882     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18883
18884   /* If TYPE is a typedef type variant, let's generate debug info
18885      for the parent typedef which TYPE is a type of.  */
18886   if (typedef_variant_p (type))
18887     {
18888       if (TREE_ASM_WRITTEN (type))
18889         return;
18890
18891       /* Prevent broken recursion; we can't hand off to the same type.  */
18892       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18893
18894       /* Use the DIE of the containing namespace as the parent DIE of
18895          the type description DIE we want to generate.  */
18896       if (DECL_FILE_SCOPE_P (TYPE_NAME (type))
18897           || (DECL_CONTEXT (TYPE_NAME (type))
18898               && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL))
18899         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18900
18901       TREE_ASM_WRITTEN (type) = 1;
18902
18903       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18904       return;
18905     }
18906
18907   /* If type is an anonymous tagged type named by a typedef, let's
18908      generate debug info for the typedef.  */
18909   if (is_naming_typedef_decl (TYPE_NAME (type)))
18910     {
18911       /* Use the DIE of the containing namespace as the parent DIE of
18912          the type description DIE we want to generate.  */
18913       if (DECL_CONTEXT (TYPE_NAME (type))
18914           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18915         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18916       
18917       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18918       return;
18919     }
18920
18921   /* If this is an array type with hidden descriptor, handle it first.  */
18922   if (!TREE_ASM_WRITTEN (type)
18923       && lang_hooks.types.get_array_descr_info
18924       && lang_hooks.types.get_array_descr_info (type, &info)
18925       && (dwarf_version >= 3 || !dwarf_strict))
18926     {
18927       gen_descr_array_type_die (type, &info, context_die);
18928       TREE_ASM_WRITTEN (type) = 1;
18929       return;
18930     }
18931
18932   /* We are going to output a DIE to represent the unqualified version
18933      of this type (i.e. without any const or volatile qualifiers) so
18934      get the main variant (i.e. the unqualified version) of this type
18935      now.  (Vectors are special because the debugging info is in the
18936      cloned type itself).  */
18937   if (TREE_CODE (type) != VECTOR_TYPE)
18938     type = type_main_variant (type);
18939
18940   if (TREE_ASM_WRITTEN (type))
18941     return;
18942
18943   switch (TREE_CODE (type))
18944     {
18945     case ERROR_MARK:
18946       break;
18947
18948     case POINTER_TYPE:
18949     case REFERENCE_TYPE:
18950       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18951          ensures that the gen_type_die recursion will terminate even if the
18952          type is recursive.  Recursive types are possible in Ada.  */
18953       /* ??? We could perhaps do this for all types before the switch
18954          statement.  */
18955       TREE_ASM_WRITTEN (type) = 1;
18956
18957       /* For these types, all that is required is that we output a DIE (or a
18958          set of DIEs) to represent the "basis" type.  */
18959       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18960                                 DINFO_USAGE_IND_USE);
18961       break;
18962
18963     case OFFSET_TYPE:
18964       /* This code is used for C++ pointer-to-data-member types.
18965          Output a description of the relevant class type.  */
18966       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18967                                         DINFO_USAGE_IND_USE);
18968
18969       /* Output a description of the type of the object pointed to.  */
18970       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18971                                         DINFO_USAGE_IND_USE);
18972
18973       /* Now output a DIE to represent this pointer-to-data-member type
18974          itself.  */
18975       gen_ptr_to_mbr_type_die (type, context_die);
18976       break;
18977
18978     case FUNCTION_TYPE:
18979       /* Force out return type (in case it wasn't forced out already).  */
18980       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18981                                         DINFO_USAGE_DIR_USE);
18982       gen_subroutine_type_die (type, context_die);
18983       break;
18984
18985     case METHOD_TYPE:
18986       /* Force out return type (in case it wasn't forced out already).  */
18987       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18988                                         DINFO_USAGE_DIR_USE);
18989       gen_subroutine_type_die (type, context_die);
18990       break;
18991
18992     case ARRAY_TYPE:
18993       gen_array_type_die (type, context_die);
18994       break;
18995
18996     case VECTOR_TYPE:
18997       gen_array_type_die (type, context_die);
18998       break;
18999
19000     case ENUMERAL_TYPE:
19001     case RECORD_TYPE:
19002     case UNION_TYPE:
19003     case QUAL_UNION_TYPE:
19004       gen_tagged_type_die (type, context_die, usage);
19005       return;
19006
19007     case VOID_TYPE:
19008     case INTEGER_TYPE:
19009     case REAL_TYPE:
19010     case FIXED_POINT_TYPE:
19011     case COMPLEX_TYPE:
19012     case BOOLEAN_TYPE:
19013       /* No DIEs needed for fundamental types.  */
19014       break;
19015
19016     case NULLPTR_TYPE:
19017     case LANG_TYPE:
19018       /* Just use DW_TAG_unspecified_type.  */
19019       {
19020         dw_die_ref type_die = lookup_type_die (type);
19021         if (type_die == NULL)
19022           {
19023             tree name = TYPE_NAME (type);
19024             if (TREE_CODE (name) == TYPE_DECL)
19025               name = DECL_NAME (name);
19026             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19027             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19028             equate_type_number_to_die (type, type_die);
19029           }
19030       }
19031       break;
19032
19033     default:
19034       gcc_unreachable ();
19035     }
19036
19037   TREE_ASM_WRITTEN (type) = 1;
19038 }
19039
19040 static void
19041 gen_type_die (tree type, dw_die_ref context_die)
19042 {
19043   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19044 }
19045
19046 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19047    things which are local to the given block.  */
19048
19049 static void
19050 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19051 {
19052   int must_output_die = 0;
19053   bool inlined_func;
19054
19055   /* Ignore blocks that are NULL.  */
19056   if (stmt == NULL_TREE)
19057     return;
19058
19059   inlined_func = inlined_function_outer_scope_p (stmt);
19060
19061   /* If the block is one fragment of a non-contiguous block, do not
19062      process the variables, since they will have been done by the
19063      origin block.  Do process subblocks.  */
19064   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19065     {
19066       tree sub;
19067
19068       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19069         gen_block_die (sub, context_die, depth + 1);
19070
19071       return;
19072     }
19073
19074   /* Determine if we need to output any Dwarf DIEs at all to represent this
19075      block.  */
19076   if (inlined_func)
19077     /* The outer scopes for inlinings *must* always be represented.  We
19078        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19079     must_output_die = 1;
19080   else
19081     {
19082       /* Determine if this block directly contains any "significant"
19083          local declarations which we will need to output DIEs for.  */
19084       if (debug_info_level > DINFO_LEVEL_TERSE)
19085         /* We are not in terse mode so *any* local declaration counts
19086            as being a "significant" one.  */
19087         must_output_die = ((BLOCK_VARS (stmt) != NULL
19088                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19089                            && (TREE_USED (stmt)
19090                                || TREE_ASM_WRITTEN (stmt)
19091                                || BLOCK_ABSTRACT (stmt)));
19092       else if ((TREE_USED (stmt)
19093                 || TREE_ASM_WRITTEN (stmt)
19094                 || BLOCK_ABSTRACT (stmt))
19095                && !dwarf2out_ignore_block (stmt))
19096         must_output_die = 1;
19097     }
19098
19099   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19100      DIE for any block which contains no significant local declarations at
19101      all.  Rather, in such cases we just call `decls_for_scope' so that any
19102      needed Dwarf info for any sub-blocks will get properly generated. Note
19103      that in terse mode, our definition of what constitutes a "significant"
19104      local declaration gets restricted to include only inlined function
19105      instances and local (nested) function definitions.  */
19106   if (must_output_die)
19107     {
19108       if (inlined_func)
19109         {
19110           /* If STMT block is abstract, that means we have been called
19111              indirectly from dwarf2out_abstract_function.
19112              That function rightfully marks the descendent blocks (of
19113              the abstract function it is dealing with) as being abstract,
19114              precisely to prevent us from emitting any
19115              DW_TAG_inlined_subroutine DIE as a descendent
19116              of an abstract function instance. So in that case, we should
19117              not call gen_inlined_subroutine_die.
19118
19119              Later though, when cgraph asks dwarf2out to emit info
19120              for the concrete instance of the function decl into which
19121              the concrete instance of STMT got inlined, the later will lead
19122              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19123           if (! BLOCK_ABSTRACT (stmt))
19124             gen_inlined_subroutine_die (stmt, context_die, depth);
19125         }
19126       else
19127         gen_lexical_block_die (stmt, context_die, depth);
19128     }
19129   else
19130     decls_for_scope (stmt, context_die, depth);
19131 }
19132
19133 /* Process variable DECL (or variable with origin ORIGIN) within
19134    block STMT and add it to CONTEXT_DIE.  */
19135 static void
19136 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19137 {
19138   dw_die_ref die;
19139   tree decl_or_origin = decl ? decl : origin;
19140
19141   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19142     die = lookup_decl_die (decl_or_origin);
19143   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19144            && TYPE_DECL_IS_STUB (decl_or_origin))
19145     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19146   else
19147     die = NULL;
19148
19149   if (die != NULL && die->die_parent == NULL)
19150     add_child_die (context_die, die);
19151   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19152     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19153                                          stmt, context_die);
19154   else
19155     gen_decl_die (decl, origin, context_die);
19156 }
19157
19158 /* Generate all of the decls declared within a given scope and (recursively)
19159    all of its sub-blocks.  */
19160
19161 static void
19162 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19163 {
19164   tree decl;
19165   unsigned int i;
19166   tree subblocks;
19167
19168   /* Ignore NULL blocks.  */
19169   if (stmt == NULL_TREE)
19170     return;
19171
19172   /* Output the DIEs to represent all of the data objects and typedefs
19173      declared directly within this block but not within any nested
19174      sub-blocks.  Also, nested function and tag DIEs have been
19175      generated with a parent of NULL; fix that up now.  */
19176   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19177     process_scope_var (stmt, decl, NULL_TREE, context_die);
19178   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19179     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19180                        context_die);
19181
19182   /* If we're at -g1, we're not interested in subblocks.  */
19183   if (debug_info_level <= DINFO_LEVEL_TERSE)
19184     return;
19185
19186   /* Output the DIEs to represent all sub-blocks (and the items declared
19187      therein) of this block.  */
19188   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19189        subblocks != NULL;
19190        subblocks = BLOCK_CHAIN (subblocks))
19191     gen_block_die (subblocks, context_die, depth + 1);
19192 }
19193
19194 /* Is this a typedef we can avoid emitting?  */
19195
19196 static inline int
19197 is_redundant_typedef (const_tree decl)
19198 {
19199   if (TYPE_DECL_IS_STUB (decl))
19200     return 1;
19201
19202   if (DECL_ARTIFICIAL (decl)
19203       && DECL_CONTEXT (decl)
19204       && is_tagged_type (DECL_CONTEXT (decl))
19205       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19206       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19207     /* Also ignore the artificial member typedef for the class name.  */
19208     return 1;
19209
19210   return 0;
19211 }
19212
19213 /* Return TRUE if TYPE is a typedef that names a type for linkage
19214    purposes. This kind of typedefs is produced by the C++ FE for
19215    constructs like:
19216
19217    typedef struct {...} foo;
19218
19219    In that case, there is no typedef variant type produced for foo.
19220    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19221    struct type.  */
19222
19223 static bool
19224 is_naming_typedef_decl (const_tree decl)
19225 {
19226   if (decl == NULL_TREE
19227       || TREE_CODE (decl) != TYPE_DECL
19228       || !is_tagged_type (TREE_TYPE (decl))
19229       || DECL_IS_BUILTIN (decl)
19230       || is_redundant_typedef (decl)
19231       /* It looks like Ada produces TYPE_DECLs that are very similar
19232          to C++ naming typedefs but that have different
19233          semantics. Let's be specific to c++ for now.  */
19234       || !is_cxx ())
19235     return FALSE;
19236
19237   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19238           && TYPE_NAME (TREE_TYPE (decl)) == decl
19239           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19240               != TYPE_NAME (TREE_TYPE (decl))));
19241 }
19242
19243 /* Returns the DIE for a context.  */
19244
19245 static inline dw_die_ref
19246 get_context_die (tree context)
19247 {
19248   if (context)
19249     {
19250       /* Find die that represents this context.  */
19251       if (TYPE_P (context))
19252         {
19253           context = TYPE_MAIN_VARIANT (context);
19254           return strip_naming_typedef (context, force_type_die (context));
19255         }
19256       else
19257         return force_decl_die (context);
19258     }
19259   return comp_unit_die ();
19260 }
19261
19262 /* Returns the DIE for decl.  A DIE will always be returned.  */
19263
19264 static dw_die_ref
19265 force_decl_die (tree decl)
19266 {
19267   dw_die_ref decl_die;
19268   unsigned saved_external_flag;
19269   tree save_fn = NULL_TREE;
19270   decl_die = lookup_decl_die (decl);
19271   if (!decl_die)
19272     {
19273       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19274
19275       decl_die = lookup_decl_die (decl);
19276       if (decl_die)
19277         return decl_die;
19278
19279       switch (TREE_CODE (decl))
19280         {
19281         case FUNCTION_DECL:
19282           /* Clear current_function_decl, so that gen_subprogram_die thinks
19283              that this is a declaration. At this point, we just want to force
19284              declaration die.  */
19285           save_fn = current_function_decl;
19286           current_function_decl = NULL_TREE;
19287           gen_subprogram_die (decl, context_die);
19288           current_function_decl = save_fn;
19289           break;
19290
19291         case VAR_DECL:
19292           /* Set external flag to force declaration die. Restore it after
19293            gen_decl_die() call.  */
19294           saved_external_flag = DECL_EXTERNAL (decl);
19295           DECL_EXTERNAL (decl) = 1;
19296           gen_decl_die (decl, NULL, context_die);
19297           DECL_EXTERNAL (decl) = saved_external_flag;
19298           break;
19299
19300         case NAMESPACE_DECL:
19301           if (dwarf_version >= 3 || !dwarf_strict)
19302             dwarf2out_decl (decl);
19303           else
19304             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19305             decl_die = comp_unit_die ();
19306           break;
19307
19308         case TRANSLATION_UNIT_DECL:
19309           decl_die = comp_unit_die ();
19310           break;
19311
19312         default:
19313           gcc_unreachable ();
19314         }
19315
19316       /* We should be able to find the DIE now.  */
19317       if (!decl_die)
19318         decl_die = lookup_decl_die (decl);
19319       gcc_assert (decl_die);
19320     }
19321
19322   return decl_die;
19323 }
19324
19325 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19326    always returned.  */
19327
19328 static dw_die_ref
19329 force_type_die (tree type)
19330 {
19331   dw_die_ref type_die;
19332
19333   type_die = lookup_type_die (type);
19334   if (!type_die)
19335     {
19336       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19337
19338       type_die = modified_type_die (type, TYPE_READONLY (type),
19339                                     TYPE_VOLATILE (type), context_die);
19340       gcc_assert (type_die);
19341     }
19342   return type_die;
19343 }
19344
19345 /* Force out any required namespaces to be able to output DECL,
19346    and return the new context_die for it, if it's changed.  */
19347
19348 static dw_die_ref
19349 setup_namespace_context (tree thing, dw_die_ref context_die)
19350 {
19351   tree context = (DECL_P (thing)
19352                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19353   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19354     /* Force out the namespace.  */
19355     context_die = force_decl_die (context);
19356
19357   return context_die;
19358 }
19359
19360 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19361    type) within its namespace, if appropriate.
19362
19363    For compatibility with older debuggers, namespace DIEs only contain
19364    declarations; all definitions are emitted at CU scope.  */
19365
19366 static dw_die_ref
19367 declare_in_namespace (tree thing, dw_die_ref context_die)
19368 {
19369   dw_die_ref ns_context;
19370
19371   if (debug_info_level <= DINFO_LEVEL_TERSE)
19372     return context_die;
19373
19374   /* If this decl is from an inlined function, then don't try to emit it in its
19375      namespace, as we will get confused.  It would have already been emitted
19376      when the abstract instance of the inline function was emitted anyways.  */
19377   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19378     return context_die;
19379
19380   ns_context = setup_namespace_context (thing, context_die);
19381
19382   if (ns_context != context_die)
19383     {
19384       if (is_fortran ())
19385         return ns_context;
19386       if (DECL_P (thing))
19387         gen_decl_die (thing, NULL, ns_context);
19388       else
19389         gen_type_die (thing, ns_context);
19390     }
19391   return context_die;
19392 }
19393
19394 /* Generate a DIE for a namespace or namespace alias.  */
19395
19396 static void
19397 gen_namespace_die (tree decl, dw_die_ref context_die)
19398 {
19399   dw_die_ref namespace_die;
19400
19401   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19402      they are an alias of.  */
19403   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19404     {
19405       /* Output a real namespace or module.  */
19406       context_die = setup_namespace_context (decl, comp_unit_die ());
19407       namespace_die = new_die (is_fortran ()
19408                                ? DW_TAG_module : DW_TAG_namespace,
19409                                context_die, decl);
19410       /* For Fortran modules defined in different CU don't add src coords.  */
19411       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19412         {
19413           const char *name = dwarf2_name (decl, 0);
19414           if (name)
19415             add_name_attribute (namespace_die, name);
19416         }
19417       else
19418         add_name_and_src_coords_attributes (namespace_die, decl);
19419       if (DECL_EXTERNAL (decl))
19420         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19421       equate_decl_number_to_die (decl, namespace_die);
19422     }
19423   else
19424     {
19425       /* Output a namespace alias.  */
19426
19427       /* Force out the namespace we are an alias of, if necessary.  */
19428       dw_die_ref origin_die
19429         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19430
19431       if (DECL_FILE_SCOPE_P (decl)
19432           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19433         context_die = setup_namespace_context (decl, comp_unit_die ());
19434       /* Now create the namespace alias DIE.  */
19435       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19436       add_name_and_src_coords_attributes (namespace_die, decl);
19437       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19438       equate_decl_number_to_die (decl, namespace_die);
19439     }
19440 }
19441
19442 /* Generate Dwarf debug information for a decl described by DECL.
19443    The return value is currently only meaningful for PARM_DECLs,
19444    for all other decls it returns NULL.  */
19445
19446 static dw_die_ref
19447 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19448 {
19449   tree decl_or_origin = decl ? decl : origin;
19450   tree class_origin = NULL, ultimate_origin;
19451
19452   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19453     return NULL;
19454
19455   switch (TREE_CODE (decl_or_origin))
19456     {
19457     case ERROR_MARK:
19458       break;
19459
19460     case CONST_DECL:
19461       if (!is_fortran () && !is_ada ())
19462         {
19463           /* The individual enumerators of an enum type get output when we output
19464              the Dwarf representation of the relevant enum type itself.  */
19465           break;
19466         }
19467
19468       /* Emit its type.  */
19469       gen_type_die (TREE_TYPE (decl), context_die);
19470
19471       /* And its containing namespace.  */
19472       context_die = declare_in_namespace (decl, context_die);
19473
19474       gen_const_die (decl, context_die);
19475       break;
19476
19477     case FUNCTION_DECL:
19478       /* Don't output any DIEs to represent mere function declarations,
19479          unless they are class members or explicit block externs.  */
19480       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19481           && DECL_FILE_SCOPE_P (decl_or_origin)
19482           && (current_function_decl == NULL_TREE
19483               || DECL_ARTIFICIAL (decl_or_origin)))
19484         break;
19485
19486 #if 0
19487       /* FIXME */
19488       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19489          on local redeclarations of global functions.  That seems broken.  */
19490       if (current_function_decl != decl)
19491         /* This is only a declaration.  */;
19492 #endif
19493
19494       /* If we're emitting a clone, emit info for the abstract instance.  */
19495       if (origin || DECL_ORIGIN (decl) != decl)
19496         dwarf2out_abstract_function (origin
19497                                      ? DECL_ORIGIN (origin)
19498                                      : DECL_ABSTRACT_ORIGIN (decl));
19499
19500       /* If we're emitting an out-of-line copy of an inline function,
19501          emit info for the abstract instance and set up to refer to it.  */
19502       else if (cgraph_function_possibly_inlined_p (decl)
19503                && ! DECL_ABSTRACT (decl)
19504                && ! class_or_namespace_scope_p (context_die)
19505                /* dwarf2out_abstract_function won't emit a die if this is just
19506                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19507                   that case, because that works only if we have a die.  */
19508                && DECL_INITIAL (decl) != NULL_TREE)
19509         {
19510           dwarf2out_abstract_function (decl);
19511           set_decl_origin_self (decl);
19512         }
19513
19514       /* Otherwise we're emitting the primary DIE for this decl.  */
19515       else if (debug_info_level > DINFO_LEVEL_TERSE)
19516         {
19517           /* Before we describe the FUNCTION_DECL itself, make sure that we
19518              have its containing type.  */
19519           if (!origin)
19520             origin = decl_class_context (decl);
19521           if (origin != NULL_TREE)
19522             gen_type_die (origin, context_die);
19523
19524           /* And its return type.  */
19525           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19526
19527           /* And its virtual context.  */
19528           if (DECL_VINDEX (decl) != NULL_TREE)
19529             gen_type_die (DECL_CONTEXT (decl), context_die);
19530
19531           /* Make sure we have a member DIE for decl.  */
19532           if (origin != NULL_TREE)
19533             gen_type_die_for_member (origin, decl, context_die);
19534
19535           /* And its containing namespace.  */
19536           context_die = declare_in_namespace (decl, context_die);
19537         }
19538
19539       /* Now output a DIE to represent the function itself.  */
19540       if (decl)
19541         gen_subprogram_die (decl, context_die);
19542       break;
19543
19544     case TYPE_DECL:
19545       /* If we are in terse mode, don't generate any DIEs to represent any
19546          actual typedefs.  */
19547       if (debug_info_level <= DINFO_LEVEL_TERSE)
19548         break;
19549
19550       /* In the special case of a TYPE_DECL node representing the declaration
19551          of some type tag, if the given TYPE_DECL is marked as having been
19552          instantiated from some other (original) TYPE_DECL node (e.g. one which
19553          was generated within the original definition of an inline function) we
19554          used to generate a special (abbreviated) DW_TAG_structure_type,
19555          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19556          should be actually referencing those DIEs, as variable DIEs with that
19557          type would be emitted already in the abstract origin, so it was always
19558          removed during unused type prunning.  Don't add anything in this
19559          case.  */
19560       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19561         break;
19562
19563       if (is_redundant_typedef (decl))
19564         gen_type_die (TREE_TYPE (decl), context_die);
19565       else
19566         /* Output a DIE to represent the typedef itself.  */
19567         gen_typedef_die (decl, context_die);
19568       break;
19569
19570     case LABEL_DECL:
19571       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19572         gen_label_die (decl, context_die);
19573       break;
19574
19575     case VAR_DECL:
19576     case RESULT_DECL:
19577       /* If we are in terse mode, don't generate any DIEs to represent any
19578          variable declarations or definitions.  */
19579       if (debug_info_level <= DINFO_LEVEL_TERSE)
19580         break;
19581
19582       /* Output any DIEs that are needed to specify the type of this data
19583          object.  */
19584       if (decl_by_reference_p (decl_or_origin))
19585         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19586       else
19587         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19588
19589       /* And its containing type.  */
19590       class_origin = decl_class_context (decl_or_origin);
19591       if (class_origin != NULL_TREE)
19592         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19593
19594       /* And its containing namespace.  */
19595       context_die = declare_in_namespace (decl_or_origin, context_die);
19596
19597       /* Now output the DIE to represent the data object itself.  This gets
19598          complicated because of the possibility that the VAR_DECL really
19599          represents an inlined instance of a formal parameter for an inline
19600          function.  */
19601       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19602       if (ultimate_origin != NULL_TREE
19603           && TREE_CODE (ultimate_origin) == PARM_DECL)
19604         gen_formal_parameter_die (decl, origin,
19605                                   true /* Emit name attribute.  */,
19606                                   context_die);
19607       else
19608         gen_variable_die (decl, origin, context_die);
19609       break;
19610
19611     case FIELD_DECL:
19612       /* Ignore the nameless fields that are used to skip bits but handle C++
19613          anonymous unions and structs.  */
19614       if (DECL_NAME (decl) != NULL_TREE
19615           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19616           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19617         {
19618           gen_type_die (member_declared_type (decl), context_die);
19619           gen_field_die (decl, context_die);
19620         }
19621       break;
19622
19623     case PARM_DECL:
19624       if (DECL_BY_REFERENCE (decl_or_origin))
19625         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19626       else
19627         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19628       return gen_formal_parameter_die (decl, origin,
19629                                        true /* Emit name attribute.  */,
19630                                        context_die);
19631
19632     case NAMESPACE_DECL:
19633     case IMPORTED_DECL:
19634       if (dwarf_version >= 3 || !dwarf_strict)
19635         gen_namespace_die (decl, context_die);
19636       break;
19637
19638     default:
19639       /* Probably some frontend-internal decl.  Assume we don't care.  */
19640       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19641       break;
19642     }
19643
19644   return NULL;
19645 }
19646 \f
19647 /* Output debug information for global decl DECL.  Called from toplev.c after
19648    compilation proper has finished.  */
19649
19650 static void
19651 dwarf2out_global_decl (tree decl)
19652 {
19653   /* Output DWARF2 information for file-scope tentative data object
19654      declarations, file-scope (extern) function declarations (which
19655      had no corresponding body) and file-scope tagged type declarations
19656      and definitions which have not yet been forced out.  */
19657   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19658     dwarf2out_decl (decl);
19659 }
19660
19661 /* Output debug information for type decl DECL.  Called from toplev.c
19662    and from language front ends (to record built-in types).  */
19663 static void
19664 dwarf2out_type_decl (tree decl, int local)
19665 {
19666   if (!local)
19667     dwarf2out_decl (decl);
19668 }
19669
19670 /* Output debug information for imported module or decl DECL.
19671    NAME is non-NULL name in the lexical block if the decl has been renamed.
19672    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19673    that DECL belongs to.
19674    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19675 static void
19676 dwarf2out_imported_module_or_decl_1 (tree decl,
19677                                      tree name,
19678                                      tree lexical_block,
19679                                      dw_die_ref lexical_block_die)
19680 {
19681   expanded_location xloc;
19682   dw_die_ref imported_die = NULL;
19683   dw_die_ref at_import_die;
19684
19685   if (TREE_CODE (decl) == IMPORTED_DECL)
19686     {
19687       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19688       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19689       gcc_assert (decl);
19690     }
19691   else
19692     xloc = expand_location (input_location);
19693
19694   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19695     {
19696       at_import_die = force_type_die (TREE_TYPE (decl));
19697       /* For namespace N { typedef void T; } using N::T; base_type_die
19698          returns NULL, but DW_TAG_imported_declaration requires
19699          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19700       if (!at_import_die)
19701         {
19702           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19703           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19704           at_import_die = lookup_type_die (TREE_TYPE (decl));
19705           gcc_assert (at_import_die);
19706         }
19707     }
19708   else
19709     {
19710       at_import_die = lookup_decl_die (decl);
19711       if (!at_import_die)
19712         {
19713           /* If we're trying to avoid duplicate debug info, we may not have
19714              emitted the member decl for this field.  Emit it now.  */
19715           if (TREE_CODE (decl) == FIELD_DECL)
19716             {
19717               tree type = DECL_CONTEXT (decl);
19718
19719               if (TYPE_CONTEXT (type)
19720                   && TYPE_P (TYPE_CONTEXT (type))
19721                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19722                                                 DINFO_USAGE_DIR_USE))
19723                 return;
19724               gen_type_die_for_member (type, decl,
19725                                        get_context_die (TYPE_CONTEXT (type)));
19726             }
19727           at_import_die = force_decl_die (decl);
19728         }
19729     }
19730
19731   if (TREE_CODE (decl) == NAMESPACE_DECL)
19732     {
19733       if (dwarf_version >= 3 || !dwarf_strict)
19734         imported_die = new_die (DW_TAG_imported_module,
19735                                 lexical_block_die,
19736                                 lexical_block);
19737       else
19738         return;
19739     }
19740   else
19741     imported_die = new_die (DW_TAG_imported_declaration,
19742                             lexical_block_die,
19743                             lexical_block);
19744
19745   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19746   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19747   if (name)
19748     add_AT_string (imported_die, DW_AT_name,
19749                    IDENTIFIER_POINTER (name));
19750   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19751 }
19752
19753 /* Output debug information for imported module or decl DECL.
19754    NAME is non-NULL name in context if the decl has been renamed.
19755    CHILD is true if decl is one of the renamed decls as part of
19756    importing whole module.  */
19757
19758 static void
19759 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19760                                    bool child)
19761 {
19762   /* dw_die_ref at_import_die;  */
19763   dw_die_ref scope_die;
19764
19765   if (debug_info_level <= DINFO_LEVEL_TERSE)
19766     return;
19767
19768   gcc_assert (decl);
19769
19770   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19771      We need decl DIE for reference and scope die. First, get DIE for the decl
19772      itself.  */
19773
19774   /* Get the scope die for decl context. Use comp_unit_die for global module
19775      or decl. If die is not found for non globals, force new die.  */
19776   if (context
19777       && TYPE_P (context)
19778       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19779     return;
19780
19781   if (!(dwarf_version >= 3 || !dwarf_strict))
19782     return;
19783
19784   scope_die = get_context_die (context);
19785
19786   if (child)
19787     {
19788       gcc_assert (scope_die->die_child);
19789       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19790       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19791       scope_die = scope_die->die_child;
19792     }
19793
19794   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19795   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19796
19797 }
19798
19799 /* Write the debugging output for DECL.  */
19800
19801 void
19802 dwarf2out_decl (tree decl)
19803 {
19804   dw_die_ref context_die = comp_unit_die ();
19805
19806   switch (TREE_CODE (decl))
19807     {
19808     case ERROR_MARK:
19809       return;
19810
19811     case FUNCTION_DECL:
19812       /* What we would really like to do here is to filter out all mere
19813          file-scope declarations of file-scope functions which are never
19814          referenced later within this translation unit (and keep all of ones
19815          that *are* referenced later on) but we aren't clairvoyant, so we have
19816          no idea which functions will be referenced in the future (i.e. later
19817          on within the current translation unit). So here we just ignore all
19818          file-scope function declarations which are not also definitions.  If
19819          and when the debugger needs to know something about these functions,
19820          it will have to hunt around and find the DWARF information associated
19821          with the definition of the function.
19822
19823          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19824          nodes represent definitions and which ones represent mere
19825          declarations.  We have to check DECL_INITIAL instead. That's because
19826          the C front-end supports some weird semantics for "extern inline"
19827          function definitions.  These can get inlined within the current
19828          translation unit (and thus, we need to generate Dwarf info for their
19829          abstract instances so that the Dwarf info for the concrete inlined
19830          instances can have something to refer to) but the compiler never
19831          generates any out-of-lines instances of such things (despite the fact
19832          that they *are* definitions).
19833
19834          The important point is that the C front-end marks these "extern
19835          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19836          them anyway. Note that the C++ front-end also plays some similar games
19837          for inline function definitions appearing within include files which
19838          also contain `#pragma interface' pragmas.  */
19839       if (DECL_INITIAL (decl) == NULL_TREE)
19840         return;
19841
19842       /* If we're a nested function, initially use a parent of NULL; if we're
19843          a plain function, this will be fixed up in decls_for_scope.  If
19844          we're a method, it will be ignored, since we already have a DIE.  */
19845       if (decl_function_context (decl)
19846           /* But if we're in terse mode, we don't care about scope.  */
19847           && debug_info_level > DINFO_LEVEL_TERSE)
19848         context_die = NULL;
19849       break;
19850
19851     case VAR_DECL:
19852       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19853          declaration and if the declaration was never even referenced from
19854          within this entire compilation unit.  We suppress these DIEs in
19855          order to save space in the .debug section (by eliminating entries
19856          which are probably useless).  Note that we must not suppress
19857          block-local extern declarations (whether used or not) because that
19858          would screw-up the debugger's name lookup mechanism and cause it to
19859          miss things which really ought to be in scope at a given point.  */
19860       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19861         return;
19862
19863       /* For local statics lookup proper context die.  */
19864       if (TREE_STATIC (decl) && decl_function_context (decl))
19865         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19866
19867       /* If we are in terse mode, don't generate any DIEs to represent any
19868          variable declarations or definitions.  */
19869       if (debug_info_level <= DINFO_LEVEL_TERSE)
19870         return;
19871       break;
19872
19873     case CONST_DECL:
19874       if (debug_info_level <= DINFO_LEVEL_TERSE)
19875         return;
19876       if (!is_fortran () && !is_ada ())
19877         return;
19878       if (TREE_STATIC (decl) && decl_function_context (decl))
19879         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19880       break;
19881
19882     case NAMESPACE_DECL:
19883     case IMPORTED_DECL:
19884       if (debug_info_level <= DINFO_LEVEL_TERSE)
19885         return;
19886       if (lookup_decl_die (decl) != NULL)
19887         return;
19888       break;
19889
19890     case TYPE_DECL:
19891       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19892       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19893         return;
19894
19895       /* Don't bother trying to generate any DIEs to represent any of the
19896          normal built-in types for the language we are compiling.  */
19897       if (DECL_IS_BUILTIN (decl))
19898         return;
19899
19900       /* If we are in terse mode, don't generate any DIEs for types.  */
19901       if (debug_info_level <= DINFO_LEVEL_TERSE)
19902         return;
19903
19904       /* If we're a function-scope tag, initially use a parent of NULL;
19905          this will be fixed up in decls_for_scope.  */
19906       if (decl_function_context (decl))
19907         context_die = NULL;
19908
19909       break;
19910
19911     default:
19912       return;
19913     }
19914
19915   gen_decl_die (decl, NULL, context_die);
19916 }
19917
19918 /* Write the debugging output for DECL.  */
19919
19920 static void
19921 dwarf2out_function_decl (tree decl)
19922 {
19923   dwarf2out_decl (decl);
19924   call_arg_locations = NULL;
19925   call_arg_loc_last = NULL;
19926   call_site_count = -1;
19927   tail_call_site_count = -1;
19928   VEC_free (dw_die_ref, heap, block_map);
19929   htab_empty (decl_loc_table);
19930   htab_empty (cached_dw_loc_list_table);
19931 }
19932
19933 /* Output a marker (i.e. a label) for the beginning of the generated code for
19934    a lexical block.  */
19935
19936 static void
19937 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19938                        unsigned int blocknum)
19939 {
19940   switch_to_section (current_function_section ());
19941   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19942 }
19943
19944 /* Output a marker (i.e. a label) for the end of the generated code for a
19945    lexical block.  */
19946
19947 static void
19948 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19949 {
19950   switch_to_section (current_function_section ());
19951   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19952 }
19953
19954 /* Returns nonzero if it is appropriate not to emit any debugging
19955    information for BLOCK, because it doesn't contain any instructions.
19956
19957    Don't allow this for blocks with nested functions or local classes
19958    as we would end up with orphans, and in the presence of scheduling
19959    we may end up calling them anyway.  */
19960
19961 static bool
19962 dwarf2out_ignore_block (const_tree block)
19963 {
19964   tree decl;
19965   unsigned int i;
19966
19967   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19968     if (TREE_CODE (decl) == FUNCTION_DECL
19969         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19970       return 0;
19971   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19972     {
19973       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19974       if (TREE_CODE (decl) == FUNCTION_DECL
19975           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19976       return 0;
19977     }
19978
19979   return 1;
19980 }
19981
19982 /* Hash table routines for file_hash.  */
19983
19984 static int
19985 file_table_eq (const void *p1_p, const void *p2_p)
19986 {
19987   const struct dwarf_file_data *const p1 =
19988     (const struct dwarf_file_data *) p1_p;
19989   const char *const p2 = (const char *) p2_p;
19990   return filename_cmp (p1->filename, p2) == 0;
19991 }
19992
19993 static hashval_t
19994 file_table_hash (const void *p_p)
19995 {
19996   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19997   return htab_hash_string (p->filename);
19998 }
19999
20000 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20001    dwarf2out.c) and return its "index".  The index of each (known) filename is
20002    just a unique number which is associated with only that one filename.  We
20003    need such numbers for the sake of generating labels (in the .debug_sfnames
20004    section) and references to those files numbers (in the .debug_srcinfo
20005    and.debug_macinfo sections).  If the filename given as an argument is not
20006    found in our current list, add it to the list and assign it the next
20007    available unique index number.  In order to speed up searches, we remember
20008    the index of the filename was looked up last.  This handles the majority of
20009    all searches.  */
20010
20011 static struct dwarf_file_data *
20012 lookup_filename (const char *file_name)
20013 {
20014   void ** slot;
20015   struct dwarf_file_data * created;
20016
20017   /* Check to see if the file name that was searched on the previous
20018      call matches this file name.  If so, return the index.  */
20019   if (file_table_last_lookup
20020       && (file_name == file_table_last_lookup->filename
20021           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20022     return file_table_last_lookup;
20023
20024   /* Didn't match the previous lookup, search the table.  */
20025   slot = htab_find_slot_with_hash (file_table, file_name,
20026                                    htab_hash_string (file_name), INSERT);
20027   if (*slot)
20028     return (struct dwarf_file_data *) *slot;
20029
20030   created = ggc_alloc_dwarf_file_data ();
20031   created->filename = file_name;
20032   created->emitted_number = 0;
20033   *slot = created;
20034   return created;
20035 }
20036
20037 /* If the assembler will construct the file table, then translate the compiler
20038    internal file table number into the assembler file table number, and emit
20039    a .file directive if we haven't already emitted one yet.  The file table
20040    numbers are different because we prune debug info for unused variables and
20041    types, which may include filenames.  */
20042
20043 static int
20044 maybe_emit_file (struct dwarf_file_data * fd)
20045 {
20046   if (! fd->emitted_number)
20047     {
20048       if (last_emitted_file)
20049         fd->emitted_number = last_emitted_file->emitted_number + 1;
20050       else
20051         fd->emitted_number = 1;
20052       last_emitted_file = fd;
20053
20054       if (DWARF2_ASM_LINE_DEBUG_INFO)
20055         {
20056           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20057           output_quoted_string (asm_out_file,
20058                                 remap_debug_filename (fd->filename));
20059           fputc ('\n', asm_out_file);
20060         }
20061     }
20062
20063   return fd->emitted_number;
20064 }
20065
20066 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20067    That generation should happen after function debug info has been
20068    generated. The value of the attribute is the constant value of ARG.  */
20069
20070 static void
20071 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20072 {
20073   die_arg_entry entry;
20074
20075   if (!die || !arg)
20076     return;
20077
20078   if (!tmpl_value_parm_die_table)
20079     tmpl_value_parm_die_table
20080       = VEC_alloc (die_arg_entry, gc, 32);
20081
20082   entry.die = die;
20083   entry.arg = arg;
20084   VEC_safe_push (die_arg_entry, gc,
20085                  tmpl_value_parm_die_table,
20086                  &entry);
20087 }
20088
20089 /* Return TRUE if T is an instance of generic type, FALSE
20090    otherwise.  */
20091
20092 static bool
20093 generic_type_p (tree t)
20094 {
20095   if (t == NULL_TREE || !TYPE_P (t))
20096     return false;
20097   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20098 }
20099
20100 /* Schedule the generation of the generic parameter dies for the
20101   instance of generic type T. The proper generation itself is later
20102   done by gen_scheduled_generic_parms_dies. */
20103
20104 static void
20105 schedule_generic_params_dies_gen (tree t)
20106 {
20107   if (!generic_type_p (t))
20108     return;
20109
20110   if (generic_type_instances == NULL)
20111     generic_type_instances = VEC_alloc (tree, gc, 256);
20112
20113   VEC_safe_push (tree, gc, generic_type_instances, t);
20114 }
20115
20116 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20117    by append_entry_to_tmpl_value_parm_die_table. This function must
20118    be called after function DIEs have been generated.  */
20119
20120 static void
20121 gen_remaining_tmpl_value_param_die_attribute (void)
20122 {
20123   if (tmpl_value_parm_die_table)
20124     {
20125       unsigned i;
20126       die_arg_entry *e;
20127
20128       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20129         tree_add_const_value_attribute (e->die, e->arg);
20130     }
20131 }
20132
20133 /* Generate generic parameters DIEs for instances of generic types
20134    that have been previously scheduled by
20135    schedule_generic_params_dies_gen. This function must be called
20136    after all the types of the CU have been laid out.  */
20137
20138 static void
20139 gen_scheduled_generic_parms_dies (void)
20140 {
20141   unsigned i;
20142   tree t;
20143
20144   if (generic_type_instances == NULL)
20145     return;
20146   
20147   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20148     gen_generic_params_dies (t);
20149 }
20150
20151
20152 /* Replace DW_AT_name for the decl with name.  */
20153
20154 static void
20155 dwarf2out_set_name (tree decl, tree name)
20156 {
20157   dw_die_ref die;
20158   dw_attr_ref attr;
20159   const char *dname;
20160
20161   die = TYPE_SYMTAB_DIE (decl);
20162   if (!die)
20163     return;
20164
20165   dname = dwarf2_name (name, 0);
20166   if (!dname)
20167     return;
20168
20169   attr = get_AT (die, DW_AT_name);
20170   if (attr)
20171     {
20172       struct indirect_string_node *node;
20173
20174       node = find_AT_string (dname);
20175       /* replace the string.  */
20176       attr->dw_attr_val.v.val_str = node;
20177     }
20178
20179   else
20180     add_name_attribute (die, dname);
20181 }
20182
20183 /* Called by the final INSN scan whenever we see a var location.  We
20184    use it to drop labels in the right places, and throw the location in
20185    our lookup table.  */
20186
20187 static void
20188 dwarf2out_var_location (rtx loc_note)
20189 {
20190   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20191   struct var_loc_node *newloc;
20192   rtx next_real, next_note;
20193   static const char *last_label;
20194   static const char *last_postcall_label;
20195   static bool last_in_cold_section_p;
20196   static rtx expected_next_loc_note;
20197   tree decl;
20198   bool var_loc_p;
20199
20200   if (!NOTE_P (loc_note))
20201     {
20202       if (CALL_P (loc_note))
20203         {
20204           call_site_count++;
20205           if (SIBLING_CALL_P (loc_note))
20206             tail_call_site_count++;
20207         }
20208       return;
20209     }
20210
20211   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20212   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20213     return;
20214
20215   /* Optimize processing a large consecutive sequence of location
20216      notes so we don't spend too much time in next_real_insn.  If the
20217      next insn is another location note, remember the next_real_insn
20218      calculation for next time.  */
20219   next_real = cached_next_real_insn;
20220   if (next_real)
20221     {
20222       if (expected_next_loc_note != loc_note)
20223         next_real = NULL_RTX;
20224     }
20225
20226   next_note = NEXT_INSN (loc_note);
20227   if (! next_note
20228       || INSN_DELETED_P (next_note)
20229       || GET_CODE (next_note) != NOTE
20230       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20231           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20232     next_note = NULL_RTX;
20233
20234   if (! next_real)
20235     next_real = next_real_insn (loc_note);
20236
20237   if (next_note)
20238     {
20239       expected_next_loc_note = next_note;
20240       cached_next_real_insn = next_real;
20241     }
20242   else
20243     cached_next_real_insn = NULL_RTX;
20244
20245   /* If there are no instructions which would be affected by this note,
20246      don't do anything.  */
20247   if (var_loc_p
20248       && next_real == NULL_RTX
20249       && !NOTE_DURING_CALL_P (loc_note))
20250     return;
20251
20252   if (next_real == NULL_RTX)
20253     next_real = get_last_insn ();
20254
20255   /* If there were any real insns between note we processed last time
20256      and this note (or if it is the first note), clear
20257      last_{,postcall_}label so that they are not reused this time.  */
20258   if (last_var_location_insn == NULL_RTX
20259       || last_var_location_insn != next_real
20260       || last_in_cold_section_p != in_cold_section_p)
20261     {
20262       last_label = NULL;
20263       last_postcall_label = NULL;
20264     }
20265
20266   if (var_loc_p)
20267     {
20268       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20269       newloc = add_var_loc_to_decl (decl, loc_note,
20270                                     NOTE_DURING_CALL_P (loc_note)
20271                                     ? last_postcall_label : last_label);
20272       if (newloc == NULL)
20273         return;
20274     }
20275   else
20276     {
20277       decl = NULL_TREE;
20278       newloc = NULL;
20279     }
20280
20281   /* If there were no real insns between note we processed last time
20282      and this note, use the label we emitted last time.  Otherwise
20283      create a new label and emit it.  */
20284   if (last_label == NULL)
20285     {
20286       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20287       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20288       loclabel_num++;
20289       last_label = ggc_strdup (loclabel);
20290     }
20291
20292   if (!var_loc_p)
20293     {
20294       struct call_arg_loc_node *ca_loc
20295         = ggc_alloc_cleared_call_arg_loc_node ();
20296       rtx prev = prev_real_insn (loc_note), x;
20297       ca_loc->call_arg_loc_note = loc_note;
20298       ca_loc->next = NULL;
20299       ca_loc->label = last_label;
20300       gcc_assert (prev
20301                   && (CALL_P (prev)
20302                       || (NONJUMP_INSN_P (prev)
20303                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20304                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20305       if (!CALL_P (prev))
20306         prev = XVECEXP (PATTERN (prev), 0, 0);
20307       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20308       x = PATTERN (prev);
20309       if (GET_CODE (x) == PARALLEL)
20310         x = XVECEXP (x, 0, 0);
20311       if (GET_CODE (x) == SET)
20312         x = SET_SRC (x);
20313       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20314         {
20315           x = XEXP (XEXP (x, 0), 0);
20316           if (GET_CODE (x) == SYMBOL_REF
20317               && SYMBOL_REF_DECL (x)
20318               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20319             ca_loc->symbol_ref = x;
20320         }
20321       ca_loc->block = insn_scope (prev);
20322       if (call_arg_locations)
20323         call_arg_loc_last->next = ca_loc;
20324       else
20325         call_arg_locations = ca_loc;
20326       call_arg_loc_last = ca_loc;
20327     }
20328   else if (!NOTE_DURING_CALL_P (loc_note))
20329     newloc->label = last_label;
20330   else
20331     {
20332       if (!last_postcall_label)
20333         {
20334           sprintf (loclabel, "%s-1", last_label);
20335           last_postcall_label = ggc_strdup (loclabel);
20336         }
20337       newloc->label = last_postcall_label;
20338     }
20339
20340   last_var_location_insn = next_real;
20341   last_in_cold_section_p = in_cold_section_p;
20342 }
20343
20344 /* Note in one location list that text section has changed.  */
20345
20346 static int
20347 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20348 {
20349   var_loc_list *list = (var_loc_list *) *slot;
20350   if (list->first)
20351     list->last_before_switch
20352       = list->last->next ? list->last->next : list->last;
20353   return 1;
20354 }
20355
20356 /* Note in all location lists that text section has changed.  */
20357
20358 static void
20359 var_location_switch_text_section (void)
20360 {
20361   if (decl_loc_table == NULL)
20362     return;
20363
20364   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20365 }
20366
20367 /* Create a new line number table.  */
20368
20369 static dw_line_info_table *
20370 new_line_info_table (void)
20371 {
20372   dw_line_info_table *table;
20373
20374   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20375   table->file_num = 1;
20376   table->line_num = 1;
20377   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20378
20379   return table;
20380 }
20381
20382 /* Lookup the "current" table into which we emit line info, so
20383    that we don't have to do it for every source line.  */
20384
20385 static void
20386 set_cur_line_info_table (section *sec)
20387 {
20388   dw_line_info_table *table;
20389
20390   if (sec == text_section)
20391     table = text_section_line_info;
20392   else if (sec == cold_text_section)
20393     {
20394       table = cold_text_section_line_info;
20395       if (!table)
20396         {
20397           cold_text_section_line_info = table = new_line_info_table ();
20398           table->end_label = cold_end_label;
20399         }
20400     }
20401   else
20402     {
20403       const char *end_label;
20404
20405       if (flag_reorder_blocks_and_partition)
20406         {
20407           if (in_cold_section_p)
20408             end_label = crtl->subsections.cold_section_end_label;
20409           else
20410             end_label = crtl->subsections.hot_section_end_label;
20411         }
20412       else
20413         {
20414           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20415           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20416                                        current_function_funcdef_no);
20417           end_label = ggc_strdup (label);
20418         }
20419
20420       table = new_line_info_table ();
20421       table->end_label = end_label;
20422
20423       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20424     }
20425
20426   if (DWARF2_ASM_LINE_DEBUG_INFO)
20427     table->is_stmt = (cur_line_info_table
20428                       ? cur_line_info_table->is_stmt
20429                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20430   cur_line_info_table = table;
20431 }
20432
20433
20434 /* We need to reset the locations at the beginning of each
20435    function. We can't do this in the end_function hook, because the
20436    declarations that use the locations won't have been output when
20437    that hook is called.  Also compute have_multiple_function_sections here.  */
20438
20439 static void
20440 dwarf2out_begin_function (tree fun)
20441 {
20442   section *sec = function_section (fun);
20443
20444   if (sec != text_section)
20445     have_multiple_function_sections = true;
20446
20447   if (flag_reorder_blocks_and_partition && !cold_text_section)
20448     {
20449       gcc_assert (current_function_decl == fun);
20450       cold_text_section = unlikely_text_section ();
20451       switch_to_section (cold_text_section);
20452       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20453       switch_to_section (sec);
20454     }
20455
20456   dwarf2out_note_section_used ();
20457   call_site_count = 0;
20458   tail_call_site_count = 0;
20459
20460   set_cur_line_info_table (sec);
20461 }
20462
20463 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20464
20465 static void
20466 push_dw_line_info_entry (dw_line_info_table *table,
20467                          enum dw_line_info_opcode opcode, unsigned int val)
20468 {
20469   dw_line_info_entry e;
20470   e.opcode = opcode;
20471   e.val = val;
20472   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20473 }
20474
20475 /* Output a label to mark the beginning of a source code line entry
20476    and record information relating to this source line, in
20477    'line_info_table' for later output of the .debug_line section.  */
20478 /* ??? The discriminator parameter ought to be unsigned.  */
20479
20480 static void
20481 dwarf2out_source_line (unsigned int line, const char *filename,
20482                        int discriminator, bool is_stmt)
20483 {
20484   unsigned int file_num;
20485   dw_line_info_table *table;
20486
20487   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20488     return;
20489
20490   /* The discriminator column was added in dwarf4.  Simplify the below
20491      by simply removing it if we're not supposed to output it.  */
20492   if (dwarf_version < 4 && dwarf_strict)
20493     discriminator = 0;
20494
20495   table = cur_line_info_table;
20496   file_num = maybe_emit_file (lookup_filename (filename));
20497
20498   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20499      the debugger has used the second (possibly duplicate) line number
20500      at the beginning of the function to mark the end of the prologue.
20501      We could eliminate any other duplicates within the function.  For
20502      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20503      that second line number entry.  */
20504   /* Recall that this end-of-prologue indication is *not* the same thing
20505      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20506      to which the hook corresponds, follows the last insn that was 
20507      emitted by gen_prologue.  What we need is to preceed the first insn
20508      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20509      insn that corresponds to something the user wrote.  These may be
20510      very different locations once scheduling is enabled.  */
20511
20512   if (0 && file_num == table->file_num
20513       && line == table->line_num
20514       && discriminator == table->discrim_num
20515       && is_stmt == table->is_stmt)
20516     return;
20517
20518   switch_to_section (current_function_section ());
20519
20520   /* If requested, emit something human-readable.  */
20521   if (flag_debug_asm)
20522     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20523
20524   if (DWARF2_ASM_LINE_DEBUG_INFO)
20525     {
20526       /* Emit the .loc directive understood by GNU as.  */
20527       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20528          file_num, line, is_stmt, discriminator */
20529       fputs ("\t.loc ", asm_out_file);
20530       fprint_ul (asm_out_file, file_num);
20531       putc (' ', asm_out_file);
20532       fprint_ul (asm_out_file, line);
20533       putc (' ', asm_out_file);
20534       putc ('0', asm_out_file);
20535
20536       if (is_stmt != table->is_stmt)
20537         {
20538           fputs (" is_stmt ", asm_out_file);
20539           putc (is_stmt ? '1' : '0', asm_out_file);
20540         }
20541       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20542         {
20543           gcc_assert (discriminator > 0);
20544           fputs (" discriminator ", asm_out_file);
20545           fprint_ul (asm_out_file, (unsigned long) discriminator);
20546         }
20547       putc ('\n', asm_out_file);
20548     }
20549   else
20550     {
20551       unsigned int label_num = ++line_info_label_num;
20552
20553       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20554
20555       push_dw_line_info_entry (table, LI_set_address, label_num);
20556       if (file_num != table->file_num)
20557         push_dw_line_info_entry (table, LI_set_file, file_num);
20558       if (discriminator != table->discrim_num)
20559         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20560       if (is_stmt != table->is_stmt)
20561         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20562       push_dw_line_info_entry (table, LI_set_line, line);
20563     }
20564
20565   table->file_num = file_num;
20566   table->line_num = line;
20567   table->discrim_num = discriminator;
20568   table->is_stmt = is_stmt;
20569   table->in_use = true;
20570 }
20571
20572 /* Record the beginning of a new source file.  */
20573
20574 static void
20575 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20576 {
20577   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20578     {
20579       /* Record the beginning of the file for break_out_includes.  */
20580       dw_die_ref bincl_die;
20581
20582       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20583       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20584     }
20585
20586   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20587     {
20588       macinfo_entry e;
20589       e.code = DW_MACINFO_start_file;
20590       e.lineno = lineno;
20591       e.info = ggc_strdup (filename);
20592       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20593     }
20594 }
20595
20596 /* Record the end of a source file.  */
20597
20598 static void
20599 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20600 {
20601   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20602     /* Record the end of the file for break_out_includes.  */
20603     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20604
20605   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20606     {
20607       macinfo_entry e;
20608       e.code = DW_MACINFO_end_file;
20609       e.lineno = lineno;
20610       e.info = NULL;
20611       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20612     }
20613 }
20614
20615 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20616    the tail part of the directive line, i.e. the part which is past the
20617    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20618
20619 static void
20620 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20621                   const char *buffer ATTRIBUTE_UNUSED)
20622 {
20623   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20624     {
20625       macinfo_entry e;
20626       /* Insert a dummy first entry to be able to optimize the whole
20627          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20628       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20629         {
20630           e.code = 0;
20631           e.lineno = 0;
20632           e.info = NULL;
20633           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20634         }
20635       e.code = DW_MACINFO_define;
20636       e.lineno = lineno;
20637       e.info = ggc_strdup (buffer);
20638       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20639     }
20640 }
20641
20642 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20643    the tail part of the directive line, i.e. the part which is past the
20644    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20645
20646 static void
20647 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20648                  const char *buffer ATTRIBUTE_UNUSED)
20649 {
20650   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20651     {
20652       macinfo_entry e;
20653       /* Insert a dummy first entry to be able to optimize the whole
20654          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20655       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20656         {
20657           e.code = 0;
20658           e.lineno = 0;
20659           e.info = NULL;
20660           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20661         }
20662       e.code = DW_MACINFO_undef;
20663       e.lineno = lineno;
20664       e.info = ggc_strdup (buffer);
20665       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20666     }
20667 }
20668
20669 /* Routines to manipulate hash table of CUs.  */
20670
20671 static hashval_t
20672 htab_macinfo_hash (const void *of)
20673 {
20674   const macinfo_entry *const entry =
20675     (const macinfo_entry *) of;
20676
20677   return htab_hash_string (entry->info);
20678 }
20679
20680 static int
20681 htab_macinfo_eq (const void *of1, const void *of2)
20682 {
20683   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20684   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20685
20686   return !strcmp (entry1->info, entry2->info);
20687 }
20688
20689 /* Output a single .debug_macinfo entry.  */
20690
20691 static void
20692 output_macinfo_op (macinfo_entry *ref)
20693 {
20694   int file_num;
20695   size_t len;
20696   struct indirect_string_node *node;
20697   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20698   struct dwarf_file_data *fd;
20699
20700   switch (ref->code)
20701     {
20702     case DW_MACINFO_start_file:
20703       fd = lookup_filename (ref->info);
20704       file_num = maybe_emit_file (fd);
20705       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20706       dw2_asm_output_data_uleb128 (ref->lineno,
20707                                    "Included from line number %lu", 
20708                                    (unsigned long) ref->lineno);
20709       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20710       break;
20711     case DW_MACINFO_end_file:
20712       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20713       break;
20714     case DW_MACINFO_define:
20715     case DW_MACINFO_undef:
20716       len = strlen (ref->info) + 1;
20717       if (!dwarf_strict
20718           && len > DWARF_OFFSET_SIZE
20719           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20720           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20721         {
20722           ref->code = ref->code == DW_MACINFO_define
20723                       ? DW_MACRO_GNU_define_indirect
20724                       : DW_MACRO_GNU_undef_indirect;
20725           output_macinfo_op (ref);
20726           return;
20727         }
20728       dw2_asm_output_data (1, ref->code,
20729                            ref->code == DW_MACINFO_define
20730                            ? "Define macro" : "Undefine macro");
20731       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20732                                    (unsigned long) ref->lineno);
20733       dw2_asm_output_nstring (ref->info, -1, "The macro");
20734       break;
20735     case DW_MACRO_GNU_define_indirect:
20736     case DW_MACRO_GNU_undef_indirect:
20737       node = find_AT_string (ref->info);
20738       if (node->form != DW_FORM_strp)
20739         {
20740           char label[32];
20741           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20742           ++dw2_string_counter;
20743           node->label = xstrdup (label);
20744           node->form = DW_FORM_strp;
20745         }
20746       dw2_asm_output_data (1, ref->code,
20747                            ref->code == DW_MACRO_GNU_define_indirect
20748                            ? "Define macro indirect"
20749                            : "Undefine macro indirect");
20750       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20751                                    (unsigned long) ref->lineno);
20752       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20753                              debug_str_section, "The macro: \"%s\"",
20754                              ref->info);
20755       break;
20756     case DW_MACRO_GNU_transparent_include:
20757       dw2_asm_output_data (1, ref->code, "Transparent include");
20758       ASM_GENERATE_INTERNAL_LABEL (label,
20759                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20760       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20761       break;
20762     default:
20763       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20764                ASM_COMMENT_START, (unsigned long) ref->code);
20765       break;
20766     }
20767 }
20768
20769 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20770    other compilation unit .debug_macinfo sections.  IDX is the first
20771    index of a define/undef, return the number of ops that should be
20772    emitted in a comdat .debug_macinfo section and emit
20773    a DW_MACRO_GNU_transparent_include entry referencing it.
20774    If the define/undef entry should be emitted normally, return 0.  */
20775
20776 static unsigned
20777 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20778                         htab_t *macinfo_htab)
20779 {
20780   macinfo_entry *first, *second, *cur, *inc;
20781   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20782   unsigned char checksum[16];
20783   struct md5_ctx ctx;
20784   char *grp_name, *tail;
20785   const char *base;
20786   unsigned int i, count, encoded_filename_len, linebuf_len;
20787   void **slot;
20788
20789   first = VEC_index (macinfo_entry, macinfo_table, idx);
20790   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20791
20792   /* Optimize only if there are at least two consecutive define/undef ops,
20793      and either all of them are before first DW_MACINFO_start_file
20794      with lineno 0 (i.e. predefined macro block), or all of them are
20795      in some included header file.  */
20796   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20797     return 0;
20798   if (VEC_empty (macinfo_entry, files))
20799     {
20800       if (first->lineno != 0 || second->lineno != 0)
20801         return 0;
20802     }
20803   else if (first->lineno == 0)
20804     return 0;
20805
20806   /* Find the last define/undef entry that can be grouped together
20807      with first and at the same time compute md5 checksum of their
20808      codes, linenumbers and strings.  */
20809   md5_init_ctx (&ctx);
20810   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20811     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20812       break;
20813     else if (first->lineno == 0 && cur->lineno != 0)
20814       break;
20815     else
20816       {
20817         unsigned char code = cur->code;
20818         md5_process_bytes (&code, 1, &ctx);
20819         checksum_uleb128 (cur->lineno, &ctx);
20820         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20821       }
20822   md5_finish_ctx (&ctx, checksum);
20823   count = i - idx;
20824
20825   /* From the containing include filename (if any) pick up just
20826      usable characters from its basename.  */
20827   if (first->lineno == 0)
20828     base = "";
20829   else
20830     base = lbasename (VEC_last (macinfo_entry, files)->info);
20831   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20832     if (ISIDNUM (base[i]) || base[i] == '.')
20833       encoded_filename_len++;
20834   /* Count . at the end.  */
20835   if (encoded_filename_len)
20836     encoded_filename_len++;
20837
20838   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20839   linebuf_len = strlen (linebuf);
20840
20841   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20842   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20843                          + 16 * 2 + 1);
20844   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20845   tail = grp_name + 4;
20846   if (encoded_filename_len)
20847     {
20848       for (i = 0; base[i]; i++)
20849         if (ISIDNUM (base[i]) || base[i] == '.')
20850           *tail++ = base[i];
20851       *tail++ = '.';
20852     }
20853   memcpy (tail, linebuf, linebuf_len);
20854   tail += linebuf_len;
20855   *tail++ = '.';
20856   for (i = 0; i < 16; i++)
20857     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20858
20859   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20860      in the empty vector entry before the first define/undef.  */
20861   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20862   inc->code = DW_MACRO_GNU_transparent_include;
20863   inc->lineno = 0;
20864   inc->info = ggc_strdup (grp_name);
20865   if (*macinfo_htab == NULL)
20866     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20867   /* Avoid emitting duplicates.  */
20868   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20869   if (*slot != NULL)
20870     {
20871       inc->code = 0;
20872       inc->info = NULL;
20873       /* If such an entry has been used before, just emit
20874          a DW_MACRO_GNU_transparent_include op.  */
20875       inc = (macinfo_entry *) *slot;
20876       output_macinfo_op (inc);
20877       /* And clear all macinfo_entry in the range to avoid emitting them
20878          in the second pass.  */
20879       for (i = idx;
20880            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20881            && i < idx + count;
20882            i++)
20883         {
20884           cur->code = 0;
20885           cur->info = NULL;
20886         }
20887     }
20888   else
20889     {
20890       *slot = inc;
20891       inc->lineno = htab_elements (*macinfo_htab);
20892       output_macinfo_op (inc);
20893     }
20894   return count;
20895 }
20896
20897 /* Output macinfo section(s).  */
20898
20899 static void
20900 output_macinfo (void)
20901 {
20902   unsigned i;
20903   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20904   macinfo_entry *ref;
20905   VEC (macinfo_entry, gc) *files = NULL;
20906   htab_t macinfo_htab = NULL;
20907
20908   if (! length)
20909     return;
20910
20911   /* output_macinfo* uses these interchangeably.  */
20912   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20913               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20914               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20915               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20916
20917   /* For .debug_macro emit the section header.  */
20918   if (!dwarf_strict)
20919     {
20920       dw2_asm_output_data (2, 4, "DWARF macro version number");
20921       if (DWARF_OFFSET_SIZE == 8)
20922         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20923       else
20924         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20925       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20926                              debug_line_section, NULL);
20927     }
20928
20929   /* In the first loop, it emits the primary .debug_macinfo section
20930      and after each emitted op the macinfo_entry is cleared.
20931      If a longer range of define/undef ops can be optimized using
20932      DW_MACRO_GNU_transparent_include, the
20933      DW_MACRO_GNU_transparent_include op is emitted and kept in
20934      the vector before the first define/undef in the range and the
20935      whole range of define/undef ops is not emitted and kept.  */
20936   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20937     {
20938       switch (ref->code)
20939         {
20940         case DW_MACINFO_start_file:
20941           VEC_safe_push (macinfo_entry, gc, files, ref);
20942           break;
20943         case DW_MACINFO_end_file:
20944           if (!VEC_empty (macinfo_entry, files))
20945             VEC_pop (macinfo_entry, files);
20946           break;
20947         case DW_MACINFO_define:
20948         case DW_MACINFO_undef:
20949           if (!dwarf_strict
20950               && HAVE_COMDAT_GROUP
20951               && VEC_length (macinfo_entry, files) != 1
20952               && i > 0
20953               && i + 1 < length
20954               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20955             {
20956               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20957               if (count)
20958                 {
20959                   i += count - 1;
20960                   continue;
20961                 }
20962             }
20963           break;
20964         case 0:
20965           /* A dummy entry may be inserted at the beginning to be able
20966              to optimize the whole block of predefined macros.  */
20967           if (i == 0)
20968             continue;
20969         default:
20970           break;
20971         }
20972       output_macinfo_op (ref);
20973       ref->info = NULL;
20974       ref->code = 0;
20975     }
20976
20977   if (macinfo_htab == NULL)
20978     return;
20979
20980   htab_delete (macinfo_htab);
20981
20982   /* If any DW_MACRO_GNU_transparent_include were used, on those
20983      DW_MACRO_GNU_transparent_include entries terminate the
20984      current chain and switch to a new comdat .debug_macinfo
20985      section and emit the define/undef entries within it.  */
20986   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20987     switch (ref->code)
20988       {
20989       case 0:
20990         continue;
20991       case DW_MACRO_GNU_transparent_include:
20992         {
20993           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20994           tree comdat_key = get_identifier (ref->info);
20995           /* Terminate the previous .debug_macinfo section.  */
20996           dw2_asm_output_data (1, 0, "End compilation unit");
20997           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20998                                          SECTION_DEBUG
20999                                          | SECTION_LINKONCE,
21000                                          comdat_key);
21001           ASM_GENERATE_INTERNAL_LABEL (label,
21002                                        DEBUG_MACRO_SECTION_LABEL,
21003                                        ref->lineno);
21004           ASM_OUTPUT_LABEL (asm_out_file, label);
21005           ref->code = 0;
21006           ref->info = NULL;
21007           dw2_asm_output_data (2, 4, "DWARF macro version number");
21008           if (DWARF_OFFSET_SIZE == 8)
21009             dw2_asm_output_data (1, 1, "Flags: 64-bit");
21010           else
21011             dw2_asm_output_data (1, 0, "Flags: 32-bit");
21012         }
21013         break;
21014       case DW_MACINFO_define:
21015       case DW_MACINFO_undef:
21016         output_macinfo_op (ref);
21017         ref->code = 0;
21018         ref->info = NULL;
21019         break;
21020       default:
21021         gcc_unreachable ();
21022       }
21023 }
21024
21025 /* Set up for Dwarf output at the start of compilation.  */
21026
21027 static void
21028 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21029 {
21030   /* Allocate the file_table.  */
21031   file_table = htab_create_ggc (50, file_table_hash,
21032                                 file_table_eq, NULL);
21033
21034   /* Allocate the decl_die_table.  */
21035   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21036                                     decl_die_table_eq, NULL);
21037
21038   /* Allocate the decl_loc_table.  */
21039   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21040                                     decl_loc_table_eq, NULL);
21041
21042   /* Allocate the cached_dw_loc_list_table.  */
21043   cached_dw_loc_list_table
21044     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21045                        cached_dw_loc_list_table_eq, NULL);
21046
21047   /* Allocate the initial hunk of the decl_scope_table.  */
21048   decl_scope_table = VEC_alloc (tree, gc, 256);
21049
21050   /* Allocate the initial hunk of the abbrev_die_table.  */
21051   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21052     (ABBREV_DIE_TABLE_INCREMENT);
21053   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21054   /* Zero-th entry is allocated, but unused.  */
21055   abbrev_die_table_in_use = 1;
21056
21057   /* Allocate the pubtypes and pubnames vectors.  */
21058   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21059   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21060
21061   incomplete_types = VEC_alloc (tree, gc, 64);
21062
21063   used_rtx_array = VEC_alloc (rtx, gc, 32);
21064
21065   debug_info_section = get_section (DEBUG_INFO_SECTION,
21066                                     SECTION_DEBUG, NULL);
21067   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21068                                       SECTION_DEBUG, NULL);
21069   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21070                                        SECTION_DEBUG, NULL);
21071   debug_macinfo_section = get_section (dwarf_strict
21072                                        ? DEBUG_MACINFO_SECTION
21073                                        : DEBUG_MACRO_SECTION,
21074                                        SECTION_DEBUG, NULL);
21075   debug_line_section = get_section (DEBUG_LINE_SECTION,
21076                                     SECTION_DEBUG, NULL);
21077   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21078                                    SECTION_DEBUG, NULL);
21079   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21080                                         SECTION_DEBUG, NULL);
21081   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21082                                         SECTION_DEBUG, NULL);
21083   debug_str_section = get_section (DEBUG_STR_SECTION,
21084                                    DEBUG_STR_SECTION_FLAGS, NULL);
21085   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21086                                       SECTION_DEBUG, NULL);
21087   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21088                                      SECTION_DEBUG, NULL);
21089
21090   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21091   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21092                                DEBUG_ABBREV_SECTION_LABEL, 0);
21093   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21094   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21095                                COLD_TEXT_SECTION_LABEL, 0);
21096   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21097
21098   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21099                                DEBUG_INFO_SECTION_LABEL, 0);
21100   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21101                                DEBUG_LINE_SECTION_LABEL, 0);
21102   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21103                                DEBUG_RANGES_SECTION_LABEL, 0);
21104   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21105                                dwarf_strict
21106                                ? DEBUG_MACINFO_SECTION_LABEL
21107                                : DEBUG_MACRO_SECTION_LABEL, 0);
21108
21109   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21110     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21111
21112   switch_to_section (text_section);
21113   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21114
21115   /* Make sure the line number table for .text always exists.  */
21116   text_section_line_info = new_line_info_table ();
21117   text_section_line_info->end_label = text_end_label;
21118 }
21119
21120 /* Called before cgraph_optimize starts outputtting functions, variables
21121    and toplevel asms into assembly.  */
21122
21123 static void
21124 dwarf2out_assembly_start (void)
21125 {
21126   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21127       && dwarf2out_do_cfi_asm ()
21128       && (!(flag_unwind_tables || flag_exceptions)
21129           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21130     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21131 }
21132
21133 /* A helper function for dwarf2out_finish called through
21134    htab_traverse.  Emit one queued .debug_str string.  */
21135
21136 static int
21137 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21138 {
21139   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21140
21141   if (node->form == DW_FORM_strp)
21142     {
21143       switch_to_section (debug_str_section);
21144       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21145       assemble_string (node->str, strlen (node->str) + 1);
21146     }
21147
21148   return 1;
21149 }
21150
21151 #if ENABLE_ASSERT_CHECKING
21152 /* Verify that all marks are clear.  */
21153
21154 static void
21155 verify_marks_clear (dw_die_ref die)
21156 {
21157   dw_die_ref c;
21158
21159   gcc_assert (! die->die_mark);
21160   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21161 }
21162 #endif /* ENABLE_ASSERT_CHECKING */
21163
21164 /* Clear the marks for a die and its children.
21165    Be cool if the mark isn't set.  */
21166
21167 static void
21168 prune_unmark_dies (dw_die_ref die)
21169 {
21170   dw_die_ref c;
21171
21172   if (die->die_mark)
21173     die->die_mark = 0;
21174   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21175 }
21176
21177 /* Given DIE that we're marking as used, find any other dies
21178    it references as attributes and mark them as used.  */
21179
21180 static void
21181 prune_unused_types_walk_attribs (dw_die_ref die)
21182 {
21183   dw_attr_ref a;
21184   unsigned ix;
21185
21186   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21187     {
21188       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21189         {
21190           /* A reference to another DIE.
21191              Make sure that it will get emitted.
21192              If it was broken out into a comdat group, don't follow it.  */
21193           if (! use_debug_types
21194               || a->dw_attr == DW_AT_specification
21195               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21196             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21197         }
21198       /* Set the string's refcount to 0 so that prune_unused_types_mark
21199          accounts properly for it.  */
21200       if (AT_class (a) == dw_val_class_str)
21201         a->dw_attr_val.v.val_str->refcount = 0;
21202     }
21203 }
21204
21205 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21206
21207 static void
21208 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21209 {
21210   dw_die_ref c;
21211
21212   if (die == NULL || die->die_child == NULL)
21213     return;
21214   c = die->die_child;
21215   do
21216     {
21217       switch (c->die_tag)
21218         {
21219         case DW_TAG_template_type_param:
21220         case DW_TAG_template_value_param:
21221         case DW_TAG_GNU_template_template_param:
21222         case DW_TAG_GNU_template_parameter_pack:
21223           prune_unused_types_mark (c, 1);
21224           break;
21225         default:
21226           break;
21227         }
21228       c = c->die_sib;
21229     } while (c && c != die->die_child);
21230 }
21231
21232 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21233    to DIE's children.  */
21234
21235 static void
21236 prune_unused_types_mark (dw_die_ref die, int dokids)
21237 {
21238   dw_die_ref c;
21239
21240   if (die->die_mark == 0)
21241     {
21242       /* We haven't done this node yet.  Mark it as used.  */
21243       die->die_mark = 1;
21244       /* If this is the DIE of a generic type instantiation,
21245          mark the children DIEs that describe its generic parms and
21246          args.  */
21247       prune_unused_types_mark_generic_parms_dies (die);
21248
21249       /* We also have to mark its parents as used.
21250          (But we don't want to mark our parents' kids due to this.)  */
21251       if (die->die_parent)
21252         prune_unused_types_mark (die->die_parent, 0);
21253
21254       /* Mark any referenced nodes.  */
21255       prune_unused_types_walk_attribs (die);
21256
21257       /* If this node is a specification,
21258          also mark the definition, if it exists.  */
21259       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21260         prune_unused_types_mark (die->die_definition, 1);
21261     }
21262
21263   if (dokids && die->die_mark != 2)
21264     {
21265       /* We need to walk the children, but haven't done so yet.
21266          Remember that we've walked the kids.  */
21267       die->die_mark = 2;
21268
21269       /* If this is an array type, we need to make sure our
21270          kids get marked, even if they're types.  If we're
21271          breaking out types into comdat sections, do this
21272          for all type definitions.  */
21273       if (die->die_tag == DW_TAG_array_type
21274           || (use_debug_types
21275               && is_type_die (die) && ! is_declaration_die (die)))
21276         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21277       else
21278         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21279     }
21280 }
21281
21282 /* For local classes, look if any static member functions were emitted
21283    and if so, mark them.  */
21284
21285 static void
21286 prune_unused_types_walk_local_classes (dw_die_ref die)
21287 {
21288   dw_die_ref c;
21289
21290   if (die->die_mark == 2)
21291     return;
21292
21293   switch (die->die_tag)
21294     {
21295     case DW_TAG_structure_type:
21296     case DW_TAG_union_type:
21297     case DW_TAG_class_type:
21298       break;
21299
21300     case DW_TAG_subprogram:
21301       if (!get_AT_flag (die, DW_AT_declaration)
21302           || die->die_definition != NULL)
21303         prune_unused_types_mark (die, 1);
21304       return;
21305
21306     default:
21307       return;
21308     }
21309
21310   /* Mark children.  */
21311   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21312 }
21313
21314 /* Walk the tree DIE and mark types that we actually use.  */
21315
21316 static void
21317 prune_unused_types_walk (dw_die_ref die)
21318 {
21319   dw_die_ref c;
21320
21321   /* Don't do anything if this node is already marked and
21322      children have been marked as well.  */
21323   if (die->die_mark == 2)
21324     return;
21325
21326   switch (die->die_tag)
21327     {
21328     case DW_TAG_structure_type:
21329     case DW_TAG_union_type:
21330     case DW_TAG_class_type:
21331       if (die->die_perennial_p)
21332         break;
21333
21334       for (c = die->die_parent; c; c = c->die_parent)
21335         if (c->die_tag == DW_TAG_subprogram)
21336           break;
21337
21338       /* Finding used static member functions inside of classes
21339          is needed just for local classes, because for other classes
21340          static member function DIEs with DW_AT_specification
21341          are emitted outside of the DW_TAG_*_type.  If we ever change
21342          it, we'd need to call this even for non-local classes.  */
21343       if (c)
21344         prune_unused_types_walk_local_classes (die);
21345
21346       /* It's a type node --- don't mark it.  */
21347       return;
21348
21349     case DW_TAG_const_type:
21350     case DW_TAG_packed_type:
21351     case DW_TAG_pointer_type:
21352     case DW_TAG_reference_type:
21353     case DW_TAG_rvalue_reference_type:
21354     case DW_TAG_volatile_type:
21355     case DW_TAG_typedef:
21356     case DW_TAG_array_type:
21357     case DW_TAG_interface_type:
21358     case DW_TAG_friend:
21359     case DW_TAG_variant_part:
21360     case DW_TAG_enumeration_type:
21361     case DW_TAG_subroutine_type:
21362     case DW_TAG_string_type:
21363     case DW_TAG_set_type:
21364     case DW_TAG_subrange_type:
21365     case DW_TAG_ptr_to_member_type:
21366     case DW_TAG_file_type:
21367       if (die->die_perennial_p)
21368         break;
21369
21370       /* It's a type node --- don't mark it.  */
21371       return;
21372
21373     default:
21374       /* Mark everything else.  */
21375       break;
21376   }
21377
21378   if (die->die_mark == 0)
21379     {
21380       die->die_mark = 1;
21381
21382       /* Now, mark any dies referenced from here.  */
21383       prune_unused_types_walk_attribs (die);
21384     }
21385
21386   die->die_mark = 2;
21387
21388   /* Mark children.  */
21389   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21390 }
21391
21392 /* Increment the string counts on strings referred to from DIE's
21393    attributes.  */
21394
21395 static void
21396 prune_unused_types_update_strings (dw_die_ref die)
21397 {
21398   dw_attr_ref a;
21399   unsigned ix;
21400
21401   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21402     if (AT_class (a) == dw_val_class_str)
21403       {
21404         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21405         s->refcount++;
21406         /* Avoid unnecessarily putting strings that are used less than
21407            twice in the hash table.  */
21408         if (s->refcount
21409             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21410           {
21411             void ** slot;
21412             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21413                                              htab_hash_string (s->str),
21414                                              INSERT);
21415             gcc_assert (*slot == NULL);
21416             *slot = s;
21417           }
21418       }
21419 }
21420
21421 /* Remove from the tree DIE any dies that aren't marked.  */
21422
21423 static void
21424 prune_unused_types_prune (dw_die_ref die)
21425 {
21426   dw_die_ref c;
21427
21428   gcc_assert (die->die_mark);
21429   prune_unused_types_update_strings (die);
21430
21431   if (! die->die_child)
21432     return;
21433
21434   c = die->die_child;
21435   do {
21436     dw_die_ref prev = c;
21437     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21438       if (c == die->die_child)
21439         {
21440           /* No marked children between 'prev' and the end of the list.  */
21441           if (prev == c)
21442             /* No marked children at all.  */
21443             die->die_child = NULL;
21444           else
21445             {
21446               prev->die_sib = c->die_sib;
21447               die->die_child = prev;
21448             }
21449           return;
21450         }
21451
21452     if (c != prev->die_sib)
21453       prev->die_sib = c;
21454     prune_unused_types_prune (c);
21455   } while (c != die->die_child);
21456 }
21457
21458 /* Remove dies representing declarations that we never use.  */
21459
21460 static void
21461 prune_unused_types (void)
21462 {
21463   unsigned int i;
21464   limbo_die_node *node;
21465   comdat_type_node *ctnode;
21466   pubname_ref pub;
21467   dw_die_ref base_type;
21468
21469 #if ENABLE_ASSERT_CHECKING
21470   /* All the marks should already be clear.  */
21471   verify_marks_clear (comp_unit_die ());
21472   for (node = limbo_die_list; node; node = node->next)
21473     verify_marks_clear (node->die);
21474   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21475     verify_marks_clear (ctnode->root_die);
21476 #endif /* ENABLE_ASSERT_CHECKING */
21477
21478   /* Mark types that are used in global variables.  */
21479   premark_types_used_by_global_vars ();
21480
21481   /* Set the mark on nodes that are actually used.  */
21482   prune_unused_types_walk (comp_unit_die ());
21483   for (node = limbo_die_list; node; node = node->next)
21484     prune_unused_types_walk (node->die);
21485   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21486     {
21487       prune_unused_types_walk (ctnode->root_die);
21488       prune_unused_types_mark (ctnode->type_die, 1);
21489     }
21490
21491   /* Also set the mark on nodes referenced from the
21492      pubname_table.  */
21493   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21494     prune_unused_types_mark (pub->die, 1);
21495   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21496     prune_unused_types_mark (base_type, 1);
21497
21498   if (debug_str_hash)
21499     htab_empty (debug_str_hash);
21500   prune_unused_types_prune (comp_unit_die ());
21501   for (node = limbo_die_list; node; node = node->next)
21502     prune_unused_types_prune (node->die);
21503   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21504     prune_unused_types_prune (ctnode->root_die);
21505
21506   /* Leave the marks clear.  */
21507   prune_unmark_dies (comp_unit_die ());
21508   for (node = limbo_die_list; node; node = node->next)
21509     prune_unmark_dies (node->die);
21510   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21511     prune_unmark_dies (ctnode->root_die);
21512 }
21513
21514 /* Set the parameter to true if there are any relative pathnames in
21515    the file table.  */
21516 static int
21517 file_table_relative_p (void ** slot, void *param)
21518 {
21519   bool *p = (bool *) param;
21520   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21521   if (!IS_ABSOLUTE_PATH (d->filename))
21522     {
21523       *p = true;
21524       return 0;
21525     }
21526   return 1;
21527 }
21528
21529 /* Routines to manipulate hash table of comdat type units.  */
21530
21531 static hashval_t
21532 htab_ct_hash (const void *of)
21533 {
21534   hashval_t h;
21535   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21536
21537   memcpy (&h, type_node->signature, sizeof (h));
21538   return h;
21539 }
21540
21541 static int
21542 htab_ct_eq (const void *of1, const void *of2)
21543 {
21544   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21545   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21546
21547   return (! memcmp (type_node_1->signature, type_node_2->signature,
21548                     DWARF_TYPE_SIGNATURE_SIZE));
21549 }
21550
21551 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21552    to the location it would have been added, should we know its
21553    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21554    probably improve compactness of debug info, removing equivalent
21555    abbrevs, and hide any differences caused by deferring the
21556    computation of the assembler name, triggered by e.g. PCH.  */
21557
21558 static inline void
21559 move_linkage_attr (dw_die_ref die)
21560 {
21561   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21562   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21563
21564   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21565               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21566
21567   while (--ix > 0)
21568     {
21569       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21570
21571       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21572         break;
21573     }
21574
21575   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21576     {
21577       VEC_pop (dw_attr_node, die->die_attr);
21578       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21579     }
21580 }
21581
21582 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21583    referenced from typed stack ops and count how often they are used.  */
21584
21585 static void
21586 mark_base_types (dw_loc_descr_ref loc)
21587 {
21588   dw_die_ref base_type = NULL;
21589
21590   for (; loc; loc = loc->dw_loc_next)
21591     {
21592       switch (loc->dw_loc_opc)
21593         {
21594         case DW_OP_GNU_regval_type:
21595         case DW_OP_GNU_deref_type:
21596           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21597           break;
21598         case DW_OP_GNU_convert:
21599         case DW_OP_GNU_reinterpret:
21600           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21601             continue;
21602           /* FALLTHRU */
21603         case DW_OP_GNU_const_type:
21604           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21605           break;
21606         case DW_OP_GNU_entry_value:
21607           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21608           continue;
21609         default:
21610           continue;
21611         }
21612       gcc_assert (base_type->die_parent == comp_unit_die ());
21613       if (base_type->die_mark)
21614         base_type->die_mark++;
21615       else
21616         {
21617           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21618           base_type->die_mark = 1;
21619         }
21620     }
21621 }
21622
21623 /* Comparison function for sorting marked base types.  */
21624
21625 static int
21626 base_type_cmp (const void *x, const void *y)
21627 {
21628   dw_die_ref dx = *(const dw_die_ref *) x;
21629   dw_die_ref dy = *(const dw_die_ref *) y;
21630   unsigned int byte_size1, byte_size2;
21631   unsigned int encoding1, encoding2;
21632   if (dx->die_mark > dy->die_mark)
21633     return -1;
21634   if (dx->die_mark < dy->die_mark)
21635     return 1;
21636   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21637   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21638   if (byte_size1 < byte_size2)
21639     return 1;
21640   if (byte_size1 > byte_size2)
21641     return -1;
21642   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21643   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21644   if (encoding1 < encoding2)
21645     return 1;
21646   if (encoding1 > encoding2)
21647     return -1;
21648   return 0;
21649 }
21650
21651 /* Move base types marked by mark_base_types as early as possible
21652    in the CU, sorted by decreasing usage count both to make the
21653    uleb128 references as small as possible and to make sure they
21654    will have die_offset already computed by calc_die_sizes when
21655    sizes of typed stack loc ops is computed.  */
21656
21657 static void
21658 move_marked_base_types (void)
21659 {
21660   unsigned int i;
21661   dw_die_ref base_type, die, c;
21662
21663   if (VEC_empty (dw_die_ref, base_types))
21664     return;
21665
21666   /* Sort by decreasing usage count, they will be added again in that
21667      order later on.  */
21668   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21669   die = comp_unit_die ();
21670   c = die->die_child;
21671   do
21672     {
21673       dw_die_ref prev = c;
21674       c = c->die_sib;
21675       while (c->die_mark)
21676         {
21677           remove_child_with_prev (c, prev);
21678           /* As base types got marked, there must be at least
21679              one node other than DW_TAG_base_type.  */
21680           gcc_assert (c != c->die_sib);
21681           c = c->die_sib;
21682         }
21683     }
21684   while (c != die->die_child);
21685   gcc_assert (die->die_child);
21686   c = die->die_child;
21687   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21688     {
21689       base_type->die_mark = 0;
21690       base_type->die_sib = c->die_sib;
21691       c->die_sib = base_type;
21692       c = base_type;
21693     }
21694 }
21695
21696 /* Helper function for resolve_addr, attempt to resolve
21697    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21698    SYMBOL_REFs refer to variables emitted in the current CU.  */
21699
21700 static int
21701 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21702 {
21703   rtx rtl = *addr;
21704
21705   if (GET_CODE (rtl) == CONST_STRING)
21706     {
21707       size_t len = strlen (XSTR (rtl, 0)) + 1;
21708       tree t = build_string (len, XSTR (rtl, 0));
21709       tree tlen = size_int (len - 1);
21710       TREE_TYPE (t)
21711         = build_array_type (char_type_node, build_index_type (tlen));
21712       rtl = lookup_constant_def (t);
21713       if (!rtl || !MEM_P (rtl))
21714         return 1;
21715       rtl = XEXP (rtl, 0);
21716       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21717       *addr = rtl;
21718       return 0;
21719     }
21720
21721   if (GET_CODE (rtl) == SYMBOL_REF
21722       && SYMBOL_REF_DECL (rtl))
21723     {
21724       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21725         {
21726           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21727             return 1;
21728         }
21729       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21730         return 1;
21731     }
21732
21733   if (GET_CODE (rtl) == CONST
21734       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21735     return 1;
21736
21737   return 0;
21738 }
21739
21740 /* Helper function for resolve_addr, handle one location
21741    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21742    the location list couldn't be resolved.  */
21743
21744 static bool
21745 resolve_addr_in_expr (dw_loc_descr_ref loc)
21746 {
21747   dw_loc_descr_ref keep = NULL;
21748   for (; loc; loc = loc->dw_loc_next)
21749     switch (loc->dw_loc_opc)
21750       {
21751       case DW_OP_addr:
21752         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21753           return false;
21754         break;
21755       case DW_OP_const4u:
21756       case DW_OP_const8u:
21757         if (loc->dtprel
21758             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21759           return false;
21760         break;
21761       case DW_OP_plus_uconst:
21762         if (size_of_loc_descr (loc)
21763             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21764               + 1
21765             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21766           {
21767             dw_loc_descr_ref repl
21768               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21769             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21770             add_loc_descr (&repl, loc->dw_loc_next);
21771             *loc = *repl;
21772           }
21773         break;
21774       case DW_OP_implicit_value:
21775         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21776             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21777           return false;
21778         break;
21779       case DW_OP_GNU_implicit_pointer:
21780       case DW_OP_GNU_parameter_ref:
21781         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21782           {
21783             dw_die_ref ref
21784               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21785             if (ref == NULL)
21786               return false;
21787             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21788             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21789             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21790           }
21791         break;
21792       case DW_OP_GNU_const_type:
21793       case DW_OP_GNU_regval_type:
21794       case DW_OP_GNU_deref_type:
21795       case DW_OP_GNU_convert:
21796       case DW_OP_GNU_reinterpret:
21797         while (loc->dw_loc_next
21798                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21799           {
21800             dw_die_ref base1, base2;
21801             unsigned enc1, enc2, size1, size2;
21802             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21803                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21804               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21805             else if (loc->dw_loc_oprnd1.val_class
21806                      == dw_val_class_unsigned_const)
21807               break;
21808             else
21809               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21810             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21811                 == dw_val_class_unsigned_const)
21812               break;
21813             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21814             gcc_assert (base1->die_tag == DW_TAG_base_type
21815                         && base2->die_tag == DW_TAG_base_type);
21816             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21817             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21818             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21819             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21820             if (size1 == size2
21821                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21822                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21823                      && loc != keep)
21824                     || enc1 == enc2))
21825               {
21826                 /* Optimize away next DW_OP_GNU_convert after
21827                    adjusting LOC's base type die reference.  */
21828                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21829                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21830                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21831                 else
21832                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21833                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21834                 continue;
21835               }
21836             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21837                point typed stack entry.  */
21838             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21839               keep = loc->dw_loc_next;
21840             break;
21841           }
21842         break;
21843       default:
21844         break;
21845       }
21846   return true;
21847 }
21848
21849 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21850    an address in .rodata section if the string literal is emitted there,
21851    or remove the containing location list or replace DW_AT_const_value
21852    with DW_AT_location and empty location expression, if it isn't found
21853    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21854    to something that has been emitted in the current CU.  */
21855
21856 static void
21857 resolve_addr (dw_die_ref die)
21858 {
21859   dw_die_ref c;
21860   dw_attr_ref a;
21861   dw_loc_list_ref *curr, *start, loc;
21862   unsigned ix;
21863
21864   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21865     switch (AT_class (a))
21866       {
21867       case dw_val_class_loc_list:
21868         start = curr = AT_loc_list_ptr (a);
21869         loc = *curr;
21870         gcc_assert (loc);
21871         /* The same list can be referenced more than once.  See if we have
21872            already recorded the result from a previous pass.  */
21873         if (loc->replaced)
21874           *curr = loc->dw_loc_next;
21875         else if (!loc->resolved_addr)
21876           {
21877             /* As things stand, we do not expect or allow one die to
21878                reference a suffix of another die's location list chain.
21879                References must be identical or completely separate.
21880                There is therefore no need to cache the result of this
21881                pass on any list other than the first; doing so
21882                would lead to unnecessary writes.  */
21883             while (*curr)
21884               {
21885                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21886                 if (!resolve_addr_in_expr ((*curr)->expr))
21887                   {
21888                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21889                     if (next && (*curr)->ll_symbol)
21890                       {
21891                         gcc_assert (!next->ll_symbol);
21892                         next->ll_symbol = (*curr)->ll_symbol;
21893                       }
21894                     *curr = next;
21895                   }
21896                 else
21897                   {
21898                     mark_base_types ((*curr)->expr);
21899                     curr = &(*curr)->dw_loc_next;
21900                   }
21901               }
21902             if (loc == *start)
21903               loc->resolved_addr = 1;
21904             else
21905               {
21906                 loc->replaced = 1;
21907                 loc->dw_loc_next = *start;
21908               }
21909           }
21910         if (!*start)
21911           {
21912             remove_AT (die, a->dw_attr);
21913             ix--;
21914           }
21915         break;
21916       case dw_val_class_loc:
21917         {
21918           dw_loc_descr_ref l = AT_loc (a);
21919           /* For -gdwarf-2 don't attempt to optimize
21920              DW_AT_data_member_location containing
21921              DW_OP_plus_uconst - older consumers might
21922              rely on it being that op instead of a more complex,
21923              but shorter, location description.  */
21924           if ((dwarf_version > 2
21925                || a->dw_attr != DW_AT_data_member_location
21926                || l == NULL
21927                || l->dw_loc_opc != DW_OP_plus_uconst
21928                || l->dw_loc_next != NULL)
21929               && !resolve_addr_in_expr (l))
21930             {
21931               remove_AT (die, a->dw_attr);
21932               ix--;
21933             }
21934           else
21935             mark_base_types (l);
21936         }
21937         break;
21938       case dw_val_class_addr:
21939         if (a->dw_attr == DW_AT_const_value
21940             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21941           {
21942             remove_AT (die, a->dw_attr);
21943             ix--;
21944           }
21945         if (die->die_tag == DW_TAG_GNU_call_site
21946             && a->dw_attr == DW_AT_abstract_origin)
21947           {
21948             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21949             dw_die_ref tdie = lookup_decl_die (tdecl);
21950             if (tdie == NULL
21951                 && DECL_EXTERNAL (tdecl)
21952                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21953               {
21954                 force_decl_die (tdecl);
21955                 tdie = lookup_decl_die (tdecl);
21956               }
21957             if (tdie)
21958               {
21959                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21960                 a->dw_attr_val.v.val_die_ref.die = tdie;
21961                 a->dw_attr_val.v.val_die_ref.external = 0;
21962               }
21963             else
21964               {
21965                 remove_AT (die, a->dw_attr);
21966                 ix--;
21967               }
21968           }
21969         break;
21970       default:
21971         break;
21972       }
21973
21974   FOR_EACH_CHILD (die, c, resolve_addr (c));
21975 }
21976 \f
21977 /* Helper routines for optimize_location_lists.
21978    This pass tries to share identical local lists in .debug_loc
21979    section.  */
21980
21981 /* Iteratively hash operands of LOC opcode.  */
21982
21983 static inline hashval_t
21984 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21985 {
21986   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21987   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21988
21989   switch (loc->dw_loc_opc)
21990     {
21991     case DW_OP_const4u:
21992     case DW_OP_const8u:
21993       if (loc->dtprel)
21994         goto hash_addr;
21995       /* FALLTHRU */
21996     case DW_OP_const1u:
21997     case DW_OP_const1s:
21998     case DW_OP_const2u:
21999     case DW_OP_const2s:
22000     case DW_OP_const4s:
22001     case DW_OP_const8s:
22002     case DW_OP_constu:
22003     case DW_OP_consts:
22004     case DW_OP_pick:
22005     case DW_OP_plus_uconst:
22006     case DW_OP_breg0:
22007     case DW_OP_breg1:
22008     case DW_OP_breg2:
22009     case DW_OP_breg3:
22010     case DW_OP_breg4:
22011     case DW_OP_breg5:
22012     case DW_OP_breg6:
22013     case DW_OP_breg7:
22014     case DW_OP_breg8:
22015     case DW_OP_breg9:
22016     case DW_OP_breg10:
22017     case DW_OP_breg11:
22018     case DW_OP_breg12:
22019     case DW_OP_breg13:
22020     case DW_OP_breg14:
22021     case DW_OP_breg15:
22022     case DW_OP_breg16:
22023     case DW_OP_breg17:
22024     case DW_OP_breg18:
22025     case DW_OP_breg19:
22026     case DW_OP_breg20:
22027     case DW_OP_breg21:
22028     case DW_OP_breg22:
22029     case DW_OP_breg23:
22030     case DW_OP_breg24:
22031     case DW_OP_breg25:
22032     case DW_OP_breg26:
22033     case DW_OP_breg27:
22034     case DW_OP_breg28:
22035     case DW_OP_breg29:
22036     case DW_OP_breg30:
22037     case DW_OP_breg31:
22038     case DW_OP_regx:
22039     case DW_OP_fbreg:
22040     case DW_OP_piece:
22041     case DW_OP_deref_size:
22042     case DW_OP_xderef_size:
22043       hash = iterative_hash_object (val1->v.val_int, hash);
22044       break;
22045     case DW_OP_skip:
22046     case DW_OP_bra:
22047       {
22048         int offset;
22049
22050         gcc_assert (val1->val_class == dw_val_class_loc);
22051         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22052         hash = iterative_hash_object (offset, hash);
22053       }
22054       break;
22055     case DW_OP_implicit_value:
22056       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22057       switch (val2->val_class)
22058         {
22059         case dw_val_class_const:
22060           hash = iterative_hash_object (val2->v.val_int, hash);
22061           break;
22062         case dw_val_class_vec:
22063           {
22064             unsigned int elt_size = val2->v.val_vec.elt_size;
22065             unsigned int len = val2->v.val_vec.length;
22066
22067             hash = iterative_hash_object (elt_size, hash);
22068             hash = iterative_hash_object (len, hash);
22069             hash = iterative_hash (val2->v.val_vec.array,
22070                                    len * elt_size, hash);
22071           }
22072           break;
22073         case dw_val_class_const_double:
22074           hash = iterative_hash_object (val2->v.val_double.low, hash);
22075           hash = iterative_hash_object (val2->v.val_double.high, hash);
22076           break;
22077         case dw_val_class_addr:
22078           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22079           break;
22080         default:
22081           gcc_unreachable ();
22082         }
22083       break;
22084     case DW_OP_bregx:
22085     case DW_OP_bit_piece:
22086       hash = iterative_hash_object (val1->v.val_int, hash);
22087       hash = iterative_hash_object (val2->v.val_int, hash);
22088       break;
22089     case DW_OP_addr:
22090     hash_addr:
22091       if (loc->dtprel)
22092         {
22093           unsigned char dtprel = 0xd1;
22094           hash = iterative_hash_object (dtprel, hash);
22095         }
22096       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22097       break;
22098     case DW_OP_GNU_implicit_pointer:
22099       hash = iterative_hash_object (val2->v.val_int, hash);
22100       break;
22101     case DW_OP_GNU_entry_value:
22102       hash = hash_loc_operands (val1->v.val_loc, hash);
22103       break;
22104     case DW_OP_GNU_regval_type:
22105     case DW_OP_GNU_deref_type:
22106       {
22107         unsigned int byte_size
22108           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22109         unsigned int encoding
22110           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22111         hash = iterative_hash_object (val1->v.val_int, hash);
22112         hash = iterative_hash_object (byte_size, hash);
22113         hash = iterative_hash_object (encoding, hash);
22114       }
22115       break;
22116     case DW_OP_GNU_convert:
22117     case DW_OP_GNU_reinterpret:
22118       if (val1->val_class == dw_val_class_unsigned_const)
22119         {
22120           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22121           break;
22122         }
22123       /* FALLTHRU */
22124     case DW_OP_GNU_const_type:
22125       {
22126         unsigned int byte_size
22127           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22128         unsigned int encoding
22129           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22130         hash = iterative_hash_object (byte_size, hash);
22131         hash = iterative_hash_object (encoding, hash);
22132         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22133           break;
22134         hash = iterative_hash_object (val2->val_class, hash);
22135         switch (val2->val_class)
22136           {
22137           case dw_val_class_const:
22138             hash = iterative_hash_object (val2->v.val_int, hash);
22139             break;
22140           case dw_val_class_vec:
22141             {
22142               unsigned int elt_size = val2->v.val_vec.elt_size;
22143               unsigned int len = val2->v.val_vec.length;
22144
22145               hash = iterative_hash_object (elt_size, hash);
22146               hash = iterative_hash_object (len, hash);
22147               hash = iterative_hash (val2->v.val_vec.array,
22148                                      len * elt_size, hash);
22149             }
22150             break;
22151           case dw_val_class_const_double:
22152             hash = iterative_hash_object (val2->v.val_double.low, hash);
22153             hash = iterative_hash_object (val2->v.val_double.high, hash);
22154             break;
22155           default:
22156             gcc_unreachable ();
22157           }
22158       }
22159       break;
22160
22161     default:
22162       /* Other codes have no operands.  */
22163       break;
22164     }
22165   return hash;
22166 }
22167
22168 /* Iteratively hash the whole DWARF location expression LOC.  */
22169
22170 static inline hashval_t
22171 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22172 {
22173   dw_loc_descr_ref l;
22174   bool sizes_computed = false;
22175   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22176   size_of_locs (loc);
22177
22178   for (l = loc; l != NULL; l = l->dw_loc_next)
22179     {
22180       enum dwarf_location_atom opc = l->dw_loc_opc;
22181       hash = iterative_hash_object (opc, hash);
22182       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22183         {
22184           size_of_locs (loc);
22185           sizes_computed = true;
22186         }
22187       hash = hash_loc_operands (l, hash);
22188     }
22189   return hash;
22190 }
22191
22192 /* Compute hash of the whole location list LIST_HEAD.  */
22193
22194 static inline void
22195 hash_loc_list (dw_loc_list_ref list_head)
22196 {
22197   dw_loc_list_ref curr = list_head;
22198   hashval_t hash = 0;
22199
22200   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22201     {
22202       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22203       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22204       if (curr->section)
22205         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22206                                hash);
22207       hash = hash_locs (curr->expr, hash);
22208     }
22209   list_head->hash = hash;
22210 }
22211
22212 /* Return true if X and Y opcodes have the same operands.  */
22213
22214 static inline bool
22215 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22216 {
22217   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22218   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22219   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22220   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22221
22222   switch (x->dw_loc_opc)
22223     {
22224     case DW_OP_const4u:
22225     case DW_OP_const8u:
22226       if (x->dtprel)
22227         goto hash_addr;
22228       /* FALLTHRU */
22229     case DW_OP_const1u:
22230     case DW_OP_const1s:
22231     case DW_OP_const2u:
22232     case DW_OP_const2s:
22233     case DW_OP_const4s:
22234     case DW_OP_const8s:
22235     case DW_OP_constu:
22236     case DW_OP_consts:
22237     case DW_OP_pick:
22238     case DW_OP_plus_uconst:
22239     case DW_OP_breg0:
22240     case DW_OP_breg1:
22241     case DW_OP_breg2:
22242     case DW_OP_breg3:
22243     case DW_OP_breg4:
22244     case DW_OP_breg5:
22245     case DW_OP_breg6:
22246     case DW_OP_breg7:
22247     case DW_OP_breg8:
22248     case DW_OP_breg9:
22249     case DW_OP_breg10:
22250     case DW_OP_breg11:
22251     case DW_OP_breg12:
22252     case DW_OP_breg13:
22253     case DW_OP_breg14:
22254     case DW_OP_breg15:
22255     case DW_OP_breg16:
22256     case DW_OP_breg17:
22257     case DW_OP_breg18:
22258     case DW_OP_breg19:
22259     case DW_OP_breg20:
22260     case DW_OP_breg21:
22261     case DW_OP_breg22:
22262     case DW_OP_breg23:
22263     case DW_OP_breg24:
22264     case DW_OP_breg25:
22265     case DW_OP_breg26:
22266     case DW_OP_breg27:
22267     case DW_OP_breg28:
22268     case DW_OP_breg29:
22269     case DW_OP_breg30:
22270     case DW_OP_breg31:
22271     case DW_OP_regx:
22272     case DW_OP_fbreg:
22273     case DW_OP_piece:
22274     case DW_OP_deref_size:
22275     case DW_OP_xderef_size:
22276       return valx1->v.val_int == valy1->v.val_int;
22277     case DW_OP_skip:
22278     case DW_OP_bra:
22279       gcc_assert (valx1->val_class == dw_val_class_loc
22280                   && valy1->val_class == dw_val_class_loc
22281                   && x->dw_loc_addr == y->dw_loc_addr);
22282       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22283     case DW_OP_implicit_value:
22284       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22285           || valx2->val_class != valy2->val_class)
22286         return false;
22287       switch (valx2->val_class)
22288         {
22289         case dw_val_class_const:
22290           return valx2->v.val_int == valy2->v.val_int;
22291         case dw_val_class_vec:
22292           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22293                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22294                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22295                             valx2->v.val_vec.elt_size
22296                             * valx2->v.val_vec.length) == 0;
22297         case dw_val_class_const_double:
22298           return valx2->v.val_double.low == valy2->v.val_double.low
22299                  && valx2->v.val_double.high == valy2->v.val_double.high;
22300         case dw_val_class_addr:
22301           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22302         default:
22303           gcc_unreachable ();
22304         }
22305     case DW_OP_bregx:
22306     case DW_OP_bit_piece:
22307       return valx1->v.val_int == valy1->v.val_int
22308              && valx2->v.val_int == valy2->v.val_int;
22309     case DW_OP_addr:
22310     hash_addr:
22311       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22312     case DW_OP_GNU_implicit_pointer:
22313       return valx1->val_class == dw_val_class_die_ref
22314              && valx1->val_class == valy1->val_class
22315              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22316              && valx2->v.val_int == valy2->v.val_int;
22317     case DW_OP_GNU_entry_value:
22318       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22319     case DW_OP_GNU_const_type:
22320       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22321           || valx2->val_class != valy2->val_class)
22322         return false;
22323       switch (valx2->val_class)
22324         {
22325         case dw_val_class_const:
22326           return valx2->v.val_int == valy2->v.val_int;
22327         case dw_val_class_vec:
22328           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22329                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22330                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22331                             valx2->v.val_vec.elt_size
22332                             * valx2->v.val_vec.length) == 0;
22333         case dw_val_class_const_double:
22334           return valx2->v.val_double.low == valy2->v.val_double.low
22335                  && valx2->v.val_double.high == valy2->v.val_double.high;
22336         default:
22337           gcc_unreachable ();
22338         }
22339     case DW_OP_GNU_regval_type:
22340     case DW_OP_GNU_deref_type:
22341       return valx1->v.val_int == valy1->v.val_int
22342              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22343     case DW_OP_GNU_convert:
22344     case DW_OP_GNU_reinterpret:
22345       if (valx1->val_class != valy1->val_class)
22346         return false;
22347       if (valx1->val_class == dw_val_class_unsigned_const)
22348         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22349       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22350     case DW_OP_GNU_parameter_ref:
22351       return valx1->val_class == dw_val_class_die_ref
22352              && valx1->val_class == valy1->val_class
22353              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22354     default:
22355       /* Other codes have no operands.  */
22356       return true;
22357     }
22358 }
22359
22360 /* Return true if DWARF location expressions X and Y are the same.  */
22361
22362 static inline bool
22363 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22364 {
22365   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22366     if (x->dw_loc_opc != y->dw_loc_opc
22367         || x->dtprel != y->dtprel
22368         || !compare_loc_operands (x, y))
22369       break;
22370   return x == NULL && y == NULL;
22371 }
22372
22373 /* Return precomputed hash of location list X.  */
22374
22375 static hashval_t
22376 loc_list_hash (const void *x)
22377 {
22378   return ((const struct dw_loc_list_struct *) x)->hash;
22379 }
22380
22381 /* Return 1 if location lists X and Y are the same.  */
22382
22383 static int
22384 loc_list_eq (const void *x, const void *y)
22385 {
22386   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22387   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22388   if (a == b)
22389     return 1;
22390   if (a->hash != b->hash)
22391     return 0;
22392   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22393     if (strcmp (a->begin, b->begin) != 0
22394         || strcmp (a->end, b->end) != 0
22395         || (a->section == NULL) != (b->section == NULL)
22396         || (a->section && strcmp (a->section, b->section) != 0)
22397         || !compare_locs (a->expr, b->expr))
22398       break;
22399   return a == NULL && b == NULL;
22400 }
22401
22402 /* Recursively optimize location lists referenced from DIE
22403    children and share them whenever possible.  */
22404
22405 static void
22406 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22407 {
22408   dw_die_ref c;
22409   dw_attr_ref a;
22410   unsigned ix;
22411   void **slot;
22412
22413   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22414     if (AT_class (a) == dw_val_class_loc_list)
22415       {
22416         dw_loc_list_ref list = AT_loc_list (a);
22417         /* TODO: perform some optimizations here, before hashing
22418            it and storing into the hash table.  */
22419         hash_loc_list (list);
22420         slot = htab_find_slot_with_hash (htab, list, list->hash,
22421                                          INSERT);
22422         if (*slot == NULL)
22423           *slot = (void *) list;
22424         else
22425           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22426       }
22427
22428   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22429 }
22430
22431 /* Optimize location lists referenced from DIE
22432    children and share them whenever possible.  */
22433
22434 static void
22435 optimize_location_lists (dw_die_ref die)
22436 {
22437   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22438   optimize_location_lists_1 (die, htab);
22439   htab_delete (htab);
22440 }
22441 \f
22442 /* Output stuff that dwarf requires at the end of every file,
22443    and generate the DWARF-2 debugging info.  */
22444
22445 static void
22446 dwarf2out_finish (const char *filename)
22447 {
22448   limbo_die_node *node, *next_node;
22449   comdat_type_node *ctnode;
22450   htab_t comdat_type_table;
22451   unsigned int i;
22452
22453   /* PCH might result in DW_AT_producer string being restored from the
22454      header compilation, fix it up if needed.  */
22455   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22456   if (strcmp (AT_string (producer), producer_string) != 0)
22457     {
22458       struct indirect_string_node *node = find_AT_string (producer_string);
22459       producer->dw_attr_val.v.val_str = node;
22460     }
22461
22462   gen_scheduled_generic_parms_dies ();
22463   gen_remaining_tmpl_value_param_die_attribute ();
22464
22465   /* Add the name for the main input file now.  We delayed this from
22466      dwarf2out_init to avoid complications with PCH.  */
22467   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22468   if (!IS_ABSOLUTE_PATH (filename))
22469     add_comp_dir_attribute (comp_unit_die ());
22470   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22471     {
22472       bool p = false;
22473       htab_traverse (file_table, file_table_relative_p, &p);
22474       if (p)
22475         add_comp_dir_attribute (comp_unit_die ());
22476     }
22477
22478   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22479     {
22480       add_location_or_const_value_attribute (
22481         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22482         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22483         false,
22484         DW_AT_location);
22485     }
22486
22487   /* Traverse the limbo die list, and add parent/child links.  The only
22488      dies without parents that should be here are concrete instances of
22489      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22490      For concrete instances, we can get the parent die from the abstract
22491      instance.  */
22492   for (node = limbo_die_list; node; node = next_node)
22493     {
22494       dw_die_ref die = node->die;
22495       next_node = node->next;
22496
22497       if (die->die_parent == NULL)
22498         {
22499           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22500
22501           if (origin && origin->die_parent)
22502             add_child_die (origin->die_parent, die);
22503           else if (is_cu_die (die))
22504             ;
22505           else if (seen_error ())
22506             /* It's OK to be confused by errors in the input.  */
22507             add_child_die (comp_unit_die (), die);
22508           else
22509             {
22510               /* In certain situations, the lexical block containing a
22511                  nested function can be optimized away, which results
22512                  in the nested function die being orphaned.  Likewise
22513                  with the return type of that nested function.  Force
22514                  this to be a child of the containing function.
22515
22516                  It may happen that even the containing function got fully
22517                  inlined and optimized out.  In that case we are lost and
22518                  assign the empty child.  This should not be big issue as
22519                  the function is likely unreachable too.  */
22520               tree context = NULL_TREE;
22521
22522               gcc_assert (node->created_for);
22523
22524               if (DECL_P (node->created_for))
22525                 context = DECL_CONTEXT (node->created_for);
22526               else if (TYPE_P (node->created_for))
22527                 context = TYPE_CONTEXT (node->created_for);
22528
22529               origin = get_context_die (context);
22530               add_child_die (origin, die);
22531             }
22532         }
22533     }
22534
22535   limbo_die_list = NULL;
22536
22537 #if ENABLE_ASSERT_CHECKING
22538   {
22539     dw_die_ref die = comp_unit_die (), c;
22540     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22541   }
22542 #endif
22543   resolve_addr (comp_unit_die ());
22544   move_marked_base_types ();
22545
22546   for (node = deferred_asm_name; node; node = node->next)
22547     {
22548       tree decl = node->created_for;
22549       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22550         {
22551           add_linkage_attr (node->die, decl);
22552           move_linkage_attr (node->die);
22553         }
22554     }
22555
22556   deferred_asm_name = NULL;
22557
22558   /* Walk through the list of incomplete types again, trying once more to
22559      emit full debugging info for them.  */
22560   retry_incomplete_types ();
22561
22562   if (flag_eliminate_unused_debug_types)
22563     prune_unused_types ();
22564
22565   /* Generate separate CUs for each of the include files we've seen.
22566      They will go into limbo_die_list.  */
22567   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22568     break_out_includes (comp_unit_die ());
22569
22570   /* Generate separate COMDAT sections for type DIEs. */
22571   if (use_debug_types)
22572     {
22573       break_out_comdat_types (comp_unit_die ());
22574
22575       /* Each new type_unit DIE was added to the limbo die list when created.
22576          Since these have all been added to comdat_type_list, clear the
22577          limbo die list.  */
22578       limbo_die_list = NULL;
22579
22580       /* For each new comdat type unit, copy declarations for incomplete
22581          types to make the new unit self-contained (i.e., no direct
22582          references to the main compile unit).  */
22583       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22584         copy_decls_for_unworthy_types (ctnode->root_die);
22585       copy_decls_for_unworthy_types (comp_unit_die ());
22586
22587       /* In the process of copying declarations from one unit to another,
22588          we may have left some declarations behind that are no longer
22589          referenced.  Prune them.  */
22590       prune_unused_types ();
22591     }
22592
22593   /* Traverse the DIE's and add add sibling attributes to those DIE's
22594      that have children.  */
22595   add_sibling_attributes (comp_unit_die ());
22596   for (node = limbo_die_list; node; node = node->next)
22597     add_sibling_attributes (node->die);
22598   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22599     add_sibling_attributes (ctnode->root_die);
22600
22601   /* Output a terminator label for the .text section.  */
22602   switch_to_section (text_section);
22603   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22604   if (cold_text_section)
22605     {
22606       switch_to_section (cold_text_section);
22607       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22608     }
22609
22610   /* We can only use the low/high_pc attributes if all of the code was
22611      in .text.  */
22612   if (!have_multiple_function_sections 
22613       || (dwarf_version < 3 && dwarf_strict))
22614     {
22615       /* Don't add if the CU has no associated code.  */
22616       if (text_section_used)
22617         {
22618           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22619           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22620         }
22621     }
22622   else
22623     {
22624       unsigned fde_idx;
22625       dw_fde_ref fde;
22626       bool range_list_added = false;
22627
22628       if (text_section_used)
22629         add_ranges_by_labels (comp_unit_die (), text_section_label,
22630                               text_end_label, &range_list_added);
22631       if (cold_text_section_used)
22632         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22633                               cold_end_label, &range_list_added);
22634
22635       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22636         {
22637           if (!fde->in_std_section)
22638             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22639                                   fde->dw_fde_end, &range_list_added);
22640           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22641             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22642                                   fde->dw_fde_second_end, &range_list_added);
22643         }
22644
22645       if (range_list_added)
22646         {
22647           /* We need to give .debug_loc and .debug_ranges an appropriate
22648              "base address".  Use zero so that these addresses become
22649              absolute.  Historically, we've emitted the unexpected
22650              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22651              Emit both to give time for other tools to adapt.  */
22652           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22653           if (! dwarf_strict && dwarf_version < 4)
22654             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22655
22656           add_ranges (NULL);
22657         }
22658     }
22659
22660   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22661     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22662                     debug_line_section_label);
22663
22664   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22665     add_AT_macptr (comp_unit_die (),
22666                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22667                    macinfo_section_label);
22668
22669   if (have_location_lists)
22670     optimize_location_lists (comp_unit_die ());
22671
22672   /* Output all of the compilation units.  We put the main one last so that
22673      the offsets are available to output_pubnames.  */
22674   for (node = limbo_die_list; node; node = node->next)
22675     output_comp_unit (node->die, 0);
22676
22677   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22678   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22679     {
22680       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22681
22682       /* Don't output duplicate types.  */
22683       if (*slot != HTAB_EMPTY_ENTRY)
22684         continue;
22685
22686       /* Add a pointer to the line table for the main compilation unit
22687          so that the debugger can make sense of DW_AT_decl_file
22688          attributes.  */
22689       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22690         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22691                         debug_line_section_label);
22692
22693       output_comdat_type_unit (ctnode);
22694       *slot = ctnode;
22695     }
22696   htab_delete (comdat_type_table);
22697
22698   /* Output the main compilation unit if non-empty or if .debug_macinfo
22699      will be emitted.  */
22700   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22701
22702   /* Output the abbreviation table.  */
22703   if (abbrev_die_table_in_use != 1)
22704     {
22705       switch_to_section (debug_abbrev_section);
22706       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22707       output_abbrev_section ();
22708     }
22709
22710   /* Output location list section if necessary.  */
22711   if (have_location_lists)
22712     {
22713       /* Output the location lists info.  */
22714       switch_to_section (debug_loc_section);
22715       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22716                                    DEBUG_LOC_SECTION_LABEL, 0);
22717       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22718       output_location_lists (comp_unit_die ());
22719     }
22720
22721   /* Output public names table if necessary.  */
22722   if (!VEC_empty (pubname_entry, pubname_table))
22723     {
22724       gcc_assert (info_section_emitted);
22725       switch_to_section (debug_pubnames_section);
22726       output_pubnames (pubname_table);
22727     }
22728
22729   /* Output public types table if necessary.  */
22730   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22731      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22732      simply won't look for the section.  */
22733   if (!VEC_empty (pubname_entry, pubtype_table))
22734     {
22735       bool empty = false;
22736       
22737       if (flag_eliminate_unused_debug_types)
22738         {
22739           /* The pubtypes table might be emptied by pruning unused items.  */
22740           unsigned i;
22741           pubname_ref p;
22742           empty = true;
22743           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22744             if (p->die->die_offset != 0)
22745               {
22746                 empty = false;
22747                 break;
22748               }
22749         }
22750       if (!empty)
22751         {
22752           gcc_assert (info_section_emitted);
22753           switch_to_section (debug_pubtypes_section);
22754           output_pubnames (pubtype_table);
22755         }
22756     }
22757
22758   /* Output the address range information if a CU (.debug_info section)
22759      was emitted.  We output an empty table even if we had no functions
22760      to put in it.  This because the consumer has no way to tell the
22761      difference between an empty table that we omitted and failure to
22762      generate a table that would have contained data.  */
22763   if (info_section_emitted)
22764     {
22765       unsigned long aranges_length = size_of_aranges ();
22766
22767       switch_to_section (debug_aranges_section);
22768       output_aranges (aranges_length);
22769     }
22770
22771   /* Output ranges section if necessary.  */
22772   if (ranges_table_in_use)
22773     {
22774       switch_to_section (debug_ranges_section);
22775       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22776       output_ranges ();
22777     }
22778
22779   /* Have to end the macro section.  */
22780   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22781     {
22782       switch_to_section (debug_macinfo_section);
22783       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22784       if (!VEC_empty (macinfo_entry, macinfo_table))
22785         output_macinfo ();
22786       dw2_asm_output_data (1, 0, "End compilation unit");
22787     }
22788
22789   /* Output the source line correspondence table.  We must do this
22790      even if there is no line information.  Otherwise, on an empty
22791      translation unit, we will generate a present, but empty,
22792      .debug_info section.  IRIX 6.5 `nm' will then complain when
22793      examining the file.  This is done late so that any filenames
22794      used by the debug_info section are marked as 'used'.  */
22795   switch_to_section (debug_line_section);
22796   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22797   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22798     output_line_info ();
22799
22800   /* If we emitted any DW_FORM_strp form attribute, output the string
22801      table too.  */
22802   if (debug_str_hash)
22803     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22804 }
22805
22806 #include "gt-dwarf2out.h"