OSDN Git Service

Backported from mainline
[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 /* Like clone_tree, but additionally enter all the children into
7411    the hash table decl_table.  */
7412
7413 static dw_die_ref
7414 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7415 {
7416   dw_die_ref c;
7417   dw_die_ref clone = clone_die (die);
7418   struct decl_table_entry *entry;
7419   void **slot = htab_find_slot_with_hash (decl_table, die,
7420                                           htab_hash_pointer (die), INSERT);
7421   /* Assert that DIE isn't in the hash table yet.  If it would be there
7422      before, the ancestors would be necessarily there as well, therefore
7423      clone_tree_hash wouldn't be called.  */
7424   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7425   entry = XCNEW (struct decl_table_entry);
7426   entry->orig = die;
7427   entry->copy = clone;
7428   *slot = entry;
7429
7430   FOR_EACH_CHILD (die, c,
7431                   add_child_die (clone, clone_tree_hash (c, decl_table)));
7432
7433   return clone;
7434 }
7435
7436 /* Walk the DIE and its children, looking for references to incomplete
7437    or trivial types that are unmarked (i.e., that are not in the current
7438    type_unit).  */
7439
7440 static void
7441 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7442 {
7443   dw_die_ref c;
7444   dw_attr_ref a;
7445   unsigned ix;
7446
7447   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7448     {
7449       if (AT_class (a) == dw_val_class_die_ref)
7450         {
7451           dw_die_ref targ = AT_ref (a);
7452           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7453           void **slot;
7454           struct decl_table_entry *entry;
7455
7456           if (targ->die_mark != 0 || type_node != NULL)
7457             continue;
7458
7459           slot = htab_find_slot_with_hash (decl_table, targ,
7460                                            htab_hash_pointer (targ), INSERT);
7461
7462           if (*slot != HTAB_EMPTY_ENTRY)
7463             {
7464               /* TARG has already been copied, so we just need to
7465                  modify the reference to point to the copy.  */
7466               entry = (struct decl_table_entry *) *slot;
7467               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7468             }
7469           else
7470             {
7471               dw_die_ref parent = unit;
7472               dw_die_ref copy = clone_die (targ);
7473
7474               /* Record in DECL_TABLE that TARG has been copied.
7475                  Need to do this now, before the recursive call,
7476                  because DECL_TABLE may be expanded and SLOT
7477                  would no longer be a valid pointer.  */
7478               entry = XCNEW (struct decl_table_entry);
7479               entry->orig = targ;
7480               entry->copy = copy;
7481               *slot = entry;
7482
7483               FOR_EACH_CHILD (targ, c,
7484                               add_child_die (copy,
7485                                              clone_tree_hash (c, decl_table)));
7486
7487               /* Make sure the cloned tree is marked as part of the
7488                  type unit.  */
7489               mark_dies (copy);
7490
7491               /* If TARG has surrounding context, copy its ancestor tree
7492                  into the new type unit.  */
7493               if (targ->die_parent != NULL
7494                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7495                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7496                 parent = copy_ancestor_tree (unit, targ->die_parent,
7497                                              decl_table);
7498
7499               add_child_die (parent, copy);
7500               a->dw_attr_val.v.val_die_ref.die = copy;
7501
7502               /* Make sure the newly-copied DIE is walked.  If it was
7503                  installed in a previously-added context, it won't
7504                  get visited otherwise.  */
7505               if (parent != unit)
7506                 {
7507                   /* Find the highest point of the newly-added tree,
7508                      mark each node along the way, and walk from there.  */
7509                   parent->die_mark = 1;
7510                   while (parent->die_parent
7511                          && parent->die_parent->die_mark == 0)
7512                     {
7513                       parent = parent->die_parent;
7514                       parent->die_mark = 1;
7515                     }
7516                   copy_decls_walk (unit, parent, decl_table);
7517                 }
7518             }
7519         }
7520     }
7521
7522   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7523 }
7524
7525 /* Copy declarations for "unworthy" types into the new comdat section.
7526    Incomplete types, modified types, and certain other types aren't broken
7527    out into comdat sections of their own, so they don't have a signature,
7528    and we need to copy the declaration into the same section so that we
7529    don't have an external reference.  */
7530
7531 static void
7532 copy_decls_for_unworthy_types (dw_die_ref unit)
7533 {
7534   htab_t decl_table;
7535
7536   mark_dies (unit);
7537   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7538   copy_decls_walk (unit, unit, decl_table);
7539   htab_delete (decl_table);
7540   unmark_dies (unit);
7541 }
7542
7543 /* Traverse the DIE and add a sibling attribute if it may have the
7544    effect of speeding up access to siblings.  To save some space,
7545    avoid generating sibling attributes for DIE's without children.  */
7546
7547 static void
7548 add_sibling_attributes (dw_die_ref die)
7549 {
7550   dw_die_ref c;
7551
7552   if (! die->die_child)
7553     return;
7554
7555   if (die->die_parent && die != die->die_parent->die_child)
7556     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7557
7558   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7559 }
7560
7561 /* Output all location lists for the DIE and its children.  */
7562
7563 static void
7564 output_location_lists (dw_die_ref die)
7565 {
7566   dw_die_ref c;
7567   dw_attr_ref a;
7568   unsigned ix;
7569
7570   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7571     if (AT_class (a) == dw_val_class_loc_list)
7572       output_loc_list (AT_loc_list (a));
7573
7574   FOR_EACH_CHILD (die, c, output_location_lists (c));
7575 }
7576
7577 /* The format of each DIE (and its attribute value pairs) is encoded in an
7578    abbreviation table.  This routine builds the abbreviation table and assigns
7579    a unique abbreviation id for each abbreviation entry.  The children of each
7580    die are visited recursively.  */
7581
7582 static void
7583 build_abbrev_table (dw_die_ref die)
7584 {
7585   unsigned long abbrev_id;
7586   unsigned int n_alloc;
7587   dw_die_ref c;
7588   dw_attr_ref a;
7589   unsigned ix;
7590
7591   /* Scan the DIE references, and mark as external any that refer to
7592      DIEs from other CUs (i.e. those which are not marked).  */
7593   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7594     if (AT_class (a) == dw_val_class_die_ref
7595         && AT_ref (a)->die_mark == 0)
7596       {
7597         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7598         set_AT_ref_external (a, 1);
7599       }
7600
7601   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7602     {
7603       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7604       dw_attr_ref die_a, abbrev_a;
7605       unsigned ix;
7606       bool ok = true;
7607
7608       if (abbrev->die_tag != die->die_tag)
7609         continue;
7610       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7611         continue;
7612
7613       if (VEC_length (dw_attr_node, abbrev->die_attr)
7614           != VEC_length (dw_attr_node, die->die_attr))
7615         continue;
7616
7617       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7618         {
7619           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7620           if ((abbrev_a->dw_attr != die_a->dw_attr)
7621               || (value_format (abbrev_a) != value_format (die_a)))
7622             {
7623               ok = false;
7624               break;
7625             }
7626         }
7627       if (ok)
7628         break;
7629     }
7630
7631   if (abbrev_id >= abbrev_die_table_in_use)
7632     {
7633       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7634         {
7635           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7636           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7637                                             n_alloc);
7638
7639           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7640                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7641           abbrev_die_table_allocated = n_alloc;
7642         }
7643
7644       ++abbrev_die_table_in_use;
7645       abbrev_die_table[abbrev_id] = die;
7646     }
7647
7648   die->die_abbrev = abbrev_id;
7649   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7650 }
7651 \f
7652 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7653
7654 static int
7655 constant_size (unsigned HOST_WIDE_INT value)
7656 {
7657   int log;
7658
7659   if (value == 0)
7660     log = 0;
7661   else
7662     log = floor_log2 (value);
7663
7664   log = log / 8;
7665   log = 1 << (floor_log2 (log) + 1);
7666
7667   return log;
7668 }
7669
7670 /* Return the size of a DIE as it is represented in the
7671    .debug_info section.  */
7672
7673 static unsigned long
7674 size_of_die (dw_die_ref die)
7675 {
7676   unsigned long size = 0;
7677   dw_attr_ref a;
7678   unsigned ix;
7679
7680   size += size_of_uleb128 (die->die_abbrev);
7681   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7682     {
7683       switch (AT_class (a))
7684         {
7685         case dw_val_class_addr:
7686           size += DWARF2_ADDR_SIZE;
7687           break;
7688         case dw_val_class_offset:
7689           size += DWARF_OFFSET_SIZE;
7690           break;
7691         case dw_val_class_loc:
7692           {
7693             unsigned long lsize = size_of_locs (AT_loc (a));
7694
7695             /* Block length.  */
7696             if (dwarf_version >= 4)
7697               size += size_of_uleb128 (lsize);
7698             else
7699               size += constant_size (lsize);
7700             size += lsize;
7701           }
7702           break;
7703         case dw_val_class_loc_list:
7704           size += DWARF_OFFSET_SIZE;
7705           break;
7706         case dw_val_class_range_list:
7707           size += DWARF_OFFSET_SIZE;
7708           break;
7709         case dw_val_class_const:
7710           size += size_of_sleb128 (AT_int (a));
7711           break;
7712         case dw_val_class_unsigned_const:
7713           {
7714             int csize = constant_size (AT_unsigned (a));
7715             if (dwarf_version == 3
7716                 && a->dw_attr == DW_AT_data_member_location
7717                 && csize >= 4)
7718               size += size_of_uleb128 (AT_unsigned (a));
7719             else
7720               size += csize;
7721           }
7722           break;
7723         case dw_val_class_const_double:
7724           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7725           if (HOST_BITS_PER_WIDE_INT >= 64)
7726             size++; /* block */
7727           break;
7728         case dw_val_class_vec:
7729           size += constant_size (a->dw_attr_val.v.val_vec.length
7730                                  * a->dw_attr_val.v.val_vec.elt_size)
7731                   + a->dw_attr_val.v.val_vec.length
7732                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7733           break;
7734         case dw_val_class_flag:
7735           if (dwarf_version >= 4)
7736             /* Currently all add_AT_flag calls pass in 1 as last argument,
7737                so DW_FORM_flag_present can be used.  If that ever changes,
7738                we'll need to use DW_FORM_flag and have some optimization
7739                in build_abbrev_table that will change those to
7740                DW_FORM_flag_present if it is set to 1 in all DIEs using
7741                the same abbrev entry.  */
7742             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7743           else
7744             size += 1;
7745           break;
7746         case dw_val_class_die_ref:
7747           if (AT_ref_external (a))
7748             {
7749               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7750                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7751                  is sized by target address length, whereas in DWARF3
7752                  it's always sized as an offset.  */
7753               if (use_debug_types)
7754                 size += DWARF_TYPE_SIGNATURE_SIZE;
7755               else if (dwarf_version == 2)
7756                 size += DWARF2_ADDR_SIZE;
7757               else
7758                 size += DWARF_OFFSET_SIZE;
7759             }
7760           else
7761             size += DWARF_OFFSET_SIZE;
7762           break;
7763         case dw_val_class_fde_ref:
7764           size += DWARF_OFFSET_SIZE;
7765           break;
7766         case dw_val_class_lbl_id:
7767           size += DWARF2_ADDR_SIZE;
7768           break;
7769         case dw_val_class_lineptr:
7770         case dw_val_class_macptr:
7771           size += DWARF_OFFSET_SIZE;
7772           break;
7773         case dw_val_class_str:
7774           if (AT_string_form (a) == DW_FORM_strp)
7775             size += DWARF_OFFSET_SIZE;
7776           else
7777             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7778           break;
7779         case dw_val_class_file:
7780           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7781           break;
7782         case dw_val_class_data8:
7783           size += 8;
7784           break;
7785         case dw_val_class_vms_delta:
7786           size += DWARF_OFFSET_SIZE;
7787           break;
7788         default:
7789           gcc_unreachable ();
7790         }
7791     }
7792
7793   return size;
7794 }
7795
7796 /* Size the debugging information associated with a given DIE.  Visits the
7797    DIE's children recursively.  Updates the global variable next_die_offset, on
7798    each time through.  Uses the current value of next_die_offset to update the
7799    die_offset field in each DIE.  */
7800
7801 static void
7802 calc_die_sizes (dw_die_ref die)
7803 {
7804   dw_die_ref c;
7805
7806   gcc_assert (die->die_offset == 0
7807               || (unsigned long int) die->die_offset == next_die_offset);
7808   die->die_offset = next_die_offset;
7809   next_die_offset += size_of_die (die);
7810
7811   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7812
7813   if (die->die_child != NULL)
7814     /* Count the null byte used to terminate sibling lists.  */
7815     next_die_offset += 1;
7816 }
7817
7818 /* Size just the base type children at the start of the CU.
7819    This is needed because build_abbrev needs to size locs
7820    and sizing of type based stack ops needs to know die_offset
7821    values for the base types.  */
7822
7823 static void
7824 calc_base_type_die_sizes (void)
7825 {
7826   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7827   unsigned int i;
7828   dw_die_ref base_type;
7829 #if ENABLE_ASSERT_CHECKING
7830   dw_die_ref prev = comp_unit_die ()->die_child;
7831 #endif
7832
7833   die_offset += size_of_die (comp_unit_die ());
7834   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7835     {
7836 #if ENABLE_ASSERT_CHECKING
7837       gcc_assert (base_type->die_offset == 0
7838                   && prev->die_sib == base_type
7839                   && base_type->die_child == NULL
7840                   && base_type->die_abbrev);
7841       prev = base_type;
7842 #endif
7843       base_type->die_offset = die_offset;
7844       die_offset += size_of_die (base_type);
7845     }
7846 }
7847
7848 /* Set the marks for a die and its children.  We do this so
7849    that we know whether or not a reference needs to use FORM_ref_addr; only
7850    DIEs in the same CU will be marked.  We used to clear out the offset
7851    and use that as the flag, but ran into ordering problems.  */
7852
7853 static void
7854 mark_dies (dw_die_ref die)
7855 {
7856   dw_die_ref c;
7857
7858   gcc_assert (!die->die_mark);
7859
7860   die->die_mark = 1;
7861   FOR_EACH_CHILD (die, c, mark_dies (c));
7862 }
7863
7864 /* Clear the marks for a die and its children.  */
7865
7866 static void
7867 unmark_dies (dw_die_ref die)
7868 {
7869   dw_die_ref c;
7870
7871   if (! use_debug_types)
7872     gcc_assert (die->die_mark);
7873
7874   die->die_mark = 0;
7875   FOR_EACH_CHILD (die, c, unmark_dies (c));
7876 }
7877
7878 /* Clear the marks for a die, its children and referred dies.  */
7879
7880 static void
7881 unmark_all_dies (dw_die_ref die)
7882 {
7883   dw_die_ref c;
7884   dw_attr_ref a;
7885   unsigned ix;
7886
7887   if (!die->die_mark)
7888     return;
7889   die->die_mark = 0;
7890
7891   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7892
7893   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7894     if (AT_class (a) == dw_val_class_die_ref)
7895       unmark_all_dies (AT_ref (a));
7896 }
7897
7898 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7899    generated for the compilation unit.  */
7900
7901 static unsigned long
7902 size_of_pubnames (VEC (pubname_entry, gc) * names)
7903 {
7904   unsigned long size;
7905   unsigned i;
7906   pubname_ref p;
7907
7908   size = DWARF_PUBNAMES_HEADER_SIZE;
7909   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7910     if (names != pubtype_table
7911         || p->die->die_offset != 0
7912         || !flag_eliminate_unused_debug_types)
7913       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7914
7915   size += DWARF_OFFSET_SIZE;
7916   return size;
7917 }
7918
7919 /* Return the size of the information in the .debug_aranges section.  */
7920
7921 static unsigned long
7922 size_of_aranges (void)
7923 {
7924   unsigned long size;
7925
7926   size = DWARF_ARANGES_HEADER_SIZE;
7927
7928   /* Count the address/length pair for this compilation unit.  */
7929   if (text_section_used)
7930     size += 2 * DWARF2_ADDR_SIZE;
7931   if (cold_text_section_used)
7932     size += 2 * DWARF2_ADDR_SIZE;
7933   if (have_multiple_function_sections)
7934     {
7935       unsigned fde_idx;
7936       dw_fde_ref fde;
7937
7938       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7939         {
7940           if (!fde->in_std_section)
7941             size += 2 * DWARF2_ADDR_SIZE;
7942           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7943             size += 2 * DWARF2_ADDR_SIZE;
7944         }
7945     }
7946
7947   /* Count the two zero words used to terminated the address range table.  */
7948   size += 2 * DWARF2_ADDR_SIZE;
7949   return size;
7950 }
7951 \f
7952 /* Select the encoding of an attribute value.  */
7953
7954 static enum dwarf_form
7955 value_format (dw_attr_ref a)
7956 {
7957   switch (a->dw_attr_val.val_class)
7958     {
7959     case dw_val_class_addr:
7960       /* Only very few attributes allow DW_FORM_addr.  */
7961       switch (a->dw_attr)
7962         {
7963         case DW_AT_low_pc:
7964         case DW_AT_high_pc:
7965         case DW_AT_entry_pc:
7966         case DW_AT_trampoline:
7967           return DW_FORM_addr;
7968         default:
7969           break;
7970         }
7971       switch (DWARF2_ADDR_SIZE)
7972         {
7973         case 1:
7974           return DW_FORM_data1;
7975         case 2:
7976           return DW_FORM_data2;
7977         case 4:
7978           return DW_FORM_data4;
7979         case 8:
7980           return DW_FORM_data8;
7981         default:
7982           gcc_unreachable ();
7983         }
7984     case dw_val_class_range_list:
7985     case dw_val_class_loc_list:
7986       if (dwarf_version >= 4)
7987         return DW_FORM_sec_offset;
7988       /* FALLTHRU */
7989     case dw_val_class_vms_delta:
7990     case dw_val_class_offset:
7991       switch (DWARF_OFFSET_SIZE)
7992         {
7993         case 4:
7994           return DW_FORM_data4;
7995         case 8:
7996           return DW_FORM_data8;
7997         default:
7998           gcc_unreachable ();
7999         }
8000     case dw_val_class_loc:
8001       if (dwarf_version >= 4)
8002         return DW_FORM_exprloc;
8003       switch (constant_size (size_of_locs (AT_loc (a))))
8004         {
8005         case 1:
8006           return DW_FORM_block1;
8007         case 2:
8008           return DW_FORM_block2;
8009         default:
8010           gcc_unreachable ();
8011         }
8012     case dw_val_class_const:
8013       return DW_FORM_sdata;
8014     case dw_val_class_unsigned_const:
8015       switch (constant_size (AT_unsigned (a)))
8016         {
8017         case 1:
8018           return DW_FORM_data1;
8019         case 2:
8020           return DW_FORM_data2;
8021         case 4:
8022           /* In DWARF3 DW_AT_data_member_location with
8023              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8024              constant, so we need to use DW_FORM_udata if we need
8025              a large constant.  */
8026           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8027             return DW_FORM_udata;
8028           return DW_FORM_data4;
8029         case 8:
8030           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8031             return DW_FORM_udata;
8032           return DW_FORM_data8;
8033         default:
8034           gcc_unreachable ();
8035         }
8036     case dw_val_class_const_double:
8037       switch (HOST_BITS_PER_WIDE_INT)
8038         {
8039         case 8:
8040           return DW_FORM_data2;
8041         case 16:
8042           return DW_FORM_data4;
8043         case 32:
8044           return DW_FORM_data8;
8045         case 64:
8046         default:
8047           return DW_FORM_block1;
8048         }
8049     case dw_val_class_vec:
8050       switch (constant_size (a->dw_attr_val.v.val_vec.length
8051                              * a->dw_attr_val.v.val_vec.elt_size))
8052         {
8053         case 1:
8054           return DW_FORM_block1;
8055         case 2:
8056           return DW_FORM_block2;
8057         case 4:
8058           return DW_FORM_block4;
8059         default:
8060           gcc_unreachable ();
8061         }
8062     case dw_val_class_flag:
8063       if (dwarf_version >= 4)
8064         {
8065           /* Currently all add_AT_flag calls pass in 1 as last argument,
8066              so DW_FORM_flag_present can be used.  If that ever changes,
8067              we'll need to use DW_FORM_flag and have some optimization
8068              in build_abbrev_table that will change those to
8069              DW_FORM_flag_present if it is set to 1 in all DIEs using
8070              the same abbrev entry.  */
8071           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8072           return DW_FORM_flag_present;
8073         }
8074       return DW_FORM_flag;
8075     case dw_val_class_die_ref:
8076       if (AT_ref_external (a))
8077         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8078       else
8079         return DW_FORM_ref;
8080     case dw_val_class_fde_ref:
8081       return DW_FORM_data;
8082     case dw_val_class_lbl_id:
8083       return DW_FORM_addr;
8084     case dw_val_class_lineptr:
8085     case dw_val_class_macptr:
8086       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8087     case dw_val_class_str:
8088       return AT_string_form (a);
8089     case dw_val_class_file:
8090       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8091         {
8092         case 1:
8093           return DW_FORM_data1;
8094         case 2:
8095           return DW_FORM_data2;
8096         case 4:
8097           return DW_FORM_data4;
8098         default:
8099           gcc_unreachable ();
8100         }
8101
8102     case dw_val_class_data8:
8103       return DW_FORM_data8;
8104
8105     default:
8106       gcc_unreachable ();
8107     }
8108 }
8109
8110 /* Output the encoding of an attribute value.  */
8111
8112 static void
8113 output_value_format (dw_attr_ref a)
8114 {
8115   enum dwarf_form form = value_format (a);
8116
8117   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8118 }
8119
8120 /* Output the .debug_abbrev section which defines the DIE abbreviation
8121    table.  */
8122
8123 static void
8124 output_abbrev_section (void)
8125 {
8126   unsigned long abbrev_id;
8127
8128   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8129     {
8130       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8131       unsigned ix;
8132       dw_attr_ref a_attr;
8133
8134       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8135       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8136                                    dwarf_tag_name (abbrev->die_tag));
8137
8138       if (abbrev->die_child != NULL)
8139         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8140       else
8141         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8142
8143       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8144            ix++)
8145         {
8146           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8147                                        dwarf_attr_name (a_attr->dw_attr));
8148           output_value_format (a_attr);
8149         }
8150
8151       dw2_asm_output_data (1, 0, NULL);
8152       dw2_asm_output_data (1, 0, NULL);
8153     }
8154
8155   /* Terminate the table.  */
8156   dw2_asm_output_data (1, 0, NULL);
8157 }
8158
8159 /* Output a symbol we can use to refer to this DIE from another CU.  */
8160
8161 static inline void
8162 output_die_symbol (dw_die_ref die)
8163 {
8164   char *sym = die->die_id.die_symbol;
8165
8166   if (sym == 0)
8167     return;
8168
8169   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8170     /* We make these global, not weak; if the target doesn't support
8171        .linkonce, it doesn't support combining the sections, so debugging
8172        will break.  */
8173     targetm.asm_out.globalize_label (asm_out_file, sym);
8174
8175   ASM_OUTPUT_LABEL (asm_out_file, sym);
8176 }
8177
8178 /* Return a new location list, given the begin and end range, and the
8179    expression.  */
8180
8181 static inline dw_loc_list_ref
8182 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8183               const char *section)
8184 {
8185   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8186
8187   retlist->begin = begin;
8188   retlist->end = end;
8189   retlist->expr = expr;
8190   retlist->section = section;
8191
8192   return retlist;
8193 }
8194
8195 /* Generate a new internal symbol for this location list node, if it
8196    hasn't got one yet.  */
8197
8198 static inline void
8199 gen_llsym (dw_loc_list_ref list)
8200 {
8201   gcc_assert (!list->ll_symbol);
8202   list->ll_symbol = gen_internal_sym ("LLST");
8203 }
8204
8205 /* Output the location list given to us.  */
8206
8207 static void
8208 output_loc_list (dw_loc_list_ref list_head)
8209 {
8210   dw_loc_list_ref curr = list_head;
8211
8212   if (list_head->emitted)
8213     return;
8214   list_head->emitted = true;
8215
8216   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8217
8218   /* Walk the location list, and output each range + expression.  */
8219   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8220     {
8221       unsigned long size;
8222       /* Don't output an entry that starts and ends at the same address.  */
8223       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8224         continue;
8225       size = size_of_locs (curr->expr);
8226       /* If the expression is too large, drop it on the floor.  We could
8227          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8228          in the expression, but >= 64KB expressions for a single value
8229          in a single range are unlikely very useful.  */
8230       if (size > 0xffff)
8231         continue;
8232       if (!have_multiple_function_sections)
8233         {
8234           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8235                                 "Location list begin address (%s)",
8236                                 list_head->ll_symbol);
8237           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8238                                 "Location list end address (%s)",
8239                                 list_head->ll_symbol);
8240         }
8241       else
8242         {
8243           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8244                                "Location list begin address (%s)",
8245                                list_head->ll_symbol);
8246           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8247                                "Location list end address (%s)",
8248                                list_head->ll_symbol);
8249         }
8250
8251       /* Output the block length for this list of location operations.  */
8252       gcc_assert (size <= 0xffff);
8253       dw2_asm_output_data (2, size, "%s", "Location expression size");
8254
8255       output_loc_sequence (curr->expr, -1);
8256     }
8257
8258   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8259                        "Location list terminator begin (%s)",
8260                        list_head->ll_symbol);
8261   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8262                        "Location list terminator end (%s)",
8263                        list_head->ll_symbol);
8264 }
8265
8266 /* Output a type signature.  */
8267
8268 static inline void
8269 output_signature (const char *sig, const char *name)
8270 {
8271   int i;
8272
8273   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8274     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8275 }
8276
8277 /* Output the DIE and its attributes.  Called recursively to generate
8278    the definitions of each child DIE.  */
8279
8280 static void
8281 output_die (dw_die_ref die)
8282 {
8283   dw_attr_ref a;
8284   dw_die_ref c;
8285   unsigned long size;
8286   unsigned ix;
8287
8288   /* If someone in another CU might refer to us, set up a symbol for
8289      them to point to.  */
8290   if (! use_debug_types && die->die_id.die_symbol)
8291     output_die_symbol (die);
8292
8293   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8294                                (unsigned long)die->die_offset,
8295                                dwarf_tag_name (die->die_tag));
8296
8297   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8298     {
8299       const char *name = dwarf_attr_name (a->dw_attr);
8300
8301       switch (AT_class (a))
8302         {
8303         case dw_val_class_addr:
8304           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8305           break;
8306
8307         case dw_val_class_offset:
8308           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8309                                "%s", name);
8310           break;
8311
8312         case dw_val_class_range_list:
8313           {
8314             char *p = strchr (ranges_section_label, '\0');
8315
8316             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8317                      a->dw_attr_val.v.val_offset);
8318             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8319                                    debug_ranges_section, "%s", name);
8320             *p = '\0';
8321           }
8322           break;
8323
8324         case dw_val_class_loc:
8325           size = size_of_locs (AT_loc (a));
8326
8327           /* Output the block length for this list of location operations.  */
8328           if (dwarf_version >= 4)
8329             dw2_asm_output_data_uleb128 (size, "%s", name);
8330           else
8331             dw2_asm_output_data (constant_size (size), size, "%s", name);
8332
8333           output_loc_sequence (AT_loc (a), -1);
8334           break;
8335
8336         case dw_val_class_const:
8337           /* ??? It would be slightly more efficient to use a scheme like is
8338              used for unsigned constants below, but gdb 4.x does not sign
8339              extend.  Gdb 5.x does sign extend.  */
8340           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8341           break;
8342
8343         case dw_val_class_unsigned_const:
8344           {
8345             int csize = constant_size (AT_unsigned (a));
8346             if (dwarf_version == 3
8347                 && a->dw_attr == DW_AT_data_member_location
8348                 && csize >= 4)
8349               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8350             else
8351               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8352           }
8353           break;
8354
8355         case dw_val_class_const_double:
8356           {
8357             unsigned HOST_WIDE_INT first, second;
8358
8359             if (HOST_BITS_PER_WIDE_INT >= 64)
8360               dw2_asm_output_data (1,
8361                                    2 * HOST_BITS_PER_WIDE_INT
8362                                    / HOST_BITS_PER_CHAR,
8363                                    NULL);
8364
8365             if (WORDS_BIG_ENDIAN)
8366               {
8367                 first = a->dw_attr_val.v.val_double.high;
8368                 second = a->dw_attr_val.v.val_double.low;
8369               }
8370             else
8371               {
8372                 first = a->dw_attr_val.v.val_double.low;
8373                 second = a->dw_attr_val.v.val_double.high;
8374               }
8375
8376             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8377                                  first, name);
8378             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8379                                  second, NULL);
8380           }
8381           break;
8382
8383         case dw_val_class_vec:
8384           {
8385             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8386             unsigned int len = a->dw_attr_val.v.val_vec.length;
8387             unsigned int i;
8388             unsigned char *p;
8389
8390             dw2_asm_output_data (constant_size (len * elt_size),
8391                                  len * elt_size, "%s", name);
8392             if (elt_size > sizeof (HOST_WIDE_INT))
8393               {
8394                 elt_size /= 2;
8395                 len *= 2;
8396               }
8397             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8398                  i < len;
8399                  i++, p += elt_size)
8400               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8401                                    "fp or vector constant word %u", i);
8402             break;
8403           }
8404
8405         case dw_val_class_flag:
8406           if (dwarf_version >= 4)
8407             {
8408               /* Currently all add_AT_flag calls pass in 1 as last argument,
8409                  so DW_FORM_flag_present can be used.  If that ever changes,
8410                  we'll need to use DW_FORM_flag and have some optimization
8411                  in build_abbrev_table that will change those to
8412                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8413                  the same abbrev entry.  */
8414               gcc_assert (AT_flag (a) == 1);
8415               if (flag_debug_asm)
8416                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8417                          ASM_COMMENT_START, name);
8418               break;
8419             }
8420           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8421           break;
8422
8423         case dw_val_class_loc_list:
8424           {
8425             char *sym = AT_loc_list (a)->ll_symbol;
8426
8427             gcc_assert (sym);
8428             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8429                                    "%s", name);
8430           }
8431           break;
8432
8433         case dw_val_class_die_ref:
8434           if (AT_ref_external (a))
8435             {
8436               if (use_debug_types)
8437                 {
8438                   comdat_type_node_ref type_node =
8439                     AT_ref (a)->die_id.die_type_node;
8440
8441                   gcc_assert (type_node);
8442                   output_signature (type_node->signature, name);
8443                 }
8444               else
8445                 {
8446                   char *sym = AT_ref (a)->die_id.die_symbol;
8447                   int size;
8448
8449                   gcc_assert (sym);
8450                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8451                      length, whereas in DWARF3 it's always sized as an
8452                      offset.  */
8453                   if (dwarf_version == 2)
8454                     size = DWARF2_ADDR_SIZE;
8455                   else
8456                     size = DWARF_OFFSET_SIZE;
8457                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8458                                          name);
8459                 }
8460             }
8461           else
8462             {
8463               gcc_assert (AT_ref (a)->die_offset);
8464               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8465                                    "%s", name);
8466             }
8467           break;
8468
8469         case dw_val_class_fde_ref:
8470           {
8471             char l1[20];
8472
8473             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8474                                          a->dw_attr_val.v.val_fde_index * 2);
8475             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8476                                    "%s", name);
8477           }
8478           break;
8479
8480         case dw_val_class_vms_delta:
8481           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8482                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8483                                     "%s", name);
8484           break;
8485
8486         case dw_val_class_lbl_id:
8487           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8488           break;
8489
8490         case dw_val_class_lineptr:
8491           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8492                                  debug_line_section, "%s", name);
8493           break;
8494
8495         case dw_val_class_macptr:
8496           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8497                                  debug_macinfo_section, "%s", name);
8498           break;
8499
8500         case dw_val_class_str:
8501           if (AT_string_form (a) == DW_FORM_strp)
8502             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8503                                    a->dw_attr_val.v.val_str->label,
8504                                    debug_str_section,
8505                                    "%s: \"%s\"", name, AT_string (a));
8506           else
8507             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8508           break;
8509
8510         case dw_val_class_file:
8511           {
8512             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8513
8514             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8515                                  a->dw_attr_val.v.val_file->filename);
8516             break;
8517           }
8518
8519         case dw_val_class_data8:
8520           {
8521             int i;
8522
8523             for (i = 0; i < 8; i++)
8524               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8525                                    i == 0 ? "%s" : NULL, name);
8526             break;
8527           }
8528
8529         default:
8530           gcc_unreachable ();
8531         }
8532     }
8533
8534   FOR_EACH_CHILD (die, c, output_die (c));
8535
8536   /* Add null byte to terminate sibling list.  */
8537   if (die->die_child != NULL)
8538     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8539                          (unsigned long) die->die_offset);
8540 }
8541
8542 /* Output the compilation unit that appears at the beginning of the
8543    .debug_info section, and precedes the DIE descriptions.  */
8544
8545 static void
8546 output_compilation_unit_header (void)
8547 {
8548   int ver = dwarf_version;
8549
8550   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8551     dw2_asm_output_data (4, 0xffffffff,
8552       "Initial length escape value indicating 64-bit DWARF extension");
8553   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8554                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8555                        "Length of Compilation Unit Info");
8556   dw2_asm_output_data (2, ver, "DWARF version number");
8557   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8558                          debug_abbrev_section,
8559                          "Offset Into Abbrev. Section");
8560   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8561 }
8562
8563 /* Output the compilation unit DIE and its children.  */
8564
8565 static void
8566 output_comp_unit (dw_die_ref die, int output_if_empty)
8567 {
8568   const char *secname;
8569   char *oldsym, *tmp;
8570
8571   /* Unless we are outputting main CU, we may throw away empty ones.  */
8572   if (!output_if_empty && die->die_child == NULL)
8573     return;
8574
8575   /* Even if there are no children of this DIE, we must output the information
8576      about the compilation unit.  Otherwise, on an empty translation unit, we
8577      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8578      will then complain when examining the file.  First mark all the DIEs in
8579      this CU so we know which get local refs.  */
8580   mark_dies (die);
8581
8582   build_abbrev_table (die);
8583
8584   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8585   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8586   calc_die_sizes (die);
8587
8588   oldsym = die->die_id.die_symbol;
8589   if (oldsym)
8590     {
8591       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8592
8593       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8594       secname = tmp;
8595       die->die_id.die_symbol = NULL;
8596       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8597     }
8598   else
8599     {
8600       switch_to_section (debug_info_section);
8601       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8602       info_section_emitted = true;
8603     }
8604
8605   /* Output debugging information.  */
8606   output_compilation_unit_header ();
8607   output_die (die);
8608
8609   /* Leave the marks on the main CU, so we can check them in
8610      output_pubnames.  */
8611   if (oldsym)
8612     {
8613       unmark_dies (die);
8614       die->die_id.die_symbol = oldsym;
8615     }
8616 }
8617
8618 /* Output a comdat type unit DIE and its children.  */
8619
8620 static void
8621 output_comdat_type_unit (comdat_type_node *node)
8622 {
8623   const char *secname;
8624   char *tmp;
8625   int i;
8626 #if defined (OBJECT_FORMAT_ELF)
8627   tree comdat_key;
8628 #endif
8629
8630   /* First mark all the DIEs in this CU so we know which get local refs.  */
8631   mark_dies (node->root_die);
8632
8633   build_abbrev_table (node->root_die);
8634
8635   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8636   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8637   calc_die_sizes (node->root_die);
8638
8639 #if defined (OBJECT_FORMAT_ELF)
8640   secname = ".debug_types";
8641   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8642   sprintf (tmp, "wt.");
8643   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8644     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8645   comdat_key = get_identifier (tmp);
8646   targetm.asm_out.named_section (secname,
8647                                  SECTION_DEBUG | SECTION_LINKONCE,
8648                                  comdat_key);
8649 #else
8650   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8651   sprintf (tmp, ".gnu.linkonce.wt.");
8652   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8653     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8654   secname = tmp;
8655   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8656 #endif
8657
8658   /* Output debugging information.  */
8659   output_compilation_unit_header ();
8660   output_signature (node->signature, "Type Signature");
8661   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8662                        "Offset to Type DIE");
8663   output_die (node->root_die);
8664
8665   unmark_dies (node->root_die);
8666 }
8667
8668 /* Return the DWARF2/3 pubname associated with a decl.  */
8669
8670 static const char *
8671 dwarf2_name (tree decl, int scope)
8672 {
8673   if (DECL_NAMELESS (decl))
8674     return NULL;
8675   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8676 }
8677
8678 /* Add a new entry to .debug_pubnames if appropriate.  */
8679
8680 static void
8681 add_pubname_string (const char *str, dw_die_ref die)
8682 {
8683   if (targetm.want_debug_pub_sections)
8684     {
8685       pubname_entry e;
8686
8687       e.die = die;
8688       e.name = xstrdup (str);
8689       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8690     }
8691 }
8692
8693 static void
8694 add_pubname (tree decl, dw_die_ref die)
8695 {
8696   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8697     {
8698       const char *name = dwarf2_name (decl, 1);
8699       if (name)
8700         add_pubname_string (name, die);
8701     }
8702 }
8703
8704 /* Add a new entry to .debug_pubtypes if appropriate.  */
8705
8706 static void
8707 add_pubtype (tree decl, dw_die_ref die)
8708 {
8709   pubname_entry e;
8710
8711   if (!targetm.want_debug_pub_sections)
8712     return;
8713
8714   e.name = NULL;
8715   if ((TREE_PUBLIC (decl)
8716        || is_cu_die (die->die_parent))
8717       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8718     {
8719       e.die = die;
8720       if (TYPE_P (decl))
8721         {
8722           if (TYPE_NAME (decl))
8723             {
8724               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8725                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8726               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8727                        && DECL_NAME (TYPE_NAME (decl)))
8728                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8729               else
8730                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8731             }
8732         }
8733       else
8734         {
8735           e.name = dwarf2_name (decl, 1);
8736           if (e.name)
8737             e.name = xstrdup (e.name);
8738         }
8739
8740       /* If we don't have a name for the type, there's no point in adding
8741          it to the table.  */
8742       if (e.name && e.name[0] != '\0')
8743         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8744     }
8745 }
8746
8747 /* Output the public names table used to speed up access to externally
8748    visible names; or the public types table used to find type definitions.  */
8749
8750 static void
8751 output_pubnames (VEC (pubname_entry, gc) * names)
8752 {
8753   unsigned i;
8754   unsigned long pubnames_length = size_of_pubnames (names);
8755   pubname_ref pub;
8756
8757   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8758     dw2_asm_output_data (4, 0xffffffff,
8759       "Initial length escape value indicating 64-bit DWARF extension");
8760   if (names == pubname_table)
8761     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8762                          "Length of Public Names Info");
8763   else
8764     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8765                          "Length of Public Type Names Info");
8766   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8767   dw2_asm_output_data (2, 2, "DWARF Version");
8768   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8769                          debug_info_section,
8770                          "Offset of Compilation Unit Info");
8771   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8772                        "Compilation Unit Length");
8773
8774   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8775     {
8776       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8777       if (names == pubname_table)
8778         gcc_assert (pub->die->die_mark);
8779
8780       if (names != pubtype_table
8781           || pub->die->die_offset != 0
8782           || !flag_eliminate_unused_debug_types)
8783         {
8784           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8785                                "DIE offset");
8786
8787           dw2_asm_output_nstring (pub->name, -1, "external name");
8788         }
8789     }
8790
8791   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8792 }
8793
8794 /* Output the information that goes into the .debug_aranges table.
8795    Namely, define the beginning and ending address range of the
8796    text section generated for this compilation unit.  */
8797
8798 static void
8799 output_aranges (unsigned long aranges_length)
8800 {
8801   unsigned i;
8802
8803   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8804     dw2_asm_output_data (4, 0xffffffff,
8805       "Initial length escape value indicating 64-bit DWARF extension");
8806   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8807                        "Length of Address Ranges Info");
8808   /* Version number for aranges is still 2, even in DWARF3.  */
8809   dw2_asm_output_data (2, 2, "DWARF Version");
8810   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8811                          debug_info_section,
8812                          "Offset of Compilation Unit Info");
8813   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8814   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8815
8816   /* We need to align to twice the pointer size here.  */
8817   if (DWARF_ARANGES_PAD_SIZE)
8818     {
8819       /* Pad using a 2 byte words so that padding is correct for any
8820          pointer size.  */
8821       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8822                            2 * DWARF2_ADDR_SIZE);
8823       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8824         dw2_asm_output_data (2, 0, NULL);
8825     }
8826
8827   /* It is necessary not to output these entries if the sections were
8828      not used; if the sections were not used, the length will be 0 and
8829      the address may end up as 0 if the section is discarded by ld
8830      --gc-sections, leaving an invalid (0, 0) entry that can be
8831      confused with the terminator.  */
8832   if (text_section_used)
8833     {
8834       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8835       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8836                             text_section_label, "Length");
8837     }
8838   if (cold_text_section_used)
8839     {
8840       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8841                            "Address");
8842       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8843                             cold_text_section_label, "Length");
8844     }
8845
8846   if (have_multiple_function_sections)
8847     {
8848       unsigned fde_idx;
8849       dw_fde_ref fde;
8850
8851       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8852         {
8853           if (!fde->in_std_section)
8854             {
8855               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8856                                    "Address");
8857               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8858                                     fde->dw_fde_begin, "Length");
8859             }
8860           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8861             {
8862               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8863                                    "Address");
8864               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8865                                     fde->dw_fde_second_begin, "Length");
8866             }
8867         }
8868     }
8869
8870   /* Output the terminator words.  */
8871   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8872   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8873 }
8874
8875 /* Add a new entry to .debug_ranges.  Return the offset at which it
8876    was placed.  */
8877
8878 static unsigned int
8879 add_ranges_num (int num)
8880 {
8881   unsigned int in_use = ranges_table_in_use;
8882
8883   if (in_use == ranges_table_allocated)
8884     {
8885       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8886       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8887                                     ranges_table_allocated);
8888       memset (ranges_table + ranges_table_in_use, 0,
8889               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8890     }
8891
8892   ranges_table[in_use].num = num;
8893   ranges_table_in_use = in_use + 1;
8894
8895   return in_use * 2 * DWARF2_ADDR_SIZE;
8896 }
8897
8898 /* Add a new entry to .debug_ranges corresponding to a block, or a
8899    range terminator if BLOCK is NULL.  */
8900
8901 static unsigned int
8902 add_ranges (const_tree block)
8903 {
8904   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8905 }
8906
8907 /* Add a new entry to .debug_ranges corresponding to a pair of
8908    labels.  */
8909
8910 static void
8911 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8912                       bool *added)
8913 {
8914   unsigned int in_use = ranges_by_label_in_use;
8915   unsigned int offset;
8916
8917   if (in_use == ranges_by_label_allocated)
8918     {
8919       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8920       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8921                                        ranges_by_label,
8922                                        ranges_by_label_allocated);
8923       memset (ranges_by_label + ranges_by_label_in_use, 0,
8924               RANGES_TABLE_INCREMENT
8925               * sizeof (struct dw_ranges_by_label_struct));
8926     }
8927
8928   ranges_by_label[in_use].begin = begin;
8929   ranges_by_label[in_use].end = end;
8930   ranges_by_label_in_use = in_use + 1;
8931
8932   offset = add_ranges_num (-(int)in_use - 1);
8933   if (!*added)
8934     {
8935       add_AT_range_list (die, DW_AT_ranges, offset);
8936       *added = true;
8937     }
8938 }
8939
8940 static void
8941 output_ranges (void)
8942 {
8943   unsigned i;
8944   static const char *const start_fmt = "Offset %#x";
8945   const char *fmt = start_fmt;
8946
8947   for (i = 0; i < ranges_table_in_use; i++)
8948     {
8949       int block_num = ranges_table[i].num;
8950
8951       if (block_num > 0)
8952         {
8953           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8954           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8955
8956           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8957           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8958
8959           /* If all code is in the text section, then the compilation
8960              unit base address defaults to DW_AT_low_pc, which is the
8961              base of the text section.  */
8962           if (!have_multiple_function_sections)
8963             {
8964               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8965                                     text_section_label,
8966                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8967               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8968                                     text_section_label, NULL);
8969             }
8970
8971           /* Otherwise, the compilation unit base address is zero,
8972              which allows us to use absolute addresses, and not worry
8973              about whether the target supports cross-section
8974              arithmetic.  */
8975           else
8976             {
8977               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8978                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8979               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8980             }
8981
8982           fmt = NULL;
8983         }
8984
8985       /* Negative block_num stands for an index into ranges_by_label.  */
8986       else if (block_num < 0)
8987         {
8988           int lab_idx = - block_num - 1;
8989
8990           if (!have_multiple_function_sections)
8991             {
8992               gcc_unreachable ();
8993 #if 0
8994               /* If we ever use add_ranges_by_labels () for a single
8995                  function section, all we have to do is to take out
8996                  the #if 0 above.  */
8997               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8998                                     ranges_by_label[lab_idx].begin,
8999                                     text_section_label,
9000                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9001               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9002                                     ranges_by_label[lab_idx].end,
9003                                     text_section_label, NULL);
9004 #endif
9005             }
9006           else
9007             {
9008               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9009                                    ranges_by_label[lab_idx].begin,
9010                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9011               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9012                                    ranges_by_label[lab_idx].end,
9013                                    NULL);
9014             }
9015         }
9016       else
9017         {
9018           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9019           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9020           fmt = start_fmt;
9021         }
9022     }
9023 }
9024
9025 /* Data structure containing information about input files.  */
9026 struct file_info
9027 {
9028   const char *path;     /* Complete file name.  */
9029   const char *fname;    /* File name part.  */
9030   int length;           /* Length of entire string.  */
9031   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9032   int dir_idx;          /* Index in directory table.  */
9033 };
9034
9035 /* Data structure containing information about directories with source
9036    files.  */
9037 struct dir_info
9038 {
9039   const char *path;     /* Path including directory name.  */
9040   int length;           /* Path length.  */
9041   int prefix;           /* Index of directory entry which is a prefix.  */
9042   int count;            /* Number of files in this directory.  */
9043   int dir_idx;          /* Index of directory used as base.  */
9044 };
9045
9046 /* Callback function for file_info comparison.  We sort by looking at
9047    the directories in the path.  */
9048
9049 static int
9050 file_info_cmp (const void *p1, const void *p2)
9051 {
9052   const struct file_info *const s1 = (const struct file_info *) p1;
9053   const struct file_info *const s2 = (const struct file_info *) p2;
9054   const unsigned char *cp1;
9055   const unsigned char *cp2;
9056
9057   /* Take care of file names without directories.  We need to make sure that
9058      we return consistent values to qsort since some will get confused if
9059      we return the same value when identical operands are passed in opposite
9060      orders.  So if neither has a directory, return 0 and otherwise return
9061      1 or -1 depending on which one has the directory.  */
9062   if ((s1->path == s1->fname || s2->path == s2->fname))
9063     return (s2->path == s2->fname) - (s1->path == s1->fname);
9064
9065   cp1 = (const unsigned char *) s1->path;
9066   cp2 = (const unsigned char *) s2->path;
9067
9068   while (1)
9069     {
9070       ++cp1;
9071       ++cp2;
9072       /* Reached the end of the first path?  If so, handle like above.  */
9073       if ((cp1 == (const unsigned char *) s1->fname)
9074           || (cp2 == (const unsigned char *) s2->fname))
9075         return ((cp2 == (const unsigned char *) s2->fname)
9076                 - (cp1 == (const unsigned char *) s1->fname));
9077
9078       /* Character of current path component the same?  */
9079       else if (*cp1 != *cp2)
9080         return *cp1 - *cp2;
9081     }
9082 }
9083
9084 struct file_name_acquire_data
9085 {
9086   struct file_info *files;
9087   int used_files;
9088   int max_files;
9089 };
9090
9091 /* Traversal function for the hash table.  */
9092
9093 static int
9094 file_name_acquire (void ** slot, void *data)
9095 {
9096   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9097   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9098   struct file_info *fi;
9099   const char *f;
9100
9101   gcc_assert (fnad->max_files >= d->emitted_number);
9102
9103   if (! d->emitted_number)
9104     return 1;
9105
9106   gcc_assert (fnad->max_files != fnad->used_files);
9107
9108   fi = fnad->files + fnad->used_files++;
9109
9110   /* Skip all leading "./".  */
9111   f = d->filename;
9112   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9113     f += 2;
9114
9115   /* Create a new array entry.  */
9116   fi->path = f;
9117   fi->length = strlen (f);
9118   fi->file_idx = d;
9119
9120   /* Search for the file name part.  */
9121   f = strrchr (f, DIR_SEPARATOR);
9122 #if defined (DIR_SEPARATOR_2)
9123   {
9124     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9125
9126     if (g != NULL)
9127       {
9128         if (f == NULL || f < g)
9129           f = g;
9130       }
9131   }
9132 #endif
9133
9134   fi->fname = f == NULL ? fi->path : f + 1;
9135   return 1;
9136 }
9137
9138 /* Output the directory table and the file name table.  We try to minimize
9139    the total amount of memory needed.  A heuristic is used to avoid large
9140    slowdowns with many input files.  */
9141
9142 static void
9143 output_file_names (void)
9144 {
9145   struct file_name_acquire_data fnad;
9146   int numfiles;
9147   struct file_info *files;
9148   struct dir_info *dirs;
9149   int *saved;
9150   int *savehere;
9151   int *backmap;
9152   int ndirs;
9153   int idx_offset;
9154   int i;
9155
9156   if (!last_emitted_file)
9157     {
9158       dw2_asm_output_data (1, 0, "End directory table");
9159       dw2_asm_output_data (1, 0, "End file name table");
9160       return;
9161     }
9162
9163   numfiles = last_emitted_file->emitted_number;
9164
9165   /* Allocate the various arrays we need.  */
9166   files = XALLOCAVEC (struct file_info, numfiles);
9167   dirs = XALLOCAVEC (struct dir_info, numfiles);
9168
9169   fnad.files = files;
9170   fnad.used_files = 0;
9171   fnad.max_files = numfiles;
9172   htab_traverse (file_table, file_name_acquire, &fnad);
9173   gcc_assert (fnad.used_files == fnad.max_files);
9174
9175   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9176
9177   /* Find all the different directories used.  */
9178   dirs[0].path = files[0].path;
9179   dirs[0].length = files[0].fname - files[0].path;
9180   dirs[0].prefix = -1;
9181   dirs[0].count = 1;
9182   dirs[0].dir_idx = 0;
9183   files[0].dir_idx = 0;
9184   ndirs = 1;
9185
9186   for (i = 1; i < numfiles; i++)
9187     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9188         && memcmp (dirs[ndirs - 1].path, files[i].path,
9189                    dirs[ndirs - 1].length) == 0)
9190       {
9191         /* Same directory as last entry.  */
9192         files[i].dir_idx = ndirs - 1;
9193         ++dirs[ndirs - 1].count;
9194       }
9195     else
9196       {
9197         int j;
9198
9199         /* This is a new directory.  */
9200         dirs[ndirs].path = files[i].path;
9201         dirs[ndirs].length = files[i].fname - files[i].path;
9202         dirs[ndirs].count = 1;
9203         dirs[ndirs].dir_idx = ndirs;
9204         files[i].dir_idx = ndirs;
9205
9206         /* Search for a prefix.  */
9207         dirs[ndirs].prefix = -1;
9208         for (j = 0; j < ndirs; j++)
9209           if (dirs[j].length < dirs[ndirs].length
9210               && dirs[j].length > 1
9211               && (dirs[ndirs].prefix == -1
9212                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9213               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9214             dirs[ndirs].prefix = j;
9215
9216         ++ndirs;
9217       }
9218
9219   /* Now to the actual work.  We have to find a subset of the directories which
9220      allow expressing the file name using references to the directory table
9221      with the least amount of characters.  We do not do an exhaustive search
9222      where we would have to check out every combination of every single
9223      possible prefix.  Instead we use a heuristic which provides nearly optimal
9224      results in most cases and never is much off.  */
9225   saved = XALLOCAVEC (int, ndirs);
9226   savehere = XALLOCAVEC (int, ndirs);
9227
9228   memset (saved, '\0', ndirs * sizeof (saved[0]));
9229   for (i = 0; i < ndirs; i++)
9230     {
9231       int j;
9232       int total;
9233
9234       /* We can always save some space for the current directory.  But this
9235          does not mean it will be enough to justify adding the directory.  */
9236       savehere[i] = dirs[i].length;
9237       total = (savehere[i] - saved[i]) * dirs[i].count;
9238
9239       for (j = i + 1; j < ndirs; j++)
9240         {
9241           savehere[j] = 0;
9242           if (saved[j] < dirs[i].length)
9243             {
9244               /* Determine whether the dirs[i] path is a prefix of the
9245                  dirs[j] path.  */
9246               int k;
9247
9248               k = dirs[j].prefix;
9249               while (k != -1 && k != (int) i)
9250                 k = dirs[k].prefix;
9251
9252               if (k == (int) i)
9253                 {
9254                   /* Yes it is.  We can possibly save some memory by
9255                      writing the filenames in dirs[j] relative to
9256                      dirs[i].  */
9257                   savehere[j] = dirs[i].length;
9258                   total += (savehere[j] - saved[j]) * dirs[j].count;
9259                 }
9260             }
9261         }
9262
9263       /* Check whether we can save enough to justify adding the dirs[i]
9264          directory.  */
9265       if (total > dirs[i].length + 1)
9266         {
9267           /* It's worthwhile adding.  */
9268           for (j = i; j < ndirs; j++)
9269             if (savehere[j] > 0)
9270               {
9271                 /* Remember how much we saved for this directory so far.  */
9272                 saved[j] = savehere[j];
9273
9274                 /* Remember the prefix directory.  */
9275                 dirs[j].dir_idx = i;
9276               }
9277         }
9278     }
9279
9280   /* Emit the directory name table.  */
9281   idx_offset = dirs[0].length > 0 ? 1 : 0;
9282   for (i = 1 - idx_offset; i < ndirs; i++)
9283     dw2_asm_output_nstring (dirs[i].path,
9284                             dirs[i].length
9285                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9286                             "Directory Entry: %#x", i + idx_offset);
9287
9288   dw2_asm_output_data (1, 0, "End directory table");
9289
9290   /* We have to emit them in the order of emitted_number since that's
9291      used in the debug info generation.  To do this efficiently we
9292      generate a back-mapping of the indices first.  */
9293   backmap = XALLOCAVEC (int, numfiles);
9294   for (i = 0; i < numfiles; i++)
9295     backmap[files[i].file_idx->emitted_number - 1] = i;
9296
9297   /* Now write all the file names.  */
9298   for (i = 0; i < numfiles; i++)
9299     {
9300       int file_idx = backmap[i];
9301       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9302
9303 #ifdef VMS_DEBUGGING_INFO
9304 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9305
9306       /* Setting these fields can lead to debugger miscomparisons,
9307          but VMS Debug requires them to be set correctly.  */
9308
9309       int ver;
9310       long long cdt;
9311       long siz;
9312       int maxfilelen = strlen (files[file_idx].path)
9313                                + dirs[dir_idx].length
9314                                + MAX_VMS_VERSION_LEN + 1;
9315       char *filebuf = XALLOCAVEC (char, maxfilelen);
9316
9317       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9318       snprintf (filebuf, maxfilelen, "%s;%d",
9319                 files[file_idx].path + dirs[dir_idx].length, ver);
9320
9321       dw2_asm_output_nstring
9322         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9323
9324       /* Include directory index.  */
9325       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9326
9327       /* Modification time.  */
9328       dw2_asm_output_data_uleb128
9329         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9330           ? cdt : 0,
9331          NULL);
9332
9333       /* File length in bytes.  */
9334       dw2_asm_output_data_uleb128
9335         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9336           ? siz : 0,
9337          NULL);
9338 #else
9339       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9340                               "File Entry: %#x", (unsigned) i + 1);
9341
9342       /* Include directory index.  */
9343       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9344
9345       /* Modification time.  */
9346       dw2_asm_output_data_uleb128 (0, NULL);
9347
9348       /* File length in bytes.  */
9349       dw2_asm_output_data_uleb128 (0, NULL);
9350 #endif /* VMS_DEBUGGING_INFO */
9351     }
9352
9353   dw2_asm_output_data (1, 0, "End file name table");
9354 }
9355
9356
9357 /* Output one line number table into the .debug_line section.  */
9358
9359 static void
9360 output_one_line_info_table (dw_line_info_table *table)
9361 {
9362   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9363   unsigned int current_line = 1;
9364   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9365   dw_line_info_entry *ent;
9366   size_t i;
9367
9368   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9369     {
9370       switch (ent->opcode)
9371         {
9372         case LI_set_address:
9373           /* ??? Unfortunately, we have little choice here currently, and
9374              must always use the most general form.  GCC does not know the
9375              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9376              ports do have length attributes which will give an upper bound
9377              on the address range.  We could perhaps use length attributes
9378              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9379           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9380
9381           /* This can handle any delta.  This takes
9382              4+DWARF2_ADDR_SIZE bytes.  */
9383           dw2_asm_output_data (1, 0, "set address %s", line_label);
9384           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9385           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9386           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9387           break;
9388
9389         case LI_set_line:
9390           if (ent->val == current_line)
9391             {
9392               /* We still need to start a new row, so output a copy insn.  */
9393               dw2_asm_output_data (1, DW_LNS_copy,
9394                                    "copy line %u", current_line);
9395             }
9396           else
9397             {
9398               int line_offset = ent->val - current_line;
9399               int line_delta = line_offset - DWARF_LINE_BASE;
9400
9401               current_line = ent->val;
9402               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9403                 {
9404                   /* This can handle deltas from -10 to 234, using the current
9405                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9406                      This takes 1 byte.  */
9407                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9408                                        "line %u", current_line);
9409                 }
9410               else
9411                 {
9412                   /* This can handle any delta.  This takes at least 4 bytes,
9413                      depending on the value being encoded.  */
9414                   dw2_asm_output_data (1, DW_LNS_advance_line,
9415                                        "advance to line %u", current_line);
9416                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9417                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9418                 }
9419             }
9420           break;
9421
9422         case LI_set_file:
9423           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9424           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9425           break;
9426
9427         case LI_set_column:
9428           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9429           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9430           break;
9431
9432         case LI_negate_stmt:
9433           current_is_stmt = !current_is_stmt;
9434           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9435                                "is_stmt %d", current_is_stmt);
9436           break;
9437
9438         case LI_set_prologue_end:
9439           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9440                                "set prologue end");
9441           break;
9442           
9443         case LI_set_epilogue_begin:
9444           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9445                                "set epilogue begin");
9446           break;
9447
9448         case LI_set_discriminator:
9449           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9450           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9451           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9452           dw2_asm_output_data_uleb128 (ent->val, NULL);
9453           break;
9454         }
9455     }
9456
9457   /* Emit debug info for the address of the end of the table.  */
9458   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9459   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9460   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9461   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9462
9463   dw2_asm_output_data (1, 0, "end sequence");
9464   dw2_asm_output_data_uleb128 (1, NULL);
9465   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9466 }
9467
9468 /* Output the source line number correspondence information.  This
9469    information goes into the .debug_line section.  */
9470
9471 static void
9472 output_line_info (void)
9473 {
9474   char l1[20], l2[20], p1[20], p2[20];
9475   int ver = dwarf_version;
9476   bool saw_one = false;
9477   int opc;
9478
9479   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9480   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9481   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9482   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9483
9484   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9485     dw2_asm_output_data (4, 0xffffffff,
9486       "Initial length escape value indicating 64-bit DWARF extension");
9487   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9488                         "Length of Source Line Info");
9489   ASM_OUTPUT_LABEL (asm_out_file, l1);
9490
9491   dw2_asm_output_data (2, ver, "DWARF Version");
9492   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9493   ASM_OUTPUT_LABEL (asm_out_file, p1);
9494
9495   /* Define the architecture-dependent minimum instruction length (in bytes).
9496      In this implementation of DWARF, this field is used for information
9497      purposes only.  Since GCC generates assembly language, we have no
9498      a priori knowledge of how many instruction bytes are generated for each
9499      source line, and therefore can use only the DW_LNE_set_address and
9500      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9501      this as '1', which is "correct enough" for all architectures,
9502      and don't let the target override.  */
9503   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9504
9505   if (ver >= 4)
9506     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9507                          "Maximum Operations Per Instruction");
9508   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9509                        "Default is_stmt_start flag");
9510   dw2_asm_output_data (1, DWARF_LINE_BASE,
9511                        "Line Base Value (Special Opcodes)");
9512   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9513                        "Line Range Value (Special Opcodes)");
9514   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9515                        "Special Opcode Base");
9516
9517   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9518     {
9519       int n_op_args;
9520       switch (opc)
9521         {
9522         case DW_LNS_advance_pc:
9523         case DW_LNS_advance_line:
9524         case DW_LNS_set_file:
9525         case DW_LNS_set_column:
9526         case DW_LNS_fixed_advance_pc:
9527         case DW_LNS_set_isa:
9528           n_op_args = 1;
9529           break;
9530         default:
9531           n_op_args = 0;
9532           break;
9533         }
9534
9535       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9536                            opc, n_op_args);
9537     }
9538
9539   /* Write out the information about the files we use.  */
9540   output_file_names ();
9541   ASM_OUTPUT_LABEL (asm_out_file, p2);
9542
9543   if (separate_line_info)
9544     {
9545       dw_line_info_table *table;
9546       size_t i;
9547
9548       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9549         if (table->in_use)
9550           {
9551             output_one_line_info_table (table);
9552             saw_one = true;
9553           }
9554     }
9555   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9556     {
9557       output_one_line_info_table (cold_text_section_line_info);
9558       saw_one = true;
9559     }
9560
9561   /* ??? Some Darwin linkers crash on a .debug_line section with no
9562      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9563      sufficient -- the address column must also be initialized.
9564      Make sure to output at least one set_address/end_sequence pair,
9565      choosing .text since that section is always present.  */
9566   if (text_section_line_info->in_use || !saw_one)
9567     output_one_line_info_table (text_section_line_info);
9568
9569   /* Output the marker for the end of the line number info.  */
9570   ASM_OUTPUT_LABEL (asm_out_file, l2);
9571 }
9572 \f
9573 /* Given a pointer to a tree node for some base type, return a pointer to
9574    a DIE that describes the given type.
9575
9576    This routine must only be called for GCC type nodes that correspond to
9577    Dwarf base (fundamental) types.  */
9578
9579 static dw_die_ref
9580 base_type_die (tree type)
9581 {
9582   dw_die_ref base_type_result;
9583   enum dwarf_type encoding;
9584
9585   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9586     return 0;
9587
9588   /* If this is a subtype that should not be emitted as a subrange type,
9589      use the base type.  See subrange_type_for_debug_p.  */
9590   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9591     type = TREE_TYPE (type);
9592
9593   switch (TREE_CODE (type))
9594     {
9595     case INTEGER_TYPE:
9596       if ((dwarf_version >= 4 || !dwarf_strict)
9597           && TYPE_NAME (type)
9598           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9599           && DECL_IS_BUILTIN (TYPE_NAME (type))
9600           && DECL_NAME (TYPE_NAME (type)))
9601         {
9602           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9603           if (strcmp (name, "char16_t") == 0
9604               || strcmp (name, "char32_t") == 0)
9605             {
9606               encoding = DW_ATE_UTF;
9607               break;
9608             }
9609         }
9610       if (TYPE_STRING_FLAG (type))
9611         {
9612           if (TYPE_UNSIGNED (type))
9613             encoding = DW_ATE_unsigned_char;
9614           else
9615             encoding = DW_ATE_signed_char;
9616         }
9617       else if (TYPE_UNSIGNED (type))
9618         encoding = DW_ATE_unsigned;
9619       else
9620         encoding = DW_ATE_signed;
9621       break;
9622
9623     case REAL_TYPE:
9624       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9625         {
9626           if (dwarf_version >= 3 || !dwarf_strict)
9627             encoding = DW_ATE_decimal_float;
9628           else
9629             encoding = DW_ATE_lo_user;
9630         }
9631       else
9632         encoding = DW_ATE_float;
9633       break;
9634
9635     case FIXED_POINT_TYPE:
9636       if (!(dwarf_version >= 3 || !dwarf_strict))
9637         encoding = DW_ATE_lo_user;
9638       else if (TYPE_UNSIGNED (type))
9639         encoding = DW_ATE_unsigned_fixed;
9640       else
9641         encoding = DW_ATE_signed_fixed;
9642       break;
9643
9644       /* Dwarf2 doesn't know anything about complex ints, so use
9645          a user defined type for it.  */
9646     case COMPLEX_TYPE:
9647       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9648         encoding = DW_ATE_complex_float;
9649       else
9650         encoding = DW_ATE_lo_user;
9651       break;
9652
9653     case BOOLEAN_TYPE:
9654       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9655       encoding = DW_ATE_boolean;
9656       break;
9657
9658     default:
9659       /* No other TREE_CODEs are Dwarf fundamental types.  */
9660       gcc_unreachable ();
9661     }
9662
9663   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9664
9665   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9666                    int_size_in_bytes (type));
9667   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9668
9669   return base_type_result;
9670 }
9671
9672 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9673    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9674
9675 static inline int
9676 is_base_type (tree type)
9677 {
9678   switch (TREE_CODE (type))
9679     {
9680     case ERROR_MARK:
9681     case VOID_TYPE:
9682     case INTEGER_TYPE:
9683     case REAL_TYPE:
9684     case FIXED_POINT_TYPE:
9685     case COMPLEX_TYPE:
9686     case BOOLEAN_TYPE:
9687       return 1;
9688
9689     case ARRAY_TYPE:
9690     case RECORD_TYPE:
9691     case UNION_TYPE:
9692     case QUAL_UNION_TYPE:
9693     case ENUMERAL_TYPE:
9694     case FUNCTION_TYPE:
9695     case METHOD_TYPE:
9696     case POINTER_TYPE:
9697     case REFERENCE_TYPE:
9698     case NULLPTR_TYPE:
9699     case OFFSET_TYPE:
9700     case LANG_TYPE:
9701     case VECTOR_TYPE:
9702       return 0;
9703
9704     default:
9705       gcc_unreachable ();
9706     }
9707
9708   return 0;
9709 }
9710
9711 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9712    node, return the size in bits for the type if it is a constant, or else
9713    return the alignment for the type if the type's size is not constant, or
9714    else return BITS_PER_WORD if the type actually turns out to be an
9715    ERROR_MARK node.  */
9716
9717 static inline unsigned HOST_WIDE_INT
9718 simple_type_size_in_bits (const_tree type)
9719 {
9720   if (TREE_CODE (type) == ERROR_MARK)
9721     return BITS_PER_WORD;
9722   else if (TYPE_SIZE (type) == NULL_TREE)
9723     return 0;
9724   else if (host_integerp (TYPE_SIZE (type), 1))
9725     return tree_low_cst (TYPE_SIZE (type), 1);
9726   else
9727     return TYPE_ALIGN (type);
9728 }
9729
9730 /* Similarly, but return a double_int instead of UHWI.  */
9731
9732 static inline double_int
9733 double_int_type_size_in_bits (const_tree type)
9734 {
9735   if (TREE_CODE (type) == ERROR_MARK)
9736     return uhwi_to_double_int (BITS_PER_WORD);
9737   else if (TYPE_SIZE (type) == NULL_TREE)
9738     return double_int_zero;
9739   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9740     return tree_to_double_int (TYPE_SIZE (type));
9741   else
9742     return uhwi_to_double_int (TYPE_ALIGN (type));
9743 }
9744
9745 /*  Given a pointer to a tree node for a subrange type, return a pointer
9746     to a DIE that describes the given type.  */
9747
9748 static dw_die_ref
9749 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9750 {
9751   dw_die_ref subrange_die;
9752   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9753
9754   if (context_die == NULL)
9755     context_die = comp_unit_die ();
9756
9757   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9758
9759   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9760     {
9761       /* The size of the subrange type and its base type do not match,
9762          so we need to generate a size attribute for the subrange type.  */
9763       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9764     }
9765
9766   if (low)
9767     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9768   if (high)
9769     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9770
9771   return subrange_die;
9772 }
9773
9774 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9775    entry that chains various modifiers in front of the given type.  */
9776
9777 static dw_die_ref
9778 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9779                    dw_die_ref context_die)
9780 {
9781   enum tree_code code = TREE_CODE (type);
9782   dw_die_ref mod_type_die;
9783   dw_die_ref sub_die = NULL;
9784   tree item_type = NULL;
9785   tree qualified_type;
9786   tree name, low, high;
9787
9788   if (code == ERROR_MARK)
9789     return NULL;
9790
9791   /* See if we already have the appropriately qualified variant of
9792      this type.  */
9793   qualified_type
9794     = get_qualified_type (type,
9795                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9796                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9797
9798   if (qualified_type == sizetype
9799       && TYPE_NAME (qualified_type)
9800       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9801     {
9802       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9803
9804       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9805                            && TYPE_PRECISION (t)
9806                            == TYPE_PRECISION (qualified_type)
9807                            && TYPE_UNSIGNED (t)
9808                            == TYPE_UNSIGNED (qualified_type));
9809       qualified_type = t;
9810     }
9811
9812   /* If we do, then we can just use its DIE, if it exists.  */
9813   if (qualified_type)
9814     {
9815       mod_type_die = lookup_type_die (qualified_type);
9816       if (mod_type_die)
9817         return mod_type_die;
9818     }
9819
9820   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9821
9822   /* Handle C typedef types.  */
9823   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9824       && !DECL_ARTIFICIAL (name))
9825     {
9826       tree dtype = TREE_TYPE (name);
9827
9828       if (qualified_type == dtype)
9829         {
9830           /* For a named type, use the typedef.  */
9831           gen_type_die (qualified_type, context_die);
9832           return lookup_type_die (qualified_type);
9833         }
9834       else if (is_const_type < TYPE_READONLY (dtype)
9835                || is_volatile_type < TYPE_VOLATILE (dtype)
9836                || (is_const_type <= TYPE_READONLY (dtype)
9837                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9838                    && DECL_ORIGINAL_TYPE (name) != type))
9839         /* cv-unqualified version of named type.  Just use the unnamed
9840            type to which it refers.  */
9841         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9842                                   is_const_type, is_volatile_type,
9843                                   context_die);
9844       /* Else cv-qualified version of named type; fall through.  */
9845     }
9846
9847   if (is_const_type
9848       /* If both is_const_type and is_volatile_type, prefer the path
9849          which leads to a qualified type.  */
9850       && (!is_volatile_type
9851           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9852           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9853     {
9854       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9855       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9856     }
9857   else if (is_volatile_type)
9858     {
9859       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9860       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9861     }
9862   else if (code == POINTER_TYPE)
9863     {
9864       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9865       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9866                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9867       item_type = TREE_TYPE (type);
9868       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9869         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9870                          TYPE_ADDR_SPACE (item_type));
9871     }
9872   else if (code == REFERENCE_TYPE)
9873     {
9874       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9875         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9876                                 type);
9877       else
9878         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9879       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9880                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9881       item_type = TREE_TYPE (type);
9882       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9883         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9884                          TYPE_ADDR_SPACE (item_type));
9885     }
9886   else if (code == INTEGER_TYPE
9887            && TREE_TYPE (type) != NULL_TREE
9888            && subrange_type_for_debug_p (type, &low, &high))
9889     {
9890       mod_type_die = subrange_type_die (type, low, high, context_die);
9891       item_type = TREE_TYPE (type);
9892     }
9893   else if (is_base_type (type))
9894     mod_type_die = base_type_die (type);
9895   else
9896     {
9897       gen_type_die (type, context_die);
9898
9899       /* We have to get the type_main_variant here (and pass that to the
9900          `lookup_type_die' routine) because the ..._TYPE node we have
9901          might simply be a *copy* of some original type node (where the
9902          copy was created to help us keep track of typedef names) and
9903          that copy might have a different TYPE_UID from the original
9904          ..._TYPE node.  */
9905       if (TREE_CODE (type) != VECTOR_TYPE)
9906         return lookup_type_die (type_main_variant (type));
9907       else
9908         /* Vectors have the debugging information in the type,
9909            not the main variant.  */
9910         return lookup_type_die (type);
9911     }
9912
9913   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9914      don't output a DW_TAG_typedef, since there isn't one in the
9915      user's program; just attach a DW_AT_name to the type.
9916      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9917      if the base type already has the same name.  */
9918   if (name
9919       && ((TREE_CODE (name) != TYPE_DECL
9920            && (qualified_type == TYPE_MAIN_VARIANT (type)
9921                || (!is_const_type && !is_volatile_type)))
9922           || (TREE_CODE (name) == TYPE_DECL
9923               && TREE_TYPE (name) == qualified_type
9924               && DECL_NAME (name))))
9925     {
9926       if (TREE_CODE (name) == TYPE_DECL)
9927         /* Could just call add_name_and_src_coords_attributes here,
9928            but since this is a builtin type it doesn't have any
9929            useful source coordinates anyway.  */
9930         name = DECL_NAME (name);
9931       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9932     }
9933   /* This probably indicates a bug.  */
9934   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9935     {
9936       name = TYPE_NAME (type);
9937       if (name
9938           && TREE_CODE (name) == TYPE_DECL)
9939         name = DECL_NAME (name);
9940       add_name_attribute (mod_type_die,
9941                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9942     }
9943
9944   if (qualified_type)
9945     equate_type_number_to_die (qualified_type, mod_type_die);
9946
9947   if (item_type)
9948     /* We must do this after the equate_type_number_to_die call, in case
9949        this is a recursive type.  This ensures that the modified_type_die
9950        recursion will terminate even if the type is recursive.  Recursive
9951        types are possible in Ada.  */
9952     sub_die = modified_type_die (item_type,
9953                                  TYPE_READONLY (item_type),
9954                                  TYPE_VOLATILE (item_type),
9955                                  context_die);
9956
9957   if (sub_die != NULL)
9958     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9959
9960   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9961   if (TYPE_ARTIFICIAL (type))
9962     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9963
9964   return mod_type_die;
9965 }
9966
9967 /* Generate DIEs for the generic parameters of T.
9968    T must be either a generic type or a generic function.
9969    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9970
9971 static void
9972 gen_generic_params_dies (tree t)
9973 {
9974   tree parms, args;
9975   int parms_num, i;
9976   dw_die_ref die = NULL;
9977
9978   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9979     return;
9980
9981   if (TYPE_P (t))
9982     die = lookup_type_die (t);
9983   else if (DECL_P (t))
9984     die = lookup_decl_die (t);
9985
9986   gcc_assert (die);
9987
9988   parms = lang_hooks.get_innermost_generic_parms (t);
9989   if (!parms)
9990     /* T has no generic parameter. It means T is neither a generic type
9991        or function. End of story.  */
9992     return;
9993
9994   parms_num = TREE_VEC_LENGTH (parms);
9995   args = lang_hooks.get_innermost_generic_args (t);
9996   for (i = 0; i < parms_num; i++)
9997     {
9998       tree parm, arg, arg_pack_elems;
9999
10000       parm = TREE_VEC_ELT (parms, i);
10001       arg = TREE_VEC_ELT (args, i);
10002       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10003       gcc_assert (parm && TREE_VALUE (parm) && arg);
10004
10005       if (parm && TREE_VALUE (parm) && arg)
10006         {
10007           /* If PARM represents a template parameter pack,
10008              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10009              by DW_TAG_template_*_parameter DIEs for the argument
10010              pack elements of ARG. Note that ARG would then be
10011              an argument pack.  */
10012           if (arg_pack_elems)
10013             template_parameter_pack_die (TREE_VALUE (parm),
10014                                          arg_pack_elems,
10015                                          die);
10016           else
10017             generic_parameter_die (TREE_VALUE (parm), arg,
10018                                    true /* Emit DW_AT_name */, die);
10019         }
10020     }
10021 }
10022
10023 /* Create and return a DIE for PARM which should be
10024    the representation of a generic type parameter.
10025    For instance, in the C++ front end, PARM would be a template parameter.
10026    ARG is the argument to PARM.
10027    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10028    name of the PARM.
10029    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10030    as a child node.  */
10031
10032 static dw_die_ref
10033 generic_parameter_die (tree parm, tree arg,
10034                        bool emit_name_p,
10035                        dw_die_ref parent_die)
10036 {
10037   dw_die_ref tmpl_die = NULL;
10038   const char *name = NULL;
10039
10040   if (!parm || !DECL_NAME (parm) || !arg)
10041     return NULL;
10042
10043   /* We support non-type generic parameters and arguments,
10044      type generic parameters and arguments, as well as
10045      generic generic parameters (a.k.a. template template parameters in C++)
10046      and arguments.  */
10047   if (TREE_CODE (parm) == PARM_DECL)
10048     /* PARM is a nontype generic parameter  */
10049     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10050   else if (TREE_CODE (parm) == TYPE_DECL)
10051     /* PARM is a type generic parameter.  */
10052     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10053   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10054     /* PARM is a generic generic parameter.
10055        Its DIE is a GNU extension. It shall have a
10056        DW_AT_name attribute to represent the name of the template template
10057        parameter, and a DW_AT_GNU_template_name attribute to represent the
10058        name of the template template argument.  */
10059     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10060                         parent_die, parm);
10061   else
10062     gcc_unreachable ();
10063
10064   if (tmpl_die)
10065     {
10066       tree tmpl_type;
10067
10068       /* If PARM is a generic parameter pack, it means we are
10069          emitting debug info for a template argument pack element.
10070          In other terms, ARG is a template argument pack element.
10071          In that case, we don't emit any DW_AT_name attribute for
10072          the die.  */
10073       if (emit_name_p)
10074         {
10075           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10076           gcc_assert (name);
10077           add_AT_string (tmpl_die, DW_AT_name, name);
10078         }
10079
10080       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10081         {
10082           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10083              TMPL_DIE should have a child DW_AT_type attribute that is set
10084              to the type of the argument to PARM, which is ARG.
10085              If PARM is a type generic parameter, TMPL_DIE should have a
10086              child DW_AT_type that is set to ARG.  */
10087           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10088           add_type_attribute (tmpl_die, tmpl_type, 0,
10089                               TREE_THIS_VOLATILE (tmpl_type),
10090                               parent_die);
10091         }
10092       else
10093         {
10094           /* So TMPL_DIE is a DIE representing a
10095              a generic generic template parameter, a.k.a template template
10096              parameter in C++ and arg is a template.  */
10097
10098           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10099              to the name of the argument.  */
10100           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10101           if (name)
10102             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10103         }
10104
10105       if (TREE_CODE (parm) == PARM_DECL)
10106         /* So PARM is a non-type generic parameter.
10107            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10108            attribute of TMPL_DIE which value represents the value
10109            of ARG.
10110            We must be careful here:
10111            The value of ARG might reference some function decls.
10112            We might currently be emitting debug info for a generic
10113            type and types are emitted before function decls, we don't
10114            know if the function decls referenced by ARG will actually be
10115            emitted after cgraph computations.
10116            So must defer the generation of the DW_AT_const_value to
10117            after cgraph is ready.  */
10118         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10119     }
10120
10121   return tmpl_die;
10122 }
10123
10124 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10125    PARM_PACK must be a template parameter pack. The returned DIE
10126    will be child DIE of PARENT_DIE.  */
10127
10128 static dw_die_ref
10129 template_parameter_pack_die (tree parm_pack,
10130                              tree parm_pack_args,
10131                              dw_die_ref parent_die)
10132 {
10133   dw_die_ref die;
10134   int j;
10135
10136   gcc_assert (parent_die && parm_pack);
10137
10138   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10139   add_name_and_src_coords_attributes (die, parm_pack);
10140   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10141     generic_parameter_die (parm_pack,
10142                            TREE_VEC_ELT (parm_pack_args, j),
10143                            false /* Don't emit DW_AT_name */,
10144                            die);
10145   return die;
10146 }
10147
10148 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10149    an enumerated type.  */
10150
10151 static inline int
10152 type_is_enum (const_tree type)
10153 {
10154   return TREE_CODE (type) == ENUMERAL_TYPE;
10155 }
10156
10157 /* Return the DBX register number described by a given RTL node.  */
10158
10159 static unsigned int
10160 dbx_reg_number (const_rtx rtl)
10161 {
10162   unsigned regno = REGNO (rtl);
10163
10164   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10165
10166 #ifdef LEAF_REG_REMAP
10167   if (current_function_uses_only_leaf_regs)
10168     {
10169       int leaf_reg = LEAF_REG_REMAP (regno);
10170       if (leaf_reg != -1)
10171         regno = (unsigned) leaf_reg;
10172     }
10173 #endif
10174
10175   return DBX_REGISTER_NUMBER (regno);
10176 }
10177
10178 /* Optionally add a DW_OP_piece term to a location description expression.
10179    DW_OP_piece is only added if the location description expression already
10180    doesn't end with DW_OP_piece.  */
10181
10182 static void
10183 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10184 {
10185   dw_loc_descr_ref loc;
10186
10187   if (*list_head != NULL)
10188     {
10189       /* Find the end of the chain.  */
10190       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10191         ;
10192
10193       if (loc->dw_loc_opc != DW_OP_piece)
10194         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10195     }
10196 }
10197
10198 /* Return a location descriptor that designates a machine register or
10199    zero if there is none.  */
10200
10201 static dw_loc_descr_ref
10202 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10203 {
10204   rtx regs;
10205
10206   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10207     return 0;
10208
10209   /* We only use "frame base" when we're sure we're talking about the
10210      post-prologue local stack frame.  We do this by *not* running
10211      register elimination until this point, and recognizing the special
10212      argument pointer and soft frame pointer rtx's.
10213      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10214   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10215       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10216     {
10217       dw_loc_descr_ref result = NULL;
10218
10219       if (dwarf_version >= 4 || !dwarf_strict)
10220         {
10221           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10222                                        initialized);
10223           if (result)
10224             add_loc_descr (&result,
10225                            new_loc_descr (DW_OP_stack_value, 0, 0));
10226         }
10227       return result;
10228     }
10229
10230   regs = targetm.dwarf_register_span (rtl);
10231
10232   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10233     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10234   else
10235     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10236 }
10237
10238 /* Return a location descriptor that designates a machine register for
10239    a given hard register number.  */
10240
10241 static dw_loc_descr_ref
10242 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10243 {
10244   dw_loc_descr_ref reg_loc_descr;
10245
10246   if (regno <= 31)
10247     reg_loc_descr
10248       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10249   else
10250     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10251
10252   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10253     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10254
10255   return reg_loc_descr;
10256 }
10257
10258 /* Given an RTL of a register, return a location descriptor that
10259    designates a value that spans more than one register.  */
10260
10261 static dw_loc_descr_ref
10262 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10263                              enum var_init_status initialized)
10264 {
10265   int nregs, size, i;
10266   unsigned reg;
10267   dw_loc_descr_ref loc_result = NULL;
10268
10269   reg = REGNO (rtl);
10270 #ifdef LEAF_REG_REMAP
10271   if (current_function_uses_only_leaf_regs)
10272     {
10273       int leaf_reg = LEAF_REG_REMAP (reg);
10274       if (leaf_reg != -1)
10275         reg = (unsigned) leaf_reg;
10276     }
10277 #endif
10278   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10279   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10280
10281   /* Simple, contiguous registers.  */
10282   if (regs == NULL_RTX)
10283     {
10284       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10285
10286       loc_result = NULL;
10287       while (nregs--)
10288         {
10289           dw_loc_descr_ref t;
10290
10291           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10292                                       VAR_INIT_STATUS_INITIALIZED);
10293           add_loc_descr (&loc_result, t);
10294           add_loc_descr_op_piece (&loc_result, size);
10295           ++reg;
10296         }
10297       return loc_result;
10298     }
10299
10300   /* Now onto stupid register sets in non contiguous locations.  */
10301
10302   gcc_assert (GET_CODE (regs) == PARALLEL);
10303
10304   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10305   loc_result = NULL;
10306
10307   for (i = 0; i < XVECLEN (regs, 0); ++i)
10308     {
10309       dw_loc_descr_ref t;
10310
10311       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10312                                   VAR_INIT_STATUS_INITIALIZED);
10313       add_loc_descr (&loc_result, t);
10314       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10315       add_loc_descr_op_piece (&loc_result, size);
10316     }
10317
10318   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10319     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10320   return loc_result;
10321 }
10322
10323 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10324
10325 /* Return a location descriptor that designates a constant i,
10326    as a compound operation from constant (i >> shift), constant shift
10327    and DW_OP_shl.  */
10328
10329 static dw_loc_descr_ref
10330 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10331 {
10332   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10333   add_loc_descr (&ret, int_loc_descriptor (shift));
10334   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10335   return ret;
10336 }
10337
10338 /* Return a location descriptor that designates a constant.  */
10339
10340 static dw_loc_descr_ref
10341 int_loc_descriptor (HOST_WIDE_INT i)
10342 {
10343   enum dwarf_location_atom op;
10344
10345   /* Pick the smallest representation of a constant, rather than just
10346      defaulting to the LEB encoding.  */
10347   if (i >= 0)
10348     {
10349       int clz = clz_hwi (i);
10350       int ctz = ctz_hwi (i);
10351       if (i <= 31)
10352         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10353       else if (i <= 0xff)
10354         op = DW_OP_const1u;
10355       else if (i <= 0xffff)
10356         op = DW_OP_const2u;
10357       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10358                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10359         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10360            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10361            while DW_OP_const4u is 5 bytes.  */
10362         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10363       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10364                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10365         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10366            while DW_OP_const4u is 5 bytes.  */
10367         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10368       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10369         op = DW_OP_const4u;
10370       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10371                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10372         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10373            while DW_OP_constu of constant >= 0x100000000 takes at least
10374            6 bytes.  */
10375         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10376       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10377                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10378                   >= HOST_BITS_PER_WIDE_INT)
10379         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10380            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10381            while DW_OP_constu takes in this case at least 6 bytes.  */
10382         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10383       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10384                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10385                && size_of_uleb128 (i) > 6)
10386         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10387         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10388       else
10389         op = DW_OP_constu;
10390     }
10391   else
10392     {
10393       if (i >= -0x80)
10394         op = DW_OP_const1s;
10395       else if (i >= -0x8000)
10396         op = DW_OP_const2s;
10397       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10398         {
10399           if (size_of_int_loc_descriptor (i) < 5)
10400             {
10401               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10402               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10403               return ret;
10404             }
10405           op = DW_OP_const4s;
10406         }
10407       else
10408         {
10409           if (size_of_int_loc_descriptor (i)
10410               < (unsigned long) 1 + size_of_sleb128 (i))
10411             {
10412               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10413               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10414               return ret;
10415             }
10416           op = DW_OP_consts;
10417         }
10418     }
10419
10420   return new_loc_descr (op, i, 0);
10421 }
10422
10423 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10424    without actually allocating it.  */
10425
10426 static unsigned long
10427 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10428 {
10429   return size_of_int_loc_descriptor (i >> shift)
10430          + size_of_int_loc_descriptor (shift)
10431          + 1;
10432 }
10433
10434 /* Return size_of_locs (int_loc_descriptor (i)) without
10435    actually allocating it.  */
10436
10437 static unsigned long
10438 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10439 {
10440   unsigned long s;
10441
10442   if (i >= 0)
10443     {
10444       int clz, ctz;
10445       if (i <= 31)
10446         return 1;
10447       else if (i <= 0xff)
10448         return 2;
10449       else if (i <= 0xffff)
10450         return 3;
10451       clz = clz_hwi (i);
10452       ctz = ctz_hwi (i);
10453       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10454           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10455         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10456                                                     - clz - 5);
10457       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10458                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10459         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10460                                                     - clz - 8);
10461       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10462         return 5;
10463       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10464       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10465           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10466         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10467                                                     - clz - 8);
10468       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10469                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10470         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10471                                                     - clz - 16);
10472       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10473                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10474                && s > 6)
10475         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10476                                                     - clz - 32);
10477       else
10478         return 1 + s;
10479     }
10480   else
10481     {
10482       if (i >= -0x80)
10483         return 2;
10484       else if (i >= -0x8000)
10485         return 3;
10486       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10487         {
10488           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10489             {
10490               s = size_of_int_loc_descriptor (-i) + 1;
10491               if (s < 5)
10492                 return s;
10493             }
10494           return 5;
10495         }
10496       else
10497         {
10498           unsigned long r = 1 + size_of_sleb128 (i);
10499           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10500             {
10501               s = size_of_int_loc_descriptor (-i) + 1;
10502               if (s < r)
10503                 return s;
10504             }
10505           return r;
10506         }
10507     }
10508 }
10509
10510 /* Return loc description representing "address" of integer value.
10511    This can appear only as toplevel expression.  */
10512
10513 static dw_loc_descr_ref
10514 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10515 {
10516   int litsize;
10517   dw_loc_descr_ref loc_result = NULL;
10518
10519   if (!(dwarf_version >= 4 || !dwarf_strict))
10520     return NULL;
10521
10522   litsize = size_of_int_loc_descriptor (i);
10523   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10524      is more compact.  For DW_OP_stack_value we need:
10525      litsize + 1 (DW_OP_stack_value)
10526      and for DW_OP_implicit_value:
10527      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10528   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10529     {
10530       loc_result = int_loc_descriptor (i);
10531       add_loc_descr (&loc_result,
10532                      new_loc_descr (DW_OP_stack_value, 0, 0));
10533       return loc_result;
10534     }
10535
10536   loc_result = new_loc_descr (DW_OP_implicit_value,
10537                               size, 0);
10538   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10539   loc_result->dw_loc_oprnd2.v.val_int = i;
10540   return loc_result;
10541 }
10542
10543 /* Return a location descriptor that designates a base+offset location.  */
10544
10545 static dw_loc_descr_ref
10546 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10547                  enum var_init_status initialized)
10548 {
10549   unsigned int regno;
10550   dw_loc_descr_ref result;
10551   dw_fde_ref fde = cfun->fde;
10552
10553   /* We only use "frame base" when we're sure we're talking about the
10554      post-prologue local stack frame.  We do this by *not* running
10555      register elimination until this point, and recognizing the special
10556      argument pointer and soft frame pointer rtx's.  */
10557   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10558     {
10559       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10560
10561       if (elim != reg)
10562         {
10563           if (GET_CODE (elim) == PLUS)
10564             {
10565               offset += INTVAL (XEXP (elim, 1));
10566               elim = XEXP (elim, 0);
10567             }
10568           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10569                        && (elim == hard_frame_pointer_rtx
10570                            || elim == stack_pointer_rtx))
10571                       || elim == (frame_pointer_needed
10572                                   ? hard_frame_pointer_rtx
10573                                   : stack_pointer_rtx));
10574
10575           /* If drap register is used to align stack, use frame
10576              pointer + offset to access stack variables.  If stack
10577              is aligned without drap, use stack pointer + offset to
10578              access stack variables.  */
10579           if (crtl->stack_realign_tried
10580               && reg == frame_pointer_rtx)
10581             {
10582               int base_reg
10583                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10584                                       ? HARD_FRAME_POINTER_REGNUM
10585                                       : REGNO (elim));
10586               return new_reg_loc_descr (base_reg, offset);
10587             }
10588
10589           gcc_assert (frame_pointer_fb_offset_valid);
10590           offset += frame_pointer_fb_offset;
10591           return new_loc_descr (DW_OP_fbreg, offset, 0);
10592         }
10593     }
10594
10595   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10596
10597   if (!optimize && fde
10598       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10599     {
10600       /* Use cfa+offset to represent the location of arguments passed
10601          on the stack when drap is used to align stack.
10602          Only do this when not optimizing, for optimized code var-tracking
10603          is supposed to track where the arguments live and the register
10604          used as vdrap or drap in some spot might be used for something
10605          else in other part of the routine.  */
10606       return new_loc_descr (DW_OP_fbreg, offset, 0);
10607     }
10608
10609   if (regno <= 31)
10610     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10611                             offset, 0);
10612   else
10613     result = new_loc_descr (DW_OP_bregx, regno, offset);
10614
10615   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10616     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10617
10618   return result;
10619 }
10620
10621 /* Return true if this RTL expression describes a base+offset calculation.  */
10622
10623 static inline int
10624 is_based_loc (const_rtx rtl)
10625 {
10626   return (GET_CODE (rtl) == PLUS
10627           && ((REG_P (XEXP (rtl, 0))
10628                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10629                && CONST_INT_P (XEXP (rtl, 1)))));
10630 }
10631
10632 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10633    failed.  */
10634
10635 static dw_loc_descr_ref
10636 tls_mem_loc_descriptor (rtx mem)
10637 {
10638   tree base;
10639   dw_loc_descr_ref loc_result;
10640
10641   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10642     return NULL;
10643
10644   base = get_base_address (MEM_EXPR (mem));
10645   if (base == NULL
10646       || TREE_CODE (base) != VAR_DECL
10647       || !DECL_THREAD_LOCAL_P (base))
10648     return NULL;
10649
10650   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10651   if (loc_result == NULL)
10652     return NULL;
10653
10654   if (MEM_OFFSET (mem))
10655     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10656
10657   return loc_result;
10658 }
10659
10660 /* Output debug info about reason why we failed to expand expression as dwarf
10661    expression.  */
10662
10663 static void
10664 expansion_failed (tree expr, rtx rtl, char const *reason)
10665 {
10666   if (dump_file && (dump_flags & TDF_DETAILS))
10667     {
10668       fprintf (dump_file, "Failed to expand as dwarf: ");
10669       if (expr)
10670         print_generic_expr (dump_file, expr, dump_flags);
10671       if (rtl)
10672         {
10673           fprintf (dump_file, "\n");
10674           print_rtl (dump_file, rtl);
10675         }
10676       fprintf (dump_file, "\nReason: %s\n", reason);
10677     }
10678 }
10679
10680 /* Helper function for const_ok_for_output, called either directly
10681    or via for_each_rtx.  */
10682
10683 static int
10684 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10685 {
10686   rtx rtl = *rtlp;
10687
10688   if (GET_CODE (rtl) == UNSPEC)
10689     {
10690       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10691          we can't express it in the debug info.  */
10692 #ifdef ENABLE_CHECKING
10693       /* Don't complain about TLS UNSPECs, those are just too hard to
10694          delegitimize.  */
10695       if (XVECLEN (rtl, 0) != 1
10696           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10697           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10698           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10699           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10700         inform (current_function_decl
10701                 ? DECL_SOURCE_LOCATION (current_function_decl)
10702                 : UNKNOWN_LOCATION,
10703 #if NUM_UNSPEC_VALUES > 0
10704                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10705                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10706                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10707                 XINT (rtl, 1));
10708 #else
10709                 "non-delegitimized UNSPEC %d found in variable location",
10710                 XINT (rtl, 1));
10711 #endif
10712 #endif
10713       expansion_failed (NULL_TREE, rtl,
10714                         "UNSPEC hasn't been delegitimized.\n");
10715       return 1;
10716     }
10717
10718   if (targetm.const_not_ok_for_debug_p (rtl))
10719     {
10720       expansion_failed (NULL_TREE, rtl,
10721                         "Expression rejected for debug by the backend.\n");
10722       return 1;
10723     }
10724
10725   if (GET_CODE (rtl) != SYMBOL_REF)
10726     return 0;
10727
10728   if (CONSTANT_POOL_ADDRESS_P (rtl))
10729     {
10730       bool marked;
10731       get_pool_constant_mark (rtl, &marked);
10732       /* If all references to this pool constant were optimized away,
10733          it was not output and thus we can't represent it.  */
10734       if (!marked)
10735         {
10736           expansion_failed (NULL_TREE, rtl,
10737                             "Constant was removed from constant pool.\n");
10738           return 1;
10739         }
10740     }
10741
10742   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10743     return 1;
10744
10745   /* Avoid references to external symbols in debug info, on several targets
10746      the linker might even refuse to link when linking a shared library,
10747      and in many other cases the relocations for .debug_info/.debug_loc are
10748      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10749      to be defined within the same shared library or executable are fine.  */
10750   if (SYMBOL_REF_EXTERNAL_P (rtl))
10751     {
10752       tree decl = SYMBOL_REF_DECL (rtl);
10753
10754       if (decl == NULL || !targetm.binds_local_p (decl))
10755         {
10756           expansion_failed (NULL_TREE, rtl,
10757                             "Symbol not defined in current TU.\n");
10758           return 1;
10759         }
10760     }
10761
10762   return 0;
10763 }
10764
10765 /* Return true if constant RTL can be emitted in DW_OP_addr or
10766    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10767    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10768
10769 static bool
10770 const_ok_for_output (rtx rtl)
10771 {
10772   if (GET_CODE (rtl) == SYMBOL_REF)
10773     return const_ok_for_output_1 (&rtl, NULL) == 0;
10774
10775   if (GET_CODE (rtl) == CONST)
10776     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10777
10778   return true;
10779 }
10780
10781 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10782    if possible, NULL otherwise.  */
10783
10784 static dw_die_ref
10785 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10786 {
10787   dw_die_ref type_die;
10788   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10789
10790   if (type == NULL)
10791     return NULL;
10792   switch (TREE_CODE (type))
10793     {
10794     case INTEGER_TYPE:
10795     case REAL_TYPE:
10796       break;
10797     default:
10798       return NULL;
10799     }
10800   type_die = lookup_type_die (type);
10801   if (!type_die)
10802     type_die = modified_type_die (type, false, false, comp_unit_die ());
10803   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10804     return NULL;
10805   return type_die;
10806 }
10807
10808 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10809    type matching MODE, or, if MODE is narrower than or as wide as
10810    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10811    possible.  */
10812
10813 static dw_loc_descr_ref
10814 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10815 {
10816   enum machine_mode outer_mode = mode;
10817   dw_die_ref type_die;
10818   dw_loc_descr_ref cvt;
10819
10820   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10821     {
10822       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10823       return op;
10824     }
10825   type_die = base_type_for_mode (outer_mode, 1);
10826   if (type_die == NULL)
10827     return NULL;
10828   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10829   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10830   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10831   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10832   add_loc_descr (&op, cvt);
10833   return op;
10834 }
10835
10836 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10837
10838 static dw_loc_descr_ref
10839 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10840                         dw_loc_descr_ref op1)
10841 {
10842   dw_loc_descr_ref ret = op0;
10843   add_loc_descr (&ret, op1);
10844   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10845   if (STORE_FLAG_VALUE != 1)
10846     {
10847       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10848       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10849     }
10850   return ret;
10851 }
10852
10853 /* Return location descriptor for signed comparison OP RTL.  */
10854
10855 static dw_loc_descr_ref
10856 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10857                          enum machine_mode mem_mode)
10858 {
10859   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10860   dw_loc_descr_ref op0, op1;
10861   int shift;
10862
10863   if (op_mode == VOIDmode)
10864     op_mode = GET_MODE (XEXP (rtl, 1));
10865   if (op_mode == VOIDmode)
10866     return NULL;
10867
10868   if (dwarf_strict
10869       && (GET_MODE_CLASS (op_mode) != MODE_INT
10870           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10871     return NULL;
10872
10873   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10874                             VAR_INIT_STATUS_INITIALIZED);
10875   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10876                             VAR_INIT_STATUS_INITIALIZED);
10877
10878   if (op0 == NULL || op1 == NULL)
10879     return NULL;
10880
10881   if (GET_MODE_CLASS (op_mode) != MODE_INT
10882       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10883     return compare_loc_descriptor (op, op0, op1);
10884
10885   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10886     {
10887       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10888       dw_loc_descr_ref cvt;
10889
10890       if (type_die == NULL)
10891         return NULL;
10892       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10893       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10894       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10895       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10896       add_loc_descr (&op0, cvt);
10897       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10898       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10899       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10900       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10901       add_loc_descr (&op1, cvt);
10902       return compare_loc_descriptor (op, op0, op1);
10903     }
10904
10905   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10906   /* For eq/ne, if the operands are known to be zero-extended,
10907      there is no need to do the fancy shifting up.  */
10908   if (op == DW_OP_eq || op == DW_OP_ne)
10909     {
10910       dw_loc_descr_ref last0, last1;
10911       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10912         ;
10913       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10914         ;
10915       /* deref_size zero extends, and for constants we can check
10916          whether they are zero extended or not.  */
10917       if (((last0->dw_loc_opc == DW_OP_deref_size
10918             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10919            || (CONST_INT_P (XEXP (rtl, 0))
10920                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10921                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10922           && ((last1->dw_loc_opc == DW_OP_deref_size
10923                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10924               || (CONST_INT_P (XEXP (rtl, 1))
10925                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10926                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10927         return compare_loc_descriptor (op, op0, op1);
10928
10929       /* EQ/NE comparison against constant in narrower type than
10930          DWARF2_ADDR_SIZE can be performed either as
10931          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10932          DW_OP_{eq,ne}
10933          or
10934          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10935          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10936       if (CONST_INT_P (XEXP (rtl, 1))
10937           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10938           && (size_of_int_loc_descriptor (shift) + 1
10939               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10940               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10941                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10942                                                & GET_MODE_MASK (op_mode))))
10943         {
10944           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10945           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10946           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10947                                     & GET_MODE_MASK (op_mode));
10948           return compare_loc_descriptor (op, op0, op1);
10949         }
10950     }
10951   add_loc_descr (&op0, int_loc_descriptor (shift));
10952   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10953   if (CONST_INT_P (XEXP (rtl, 1)))
10954     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10955   else
10956     {
10957       add_loc_descr (&op1, int_loc_descriptor (shift));
10958       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10959     }
10960   return compare_loc_descriptor (op, op0, op1);
10961 }
10962
10963 /* Return location descriptor for unsigned comparison OP RTL.  */
10964
10965 static dw_loc_descr_ref
10966 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10967                          enum machine_mode mem_mode)
10968 {
10969   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10970   dw_loc_descr_ref op0, op1;
10971
10972   if (op_mode == VOIDmode)
10973     op_mode = GET_MODE (XEXP (rtl, 1));
10974   if (op_mode == VOIDmode)
10975     return NULL;
10976   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10977     return NULL;
10978
10979   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10980     return NULL;
10981
10982   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10983                             VAR_INIT_STATUS_INITIALIZED);
10984   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10985                             VAR_INIT_STATUS_INITIALIZED);
10986
10987   if (op0 == NULL || op1 == NULL)
10988     return NULL;
10989
10990   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10991     {
10992       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10993       dw_loc_descr_ref last0, last1;
10994       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10995         ;
10996       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10997         ;
10998       if (CONST_INT_P (XEXP (rtl, 0)))
10999         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11000       /* deref_size zero extends, so no need to mask it again.  */
11001       else if (last0->dw_loc_opc != DW_OP_deref_size
11002                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11003         {
11004           add_loc_descr (&op0, int_loc_descriptor (mask));
11005           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11006         }
11007       if (CONST_INT_P (XEXP (rtl, 1)))
11008         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11009       /* deref_size zero extends, so no need to mask it again.  */
11010       else if (last1->dw_loc_opc != DW_OP_deref_size
11011                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11012         {
11013           add_loc_descr (&op1, int_loc_descriptor (mask));
11014           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11015         }
11016     }
11017   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11018     {
11019       HOST_WIDE_INT bias = 1;
11020       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11021       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11022       if (CONST_INT_P (XEXP (rtl, 1)))
11023         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11024                                   + INTVAL (XEXP (rtl, 1)));
11025       else
11026         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11027                                             bias, 0));
11028     }
11029   return compare_loc_descriptor (op, op0, op1);
11030 }
11031
11032 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11033
11034 static dw_loc_descr_ref
11035 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11036                        enum machine_mode mem_mode)
11037 {
11038   enum dwarf_location_atom op;
11039   dw_loc_descr_ref op0, op1, ret;
11040   dw_loc_descr_ref bra_node, drop_node;
11041
11042   if (dwarf_strict
11043       && (GET_MODE_CLASS (mode) != MODE_INT
11044           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11045     return NULL;
11046
11047   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11048                             VAR_INIT_STATUS_INITIALIZED);
11049   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11050                             VAR_INIT_STATUS_INITIALIZED);
11051
11052   if (op0 == NULL || op1 == NULL)
11053     return NULL;
11054
11055   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11056   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11057   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11058   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11059     {
11060       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11061         {
11062           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11063           add_loc_descr (&op0, int_loc_descriptor (mask));
11064           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11065           add_loc_descr (&op1, int_loc_descriptor (mask));
11066           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11067         }
11068       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11069         {
11070           HOST_WIDE_INT bias = 1;
11071           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11072           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11073           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11074         }
11075     }
11076   else if (GET_MODE_CLASS (mode) == MODE_INT
11077            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11078     {
11079       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11080       add_loc_descr (&op0, int_loc_descriptor (shift));
11081       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11082       add_loc_descr (&op1, int_loc_descriptor (shift));
11083       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11084     }
11085   else if (GET_MODE_CLASS (mode) == MODE_INT
11086            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11087     {
11088       dw_die_ref type_die = base_type_for_mode (mode, 0);
11089       dw_loc_descr_ref cvt;
11090       if (type_die == NULL)
11091         return NULL;
11092       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11093       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11094       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11095       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11096       add_loc_descr (&op0, cvt);
11097       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11098       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11099       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11100       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11101       add_loc_descr (&op1, cvt);
11102     }
11103
11104   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11105     op = DW_OP_lt;
11106   else
11107     op = DW_OP_gt;
11108   ret = op0;
11109   add_loc_descr (&ret, op1);
11110   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11111   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11112   add_loc_descr (&ret, bra_node);
11113   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11114   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11115   add_loc_descr (&ret, drop_node);
11116   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11117   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11118   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11119       && GET_MODE_CLASS (mode) == MODE_INT
11120       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11121     ret = convert_descriptor_to_mode (mode, ret);
11122   return ret;
11123 }
11124
11125 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11126    but after converting arguments to type_die, afterwards
11127    convert back to unsigned.  */
11128
11129 static dw_loc_descr_ref
11130 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11131              enum machine_mode mode, enum machine_mode mem_mode)
11132 {
11133   dw_loc_descr_ref cvt, op0, op1;
11134
11135   if (type_die == NULL)
11136     return NULL;
11137   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11138                             VAR_INIT_STATUS_INITIALIZED);
11139   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11140                             VAR_INIT_STATUS_INITIALIZED);
11141   if (op0 == NULL || op1 == NULL)
11142     return NULL;
11143   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11144   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11145   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11146   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11147   add_loc_descr (&op0, cvt);
11148   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11149   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11150   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11151   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11152   add_loc_descr (&op1, cvt);
11153   add_loc_descr (&op0, op1);
11154   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11155   return convert_descriptor_to_mode (mode, op0);
11156 }
11157
11158 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11159    const0 is DW_OP_lit0 or corresponding typed constant,
11160    const1 is DW_OP_lit1 or corresponding typed constant
11161    and constMSB is constant with just the MSB bit set
11162    for the mode):
11163        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11164    L1: const0 DW_OP_swap
11165    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11166        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11167    L3: DW_OP_drop
11168    L4: DW_OP_nop
11169
11170    CTZ is similar:
11171        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11172    L1: const0 DW_OP_swap
11173    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11174        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11175    L3: DW_OP_drop
11176    L4: DW_OP_nop
11177
11178    FFS is similar:
11179        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11180    L1: const1 DW_OP_swap
11181    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11182        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11183    L3: DW_OP_drop
11184    L4: DW_OP_nop  */
11185
11186 static dw_loc_descr_ref
11187 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11188                     enum machine_mode mem_mode)
11189 {
11190   dw_loc_descr_ref op0, ret, tmp;
11191   HOST_WIDE_INT valv;
11192   dw_loc_descr_ref l1jump, l1label;
11193   dw_loc_descr_ref l2jump, l2label;
11194   dw_loc_descr_ref l3jump, l3label;
11195   dw_loc_descr_ref l4jump, l4label;
11196   rtx msb;
11197
11198   if (GET_MODE_CLASS (mode) != MODE_INT
11199       || GET_MODE (XEXP (rtl, 0)) != mode
11200       || (GET_CODE (rtl) == CLZ
11201           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11202     return NULL;
11203
11204   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11205                             VAR_INIT_STATUS_INITIALIZED);
11206   if (op0 == NULL)
11207     return NULL;
11208   ret = op0;
11209   if (GET_CODE (rtl) == CLZ)
11210     {
11211       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11212         valv = GET_MODE_BITSIZE (mode);
11213     }
11214   else if (GET_CODE (rtl) == FFS)
11215     valv = 0;
11216   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11217     valv = GET_MODE_BITSIZE (mode);
11218   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11219   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11220   add_loc_descr (&ret, l1jump);
11221   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11222   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11223                             VAR_INIT_STATUS_INITIALIZED);
11224   if (tmp == NULL)
11225     return NULL;
11226   add_loc_descr (&ret, tmp);
11227   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11228   add_loc_descr (&ret, l4jump);
11229   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11230                                 ? const1_rtx : const0_rtx,
11231                                 mode, mem_mode,
11232                                 VAR_INIT_STATUS_INITIALIZED);
11233   if (l1label == NULL)
11234     return NULL;
11235   add_loc_descr (&ret, l1label);
11236   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11237   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11238   add_loc_descr (&ret, l2label);
11239   if (GET_CODE (rtl) != CLZ)
11240     msb = const1_rtx;
11241   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11242     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11243                    << (GET_MODE_BITSIZE (mode) - 1));
11244   else
11245     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11246                                   << (GET_MODE_BITSIZE (mode)
11247                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11248   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11249     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11250                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11251                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11252   else
11253     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11254                               VAR_INIT_STATUS_INITIALIZED);
11255   if (tmp == NULL)
11256     return NULL;
11257   add_loc_descr (&ret, tmp);
11258   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11259   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11260   add_loc_descr (&ret, l3jump);
11261   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11262                             VAR_INIT_STATUS_INITIALIZED);
11263   if (tmp == NULL)
11264     return NULL;
11265   add_loc_descr (&ret, tmp);
11266   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11267                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11268   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11269   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11270   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11271   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11272   add_loc_descr (&ret, l2jump);
11273   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11274   add_loc_descr (&ret, l3label);
11275   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11276   add_loc_descr (&ret, l4label);
11277   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11278   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11279   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11280   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11281   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11282   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11283   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11284   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11285   return ret;
11286 }
11287
11288 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11289    const1 is DW_OP_lit1 or corresponding typed constant):
11290        const0 DW_OP_swap
11291    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11292        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11293    L2: DW_OP_drop
11294
11295    PARITY is similar:
11296    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11297        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11298    L2: DW_OP_drop  */
11299
11300 static dw_loc_descr_ref
11301 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11302                          enum machine_mode mem_mode)
11303 {
11304   dw_loc_descr_ref op0, ret, tmp;
11305   dw_loc_descr_ref l1jump, l1label;
11306   dw_loc_descr_ref l2jump, l2label;
11307
11308   if (GET_MODE_CLASS (mode) != MODE_INT
11309       || GET_MODE (XEXP (rtl, 0)) != mode)
11310     return NULL;
11311
11312   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11313                             VAR_INIT_STATUS_INITIALIZED);
11314   if (op0 == NULL)
11315     return NULL;
11316   ret = op0;
11317   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11318                             VAR_INIT_STATUS_INITIALIZED);
11319   if (tmp == NULL)
11320     return NULL;
11321   add_loc_descr (&ret, tmp);
11322   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11323   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11324   add_loc_descr (&ret, l1label);
11325   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11326   add_loc_descr (&ret, l2jump);
11327   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11328   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11329   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11330                             VAR_INIT_STATUS_INITIALIZED);
11331   if (tmp == NULL)
11332     return NULL;
11333   add_loc_descr (&ret, tmp);
11334   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11335   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11336                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11337   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11338   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11339                             VAR_INIT_STATUS_INITIALIZED);
11340   add_loc_descr (&ret, tmp);
11341   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11342   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11343   add_loc_descr (&ret, l1jump);
11344   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11345   add_loc_descr (&ret, l2label);
11346   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11347   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11348   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11349   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11350   return ret;
11351 }
11352
11353 /* BSWAP (constS is initial shift count, either 56 or 24):
11354        constS const0
11355    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11356        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11357        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11358        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11359    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11360
11361 static dw_loc_descr_ref
11362 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11363                       enum machine_mode mem_mode)
11364 {
11365   dw_loc_descr_ref op0, ret, tmp;
11366   dw_loc_descr_ref l1jump, l1label;
11367   dw_loc_descr_ref l2jump, l2label;
11368
11369   if (GET_MODE_CLASS (mode) != MODE_INT
11370       || BITS_PER_UNIT != 8
11371       || (GET_MODE_BITSIZE (mode) != 32
11372           &&  GET_MODE_BITSIZE (mode) != 64))
11373     return NULL;
11374
11375   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11376                             VAR_INIT_STATUS_INITIALIZED);
11377   if (op0 == NULL)
11378     return NULL;
11379
11380   ret = op0;
11381   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11382                             mode, mem_mode,
11383                             VAR_INIT_STATUS_INITIALIZED);
11384   if (tmp == NULL)
11385     return NULL;
11386   add_loc_descr (&ret, tmp);
11387   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11388                             VAR_INIT_STATUS_INITIALIZED);
11389   if (tmp == NULL)
11390     return NULL;
11391   add_loc_descr (&ret, tmp);
11392   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11393   add_loc_descr (&ret, l1label);
11394   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11395                             mode, mem_mode,
11396                             VAR_INIT_STATUS_INITIALIZED);
11397   add_loc_descr (&ret, tmp);
11398   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11399   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11400   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11401   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11402                             VAR_INIT_STATUS_INITIALIZED);
11403   if (tmp == NULL)
11404     return NULL;
11405   add_loc_descr (&ret, tmp);
11406   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11407   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11408   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11409   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11410   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11411   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11412   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11413                             VAR_INIT_STATUS_INITIALIZED);
11414   add_loc_descr (&ret, tmp);
11415   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11416   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11417   add_loc_descr (&ret, l2jump);
11418   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11419                             VAR_INIT_STATUS_INITIALIZED);
11420   add_loc_descr (&ret, tmp);
11421   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11422   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11423   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11424   add_loc_descr (&ret, l1jump);
11425   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11426   add_loc_descr (&ret, l2label);
11427   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11428   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11429   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11430   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11431   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11432   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11433   return ret;
11434 }
11435
11436 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11437    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11438    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11439    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11440
11441    ROTATERT is similar:
11442    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11443    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11444    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11445
11446 static dw_loc_descr_ref
11447 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11448                        enum machine_mode mem_mode)
11449 {
11450   rtx rtlop1 = XEXP (rtl, 1);
11451   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11452   int i;
11453
11454   if (GET_MODE_CLASS (mode) != MODE_INT)
11455     return NULL;
11456
11457   if (GET_MODE (rtlop1) != VOIDmode
11458       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11459     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11460   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11461                             VAR_INIT_STATUS_INITIALIZED);
11462   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11463                             VAR_INIT_STATUS_INITIALIZED);
11464   if (op0 == NULL || op1 == NULL)
11465     return NULL;
11466   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11467     for (i = 0; i < 2; i++)
11468       {
11469         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11470           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11471                                         mode, mem_mode,
11472                                         VAR_INIT_STATUS_INITIALIZED);
11473         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11474           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11475                                    ? DW_OP_const4u
11476                                    : HOST_BITS_PER_WIDE_INT == 64
11477                                    ? DW_OP_const8u : DW_OP_constu,
11478                                    GET_MODE_MASK (mode), 0);
11479         else
11480           mask[i] = NULL;
11481         if (mask[i] == NULL)
11482           return NULL;
11483         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11484       }
11485   ret = op0;
11486   add_loc_descr (&ret, op1);
11487   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11488   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11489   if (GET_CODE (rtl) == ROTATERT)
11490     {
11491       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11492       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11493                                           GET_MODE_BITSIZE (mode), 0));
11494     }
11495   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11496   if (mask[0] != NULL)
11497     add_loc_descr (&ret, mask[0]);
11498   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11499   if (mask[1] != NULL)
11500     {
11501       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11502       add_loc_descr (&ret, mask[1]);
11503       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11504     }
11505   if (GET_CODE (rtl) == ROTATE)
11506     {
11507       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11508       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11509                                           GET_MODE_BITSIZE (mode), 0));
11510     }
11511   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11512   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11513   return ret;
11514 }
11515
11516 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11517    for DEBUG_PARAMETER_REF RTL.  */
11518
11519 static dw_loc_descr_ref
11520 parameter_ref_descriptor (rtx rtl)
11521 {
11522   dw_loc_descr_ref ret;
11523   dw_die_ref ref;
11524
11525   if (dwarf_strict)
11526     return NULL;
11527   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11528   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11529   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11530   if (ref)
11531     {
11532       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11533       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11534       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11535     }
11536   else
11537     {
11538       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11539       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11540     }
11541   return ret;
11542 }
11543
11544 /* Helper function to get mode of MEM's address.  */
11545
11546 enum machine_mode
11547 get_address_mode (rtx mem)
11548 {
11549   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11550   if (mode != VOIDmode)
11551     return mode;
11552   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11553 }
11554
11555 /* The following routine converts the RTL for a variable or parameter
11556    (resident in memory) into an equivalent Dwarf representation of a
11557    mechanism for getting the address of that same variable onto the top of a
11558    hypothetical "address evaluation" stack.
11559
11560    When creating memory location descriptors, we are effectively transforming
11561    the RTL for a memory-resident object into its Dwarf postfix expression
11562    equivalent.  This routine recursively descends an RTL tree, turning
11563    it into Dwarf postfix code as it goes.
11564
11565    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11566
11567    MEM_MODE is the mode of the memory reference, needed to handle some
11568    autoincrement addressing modes.
11569
11570    Return 0 if we can't represent the location.  */
11571
11572 dw_loc_descr_ref
11573 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11574                     enum machine_mode mem_mode,
11575                     enum var_init_status initialized)
11576 {
11577   dw_loc_descr_ref mem_loc_result = NULL;
11578   enum dwarf_location_atom op;
11579   dw_loc_descr_ref op0, op1;
11580
11581   if (mode == VOIDmode)
11582     mode = GET_MODE (rtl);
11583
11584   /* Note that for a dynamically sized array, the location we will generate a
11585      description of here will be the lowest numbered location which is
11586      actually within the array.  That's *not* necessarily the same as the
11587      zeroth element of the array.  */
11588
11589   rtl = targetm.delegitimize_address (rtl);
11590
11591   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11592     return NULL;
11593
11594   switch (GET_CODE (rtl))
11595     {
11596     case POST_INC:
11597     case POST_DEC:
11598     case POST_MODIFY:
11599       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11600
11601     case SUBREG:
11602       /* The case of a subreg may arise when we have a local (register)
11603          variable or a formal (register) parameter which doesn't quite fill
11604          up an entire register.  For now, just assume that it is
11605          legitimate to make the Dwarf info refer to the whole register which
11606          contains the given subreg.  */
11607       if (!subreg_lowpart_p (rtl))
11608         break;
11609       if (GET_MODE_CLASS (mode) == MODE_INT
11610           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11611           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11612 #ifdef POINTERS_EXTEND_UNSIGNED
11613               || (mode == Pmode && mem_mode != VOIDmode)
11614 #endif
11615              )
11616           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11617         {
11618           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11619                                                GET_MODE (SUBREG_REG (rtl)),
11620                                                mem_mode, initialized);
11621           break;
11622         }
11623       if (dwarf_strict)
11624         break;
11625       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11626         break;
11627       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11628           && (GET_MODE_CLASS (mode) != MODE_INT
11629               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11630         break;
11631       else
11632         {
11633           dw_die_ref type_die;
11634           dw_loc_descr_ref cvt;
11635
11636           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11637                                                GET_MODE (SUBREG_REG (rtl)),
11638                                                mem_mode, initialized);
11639           if (mem_loc_result == NULL)
11640             break;
11641           type_die = base_type_for_mode (mode,
11642                                          GET_MODE_CLASS (mode) == MODE_INT);
11643           if (type_die == NULL)
11644             {
11645               mem_loc_result = NULL;
11646               break;
11647             }
11648           if (GET_MODE_SIZE (mode)
11649               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11650             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11651           else
11652             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11653           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11654           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11655           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11656           add_loc_descr (&mem_loc_result, cvt);
11657         }
11658       break;
11659
11660     case REG:
11661       if (GET_MODE_CLASS (mode) != MODE_INT
11662           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11663 #ifdef POINTERS_EXTEND_UNSIGNED
11664               && (mode != Pmode || mem_mode == VOIDmode)
11665 #endif
11666               ))
11667         {
11668           dw_die_ref type_die;
11669
11670           if (dwarf_strict)
11671             break;
11672           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11673             break;
11674           type_die = base_type_for_mode (mode,
11675                                          GET_MODE_CLASS (mode) == MODE_INT);
11676           if (type_die == NULL)
11677             break;
11678           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11679                                           dbx_reg_number (rtl), 0);
11680           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11681           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11682           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11683           break;
11684         }
11685       /* Whenever a register number forms a part of the description of the
11686          method for calculating the (dynamic) address of a memory resident
11687          object, DWARF rules require the register number be referred to as
11688          a "base register".  This distinction is not based in any way upon
11689          what category of register the hardware believes the given register
11690          belongs to.  This is strictly DWARF terminology we're dealing with
11691          here. Note that in cases where the location of a memory-resident
11692          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11693          OP_CONST (0)) the actual DWARF location descriptor that we generate
11694          may just be OP_BASEREG (basereg).  This may look deceptively like
11695          the object in question was allocated to a register (rather than in
11696          memory) so DWARF consumers need to be aware of the subtle
11697          distinction between OP_REG and OP_BASEREG.  */
11698       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11699         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11700       else if (stack_realign_drap
11701                && crtl->drap_reg
11702                && crtl->args.internal_arg_pointer == rtl
11703                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11704         {
11705           /* If RTL is internal_arg_pointer, which has been optimized
11706              out, use DRAP instead.  */
11707           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11708                                             VAR_INIT_STATUS_INITIALIZED);
11709         }
11710       break;
11711
11712     case SIGN_EXTEND:
11713     case ZERO_EXTEND:
11714       if (GET_MODE_CLASS (mode) != MODE_INT)
11715         break;
11716       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11717                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11718       if (op0 == 0)
11719         break;
11720       else if (GET_CODE (rtl) == ZERO_EXTEND
11721                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11722                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11723                   < HOST_BITS_PER_WIDE_INT
11724                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11725                   to expand zero extend as two shifts instead of
11726                   masking.  */
11727                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11728         {
11729           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11730           mem_loc_result = op0;
11731           add_loc_descr (&mem_loc_result,
11732                          int_loc_descriptor (GET_MODE_MASK (imode)));
11733           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11734         }
11735       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11736         {
11737           int shift = DWARF2_ADDR_SIZE
11738                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11739           shift *= BITS_PER_UNIT;
11740           if (GET_CODE (rtl) == SIGN_EXTEND)
11741             op = DW_OP_shra;
11742           else
11743             op = DW_OP_shr;
11744           mem_loc_result = op0;
11745           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11746           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11747           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11748           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11749         }
11750       else if (!dwarf_strict)
11751         {
11752           dw_die_ref type_die1, type_die2;
11753           dw_loc_descr_ref cvt;
11754
11755           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11756                                           GET_CODE (rtl) == ZERO_EXTEND);
11757           if (type_die1 == NULL)
11758             break;
11759           type_die2 = base_type_for_mode (mode, 1);
11760           if (type_die2 == NULL)
11761             break;
11762           mem_loc_result = op0;
11763           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11764           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11765           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11766           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11767           add_loc_descr (&mem_loc_result, cvt);
11768           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11769           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11770           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11771           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11772           add_loc_descr (&mem_loc_result, cvt);
11773         }
11774       break;
11775
11776     case MEM:
11777       {
11778         rtx new_rtl = avoid_constant_pool_reference (rtl);
11779         if (new_rtl != rtl)
11780           {
11781             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11782                                                  initialized);
11783             if (mem_loc_result != NULL)
11784               return mem_loc_result;
11785           }
11786       }
11787       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11788                                            get_address_mode (rtl), mode,
11789                                            VAR_INIT_STATUS_INITIALIZED);
11790       if (mem_loc_result == NULL)
11791         mem_loc_result = tls_mem_loc_descriptor (rtl);
11792       if (mem_loc_result != NULL)
11793         {
11794           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11795               || GET_MODE_CLASS (mode) != MODE_INT)
11796             {
11797               dw_die_ref type_die;
11798               dw_loc_descr_ref deref;
11799
11800               if (dwarf_strict)
11801                 return NULL;
11802               type_die
11803                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11804               if (type_die == NULL)
11805                 return NULL;
11806               deref = new_loc_descr (DW_OP_GNU_deref_type,
11807                                      GET_MODE_SIZE (mode), 0);
11808               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11809               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11810               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11811               add_loc_descr (&mem_loc_result, deref);
11812             }
11813           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11814             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11815           else
11816             add_loc_descr (&mem_loc_result,
11817                            new_loc_descr (DW_OP_deref_size,
11818                                           GET_MODE_SIZE (mode), 0));
11819         }
11820       break;
11821
11822     case LO_SUM:
11823       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11824
11825     case LABEL_REF:
11826       /* Some ports can transform a symbol ref into a label ref, because
11827          the symbol ref is too far away and has to be dumped into a constant
11828          pool.  */
11829     case CONST:
11830     case SYMBOL_REF:
11831       if (GET_MODE_CLASS (mode) != MODE_INT
11832           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11833 #ifdef POINTERS_EXTEND_UNSIGNED
11834               && (mode != Pmode || mem_mode == VOIDmode)
11835 #endif
11836               ))
11837         break;
11838       if (GET_CODE (rtl) == SYMBOL_REF
11839           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11840         {
11841           dw_loc_descr_ref temp;
11842
11843           /* If this is not defined, we have no way to emit the data.  */
11844           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11845             break;
11846
11847           /* We used to emit DW_OP_addr here, but that's wrong, since
11848              DW_OP_addr should be relocated by the debug info consumer,
11849              while DW_OP_GNU_push_tls_address operand should not.  */
11850           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11851                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11852           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11853           temp->dw_loc_oprnd1.v.val_addr = rtl;
11854           temp->dtprel = true;
11855
11856           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11857           add_loc_descr (&mem_loc_result, temp);
11858
11859           break;
11860         }
11861
11862       if (!const_ok_for_output (rtl))
11863         break;
11864
11865     symref:
11866       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11867       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11868       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11869       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11870       break;
11871
11872     case CONCAT:
11873     case CONCATN:
11874     case VAR_LOCATION:
11875     case DEBUG_IMPLICIT_PTR:
11876       expansion_failed (NULL_TREE, rtl,
11877                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11878       return 0;
11879
11880     case ENTRY_VALUE:
11881       if (dwarf_strict)
11882         return NULL;
11883       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11884         {
11885           if (GET_MODE_CLASS (mode) != MODE_INT
11886               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11887             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11888                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11889           else
11890             op0
11891               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11892                                         VAR_INIT_STATUS_INITIALIZED);
11893         }
11894       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11895                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11896         {
11897           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11898                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11899           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11900             return NULL;
11901         }
11902       else
11903         gcc_unreachable ();
11904       if (op0 == NULL)
11905         return NULL;
11906       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11907       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11908       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11909       break;
11910
11911     case DEBUG_PARAMETER_REF:
11912       mem_loc_result = parameter_ref_descriptor (rtl);
11913       break;
11914
11915     case PRE_MODIFY:
11916       /* Extract the PLUS expression nested inside and fall into
11917          PLUS code below.  */
11918       rtl = XEXP (rtl, 1);
11919       goto plus;
11920
11921     case PRE_INC:
11922     case PRE_DEC:
11923       /* Turn these into a PLUS expression and fall into the PLUS code
11924          below.  */
11925       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11926                           GEN_INT (GET_CODE (rtl) == PRE_INC
11927                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11928                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11929
11930       /* ... fall through ...  */
11931
11932     case PLUS:
11933     plus:
11934       if (is_based_loc (rtl)
11935           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11936           && GET_MODE_CLASS (mode) == MODE_INT)
11937         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11938                                           INTVAL (XEXP (rtl, 1)),
11939                                           VAR_INIT_STATUS_INITIALIZED);
11940       else
11941         {
11942           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11943                                                VAR_INIT_STATUS_INITIALIZED);
11944           if (mem_loc_result == 0)
11945             break;
11946
11947           if (CONST_INT_P (XEXP (rtl, 1))
11948               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11949             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11950           else
11951             {
11952               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11953                                         VAR_INIT_STATUS_INITIALIZED);
11954               if (op1 == 0)
11955                 break;
11956               add_loc_descr (&mem_loc_result, op1);
11957               add_loc_descr (&mem_loc_result,
11958                              new_loc_descr (DW_OP_plus, 0, 0));
11959             }
11960         }
11961       break;
11962
11963     /* If a pseudo-reg is optimized away, it is possible for it to
11964        be replaced with a MEM containing a multiply or shift.  */
11965     case MINUS:
11966       op = DW_OP_minus;
11967       goto do_binop;
11968
11969     case MULT:
11970       op = DW_OP_mul;
11971       goto do_binop;
11972
11973     case DIV:
11974       if (!dwarf_strict
11975           && GET_MODE_CLASS (mode) == MODE_INT
11976           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11977         {
11978           mem_loc_result = typed_binop (DW_OP_div, rtl,
11979                                         base_type_for_mode (mode, 0),
11980                                         mode, mem_mode);
11981           break;
11982         }
11983       op = DW_OP_div;
11984       goto do_binop;
11985
11986     case UMOD:
11987       op = DW_OP_mod;
11988       goto do_binop;
11989
11990     case ASHIFT:
11991       op = DW_OP_shl;
11992       goto do_shift;
11993
11994     case ASHIFTRT:
11995       op = DW_OP_shra;
11996       goto do_shift;
11997
11998     case LSHIFTRT:
11999       op = DW_OP_shr;
12000       goto do_shift;
12001
12002     do_shift:
12003       if (GET_MODE_CLASS (mode) != MODE_INT)
12004         break;
12005       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12006                                 VAR_INIT_STATUS_INITIALIZED);
12007       {
12008         rtx rtlop1 = XEXP (rtl, 1);
12009         if (GET_MODE (rtlop1) != VOIDmode
12010             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12011                < GET_MODE_BITSIZE (mode))
12012           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12013         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12014                                   VAR_INIT_STATUS_INITIALIZED);
12015       }
12016
12017       if (op0 == 0 || op1 == 0)
12018         break;
12019
12020       mem_loc_result = op0;
12021       add_loc_descr (&mem_loc_result, op1);
12022       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12023       break;
12024
12025     case AND:
12026       op = DW_OP_and;
12027       goto do_binop;
12028
12029     case IOR:
12030       op = DW_OP_or;
12031       goto do_binop;
12032
12033     case XOR:
12034       op = DW_OP_xor;
12035       goto do_binop;
12036
12037     do_binop:
12038       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12039                                 VAR_INIT_STATUS_INITIALIZED);
12040       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12041                                 VAR_INIT_STATUS_INITIALIZED);
12042
12043       if (op0 == 0 || op1 == 0)
12044         break;
12045
12046       mem_loc_result = op0;
12047       add_loc_descr (&mem_loc_result, op1);
12048       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12049       break;
12050
12051     case MOD:
12052       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12053         {
12054           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12055                                         base_type_for_mode (mode, 0),
12056                                         mode, mem_mode);
12057           break;
12058         }
12059
12060       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12061                                 VAR_INIT_STATUS_INITIALIZED);
12062       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12063                                 VAR_INIT_STATUS_INITIALIZED);
12064
12065       if (op0 == 0 || op1 == 0)
12066         break;
12067
12068       mem_loc_result = op0;
12069       add_loc_descr (&mem_loc_result, op1);
12070       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12071       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12072       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12073       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12074       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12075       break;
12076
12077     case UDIV:
12078       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12079         {
12080           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12081             {
12082               op = DW_OP_div;
12083               goto do_binop;
12084             }
12085           mem_loc_result = typed_binop (DW_OP_div, rtl,
12086                                         base_type_for_mode (mode, 1),
12087                                         mode, mem_mode);
12088         }
12089       break;
12090
12091     case NOT:
12092       op = DW_OP_not;
12093       goto do_unop;
12094
12095     case ABS:
12096       op = DW_OP_abs;
12097       goto do_unop;
12098
12099     case NEG:
12100       op = DW_OP_neg;
12101       goto do_unop;
12102
12103     do_unop:
12104       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12105                                 VAR_INIT_STATUS_INITIALIZED);
12106
12107       if (op0 == 0)
12108         break;
12109
12110       mem_loc_result = op0;
12111       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12112       break;
12113
12114     case CONST_INT:
12115       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12116 #ifdef POINTERS_EXTEND_UNSIGNED
12117           || (mode == Pmode
12118               && mem_mode != VOIDmode
12119               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12120 #endif
12121           )
12122         {
12123           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12124           break;
12125         }
12126       if (!dwarf_strict
12127           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12128               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12129         {
12130           dw_die_ref type_die = base_type_for_mode (mode, 1);
12131           enum machine_mode amode;
12132           if (type_die == NULL)
12133             return NULL;
12134           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12135                                  MODE_INT, 0);
12136           if (INTVAL (rtl) >= 0
12137               && amode != BLKmode
12138               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12139               /* const DW_OP_GNU_convert <XXX> vs.
12140                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12141               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12142                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12143             {
12144               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12145               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12146               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12147               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12148               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12149               add_loc_descr (&mem_loc_result, op0);
12150               return mem_loc_result;
12151             }
12152           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12153                                           INTVAL (rtl));
12154           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12155           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12156           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12157           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12158             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12159           else
12160             {
12161               mem_loc_result->dw_loc_oprnd2.val_class
12162                 = dw_val_class_const_double;
12163               mem_loc_result->dw_loc_oprnd2.v.val_double
12164                 = shwi_to_double_int (INTVAL (rtl));
12165             }
12166         }
12167       break;
12168
12169     case CONST_DOUBLE:
12170       if (!dwarf_strict)
12171         {
12172           dw_die_ref type_die;
12173
12174           /* Note that a CONST_DOUBLE rtx could represent either an integer
12175              or a floating-point constant.  A CONST_DOUBLE is used whenever
12176              the constant requires more than one word in order to be
12177              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12178           if (mode == VOIDmode
12179               || (GET_MODE (rtl) == VOIDmode
12180                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12181             break;
12182           type_die = base_type_for_mode (mode,
12183                                          GET_MODE_CLASS (mode) == MODE_INT);
12184           if (type_die == NULL)
12185             return NULL;
12186           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12187           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12188           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12189           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12190           if (SCALAR_FLOAT_MODE_P (mode))
12191             {
12192               unsigned int length = GET_MODE_SIZE (mode);
12193               unsigned char *array
12194                   = (unsigned char*) ggc_alloc_atomic (length);
12195
12196               insert_float (rtl, array);
12197               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12198               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12199               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12200               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12201             }
12202           else
12203             {
12204               mem_loc_result->dw_loc_oprnd2.val_class
12205                 = dw_val_class_const_double;
12206               mem_loc_result->dw_loc_oprnd2.v.val_double
12207                 = rtx_to_double_int (rtl);
12208             }
12209         }
12210       break;
12211
12212     case EQ:
12213       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12214       break;
12215
12216     case GE:
12217       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12218       break;
12219
12220     case GT:
12221       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12222       break;
12223
12224     case LE:
12225       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12226       break;
12227
12228     case LT:
12229       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12230       break;
12231
12232     case NE:
12233       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12234       break;
12235
12236     case GEU:
12237       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12238       break;
12239
12240     case GTU:
12241       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12242       break;
12243
12244     case LEU:
12245       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12246       break;
12247
12248     case LTU:
12249       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12250       break;
12251
12252     case UMIN:
12253     case UMAX:
12254       if (GET_MODE_CLASS (mode) != MODE_INT)
12255         break;
12256       /* FALLTHRU */
12257     case SMIN:
12258     case SMAX:
12259       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12260       break;
12261
12262     case ZERO_EXTRACT:
12263     case SIGN_EXTRACT:
12264       if (CONST_INT_P (XEXP (rtl, 1))
12265           && CONST_INT_P (XEXP (rtl, 2))
12266           && ((unsigned) INTVAL (XEXP (rtl, 1))
12267               + (unsigned) INTVAL (XEXP (rtl, 2))
12268               <= GET_MODE_BITSIZE (mode))
12269           && GET_MODE_CLASS (mode) == MODE_INT
12270           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12271           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12272         {
12273           int shift, size;
12274           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12275                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12276           if (op0 == 0)
12277             break;
12278           if (GET_CODE (rtl) == SIGN_EXTRACT)
12279             op = DW_OP_shra;
12280           else
12281             op = DW_OP_shr;
12282           mem_loc_result = op0;
12283           size = INTVAL (XEXP (rtl, 1));
12284           shift = INTVAL (XEXP (rtl, 2));
12285           if (BITS_BIG_ENDIAN)
12286             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12287                     - shift - size;
12288           if (shift + size != (int) DWARF2_ADDR_SIZE)
12289             {
12290               add_loc_descr (&mem_loc_result,
12291                              int_loc_descriptor (DWARF2_ADDR_SIZE
12292                                                  - shift - size));
12293               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12294             }
12295           if (size != (int) DWARF2_ADDR_SIZE)
12296             {
12297               add_loc_descr (&mem_loc_result,
12298                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12299               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12300             }
12301         }
12302       break;
12303
12304     case IF_THEN_ELSE:
12305       {
12306         dw_loc_descr_ref op2, bra_node, drop_node;
12307         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12308                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12309                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12310                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12311         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12312                                   VAR_INIT_STATUS_INITIALIZED);
12313         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12314                                   VAR_INIT_STATUS_INITIALIZED);
12315         if (op0 == NULL || op1 == NULL || op2 == NULL)
12316           break;
12317
12318         mem_loc_result = op1;
12319         add_loc_descr (&mem_loc_result, op2);
12320         add_loc_descr (&mem_loc_result, op0);
12321         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12322         add_loc_descr (&mem_loc_result, bra_node);
12323         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12324         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12325         add_loc_descr (&mem_loc_result, drop_node);
12326         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12327         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12328       }
12329       break;
12330
12331     case FLOAT_EXTEND:
12332     case FLOAT_TRUNCATE:
12333     case FLOAT:
12334     case UNSIGNED_FLOAT:
12335     case FIX:
12336     case UNSIGNED_FIX:
12337       if (!dwarf_strict)
12338         {
12339           dw_die_ref type_die;
12340           dw_loc_descr_ref cvt;
12341
12342           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12343                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12344           if (op0 == NULL)
12345             break;
12346           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12347               && (GET_CODE (rtl) == FLOAT
12348                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12349                      <= DWARF2_ADDR_SIZE))
12350             {
12351               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12352                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12353               if (type_die == NULL)
12354                 break;
12355               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12356               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12357               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12358               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12359               add_loc_descr (&op0, cvt);
12360             }
12361           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12362           if (type_die == NULL)
12363             break;
12364           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12365           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12366           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12367           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12368           add_loc_descr (&op0, cvt);
12369           if (GET_MODE_CLASS (mode) == MODE_INT
12370               && (GET_CODE (rtl) == FIX
12371                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12372             {
12373               op0 = convert_descriptor_to_mode (mode, op0);
12374               if (op0 == NULL)
12375                 break;
12376             }
12377           mem_loc_result = op0;
12378         }
12379       break;
12380
12381     case CLZ:
12382     case CTZ:
12383     case FFS:
12384       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12385       break;
12386
12387     case POPCOUNT:
12388     case PARITY:
12389       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12390       break;
12391
12392     case BSWAP:
12393       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12394       break;
12395
12396     case ROTATE:
12397     case ROTATERT:
12398       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12399       break;
12400
12401     case COMPARE:
12402     case TRUNCATE:
12403       /* In theory, we could implement the above.  */
12404       /* DWARF cannot represent the unsigned compare operations
12405          natively.  */
12406     case SS_MULT:
12407     case US_MULT:
12408     case SS_DIV:
12409     case US_DIV:
12410     case SS_PLUS:
12411     case US_PLUS:
12412     case SS_MINUS:
12413     case US_MINUS:
12414     case SS_NEG:
12415     case US_NEG:
12416     case SS_ABS:
12417     case SS_ASHIFT:
12418     case US_ASHIFT:
12419     case SS_TRUNCATE:
12420     case US_TRUNCATE:
12421     case UNORDERED:
12422     case ORDERED:
12423     case UNEQ:
12424     case UNGE:
12425     case UNGT:
12426     case UNLE:
12427     case UNLT:
12428     case LTGT:
12429     case FRACT_CONVERT:
12430     case UNSIGNED_FRACT_CONVERT:
12431     case SAT_FRACT:
12432     case UNSIGNED_SAT_FRACT:
12433     case SQRT:
12434     case ASM_OPERANDS:
12435     case VEC_MERGE:
12436     case VEC_SELECT:
12437     case VEC_CONCAT:
12438     case VEC_DUPLICATE:
12439     case UNSPEC:
12440     case HIGH:
12441     case FMA:
12442     case STRICT_LOW_PART:
12443     case CONST_VECTOR:
12444     case CONST_FIXED:
12445     case CLRSB:
12446       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12447          can't express it in the debug info.  This can happen e.g. with some
12448          TLS UNSPECs.  */
12449       break;
12450
12451     case CONST_STRING:
12452       resolve_one_addr (&rtl, NULL);
12453       goto symref;
12454
12455     default:
12456 #ifdef ENABLE_CHECKING
12457       print_rtl (stderr, rtl);
12458       gcc_unreachable ();
12459 #else
12460       break;
12461 #endif
12462     }
12463
12464   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12465     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12466
12467   return mem_loc_result;
12468 }
12469
12470 /* Return a descriptor that describes the concatenation of two locations.
12471    This is typically a complex variable.  */
12472
12473 static dw_loc_descr_ref
12474 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12475 {
12476   dw_loc_descr_ref cc_loc_result = NULL;
12477   dw_loc_descr_ref x0_ref
12478     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12479   dw_loc_descr_ref x1_ref
12480     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12481
12482   if (x0_ref == 0 || x1_ref == 0)
12483     return 0;
12484
12485   cc_loc_result = x0_ref;
12486   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12487
12488   add_loc_descr (&cc_loc_result, x1_ref);
12489   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12490
12491   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12492     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12493
12494   return cc_loc_result;
12495 }
12496
12497 /* Return a descriptor that describes the concatenation of N
12498    locations.  */
12499
12500 static dw_loc_descr_ref
12501 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12502 {
12503   unsigned int i;
12504   dw_loc_descr_ref cc_loc_result = NULL;
12505   unsigned int n = XVECLEN (concatn, 0);
12506
12507   for (i = 0; i < n; ++i)
12508     {
12509       dw_loc_descr_ref ref;
12510       rtx x = XVECEXP (concatn, 0, i);
12511
12512       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12513       if (ref == NULL)
12514         return NULL;
12515
12516       add_loc_descr (&cc_loc_result, ref);
12517       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12518     }
12519
12520   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12521     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12522
12523   return cc_loc_result;
12524 }
12525
12526 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12527    for DEBUG_IMPLICIT_PTR RTL.  */
12528
12529 static dw_loc_descr_ref
12530 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12531 {
12532   dw_loc_descr_ref ret;
12533   dw_die_ref ref;
12534
12535   if (dwarf_strict)
12536     return NULL;
12537   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12538               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12539               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12540   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12541   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12542   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12543   if (ref)
12544     {
12545       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12546       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12547       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12548     }
12549   else
12550     {
12551       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12552       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12553     }
12554   return ret;
12555 }
12556
12557 /* Output a proper Dwarf location descriptor for a variable or parameter
12558    which is either allocated in a register or in a memory location.  For a
12559    register, we just generate an OP_REG and the register number.  For a
12560    memory location we provide a Dwarf postfix expression describing how to
12561    generate the (dynamic) address of the object onto the address stack.
12562
12563    MODE is mode of the decl if this loc_descriptor is going to be used in
12564    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12565    allowed, VOIDmode otherwise.
12566
12567    If we don't know how to describe it, return 0.  */
12568
12569 static dw_loc_descr_ref
12570 loc_descriptor (rtx rtl, enum machine_mode mode,
12571                 enum var_init_status initialized)
12572 {
12573   dw_loc_descr_ref loc_result = NULL;
12574
12575   switch (GET_CODE (rtl))
12576     {
12577     case SUBREG:
12578       /* The case of a subreg may arise when we have a local (register)
12579          variable or a formal (register) parameter which doesn't quite fill
12580          up an entire register.  For now, just assume that it is
12581          legitimate to make the Dwarf info refer to the whole register which
12582          contains the given subreg.  */
12583       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12584         loc_result = loc_descriptor (SUBREG_REG (rtl),
12585                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12586       else
12587         goto do_default;
12588       break;
12589
12590     case REG:
12591       loc_result = reg_loc_descriptor (rtl, initialized);
12592       break;
12593
12594     case MEM:
12595       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12596                                        GET_MODE (rtl), initialized);
12597       if (loc_result == NULL)
12598         loc_result = tls_mem_loc_descriptor (rtl);
12599       if (loc_result == NULL)
12600         {
12601           rtx new_rtl = avoid_constant_pool_reference (rtl);
12602           if (new_rtl != rtl)
12603             loc_result = loc_descriptor (new_rtl, mode, initialized);
12604         }
12605       break;
12606
12607     case CONCAT:
12608       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12609                                           initialized);
12610       break;
12611
12612     case CONCATN:
12613       loc_result = concatn_loc_descriptor (rtl, initialized);
12614       break;
12615
12616     case VAR_LOCATION:
12617       /* Single part.  */
12618       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12619         {
12620           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12621           if (GET_CODE (loc) == EXPR_LIST)
12622             loc = XEXP (loc, 0);
12623           loc_result = loc_descriptor (loc, mode, initialized);
12624           break;
12625         }
12626
12627       rtl = XEXP (rtl, 1);
12628       /* FALLTHRU */
12629
12630     case PARALLEL:
12631       {
12632         rtvec par_elems = XVEC (rtl, 0);
12633         int num_elem = GET_NUM_ELEM (par_elems);
12634         enum machine_mode mode;
12635         int i;
12636
12637         /* Create the first one, so we have something to add to.  */
12638         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12639                                      VOIDmode, initialized);
12640         if (loc_result == NULL)
12641           return NULL;
12642         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12643         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12644         for (i = 1; i < num_elem; i++)
12645           {
12646             dw_loc_descr_ref temp;
12647
12648             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12649                                    VOIDmode, initialized);
12650             if (temp == NULL)
12651               return NULL;
12652             add_loc_descr (&loc_result, temp);
12653             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12654             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12655           }
12656       }
12657       break;
12658
12659     case CONST_INT:
12660       if (mode != VOIDmode && mode != BLKmode)
12661         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12662                                                     INTVAL (rtl));
12663       break;
12664
12665     case CONST_DOUBLE:
12666       if (mode == VOIDmode)
12667         mode = GET_MODE (rtl);
12668
12669       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12670         {
12671           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12672
12673           /* Note that a CONST_DOUBLE rtx could represent either an integer
12674              or a floating-point constant.  A CONST_DOUBLE is used whenever
12675              the constant requires more than one word in order to be
12676              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12677           loc_result = new_loc_descr (DW_OP_implicit_value,
12678                                       GET_MODE_SIZE (mode), 0);
12679           if (SCALAR_FLOAT_MODE_P (mode))
12680             {
12681               unsigned int length = GET_MODE_SIZE (mode);
12682               unsigned char *array
12683                   = (unsigned char*) ggc_alloc_atomic (length);
12684
12685               insert_float (rtl, array);
12686               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12687               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12688               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12689               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12690             }
12691           else
12692             {
12693               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12694               loc_result->dw_loc_oprnd2.v.val_double
12695                 = rtx_to_double_int (rtl);
12696             }
12697         }
12698       break;
12699
12700     case CONST_VECTOR:
12701       if (mode == VOIDmode)
12702         mode = GET_MODE (rtl);
12703
12704       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12705         {
12706           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12707           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12708           unsigned char *array = (unsigned char *)
12709             ggc_alloc_atomic (length * elt_size);
12710           unsigned int i;
12711           unsigned char *p;
12712
12713           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12714           switch (GET_MODE_CLASS (mode))
12715             {
12716             case MODE_VECTOR_INT:
12717               for (i = 0, p = array; i < length; i++, p += elt_size)
12718                 {
12719                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12720                   double_int val = rtx_to_double_int (elt);
12721
12722                   if (elt_size <= sizeof (HOST_WIDE_INT))
12723                     insert_int (double_int_to_shwi (val), elt_size, p);
12724                   else
12725                     {
12726                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12727                       insert_double (val, p);
12728                     }
12729                 }
12730               break;
12731
12732             case MODE_VECTOR_FLOAT:
12733               for (i = 0, p = array; i < length; i++, p += elt_size)
12734                 {
12735                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12736                   insert_float (elt, p);
12737                 }
12738               break;
12739
12740             default:
12741               gcc_unreachable ();
12742             }
12743
12744           loc_result = new_loc_descr (DW_OP_implicit_value,
12745                                       length * elt_size, 0);
12746           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12747           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12748           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12749           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12750         }
12751       break;
12752
12753     case CONST:
12754       if (mode == VOIDmode
12755           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12756           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12757           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12758         {
12759           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12760           break;
12761         }
12762       /* FALLTHROUGH */
12763     case SYMBOL_REF:
12764       if (!const_ok_for_output (rtl))
12765         break;
12766     case LABEL_REF:
12767       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12768           && (dwarf_version >= 4 || !dwarf_strict))
12769         {
12770           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12771           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12772           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12773           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12774           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12775         }
12776       break;
12777
12778     case DEBUG_IMPLICIT_PTR:
12779       loc_result = implicit_ptr_descriptor (rtl, 0);
12780       break;
12781
12782     case PLUS:
12783       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12784           && CONST_INT_P (XEXP (rtl, 1)))
12785         {
12786           loc_result
12787             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12788           break;
12789         }
12790       /* FALLTHRU */
12791     do_default:
12792     default:
12793       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12794            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12795            && dwarf_version >= 4)
12796           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12797         {
12798           /* Value expression.  */
12799           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12800           if (loc_result)
12801             add_loc_descr (&loc_result,
12802                            new_loc_descr (DW_OP_stack_value, 0, 0));
12803         }
12804       break;
12805     }
12806
12807   return loc_result;
12808 }
12809
12810 /* We need to figure out what section we should use as the base for the
12811    address ranges where a given location is valid.
12812    1. If this particular DECL has a section associated with it, use that.
12813    2. If this function has a section associated with it, use that.
12814    3. Otherwise, use the text section.
12815    XXX: If you split a variable across multiple sections, we won't notice.  */
12816
12817 static const char *
12818 secname_for_decl (const_tree decl)
12819 {
12820   const char *secname;
12821
12822   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12823     {
12824       tree sectree = DECL_SECTION_NAME (decl);
12825       secname = TREE_STRING_POINTER (sectree);
12826     }
12827   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12828     {
12829       tree sectree = DECL_SECTION_NAME (current_function_decl);
12830       secname = TREE_STRING_POINTER (sectree);
12831     }
12832   else if (cfun && in_cold_section_p)
12833     secname = crtl->subsections.cold_section_label;
12834   else
12835     secname = text_section_label;
12836
12837   return secname;
12838 }
12839
12840 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12841
12842 static bool
12843 decl_by_reference_p (tree decl)
12844 {
12845   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12846            || TREE_CODE (decl) == VAR_DECL)
12847           && DECL_BY_REFERENCE (decl));
12848 }
12849
12850 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12851    for VARLOC.  */
12852
12853 static dw_loc_descr_ref
12854 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12855                enum var_init_status initialized)
12856 {
12857   int have_address = 0;
12858   dw_loc_descr_ref descr;
12859   enum machine_mode mode;
12860
12861   if (want_address != 2)
12862     {
12863       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12864       /* Single part.  */
12865       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12866         {
12867           varloc = PAT_VAR_LOCATION_LOC (varloc);
12868           if (GET_CODE (varloc) == EXPR_LIST)
12869             varloc = XEXP (varloc, 0);
12870           mode = GET_MODE (varloc);
12871           if (MEM_P (varloc))
12872             {
12873               rtx addr = XEXP (varloc, 0);
12874               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12875                                           mode, initialized);
12876               if (descr)
12877                 have_address = 1;
12878               else
12879                 {
12880                   rtx x = avoid_constant_pool_reference (varloc);
12881                   if (x != varloc)
12882                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12883                                                 initialized);
12884                 }
12885             }
12886           else
12887             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12888         }
12889       else
12890         return 0;
12891     }
12892   else
12893     {
12894       if (GET_CODE (varloc) == VAR_LOCATION)
12895         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12896       else
12897         mode = DECL_MODE (loc);
12898       descr = loc_descriptor (varloc, mode, initialized);
12899       have_address = 1;
12900     }
12901
12902   if (!descr)
12903     return 0;
12904
12905   if (want_address == 2 && !have_address
12906       && (dwarf_version >= 4 || !dwarf_strict))
12907     {
12908       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12909         {
12910           expansion_failed (loc, NULL_RTX,
12911                             "DWARF address size mismatch");
12912           return 0;
12913         }
12914       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12915       have_address = 1;
12916     }
12917   /* Show if we can't fill the request for an address.  */
12918   if (want_address && !have_address)
12919     {
12920       expansion_failed (loc, NULL_RTX,
12921                         "Want address and only have value");
12922       return 0;
12923     }
12924
12925   /* If we've got an address and don't want one, dereference.  */
12926   if (!want_address && have_address)
12927     {
12928       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12929       enum dwarf_location_atom op;
12930
12931       if (size > DWARF2_ADDR_SIZE || size == -1)
12932         {
12933           expansion_failed (loc, NULL_RTX,
12934                             "DWARF address size mismatch");
12935           return 0;
12936         }
12937       else if (size == DWARF2_ADDR_SIZE)
12938         op = DW_OP_deref;
12939       else
12940         op = DW_OP_deref_size;
12941
12942       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12943     }
12944
12945   return descr;
12946 }
12947
12948 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12949    if it is not possible.  */
12950
12951 static dw_loc_descr_ref
12952 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12953 {
12954   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12955     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12956   else if (dwarf_version >= 3 || !dwarf_strict)
12957     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12958   else
12959     return NULL;
12960 }
12961
12962 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12963    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12964
12965 static dw_loc_descr_ref
12966 dw_sra_loc_expr (tree decl, rtx loc)
12967 {
12968   rtx p;
12969   unsigned int padsize = 0;
12970   dw_loc_descr_ref descr, *descr_tail;
12971   unsigned HOST_WIDE_INT decl_size;
12972   rtx varloc;
12973   enum var_init_status initialized;
12974
12975   if (DECL_SIZE (decl) == NULL
12976       || !host_integerp (DECL_SIZE (decl), 1))
12977     return NULL;
12978
12979   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12980   descr = NULL;
12981   descr_tail = &descr;
12982
12983   for (p = loc; p; p = XEXP (p, 1))
12984     {
12985       unsigned int bitsize = decl_piece_bitsize (p);
12986       rtx loc_note = *decl_piece_varloc_ptr (p);
12987       dw_loc_descr_ref cur_descr;
12988       dw_loc_descr_ref *tail, last = NULL;
12989       unsigned int opsize = 0;
12990
12991       if (loc_note == NULL_RTX
12992           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12993         {
12994           padsize += bitsize;
12995           continue;
12996         }
12997       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12998       varloc = NOTE_VAR_LOCATION (loc_note);
12999       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13000       if (cur_descr == NULL)
13001         {
13002           padsize += bitsize;
13003           continue;
13004         }
13005
13006       /* Check that cur_descr either doesn't use
13007          DW_OP_*piece operations, or their sum is equal
13008          to bitsize.  Otherwise we can't embed it.  */
13009       for (tail = &cur_descr; *tail != NULL;
13010            tail = &(*tail)->dw_loc_next)
13011         if ((*tail)->dw_loc_opc == DW_OP_piece)
13012           {
13013             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13014                       * BITS_PER_UNIT;
13015             last = *tail;
13016           }
13017         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13018           {
13019             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13020             last = *tail;
13021           }
13022
13023       if (last != NULL && opsize != bitsize)
13024         {
13025           padsize += bitsize;
13026           continue;
13027         }
13028
13029       /* If there is a hole, add DW_OP_*piece after empty DWARF
13030          expression, which means that those bits are optimized out.  */
13031       if (padsize)
13032         {
13033           if (padsize > decl_size)
13034             return NULL;
13035           decl_size -= padsize;
13036           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13037           if (*descr_tail == NULL)
13038             return NULL;
13039           descr_tail = &(*descr_tail)->dw_loc_next;
13040           padsize = 0;
13041         }
13042       *descr_tail = cur_descr;
13043       descr_tail = tail;
13044       if (bitsize > decl_size)
13045         return NULL;
13046       decl_size -= bitsize;
13047       if (last == NULL)
13048         {
13049           HOST_WIDE_INT offset = 0;
13050           if (GET_CODE (varloc) == VAR_LOCATION
13051               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13052             {
13053               varloc = PAT_VAR_LOCATION_LOC (varloc);
13054               if (GET_CODE (varloc) == EXPR_LIST)
13055                 varloc = XEXP (varloc, 0);
13056             }
13057           do 
13058             {
13059               if (GET_CODE (varloc) == CONST
13060                   || GET_CODE (varloc) == SIGN_EXTEND
13061                   || GET_CODE (varloc) == ZERO_EXTEND)
13062                 varloc = XEXP (varloc, 0);
13063               else if (GET_CODE (varloc) == SUBREG)
13064                 varloc = SUBREG_REG (varloc);
13065               else
13066                 break;
13067             }
13068           while (1);
13069           /* DW_OP_bit_size offset should be zero for register
13070              or implicit location descriptions and empty location
13071              descriptions, but for memory addresses needs big endian
13072              adjustment.  */
13073           if (MEM_P (varloc))
13074             {
13075               unsigned HOST_WIDE_INT memsize
13076                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13077               if (memsize != bitsize)
13078                 {
13079                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13080                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13081                     return NULL;
13082                   if (memsize < bitsize)
13083                     return NULL;
13084                   if (BITS_BIG_ENDIAN)
13085                     offset = memsize - bitsize;
13086                 }
13087             }
13088
13089           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13090           if (*descr_tail == NULL)
13091             return NULL;
13092           descr_tail = &(*descr_tail)->dw_loc_next;
13093         }
13094     }
13095
13096   /* If there were any non-empty expressions, add padding till the end of
13097      the decl.  */
13098   if (descr != NULL && decl_size != 0)
13099     {
13100       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13101       if (*descr_tail == NULL)
13102         return NULL;
13103     }
13104   return descr;
13105 }
13106
13107 /* Return the dwarf representation of the location list LOC_LIST of
13108    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13109    function.  */
13110
13111 static dw_loc_list_ref
13112 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13113 {
13114   const char *endname, *secname;
13115   rtx varloc;
13116   enum var_init_status initialized;
13117   struct var_loc_node *node;
13118   dw_loc_descr_ref descr;
13119   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13120   dw_loc_list_ref list = NULL;
13121   dw_loc_list_ref *listp = &list;
13122
13123   /* Now that we know what section we are using for a base,
13124      actually construct the list of locations.
13125      The first location information is what is passed to the
13126      function that creates the location list, and the remaining
13127      locations just get added on to that list.
13128      Note that we only know the start address for a location
13129      (IE location changes), so to build the range, we use
13130      the range [current location start, next location start].
13131      This means we have to special case the last node, and generate
13132      a range of [last location start, end of function label].  */
13133
13134   secname = secname_for_decl (decl);
13135
13136   for (node = loc_list->first; node; node = node->next)
13137     if (GET_CODE (node->loc) == EXPR_LIST
13138         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13139       {
13140         if (GET_CODE (node->loc) == EXPR_LIST)
13141           {
13142             /* This requires DW_OP_{,bit_}piece, which is not usable
13143                inside DWARF expressions.  */
13144             if (want_address != 2)
13145               continue;
13146             descr = dw_sra_loc_expr (decl, node->loc);
13147             if (descr == NULL)
13148               continue;
13149           }
13150         else
13151           {
13152             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13153             varloc = NOTE_VAR_LOCATION (node->loc);
13154             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13155           }
13156         if (descr)
13157           {
13158             bool range_across_switch = false;
13159             /* If section switch happens in between node->label
13160                and node->next->label (or end of function) and
13161                we can't emit it as a single entry list,
13162                emit two ranges, first one ending at the end
13163                of first partition and second one starting at the
13164                beginning of second partition.  */
13165             if (node == loc_list->last_before_switch
13166                 && (node != loc_list->first || loc_list->first->next)
13167                 && current_function_decl)
13168               {
13169                 endname = cfun->fde->dw_fde_end;
13170                 range_across_switch = true;
13171               }
13172             /* The variable has a location between NODE->LABEL and
13173                NODE->NEXT->LABEL.  */
13174             else if (node->next)
13175               endname = node->next->label;
13176             /* If the variable has a location at the last label
13177                it keeps its location until the end of function.  */
13178             else if (!current_function_decl)
13179               endname = text_end_label;
13180             else
13181               {
13182                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13183                                              current_function_funcdef_no);
13184                 endname = ggc_strdup (label_id);
13185               }
13186
13187             *listp = new_loc_list (descr, node->label, endname, secname);
13188             if (TREE_CODE (decl) == PARM_DECL
13189                 && node == loc_list->first
13190                 && GET_CODE (node->loc) == NOTE
13191                 && strcmp (node->label, endname) == 0)
13192               (*listp)->force = true;
13193             listp = &(*listp)->dw_loc_next;
13194
13195             if (range_across_switch)
13196               {
13197                 if (GET_CODE (node->loc) == EXPR_LIST)
13198                   descr = dw_sra_loc_expr (decl, node->loc);
13199                 else
13200                   {
13201                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13202                     varloc = NOTE_VAR_LOCATION (node->loc);
13203                     descr = dw_loc_list_1 (decl, varloc, want_address,
13204                                            initialized);
13205                   }
13206                 gcc_assert (descr);
13207                 /* The variable has a location between NODE->LABEL and
13208                    NODE->NEXT->LABEL.  */
13209                 if (node->next)
13210                   endname = node->next->label;
13211                 else
13212                   endname = cfun->fde->dw_fde_second_end;
13213                 *listp = new_loc_list (descr,
13214                                        cfun->fde->dw_fde_second_begin,
13215                                        endname, secname);
13216                 listp = &(*listp)->dw_loc_next;
13217               }
13218           }
13219       }
13220
13221   /* Try to avoid the overhead of a location list emitting a location
13222      expression instead, but only if we didn't have more than one
13223      location entry in the first place.  If some entries were not
13224      representable, we don't want to pretend a single entry that was
13225      applies to the entire scope in which the variable is
13226      available.  */
13227   if (list && loc_list->first->next)
13228     gen_llsym (list);
13229
13230   return list;
13231 }
13232
13233 /* Return if the loc_list has only single element and thus can be represented
13234    as location description.   */
13235
13236 static bool
13237 single_element_loc_list_p (dw_loc_list_ref list)
13238 {
13239   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13240   return !list->ll_symbol;
13241 }
13242
13243 /* To each location in list LIST add loc descr REF.  */
13244
13245 static void
13246 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13247 {
13248   dw_loc_descr_ref copy;
13249   add_loc_descr (&list->expr, ref);
13250   list = list->dw_loc_next;
13251   while (list)
13252     {
13253       copy = ggc_alloc_dw_loc_descr_node ();
13254       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13255       add_loc_descr (&list->expr, copy);
13256       while (copy->dw_loc_next)
13257         {
13258           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13259           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13260           copy->dw_loc_next = new_copy;
13261           copy = new_copy;
13262         }
13263       list = list->dw_loc_next;
13264     }
13265 }
13266
13267 /* Given two lists RET and LIST
13268    produce location list that is result of adding expression in LIST
13269    to expression in RET on each possition in program.
13270    Might be destructive on both RET and LIST.
13271
13272    TODO: We handle only simple cases of RET or LIST having at most one
13273    element. General case would inolve sorting the lists in program order
13274    and merging them that will need some additional work.
13275    Adding that will improve quality of debug info especially for SRA-ed
13276    structures.  */
13277
13278 static void
13279 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13280 {
13281   if (!list)
13282     return;
13283   if (!*ret)
13284     {
13285       *ret = list;
13286       return;
13287     }
13288   if (!list->dw_loc_next)
13289     {
13290       add_loc_descr_to_each (*ret, list->expr);
13291       return;
13292     }
13293   if (!(*ret)->dw_loc_next)
13294     {
13295       add_loc_descr_to_each (list, (*ret)->expr);
13296       *ret = list;
13297       return;
13298     }
13299   expansion_failed (NULL_TREE, NULL_RTX,
13300                     "Don't know how to merge two non-trivial"
13301                     " location lists.\n");
13302   *ret = NULL;
13303   return;
13304 }
13305
13306 /* LOC is constant expression.  Try a luck, look it up in constant
13307    pool and return its loc_descr of its address.  */
13308
13309 static dw_loc_descr_ref
13310 cst_pool_loc_descr (tree loc)
13311 {
13312   /* Get an RTL for this, if something has been emitted.  */
13313   rtx rtl = lookup_constant_def (loc);
13314
13315   if (!rtl || !MEM_P (rtl))
13316     {
13317       gcc_assert (!rtl);
13318       return 0;
13319     }
13320   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13321
13322   /* TODO: We might get more coverage if we was actually delaying expansion
13323      of all expressions till end of compilation when constant pools are fully
13324      populated.  */
13325   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13326     {
13327       expansion_failed (loc, NULL_RTX,
13328                         "CST value in contant pool but not marked.");
13329       return 0;
13330     }
13331   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13332                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13333 }
13334
13335 /* Return dw_loc_list representing address of addr_expr LOC
13336    by looking for innder INDIRECT_REF expression and turing it
13337    into simple arithmetics.  */
13338
13339 static dw_loc_list_ref
13340 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13341 {
13342   tree obj, offset;
13343   HOST_WIDE_INT bitsize, bitpos, bytepos;
13344   enum machine_mode mode;
13345   int volatilep;
13346   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13347   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13348
13349   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13350                              &bitsize, &bitpos, &offset, &mode,
13351                              &unsignedp, &volatilep, false);
13352   STRIP_NOPS (obj);
13353   if (bitpos % BITS_PER_UNIT)
13354     {
13355       expansion_failed (loc, NULL_RTX, "bitfield access");
13356       return 0;
13357     }
13358   if (!INDIRECT_REF_P (obj))
13359     {
13360       expansion_failed (obj,
13361                         NULL_RTX, "no indirect ref in inner refrence");
13362       return 0;
13363     }
13364   if (!offset && !bitpos)
13365     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13366   else if (toplev
13367            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13368            && (dwarf_version >= 4 || !dwarf_strict))
13369     {
13370       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13371       if (!list_ret)
13372         return 0;
13373       if (offset)
13374         {
13375           /* Variable offset.  */
13376           list_ret1 = loc_list_from_tree (offset, 0);
13377           if (list_ret1 == 0)
13378             return 0;
13379           add_loc_list (&list_ret, list_ret1);
13380           if (!list_ret)
13381             return 0;
13382           add_loc_descr_to_each (list_ret,
13383                                  new_loc_descr (DW_OP_plus, 0, 0));
13384         }
13385       bytepos = bitpos / BITS_PER_UNIT;
13386       if (bytepos > 0)
13387         add_loc_descr_to_each (list_ret,
13388                                new_loc_descr (DW_OP_plus_uconst,
13389                                               bytepos, 0));
13390       else if (bytepos < 0)
13391         loc_list_plus_const (list_ret, bytepos);
13392       add_loc_descr_to_each (list_ret,
13393                              new_loc_descr (DW_OP_stack_value, 0, 0));
13394     }
13395   return list_ret;
13396 }
13397
13398
13399 /* Generate Dwarf location list representing LOC.
13400    If WANT_ADDRESS is false, expression computing LOC will be computed
13401    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13402    if WANT_ADDRESS is 2, expression computing address useable in location
13403      will be returned (i.e. DW_OP_reg can be used
13404      to refer to register values).  */
13405
13406 static dw_loc_list_ref
13407 loc_list_from_tree (tree loc, int want_address)
13408 {
13409   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13410   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13411   int have_address = 0;
13412   enum dwarf_location_atom op;
13413
13414   /* ??? Most of the time we do not take proper care for sign/zero
13415      extending the values properly.  Hopefully this won't be a real
13416      problem...  */
13417
13418   switch (TREE_CODE (loc))
13419     {
13420     case ERROR_MARK:
13421       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13422       return 0;
13423
13424     case PLACEHOLDER_EXPR:
13425       /* This case involves extracting fields from an object to determine the
13426          position of other fields.  We don't try to encode this here.  The
13427          only user of this is Ada, which encodes the needed information using
13428          the names of types.  */
13429       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13430       return 0;
13431
13432     case CALL_EXPR:
13433       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13434       /* There are no opcodes for these operations.  */
13435       return 0;
13436
13437     case PREINCREMENT_EXPR:
13438     case PREDECREMENT_EXPR:
13439     case POSTINCREMENT_EXPR:
13440     case POSTDECREMENT_EXPR:
13441       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13442       /* There are no opcodes for these operations.  */
13443       return 0;
13444
13445     case ADDR_EXPR:
13446       /* If we already want an address, see if there is INDIRECT_REF inside
13447          e.g. for &this->field.  */
13448       if (want_address)
13449         {
13450           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13451                        (loc, want_address == 2);
13452           if (list_ret)
13453             have_address = 1;
13454           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13455                    && (ret = cst_pool_loc_descr (loc)))
13456             have_address = 1;
13457         }
13458         /* Otherwise, process the argument and look for the address.  */
13459       if (!list_ret && !ret)
13460         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13461       else
13462         {
13463           if (want_address)
13464             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13465           return NULL;
13466         }
13467       break;
13468
13469     case VAR_DECL:
13470       if (DECL_THREAD_LOCAL_P (loc))
13471         {
13472           rtx rtl;
13473           enum dwarf_location_atom first_op;
13474           enum dwarf_location_atom second_op;
13475           bool dtprel = false;
13476
13477           if (targetm.have_tls)
13478             {
13479               /* If this is not defined, we have no way to emit the
13480                  data.  */
13481               if (!targetm.asm_out.output_dwarf_dtprel)
13482                 return 0;
13483
13484                /* The way DW_OP_GNU_push_tls_address is specified, we
13485                   can only look up addresses of objects in the current
13486                   module.  We used DW_OP_addr as first op, but that's
13487                   wrong, because DW_OP_addr is relocated by the debug
13488                   info consumer, while DW_OP_GNU_push_tls_address
13489                   operand shouldn't be.  */
13490               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13491                 return 0;
13492               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13493               dtprel = true;
13494               second_op = DW_OP_GNU_push_tls_address;
13495             }
13496           else
13497             {
13498               if (!targetm.emutls.debug_form_tls_address
13499                   || !(dwarf_version >= 3 || !dwarf_strict))
13500                 return 0;
13501               /* We stuffed the control variable into the DECL_VALUE_EXPR
13502                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13503                  no longer appear in gimple code.  We used the control
13504                  variable in specific so that we could pick it up here.  */
13505               loc = DECL_VALUE_EXPR (loc);
13506               first_op = DW_OP_addr;
13507               second_op = DW_OP_form_tls_address;
13508             }
13509
13510           rtl = rtl_for_decl_location (loc);
13511           if (rtl == NULL_RTX)
13512             return 0;
13513
13514           if (!MEM_P (rtl))
13515             return 0;
13516           rtl = XEXP (rtl, 0);
13517           if (! CONSTANT_P (rtl))
13518             return 0;
13519
13520           ret = new_loc_descr (first_op, 0, 0);
13521           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13522           ret->dw_loc_oprnd1.v.val_addr = rtl;
13523           ret->dtprel = dtprel;
13524
13525           ret1 = new_loc_descr (second_op, 0, 0);
13526           add_loc_descr (&ret, ret1);
13527
13528           have_address = 1;
13529           break;
13530         }
13531       /* FALLTHRU */
13532
13533     case PARM_DECL:
13534     case RESULT_DECL:
13535       if (DECL_HAS_VALUE_EXPR_P (loc))
13536         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13537                                    want_address);
13538       /* FALLTHRU */
13539
13540     case FUNCTION_DECL:
13541       {
13542         rtx rtl;
13543         var_loc_list *loc_list = lookup_decl_loc (loc);
13544
13545         if (loc_list && loc_list->first)
13546           {
13547             list_ret = dw_loc_list (loc_list, loc, want_address);
13548             have_address = want_address != 0;
13549             break;
13550           }
13551         rtl = rtl_for_decl_location (loc);
13552         if (rtl == NULL_RTX)
13553           {
13554             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13555             return 0;
13556           }
13557         else if (CONST_INT_P (rtl))
13558           {
13559             HOST_WIDE_INT val = INTVAL (rtl);
13560             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13561               val &= GET_MODE_MASK (DECL_MODE (loc));
13562             ret = int_loc_descriptor (val);
13563           }
13564         else if (GET_CODE (rtl) == CONST_STRING)
13565           {
13566             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13567             return 0;
13568           }
13569         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13570           {
13571             ret = new_loc_descr (DW_OP_addr, 0, 0);
13572             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13573             ret->dw_loc_oprnd1.v.val_addr = rtl;
13574           }
13575         else
13576           {
13577             enum machine_mode mode, mem_mode;
13578
13579             /* Certain constructs can only be represented at top-level.  */
13580             if (want_address == 2)
13581               {
13582                 ret = loc_descriptor (rtl, VOIDmode,
13583                                       VAR_INIT_STATUS_INITIALIZED);
13584                 have_address = 1;
13585               }
13586             else
13587               {
13588                 mode = GET_MODE (rtl);
13589                 mem_mode = VOIDmode;
13590                 if (MEM_P (rtl))
13591                   {
13592                     mem_mode = mode;
13593                     mode = get_address_mode (rtl);
13594                     rtl = XEXP (rtl, 0);
13595                     have_address = 1;
13596                   }
13597                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13598                                           VAR_INIT_STATUS_INITIALIZED);
13599               }
13600             if (!ret)
13601               expansion_failed (loc, rtl,
13602                                 "failed to produce loc descriptor for rtl");
13603           }
13604       }
13605       break;
13606
13607     case MEM_REF:
13608       /* ??? FIXME.  */
13609       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13610         return 0;
13611       /* Fallthru.  */
13612     case INDIRECT_REF:
13613       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13614       have_address = 1;
13615       break;
13616
13617     case COMPOUND_EXPR:
13618       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13619
13620     CASE_CONVERT:
13621     case VIEW_CONVERT_EXPR:
13622     case SAVE_EXPR:
13623     case MODIFY_EXPR:
13624       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13625
13626     case COMPONENT_REF:
13627     case BIT_FIELD_REF:
13628     case ARRAY_REF:
13629     case ARRAY_RANGE_REF:
13630     case REALPART_EXPR:
13631     case IMAGPART_EXPR:
13632       {
13633         tree obj, offset;
13634         HOST_WIDE_INT bitsize, bitpos, bytepos;
13635         enum machine_mode mode;
13636         int volatilep;
13637         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13638
13639         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13640                                    &unsignedp, &volatilep, false);
13641
13642         gcc_assert (obj != loc);
13643
13644         list_ret = loc_list_from_tree (obj,
13645                                        want_address == 2
13646                                        && !bitpos && !offset ? 2 : 1);
13647         /* TODO: We can extract value of the small expression via shifting even
13648            for nonzero bitpos.  */
13649         if (list_ret == 0)
13650           return 0;
13651         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13652           {
13653             expansion_failed (loc, NULL_RTX,
13654                               "bitfield access");
13655             return 0;
13656           }
13657
13658         if (offset != NULL_TREE)
13659           {
13660             /* Variable offset.  */
13661             list_ret1 = loc_list_from_tree (offset, 0);
13662             if (list_ret1 == 0)
13663               return 0;
13664             add_loc_list (&list_ret, list_ret1);
13665             if (!list_ret)
13666               return 0;
13667             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13668           }
13669
13670         bytepos = bitpos / BITS_PER_UNIT;
13671         if (bytepos > 0)
13672           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13673         else if (bytepos < 0)
13674           loc_list_plus_const (list_ret, bytepos);
13675
13676         have_address = 1;
13677         break;
13678       }
13679
13680     case INTEGER_CST:
13681       if ((want_address || !host_integerp (loc, 0))
13682           && (ret = cst_pool_loc_descr (loc)))
13683         have_address = 1;
13684       else if (want_address == 2
13685                && host_integerp (loc, 0)
13686                && (ret = address_of_int_loc_descriptor
13687                            (int_size_in_bytes (TREE_TYPE (loc)),
13688                             tree_low_cst (loc, 0))))
13689         have_address = 1;
13690       else if (host_integerp (loc, 0))
13691         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13692       else
13693         {
13694           expansion_failed (loc, NULL_RTX,
13695                             "Integer operand is not host integer");
13696           return 0;
13697         }
13698       break;
13699
13700     case CONSTRUCTOR:
13701     case REAL_CST:
13702     case STRING_CST:
13703     case COMPLEX_CST:
13704       if ((ret = cst_pool_loc_descr (loc)))
13705         have_address = 1;
13706       else
13707       /* We can construct small constants here using int_loc_descriptor.  */
13708         expansion_failed (loc, NULL_RTX,
13709                           "constructor or constant not in constant pool");
13710       break;
13711
13712     case TRUTH_AND_EXPR:
13713     case TRUTH_ANDIF_EXPR:
13714     case BIT_AND_EXPR:
13715       op = DW_OP_and;
13716       goto do_binop;
13717
13718     case TRUTH_XOR_EXPR:
13719     case BIT_XOR_EXPR:
13720       op = DW_OP_xor;
13721       goto do_binop;
13722
13723     case TRUTH_OR_EXPR:
13724     case TRUTH_ORIF_EXPR:
13725     case BIT_IOR_EXPR:
13726       op = DW_OP_or;
13727       goto do_binop;
13728
13729     case FLOOR_DIV_EXPR:
13730     case CEIL_DIV_EXPR:
13731     case ROUND_DIV_EXPR:
13732     case TRUNC_DIV_EXPR:
13733       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13734         return 0;
13735       op = DW_OP_div;
13736       goto do_binop;
13737
13738     case MINUS_EXPR:
13739       op = DW_OP_minus;
13740       goto do_binop;
13741
13742     case FLOOR_MOD_EXPR:
13743     case CEIL_MOD_EXPR:
13744     case ROUND_MOD_EXPR:
13745     case TRUNC_MOD_EXPR:
13746       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13747         {
13748           op = DW_OP_mod;
13749           goto do_binop;
13750         }
13751       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13752       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13753       if (list_ret == 0 || list_ret1 == 0)
13754         return 0;
13755
13756       add_loc_list (&list_ret, list_ret1);
13757       if (list_ret == 0)
13758         return 0;
13759       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13760       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13761       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13762       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13763       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13764       break;
13765
13766     case MULT_EXPR:
13767       op = DW_OP_mul;
13768       goto do_binop;
13769
13770     case LSHIFT_EXPR:
13771       op = DW_OP_shl;
13772       goto do_binop;
13773
13774     case RSHIFT_EXPR:
13775       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13776       goto do_binop;
13777
13778     case POINTER_PLUS_EXPR:
13779     case PLUS_EXPR:
13780       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13781         {
13782           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13783           if (list_ret == 0)
13784             return 0;
13785
13786           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13787           break;
13788         }
13789
13790       op = DW_OP_plus;
13791       goto do_binop;
13792
13793     case LE_EXPR:
13794       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13795         return 0;
13796
13797       op = DW_OP_le;
13798       goto do_binop;
13799
13800     case GE_EXPR:
13801       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13802         return 0;
13803
13804       op = DW_OP_ge;
13805       goto do_binop;
13806
13807     case LT_EXPR:
13808       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13809         return 0;
13810
13811       op = DW_OP_lt;
13812       goto do_binop;
13813
13814     case GT_EXPR:
13815       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13816         return 0;
13817
13818       op = DW_OP_gt;
13819       goto do_binop;
13820
13821     case EQ_EXPR:
13822       op = DW_OP_eq;
13823       goto do_binop;
13824
13825     case NE_EXPR:
13826       op = DW_OP_ne;
13827       goto do_binop;
13828
13829     do_binop:
13830       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13831       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13832       if (list_ret == 0 || list_ret1 == 0)
13833         return 0;
13834
13835       add_loc_list (&list_ret, list_ret1);
13836       if (list_ret == 0)
13837         return 0;
13838       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13839       break;
13840
13841     case TRUTH_NOT_EXPR:
13842     case BIT_NOT_EXPR:
13843       op = DW_OP_not;
13844       goto do_unop;
13845
13846     case ABS_EXPR:
13847       op = DW_OP_abs;
13848       goto do_unop;
13849
13850     case NEGATE_EXPR:
13851       op = DW_OP_neg;
13852       goto do_unop;
13853
13854     do_unop:
13855       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13856       if (list_ret == 0)
13857         return 0;
13858
13859       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13860       break;
13861
13862     case MIN_EXPR:
13863     case MAX_EXPR:
13864       {
13865         const enum tree_code code =
13866           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13867
13868         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13869                       build2 (code, integer_type_node,
13870                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13871                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13872       }
13873
13874       /* ... fall through ...  */
13875
13876     case COND_EXPR:
13877       {
13878         dw_loc_descr_ref lhs
13879           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13880         dw_loc_list_ref rhs
13881           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13882         dw_loc_descr_ref bra_node, jump_node, tmp;
13883
13884         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13885         if (list_ret == 0 || lhs == 0 || rhs == 0)
13886           return 0;
13887
13888         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13889         add_loc_descr_to_each (list_ret, bra_node);
13890
13891         add_loc_list (&list_ret, rhs);
13892         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13893         add_loc_descr_to_each (list_ret, jump_node);
13894
13895         add_loc_descr_to_each (list_ret, lhs);
13896         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13897         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13898
13899         /* ??? Need a node to point the skip at.  Use a nop.  */
13900         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13901         add_loc_descr_to_each (list_ret, tmp);
13902         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13903         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13904       }
13905       break;
13906
13907     case FIX_TRUNC_EXPR:
13908       return 0;
13909
13910     default:
13911       /* Leave front-end specific codes as simply unknown.  This comes
13912          up, for instance, with the C STMT_EXPR.  */
13913       if ((unsigned int) TREE_CODE (loc)
13914           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13915         {
13916           expansion_failed (loc, NULL_RTX,
13917                             "language specific tree node");
13918           return 0;
13919         }
13920
13921 #ifdef ENABLE_CHECKING
13922       /* Otherwise this is a generic code; we should just lists all of
13923          these explicitly.  We forgot one.  */
13924       gcc_unreachable ();
13925 #else
13926       /* In a release build, we want to degrade gracefully: better to
13927          generate incomplete debugging information than to crash.  */
13928       return NULL;
13929 #endif
13930     }
13931
13932   if (!ret && !list_ret)
13933     return 0;
13934
13935   if (want_address == 2 && !have_address
13936       && (dwarf_version >= 4 || !dwarf_strict))
13937     {
13938       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13939         {
13940           expansion_failed (loc, NULL_RTX,
13941                             "DWARF address size mismatch");
13942           return 0;
13943         }
13944       if (ret)
13945         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13946       else
13947         add_loc_descr_to_each (list_ret,
13948                                new_loc_descr (DW_OP_stack_value, 0, 0));
13949       have_address = 1;
13950     }
13951   /* Show if we can't fill the request for an address.  */
13952   if (want_address && !have_address)
13953     {
13954       expansion_failed (loc, NULL_RTX,
13955                         "Want address and only have value");
13956       return 0;
13957     }
13958
13959   gcc_assert (!ret || !list_ret);
13960
13961   /* If we've got an address and don't want one, dereference.  */
13962   if (!want_address && have_address)
13963     {
13964       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13965
13966       if (size > DWARF2_ADDR_SIZE || size == -1)
13967         {
13968           expansion_failed (loc, NULL_RTX,
13969                             "DWARF address size mismatch");
13970           return 0;
13971         }
13972       else if (size == DWARF2_ADDR_SIZE)
13973         op = DW_OP_deref;
13974       else
13975         op = DW_OP_deref_size;
13976
13977       if (ret)
13978         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13979       else
13980         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13981     }
13982   if (ret)
13983     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13984
13985   return list_ret;
13986 }
13987
13988 /* Same as above but return only single location expression.  */
13989 static dw_loc_descr_ref
13990 loc_descriptor_from_tree (tree loc, int want_address)
13991 {
13992   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13993   if (!ret)
13994     return NULL;
13995   if (ret->dw_loc_next)
13996     {
13997       expansion_failed (loc, NULL_RTX,
13998                         "Location list where only loc descriptor needed");
13999       return NULL;
14000     }
14001   return ret->expr;
14002 }
14003
14004 /* Given a value, round it up to the lowest multiple of `boundary'
14005    which is not less than the value itself.  */
14006
14007 static inline HOST_WIDE_INT
14008 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14009 {
14010   return (((value + boundary - 1) / boundary) * boundary);
14011 }
14012
14013 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14014    pointer to the declared type for the relevant field variable, or return
14015    `integer_type_node' if the given node turns out to be an
14016    ERROR_MARK node.  */
14017
14018 static inline tree
14019 field_type (const_tree decl)
14020 {
14021   tree type;
14022
14023   if (TREE_CODE (decl) == ERROR_MARK)
14024     return integer_type_node;
14025
14026   type = DECL_BIT_FIELD_TYPE (decl);
14027   if (type == NULL_TREE)
14028     type = TREE_TYPE (decl);
14029
14030   return type;
14031 }
14032
14033 /* Given a pointer to a tree node, return the alignment in bits for
14034    it, or else return BITS_PER_WORD if the node actually turns out to
14035    be an ERROR_MARK node.  */
14036
14037 static inline unsigned
14038 simple_type_align_in_bits (const_tree type)
14039 {
14040   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14041 }
14042
14043 static inline unsigned
14044 simple_decl_align_in_bits (const_tree decl)
14045 {
14046   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14047 }
14048
14049 /* Return the result of rounding T up to ALIGN.  */
14050
14051 static inline double_int
14052 round_up_to_align (double_int t, unsigned int align)
14053 {
14054   double_int alignd = uhwi_to_double_int (align);
14055   t = double_int_add (t, alignd);
14056   t = double_int_add (t, double_int_minus_one);
14057   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14058   t = double_int_mul (t, alignd);
14059   return t;
14060 }
14061
14062 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14063    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14064    or return 0 if we are unable to determine what that offset is, either
14065    because the argument turns out to be a pointer to an ERROR_MARK node, or
14066    because the offset is actually variable.  (We can't handle the latter case
14067    just yet).  */
14068
14069 static HOST_WIDE_INT
14070 field_byte_offset (const_tree decl)
14071 {
14072   double_int object_offset_in_bits;
14073   double_int object_offset_in_bytes;
14074   double_int bitpos_int;
14075
14076   if (TREE_CODE (decl) == ERROR_MARK)
14077     return 0;
14078
14079   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14080
14081   /* We cannot yet cope with fields whose positions are variable, so
14082      for now, when we see such things, we simply return 0.  Someday, we may
14083      be able to handle such cases, but it will be damn difficult.  */
14084   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14085     return 0;
14086
14087   bitpos_int = tree_to_double_int (bit_position (decl));
14088
14089 #ifdef PCC_BITFIELD_TYPE_MATTERS
14090   if (PCC_BITFIELD_TYPE_MATTERS)
14091     {
14092       tree type;
14093       tree field_size_tree;
14094       double_int deepest_bitpos;
14095       double_int field_size_in_bits;
14096       unsigned int type_align_in_bits;
14097       unsigned int decl_align_in_bits;
14098       double_int type_size_in_bits;
14099
14100       type = field_type (decl);
14101       type_size_in_bits = double_int_type_size_in_bits (type);
14102       type_align_in_bits = simple_type_align_in_bits (type);
14103
14104       field_size_tree = DECL_SIZE (decl);
14105
14106       /* The size could be unspecified if there was an error, or for
14107          a flexible array member.  */
14108       if (!field_size_tree)
14109         field_size_tree = bitsize_zero_node;
14110
14111       /* If the size of the field is not constant, use the type size.  */
14112       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14113         field_size_in_bits = tree_to_double_int (field_size_tree);
14114       else
14115         field_size_in_bits = type_size_in_bits;
14116
14117       decl_align_in_bits = simple_decl_align_in_bits (decl);
14118
14119       /* The GCC front-end doesn't make any attempt to keep track of the
14120          starting bit offset (relative to the start of the containing
14121          structure type) of the hypothetical "containing object" for a
14122          bit-field.  Thus, when computing the byte offset value for the
14123          start of the "containing object" of a bit-field, we must deduce
14124          this information on our own. This can be rather tricky to do in
14125          some cases.  For example, handling the following structure type
14126          definition when compiling for an i386/i486 target (which only
14127          aligns long long's to 32-bit boundaries) can be very tricky:
14128
14129          struct S { int field1; long long field2:31; };
14130
14131          Fortunately, there is a simple rule-of-thumb which can be used
14132          in such cases.  When compiling for an i386/i486, GCC will
14133          allocate 8 bytes for the structure shown above.  It decides to
14134          do this based upon one simple rule for bit-field allocation.
14135          GCC allocates each "containing object" for each bit-field at
14136          the first (i.e. lowest addressed) legitimate alignment boundary
14137          (based upon the required minimum alignment for the declared
14138          type of the field) which it can possibly use, subject to the
14139          condition that there is still enough available space remaining
14140          in the containing object (when allocated at the selected point)
14141          to fully accommodate all of the bits of the bit-field itself.
14142
14143          This simple rule makes it obvious why GCC allocates 8 bytes for
14144          each object of the structure type shown above.  When looking
14145          for a place to allocate the "containing object" for `field2',
14146          the compiler simply tries to allocate a 64-bit "containing
14147          object" at each successive 32-bit boundary (starting at zero)
14148          until it finds a place to allocate that 64- bit field such that
14149          at least 31 contiguous (and previously unallocated) bits remain
14150          within that selected 64 bit field.  (As it turns out, for the
14151          example above, the compiler finds it is OK to allocate the
14152          "containing object" 64-bit field at bit-offset zero within the
14153          structure type.)
14154
14155          Here we attempt to work backwards from the limited set of facts
14156          we're given, and we try to deduce from those facts, where GCC
14157          must have believed that the containing object started (within
14158          the structure type). The value we deduce is then used (by the
14159          callers of this routine) to generate DW_AT_location and
14160          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14161          the case of DW_AT_location, regular fields as well).  */
14162
14163       /* Figure out the bit-distance from the start of the structure to
14164          the "deepest" bit of the bit-field.  */
14165       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14166
14167       /* This is the tricky part.  Use some fancy footwork to deduce
14168          where the lowest addressed bit of the containing object must
14169          be.  */
14170       object_offset_in_bits
14171         = double_int_sub (deepest_bitpos, type_size_in_bits);
14172
14173       /* Round up to type_align by default.  This works best for
14174          bitfields.  */
14175       object_offset_in_bits
14176         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14177
14178       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14179         {
14180           object_offset_in_bits
14181             = double_int_sub (deepest_bitpos, type_size_in_bits);
14182
14183           /* Round up to decl_align instead.  */
14184           object_offset_in_bits
14185             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14186         }
14187     }
14188   else
14189 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14190     object_offset_in_bits = bitpos_int;
14191
14192   object_offset_in_bytes
14193     = double_int_div (object_offset_in_bits,
14194                       uhwi_to_double_int (BITS_PER_UNIT), true,
14195                       TRUNC_DIV_EXPR);
14196   return double_int_to_shwi (object_offset_in_bytes);
14197 }
14198 \f
14199 /* The following routines define various Dwarf attributes and any data
14200    associated with them.  */
14201
14202 /* Add a location description attribute value to a DIE.
14203
14204    This emits location attributes suitable for whole variables and
14205    whole parameters.  Note that the location attributes for struct fields are
14206    generated by the routine `data_member_location_attribute' below.  */
14207
14208 static inline void
14209 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14210                              dw_loc_list_ref descr)
14211 {
14212   if (descr == 0)
14213     return;
14214   if (single_element_loc_list_p (descr))
14215     add_AT_loc (die, attr_kind, descr->expr);
14216   else
14217     add_AT_loc_list (die, attr_kind, descr);
14218 }
14219
14220 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14221
14222 static void
14223 add_accessibility_attribute (dw_die_ref die, tree decl)
14224 {
14225   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14226      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14227      the default has always been DW_ACCESS_public.  */
14228   if (TREE_PROTECTED (decl))
14229     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14230   else if (TREE_PRIVATE (decl))
14231     {
14232       if (dwarf_version == 2
14233           || die->die_parent == NULL
14234           || die->die_parent->die_tag != DW_TAG_class_type)
14235         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14236     }
14237   else if (dwarf_version > 2
14238            && die->die_parent
14239            && die->die_parent->die_tag == DW_TAG_class_type)
14240     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14241 }
14242
14243 /* Attach the specialized form of location attribute used for data members of
14244    struct and union types.  In the special case of a FIELD_DECL node which
14245    represents a bit-field, the "offset" part of this special location
14246    descriptor must indicate the distance in bytes from the lowest-addressed
14247    byte of the containing struct or union type to the lowest-addressed byte of
14248    the "containing object" for the bit-field.  (See the `field_byte_offset'
14249    function above).
14250
14251    For any given bit-field, the "containing object" is a hypothetical object
14252    (of some integral or enum type) within which the given bit-field lives.  The
14253    type of this hypothetical "containing object" is always the same as the
14254    declared type of the individual bit-field itself (for GCC anyway... the
14255    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14256    bytes) of the hypothetical "containing object" which will be given in the
14257    DW_AT_byte_size attribute for this bit-field.  (See the
14258    `byte_size_attribute' function below.)  It is also used when calculating the
14259    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14260    function below.)  */
14261
14262 static void
14263 add_data_member_location_attribute (dw_die_ref die, tree decl)
14264 {
14265   HOST_WIDE_INT offset;
14266   dw_loc_descr_ref loc_descr = 0;
14267
14268   if (TREE_CODE (decl) == TREE_BINFO)
14269     {
14270       /* We're working on the TAG_inheritance for a base class.  */
14271       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14272         {
14273           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14274              aren't at a fixed offset from all (sub)objects of the same
14275              type.  We need to extract the appropriate offset from our
14276              vtable.  The following dwarf expression means
14277
14278                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14279
14280              This is specific to the V3 ABI, of course.  */
14281
14282           dw_loc_descr_ref tmp;
14283
14284           /* Make a copy of the object address.  */
14285           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14286           add_loc_descr (&loc_descr, tmp);
14287
14288           /* Extract the vtable address.  */
14289           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14290           add_loc_descr (&loc_descr, tmp);
14291
14292           /* Calculate the address of the offset.  */
14293           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14294           gcc_assert (offset < 0);
14295
14296           tmp = int_loc_descriptor (-offset);
14297           add_loc_descr (&loc_descr, tmp);
14298           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14299           add_loc_descr (&loc_descr, tmp);
14300
14301           /* Extract the offset.  */
14302           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14303           add_loc_descr (&loc_descr, tmp);
14304
14305           /* Add it to the object address.  */
14306           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14307           add_loc_descr (&loc_descr, tmp);
14308         }
14309       else
14310         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14311     }
14312   else
14313     offset = field_byte_offset (decl);
14314
14315   if (! loc_descr)
14316     {
14317       if (dwarf_version > 2)
14318         {
14319           /* Don't need to output a location expression, just the constant. */
14320           if (offset < 0)
14321             add_AT_int (die, DW_AT_data_member_location, offset);
14322           else
14323             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14324           return;
14325         }
14326       else
14327         {
14328           enum dwarf_location_atom op;
14329
14330           /* The DWARF2 standard says that we should assume that the structure
14331              address is already on the stack, so we can specify a structure
14332              field address by using DW_OP_plus_uconst.  */
14333
14334 #ifdef MIPS_DEBUGGING_INFO
14335           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14336              operator correctly.  It works only if we leave the offset on the
14337              stack.  */
14338           op = DW_OP_constu;
14339 #else
14340           op = DW_OP_plus_uconst;
14341 #endif
14342
14343           loc_descr = new_loc_descr (op, offset, 0);
14344         }
14345     }
14346
14347   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14348 }
14349
14350 /* Writes integer values to dw_vec_const array.  */
14351
14352 static void
14353 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14354 {
14355   while (size != 0)
14356     {
14357       *dest++ = val & 0xff;
14358       val >>= 8;
14359       --size;
14360     }
14361 }
14362
14363 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14364
14365 static HOST_WIDE_INT
14366 extract_int (const unsigned char *src, unsigned int size)
14367 {
14368   HOST_WIDE_INT val = 0;
14369
14370   src += size;
14371   while (size != 0)
14372     {
14373       val <<= 8;
14374       val |= *--src & 0xff;
14375       --size;
14376     }
14377   return val;
14378 }
14379
14380 /* Writes double_int values to dw_vec_const array.  */
14381
14382 static void
14383 insert_double (double_int val, unsigned char *dest)
14384 {
14385   unsigned char *p0 = dest;
14386   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14387
14388   if (WORDS_BIG_ENDIAN)
14389     {
14390       p0 = p1;
14391       p1 = dest;
14392     }
14393
14394   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14395   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14396 }
14397
14398 /* Writes floating point values to dw_vec_const array.  */
14399
14400 static void
14401 insert_float (const_rtx rtl, unsigned char *array)
14402 {
14403   REAL_VALUE_TYPE rv;
14404   long val[4];
14405   int i;
14406
14407   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14408   real_to_target (val, &rv, GET_MODE (rtl));
14409
14410   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14411   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14412     {
14413       insert_int (val[i], 4, array);
14414       array += 4;
14415     }
14416 }
14417
14418 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14419    does not have a "location" either in memory or in a register.  These
14420    things can arise in GNU C when a constant is passed as an actual parameter
14421    to an inlined function.  They can also arise in C++ where declared
14422    constants do not necessarily get memory "homes".  */
14423
14424 static bool
14425 add_const_value_attribute (dw_die_ref die, rtx rtl)
14426 {
14427   switch (GET_CODE (rtl))
14428     {
14429     case CONST_INT:
14430       {
14431         HOST_WIDE_INT val = INTVAL (rtl);
14432
14433         if (val < 0)
14434           add_AT_int (die, DW_AT_const_value, val);
14435         else
14436           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14437       }
14438       return true;
14439
14440     case CONST_DOUBLE:
14441       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14442          floating-point constant.  A CONST_DOUBLE is used whenever the
14443          constant requires more than one word in order to be adequately
14444          represented.  */
14445       {
14446         enum machine_mode mode = GET_MODE (rtl);
14447
14448         if (SCALAR_FLOAT_MODE_P (mode))
14449           {
14450             unsigned int length = GET_MODE_SIZE (mode);
14451             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14452
14453             insert_float (rtl, array);
14454             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14455           }
14456         else
14457           add_AT_double (die, DW_AT_const_value,
14458                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14459       }
14460       return true;
14461
14462     case CONST_VECTOR:
14463       {
14464         enum machine_mode mode = GET_MODE (rtl);
14465         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14466         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14467         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14468           (length * elt_size);
14469         unsigned int i;
14470         unsigned char *p;
14471
14472         switch (GET_MODE_CLASS (mode))
14473           {
14474           case MODE_VECTOR_INT:
14475             for (i = 0, p = array; i < length; i++, p += elt_size)
14476               {
14477                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14478                 double_int val = rtx_to_double_int (elt);
14479
14480                 if (elt_size <= sizeof (HOST_WIDE_INT))
14481                   insert_int (double_int_to_shwi (val), elt_size, p);
14482                 else
14483                   {
14484                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14485                     insert_double (val, p);
14486                   }
14487               }
14488             break;
14489
14490           case MODE_VECTOR_FLOAT:
14491             for (i = 0, p = array; i < length; i++, p += elt_size)
14492               {
14493                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14494                 insert_float (elt, p);
14495               }
14496             break;
14497
14498           default:
14499             gcc_unreachable ();
14500           }
14501
14502         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14503       }
14504       return true;
14505
14506     case CONST_STRING:
14507       if (dwarf_version >= 4 || !dwarf_strict)
14508         {
14509           dw_loc_descr_ref loc_result;
14510           resolve_one_addr (&rtl, NULL);
14511         rtl_addr:
14512           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14513           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14514           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14515           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14516           add_AT_loc (die, DW_AT_location, loc_result);
14517           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14518           return true;
14519         }
14520       return false;
14521
14522     case CONST:
14523       if (CONSTANT_P (XEXP (rtl, 0)))
14524         return add_const_value_attribute (die, XEXP (rtl, 0));
14525       /* FALLTHROUGH */
14526     case SYMBOL_REF:
14527       if (!const_ok_for_output (rtl))
14528         return false;
14529     case LABEL_REF:
14530       if (dwarf_version >= 4 || !dwarf_strict)
14531         goto rtl_addr;
14532       return false;
14533
14534     case PLUS:
14535       /* In cases where an inlined instance of an inline function is passed
14536          the address of an `auto' variable (which is local to the caller) we
14537          can get a situation where the DECL_RTL of the artificial local
14538          variable (for the inlining) which acts as a stand-in for the
14539          corresponding formal parameter (of the inline function) will look
14540          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14541          exactly a compile-time constant expression, but it isn't the address
14542          of the (artificial) local variable either.  Rather, it represents the
14543          *value* which the artificial local variable always has during its
14544          lifetime.  We currently have no way to represent such quasi-constant
14545          values in Dwarf, so for now we just punt and generate nothing.  */
14546       return false;
14547
14548     case HIGH:
14549     case CONST_FIXED:
14550       return false;
14551
14552     case MEM:
14553       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14554           && MEM_READONLY_P (rtl)
14555           && GET_MODE (rtl) == BLKmode)
14556         {
14557           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14558           return true;
14559         }
14560       return false;
14561
14562     default:
14563       /* No other kinds of rtx should be possible here.  */
14564       gcc_unreachable ();
14565     }
14566   return false;
14567 }
14568
14569 /* Determine whether the evaluation of EXPR references any variables
14570    or functions which aren't otherwise used (and therefore may not be
14571    output).  */
14572 static tree
14573 reference_to_unused (tree * tp, int * walk_subtrees,
14574                      void * data ATTRIBUTE_UNUSED)
14575 {
14576   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14577     *walk_subtrees = 0;
14578
14579   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14580       && ! TREE_ASM_WRITTEN (*tp))
14581     return *tp;
14582   /* ???  The C++ FE emits debug information for using decls, so
14583      putting gcc_unreachable here falls over.  See PR31899.  For now
14584      be conservative.  */
14585   else if (!cgraph_global_info_ready
14586            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14587     return *tp;
14588   else if (TREE_CODE (*tp) == VAR_DECL)
14589     {
14590       struct varpool_node *node = varpool_get_node (*tp);
14591       if (!node || !node->needed)
14592         return *tp;
14593     }
14594   else if (TREE_CODE (*tp) == FUNCTION_DECL
14595            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14596     {
14597       /* The call graph machinery must have finished analyzing,
14598          optimizing and gimplifying the CU by now.
14599          So if *TP has no call graph node associated
14600          to it, it means *TP will not be emitted.  */
14601       if (!cgraph_get_node (*tp))
14602         return *tp;
14603     }
14604   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14605     return *tp;
14606
14607   return NULL_TREE;
14608 }
14609
14610 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14611    for use in a later add_const_value_attribute call.  */
14612
14613 static rtx
14614 rtl_for_decl_init (tree init, tree type)
14615 {
14616   rtx rtl = NULL_RTX;
14617
14618   STRIP_NOPS (init);
14619
14620   /* If a variable is initialized with a string constant without embedded
14621      zeros, build CONST_STRING.  */
14622   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14623     {
14624       tree enttype = TREE_TYPE (type);
14625       tree domain = TYPE_DOMAIN (type);
14626       enum machine_mode mode = TYPE_MODE (enttype);
14627
14628       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14629           && domain
14630           && integer_zerop (TYPE_MIN_VALUE (domain))
14631           && compare_tree_int (TYPE_MAX_VALUE (domain),
14632                                TREE_STRING_LENGTH (init) - 1) == 0
14633           && ((size_t) TREE_STRING_LENGTH (init)
14634               == strlen (TREE_STRING_POINTER (init)) + 1))
14635         {
14636           rtl = gen_rtx_CONST_STRING (VOIDmode,
14637                                       ggc_strdup (TREE_STRING_POINTER (init)));
14638           rtl = gen_rtx_MEM (BLKmode, rtl);
14639           MEM_READONLY_P (rtl) = 1;
14640         }
14641     }
14642   /* Other aggregates, and complex values, could be represented using
14643      CONCAT: FIXME!  */
14644   else if (AGGREGATE_TYPE_P (type)
14645            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14646                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14647            || TREE_CODE (type) == COMPLEX_TYPE)
14648     ;
14649   /* Vectors only work if their mode is supported by the target.
14650      FIXME: generic vectors ought to work too.  */
14651   else if (TREE_CODE (type) == VECTOR_TYPE
14652            && !VECTOR_MODE_P (TYPE_MODE (type)))
14653     ;
14654   /* If the initializer is something that we know will expand into an
14655      immediate RTL constant, expand it now.  We must be careful not to
14656      reference variables which won't be output.  */
14657   else if (initializer_constant_valid_p (init, type)
14658            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14659     {
14660       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14661          possible.  */
14662       if (TREE_CODE (type) == VECTOR_TYPE)
14663         switch (TREE_CODE (init))
14664           {
14665           case VECTOR_CST:
14666             break;
14667           case CONSTRUCTOR:
14668             if (TREE_CONSTANT (init))
14669               {
14670                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14671                 bool constant_p = true;
14672                 tree value;
14673                 unsigned HOST_WIDE_INT ix;
14674
14675                 /* Even when ctor is constant, it might contain non-*_CST
14676                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14677                    belong into VECTOR_CST nodes.  */
14678                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14679                   if (!CONSTANT_CLASS_P (value))
14680                     {
14681                       constant_p = false;
14682                       break;
14683                     }
14684
14685                 if (constant_p)
14686                   {
14687                     init = build_vector_from_ctor (type, elts);
14688                     break;
14689                   }
14690               }
14691             /* FALLTHRU */
14692
14693           default:
14694             return NULL;
14695           }
14696
14697       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14698
14699       /* If expand_expr returns a MEM, it wasn't immediate.  */
14700       gcc_assert (!rtl || !MEM_P (rtl));
14701     }
14702
14703   return rtl;
14704 }
14705
14706 /* Generate RTL for the variable DECL to represent its location.  */
14707
14708 static rtx
14709 rtl_for_decl_location (tree decl)
14710 {
14711   rtx rtl;
14712
14713   /* Here we have to decide where we are going to say the parameter "lives"
14714      (as far as the debugger is concerned).  We only have a couple of
14715      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14716
14717      DECL_RTL normally indicates where the parameter lives during most of the
14718      activation of the function.  If optimization is enabled however, this
14719      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14720      that the parameter doesn't really live anywhere (as far as the code
14721      generation parts of GCC are concerned) during most of the function's
14722      activation.  That will happen (for example) if the parameter is never
14723      referenced within the function.
14724
14725      We could just generate a location descriptor here for all non-NULL
14726      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14727      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14728      where DECL_RTL is NULL or is a pseudo-reg.
14729
14730      Note however that we can only get away with using DECL_INCOMING_RTL as
14731      a backup substitute for DECL_RTL in certain limited cases.  In cases
14732      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14733      we can be sure that the parameter was passed using the same type as it is
14734      declared to have within the function, and that its DECL_INCOMING_RTL
14735      points us to a place where a value of that type is passed.
14736
14737      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14738      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14739      because in these cases DECL_INCOMING_RTL points us to a value of some
14740      type which is *different* from the type of the parameter itself.  Thus,
14741      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14742      such cases, the debugger would end up (for example) trying to fetch a
14743      `float' from a place which actually contains the first part of a
14744      `double'.  That would lead to really incorrect and confusing
14745      output at debug-time.
14746
14747      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14748      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14749      are a couple of exceptions however.  On little-endian machines we can
14750      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14751      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14752      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14753      when (on a little-endian machine) a non-prototyped function has a
14754      parameter declared to be of type `short' or `char'.  In such cases,
14755      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14756      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14757      passed `int' value.  If the debugger then uses that address to fetch
14758      a `short' or a `char' (on a little-endian machine) the result will be
14759      the correct data, so we allow for such exceptional cases below.
14760
14761      Note that our goal here is to describe the place where the given formal
14762      parameter lives during most of the function's activation (i.e. between the
14763      end of the prologue and the start of the epilogue).  We'll do that as best
14764      as we can. Note however that if the given formal parameter is modified
14765      sometime during the execution of the function, then a stack backtrace (at
14766      debug-time) will show the function as having been called with the *new*
14767      value rather than the value which was originally passed in.  This happens
14768      rarely enough that it is not a major problem, but it *is* a problem, and
14769      I'd like to fix it.
14770
14771      A future version of dwarf2out.c may generate two additional attributes for
14772      any given DW_TAG_formal_parameter DIE which will describe the "passed
14773      type" and the "passed location" for the given formal parameter in addition
14774      to the attributes we now generate to indicate the "declared type" and the
14775      "active location" for each parameter.  This additional set of attributes
14776      could be used by debuggers for stack backtraces. Separately, note that
14777      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14778      This happens (for example) for inlined-instances of inline function formal
14779      parameters which are never referenced.  This really shouldn't be
14780      happening.  All PARM_DECL nodes should get valid non-NULL
14781      DECL_INCOMING_RTL values.  FIXME.  */
14782
14783   /* Use DECL_RTL as the "location" unless we find something better.  */
14784   rtl = DECL_RTL_IF_SET (decl);
14785
14786   /* When generating abstract instances, ignore everything except
14787      constants, symbols living in memory, and symbols living in
14788      fixed registers.  */
14789   if (! reload_completed)
14790     {
14791       if (rtl
14792           && (CONSTANT_P (rtl)
14793               || (MEM_P (rtl)
14794                   && CONSTANT_P (XEXP (rtl, 0)))
14795               || (REG_P (rtl)
14796                   && TREE_CODE (decl) == VAR_DECL
14797                   && TREE_STATIC (decl))))
14798         {
14799           rtl = targetm.delegitimize_address (rtl);
14800           return rtl;
14801         }
14802       rtl = NULL_RTX;
14803     }
14804   else if (TREE_CODE (decl) == PARM_DECL)
14805     {
14806       if (rtl == NULL_RTX
14807           || is_pseudo_reg (rtl)
14808           || (MEM_P (rtl)
14809               && is_pseudo_reg (XEXP (rtl, 0))
14810               && DECL_INCOMING_RTL (decl)
14811               && MEM_P (DECL_INCOMING_RTL (decl))
14812               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14813         {
14814           tree declared_type = TREE_TYPE (decl);
14815           tree passed_type = DECL_ARG_TYPE (decl);
14816           enum machine_mode dmode = TYPE_MODE (declared_type);
14817           enum machine_mode pmode = TYPE_MODE (passed_type);
14818
14819           /* This decl represents a formal parameter which was optimized out.
14820              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14821              all cases where (rtl == NULL_RTX) just below.  */
14822           if (dmode == pmode)
14823             rtl = DECL_INCOMING_RTL (decl);
14824           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14825                    && SCALAR_INT_MODE_P (dmode)
14826                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14827                    && DECL_INCOMING_RTL (decl))
14828             {
14829               rtx inc = DECL_INCOMING_RTL (decl);
14830               if (REG_P (inc))
14831                 rtl = inc;
14832               else if (MEM_P (inc))
14833                 {
14834                   if (BYTES_BIG_ENDIAN)
14835                     rtl = adjust_address_nv (inc, dmode,
14836                                              GET_MODE_SIZE (pmode)
14837                                              - GET_MODE_SIZE (dmode));
14838                   else
14839                     rtl = inc;
14840                 }
14841             }
14842         }
14843
14844       /* If the parm was passed in registers, but lives on the stack, then
14845          make a big endian correction if the mode of the type of the
14846          parameter is not the same as the mode of the rtl.  */
14847       /* ??? This is the same series of checks that are made in dbxout.c before
14848          we reach the big endian correction code there.  It isn't clear if all
14849          of these checks are necessary here, but keeping them all is the safe
14850          thing to do.  */
14851       else if (MEM_P (rtl)
14852                && XEXP (rtl, 0) != const0_rtx
14853                && ! CONSTANT_P (XEXP (rtl, 0))
14854                /* Not passed in memory.  */
14855                && !MEM_P (DECL_INCOMING_RTL (decl))
14856                /* Not passed by invisible reference.  */
14857                && (!REG_P (XEXP (rtl, 0))
14858                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14859                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14860 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14861                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14862 #endif
14863                      )
14864                /* Big endian correction check.  */
14865                && BYTES_BIG_ENDIAN
14866                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14867                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14868                    < UNITS_PER_WORD))
14869         {
14870           int offset = (UNITS_PER_WORD
14871                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14872
14873           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14874                              plus_constant (XEXP (rtl, 0), offset));
14875         }
14876     }
14877   else if (TREE_CODE (decl) == VAR_DECL
14878            && rtl
14879            && MEM_P (rtl)
14880            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14881            && BYTES_BIG_ENDIAN)
14882     {
14883       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14884       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14885
14886       /* If a variable is declared "register" yet is smaller than
14887          a register, then if we store the variable to memory, it
14888          looks like we're storing a register-sized value, when in
14889          fact we are not.  We need to adjust the offset of the
14890          storage location to reflect the actual value's bytes,
14891          else gdb will not be able to display it.  */
14892       if (rsize > dsize)
14893         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14894                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14895     }
14896
14897   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14898      and will have been substituted directly into all expressions that use it.
14899      C does not have such a concept, but C++ and other languages do.  */
14900   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14901     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14902
14903   if (rtl)
14904     rtl = targetm.delegitimize_address (rtl);
14905
14906   /* If we don't look past the constant pool, we risk emitting a
14907      reference to a constant pool entry that isn't referenced from
14908      code, and thus is not emitted.  */
14909   if (rtl)
14910     rtl = avoid_constant_pool_reference (rtl);
14911
14912   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14913      in the current CU, resolve_addr will remove the expression referencing
14914      it.  */
14915   if (rtl == NULL_RTX
14916       && TREE_CODE (decl) == VAR_DECL
14917       && !DECL_EXTERNAL (decl)
14918       && TREE_STATIC (decl)
14919       && DECL_NAME (decl)
14920       && !DECL_HARD_REGISTER (decl)
14921       && DECL_MODE (decl) != VOIDmode)
14922     {
14923       rtl = make_decl_rtl_for_debug (decl);
14924       if (!MEM_P (rtl)
14925           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14926           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14927         rtl = NULL_RTX;
14928     }
14929
14930   return rtl;
14931 }
14932
14933 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14934    returned.  If so, the decl for the COMMON block is returned, and the
14935    value is the offset into the common block for the symbol.  */
14936
14937 static tree
14938 fortran_common (tree decl, HOST_WIDE_INT *value)
14939 {
14940   tree val_expr, cvar;
14941   enum machine_mode mode;
14942   HOST_WIDE_INT bitsize, bitpos;
14943   tree offset;
14944   int volatilep = 0, unsignedp = 0;
14945
14946   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14947      it does not have a value (the offset into the common area), or if it
14948      is thread local (as opposed to global) then it isn't common, and shouldn't
14949      be handled as such.  */
14950   if (TREE_CODE (decl) != VAR_DECL
14951       || !TREE_STATIC (decl)
14952       || !DECL_HAS_VALUE_EXPR_P (decl)
14953       || !is_fortran ())
14954     return NULL_TREE;
14955
14956   val_expr = DECL_VALUE_EXPR (decl);
14957   if (TREE_CODE (val_expr) != COMPONENT_REF)
14958     return NULL_TREE;
14959
14960   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14961                               &mode, &unsignedp, &volatilep, true);
14962
14963   if (cvar == NULL_TREE
14964       || TREE_CODE (cvar) != VAR_DECL
14965       || DECL_ARTIFICIAL (cvar)
14966       || !TREE_PUBLIC (cvar))
14967     return NULL_TREE;
14968
14969   *value = 0;
14970   if (offset != NULL)
14971     {
14972       if (!host_integerp (offset, 0))
14973         return NULL_TREE;
14974       *value = tree_low_cst (offset, 0);
14975     }
14976   if (bitpos != 0)
14977     *value += bitpos / BITS_PER_UNIT;
14978
14979   return cvar;
14980 }
14981
14982 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14983    data attribute for a variable or a parameter.  We generate the
14984    DW_AT_const_value attribute only in those cases where the given variable
14985    or parameter does not have a true "location" either in memory or in a
14986    register.  This can happen (for example) when a constant is passed as an
14987    actual argument in a call to an inline function.  (It's possible that
14988    these things can crop up in other ways also.)  Note that one type of
14989    constant value which can be passed into an inlined function is a constant
14990    pointer.  This can happen for example if an actual argument in an inlined
14991    function call evaluates to a compile-time constant address.
14992
14993    CACHE_P is true if it is worth caching the location list for DECL,
14994    so that future calls can reuse it rather than regenerate it from scratch.
14995    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14996    since we will need to refer to them each time the function is inlined.  */
14997
14998 static bool
14999 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15000                                        enum dwarf_attribute attr)
15001 {
15002   rtx rtl;
15003   dw_loc_list_ref list;
15004   var_loc_list *loc_list;
15005   cached_dw_loc_list *cache;
15006   void **slot;
15007
15008   if (TREE_CODE (decl) == ERROR_MARK)
15009     return false;
15010
15011   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15012               || TREE_CODE (decl) == RESULT_DECL);
15013
15014   /* Try to get some constant RTL for this decl, and use that as the value of
15015      the location.  */
15016
15017   rtl = rtl_for_decl_location (decl);
15018   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15019       && add_const_value_attribute (die, rtl))
15020     return true;
15021
15022   /* See if we have single element location list that is equivalent to
15023      a constant value.  That way we are better to use add_const_value_attribute
15024      rather than expanding constant value equivalent.  */
15025   loc_list = lookup_decl_loc (decl);
15026   if (loc_list
15027       && loc_list->first
15028       && loc_list->first->next == NULL
15029       && NOTE_P (loc_list->first->loc)
15030       && NOTE_VAR_LOCATION (loc_list->first->loc)
15031       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15032     {
15033       struct var_loc_node *node;
15034
15035       node = loc_list->first;
15036       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15037       if (GET_CODE (rtl) == EXPR_LIST)
15038         rtl = XEXP (rtl, 0);
15039       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15040           && add_const_value_attribute (die, rtl))
15041          return true;
15042     }
15043   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15044      list several times.  See if we've already cached the contents.  */
15045   list = NULL;
15046   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15047     cache_p = false;
15048   if (cache_p)
15049     {
15050       cache = (cached_dw_loc_list *)
15051         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15052       if (cache)
15053         list = cache->loc_list;
15054     }
15055   if (list == NULL)
15056     {
15057       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15058       /* It is usually worth caching this result if the decl is from
15059          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15060       if (cache_p && list && list->dw_loc_next)
15061         {
15062           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15063                                            DECL_UID (decl), INSERT);
15064           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15065           cache->decl_id = DECL_UID (decl);
15066           cache->loc_list = list;
15067           *slot = cache;
15068         }
15069     }
15070   if (list)
15071     {
15072       add_AT_location_description (die, attr, list);
15073       return true;
15074     }
15075   /* None of that worked, so it must not really have a location;
15076      try adding a constant value attribute from the DECL_INITIAL.  */
15077   return tree_add_const_value_attribute_for_decl (die, decl);
15078 }
15079
15080 /* Add VARIABLE and DIE into deferred locations list.  */
15081
15082 static void
15083 defer_location (tree variable, dw_die_ref die)
15084 {
15085   deferred_locations entry;
15086   entry.variable = variable;
15087   entry.die = die;
15088   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15089 }
15090
15091 /* Helper function for tree_add_const_value_attribute.  Natively encode
15092    initializer INIT into an array.  Return true if successful.  */
15093
15094 static bool
15095 native_encode_initializer (tree init, unsigned char *array, int size)
15096 {
15097   tree type;
15098
15099   if (init == NULL_TREE)
15100     return false;
15101
15102   STRIP_NOPS (init);
15103   switch (TREE_CODE (init))
15104     {
15105     case STRING_CST:
15106       type = TREE_TYPE (init);
15107       if (TREE_CODE (type) == ARRAY_TYPE)
15108         {
15109           tree enttype = TREE_TYPE (type);
15110           enum machine_mode mode = TYPE_MODE (enttype);
15111
15112           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15113             return false;
15114           if (int_size_in_bytes (type) != size)
15115             return false;
15116           if (size > TREE_STRING_LENGTH (init))
15117             {
15118               memcpy (array, TREE_STRING_POINTER (init),
15119                       TREE_STRING_LENGTH (init));
15120               memset (array + TREE_STRING_LENGTH (init),
15121                       '\0', size - TREE_STRING_LENGTH (init));
15122             }
15123           else
15124             memcpy (array, TREE_STRING_POINTER (init), size);
15125           return true;
15126         }
15127       return false;
15128     case CONSTRUCTOR:
15129       type = TREE_TYPE (init);
15130       if (int_size_in_bytes (type) != size)
15131         return false;
15132       if (TREE_CODE (type) == ARRAY_TYPE)
15133         {
15134           HOST_WIDE_INT min_index;
15135           unsigned HOST_WIDE_INT cnt;
15136           int curpos = 0, fieldsize;
15137           constructor_elt *ce;
15138
15139           if (TYPE_DOMAIN (type) == NULL_TREE
15140               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15141             return false;
15142
15143           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15144           if (fieldsize <= 0)
15145             return false;
15146
15147           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15148           memset (array, '\0', size);
15149           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15150             {
15151               tree val = ce->value;
15152               tree index = ce->index;
15153               int pos = curpos;
15154               if (index && TREE_CODE (index) == RANGE_EXPR)
15155                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15156                       * fieldsize;
15157               else if (index)
15158                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15159
15160               if (val)
15161                 {
15162                   STRIP_NOPS (val);
15163                   if (!native_encode_initializer (val, array + pos, fieldsize))
15164                     return false;
15165                 }
15166               curpos = pos + fieldsize;
15167               if (index && TREE_CODE (index) == RANGE_EXPR)
15168                 {
15169                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15170                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15171                   while (count-- > 0)
15172                     {
15173                       if (val)
15174                         memcpy (array + curpos, array + pos, fieldsize);
15175                       curpos += fieldsize;
15176                     }
15177                 }
15178               gcc_assert (curpos <= size);
15179             }
15180           return true;
15181         }
15182       else if (TREE_CODE (type) == RECORD_TYPE
15183                || TREE_CODE (type) == UNION_TYPE)
15184         {
15185           tree field = NULL_TREE;
15186           unsigned HOST_WIDE_INT cnt;
15187           constructor_elt *ce;
15188
15189           if (int_size_in_bytes (type) != size)
15190             return false;
15191
15192           if (TREE_CODE (type) == RECORD_TYPE)
15193             field = TYPE_FIELDS (type);
15194
15195           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15196             {
15197               tree val = ce->value;
15198               int pos, fieldsize;
15199
15200               if (ce->index != 0)
15201                 field = ce->index;
15202
15203               if (val)
15204                 STRIP_NOPS (val);
15205
15206               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15207                 return false;
15208
15209               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15210                   && TYPE_DOMAIN (TREE_TYPE (field))
15211                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15212                 return false;
15213               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15214                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15215                 return false;
15216               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15217               pos = int_byte_position (field);
15218               gcc_assert (pos + fieldsize <= size);
15219               if (val
15220                   && !native_encode_initializer (val, array + pos, fieldsize))
15221                 return false;
15222             }
15223           return true;
15224         }
15225       return false;
15226     case VIEW_CONVERT_EXPR:
15227     case NON_LVALUE_EXPR:
15228       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15229     default:
15230       return native_encode_expr (init, array, size) == size;
15231     }
15232 }
15233
15234 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15235    attribute is the const value T.  */
15236
15237 static bool
15238 tree_add_const_value_attribute (dw_die_ref die, tree t)
15239 {
15240   tree init;
15241   tree type = TREE_TYPE (t);
15242   rtx rtl;
15243
15244   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15245     return false;
15246
15247   init = t;
15248   gcc_assert (!DECL_P (init));
15249
15250   rtl = rtl_for_decl_init (init, type);
15251   if (rtl)
15252     return add_const_value_attribute (die, rtl);
15253   /* If the host and target are sane, try harder.  */
15254   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15255            && initializer_constant_valid_p (init, type))
15256     {
15257       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15258       if (size > 0 && (int) size == size)
15259         {
15260           unsigned char *array = (unsigned char *)
15261             ggc_alloc_cleared_atomic (size);
15262
15263           if (native_encode_initializer (init, array, size))
15264             {
15265               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15266               return true;
15267             }
15268         }
15269     }
15270   return false;
15271 }
15272
15273 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15274    attribute is the const value of T, where T is an integral constant
15275    variable with static storage duration
15276    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15277
15278 static bool
15279 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15280 {
15281
15282   if (!decl
15283       || (TREE_CODE (decl) != VAR_DECL
15284           && TREE_CODE (decl) != CONST_DECL)
15285       || (TREE_CODE (decl) == VAR_DECL
15286           && !TREE_STATIC (decl)))
15287     return false;
15288
15289     if (TREE_READONLY (decl)
15290         && ! TREE_THIS_VOLATILE (decl)
15291         && DECL_INITIAL (decl))
15292       /* OK */;
15293     else
15294       return false;
15295
15296   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15297   if (get_AT (var_die, DW_AT_const_value))
15298     return false;
15299
15300   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15301 }
15302
15303 /* Convert the CFI instructions for the current function into a
15304    location list.  This is used for DW_AT_frame_base when we targeting
15305    a dwarf2 consumer that does not support the dwarf3
15306    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15307    expressions.  */
15308
15309 static dw_loc_list_ref
15310 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15311 {
15312   int ix;
15313   dw_fde_ref fde;
15314   dw_loc_list_ref list, *list_tail;
15315   dw_cfi_ref cfi;
15316   dw_cfa_location last_cfa, next_cfa;
15317   const char *start_label, *last_label, *section;
15318   dw_cfa_location remember;
15319
15320   fde = cfun->fde;
15321   gcc_assert (fde != NULL);
15322
15323   section = secname_for_decl (current_function_decl);
15324   list_tail = &list;
15325   list = NULL;
15326
15327   memset (&next_cfa, 0, sizeof (next_cfa));
15328   next_cfa.reg = INVALID_REGNUM;
15329   remember = next_cfa;
15330
15331   start_label = fde->dw_fde_begin;
15332
15333   /* ??? Bald assumption that the CIE opcode list does not contain
15334      advance opcodes.  */
15335   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15336     lookup_cfa_1 (cfi, &next_cfa, &remember);
15337
15338   last_cfa = next_cfa;
15339   last_label = start_label;
15340
15341   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15342     {
15343       /* If the first partition contained no CFI adjustments, the
15344          CIE opcodes apply to the whole first partition.  */
15345       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15346                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15347       list_tail =&(*list_tail)->dw_loc_next;
15348       start_label = last_label = fde->dw_fde_second_begin;
15349     }
15350
15351   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15352     {
15353       switch (cfi->dw_cfi_opc)
15354         {
15355         case DW_CFA_set_loc:
15356         case DW_CFA_advance_loc1:
15357         case DW_CFA_advance_loc2:
15358         case DW_CFA_advance_loc4:
15359           if (!cfa_equal_p (&last_cfa, &next_cfa))
15360             {
15361               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15362                                          start_label, last_label, section);
15363
15364               list_tail = &(*list_tail)->dw_loc_next;
15365               last_cfa = next_cfa;
15366               start_label = last_label;
15367             }
15368           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15369           break;
15370
15371         case DW_CFA_advance_loc:
15372           /* The encoding is complex enough that we should never emit this.  */
15373           gcc_unreachable ();
15374
15375         default:
15376           lookup_cfa_1 (cfi, &next_cfa, &remember);
15377           break;
15378         }
15379       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15380         {
15381           if (!cfa_equal_p (&last_cfa, &next_cfa))
15382             {
15383               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15384                                          start_label, last_label, section);
15385
15386               list_tail = &(*list_tail)->dw_loc_next;
15387               last_cfa = next_cfa;
15388               start_label = last_label;
15389             }
15390           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15391                                      start_label, fde->dw_fde_end, section);
15392           list_tail = &(*list_tail)->dw_loc_next;
15393           start_label = last_label = fde->dw_fde_second_begin;
15394         }
15395     }
15396
15397   if (!cfa_equal_p (&last_cfa, &next_cfa))
15398     {
15399       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15400                                  start_label, last_label, section);
15401       list_tail = &(*list_tail)->dw_loc_next;
15402       start_label = last_label;
15403     }
15404
15405   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15406                              start_label,
15407                              fde->dw_fde_second_begin
15408                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15409                              section);
15410
15411   if (list && list->dw_loc_next)
15412     gen_llsym (list);
15413
15414   return list;
15415 }
15416
15417 /* Compute a displacement from the "steady-state frame pointer" to the
15418    frame base (often the same as the CFA), and store it in
15419    frame_pointer_fb_offset.  OFFSET is added to the displacement
15420    before the latter is negated.  */
15421
15422 static void
15423 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15424 {
15425   rtx reg, elim;
15426
15427 #ifdef FRAME_POINTER_CFA_OFFSET
15428   reg = frame_pointer_rtx;
15429   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15430 #else
15431   reg = arg_pointer_rtx;
15432   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15433 #endif
15434
15435   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15436   if (GET_CODE (elim) == PLUS)
15437     {
15438       offset += INTVAL (XEXP (elim, 1));
15439       elim = XEXP (elim, 0);
15440     }
15441
15442   frame_pointer_fb_offset = -offset;
15443
15444   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15445      in which to eliminate.  This is because it's stack pointer isn't 
15446      directly accessible as a register within the ISA.  To work around
15447      this, assume that while we cannot provide a proper value for
15448      frame_pointer_fb_offset, we won't need one either.  */
15449   frame_pointer_fb_offset_valid
15450     = ((SUPPORTS_STACK_ALIGNMENT
15451         && (elim == hard_frame_pointer_rtx
15452             || elim == stack_pointer_rtx))
15453        || elim == (frame_pointer_needed
15454                    ? hard_frame_pointer_rtx
15455                    : stack_pointer_rtx));
15456 }
15457
15458 /* Generate a DW_AT_name attribute given some string value to be included as
15459    the value of the attribute.  */
15460
15461 static void
15462 add_name_attribute (dw_die_ref die, const char *name_string)
15463 {
15464   if (name_string != NULL && *name_string != 0)
15465     {
15466       if (demangle_name_func)
15467         name_string = (*demangle_name_func) (name_string);
15468
15469       add_AT_string (die, DW_AT_name, name_string);
15470     }
15471 }
15472
15473 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15474    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15475    of TYPE accordingly.
15476
15477    ??? This is a temporary measure until after we're able to generate
15478    regular DWARF for the complex Ada type system.  */
15479
15480 static void 
15481 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15482                                      dw_die_ref context_die)
15483 {
15484   tree dtype;
15485   dw_die_ref dtype_die;
15486
15487   if (!lang_hooks.types.descriptive_type)
15488     return;
15489
15490   dtype = lang_hooks.types.descriptive_type (type);
15491   if (!dtype)
15492     return;
15493
15494   dtype_die = lookup_type_die (dtype);
15495   if (!dtype_die)
15496     {
15497       gen_type_die (dtype, context_die);
15498       dtype_die = lookup_type_die (dtype);
15499       gcc_assert (dtype_die);
15500     }
15501
15502   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15503 }
15504
15505 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15506
15507 static void
15508 add_comp_dir_attribute (dw_die_ref die)
15509 {
15510   const char *wd = get_src_pwd ();
15511   char *wd1;
15512
15513   if (wd == NULL)
15514     return;
15515
15516   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15517     {
15518       int wdlen;
15519
15520       wdlen = strlen (wd);
15521       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15522       strcpy (wd1, wd);
15523       wd1 [wdlen] = DIR_SEPARATOR;
15524       wd1 [wdlen + 1] = 0;
15525       wd = wd1;
15526     }
15527
15528     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15529 }
15530
15531 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15532    default.  */
15533
15534 static int
15535 lower_bound_default (void)
15536 {
15537   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15538     {
15539     case DW_LANG_C:
15540     case DW_LANG_C89:
15541     case DW_LANG_C99:
15542     case DW_LANG_C_plus_plus:
15543     case DW_LANG_ObjC:
15544     case DW_LANG_ObjC_plus_plus:
15545     case DW_LANG_Java:
15546       return 0;
15547     case DW_LANG_Fortran77:
15548     case DW_LANG_Fortran90:
15549     case DW_LANG_Fortran95:
15550       return 1;
15551     case DW_LANG_UPC:
15552     case DW_LANG_D:
15553     case DW_LANG_Python:
15554       return dwarf_version >= 4 ? 0 : -1;
15555     case DW_LANG_Ada95:
15556     case DW_LANG_Ada83:
15557     case DW_LANG_Cobol74:
15558     case DW_LANG_Cobol85:
15559     case DW_LANG_Pascal83:
15560     case DW_LANG_Modula2:
15561     case DW_LANG_PLI:
15562       return dwarf_version >= 4 ? 1 : -1;
15563     default:
15564       return -1;
15565     }
15566 }
15567
15568 /* Given a tree node describing an array bound (either lower or upper) output
15569    a representation for that bound.  */
15570
15571 static void
15572 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15573 {
15574   switch (TREE_CODE (bound))
15575     {
15576     case ERROR_MARK:
15577       return;
15578
15579     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15580     case INTEGER_CST:
15581       {
15582         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15583         int dflt;
15584
15585         /* Use the default if possible.  */
15586         if (bound_attr == DW_AT_lower_bound
15587             && host_integerp (bound, 0)
15588             && (dflt = lower_bound_default ()) != -1
15589             && tree_low_cst (bound, 0) == dflt)
15590           ;
15591
15592         /* Otherwise represent the bound as an unsigned value with the
15593            precision of its type.  The precision and signedness of the
15594            type will be necessary to re-interpret it unambiguously.  */
15595         else if (prec < HOST_BITS_PER_WIDE_INT)
15596           {
15597             unsigned HOST_WIDE_INT mask
15598               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15599             add_AT_unsigned (subrange_die, bound_attr,
15600                              TREE_INT_CST_LOW (bound) & mask);
15601           }
15602         else if (prec == HOST_BITS_PER_WIDE_INT
15603                  || TREE_INT_CST_HIGH (bound) == 0)
15604           add_AT_unsigned (subrange_die, bound_attr,
15605                            TREE_INT_CST_LOW (bound));
15606         else
15607           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15608                          TREE_INT_CST_LOW (bound));
15609       }
15610       break;
15611
15612     CASE_CONVERT:
15613     case VIEW_CONVERT_EXPR:
15614       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15615       break;
15616
15617     case SAVE_EXPR:
15618       break;
15619
15620     case VAR_DECL:
15621     case PARM_DECL:
15622     case RESULT_DECL:
15623       {
15624         dw_die_ref decl_die = lookup_decl_die (bound);
15625
15626         /* ??? Can this happen, or should the variable have been bound
15627            first?  Probably it can, since I imagine that we try to create
15628            the types of parameters in the order in which they exist in
15629            the list, and won't have created a forward reference to a
15630            later parameter.  */
15631         if (decl_die != NULL)
15632           {
15633             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15634             break;
15635           }
15636       }
15637       /* FALLTHRU */
15638
15639     default:
15640       {
15641         /* Otherwise try to create a stack operation procedure to
15642            evaluate the value of the array bound.  */
15643
15644         dw_die_ref ctx, decl_die;
15645         dw_loc_list_ref list;
15646
15647         list = loc_list_from_tree (bound, 2);
15648         if (list == NULL || single_element_loc_list_p (list))
15649           {
15650             /* If DW_AT_*bound is not a reference nor constant, it is
15651                a DWARF expression rather than location description.
15652                For that loc_list_from_tree (bound, 0) is needed.
15653                If that fails to give a single element list,
15654                fall back to outputting this as a reference anyway.  */
15655             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15656             if (list2 && single_element_loc_list_p (list2))
15657               {
15658                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15659                 break;
15660               }
15661           }
15662         if (list == NULL)
15663           break;
15664
15665         if (current_function_decl == 0)
15666           ctx = comp_unit_die ();
15667         else
15668           ctx = lookup_decl_die (current_function_decl);
15669
15670         decl_die = new_die (DW_TAG_variable, ctx, bound);
15671         add_AT_flag (decl_die, DW_AT_artificial, 1);
15672         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15673         add_AT_location_description (decl_die, DW_AT_location, list);
15674         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15675         break;
15676       }
15677     }
15678 }
15679
15680 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15681    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15682    Note that the block of subscript information for an array type also
15683    includes information about the element type of the given array type.  */
15684
15685 static void
15686 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15687 {
15688   unsigned dimension_number;
15689   tree lower, upper;
15690   dw_die_ref subrange_die;
15691
15692   for (dimension_number = 0;
15693        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15694        type = TREE_TYPE (type), dimension_number++)
15695     {
15696       tree domain = TYPE_DOMAIN (type);
15697
15698       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15699         break;
15700
15701       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15702          and (in GNU C only) variable bounds.  Handle all three forms
15703          here.  */
15704       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15705       if (domain)
15706         {
15707           /* We have an array type with specified bounds.  */
15708           lower = TYPE_MIN_VALUE (domain);
15709           upper = TYPE_MAX_VALUE (domain);
15710
15711           /* Define the index type.  */
15712           if (TREE_TYPE (domain))
15713             {
15714               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15715                  TREE_TYPE field.  We can't emit debug info for this
15716                  because it is an unnamed integral type.  */
15717               if (TREE_CODE (domain) == INTEGER_TYPE
15718                   && TYPE_NAME (domain) == NULL_TREE
15719                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15720                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15721                 ;
15722               else
15723                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15724                                     type_die);
15725             }
15726
15727           /* ??? If upper is NULL, the array has unspecified length,
15728              but it does have a lower bound.  This happens with Fortran
15729                dimension arr(N:*)
15730              Since the debugger is definitely going to need to know N
15731              to produce useful results, go ahead and output the lower
15732              bound solo, and hope the debugger can cope.  */
15733
15734           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15735           if (upper)
15736             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15737         }
15738
15739       /* Otherwise we have an array type with an unspecified length.  The
15740          DWARF-2 spec does not say how to handle this; let's just leave out the
15741          bounds.  */
15742     }
15743 }
15744
15745 static void
15746 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15747 {
15748   unsigned size;
15749
15750   switch (TREE_CODE (tree_node))
15751     {
15752     case ERROR_MARK:
15753       size = 0;
15754       break;
15755     case ENUMERAL_TYPE:
15756     case RECORD_TYPE:
15757     case UNION_TYPE:
15758     case QUAL_UNION_TYPE:
15759       size = int_size_in_bytes (tree_node);
15760       break;
15761     case FIELD_DECL:
15762       /* For a data member of a struct or union, the DW_AT_byte_size is
15763          generally given as the number of bytes normally allocated for an
15764          object of the *declared* type of the member itself.  This is true
15765          even for bit-fields.  */
15766       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15767       break;
15768     default:
15769       gcc_unreachable ();
15770     }
15771
15772   /* Note that `size' might be -1 when we get to this point.  If it is, that
15773      indicates that the byte size of the entity in question is variable.  We
15774      have no good way of expressing this fact in Dwarf at the present time,
15775      so just let the -1 pass on through.  */
15776   add_AT_unsigned (die, DW_AT_byte_size, size);
15777 }
15778
15779 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15780    which specifies the distance in bits from the highest order bit of the
15781    "containing object" for the bit-field to the highest order bit of the
15782    bit-field itself.
15783
15784    For any given bit-field, the "containing object" is a hypothetical object
15785    (of some integral or enum type) within which the given bit-field lives.  The
15786    type of this hypothetical "containing object" is always the same as the
15787    declared type of the individual bit-field itself.  The determination of the
15788    exact location of the "containing object" for a bit-field is rather
15789    complicated.  It's handled by the `field_byte_offset' function (above).
15790
15791    Note that it is the size (in bytes) of the hypothetical "containing object"
15792    which will be given in the DW_AT_byte_size attribute for this bit-field.
15793    (See `byte_size_attribute' above).  */
15794
15795 static inline void
15796 add_bit_offset_attribute (dw_die_ref die, tree decl)
15797 {
15798   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15799   tree type = DECL_BIT_FIELD_TYPE (decl);
15800   HOST_WIDE_INT bitpos_int;
15801   HOST_WIDE_INT highest_order_object_bit_offset;
15802   HOST_WIDE_INT highest_order_field_bit_offset;
15803   HOST_WIDE_INT bit_offset;
15804
15805   /* Must be a field and a bit field.  */
15806   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15807
15808   /* We can't yet handle bit-fields whose offsets are variable, so if we
15809      encounter such things, just return without generating any attribute
15810      whatsoever.  Likewise for variable or too large size.  */
15811   if (! host_integerp (bit_position (decl), 0)
15812       || ! host_integerp (DECL_SIZE (decl), 1))
15813     return;
15814
15815   bitpos_int = int_bit_position (decl);
15816
15817   /* Note that the bit offset is always the distance (in bits) from the
15818      highest-order bit of the "containing object" to the highest-order bit of
15819      the bit-field itself.  Since the "high-order end" of any object or field
15820      is different on big-endian and little-endian machines, the computation
15821      below must take account of these differences.  */
15822   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15823   highest_order_field_bit_offset = bitpos_int;
15824
15825   if (! BYTES_BIG_ENDIAN)
15826     {
15827       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15828       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15829     }
15830
15831   bit_offset
15832     = (! BYTES_BIG_ENDIAN
15833        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15834        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15835
15836   if (bit_offset < 0)
15837     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15838   else
15839     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15840 }
15841
15842 /* For a FIELD_DECL node which represents a bit field, output an attribute
15843    which specifies the length in bits of the given field.  */
15844
15845 static inline void
15846 add_bit_size_attribute (dw_die_ref die, tree decl)
15847 {
15848   /* Must be a field and a bit field.  */
15849   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15850               && DECL_BIT_FIELD_TYPE (decl));
15851
15852   if (host_integerp (DECL_SIZE (decl), 1))
15853     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15854 }
15855
15856 /* If the compiled language is ANSI C, then add a 'prototyped'
15857    attribute, if arg types are given for the parameters of a function.  */
15858
15859 static inline void
15860 add_prototyped_attribute (dw_die_ref die, tree func_type)
15861 {
15862   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15863       && prototype_p (func_type))
15864     add_AT_flag (die, DW_AT_prototyped, 1);
15865 }
15866
15867 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15868    by looking in either the type declaration or object declaration
15869    equate table.  */
15870
15871 static inline dw_die_ref
15872 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15873 {
15874   dw_die_ref origin_die = NULL;
15875
15876   if (TREE_CODE (origin) != FUNCTION_DECL)
15877     {
15878       /* We may have gotten separated from the block for the inlined
15879          function, if we're in an exception handler or some such; make
15880          sure that the abstract function has been written out.
15881
15882          Doing this for nested functions is wrong, however; functions are
15883          distinct units, and our context might not even be inline.  */
15884       tree fn = origin;
15885
15886       if (TYPE_P (fn))
15887         fn = TYPE_STUB_DECL (fn);
15888
15889       fn = decl_function_context (fn);
15890       if (fn)
15891         dwarf2out_abstract_function (fn);
15892     }
15893
15894   if (DECL_P (origin))
15895     origin_die = lookup_decl_die (origin);
15896   else if (TYPE_P (origin))
15897     origin_die = lookup_type_die (origin);
15898
15899   /* XXX: Functions that are never lowered don't always have correct block
15900      trees (in the case of java, they simply have no block tree, in some other
15901      languages).  For these functions, there is nothing we can really do to
15902      output correct debug info for inlined functions in all cases.  Rather
15903      than die, we'll just produce deficient debug info now, in that we will
15904      have variables without a proper abstract origin.  In the future, when all
15905      functions are lowered, we should re-add a gcc_assert (origin_die)
15906      here.  */
15907
15908   if (origin_die)
15909     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15910   return origin_die;
15911 }
15912
15913 /* We do not currently support the pure_virtual attribute.  */
15914
15915 static inline void
15916 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15917 {
15918   if (DECL_VINDEX (func_decl))
15919     {
15920       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15921
15922       if (host_integerp (DECL_VINDEX (func_decl), 0))
15923         add_AT_loc (die, DW_AT_vtable_elem_location,
15924                     new_loc_descr (DW_OP_constu,
15925                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15926                                    0));
15927
15928       /* GNU extension: Record what type this method came from originally.  */
15929       if (debug_info_level > DINFO_LEVEL_TERSE
15930           && DECL_CONTEXT (func_decl))
15931         add_AT_die_ref (die, DW_AT_containing_type,
15932                         lookup_type_die (DECL_CONTEXT (func_decl)));
15933     }
15934 }
15935 \f
15936 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15937    given decl.  This used to be a vendor extension until after DWARF 4
15938    standardized it.  */
15939
15940 static void
15941 add_linkage_attr (dw_die_ref die, tree decl)
15942 {
15943   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15944
15945   /* Mimic what assemble_name_raw does with a leading '*'.  */
15946   if (name[0] == '*')
15947     name = &name[1];
15948
15949   if (dwarf_version >= 4)
15950     add_AT_string (die, DW_AT_linkage_name, name);
15951   else
15952     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15953 }
15954
15955 /* Add source coordinate attributes for the given decl.  */
15956
15957 static void
15958 add_src_coords_attributes (dw_die_ref die, tree decl)
15959 {
15960   expanded_location s;
15961
15962   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15963     return;
15964   s = expand_location (DECL_SOURCE_LOCATION (decl));
15965   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15966   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15967 }
15968
15969 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15970
15971 static void
15972 add_linkage_name (dw_die_ref die, tree decl)
15973 {
15974   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15975        && TREE_PUBLIC (decl)
15976        && !DECL_ABSTRACT (decl)
15977        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15978        && die->die_tag != DW_TAG_member)
15979     {
15980       /* Defer until we have an assembler name set.  */
15981       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15982         {
15983           limbo_die_node *asm_name;
15984
15985           asm_name = ggc_alloc_cleared_limbo_die_node ();
15986           asm_name->die = die;
15987           asm_name->created_for = decl;
15988           asm_name->next = deferred_asm_name;
15989           deferred_asm_name = asm_name;
15990         }
15991       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15992         add_linkage_attr (die, decl);
15993     }
15994 }
15995
15996 /* Add a DW_AT_name attribute and source coordinate attribute for the
15997    given decl, but only if it actually has a name.  */
15998
15999 static void
16000 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16001 {
16002   tree decl_name;
16003
16004   decl_name = DECL_NAME (decl);
16005   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16006     {
16007       const char *name = dwarf2_name (decl, 0);
16008       if (name)
16009         add_name_attribute (die, name);
16010       if (! DECL_ARTIFICIAL (decl))
16011         add_src_coords_attributes (die, decl);
16012
16013       add_linkage_name (die, decl);
16014     }
16015
16016 #ifdef VMS_DEBUGGING_INFO
16017   /* Get the function's name, as described by its RTL.  This may be different
16018      from the DECL_NAME name used in the source file.  */
16019   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16020     {
16021       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16022                    XEXP (DECL_RTL (decl), 0));
16023       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16024     }
16025 #endif /* VMS_DEBUGGING_INFO */
16026 }
16027
16028 #ifdef VMS_DEBUGGING_INFO
16029 /* Output the debug main pointer die for VMS */
16030
16031 void
16032 dwarf2out_vms_debug_main_pointer (void)
16033 {
16034   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16035   dw_die_ref die;
16036
16037   /* Allocate the VMS debug main subprogram die.  */
16038   die = ggc_alloc_cleared_die_node ();
16039   die->die_tag = DW_TAG_subprogram;
16040   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16041   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16042                                current_function_funcdef_no);
16043   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16044
16045   /* Make it the first child of comp_unit_die ().  */
16046   die->die_parent = comp_unit_die ();
16047   if (comp_unit_die ()->die_child)
16048     {
16049       die->die_sib = comp_unit_die ()->die_child->die_sib;
16050       comp_unit_die ()->die_child->die_sib = die;
16051     }
16052   else
16053     {
16054       die->die_sib = die;
16055       comp_unit_die ()->die_child = die;
16056     }
16057 }
16058 #endif /* VMS_DEBUGGING_INFO */
16059
16060 /* Push a new declaration scope.  */
16061
16062 static void
16063 push_decl_scope (tree scope)
16064 {
16065   VEC_safe_push (tree, gc, decl_scope_table, scope);
16066 }
16067
16068 /* Pop a declaration scope.  */
16069
16070 static inline void
16071 pop_decl_scope (void)
16072 {
16073   VEC_pop (tree, decl_scope_table);
16074 }
16075
16076 /* Return the DIE for the scope that immediately contains this type.
16077    Non-named types get global scope.  Named types nested in other
16078    types get their containing scope if it's open, or global scope
16079    otherwise.  All other types (i.e. function-local named types) get
16080    the current active scope.  */
16081
16082 static dw_die_ref
16083 scope_die_for (tree t, dw_die_ref context_die)
16084 {
16085   dw_die_ref scope_die = NULL;
16086   tree containing_scope;
16087   int i;
16088
16089   /* Non-types always go in the current scope.  */
16090   gcc_assert (TYPE_P (t));
16091
16092   containing_scope = TYPE_CONTEXT (t);
16093
16094   /* Use the containing namespace if it was passed in (for a declaration).  */
16095   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16096     {
16097       if (context_die == lookup_decl_die (containing_scope))
16098         /* OK */;
16099       else
16100         containing_scope = NULL_TREE;
16101     }
16102
16103   /* Ignore function type "scopes" from the C frontend.  They mean that
16104      a tagged type is local to a parmlist of a function declarator, but
16105      that isn't useful to DWARF.  */
16106   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16107     containing_scope = NULL_TREE;
16108
16109   if (SCOPE_FILE_SCOPE_P (containing_scope))
16110     scope_die = comp_unit_die ();
16111   else if (TYPE_P (containing_scope))
16112     {
16113       /* For types, we can just look up the appropriate DIE.  But
16114          first we check to see if we're in the middle of emitting it
16115          so we know where the new DIE should go.  */
16116       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16117         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16118           break;
16119
16120       if (i < 0)
16121         {
16122           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16123                       || TREE_ASM_WRITTEN (containing_scope));
16124           /*We are not in the middle of emitting the type
16125             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16126           scope_die = lookup_type_die (containing_scope);
16127
16128           /* If none of the current dies are suitable, we get file scope.  */
16129           if (scope_die == NULL)
16130             scope_die = comp_unit_die ();
16131         }
16132       else
16133         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16134     }
16135   else
16136     scope_die = context_die;
16137
16138   return scope_die;
16139 }
16140
16141 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16142
16143 static inline int
16144 local_scope_p (dw_die_ref context_die)
16145 {
16146   for (; context_die; context_die = context_die->die_parent)
16147     if (context_die->die_tag == DW_TAG_inlined_subroutine
16148         || context_die->die_tag == DW_TAG_subprogram)
16149       return 1;
16150
16151   return 0;
16152 }
16153
16154 /* Returns nonzero if CONTEXT_DIE is a class.  */
16155
16156 static inline int
16157 class_scope_p (dw_die_ref context_die)
16158 {
16159   return (context_die
16160           && (context_die->die_tag == DW_TAG_structure_type
16161               || context_die->die_tag == DW_TAG_class_type
16162               || context_die->die_tag == DW_TAG_interface_type
16163               || context_die->die_tag == DW_TAG_union_type));
16164 }
16165
16166 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16167    whether or not to treat a DIE in this context as a declaration.  */
16168
16169 static inline int
16170 class_or_namespace_scope_p (dw_die_ref context_die)
16171 {
16172   return (class_scope_p (context_die)
16173           || (context_die && context_die->die_tag == DW_TAG_namespace));
16174 }
16175
16176 /* Many forms of DIEs require a "type description" attribute.  This
16177    routine locates the proper "type descriptor" die for the type given
16178    by 'type', and adds a DW_AT_type attribute below the given die.  */
16179
16180 static void
16181 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16182                     int decl_volatile, dw_die_ref context_die)
16183 {
16184   enum tree_code code  = TREE_CODE (type);
16185   dw_die_ref type_die  = NULL;
16186
16187   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16188      or fixed-point type, use the inner type.  This is because we have no
16189      support for unnamed types in base_type_die.  This can happen if this is
16190      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16191   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16192       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16193     type = TREE_TYPE (type), code = TREE_CODE (type);
16194
16195   if (code == ERROR_MARK
16196       /* Handle a special case.  For functions whose return type is void, we
16197          generate *no* type attribute.  (Note that no object may have type
16198          `void', so this only applies to function return types).  */
16199       || code == VOID_TYPE)
16200     return;
16201
16202   type_die = modified_type_die (type,
16203                                 decl_const || TYPE_READONLY (type),
16204                                 decl_volatile || TYPE_VOLATILE (type),
16205                                 context_die);
16206
16207   if (type_die != NULL)
16208     add_AT_die_ref (object_die, DW_AT_type, type_die);
16209 }
16210
16211 /* Given an object die, add the calling convention attribute for the
16212    function call type.  */
16213 static void
16214 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16215 {
16216   enum dwarf_calling_convention value = DW_CC_normal;
16217
16218   value = ((enum dwarf_calling_convention)
16219            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16220
16221   if (is_fortran ()
16222       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16223     {
16224       /* DWARF 2 doesn't provide a way to identify a program's source-level
16225         entry point.  DW_AT_calling_convention attributes are only meant
16226         to describe functions' calling conventions.  However, lacking a
16227         better way to signal the Fortran main program, we used this for 
16228         a long time, following existing custom.  Now, DWARF 4 has 
16229         DW_AT_main_subprogram, which we add below, but some tools still
16230         rely on the old way, which we thus keep.  */
16231       value = DW_CC_program;
16232
16233       if (dwarf_version >= 4 || !dwarf_strict)
16234         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16235     }
16236
16237   /* Only add the attribute if the backend requests it, and
16238      is not DW_CC_normal.  */
16239   if (value && (value != DW_CC_normal))
16240     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16241 }
16242
16243 /* Given a tree pointer to a struct, class, union, or enum type node, return
16244    a pointer to the (string) tag name for the given type, or zero if the type
16245    was declared without a tag.  */
16246
16247 static const char *
16248 type_tag (const_tree type)
16249 {
16250   const char *name = 0;
16251
16252   if (TYPE_NAME (type) != 0)
16253     {
16254       tree t = 0;
16255
16256       /* Find the IDENTIFIER_NODE for the type name.  */
16257       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16258           && !TYPE_NAMELESS (type))
16259         t = TYPE_NAME (type);
16260
16261       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16262          a TYPE_DECL node, regardless of whether or not a `typedef' was
16263          involved.  */
16264       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16265                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16266         {
16267           /* We want to be extra verbose.  Don't call dwarf_name if
16268              DECL_NAME isn't set.  The default hook for decl_printable_name
16269              doesn't like that, and in this context it's correct to return
16270              0, instead of "<anonymous>" or the like.  */
16271           if (DECL_NAME (TYPE_NAME (type))
16272               && !DECL_NAMELESS (TYPE_NAME (type)))
16273             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16274         }
16275
16276       /* Now get the name as a string, or invent one.  */
16277       if (!name && t != 0)
16278         name = IDENTIFIER_POINTER (t);
16279     }
16280
16281   return (name == 0 || *name == '\0') ? 0 : name;
16282 }
16283
16284 /* Return the type associated with a data member, make a special check
16285    for bit field types.  */
16286
16287 static inline tree
16288 member_declared_type (const_tree member)
16289 {
16290   return (DECL_BIT_FIELD_TYPE (member)
16291           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16292 }
16293
16294 /* Get the decl's label, as described by its RTL. This may be different
16295    from the DECL_NAME name used in the source file.  */
16296
16297 #if 0
16298 static const char *
16299 decl_start_label (tree decl)
16300 {
16301   rtx x;
16302   const char *fnname;
16303
16304   x = DECL_RTL (decl);
16305   gcc_assert (MEM_P (x));
16306
16307   x = XEXP (x, 0);
16308   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16309
16310   fnname = XSTR (x, 0);
16311   return fnname;
16312 }
16313 #endif
16314 \f
16315 /* These routines generate the internal representation of the DIE's for
16316    the compilation unit.  Debugging information is collected by walking
16317    the declaration trees passed in from dwarf2out_decl().  */
16318
16319 static void
16320 gen_array_type_die (tree type, dw_die_ref context_die)
16321 {
16322   dw_die_ref scope_die = scope_die_for (type, context_die);
16323   dw_die_ref array_die;
16324
16325   /* GNU compilers represent multidimensional array types as sequences of one
16326      dimensional array types whose element types are themselves array types.
16327      We sometimes squish that down to a single array_type DIE with multiple
16328      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16329      say that we are allowed to do this kind of compression in C, because
16330      there is no difference between an array of arrays and a multidimensional
16331      array.  We don't do this for Ada to remain as close as possible to the
16332      actual representation, which is especially important against the language
16333      flexibilty wrt arrays of variable size.  */
16334
16335   bool collapse_nested_arrays = !is_ada ();
16336   tree element_type;
16337
16338   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16339      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16340   if (TYPE_STRING_FLAG (type)
16341       && TREE_CODE (type) == ARRAY_TYPE
16342       && is_fortran ()
16343       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16344     {
16345       HOST_WIDE_INT size;
16346
16347       array_die = new_die (DW_TAG_string_type, scope_die, type);
16348       add_name_attribute (array_die, type_tag (type));
16349       equate_type_number_to_die (type, array_die);
16350       size = int_size_in_bytes (type);
16351       if (size >= 0)
16352         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16353       else if (TYPE_DOMAIN (type) != NULL_TREE
16354                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16355                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16356         {
16357           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16358           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16359
16360           size = int_size_in_bytes (TREE_TYPE (szdecl));
16361           if (loc && size > 0)
16362             {
16363               add_AT_location_description (array_die, DW_AT_string_length, loc);
16364               if (size != DWARF2_ADDR_SIZE)
16365                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16366             }
16367         }
16368       return;
16369     }
16370
16371   /* ??? The SGI dwarf reader fails for array of array of enum types
16372      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16373      array type comes before the outer array type.  We thus call gen_type_die
16374      before we new_die and must prevent nested array types collapsing for this
16375      target.  */
16376
16377 #ifdef MIPS_DEBUGGING_INFO
16378   gen_type_die (TREE_TYPE (type), context_die);
16379   collapse_nested_arrays = false;
16380 #endif
16381
16382   array_die = new_die (DW_TAG_array_type, scope_die, type);
16383   add_name_attribute (array_die, type_tag (type));
16384   equate_type_number_to_die (type, array_die);
16385
16386   if (TREE_CODE (type) == VECTOR_TYPE)
16387     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16388
16389   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16390   if (is_fortran ()
16391       && TREE_CODE (type) == ARRAY_TYPE
16392       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16393       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16394     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16395
16396 #if 0
16397   /* We default the array ordering.  SDB will probably do
16398      the right things even if DW_AT_ordering is not present.  It's not even
16399      an issue until we start to get into multidimensional arrays anyway.  If
16400      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16401      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16402      and when we find out that we need to put these in, we will only do so
16403      for multidimensional arrays.  */
16404   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16405 #endif
16406
16407 #ifdef MIPS_DEBUGGING_INFO
16408   /* The SGI compilers handle arrays of unknown bound by setting
16409      AT_declaration and not emitting any subrange DIEs.  */
16410   if (TREE_CODE (type) == ARRAY_TYPE
16411       && ! TYPE_DOMAIN (type))
16412     add_AT_flag (array_die, DW_AT_declaration, 1);
16413   else
16414 #endif
16415   if (TREE_CODE (type) == VECTOR_TYPE)
16416     {
16417       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16418       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16419       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16420       add_bound_info (subrange_die, DW_AT_upper_bound,
16421                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16422     }
16423   else
16424     add_subscript_info (array_die, type, collapse_nested_arrays);
16425
16426   /* Add representation of the type of the elements of this array type and
16427      emit the corresponding DIE if we haven't done it already.  */
16428   element_type = TREE_TYPE (type);
16429   if (collapse_nested_arrays)
16430     while (TREE_CODE (element_type) == ARRAY_TYPE)
16431       {
16432         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16433           break;
16434         element_type = TREE_TYPE (element_type);
16435       }
16436
16437 #ifndef MIPS_DEBUGGING_INFO
16438   gen_type_die (element_type, context_die);
16439 #endif
16440
16441   add_type_attribute (array_die, element_type, 0, 0, context_die);
16442
16443   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16444   if (TYPE_ARTIFICIAL (type))
16445     add_AT_flag (array_die, DW_AT_artificial, 1);
16446
16447   if (get_AT (array_die, DW_AT_name))
16448     add_pubtype (type, array_die);
16449 }
16450
16451 static dw_loc_descr_ref
16452 descr_info_loc (tree val, tree base_decl)
16453 {
16454   HOST_WIDE_INT size;
16455   dw_loc_descr_ref loc, loc2;
16456   enum dwarf_location_atom op;
16457
16458   if (val == base_decl)
16459     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16460
16461   switch (TREE_CODE (val))
16462     {
16463     CASE_CONVERT:
16464       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16465     case VAR_DECL:
16466       return loc_descriptor_from_tree (val, 0);
16467     case INTEGER_CST:
16468       if (host_integerp (val, 0))
16469         return int_loc_descriptor (tree_low_cst (val, 0));
16470       break;
16471     case INDIRECT_REF:
16472       size = int_size_in_bytes (TREE_TYPE (val));
16473       if (size < 0)
16474         break;
16475       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16476       if (!loc)
16477         break;
16478       if (size == DWARF2_ADDR_SIZE)
16479         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16480       else
16481         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16482       return loc;
16483     case POINTER_PLUS_EXPR:
16484     case PLUS_EXPR:
16485       if (host_integerp (TREE_OPERAND (val, 1), 1)
16486           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16487              < 16384)
16488         {
16489           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16490           if (!loc)
16491             break;
16492           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16493         }
16494       else
16495         {
16496           op = DW_OP_plus;
16497         do_binop:
16498           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16499           if (!loc)
16500             break;
16501           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16502           if (!loc2)
16503             break;
16504           add_loc_descr (&loc, loc2);
16505           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16506         }
16507       return loc;
16508     case MINUS_EXPR:
16509       op = DW_OP_minus;
16510       goto do_binop;
16511     case MULT_EXPR:
16512       op = DW_OP_mul;
16513       goto do_binop;
16514     case EQ_EXPR:
16515       op = DW_OP_eq;
16516       goto do_binop;
16517     case NE_EXPR:
16518       op = DW_OP_ne;
16519       goto do_binop;
16520     default:
16521       break;
16522     }
16523   return NULL;
16524 }
16525
16526 static void
16527 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16528                       tree val, tree base_decl)
16529 {
16530   dw_loc_descr_ref loc;
16531
16532   if (host_integerp (val, 0))
16533     {
16534       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16535       return;
16536     }
16537
16538   loc = descr_info_loc (val, base_decl);
16539   if (!loc)
16540     return;
16541
16542   add_AT_loc (die, attr, loc);
16543 }
16544
16545 /* This routine generates DIE for array with hidden descriptor, details
16546    are filled into *info by a langhook.  */
16547
16548 static void
16549 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16550                           dw_die_ref context_die)
16551 {
16552   dw_die_ref scope_die = scope_die_for (type, context_die);
16553   dw_die_ref array_die;
16554   int dim;
16555
16556   array_die = new_die (DW_TAG_array_type, scope_die, type);
16557   add_name_attribute (array_die, type_tag (type));
16558   equate_type_number_to_die (type, array_die);
16559
16560   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16561   if (is_fortran ()
16562       && info->ndimensions >= 2)
16563     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16564
16565   if (info->data_location)
16566     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16567                           info->base_decl);
16568   if (info->associated)
16569     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16570                           info->base_decl);
16571   if (info->allocated)
16572     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16573                           info->base_decl);
16574
16575   for (dim = 0; dim < info->ndimensions; dim++)
16576     {
16577       dw_die_ref subrange_die
16578         = new_die (DW_TAG_subrange_type, array_die, NULL);
16579
16580       if (info->dimen[dim].lower_bound)
16581         {
16582           /* If it is the default value, omit it.  */
16583           int dflt;
16584
16585           if (host_integerp (info->dimen[dim].lower_bound, 0)
16586               && (dflt = lower_bound_default ()) != -1
16587               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16588             ;
16589           else
16590             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16591                                   info->dimen[dim].lower_bound,
16592                                   info->base_decl);
16593         }
16594       if (info->dimen[dim].upper_bound)
16595         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16596                               info->dimen[dim].upper_bound,
16597                               info->base_decl);
16598       if (info->dimen[dim].stride)
16599         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16600                               info->dimen[dim].stride,
16601                               info->base_decl);
16602     }
16603
16604   gen_type_die (info->element_type, context_die);
16605   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16606
16607   if (get_AT (array_die, DW_AT_name))
16608     add_pubtype (type, array_die);
16609 }
16610
16611 #if 0
16612 static void
16613 gen_entry_point_die (tree decl, dw_die_ref context_die)
16614 {
16615   tree origin = decl_ultimate_origin (decl);
16616   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16617
16618   if (origin != NULL)
16619     add_abstract_origin_attribute (decl_die, origin);
16620   else
16621     {
16622       add_name_and_src_coords_attributes (decl_die, decl);
16623       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16624                           0, 0, context_die);
16625     }
16626
16627   if (DECL_ABSTRACT (decl))
16628     equate_decl_number_to_die (decl, decl_die);
16629   else
16630     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16631 }
16632 #endif
16633
16634 /* Walk through the list of incomplete types again, trying once more to
16635    emit full debugging info for them.  */
16636
16637 static void
16638 retry_incomplete_types (void)
16639 {
16640   int i;
16641
16642   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16643     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16644                                   DINFO_USAGE_DIR_USE))
16645       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16646 }
16647
16648 /* Determine what tag to use for a record type.  */
16649
16650 static enum dwarf_tag
16651 record_type_tag (tree type)
16652 {
16653   if (! lang_hooks.types.classify_record)
16654     return DW_TAG_structure_type;
16655
16656   switch (lang_hooks.types.classify_record (type))
16657     {
16658     case RECORD_IS_STRUCT:
16659       return DW_TAG_structure_type;
16660
16661     case RECORD_IS_CLASS:
16662       return DW_TAG_class_type;
16663
16664     case RECORD_IS_INTERFACE:
16665       if (dwarf_version >= 3 || !dwarf_strict)
16666         return DW_TAG_interface_type;
16667       return DW_TAG_structure_type;
16668
16669     default:
16670       gcc_unreachable ();
16671     }
16672 }
16673
16674 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16675    include all of the information about the enumeration values also. Each
16676    enumerated type name/value is listed as a child of the enumerated type
16677    DIE.  */
16678
16679 static dw_die_ref
16680 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16681 {
16682   dw_die_ref type_die = lookup_type_die (type);
16683
16684   if (type_die == NULL)
16685     {
16686       type_die = new_die (DW_TAG_enumeration_type,
16687                           scope_die_for (type, context_die), type);
16688       equate_type_number_to_die (type, type_die);
16689       add_name_attribute (type_die, type_tag (type));
16690       if (dwarf_version >= 4 || !dwarf_strict)
16691         {
16692           if (ENUM_IS_SCOPED (type))
16693             add_AT_flag (type_die, DW_AT_enum_class, 1);
16694           if (ENUM_IS_OPAQUE (type))
16695             add_AT_flag (type_die, DW_AT_declaration, 1);
16696         }
16697     }
16698   else if (! TYPE_SIZE (type))
16699     return type_die;
16700   else
16701     remove_AT (type_die, DW_AT_declaration);
16702
16703   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16704      given enum type is incomplete, do not generate the DW_AT_byte_size
16705      attribute or the DW_AT_element_list attribute.  */
16706   if (TYPE_SIZE (type))
16707     {
16708       tree link;
16709
16710       TREE_ASM_WRITTEN (type) = 1;
16711       add_byte_size_attribute (type_die, type);
16712       if (TYPE_STUB_DECL (type) != NULL_TREE)
16713         {
16714           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16715           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16716         }
16717
16718       /* If the first reference to this type was as the return type of an
16719          inline function, then it may not have a parent.  Fix this now.  */
16720       if (type_die->die_parent == NULL)
16721         add_child_die (scope_die_for (type, context_die), type_die);
16722
16723       for (link = TYPE_VALUES (type);
16724            link != NULL; link = TREE_CHAIN (link))
16725         {
16726           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16727           tree value = TREE_VALUE (link);
16728
16729           add_name_attribute (enum_die,
16730                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16731
16732           if (TREE_CODE (value) == CONST_DECL)
16733             value = DECL_INITIAL (value);
16734
16735           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16736             /* DWARF2 does not provide a way of indicating whether or
16737                not enumeration constants are signed or unsigned.  GDB
16738                always assumes the values are signed, so we output all
16739                values as if they were signed.  That means that
16740                enumeration constants with very large unsigned values
16741                will appear to have negative values in the debugger.  */
16742             add_AT_int (enum_die, DW_AT_const_value,
16743                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16744         }
16745
16746       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16747       if (TYPE_ARTIFICIAL (type))
16748         add_AT_flag (type_die, DW_AT_artificial, 1);
16749     }
16750   else
16751     add_AT_flag (type_die, DW_AT_declaration, 1);
16752
16753   if (get_AT (type_die, DW_AT_name))
16754     add_pubtype (type, type_die);
16755
16756   return type_die;
16757 }
16758
16759 /* Generate a DIE to represent either a real live formal parameter decl or to
16760    represent just the type of some formal parameter position in some function
16761    type.
16762
16763    Note that this routine is a bit unusual because its argument may be a
16764    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16765    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16766    node.  If it's the former then this function is being called to output a
16767    DIE to represent a formal parameter object (or some inlining thereof).  If
16768    it's the latter, then this function is only being called to output a
16769    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16770    argument type of some subprogram type.
16771    If EMIT_NAME_P is true, name and source coordinate attributes
16772    are emitted.  */
16773
16774 static dw_die_ref
16775 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16776                           dw_die_ref context_die)
16777 {
16778   tree node_or_origin = node ? node : origin;
16779   tree ultimate_origin;
16780   dw_die_ref parm_die
16781     = new_die (DW_TAG_formal_parameter, context_die, node);
16782
16783   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16784     {
16785     case tcc_declaration:
16786       ultimate_origin = decl_ultimate_origin (node_or_origin);
16787       if (node || ultimate_origin)
16788         origin = ultimate_origin;
16789       if (origin != NULL)
16790         add_abstract_origin_attribute (parm_die, origin);
16791       else if (emit_name_p)
16792         add_name_and_src_coords_attributes (parm_die, node);
16793       if (origin == NULL
16794           || (! DECL_ABSTRACT (node_or_origin)
16795               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16796                                            decl_function_context
16797                                                             (node_or_origin))))
16798         {
16799           tree type = TREE_TYPE (node_or_origin);
16800           if (decl_by_reference_p (node_or_origin))
16801             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16802                                 context_die);
16803           else
16804             add_type_attribute (parm_die, type,
16805                                 TREE_READONLY (node_or_origin),
16806                                 TREE_THIS_VOLATILE (node_or_origin),
16807                                 context_die);
16808         }
16809       if (origin == NULL && DECL_ARTIFICIAL (node))
16810         add_AT_flag (parm_die, DW_AT_artificial, 1);
16811
16812       if (node && node != origin)
16813         equate_decl_number_to_die (node, parm_die);
16814       if (! DECL_ABSTRACT (node_or_origin))
16815         add_location_or_const_value_attribute (parm_die, node_or_origin,
16816                                                node == NULL, DW_AT_location);
16817
16818       break;
16819
16820     case tcc_type:
16821       /* We were called with some kind of a ..._TYPE node.  */
16822       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16823       break;
16824
16825     default:
16826       gcc_unreachable ();
16827     }
16828
16829   return parm_die;
16830 }
16831
16832 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16833    children DW_TAG_formal_parameter DIEs representing the arguments of the
16834    parameter pack.
16835
16836    PARM_PACK must be a function parameter pack.
16837    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16838    must point to the subsequent arguments of the function PACK_ARG belongs to.
16839    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16840    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16841    following the last one for which a DIE was generated.  */
16842
16843 static dw_die_ref
16844 gen_formal_parameter_pack_die  (tree parm_pack,
16845                                 tree pack_arg,
16846                                 dw_die_ref subr_die,
16847                                 tree *next_arg)
16848 {
16849   tree arg;
16850   dw_die_ref parm_pack_die;
16851
16852   gcc_assert (parm_pack
16853               && lang_hooks.function_parameter_pack_p (parm_pack)
16854               && subr_die);
16855
16856   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16857   add_src_coords_attributes (parm_pack_die, parm_pack);
16858
16859   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16860     {
16861       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16862                                                                  parm_pack))
16863         break;
16864       gen_formal_parameter_die (arg, NULL,
16865                                 false /* Don't emit name attribute.  */,
16866                                 parm_pack_die);
16867     }
16868   if (next_arg)
16869     *next_arg = arg;
16870   return parm_pack_die;
16871 }
16872
16873 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16874    at the end of an (ANSI prototyped) formal parameters list.  */
16875
16876 static void
16877 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16878 {
16879   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16880 }
16881
16882 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16883    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16884    parameters as specified in some function type specification (except for
16885    those which appear as part of a function *definition*).  */
16886
16887 static void
16888 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16889 {
16890   tree link;
16891   tree formal_type = NULL;
16892   tree first_parm_type;
16893   tree arg;
16894
16895   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16896     {
16897       arg = DECL_ARGUMENTS (function_or_method_type);
16898       function_or_method_type = TREE_TYPE (function_or_method_type);
16899     }
16900   else
16901     arg = NULL_TREE;
16902
16903   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16904
16905   /* Make our first pass over the list of formal parameter types and output a
16906      DW_TAG_formal_parameter DIE for each one.  */
16907   for (link = first_parm_type; link; )
16908     {
16909       dw_die_ref parm_die;
16910
16911       formal_type = TREE_VALUE (link);
16912       if (formal_type == void_type_node)
16913         break;
16914
16915       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16916       parm_die = gen_formal_parameter_die (formal_type, NULL,
16917                                            true /* Emit name attribute.  */,
16918                                            context_die);
16919       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16920           && link == first_parm_type)
16921         {
16922           add_AT_flag (parm_die, DW_AT_artificial, 1);
16923           if (dwarf_version >= 3 || !dwarf_strict)
16924             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16925         }
16926       else if (arg && DECL_ARTIFICIAL (arg))
16927         add_AT_flag (parm_die, DW_AT_artificial, 1);
16928
16929       link = TREE_CHAIN (link);
16930       if (arg)
16931         arg = DECL_CHAIN (arg);
16932     }
16933
16934   /* If this function type has an ellipsis, add a
16935      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16936   if (formal_type != void_type_node)
16937     gen_unspecified_parameters_die (function_or_method_type, context_die);
16938
16939   /* Make our second (and final) pass over the list of formal parameter types
16940      and output DIEs to represent those types (as necessary).  */
16941   for (link = TYPE_ARG_TYPES (function_or_method_type);
16942        link && TREE_VALUE (link);
16943        link = TREE_CHAIN (link))
16944     gen_type_die (TREE_VALUE (link), context_die);
16945 }
16946
16947 /* We want to generate the DIE for TYPE so that we can generate the
16948    die for MEMBER, which has been defined; we will need to refer back
16949    to the member declaration nested within TYPE.  If we're trying to
16950    generate minimal debug info for TYPE, processing TYPE won't do the
16951    trick; we need to attach the member declaration by hand.  */
16952
16953 static void
16954 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16955 {
16956   gen_type_die (type, context_die);
16957
16958   /* If we're trying to avoid duplicate debug info, we may not have
16959      emitted the member decl for this function.  Emit it now.  */
16960   if (TYPE_STUB_DECL (type)
16961       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16962       && ! lookup_decl_die (member))
16963     {
16964       dw_die_ref type_die;
16965       gcc_assert (!decl_ultimate_origin (member));
16966
16967       push_decl_scope (type);
16968       type_die = lookup_type_die_strip_naming_typedef (type);
16969       if (TREE_CODE (member) == FUNCTION_DECL)
16970         gen_subprogram_die (member, type_die);
16971       else if (TREE_CODE (member) == FIELD_DECL)
16972         {
16973           /* Ignore the nameless fields that are used to skip bits but handle
16974              C++ anonymous unions and structs.  */
16975           if (DECL_NAME (member) != NULL_TREE
16976               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16977               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16978             {
16979               gen_type_die (member_declared_type (member), type_die);
16980               gen_field_die (member, type_die);
16981             }
16982         }
16983       else
16984         gen_variable_die (member, NULL_TREE, type_die);
16985
16986       pop_decl_scope ();
16987     }
16988 }
16989
16990 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16991    may later generate inlined and/or out-of-line instances of.  */
16992
16993 static void
16994 dwarf2out_abstract_function (tree decl)
16995 {
16996   dw_die_ref old_die;
16997   tree save_fn;
16998   tree context;
16999   int was_abstract;
17000   htab_t old_decl_loc_table;
17001   htab_t old_cached_dw_loc_list_table;
17002   int old_call_site_count, old_tail_call_site_count;
17003   struct call_arg_loc_node *old_call_arg_locations;
17004
17005   /* Make sure we have the actual abstract inline, not a clone.  */
17006   decl = DECL_ORIGIN (decl);
17007
17008   old_die = lookup_decl_die (decl);
17009   if (old_die && get_AT (old_die, DW_AT_inline))
17010     /* We've already generated the abstract instance.  */
17011     return;
17012
17013   /* We can be called while recursively when seeing block defining inlined subroutine
17014      DIE.  Be sure to not clobber the outer location table nor use it or we would
17015      get locations in abstract instantces.  */
17016   old_decl_loc_table = decl_loc_table;
17017   decl_loc_table = NULL;
17018   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17019   cached_dw_loc_list_table = NULL;
17020   old_call_arg_locations = call_arg_locations;
17021   call_arg_locations = NULL;
17022   old_call_site_count = call_site_count;
17023   call_site_count = -1;
17024   old_tail_call_site_count = tail_call_site_count;
17025   tail_call_site_count = -1;
17026
17027   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17028      we don't get confused by DECL_ABSTRACT.  */
17029   if (debug_info_level > DINFO_LEVEL_TERSE)
17030     {
17031       context = decl_class_context (decl);
17032       if (context)
17033         gen_type_die_for_member
17034           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17035     }
17036
17037   /* Pretend we've just finished compiling this function.  */
17038   save_fn = current_function_decl;
17039   current_function_decl = decl;
17040   push_cfun (DECL_STRUCT_FUNCTION (decl));
17041
17042   was_abstract = DECL_ABSTRACT (decl);
17043   set_decl_abstract_flags (decl, 1);
17044   dwarf2out_decl (decl);
17045   if (! was_abstract)
17046     set_decl_abstract_flags (decl, 0);
17047
17048   current_function_decl = save_fn;
17049   decl_loc_table = old_decl_loc_table;
17050   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17051   call_arg_locations = old_call_arg_locations;
17052   call_site_count = old_call_site_count;
17053   tail_call_site_count = old_tail_call_site_count;
17054   pop_cfun ();
17055 }
17056
17057 /* Helper function of premark_used_types() which gets called through
17058    htab_traverse.
17059
17060    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17061    marked as unused by prune_unused_types.  */
17062
17063 static int
17064 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17065 {
17066   tree type;
17067   dw_die_ref die;
17068
17069   type = (tree) *slot;
17070   die = lookup_type_die (type);
17071   if (die != NULL)
17072     die->die_perennial_p = 1;
17073   return 1;
17074 }
17075
17076 /* Helper function of premark_types_used_by_global_vars which gets called
17077    through htab_traverse.
17078
17079    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17080    marked as unused by prune_unused_types. The DIE of the type is marked
17081    only if the global variable using the type will actually be emitted.  */
17082
17083 static int
17084 premark_types_used_by_global_vars_helper (void **slot,
17085                                           void *data ATTRIBUTE_UNUSED)
17086 {
17087   struct types_used_by_vars_entry *entry;
17088   dw_die_ref die;
17089
17090   entry = (struct types_used_by_vars_entry *) *slot;
17091   gcc_assert (entry->type != NULL
17092               && entry->var_decl != NULL);
17093   die = lookup_type_die (entry->type);
17094   if (die)
17095     {
17096       /* Ask cgraph if the global variable really is to be emitted.
17097          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17098       struct varpool_node *node = varpool_get_node (entry->var_decl);
17099       if (node && node->needed)
17100         {
17101           die->die_perennial_p = 1;
17102           /* Keep the parent DIEs as well.  */
17103           while ((die = die->die_parent) && die->die_perennial_p == 0)
17104             die->die_perennial_p = 1;
17105         }
17106     }
17107   return 1;
17108 }
17109
17110 /* Mark all members of used_types_hash as perennial.  */
17111
17112 static void
17113 premark_used_types (void)
17114 {
17115   if (cfun && cfun->used_types_hash)
17116     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17117 }
17118
17119 /* Mark all members of types_used_by_vars_entry as perennial.  */
17120
17121 static void
17122 premark_types_used_by_global_vars (void)
17123 {
17124   if (types_used_by_vars_hash)
17125     htab_traverse (types_used_by_vars_hash,
17126                    premark_types_used_by_global_vars_helper, NULL);
17127 }
17128
17129 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17130    for CA_LOC call arg loc node.  */
17131
17132 static dw_die_ref
17133 gen_call_site_die (tree decl, dw_die_ref subr_die,
17134                    struct call_arg_loc_node *ca_loc)
17135 {
17136   dw_die_ref stmt_die = NULL, die;
17137   tree block = ca_loc->block;
17138
17139   while (block
17140          && block != DECL_INITIAL (decl)
17141          && TREE_CODE (block) == BLOCK)
17142     {
17143       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17144         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17145       if (stmt_die)
17146         break;
17147       block = BLOCK_SUPERCONTEXT (block);
17148     }
17149   if (stmt_die == NULL)
17150     stmt_die = subr_die;
17151   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17152   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17153   if (ca_loc->tail_call_p)
17154     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17155   if (ca_loc->symbol_ref)
17156     {
17157       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17158       if (tdie)
17159         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17160       else
17161         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17162     }
17163   return die;
17164 }
17165
17166 /* Generate a DIE to represent a declared function (either file-scope or
17167    block-local).  */
17168
17169 static void
17170 gen_subprogram_die (tree decl, dw_die_ref context_die)
17171 {
17172   tree origin = decl_ultimate_origin (decl);
17173   dw_die_ref subr_die;
17174   tree outer_scope;
17175   dw_die_ref old_die = lookup_decl_die (decl);
17176   int declaration = (current_function_decl != decl
17177                      || class_or_namespace_scope_p (context_die));
17178
17179   premark_used_types ();
17180
17181   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17182      started to generate the abstract instance of an inline, decided to output
17183      its containing class, and proceeded to emit the declaration of the inline
17184      from the member list for the class.  If so, DECLARATION takes priority;
17185      we'll get back to the abstract instance when done with the class.  */
17186
17187   /* The class-scope declaration DIE must be the primary DIE.  */
17188   if (origin && declaration && class_or_namespace_scope_p (context_die))
17189     {
17190       origin = NULL;
17191       gcc_assert (!old_die);
17192     }
17193
17194   /* Now that the C++ front end lazily declares artificial member fns, we
17195      might need to retrofit the declaration into its class.  */
17196   if (!declaration && !origin && !old_die
17197       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17198       && !class_or_namespace_scope_p (context_die)
17199       && debug_info_level > DINFO_LEVEL_TERSE)
17200     old_die = force_decl_die (decl);
17201
17202   if (origin != NULL)
17203     {
17204       gcc_assert (!declaration || local_scope_p (context_die));
17205
17206       /* Fixup die_parent for the abstract instance of a nested
17207          inline function.  */
17208       if (old_die && old_die->die_parent == NULL)
17209         add_child_die (context_die, old_die);
17210
17211       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17212       add_abstract_origin_attribute (subr_die, origin);
17213       /*  This is where the actual code for a cloned function is.
17214           Let's emit linkage name attribute for it.  This helps
17215           debuggers to e.g, set breakpoints into
17216           constructors/destructors when the user asks "break
17217           K::K".  */
17218       add_linkage_name (subr_die, decl);
17219     }
17220   else if (old_die)
17221     {
17222       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17223       struct dwarf_file_data * file_index = lookup_filename (s.file);
17224
17225       if (!get_AT_flag (old_die, DW_AT_declaration)
17226           /* We can have a normal definition following an inline one in the
17227              case of redefinition of GNU C extern inlines.
17228              It seems reasonable to use AT_specification in this case.  */
17229           && !get_AT (old_die, DW_AT_inline))
17230         {
17231           /* Detect and ignore this case, where we are trying to output
17232              something we have already output.  */
17233           return;
17234         }
17235
17236       /* If the definition comes from the same place as the declaration,
17237          maybe use the old DIE.  We always want the DIE for this function
17238          that has the *_pc attributes to be under comp_unit_die so the
17239          debugger can find it.  We also need to do this for abstract
17240          instances of inlines, since the spec requires the out-of-line copy
17241          to have the same parent.  For local class methods, this doesn't
17242          apply; we just use the old DIE.  */
17243       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17244           && (DECL_ARTIFICIAL (decl)
17245               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17246                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17247                       == (unsigned) s.line))))
17248         {
17249           subr_die = old_die;
17250
17251           /* Clear out the declaration attribute and the formal parameters.
17252              Do not remove all children, because it is possible that this
17253              declaration die was forced using force_decl_die(). In such
17254              cases die that forced declaration die (e.g. TAG_imported_module)
17255              is one of the children that we do not want to remove.  */
17256           remove_AT (subr_die, DW_AT_declaration);
17257           remove_AT (subr_die, DW_AT_object_pointer);
17258           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17259         }
17260       else
17261         {
17262           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17263           add_AT_specification (subr_die, old_die);
17264           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17265             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17266           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17267             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17268         }
17269     }
17270   else
17271     {
17272       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17273
17274       if (TREE_PUBLIC (decl))
17275         add_AT_flag (subr_die, DW_AT_external, 1);
17276
17277       add_name_and_src_coords_attributes (subr_die, decl);
17278       if (debug_info_level > DINFO_LEVEL_TERSE)
17279         {
17280           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17281           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17282                               0, 0, context_die);
17283         }
17284
17285       add_pure_or_virtual_attribute (subr_die, decl);
17286       if (DECL_ARTIFICIAL (decl))
17287         add_AT_flag (subr_die, DW_AT_artificial, 1);
17288
17289       add_accessibility_attribute (subr_die, decl);
17290     }
17291
17292   if (declaration)
17293     {
17294       if (!old_die || !get_AT (old_die, DW_AT_inline))
17295         {
17296           add_AT_flag (subr_die, DW_AT_declaration, 1);
17297
17298           /* If this is an explicit function declaration then generate
17299              a DW_AT_explicit attribute.  */
17300           if (lang_hooks.decls.function_decl_explicit_p (decl)
17301               && (dwarf_version >= 3 || !dwarf_strict))
17302             add_AT_flag (subr_die, DW_AT_explicit, 1);
17303
17304           /* The first time we see a member function, it is in the context of
17305              the class to which it belongs.  We make sure of this by emitting
17306              the class first.  The next time is the definition, which is
17307              handled above.  The two may come from the same source text.
17308
17309              Note that force_decl_die() forces function declaration die. It is
17310              later reused to represent definition.  */
17311           equate_decl_number_to_die (decl, subr_die);
17312         }
17313     }
17314   else if (DECL_ABSTRACT (decl))
17315     {
17316       if (DECL_DECLARED_INLINE_P (decl))
17317         {
17318           if (cgraph_function_possibly_inlined_p (decl))
17319             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17320           else
17321             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17322         }
17323       else
17324         {
17325           if (cgraph_function_possibly_inlined_p (decl))
17326             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17327           else
17328             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17329         }
17330
17331       if (DECL_DECLARED_INLINE_P (decl)
17332           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17333         add_AT_flag (subr_die, DW_AT_artificial, 1);
17334
17335       equate_decl_number_to_die (decl, subr_die);
17336     }
17337   else if (!DECL_EXTERNAL (decl))
17338     {
17339       HOST_WIDE_INT cfa_fb_offset;
17340
17341       if (!old_die || !get_AT (old_die, DW_AT_inline))
17342         equate_decl_number_to_die (decl, subr_die);
17343
17344       if (!flag_reorder_blocks_and_partition)
17345         {
17346           dw_fde_ref fde = cfun->fde;
17347           if (fde->dw_fde_begin)
17348             {
17349               /* We have already generated the labels.  */
17350               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17351               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17352             }
17353           else
17354             {
17355               /* Create start/end labels and add the range.  */
17356               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17357               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17358                                            current_function_funcdef_no);
17359               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17360               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17361                                            current_function_funcdef_no);
17362               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17363             }
17364
17365 #if VMS_DEBUGGING_INFO
17366       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17367          Section 2.3 Prologue and Epilogue Attributes:
17368          When a breakpoint is set on entry to a function, it is generally
17369          desirable for execution to be suspended, not on the very first
17370          instruction of the function, but rather at a point after the
17371          function's frame has been set up, after any language defined local
17372          declaration processing has been completed, and before execution of
17373          the first statement of the function begins. Debuggers generally
17374          cannot properly determine where this point is.  Similarly for a
17375          breakpoint set on exit from a function. The prologue and epilogue
17376          attributes allow a compiler to communicate the location(s) to use.  */
17377
17378       {
17379         if (fde->dw_fde_vms_end_prologue)
17380           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17381             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17382
17383         if (fde->dw_fde_vms_begin_epilogue)
17384           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17385             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17386       }
17387 #endif
17388
17389           add_pubname (decl, subr_die);
17390         }
17391       else
17392         {
17393           /* Generate pubnames entries for the split function code ranges.  */
17394           dw_fde_ref fde = cfun->fde;
17395
17396           if (fde->dw_fde_second_begin)
17397             {
17398               if (dwarf_version >= 3 || !dwarf_strict)
17399                 {
17400                   /* We should use ranges for non-contiguous code section 
17401                      addresses.  Use the actual code range for the initial
17402                      section, since the HOT/COLD labels might precede an 
17403                      alignment offset.  */
17404                   bool range_list_added = false;
17405                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17406                                         fde->dw_fde_end, &range_list_added);
17407                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17408                                         fde->dw_fde_second_end,
17409                                         &range_list_added);
17410                   add_pubname (decl, subr_die);
17411                   if (range_list_added)
17412                     add_ranges (NULL);
17413                 }
17414               else
17415                 {
17416                   /* There is no real support in DW2 for this .. so we make
17417                      a work-around.  First, emit the pub name for the segment
17418                      containing the function label.  Then make and emit a
17419                      simplified subprogram DIE for the second segment with the
17420                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17421                      linkage name for the second die so that gdb will find both
17422                      sections when given "b foo".  */
17423                   const char *name = NULL;
17424                   tree decl_name = DECL_NAME (decl);
17425                   dw_die_ref seg_die;
17426
17427                   /* Do the 'primary' section.   */
17428                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17429                                  fde->dw_fde_begin);
17430                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17431                                  fde->dw_fde_end);
17432                   /* Add it.   */
17433                   add_pubname (decl, subr_die);
17434
17435                   /* Build a minimal DIE for the secondary section.  */
17436                   seg_die = new_die (DW_TAG_subprogram,
17437                                      subr_die->die_parent, decl);
17438
17439                   if (TREE_PUBLIC (decl))
17440                     add_AT_flag (seg_die, DW_AT_external, 1);
17441
17442                   if (decl_name != NULL 
17443                       && IDENTIFIER_POINTER (decl_name) != NULL)
17444                     {
17445                       name = dwarf2_name (decl, 1);
17446                       if (! DECL_ARTIFICIAL (decl))
17447                         add_src_coords_attributes (seg_die, decl);
17448
17449                       add_linkage_name (seg_die, decl);
17450                     }
17451                   gcc_assert (name != NULL);
17452                   add_pure_or_virtual_attribute (seg_die, decl);
17453                   if (DECL_ARTIFICIAL (decl))
17454                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17455
17456                   name = concat ("__second_sect_of_", name, NULL); 
17457                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17458                                  fde->dw_fde_second_begin);
17459                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17460                                  fde->dw_fde_second_end);
17461                   add_name_attribute (seg_die, name);
17462                   add_pubname_string (name, seg_die);
17463                 }
17464             }
17465           else
17466             {
17467               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17468               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17469               add_pubname (decl, subr_die);
17470             }
17471         }
17472
17473 #ifdef MIPS_DEBUGGING_INFO
17474       /* Add a reference to the FDE for this routine.  */
17475       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17476 #endif
17477
17478       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17479
17480       /* We define the "frame base" as the function's CFA.  This is more
17481          convenient for several reasons: (1) It's stable across the prologue
17482          and epilogue, which makes it better than just a frame pointer,
17483          (2) With dwarf3, there exists a one-byte encoding that allows us
17484          to reference the .debug_frame data by proxy, but failing that,
17485          (3) We can at least reuse the code inspection and interpretation
17486          code that determines the CFA position at various points in the
17487          function.  */
17488       if (dwarf_version >= 3)
17489         {
17490           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17491           add_AT_loc (subr_die, DW_AT_frame_base, op);
17492         }
17493       else
17494         {
17495           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17496           if (list->dw_loc_next)
17497             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17498           else
17499             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17500         }
17501
17502       /* Compute a displacement from the "steady-state frame pointer" to
17503          the CFA.  The former is what all stack slots and argument slots
17504          will reference in the rtl; the later is what we've told the
17505          debugger about.  We'll need to adjust all frame_base references
17506          by this displacement.  */
17507       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17508
17509       if (cfun->static_chain_decl)
17510         add_AT_location_description (subr_die, DW_AT_static_link,
17511                  loc_list_from_tree (cfun->static_chain_decl, 2));
17512     }
17513
17514   /* Generate child dies for template paramaters.  */
17515   if (debug_info_level > DINFO_LEVEL_TERSE)
17516     gen_generic_params_dies (decl);
17517
17518   /* Now output descriptions of the arguments for this function. This gets
17519      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17520      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17521      `...' at the end of the formal parameter list.  In order to find out if
17522      there was a trailing ellipsis or not, we must instead look at the type
17523      associated with the FUNCTION_DECL.  This will be a node of type
17524      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17525      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17526      an ellipsis at the end.  */
17527
17528   /* In the case where we are describing a mere function declaration, all we
17529      need to do here (and all we *can* do here) is to describe the *types* of
17530      its formal parameters.  */
17531   if (debug_info_level <= DINFO_LEVEL_TERSE)
17532     ;
17533   else if (declaration)
17534     gen_formal_types_die (decl, subr_die);
17535   else
17536     {
17537       /* Generate DIEs to represent all known formal parameters.  */
17538       tree parm = DECL_ARGUMENTS (decl);
17539       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17540       tree generic_decl_parm = generic_decl
17541                                 ? DECL_ARGUMENTS (generic_decl)
17542                                 : NULL;
17543
17544       /* Now we want to walk the list of parameters of the function and
17545          emit their relevant DIEs.
17546
17547          We consider the case of DECL being an instance of a generic function
17548          as well as it being a normal function.
17549
17550          If DECL is an instance of a generic function we walk the
17551          parameters of the generic function declaration _and_ the parameters of
17552          DECL itself. This is useful because we want to emit specific DIEs for
17553          function parameter packs and those are declared as part of the
17554          generic function declaration. In that particular case,
17555          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17556          That DIE has children DIEs representing the set of arguments
17557          of the pack. Note that the set of pack arguments can be empty.
17558          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17559          children DIE.
17560
17561          Otherwise, we just consider the parameters of DECL.  */
17562       while (generic_decl_parm || parm)
17563         {
17564           if (generic_decl_parm
17565               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17566             gen_formal_parameter_pack_die (generic_decl_parm,
17567                                            parm, subr_die,
17568                                            &parm);
17569           else if (parm)
17570             {
17571               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17572
17573               if (parm == DECL_ARGUMENTS (decl)
17574                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17575                   && parm_die
17576                   && (dwarf_version >= 3 || !dwarf_strict))
17577                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17578
17579               parm = DECL_CHAIN (parm);
17580             }
17581
17582           if (generic_decl_parm)
17583             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17584         }
17585
17586       /* Decide whether we need an unspecified_parameters DIE at the end.
17587          There are 2 more cases to do this for: 1) the ansi ... declaration -
17588          this is detectable when the end of the arg list is not a
17589          void_type_node 2) an unprototyped function declaration (not a
17590          definition).  This just means that we have no info about the
17591          parameters at all.  */
17592       if (prototype_p (TREE_TYPE (decl)))
17593         {
17594           /* This is the prototyped case, check for....  */
17595           if (stdarg_p (TREE_TYPE (decl)))
17596             gen_unspecified_parameters_die (decl, subr_die);
17597         }
17598       else if (DECL_INITIAL (decl) == NULL_TREE)
17599         gen_unspecified_parameters_die (decl, subr_die);
17600     }
17601
17602   /* Output Dwarf info for all of the stuff within the body of the function
17603      (if it has one - it may be just a declaration).  */
17604   outer_scope = DECL_INITIAL (decl);
17605
17606   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17607      a function.  This BLOCK actually represents the outermost binding contour
17608      for the function, i.e. the contour in which the function's formal
17609      parameters and labels get declared. Curiously, it appears that the front
17610      end doesn't actually put the PARM_DECL nodes for the current function onto
17611      the BLOCK_VARS list for this outer scope, but are strung off of the
17612      DECL_ARGUMENTS list for the function instead.
17613
17614      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17615      the LABEL_DECL nodes for the function however, and we output DWARF info
17616      for those in decls_for_scope.  Just within the `outer_scope' there will be
17617      a BLOCK node representing the function's outermost pair of curly braces,
17618      and any blocks used for the base and member initializers of a C++
17619      constructor function.  */
17620   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17621     {
17622       int call_site_note_count = 0;
17623       int tail_call_site_note_count = 0;
17624
17625       /* Emit a DW_TAG_variable DIE for a named return value.  */
17626       if (DECL_NAME (DECL_RESULT (decl)))
17627         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17628
17629       current_function_has_inlines = 0;
17630       decls_for_scope (outer_scope, subr_die, 0);
17631
17632       if (call_arg_locations && !dwarf_strict)
17633         {
17634           struct call_arg_loc_node *ca_loc;
17635           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17636             {
17637               dw_die_ref die = NULL;
17638               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17639               rtx arg, next_arg;
17640
17641               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17642                    arg; arg = next_arg)
17643                 {
17644                   dw_loc_descr_ref reg, val;
17645                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17646                   dw_die_ref cdie, tdie = NULL;
17647
17648                   next_arg = XEXP (arg, 1);
17649                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17650                       && next_arg
17651                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17652                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17653                       && REGNO (XEXP (XEXP (arg, 0), 0))
17654                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17655                     next_arg = XEXP (next_arg, 1);
17656                   if (mode == VOIDmode)
17657                     {
17658                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17659                       if (mode == VOIDmode)
17660                         mode = GET_MODE (XEXP (arg, 0));
17661                     }
17662                   if (mode == VOIDmode || mode == BLKmode)
17663                     continue;
17664                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17665                     {
17666                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17667                       tloc = XEXP (XEXP (arg, 0), 1);
17668                       continue;
17669                     }
17670                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17671                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17672                     {
17673                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17674                       tlocc = XEXP (XEXP (arg, 0), 1);
17675                       continue;
17676                     }
17677                   reg = NULL;
17678                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17679                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17680                                               VAR_INIT_STATUS_INITIALIZED);
17681                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17682                     {
17683                       rtx mem = XEXP (XEXP (arg, 0), 0);
17684                       reg = mem_loc_descriptor (XEXP (mem, 0),
17685                                                 get_address_mode (mem),
17686                                                 GET_MODE (mem),
17687                                                 VAR_INIT_STATUS_INITIALIZED);
17688                     }
17689                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17690                            == DEBUG_PARAMETER_REF)
17691                     {
17692                       tree tdecl
17693                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17694                       tdie = lookup_decl_die (tdecl);
17695                       if (tdie == NULL)
17696                         continue;
17697                     }
17698                   else
17699                     continue;
17700                   if (reg == NULL
17701                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17702                          != DEBUG_PARAMETER_REF)
17703                     continue;
17704                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17705                                             VOIDmode,
17706                                             VAR_INIT_STATUS_INITIALIZED);
17707                   if (val == NULL)
17708                     continue;
17709                   if (die == NULL)
17710                     die = gen_call_site_die (decl, subr_die, ca_loc);
17711                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17712                                   NULL_TREE);
17713                   if (reg != NULL)
17714                     add_AT_loc (cdie, DW_AT_location, reg);
17715                   else if (tdie != NULL)
17716                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17717                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17718                   if (next_arg != XEXP (arg, 1))
17719                     {
17720                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17721                       if (mode == VOIDmode)
17722                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17723                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17724                                                             0), 1),
17725                                                 mode, VOIDmode,
17726                                                 VAR_INIT_STATUS_INITIALIZED);
17727                       if (val != NULL)
17728                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17729                     }
17730                 }
17731               if (die == NULL
17732                   && (ca_loc->symbol_ref || tloc))
17733                 die = gen_call_site_die (decl, subr_die, ca_loc);
17734               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17735                 {
17736                   dw_loc_descr_ref tval = NULL;
17737
17738                   if (tloc != NULL_RTX)
17739                     tval = mem_loc_descriptor (tloc,
17740                                                GET_MODE (tloc) == VOIDmode
17741                                                ? Pmode : GET_MODE (tloc),
17742                                                VOIDmode,
17743                                                VAR_INIT_STATUS_INITIALIZED);
17744                   if (tval)
17745                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17746                   else if (tlocc != NULL_RTX)
17747                     {
17748                       tval = mem_loc_descriptor (tlocc,
17749                                                  GET_MODE (tlocc) == VOIDmode
17750                                                  ? Pmode : GET_MODE (tlocc),
17751                                                  VOIDmode,
17752                                                  VAR_INIT_STATUS_INITIALIZED);
17753                       if (tval)
17754                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17755                                     tval);
17756                     }
17757                 }
17758               if (die != NULL)
17759                 {
17760                   call_site_note_count++;
17761                   if (ca_loc->tail_call_p)
17762                     tail_call_site_note_count++;
17763                 }
17764             }
17765         }
17766       call_arg_locations = NULL;
17767       call_arg_loc_last = NULL;
17768       if (tail_call_site_count >= 0
17769           && tail_call_site_count == tail_call_site_note_count
17770           && !dwarf_strict)
17771         {
17772           if (call_site_count >= 0
17773               && call_site_count == call_site_note_count)
17774             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17775           else
17776             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17777         }
17778       call_site_count = -1;
17779       tail_call_site_count = -1;
17780     }
17781   /* Add the calling convention attribute if requested.  */
17782   add_calling_convention_attribute (subr_die, decl);
17783
17784 }
17785
17786 /* Returns a hash value for X (which really is a die_struct).  */
17787
17788 static hashval_t
17789 common_block_die_table_hash (const void *x)
17790 {
17791   const_dw_die_ref d = (const_dw_die_ref) x;
17792   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17793 }
17794
17795 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17796    as decl_id and die_parent of die_struct Y.  */
17797
17798 static int
17799 common_block_die_table_eq (const void *x, const void *y)
17800 {
17801   const_dw_die_ref d = (const_dw_die_ref) x;
17802   const_dw_die_ref e = (const_dw_die_ref) y;
17803   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17804 }
17805
17806 /* Generate a DIE to represent a declared data object.
17807    Either DECL or ORIGIN must be non-null.  */
17808
17809 static void
17810 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17811 {
17812   HOST_WIDE_INT off;
17813   tree com_decl;
17814   tree decl_or_origin = decl ? decl : origin;
17815   tree ultimate_origin;
17816   dw_die_ref var_die;
17817   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17818   dw_die_ref origin_die;
17819   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17820                       || class_or_namespace_scope_p (context_die));
17821   bool specialization_p = false;
17822
17823   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17824   if (decl || ultimate_origin)
17825     origin = ultimate_origin;
17826   com_decl = fortran_common (decl_or_origin, &off);
17827
17828   /* Symbol in common gets emitted as a child of the common block, in the form
17829      of a data member.  */
17830   if (com_decl)
17831     {
17832       dw_die_ref com_die;
17833       dw_loc_list_ref loc;
17834       die_node com_die_arg;
17835
17836       var_die = lookup_decl_die (decl_or_origin);
17837       if (var_die)
17838         {
17839           if (get_AT (var_die, DW_AT_location) == NULL)
17840             {
17841               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17842               if (loc)
17843                 {
17844                   if (off)
17845                     {
17846                       /* Optimize the common case.  */
17847                       if (single_element_loc_list_p (loc)
17848                           && loc->expr->dw_loc_opc == DW_OP_addr
17849                           && loc->expr->dw_loc_next == NULL
17850                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17851                              == SYMBOL_REF)
17852                         loc->expr->dw_loc_oprnd1.v.val_addr
17853                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17854                         else
17855                           loc_list_plus_const (loc, off);
17856                     }
17857                   add_AT_location_description (var_die, DW_AT_location, loc);
17858                   remove_AT (var_die, DW_AT_declaration);
17859                 }
17860             }
17861           return;
17862         }
17863
17864       if (common_block_die_table == NULL)
17865         common_block_die_table
17866           = htab_create_ggc (10, common_block_die_table_hash,
17867                              common_block_die_table_eq, NULL);
17868
17869       com_die_arg.decl_id = DECL_UID (com_decl);
17870       com_die_arg.die_parent = context_die;
17871       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17872       loc = loc_list_from_tree (com_decl, 2);
17873       if (com_die == NULL)
17874         {
17875           const char *cnam
17876             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17877           void **slot;
17878
17879           com_die = new_die (DW_TAG_common_block, context_die, decl);
17880           add_name_and_src_coords_attributes (com_die, com_decl);
17881           if (loc)
17882             {
17883               add_AT_location_description (com_die, DW_AT_location, loc);
17884               /* Avoid sharing the same loc descriptor between
17885                  DW_TAG_common_block and DW_TAG_variable.  */
17886               loc = loc_list_from_tree (com_decl, 2);
17887             }
17888           else if (DECL_EXTERNAL (decl))
17889             add_AT_flag (com_die, DW_AT_declaration, 1);
17890           add_pubname_string (cnam, com_die); /* ??? needed? */
17891           com_die->decl_id = DECL_UID (com_decl);
17892           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17893           *slot = (void *) com_die;
17894         }
17895       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17896         {
17897           add_AT_location_description (com_die, DW_AT_location, loc);
17898           loc = loc_list_from_tree (com_decl, 2);
17899           remove_AT (com_die, DW_AT_declaration);
17900         }
17901       var_die = new_die (DW_TAG_variable, com_die, decl);
17902       add_name_and_src_coords_attributes (var_die, decl);
17903       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17904                           TREE_THIS_VOLATILE (decl), context_die);
17905       add_AT_flag (var_die, DW_AT_external, 1);
17906       if (loc)
17907         {
17908           if (off)
17909             {
17910               /* Optimize the common case.  */
17911               if (single_element_loc_list_p (loc)
17912                   && loc->expr->dw_loc_opc == DW_OP_addr
17913                   && loc->expr->dw_loc_next == NULL
17914                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17915                 loc->expr->dw_loc_oprnd1.v.val_addr
17916                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17917               else
17918                 loc_list_plus_const (loc, off);
17919             }
17920           add_AT_location_description (var_die, DW_AT_location, loc);
17921         }
17922       else if (DECL_EXTERNAL (decl))
17923         add_AT_flag (var_die, DW_AT_declaration, 1);
17924       equate_decl_number_to_die (decl, var_die);
17925       return;
17926     }
17927
17928   /* If the compiler emitted a definition for the DECL declaration
17929      and if we already emitted a DIE for it, don't emit a second
17930      DIE for it again. Allow re-declarations of DECLs that are
17931      inside functions, though.  */
17932   if (old_die && declaration && !local_scope_p (context_die))
17933     return;
17934
17935   /* For static data members, the declaration in the class is supposed
17936      to have DW_TAG_member tag; the specification should still be
17937      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17938   if (declaration && class_scope_p (context_die))
17939     var_die = new_die (DW_TAG_member, context_die, decl);
17940   else
17941     var_die = new_die (DW_TAG_variable, context_die, decl);
17942
17943   origin_die = NULL;
17944   if (origin != NULL)
17945     origin_die = add_abstract_origin_attribute (var_die, origin);
17946
17947   /* Loop unrolling can create multiple blocks that refer to the same
17948      static variable, so we must test for the DW_AT_declaration flag.
17949
17950      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17951      copy decls and set the DECL_ABSTRACT flag on them instead of
17952      sharing them.
17953
17954      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17955
17956      ??? The declare_in_namespace support causes us to get two DIEs for one
17957      variable, both of which are declarations.  We want to avoid considering
17958      one to be a specification, so we must test that this DIE is not a
17959      declaration.  */
17960   else if (old_die && TREE_STATIC (decl) && ! declaration
17961            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17962     {
17963       /* This is a definition of a C++ class level static.  */
17964       add_AT_specification (var_die, old_die);
17965       specialization_p = true;
17966       if (DECL_NAME (decl))
17967         {
17968           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17969           struct dwarf_file_data * file_index = lookup_filename (s.file);
17970
17971           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17972             add_AT_file (var_die, DW_AT_decl_file, file_index);
17973
17974           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17975             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17976
17977           if (old_die->die_tag == DW_TAG_member)
17978             add_linkage_name (var_die, decl);
17979         }
17980     }
17981   else
17982     add_name_and_src_coords_attributes (var_die, decl);
17983
17984   if ((origin == NULL && !specialization_p)
17985       || (origin != NULL
17986           && !DECL_ABSTRACT (decl_or_origin)
17987           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17988                                        decl_function_context
17989                                                         (decl_or_origin))))
17990     {
17991       tree type = TREE_TYPE (decl_or_origin);
17992
17993       if (decl_by_reference_p (decl_or_origin))
17994         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17995       else
17996         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17997                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17998     }
17999
18000   if (origin == NULL && !specialization_p)
18001     {
18002       if (TREE_PUBLIC (decl))
18003         add_AT_flag (var_die, DW_AT_external, 1);
18004
18005       if (DECL_ARTIFICIAL (decl))
18006         add_AT_flag (var_die, DW_AT_artificial, 1);
18007
18008       add_accessibility_attribute (var_die, decl);
18009     }
18010
18011   if (declaration)
18012     add_AT_flag (var_die, DW_AT_declaration, 1);
18013
18014   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18015     equate_decl_number_to_die (decl, var_die);
18016
18017   if (! declaration
18018       && (! DECL_ABSTRACT (decl_or_origin)
18019           /* Local static vars are shared between all clones/inlines,
18020              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18021              already set.  */
18022           || (TREE_CODE (decl_or_origin) == VAR_DECL
18023               && TREE_STATIC (decl_or_origin)
18024               && DECL_RTL_SET_P (decl_or_origin)))
18025       /* When abstract origin already has DW_AT_location attribute, no need
18026          to add it again.  */
18027       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18028     {
18029       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18030           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18031         defer_location (decl_or_origin, var_die);
18032       else
18033         add_location_or_const_value_attribute (var_die, decl_or_origin,
18034                                                decl == NULL, DW_AT_location);
18035       add_pubname (decl_or_origin, var_die);
18036     }
18037   else
18038     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18039 }
18040
18041 /* Generate a DIE to represent a named constant.  */
18042
18043 static void
18044 gen_const_die (tree decl, dw_die_ref context_die)
18045 {
18046   dw_die_ref const_die;
18047   tree type = TREE_TYPE (decl);
18048
18049   const_die = new_die (DW_TAG_constant, context_die, decl);
18050   add_name_and_src_coords_attributes (const_die, decl);
18051   add_type_attribute (const_die, type, 1, 0, context_die);
18052   if (TREE_PUBLIC (decl))
18053     add_AT_flag (const_die, DW_AT_external, 1);
18054   if (DECL_ARTIFICIAL (decl))
18055     add_AT_flag (const_die, DW_AT_artificial, 1);
18056   tree_add_const_value_attribute_for_decl (const_die, decl);
18057 }
18058
18059 /* Generate a DIE to represent a label identifier.  */
18060
18061 static void
18062 gen_label_die (tree decl, dw_die_ref context_die)
18063 {
18064   tree origin = decl_ultimate_origin (decl);
18065   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18066   rtx insn;
18067   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18068
18069   if (origin != NULL)
18070     add_abstract_origin_attribute (lbl_die, origin);
18071   else
18072     add_name_and_src_coords_attributes (lbl_die, decl);
18073
18074   if (DECL_ABSTRACT (decl))
18075     equate_decl_number_to_die (decl, lbl_die);
18076   else
18077     {
18078       insn = DECL_RTL_IF_SET (decl);
18079
18080       /* Deleted labels are programmer specified labels which have been
18081          eliminated because of various optimizations.  We still emit them
18082          here so that it is possible to put breakpoints on them.  */
18083       if (insn
18084           && (LABEL_P (insn)
18085               || ((NOTE_P (insn)
18086                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18087         {
18088           /* When optimization is enabled (via -O) some parts of the compiler
18089              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18090              represent source-level labels which were explicitly declared by
18091              the user.  This really shouldn't be happening though, so catch
18092              it if it ever does happen.  */
18093           gcc_assert (!INSN_DELETED_P (insn));
18094
18095           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18096           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18097         }
18098       else if (insn
18099                && NOTE_P (insn)
18100                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18101                && CODE_LABEL_NUMBER (insn) != -1)
18102         {
18103           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18104           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18105         }
18106     }
18107 }
18108
18109 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18110    attributes to the DIE for a block STMT, to describe where the inlined
18111    function was called from.  This is similar to add_src_coords_attributes.  */
18112
18113 static inline void
18114 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18115 {
18116   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18117
18118   if (dwarf_version >= 3 || !dwarf_strict)
18119     {
18120       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18121       add_AT_unsigned (die, DW_AT_call_line, s.line);
18122     }
18123 }
18124
18125
18126 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18127    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18128
18129 static inline void
18130 add_high_low_attributes (tree stmt, dw_die_ref die)
18131 {
18132   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18133
18134   if (BLOCK_FRAGMENT_CHAIN (stmt)
18135       && (dwarf_version >= 3 || !dwarf_strict))
18136     {
18137       tree chain;
18138
18139       if (inlined_function_outer_scope_p (stmt))
18140         {
18141           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18142                                        BLOCK_NUMBER (stmt));
18143           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18144         }
18145
18146       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18147
18148       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18149       do
18150         {
18151           add_ranges (chain);
18152           chain = BLOCK_FRAGMENT_CHAIN (chain);
18153         }
18154       while (chain);
18155       add_ranges (NULL);
18156     }
18157   else
18158     {
18159       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18160                                    BLOCK_NUMBER (stmt));
18161       add_AT_lbl_id (die, DW_AT_low_pc, label);
18162       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18163                                    BLOCK_NUMBER (stmt));
18164       add_AT_lbl_id (die, DW_AT_high_pc, label);
18165     }
18166 }
18167
18168 /* Generate a DIE for a lexical block.  */
18169
18170 static void
18171 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18172 {
18173   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18174
18175   if (call_arg_locations)
18176     {
18177       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18178         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18179                                BLOCK_NUMBER (stmt) + 1);
18180       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18181     }
18182
18183   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18184     add_high_low_attributes (stmt, stmt_die);
18185
18186   decls_for_scope (stmt, stmt_die, depth);
18187 }
18188
18189 /* Generate a DIE for an inlined subprogram.  */
18190
18191 static void
18192 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18193 {
18194   tree decl;
18195
18196   /* The instance of function that is effectively being inlined shall not
18197      be abstract.  */
18198   gcc_assert (! BLOCK_ABSTRACT (stmt));
18199
18200   decl = block_ultimate_origin (stmt);
18201
18202   /* Emit info for the abstract instance first, if we haven't yet.  We
18203      must emit this even if the block is abstract, otherwise when we
18204      emit the block below (or elsewhere), we may end up trying to emit
18205      a die whose origin die hasn't been emitted, and crashing.  */
18206   dwarf2out_abstract_function (decl);
18207
18208   if (! BLOCK_ABSTRACT (stmt))
18209     {
18210       dw_die_ref subr_die
18211         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18212
18213       if (call_arg_locations)
18214         {
18215           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18216             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18217                                    BLOCK_NUMBER (stmt) + 1);
18218           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18219         }
18220       add_abstract_origin_attribute (subr_die, decl);
18221       if (TREE_ASM_WRITTEN (stmt))
18222         add_high_low_attributes (stmt, subr_die);
18223       add_call_src_coords_attributes (stmt, subr_die);
18224
18225       decls_for_scope (stmt, subr_die, depth);
18226       current_function_has_inlines = 1;
18227     }
18228 }
18229
18230 /* Generate a DIE for a field in a record, or structure.  */
18231
18232 static void
18233 gen_field_die (tree decl, dw_die_ref context_die)
18234 {
18235   dw_die_ref decl_die;
18236
18237   if (TREE_TYPE (decl) == error_mark_node)
18238     return;
18239
18240   decl_die = new_die (DW_TAG_member, context_die, decl);
18241   add_name_and_src_coords_attributes (decl_die, decl);
18242   add_type_attribute (decl_die, member_declared_type (decl),
18243                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18244                       context_die);
18245
18246   if (DECL_BIT_FIELD_TYPE (decl))
18247     {
18248       add_byte_size_attribute (decl_die, decl);
18249       add_bit_size_attribute (decl_die, decl);
18250       add_bit_offset_attribute (decl_die, decl);
18251     }
18252
18253   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18254     add_data_member_location_attribute (decl_die, decl);
18255
18256   if (DECL_ARTIFICIAL (decl))
18257     add_AT_flag (decl_die, DW_AT_artificial, 1);
18258
18259   add_accessibility_attribute (decl_die, decl);
18260
18261   /* Equate decl number to die, so that we can look up this decl later on.  */
18262   equate_decl_number_to_die (decl, decl_die);
18263 }
18264
18265 #if 0
18266 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18267    Use modified_type_die instead.
18268    We keep this code here just in case these types of DIEs may be needed to
18269    represent certain things in other languages (e.g. Pascal) someday.  */
18270
18271 static void
18272 gen_pointer_type_die (tree type, dw_die_ref context_die)
18273 {
18274   dw_die_ref ptr_die
18275     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18276
18277   equate_type_number_to_die (type, ptr_die);
18278   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18279   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18280 }
18281
18282 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18283    Use modified_type_die instead.
18284    We keep this code here just in case these types of DIEs may be needed to
18285    represent certain things in other languages (e.g. Pascal) someday.  */
18286
18287 static void
18288 gen_reference_type_die (tree type, dw_die_ref context_die)
18289 {
18290   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18291
18292   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18293     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18294   else
18295     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18296
18297   equate_type_number_to_die (type, ref_die);
18298   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18299   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18300 }
18301 #endif
18302
18303 /* Generate a DIE for a pointer to a member type.  */
18304
18305 static void
18306 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18307 {
18308   dw_die_ref ptr_die
18309     = new_die (DW_TAG_ptr_to_member_type,
18310                scope_die_for (type, context_die), type);
18311
18312   equate_type_number_to_die (type, ptr_die);
18313   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18314                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18315   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18316 }
18317
18318 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18319 DEF_VEC_P(dchar_p);
18320 DEF_VEC_ALLOC_P(dchar_p,heap);
18321
18322 static char *producer_string;
18323
18324 /* Return a heap allocated producer string including command line options
18325    if -grecord-gcc-switches.  */
18326
18327 static char *
18328 gen_producer_string (void)
18329 {
18330   size_t j;
18331   VEC(dchar_p, heap) *switches = NULL;
18332   const char *language_string = lang_hooks.name;
18333   char *producer, *tail;
18334   const char *p;
18335   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18336   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18337
18338   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18339     switch (save_decoded_options[j].opt_index)
18340       {
18341       case OPT_o:
18342       case OPT_d:
18343       case OPT_dumpbase:
18344       case OPT_dumpdir:
18345       case OPT_auxbase:
18346       case OPT_auxbase_strip:
18347       case OPT_quiet:
18348       case OPT_version:
18349       case OPT_v:
18350       case OPT_w:
18351       case OPT_L:
18352       case OPT_D:
18353       case OPT_I:
18354       case OPT_U:
18355       case OPT_SPECIAL_unknown:
18356       case OPT_SPECIAL_ignore:
18357       case OPT_SPECIAL_program_name:
18358       case OPT_SPECIAL_input_file:
18359       case OPT_grecord_gcc_switches:
18360       case OPT_gno_record_gcc_switches:
18361       case OPT__output_pch_:
18362       case OPT_fdiagnostics_show_location_:
18363       case OPT_fdiagnostics_show_option:
18364       case OPT_fverbose_asm:
18365       case OPT____:
18366       case OPT__sysroot_:
18367       case OPT_nostdinc:
18368       case OPT_nostdinc__:
18369         /* Ignore these.  */
18370         continue;
18371       default:
18372         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18373                              == '-');
18374         switch (save_decoded_options[j].canonical_option[0][1])
18375           {
18376           case 'M':
18377           case 'i':
18378           case 'W':
18379             continue;
18380           case 'f':
18381             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18382                          "dump", 4) == 0)
18383               continue;
18384             break;
18385           default:
18386             break;
18387           }
18388         VEC_safe_push (dchar_p, heap, switches,
18389                        save_decoded_options[j].orig_option_with_args_text);
18390         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18391         break;
18392       }
18393
18394   producer = XNEWVEC (char, plen + 1 + len + 1);
18395   tail = producer;
18396   sprintf (tail, "%s %s", language_string, version_string);
18397   tail += plen;
18398
18399   if (!dwarf_record_gcc_switches)
18400     {
18401 #ifdef MIPS_DEBUGGING_INFO
18402       /* The MIPS/SGI compilers place the 'cc' command line options in the
18403          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18404          if they do not appear in the producer string, the debugger reaches
18405          the conclusion that the object file is stripped and has no debugging
18406          information.  To get the MIPS/SGI debugger to believe that there is
18407          debugging information in the object file, we add a -g to the producer
18408          string.  */
18409       if (debug_info_level > DINFO_LEVEL_TERSE)
18410         {
18411           memcpy (tail, " -g", 3);
18412           tail += 3;
18413         }
18414 #endif
18415     }
18416
18417   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18418     {
18419       len = strlen (p);
18420       *tail = ' ';
18421       memcpy (tail + 1, p, len);
18422       tail += len + 1;
18423     }
18424
18425   *tail = '\0';
18426   VEC_free (dchar_p, heap, switches);
18427   return producer;
18428 }
18429
18430 /* Generate the DIE for the compilation unit.  */
18431
18432 static dw_die_ref
18433 gen_compile_unit_die (const char *filename)
18434 {
18435   dw_die_ref die;
18436   const char *language_string = lang_hooks.name;
18437   int language;
18438
18439   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18440
18441   if (filename)
18442     {
18443       add_name_attribute (die, filename);
18444       /* Don't add cwd for <built-in>.  */
18445       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18446         add_comp_dir_attribute (die);
18447     }
18448
18449   if (producer_string == NULL)
18450     producer_string = gen_producer_string ();
18451   add_AT_string (die, DW_AT_producer, producer_string);
18452
18453   /* If our producer is LTO try to figure out a common language to use
18454      from the global list of translation units.  */
18455   if (strcmp (language_string, "GNU GIMPLE") == 0)
18456     {
18457       unsigned i;
18458       tree t;
18459       const char *common_lang = NULL;
18460
18461       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18462         {
18463           if (!TRANSLATION_UNIT_LANGUAGE (t))
18464             continue;
18465           if (!common_lang)
18466             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18467           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18468             ;
18469           else if (strncmp (common_lang, "GNU C", 5) == 0
18470                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18471             /* Mixing C and C++ is ok, use C++ in that case.  */
18472             common_lang = "GNU C++";
18473           else
18474             {
18475               /* Fall back to C.  */
18476               common_lang = NULL;
18477               break;
18478             }
18479         }
18480
18481       if (common_lang)
18482         language_string = common_lang;
18483     }
18484
18485   language = DW_LANG_C89;
18486   if (strcmp (language_string, "GNU C++") == 0)
18487     language = DW_LANG_C_plus_plus;
18488   else if (strcmp (language_string, "GNU F77") == 0)
18489     language = DW_LANG_Fortran77;
18490   else if (strcmp (language_string, "GNU Pascal") == 0)
18491     language = DW_LANG_Pascal83;
18492   else if (dwarf_version >= 3 || !dwarf_strict)
18493     {
18494       if (strcmp (language_string, "GNU Ada") == 0)
18495         language = DW_LANG_Ada95;
18496       else if (strcmp (language_string, "GNU Fortran") == 0)
18497         language = DW_LANG_Fortran95;
18498       else if (strcmp (language_string, "GNU Java") == 0)
18499         language = DW_LANG_Java;
18500       else if (strcmp (language_string, "GNU Objective-C") == 0)
18501         language = DW_LANG_ObjC;
18502       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18503         language = DW_LANG_ObjC_plus_plus;
18504       else if (dwarf_version >= 5 || !dwarf_strict)
18505         {
18506           if (strcmp (language_string, "GNU Go") == 0)
18507             language = DW_LANG_Go;
18508         }
18509     }
18510
18511   add_AT_unsigned (die, DW_AT_language, language);
18512
18513   switch (language)
18514     {
18515     case DW_LANG_Fortran77:
18516     case DW_LANG_Fortran90:
18517     case DW_LANG_Fortran95:
18518       /* Fortran has case insensitive identifiers and the front-end
18519          lowercases everything.  */
18520       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18521       break;
18522     default:
18523       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18524       break;
18525     }
18526   return die;
18527 }
18528
18529 /* Generate the DIE for a base class.  */
18530
18531 static void
18532 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18533 {
18534   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18535
18536   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18537   add_data_member_location_attribute (die, binfo);
18538
18539   if (BINFO_VIRTUAL_P (binfo))
18540     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18541
18542   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18543      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18544      the default has always been DW_ACCESS_private.  */
18545   if (access == access_public_node)
18546     {
18547       if (dwarf_version == 2
18548           || context_die->die_tag == DW_TAG_class_type)
18549       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18550     }
18551   else if (access == access_protected_node)
18552     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18553   else if (dwarf_version > 2
18554            && context_die->die_tag != DW_TAG_class_type)
18555     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18556 }
18557
18558 /* Generate a DIE for a class member.  */
18559
18560 static void
18561 gen_member_die (tree type, dw_die_ref context_die)
18562 {
18563   tree member;
18564   tree binfo = TYPE_BINFO (type);
18565   dw_die_ref child;
18566
18567   /* If this is not an incomplete type, output descriptions of each of its
18568      members. Note that as we output the DIEs necessary to represent the
18569      members of this record or union type, we will also be trying to output
18570      DIEs to represent the *types* of those members. However the `type'
18571      function (above) will specifically avoid generating type DIEs for member
18572      types *within* the list of member DIEs for this (containing) type except
18573      for those types (of members) which are explicitly marked as also being
18574      members of this (containing) type themselves.  The g++ front- end can
18575      force any given type to be treated as a member of some other (containing)
18576      type by setting the TYPE_CONTEXT of the given (member) type to point to
18577      the TREE node representing the appropriate (containing) type.  */
18578
18579   /* First output info about the base classes.  */
18580   if (binfo)
18581     {
18582       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18583       int i;
18584       tree base;
18585
18586       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18587         gen_inheritance_die (base,
18588                              (accesses ? VEC_index (tree, accesses, i)
18589                               : access_public_node), context_die);
18590     }
18591
18592   /* Now output info about the data members and type members.  */
18593   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18594     {
18595       /* If we thought we were generating minimal debug info for TYPE
18596          and then changed our minds, some of the member declarations
18597          may have already been defined.  Don't define them again, but
18598          do put them in the right order.  */
18599
18600       child = lookup_decl_die (member);
18601       if (child)
18602         splice_child_die (context_die, child);
18603       else
18604         gen_decl_die (member, NULL, context_die);
18605     }
18606
18607   /* Now output info about the function members (if any).  */
18608   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18609     {
18610       /* Don't include clones in the member list.  */
18611       if (DECL_ABSTRACT_ORIGIN (member))
18612         continue;
18613
18614       child = lookup_decl_die (member);
18615       if (child)
18616         splice_child_die (context_die, child);
18617       else
18618         gen_decl_die (member, NULL, context_die);
18619     }
18620 }
18621
18622 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18623    is set, we pretend that the type was never defined, so we only get the
18624    member DIEs needed by later specification DIEs.  */
18625
18626 static void
18627 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18628                                 enum debug_info_usage usage)
18629 {
18630   dw_die_ref type_die = lookup_type_die (type);
18631   dw_die_ref scope_die = 0;
18632   int nested = 0;
18633   int complete = (TYPE_SIZE (type)
18634                   && (! TYPE_STUB_DECL (type)
18635                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18636   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18637   complete = complete && should_emit_struct_debug (type, usage);
18638
18639   if (type_die && ! complete)
18640     return;
18641
18642   if (TYPE_CONTEXT (type) != NULL_TREE
18643       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18644           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18645     nested = 1;
18646
18647   scope_die = scope_die_for (type, context_die);
18648
18649   if (! type_die || (nested && is_cu_die (scope_die)))
18650     /* First occurrence of type or toplevel definition of nested class.  */
18651     {
18652       dw_die_ref old_die = type_die;
18653
18654       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18655                           ? record_type_tag (type) : DW_TAG_union_type,
18656                           scope_die, type);
18657       equate_type_number_to_die (type, type_die);
18658       if (old_die)
18659         add_AT_specification (type_die, old_die);
18660       else
18661         add_name_attribute (type_die, type_tag (type));
18662     }
18663   else
18664     remove_AT (type_die, DW_AT_declaration);
18665
18666   /* Generate child dies for template paramaters.  */
18667   if (debug_info_level > DINFO_LEVEL_TERSE
18668       && COMPLETE_TYPE_P (type))
18669     schedule_generic_params_dies_gen (type);
18670
18671   /* If this type has been completed, then give it a byte_size attribute and
18672      then give a list of members.  */
18673   if (complete && !ns_decl)
18674     {
18675       /* Prevent infinite recursion in cases where the type of some member of
18676          this type is expressed in terms of this type itself.  */
18677       TREE_ASM_WRITTEN (type) = 1;
18678       add_byte_size_attribute (type_die, type);
18679       if (TYPE_STUB_DECL (type) != NULL_TREE)
18680         {
18681           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18682           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18683         }
18684
18685       /* If the first reference to this type was as the return type of an
18686          inline function, then it may not have a parent.  Fix this now.  */
18687       if (type_die->die_parent == NULL)
18688         add_child_die (scope_die, type_die);
18689
18690       push_decl_scope (type);
18691       gen_member_die (type, type_die);
18692       pop_decl_scope ();
18693
18694       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18695       if (TYPE_ARTIFICIAL (type))
18696         add_AT_flag (type_die, DW_AT_artificial, 1);
18697
18698       /* GNU extension: Record what type our vtable lives in.  */
18699       if (TYPE_VFIELD (type))
18700         {
18701           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18702
18703           gen_type_die (vtype, context_die);
18704           add_AT_die_ref (type_die, DW_AT_containing_type,
18705                           lookup_type_die (vtype));
18706         }
18707     }
18708   else
18709     {
18710       add_AT_flag (type_die, DW_AT_declaration, 1);
18711
18712       /* We don't need to do this for function-local types.  */
18713       if (TYPE_STUB_DECL (type)
18714           && ! decl_function_context (TYPE_STUB_DECL (type)))
18715         VEC_safe_push (tree, gc, incomplete_types, type);
18716     }
18717
18718   if (get_AT (type_die, DW_AT_name))
18719     add_pubtype (type, type_die);
18720 }
18721
18722 /* Generate a DIE for a subroutine _type_.  */
18723
18724 static void
18725 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18726 {
18727   tree return_type = TREE_TYPE (type);
18728   dw_die_ref subr_die
18729     = new_die (DW_TAG_subroutine_type,
18730                scope_die_for (type, context_die), type);
18731
18732   equate_type_number_to_die (type, subr_die);
18733   add_prototyped_attribute (subr_die, type);
18734   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18735   gen_formal_types_die (type, subr_die);
18736
18737   if (get_AT (subr_die, DW_AT_name))
18738     add_pubtype (type, subr_die);
18739 }
18740
18741 /* Generate a DIE for a type definition.  */
18742
18743 static void
18744 gen_typedef_die (tree decl, dw_die_ref context_die)
18745 {
18746   dw_die_ref type_die;
18747   tree origin;
18748
18749   if (TREE_ASM_WRITTEN (decl))
18750     return;
18751
18752   TREE_ASM_WRITTEN (decl) = 1;
18753   type_die = new_die (DW_TAG_typedef, context_die, decl);
18754   origin = decl_ultimate_origin (decl);
18755   if (origin != NULL)
18756     add_abstract_origin_attribute (type_die, origin);
18757   else
18758     {
18759       tree type;
18760
18761       add_name_and_src_coords_attributes (type_die, decl);
18762       if (DECL_ORIGINAL_TYPE (decl))
18763         {
18764           type = DECL_ORIGINAL_TYPE (decl);
18765
18766           gcc_assert (type != TREE_TYPE (decl));
18767           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18768         }
18769       else
18770         {
18771           type = TREE_TYPE (decl);
18772
18773           if (is_naming_typedef_decl (TYPE_NAME (type)))
18774             {
18775               /* Here, we are in the case of decl being a typedef naming
18776                  an anonymous type, e.g:
18777                      typedef struct {...} foo;
18778                  In that case TREE_TYPE (decl) is not a typedef variant
18779                  type and TYPE_NAME of the anonymous type is set to the
18780                  TYPE_DECL of the typedef. This construct is emitted by
18781                  the C++ FE.
18782
18783                  TYPE is the anonymous struct named by the typedef
18784                  DECL. As we need the DW_AT_type attribute of the
18785                  DW_TAG_typedef to point to the DIE of TYPE, let's
18786                  generate that DIE right away. add_type_attribute
18787                  called below will then pick (via lookup_type_die) that
18788                  anonymous struct DIE.  */
18789               if (!TREE_ASM_WRITTEN (type))
18790                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18791
18792               /* This is a GNU Extension.  We are adding a
18793                  DW_AT_linkage_name attribute to the DIE of the
18794                  anonymous struct TYPE.  The value of that attribute
18795                  is the name of the typedef decl naming the anonymous
18796                  struct.  This greatly eases the work of consumers of
18797                  this debug info.  */
18798               add_linkage_attr (lookup_type_die (type), decl);
18799             }
18800         }
18801
18802       add_type_attribute (type_die, type, TREE_READONLY (decl),
18803                           TREE_THIS_VOLATILE (decl), context_die);
18804
18805       if (is_naming_typedef_decl (decl))
18806         /* We want that all subsequent calls to lookup_type_die with
18807            TYPE in argument yield the DW_TAG_typedef we have just
18808            created.  */
18809         equate_type_number_to_die (type, type_die);
18810
18811       add_accessibility_attribute (type_die, decl);
18812     }
18813
18814   if (DECL_ABSTRACT (decl))
18815     equate_decl_number_to_die (decl, type_die);
18816
18817   if (get_AT (type_die, DW_AT_name))
18818     add_pubtype (decl, type_die);
18819 }
18820
18821 /* Generate a DIE for a struct, class, enum or union type.  */
18822
18823 static void
18824 gen_tagged_type_die (tree type,
18825                      dw_die_ref context_die,
18826                      enum debug_info_usage usage)
18827 {
18828   int need_pop;
18829
18830   if (type == NULL_TREE
18831       || !is_tagged_type (type))
18832     return;
18833
18834   /* If this is a nested type whose containing class hasn't been written
18835      out yet, writing it out will cover this one, too.  This does not apply
18836      to instantiations of member class templates; they need to be added to
18837      the containing class as they are generated.  FIXME: This hurts the
18838      idea of combining type decls from multiple TUs, since we can't predict
18839      what set of template instantiations we'll get.  */
18840   if (TYPE_CONTEXT (type)
18841       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18842       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18843     {
18844       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18845
18846       if (TREE_ASM_WRITTEN (type))
18847         return;
18848
18849       /* If that failed, attach ourselves to the stub.  */
18850       push_decl_scope (TYPE_CONTEXT (type));
18851       context_die = lookup_type_die (TYPE_CONTEXT (type));
18852       need_pop = 1;
18853     }
18854   else if (TYPE_CONTEXT (type) != NULL_TREE
18855            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18856     {
18857       /* If this type is local to a function that hasn't been written
18858          out yet, use a NULL context for now; it will be fixed up in
18859          decls_for_scope.  */
18860       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18861       /* A declaration DIE doesn't count; nested types need to go in the
18862          specification.  */
18863       if (context_die && is_declaration_die (context_die))
18864         context_die = NULL;
18865       need_pop = 0;
18866     }
18867   else
18868     {
18869       context_die = declare_in_namespace (type, context_die);
18870       need_pop = 0;
18871     }
18872
18873   if (TREE_CODE (type) == ENUMERAL_TYPE)
18874     {
18875       /* This might have been written out by the call to
18876          declare_in_namespace.  */
18877       if (!TREE_ASM_WRITTEN (type))
18878         gen_enumeration_type_die (type, context_die);
18879     }
18880   else
18881     gen_struct_or_union_type_die (type, context_die, usage);
18882
18883   if (need_pop)
18884     pop_decl_scope ();
18885
18886   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18887      it up if it is ever completed.  gen_*_type_die will set it for us
18888      when appropriate.  */
18889 }
18890
18891 /* Generate a type description DIE.  */
18892
18893 static void
18894 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18895                          enum debug_info_usage usage)
18896 {
18897   struct array_descr_info info;
18898
18899   if (type == NULL_TREE || type == error_mark_node)
18900     return;
18901
18902   if (TYPE_NAME (type) != NULL_TREE
18903       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18904       && is_redundant_typedef (TYPE_NAME (type))
18905       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18906     /* The DECL of this type is a typedef we don't want to emit debug
18907        info for but we want debug info for its underlying typedef.
18908        This can happen for e.g, the injected-class-name of a C++
18909        type.  */
18910     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18911
18912   /* If TYPE is a typedef type variant, let's generate debug info
18913      for the parent typedef which TYPE is a type of.  */
18914   if (typedef_variant_p (type))
18915     {
18916       if (TREE_ASM_WRITTEN (type))
18917         return;
18918
18919       /* Prevent broken recursion; we can't hand off to the same type.  */
18920       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18921
18922       /* Use the DIE of the containing namespace as the parent DIE of
18923          the type description DIE we want to generate.  */
18924       if (DECL_FILE_SCOPE_P (TYPE_NAME (type))
18925           || (DECL_CONTEXT (TYPE_NAME (type))
18926               && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL))
18927         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18928
18929       TREE_ASM_WRITTEN (type) = 1;
18930
18931       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18932       return;
18933     }
18934
18935   /* If type is an anonymous tagged type named by a typedef, let's
18936      generate debug info for the typedef.  */
18937   if (is_naming_typedef_decl (TYPE_NAME (type)))
18938     {
18939       /* Use the DIE of the containing namespace as the parent DIE of
18940          the type description DIE we want to generate.  */
18941       if (DECL_CONTEXT (TYPE_NAME (type))
18942           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18943         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18944       
18945       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18946       return;
18947     }
18948
18949   /* If this is an array type with hidden descriptor, handle it first.  */
18950   if (!TREE_ASM_WRITTEN (type)
18951       && lang_hooks.types.get_array_descr_info
18952       && lang_hooks.types.get_array_descr_info (type, &info)
18953       && (dwarf_version >= 3 || !dwarf_strict))
18954     {
18955       gen_descr_array_type_die (type, &info, context_die);
18956       TREE_ASM_WRITTEN (type) = 1;
18957       return;
18958     }
18959
18960   /* We are going to output a DIE to represent the unqualified version
18961      of this type (i.e. without any const or volatile qualifiers) so
18962      get the main variant (i.e. the unqualified version) of this type
18963      now.  (Vectors are special because the debugging info is in the
18964      cloned type itself).  */
18965   if (TREE_CODE (type) != VECTOR_TYPE)
18966     type = type_main_variant (type);
18967
18968   if (TREE_ASM_WRITTEN (type))
18969     return;
18970
18971   switch (TREE_CODE (type))
18972     {
18973     case ERROR_MARK:
18974       break;
18975
18976     case POINTER_TYPE:
18977     case REFERENCE_TYPE:
18978       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18979          ensures that the gen_type_die recursion will terminate even if the
18980          type is recursive.  Recursive types are possible in Ada.  */
18981       /* ??? We could perhaps do this for all types before the switch
18982          statement.  */
18983       TREE_ASM_WRITTEN (type) = 1;
18984
18985       /* For these types, all that is required is that we output a DIE (or a
18986          set of DIEs) to represent the "basis" type.  */
18987       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18988                                 DINFO_USAGE_IND_USE);
18989       break;
18990
18991     case OFFSET_TYPE:
18992       /* This code is used for C++ pointer-to-data-member types.
18993          Output a description of the relevant class type.  */
18994       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18995                                         DINFO_USAGE_IND_USE);
18996
18997       /* Output a description of the type of the object pointed to.  */
18998       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18999                                         DINFO_USAGE_IND_USE);
19000
19001       /* Now output a DIE to represent this pointer-to-data-member type
19002          itself.  */
19003       gen_ptr_to_mbr_type_die (type, context_die);
19004       break;
19005
19006     case FUNCTION_TYPE:
19007       /* Force out return type (in case it wasn't forced out already).  */
19008       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19009                                         DINFO_USAGE_DIR_USE);
19010       gen_subroutine_type_die (type, context_die);
19011       break;
19012
19013     case METHOD_TYPE:
19014       /* Force out return type (in case it wasn't forced out already).  */
19015       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19016                                         DINFO_USAGE_DIR_USE);
19017       gen_subroutine_type_die (type, context_die);
19018       break;
19019
19020     case ARRAY_TYPE:
19021       gen_array_type_die (type, context_die);
19022       break;
19023
19024     case VECTOR_TYPE:
19025       gen_array_type_die (type, context_die);
19026       break;
19027
19028     case ENUMERAL_TYPE:
19029     case RECORD_TYPE:
19030     case UNION_TYPE:
19031     case QUAL_UNION_TYPE:
19032       gen_tagged_type_die (type, context_die, usage);
19033       return;
19034
19035     case VOID_TYPE:
19036     case INTEGER_TYPE:
19037     case REAL_TYPE:
19038     case FIXED_POINT_TYPE:
19039     case COMPLEX_TYPE:
19040     case BOOLEAN_TYPE:
19041       /* No DIEs needed for fundamental types.  */
19042       break;
19043
19044     case NULLPTR_TYPE:
19045     case LANG_TYPE:
19046       /* Just use DW_TAG_unspecified_type.  */
19047       {
19048         dw_die_ref type_die = lookup_type_die (type);
19049         if (type_die == NULL)
19050           {
19051             tree name = TYPE_NAME (type);
19052             if (TREE_CODE (name) == TYPE_DECL)
19053               name = DECL_NAME (name);
19054             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19055             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19056             equate_type_number_to_die (type, type_die);
19057           }
19058       }
19059       break;
19060
19061     default:
19062       gcc_unreachable ();
19063     }
19064
19065   TREE_ASM_WRITTEN (type) = 1;
19066 }
19067
19068 static void
19069 gen_type_die (tree type, dw_die_ref context_die)
19070 {
19071   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19072 }
19073
19074 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19075    things which are local to the given block.  */
19076
19077 static void
19078 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19079 {
19080   int must_output_die = 0;
19081   bool inlined_func;
19082
19083   /* Ignore blocks that are NULL.  */
19084   if (stmt == NULL_TREE)
19085     return;
19086
19087   inlined_func = inlined_function_outer_scope_p (stmt);
19088
19089   /* If the block is one fragment of a non-contiguous block, do not
19090      process the variables, since they will have been done by the
19091      origin block.  Do process subblocks.  */
19092   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19093     {
19094       tree sub;
19095
19096       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19097         gen_block_die (sub, context_die, depth + 1);
19098
19099       return;
19100     }
19101
19102   /* Determine if we need to output any Dwarf DIEs at all to represent this
19103      block.  */
19104   if (inlined_func)
19105     /* The outer scopes for inlinings *must* always be represented.  We
19106        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19107     must_output_die = 1;
19108   else
19109     {
19110       /* Determine if this block directly contains any "significant"
19111          local declarations which we will need to output DIEs for.  */
19112       if (debug_info_level > DINFO_LEVEL_TERSE)
19113         /* We are not in terse mode so *any* local declaration counts
19114            as being a "significant" one.  */
19115         must_output_die = ((BLOCK_VARS (stmt) != NULL
19116                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19117                            && (TREE_USED (stmt)
19118                                || TREE_ASM_WRITTEN (stmt)
19119                                || BLOCK_ABSTRACT (stmt)));
19120       else if ((TREE_USED (stmt)
19121                 || TREE_ASM_WRITTEN (stmt)
19122                 || BLOCK_ABSTRACT (stmt))
19123                && !dwarf2out_ignore_block (stmt))
19124         must_output_die = 1;
19125     }
19126
19127   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19128      DIE for any block which contains no significant local declarations at
19129      all.  Rather, in such cases we just call `decls_for_scope' so that any
19130      needed Dwarf info for any sub-blocks will get properly generated. Note
19131      that in terse mode, our definition of what constitutes a "significant"
19132      local declaration gets restricted to include only inlined function
19133      instances and local (nested) function definitions.  */
19134   if (must_output_die)
19135     {
19136       if (inlined_func)
19137         {
19138           /* If STMT block is abstract, that means we have been called
19139              indirectly from dwarf2out_abstract_function.
19140              That function rightfully marks the descendent blocks (of
19141              the abstract function it is dealing with) as being abstract,
19142              precisely to prevent us from emitting any
19143              DW_TAG_inlined_subroutine DIE as a descendent
19144              of an abstract function instance. So in that case, we should
19145              not call gen_inlined_subroutine_die.
19146
19147              Later though, when cgraph asks dwarf2out to emit info
19148              for the concrete instance of the function decl into which
19149              the concrete instance of STMT got inlined, the later will lead
19150              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19151           if (! BLOCK_ABSTRACT (stmt))
19152             gen_inlined_subroutine_die (stmt, context_die, depth);
19153         }
19154       else
19155         gen_lexical_block_die (stmt, context_die, depth);
19156     }
19157   else
19158     decls_for_scope (stmt, context_die, depth);
19159 }
19160
19161 /* Process variable DECL (or variable with origin ORIGIN) within
19162    block STMT and add it to CONTEXT_DIE.  */
19163 static void
19164 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19165 {
19166   dw_die_ref die;
19167   tree decl_or_origin = decl ? decl : origin;
19168
19169   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19170     die = lookup_decl_die (decl_or_origin);
19171   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19172            && TYPE_DECL_IS_STUB (decl_or_origin))
19173     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19174   else
19175     die = NULL;
19176
19177   if (die != NULL && die->die_parent == NULL)
19178     add_child_die (context_die, die);
19179   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19180     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19181                                          stmt, context_die);
19182   else
19183     gen_decl_die (decl, origin, context_die);
19184 }
19185
19186 /* Generate all of the decls declared within a given scope and (recursively)
19187    all of its sub-blocks.  */
19188
19189 static void
19190 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19191 {
19192   tree decl;
19193   unsigned int i;
19194   tree subblocks;
19195
19196   /* Ignore NULL blocks.  */
19197   if (stmt == NULL_TREE)
19198     return;
19199
19200   /* Output the DIEs to represent all of the data objects and typedefs
19201      declared directly within this block but not within any nested
19202      sub-blocks.  Also, nested function and tag DIEs have been
19203      generated with a parent of NULL; fix that up now.  */
19204   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19205     process_scope_var (stmt, decl, NULL_TREE, context_die);
19206   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19207     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19208                        context_die);
19209
19210   /* If we're at -g1, we're not interested in subblocks.  */
19211   if (debug_info_level <= DINFO_LEVEL_TERSE)
19212     return;
19213
19214   /* Output the DIEs to represent all sub-blocks (and the items declared
19215      therein) of this block.  */
19216   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19217        subblocks != NULL;
19218        subblocks = BLOCK_CHAIN (subblocks))
19219     gen_block_die (subblocks, context_die, depth + 1);
19220 }
19221
19222 /* Is this a typedef we can avoid emitting?  */
19223
19224 static inline int
19225 is_redundant_typedef (const_tree decl)
19226 {
19227   if (TYPE_DECL_IS_STUB (decl))
19228     return 1;
19229
19230   if (DECL_ARTIFICIAL (decl)
19231       && DECL_CONTEXT (decl)
19232       && is_tagged_type (DECL_CONTEXT (decl))
19233       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19234       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19235     /* Also ignore the artificial member typedef for the class name.  */
19236     return 1;
19237
19238   return 0;
19239 }
19240
19241 /* Return TRUE if TYPE is a typedef that names a type for linkage
19242    purposes. This kind of typedefs is produced by the C++ FE for
19243    constructs like:
19244
19245    typedef struct {...} foo;
19246
19247    In that case, there is no typedef variant type produced for foo.
19248    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19249    struct type.  */
19250
19251 static bool
19252 is_naming_typedef_decl (const_tree decl)
19253 {
19254   if (decl == NULL_TREE
19255       || TREE_CODE (decl) != TYPE_DECL
19256       || !is_tagged_type (TREE_TYPE (decl))
19257       || DECL_IS_BUILTIN (decl)
19258       || is_redundant_typedef (decl)
19259       /* It looks like Ada produces TYPE_DECLs that are very similar
19260          to C++ naming typedefs but that have different
19261          semantics. Let's be specific to c++ for now.  */
19262       || !is_cxx ())
19263     return FALSE;
19264
19265   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19266           && TYPE_NAME (TREE_TYPE (decl)) == decl
19267           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19268               != TYPE_NAME (TREE_TYPE (decl))));
19269 }
19270
19271 /* Returns the DIE for a context.  */
19272
19273 static inline dw_die_ref
19274 get_context_die (tree context)
19275 {
19276   if (context)
19277     {
19278       /* Find die that represents this context.  */
19279       if (TYPE_P (context))
19280         {
19281           context = TYPE_MAIN_VARIANT (context);
19282           return strip_naming_typedef (context, force_type_die (context));
19283         }
19284       else
19285         return force_decl_die (context);
19286     }
19287   return comp_unit_die ();
19288 }
19289
19290 /* Returns the DIE for decl.  A DIE will always be returned.  */
19291
19292 static dw_die_ref
19293 force_decl_die (tree decl)
19294 {
19295   dw_die_ref decl_die;
19296   unsigned saved_external_flag;
19297   tree save_fn = NULL_TREE;
19298   decl_die = lookup_decl_die (decl);
19299   if (!decl_die)
19300     {
19301       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19302
19303       decl_die = lookup_decl_die (decl);
19304       if (decl_die)
19305         return decl_die;
19306
19307       switch (TREE_CODE (decl))
19308         {
19309         case FUNCTION_DECL:
19310           /* Clear current_function_decl, so that gen_subprogram_die thinks
19311              that this is a declaration. At this point, we just want to force
19312              declaration die.  */
19313           save_fn = current_function_decl;
19314           current_function_decl = NULL_TREE;
19315           gen_subprogram_die (decl, context_die);
19316           current_function_decl = save_fn;
19317           break;
19318
19319         case VAR_DECL:
19320           /* Set external flag to force declaration die. Restore it after
19321            gen_decl_die() call.  */
19322           saved_external_flag = DECL_EXTERNAL (decl);
19323           DECL_EXTERNAL (decl) = 1;
19324           gen_decl_die (decl, NULL, context_die);
19325           DECL_EXTERNAL (decl) = saved_external_flag;
19326           break;
19327
19328         case NAMESPACE_DECL:
19329           if (dwarf_version >= 3 || !dwarf_strict)
19330             dwarf2out_decl (decl);
19331           else
19332             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19333             decl_die = comp_unit_die ();
19334           break;
19335
19336         case TRANSLATION_UNIT_DECL:
19337           decl_die = comp_unit_die ();
19338           break;
19339
19340         default:
19341           gcc_unreachable ();
19342         }
19343
19344       /* We should be able to find the DIE now.  */
19345       if (!decl_die)
19346         decl_die = lookup_decl_die (decl);
19347       gcc_assert (decl_die);
19348     }
19349
19350   return decl_die;
19351 }
19352
19353 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19354    always returned.  */
19355
19356 static dw_die_ref
19357 force_type_die (tree type)
19358 {
19359   dw_die_ref type_die;
19360
19361   type_die = lookup_type_die (type);
19362   if (!type_die)
19363     {
19364       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19365
19366       type_die = modified_type_die (type, TYPE_READONLY (type),
19367                                     TYPE_VOLATILE (type), context_die);
19368       gcc_assert (type_die);
19369     }
19370   return type_die;
19371 }
19372
19373 /* Force out any required namespaces to be able to output DECL,
19374    and return the new context_die for it, if it's changed.  */
19375
19376 static dw_die_ref
19377 setup_namespace_context (tree thing, dw_die_ref context_die)
19378 {
19379   tree context = (DECL_P (thing)
19380                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19381   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19382     /* Force out the namespace.  */
19383     context_die = force_decl_die (context);
19384
19385   return context_die;
19386 }
19387
19388 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19389    type) within its namespace, if appropriate.
19390
19391    For compatibility with older debuggers, namespace DIEs only contain
19392    declarations; all definitions are emitted at CU scope.  */
19393
19394 static dw_die_ref
19395 declare_in_namespace (tree thing, dw_die_ref context_die)
19396 {
19397   dw_die_ref ns_context;
19398
19399   if (debug_info_level <= DINFO_LEVEL_TERSE)
19400     return context_die;
19401
19402   /* If this decl is from an inlined function, then don't try to emit it in its
19403      namespace, as we will get confused.  It would have already been emitted
19404      when the abstract instance of the inline function was emitted anyways.  */
19405   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19406     return context_die;
19407
19408   ns_context = setup_namespace_context (thing, context_die);
19409
19410   if (ns_context != context_die)
19411     {
19412       if (is_fortran ())
19413         return ns_context;
19414       if (DECL_P (thing))
19415         gen_decl_die (thing, NULL, ns_context);
19416       else
19417         gen_type_die (thing, ns_context);
19418     }
19419   return context_die;
19420 }
19421
19422 /* Generate a DIE for a namespace or namespace alias.  */
19423
19424 static void
19425 gen_namespace_die (tree decl, dw_die_ref context_die)
19426 {
19427   dw_die_ref namespace_die;
19428
19429   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19430      they are an alias of.  */
19431   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19432     {
19433       /* Output a real namespace or module.  */
19434       context_die = setup_namespace_context (decl, comp_unit_die ());
19435       namespace_die = new_die (is_fortran ()
19436                                ? DW_TAG_module : DW_TAG_namespace,
19437                                context_die, decl);
19438       /* For Fortran modules defined in different CU don't add src coords.  */
19439       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19440         {
19441           const char *name = dwarf2_name (decl, 0);
19442           if (name)
19443             add_name_attribute (namespace_die, name);
19444         }
19445       else
19446         add_name_and_src_coords_attributes (namespace_die, decl);
19447       if (DECL_EXTERNAL (decl))
19448         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19449       equate_decl_number_to_die (decl, namespace_die);
19450     }
19451   else
19452     {
19453       /* Output a namespace alias.  */
19454
19455       /* Force out the namespace we are an alias of, if necessary.  */
19456       dw_die_ref origin_die
19457         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19458
19459       if (DECL_FILE_SCOPE_P (decl)
19460           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19461         context_die = setup_namespace_context (decl, comp_unit_die ());
19462       /* Now create the namespace alias DIE.  */
19463       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19464       add_name_and_src_coords_attributes (namespace_die, decl);
19465       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19466       equate_decl_number_to_die (decl, namespace_die);
19467     }
19468 }
19469
19470 /* Generate Dwarf debug information for a decl described by DECL.
19471    The return value is currently only meaningful for PARM_DECLs,
19472    for all other decls it returns NULL.  */
19473
19474 static dw_die_ref
19475 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19476 {
19477   tree decl_or_origin = decl ? decl : origin;
19478   tree class_origin = NULL, ultimate_origin;
19479
19480   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19481     return NULL;
19482
19483   switch (TREE_CODE (decl_or_origin))
19484     {
19485     case ERROR_MARK:
19486       break;
19487
19488     case CONST_DECL:
19489       if (!is_fortran () && !is_ada ())
19490         {
19491           /* The individual enumerators of an enum type get output when we output
19492              the Dwarf representation of the relevant enum type itself.  */
19493           break;
19494         }
19495
19496       /* Emit its type.  */
19497       gen_type_die (TREE_TYPE (decl), context_die);
19498
19499       /* And its containing namespace.  */
19500       context_die = declare_in_namespace (decl, context_die);
19501
19502       gen_const_die (decl, context_die);
19503       break;
19504
19505     case FUNCTION_DECL:
19506       /* Don't output any DIEs to represent mere function declarations,
19507          unless they are class members or explicit block externs.  */
19508       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19509           && DECL_FILE_SCOPE_P (decl_or_origin)
19510           && (current_function_decl == NULL_TREE
19511               || DECL_ARTIFICIAL (decl_or_origin)))
19512         break;
19513
19514 #if 0
19515       /* FIXME */
19516       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19517          on local redeclarations of global functions.  That seems broken.  */
19518       if (current_function_decl != decl)
19519         /* This is only a declaration.  */;
19520 #endif
19521
19522       /* If we're emitting a clone, emit info for the abstract instance.  */
19523       if (origin || DECL_ORIGIN (decl) != decl)
19524         dwarf2out_abstract_function (origin
19525                                      ? DECL_ORIGIN (origin)
19526                                      : DECL_ABSTRACT_ORIGIN (decl));
19527
19528       /* If we're emitting an out-of-line copy of an inline function,
19529          emit info for the abstract instance and set up to refer to it.  */
19530       else if (cgraph_function_possibly_inlined_p (decl)
19531                && ! DECL_ABSTRACT (decl)
19532                && ! class_or_namespace_scope_p (context_die)
19533                /* dwarf2out_abstract_function won't emit a die if this is just
19534                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19535                   that case, because that works only if we have a die.  */
19536                && DECL_INITIAL (decl) != NULL_TREE)
19537         {
19538           dwarf2out_abstract_function (decl);
19539           set_decl_origin_self (decl);
19540         }
19541
19542       /* Otherwise we're emitting the primary DIE for this decl.  */
19543       else if (debug_info_level > DINFO_LEVEL_TERSE)
19544         {
19545           /* Before we describe the FUNCTION_DECL itself, make sure that we
19546              have its containing type.  */
19547           if (!origin)
19548             origin = decl_class_context (decl);
19549           if (origin != NULL_TREE)
19550             gen_type_die (origin, context_die);
19551
19552           /* And its return type.  */
19553           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19554
19555           /* And its virtual context.  */
19556           if (DECL_VINDEX (decl) != NULL_TREE)
19557             gen_type_die (DECL_CONTEXT (decl), context_die);
19558
19559           /* Make sure we have a member DIE for decl.  */
19560           if (origin != NULL_TREE)
19561             gen_type_die_for_member (origin, decl, context_die);
19562
19563           /* And its containing namespace.  */
19564           context_die = declare_in_namespace (decl, context_die);
19565         }
19566
19567       /* Now output a DIE to represent the function itself.  */
19568       if (decl)
19569         gen_subprogram_die (decl, context_die);
19570       break;
19571
19572     case TYPE_DECL:
19573       /* If we are in terse mode, don't generate any DIEs to represent any
19574          actual typedefs.  */
19575       if (debug_info_level <= DINFO_LEVEL_TERSE)
19576         break;
19577
19578       /* In the special case of a TYPE_DECL node representing the declaration
19579          of some type tag, if the given TYPE_DECL is marked as having been
19580          instantiated from some other (original) TYPE_DECL node (e.g. one which
19581          was generated within the original definition of an inline function) we
19582          used to generate a special (abbreviated) DW_TAG_structure_type,
19583          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19584          should be actually referencing those DIEs, as variable DIEs with that
19585          type would be emitted already in the abstract origin, so it was always
19586          removed during unused type prunning.  Don't add anything in this
19587          case.  */
19588       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19589         break;
19590
19591       if (is_redundant_typedef (decl))
19592         gen_type_die (TREE_TYPE (decl), context_die);
19593       else
19594         /* Output a DIE to represent the typedef itself.  */
19595         gen_typedef_die (decl, context_die);
19596       break;
19597
19598     case LABEL_DECL:
19599       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19600         gen_label_die (decl, context_die);
19601       break;
19602
19603     case VAR_DECL:
19604     case RESULT_DECL:
19605       /* If we are in terse mode, don't generate any DIEs to represent any
19606          variable declarations or definitions.  */
19607       if (debug_info_level <= DINFO_LEVEL_TERSE)
19608         break;
19609
19610       /* Output any DIEs that are needed to specify the type of this data
19611          object.  */
19612       if (decl_by_reference_p (decl_or_origin))
19613         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19614       else
19615         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19616
19617       /* And its containing type.  */
19618       class_origin = decl_class_context (decl_or_origin);
19619       if (class_origin != NULL_TREE)
19620         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19621
19622       /* And its containing namespace.  */
19623       context_die = declare_in_namespace (decl_or_origin, context_die);
19624
19625       /* Now output the DIE to represent the data object itself.  This gets
19626          complicated because of the possibility that the VAR_DECL really
19627          represents an inlined instance of a formal parameter for an inline
19628          function.  */
19629       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19630       if (ultimate_origin != NULL_TREE
19631           && TREE_CODE (ultimate_origin) == PARM_DECL)
19632         gen_formal_parameter_die (decl, origin,
19633                                   true /* Emit name attribute.  */,
19634                                   context_die);
19635       else
19636         gen_variable_die (decl, origin, context_die);
19637       break;
19638
19639     case FIELD_DECL:
19640       /* Ignore the nameless fields that are used to skip bits but handle C++
19641          anonymous unions and structs.  */
19642       if (DECL_NAME (decl) != NULL_TREE
19643           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19644           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19645         {
19646           gen_type_die (member_declared_type (decl), context_die);
19647           gen_field_die (decl, context_die);
19648         }
19649       break;
19650
19651     case PARM_DECL:
19652       if (DECL_BY_REFERENCE (decl_or_origin))
19653         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19654       else
19655         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19656       return gen_formal_parameter_die (decl, origin,
19657                                        true /* Emit name attribute.  */,
19658                                        context_die);
19659
19660     case NAMESPACE_DECL:
19661     case IMPORTED_DECL:
19662       if (dwarf_version >= 3 || !dwarf_strict)
19663         gen_namespace_die (decl, context_die);
19664       break;
19665
19666     default:
19667       /* Probably some frontend-internal decl.  Assume we don't care.  */
19668       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19669       break;
19670     }
19671
19672   return NULL;
19673 }
19674 \f
19675 /* Output debug information for global decl DECL.  Called from toplev.c after
19676    compilation proper has finished.  */
19677
19678 static void
19679 dwarf2out_global_decl (tree decl)
19680 {
19681   /* Output DWARF2 information for file-scope tentative data object
19682      declarations, file-scope (extern) function declarations (which
19683      had no corresponding body) and file-scope tagged type declarations
19684      and definitions which have not yet been forced out.  */
19685   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19686     dwarf2out_decl (decl);
19687 }
19688
19689 /* Output debug information for type decl DECL.  Called from toplev.c
19690    and from language front ends (to record built-in types).  */
19691 static void
19692 dwarf2out_type_decl (tree decl, int local)
19693 {
19694   if (!local)
19695     dwarf2out_decl (decl);
19696 }
19697
19698 /* Output debug information for imported module or decl DECL.
19699    NAME is non-NULL name in the lexical block if the decl has been renamed.
19700    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19701    that DECL belongs to.
19702    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19703 static void
19704 dwarf2out_imported_module_or_decl_1 (tree decl,
19705                                      tree name,
19706                                      tree lexical_block,
19707                                      dw_die_ref lexical_block_die)
19708 {
19709   expanded_location xloc;
19710   dw_die_ref imported_die = NULL;
19711   dw_die_ref at_import_die;
19712
19713   if (TREE_CODE (decl) == IMPORTED_DECL)
19714     {
19715       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19716       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19717       gcc_assert (decl);
19718     }
19719   else
19720     xloc = expand_location (input_location);
19721
19722   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19723     {
19724       at_import_die = force_type_die (TREE_TYPE (decl));
19725       /* For namespace N { typedef void T; } using N::T; base_type_die
19726          returns NULL, but DW_TAG_imported_declaration requires
19727          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19728       if (!at_import_die)
19729         {
19730           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19731           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19732           at_import_die = lookup_type_die (TREE_TYPE (decl));
19733           gcc_assert (at_import_die);
19734         }
19735     }
19736   else
19737     {
19738       at_import_die = lookup_decl_die (decl);
19739       if (!at_import_die)
19740         {
19741           /* If we're trying to avoid duplicate debug info, we may not have
19742              emitted the member decl for this field.  Emit it now.  */
19743           if (TREE_CODE (decl) == FIELD_DECL)
19744             {
19745               tree type = DECL_CONTEXT (decl);
19746
19747               if (TYPE_CONTEXT (type)
19748                   && TYPE_P (TYPE_CONTEXT (type))
19749                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19750                                                 DINFO_USAGE_DIR_USE))
19751                 return;
19752               gen_type_die_for_member (type, decl,
19753                                        get_context_die (TYPE_CONTEXT (type)));
19754             }
19755           at_import_die = force_decl_die (decl);
19756         }
19757     }
19758
19759   if (TREE_CODE (decl) == NAMESPACE_DECL)
19760     {
19761       if (dwarf_version >= 3 || !dwarf_strict)
19762         imported_die = new_die (DW_TAG_imported_module,
19763                                 lexical_block_die,
19764                                 lexical_block);
19765       else
19766         return;
19767     }
19768   else
19769     imported_die = new_die (DW_TAG_imported_declaration,
19770                             lexical_block_die,
19771                             lexical_block);
19772
19773   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19774   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19775   if (name)
19776     add_AT_string (imported_die, DW_AT_name,
19777                    IDENTIFIER_POINTER (name));
19778   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19779 }
19780
19781 /* Output debug information for imported module or decl DECL.
19782    NAME is non-NULL name in context if the decl has been renamed.
19783    CHILD is true if decl is one of the renamed decls as part of
19784    importing whole module.  */
19785
19786 static void
19787 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19788                                    bool child)
19789 {
19790   /* dw_die_ref at_import_die;  */
19791   dw_die_ref scope_die;
19792
19793   if (debug_info_level <= DINFO_LEVEL_TERSE)
19794     return;
19795
19796   gcc_assert (decl);
19797
19798   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19799      We need decl DIE for reference and scope die. First, get DIE for the decl
19800      itself.  */
19801
19802   /* Get the scope die for decl context. Use comp_unit_die for global module
19803      or decl. If die is not found for non globals, force new die.  */
19804   if (context
19805       && TYPE_P (context)
19806       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19807     return;
19808
19809   if (!(dwarf_version >= 3 || !dwarf_strict))
19810     return;
19811
19812   scope_die = get_context_die (context);
19813
19814   if (child)
19815     {
19816       gcc_assert (scope_die->die_child);
19817       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19818       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19819       scope_die = scope_die->die_child;
19820     }
19821
19822   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19823   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19824
19825 }
19826
19827 /* Write the debugging output for DECL.  */
19828
19829 void
19830 dwarf2out_decl (tree decl)
19831 {
19832   dw_die_ref context_die = comp_unit_die ();
19833
19834   switch (TREE_CODE (decl))
19835     {
19836     case ERROR_MARK:
19837       return;
19838
19839     case FUNCTION_DECL:
19840       /* What we would really like to do here is to filter out all mere
19841          file-scope declarations of file-scope functions which are never
19842          referenced later within this translation unit (and keep all of ones
19843          that *are* referenced later on) but we aren't clairvoyant, so we have
19844          no idea which functions will be referenced in the future (i.e. later
19845          on within the current translation unit). So here we just ignore all
19846          file-scope function declarations which are not also definitions.  If
19847          and when the debugger needs to know something about these functions,
19848          it will have to hunt around and find the DWARF information associated
19849          with the definition of the function.
19850
19851          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19852          nodes represent definitions and which ones represent mere
19853          declarations.  We have to check DECL_INITIAL instead. That's because
19854          the C front-end supports some weird semantics for "extern inline"
19855          function definitions.  These can get inlined within the current
19856          translation unit (and thus, we need to generate Dwarf info for their
19857          abstract instances so that the Dwarf info for the concrete inlined
19858          instances can have something to refer to) but the compiler never
19859          generates any out-of-lines instances of such things (despite the fact
19860          that they *are* definitions).
19861
19862          The important point is that the C front-end marks these "extern
19863          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19864          them anyway. Note that the C++ front-end also plays some similar games
19865          for inline function definitions appearing within include files which
19866          also contain `#pragma interface' pragmas.  */
19867       if (DECL_INITIAL (decl) == NULL_TREE)
19868         return;
19869
19870       /* If we're a nested function, initially use a parent of NULL; if we're
19871          a plain function, this will be fixed up in decls_for_scope.  If
19872          we're a method, it will be ignored, since we already have a DIE.  */
19873       if (decl_function_context (decl)
19874           /* But if we're in terse mode, we don't care about scope.  */
19875           && debug_info_level > DINFO_LEVEL_TERSE)
19876         context_die = NULL;
19877       break;
19878
19879     case VAR_DECL:
19880       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19881          declaration and if the declaration was never even referenced from
19882          within this entire compilation unit.  We suppress these DIEs in
19883          order to save space in the .debug section (by eliminating entries
19884          which are probably useless).  Note that we must not suppress
19885          block-local extern declarations (whether used or not) because that
19886          would screw-up the debugger's name lookup mechanism and cause it to
19887          miss things which really ought to be in scope at a given point.  */
19888       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19889         return;
19890
19891       /* For local statics lookup proper context die.  */
19892       if (TREE_STATIC (decl) && decl_function_context (decl))
19893         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19894
19895       /* If we are in terse mode, don't generate any DIEs to represent any
19896          variable declarations or definitions.  */
19897       if (debug_info_level <= DINFO_LEVEL_TERSE)
19898         return;
19899       break;
19900
19901     case CONST_DECL:
19902       if (debug_info_level <= DINFO_LEVEL_TERSE)
19903         return;
19904       if (!is_fortran () && !is_ada ())
19905         return;
19906       if (TREE_STATIC (decl) && decl_function_context (decl))
19907         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19908       break;
19909
19910     case NAMESPACE_DECL:
19911     case IMPORTED_DECL:
19912       if (debug_info_level <= DINFO_LEVEL_TERSE)
19913         return;
19914       if (lookup_decl_die (decl) != NULL)
19915         return;
19916       break;
19917
19918     case TYPE_DECL:
19919       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19920       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19921         return;
19922
19923       /* Don't bother trying to generate any DIEs to represent any of the
19924          normal built-in types for the language we are compiling.  */
19925       if (DECL_IS_BUILTIN (decl))
19926         return;
19927
19928       /* If we are in terse mode, don't generate any DIEs for types.  */
19929       if (debug_info_level <= DINFO_LEVEL_TERSE)
19930         return;
19931
19932       /* If we're a function-scope tag, initially use a parent of NULL;
19933          this will be fixed up in decls_for_scope.  */
19934       if (decl_function_context (decl))
19935         context_die = NULL;
19936
19937       break;
19938
19939     default:
19940       return;
19941     }
19942
19943   gen_decl_die (decl, NULL, context_die);
19944 }
19945
19946 /* Write the debugging output for DECL.  */
19947
19948 static void
19949 dwarf2out_function_decl (tree decl)
19950 {
19951   dwarf2out_decl (decl);
19952   call_arg_locations = NULL;
19953   call_arg_loc_last = NULL;
19954   call_site_count = -1;
19955   tail_call_site_count = -1;
19956   VEC_free (dw_die_ref, heap, block_map);
19957   htab_empty (decl_loc_table);
19958   htab_empty (cached_dw_loc_list_table);
19959 }
19960
19961 /* Output a marker (i.e. a label) for the beginning of the generated code for
19962    a lexical block.  */
19963
19964 static void
19965 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19966                        unsigned int blocknum)
19967 {
19968   switch_to_section (current_function_section ());
19969   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19970 }
19971
19972 /* Output a marker (i.e. a label) for the end of the generated code for a
19973    lexical block.  */
19974
19975 static void
19976 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19977 {
19978   switch_to_section (current_function_section ());
19979   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19980 }
19981
19982 /* Returns nonzero if it is appropriate not to emit any debugging
19983    information for BLOCK, because it doesn't contain any instructions.
19984
19985    Don't allow this for blocks with nested functions or local classes
19986    as we would end up with orphans, and in the presence of scheduling
19987    we may end up calling them anyway.  */
19988
19989 static bool
19990 dwarf2out_ignore_block (const_tree block)
19991 {
19992   tree decl;
19993   unsigned int i;
19994
19995   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19996     if (TREE_CODE (decl) == FUNCTION_DECL
19997         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19998       return 0;
19999   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20000     {
20001       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20002       if (TREE_CODE (decl) == FUNCTION_DECL
20003           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20004       return 0;
20005     }
20006
20007   return 1;
20008 }
20009
20010 /* Hash table routines for file_hash.  */
20011
20012 static int
20013 file_table_eq (const void *p1_p, const void *p2_p)
20014 {
20015   const struct dwarf_file_data *const p1 =
20016     (const struct dwarf_file_data *) p1_p;
20017   const char *const p2 = (const char *) p2_p;
20018   return filename_cmp (p1->filename, p2) == 0;
20019 }
20020
20021 static hashval_t
20022 file_table_hash (const void *p_p)
20023 {
20024   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20025   return htab_hash_string (p->filename);
20026 }
20027
20028 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20029    dwarf2out.c) and return its "index".  The index of each (known) filename is
20030    just a unique number which is associated with only that one filename.  We
20031    need such numbers for the sake of generating labels (in the .debug_sfnames
20032    section) and references to those files numbers (in the .debug_srcinfo
20033    and.debug_macinfo sections).  If the filename given as an argument is not
20034    found in our current list, add it to the list and assign it the next
20035    available unique index number.  In order to speed up searches, we remember
20036    the index of the filename was looked up last.  This handles the majority of
20037    all searches.  */
20038
20039 static struct dwarf_file_data *
20040 lookup_filename (const char *file_name)
20041 {
20042   void ** slot;
20043   struct dwarf_file_data * created;
20044
20045   /* Check to see if the file name that was searched on the previous
20046      call matches this file name.  If so, return the index.  */
20047   if (file_table_last_lookup
20048       && (file_name == file_table_last_lookup->filename
20049           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20050     return file_table_last_lookup;
20051
20052   /* Didn't match the previous lookup, search the table.  */
20053   slot = htab_find_slot_with_hash (file_table, file_name,
20054                                    htab_hash_string (file_name), INSERT);
20055   if (*slot)
20056     return (struct dwarf_file_data *) *slot;
20057
20058   created = ggc_alloc_dwarf_file_data ();
20059   created->filename = file_name;
20060   created->emitted_number = 0;
20061   *slot = created;
20062   return created;
20063 }
20064
20065 /* If the assembler will construct the file table, then translate the compiler
20066    internal file table number into the assembler file table number, and emit
20067    a .file directive if we haven't already emitted one yet.  The file table
20068    numbers are different because we prune debug info for unused variables and
20069    types, which may include filenames.  */
20070
20071 static int
20072 maybe_emit_file (struct dwarf_file_data * fd)
20073 {
20074   if (! fd->emitted_number)
20075     {
20076       if (last_emitted_file)
20077         fd->emitted_number = last_emitted_file->emitted_number + 1;
20078       else
20079         fd->emitted_number = 1;
20080       last_emitted_file = fd;
20081
20082       if (DWARF2_ASM_LINE_DEBUG_INFO)
20083         {
20084           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20085           output_quoted_string (asm_out_file,
20086                                 remap_debug_filename (fd->filename));
20087           fputc ('\n', asm_out_file);
20088         }
20089     }
20090
20091   return fd->emitted_number;
20092 }
20093
20094 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20095    That generation should happen after function debug info has been
20096    generated. The value of the attribute is the constant value of ARG.  */
20097
20098 static void
20099 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20100 {
20101   die_arg_entry entry;
20102
20103   if (!die || !arg)
20104     return;
20105
20106   if (!tmpl_value_parm_die_table)
20107     tmpl_value_parm_die_table
20108       = VEC_alloc (die_arg_entry, gc, 32);
20109
20110   entry.die = die;
20111   entry.arg = arg;
20112   VEC_safe_push (die_arg_entry, gc,
20113                  tmpl_value_parm_die_table,
20114                  &entry);
20115 }
20116
20117 /* Return TRUE if T is an instance of generic type, FALSE
20118    otherwise.  */
20119
20120 static bool
20121 generic_type_p (tree t)
20122 {
20123   if (t == NULL_TREE || !TYPE_P (t))
20124     return false;
20125   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20126 }
20127
20128 /* Schedule the generation of the generic parameter dies for the
20129   instance of generic type T. The proper generation itself is later
20130   done by gen_scheduled_generic_parms_dies. */
20131
20132 static void
20133 schedule_generic_params_dies_gen (tree t)
20134 {
20135   if (!generic_type_p (t))
20136     return;
20137
20138   if (generic_type_instances == NULL)
20139     generic_type_instances = VEC_alloc (tree, gc, 256);
20140
20141   VEC_safe_push (tree, gc, generic_type_instances, t);
20142 }
20143
20144 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20145    by append_entry_to_tmpl_value_parm_die_table. This function must
20146    be called after function DIEs have been generated.  */
20147
20148 static void
20149 gen_remaining_tmpl_value_param_die_attribute (void)
20150 {
20151   if (tmpl_value_parm_die_table)
20152     {
20153       unsigned i;
20154       die_arg_entry *e;
20155
20156       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20157         tree_add_const_value_attribute (e->die, e->arg);
20158     }
20159 }
20160
20161 /* Generate generic parameters DIEs for instances of generic types
20162    that have been previously scheduled by
20163    schedule_generic_params_dies_gen. This function must be called
20164    after all the types of the CU have been laid out.  */
20165
20166 static void
20167 gen_scheduled_generic_parms_dies (void)
20168 {
20169   unsigned i;
20170   tree t;
20171
20172   if (generic_type_instances == NULL)
20173     return;
20174   
20175   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20176     gen_generic_params_dies (t);
20177 }
20178
20179
20180 /* Replace DW_AT_name for the decl with name.  */
20181
20182 static void
20183 dwarf2out_set_name (tree decl, tree name)
20184 {
20185   dw_die_ref die;
20186   dw_attr_ref attr;
20187   const char *dname;
20188
20189   die = TYPE_SYMTAB_DIE (decl);
20190   if (!die)
20191     return;
20192
20193   dname = dwarf2_name (name, 0);
20194   if (!dname)
20195     return;
20196
20197   attr = get_AT (die, DW_AT_name);
20198   if (attr)
20199     {
20200       struct indirect_string_node *node;
20201
20202       node = find_AT_string (dname);
20203       /* replace the string.  */
20204       attr->dw_attr_val.v.val_str = node;
20205     }
20206
20207   else
20208     add_name_attribute (die, dname);
20209 }
20210
20211 /* Called by the final INSN scan whenever we see a var location.  We
20212    use it to drop labels in the right places, and throw the location in
20213    our lookup table.  */
20214
20215 static void
20216 dwarf2out_var_location (rtx loc_note)
20217 {
20218   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20219   struct var_loc_node *newloc;
20220   rtx next_real, next_note;
20221   static const char *last_label;
20222   static const char *last_postcall_label;
20223   static bool last_in_cold_section_p;
20224   static rtx expected_next_loc_note;
20225   tree decl;
20226   bool var_loc_p;
20227
20228   if (!NOTE_P (loc_note))
20229     {
20230       if (CALL_P (loc_note))
20231         {
20232           call_site_count++;
20233           if (SIBLING_CALL_P (loc_note))
20234             tail_call_site_count++;
20235         }
20236       return;
20237     }
20238
20239   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20240   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20241     return;
20242
20243   /* Optimize processing a large consecutive sequence of location
20244      notes so we don't spend too much time in next_real_insn.  If the
20245      next insn is another location note, remember the next_real_insn
20246      calculation for next time.  */
20247   next_real = cached_next_real_insn;
20248   if (next_real)
20249     {
20250       if (expected_next_loc_note != loc_note)
20251         next_real = NULL_RTX;
20252     }
20253
20254   next_note = NEXT_INSN (loc_note);
20255   if (! next_note
20256       || INSN_DELETED_P (next_note)
20257       || GET_CODE (next_note) != NOTE
20258       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20259           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20260     next_note = NULL_RTX;
20261
20262   if (! next_real)
20263     next_real = next_real_insn (loc_note);
20264
20265   if (next_note)
20266     {
20267       expected_next_loc_note = next_note;
20268       cached_next_real_insn = next_real;
20269     }
20270   else
20271     cached_next_real_insn = NULL_RTX;
20272
20273   /* If there are no instructions which would be affected by this note,
20274      don't do anything.  */
20275   if (var_loc_p
20276       && next_real == NULL_RTX
20277       && !NOTE_DURING_CALL_P (loc_note))
20278     return;
20279
20280   if (next_real == NULL_RTX)
20281     next_real = get_last_insn ();
20282
20283   /* If there were any real insns between note we processed last time
20284      and this note (or if it is the first note), clear
20285      last_{,postcall_}label so that they are not reused this time.  */
20286   if (last_var_location_insn == NULL_RTX
20287       || last_var_location_insn != next_real
20288       || last_in_cold_section_p != in_cold_section_p)
20289     {
20290       last_label = NULL;
20291       last_postcall_label = NULL;
20292     }
20293
20294   if (var_loc_p)
20295     {
20296       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20297       newloc = add_var_loc_to_decl (decl, loc_note,
20298                                     NOTE_DURING_CALL_P (loc_note)
20299                                     ? last_postcall_label : last_label);
20300       if (newloc == NULL)
20301         return;
20302     }
20303   else
20304     {
20305       decl = NULL_TREE;
20306       newloc = NULL;
20307     }
20308
20309   /* If there were no real insns between note we processed last time
20310      and this note, use the label we emitted last time.  Otherwise
20311      create a new label and emit it.  */
20312   if (last_label == NULL)
20313     {
20314       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20315       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20316       loclabel_num++;
20317       last_label = ggc_strdup (loclabel);
20318     }
20319
20320   if (!var_loc_p)
20321     {
20322       struct call_arg_loc_node *ca_loc
20323         = ggc_alloc_cleared_call_arg_loc_node ();
20324       rtx prev = prev_real_insn (loc_note), x;
20325       ca_loc->call_arg_loc_note = loc_note;
20326       ca_loc->next = NULL;
20327       ca_loc->label = last_label;
20328       gcc_assert (prev
20329                   && (CALL_P (prev)
20330                       || (NONJUMP_INSN_P (prev)
20331                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20332                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20333       if (!CALL_P (prev))
20334         prev = XVECEXP (PATTERN (prev), 0, 0);
20335       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20336       x = PATTERN (prev);
20337       if (GET_CODE (x) == PARALLEL)
20338         x = XVECEXP (x, 0, 0);
20339       if (GET_CODE (x) == SET)
20340         x = SET_SRC (x);
20341       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20342         {
20343           x = XEXP (XEXP (x, 0), 0);
20344           if (GET_CODE (x) == SYMBOL_REF
20345               && SYMBOL_REF_DECL (x)
20346               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20347             ca_loc->symbol_ref = x;
20348         }
20349       ca_loc->block = insn_scope (prev);
20350       if (call_arg_locations)
20351         call_arg_loc_last->next = ca_loc;
20352       else
20353         call_arg_locations = ca_loc;
20354       call_arg_loc_last = ca_loc;
20355     }
20356   else if (!NOTE_DURING_CALL_P (loc_note))
20357     newloc->label = last_label;
20358   else
20359     {
20360       if (!last_postcall_label)
20361         {
20362           sprintf (loclabel, "%s-1", last_label);
20363           last_postcall_label = ggc_strdup (loclabel);
20364         }
20365       newloc->label = last_postcall_label;
20366     }
20367
20368   last_var_location_insn = next_real;
20369   last_in_cold_section_p = in_cold_section_p;
20370 }
20371
20372 /* Note in one location list that text section has changed.  */
20373
20374 static int
20375 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20376 {
20377   var_loc_list *list = (var_loc_list *) *slot;
20378   if (list->first)
20379     list->last_before_switch
20380       = list->last->next ? list->last->next : list->last;
20381   return 1;
20382 }
20383
20384 /* Note in all location lists that text section has changed.  */
20385
20386 static void
20387 var_location_switch_text_section (void)
20388 {
20389   if (decl_loc_table == NULL)
20390     return;
20391
20392   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20393 }
20394
20395 /* Create a new line number table.  */
20396
20397 static dw_line_info_table *
20398 new_line_info_table (void)
20399 {
20400   dw_line_info_table *table;
20401
20402   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20403   table->file_num = 1;
20404   table->line_num = 1;
20405   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20406
20407   return table;
20408 }
20409
20410 /* Lookup the "current" table into which we emit line info, so
20411    that we don't have to do it for every source line.  */
20412
20413 static void
20414 set_cur_line_info_table (section *sec)
20415 {
20416   dw_line_info_table *table;
20417
20418   if (sec == text_section)
20419     table = text_section_line_info;
20420   else if (sec == cold_text_section)
20421     {
20422       table = cold_text_section_line_info;
20423       if (!table)
20424         {
20425           cold_text_section_line_info = table = new_line_info_table ();
20426           table->end_label = cold_end_label;
20427         }
20428     }
20429   else
20430     {
20431       const char *end_label;
20432
20433       if (flag_reorder_blocks_and_partition)
20434         {
20435           if (in_cold_section_p)
20436             end_label = crtl->subsections.cold_section_end_label;
20437           else
20438             end_label = crtl->subsections.hot_section_end_label;
20439         }
20440       else
20441         {
20442           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20443           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20444                                        current_function_funcdef_no);
20445           end_label = ggc_strdup (label);
20446         }
20447
20448       table = new_line_info_table ();
20449       table->end_label = end_label;
20450
20451       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20452     }
20453
20454   if (DWARF2_ASM_LINE_DEBUG_INFO)
20455     table->is_stmt = (cur_line_info_table
20456                       ? cur_line_info_table->is_stmt
20457                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20458   cur_line_info_table = table;
20459 }
20460
20461
20462 /* We need to reset the locations at the beginning of each
20463    function. We can't do this in the end_function hook, because the
20464    declarations that use the locations won't have been output when
20465    that hook is called.  Also compute have_multiple_function_sections here.  */
20466
20467 static void
20468 dwarf2out_begin_function (tree fun)
20469 {
20470   section *sec = function_section (fun);
20471
20472   if (sec != text_section)
20473     have_multiple_function_sections = true;
20474
20475   if (flag_reorder_blocks_and_partition && !cold_text_section)
20476     {
20477       gcc_assert (current_function_decl == fun);
20478       cold_text_section = unlikely_text_section ();
20479       switch_to_section (cold_text_section);
20480       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20481       switch_to_section (sec);
20482     }
20483
20484   dwarf2out_note_section_used ();
20485   call_site_count = 0;
20486   tail_call_site_count = 0;
20487
20488   set_cur_line_info_table (sec);
20489 }
20490
20491 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20492
20493 static void
20494 push_dw_line_info_entry (dw_line_info_table *table,
20495                          enum dw_line_info_opcode opcode, unsigned int val)
20496 {
20497   dw_line_info_entry e;
20498   e.opcode = opcode;
20499   e.val = val;
20500   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20501 }
20502
20503 /* Output a label to mark the beginning of a source code line entry
20504    and record information relating to this source line, in
20505    'line_info_table' for later output of the .debug_line section.  */
20506 /* ??? The discriminator parameter ought to be unsigned.  */
20507
20508 static void
20509 dwarf2out_source_line (unsigned int line, const char *filename,
20510                        int discriminator, bool is_stmt)
20511 {
20512   unsigned int file_num;
20513   dw_line_info_table *table;
20514
20515   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20516     return;
20517
20518   /* The discriminator column was added in dwarf4.  Simplify the below
20519      by simply removing it if we're not supposed to output it.  */
20520   if (dwarf_version < 4 && dwarf_strict)
20521     discriminator = 0;
20522
20523   table = cur_line_info_table;
20524   file_num = maybe_emit_file (lookup_filename (filename));
20525
20526   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20527      the debugger has used the second (possibly duplicate) line number
20528      at the beginning of the function to mark the end of the prologue.
20529      We could eliminate any other duplicates within the function.  For
20530      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20531      that second line number entry.  */
20532   /* Recall that this end-of-prologue indication is *not* the same thing
20533      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20534      to which the hook corresponds, follows the last insn that was 
20535      emitted by gen_prologue.  What we need is to preceed the first insn
20536      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20537      insn that corresponds to something the user wrote.  These may be
20538      very different locations once scheduling is enabled.  */
20539
20540   if (0 && file_num == table->file_num
20541       && line == table->line_num
20542       && discriminator == table->discrim_num
20543       && is_stmt == table->is_stmt)
20544     return;
20545
20546   switch_to_section (current_function_section ());
20547
20548   /* If requested, emit something human-readable.  */
20549   if (flag_debug_asm)
20550     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20551
20552   if (DWARF2_ASM_LINE_DEBUG_INFO)
20553     {
20554       /* Emit the .loc directive understood by GNU as.  */
20555       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20556          file_num, line, is_stmt, discriminator */
20557       fputs ("\t.loc ", asm_out_file);
20558       fprint_ul (asm_out_file, file_num);
20559       putc (' ', asm_out_file);
20560       fprint_ul (asm_out_file, line);
20561       putc (' ', asm_out_file);
20562       putc ('0', asm_out_file);
20563
20564       if (is_stmt != table->is_stmt)
20565         {
20566           fputs (" is_stmt ", asm_out_file);
20567           putc (is_stmt ? '1' : '0', asm_out_file);
20568         }
20569       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20570         {
20571           gcc_assert (discriminator > 0);
20572           fputs (" discriminator ", asm_out_file);
20573           fprint_ul (asm_out_file, (unsigned long) discriminator);
20574         }
20575       putc ('\n', asm_out_file);
20576     }
20577   else
20578     {
20579       unsigned int label_num = ++line_info_label_num;
20580
20581       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20582
20583       push_dw_line_info_entry (table, LI_set_address, label_num);
20584       if (file_num != table->file_num)
20585         push_dw_line_info_entry (table, LI_set_file, file_num);
20586       if (discriminator != table->discrim_num)
20587         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20588       if (is_stmt != table->is_stmt)
20589         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20590       push_dw_line_info_entry (table, LI_set_line, line);
20591     }
20592
20593   table->file_num = file_num;
20594   table->line_num = line;
20595   table->discrim_num = discriminator;
20596   table->is_stmt = is_stmt;
20597   table->in_use = true;
20598 }
20599
20600 /* Record the beginning of a new source file.  */
20601
20602 static void
20603 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20604 {
20605   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20606     {
20607       /* Record the beginning of the file for break_out_includes.  */
20608       dw_die_ref bincl_die;
20609
20610       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20611       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20612     }
20613
20614   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20615     {
20616       macinfo_entry e;
20617       e.code = DW_MACINFO_start_file;
20618       e.lineno = lineno;
20619       e.info = ggc_strdup (filename);
20620       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20621     }
20622 }
20623
20624 /* Record the end of a source file.  */
20625
20626 static void
20627 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20628 {
20629   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20630     /* Record the end of the file for break_out_includes.  */
20631     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20632
20633   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20634     {
20635       macinfo_entry e;
20636       e.code = DW_MACINFO_end_file;
20637       e.lineno = lineno;
20638       e.info = NULL;
20639       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20640     }
20641 }
20642
20643 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20644    the tail part of the directive line, i.e. the part which is past the
20645    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20646
20647 static void
20648 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20649                   const char *buffer ATTRIBUTE_UNUSED)
20650 {
20651   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20652     {
20653       macinfo_entry e;
20654       /* Insert a dummy first entry to be able to optimize the whole
20655          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20656       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20657         {
20658           e.code = 0;
20659           e.lineno = 0;
20660           e.info = NULL;
20661           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20662         }
20663       e.code = DW_MACINFO_define;
20664       e.lineno = lineno;
20665       e.info = ggc_strdup (buffer);
20666       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20667     }
20668 }
20669
20670 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20671    the tail part of the directive line, i.e. the part which is past the
20672    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20673
20674 static void
20675 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20676                  const char *buffer ATTRIBUTE_UNUSED)
20677 {
20678   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20679     {
20680       macinfo_entry e;
20681       /* Insert a dummy first entry to be able to optimize the whole
20682          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20683       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20684         {
20685           e.code = 0;
20686           e.lineno = 0;
20687           e.info = NULL;
20688           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20689         }
20690       e.code = DW_MACINFO_undef;
20691       e.lineno = lineno;
20692       e.info = ggc_strdup (buffer);
20693       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20694     }
20695 }
20696
20697 /* Routines to manipulate hash table of CUs.  */
20698
20699 static hashval_t
20700 htab_macinfo_hash (const void *of)
20701 {
20702   const macinfo_entry *const entry =
20703     (const macinfo_entry *) of;
20704
20705   return htab_hash_string (entry->info);
20706 }
20707
20708 static int
20709 htab_macinfo_eq (const void *of1, const void *of2)
20710 {
20711   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20712   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20713
20714   return !strcmp (entry1->info, entry2->info);
20715 }
20716
20717 /* Output a single .debug_macinfo entry.  */
20718
20719 static void
20720 output_macinfo_op (macinfo_entry *ref)
20721 {
20722   int file_num;
20723   size_t len;
20724   struct indirect_string_node *node;
20725   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20726   struct dwarf_file_data *fd;
20727
20728   switch (ref->code)
20729     {
20730     case DW_MACINFO_start_file:
20731       fd = lookup_filename (ref->info);
20732       file_num = maybe_emit_file (fd);
20733       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20734       dw2_asm_output_data_uleb128 (ref->lineno,
20735                                    "Included from line number %lu", 
20736                                    (unsigned long) ref->lineno);
20737       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20738       break;
20739     case DW_MACINFO_end_file:
20740       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20741       break;
20742     case DW_MACINFO_define:
20743     case DW_MACINFO_undef:
20744       len = strlen (ref->info) + 1;
20745       if (!dwarf_strict
20746           && len > DWARF_OFFSET_SIZE
20747           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20748           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20749         {
20750           ref->code = ref->code == DW_MACINFO_define
20751                       ? DW_MACRO_GNU_define_indirect
20752                       : DW_MACRO_GNU_undef_indirect;
20753           output_macinfo_op (ref);
20754           return;
20755         }
20756       dw2_asm_output_data (1, ref->code,
20757                            ref->code == DW_MACINFO_define
20758                            ? "Define macro" : "Undefine macro");
20759       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20760                                    (unsigned long) ref->lineno);
20761       dw2_asm_output_nstring (ref->info, -1, "The macro");
20762       break;
20763     case DW_MACRO_GNU_define_indirect:
20764     case DW_MACRO_GNU_undef_indirect:
20765       node = find_AT_string (ref->info);
20766       if (node->form != DW_FORM_strp)
20767         {
20768           char label[32];
20769           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20770           ++dw2_string_counter;
20771           node->label = xstrdup (label);
20772           node->form = DW_FORM_strp;
20773         }
20774       dw2_asm_output_data (1, ref->code,
20775                            ref->code == DW_MACRO_GNU_define_indirect
20776                            ? "Define macro indirect"
20777                            : "Undefine macro indirect");
20778       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20779                                    (unsigned long) ref->lineno);
20780       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20781                              debug_str_section, "The macro: \"%s\"",
20782                              ref->info);
20783       break;
20784     case DW_MACRO_GNU_transparent_include:
20785       dw2_asm_output_data (1, ref->code, "Transparent include");
20786       ASM_GENERATE_INTERNAL_LABEL (label,
20787                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20788       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20789       break;
20790     default:
20791       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20792                ASM_COMMENT_START, (unsigned long) ref->code);
20793       break;
20794     }
20795 }
20796
20797 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20798    other compilation unit .debug_macinfo sections.  IDX is the first
20799    index of a define/undef, return the number of ops that should be
20800    emitted in a comdat .debug_macinfo section and emit
20801    a DW_MACRO_GNU_transparent_include entry referencing it.
20802    If the define/undef entry should be emitted normally, return 0.  */
20803
20804 static unsigned
20805 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20806                         htab_t *macinfo_htab)
20807 {
20808   macinfo_entry *first, *second, *cur, *inc;
20809   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20810   unsigned char checksum[16];
20811   struct md5_ctx ctx;
20812   char *grp_name, *tail;
20813   const char *base;
20814   unsigned int i, count, encoded_filename_len, linebuf_len;
20815   void **slot;
20816
20817   first = VEC_index (macinfo_entry, macinfo_table, idx);
20818   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20819
20820   /* Optimize only if there are at least two consecutive define/undef ops,
20821      and either all of them are before first DW_MACINFO_start_file
20822      with lineno 0 (i.e. predefined macro block), or all of them are
20823      in some included header file.  */
20824   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20825     return 0;
20826   if (VEC_empty (macinfo_entry, files))
20827     {
20828       if (first->lineno != 0 || second->lineno != 0)
20829         return 0;
20830     }
20831   else if (first->lineno == 0)
20832     return 0;
20833
20834   /* Find the last define/undef entry that can be grouped together
20835      with first and at the same time compute md5 checksum of their
20836      codes, linenumbers and strings.  */
20837   md5_init_ctx (&ctx);
20838   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20839     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20840       break;
20841     else if (first->lineno == 0 && cur->lineno != 0)
20842       break;
20843     else
20844       {
20845         unsigned char code = cur->code;
20846         md5_process_bytes (&code, 1, &ctx);
20847         checksum_uleb128 (cur->lineno, &ctx);
20848         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20849       }
20850   md5_finish_ctx (&ctx, checksum);
20851   count = i - idx;
20852
20853   /* From the containing include filename (if any) pick up just
20854      usable characters from its basename.  */
20855   if (first->lineno == 0)
20856     base = "";
20857   else
20858     base = lbasename (VEC_last (macinfo_entry, files)->info);
20859   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20860     if (ISIDNUM (base[i]) || base[i] == '.')
20861       encoded_filename_len++;
20862   /* Count . at the end.  */
20863   if (encoded_filename_len)
20864     encoded_filename_len++;
20865
20866   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20867   linebuf_len = strlen (linebuf);
20868
20869   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20870   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20871                          + 16 * 2 + 1);
20872   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20873   tail = grp_name + 4;
20874   if (encoded_filename_len)
20875     {
20876       for (i = 0; base[i]; i++)
20877         if (ISIDNUM (base[i]) || base[i] == '.')
20878           *tail++ = base[i];
20879       *tail++ = '.';
20880     }
20881   memcpy (tail, linebuf, linebuf_len);
20882   tail += linebuf_len;
20883   *tail++ = '.';
20884   for (i = 0; i < 16; i++)
20885     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20886
20887   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20888      in the empty vector entry before the first define/undef.  */
20889   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20890   inc->code = DW_MACRO_GNU_transparent_include;
20891   inc->lineno = 0;
20892   inc->info = ggc_strdup (grp_name);
20893   if (*macinfo_htab == NULL)
20894     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20895   /* Avoid emitting duplicates.  */
20896   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20897   if (*slot != NULL)
20898     {
20899       inc->code = 0;
20900       inc->info = NULL;
20901       /* If such an entry has been used before, just emit
20902          a DW_MACRO_GNU_transparent_include op.  */
20903       inc = (macinfo_entry *) *slot;
20904       output_macinfo_op (inc);
20905       /* And clear all macinfo_entry in the range to avoid emitting them
20906          in the second pass.  */
20907       for (i = idx;
20908            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20909            && i < idx + count;
20910            i++)
20911         {
20912           cur->code = 0;
20913           cur->info = NULL;
20914         }
20915     }
20916   else
20917     {
20918       *slot = inc;
20919       inc->lineno = htab_elements (*macinfo_htab);
20920       output_macinfo_op (inc);
20921     }
20922   return count;
20923 }
20924
20925 /* Output macinfo section(s).  */
20926
20927 static void
20928 output_macinfo (void)
20929 {
20930   unsigned i;
20931   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20932   macinfo_entry *ref;
20933   VEC (macinfo_entry, gc) *files = NULL;
20934   htab_t macinfo_htab = NULL;
20935
20936   if (! length)
20937     return;
20938
20939   /* output_macinfo* uses these interchangeably.  */
20940   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20941               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20942               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20943               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20944
20945   /* For .debug_macro emit the section header.  */
20946   if (!dwarf_strict)
20947     {
20948       dw2_asm_output_data (2, 4, "DWARF macro version number");
20949       if (DWARF_OFFSET_SIZE == 8)
20950         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20951       else
20952         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20953       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20954                              debug_line_section, NULL);
20955     }
20956
20957   /* In the first loop, it emits the primary .debug_macinfo section
20958      and after each emitted op the macinfo_entry is cleared.
20959      If a longer range of define/undef ops can be optimized using
20960      DW_MACRO_GNU_transparent_include, the
20961      DW_MACRO_GNU_transparent_include op is emitted and kept in
20962      the vector before the first define/undef in the range and the
20963      whole range of define/undef ops is not emitted and kept.  */
20964   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20965     {
20966       switch (ref->code)
20967         {
20968         case DW_MACINFO_start_file:
20969           VEC_safe_push (macinfo_entry, gc, files, ref);
20970           break;
20971         case DW_MACINFO_end_file:
20972           if (!VEC_empty (macinfo_entry, files))
20973             VEC_pop (macinfo_entry, files);
20974           break;
20975         case DW_MACINFO_define:
20976         case DW_MACINFO_undef:
20977           if (!dwarf_strict
20978               && HAVE_COMDAT_GROUP
20979               && VEC_length (macinfo_entry, files) != 1
20980               && i > 0
20981               && i + 1 < length
20982               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20983             {
20984               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20985               if (count)
20986                 {
20987                   i += count - 1;
20988                   continue;
20989                 }
20990             }
20991           break;
20992         case 0:
20993           /* A dummy entry may be inserted at the beginning to be able
20994              to optimize the whole block of predefined macros.  */
20995           if (i == 0)
20996             continue;
20997         default:
20998           break;
20999         }
21000       output_macinfo_op (ref);
21001       ref->info = NULL;
21002       ref->code = 0;
21003     }
21004
21005   if (macinfo_htab == NULL)
21006     return;
21007
21008   htab_delete (macinfo_htab);
21009
21010   /* If any DW_MACRO_GNU_transparent_include were used, on those
21011      DW_MACRO_GNU_transparent_include entries terminate the
21012      current chain and switch to a new comdat .debug_macinfo
21013      section and emit the define/undef entries within it.  */
21014   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21015     switch (ref->code)
21016       {
21017       case 0:
21018         continue;
21019       case DW_MACRO_GNU_transparent_include:
21020         {
21021           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21022           tree comdat_key = get_identifier (ref->info);
21023           /* Terminate the previous .debug_macinfo section.  */
21024           dw2_asm_output_data (1, 0, "End compilation unit");
21025           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21026                                          SECTION_DEBUG
21027                                          | SECTION_LINKONCE,
21028                                          comdat_key);
21029           ASM_GENERATE_INTERNAL_LABEL (label,
21030                                        DEBUG_MACRO_SECTION_LABEL,
21031                                        ref->lineno);
21032           ASM_OUTPUT_LABEL (asm_out_file, label);
21033           ref->code = 0;
21034           ref->info = NULL;
21035           dw2_asm_output_data (2, 4, "DWARF macro version number");
21036           if (DWARF_OFFSET_SIZE == 8)
21037             dw2_asm_output_data (1, 1, "Flags: 64-bit");
21038           else
21039             dw2_asm_output_data (1, 0, "Flags: 32-bit");
21040         }
21041         break;
21042       case DW_MACINFO_define:
21043       case DW_MACINFO_undef:
21044         output_macinfo_op (ref);
21045         ref->code = 0;
21046         ref->info = NULL;
21047         break;
21048       default:
21049         gcc_unreachable ();
21050       }
21051 }
21052
21053 /* Set up for Dwarf output at the start of compilation.  */
21054
21055 static void
21056 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21057 {
21058   /* Allocate the file_table.  */
21059   file_table = htab_create_ggc (50, file_table_hash,
21060                                 file_table_eq, NULL);
21061
21062   /* Allocate the decl_die_table.  */
21063   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21064                                     decl_die_table_eq, NULL);
21065
21066   /* Allocate the decl_loc_table.  */
21067   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21068                                     decl_loc_table_eq, NULL);
21069
21070   /* Allocate the cached_dw_loc_list_table.  */
21071   cached_dw_loc_list_table
21072     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21073                        cached_dw_loc_list_table_eq, NULL);
21074
21075   /* Allocate the initial hunk of the decl_scope_table.  */
21076   decl_scope_table = VEC_alloc (tree, gc, 256);
21077
21078   /* Allocate the initial hunk of the abbrev_die_table.  */
21079   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21080     (ABBREV_DIE_TABLE_INCREMENT);
21081   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21082   /* Zero-th entry is allocated, but unused.  */
21083   abbrev_die_table_in_use = 1;
21084
21085   /* Allocate the pubtypes and pubnames vectors.  */
21086   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21087   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21088
21089   incomplete_types = VEC_alloc (tree, gc, 64);
21090
21091   used_rtx_array = VEC_alloc (rtx, gc, 32);
21092
21093   debug_info_section = get_section (DEBUG_INFO_SECTION,
21094                                     SECTION_DEBUG, NULL);
21095   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21096                                       SECTION_DEBUG, NULL);
21097   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21098                                        SECTION_DEBUG, NULL);
21099   debug_macinfo_section = get_section (dwarf_strict
21100                                        ? DEBUG_MACINFO_SECTION
21101                                        : DEBUG_MACRO_SECTION,
21102                                        SECTION_DEBUG, NULL);
21103   debug_line_section = get_section (DEBUG_LINE_SECTION,
21104                                     SECTION_DEBUG, NULL);
21105   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21106                                    SECTION_DEBUG, NULL);
21107   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21108                                         SECTION_DEBUG, NULL);
21109   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21110                                         SECTION_DEBUG, NULL);
21111   debug_str_section = get_section (DEBUG_STR_SECTION,
21112                                    DEBUG_STR_SECTION_FLAGS, NULL);
21113   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21114                                       SECTION_DEBUG, NULL);
21115   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21116                                      SECTION_DEBUG, NULL);
21117
21118   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21119   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21120                                DEBUG_ABBREV_SECTION_LABEL, 0);
21121   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21122   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21123                                COLD_TEXT_SECTION_LABEL, 0);
21124   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21125
21126   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21127                                DEBUG_INFO_SECTION_LABEL, 0);
21128   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21129                                DEBUG_LINE_SECTION_LABEL, 0);
21130   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21131                                DEBUG_RANGES_SECTION_LABEL, 0);
21132   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21133                                dwarf_strict
21134                                ? DEBUG_MACINFO_SECTION_LABEL
21135                                : DEBUG_MACRO_SECTION_LABEL, 0);
21136
21137   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21138     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21139
21140   switch_to_section (text_section);
21141   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21142
21143   /* Make sure the line number table for .text always exists.  */
21144   text_section_line_info = new_line_info_table ();
21145   text_section_line_info->end_label = text_end_label;
21146 }
21147
21148 /* Called before cgraph_optimize starts outputtting functions, variables
21149    and toplevel asms into assembly.  */
21150
21151 static void
21152 dwarf2out_assembly_start (void)
21153 {
21154   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21155       && dwarf2out_do_cfi_asm ()
21156       && (!(flag_unwind_tables || flag_exceptions)
21157           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21158     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21159 }
21160
21161 /* A helper function for dwarf2out_finish called through
21162    htab_traverse.  Emit one queued .debug_str string.  */
21163
21164 static int
21165 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21166 {
21167   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21168
21169   if (node->form == DW_FORM_strp)
21170     {
21171       switch_to_section (debug_str_section);
21172       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21173       assemble_string (node->str, strlen (node->str) + 1);
21174     }
21175
21176   return 1;
21177 }
21178
21179 #if ENABLE_ASSERT_CHECKING
21180 /* Verify that all marks are clear.  */
21181
21182 static void
21183 verify_marks_clear (dw_die_ref die)
21184 {
21185   dw_die_ref c;
21186
21187   gcc_assert (! die->die_mark);
21188   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21189 }
21190 #endif /* ENABLE_ASSERT_CHECKING */
21191
21192 /* Clear the marks for a die and its children.
21193    Be cool if the mark isn't set.  */
21194
21195 static void
21196 prune_unmark_dies (dw_die_ref die)
21197 {
21198   dw_die_ref c;
21199
21200   if (die->die_mark)
21201     die->die_mark = 0;
21202   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21203 }
21204
21205 /* Given DIE that we're marking as used, find any other dies
21206    it references as attributes and mark them as used.  */
21207
21208 static void
21209 prune_unused_types_walk_attribs (dw_die_ref die)
21210 {
21211   dw_attr_ref a;
21212   unsigned ix;
21213
21214   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21215     {
21216       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21217         {
21218           /* A reference to another DIE.
21219              Make sure that it will get emitted.
21220              If it was broken out into a comdat group, don't follow it.  */
21221           if (! use_debug_types
21222               || a->dw_attr == DW_AT_specification
21223               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21224             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21225         }
21226       /* Set the string's refcount to 0 so that prune_unused_types_mark
21227          accounts properly for it.  */
21228       if (AT_class (a) == dw_val_class_str)
21229         a->dw_attr_val.v.val_str->refcount = 0;
21230     }
21231 }
21232
21233 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21234
21235 static void
21236 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21237 {
21238   dw_die_ref c;
21239
21240   if (die == NULL || die->die_child == NULL)
21241     return;
21242   c = die->die_child;
21243   do
21244     {
21245       switch (c->die_tag)
21246         {
21247         case DW_TAG_template_type_param:
21248         case DW_TAG_template_value_param:
21249         case DW_TAG_GNU_template_template_param:
21250         case DW_TAG_GNU_template_parameter_pack:
21251           prune_unused_types_mark (c, 1);
21252           break;
21253         default:
21254           break;
21255         }
21256       c = c->die_sib;
21257     } while (c && c != die->die_child);
21258 }
21259
21260 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21261    to DIE's children.  */
21262
21263 static void
21264 prune_unused_types_mark (dw_die_ref die, int dokids)
21265 {
21266   dw_die_ref c;
21267
21268   if (die->die_mark == 0)
21269     {
21270       /* We haven't done this node yet.  Mark it as used.  */
21271       die->die_mark = 1;
21272       /* If this is the DIE of a generic type instantiation,
21273          mark the children DIEs that describe its generic parms and
21274          args.  */
21275       prune_unused_types_mark_generic_parms_dies (die);
21276
21277       /* We also have to mark its parents as used.
21278          (But we don't want to mark our parents' kids due to this.)  */
21279       if (die->die_parent)
21280         prune_unused_types_mark (die->die_parent, 0);
21281
21282       /* Mark any referenced nodes.  */
21283       prune_unused_types_walk_attribs (die);
21284
21285       /* If this node is a specification,
21286          also mark the definition, if it exists.  */
21287       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21288         prune_unused_types_mark (die->die_definition, 1);
21289     }
21290
21291   if (dokids && die->die_mark != 2)
21292     {
21293       /* We need to walk the children, but haven't done so yet.
21294          Remember that we've walked the kids.  */
21295       die->die_mark = 2;
21296
21297       /* If this is an array type, we need to make sure our
21298          kids get marked, even if they're types.  If we're
21299          breaking out types into comdat sections, do this
21300          for all type definitions.  */
21301       if (die->die_tag == DW_TAG_array_type
21302           || (use_debug_types
21303               && is_type_die (die) && ! is_declaration_die (die)))
21304         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21305       else
21306         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21307     }
21308 }
21309
21310 /* For local classes, look if any static member functions were emitted
21311    and if so, mark them.  */
21312
21313 static void
21314 prune_unused_types_walk_local_classes (dw_die_ref die)
21315 {
21316   dw_die_ref c;
21317
21318   if (die->die_mark == 2)
21319     return;
21320
21321   switch (die->die_tag)
21322     {
21323     case DW_TAG_structure_type:
21324     case DW_TAG_union_type:
21325     case DW_TAG_class_type:
21326       break;
21327
21328     case DW_TAG_subprogram:
21329       if (!get_AT_flag (die, DW_AT_declaration)
21330           || die->die_definition != NULL)
21331         prune_unused_types_mark (die, 1);
21332       return;
21333
21334     default:
21335       return;
21336     }
21337
21338   /* Mark children.  */
21339   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21340 }
21341
21342 /* Walk the tree DIE and mark types that we actually use.  */
21343
21344 static void
21345 prune_unused_types_walk (dw_die_ref die)
21346 {
21347   dw_die_ref c;
21348
21349   /* Don't do anything if this node is already marked and
21350      children have been marked as well.  */
21351   if (die->die_mark == 2)
21352     return;
21353
21354   switch (die->die_tag)
21355     {
21356     case DW_TAG_structure_type:
21357     case DW_TAG_union_type:
21358     case DW_TAG_class_type:
21359       if (die->die_perennial_p)
21360         break;
21361
21362       for (c = die->die_parent; c; c = c->die_parent)
21363         if (c->die_tag == DW_TAG_subprogram)
21364           break;
21365
21366       /* Finding used static member functions inside of classes
21367          is needed just for local classes, because for other classes
21368          static member function DIEs with DW_AT_specification
21369          are emitted outside of the DW_TAG_*_type.  If we ever change
21370          it, we'd need to call this even for non-local classes.  */
21371       if (c)
21372         prune_unused_types_walk_local_classes (die);
21373
21374       /* It's a type node --- don't mark it.  */
21375       return;
21376
21377     case DW_TAG_const_type:
21378     case DW_TAG_packed_type:
21379     case DW_TAG_pointer_type:
21380     case DW_TAG_reference_type:
21381     case DW_TAG_rvalue_reference_type:
21382     case DW_TAG_volatile_type:
21383     case DW_TAG_typedef:
21384     case DW_TAG_array_type:
21385     case DW_TAG_interface_type:
21386     case DW_TAG_friend:
21387     case DW_TAG_variant_part:
21388     case DW_TAG_enumeration_type:
21389     case DW_TAG_subroutine_type:
21390     case DW_TAG_string_type:
21391     case DW_TAG_set_type:
21392     case DW_TAG_subrange_type:
21393     case DW_TAG_ptr_to_member_type:
21394     case DW_TAG_file_type:
21395       if (die->die_perennial_p)
21396         break;
21397
21398       /* It's a type node --- don't mark it.  */
21399       return;
21400
21401     default:
21402       /* Mark everything else.  */
21403       break;
21404   }
21405
21406   if (die->die_mark == 0)
21407     {
21408       die->die_mark = 1;
21409
21410       /* Now, mark any dies referenced from here.  */
21411       prune_unused_types_walk_attribs (die);
21412     }
21413
21414   die->die_mark = 2;
21415
21416   /* Mark children.  */
21417   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21418 }
21419
21420 /* Increment the string counts on strings referred to from DIE's
21421    attributes.  */
21422
21423 static void
21424 prune_unused_types_update_strings (dw_die_ref die)
21425 {
21426   dw_attr_ref a;
21427   unsigned ix;
21428
21429   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21430     if (AT_class (a) == dw_val_class_str)
21431       {
21432         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21433         s->refcount++;
21434         /* Avoid unnecessarily putting strings that are used less than
21435            twice in the hash table.  */
21436         if (s->refcount
21437             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21438           {
21439             void ** slot;
21440             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21441                                              htab_hash_string (s->str),
21442                                              INSERT);
21443             gcc_assert (*slot == NULL);
21444             *slot = s;
21445           }
21446       }
21447 }
21448
21449 /* Remove from the tree DIE any dies that aren't marked.  */
21450
21451 static void
21452 prune_unused_types_prune (dw_die_ref die)
21453 {
21454   dw_die_ref c;
21455
21456   gcc_assert (die->die_mark);
21457   prune_unused_types_update_strings (die);
21458
21459   if (! die->die_child)
21460     return;
21461
21462   c = die->die_child;
21463   do {
21464     dw_die_ref prev = c;
21465     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21466       if (c == die->die_child)
21467         {
21468           /* No marked children between 'prev' and the end of the list.  */
21469           if (prev == c)
21470             /* No marked children at all.  */
21471             die->die_child = NULL;
21472           else
21473             {
21474               prev->die_sib = c->die_sib;
21475               die->die_child = prev;
21476             }
21477           return;
21478         }
21479
21480     if (c != prev->die_sib)
21481       prev->die_sib = c;
21482     prune_unused_types_prune (c);
21483   } while (c != die->die_child);
21484 }
21485
21486 /* Remove dies representing declarations that we never use.  */
21487
21488 static void
21489 prune_unused_types (void)
21490 {
21491   unsigned int i;
21492   limbo_die_node *node;
21493   comdat_type_node *ctnode;
21494   pubname_ref pub;
21495   dw_die_ref base_type;
21496
21497 #if ENABLE_ASSERT_CHECKING
21498   /* All the marks should already be clear.  */
21499   verify_marks_clear (comp_unit_die ());
21500   for (node = limbo_die_list; node; node = node->next)
21501     verify_marks_clear (node->die);
21502   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21503     verify_marks_clear (ctnode->root_die);
21504 #endif /* ENABLE_ASSERT_CHECKING */
21505
21506   /* Mark types that are used in global variables.  */
21507   premark_types_used_by_global_vars ();
21508
21509   /* Set the mark on nodes that are actually used.  */
21510   prune_unused_types_walk (comp_unit_die ());
21511   for (node = limbo_die_list; node; node = node->next)
21512     prune_unused_types_walk (node->die);
21513   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21514     {
21515       prune_unused_types_walk (ctnode->root_die);
21516       prune_unused_types_mark (ctnode->type_die, 1);
21517     }
21518
21519   /* Also set the mark on nodes referenced from the
21520      pubname_table.  */
21521   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21522     prune_unused_types_mark (pub->die, 1);
21523   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21524     prune_unused_types_mark (base_type, 1);
21525
21526   if (debug_str_hash)
21527     htab_empty (debug_str_hash);
21528   prune_unused_types_prune (comp_unit_die ());
21529   for (node = limbo_die_list; node; node = node->next)
21530     prune_unused_types_prune (node->die);
21531   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21532     prune_unused_types_prune (ctnode->root_die);
21533
21534   /* Leave the marks clear.  */
21535   prune_unmark_dies (comp_unit_die ());
21536   for (node = limbo_die_list; node; node = node->next)
21537     prune_unmark_dies (node->die);
21538   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21539     prune_unmark_dies (ctnode->root_die);
21540 }
21541
21542 /* Set the parameter to true if there are any relative pathnames in
21543    the file table.  */
21544 static int
21545 file_table_relative_p (void ** slot, void *param)
21546 {
21547   bool *p = (bool *) param;
21548   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21549   if (!IS_ABSOLUTE_PATH (d->filename))
21550     {
21551       *p = true;
21552       return 0;
21553     }
21554   return 1;
21555 }
21556
21557 /* Routines to manipulate hash table of comdat type units.  */
21558
21559 static hashval_t
21560 htab_ct_hash (const void *of)
21561 {
21562   hashval_t h;
21563   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21564
21565   memcpy (&h, type_node->signature, sizeof (h));
21566   return h;
21567 }
21568
21569 static int
21570 htab_ct_eq (const void *of1, const void *of2)
21571 {
21572   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21573   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21574
21575   return (! memcmp (type_node_1->signature, type_node_2->signature,
21576                     DWARF_TYPE_SIGNATURE_SIZE));
21577 }
21578
21579 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21580    to the location it would have been added, should we know its
21581    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21582    probably improve compactness of debug info, removing equivalent
21583    abbrevs, and hide any differences caused by deferring the
21584    computation of the assembler name, triggered by e.g. PCH.  */
21585
21586 static inline void
21587 move_linkage_attr (dw_die_ref die)
21588 {
21589   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21590   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21591
21592   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21593               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21594
21595   while (--ix > 0)
21596     {
21597       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21598
21599       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21600         break;
21601     }
21602
21603   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21604     {
21605       VEC_pop (dw_attr_node, die->die_attr);
21606       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21607     }
21608 }
21609
21610 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21611    referenced from typed stack ops and count how often they are used.  */
21612
21613 static void
21614 mark_base_types (dw_loc_descr_ref loc)
21615 {
21616   dw_die_ref base_type = NULL;
21617
21618   for (; loc; loc = loc->dw_loc_next)
21619     {
21620       switch (loc->dw_loc_opc)
21621         {
21622         case DW_OP_GNU_regval_type:
21623         case DW_OP_GNU_deref_type:
21624           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21625           break;
21626         case DW_OP_GNU_convert:
21627         case DW_OP_GNU_reinterpret:
21628           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21629             continue;
21630           /* FALLTHRU */
21631         case DW_OP_GNU_const_type:
21632           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21633           break;
21634         case DW_OP_GNU_entry_value:
21635           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21636           continue;
21637         default:
21638           continue;
21639         }
21640       gcc_assert (base_type->die_parent == comp_unit_die ());
21641       if (base_type->die_mark)
21642         base_type->die_mark++;
21643       else
21644         {
21645           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21646           base_type->die_mark = 1;
21647         }
21648     }
21649 }
21650
21651 /* Comparison function for sorting marked base types.  */
21652
21653 static int
21654 base_type_cmp (const void *x, const void *y)
21655 {
21656   dw_die_ref dx = *(const dw_die_ref *) x;
21657   dw_die_ref dy = *(const dw_die_ref *) y;
21658   unsigned int byte_size1, byte_size2;
21659   unsigned int encoding1, encoding2;
21660   if (dx->die_mark > dy->die_mark)
21661     return -1;
21662   if (dx->die_mark < dy->die_mark)
21663     return 1;
21664   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21665   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21666   if (byte_size1 < byte_size2)
21667     return 1;
21668   if (byte_size1 > byte_size2)
21669     return -1;
21670   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21671   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21672   if (encoding1 < encoding2)
21673     return 1;
21674   if (encoding1 > encoding2)
21675     return -1;
21676   return 0;
21677 }
21678
21679 /* Move base types marked by mark_base_types as early as possible
21680    in the CU, sorted by decreasing usage count both to make the
21681    uleb128 references as small as possible and to make sure they
21682    will have die_offset already computed by calc_die_sizes when
21683    sizes of typed stack loc ops is computed.  */
21684
21685 static void
21686 move_marked_base_types (void)
21687 {
21688   unsigned int i;
21689   dw_die_ref base_type, die, c;
21690
21691   if (VEC_empty (dw_die_ref, base_types))
21692     return;
21693
21694   /* Sort by decreasing usage count, they will be added again in that
21695      order later on.  */
21696   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21697   die = comp_unit_die ();
21698   c = die->die_child;
21699   do
21700     {
21701       dw_die_ref prev = c;
21702       c = c->die_sib;
21703       while (c->die_mark)
21704         {
21705           remove_child_with_prev (c, prev);
21706           /* As base types got marked, there must be at least
21707              one node other than DW_TAG_base_type.  */
21708           gcc_assert (c != c->die_sib);
21709           c = c->die_sib;
21710         }
21711     }
21712   while (c != die->die_child);
21713   gcc_assert (die->die_child);
21714   c = die->die_child;
21715   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21716     {
21717       base_type->die_mark = 0;
21718       base_type->die_sib = c->die_sib;
21719       c->die_sib = base_type;
21720       c = base_type;
21721     }
21722 }
21723
21724 /* Helper function for resolve_addr, attempt to resolve
21725    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21726    SYMBOL_REFs refer to variables emitted in the current CU.  */
21727
21728 static int
21729 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21730 {
21731   rtx rtl = *addr;
21732
21733   if (GET_CODE (rtl) == CONST_STRING)
21734     {
21735       size_t len = strlen (XSTR (rtl, 0)) + 1;
21736       tree t = build_string (len, XSTR (rtl, 0));
21737       tree tlen = size_int (len - 1);
21738       TREE_TYPE (t)
21739         = build_array_type (char_type_node, build_index_type (tlen));
21740       rtl = lookup_constant_def (t);
21741       if (!rtl || !MEM_P (rtl))
21742         return 1;
21743       rtl = XEXP (rtl, 0);
21744       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21745       *addr = rtl;
21746       return 0;
21747     }
21748
21749   if (GET_CODE (rtl) == SYMBOL_REF
21750       && SYMBOL_REF_DECL (rtl))
21751     {
21752       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21753         {
21754           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21755             return 1;
21756         }
21757       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21758         return 1;
21759     }
21760
21761   if (GET_CODE (rtl) == CONST
21762       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21763     return 1;
21764
21765   return 0;
21766 }
21767
21768 /* Helper function for resolve_addr, handle one location
21769    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21770    the location list couldn't be resolved.  */
21771
21772 static bool
21773 resolve_addr_in_expr (dw_loc_descr_ref loc)
21774 {
21775   dw_loc_descr_ref keep = NULL;
21776   for (; loc; loc = loc->dw_loc_next)
21777     switch (loc->dw_loc_opc)
21778       {
21779       case DW_OP_addr:
21780         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21781           return false;
21782         break;
21783       case DW_OP_const4u:
21784       case DW_OP_const8u:
21785         if (loc->dtprel
21786             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21787           return false;
21788         break;
21789       case DW_OP_plus_uconst:
21790         if (size_of_loc_descr (loc)
21791             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21792               + 1
21793             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21794           {
21795             dw_loc_descr_ref repl
21796               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21797             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21798             add_loc_descr (&repl, loc->dw_loc_next);
21799             *loc = *repl;
21800           }
21801         break;
21802       case DW_OP_implicit_value:
21803         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21804             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21805           return false;
21806         break;
21807       case DW_OP_GNU_implicit_pointer:
21808       case DW_OP_GNU_parameter_ref:
21809         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21810           {
21811             dw_die_ref ref
21812               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21813             if (ref == NULL)
21814               return false;
21815             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21816             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21817             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21818           }
21819         break;
21820       case DW_OP_GNU_const_type:
21821       case DW_OP_GNU_regval_type:
21822       case DW_OP_GNU_deref_type:
21823       case DW_OP_GNU_convert:
21824       case DW_OP_GNU_reinterpret:
21825         while (loc->dw_loc_next
21826                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21827           {
21828             dw_die_ref base1, base2;
21829             unsigned enc1, enc2, size1, size2;
21830             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21831                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21832               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21833             else if (loc->dw_loc_oprnd1.val_class
21834                      == dw_val_class_unsigned_const)
21835               break;
21836             else
21837               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21838             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21839                 == dw_val_class_unsigned_const)
21840               break;
21841             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21842             gcc_assert (base1->die_tag == DW_TAG_base_type
21843                         && base2->die_tag == DW_TAG_base_type);
21844             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21845             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21846             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21847             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21848             if (size1 == size2
21849                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21850                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21851                      && loc != keep)
21852                     || enc1 == enc2))
21853               {
21854                 /* Optimize away next DW_OP_GNU_convert after
21855                    adjusting LOC's base type die reference.  */
21856                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21857                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21858                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21859                 else
21860                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21861                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21862                 continue;
21863               }
21864             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21865                point typed stack entry.  */
21866             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21867               keep = loc->dw_loc_next;
21868             break;
21869           }
21870         break;
21871       default:
21872         break;
21873       }
21874   return true;
21875 }
21876
21877 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21878    an address in .rodata section if the string literal is emitted there,
21879    or remove the containing location list or replace DW_AT_const_value
21880    with DW_AT_location and empty location expression, if it isn't found
21881    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21882    to something that has been emitted in the current CU.  */
21883
21884 static void
21885 resolve_addr (dw_die_ref die)
21886 {
21887   dw_die_ref c;
21888   dw_attr_ref a;
21889   dw_loc_list_ref *curr, *start, loc;
21890   unsigned ix;
21891
21892   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21893     switch (AT_class (a))
21894       {
21895       case dw_val_class_loc_list:
21896         start = curr = AT_loc_list_ptr (a);
21897         loc = *curr;
21898         gcc_assert (loc);
21899         /* The same list can be referenced more than once.  See if we have
21900            already recorded the result from a previous pass.  */
21901         if (loc->replaced)
21902           *curr = loc->dw_loc_next;
21903         else if (!loc->resolved_addr)
21904           {
21905             /* As things stand, we do not expect or allow one die to
21906                reference a suffix of another die's location list chain.
21907                References must be identical or completely separate.
21908                There is therefore no need to cache the result of this
21909                pass on any list other than the first; doing so
21910                would lead to unnecessary writes.  */
21911             while (*curr)
21912               {
21913                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21914                 if (!resolve_addr_in_expr ((*curr)->expr))
21915                   {
21916                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21917                     if (next && (*curr)->ll_symbol)
21918                       {
21919                         gcc_assert (!next->ll_symbol);
21920                         next->ll_symbol = (*curr)->ll_symbol;
21921                       }
21922                     *curr = next;
21923                   }
21924                 else
21925                   {
21926                     mark_base_types ((*curr)->expr);
21927                     curr = &(*curr)->dw_loc_next;
21928                   }
21929               }
21930             if (loc == *start)
21931               loc->resolved_addr = 1;
21932             else
21933               {
21934                 loc->replaced = 1;
21935                 loc->dw_loc_next = *start;
21936               }
21937           }
21938         if (!*start)
21939           {
21940             remove_AT (die, a->dw_attr);
21941             ix--;
21942           }
21943         break;
21944       case dw_val_class_loc:
21945         {
21946           dw_loc_descr_ref l = AT_loc (a);
21947           /* For -gdwarf-2 don't attempt to optimize
21948              DW_AT_data_member_location containing
21949              DW_OP_plus_uconst - older consumers might
21950              rely on it being that op instead of a more complex,
21951              but shorter, location description.  */
21952           if ((dwarf_version > 2
21953                || a->dw_attr != DW_AT_data_member_location
21954                || l == NULL
21955                || l->dw_loc_opc != DW_OP_plus_uconst
21956                || l->dw_loc_next != NULL)
21957               && !resolve_addr_in_expr (l))
21958             {
21959               remove_AT (die, a->dw_attr);
21960               ix--;
21961             }
21962           else
21963             mark_base_types (l);
21964         }
21965         break;
21966       case dw_val_class_addr:
21967         if (a->dw_attr == DW_AT_const_value
21968             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21969           {
21970             remove_AT (die, a->dw_attr);
21971             ix--;
21972           }
21973         if (die->die_tag == DW_TAG_GNU_call_site
21974             && a->dw_attr == DW_AT_abstract_origin)
21975           {
21976             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21977             dw_die_ref tdie = lookup_decl_die (tdecl);
21978             if (tdie == NULL
21979                 && DECL_EXTERNAL (tdecl)
21980                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21981               {
21982                 force_decl_die (tdecl);
21983                 tdie = lookup_decl_die (tdecl);
21984               }
21985             if (tdie)
21986               {
21987                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21988                 a->dw_attr_val.v.val_die_ref.die = tdie;
21989                 a->dw_attr_val.v.val_die_ref.external = 0;
21990               }
21991             else
21992               {
21993                 remove_AT (die, a->dw_attr);
21994                 ix--;
21995               }
21996           }
21997         break;
21998       default:
21999         break;
22000       }
22001
22002   FOR_EACH_CHILD (die, c, resolve_addr (c));
22003 }
22004 \f
22005 /* Helper routines for optimize_location_lists.
22006    This pass tries to share identical local lists in .debug_loc
22007    section.  */
22008
22009 /* Iteratively hash operands of LOC opcode.  */
22010
22011 static inline hashval_t
22012 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22013 {
22014   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22015   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22016
22017   switch (loc->dw_loc_opc)
22018     {
22019     case DW_OP_const4u:
22020     case DW_OP_const8u:
22021       if (loc->dtprel)
22022         goto hash_addr;
22023       /* FALLTHRU */
22024     case DW_OP_const1u:
22025     case DW_OP_const1s:
22026     case DW_OP_const2u:
22027     case DW_OP_const2s:
22028     case DW_OP_const4s:
22029     case DW_OP_const8s:
22030     case DW_OP_constu:
22031     case DW_OP_consts:
22032     case DW_OP_pick:
22033     case DW_OP_plus_uconst:
22034     case DW_OP_breg0:
22035     case DW_OP_breg1:
22036     case DW_OP_breg2:
22037     case DW_OP_breg3:
22038     case DW_OP_breg4:
22039     case DW_OP_breg5:
22040     case DW_OP_breg6:
22041     case DW_OP_breg7:
22042     case DW_OP_breg8:
22043     case DW_OP_breg9:
22044     case DW_OP_breg10:
22045     case DW_OP_breg11:
22046     case DW_OP_breg12:
22047     case DW_OP_breg13:
22048     case DW_OP_breg14:
22049     case DW_OP_breg15:
22050     case DW_OP_breg16:
22051     case DW_OP_breg17:
22052     case DW_OP_breg18:
22053     case DW_OP_breg19:
22054     case DW_OP_breg20:
22055     case DW_OP_breg21:
22056     case DW_OP_breg22:
22057     case DW_OP_breg23:
22058     case DW_OP_breg24:
22059     case DW_OP_breg25:
22060     case DW_OP_breg26:
22061     case DW_OP_breg27:
22062     case DW_OP_breg28:
22063     case DW_OP_breg29:
22064     case DW_OP_breg30:
22065     case DW_OP_breg31:
22066     case DW_OP_regx:
22067     case DW_OP_fbreg:
22068     case DW_OP_piece:
22069     case DW_OP_deref_size:
22070     case DW_OP_xderef_size:
22071       hash = iterative_hash_object (val1->v.val_int, hash);
22072       break;
22073     case DW_OP_skip:
22074     case DW_OP_bra:
22075       {
22076         int offset;
22077
22078         gcc_assert (val1->val_class == dw_val_class_loc);
22079         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22080         hash = iterative_hash_object (offset, hash);
22081       }
22082       break;
22083     case DW_OP_implicit_value:
22084       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22085       switch (val2->val_class)
22086         {
22087         case dw_val_class_const:
22088           hash = iterative_hash_object (val2->v.val_int, hash);
22089           break;
22090         case dw_val_class_vec:
22091           {
22092             unsigned int elt_size = val2->v.val_vec.elt_size;
22093             unsigned int len = val2->v.val_vec.length;
22094
22095             hash = iterative_hash_object (elt_size, hash);
22096             hash = iterative_hash_object (len, hash);
22097             hash = iterative_hash (val2->v.val_vec.array,
22098                                    len * elt_size, hash);
22099           }
22100           break;
22101         case dw_val_class_const_double:
22102           hash = iterative_hash_object (val2->v.val_double.low, hash);
22103           hash = iterative_hash_object (val2->v.val_double.high, hash);
22104           break;
22105         case dw_val_class_addr:
22106           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22107           break;
22108         default:
22109           gcc_unreachable ();
22110         }
22111       break;
22112     case DW_OP_bregx:
22113     case DW_OP_bit_piece:
22114       hash = iterative_hash_object (val1->v.val_int, hash);
22115       hash = iterative_hash_object (val2->v.val_int, hash);
22116       break;
22117     case DW_OP_addr:
22118     hash_addr:
22119       if (loc->dtprel)
22120         {
22121           unsigned char dtprel = 0xd1;
22122           hash = iterative_hash_object (dtprel, hash);
22123         }
22124       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22125       break;
22126     case DW_OP_GNU_implicit_pointer:
22127       hash = iterative_hash_object (val2->v.val_int, hash);
22128       break;
22129     case DW_OP_GNU_entry_value:
22130       hash = hash_loc_operands (val1->v.val_loc, hash);
22131       break;
22132     case DW_OP_GNU_regval_type:
22133     case DW_OP_GNU_deref_type:
22134       {
22135         unsigned int byte_size
22136           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22137         unsigned int encoding
22138           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22139         hash = iterative_hash_object (val1->v.val_int, hash);
22140         hash = iterative_hash_object (byte_size, hash);
22141         hash = iterative_hash_object (encoding, hash);
22142       }
22143       break;
22144     case DW_OP_GNU_convert:
22145     case DW_OP_GNU_reinterpret:
22146       if (val1->val_class == dw_val_class_unsigned_const)
22147         {
22148           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22149           break;
22150         }
22151       /* FALLTHRU */
22152     case DW_OP_GNU_const_type:
22153       {
22154         unsigned int byte_size
22155           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22156         unsigned int encoding
22157           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22158         hash = iterative_hash_object (byte_size, hash);
22159         hash = iterative_hash_object (encoding, hash);
22160         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22161           break;
22162         hash = iterative_hash_object (val2->val_class, hash);
22163         switch (val2->val_class)
22164           {
22165           case dw_val_class_const:
22166             hash = iterative_hash_object (val2->v.val_int, hash);
22167             break;
22168           case dw_val_class_vec:
22169             {
22170               unsigned int elt_size = val2->v.val_vec.elt_size;
22171               unsigned int len = val2->v.val_vec.length;
22172
22173               hash = iterative_hash_object (elt_size, hash);
22174               hash = iterative_hash_object (len, hash);
22175               hash = iterative_hash (val2->v.val_vec.array,
22176                                      len * elt_size, hash);
22177             }
22178             break;
22179           case dw_val_class_const_double:
22180             hash = iterative_hash_object (val2->v.val_double.low, hash);
22181             hash = iterative_hash_object (val2->v.val_double.high, hash);
22182             break;
22183           default:
22184             gcc_unreachable ();
22185           }
22186       }
22187       break;
22188
22189     default:
22190       /* Other codes have no operands.  */
22191       break;
22192     }
22193   return hash;
22194 }
22195
22196 /* Iteratively hash the whole DWARF location expression LOC.  */
22197
22198 static inline hashval_t
22199 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22200 {
22201   dw_loc_descr_ref l;
22202   bool sizes_computed = false;
22203   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22204   size_of_locs (loc);
22205
22206   for (l = loc; l != NULL; l = l->dw_loc_next)
22207     {
22208       enum dwarf_location_atom opc = l->dw_loc_opc;
22209       hash = iterative_hash_object (opc, hash);
22210       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22211         {
22212           size_of_locs (loc);
22213           sizes_computed = true;
22214         }
22215       hash = hash_loc_operands (l, hash);
22216     }
22217   return hash;
22218 }
22219
22220 /* Compute hash of the whole location list LIST_HEAD.  */
22221
22222 static inline void
22223 hash_loc_list (dw_loc_list_ref list_head)
22224 {
22225   dw_loc_list_ref curr = list_head;
22226   hashval_t hash = 0;
22227
22228   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22229     {
22230       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22231       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22232       if (curr->section)
22233         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22234                                hash);
22235       hash = hash_locs (curr->expr, hash);
22236     }
22237   list_head->hash = hash;
22238 }
22239
22240 /* Return true if X and Y opcodes have the same operands.  */
22241
22242 static inline bool
22243 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22244 {
22245   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22246   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22247   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22248   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22249
22250   switch (x->dw_loc_opc)
22251     {
22252     case DW_OP_const4u:
22253     case DW_OP_const8u:
22254       if (x->dtprel)
22255         goto hash_addr;
22256       /* FALLTHRU */
22257     case DW_OP_const1u:
22258     case DW_OP_const1s:
22259     case DW_OP_const2u:
22260     case DW_OP_const2s:
22261     case DW_OP_const4s:
22262     case DW_OP_const8s:
22263     case DW_OP_constu:
22264     case DW_OP_consts:
22265     case DW_OP_pick:
22266     case DW_OP_plus_uconst:
22267     case DW_OP_breg0:
22268     case DW_OP_breg1:
22269     case DW_OP_breg2:
22270     case DW_OP_breg3:
22271     case DW_OP_breg4:
22272     case DW_OP_breg5:
22273     case DW_OP_breg6:
22274     case DW_OP_breg7:
22275     case DW_OP_breg8:
22276     case DW_OP_breg9:
22277     case DW_OP_breg10:
22278     case DW_OP_breg11:
22279     case DW_OP_breg12:
22280     case DW_OP_breg13:
22281     case DW_OP_breg14:
22282     case DW_OP_breg15:
22283     case DW_OP_breg16:
22284     case DW_OP_breg17:
22285     case DW_OP_breg18:
22286     case DW_OP_breg19:
22287     case DW_OP_breg20:
22288     case DW_OP_breg21:
22289     case DW_OP_breg22:
22290     case DW_OP_breg23:
22291     case DW_OP_breg24:
22292     case DW_OP_breg25:
22293     case DW_OP_breg26:
22294     case DW_OP_breg27:
22295     case DW_OP_breg28:
22296     case DW_OP_breg29:
22297     case DW_OP_breg30:
22298     case DW_OP_breg31:
22299     case DW_OP_regx:
22300     case DW_OP_fbreg:
22301     case DW_OP_piece:
22302     case DW_OP_deref_size:
22303     case DW_OP_xderef_size:
22304       return valx1->v.val_int == valy1->v.val_int;
22305     case DW_OP_skip:
22306     case DW_OP_bra:
22307       gcc_assert (valx1->val_class == dw_val_class_loc
22308                   && valy1->val_class == dw_val_class_loc
22309                   && x->dw_loc_addr == y->dw_loc_addr);
22310       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22311     case DW_OP_implicit_value:
22312       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22313           || valx2->val_class != valy2->val_class)
22314         return false;
22315       switch (valx2->val_class)
22316         {
22317         case dw_val_class_const:
22318           return valx2->v.val_int == valy2->v.val_int;
22319         case dw_val_class_vec:
22320           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22321                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22322                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22323                             valx2->v.val_vec.elt_size
22324                             * valx2->v.val_vec.length) == 0;
22325         case dw_val_class_const_double:
22326           return valx2->v.val_double.low == valy2->v.val_double.low
22327                  && valx2->v.val_double.high == valy2->v.val_double.high;
22328         case dw_val_class_addr:
22329           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22330         default:
22331           gcc_unreachable ();
22332         }
22333     case DW_OP_bregx:
22334     case DW_OP_bit_piece:
22335       return valx1->v.val_int == valy1->v.val_int
22336              && valx2->v.val_int == valy2->v.val_int;
22337     case DW_OP_addr:
22338     hash_addr:
22339       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22340     case DW_OP_GNU_implicit_pointer:
22341       return valx1->val_class == dw_val_class_die_ref
22342              && valx1->val_class == valy1->val_class
22343              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22344              && valx2->v.val_int == valy2->v.val_int;
22345     case DW_OP_GNU_entry_value:
22346       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22347     case DW_OP_GNU_const_type:
22348       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22349           || valx2->val_class != valy2->val_class)
22350         return false;
22351       switch (valx2->val_class)
22352         {
22353         case dw_val_class_const:
22354           return valx2->v.val_int == valy2->v.val_int;
22355         case dw_val_class_vec:
22356           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22357                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22358                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22359                             valx2->v.val_vec.elt_size
22360                             * valx2->v.val_vec.length) == 0;
22361         case dw_val_class_const_double:
22362           return valx2->v.val_double.low == valy2->v.val_double.low
22363                  && valx2->v.val_double.high == valy2->v.val_double.high;
22364         default:
22365           gcc_unreachable ();
22366         }
22367     case DW_OP_GNU_regval_type:
22368     case DW_OP_GNU_deref_type:
22369       return valx1->v.val_int == valy1->v.val_int
22370              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22371     case DW_OP_GNU_convert:
22372     case DW_OP_GNU_reinterpret:
22373       if (valx1->val_class != valy1->val_class)
22374         return false;
22375       if (valx1->val_class == dw_val_class_unsigned_const)
22376         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22377       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22378     case DW_OP_GNU_parameter_ref:
22379       return valx1->val_class == dw_val_class_die_ref
22380              && valx1->val_class == valy1->val_class
22381              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22382     default:
22383       /* Other codes have no operands.  */
22384       return true;
22385     }
22386 }
22387
22388 /* Return true if DWARF location expressions X and Y are the same.  */
22389
22390 static inline bool
22391 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22392 {
22393   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22394     if (x->dw_loc_opc != y->dw_loc_opc
22395         || x->dtprel != y->dtprel
22396         || !compare_loc_operands (x, y))
22397       break;
22398   return x == NULL && y == NULL;
22399 }
22400
22401 /* Return precomputed hash of location list X.  */
22402
22403 static hashval_t
22404 loc_list_hash (const void *x)
22405 {
22406   return ((const struct dw_loc_list_struct *) x)->hash;
22407 }
22408
22409 /* Return 1 if location lists X and Y are the same.  */
22410
22411 static int
22412 loc_list_eq (const void *x, const void *y)
22413 {
22414   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22415   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22416   if (a == b)
22417     return 1;
22418   if (a->hash != b->hash)
22419     return 0;
22420   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22421     if (strcmp (a->begin, b->begin) != 0
22422         || strcmp (a->end, b->end) != 0
22423         || (a->section == NULL) != (b->section == NULL)
22424         || (a->section && strcmp (a->section, b->section) != 0)
22425         || !compare_locs (a->expr, b->expr))
22426       break;
22427   return a == NULL && b == NULL;
22428 }
22429
22430 /* Recursively optimize location lists referenced from DIE
22431    children and share them whenever possible.  */
22432
22433 static void
22434 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22435 {
22436   dw_die_ref c;
22437   dw_attr_ref a;
22438   unsigned ix;
22439   void **slot;
22440
22441   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22442     if (AT_class (a) == dw_val_class_loc_list)
22443       {
22444         dw_loc_list_ref list = AT_loc_list (a);
22445         /* TODO: perform some optimizations here, before hashing
22446            it and storing into the hash table.  */
22447         hash_loc_list (list);
22448         slot = htab_find_slot_with_hash (htab, list, list->hash,
22449                                          INSERT);
22450         if (*slot == NULL)
22451           *slot = (void *) list;
22452         else
22453           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22454       }
22455
22456   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22457 }
22458
22459 /* Optimize location lists referenced from DIE
22460    children and share them whenever possible.  */
22461
22462 static void
22463 optimize_location_lists (dw_die_ref die)
22464 {
22465   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22466   optimize_location_lists_1 (die, htab);
22467   htab_delete (htab);
22468 }
22469 \f
22470 /* Output stuff that dwarf requires at the end of every file,
22471    and generate the DWARF-2 debugging info.  */
22472
22473 static void
22474 dwarf2out_finish (const char *filename)
22475 {
22476   limbo_die_node *node, *next_node;
22477   comdat_type_node *ctnode;
22478   htab_t comdat_type_table;
22479   unsigned int i;
22480
22481   /* PCH might result in DW_AT_producer string being restored from the
22482      header compilation, fix it up if needed.  */
22483   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22484   if (strcmp (AT_string (producer), producer_string) != 0)
22485     {
22486       struct indirect_string_node *node = find_AT_string (producer_string);
22487       producer->dw_attr_val.v.val_str = node;
22488     }
22489
22490   gen_scheduled_generic_parms_dies ();
22491   gen_remaining_tmpl_value_param_die_attribute ();
22492
22493   /* Add the name for the main input file now.  We delayed this from
22494      dwarf2out_init to avoid complications with PCH.  */
22495   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22496   if (!IS_ABSOLUTE_PATH (filename))
22497     add_comp_dir_attribute (comp_unit_die ());
22498   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22499     {
22500       bool p = false;
22501       htab_traverse (file_table, file_table_relative_p, &p);
22502       if (p)
22503         add_comp_dir_attribute (comp_unit_die ());
22504     }
22505
22506   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22507     {
22508       add_location_or_const_value_attribute (
22509         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22510         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22511         false,
22512         DW_AT_location);
22513     }
22514
22515   /* Traverse the limbo die list, and add parent/child links.  The only
22516      dies without parents that should be here are concrete instances of
22517      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22518      For concrete instances, we can get the parent die from the abstract
22519      instance.  */
22520   for (node = limbo_die_list; node; node = next_node)
22521     {
22522       dw_die_ref die = node->die;
22523       next_node = node->next;
22524
22525       if (die->die_parent == NULL)
22526         {
22527           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22528
22529           if (origin && origin->die_parent)
22530             add_child_die (origin->die_parent, die);
22531           else if (is_cu_die (die))
22532             ;
22533           else if (seen_error ())
22534             /* It's OK to be confused by errors in the input.  */
22535             add_child_die (comp_unit_die (), die);
22536           else
22537             {
22538               /* In certain situations, the lexical block containing a
22539                  nested function can be optimized away, which results
22540                  in the nested function die being orphaned.  Likewise
22541                  with the return type of that nested function.  Force
22542                  this to be a child of the containing function.
22543
22544                  It may happen that even the containing function got fully
22545                  inlined and optimized out.  In that case we are lost and
22546                  assign the empty child.  This should not be big issue as
22547                  the function is likely unreachable too.  */
22548               tree context = NULL_TREE;
22549
22550               gcc_assert (node->created_for);
22551
22552               if (DECL_P (node->created_for))
22553                 context = DECL_CONTEXT (node->created_for);
22554               else if (TYPE_P (node->created_for))
22555                 context = TYPE_CONTEXT (node->created_for);
22556
22557               origin = get_context_die (context);
22558               add_child_die (origin, die);
22559             }
22560         }
22561     }
22562
22563   limbo_die_list = NULL;
22564
22565 #if ENABLE_ASSERT_CHECKING
22566   {
22567     dw_die_ref die = comp_unit_die (), c;
22568     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22569   }
22570 #endif
22571   resolve_addr (comp_unit_die ());
22572   move_marked_base_types ();
22573
22574   for (node = deferred_asm_name; node; node = node->next)
22575     {
22576       tree decl = node->created_for;
22577       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22578         {
22579           add_linkage_attr (node->die, decl);
22580           move_linkage_attr (node->die);
22581         }
22582     }
22583
22584   deferred_asm_name = NULL;
22585
22586   /* Walk through the list of incomplete types again, trying once more to
22587      emit full debugging info for them.  */
22588   retry_incomplete_types ();
22589
22590   if (flag_eliminate_unused_debug_types)
22591     prune_unused_types ();
22592
22593   /* Generate separate CUs for each of the include files we've seen.
22594      They will go into limbo_die_list.  */
22595   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22596     break_out_includes (comp_unit_die ());
22597
22598   /* Generate separate COMDAT sections for type DIEs. */
22599   if (use_debug_types)
22600     {
22601       break_out_comdat_types (comp_unit_die ());
22602
22603       /* Each new type_unit DIE was added to the limbo die list when created.
22604          Since these have all been added to comdat_type_list, clear the
22605          limbo die list.  */
22606       limbo_die_list = NULL;
22607
22608       /* For each new comdat type unit, copy declarations for incomplete
22609          types to make the new unit self-contained (i.e., no direct
22610          references to the main compile unit).  */
22611       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22612         copy_decls_for_unworthy_types (ctnode->root_die);
22613       copy_decls_for_unworthy_types (comp_unit_die ());
22614
22615       /* In the process of copying declarations from one unit to another,
22616          we may have left some declarations behind that are no longer
22617          referenced.  Prune them.  */
22618       prune_unused_types ();
22619     }
22620
22621   /* Traverse the DIE's and add add sibling attributes to those DIE's
22622      that have children.  */
22623   add_sibling_attributes (comp_unit_die ());
22624   for (node = limbo_die_list; node; node = node->next)
22625     add_sibling_attributes (node->die);
22626   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22627     add_sibling_attributes (ctnode->root_die);
22628
22629   /* Output a terminator label for the .text section.  */
22630   switch_to_section (text_section);
22631   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22632   if (cold_text_section)
22633     {
22634       switch_to_section (cold_text_section);
22635       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22636     }
22637
22638   /* We can only use the low/high_pc attributes if all of the code was
22639      in .text.  */
22640   if (!have_multiple_function_sections 
22641       || (dwarf_version < 3 && dwarf_strict))
22642     {
22643       /* Don't add if the CU has no associated code.  */
22644       if (text_section_used)
22645         {
22646           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22647           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22648         }
22649     }
22650   else
22651     {
22652       unsigned fde_idx;
22653       dw_fde_ref fde;
22654       bool range_list_added = false;
22655
22656       if (text_section_used)
22657         add_ranges_by_labels (comp_unit_die (), text_section_label,
22658                               text_end_label, &range_list_added);
22659       if (cold_text_section_used)
22660         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22661                               cold_end_label, &range_list_added);
22662
22663       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22664         {
22665           if (!fde->in_std_section)
22666             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22667                                   fde->dw_fde_end, &range_list_added);
22668           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22669             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22670                                   fde->dw_fde_second_end, &range_list_added);
22671         }
22672
22673       if (range_list_added)
22674         {
22675           /* We need to give .debug_loc and .debug_ranges an appropriate
22676              "base address".  Use zero so that these addresses become
22677              absolute.  Historically, we've emitted the unexpected
22678              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22679              Emit both to give time for other tools to adapt.  */
22680           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22681           if (! dwarf_strict && dwarf_version < 4)
22682             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22683
22684           add_ranges (NULL);
22685         }
22686     }
22687
22688   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22689     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22690                     debug_line_section_label);
22691
22692   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22693     add_AT_macptr (comp_unit_die (),
22694                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22695                    macinfo_section_label);
22696
22697   if (have_location_lists)
22698     optimize_location_lists (comp_unit_die ());
22699
22700   /* Output all of the compilation units.  We put the main one last so that
22701      the offsets are available to output_pubnames.  */
22702   for (node = limbo_die_list; node; node = node->next)
22703     output_comp_unit (node->die, 0);
22704
22705   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22706   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22707     {
22708       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22709
22710       /* Don't output duplicate types.  */
22711       if (*slot != HTAB_EMPTY_ENTRY)
22712         continue;
22713
22714       /* Add a pointer to the line table for the main compilation unit
22715          so that the debugger can make sense of DW_AT_decl_file
22716          attributes.  */
22717       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22718         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22719                         debug_line_section_label);
22720
22721       output_comdat_type_unit (ctnode);
22722       *slot = ctnode;
22723     }
22724   htab_delete (comdat_type_table);
22725
22726   /* Output the main compilation unit if non-empty or if .debug_macinfo
22727      will be emitted.  */
22728   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22729
22730   /* Output the abbreviation table.  */
22731   if (abbrev_die_table_in_use != 1)
22732     {
22733       switch_to_section (debug_abbrev_section);
22734       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22735       output_abbrev_section ();
22736     }
22737
22738   /* Output location list section if necessary.  */
22739   if (have_location_lists)
22740     {
22741       /* Output the location lists info.  */
22742       switch_to_section (debug_loc_section);
22743       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22744                                    DEBUG_LOC_SECTION_LABEL, 0);
22745       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22746       output_location_lists (comp_unit_die ());
22747     }
22748
22749   /* Output public names table if necessary.  */
22750   if (!VEC_empty (pubname_entry, pubname_table))
22751     {
22752       gcc_assert (info_section_emitted);
22753       switch_to_section (debug_pubnames_section);
22754       output_pubnames (pubname_table);
22755     }
22756
22757   /* Output public types table if necessary.  */
22758   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22759      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22760      simply won't look for the section.  */
22761   if (!VEC_empty (pubname_entry, pubtype_table))
22762     {
22763       bool empty = false;
22764       
22765       if (flag_eliminate_unused_debug_types)
22766         {
22767           /* The pubtypes table might be emptied by pruning unused items.  */
22768           unsigned i;
22769           pubname_ref p;
22770           empty = true;
22771           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22772             if (p->die->die_offset != 0)
22773               {
22774                 empty = false;
22775                 break;
22776               }
22777         }
22778       if (!empty)
22779         {
22780           gcc_assert (info_section_emitted);
22781           switch_to_section (debug_pubtypes_section);
22782           output_pubnames (pubtype_table);
22783         }
22784     }
22785
22786   /* Output the address range information if a CU (.debug_info section)
22787      was emitted.  We output an empty table even if we had no functions
22788      to put in it.  This because the consumer has no way to tell the
22789      difference between an empty table that we omitted and failure to
22790      generate a table that would have contained data.  */
22791   if (info_section_emitted)
22792     {
22793       unsigned long aranges_length = size_of_aranges ();
22794
22795       switch_to_section (debug_aranges_section);
22796       output_aranges (aranges_length);
22797     }
22798
22799   /* Output ranges section if necessary.  */
22800   if (ranges_table_in_use)
22801     {
22802       switch_to_section (debug_ranges_section);
22803       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22804       output_ranges ();
22805     }
22806
22807   /* Have to end the macro section.  */
22808   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22809     {
22810       switch_to_section (debug_macinfo_section);
22811       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22812       if (!VEC_empty (macinfo_entry, macinfo_table))
22813         output_macinfo ();
22814       dw2_asm_output_data (1, 0, "End compilation unit");
22815     }
22816
22817   /* Output the source line correspondence table.  We must do this
22818      even if there is no line information.  Otherwise, on an empty
22819      translation unit, we will generate a present, but empty,
22820      .debug_info section.  IRIX 6.5 `nm' will then complain when
22821      examining the file.  This is done late so that any filenames
22822      used by the debug_info section are marked as 'used'.  */
22823   switch_to_section (debug_line_section);
22824   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22825   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22826     output_line_info ();
22827
22828   /* If we emitted any DW_FORM_strp form attribute, output the string
22829      table too.  */
22830   if (debug_str_hash)
22831     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22832 }
22833
22834 #include "gt-dwarf2out.h"