OSDN Git Service

dwarf2cfi: Implement change_cfi_row.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97 #include "opts.h"
98
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_insn;
101
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104
105 /* Define this macro to be a nonzero value if the directory specifications
106     which are output in the debug info should end with a separator.  */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109    from generating indirect strings in DWARF2 debug information, for instance
110    if your target is stuck with an old version of GDB that is unable to
111    process them properly or uses VMS Debug.  */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
117
118 /* ??? Poison these here until it can be done generically.  They've been
119    totally replaced in this file; make sure it stays that way.  */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
125
126 /* The size of the target's pointer type.  */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
130
131 /* Array of RTXes referenced by the debugging information, which therefore
132    must be kept around forever.  */
133 static GTY(()) VEC(rtx,gc) *used_rtx_array;
134
135 /* A pointer to the base of a list of incomplete types which might be
136    completed at some later time.  incomplete_types_list needs to be a
137    VEC(tree,gc) because we want to tell the garbage collector about
138    it.  */
139 static GTY(()) VEC(tree,gc) *incomplete_types;
140
141 /* A pointer to the base of a table of references to declaration
142    scopes.  This table is a display which tracks the nesting
143    of declaration scopes at the current scope and containing
144    scopes.  This table is used to find the proper place to
145    define type declaration DIE's.  */
146 static GTY(()) VEC(tree,gc) *decl_scope_table;
147
148 /* Pointers to various DWARF2 sections.  */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_macinfo_section;
153 static GTY(()) section *debug_line_section;
154 static GTY(()) section *debug_loc_section;
155 static GTY(()) section *debug_pubnames_section;
156 static GTY(()) section *debug_pubtypes_section;
157 static GTY(()) section *debug_str_section;
158 static GTY(()) section *debug_ranges_section;
159 static GTY(()) section *debug_frame_section;
160
161 /* Maximum size (in bytes) of an artificially generated label.  */
162 #define MAX_ARTIFICIAL_LABEL_BYTES      30
163
164 /* According to the (draft) DWARF 3 specification, the initial length
165    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
166    bytes are 0xffffffff, followed by the length stored in the next 8
167    bytes.
168
169    However, the SGI/MIPS ABI uses an initial length which is equal to
170    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
171
172 #ifndef DWARF_INITIAL_LENGTH_SIZE
173 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
174 #endif
175
176 /* Round SIZE up to the nearest BOUNDARY.  */
177 #define DWARF_ROUND(SIZE,BOUNDARY) \
178   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
179
180 /* CIE identifier.  */
181 #if HOST_BITS_PER_WIDE_INT >= 64
182 #define DWARF_CIE_ID \
183   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
184 #else
185 #define DWARF_CIE_ID DW_CIE_ID
186 #endif
187
188 DEF_VEC_P (dw_fde_ref);
189 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
190
191 /* A vector for a table that contains frame description
192    information for each routine.  */
193 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
194
195 struct GTY(()) indirect_string_node {
196   const char *str;
197   unsigned int refcount;
198   enum dwarf_form form;
199   char *label;
200 };
201
202 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
203
204 static GTY(()) int dw2_string_counter;
205
206 /* True if the compilation unit places functions in more than one section.  */
207 static GTY(()) bool have_multiple_function_sections = false;
208
209 /* Whether the default text and cold text sections have been used at all.  */
210
211 static GTY(()) bool text_section_used = false;
212 static GTY(()) bool cold_text_section_used = false;
213
214 /* The default cold text section.  */
215 static GTY(()) section *cold_text_section;
216
217 /* Forward declarations for functions defined in this file.  */
218
219 static char *stripattributes (const char *);
220 static void output_call_frame_info (int);
221 static void dwarf2out_note_section_used (void);
222
223 /* Personality decl of current unit.  Used only when assembler does not support
224    personality CFI.  */
225 static GTY(()) rtx current_unit_personality;
226
227 /* Data and reference forms for relocatable data.  */
228 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
229 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
230
231 #ifndef DEBUG_FRAME_SECTION
232 #define DEBUG_FRAME_SECTION     ".debug_frame"
233 #endif
234
235 #ifndef FUNC_BEGIN_LABEL
236 #define FUNC_BEGIN_LABEL        "LFB"
237 #endif
238
239 #ifndef FUNC_END_LABEL
240 #define FUNC_END_LABEL          "LFE"
241 #endif
242
243 #ifndef PROLOGUE_END_LABEL
244 #define PROLOGUE_END_LABEL      "LPE"
245 #endif
246
247 #ifndef EPILOGUE_BEGIN_LABEL
248 #define EPILOGUE_BEGIN_LABEL    "LEB"
249 #endif
250
251 #ifndef FRAME_BEGIN_LABEL
252 #define FRAME_BEGIN_LABEL       "Lframe"
253 #endif
254 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
255 #define CIE_END_LABEL           "LECIE"
256 #define FDE_LABEL               "LSFDE"
257 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
258 #define FDE_END_LABEL           "LEFDE"
259 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
260 #define LINE_NUMBER_END_LABEL   "LELT"
261 #define LN_PROLOG_AS_LABEL      "LASLTP"
262 #define LN_PROLOG_END_LABEL     "LELTP"
263 #define DIE_LABEL_PREFIX        "DW"
264 \f
265 /* Match the base name of a file to the base name of a compilation unit. */
266
267 static int
268 matches_main_base (const char *path)
269 {
270   /* Cache the last query. */
271   static const char *last_path = NULL;
272   static int last_match = 0;
273   if (path != last_path)
274     {
275       const char *base;
276       int length = base_of_path (path, &base);
277       last_path = path;
278       last_match = (length == main_input_baselength
279                     && memcmp (base, main_input_basename, length) == 0);
280     }
281   return last_match;
282 }
283
284 #ifdef DEBUG_DEBUG_STRUCT
285
286 static int
287 dump_struct_debug (tree type, enum debug_info_usage usage,
288                    enum debug_struct_file criterion, int generic,
289                    int matches, int result)
290 {
291   /* Find the type name. */
292   tree type_decl = TYPE_STUB_DECL (type);
293   tree t = type_decl;
294   const char *name = 0;
295   if (TREE_CODE (t) == TYPE_DECL)
296     t = DECL_NAME (t);
297   if (t)
298     name = IDENTIFIER_POINTER (t);
299
300   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
301            criterion,
302            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
303            matches ? "bas" : "hdr",
304            generic ? "gen" : "ord",
305            usage == DINFO_USAGE_DFN ? ";" :
306              usage == DINFO_USAGE_DIR_USE ? "." : "*",
307            result,
308            (void*) type_decl, name);
309   return result;
310 }
311 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
312   dump_struct_debug (type, usage, criterion, generic, matches, result)
313
314 #else
315
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317   (result)
318
319 #endif
320
321 static bool
322 should_emit_struct_debug (tree type, enum debug_info_usage usage)
323 {
324   enum debug_struct_file criterion;
325   tree type_decl;
326   bool generic = lang_hooks.types.generic_p (type);
327
328   if (generic)
329     criterion = debug_struct_generic[usage];
330   else
331     criterion = debug_struct_ordinary[usage];
332
333   if (criterion == DINFO_STRUCT_FILE_NONE)
334     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335   if (criterion == DINFO_STRUCT_FILE_ANY)
336     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
337
338   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
339
340   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
341     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
342
343   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
344     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
345   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
346 }
347 \f
348 /* Return a pointer to a copy of the section string name S with all
349    attributes stripped off, and an asterisk prepended (for assemble_name).  */
350
351 static inline char *
352 stripattributes (const char *s)
353 {
354   char *stripped = XNEWVEC (char, strlen (s) + 2);
355   char *p = stripped;
356
357   *p++ = '*';
358
359   while (*s && *s != ',')
360     *p++ = *s++;
361
362   *p = '\0';
363   return stripped;
364 }
365
366 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
367    switch to the data section instead, and write out a synthetic start label
368    for collect2 the first time around.  */
369
370 static void
371 switch_to_eh_frame_section (bool back)
372 {
373   tree label;
374
375 #ifdef EH_FRAME_SECTION_NAME
376   if (eh_frame_section == 0)
377     {
378       int flags;
379
380       if (EH_TABLES_CAN_BE_READ_ONLY)
381         {
382           int fde_encoding;
383           int per_encoding;
384           int lsda_encoding;
385
386           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
387                                                        /*global=*/0);
388           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
389                                                        /*global=*/1);
390           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
391                                                         /*global=*/0);
392           flags = ((! flag_pic
393                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
394                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
395                         && (per_encoding & 0x70) != DW_EH_PE_absptr
396                         && (per_encoding & 0x70) != DW_EH_PE_aligned
397                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
398                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
399                    ? 0 : SECTION_WRITE);
400         }
401       else
402         flags = SECTION_WRITE;
403       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
404     }
405 #endif /* EH_FRAME_SECTION_NAME */
406
407   if (eh_frame_section)
408     switch_to_section (eh_frame_section);
409   else
410     {
411       /* We have no special eh_frame section.  Put the information in
412          the data section and emit special labels to guide collect2.  */
413       switch_to_section (data_section);
414
415       if (!back)
416         {
417           label = get_file_function_name ("F");
418           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
419           targetm.asm_out.globalize_label (asm_out_file,
420                                            IDENTIFIER_POINTER (label));
421           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
422         }
423     }
424 }
425
426 /* Switch [BACK] to the eh or debug frame table section, depending on
427    FOR_EH.  */
428
429 static void
430 switch_to_frame_table_section (int for_eh, bool back)
431 {
432   if (for_eh)
433     switch_to_eh_frame_section (back);
434   else
435     {
436       if (!debug_frame_section)
437         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
438                                            SECTION_DEBUG, NULL);
439       switch_to_section (debug_frame_section);
440     }
441 }
442
443 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
444
445 enum dw_cfi_oprnd_type
446 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
447 {
448   switch (cfi)
449     {
450     case DW_CFA_nop:
451     case DW_CFA_GNU_window_save:
452     case DW_CFA_remember_state:
453     case DW_CFA_restore_state:
454       return dw_cfi_oprnd_unused;
455
456     case DW_CFA_set_loc:
457     case DW_CFA_advance_loc1:
458     case DW_CFA_advance_loc2:
459     case DW_CFA_advance_loc4:
460     case DW_CFA_MIPS_advance_loc8:
461       return dw_cfi_oprnd_addr;
462
463     case DW_CFA_offset:
464     case DW_CFA_offset_extended:
465     case DW_CFA_def_cfa:
466     case DW_CFA_offset_extended_sf:
467     case DW_CFA_def_cfa_sf:
468     case DW_CFA_restore:
469     case DW_CFA_restore_extended:
470     case DW_CFA_undefined:
471     case DW_CFA_same_value:
472     case DW_CFA_def_cfa_register:
473     case DW_CFA_register:
474     case DW_CFA_expression:
475       return dw_cfi_oprnd_reg_num;
476
477     case DW_CFA_def_cfa_offset:
478     case DW_CFA_GNU_args_size:
479     case DW_CFA_def_cfa_offset_sf:
480       return dw_cfi_oprnd_offset;
481
482     case DW_CFA_def_cfa_expression:
483       return dw_cfi_oprnd_loc;
484
485     default:
486       gcc_unreachable ();
487     }
488 }
489
490 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
491
492 enum dw_cfi_oprnd_type
493 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
494 {
495   switch (cfi)
496     {
497     case DW_CFA_def_cfa:
498     case DW_CFA_def_cfa_sf:
499     case DW_CFA_offset:
500     case DW_CFA_offset_extended_sf:
501     case DW_CFA_offset_extended:
502       return dw_cfi_oprnd_offset;
503
504     case DW_CFA_register:
505       return dw_cfi_oprnd_reg_num;
506
507     case DW_CFA_expression:
508       return dw_cfi_oprnd_loc;
509
510     default:
511       return dw_cfi_oprnd_unused;
512     }
513 }
514
515 /* Output one FDE.  */
516
517 static void
518 output_fde (dw_fde_ref fde, bool for_eh, bool second,
519             char *section_start_label, int fde_encoding, char *augmentation,
520             bool any_lsda_needed, int lsda_encoding)
521 {
522   const char *begin, *end;
523   static unsigned int j;
524   char l1[20], l2[20];
525
526   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
527                                      /* empty */ 0);
528   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
529                                   for_eh + j);
530   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
531   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
532   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
533     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
534                          " indicating 64-bit DWARF extension");
535   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
536                         "FDE Length");
537   ASM_OUTPUT_LABEL (asm_out_file, l1);
538
539   if (for_eh)
540     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
541   else
542     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
543                            debug_frame_section, "FDE CIE offset");
544
545   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
546   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
547
548   if (for_eh)
549     {
550       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
551       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
552       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
553                                        "FDE initial location");
554       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
555                             end, begin, "FDE address range");
556     }
557   else
558     {
559       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
560       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
561     }
562
563   if (augmentation[0])
564     {
565       if (any_lsda_needed)
566         {
567           int size = size_of_encoded_value (lsda_encoding);
568
569           if (lsda_encoding == DW_EH_PE_aligned)
570             {
571               int offset = (  4         /* Length */
572                             + 4         /* CIE offset */
573                             + 2 * size_of_encoded_value (fde_encoding)
574                             + 1         /* Augmentation size */ );
575               int pad = -offset & (PTR_SIZE - 1);
576
577               size += pad;
578               gcc_assert (size_of_uleb128 (size) == 1);
579             }
580
581           dw2_asm_output_data_uleb128 (size, "Augmentation size");
582
583           if (fde->uses_eh_lsda)
584             {
585               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
586                                            fde->funcdef_number);
587               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
588                                                gen_rtx_SYMBOL_REF (Pmode, l1),
589                                                false,
590                                                "Language Specific Data Area");
591             }
592           else
593             {
594               if (lsda_encoding == DW_EH_PE_aligned)
595                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
596               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
597                                    "Language Specific Data Area (none)");
598             }
599         }
600       else
601         dw2_asm_output_data_uleb128 (0, "Augmentation size");
602     }
603
604   /* Loop through the Call Frame Instructions associated with this FDE.  */
605   fde->dw_fde_current_label = begin;
606   {
607     size_t from, until, i;
608
609     from = 0;
610     until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
611
612     if (fde->dw_fde_second_begin == NULL)
613       ;
614     else if (!second)
615       until = fde->dw_fde_switch_cfi_index;
616     else
617       from = fde->dw_fde_switch_cfi_index;
618
619     for (i = from; i < until; i++)
620       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
621   }
622
623   /* If we are to emit a ref/link from function bodies to their frame tables,
624      do it now.  This is typically performed to make sure that tables
625      associated with functions are dragged with them and not discarded in
626      garbage collecting links. We need to do this on a per function basis to
627      cope with -ffunction-sections.  */
628
629 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
630   /* Switch to the function section, emit the ref to the tables, and
631      switch *back* into the table section.  */
632   switch_to_section (function_section (fde->decl));
633   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
634   switch_to_frame_table_section (for_eh, true);
635 #endif
636
637   /* Pad the FDE out to an address sized boundary.  */
638   ASM_OUTPUT_ALIGN (asm_out_file,
639                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
640   ASM_OUTPUT_LABEL (asm_out_file, l2);
641
642   j += 2;
643 }
644
645 /* Return true if frame description entry FDE is needed for EH.  */
646
647 static bool
648 fde_needed_for_eh_p (dw_fde_ref fde)
649 {
650   if (flag_asynchronous_unwind_tables)
651     return true;
652
653   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
654     return true;
655
656   if (fde->uses_eh_lsda)
657     return true;
658
659   /* If exceptions are enabled, we have collected nothrow info.  */
660   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
661     return false;
662
663   return true;
664 }
665
666 /* Output the call frame information used to record information
667    that relates to calculating the frame pointer, and records the
668    location of saved registers.  */
669
670 static void
671 output_call_frame_info (int for_eh)
672 {
673   unsigned int i;
674   dw_fde_ref fde;
675   dw_cfi_ref cfi;
676   char l1[20], l2[20], section_start_label[20];
677   bool any_lsda_needed = false;
678   char augmentation[6];
679   int augmentation_size;
680   int fde_encoding = DW_EH_PE_absptr;
681   int per_encoding = DW_EH_PE_absptr;
682   int lsda_encoding = DW_EH_PE_absptr;
683   int return_reg;
684   rtx personality = NULL;
685   int dw_cie_version;
686
687   /* Don't emit a CIE if there won't be any FDEs.  */
688   if (fde_vec == NULL)
689     return;
690
691   /* Nothing to do if the assembler's doing it all.  */
692   if (dwarf2out_do_cfi_asm ())
693     return;
694
695   /* If we don't have any functions we'll want to unwind out of, don't emit
696      any EH unwind information.  If we make FDEs linkonce, we may have to
697      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
698      want to avoid having an FDE kept around when the function it refers to
699      is discarded.  Example where this matters: a primary function template
700      in C++ requires EH information, an explicit specialization doesn't.  */
701   if (for_eh)
702     {
703       bool any_eh_needed = false;
704
705       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
706         {
707           if (fde->uses_eh_lsda)
708             any_eh_needed = any_lsda_needed = true;
709           else if (fde_needed_for_eh_p (fde))
710             any_eh_needed = true;
711           else if (TARGET_USES_WEAK_UNWIND_INFO)
712             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
713         }
714
715       if (!any_eh_needed)
716         return;
717     }
718
719   /* We're going to be generating comments, so turn on app.  */
720   if (flag_debug_asm)
721     app_enable ();
722
723   /* Switch to the proper frame section, first time.  */
724   switch_to_frame_table_section (for_eh, false);
725
726   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
727   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
728
729   /* Output the CIE.  */
730   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
731   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
732   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
733     dw2_asm_output_data (4, 0xffffffff,
734       "Initial length escape value indicating 64-bit DWARF extension");
735   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
736                         "Length of Common Information Entry");
737   ASM_OUTPUT_LABEL (asm_out_file, l1);
738
739   /* Now that the CIE pointer is PC-relative for EH,
740      use 0 to identify the CIE.  */
741   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
742                        (for_eh ? 0 : DWARF_CIE_ID),
743                        "CIE Identifier Tag");
744
745   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
746      use CIE version 1, unless that would produce incorrect results
747      due to overflowing the return register column.  */
748   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
749   dw_cie_version = 1;
750   if (return_reg >= 256 || dwarf_version > 2)
751     dw_cie_version = 3;
752   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
753
754   augmentation[0] = 0;
755   augmentation_size = 0;
756
757   personality = current_unit_personality;
758   if (for_eh)
759     {
760       char *p;
761
762       /* Augmentation:
763          z      Indicates that a uleb128 is present to size the
764                 augmentation section.
765          L      Indicates the encoding (and thus presence) of
766                 an LSDA pointer in the FDE augmentation.
767          R      Indicates a non-default pointer encoding for
768                 FDE code pointers.
769          P      Indicates the presence of an encoding + language
770                 personality routine in the CIE augmentation.  */
771
772       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
773       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
774       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
775
776       p = augmentation + 1;
777       if (personality)
778         {
779           *p++ = 'P';
780           augmentation_size += 1 + size_of_encoded_value (per_encoding);
781           assemble_external_libcall (personality);
782         }
783       if (any_lsda_needed)
784         {
785           *p++ = 'L';
786           augmentation_size += 1;
787         }
788       if (fde_encoding != DW_EH_PE_absptr)
789         {
790           *p++ = 'R';
791           augmentation_size += 1;
792         }
793       if (p > augmentation + 1)
794         {
795           augmentation[0] = 'z';
796           *p = '\0';
797         }
798
799       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
800       if (personality && per_encoding == DW_EH_PE_aligned)
801         {
802           int offset = (  4             /* Length */
803                         + 4             /* CIE Id */
804                         + 1             /* CIE version */
805                         + strlen (augmentation) + 1     /* Augmentation */
806                         + size_of_uleb128 (1)           /* Code alignment */
807                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
808                         + 1             /* RA column */
809                         + 1             /* Augmentation size */
810                         + 1             /* Personality encoding */ );
811           int pad = -offset & (PTR_SIZE - 1);
812
813           augmentation_size += pad;
814
815           /* Augmentations should be small, so there's scarce need to
816              iterate for a solution.  Die if we exceed one uleb128 byte.  */
817           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
818         }
819     }
820
821   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
822   if (dw_cie_version >= 4)
823     {
824       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
825       dw2_asm_output_data (1, 0, "CIE Segment Size");
826     }
827   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
828   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
829                                "CIE Data Alignment Factor");
830
831   if (dw_cie_version == 1)
832     dw2_asm_output_data (1, return_reg, "CIE RA Column");
833   else
834     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
835
836   if (augmentation[0])
837     {
838       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
839       if (personality)
840         {
841           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
842                                eh_data_format_name (per_encoding));
843           dw2_asm_output_encoded_addr_rtx (per_encoding,
844                                            personality,
845                                            true, NULL);
846         }
847
848       if (any_lsda_needed)
849         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
850                              eh_data_format_name (lsda_encoding));
851
852       if (fde_encoding != DW_EH_PE_absptr)
853         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
854                              eh_data_format_name (fde_encoding));
855     }
856
857   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
858     output_cfi (cfi, NULL, for_eh);
859
860   /* Pad the CIE out to an address sized boundary.  */
861   ASM_OUTPUT_ALIGN (asm_out_file,
862                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
863   ASM_OUTPUT_LABEL (asm_out_file, l2);
864
865   /* Loop through all of the FDE's.  */
866   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
867     {
868       unsigned int k;
869
870       /* Don't emit EH unwind info for leaf functions that don't need it.  */
871       if (for_eh && !fde_needed_for_eh_p (fde))
872         continue;
873
874       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
875         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
876                     augmentation, any_lsda_needed, lsda_encoding);
877     }
878
879   if (for_eh && targetm.terminate_dw2_eh_frame_info)
880     dw2_asm_output_data (4, 0, "End of Table");
881 #ifdef MIPS_DEBUGGING_INFO
882   /* Work around Irix 6 assembler bug whereby labels at the end of a section
883      get a value of 0.  Putting .align 0 after the label fixes it.  */
884   ASM_OUTPUT_ALIGN (asm_out_file, 0);
885 #endif
886
887   /* Turn off app to make assembly quicker.  */
888   if (flag_debug_asm)
889     app_disable ();
890 }
891
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
893
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
896 {
897   int enc;
898   rtx ref;
899   rtx personality = get_personality_function (current_function_decl);
900
901   fprintf (asm_out_file, "\t.cfi_startproc\n");
902
903   if (personality)
904     {
905       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906       ref = personality;
907
908       /* ??? The GAS support isn't entirely consistent.  We have to
909          handle indirect support ourselves, but PC-relative is done
910          in the assembler.  Further, the assembler can't handle any
911          of the weirder relocation types.  */
912       if (enc & DW_EH_PE_indirect)
913         ref = dw2_force_const_mem (ref, true);
914
915       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916       output_addr_const (asm_out_file, ref);
917       fputc ('\n', asm_out_file);
918     }
919
920   if (crtl->uses_eh_lsda)
921     {
922       char lab[20];
923
924       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926                                    current_function_funcdef_no);
927       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
929
930       if (enc & DW_EH_PE_indirect)
931         ref = dw2_force_const_mem (ref, true);
932
933       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934       output_addr_const (asm_out_file, ref);
935       fputc ('\n', asm_out_file);
936     }
937 }
938
939 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
940    this allocation may be done before pass_final.  */
941
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
944 {
945   dw_fde_ref fde;
946
947   fde = ggc_alloc_cleared_dw_fde_node ();
948   fde->decl = current_function_decl;
949   fde->funcdef_number = current_function_funcdef_no;
950   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
951   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952   fde->uses_eh_lsda = crtl->uses_eh_lsda;
953   fde->nothrow = crtl->nothrow;
954   fde->drap_reg = INVALID_REGNUM;
955   fde->vdrap_reg = INVALID_REGNUM;
956
957   /* Record the FDE associated with this function.  */
958   cfun->fde = fde;
959   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
960
961   return fde;
962 }
963
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965    the prologue.  */
966
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969                           const char *file ATTRIBUTE_UNUSED)
970 {
971   char label[MAX_ARTIFICIAL_LABEL_BYTES];
972   char * dup_label;
973   dw_fde_ref fde;
974   section *fnsec;
975   bool do_frame;
976
977   current_function_func_begin_label = NULL;
978
979   do_frame = dwarf2out_do_frame ();
980
981   /* ??? current_function_func_begin_label is also used by except.c for
982      call-site information.  We must emit this label if it might be used.  */
983   if (!do_frame
984       && (!flag_exceptions
985           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
986     return;
987
988   fnsec = function_section (current_function_decl);
989   switch_to_section (fnsec);
990   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991                                current_function_funcdef_no);
992   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993                           current_function_funcdef_no);
994   dup_label = xstrdup (label);
995   current_function_func_begin_label = dup_label;
996
997   /* We can elide the fde allocation if we're not emitting debug info.  */
998   if (!do_frame)
999     return;
1000
1001   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1004      do so now.  */
1005   fde = cfun->fde;
1006   if (fde == NULL)
1007     fde = dwarf2out_alloc_current_fde ();
1008
1009   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1010   fde->dw_fde_begin = dup_label;
1011   fde->dw_fde_current_label = dup_label;
1012   fde->in_std_section = (fnsec == text_section
1013                          || (cold_text_section && fnsec == cold_text_section));
1014
1015   /* We only want to output line number information for the genuine dwarf2
1016      prologue case, not the eh frame case.  */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018   if (file)
1019     dwarf2out_source_line (line, file, 0, true);
1020 #endif
1021
1022   if (dwarf2out_do_cfi_asm ())
1023     dwarf2out_do_cfi_startproc (false);
1024   else
1025     {
1026       rtx personality = get_personality_function (current_function_decl);
1027       if (!current_unit_personality)
1028         current_unit_personality = personality;
1029
1030       /* We cannot keep a current personality per function as without CFI
1031          asm, at the point where we emit the CFI data, there is no current
1032          function anymore.  */
1033       if (personality && current_unit_personality != personality)
1034         sorry ("multiple EH personalities are supported only with assemblers "
1035                "supporting .cfi_personality directive");
1036     }
1037 }
1038
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040    for a function prologue.  This gets called *after* the prologue code has
1041    been generated.  */
1042
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045                         const char *file ATTRIBUTE_UNUSED)
1046 {
1047   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1048
1049   /* Output a label to mark the endpoint of the code generated for this
1050      function.  */
1051   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052                                current_function_funcdef_no);
1053   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054                           current_function_funcdef_no);
1055   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1056 }
1057
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059    for a function epilogue.  This gets called *before* the prologue code has
1060    been generated.  */
1061
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064                           const char *file ATTRIBUTE_UNUSED)
1065 {
1066   dw_fde_ref fde = cfun->fde;
1067   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1068
1069   if (fde->dw_fde_vms_begin_epilogue)
1070     return;
1071
1072   /* Output a label to mark the endpoint of the code generated for this
1073      function.  */
1074   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075                                current_function_funcdef_no);
1076   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077                           current_function_funcdef_no);
1078   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1079 }
1080
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082    for a function definition.  This gets called *after* the epilogue code has
1083    been generated.  */
1084
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087                         const char *file ATTRIBUTE_UNUSED)
1088 {
1089   dw_fde_ref fde;
1090   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1091
1092   last_var_location_insn = NULL_RTX;
1093
1094   if (dwarf2out_do_cfi_asm ())
1095     fprintf (asm_out_file, "\t.cfi_endproc\n");
1096
1097   /* Output a label to mark the endpoint of the code generated for this
1098      function.  */
1099   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1100                                current_function_funcdef_no);
1101   ASM_OUTPUT_LABEL (asm_out_file, label);
1102   fde = cfun->fde;
1103   gcc_assert (fde != NULL);
1104   if (fde->dw_fde_second_begin == NULL)
1105     fde->dw_fde_end = xstrdup (label);
1106 }
1107
1108 void
1109 dwarf2out_frame_finish (void)
1110 {
1111   /* Output call frame information.  */
1112   if (targetm.debug_unwind_info () == UI_DWARF2)
1113     output_call_frame_info (0);
1114
1115   /* Output another copy for the unwinder.  */
1116   if ((flag_unwind_tables || flag_exceptions)
1117       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1118     output_call_frame_info (1);
1119 }
1120
1121 /* Note that the current function section is being used for code.  */
1122
1123 static void
1124 dwarf2out_note_section_used (void)
1125 {
1126   section *sec = current_function_section ();
1127   if (sec == text_section)
1128     text_section_used = true;
1129   else if (sec == cold_text_section)
1130     cold_text_section_used = true;
1131 }
1132
1133 static void var_location_switch_text_section (void);
1134 static void set_cur_line_info_table (section *);
1135
1136 void
1137 dwarf2out_switch_text_section (void)
1138 {
1139   section *sect;
1140   dw_fde_ref fde = cfun->fde;
1141
1142   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1143
1144   if (!in_cold_section_p)
1145     {
1146       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1147       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1148       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1149     }
1150   else
1151     {
1152       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1153       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1154       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1155     }
1156   have_multiple_function_sections = true;
1157
1158   /* There is no need to mark used sections when not debugging.  */
1159   if (cold_text_section != NULL)
1160     dwarf2out_note_section_used ();
1161
1162   if (dwarf2out_do_cfi_asm ())
1163     fprintf (asm_out_file, "\t.cfi_endproc\n");
1164
1165   /* Now do the real section switch.  */
1166   sect = current_function_section ();
1167   switch_to_section (sect);
1168
1169   fde->second_in_std_section
1170     = (sect == text_section
1171        || (cold_text_section && sect == cold_text_section));
1172
1173   if (dwarf2out_do_cfi_asm ())
1174     dwarf2out_do_cfi_startproc (true);
1175
1176   var_location_switch_text_section ();
1177
1178   set_cur_line_info_table (sect);
1179 }
1180 \f
1181 /* And now, the subset of the debugging information support code necessary
1182    for emitting location expressions.  */
1183
1184 /* Data about a single source file.  */
1185 struct GTY(()) dwarf_file_data {
1186   const char * filename;
1187   int emitted_number;
1188 };
1189
1190 typedef struct GTY(()) deferred_locations_struct
1191 {
1192   tree variable;
1193   dw_die_ref die;
1194 } deferred_locations;
1195
1196 DEF_VEC_O(deferred_locations);
1197 DEF_VEC_ALLOC_O(deferred_locations,gc);
1198
1199 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1200
1201 DEF_VEC_P(dw_die_ref);
1202 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1203
1204 /* Location lists are ranges + location descriptions for that range,
1205    so you can track variables that are in different places over
1206    their entire life.  */
1207 typedef struct GTY(()) dw_loc_list_struct {
1208   dw_loc_list_ref dw_loc_next;
1209   const char *begin; /* Label for begin address of range */
1210   const char *end;  /* Label for end address of range */
1211   char *ll_symbol; /* Label for beginning of location list.
1212                       Only on head of list */
1213   const char *section; /* Section this loclist is relative to */
1214   dw_loc_descr_ref expr;
1215   hashval_t hash;
1216   /* True if all addresses in this and subsequent lists are known to be
1217      resolved.  */
1218   bool resolved_addr;
1219   /* True if this list has been replaced by dw_loc_next.  */
1220   bool replaced;
1221   bool emitted;
1222   /* True if the range should be emitted even if begin and end
1223      are the same.  */
1224   bool force;
1225 } dw_loc_list_node;
1226
1227 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1228
1229 /* Convert a DWARF stack opcode into its string name.  */
1230
1231 static const char *
1232 dwarf_stack_op_name (unsigned int op)
1233 {
1234   switch (op)
1235     {
1236     case DW_OP_addr:
1237       return "DW_OP_addr";
1238     case DW_OP_deref:
1239       return "DW_OP_deref";
1240     case DW_OP_const1u:
1241       return "DW_OP_const1u";
1242     case DW_OP_const1s:
1243       return "DW_OP_const1s";
1244     case DW_OP_const2u:
1245       return "DW_OP_const2u";
1246     case DW_OP_const2s:
1247       return "DW_OP_const2s";
1248     case DW_OP_const4u:
1249       return "DW_OP_const4u";
1250     case DW_OP_const4s:
1251       return "DW_OP_const4s";
1252     case DW_OP_const8u:
1253       return "DW_OP_const8u";
1254     case DW_OP_const8s:
1255       return "DW_OP_const8s";
1256     case DW_OP_constu:
1257       return "DW_OP_constu";
1258     case DW_OP_consts:
1259       return "DW_OP_consts";
1260     case DW_OP_dup:
1261       return "DW_OP_dup";
1262     case DW_OP_drop:
1263       return "DW_OP_drop";
1264     case DW_OP_over:
1265       return "DW_OP_over";
1266     case DW_OP_pick:
1267       return "DW_OP_pick";
1268     case DW_OP_swap:
1269       return "DW_OP_swap";
1270     case DW_OP_rot:
1271       return "DW_OP_rot";
1272     case DW_OP_xderef:
1273       return "DW_OP_xderef";
1274     case DW_OP_abs:
1275       return "DW_OP_abs";
1276     case DW_OP_and:
1277       return "DW_OP_and";
1278     case DW_OP_div:
1279       return "DW_OP_div";
1280     case DW_OP_minus:
1281       return "DW_OP_minus";
1282     case DW_OP_mod:
1283       return "DW_OP_mod";
1284     case DW_OP_mul:
1285       return "DW_OP_mul";
1286     case DW_OP_neg:
1287       return "DW_OP_neg";
1288     case DW_OP_not:
1289       return "DW_OP_not";
1290     case DW_OP_or:
1291       return "DW_OP_or";
1292     case DW_OP_plus:
1293       return "DW_OP_plus";
1294     case DW_OP_plus_uconst:
1295       return "DW_OP_plus_uconst";
1296     case DW_OP_shl:
1297       return "DW_OP_shl";
1298     case DW_OP_shr:
1299       return "DW_OP_shr";
1300     case DW_OP_shra:
1301       return "DW_OP_shra";
1302     case DW_OP_xor:
1303       return "DW_OP_xor";
1304     case DW_OP_bra:
1305       return "DW_OP_bra";
1306     case DW_OP_eq:
1307       return "DW_OP_eq";
1308     case DW_OP_ge:
1309       return "DW_OP_ge";
1310     case DW_OP_gt:
1311       return "DW_OP_gt";
1312     case DW_OP_le:
1313       return "DW_OP_le";
1314     case DW_OP_lt:
1315       return "DW_OP_lt";
1316     case DW_OP_ne:
1317       return "DW_OP_ne";
1318     case DW_OP_skip:
1319       return "DW_OP_skip";
1320     case DW_OP_lit0:
1321       return "DW_OP_lit0";
1322     case DW_OP_lit1:
1323       return "DW_OP_lit1";
1324     case DW_OP_lit2:
1325       return "DW_OP_lit2";
1326     case DW_OP_lit3:
1327       return "DW_OP_lit3";
1328     case DW_OP_lit4:
1329       return "DW_OP_lit4";
1330     case DW_OP_lit5:
1331       return "DW_OP_lit5";
1332     case DW_OP_lit6:
1333       return "DW_OP_lit6";
1334     case DW_OP_lit7:
1335       return "DW_OP_lit7";
1336     case DW_OP_lit8:
1337       return "DW_OP_lit8";
1338     case DW_OP_lit9:
1339       return "DW_OP_lit9";
1340     case DW_OP_lit10:
1341       return "DW_OP_lit10";
1342     case DW_OP_lit11:
1343       return "DW_OP_lit11";
1344     case DW_OP_lit12:
1345       return "DW_OP_lit12";
1346     case DW_OP_lit13:
1347       return "DW_OP_lit13";
1348     case DW_OP_lit14:
1349       return "DW_OP_lit14";
1350     case DW_OP_lit15:
1351       return "DW_OP_lit15";
1352     case DW_OP_lit16:
1353       return "DW_OP_lit16";
1354     case DW_OP_lit17:
1355       return "DW_OP_lit17";
1356     case DW_OP_lit18:
1357       return "DW_OP_lit18";
1358     case DW_OP_lit19:
1359       return "DW_OP_lit19";
1360     case DW_OP_lit20:
1361       return "DW_OP_lit20";
1362     case DW_OP_lit21:
1363       return "DW_OP_lit21";
1364     case DW_OP_lit22:
1365       return "DW_OP_lit22";
1366     case DW_OP_lit23:
1367       return "DW_OP_lit23";
1368     case DW_OP_lit24:
1369       return "DW_OP_lit24";
1370     case DW_OP_lit25:
1371       return "DW_OP_lit25";
1372     case DW_OP_lit26:
1373       return "DW_OP_lit26";
1374     case DW_OP_lit27:
1375       return "DW_OP_lit27";
1376     case DW_OP_lit28:
1377       return "DW_OP_lit28";
1378     case DW_OP_lit29:
1379       return "DW_OP_lit29";
1380     case DW_OP_lit30:
1381       return "DW_OP_lit30";
1382     case DW_OP_lit31:
1383       return "DW_OP_lit31";
1384     case DW_OP_reg0:
1385       return "DW_OP_reg0";
1386     case DW_OP_reg1:
1387       return "DW_OP_reg1";
1388     case DW_OP_reg2:
1389       return "DW_OP_reg2";
1390     case DW_OP_reg3:
1391       return "DW_OP_reg3";
1392     case DW_OP_reg4:
1393       return "DW_OP_reg4";
1394     case DW_OP_reg5:
1395       return "DW_OP_reg5";
1396     case DW_OP_reg6:
1397       return "DW_OP_reg6";
1398     case DW_OP_reg7:
1399       return "DW_OP_reg7";
1400     case DW_OP_reg8:
1401       return "DW_OP_reg8";
1402     case DW_OP_reg9:
1403       return "DW_OP_reg9";
1404     case DW_OP_reg10:
1405       return "DW_OP_reg10";
1406     case DW_OP_reg11:
1407       return "DW_OP_reg11";
1408     case DW_OP_reg12:
1409       return "DW_OP_reg12";
1410     case DW_OP_reg13:
1411       return "DW_OP_reg13";
1412     case DW_OP_reg14:
1413       return "DW_OP_reg14";
1414     case DW_OP_reg15:
1415       return "DW_OP_reg15";
1416     case DW_OP_reg16:
1417       return "DW_OP_reg16";
1418     case DW_OP_reg17:
1419       return "DW_OP_reg17";
1420     case DW_OP_reg18:
1421       return "DW_OP_reg18";
1422     case DW_OP_reg19:
1423       return "DW_OP_reg19";
1424     case DW_OP_reg20:
1425       return "DW_OP_reg20";
1426     case DW_OP_reg21:
1427       return "DW_OP_reg21";
1428     case DW_OP_reg22:
1429       return "DW_OP_reg22";
1430     case DW_OP_reg23:
1431       return "DW_OP_reg23";
1432     case DW_OP_reg24:
1433       return "DW_OP_reg24";
1434     case DW_OP_reg25:
1435       return "DW_OP_reg25";
1436     case DW_OP_reg26:
1437       return "DW_OP_reg26";
1438     case DW_OP_reg27:
1439       return "DW_OP_reg27";
1440     case DW_OP_reg28:
1441       return "DW_OP_reg28";
1442     case DW_OP_reg29:
1443       return "DW_OP_reg29";
1444     case DW_OP_reg30:
1445       return "DW_OP_reg30";
1446     case DW_OP_reg31:
1447       return "DW_OP_reg31";
1448     case DW_OP_breg0:
1449       return "DW_OP_breg0";
1450     case DW_OP_breg1:
1451       return "DW_OP_breg1";
1452     case DW_OP_breg2:
1453       return "DW_OP_breg2";
1454     case DW_OP_breg3:
1455       return "DW_OP_breg3";
1456     case DW_OP_breg4:
1457       return "DW_OP_breg4";
1458     case DW_OP_breg5:
1459       return "DW_OP_breg5";
1460     case DW_OP_breg6:
1461       return "DW_OP_breg6";
1462     case DW_OP_breg7:
1463       return "DW_OP_breg7";
1464     case DW_OP_breg8:
1465       return "DW_OP_breg8";
1466     case DW_OP_breg9:
1467       return "DW_OP_breg9";
1468     case DW_OP_breg10:
1469       return "DW_OP_breg10";
1470     case DW_OP_breg11:
1471       return "DW_OP_breg11";
1472     case DW_OP_breg12:
1473       return "DW_OP_breg12";
1474     case DW_OP_breg13:
1475       return "DW_OP_breg13";
1476     case DW_OP_breg14:
1477       return "DW_OP_breg14";
1478     case DW_OP_breg15:
1479       return "DW_OP_breg15";
1480     case DW_OP_breg16:
1481       return "DW_OP_breg16";
1482     case DW_OP_breg17:
1483       return "DW_OP_breg17";
1484     case DW_OP_breg18:
1485       return "DW_OP_breg18";
1486     case DW_OP_breg19:
1487       return "DW_OP_breg19";
1488     case DW_OP_breg20:
1489       return "DW_OP_breg20";
1490     case DW_OP_breg21:
1491       return "DW_OP_breg21";
1492     case DW_OP_breg22:
1493       return "DW_OP_breg22";
1494     case DW_OP_breg23:
1495       return "DW_OP_breg23";
1496     case DW_OP_breg24:
1497       return "DW_OP_breg24";
1498     case DW_OP_breg25:
1499       return "DW_OP_breg25";
1500     case DW_OP_breg26:
1501       return "DW_OP_breg26";
1502     case DW_OP_breg27:
1503       return "DW_OP_breg27";
1504     case DW_OP_breg28:
1505       return "DW_OP_breg28";
1506     case DW_OP_breg29:
1507       return "DW_OP_breg29";
1508     case DW_OP_breg30:
1509       return "DW_OP_breg30";
1510     case DW_OP_breg31:
1511       return "DW_OP_breg31";
1512     case DW_OP_regx:
1513       return "DW_OP_regx";
1514     case DW_OP_fbreg:
1515       return "DW_OP_fbreg";
1516     case DW_OP_bregx:
1517       return "DW_OP_bregx";
1518     case DW_OP_piece:
1519       return "DW_OP_piece";
1520     case DW_OP_deref_size:
1521       return "DW_OP_deref_size";
1522     case DW_OP_xderef_size:
1523       return "DW_OP_xderef_size";
1524     case DW_OP_nop:
1525       return "DW_OP_nop";
1526
1527     case DW_OP_push_object_address:
1528       return "DW_OP_push_object_address";
1529     case DW_OP_call2:
1530       return "DW_OP_call2";
1531     case DW_OP_call4:
1532       return "DW_OP_call4";
1533     case DW_OP_call_ref:
1534       return "DW_OP_call_ref";
1535     case DW_OP_implicit_value:
1536       return "DW_OP_implicit_value";
1537     case DW_OP_stack_value:
1538       return "DW_OP_stack_value";
1539     case DW_OP_form_tls_address:
1540       return "DW_OP_form_tls_address";
1541     case DW_OP_call_frame_cfa:
1542       return "DW_OP_call_frame_cfa";
1543     case DW_OP_bit_piece:
1544       return "DW_OP_bit_piece";
1545
1546     case DW_OP_GNU_push_tls_address:
1547       return "DW_OP_GNU_push_tls_address";
1548     case DW_OP_GNU_uninit:
1549       return "DW_OP_GNU_uninit";
1550     case DW_OP_GNU_encoded_addr:
1551       return "DW_OP_GNU_encoded_addr";
1552     case DW_OP_GNU_implicit_pointer:
1553       return "DW_OP_GNU_implicit_pointer";
1554     case DW_OP_GNU_entry_value:
1555       return "DW_OP_GNU_entry_value";
1556     case DW_OP_GNU_const_type:
1557       return "DW_OP_GNU_const_type";
1558     case DW_OP_GNU_regval_type:
1559       return "DW_OP_GNU_regval_type";
1560     case DW_OP_GNU_deref_type:
1561       return "DW_OP_GNU_deref_type";
1562     case DW_OP_GNU_convert:
1563       return "DW_OP_GNU_convert";
1564     case DW_OP_GNU_reinterpret:
1565       return "DW_OP_GNU_reinterpret";
1566     case DW_OP_GNU_parameter_ref:
1567       return "DW_OP_GNU_parameter_ref";
1568
1569     default:
1570       return "OP_<unknown>";
1571     }
1572 }
1573
1574 /* Return a pointer to a newly allocated location description.  Location
1575    descriptions are simple expression terms that can be strung
1576    together to form more complicated location (address) descriptions.  */
1577
1578 static inline dw_loc_descr_ref
1579 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1580                unsigned HOST_WIDE_INT oprnd2)
1581 {
1582   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1583
1584   descr->dw_loc_opc = op;
1585   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1586   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1587   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1588   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1589
1590   return descr;
1591 }
1592
1593 /* Return a pointer to a newly allocated location description for
1594    REG and OFFSET.  */
1595
1596 static inline dw_loc_descr_ref
1597 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1598 {
1599   if (reg <= 31)
1600     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1601                           offset, 0);
1602   else
1603     return new_loc_descr (DW_OP_bregx, reg, offset);
1604 }
1605
1606 /* Add a location description term to a location description expression.  */
1607
1608 static inline void
1609 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1610 {
1611   dw_loc_descr_ref *d;
1612
1613   /* Find the end of the chain.  */
1614   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1615     ;
1616
1617   *d = descr;
1618 }
1619
1620 /* Compare two location operands for exact equality.  */
1621
1622 static bool
1623 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1624 {
1625   if (a->val_class != b->val_class)
1626     return false;
1627   switch (a->val_class)
1628     {
1629     case dw_val_class_none:
1630       return true;
1631     case dw_val_class_addr:
1632       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1633
1634     case dw_val_class_offset:
1635     case dw_val_class_unsigned_const:
1636     case dw_val_class_const:
1637     case dw_val_class_range_list:
1638     case dw_val_class_lineptr:
1639     case dw_val_class_macptr:
1640       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1641       return a->v.val_unsigned == b->v.val_unsigned;
1642
1643     case dw_val_class_loc:
1644       return a->v.val_loc == b->v.val_loc;
1645     case dw_val_class_loc_list:
1646       return a->v.val_loc_list == b->v.val_loc_list;
1647     case dw_val_class_die_ref:
1648       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1649     case dw_val_class_fde_ref:
1650       return a->v.val_fde_index == b->v.val_fde_index;
1651     case dw_val_class_lbl_id:
1652       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1653     case dw_val_class_str:
1654       return a->v.val_str == b->v.val_str;
1655     case dw_val_class_flag:
1656       return a->v.val_flag == b->v.val_flag;
1657     case dw_val_class_file:
1658       return a->v.val_file == b->v.val_file;
1659     case dw_val_class_decl_ref:
1660       return a->v.val_decl_ref == b->v.val_decl_ref;
1661     
1662     case dw_val_class_const_double:
1663       return (a->v.val_double.high == b->v.val_double.high
1664               && a->v.val_double.low == b->v.val_double.low);
1665
1666     case dw_val_class_vec:
1667       {
1668         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1669         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1670
1671         return (a_len == b_len
1672                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1673       }
1674
1675     case dw_val_class_data8:
1676       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1677
1678     case dw_val_class_vms_delta:
1679       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1680               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1681     }
1682   gcc_unreachable ();
1683 }
1684
1685 /* Compare two location atoms for exact equality.  */
1686
1687 static bool
1688 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1689 {
1690   if (a->dw_loc_opc != b->dw_loc_opc)
1691     return false;
1692
1693   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1694      address size, but since we always allocate cleared storage it
1695      should be zero for other types of locations.  */
1696   if (a->dtprel != b->dtprel)
1697     return false;
1698
1699   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1700           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1701 }
1702
1703 /* Compare two complete location expressions for exact equality.  */
1704
1705 bool
1706 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1707 {
1708   while (1)
1709     {
1710       if (a == b)
1711         return true;
1712       if (a == NULL || b == NULL)
1713         return false;
1714       if (!loc_descr_equal_p_1 (a, b))
1715         return false;
1716
1717       a = a->dw_loc_next;
1718       b = b->dw_loc_next;
1719     }
1720 }
1721
1722
1723 /* Add a constant OFFSET to a location expression.  */
1724
1725 static void
1726 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1727 {
1728   dw_loc_descr_ref loc;
1729   HOST_WIDE_INT *p;
1730
1731   gcc_assert (*list_head != NULL);
1732
1733   if (!offset)
1734     return;
1735
1736   /* Find the end of the chain.  */
1737   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1738     ;
1739
1740   p = NULL;
1741   if (loc->dw_loc_opc == DW_OP_fbreg
1742       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1743     p = &loc->dw_loc_oprnd1.v.val_int;
1744   else if (loc->dw_loc_opc == DW_OP_bregx)
1745     p = &loc->dw_loc_oprnd2.v.val_int;
1746
1747   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1748      offset.  Don't optimize if an signed integer overflow would happen.  */
1749   if (p != NULL
1750       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1751           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1752     *p += offset;
1753
1754   else if (offset > 0)
1755     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1756
1757   else
1758     {
1759       loc->dw_loc_next = int_loc_descriptor (-offset);
1760       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1761     }
1762 }
1763
1764 /* Add a constant OFFSET to a location list.  */
1765
1766 static void
1767 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1768 {
1769   dw_loc_list_ref d;
1770   for (d = list_head; d != NULL; d = d->dw_loc_next)
1771     loc_descr_plus_const (&d->expr, offset);
1772 }
1773
1774 #define DWARF_REF_SIZE  \
1775   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1776
1777 static unsigned long int get_base_type_offset (dw_die_ref);
1778
1779 /* Return the size of a location descriptor.  */
1780
1781 static unsigned long
1782 size_of_loc_descr (dw_loc_descr_ref loc)
1783 {
1784   unsigned long size = 1;
1785
1786   switch (loc->dw_loc_opc)
1787     {
1788     case DW_OP_addr:
1789       size += DWARF2_ADDR_SIZE;
1790       break;
1791     case DW_OP_const1u:
1792     case DW_OP_const1s:
1793       size += 1;
1794       break;
1795     case DW_OP_const2u:
1796     case DW_OP_const2s:
1797       size += 2;
1798       break;
1799     case DW_OP_const4u:
1800     case DW_OP_const4s:
1801       size += 4;
1802       break;
1803     case DW_OP_const8u:
1804     case DW_OP_const8s:
1805       size += 8;
1806       break;
1807     case DW_OP_constu:
1808       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1809       break;
1810     case DW_OP_consts:
1811       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1812       break;
1813     case DW_OP_pick:
1814       size += 1;
1815       break;
1816     case DW_OP_plus_uconst:
1817       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1818       break;
1819     case DW_OP_skip:
1820     case DW_OP_bra:
1821       size += 2;
1822       break;
1823     case DW_OP_breg0:
1824     case DW_OP_breg1:
1825     case DW_OP_breg2:
1826     case DW_OP_breg3:
1827     case DW_OP_breg4:
1828     case DW_OP_breg5:
1829     case DW_OP_breg6:
1830     case DW_OP_breg7:
1831     case DW_OP_breg8:
1832     case DW_OP_breg9:
1833     case DW_OP_breg10:
1834     case DW_OP_breg11:
1835     case DW_OP_breg12:
1836     case DW_OP_breg13:
1837     case DW_OP_breg14:
1838     case DW_OP_breg15:
1839     case DW_OP_breg16:
1840     case DW_OP_breg17:
1841     case DW_OP_breg18:
1842     case DW_OP_breg19:
1843     case DW_OP_breg20:
1844     case DW_OP_breg21:
1845     case DW_OP_breg22:
1846     case DW_OP_breg23:
1847     case DW_OP_breg24:
1848     case DW_OP_breg25:
1849     case DW_OP_breg26:
1850     case DW_OP_breg27:
1851     case DW_OP_breg28:
1852     case DW_OP_breg29:
1853     case DW_OP_breg30:
1854     case DW_OP_breg31:
1855       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1856       break;
1857     case DW_OP_regx:
1858       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859       break;
1860     case DW_OP_fbreg:
1861       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1862       break;
1863     case DW_OP_bregx:
1864       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1865       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1866       break;
1867     case DW_OP_piece:
1868       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1869       break;
1870     case DW_OP_bit_piece:
1871       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1872       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1873       break;
1874     case DW_OP_deref_size:
1875     case DW_OP_xderef_size:
1876       size += 1;
1877       break;
1878     case DW_OP_call2:
1879       size += 2;
1880       break;
1881     case DW_OP_call4:
1882       size += 4;
1883       break;
1884     case DW_OP_call_ref:
1885       size += DWARF_REF_SIZE;
1886       break;
1887     case DW_OP_implicit_value:
1888       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1889               + loc->dw_loc_oprnd1.v.val_unsigned;
1890       break;
1891     case DW_OP_GNU_implicit_pointer:
1892       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1893       break;
1894     case DW_OP_GNU_entry_value:
1895       {
1896         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1897         size += size_of_uleb128 (op_size) + op_size;
1898         break;
1899       }
1900     case DW_OP_GNU_const_type:
1901       {
1902         unsigned long o
1903           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1904         size += size_of_uleb128 (o) + 1;
1905         switch (loc->dw_loc_oprnd2.val_class)
1906           {
1907           case dw_val_class_vec:
1908             size += loc->dw_loc_oprnd2.v.val_vec.length
1909                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1910             break;
1911           case dw_val_class_const:
1912             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1913             break;
1914           case dw_val_class_const_double:
1915             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1916             break;
1917           default:
1918             gcc_unreachable ();
1919           }
1920         break;
1921       }
1922     case DW_OP_GNU_regval_type:
1923       {
1924         unsigned long o
1925           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1926         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1927                 + size_of_uleb128 (o);
1928       }
1929       break;
1930     case DW_OP_GNU_deref_type:
1931       {
1932         unsigned long o
1933           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1934         size += 1 + size_of_uleb128 (o);
1935       }
1936       break;
1937     case DW_OP_GNU_convert:
1938     case DW_OP_GNU_reinterpret:
1939       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1940         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1941       else
1942         {
1943           unsigned long o
1944             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1945           size += size_of_uleb128 (o);
1946         }
1947       break;
1948     case DW_OP_GNU_parameter_ref:
1949       size += 4;
1950       break;
1951     default:
1952       break;
1953     }
1954
1955   return size;
1956 }
1957
1958 /* Return the size of a series of location descriptors.  */
1959
1960 unsigned long
1961 size_of_locs (dw_loc_descr_ref loc)
1962 {
1963   dw_loc_descr_ref l;
1964   unsigned long size;
1965
1966   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1967      field, to avoid writing to a PCH file.  */
1968   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1969     {
1970       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1971         break;
1972       size += size_of_loc_descr (l);
1973     }
1974   if (! l)
1975     return size;
1976
1977   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1978     {
1979       l->dw_loc_addr = size;
1980       size += size_of_loc_descr (l);
1981     }
1982
1983   return size;
1984 }
1985
1986 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1987 static void get_ref_die_offset_label (char *, dw_die_ref);
1988 static unsigned long int get_ref_die_offset (dw_die_ref);
1989
1990 /* Output location description stack opcode's operands (if any).
1991    The for_eh_or_skip parameter controls whether register numbers are
1992    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1993    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1994    info).  This should be suppressed for the cases that have not been converted
1995    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1996
1997 static void
1998 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1999 {
2000   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2001   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2002
2003   switch (loc->dw_loc_opc)
2004     {
2005 #ifdef DWARF2_DEBUGGING_INFO
2006     case DW_OP_const2u:
2007     case DW_OP_const2s:
2008       dw2_asm_output_data (2, val1->v.val_int, NULL);
2009       break;
2010     case DW_OP_const4u:
2011       if (loc->dtprel)
2012         {
2013           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2014           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2015                                                val1->v.val_addr);
2016           fputc ('\n', asm_out_file);
2017           break;
2018         }
2019       /* FALLTHRU */
2020     case DW_OP_const4s:
2021       dw2_asm_output_data (4, val1->v.val_int, NULL);
2022       break;
2023     case DW_OP_const8u:
2024       if (loc->dtprel)
2025         {
2026           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2027           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2028                                                val1->v.val_addr);
2029           fputc ('\n', asm_out_file);
2030           break;
2031         }
2032       /* FALLTHRU */
2033     case DW_OP_const8s:
2034       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2035       dw2_asm_output_data (8, val1->v.val_int, NULL);
2036       break;
2037     case DW_OP_skip:
2038     case DW_OP_bra:
2039       {
2040         int offset;
2041
2042         gcc_assert (val1->val_class == dw_val_class_loc);
2043         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2044
2045         dw2_asm_output_data (2, offset, NULL);
2046       }
2047       break;
2048     case DW_OP_implicit_value:
2049       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2050       switch (val2->val_class)
2051         {
2052         case dw_val_class_const:
2053           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2054           break;
2055         case dw_val_class_vec:
2056           {
2057             unsigned int elt_size = val2->v.val_vec.elt_size;
2058             unsigned int len = val2->v.val_vec.length;
2059             unsigned int i;
2060             unsigned char *p;
2061
2062             if (elt_size > sizeof (HOST_WIDE_INT))
2063               {
2064                 elt_size /= 2;
2065                 len *= 2;
2066               }
2067             for (i = 0, p = val2->v.val_vec.array;
2068                  i < len;
2069                  i++, p += elt_size)
2070               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2071                                    "fp or vector constant word %u", i);
2072           }
2073           break;
2074         case dw_val_class_const_double:
2075           {
2076             unsigned HOST_WIDE_INT first, second;
2077
2078             if (WORDS_BIG_ENDIAN)
2079               {
2080                 first = val2->v.val_double.high;
2081                 second = val2->v.val_double.low;
2082               }
2083             else
2084               {
2085                 first = val2->v.val_double.low;
2086                 second = val2->v.val_double.high;
2087               }
2088             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2089                                  first, NULL);
2090             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2091                                  second, NULL);
2092           }
2093           break;
2094         case dw_val_class_addr:
2095           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2096           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2097           break;
2098         default:
2099           gcc_unreachable ();
2100         }
2101       break;
2102 #else
2103     case DW_OP_const2u:
2104     case DW_OP_const2s:
2105     case DW_OP_const4u:
2106     case DW_OP_const4s:
2107     case DW_OP_const8u:
2108     case DW_OP_const8s:
2109     case DW_OP_skip:
2110     case DW_OP_bra:
2111     case DW_OP_implicit_value:
2112       /* We currently don't make any attempt to make sure these are
2113          aligned properly like we do for the main unwind info, so
2114          don't support emitting things larger than a byte if we're
2115          only doing unwinding.  */
2116       gcc_unreachable ();
2117 #endif
2118     case DW_OP_const1u:
2119     case DW_OP_const1s:
2120       dw2_asm_output_data (1, val1->v.val_int, NULL);
2121       break;
2122     case DW_OP_constu:
2123       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2124       break;
2125     case DW_OP_consts:
2126       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2127       break;
2128     case DW_OP_pick:
2129       dw2_asm_output_data (1, val1->v.val_int, NULL);
2130       break;
2131     case DW_OP_plus_uconst:
2132       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2133       break;
2134     case DW_OP_breg0:
2135     case DW_OP_breg1:
2136     case DW_OP_breg2:
2137     case DW_OP_breg3:
2138     case DW_OP_breg4:
2139     case DW_OP_breg5:
2140     case DW_OP_breg6:
2141     case DW_OP_breg7:
2142     case DW_OP_breg8:
2143     case DW_OP_breg9:
2144     case DW_OP_breg10:
2145     case DW_OP_breg11:
2146     case DW_OP_breg12:
2147     case DW_OP_breg13:
2148     case DW_OP_breg14:
2149     case DW_OP_breg15:
2150     case DW_OP_breg16:
2151     case DW_OP_breg17:
2152     case DW_OP_breg18:
2153     case DW_OP_breg19:
2154     case DW_OP_breg20:
2155     case DW_OP_breg21:
2156     case DW_OP_breg22:
2157     case DW_OP_breg23:
2158     case DW_OP_breg24:
2159     case DW_OP_breg25:
2160     case DW_OP_breg26:
2161     case DW_OP_breg27:
2162     case DW_OP_breg28:
2163     case DW_OP_breg29:
2164     case DW_OP_breg30:
2165     case DW_OP_breg31:
2166       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2167       break;
2168     case DW_OP_regx:
2169       {
2170         unsigned r = val1->v.val_unsigned;
2171         if (for_eh_or_skip >= 0)
2172           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2173         gcc_assert (size_of_uleb128 (r) 
2174                     == size_of_uleb128 (val1->v.val_unsigned));
2175         dw2_asm_output_data_uleb128 (r, NULL);  
2176       }
2177       break;
2178     case DW_OP_fbreg:
2179       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2180       break;
2181     case DW_OP_bregx:
2182       {
2183         unsigned r = val1->v.val_unsigned;
2184         if (for_eh_or_skip >= 0)
2185           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2186         gcc_assert (size_of_uleb128 (r) 
2187                     == size_of_uleb128 (val1->v.val_unsigned));
2188         dw2_asm_output_data_uleb128 (r, NULL);  
2189         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2190       }
2191       break;
2192     case DW_OP_piece:
2193       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2194       break;
2195     case DW_OP_bit_piece:
2196       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2197       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2198       break;
2199     case DW_OP_deref_size:
2200     case DW_OP_xderef_size:
2201       dw2_asm_output_data (1, val1->v.val_int, NULL);
2202       break;
2203
2204     case DW_OP_addr:
2205       if (loc->dtprel)
2206         {
2207           if (targetm.asm_out.output_dwarf_dtprel)
2208             {
2209               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2210                                                    DWARF2_ADDR_SIZE,
2211                                                    val1->v.val_addr);
2212               fputc ('\n', asm_out_file);
2213             }
2214           else
2215             gcc_unreachable ();
2216         }
2217       else
2218         {
2219 #ifdef DWARF2_DEBUGGING_INFO
2220           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2221 #else
2222           gcc_unreachable ();
2223 #endif
2224         }
2225       break;
2226
2227     case DW_OP_GNU_implicit_pointer:
2228       {
2229         char label[MAX_ARTIFICIAL_LABEL_BYTES
2230                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2231         gcc_assert (val1->val_class == dw_val_class_die_ref);
2232         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2233         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2234         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2235       }
2236       break;
2237
2238     case DW_OP_GNU_entry_value:
2239       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2240       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2241       break;
2242
2243     case DW_OP_GNU_const_type:
2244       {
2245         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2246         gcc_assert (o);
2247         dw2_asm_output_data_uleb128 (o, NULL);
2248         switch (val2->val_class)
2249           {
2250           case dw_val_class_const:
2251             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2252             dw2_asm_output_data (1, l, NULL);
2253             dw2_asm_output_data (l, val2->v.val_int, NULL);
2254             break;
2255           case dw_val_class_vec:
2256             {
2257               unsigned int elt_size = val2->v.val_vec.elt_size;
2258               unsigned int len = val2->v.val_vec.length;
2259               unsigned int i;
2260               unsigned char *p;
2261
2262               l = len * elt_size;
2263               dw2_asm_output_data (1, l, NULL);
2264               if (elt_size > sizeof (HOST_WIDE_INT))
2265                 {
2266                   elt_size /= 2;
2267                   len *= 2;
2268                 }
2269               for (i = 0, p = val2->v.val_vec.array;
2270                    i < len;
2271                    i++, p += elt_size)
2272                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2273                                      "fp or vector constant word %u", i);
2274             }
2275             break;
2276           case dw_val_class_const_double:
2277             {
2278               unsigned HOST_WIDE_INT first, second;
2279               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2280
2281               dw2_asm_output_data (1, 2 * l, NULL);
2282               if (WORDS_BIG_ENDIAN)
2283                 {
2284                   first = val2->v.val_double.high;
2285                   second = val2->v.val_double.low;
2286                 }
2287               else
2288                 {
2289                   first = val2->v.val_double.low;
2290                   second = val2->v.val_double.high;
2291                 }
2292               dw2_asm_output_data (l, first, NULL);
2293               dw2_asm_output_data (l, second, NULL);
2294             }
2295             break;
2296           default:
2297             gcc_unreachable ();
2298           }
2299       }
2300       break;
2301     case DW_OP_GNU_regval_type:
2302       {
2303         unsigned r = val1->v.val_unsigned;
2304         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2305         gcc_assert (o);
2306         if (for_eh_or_skip >= 0)
2307           {
2308             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2309             gcc_assert (size_of_uleb128 (r)
2310                         == size_of_uleb128 (val1->v.val_unsigned));
2311           }
2312         dw2_asm_output_data_uleb128 (r, NULL);
2313         dw2_asm_output_data_uleb128 (o, NULL);
2314       }
2315       break;
2316     case DW_OP_GNU_deref_type:
2317       {
2318         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2319         gcc_assert (o);
2320         dw2_asm_output_data (1, val1->v.val_int, NULL);
2321         dw2_asm_output_data_uleb128 (o, NULL);
2322       }
2323       break;
2324     case DW_OP_GNU_convert:
2325     case DW_OP_GNU_reinterpret:
2326       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2327         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2328       else
2329         {
2330           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2331           gcc_assert (o);
2332           dw2_asm_output_data_uleb128 (o, NULL);
2333         }
2334       break;
2335
2336     case DW_OP_GNU_parameter_ref:
2337       {
2338         unsigned long o;
2339         gcc_assert (val1->val_class == dw_val_class_die_ref);
2340         o = get_ref_die_offset (val1->v.val_die_ref.die);
2341         dw2_asm_output_data (4, o, NULL);
2342       }
2343       break;
2344
2345     default:
2346       /* Other codes have no operands.  */
2347       break;
2348     }
2349 }
2350
2351 /* Output a sequence of location operations.  
2352    The for_eh_or_skip parameter controls whether register numbers are
2353    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2354    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2355    info).  This should be suppressed for the cases that have not been converted
2356    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2357
2358 void
2359 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2360 {
2361   for (; loc != NULL; loc = loc->dw_loc_next)
2362     {
2363       enum dwarf_location_atom opc = loc->dw_loc_opc;
2364       /* Output the opcode.  */
2365       if (for_eh_or_skip >= 0 
2366           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2367         {
2368           unsigned r = (opc - DW_OP_breg0);
2369           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2370           gcc_assert (r <= 31);
2371           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2372         }
2373       else if (for_eh_or_skip >= 0 
2374                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2375         {
2376           unsigned r = (opc - DW_OP_reg0);
2377           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2378           gcc_assert (r <= 31);
2379           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2380         }
2381
2382       dw2_asm_output_data (1, opc,
2383                              "%s", dwarf_stack_op_name (opc));
2384
2385       /* Output the operand(s) (if any).  */
2386       output_loc_operands (loc, for_eh_or_skip);
2387     }
2388 }
2389
2390 /* Output location description stack opcode's operands (if any).
2391    The output is single bytes on a line, suitable for .cfi_escape.  */
2392
2393 static void
2394 output_loc_operands_raw (dw_loc_descr_ref loc)
2395 {
2396   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2397   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2398
2399   switch (loc->dw_loc_opc)
2400     {
2401     case DW_OP_addr:
2402     case DW_OP_implicit_value:
2403       /* We cannot output addresses in .cfi_escape, only bytes.  */
2404       gcc_unreachable ();
2405
2406     case DW_OP_const1u:
2407     case DW_OP_const1s:
2408     case DW_OP_pick:
2409     case DW_OP_deref_size:
2410     case DW_OP_xderef_size:
2411       fputc (',', asm_out_file);
2412       dw2_asm_output_data_raw (1, val1->v.val_int);
2413       break;
2414
2415     case DW_OP_const2u:
2416     case DW_OP_const2s:
2417       fputc (',', asm_out_file);
2418       dw2_asm_output_data_raw (2, val1->v.val_int);
2419       break;
2420
2421     case DW_OP_const4u:
2422     case DW_OP_const4s:
2423       fputc (',', asm_out_file);
2424       dw2_asm_output_data_raw (4, val1->v.val_int);
2425       break;
2426
2427     case DW_OP_const8u:
2428     case DW_OP_const8s:
2429       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2430       fputc (',', asm_out_file);
2431       dw2_asm_output_data_raw (8, val1->v.val_int);
2432       break;
2433
2434     case DW_OP_skip:
2435     case DW_OP_bra:
2436       {
2437         int offset;
2438
2439         gcc_assert (val1->val_class == dw_val_class_loc);
2440         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2441
2442         fputc (',', asm_out_file);
2443         dw2_asm_output_data_raw (2, offset);
2444       }
2445       break;
2446
2447     case DW_OP_regx:
2448       {
2449         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2450         gcc_assert (size_of_uleb128 (r) 
2451                     == size_of_uleb128 (val1->v.val_unsigned));
2452         fputc (',', asm_out_file);
2453         dw2_asm_output_data_uleb128_raw (r);
2454       }
2455       break;
2456       
2457     case DW_OP_constu:
2458     case DW_OP_plus_uconst:
2459     case DW_OP_piece:
2460       fputc (',', asm_out_file);
2461       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2462       break;
2463
2464     case DW_OP_bit_piece:
2465       fputc (',', asm_out_file);
2466       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2467       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2468       break;
2469
2470     case DW_OP_consts:
2471     case DW_OP_breg0:
2472     case DW_OP_breg1:
2473     case DW_OP_breg2:
2474     case DW_OP_breg3:
2475     case DW_OP_breg4:
2476     case DW_OP_breg5:
2477     case DW_OP_breg6:
2478     case DW_OP_breg7:
2479     case DW_OP_breg8:
2480     case DW_OP_breg9:
2481     case DW_OP_breg10:
2482     case DW_OP_breg11:
2483     case DW_OP_breg12:
2484     case DW_OP_breg13:
2485     case DW_OP_breg14:
2486     case DW_OP_breg15:
2487     case DW_OP_breg16:
2488     case DW_OP_breg17:
2489     case DW_OP_breg18:
2490     case DW_OP_breg19:
2491     case DW_OP_breg20:
2492     case DW_OP_breg21:
2493     case DW_OP_breg22:
2494     case DW_OP_breg23:
2495     case DW_OP_breg24:
2496     case DW_OP_breg25:
2497     case DW_OP_breg26:
2498     case DW_OP_breg27:
2499     case DW_OP_breg28:
2500     case DW_OP_breg29:
2501     case DW_OP_breg30:
2502     case DW_OP_breg31:
2503     case DW_OP_fbreg:
2504       fputc (',', asm_out_file);
2505       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2506       break;
2507
2508     case DW_OP_bregx:
2509       {
2510         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2511         gcc_assert (size_of_uleb128 (r) 
2512                     == size_of_uleb128 (val1->v.val_unsigned));
2513         fputc (',', asm_out_file);
2514         dw2_asm_output_data_uleb128_raw (r);
2515         fputc (',', asm_out_file);
2516         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2517       }
2518       break;
2519
2520     case DW_OP_GNU_implicit_pointer:
2521     case DW_OP_GNU_entry_value:
2522     case DW_OP_GNU_const_type:
2523     case DW_OP_GNU_regval_type:
2524     case DW_OP_GNU_deref_type:
2525     case DW_OP_GNU_convert:
2526     case DW_OP_GNU_reinterpret:
2527     case DW_OP_GNU_parameter_ref:
2528       gcc_unreachable ();
2529       break;
2530
2531     default:
2532       /* Other codes have no operands.  */
2533       break;
2534     }
2535 }
2536
2537 void
2538 output_loc_sequence_raw (dw_loc_descr_ref loc)
2539 {
2540   while (1)
2541     {
2542       enum dwarf_location_atom opc = loc->dw_loc_opc;
2543       /* Output the opcode.  */
2544       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2545         {
2546           unsigned r = (opc - DW_OP_breg0);
2547           r = DWARF2_FRAME_REG_OUT (r, 1);
2548           gcc_assert (r <= 31);
2549           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2550         }
2551       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2552         {
2553           unsigned r = (opc - DW_OP_reg0);
2554           r = DWARF2_FRAME_REG_OUT (r, 1);
2555           gcc_assert (r <= 31);
2556           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2557         }
2558       /* Output the opcode.  */
2559       fprintf (asm_out_file, "%#x", opc);
2560       output_loc_operands_raw (loc);
2561
2562       if (!loc->dw_loc_next)
2563         break;
2564       loc = loc->dw_loc_next;
2565
2566       fputc (',', asm_out_file);
2567     }
2568 }
2569
2570 /* This function builds a dwarf location descriptor sequence from a
2571    dw_cfa_location, adding the given OFFSET to the result of the
2572    expression.  */
2573
2574 struct dw_loc_descr_struct *
2575 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2576 {
2577   struct dw_loc_descr_struct *head, *tmp;
2578
2579   offset += cfa->offset;
2580
2581   if (cfa->indirect)
2582     {
2583       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2584       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2585       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2586       add_loc_descr (&head, tmp);
2587       if (offset != 0)
2588         {
2589           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2590           add_loc_descr (&head, tmp);
2591         }
2592     }
2593   else
2594     head = new_reg_loc_descr (cfa->reg, offset);
2595
2596   return head;
2597 }
2598
2599 /* This function builds a dwarf location descriptor sequence for
2600    the address at OFFSET from the CFA when stack is aligned to
2601    ALIGNMENT byte.  */
2602
2603 struct dw_loc_descr_struct *
2604 build_cfa_aligned_loc (dw_cfa_location *cfa,
2605                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2606 {
2607   struct dw_loc_descr_struct *head;
2608   unsigned int dwarf_fp
2609     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2610
2611   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2612   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2613     {
2614       head = new_reg_loc_descr (dwarf_fp, 0);
2615       add_loc_descr (&head, int_loc_descriptor (alignment));
2616       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2617       loc_descr_plus_const (&head, offset);
2618     }
2619   else
2620     head = new_reg_loc_descr (dwarf_fp, offset);
2621   return head;
2622 }
2623 \f
2624 /* And now, the support for symbolic debugging information.  */
2625
2626 /* .debug_str support.  */
2627 static int output_indirect_string (void **, void *);
2628
2629 static void dwarf2out_init (const char *);
2630 static void dwarf2out_finish (const char *);
2631 static void dwarf2out_assembly_start (void);
2632 static void dwarf2out_define (unsigned int, const char *);
2633 static void dwarf2out_undef (unsigned int, const char *);
2634 static void dwarf2out_start_source_file (unsigned, const char *);
2635 static void dwarf2out_end_source_file (unsigned);
2636 static void dwarf2out_function_decl (tree);
2637 static void dwarf2out_begin_block (unsigned, unsigned);
2638 static void dwarf2out_end_block (unsigned, unsigned);
2639 static bool dwarf2out_ignore_block (const_tree);
2640 static void dwarf2out_global_decl (tree);
2641 static void dwarf2out_type_decl (tree, int);
2642 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2643 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2644                                                  dw_die_ref);
2645 static void dwarf2out_abstract_function (tree);
2646 static void dwarf2out_var_location (rtx);
2647 static void dwarf2out_begin_function (tree);
2648 static void dwarf2out_set_name (tree, tree);
2649
2650 /* The debug hooks structure.  */
2651
2652 const struct gcc_debug_hooks dwarf2_debug_hooks =
2653 {
2654   dwarf2out_init,
2655   dwarf2out_finish,
2656   dwarf2out_assembly_start,
2657   dwarf2out_define,
2658   dwarf2out_undef,
2659   dwarf2out_start_source_file,
2660   dwarf2out_end_source_file,
2661   dwarf2out_begin_block,
2662   dwarf2out_end_block,
2663   dwarf2out_ignore_block,
2664   dwarf2out_source_line,
2665   dwarf2out_begin_prologue,
2666 #if VMS_DEBUGGING_INFO
2667   dwarf2out_vms_end_prologue,
2668   dwarf2out_vms_begin_epilogue,
2669 #else
2670   debug_nothing_int_charstar,
2671   debug_nothing_int_charstar,
2672 #endif
2673   dwarf2out_end_epilogue,
2674   dwarf2out_begin_function,
2675   debug_nothing_int,            /* end_function */
2676   dwarf2out_function_decl,      /* function_decl */
2677   dwarf2out_global_decl,
2678   dwarf2out_type_decl,          /* type_decl */
2679   dwarf2out_imported_module_or_decl,
2680   debug_nothing_tree,           /* deferred_inline_function */
2681   /* The DWARF 2 backend tries to reduce debugging bloat by not
2682      emitting the abstract description of inline functions until
2683      something tries to reference them.  */
2684   dwarf2out_abstract_function,  /* outlining_inline_function */
2685   debug_nothing_rtx,            /* label */
2686   debug_nothing_int,            /* handle_pch */
2687   dwarf2out_var_location,
2688   dwarf2out_switch_text_section,
2689   dwarf2out_set_name,
2690   1,                            /* start_end_main_source_file */
2691   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2692 };
2693 \f
2694 /* NOTE: In the comments in this file, many references are made to
2695    "Debugging Information Entries".  This term is abbreviated as `DIE'
2696    throughout the remainder of this file.  */
2697
2698 /* An internal representation of the DWARF output is built, and then
2699    walked to generate the DWARF debugging info.  The walk of the internal
2700    representation is done after the entire program has been compiled.
2701    The types below are used to describe the internal representation.  */
2702
2703 /* Whether to put type DIEs into their own section .debug_types instead
2704    of making them part of the .debug_info section.  Only supported for
2705    Dwarf V4 or higher and the user didn't disable them through
2706    -fno-debug-types-section.  It is more efficient to put them in a
2707    separate comdat sections since the linker will then be able to
2708    remove duplicates.  But not all tools support .debug_types sections
2709    yet.  */
2710
2711 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2712
2713 /* Various DIE's use offsets relative to the beginning of the
2714    .debug_info section to refer to each other.  */
2715
2716 typedef long int dw_offset;
2717
2718 /* Define typedefs here to avoid circular dependencies.  */
2719
2720 typedef struct dw_attr_struct *dw_attr_ref;
2721 typedef struct dw_line_info_struct *dw_line_info_ref;
2722 typedef struct pubname_struct *pubname_ref;
2723 typedef struct dw_ranges_struct *dw_ranges_ref;
2724 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2725 typedef struct comdat_type_struct *comdat_type_node_ref;
2726
2727 /* The entries in the line_info table more-or-less mirror the opcodes
2728    that are used in the real dwarf line table.  Arrays of these entries
2729    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2730    supported.  */
2731
2732 enum dw_line_info_opcode {
2733   /* Emit DW_LNE_set_address; the operand is the label index.  */
2734   LI_set_address,
2735
2736   /* Emit a row to the matrix with the given line.  This may be done
2737      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2738      special opcodes.  */
2739   LI_set_line,
2740
2741   /* Emit a DW_LNS_set_file.  */
2742   LI_set_file,
2743
2744   /* Emit a DW_LNS_set_column.  */
2745   LI_set_column,
2746
2747   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2748   LI_negate_stmt,
2749
2750   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2751   LI_set_prologue_end,
2752   LI_set_epilogue_begin,
2753
2754   /* Emit a DW_LNE_set_discriminator.  */
2755   LI_set_discriminator
2756 };
2757
2758 typedef struct GTY(()) dw_line_info_struct {
2759   enum dw_line_info_opcode opcode;
2760   unsigned int val;
2761 } dw_line_info_entry;
2762
2763 DEF_VEC_O(dw_line_info_entry);
2764 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2765
2766 typedef struct GTY(()) dw_line_info_table_struct {
2767   /* The label that marks the end of this section.  */
2768   const char *end_label;
2769
2770   /* The values for the last row of the matrix, as collected in the table.
2771      These are used to minimize the changes to the next row.  */
2772   unsigned int file_num;
2773   unsigned int line_num;
2774   unsigned int column_num;
2775   int discrim_num;
2776   bool is_stmt;
2777   bool in_use;
2778
2779   VEC(dw_line_info_entry, gc) *entries;
2780 } dw_line_info_table;
2781
2782 typedef dw_line_info_table *dw_line_info_table_p;
2783
2784 DEF_VEC_P(dw_line_info_table_p);
2785 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2786
2787 /* Each DIE attribute has a field specifying the attribute kind,
2788    a link to the next attribute in the chain, and an attribute value.
2789    Attributes are typically linked below the DIE they modify.  */
2790
2791 typedef struct GTY(()) dw_attr_struct {
2792   enum dwarf_attribute dw_attr;
2793   dw_val_node dw_attr_val;
2794 }
2795 dw_attr_node;
2796
2797 DEF_VEC_O(dw_attr_node);
2798 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2799
2800 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2801    The children of each node form a circular list linked by
2802    die_sib.  die_child points to the node *before* the "first" child node.  */
2803
2804 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2805   union die_symbol_or_type_node
2806     {
2807       char * GTY ((tag ("0"))) die_symbol;
2808       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2809     }
2810   GTY ((desc ("use_debug_types"))) die_id;
2811   VEC(dw_attr_node,gc) * die_attr;
2812   dw_die_ref die_parent;
2813   dw_die_ref die_child;
2814   dw_die_ref die_sib;
2815   dw_die_ref die_definition; /* ref from a specification to its definition */
2816   dw_offset die_offset;
2817   unsigned long die_abbrev;
2818   int die_mark;
2819   /* Die is used and must not be pruned as unused.  */
2820   int die_perennial_p;
2821   unsigned int decl_id;
2822   enum dwarf_tag die_tag;
2823 }
2824 die_node;
2825
2826 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2827 #define FOR_EACH_CHILD(die, c, expr) do {       \
2828   c = die->die_child;                           \
2829   if (c) do {                                   \
2830     c = c->die_sib;                             \
2831     expr;                                       \
2832   } while (c != die->die_child);                \
2833 } while (0)
2834
2835 /* The pubname structure */
2836
2837 typedef struct GTY(()) pubname_struct {
2838   dw_die_ref die;
2839   const char *name;
2840 }
2841 pubname_entry;
2842
2843 DEF_VEC_O(pubname_entry);
2844 DEF_VEC_ALLOC_O(pubname_entry, gc);
2845
2846 struct GTY(()) dw_ranges_struct {
2847   /* If this is positive, it's a block number, otherwise it's a
2848      bitwise-negated index into dw_ranges_by_label.  */
2849   int num;
2850 };
2851
2852 /* A structure to hold a macinfo entry.  */
2853
2854 typedef struct GTY(()) macinfo_struct {
2855   unsigned char code;
2856   unsigned HOST_WIDE_INT lineno;
2857   const char *info;
2858 }
2859 macinfo_entry;
2860
2861 DEF_VEC_O(macinfo_entry);
2862 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2863
2864 struct GTY(()) dw_ranges_by_label_struct {
2865   const char *begin;
2866   const char *end;
2867 };
2868
2869 /* The comdat type node structure.  */
2870 typedef struct GTY(()) comdat_type_struct
2871 {
2872   dw_die_ref root_die;
2873   dw_die_ref type_die;
2874   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2875   struct comdat_type_struct *next;
2876 }
2877 comdat_type_node;
2878
2879 /* The limbo die list structure.  */
2880 typedef struct GTY(()) limbo_die_struct {
2881   dw_die_ref die;
2882   tree created_for;
2883   struct limbo_die_struct *next;
2884 }
2885 limbo_die_node;
2886
2887 typedef struct skeleton_chain_struct
2888 {
2889   dw_die_ref old_die;
2890   dw_die_ref new_die;
2891   struct skeleton_chain_struct *parent;
2892 }
2893 skeleton_chain_node;
2894
2895 /* Define a macro which returns nonzero for a TYPE_DECL which was
2896    implicitly generated for a type.
2897
2898    Note that, unlike the C front-end (which generates a NULL named
2899    TYPE_DECL node for each complete tagged type, each array type,
2900    and each function type node created) the C++ front-end generates
2901    a _named_ TYPE_DECL node for each tagged type node created.
2902    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2903    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2904    front-end, but for each type, tagged or not.  */
2905
2906 #define TYPE_DECL_IS_STUB(decl)                         \
2907   (DECL_NAME (decl) == NULL_TREE                        \
2908    || (DECL_ARTIFICIAL (decl)                           \
2909        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2910            /* This is necessary for stub decls that     \
2911               appear in nested inline functions.  */    \
2912            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2913                && (decl_ultimate_origin (decl)          \
2914                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2915
2916 /* Information concerning the compilation unit's programming
2917    language, and compiler version.  */
2918
2919 /* Fixed size portion of the DWARF compilation unit header.  */
2920 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2921   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2922
2923 /* Fixed size portion of the DWARF comdat type unit header.  */
2924 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2925   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2926    + DWARF_OFFSET_SIZE)
2927
2928 /* Fixed size portion of public names info.  */
2929 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2930
2931 /* Fixed size portion of the address range info.  */
2932 #define DWARF_ARANGES_HEADER_SIZE                                       \
2933   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2934                 DWARF2_ADDR_SIZE * 2)                                   \
2935    - DWARF_INITIAL_LENGTH_SIZE)
2936
2937 /* Size of padding portion in the address range info.  It must be
2938    aligned to twice the pointer size.  */
2939 #define DWARF_ARANGES_PAD_SIZE \
2940   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2941                 DWARF2_ADDR_SIZE * 2)                              \
2942    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2943
2944 /* Use assembler line directives if available.  */
2945 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2946 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2947 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2948 #else
2949 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2950 #endif
2951 #endif
2952
2953 /* Minimum line offset in a special line info. opcode.
2954    This value was chosen to give a reasonable range of values.  */
2955 #define DWARF_LINE_BASE  -10
2956
2957 /* First special line opcode - leave room for the standard opcodes.  */
2958 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2959
2960 /* Range of line offsets in a special line info. opcode.  */
2961 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2962
2963 /* Flag that indicates the initial value of the is_stmt_start flag.
2964    In the present implementation, we do not mark any lines as
2965    the beginning of a source statement, because that information
2966    is not made available by the GCC front-end.  */
2967 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2968
2969 /* Maximum number of operations per instruction bundle.  */
2970 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2971 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2972 #endif
2973
2974 /* This location is used by calc_die_sizes() to keep track
2975    the offset of each DIE within the .debug_info section.  */
2976 static unsigned long next_die_offset;
2977
2978 /* Record the root of the DIE's built for the current compilation unit.  */
2979 static GTY(()) dw_die_ref single_comp_unit_die;
2980
2981 /* A list of type DIEs that have been separated into comdat sections.  */
2982 static GTY(()) comdat_type_node *comdat_type_list;
2983
2984 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2985 static GTY(()) limbo_die_node *limbo_die_list;
2986
2987 /* A list of DIEs for which we may have to generate
2988    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2989 static GTY(()) limbo_die_node *deferred_asm_name;
2990
2991 /* Filenames referenced by this compilation unit.  */
2992 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2993
2994 /* A hash table of references to DIE's that describe declarations.
2995    The key is a DECL_UID() which is a unique number identifying each decl.  */
2996 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2997
2998 /* A hash table of references to DIE's that describe COMMON blocks.
2999    The key is DECL_UID() ^ die_parent.  */
3000 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3001
3002 typedef struct GTY(()) die_arg_entry_struct {
3003     dw_die_ref die;
3004     tree arg;
3005 } die_arg_entry;
3006
3007 DEF_VEC_O(die_arg_entry);
3008 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3009
3010 /* Node of the variable location list.  */
3011 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3012   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3013      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3014      in mode of the EXPR_LIST node and first EXPR_LIST operand
3015      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3016      location or NULL for padding.  For larger bitsizes,
3017      mode is 0 and first operand is a CONCAT with bitsize
3018      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3019      NULL as second operand.  */
3020   rtx GTY (()) loc;
3021   const char * GTY (()) label;
3022   struct var_loc_node * GTY (()) next;
3023 };
3024
3025 /* Variable location list.  */
3026 struct GTY (()) var_loc_list_def {
3027   struct var_loc_node * GTY (()) first;
3028
3029   /* Pointer to the last but one or last element of the
3030      chained list.  If the list is empty, both first and
3031      last are NULL, if the list contains just one node
3032      or the last node certainly is not redundant, it points
3033      to the last node, otherwise points to the last but one.
3034      Do not mark it for GC because it is marked through the chain.  */
3035   struct var_loc_node * GTY ((skip ("%h"))) last;
3036
3037   /* Pointer to the last element before section switch,
3038      if NULL, either sections weren't switched or first
3039      is after section switch.  */
3040   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3041
3042   /* DECL_UID of the variable decl.  */
3043   unsigned int decl_id;
3044 };
3045 typedef struct var_loc_list_def var_loc_list;
3046
3047 /* Call argument location list.  */
3048 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3049   rtx GTY (()) call_arg_loc_note;
3050   const char * GTY (()) label;
3051   tree GTY (()) block;
3052   bool tail_call_p;
3053   rtx GTY (()) symbol_ref;
3054   struct call_arg_loc_node * GTY (()) next;
3055 };
3056
3057
3058 /* Table of decl location linked lists.  */
3059 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3060
3061 /* Head and tail of call_arg_loc chain.  */
3062 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3063 static struct call_arg_loc_node *call_arg_loc_last;
3064
3065 /* Number of call sites in the current function.  */
3066 static int call_site_count = -1;
3067 /* Number of tail call sites in the current function.  */
3068 static int tail_call_site_count = -1;
3069
3070 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3071    DIEs.  */
3072 static VEC (dw_die_ref, heap) *block_map;
3073
3074 /* A cached location list.  */
3075 struct GTY (()) cached_dw_loc_list_def {
3076   /* The DECL_UID of the decl that this entry describes.  */
3077   unsigned int decl_id;
3078
3079   /* The cached location list.  */
3080   dw_loc_list_ref loc_list;
3081 };
3082 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3083
3084 /* Table of cached location lists.  */
3085 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3086
3087 /* A pointer to the base of a list of references to DIE's that
3088    are uniquely identified by their tag, presence/absence of
3089    children DIE's, and list of attribute/value pairs.  */
3090 static GTY((length ("abbrev_die_table_allocated")))
3091   dw_die_ref *abbrev_die_table;
3092
3093 /* Number of elements currently allocated for abbrev_die_table.  */
3094 static GTY(()) unsigned abbrev_die_table_allocated;
3095
3096 /* Number of elements in type_die_table currently in use.  */
3097 static GTY(()) unsigned abbrev_die_table_in_use;
3098
3099 /* Size (in elements) of increments by which we may expand the
3100    abbrev_die_table.  */
3101 #define ABBREV_DIE_TABLE_INCREMENT 256
3102
3103 /* A global counter for generating labels for line number data.  */
3104 static unsigned int line_info_label_num;
3105
3106 /* The current table to which we should emit line number information
3107    for the current function.  This will be set up at the beginning of
3108    assembly for the function.  */
3109 static dw_line_info_table *cur_line_info_table;
3110
3111 /* The two default tables of line number info.  */
3112 static GTY(()) dw_line_info_table *text_section_line_info;
3113 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3114
3115 /* The set of all non-default tables of line number info.  */
3116 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3117
3118 /* A flag to tell pubnames/types export if there is an info section to
3119    refer to.  */
3120 static bool info_section_emitted;
3121
3122 /* A pointer to the base of a table that contains a list of publicly
3123    accessible names.  */
3124 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3125
3126 /* A pointer to the base of a table that contains a list of publicly
3127    accessible types.  */
3128 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3129
3130 /* A pointer to the base of a table that contains a list of macro
3131    defines/undefines (and file start/end markers).  */
3132 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3133
3134 /* Array of dies for which we should generate .debug_ranges info.  */
3135 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3136
3137 /* Number of elements currently allocated for ranges_table.  */
3138 static GTY(()) unsigned ranges_table_allocated;
3139
3140 /* Number of elements in ranges_table currently in use.  */
3141 static GTY(()) unsigned ranges_table_in_use;
3142
3143 /* Array of pairs of labels referenced in ranges_table.  */
3144 static GTY ((length ("ranges_by_label_allocated")))
3145      dw_ranges_by_label_ref ranges_by_label;
3146
3147 /* Number of elements currently allocated for ranges_by_label.  */
3148 static GTY(()) unsigned ranges_by_label_allocated;
3149
3150 /* Number of elements in ranges_by_label currently in use.  */
3151 static GTY(()) unsigned ranges_by_label_in_use;
3152
3153 /* Size (in elements) of increments by which we may expand the
3154    ranges_table.  */
3155 #define RANGES_TABLE_INCREMENT 64
3156
3157 /* Whether we have location lists that need outputting */
3158 static GTY(()) bool have_location_lists;
3159
3160 /* Unique label counter.  */
3161 static GTY(()) unsigned int loclabel_num;
3162
3163 /* Unique label counter for point-of-call tables.  */
3164 static GTY(()) unsigned int poc_label_num;
3165
3166 /* Record whether the function being analyzed contains inlined functions.  */
3167 static int current_function_has_inlines;
3168
3169 /* The last file entry emitted by maybe_emit_file().  */
3170 static GTY(()) struct dwarf_file_data * last_emitted_file;
3171
3172 /* Number of internal labels generated by gen_internal_sym().  */
3173 static GTY(()) int label_num;
3174
3175 /* Cached result of previous call to lookup_filename.  */
3176 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3177
3178 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3179
3180 /* Instances of generic types for which we need to generate debug
3181    info that describe their generic parameters and arguments. That
3182    generation needs to happen once all types are properly laid out so
3183    we do it at the end of compilation.  */
3184 static GTY(()) VEC(tree,gc) *generic_type_instances;
3185
3186 /* Offset from the "steady-state frame pointer" to the frame base,
3187    within the current function.  */
3188 static HOST_WIDE_INT frame_pointer_fb_offset;
3189 static bool frame_pointer_fb_offset_valid;
3190
3191 static VEC (dw_die_ref, heap) *base_types;
3192
3193 /* Forward declarations for functions defined in this file.  */
3194
3195 static int is_pseudo_reg (const_rtx);
3196 static tree type_main_variant (tree);
3197 static int is_tagged_type (const_tree);
3198 static const char *dwarf_tag_name (unsigned);
3199 static const char *dwarf_attr_name (unsigned);
3200 static const char *dwarf_form_name (unsigned);
3201 static tree decl_ultimate_origin (const_tree);
3202 static tree decl_class_context (tree);
3203 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3204 static inline enum dw_val_class AT_class (dw_attr_ref);
3205 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3206 static inline unsigned AT_flag (dw_attr_ref);
3207 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3208 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3209 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3210 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3211 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3212                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3213 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3214                                unsigned int, unsigned char *);
3215 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3216 static hashval_t debug_str_do_hash (const void *);
3217 static int debug_str_eq (const void *, const void *);
3218 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3219 static inline const char *AT_string (dw_attr_ref);
3220 static enum dwarf_form AT_string_form (dw_attr_ref);
3221 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3222 static void add_AT_specification (dw_die_ref, dw_die_ref);
3223 static inline dw_die_ref AT_ref (dw_attr_ref);
3224 static inline int AT_ref_external (dw_attr_ref);
3225 static inline void set_AT_ref_external (dw_attr_ref, int);
3226 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3227 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3228 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3229 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3230                              dw_loc_list_ref);
3231 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3232 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3233 static inline rtx AT_addr (dw_attr_ref);
3234 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3235 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3236 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3237 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3238                            unsigned HOST_WIDE_INT);
3239 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3240                                unsigned long);
3241 static inline const char *AT_lbl (dw_attr_ref);
3242 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3243 static const char *get_AT_low_pc (dw_die_ref);
3244 static const char *get_AT_hi_pc (dw_die_ref);
3245 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3246 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3247 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3248 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3249 static bool is_cxx (void);
3250 static bool is_fortran (void);
3251 static bool is_ada (void);
3252 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3253 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3254 static void add_child_die (dw_die_ref, dw_die_ref);
3255 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3256 static dw_die_ref lookup_type_die (tree);
3257 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3258 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3259 static void equate_type_number_to_die (tree, dw_die_ref);
3260 static hashval_t decl_die_table_hash (const void *);
3261 static int decl_die_table_eq (const void *, const void *);
3262 static dw_die_ref lookup_decl_die (tree);
3263 static hashval_t common_block_die_table_hash (const void *);
3264 static int common_block_die_table_eq (const void *, const void *);
3265 static hashval_t decl_loc_table_hash (const void *);
3266 static int decl_loc_table_eq (const void *, const void *);
3267 static var_loc_list *lookup_decl_loc (const_tree);
3268 static void equate_decl_number_to_die (tree, dw_die_ref);
3269 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3270 static void print_spaces (FILE *);
3271 static void print_die (dw_die_ref, FILE *);
3272 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3273 static dw_die_ref pop_compile_unit (dw_die_ref);
3274 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3275 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3276 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3277 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3278 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3279 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3280 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3281                                    struct md5_ctx *, int *);
3282 struct checksum_attributes;
3283 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3284 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3285 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3286 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3287 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3288 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3289 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3290 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3291 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3292 static void compute_section_prefix (dw_die_ref);
3293 static int is_type_die (dw_die_ref);
3294 static int is_comdat_die (dw_die_ref);
3295 static int is_symbol_die (dw_die_ref);
3296 static void assign_symbol_names (dw_die_ref);
3297 static void break_out_includes (dw_die_ref);
3298 static int is_declaration_die (dw_die_ref);
3299 static int should_move_die_to_comdat (dw_die_ref);
3300 static dw_die_ref clone_as_declaration (dw_die_ref);
3301 static dw_die_ref clone_die (dw_die_ref);
3302 static dw_die_ref clone_tree (dw_die_ref);
3303 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3304 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3305 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3306 static dw_die_ref generate_skeleton (dw_die_ref);
3307 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3308                                                          dw_die_ref);
3309 static void break_out_comdat_types (dw_die_ref);
3310 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3311 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3312 static void copy_decls_for_unworthy_types (dw_die_ref);
3313
3314 static hashval_t htab_cu_hash (const void *);
3315 static int htab_cu_eq (const void *, const void *);
3316 static void htab_cu_del (void *);
3317 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3318 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3319 static void add_sibling_attributes (dw_die_ref);
3320 static void build_abbrev_table (dw_die_ref);
3321 static void output_location_lists (dw_die_ref);
3322 static int constant_size (unsigned HOST_WIDE_INT);
3323 static unsigned long size_of_die (dw_die_ref);
3324 static void calc_die_sizes (dw_die_ref);
3325 static void calc_base_type_die_sizes (void);
3326 static void mark_dies (dw_die_ref);
3327 static void unmark_dies (dw_die_ref);
3328 static void unmark_all_dies (dw_die_ref);
3329 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3330 static unsigned long size_of_aranges (void);
3331 static enum dwarf_form value_format (dw_attr_ref);
3332 static void output_value_format (dw_attr_ref);
3333 static void output_abbrev_section (void);
3334 static void output_die_symbol (dw_die_ref);
3335 static void output_die (dw_die_ref);
3336 static void output_compilation_unit_header (void);
3337 static void output_comp_unit (dw_die_ref, int);
3338 static void output_comdat_type_unit (comdat_type_node *);
3339 static const char *dwarf2_name (tree, int);
3340 static void add_pubname (tree, dw_die_ref);
3341 static void add_pubname_string (const char *, dw_die_ref);
3342 static void add_pubtype (tree, dw_die_ref);
3343 static void output_pubnames (VEC (pubname_entry,gc) *);
3344 static void output_aranges (unsigned long);
3345 static unsigned int add_ranges_num (int);
3346 static unsigned int add_ranges (const_tree);
3347 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3348                                   bool *);
3349 static void output_ranges (void);
3350 static dw_line_info_table *new_line_info_table (void);
3351 static void output_line_info (void);
3352 static void output_file_names (void);
3353 static dw_die_ref base_type_die (tree);
3354 static int is_base_type (tree);
3355 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3356 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3357 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3358 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3359 static int type_is_enum (const_tree);
3360 static unsigned int dbx_reg_number (const_rtx);
3361 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3362 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3363 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3364                                                 enum var_init_status);
3365 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3366                                                      enum var_init_status);
3367 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3368                                          enum var_init_status);
3369 static int is_based_loc (const_rtx);
3370 static int resolve_one_addr (rtx *, void *);
3371 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3372                                                enum var_init_status);
3373 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3374                                         enum var_init_status);
3375 static dw_loc_list_ref loc_list_from_tree (tree, int);
3376 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3377 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3378 static tree field_type (const_tree);
3379 static unsigned int simple_type_align_in_bits (const_tree);
3380 static unsigned int simple_decl_align_in_bits (const_tree);
3381 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3382 static HOST_WIDE_INT field_byte_offset (const_tree);
3383 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3384                                          dw_loc_list_ref);
3385 static void add_data_member_location_attribute (dw_die_ref, tree);
3386 static bool add_const_value_attribute (dw_die_ref, rtx);
3387 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3388 static void insert_double (double_int, unsigned char *);
3389 static void insert_float (const_rtx, unsigned char *);
3390 static rtx rtl_for_decl_location (tree);
3391 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3392                                                    enum dwarf_attribute);
3393 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3394 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3395 static void add_name_attribute (dw_die_ref, const char *);
3396 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3397 static void add_comp_dir_attribute (dw_die_ref);
3398 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3399 static void add_subscript_info (dw_die_ref, tree, bool);
3400 static void add_byte_size_attribute (dw_die_ref, tree);
3401 static void add_bit_offset_attribute (dw_die_ref, tree);
3402 static void add_bit_size_attribute (dw_die_ref, tree);
3403 static void add_prototyped_attribute (dw_die_ref, tree);
3404 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3405 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3406 static void add_src_coords_attributes (dw_die_ref, tree);
3407 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3408 static void push_decl_scope (tree);
3409 static void pop_decl_scope (void);
3410 static dw_die_ref scope_die_for (tree, dw_die_ref);
3411 static inline int local_scope_p (dw_die_ref);
3412 static inline int class_scope_p (dw_die_ref);
3413 static inline int class_or_namespace_scope_p (dw_die_ref);
3414 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3415 static void add_calling_convention_attribute (dw_die_ref, tree);
3416 static const char *type_tag (const_tree);
3417 static tree member_declared_type (const_tree);
3418 #if 0
3419 static const char *decl_start_label (tree);
3420 #endif
3421 static void gen_array_type_die (tree, dw_die_ref);
3422 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3423 #if 0
3424 static void gen_entry_point_die (tree, dw_die_ref);
3425 #endif
3426 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3427 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3428 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3429 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3430 static void gen_formal_types_die (tree, dw_die_ref);
3431 static void gen_subprogram_die (tree, dw_die_ref);
3432 static void gen_variable_die (tree, tree, dw_die_ref);
3433 static void gen_const_die (tree, dw_die_ref);
3434 static void gen_label_die (tree, dw_die_ref);
3435 static void gen_lexical_block_die (tree, dw_die_ref, int);
3436 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3437 static void gen_field_die (tree, dw_die_ref);
3438 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3439 static dw_die_ref gen_compile_unit_die (const char *);
3440 static void gen_inheritance_die (tree, tree, dw_die_ref);
3441 static void gen_member_die (tree, dw_die_ref);
3442 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3443                                                 enum debug_info_usage);
3444 static void gen_subroutine_type_die (tree, dw_die_ref);
3445 static void gen_typedef_die (tree, dw_die_ref);
3446 static void gen_type_die (tree, dw_die_ref);
3447 static void gen_block_die (tree, dw_die_ref, int);
3448 static void decls_for_scope (tree, dw_die_ref, int);
3449 static inline int is_redundant_typedef (const_tree);
3450 static bool is_naming_typedef_decl (const_tree);
3451 static inline dw_die_ref get_context_die (tree);
3452 static void gen_namespace_die (tree, dw_die_ref);
3453 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3454 static dw_die_ref force_decl_die (tree);
3455 static dw_die_ref force_type_die (tree);
3456 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3457 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3458 static struct dwarf_file_data * lookup_filename (const char *);
3459 static void retry_incomplete_types (void);
3460 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3461 static void gen_generic_params_dies (tree);
3462 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3463 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3464 static void splice_child_die (dw_die_ref, dw_die_ref);
3465 static int file_info_cmp (const void *, const void *);
3466 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3467                                      const char *, const char *);
3468 static void output_loc_list (dw_loc_list_ref);
3469 static char *gen_internal_sym (const char *);
3470
3471 static void prune_unmark_dies (dw_die_ref);
3472 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3473 static void prune_unused_types_mark (dw_die_ref, int);
3474 static void prune_unused_types_walk (dw_die_ref);
3475 static void prune_unused_types_walk_attribs (dw_die_ref);
3476 static void prune_unused_types_prune (dw_die_ref);
3477 static void prune_unused_types (void);
3478 static int maybe_emit_file (struct dwarf_file_data *fd);
3479 static inline const char *AT_vms_delta1 (dw_attr_ref);
3480 static inline const char *AT_vms_delta2 (dw_attr_ref);
3481 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3482                                      const char *, const char *);
3483 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3484 static void gen_remaining_tmpl_value_param_die_attribute (void);
3485 static bool generic_type_p (tree);
3486 static void schedule_generic_params_dies_gen (tree t);
3487 static void gen_scheduled_generic_parms_dies (void);
3488
3489 /* Section names used to hold DWARF debugging information.  */
3490 #ifndef DEBUG_INFO_SECTION
3491 #define DEBUG_INFO_SECTION      ".debug_info"
3492 #endif
3493 #ifndef DEBUG_ABBREV_SECTION
3494 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3495 #endif
3496 #ifndef DEBUG_ARANGES_SECTION
3497 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3498 #endif
3499 #ifndef DEBUG_MACINFO_SECTION
3500 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3501 #endif
3502 #ifndef DEBUG_MACRO_SECTION
3503 #define DEBUG_MACRO_SECTION     ".debug_macro"
3504 #endif
3505 #ifndef DEBUG_LINE_SECTION
3506 #define DEBUG_LINE_SECTION      ".debug_line"
3507 #endif
3508 #ifndef DEBUG_LOC_SECTION
3509 #define DEBUG_LOC_SECTION       ".debug_loc"
3510 #endif
3511 #ifndef DEBUG_PUBNAMES_SECTION
3512 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3513 #endif
3514 #ifndef DEBUG_PUBTYPES_SECTION
3515 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3516 #endif
3517 #ifndef DEBUG_STR_SECTION
3518 #define DEBUG_STR_SECTION       ".debug_str"
3519 #endif
3520 #ifndef DEBUG_RANGES_SECTION
3521 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3522 #endif
3523
3524 /* Standard ELF section names for compiled code and data.  */
3525 #ifndef TEXT_SECTION_NAME
3526 #define TEXT_SECTION_NAME       ".text"
3527 #endif
3528
3529 /* Section flags for .debug_str section.  */
3530 #define DEBUG_STR_SECTION_FLAGS \
3531   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3532    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3533    : SECTION_DEBUG)
3534
3535 /* Labels we insert at beginning sections we can reference instead of
3536    the section names themselves.  */
3537
3538 #ifndef TEXT_SECTION_LABEL
3539 #define TEXT_SECTION_LABEL              "Ltext"
3540 #endif
3541 #ifndef COLD_TEXT_SECTION_LABEL
3542 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3543 #endif
3544 #ifndef DEBUG_LINE_SECTION_LABEL
3545 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3546 #endif
3547 #ifndef DEBUG_INFO_SECTION_LABEL
3548 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3549 #endif
3550 #ifndef DEBUG_ABBREV_SECTION_LABEL
3551 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3552 #endif
3553 #ifndef DEBUG_LOC_SECTION_LABEL
3554 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3555 #endif
3556 #ifndef DEBUG_RANGES_SECTION_LABEL
3557 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3558 #endif
3559 #ifndef DEBUG_MACINFO_SECTION_LABEL
3560 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3561 #endif
3562 #ifndef DEBUG_MACRO_SECTION_LABEL
3563 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3564 #endif
3565
3566
3567 /* Definitions of defaults for formats and names of various special
3568    (artificial) labels which may be generated within this file (when the -g
3569    options is used and DWARF2_DEBUGGING_INFO is in effect.
3570    If necessary, these may be overridden from within the tm.h file, but
3571    typically, overriding these defaults is unnecessary.  */
3572
3573 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3574 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3575 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3583
3584 #ifndef TEXT_END_LABEL
3585 #define TEXT_END_LABEL          "Letext"
3586 #endif
3587 #ifndef COLD_END_LABEL
3588 #define COLD_END_LABEL          "Letext_cold"
3589 #endif
3590 #ifndef BLOCK_BEGIN_LABEL
3591 #define BLOCK_BEGIN_LABEL       "LBB"
3592 #endif
3593 #ifndef BLOCK_END_LABEL
3594 #define BLOCK_END_LABEL         "LBE"
3595 #endif
3596 #ifndef LINE_CODE_LABEL
3597 #define LINE_CODE_LABEL         "LM"
3598 #endif
3599
3600 \f
3601 /* Return the root of the DIE's built for the current compilation unit.  */
3602 static dw_die_ref
3603 comp_unit_die (void)
3604 {
3605   if (!single_comp_unit_die)
3606     single_comp_unit_die = gen_compile_unit_die (NULL);
3607   return single_comp_unit_die;
3608 }
3609
3610 /* We allow a language front-end to designate a function that is to be
3611    called to "demangle" any name before it is put into a DIE.  */
3612
3613 static const char *(*demangle_name_func) (const char *);
3614
3615 void
3616 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3617 {
3618   demangle_name_func = func;
3619 }
3620
3621 /* Test if rtl node points to a pseudo register.  */
3622
3623 static inline int
3624 is_pseudo_reg (const_rtx rtl)
3625 {
3626   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3627           || (GET_CODE (rtl) == SUBREG
3628               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3629 }
3630
3631 /* Return a reference to a type, with its const and volatile qualifiers
3632    removed.  */
3633
3634 static inline tree
3635 type_main_variant (tree type)
3636 {
3637   type = TYPE_MAIN_VARIANT (type);
3638
3639   /* ??? There really should be only one main variant among any group of
3640      variants of a given type (and all of the MAIN_VARIANT values for all
3641      members of the group should point to that one type) but sometimes the C
3642      front-end messes this up for array types, so we work around that bug
3643      here.  */
3644   if (TREE_CODE (type) == ARRAY_TYPE)
3645     while (type != TYPE_MAIN_VARIANT (type))
3646       type = TYPE_MAIN_VARIANT (type);
3647
3648   return type;
3649 }
3650
3651 /* Return nonzero if the given type node represents a tagged type.  */
3652
3653 static inline int
3654 is_tagged_type (const_tree type)
3655 {
3656   enum tree_code code = TREE_CODE (type);
3657
3658   return (code == RECORD_TYPE || code == UNION_TYPE
3659           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3660 }
3661
3662 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3663
3664 static void
3665 get_ref_die_offset_label (char *label, dw_die_ref ref)
3666 {
3667   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3668 }
3669
3670 /* Return die_offset of a DIE reference to a base type.  */
3671
3672 static unsigned long int
3673 get_base_type_offset (dw_die_ref ref)
3674 {
3675   if (ref->die_offset)
3676     return ref->die_offset;
3677   if (comp_unit_die ()->die_abbrev)
3678     {
3679       calc_base_type_die_sizes ();
3680       gcc_assert (ref->die_offset);
3681     }
3682   return ref->die_offset;
3683 }
3684
3685 /* Return die_offset of a DIE reference other than base type.  */
3686
3687 static unsigned long int
3688 get_ref_die_offset (dw_die_ref ref)
3689 {
3690   gcc_assert (ref->die_offset);
3691   return ref->die_offset;
3692 }
3693
3694 /* Convert a DIE tag into its string name.  */
3695
3696 static const char *
3697 dwarf_tag_name (unsigned int tag)
3698 {
3699   switch (tag)
3700     {
3701     case DW_TAG_padding:
3702       return "DW_TAG_padding";
3703     case DW_TAG_array_type:
3704       return "DW_TAG_array_type";
3705     case DW_TAG_class_type:
3706       return "DW_TAG_class_type";
3707     case DW_TAG_entry_point:
3708       return "DW_TAG_entry_point";
3709     case DW_TAG_enumeration_type:
3710       return "DW_TAG_enumeration_type";
3711     case DW_TAG_formal_parameter:
3712       return "DW_TAG_formal_parameter";
3713     case DW_TAG_imported_declaration:
3714       return "DW_TAG_imported_declaration";
3715     case DW_TAG_label:
3716       return "DW_TAG_label";
3717     case DW_TAG_lexical_block:
3718       return "DW_TAG_lexical_block";
3719     case DW_TAG_member:
3720       return "DW_TAG_member";
3721     case DW_TAG_pointer_type:
3722       return "DW_TAG_pointer_type";
3723     case DW_TAG_reference_type:
3724       return "DW_TAG_reference_type";
3725     case DW_TAG_compile_unit:
3726       return "DW_TAG_compile_unit";
3727     case DW_TAG_string_type:
3728       return "DW_TAG_string_type";
3729     case DW_TAG_structure_type:
3730       return "DW_TAG_structure_type";
3731     case DW_TAG_subroutine_type:
3732       return "DW_TAG_subroutine_type";
3733     case DW_TAG_typedef:
3734       return "DW_TAG_typedef";
3735     case DW_TAG_union_type:
3736       return "DW_TAG_union_type";
3737     case DW_TAG_unspecified_parameters:
3738       return "DW_TAG_unspecified_parameters";
3739     case DW_TAG_variant:
3740       return "DW_TAG_variant";
3741     case DW_TAG_common_block:
3742       return "DW_TAG_common_block";
3743     case DW_TAG_common_inclusion:
3744       return "DW_TAG_common_inclusion";
3745     case DW_TAG_inheritance:
3746       return "DW_TAG_inheritance";
3747     case DW_TAG_inlined_subroutine:
3748       return "DW_TAG_inlined_subroutine";
3749     case DW_TAG_module:
3750       return "DW_TAG_module";
3751     case DW_TAG_ptr_to_member_type:
3752       return "DW_TAG_ptr_to_member_type";
3753     case DW_TAG_set_type:
3754       return "DW_TAG_set_type";
3755     case DW_TAG_subrange_type:
3756       return "DW_TAG_subrange_type";
3757     case DW_TAG_with_stmt:
3758       return "DW_TAG_with_stmt";
3759     case DW_TAG_access_declaration:
3760       return "DW_TAG_access_declaration";
3761     case DW_TAG_base_type:
3762       return "DW_TAG_base_type";
3763     case DW_TAG_catch_block:
3764       return "DW_TAG_catch_block";
3765     case DW_TAG_const_type:
3766       return "DW_TAG_const_type";
3767     case DW_TAG_constant:
3768       return "DW_TAG_constant";
3769     case DW_TAG_enumerator:
3770       return "DW_TAG_enumerator";
3771     case DW_TAG_file_type:
3772       return "DW_TAG_file_type";
3773     case DW_TAG_friend:
3774       return "DW_TAG_friend";
3775     case DW_TAG_namelist:
3776       return "DW_TAG_namelist";
3777     case DW_TAG_namelist_item:
3778       return "DW_TAG_namelist_item";
3779     case DW_TAG_packed_type:
3780       return "DW_TAG_packed_type";
3781     case DW_TAG_subprogram:
3782       return "DW_TAG_subprogram";
3783     case DW_TAG_template_type_param:
3784       return "DW_TAG_template_type_param";
3785     case DW_TAG_template_value_param:
3786       return "DW_TAG_template_value_param";
3787     case DW_TAG_thrown_type:
3788       return "DW_TAG_thrown_type";
3789     case DW_TAG_try_block:
3790       return "DW_TAG_try_block";
3791     case DW_TAG_variant_part:
3792       return "DW_TAG_variant_part";
3793     case DW_TAG_variable:
3794       return "DW_TAG_variable";
3795     case DW_TAG_volatile_type:
3796       return "DW_TAG_volatile_type";
3797     case DW_TAG_dwarf_procedure:
3798       return "DW_TAG_dwarf_procedure";
3799     case DW_TAG_restrict_type:
3800       return "DW_TAG_restrict_type";
3801     case DW_TAG_interface_type:
3802       return "DW_TAG_interface_type";
3803     case DW_TAG_namespace:
3804       return "DW_TAG_namespace";
3805     case DW_TAG_imported_module:
3806       return "DW_TAG_imported_module";
3807     case DW_TAG_unspecified_type:
3808       return "DW_TAG_unspecified_type";
3809     case DW_TAG_partial_unit:
3810       return "DW_TAG_partial_unit";
3811     case DW_TAG_imported_unit:
3812       return "DW_TAG_imported_unit";
3813     case DW_TAG_condition:
3814       return "DW_TAG_condition";
3815     case DW_TAG_shared_type:
3816       return "DW_TAG_shared_type";
3817     case DW_TAG_type_unit:
3818       return "DW_TAG_type_unit";
3819     case DW_TAG_rvalue_reference_type:
3820       return "DW_TAG_rvalue_reference_type";
3821     case DW_TAG_template_alias:
3822       return "DW_TAG_template_alias";
3823     case DW_TAG_GNU_template_parameter_pack:
3824       return "DW_TAG_GNU_template_parameter_pack";
3825     case DW_TAG_GNU_formal_parameter_pack:
3826       return "DW_TAG_GNU_formal_parameter_pack";
3827     case DW_TAG_MIPS_loop:
3828       return "DW_TAG_MIPS_loop";
3829     case DW_TAG_format_label:
3830       return "DW_TAG_format_label";
3831     case DW_TAG_function_template:
3832       return "DW_TAG_function_template";
3833     case DW_TAG_class_template:
3834       return "DW_TAG_class_template";
3835     case DW_TAG_GNU_BINCL:
3836       return "DW_TAG_GNU_BINCL";
3837     case DW_TAG_GNU_EINCL:
3838       return "DW_TAG_GNU_EINCL";
3839     case DW_TAG_GNU_template_template_param:
3840       return "DW_TAG_GNU_template_template_param";
3841     case DW_TAG_GNU_call_site:
3842       return "DW_TAG_GNU_call_site";
3843     case DW_TAG_GNU_call_site_parameter:
3844       return "DW_TAG_GNU_call_site_parameter";
3845     default:
3846       return "DW_TAG_<unknown>";
3847     }
3848 }
3849
3850 /* Convert a DWARF attribute code into its string name.  */
3851
3852 static const char *
3853 dwarf_attr_name (unsigned int attr)
3854 {
3855   switch (attr)
3856     {
3857     case DW_AT_sibling:
3858       return "DW_AT_sibling";
3859     case DW_AT_location:
3860       return "DW_AT_location";
3861     case DW_AT_name:
3862       return "DW_AT_name";
3863     case DW_AT_ordering:
3864       return "DW_AT_ordering";
3865     case DW_AT_subscr_data:
3866       return "DW_AT_subscr_data";
3867     case DW_AT_byte_size:
3868       return "DW_AT_byte_size";
3869     case DW_AT_bit_offset:
3870       return "DW_AT_bit_offset";
3871     case DW_AT_bit_size:
3872       return "DW_AT_bit_size";
3873     case DW_AT_element_list:
3874       return "DW_AT_element_list";
3875     case DW_AT_stmt_list:
3876       return "DW_AT_stmt_list";
3877     case DW_AT_low_pc:
3878       return "DW_AT_low_pc";
3879     case DW_AT_high_pc:
3880       return "DW_AT_high_pc";
3881     case DW_AT_language:
3882       return "DW_AT_language";
3883     case DW_AT_member:
3884       return "DW_AT_member";
3885     case DW_AT_discr:
3886       return "DW_AT_discr";
3887     case DW_AT_discr_value:
3888       return "DW_AT_discr_value";
3889     case DW_AT_visibility:
3890       return "DW_AT_visibility";
3891     case DW_AT_import:
3892       return "DW_AT_import";
3893     case DW_AT_string_length:
3894       return "DW_AT_string_length";
3895     case DW_AT_common_reference:
3896       return "DW_AT_common_reference";
3897     case DW_AT_comp_dir:
3898       return "DW_AT_comp_dir";
3899     case DW_AT_const_value:
3900       return "DW_AT_const_value";
3901     case DW_AT_containing_type:
3902       return "DW_AT_containing_type";
3903     case DW_AT_default_value:
3904       return "DW_AT_default_value";
3905     case DW_AT_inline:
3906       return "DW_AT_inline";
3907     case DW_AT_is_optional:
3908       return "DW_AT_is_optional";
3909     case DW_AT_lower_bound:
3910       return "DW_AT_lower_bound";
3911     case DW_AT_producer:
3912       return "DW_AT_producer";
3913     case DW_AT_prototyped:
3914       return "DW_AT_prototyped";
3915     case DW_AT_return_addr:
3916       return "DW_AT_return_addr";
3917     case DW_AT_start_scope:
3918       return "DW_AT_start_scope";
3919     case DW_AT_bit_stride:
3920       return "DW_AT_bit_stride";
3921     case DW_AT_upper_bound:
3922       return "DW_AT_upper_bound";
3923     case DW_AT_abstract_origin:
3924       return "DW_AT_abstract_origin";
3925     case DW_AT_accessibility:
3926       return "DW_AT_accessibility";
3927     case DW_AT_address_class:
3928       return "DW_AT_address_class";
3929     case DW_AT_artificial:
3930       return "DW_AT_artificial";
3931     case DW_AT_base_types:
3932       return "DW_AT_base_types";
3933     case DW_AT_calling_convention:
3934       return "DW_AT_calling_convention";
3935     case DW_AT_count:
3936       return "DW_AT_count";
3937     case DW_AT_data_member_location:
3938       return "DW_AT_data_member_location";
3939     case DW_AT_decl_column:
3940       return "DW_AT_decl_column";
3941     case DW_AT_decl_file:
3942       return "DW_AT_decl_file";
3943     case DW_AT_decl_line:
3944       return "DW_AT_decl_line";
3945     case DW_AT_declaration:
3946       return "DW_AT_declaration";
3947     case DW_AT_discr_list:
3948       return "DW_AT_discr_list";
3949     case DW_AT_encoding:
3950       return "DW_AT_encoding";
3951     case DW_AT_external:
3952       return "DW_AT_external";
3953     case DW_AT_explicit:
3954       return "DW_AT_explicit";
3955     case DW_AT_frame_base:
3956       return "DW_AT_frame_base";
3957     case DW_AT_friend:
3958       return "DW_AT_friend";
3959     case DW_AT_identifier_case:
3960       return "DW_AT_identifier_case";
3961     case DW_AT_macro_info:
3962       return "DW_AT_macro_info";
3963     case DW_AT_namelist_items:
3964       return "DW_AT_namelist_items";
3965     case DW_AT_priority:
3966       return "DW_AT_priority";
3967     case DW_AT_segment:
3968       return "DW_AT_segment";
3969     case DW_AT_specification:
3970       return "DW_AT_specification";
3971     case DW_AT_static_link:
3972       return "DW_AT_static_link";
3973     case DW_AT_type:
3974       return "DW_AT_type";
3975     case DW_AT_use_location:
3976       return "DW_AT_use_location";
3977     case DW_AT_variable_parameter:
3978       return "DW_AT_variable_parameter";
3979     case DW_AT_virtuality:
3980       return "DW_AT_virtuality";
3981     case DW_AT_vtable_elem_location:
3982       return "DW_AT_vtable_elem_location";
3983
3984     case DW_AT_allocated:
3985       return "DW_AT_allocated";
3986     case DW_AT_associated:
3987       return "DW_AT_associated";
3988     case DW_AT_data_location:
3989       return "DW_AT_data_location";
3990     case DW_AT_byte_stride:
3991       return "DW_AT_byte_stride";
3992     case DW_AT_entry_pc:
3993       return "DW_AT_entry_pc";
3994     case DW_AT_use_UTF8:
3995       return "DW_AT_use_UTF8";
3996     case DW_AT_extension:
3997       return "DW_AT_extension";
3998     case DW_AT_ranges:
3999       return "DW_AT_ranges";
4000     case DW_AT_trampoline:
4001       return "DW_AT_trampoline";
4002     case DW_AT_call_column:
4003       return "DW_AT_call_column";
4004     case DW_AT_call_file:
4005       return "DW_AT_call_file";
4006     case DW_AT_call_line:
4007       return "DW_AT_call_line";
4008     case DW_AT_object_pointer:
4009       return "DW_AT_object_pointer";
4010
4011     case DW_AT_signature:
4012       return "DW_AT_signature";
4013     case DW_AT_main_subprogram:
4014       return "DW_AT_main_subprogram";
4015     case DW_AT_data_bit_offset:
4016       return "DW_AT_data_bit_offset";
4017     case DW_AT_const_expr:
4018       return "DW_AT_const_expr";
4019     case DW_AT_enum_class:
4020       return "DW_AT_enum_class";
4021     case DW_AT_linkage_name:
4022       return "DW_AT_linkage_name";
4023
4024     case DW_AT_MIPS_fde:
4025       return "DW_AT_MIPS_fde";
4026     case DW_AT_MIPS_loop_begin:
4027       return "DW_AT_MIPS_loop_begin";
4028     case DW_AT_MIPS_tail_loop_begin:
4029       return "DW_AT_MIPS_tail_loop_begin";
4030     case DW_AT_MIPS_epilog_begin:
4031       return "DW_AT_MIPS_epilog_begin";
4032 #if VMS_DEBUGGING_INFO
4033     case DW_AT_HP_prologue:
4034       return "DW_AT_HP_prologue";
4035 #else
4036     case DW_AT_MIPS_loop_unroll_factor:
4037       return "DW_AT_MIPS_loop_unroll_factor";
4038 #endif
4039     case DW_AT_MIPS_software_pipeline_depth:
4040       return "DW_AT_MIPS_software_pipeline_depth";
4041     case DW_AT_MIPS_linkage_name:
4042       return "DW_AT_MIPS_linkage_name";
4043 #if VMS_DEBUGGING_INFO
4044     case DW_AT_HP_epilogue:
4045       return "DW_AT_HP_epilogue";
4046 #else
4047     case DW_AT_MIPS_stride:
4048       return "DW_AT_MIPS_stride";
4049 #endif
4050     case DW_AT_MIPS_abstract_name:
4051       return "DW_AT_MIPS_abstract_name";
4052     case DW_AT_MIPS_clone_origin:
4053       return "DW_AT_MIPS_clone_origin";
4054     case DW_AT_MIPS_has_inlines:
4055       return "DW_AT_MIPS_has_inlines";
4056
4057     case DW_AT_sf_names:
4058       return "DW_AT_sf_names";
4059     case DW_AT_src_info:
4060       return "DW_AT_src_info";
4061     case DW_AT_mac_info:
4062       return "DW_AT_mac_info";
4063     case DW_AT_src_coords:
4064       return "DW_AT_src_coords";
4065     case DW_AT_body_begin:
4066       return "DW_AT_body_begin";
4067     case DW_AT_body_end:
4068       return "DW_AT_body_end";
4069
4070     case DW_AT_GNU_vector:
4071       return "DW_AT_GNU_vector";
4072     case DW_AT_GNU_guarded_by:
4073       return "DW_AT_GNU_guarded_by";
4074     case DW_AT_GNU_pt_guarded_by:
4075       return "DW_AT_GNU_pt_guarded_by";
4076     case DW_AT_GNU_guarded:
4077       return "DW_AT_GNU_guarded";
4078     case DW_AT_GNU_pt_guarded:
4079       return "DW_AT_GNU_pt_guarded";
4080     case DW_AT_GNU_locks_excluded:
4081       return "DW_AT_GNU_locks_excluded";
4082     case DW_AT_GNU_exclusive_locks_required:
4083       return "DW_AT_GNU_exclusive_locks_required";
4084     case DW_AT_GNU_shared_locks_required:
4085       return "DW_AT_GNU_shared_locks_required";
4086     case DW_AT_GNU_odr_signature:
4087       return "DW_AT_GNU_odr_signature";
4088     case DW_AT_GNU_template_name:
4089       return "DW_AT_GNU_template_name";
4090     case DW_AT_GNU_call_site_value:
4091       return "DW_AT_GNU_call_site_value";
4092     case DW_AT_GNU_call_site_data_value:
4093       return "DW_AT_GNU_call_site_data_value";
4094     case DW_AT_GNU_call_site_target:
4095       return "DW_AT_GNU_call_site_target";
4096     case DW_AT_GNU_call_site_target_clobbered:
4097       return "DW_AT_GNU_call_site_target_clobbered";
4098     case DW_AT_GNU_tail_call:
4099       return "DW_AT_GNU_tail_call";
4100     case DW_AT_GNU_all_tail_call_sites:
4101       return "DW_AT_GNU_all_tail_call_sites";
4102     case DW_AT_GNU_all_call_sites:
4103       return "DW_AT_GNU_all_call_sites";
4104     case DW_AT_GNU_all_source_call_sites:
4105       return "DW_AT_GNU_all_source_call_sites";
4106     case DW_AT_GNU_macros:
4107       return "DW_AT_GNU_macros";
4108
4109     case DW_AT_GNAT_descriptive_type:
4110       return "DW_AT_GNAT_descriptive_type";
4111
4112     case DW_AT_VMS_rtnbeg_pd_address:
4113       return "DW_AT_VMS_rtnbeg_pd_address";
4114
4115     default:
4116       return "DW_AT_<unknown>";
4117     }
4118 }
4119
4120 /* Convert a DWARF value form code into its string name.  */
4121
4122 static const char *
4123 dwarf_form_name (unsigned int form)
4124 {
4125   switch (form)
4126     {
4127     case DW_FORM_addr:
4128       return "DW_FORM_addr";
4129     case DW_FORM_block2:
4130       return "DW_FORM_block2";
4131     case DW_FORM_block4:
4132       return "DW_FORM_block4";
4133     case DW_FORM_data2:
4134       return "DW_FORM_data2";
4135     case DW_FORM_data4:
4136       return "DW_FORM_data4";
4137     case DW_FORM_data8:
4138       return "DW_FORM_data8";
4139     case DW_FORM_string:
4140       return "DW_FORM_string";
4141     case DW_FORM_block:
4142       return "DW_FORM_block";
4143     case DW_FORM_block1:
4144       return "DW_FORM_block1";
4145     case DW_FORM_data1:
4146       return "DW_FORM_data1";
4147     case DW_FORM_flag:
4148       return "DW_FORM_flag";
4149     case DW_FORM_sdata:
4150       return "DW_FORM_sdata";
4151     case DW_FORM_strp:
4152       return "DW_FORM_strp";
4153     case DW_FORM_udata:
4154       return "DW_FORM_udata";
4155     case DW_FORM_ref_addr:
4156       return "DW_FORM_ref_addr";
4157     case DW_FORM_ref1:
4158       return "DW_FORM_ref1";
4159     case DW_FORM_ref2:
4160       return "DW_FORM_ref2";
4161     case DW_FORM_ref4:
4162       return "DW_FORM_ref4";
4163     case DW_FORM_ref8:
4164       return "DW_FORM_ref8";
4165     case DW_FORM_ref_udata:
4166       return "DW_FORM_ref_udata";
4167     case DW_FORM_indirect:
4168       return "DW_FORM_indirect";
4169     case DW_FORM_sec_offset:
4170       return "DW_FORM_sec_offset";
4171     case DW_FORM_exprloc:
4172       return "DW_FORM_exprloc";
4173     case DW_FORM_flag_present:
4174       return "DW_FORM_flag_present";
4175     case DW_FORM_ref_sig8:
4176       return "DW_FORM_ref_sig8";
4177     default:
4178       return "DW_FORM_<unknown>";
4179     }
4180 }
4181 \f
4182 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4183    instance of an inlined instance of a decl which is local to an inline
4184    function, so we have to trace all of the way back through the origin chain
4185    to find out what sort of node actually served as the original seed for the
4186    given block.  */
4187
4188 static tree
4189 decl_ultimate_origin (const_tree decl)
4190 {
4191   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4192     return NULL_TREE;
4193
4194   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4195      nodes in the function to point to themselves; ignore that if
4196      we're trying to output the abstract instance of this function.  */
4197   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4198     return NULL_TREE;
4199
4200   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4201      most distant ancestor, this should never happen.  */
4202   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4203
4204   return DECL_ABSTRACT_ORIGIN (decl);
4205 }
4206
4207 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4208    of a virtual function may refer to a base class, so we check the 'this'
4209    parameter.  */
4210
4211 static tree
4212 decl_class_context (tree decl)
4213 {
4214   tree context = NULL_TREE;
4215
4216   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4217     context = DECL_CONTEXT (decl);
4218   else
4219     context = TYPE_MAIN_VARIANT
4220       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4221
4222   if (context && !TYPE_P (context))
4223     context = NULL_TREE;
4224
4225   return context;
4226 }
4227 \f
4228 /* Add an attribute/value pair to a DIE.  */
4229
4230 static inline void
4231 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4232 {
4233   /* Maybe this should be an assert?  */
4234   if (die == NULL)
4235     return;
4236
4237   if (die->die_attr == NULL)
4238     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4239   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4240 }
4241
4242 static inline enum dw_val_class
4243 AT_class (dw_attr_ref a)
4244 {
4245   return a->dw_attr_val.val_class;
4246 }
4247
4248 /* Add a flag value attribute to a DIE.  */
4249
4250 static inline void
4251 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4252 {
4253   dw_attr_node attr;
4254
4255   attr.dw_attr = attr_kind;
4256   attr.dw_attr_val.val_class = dw_val_class_flag;
4257   attr.dw_attr_val.v.val_flag = flag;
4258   add_dwarf_attr (die, &attr);
4259 }
4260
4261 static inline unsigned
4262 AT_flag (dw_attr_ref a)
4263 {
4264   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4265   return a->dw_attr_val.v.val_flag;
4266 }
4267
4268 /* Add a signed integer attribute value to a DIE.  */
4269
4270 static inline void
4271 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4272 {
4273   dw_attr_node attr;
4274
4275   attr.dw_attr = attr_kind;
4276   attr.dw_attr_val.val_class = dw_val_class_const;
4277   attr.dw_attr_val.v.val_int = int_val;
4278   add_dwarf_attr (die, &attr);
4279 }
4280
4281 static inline HOST_WIDE_INT
4282 AT_int (dw_attr_ref a)
4283 {
4284   gcc_assert (a && AT_class (a) == dw_val_class_const);
4285   return a->dw_attr_val.v.val_int;
4286 }
4287
4288 /* Add an unsigned integer attribute value to a DIE.  */
4289
4290 static inline void
4291 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4292                  unsigned HOST_WIDE_INT unsigned_val)
4293 {
4294   dw_attr_node attr;
4295
4296   attr.dw_attr = attr_kind;
4297   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4298   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4299   add_dwarf_attr (die, &attr);
4300 }
4301
4302 static inline unsigned HOST_WIDE_INT
4303 AT_unsigned (dw_attr_ref a)
4304 {
4305   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4306   return a->dw_attr_val.v.val_unsigned;
4307 }
4308
4309 /* Add an unsigned double integer attribute value to a DIE.  */
4310
4311 static inline void
4312 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4313                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4314 {
4315   dw_attr_node attr;
4316
4317   attr.dw_attr = attr_kind;
4318   attr.dw_attr_val.val_class = dw_val_class_const_double;
4319   attr.dw_attr_val.v.val_double.high = high;
4320   attr.dw_attr_val.v.val_double.low = low;
4321   add_dwarf_attr (die, &attr);
4322 }
4323
4324 /* Add a floating point attribute value to a DIE and return it.  */
4325
4326 static inline void
4327 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4328             unsigned int length, unsigned int elt_size, unsigned char *array)
4329 {
4330   dw_attr_node attr;
4331
4332   attr.dw_attr = attr_kind;
4333   attr.dw_attr_val.val_class = dw_val_class_vec;
4334   attr.dw_attr_val.v.val_vec.length = length;
4335   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4336   attr.dw_attr_val.v.val_vec.array = array;
4337   add_dwarf_attr (die, &attr);
4338 }
4339
4340 /* Add an 8-byte data attribute value to a DIE.  */
4341
4342 static inline void
4343 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4344               unsigned char data8[8])
4345 {
4346   dw_attr_node attr;
4347
4348   attr.dw_attr = attr_kind;
4349   attr.dw_attr_val.val_class = dw_val_class_data8;
4350   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4351   add_dwarf_attr (die, &attr);
4352 }
4353
4354 /* Hash and equality functions for debug_str_hash.  */
4355
4356 static hashval_t
4357 debug_str_do_hash (const void *x)
4358 {
4359   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4360 }
4361
4362 static int
4363 debug_str_eq (const void *x1, const void *x2)
4364 {
4365   return strcmp ((((const struct indirect_string_node *)x1)->str),
4366                  (const char *)x2) == 0;
4367 }
4368
4369 /* Add STR to the indirect string hash table.  */
4370
4371 static struct indirect_string_node *
4372 find_AT_string (const char *str)
4373 {
4374   struct indirect_string_node *node;
4375   void **slot;
4376
4377   if (! debug_str_hash)
4378     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4379                                       debug_str_eq, NULL);
4380
4381   slot = htab_find_slot_with_hash (debug_str_hash, str,
4382                                    htab_hash_string (str), INSERT);
4383   if (*slot == NULL)
4384     {
4385       node = ggc_alloc_cleared_indirect_string_node ();
4386       node->str = ggc_strdup (str);
4387       *slot = node;
4388     }
4389   else
4390     node = (struct indirect_string_node *) *slot;
4391
4392   node->refcount++;
4393   return node;
4394 }
4395
4396 /* Add a string attribute value to a DIE.  */
4397
4398 static inline void
4399 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4400 {
4401   dw_attr_node attr;
4402   struct indirect_string_node *node;
4403
4404   node = find_AT_string (str);
4405
4406   attr.dw_attr = attr_kind;
4407   attr.dw_attr_val.val_class = dw_val_class_str;
4408   attr.dw_attr_val.v.val_str = node;
4409   add_dwarf_attr (die, &attr);
4410 }
4411
4412 static inline const char *
4413 AT_string (dw_attr_ref a)
4414 {
4415   gcc_assert (a && AT_class (a) == dw_val_class_str);
4416   return a->dw_attr_val.v.val_str->str;
4417 }
4418
4419 /* Find out whether a string should be output inline in DIE
4420    or out-of-line in .debug_str section.  */
4421
4422 static enum dwarf_form
4423 AT_string_form (dw_attr_ref a)
4424 {
4425   struct indirect_string_node *node;
4426   unsigned int len;
4427   char label[32];
4428
4429   gcc_assert (a && AT_class (a) == dw_val_class_str);
4430
4431   node = a->dw_attr_val.v.val_str;
4432   if (node->form)
4433     return node->form;
4434
4435   len = strlen (node->str) + 1;
4436
4437   /* If the string is shorter or equal to the size of the reference, it is
4438      always better to put it inline.  */
4439   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4440     return node->form = DW_FORM_string;
4441
4442   /* If we cannot expect the linker to merge strings in .debug_str
4443      section, only put it into .debug_str if it is worth even in this
4444      single module.  */
4445   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4446       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4447       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4448     return node->form = DW_FORM_string;
4449
4450   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4451   ++dw2_string_counter;
4452   node->label = xstrdup (label);
4453
4454   return node->form = DW_FORM_strp;
4455 }
4456
4457 /* Add a DIE reference attribute value to a DIE.  */
4458
4459 static inline void
4460 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4461 {
4462   dw_attr_node attr;
4463
4464 #ifdef ENABLE_CHECKING
4465   gcc_assert (targ_die != NULL);
4466 #else
4467   /* With LTO we can end up trying to reference something we didn't create
4468      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4469   if (targ_die == NULL)
4470     return;
4471 #endif
4472
4473   attr.dw_attr = attr_kind;
4474   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4475   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4476   attr.dw_attr_val.v.val_die_ref.external = 0;
4477   add_dwarf_attr (die, &attr);
4478 }
4479
4480 /* Add an AT_specification attribute to a DIE, and also make the back
4481    pointer from the specification to the definition.  */
4482
4483 static inline void
4484 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4485 {
4486   add_AT_die_ref (die, DW_AT_specification, targ_die);
4487   gcc_assert (!targ_die->die_definition);
4488   targ_die->die_definition = die;
4489 }
4490
4491 static inline dw_die_ref
4492 AT_ref (dw_attr_ref a)
4493 {
4494   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4495   return a->dw_attr_val.v.val_die_ref.die;
4496 }
4497
4498 static inline int
4499 AT_ref_external (dw_attr_ref a)
4500 {
4501   if (a && AT_class (a) == dw_val_class_die_ref)
4502     return a->dw_attr_val.v.val_die_ref.external;
4503
4504   return 0;
4505 }
4506
4507 static inline void
4508 set_AT_ref_external (dw_attr_ref a, int i)
4509 {
4510   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4511   a->dw_attr_val.v.val_die_ref.external = i;
4512 }
4513
4514 /* Add an FDE reference attribute value to a DIE.  */
4515
4516 static inline void
4517 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4518 {
4519   dw_attr_node attr;
4520
4521   attr.dw_attr = attr_kind;
4522   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4523   attr.dw_attr_val.v.val_fde_index = targ_fde;
4524   add_dwarf_attr (die, &attr);
4525 }
4526
4527 /* Add a location description attribute value to a DIE.  */
4528
4529 static inline void
4530 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4531 {
4532   dw_attr_node attr;
4533
4534   attr.dw_attr = attr_kind;
4535   attr.dw_attr_val.val_class = dw_val_class_loc;
4536   attr.dw_attr_val.v.val_loc = loc;
4537   add_dwarf_attr (die, &attr);
4538 }
4539
4540 static inline dw_loc_descr_ref
4541 AT_loc (dw_attr_ref a)
4542 {
4543   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4544   return a->dw_attr_val.v.val_loc;
4545 }
4546
4547 static inline void
4548 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4549 {
4550   dw_attr_node attr;
4551
4552   attr.dw_attr = attr_kind;
4553   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4554   attr.dw_attr_val.v.val_loc_list = loc_list;
4555   add_dwarf_attr (die, &attr);
4556   have_location_lists = true;
4557 }
4558
4559 static inline dw_loc_list_ref
4560 AT_loc_list (dw_attr_ref a)
4561 {
4562   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4563   return a->dw_attr_val.v.val_loc_list;
4564 }
4565
4566 static inline dw_loc_list_ref *
4567 AT_loc_list_ptr (dw_attr_ref a)
4568 {
4569   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4570   return &a->dw_attr_val.v.val_loc_list;
4571 }
4572
4573 /* Add an address constant attribute value to a DIE.  */
4574
4575 static inline void
4576 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4577 {
4578   dw_attr_node attr;
4579
4580   attr.dw_attr = attr_kind;
4581   attr.dw_attr_val.val_class = dw_val_class_addr;
4582   attr.dw_attr_val.v.val_addr = addr;
4583   add_dwarf_attr (die, &attr);
4584 }
4585
4586 /* Get the RTX from to an address DIE attribute.  */
4587
4588 static inline rtx
4589 AT_addr (dw_attr_ref a)
4590 {
4591   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4592   return a->dw_attr_val.v.val_addr;
4593 }
4594
4595 /* Add a file attribute value to a DIE.  */
4596
4597 static inline void
4598 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4599              struct dwarf_file_data *fd)
4600 {
4601   dw_attr_node attr;
4602
4603   attr.dw_attr = attr_kind;
4604   attr.dw_attr_val.val_class = dw_val_class_file;
4605   attr.dw_attr_val.v.val_file = fd;
4606   add_dwarf_attr (die, &attr);
4607 }
4608
4609 /* Get the dwarf_file_data from a file DIE attribute.  */
4610
4611 static inline struct dwarf_file_data *
4612 AT_file (dw_attr_ref a)
4613 {
4614   gcc_assert (a && AT_class (a) == dw_val_class_file);
4615   return a->dw_attr_val.v.val_file;
4616 }
4617
4618 /* Add a vms delta attribute value to a DIE.  */
4619
4620 static inline void
4621 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4622                   const char *lbl1, const char *lbl2)
4623 {
4624   dw_attr_node attr;
4625
4626   attr.dw_attr = attr_kind;
4627   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4628   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4629   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4630   add_dwarf_attr (die, &attr);
4631 }
4632
4633 /* Add a label identifier attribute value to a DIE.  */
4634
4635 static inline void
4636 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4637 {
4638   dw_attr_node attr;
4639
4640   attr.dw_attr = attr_kind;
4641   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4642   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4643   add_dwarf_attr (die, &attr);
4644 }
4645
4646 /* Add a section offset attribute value to a DIE, an offset into the
4647    debug_line section.  */
4648
4649 static inline void
4650 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4651                 const char *label)
4652 {
4653   dw_attr_node attr;
4654
4655   attr.dw_attr = attr_kind;
4656   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4657   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4658   add_dwarf_attr (die, &attr);
4659 }
4660
4661 /* Add a section offset attribute value to a DIE, an offset into the
4662    debug_macinfo section.  */
4663
4664 static inline void
4665 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4666                const char *label)
4667 {
4668   dw_attr_node attr;
4669
4670   attr.dw_attr = attr_kind;
4671   attr.dw_attr_val.val_class = dw_val_class_macptr;
4672   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4673   add_dwarf_attr (die, &attr);
4674 }
4675
4676 /* Add an offset attribute value to a DIE.  */
4677
4678 static inline void
4679 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4680                unsigned HOST_WIDE_INT offset)
4681 {
4682   dw_attr_node attr;
4683
4684   attr.dw_attr = attr_kind;
4685   attr.dw_attr_val.val_class = dw_val_class_offset;
4686   attr.dw_attr_val.v.val_offset = offset;
4687   add_dwarf_attr (die, &attr);
4688 }
4689
4690 /* Add an range_list attribute value to a DIE.  */
4691
4692 static void
4693 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4694                    long unsigned int offset)
4695 {
4696   dw_attr_node attr;
4697
4698   attr.dw_attr = attr_kind;
4699   attr.dw_attr_val.val_class = dw_val_class_range_list;
4700   attr.dw_attr_val.v.val_offset = offset;
4701   add_dwarf_attr (die, &attr);
4702 }
4703
4704 /* Return the start label of a delta attribute.  */
4705
4706 static inline const char *
4707 AT_vms_delta1 (dw_attr_ref a)
4708 {
4709   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4710   return a->dw_attr_val.v.val_vms_delta.lbl1;
4711 }
4712
4713 /* Return the end label of a delta attribute.  */
4714
4715 static inline const char *
4716 AT_vms_delta2 (dw_attr_ref a)
4717 {
4718   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4719   return a->dw_attr_val.v.val_vms_delta.lbl2;
4720 }
4721
4722 static inline const char *
4723 AT_lbl (dw_attr_ref a)
4724 {
4725   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4726                     || AT_class (a) == dw_val_class_lineptr
4727                     || AT_class (a) == dw_val_class_macptr));
4728   return a->dw_attr_val.v.val_lbl_id;
4729 }
4730
4731 /* Get the attribute of type attr_kind.  */
4732
4733 static dw_attr_ref
4734 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4735 {
4736   dw_attr_ref a;
4737   unsigned ix;
4738   dw_die_ref spec = NULL;
4739
4740   if (! die)
4741     return NULL;
4742
4743   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4744     if (a->dw_attr == attr_kind)
4745       return a;
4746     else if (a->dw_attr == DW_AT_specification
4747              || a->dw_attr == DW_AT_abstract_origin)
4748       spec = AT_ref (a);
4749
4750   if (spec)
4751     return get_AT (spec, attr_kind);
4752
4753   return NULL;
4754 }
4755
4756 /* Return the "low pc" attribute value, typically associated with a subprogram
4757    DIE.  Return null if the "low pc" attribute is either not present, or if it
4758    cannot be represented as an assembler label identifier.  */
4759
4760 static inline const char *
4761 get_AT_low_pc (dw_die_ref die)
4762 {
4763   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4764
4765   return a ? AT_lbl (a) : NULL;
4766 }
4767
4768 /* Return the "high pc" attribute value, typically associated with a subprogram
4769    DIE.  Return null if the "high pc" attribute is either not present, or if it
4770    cannot be represented as an assembler label identifier.  */
4771
4772 static inline const char *
4773 get_AT_hi_pc (dw_die_ref die)
4774 {
4775   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4776
4777   return a ? AT_lbl (a) : NULL;
4778 }
4779
4780 /* Return the value of the string attribute designated by ATTR_KIND, or
4781    NULL if it is not present.  */
4782
4783 static inline const char *
4784 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4785 {
4786   dw_attr_ref a = get_AT (die, attr_kind);
4787
4788   return a ? AT_string (a) : NULL;
4789 }
4790
4791 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4792    if it is not present.  */
4793
4794 static inline int
4795 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4796 {
4797   dw_attr_ref a = get_AT (die, attr_kind);
4798
4799   return a ? AT_flag (a) : 0;
4800 }
4801
4802 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4803    if it is not present.  */
4804
4805 static inline unsigned
4806 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4807 {
4808   dw_attr_ref a = get_AT (die, attr_kind);
4809
4810   return a ? AT_unsigned (a) : 0;
4811 }
4812
4813 static inline dw_die_ref
4814 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4815 {
4816   dw_attr_ref a = get_AT (die, attr_kind);
4817
4818   return a ? AT_ref (a) : NULL;
4819 }
4820
4821 static inline struct dwarf_file_data *
4822 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4823 {
4824   dw_attr_ref a = get_AT (die, attr_kind);
4825
4826   return a ? AT_file (a) : NULL;
4827 }
4828
4829 /* Return TRUE if the language is C++.  */
4830
4831 static inline bool
4832 is_cxx (void)
4833 {
4834   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4835
4836   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4837 }
4838
4839 /* Return TRUE if the language is Fortran.  */
4840
4841 static inline bool
4842 is_fortran (void)
4843 {
4844   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4845
4846   return (lang == DW_LANG_Fortran77
4847           || lang == DW_LANG_Fortran90
4848           || lang == DW_LANG_Fortran95);
4849 }
4850
4851 /* Return TRUE if the language is Ada.  */
4852
4853 static inline bool
4854 is_ada (void)
4855 {
4856   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4857
4858   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4859 }
4860
4861 /* Remove the specified attribute if present.  */
4862
4863 static void
4864 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4865 {
4866   dw_attr_ref a;
4867   unsigned ix;
4868
4869   if (! die)
4870     return;
4871
4872   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4873     if (a->dw_attr == attr_kind)
4874       {
4875         if (AT_class (a) == dw_val_class_str)
4876           if (a->dw_attr_val.v.val_str->refcount)
4877             a->dw_attr_val.v.val_str->refcount--;
4878
4879         /* VEC_ordered_remove should help reduce the number of abbrevs
4880            that are needed.  */
4881         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4882         return;
4883       }
4884 }
4885
4886 /* Remove CHILD from its parent.  PREV must have the property that
4887    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4888
4889 static void
4890 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4891 {
4892   gcc_assert (child->die_parent == prev->die_parent);
4893   gcc_assert (prev->die_sib == child);
4894   if (prev == child)
4895     {
4896       gcc_assert (child->die_parent->die_child == child);
4897       prev = NULL;
4898     }
4899   else
4900     prev->die_sib = child->die_sib;
4901   if (child->die_parent->die_child == child)
4902     child->die_parent->die_child = prev;
4903 }
4904
4905 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4906    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4907
4908 static void
4909 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4910 {
4911   dw_die_ref parent = old_child->die_parent;
4912
4913   gcc_assert (parent == prev->die_parent);
4914   gcc_assert (prev->die_sib == old_child);
4915
4916   new_child->die_parent = parent;
4917   if (prev == old_child)
4918     {
4919       gcc_assert (parent->die_child == old_child);
4920       new_child->die_sib = new_child;
4921     }
4922   else
4923     {
4924       prev->die_sib = new_child;
4925       new_child->die_sib = old_child->die_sib;
4926     }
4927   if (old_child->die_parent->die_child == old_child)
4928     old_child->die_parent->die_child = new_child;
4929 }
4930
4931 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4932
4933 static void
4934 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4935 {
4936   dw_die_ref c;
4937   new_parent->die_child = old_parent->die_child;
4938   old_parent->die_child = NULL;
4939   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4940 }
4941
4942 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4943    matches TAG.  */
4944
4945 static void
4946 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4947 {
4948   dw_die_ref c;
4949
4950   c = die->die_child;
4951   if (c) do {
4952     dw_die_ref prev = c;
4953     c = c->die_sib;
4954     while (c->die_tag == tag)
4955       {
4956         remove_child_with_prev (c, prev);
4957         /* Might have removed every child.  */
4958         if (c == c->die_sib)
4959           return;
4960         c = c->die_sib;
4961       }
4962   } while (c != die->die_child);
4963 }
4964
4965 /* Add a CHILD_DIE as the last child of DIE.  */
4966
4967 static void
4968 add_child_die (dw_die_ref die, dw_die_ref child_die)
4969 {
4970   /* FIXME this should probably be an assert.  */
4971   if (! die || ! child_die)
4972     return;
4973   gcc_assert (die != child_die);
4974
4975   child_die->die_parent = die;
4976   if (die->die_child)
4977     {
4978       child_die->die_sib = die->die_child->die_sib;
4979       die->die_child->die_sib = child_die;
4980     }
4981   else
4982     child_die->die_sib = child_die;
4983   die->die_child = child_die;
4984 }
4985
4986 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4987    is the specification, to the end of PARENT's list of children.
4988    This is done by removing and re-adding it.  */
4989
4990 static void
4991 splice_child_die (dw_die_ref parent, dw_die_ref child)
4992 {
4993   dw_die_ref p;
4994
4995   /* We want the declaration DIE from inside the class, not the
4996      specification DIE at toplevel.  */
4997   if (child->die_parent != parent)
4998     {
4999       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5000
5001       if (tmp)
5002         child = tmp;
5003     }
5004
5005   gcc_assert (child->die_parent == parent
5006               || (child->die_parent
5007                   == get_AT_ref (parent, DW_AT_specification)));
5008
5009   for (p = child->die_parent->die_child; ; p = p->die_sib)
5010     if (p->die_sib == child)
5011       {
5012         remove_child_with_prev (child, p);
5013         break;
5014       }
5015
5016   add_child_die (parent, child);
5017 }
5018
5019 /* Return a pointer to a newly created DIE node.  */
5020
5021 static inline dw_die_ref
5022 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5023 {
5024   dw_die_ref die = ggc_alloc_cleared_die_node ();
5025
5026   die->die_tag = tag_value;
5027
5028   if (parent_die != NULL)
5029     add_child_die (parent_die, die);
5030   else
5031     {
5032       limbo_die_node *limbo_node;
5033
5034       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5035       limbo_node->die = die;
5036       limbo_node->created_for = t;
5037       limbo_node->next = limbo_die_list;
5038       limbo_die_list = limbo_node;
5039     }
5040
5041   return die;
5042 }
5043
5044 /* Return the DIE associated with the given type specifier.  */
5045
5046 static inline dw_die_ref
5047 lookup_type_die (tree type)
5048 {
5049   return TYPE_SYMTAB_DIE (type);
5050 }
5051
5052 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5053    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5054    anonymous type instead the one of the naming typedef.  */
5055
5056 static inline dw_die_ref
5057 strip_naming_typedef (tree type, dw_die_ref type_die)
5058 {
5059   if (type
5060       && TREE_CODE (type) == RECORD_TYPE
5061       && type_die
5062       && type_die->die_tag == DW_TAG_typedef
5063       && is_naming_typedef_decl (TYPE_NAME (type)))
5064     type_die = get_AT_ref (type_die, DW_AT_type);
5065   return type_die;
5066 }
5067
5068 /* Like lookup_type_die, but if type is an anonymous type named by a
5069    typedef[1], return the DIE of the anonymous type instead the one of
5070    the naming typedef.  This is because in gen_typedef_die, we did
5071    equate the anonymous struct named by the typedef with the DIE of
5072    the naming typedef. So by default, lookup_type_die on an anonymous
5073    struct yields the DIE of the naming typedef.
5074
5075    [1]: Read the comment of is_naming_typedef_decl to learn about what
5076    a naming typedef is.  */
5077
5078 static inline dw_die_ref
5079 lookup_type_die_strip_naming_typedef (tree type)
5080 {
5081   dw_die_ref die = lookup_type_die (type);
5082   return strip_naming_typedef (type, die);
5083 }
5084
5085 /* Equate a DIE to a given type specifier.  */
5086
5087 static inline void
5088 equate_type_number_to_die (tree type, dw_die_ref type_die)
5089 {
5090   TYPE_SYMTAB_DIE (type) = type_die;
5091 }
5092
5093 /* Returns a hash value for X (which really is a die_struct).  */
5094
5095 static hashval_t
5096 decl_die_table_hash (const void *x)
5097 {
5098   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5099 }
5100
5101 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5102
5103 static int
5104 decl_die_table_eq (const void *x, const void *y)
5105 {
5106   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5107 }
5108
5109 /* Return the DIE associated with a given declaration.  */
5110
5111 static inline dw_die_ref
5112 lookup_decl_die (tree decl)
5113 {
5114   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5115 }
5116
5117 /* Returns a hash value for X (which really is a var_loc_list).  */
5118
5119 static hashval_t
5120 decl_loc_table_hash (const void *x)
5121 {
5122   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5123 }
5124
5125 /* Return nonzero if decl_id of var_loc_list X is the same as
5126    UID of decl *Y.  */
5127
5128 static int
5129 decl_loc_table_eq (const void *x, const void *y)
5130 {
5131   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5132 }
5133
5134 /* Return the var_loc list associated with a given declaration.  */
5135
5136 static inline var_loc_list *
5137 lookup_decl_loc (const_tree decl)
5138 {
5139   if (!decl_loc_table)
5140     return NULL;
5141   return (var_loc_list *)
5142     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5143 }
5144
5145 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5146
5147 static hashval_t
5148 cached_dw_loc_list_table_hash (const void *x)
5149 {
5150   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5151 }
5152
5153 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5154    UID of decl *Y.  */
5155
5156 static int
5157 cached_dw_loc_list_table_eq (const void *x, const void *y)
5158 {
5159   return (((const cached_dw_loc_list *) x)->decl_id
5160           == DECL_UID ((const_tree) y));
5161 }
5162
5163 /* Equate a DIE to a particular declaration.  */
5164
5165 static void
5166 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5167 {
5168   unsigned int decl_id = DECL_UID (decl);
5169   void **slot;
5170
5171   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5172   *slot = decl_die;
5173   decl_die->decl_id = decl_id;
5174 }
5175
5176 /* Return how many bits covers PIECE EXPR_LIST.  */
5177
5178 static int
5179 decl_piece_bitsize (rtx piece)
5180 {
5181   int ret = (int) GET_MODE (piece);
5182   if (ret)
5183     return ret;
5184   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5185               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5186   return INTVAL (XEXP (XEXP (piece, 0), 0));
5187 }
5188
5189 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5190
5191 static rtx *
5192 decl_piece_varloc_ptr (rtx piece)
5193 {
5194   if ((int) GET_MODE (piece))
5195     return &XEXP (piece, 0);
5196   else
5197     return &XEXP (XEXP (piece, 0), 1);
5198 }
5199
5200 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5201    Next is the chain of following piece nodes.  */
5202
5203 static rtx
5204 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5205 {
5206   if (bitsize <= (int) MAX_MACHINE_MODE)
5207     return alloc_EXPR_LIST (bitsize, loc_note, next);
5208   else
5209     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5210                                                GEN_INT (bitsize),
5211                                                loc_note), next);
5212 }
5213
5214 /* Return rtx that should be stored into loc field for
5215    LOC_NOTE and BITPOS/BITSIZE.  */
5216
5217 static rtx
5218 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5219                       HOST_WIDE_INT bitsize)
5220 {
5221   if (bitsize != -1)
5222     {
5223       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5224       if (bitpos != 0)
5225         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5226     }
5227   return loc_note;
5228 }
5229
5230 /* This function either modifies location piece list *DEST in
5231    place (if SRC and INNER is NULL), or copies location piece list
5232    *SRC to *DEST while modifying it.  Location BITPOS is modified
5233    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5234    not copied and if needed some padding around it is added.
5235    When modifying in place, DEST should point to EXPR_LIST where
5236    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5237    to the start of the whole list and INNER points to the EXPR_LIST
5238    where earlier pieces cover PIECE_BITPOS bits.  */
5239
5240 static void
5241 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5242                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5243                    HOST_WIDE_INT bitsize, rtx loc_note)
5244 {
5245   int diff;
5246   bool copy = inner != NULL;
5247
5248   if (copy)
5249     {
5250       /* First copy all nodes preceeding the current bitpos.  */
5251       while (src != inner)
5252         {
5253           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5254                                    decl_piece_bitsize (*src), NULL_RTX);
5255           dest = &XEXP (*dest, 1);
5256           src = &XEXP (*src, 1);
5257         }
5258     }
5259   /* Add padding if needed.  */
5260   if (bitpos != piece_bitpos)
5261     {
5262       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5263                                copy ? NULL_RTX : *dest);
5264       dest = &XEXP (*dest, 1);
5265     }
5266   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5267     {
5268       gcc_assert (!copy);
5269       /* A piece with correct bitpos and bitsize already exist,
5270          just update the location for it and return.  */
5271       *decl_piece_varloc_ptr (*dest) = loc_note;
5272       return;
5273     }
5274   /* Add the piece that changed.  */
5275   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5276   dest = &XEXP (*dest, 1);
5277   /* Skip over pieces that overlap it.  */
5278   diff = bitpos - piece_bitpos + bitsize;
5279   if (!copy)
5280     src = dest;
5281   while (diff > 0 && *src)
5282     {
5283       rtx piece = *src;
5284       diff -= decl_piece_bitsize (piece);
5285       if (copy)
5286         src = &XEXP (piece, 1);
5287       else
5288         {
5289           *src = XEXP (piece, 1);
5290           free_EXPR_LIST_node (piece);
5291         }
5292     }
5293   /* Add padding if needed.  */
5294   if (diff < 0 && *src)
5295     {
5296       if (!copy)
5297         dest = src;
5298       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5299       dest = &XEXP (*dest, 1);
5300     }
5301   if (!copy)
5302     return;
5303   /* Finally copy all nodes following it.  */
5304   while (*src)
5305     {
5306       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5307                                decl_piece_bitsize (*src), NULL_RTX);
5308       dest = &XEXP (*dest, 1);
5309       src = &XEXP (*src, 1);
5310     }
5311 }
5312
5313 /* Add a variable location node to the linked list for DECL.  */
5314
5315 static struct var_loc_node *
5316 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5317 {
5318   unsigned int decl_id;
5319   var_loc_list *temp;
5320   void **slot;
5321   struct var_loc_node *loc = NULL;
5322   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5323
5324   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5325     {
5326       tree realdecl = DECL_DEBUG_EXPR (decl);
5327       if (realdecl && handled_component_p (realdecl))
5328         {
5329           HOST_WIDE_INT maxsize;
5330           tree innerdecl;
5331           innerdecl
5332             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5333           if (!DECL_P (innerdecl)
5334               || DECL_IGNORED_P (innerdecl)
5335               || TREE_STATIC (innerdecl)
5336               || bitsize <= 0
5337               || bitpos + bitsize > 256
5338               || bitsize != maxsize)
5339             return NULL;
5340           decl = innerdecl;
5341         }
5342     }
5343
5344   decl_id = DECL_UID (decl);
5345   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5346   if (*slot == NULL)
5347     {
5348       temp = ggc_alloc_cleared_var_loc_list ();
5349       temp->decl_id = decl_id;
5350       *slot = temp;
5351     }
5352   else
5353     temp = (var_loc_list *) *slot;
5354
5355   /* For PARM_DECLs try to keep around the original incoming value,
5356      even if that means we'll emit a zero-range .debug_loc entry.  */
5357   if (temp->last
5358       && temp->first == temp->last
5359       && TREE_CODE (decl) == PARM_DECL
5360       && GET_CODE (temp->first->loc) == NOTE
5361       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5362       && DECL_INCOMING_RTL (decl)
5363       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5364       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5365          == GET_CODE (DECL_INCOMING_RTL (decl))
5366       && prev_real_insn (temp->first->loc) == NULL_RTX
5367       && (bitsize != -1
5368           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5369                            NOTE_VAR_LOCATION_LOC (loc_note))
5370           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5371               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5372     {
5373       loc = ggc_alloc_cleared_var_loc_node ();
5374       temp->first->next = loc;
5375       temp->last = loc;
5376       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377     }
5378   else if (temp->last)
5379     {
5380       struct var_loc_node *last = temp->last, *unused = NULL;
5381       rtx *piece_loc = NULL, last_loc_note;
5382       int piece_bitpos = 0;
5383       if (last->next)
5384         {
5385           last = last->next;
5386           gcc_assert (last->next == NULL);
5387         }
5388       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5389         {
5390           piece_loc = &last->loc;
5391           do
5392             {
5393               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5394               if (piece_bitpos + cur_bitsize > bitpos)
5395                 break;
5396               piece_bitpos += cur_bitsize;
5397               piece_loc = &XEXP (*piece_loc, 1);
5398             }
5399           while (*piece_loc);
5400         }
5401       /* TEMP->LAST here is either pointer to the last but one or
5402          last element in the chained list, LAST is pointer to the
5403          last element.  */
5404       if (label && strcmp (last->label, label) == 0)
5405         {
5406           /* For SRA optimized variables if there weren't any real
5407              insns since last note, just modify the last node.  */
5408           if (piece_loc != NULL)
5409             {
5410               adjust_piece_list (piece_loc, NULL, NULL,
5411                                  bitpos, piece_bitpos, bitsize, loc_note);
5412               return NULL;
5413             }
5414           /* If the last note doesn't cover any instructions, remove it.  */
5415           if (temp->last != last)
5416             {
5417               temp->last->next = NULL;
5418               unused = last;
5419               last = temp->last;
5420               gcc_assert (strcmp (last->label, label) != 0);
5421             }
5422           else
5423             {
5424               gcc_assert (temp->first == temp->last
5425                           || (temp->first->next == temp->last
5426                               && TREE_CODE (decl) == PARM_DECL));
5427               memset (temp->last, '\0', sizeof (*temp->last));
5428               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5429               return temp->last;
5430             }
5431         }
5432       if (bitsize == -1 && NOTE_P (last->loc))
5433         last_loc_note = last->loc;
5434       else if (piece_loc != NULL
5435                && *piece_loc != NULL_RTX
5436                && piece_bitpos == bitpos
5437                && decl_piece_bitsize (*piece_loc) == bitsize)
5438         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5439       else
5440         last_loc_note = NULL_RTX;
5441       /* If the current location is the same as the end of the list,
5442          and either both or neither of the locations is uninitialized,
5443          we have nothing to do.  */
5444       if (last_loc_note == NULL_RTX
5445           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5446                             NOTE_VAR_LOCATION_LOC (loc_note)))
5447           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5448                != NOTE_VAR_LOCATION_STATUS (loc_note))
5449               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5450                    == VAR_INIT_STATUS_UNINITIALIZED)
5451                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5452                       == VAR_INIT_STATUS_UNINITIALIZED))))
5453         {
5454           /* Add LOC to the end of list and update LAST.  If the last
5455              element of the list has been removed above, reuse its
5456              memory for the new node, otherwise allocate a new one.  */
5457           if (unused)
5458             {
5459               loc = unused;
5460               memset (loc, '\0', sizeof (*loc));
5461             }
5462           else
5463             loc = ggc_alloc_cleared_var_loc_node ();
5464           if (bitsize == -1 || piece_loc == NULL)
5465             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5466           else
5467             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5468                                bitpos, piece_bitpos, bitsize, loc_note);
5469           last->next = loc;
5470           /* Ensure TEMP->LAST will point either to the new last but one
5471              element of the chain, or to the last element in it.  */
5472           if (last != temp->last)
5473             temp->last = last;
5474         }
5475       else if (unused)
5476         ggc_free (unused);
5477     }
5478   else
5479     {
5480       loc = ggc_alloc_cleared_var_loc_node ();
5481       temp->first = loc;
5482       temp->last = loc;
5483       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5484     }
5485   return loc;
5486 }
5487 \f
5488 /* Keep track of the number of spaces used to indent the
5489    output of the debugging routines that print the structure of
5490    the DIE internal representation.  */
5491 static int print_indent;
5492
5493 /* Indent the line the number of spaces given by print_indent.  */
5494
5495 static inline void
5496 print_spaces (FILE *outfile)
5497 {
5498   fprintf (outfile, "%*s", print_indent, "");
5499 }
5500
5501 /* Print a type signature in hex.  */
5502
5503 static inline void
5504 print_signature (FILE *outfile, char *sig)
5505 {
5506   int i;
5507
5508   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5509     fprintf (outfile, "%02x", sig[i] & 0xff);
5510 }
5511
5512 /* Print the information associated with a given DIE, and its children.
5513    This routine is a debugging aid only.  */
5514
5515 static void
5516 print_die (dw_die_ref die, FILE *outfile)
5517 {
5518   dw_attr_ref a;
5519   dw_die_ref c;
5520   unsigned ix;
5521
5522   print_spaces (outfile);
5523   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5524            die->die_offset, dwarf_tag_name (die->die_tag),
5525            (void*) die);
5526   print_spaces (outfile);
5527   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5528   fprintf (outfile, " offset: %ld", die->die_offset);
5529   fprintf (outfile, " mark: %d\n", die->die_mark);
5530
5531   if (use_debug_types && die->die_id.die_type_node)
5532     {
5533       print_spaces (outfile);
5534       fprintf (outfile, "  signature: ");
5535       print_signature (outfile, die->die_id.die_type_node->signature);
5536       fprintf (outfile, "\n");
5537     }
5538
5539   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5540     {
5541       print_spaces (outfile);
5542       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5543
5544       switch (AT_class (a))
5545         {
5546         case dw_val_class_addr:
5547           fprintf (outfile, "address");
5548           break;
5549         case dw_val_class_offset:
5550           fprintf (outfile, "offset");
5551           break;
5552         case dw_val_class_loc:
5553           fprintf (outfile, "location descriptor");
5554           break;
5555         case dw_val_class_loc_list:
5556           fprintf (outfile, "location list -> label:%s",
5557                    AT_loc_list (a)->ll_symbol);
5558           break;
5559         case dw_val_class_range_list:
5560           fprintf (outfile, "range list");
5561           break;
5562         case dw_val_class_const:
5563           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5564           break;
5565         case dw_val_class_unsigned_const:
5566           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5567           break;
5568         case dw_val_class_const_double:
5569           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5570                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5571                    a->dw_attr_val.v.val_double.high,
5572                    a->dw_attr_val.v.val_double.low);
5573           break;
5574         case dw_val_class_vec:
5575           fprintf (outfile, "floating-point or vector constant");
5576           break;
5577         case dw_val_class_flag:
5578           fprintf (outfile, "%u", AT_flag (a));
5579           break;
5580         case dw_val_class_die_ref:
5581           if (AT_ref (a) != NULL)
5582             {
5583               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5584                 {
5585                   fprintf (outfile, "die -> signature: ");
5586                   print_signature (outfile,
5587                                    AT_ref (a)->die_id.die_type_node->signature);
5588                 }
5589               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5590                 fprintf (outfile, "die -> label: %s",
5591                          AT_ref (a)->die_id.die_symbol);
5592               else
5593                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5594               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5595             }
5596           else
5597             fprintf (outfile, "die -> <null>");
5598           break;
5599         case dw_val_class_vms_delta:
5600           fprintf (outfile, "delta: @slotcount(%s-%s)",
5601                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5602           break;
5603         case dw_val_class_lbl_id:
5604         case dw_val_class_lineptr:
5605         case dw_val_class_macptr:
5606           fprintf (outfile, "label: %s", AT_lbl (a));
5607           break;
5608         case dw_val_class_str:
5609           if (AT_string (a) != NULL)
5610             fprintf (outfile, "\"%s\"", AT_string (a));
5611           else
5612             fprintf (outfile, "<null>");
5613           break;
5614         case dw_val_class_file:
5615           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5616                    AT_file (a)->emitted_number);
5617           break;
5618         case dw_val_class_data8:
5619           {
5620             int i;
5621
5622             for (i = 0; i < 8; i++)
5623               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5624             break;
5625           }
5626         default:
5627           break;
5628         }
5629
5630       fprintf (outfile, "\n");
5631     }
5632
5633   if (die->die_child != NULL)
5634     {
5635       print_indent += 4;
5636       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5637       print_indent -= 4;
5638     }
5639   if (print_indent == 0)
5640     fprintf (outfile, "\n");
5641 }
5642
5643 /* Print the information collected for a given DIE.  */
5644
5645 DEBUG_FUNCTION void
5646 debug_dwarf_die (dw_die_ref die)
5647 {
5648   print_die (die, stderr);
5649 }
5650
5651 /* Print all DWARF information collected for the compilation unit.
5652    This routine is a debugging aid only.  */
5653
5654 DEBUG_FUNCTION void
5655 debug_dwarf (void)
5656 {
5657   print_indent = 0;
5658   print_die (comp_unit_die (), stderr);
5659 }
5660 \f
5661 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5662    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5663    DIE that marks the start of the DIEs for this include file.  */
5664
5665 static dw_die_ref
5666 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5667 {
5668   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5669   dw_die_ref new_unit = gen_compile_unit_die (filename);
5670
5671   new_unit->die_sib = old_unit;
5672   return new_unit;
5673 }
5674
5675 /* Close an include-file CU and reopen the enclosing one.  */
5676
5677 static dw_die_ref
5678 pop_compile_unit (dw_die_ref old_unit)
5679 {
5680   dw_die_ref new_unit = old_unit->die_sib;
5681
5682   old_unit->die_sib = NULL;
5683   return new_unit;
5684 }
5685
5686 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5687 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5688
5689 /* Calculate the checksum of a location expression.  */
5690
5691 static inline void
5692 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5693 {
5694   int tem;
5695
5696   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5697   CHECKSUM (tem);
5698   CHECKSUM (loc->dw_loc_oprnd1);
5699   CHECKSUM (loc->dw_loc_oprnd2);
5700 }
5701
5702 /* Calculate the checksum of an attribute.  */
5703
5704 static void
5705 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5706 {
5707   dw_loc_descr_ref loc;
5708   rtx r;
5709
5710   CHECKSUM (at->dw_attr);
5711
5712   /* We don't care that this was compiled with a different compiler
5713      snapshot; if the output is the same, that's what matters.  */
5714   if (at->dw_attr == DW_AT_producer)
5715     return;
5716
5717   switch (AT_class (at))
5718     {
5719     case dw_val_class_const:
5720       CHECKSUM (at->dw_attr_val.v.val_int);
5721       break;
5722     case dw_val_class_unsigned_const:
5723       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5724       break;
5725     case dw_val_class_const_double:
5726       CHECKSUM (at->dw_attr_val.v.val_double);
5727       break;
5728     case dw_val_class_vec:
5729       CHECKSUM (at->dw_attr_val.v.val_vec);
5730       break;
5731     case dw_val_class_flag:
5732       CHECKSUM (at->dw_attr_val.v.val_flag);
5733       break;
5734     case dw_val_class_str:
5735       CHECKSUM_STRING (AT_string (at));
5736       break;
5737
5738     case dw_val_class_addr:
5739       r = AT_addr (at);
5740       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5741       CHECKSUM_STRING (XSTR (r, 0));
5742       break;
5743
5744     case dw_val_class_offset:
5745       CHECKSUM (at->dw_attr_val.v.val_offset);
5746       break;
5747
5748     case dw_val_class_loc:
5749       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5750         loc_checksum (loc, ctx);
5751       break;
5752
5753     case dw_val_class_die_ref:
5754       die_checksum (AT_ref (at), ctx, mark);
5755       break;
5756
5757     case dw_val_class_fde_ref:
5758     case dw_val_class_vms_delta:
5759     case dw_val_class_lbl_id:
5760     case dw_val_class_lineptr:
5761     case dw_val_class_macptr:
5762       break;
5763
5764     case dw_val_class_file:
5765       CHECKSUM_STRING (AT_file (at)->filename);
5766       break;
5767
5768     case dw_val_class_data8:
5769       CHECKSUM (at->dw_attr_val.v.val_data8);
5770       break;
5771
5772     default:
5773       break;
5774     }
5775 }
5776
5777 /* Calculate the checksum of a DIE.  */
5778
5779 static void
5780 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5781 {
5782   dw_die_ref c;
5783   dw_attr_ref a;
5784   unsigned ix;
5785
5786   /* To avoid infinite recursion.  */
5787   if (die->die_mark)
5788     {
5789       CHECKSUM (die->die_mark);
5790       return;
5791     }
5792   die->die_mark = ++(*mark);
5793
5794   CHECKSUM (die->die_tag);
5795
5796   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5797     attr_checksum (a, ctx, mark);
5798
5799   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5800 }
5801
5802 #undef CHECKSUM
5803 #undef CHECKSUM_STRING
5804
5805 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5806 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5807 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5808 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5809 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5810 #define CHECKSUM_ATTR(FOO) \
5811   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5812
5813 /* Calculate the checksum of a number in signed LEB128 format.  */
5814
5815 static void
5816 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5817 {
5818   unsigned char byte;
5819   bool more;
5820
5821   while (1)
5822     {
5823       byte = (value & 0x7f);
5824       value >>= 7;
5825       more = !((value == 0 && (byte & 0x40) == 0)
5826                 || (value == -1 && (byte & 0x40) != 0));
5827       if (more)
5828         byte |= 0x80;
5829       CHECKSUM (byte);
5830       if (!more)
5831         break;
5832     }
5833 }
5834
5835 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5836
5837 static void
5838 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5839 {
5840   while (1)
5841     {
5842       unsigned char byte = (value & 0x7f);
5843       value >>= 7;
5844       if (value != 0)
5845         /* More bytes to follow.  */
5846         byte |= 0x80;
5847       CHECKSUM (byte);
5848       if (value == 0)
5849         break;
5850     }
5851 }
5852
5853 /* Checksum the context of the DIE.  This adds the names of any
5854    surrounding namespaces or structures to the checksum.  */
5855
5856 static void
5857 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5858 {
5859   const char *name;
5860   dw_die_ref spec;
5861   int tag = die->die_tag;
5862
5863   if (tag != DW_TAG_namespace
5864       && tag != DW_TAG_structure_type
5865       && tag != DW_TAG_class_type)
5866     return;
5867
5868   name = get_AT_string (die, DW_AT_name);
5869
5870   spec = get_AT_ref (die, DW_AT_specification);
5871   if (spec != NULL)
5872     die = spec;
5873
5874   if (die->die_parent != NULL)
5875     checksum_die_context (die->die_parent, ctx);
5876
5877   CHECKSUM_ULEB128 ('C');
5878   CHECKSUM_ULEB128 (tag);
5879   if (name != NULL)
5880     CHECKSUM_STRING (name);
5881 }
5882
5883 /* Calculate the checksum of a location expression.  */
5884
5885 static inline void
5886 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5887 {
5888   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5889      were emitted as a DW_FORM_sdata instead of a location expression.  */
5890   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5891     {
5892       CHECKSUM_ULEB128 (DW_FORM_sdata);
5893       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5894       return;
5895     }
5896
5897   /* Otherwise, just checksum the raw location expression.  */
5898   while (loc != NULL)
5899     {
5900       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5901       CHECKSUM (loc->dw_loc_oprnd1);
5902       CHECKSUM (loc->dw_loc_oprnd2);
5903       loc = loc->dw_loc_next;
5904     }
5905 }
5906
5907 /* Calculate the checksum of an attribute.  */
5908
5909 static void
5910 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5911                        struct md5_ctx *ctx, int *mark)
5912 {
5913   dw_loc_descr_ref loc;
5914   rtx r;
5915
5916   if (AT_class (at) == dw_val_class_die_ref)
5917     {
5918       dw_die_ref target_die = AT_ref (at);
5919
5920       /* For pointer and reference types, we checksum only the (qualified)
5921          name of the target type (if there is a name).  For friend entries,
5922          we checksum only the (qualified) name of the target type or function.
5923          This allows the checksum to remain the same whether the target type
5924          is complete or not.  */
5925       if ((at->dw_attr == DW_AT_type
5926            && (tag == DW_TAG_pointer_type
5927                || tag == DW_TAG_reference_type
5928                || tag == DW_TAG_rvalue_reference_type
5929                || tag == DW_TAG_ptr_to_member_type))
5930           || (at->dw_attr == DW_AT_friend
5931               && tag == DW_TAG_friend))
5932         {
5933           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5934
5935           if (name_attr != NULL)
5936             {
5937               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5938
5939               if (decl == NULL)
5940                 decl = target_die;
5941               CHECKSUM_ULEB128 ('N');
5942               CHECKSUM_ULEB128 (at->dw_attr);
5943               if (decl->die_parent != NULL)
5944                 checksum_die_context (decl->die_parent, ctx);
5945               CHECKSUM_ULEB128 ('E');
5946               CHECKSUM_STRING (AT_string (name_attr));
5947               return;
5948             }
5949         }
5950
5951       /* For all other references to another DIE, we check to see if the
5952          target DIE has already been visited.  If it has, we emit a
5953          backward reference; if not, we descend recursively.  */
5954       if (target_die->die_mark > 0)
5955         {
5956           CHECKSUM_ULEB128 ('R');
5957           CHECKSUM_ULEB128 (at->dw_attr);
5958           CHECKSUM_ULEB128 (target_die->die_mark);
5959         }
5960       else
5961         {
5962           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5963
5964           if (decl == NULL)
5965             decl = target_die;
5966           target_die->die_mark = ++(*mark);
5967           CHECKSUM_ULEB128 ('T');
5968           CHECKSUM_ULEB128 (at->dw_attr);
5969           if (decl->die_parent != NULL)
5970             checksum_die_context (decl->die_parent, ctx);
5971           die_checksum_ordered (target_die, ctx, mark);
5972         }
5973       return;
5974     }
5975
5976   CHECKSUM_ULEB128 ('A');
5977   CHECKSUM_ULEB128 (at->dw_attr);
5978
5979   switch (AT_class (at))
5980     {
5981     case dw_val_class_const:
5982       CHECKSUM_ULEB128 (DW_FORM_sdata);
5983       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5984       break;
5985
5986     case dw_val_class_unsigned_const:
5987       CHECKSUM_ULEB128 (DW_FORM_sdata);
5988       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5989       break;
5990
5991     case dw_val_class_const_double:
5992       CHECKSUM_ULEB128 (DW_FORM_block);
5993       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5994       CHECKSUM (at->dw_attr_val.v.val_double);
5995       break;
5996
5997     case dw_val_class_vec:
5998       CHECKSUM_ULEB128 (DW_FORM_block);
5999       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6000       CHECKSUM (at->dw_attr_val.v.val_vec);
6001       break;
6002
6003     case dw_val_class_flag:
6004       CHECKSUM_ULEB128 (DW_FORM_flag);
6005       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6006       break;
6007
6008     case dw_val_class_str:
6009       CHECKSUM_ULEB128 (DW_FORM_string);
6010       CHECKSUM_STRING (AT_string (at));
6011       break;
6012
6013     case dw_val_class_addr:
6014       r = AT_addr (at);
6015       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6016       CHECKSUM_ULEB128 (DW_FORM_string);
6017       CHECKSUM_STRING (XSTR (r, 0));
6018       break;
6019
6020     case dw_val_class_offset:
6021       CHECKSUM_ULEB128 (DW_FORM_sdata);
6022       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6023       break;
6024
6025     case dw_val_class_loc:
6026       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6027         loc_checksum_ordered (loc, ctx);
6028       break;
6029
6030     case dw_val_class_fde_ref:
6031     case dw_val_class_lbl_id:
6032     case dw_val_class_lineptr:
6033     case dw_val_class_macptr:
6034       break;
6035
6036     case dw_val_class_file:
6037       CHECKSUM_ULEB128 (DW_FORM_string);
6038       CHECKSUM_STRING (AT_file (at)->filename);
6039       break;
6040
6041     case dw_val_class_data8:
6042       CHECKSUM (at->dw_attr_val.v.val_data8);
6043       break;
6044
6045     default:
6046       break;
6047     }
6048 }
6049
6050 struct checksum_attributes
6051 {
6052   dw_attr_ref at_name;
6053   dw_attr_ref at_type;
6054   dw_attr_ref at_friend;
6055   dw_attr_ref at_accessibility;
6056   dw_attr_ref at_address_class;
6057   dw_attr_ref at_allocated;
6058   dw_attr_ref at_artificial;
6059   dw_attr_ref at_associated;
6060   dw_attr_ref at_binary_scale;
6061   dw_attr_ref at_bit_offset;
6062   dw_attr_ref at_bit_size;
6063   dw_attr_ref at_bit_stride;
6064   dw_attr_ref at_byte_size;
6065   dw_attr_ref at_byte_stride;
6066   dw_attr_ref at_const_value;
6067   dw_attr_ref at_containing_type;
6068   dw_attr_ref at_count;
6069   dw_attr_ref at_data_location;
6070   dw_attr_ref at_data_member_location;
6071   dw_attr_ref at_decimal_scale;
6072   dw_attr_ref at_decimal_sign;
6073   dw_attr_ref at_default_value;
6074   dw_attr_ref at_digit_count;
6075   dw_attr_ref at_discr;
6076   dw_attr_ref at_discr_list;
6077   dw_attr_ref at_discr_value;
6078   dw_attr_ref at_encoding;
6079   dw_attr_ref at_endianity;
6080   dw_attr_ref at_explicit;
6081   dw_attr_ref at_is_optional;
6082   dw_attr_ref at_location;
6083   dw_attr_ref at_lower_bound;
6084   dw_attr_ref at_mutable;
6085   dw_attr_ref at_ordering;
6086   dw_attr_ref at_picture_string;
6087   dw_attr_ref at_prototyped;
6088   dw_attr_ref at_small;
6089   dw_attr_ref at_segment;
6090   dw_attr_ref at_string_length;
6091   dw_attr_ref at_threads_scaled;
6092   dw_attr_ref at_upper_bound;
6093   dw_attr_ref at_use_location;
6094   dw_attr_ref at_use_UTF8;
6095   dw_attr_ref at_variable_parameter;
6096   dw_attr_ref at_virtuality;
6097   dw_attr_ref at_visibility;
6098   dw_attr_ref at_vtable_elem_location;
6099 };
6100
6101 /* Collect the attributes that we will want to use for the checksum.  */
6102
6103 static void
6104 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6105 {
6106   dw_attr_ref a;
6107   unsigned ix;
6108
6109   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6110     {
6111       switch (a->dw_attr)
6112         {
6113         case DW_AT_name:
6114           attrs->at_name = a;
6115           break;
6116         case DW_AT_type:
6117           attrs->at_type = a;
6118           break;
6119         case DW_AT_friend:
6120           attrs->at_friend = a;
6121           break;
6122         case DW_AT_accessibility:
6123           attrs->at_accessibility = a;
6124           break;
6125         case DW_AT_address_class:
6126           attrs->at_address_class = a;
6127           break;
6128         case DW_AT_allocated:
6129           attrs->at_allocated = a;
6130           break;
6131         case DW_AT_artificial:
6132           attrs->at_artificial = a;
6133           break;
6134         case DW_AT_associated:
6135           attrs->at_associated = a;
6136           break;
6137         case DW_AT_binary_scale:
6138           attrs->at_binary_scale = a;
6139           break;
6140         case DW_AT_bit_offset:
6141           attrs->at_bit_offset = a;
6142           break;
6143         case DW_AT_bit_size:
6144           attrs->at_bit_size = a;
6145           break;
6146         case DW_AT_bit_stride:
6147           attrs->at_bit_stride = a;
6148           break;
6149         case DW_AT_byte_size:
6150           attrs->at_byte_size = a;
6151           break;
6152         case DW_AT_byte_stride:
6153           attrs->at_byte_stride = a;
6154           break;
6155         case DW_AT_const_value:
6156           attrs->at_const_value = a;
6157           break;
6158         case DW_AT_containing_type:
6159           attrs->at_containing_type = a;
6160           break;
6161         case DW_AT_count:
6162           attrs->at_count = a;
6163           break;
6164         case DW_AT_data_location:
6165           attrs->at_data_location = a;
6166           break;
6167         case DW_AT_data_member_location:
6168           attrs->at_data_member_location = a;
6169           break;
6170         case DW_AT_decimal_scale:
6171           attrs->at_decimal_scale = a;
6172           break;
6173         case DW_AT_decimal_sign:
6174           attrs->at_decimal_sign = a;
6175           break;
6176         case DW_AT_default_value:
6177           attrs->at_default_value = a;
6178           break;
6179         case DW_AT_digit_count:
6180           attrs->at_digit_count = a;
6181           break;
6182         case DW_AT_discr:
6183           attrs->at_discr = a;
6184           break;
6185         case DW_AT_discr_list:
6186           attrs->at_discr_list = a;
6187           break;
6188         case DW_AT_discr_value:
6189           attrs->at_discr_value = a;
6190           break;
6191         case DW_AT_encoding:
6192           attrs->at_encoding = a;
6193           break;
6194         case DW_AT_endianity:
6195           attrs->at_endianity = a;
6196           break;
6197         case DW_AT_explicit:
6198           attrs->at_explicit = a;
6199           break;
6200         case DW_AT_is_optional:
6201           attrs->at_is_optional = a;
6202           break;
6203         case DW_AT_location:
6204           attrs->at_location = a;
6205           break;
6206         case DW_AT_lower_bound:
6207           attrs->at_lower_bound = a;
6208           break;
6209         case DW_AT_mutable:
6210           attrs->at_mutable = a;
6211           break;
6212         case DW_AT_ordering:
6213           attrs->at_ordering = a;
6214           break;
6215         case DW_AT_picture_string:
6216           attrs->at_picture_string = a;
6217           break;
6218         case DW_AT_prototyped:
6219           attrs->at_prototyped = a;
6220           break;
6221         case DW_AT_small:
6222           attrs->at_small = a;
6223           break;
6224         case DW_AT_segment:
6225           attrs->at_segment = a;
6226           break;
6227         case DW_AT_string_length:
6228           attrs->at_string_length = a;
6229           break;
6230         case DW_AT_threads_scaled:
6231           attrs->at_threads_scaled = a;
6232           break;
6233         case DW_AT_upper_bound:
6234           attrs->at_upper_bound = a;
6235           break;
6236         case DW_AT_use_location:
6237           attrs->at_use_location = a;
6238           break;
6239         case DW_AT_use_UTF8:
6240           attrs->at_use_UTF8 = a;
6241           break;
6242         case DW_AT_variable_parameter:
6243           attrs->at_variable_parameter = a;
6244           break;
6245         case DW_AT_virtuality:
6246           attrs->at_virtuality = a;
6247           break;
6248         case DW_AT_visibility:
6249           attrs->at_visibility = a;
6250           break;
6251         case DW_AT_vtable_elem_location:
6252           attrs->at_vtable_elem_location = a;
6253           break;
6254         default:
6255           break;
6256         }
6257     }
6258 }
6259
6260 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6261
6262 static void
6263 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6264 {
6265   dw_die_ref c;
6266   dw_die_ref decl;
6267   struct checksum_attributes attrs;
6268
6269   CHECKSUM_ULEB128 ('D');
6270   CHECKSUM_ULEB128 (die->die_tag);
6271
6272   memset (&attrs, 0, sizeof (attrs));
6273
6274   decl = get_AT_ref (die, DW_AT_specification);
6275   if (decl != NULL)
6276     collect_checksum_attributes (&attrs, decl);
6277   collect_checksum_attributes (&attrs, die);
6278
6279   CHECKSUM_ATTR (attrs.at_name);
6280   CHECKSUM_ATTR (attrs.at_accessibility);
6281   CHECKSUM_ATTR (attrs.at_address_class);
6282   CHECKSUM_ATTR (attrs.at_allocated);
6283   CHECKSUM_ATTR (attrs.at_artificial);
6284   CHECKSUM_ATTR (attrs.at_associated);
6285   CHECKSUM_ATTR (attrs.at_binary_scale);
6286   CHECKSUM_ATTR (attrs.at_bit_offset);
6287   CHECKSUM_ATTR (attrs.at_bit_size);
6288   CHECKSUM_ATTR (attrs.at_bit_stride);
6289   CHECKSUM_ATTR (attrs.at_byte_size);
6290   CHECKSUM_ATTR (attrs.at_byte_stride);
6291   CHECKSUM_ATTR (attrs.at_const_value);
6292   CHECKSUM_ATTR (attrs.at_containing_type);
6293   CHECKSUM_ATTR (attrs.at_count);
6294   CHECKSUM_ATTR (attrs.at_data_location);
6295   CHECKSUM_ATTR (attrs.at_data_member_location);
6296   CHECKSUM_ATTR (attrs.at_decimal_scale);
6297   CHECKSUM_ATTR (attrs.at_decimal_sign);
6298   CHECKSUM_ATTR (attrs.at_default_value);
6299   CHECKSUM_ATTR (attrs.at_digit_count);
6300   CHECKSUM_ATTR (attrs.at_discr);
6301   CHECKSUM_ATTR (attrs.at_discr_list);
6302   CHECKSUM_ATTR (attrs.at_discr_value);
6303   CHECKSUM_ATTR (attrs.at_encoding);
6304   CHECKSUM_ATTR (attrs.at_endianity);
6305   CHECKSUM_ATTR (attrs.at_explicit);
6306   CHECKSUM_ATTR (attrs.at_is_optional);
6307   CHECKSUM_ATTR (attrs.at_location);
6308   CHECKSUM_ATTR (attrs.at_lower_bound);
6309   CHECKSUM_ATTR (attrs.at_mutable);
6310   CHECKSUM_ATTR (attrs.at_ordering);
6311   CHECKSUM_ATTR (attrs.at_picture_string);
6312   CHECKSUM_ATTR (attrs.at_prototyped);
6313   CHECKSUM_ATTR (attrs.at_small);
6314   CHECKSUM_ATTR (attrs.at_segment);
6315   CHECKSUM_ATTR (attrs.at_string_length);
6316   CHECKSUM_ATTR (attrs.at_threads_scaled);
6317   CHECKSUM_ATTR (attrs.at_upper_bound);
6318   CHECKSUM_ATTR (attrs.at_use_location);
6319   CHECKSUM_ATTR (attrs.at_use_UTF8);
6320   CHECKSUM_ATTR (attrs.at_variable_parameter);
6321   CHECKSUM_ATTR (attrs.at_virtuality);
6322   CHECKSUM_ATTR (attrs.at_visibility);
6323   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6324   CHECKSUM_ATTR (attrs.at_type);
6325   CHECKSUM_ATTR (attrs.at_friend);
6326
6327   /* Checksum the child DIEs, except for nested types and member functions.  */
6328   c = die->die_child;
6329   if (c) do {
6330     dw_attr_ref name_attr;
6331
6332     c = c->die_sib;
6333     name_attr = get_AT (c, DW_AT_name);
6334     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6335         && name_attr != NULL)
6336       {
6337         CHECKSUM_ULEB128 ('S');
6338         CHECKSUM_ULEB128 (c->die_tag);
6339         CHECKSUM_STRING (AT_string (name_attr));
6340       }
6341     else
6342       {
6343         /* Mark this DIE so it gets processed when unmarking.  */
6344         if (c->die_mark == 0)
6345           c->die_mark = -1;
6346         die_checksum_ordered (c, ctx, mark);
6347       }
6348   } while (c != die->die_child);
6349
6350   CHECKSUM_ULEB128 (0);
6351 }
6352
6353 #undef CHECKSUM
6354 #undef CHECKSUM_STRING
6355 #undef CHECKSUM_ATTR
6356 #undef CHECKSUM_LEB128
6357 #undef CHECKSUM_ULEB128
6358
6359 /* Generate the type signature for DIE.  This is computed by generating an
6360    MD5 checksum over the DIE's tag, its relevant attributes, and its
6361    children.  Attributes that are references to other DIEs are processed
6362    by recursion, using the MARK field to prevent infinite recursion.
6363    If the DIE is nested inside a namespace or another type, we also
6364    need to include that context in the signature.  The lower 64 bits
6365    of the resulting MD5 checksum comprise the signature.  */
6366
6367 static void
6368 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6369 {
6370   int mark;
6371   const char *name;
6372   unsigned char checksum[16];
6373   struct md5_ctx ctx;
6374   dw_die_ref decl;
6375
6376   name = get_AT_string (die, DW_AT_name);
6377   decl = get_AT_ref (die, DW_AT_specification);
6378
6379   /* First, compute a signature for just the type name (and its surrounding
6380      context, if any.  This is stored in the type unit DIE for link-time
6381      ODR (one-definition rule) checking.  */
6382
6383   if (is_cxx() && name != NULL)
6384     {
6385       md5_init_ctx (&ctx);
6386
6387       /* Checksum the names of surrounding namespaces and structures.  */
6388       if (decl != NULL && decl->die_parent != NULL)
6389         checksum_die_context (decl->die_parent, &ctx);
6390
6391       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6392       md5_process_bytes (name, strlen (name) + 1, &ctx);
6393       md5_finish_ctx (&ctx, checksum);
6394
6395       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6396     }
6397
6398   /* Next, compute the complete type signature.  */
6399
6400   md5_init_ctx (&ctx);
6401   mark = 1;
6402   die->die_mark = mark;
6403
6404   /* Checksum the names of surrounding namespaces and structures.  */
6405   if (decl != NULL && decl->die_parent != NULL)
6406     checksum_die_context (decl->die_parent, &ctx);
6407
6408   /* Checksum the DIE and its children.  */
6409   die_checksum_ordered (die, &ctx, &mark);
6410   unmark_all_dies (die);
6411   md5_finish_ctx (&ctx, checksum);
6412
6413   /* Store the signature in the type node and link the type DIE and the
6414      type node together.  */
6415   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6416           DWARF_TYPE_SIGNATURE_SIZE);
6417   die->die_id.die_type_node = type_node;
6418   type_node->type_die = die;
6419
6420   /* If the DIE is a specification, link its declaration to the type node
6421      as well.  */
6422   if (decl != NULL)
6423     decl->die_id.die_type_node = type_node;
6424 }
6425
6426 /* Do the location expressions look same?  */
6427 static inline int
6428 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6429 {
6430   return loc1->dw_loc_opc == loc2->dw_loc_opc
6431          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6432          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6433 }
6434
6435 /* Do the values look the same?  */
6436 static int
6437 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6438 {
6439   dw_loc_descr_ref loc1, loc2;
6440   rtx r1, r2;
6441
6442   if (v1->val_class != v2->val_class)
6443     return 0;
6444
6445   switch (v1->val_class)
6446     {
6447     case dw_val_class_const:
6448       return v1->v.val_int == v2->v.val_int;
6449     case dw_val_class_unsigned_const:
6450       return v1->v.val_unsigned == v2->v.val_unsigned;
6451     case dw_val_class_const_double:
6452       return v1->v.val_double.high == v2->v.val_double.high
6453              && v1->v.val_double.low == v2->v.val_double.low;
6454     case dw_val_class_vec:
6455       if (v1->v.val_vec.length != v2->v.val_vec.length
6456           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6457         return 0;
6458       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6459                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6460         return 0;
6461       return 1;
6462     case dw_val_class_flag:
6463       return v1->v.val_flag == v2->v.val_flag;
6464     case dw_val_class_str:
6465       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6466
6467     case dw_val_class_addr:
6468       r1 = v1->v.val_addr;
6469       r2 = v2->v.val_addr;
6470       if (GET_CODE (r1) != GET_CODE (r2))
6471         return 0;
6472       return !rtx_equal_p (r1, r2);
6473
6474     case dw_val_class_offset:
6475       return v1->v.val_offset == v2->v.val_offset;
6476
6477     case dw_val_class_loc:
6478       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6479            loc1 && loc2;
6480            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6481         if (!same_loc_p (loc1, loc2, mark))
6482           return 0;
6483       return !loc1 && !loc2;
6484
6485     case dw_val_class_die_ref:
6486       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6487
6488     case dw_val_class_fde_ref:
6489     case dw_val_class_vms_delta:
6490     case dw_val_class_lbl_id:
6491     case dw_val_class_lineptr:
6492     case dw_val_class_macptr:
6493       return 1;
6494
6495     case dw_val_class_file:
6496       return v1->v.val_file == v2->v.val_file;
6497
6498     case dw_val_class_data8:
6499       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6500
6501     default:
6502       return 1;
6503     }
6504 }
6505
6506 /* Do the attributes look the same?  */
6507
6508 static int
6509 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6510 {
6511   if (at1->dw_attr != at2->dw_attr)
6512     return 0;
6513
6514   /* We don't care that this was compiled with a different compiler
6515      snapshot; if the output is the same, that's what matters. */
6516   if (at1->dw_attr == DW_AT_producer)
6517     return 1;
6518
6519   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6520 }
6521
6522 /* Do the dies look the same?  */
6523
6524 static int
6525 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6526 {
6527   dw_die_ref c1, c2;
6528   dw_attr_ref a1;
6529   unsigned ix;
6530
6531   /* To avoid infinite recursion.  */
6532   if (die1->die_mark)
6533     return die1->die_mark == die2->die_mark;
6534   die1->die_mark = die2->die_mark = ++(*mark);
6535
6536   if (die1->die_tag != die2->die_tag)
6537     return 0;
6538
6539   if (VEC_length (dw_attr_node, die1->die_attr)
6540       != VEC_length (dw_attr_node, die2->die_attr))
6541     return 0;
6542
6543   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6544     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6545       return 0;
6546
6547   c1 = die1->die_child;
6548   c2 = die2->die_child;
6549   if (! c1)
6550     {
6551       if (c2)
6552         return 0;
6553     }
6554   else
6555     for (;;)
6556       {
6557         if (!same_die_p (c1, c2, mark))
6558           return 0;
6559         c1 = c1->die_sib;
6560         c2 = c2->die_sib;
6561         if (c1 == die1->die_child)
6562           {
6563             if (c2 == die2->die_child)
6564               break;
6565             else
6566               return 0;
6567           }
6568     }
6569
6570   return 1;
6571 }
6572
6573 /* Do the dies look the same?  Wrapper around same_die_p.  */
6574
6575 static int
6576 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6577 {
6578   int mark = 0;
6579   int ret = same_die_p (die1, die2, &mark);
6580
6581   unmark_all_dies (die1);
6582   unmark_all_dies (die2);
6583
6584   return ret;
6585 }
6586
6587 /* The prefix to attach to symbols on DIEs in the current comdat debug
6588    info section.  */
6589 static char *comdat_symbol_id;
6590
6591 /* The index of the current symbol within the current comdat CU.  */
6592 static unsigned int comdat_symbol_number;
6593
6594 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6595    children, and set comdat_symbol_id accordingly.  */
6596
6597 static void
6598 compute_section_prefix (dw_die_ref unit_die)
6599 {
6600   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6601   const char *base = die_name ? lbasename (die_name) : "anonymous";
6602   char *name = XALLOCAVEC (char, strlen (base) + 64);
6603   char *p;
6604   int i, mark;
6605   unsigned char checksum[16];
6606   struct md5_ctx ctx;
6607
6608   /* Compute the checksum of the DIE, then append part of it as hex digits to
6609      the name filename of the unit.  */
6610
6611   md5_init_ctx (&ctx);
6612   mark = 0;
6613   die_checksum (unit_die, &ctx, &mark);
6614   unmark_all_dies (unit_die);
6615   md5_finish_ctx (&ctx, checksum);
6616
6617   sprintf (name, "%s.", base);
6618   clean_symbol_name (name);
6619
6620   p = name + strlen (name);
6621   for (i = 0; i < 4; i++)
6622     {
6623       sprintf (p, "%.2x", checksum[i]);
6624       p += 2;
6625     }
6626
6627   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6628   comdat_symbol_number = 0;
6629 }
6630
6631 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6632
6633 static int
6634 is_type_die (dw_die_ref die)
6635 {
6636   switch (die->die_tag)
6637     {
6638     case DW_TAG_array_type:
6639     case DW_TAG_class_type:
6640     case DW_TAG_interface_type:
6641     case DW_TAG_enumeration_type:
6642     case DW_TAG_pointer_type:
6643     case DW_TAG_reference_type:
6644     case DW_TAG_rvalue_reference_type:
6645     case DW_TAG_string_type:
6646     case DW_TAG_structure_type:
6647     case DW_TAG_subroutine_type:
6648     case DW_TAG_union_type:
6649     case DW_TAG_ptr_to_member_type:
6650     case DW_TAG_set_type:
6651     case DW_TAG_subrange_type:
6652     case DW_TAG_base_type:
6653     case DW_TAG_const_type:
6654     case DW_TAG_file_type:
6655     case DW_TAG_packed_type:
6656     case DW_TAG_volatile_type:
6657     case DW_TAG_typedef:
6658       return 1;
6659     default:
6660       return 0;
6661     }
6662 }
6663
6664 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6665    Basically, we want to choose the bits that are likely to be shared between
6666    compilations (types) and leave out the bits that are specific to individual
6667    compilations (functions).  */
6668
6669 static int
6670 is_comdat_die (dw_die_ref c)
6671 {
6672   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6673      we do for stabs.  The advantage is a greater likelihood of sharing between
6674      objects that don't include headers in the same order (and therefore would
6675      put the base types in a different comdat).  jason 8/28/00 */
6676
6677   if (c->die_tag == DW_TAG_base_type)
6678     return 0;
6679
6680   if (c->die_tag == DW_TAG_pointer_type
6681       || c->die_tag == DW_TAG_reference_type
6682       || c->die_tag == DW_TAG_rvalue_reference_type
6683       || c->die_tag == DW_TAG_const_type
6684       || c->die_tag == DW_TAG_volatile_type)
6685     {
6686       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6687
6688       return t ? is_comdat_die (t) : 0;
6689     }
6690
6691   return is_type_die (c);
6692 }
6693
6694 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6695    compilation unit.  */
6696
6697 static int
6698 is_symbol_die (dw_die_ref c)
6699 {
6700   return (is_type_die (c)
6701           || is_declaration_die (c)
6702           || c->die_tag == DW_TAG_namespace
6703           || c->die_tag == DW_TAG_module);
6704 }
6705
6706 /* Returns true iff C is a compile-unit DIE.  */
6707
6708 static inline bool
6709 is_cu_die (dw_die_ref c)
6710 {
6711   return c && c->die_tag == DW_TAG_compile_unit;
6712 }
6713
6714 static char *
6715 gen_internal_sym (const char *prefix)
6716 {
6717   char buf[256];
6718
6719   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6720   return xstrdup (buf);
6721 }
6722
6723 /* Assign symbols to all worthy DIEs under DIE.  */
6724
6725 static void
6726 assign_symbol_names (dw_die_ref die)
6727 {
6728   dw_die_ref c;
6729
6730   if (is_symbol_die (die))
6731     {
6732       if (comdat_symbol_id)
6733         {
6734           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6735
6736           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6737                    comdat_symbol_id, comdat_symbol_number++);
6738           die->die_id.die_symbol = xstrdup (p);
6739         }
6740       else
6741         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6742     }
6743
6744   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6745 }
6746
6747 struct cu_hash_table_entry
6748 {
6749   dw_die_ref cu;
6750   unsigned min_comdat_num, max_comdat_num;
6751   struct cu_hash_table_entry *next;
6752 };
6753
6754 /* Routines to manipulate hash table of CUs.  */
6755 static hashval_t
6756 htab_cu_hash (const void *of)
6757 {
6758   const struct cu_hash_table_entry *const entry =
6759     (const struct cu_hash_table_entry *) of;
6760
6761   return htab_hash_string (entry->cu->die_id.die_symbol);
6762 }
6763
6764 static int
6765 htab_cu_eq (const void *of1, const void *of2)
6766 {
6767   const struct cu_hash_table_entry *const entry1 =
6768     (const struct cu_hash_table_entry *) of1;
6769   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6770
6771   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6772 }
6773
6774 static void
6775 htab_cu_del (void *what)
6776 {
6777   struct cu_hash_table_entry *next,
6778     *entry = (struct cu_hash_table_entry *) what;
6779
6780   while (entry)
6781     {
6782       next = entry->next;
6783       free (entry);
6784       entry = next;
6785     }
6786 }
6787
6788 /* Check whether we have already seen this CU and set up SYM_NUM
6789    accordingly.  */
6790 static int
6791 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6792 {
6793   struct cu_hash_table_entry dummy;
6794   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6795
6796   dummy.max_comdat_num = 0;
6797
6798   slot = (struct cu_hash_table_entry **)
6799     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6800         INSERT);
6801   entry = *slot;
6802
6803   for (; entry; last = entry, entry = entry->next)
6804     {
6805       if (same_die_p_wrap (cu, entry->cu))
6806         break;
6807     }
6808
6809   if (entry)
6810     {
6811       *sym_num = entry->min_comdat_num;
6812       return 1;
6813     }
6814
6815   entry = XCNEW (struct cu_hash_table_entry);
6816   entry->cu = cu;
6817   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6818   entry->next = *slot;
6819   *slot = entry;
6820
6821   return 0;
6822 }
6823
6824 /* Record SYM_NUM to record of CU in HTABLE.  */
6825 static void
6826 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6827 {
6828   struct cu_hash_table_entry **slot, *entry;
6829
6830   slot = (struct cu_hash_table_entry **)
6831     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6832         NO_INSERT);
6833   entry = *slot;
6834
6835   entry->max_comdat_num = sym_num;
6836 }
6837
6838 /* Traverse the DIE (which is always comp_unit_die), and set up
6839    additional compilation units for each of the include files we see
6840    bracketed by BINCL/EINCL.  */
6841
6842 static void
6843 break_out_includes (dw_die_ref die)
6844 {
6845   dw_die_ref c;
6846   dw_die_ref unit = NULL;
6847   limbo_die_node *node, **pnode;
6848   htab_t cu_hash_table;
6849
6850   c = die->die_child;
6851   if (c) do {
6852     dw_die_ref prev = c;
6853     c = c->die_sib;
6854     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6855            || (unit && is_comdat_die (c)))
6856       {
6857         dw_die_ref next = c->die_sib;
6858
6859         /* This DIE is for a secondary CU; remove it from the main one.  */
6860         remove_child_with_prev (c, prev);
6861
6862         if (c->die_tag == DW_TAG_GNU_BINCL)
6863           unit = push_new_compile_unit (unit, c);
6864         else if (c->die_tag == DW_TAG_GNU_EINCL)
6865           unit = pop_compile_unit (unit);
6866         else
6867           add_child_die (unit, c);
6868         c = next;
6869         if (c == die->die_child)
6870           break;
6871       }
6872   } while (c != die->die_child);
6873
6874 #if 0
6875   /* We can only use this in debugging, since the frontend doesn't check
6876      to make sure that we leave every include file we enter.  */
6877   gcc_assert (!unit);
6878 #endif
6879
6880   assign_symbol_names (die);
6881   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6882   for (node = limbo_die_list, pnode = &limbo_die_list;
6883        node;
6884        node = node->next)
6885     {
6886       int is_dupl;
6887
6888       compute_section_prefix (node->die);
6889       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6890                         &comdat_symbol_number);
6891       assign_symbol_names (node->die);
6892       if (is_dupl)
6893         *pnode = node->next;
6894       else
6895         {
6896           pnode = &node->next;
6897           record_comdat_symbol_number (node->die, cu_hash_table,
6898                 comdat_symbol_number);
6899         }
6900     }
6901   htab_delete (cu_hash_table);
6902 }
6903
6904 /* Return non-zero if this DIE is a declaration.  */
6905
6906 static int
6907 is_declaration_die (dw_die_ref die)
6908 {
6909   dw_attr_ref a;
6910   unsigned ix;
6911
6912   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6913     if (a->dw_attr == DW_AT_declaration)
6914       return 1;
6915
6916   return 0;
6917 }
6918
6919 /* Return non-zero if this DIE is nested inside a subprogram.  */
6920
6921 static int
6922 is_nested_in_subprogram (dw_die_ref die)
6923 {
6924   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6925
6926   if (decl == NULL)
6927     decl = die;
6928   return local_scope_p (decl);
6929 }
6930
6931 /* Return non-zero if this DIE contains a defining declaration of a
6932    subprogram.  */
6933
6934 static int
6935 contains_subprogram_definition (dw_die_ref die)
6936 {
6937   dw_die_ref c;
6938
6939   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6940     return 1;
6941   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6942   return 0;
6943 }
6944
6945 /* Return non-zero if this is a type DIE that should be moved to a
6946    COMDAT .debug_types section.  */
6947
6948 static int
6949 should_move_die_to_comdat (dw_die_ref die)
6950 {
6951   switch (die->die_tag)
6952     {
6953     case DW_TAG_class_type:
6954     case DW_TAG_structure_type:
6955     case DW_TAG_enumeration_type:
6956     case DW_TAG_union_type:
6957       /* Don't move declarations, inlined instances, or types nested in a
6958          subprogram.  */
6959       if (is_declaration_die (die)
6960           || get_AT (die, DW_AT_abstract_origin)
6961           || is_nested_in_subprogram (die))
6962         return 0;
6963       /* A type definition should never contain a subprogram definition.  */
6964       gcc_assert (!contains_subprogram_definition (die));
6965       return 1;
6966     case DW_TAG_array_type:
6967     case DW_TAG_interface_type:
6968     case DW_TAG_pointer_type:
6969     case DW_TAG_reference_type:
6970     case DW_TAG_rvalue_reference_type:
6971     case DW_TAG_string_type:
6972     case DW_TAG_subroutine_type:
6973     case DW_TAG_ptr_to_member_type:
6974     case DW_TAG_set_type:
6975     case DW_TAG_subrange_type:
6976     case DW_TAG_base_type:
6977     case DW_TAG_const_type:
6978     case DW_TAG_file_type:
6979     case DW_TAG_packed_type:
6980     case DW_TAG_volatile_type:
6981     case DW_TAG_typedef:
6982     default:
6983       return 0;
6984     }
6985 }
6986
6987 /* Make a clone of DIE.  */
6988
6989 static dw_die_ref
6990 clone_die (dw_die_ref die)
6991 {
6992   dw_die_ref clone;
6993   dw_attr_ref a;
6994   unsigned ix;
6995
6996   clone = ggc_alloc_cleared_die_node ();
6997   clone->die_tag = die->die_tag;
6998
6999   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7000     add_dwarf_attr (clone, a);
7001
7002   return clone;
7003 }
7004
7005 /* Make a clone of the tree rooted at DIE.  */
7006
7007 static dw_die_ref
7008 clone_tree (dw_die_ref die)
7009 {
7010   dw_die_ref c;
7011   dw_die_ref clone = clone_die (die);
7012
7013   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7014
7015   return clone;
7016 }
7017
7018 /* Make a clone of DIE as a declaration.  */
7019
7020 static dw_die_ref
7021 clone_as_declaration (dw_die_ref die)
7022 {
7023   dw_die_ref clone;
7024   dw_die_ref decl;
7025   dw_attr_ref a;
7026   unsigned ix;
7027
7028   /* If the DIE is already a declaration, just clone it.  */
7029   if (is_declaration_die (die))
7030     return clone_die (die);
7031
7032   /* If the DIE is a specification, just clone its declaration DIE.  */
7033   decl = get_AT_ref (die, DW_AT_specification);
7034   if (decl != NULL)
7035     return clone_die (decl);
7036
7037   clone = ggc_alloc_cleared_die_node ();
7038   clone->die_tag = die->die_tag;
7039
7040   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7041     {
7042       /* We don't want to copy over all attributes.
7043          For example we don't want DW_AT_byte_size because otherwise we will no
7044          longer have a declaration and GDB will treat it as a definition.  */
7045
7046       switch (a->dw_attr)
7047         {
7048         case DW_AT_artificial:
7049         case DW_AT_containing_type:
7050         case DW_AT_external:
7051         case DW_AT_name:
7052         case DW_AT_type:
7053         case DW_AT_virtuality:
7054         case DW_AT_linkage_name:
7055         case DW_AT_MIPS_linkage_name:
7056           add_dwarf_attr (clone, a);
7057           break;
7058         case DW_AT_byte_size:
7059         default:
7060           break;
7061         }
7062     }
7063
7064   if (die->die_id.die_type_node)
7065     add_AT_die_ref (clone, DW_AT_signature, die);
7066
7067   add_AT_flag (clone, DW_AT_declaration, 1);
7068   return clone;
7069 }
7070
7071 /* Copy the declaration context to the new compile unit DIE.  This includes
7072    any surrounding namespace or type declarations.  If the DIE has an
7073    AT_specification attribute, it also includes attributes and children
7074    attached to the specification.  */
7075
7076 static void
7077 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7078 {
7079   dw_die_ref decl;
7080   dw_die_ref new_decl;
7081
7082   decl = get_AT_ref (die, DW_AT_specification);
7083   if (decl == NULL)
7084     decl = die;
7085   else
7086     {
7087       unsigned ix;
7088       dw_die_ref c;
7089       dw_attr_ref a;
7090
7091       /* Copy the type node pointer from the new DIE to the original
7092          declaration DIE so we can forward references later.  */
7093       decl->die_id.die_type_node = die->die_id.die_type_node;
7094
7095       remove_AT (die, DW_AT_specification);
7096
7097       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7098         {
7099           if (a->dw_attr != DW_AT_name
7100               && a->dw_attr != DW_AT_declaration
7101               && a->dw_attr != DW_AT_external)
7102             add_dwarf_attr (die, a);
7103         }
7104
7105       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7106     }
7107
7108   if (decl->die_parent != NULL
7109       && decl->die_parent->die_tag != DW_TAG_compile_unit
7110       && decl->die_parent->die_tag != DW_TAG_type_unit)
7111     {
7112       new_decl = copy_ancestor_tree (unit, decl, NULL);
7113       if (new_decl != NULL)
7114         {
7115           remove_AT (new_decl, DW_AT_signature);
7116           add_AT_specification (die, new_decl);
7117         }
7118     }
7119 }
7120
7121 /* Generate the skeleton ancestor tree for the given NODE, then clone
7122    the DIE and add the clone into the tree.  */
7123
7124 static void
7125 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7126 {
7127   if (node->new_die != NULL)
7128     return;
7129
7130   node->new_die = clone_as_declaration (node->old_die);
7131
7132   if (node->parent != NULL)
7133     {
7134       generate_skeleton_ancestor_tree (node->parent);
7135       add_child_die (node->parent->new_die, node->new_die);
7136     }
7137 }
7138
7139 /* Generate a skeleton tree of DIEs containing any declarations that are
7140    found in the original tree.  We traverse the tree looking for declaration
7141    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7142
7143 static void
7144 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7145 {
7146   skeleton_chain_node node;
7147   dw_die_ref c;
7148   dw_die_ref first;
7149   dw_die_ref prev = NULL;
7150   dw_die_ref next = NULL;
7151
7152   node.parent = parent;
7153
7154   first = c = parent->old_die->die_child;
7155   if (c)
7156     next = c->die_sib;
7157   if (c) do {
7158     if (prev == NULL || prev->die_sib == c)
7159       prev = c;
7160     c = next;
7161     next = (c == first ? NULL : c->die_sib);
7162     node.old_die = c;
7163     node.new_die = NULL;
7164     if (is_declaration_die (c))
7165       {
7166         /* Clone the existing DIE, move the original to the skeleton
7167            tree (which is in the main CU), and put the clone, with
7168            all the original's children, where the original came from.  */
7169         dw_die_ref clone = clone_die (c);
7170         move_all_children (c, clone);
7171
7172         replace_child (c, clone, prev);
7173         generate_skeleton_ancestor_tree (parent);
7174         add_child_die (parent->new_die, c);
7175         node.new_die = c;
7176         c = clone;
7177       }
7178     generate_skeleton_bottom_up (&node);
7179   } while (next != NULL);
7180 }
7181
7182 /* Wrapper function for generate_skeleton_bottom_up.  */
7183
7184 static dw_die_ref
7185 generate_skeleton (dw_die_ref die)
7186 {
7187   skeleton_chain_node node;
7188
7189   node.old_die = die;
7190   node.new_die = NULL;
7191   node.parent = NULL;
7192
7193   /* If this type definition is nested inside another type,
7194      always leave at least a declaration in its place.  */
7195   if (die->die_parent != NULL && is_type_die (die->die_parent))
7196     node.new_die = clone_as_declaration (die);
7197
7198   generate_skeleton_bottom_up (&node);
7199   return node.new_die;
7200 }
7201
7202 /* Remove the DIE from its parent, possibly replacing it with a cloned
7203    declaration.  The original DIE will be moved to a new compile unit
7204    so that existing references to it follow it to the new location.  If
7205    any of the original DIE's descendants is a declaration, we need to
7206    replace the original DIE with a skeleton tree and move the
7207    declarations back into the skeleton tree.  */
7208
7209 static dw_die_ref
7210 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7211 {
7212   dw_die_ref skeleton;
7213
7214   skeleton = generate_skeleton (child);
7215   if (skeleton == NULL)
7216     remove_child_with_prev (child, prev);
7217   else
7218     {
7219       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7220       replace_child (child, skeleton, prev);
7221     }
7222
7223   return skeleton;
7224 }
7225
7226 /* Traverse the DIE and set up additional .debug_types sections for each
7227    type worthy of being placed in a COMDAT section.  */
7228
7229 static void
7230 break_out_comdat_types (dw_die_ref die)
7231 {
7232   dw_die_ref c;
7233   dw_die_ref first;
7234   dw_die_ref prev = NULL;
7235   dw_die_ref next = NULL;
7236   dw_die_ref unit = NULL;
7237
7238   first = c = die->die_child;
7239   if (c)
7240     next = c->die_sib;
7241   if (c) do {
7242     if (prev == NULL || prev->die_sib == c)
7243       prev = c;
7244     c = next;
7245     next = (c == first ? NULL : c->die_sib);
7246     if (should_move_die_to_comdat (c))
7247       {
7248         dw_die_ref replacement;
7249         comdat_type_node_ref type_node;
7250
7251         /* Create a new type unit DIE as the root for the new tree, and
7252            add it to the list of comdat types.  */
7253         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7254         add_AT_unsigned (unit, DW_AT_language,
7255                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7256         type_node = ggc_alloc_cleared_comdat_type_node ();
7257         type_node->root_die = unit;
7258         type_node->next = comdat_type_list;
7259         comdat_type_list = type_node;
7260
7261         /* Generate the type signature.  */
7262         generate_type_signature (c, type_node);
7263
7264         /* Copy the declaration context, attributes, and children of the
7265            declaration into the new compile unit DIE.  */
7266         copy_declaration_context (unit, c);
7267
7268         /* Remove this DIE from the main CU.  */
7269         replacement = remove_child_or_replace_with_skeleton (c, prev);
7270
7271         /* Break out nested types into their own type units.  */
7272         break_out_comdat_types (c);
7273
7274         /* Add the DIE to the new compunit.  */
7275         add_child_die (unit, c);
7276
7277         if (replacement != NULL)
7278           c = replacement;
7279       }
7280     else if (c->die_tag == DW_TAG_namespace
7281              || c->die_tag == DW_TAG_class_type
7282              || c->die_tag == DW_TAG_structure_type
7283              || c->die_tag == DW_TAG_union_type)
7284       {
7285         /* Look for nested types that can be broken out.  */
7286         break_out_comdat_types (c);
7287       }
7288   } while (next != NULL);
7289 }
7290
7291 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7292
7293 struct decl_table_entry
7294 {
7295   dw_die_ref orig;
7296   dw_die_ref copy;
7297 };
7298
7299 /* Routines to manipulate hash table of copied declarations.  */
7300
7301 static hashval_t
7302 htab_decl_hash (const void *of)
7303 {
7304   const struct decl_table_entry *const entry =
7305     (const struct decl_table_entry *) of;
7306
7307   return htab_hash_pointer (entry->orig);
7308 }
7309
7310 static int
7311 htab_decl_eq (const void *of1, const void *of2)
7312 {
7313   const struct decl_table_entry *const entry1 =
7314     (const struct decl_table_entry *) of1;
7315   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7316
7317   return entry1->orig == entry2;
7318 }
7319
7320 static void
7321 htab_decl_del (void *what)
7322 {
7323   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7324
7325   free (entry);
7326 }
7327
7328 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7329    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7330    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7331    to check if the ancestor has already been copied into UNIT.  */
7332
7333 static dw_die_ref
7334 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7335 {
7336   dw_die_ref parent = die->die_parent;
7337   dw_die_ref new_parent = unit;
7338   dw_die_ref copy;
7339   void **slot = NULL;
7340   struct decl_table_entry *entry = NULL;
7341
7342   if (decl_table)
7343     {
7344       /* Check if the entry has already been copied to UNIT.  */
7345       slot = htab_find_slot_with_hash (decl_table, die,
7346                                        htab_hash_pointer (die), INSERT);
7347       if (*slot != HTAB_EMPTY_ENTRY)
7348         {
7349           entry = (struct decl_table_entry *) *slot;
7350           return entry->copy;
7351         }
7352
7353       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7354       entry = XCNEW (struct decl_table_entry);
7355       entry->orig = die;
7356       entry->copy = NULL;
7357       *slot = entry;
7358     }
7359
7360   if (parent != NULL)
7361     {
7362       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7363       if (spec != NULL)
7364         parent = spec;
7365       if (parent->die_tag != DW_TAG_compile_unit
7366           && parent->die_tag != DW_TAG_type_unit)
7367         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7368     }
7369
7370   copy = clone_as_declaration (die);
7371   add_child_die (new_parent, copy);
7372
7373   if (decl_table != NULL)
7374     {
7375       /* Record the pointer to the copy.  */
7376       entry->copy = copy;
7377     }
7378
7379   return copy;
7380 }
7381
7382 /* Walk the DIE and its children, looking for references to incomplete
7383    or trivial types that are unmarked (i.e., that are not in the current
7384    type_unit).  */
7385
7386 static void
7387 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7388 {
7389   dw_die_ref c;
7390   dw_attr_ref a;
7391   unsigned ix;
7392
7393   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7394     {
7395       if (AT_class (a) == dw_val_class_die_ref)
7396         {
7397           dw_die_ref targ = AT_ref (a);
7398           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7399           void **slot;
7400           struct decl_table_entry *entry;
7401
7402           if (targ->die_mark != 0 || type_node != NULL)
7403             continue;
7404
7405           slot = htab_find_slot_with_hash (decl_table, targ,
7406                                            htab_hash_pointer (targ), INSERT);
7407
7408           if (*slot != HTAB_EMPTY_ENTRY)
7409             {
7410               /* TARG has already been copied, so we just need to
7411                  modify the reference to point to the copy.  */
7412               entry = (struct decl_table_entry *) *slot;
7413               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7414             }
7415           else
7416             {
7417               dw_die_ref parent = unit;
7418               dw_die_ref copy = clone_tree (targ);
7419
7420               /* Make sure the cloned tree is marked as part of the
7421                  type unit.  */
7422               mark_dies (copy);
7423
7424               /* Record in DECL_TABLE that TARG has been copied.
7425                  Need to do this now, before the recursive call,
7426                  because DECL_TABLE may be expanded and SLOT
7427                  would no longer be a valid pointer.  */
7428               entry = XCNEW (struct decl_table_entry);
7429               entry->orig = targ;
7430               entry->copy = copy;
7431               *slot = entry;
7432
7433               /* If TARG has surrounding context, copy its ancestor tree
7434                  into the new type unit.  */
7435               if (targ->die_parent != NULL
7436                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7437                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7438                 parent = copy_ancestor_tree (unit, targ->die_parent,
7439                                              decl_table);
7440
7441               add_child_die (parent, copy);
7442               a->dw_attr_val.v.val_die_ref.die = copy;
7443
7444               /* Make sure the newly-copied DIE is walked.  If it was
7445                  installed in a previously-added context, it won't
7446                  get visited otherwise.  */
7447               if (parent != unit)
7448                 {
7449                   /* Find the highest point of the newly-added tree,
7450                      mark each node along the way, and walk from there.  */
7451                   parent->die_mark = 1;
7452                   while (parent->die_parent
7453                          && parent->die_parent->die_mark == 0)
7454                     {
7455                       parent = parent->die_parent;
7456                       parent->die_mark = 1;
7457                     }
7458                   copy_decls_walk (unit, parent, decl_table);
7459                 }
7460             }
7461         }
7462     }
7463
7464   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7465 }
7466
7467 /* Copy declarations for "unworthy" types into the new comdat section.
7468    Incomplete types, modified types, and certain other types aren't broken
7469    out into comdat sections of their own, so they don't have a signature,
7470    and we need to copy the declaration into the same section so that we
7471    don't have an external reference.  */
7472
7473 static void
7474 copy_decls_for_unworthy_types (dw_die_ref unit)
7475 {
7476   htab_t decl_table;
7477
7478   mark_dies (unit);
7479   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7480   copy_decls_walk (unit, unit, decl_table);
7481   htab_delete (decl_table);
7482   unmark_dies (unit);
7483 }
7484
7485 /* Traverse the DIE and add a sibling attribute if it may have the
7486    effect of speeding up access to siblings.  To save some space,
7487    avoid generating sibling attributes for DIE's without children.  */
7488
7489 static void
7490 add_sibling_attributes (dw_die_ref die)
7491 {
7492   dw_die_ref c;
7493
7494   if (! die->die_child)
7495     return;
7496
7497   if (die->die_parent && die != die->die_parent->die_child)
7498     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7499
7500   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7501 }
7502
7503 /* Output all location lists for the DIE and its children.  */
7504
7505 static void
7506 output_location_lists (dw_die_ref die)
7507 {
7508   dw_die_ref c;
7509   dw_attr_ref a;
7510   unsigned ix;
7511
7512   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7513     if (AT_class (a) == dw_val_class_loc_list)
7514       output_loc_list (AT_loc_list (a));
7515
7516   FOR_EACH_CHILD (die, c, output_location_lists (c));
7517 }
7518
7519 /* The format of each DIE (and its attribute value pairs) is encoded in an
7520    abbreviation table.  This routine builds the abbreviation table and assigns
7521    a unique abbreviation id for each abbreviation entry.  The children of each
7522    die are visited recursively.  */
7523
7524 static void
7525 build_abbrev_table (dw_die_ref die)
7526 {
7527   unsigned long abbrev_id;
7528   unsigned int n_alloc;
7529   dw_die_ref c;
7530   dw_attr_ref a;
7531   unsigned ix;
7532
7533   /* Scan the DIE references, and mark as external any that refer to
7534      DIEs from other CUs (i.e. those which are not marked).  */
7535   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7536     if (AT_class (a) == dw_val_class_die_ref
7537         && AT_ref (a)->die_mark == 0)
7538       {
7539         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7540         set_AT_ref_external (a, 1);
7541       }
7542
7543   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7544     {
7545       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7546       dw_attr_ref die_a, abbrev_a;
7547       unsigned ix;
7548       bool ok = true;
7549
7550       if (abbrev->die_tag != die->die_tag)
7551         continue;
7552       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7553         continue;
7554
7555       if (VEC_length (dw_attr_node, abbrev->die_attr)
7556           != VEC_length (dw_attr_node, die->die_attr))
7557         continue;
7558
7559       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7560         {
7561           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7562           if ((abbrev_a->dw_attr != die_a->dw_attr)
7563               || (value_format (abbrev_a) != value_format (die_a)))
7564             {
7565               ok = false;
7566               break;
7567             }
7568         }
7569       if (ok)
7570         break;
7571     }
7572
7573   if (abbrev_id >= abbrev_die_table_in_use)
7574     {
7575       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7576         {
7577           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7578           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7579                                             n_alloc);
7580
7581           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7582                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7583           abbrev_die_table_allocated = n_alloc;
7584         }
7585
7586       ++abbrev_die_table_in_use;
7587       abbrev_die_table[abbrev_id] = die;
7588     }
7589
7590   die->die_abbrev = abbrev_id;
7591   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7592 }
7593 \f
7594 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7595
7596 static int
7597 constant_size (unsigned HOST_WIDE_INT value)
7598 {
7599   int log;
7600
7601   if (value == 0)
7602     log = 0;
7603   else
7604     log = floor_log2 (value);
7605
7606   log = log / 8;
7607   log = 1 << (floor_log2 (log) + 1);
7608
7609   return log;
7610 }
7611
7612 /* Return the size of a DIE as it is represented in the
7613    .debug_info section.  */
7614
7615 static unsigned long
7616 size_of_die (dw_die_ref die)
7617 {
7618   unsigned long size = 0;
7619   dw_attr_ref a;
7620   unsigned ix;
7621
7622   size += size_of_uleb128 (die->die_abbrev);
7623   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7624     {
7625       switch (AT_class (a))
7626         {
7627         case dw_val_class_addr:
7628           size += DWARF2_ADDR_SIZE;
7629           break;
7630         case dw_val_class_offset:
7631           size += DWARF_OFFSET_SIZE;
7632           break;
7633         case dw_val_class_loc:
7634           {
7635             unsigned long lsize = size_of_locs (AT_loc (a));
7636
7637             /* Block length.  */
7638             if (dwarf_version >= 4)
7639               size += size_of_uleb128 (lsize);
7640             else
7641               size += constant_size (lsize);
7642             size += lsize;
7643           }
7644           break;
7645         case dw_val_class_loc_list:
7646           size += DWARF_OFFSET_SIZE;
7647           break;
7648         case dw_val_class_range_list:
7649           size += DWARF_OFFSET_SIZE;
7650           break;
7651         case dw_val_class_const:
7652           size += size_of_sleb128 (AT_int (a));
7653           break;
7654         case dw_val_class_unsigned_const:
7655           size += constant_size (AT_unsigned (a));
7656           break;
7657         case dw_val_class_const_double:
7658           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7659           if (HOST_BITS_PER_WIDE_INT >= 64)
7660             size++; /* block */
7661           break;
7662         case dw_val_class_vec:
7663           size += constant_size (a->dw_attr_val.v.val_vec.length
7664                                  * a->dw_attr_val.v.val_vec.elt_size)
7665                   + a->dw_attr_val.v.val_vec.length
7666                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7667           break;
7668         case dw_val_class_flag:
7669           if (dwarf_version >= 4)
7670             /* Currently all add_AT_flag calls pass in 1 as last argument,
7671                so DW_FORM_flag_present can be used.  If that ever changes,
7672                we'll need to use DW_FORM_flag and have some optimization
7673                in build_abbrev_table that will change those to
7674                DW_FORM_flag_present if it is set to 1 in all DIEs using
7675                the same abbrev entry.  */
7676             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7677           else
7678             size += 1;
7679           break;
7680         case dw_val_class_die_ref:
7681           if (AT_ref_external (a))
7682             {
7683               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7684                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7685                  is sized by target address length, whereas in DWARF3
7686                  it's always sized as an offset.  */
7687               if (use_debug_types)
7688                 size += DWARF_TYPE_SIGNATURE_SIZE;
7689               else if (dwarf_version == 2)
7690                 size += DWARF2_ADDR_SIZE;
7691               else
7692                 size += DWARF_OFFSET_SIZE;
7693             }
7694           else
7695             size += DWARF_OFFSET_SIZE;
7696           break;
7697         case dw_val_class_fde_ref:
7698           size += DWARF_OFFSET_SIZE;
7699           break;
7700         case dw_val_class_lbl_id:
7701           size += DWARF2_ADDR_SIZE;
7702           break;
7703         case dw_val_class_lineptr:
7704         case dw_val_class_macptr:
7705           size += DWARF_OFFSET_SIZE;
7706           break;
7707         case dw_val_class_str:
7708           if (AT_string_form (a) == DW_FORM_strp)
7709             size += DWARF_OFFSET_SIZE;
7710           else
7711             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7712           break;
7713         case dw_val_class_file:
7714           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7715           break;
7716         case dw_val_class_data8:
7717           size += 8;
7718           break;
7719         case dw_val_class_vms_delta:
7720           size += DWARF_OFFSET_SIZE;
7721           break;
7722         default:
7723           gcc_unreachable ();
7724         }
7725     }
7726
7727   return size;
7728 }
7729
7730 /* Size the debugging information associated with a given DIE.  Visits the
7731    DIE's children recursively.  Updates the global variable next_die_offset, on
7732    each time through.  Uses the current value of next_die_offset to update the
7733    die_offset field in each DIE.  */
7734
7735 static void
7736 calc_die_sizes (dw_die_ref die)
7737 {
7738   dw_die_ref c;
7739
7740   gcc_assert (die->die_offset == 0
7741               || (unsigned long int) die->die_offset == next_die_offset);
7742   die->die_offset = next_die_offset;
7743   next_die_offset += size_of_die (die);
7744
7745   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7746
7747   if (die->die_child != NULL)
7748     /* Count the null byte used to terminate sibling lists.  */
7749     next_die_offset += 1;
7750 }
7751
7752 /* Size just the base type children at the start of the CU.
7753    This is needed because build_abbrev needs to size locs
7754    and sizing of type based stack ops needs to know die_offset
7755    values for the base types.  */
7756
7757 static void
7758 calc_base_type_die_sizes (void)
7759 {
7760   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7761   unsigned int i;
7762   dw_die_ref base_type;
7763 #if ENABLE_ASSERT_CHECKING
7764   dw_die_ref prev = comp_unit_die ()->die_child;
7765 #endif
7766
7767   die_offset += size_of_die (comp_unit_die ());
7768   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7769     {
7770 #if ENABLE_ASSERT_CHECKING
7771       gcc_assert (base_type->die_offset == 0
7772                   && prev->die_sib == base_type
7773                   && base_type->die_child == NULL
7774                   && base_type->die_abbrev);
7775       prev = base_type;
7776 #endif
7777       base_type->die_offset = die_offset;
7778       die_offset += size_of_die (base_type);
7779     }
7780 }
7781
7782 /* Set the marks for a die and its children.  We do this so
7783    that we know whether or not a reference needs to use FORM_ref_addr; only
7784    DIEs in the same CU will be marked.  We used to clear out the offset
7785    and use that as the flag, but ran into ordering problems.  */
7786
7787 static void
7788 mark_dies (dw_die_ref die)
7789 {
7790   dw_die_ref c;
7791
7792   gcc_assert (!die->die_mark);
7793
7794   die->die_mark = 1;
7795   FOR_EACH_CHILD (die, c, mark_dies (c));
7796 }
7797
7798 /* Clear the marks for a die and its children.  */
7799
7800 static void
7801 unmark_dies (dw_die_ref die)
7802 {
7803   dw_die_ref c;
7804
7805   if (! use_debug_types)
7806     gcc_assert (die->die_mark);
7807
7808   die->die_mark = 0;
7809   FOR_EACH_CHILD (die, c, unmark_dies (c));
7810 }
7811
7812 /* Clear the marks for a die, its children and referred dies.  */
7813
7814 static void
7815 unmark_all_dies (dw_die_ref die)
7816 {
7817   dw_die_ref c;
7818   dw_attr_ref a;
7819   unsigned ix;
7820
7821   if (!die->die_mark)
7822     return;
7823   die->die_mark = 0;
7824
7825   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7826
7827   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7828     if (AT_class (a) == dw_val_class_die_ref)
7829       unmark_all_dies (AT_ref (a));
7830 }
7831
7832 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7833    generated for the compilation unit.  */
7834
7835 static unsigned long
7836 size_of_pubnames (VEC (pubname_entry, gc) * names)
7837 {
7838   unsigned long size;
7839   unsigned i;
7840   pubname_ref p;
7841
7842   size = DWARF_PUBNAMES_HEADER_SIZE;
7843   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7844     if (names != pubtype_table
7845         || p->die->die_offset != 0
7846         || !flag_eliminate_unused_debug_types)
7847       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7848
7849   size += DWARF_OFFSET_SIZE;
7850   return size;
7851 }
7852
7853 /* Return the size of the information in the .debug_aranges section.  */
7854
7855 static unsigned long
7856 size_of_aranges (void)
7857 {
7858   unsigned long size;
7859
7860   size = DWARF_ARANGES_HEADER_SIZE;
7861
7862   /* Count the address/length pair for this compilation unit.  */
7863   if (text_section_used)
7864     size += 2 * DWARF2_ADDR_SIZE;
7865   if (cold_text_section_used)
7866     size += 2 * DWARF2_ADDR_SIZE;
7867   if (have_multiple_function_sections)
7868     {
7869       unsigned fde_idx;
7870       dw_fde_ref fde;
7871
7872       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7873         {
7874           if (!fde->in_std_section)
7875             size += 2 * DWARF2_ADDR_SIZE;
7876           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7877             size += 2 * DWARF2_ADDR_SIZE;
7878         }
7879     }
7880
7881   /* Count the two zero words used to terminated the address range table.  */
7882   size += 2 * DWARF2_ADDR_SIZE;
7883   return size;
7884 }
7885 \f
7886 /* Select the encoding of an attribute value.  */
7887
7888 static enum dwarf_form
7889 value_format (dw_attr_ref a)
7890 {
7891   switch (a->dw_attr_val.val_class)
7892     {
7893     case dw_val_class_addr:
7894       /* Only very few attributes allow DW_FORM_addr.  */
7895       switch (a->dw_attr)
7896         {
7897         case DW_AT_low_pc:
7898         case DW_AT_high_pc:
7899         case DW_AT_entry_pc:
7900         case DW_AT_trampoline:
7901           return DW_FORM_addr;
7902         default:
7903           break;
7904         }
7905       switch (DWARF2_ADDR_SIZE)
7906         {
7907         case 1:
7908           return DW_FORM_data1;
7909         case 2:
7910           return DW_FORM_data2;
7911         case 4:
7912           return DW_FORM_data4;
7913         case 8:
7914           return DW_FORM_data8;
7915         default:
7916           gcc_unreachable ();
7917         }
7918     case dw_val_class_range_list:
7919     case dw_val_class_loc_list:
7920       if (dwarf_version >= 4)
7921         return DW_FORM_sec_offset;
7922       /* FALLTHRU */
7923     case dw_val_class_vms_delta:
7924     case dw_val_class_offset:
7925       switch (DWARF_OFFSET_SIZE)
7926         {
7927         case 4:
7928           return DW_FORM_data4;
7929         case 8:
7930           return DW_FORM_data8;
7931         default:
7932           gcc_unreachable ();
7933         }
7934     case dw_val_class_loc:
7935       if (dwarf_version >= 4)
7936         return DW_FORM_exprloc;
7937       switch (constant_size (size_of_locs (AT_loc (a))))
7938         {
7939         case 1:
7940           return DW_FORM_block1;
7941         case 2:
7942           return DW_FORM_block2;
7943         default:
7944           gcc_unreachable ();
7945         }
7946     case dw_val_class_const:
7947       return DW_FORM_sdata;
7948     case dw_val_class_unsigned_const:
7949       switch (constant_size (AT_unsigned (a)))
7950         {
7951         case 1:
7952           return DW_FORM_data1;
7953         case 2:
7954           return DW_FORM_data2;
7955         case 4:
7956           return DW_FORM_data4;
7957         case 8:
7958           return DW_FORM_data8;
7959         default:
7960           gcc_unreachable ();
7961         }
7962     case dw_val_class_const_double:
7963       switch (HOST_BITS_PER_WIDE_INT)
7964         {
7965         case 8:
7966           return DW_FORM_data2;
7967         case 16:
7968           return DW_FORM_data4;
7969         case 32:
7970           return DW_FORM_data8;
7971         case 64:
7972         default:
7973           return DW_FORM_block1;
7974         }
7975     case dw_val_class_vec:
7976       switch (constant_size (a->dw_attr_val.v.val_vec.length
7977                              * a->dw_attr_val.v.val_vec.elt_size))
7978         {
7979         case 1:
7980           return DW_FORM_block1;
7981         case 2:
7982           return DW_FORM_block2;
7983         case 4:
7984           return DW_FORM_block4;
7985         default:
7986           gcc_unreachable ();
7987         }
7988     case dw_val_class_flag:
7989       if (dwarf_version >= 4)
7990         {
7991           /* Currently all add_AT_flag calls pass in 1 as last argument,
7992              so DW_FORM_flag_present can be used.  If that ever changes,
7993              we'll need to use DW_FORM_flag and have some optimization
7994              in build_abbrev_table that will change those to
7995              DW_FORM_flag_present if it is set to 1 in all DIEs using
7996              the same abbrev entry.  */
7997           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7998           return DW_FORM_flag_present;
7999         }
8000       return DW_FORM_flag;
8001     case dw_val_class_die_ref:
8002       if (AT_ref_external (a))
8003         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8004       else
8005         return DW_FORM_ref;
8006     case dw_val_class_fde_ref:
8007       return DW_FORM_data;
8008     case dw_val_class_lbl_id:
8009       return DW_FORM_addr;
8010     case dw_val_class_lineptr:
8011     case dw_val_class_macptr:
8012       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8013     case dw_val_class_str:
8014       return AT_string_form (a);
8015     case dw_val_class_file:
8016       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8017         {
8018         case 1:
8019           return DW_FORM_data1;
8020         case 2:
8021           return DW_FORM_data2;
8022         case 4:
8023           return DW_FORM_data4;
8024         default:
8025           gcc_unreachable ();
8026         }
8027
8028     case dw_val_class_data8:
8029       return DW_FORM_data8;
8030
8031     default:
8032       gcc_unreachable ();
8033     }
8034 }
8035
8036 /* Output the encoding of an attribute value.  */
8037
8038 static void
8039 output_value_format (dw_attr_ref a)
8040 {
8041   enum dwarf_form form = value_format (a);
8042
8043   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8044 }
8045
8046 /* Output the .debug_abbrev section which defines the DIE abbreviation
8047    table.  */
8048
8049 static void
8050 output_abbrev_section (void)
8051 {
8052   unsigned long abbrev_id;
8053
8054   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8055     {
8056       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8057       unsigned ix;
8058       dw_attr_ref a_attr;
8059
8060       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8061       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8062                                    dwarf_tag_name (abbrev->die_tag));
8063
8064       if (abbrev->die_child != NULL)
8065         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8066       else
8067         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8068
8069       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8070            ix++)
8071         {
8072           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8073                                        dwarf_attr_name (a_attr->dw_attr));
8074           output_value_format (a_attr);
8075         }
8076
8077       dw2_asm_output_data (1, 0, NULL);
8078       dw2_asm_output_data (1, 0, NULL);
8079     }
8080
8081   /* Terminate the table.  */
8082   dw2_asm_output_data (1, 0, NULL);
8083 }
8084
8085 /* Output a symbol we can use to refer to this DIE from another CU.  */
8086
8087 static inline void
8088 output_die_symbol (dw_die_ref die)
8089 {
8090   char *sym = die->die_id.die_symbol;
8091
8092   if (sym == 0)
8093     return;
8094
8095   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8096     /* We make these global, not weak; if the target doesn't support
8097        .linkonce, it doesn't support combining the sections, so debugging
8098        will break.  */
8099     targetm.asm_out.globalize_label (asm_out_file, sym);
8100
8101   ASM_OUTPUT_LABEL (asm_out_file, sym);
8102 }
8103
8104 /* Return a new location list, given the begin and end range, and the
8105    expression.  */
8106
8107 static inline dw_loc_list_ref
8108 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8109               const char *section)
8110 {
8111   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8112
8113   retlist->begin = begin;
8114   retlist->end = end;
8115   retlist->expr = expr;
8116   retlist->section = section;
8117
8118   return retlist;
8119 }
8120
8121 /* Generate a new internal symbol for this location list node, if it
8122    hasn't got one yet.  */
8123
8124 static inline void
8125 gen_llsym (dw_loc_list_ref list)
8126 {
8127   gcc_assert (!list->ll_symbol);
8128   list->ll_symbol = gen_internal_sym ("LLST");
8129 }
8130
8131 /* Output the location list given to us.  */
8132
8133 static void
8134 output_loc_list (dw_loc_list_ref list_head)
8135 {
8136   dw_loc_list_ref curr = list_head;
8137
8138   if (list_head->emitted)
8139     return;
8140   list_head->emitted = true;
8141
8142   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8143
8144   /* Walk the location list, and output each range + expression.  */
8145   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8146     {
8147       unsigned long size;
8148       /* Don't output an entry that starts and ends at the same address.  */
8149       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8150         continue;
8151       if (!have_multiple_function_sections)
8152         {
8153           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8154                                 "Location list begin address (%s)",
8155                                 list_head->ll_symbol);
8156           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8157                                 "Location list end address (%s)",
8158                                 list_head->ll_symbol);
8159         }
8160       else
8161         {
8162           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8163                                "Location list begin address (%s)",
8164                                list_head->ll_symbol);
8165           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8166                                "Location list end address (%s)",
8167                                list_head->ll_symbol);
8168         }
8169       size = size_of_locs (curr->expr);
8170
8171       /* Output the block length for this list of location operations.  */
8172       gcc_assert (size <= 0xffff);
8173       dw2_asm_output_data (2, size, "%s", "Location expression size");
8174
8175       output_loc_sequence (curr->expr, -1);
8176     }
8177
8178   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8179                        "Location list terminator begin (%s)",
8180                        list_head->ll_symbol);
8181   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8182                        "Location list terminator end (%s)",
8183                        list_head->ll_symbol);
8184 }
8185
8186 /* Output a type signature.  */
8187
8188 static inline void
8189 output_signature (const char *sig, const char *name)
8190 {
8191   int i;
8192
8193   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8194     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8195 }
8196
8197 /* Output the DIE and its attributes.  Called recursively to generate
8198    the definitions of each child DIE.  */
8199
8200 static void
8201 output_die (dw_die_ref die)
8202 {
8203   dw_attr_ref a;
8204   dw_die_ref c;
8205   unsigned long size;
8206   unsigned ix;
8207
8208   /* If someone in another CU might refer to us, set up a symbol for
8209      them to point to.  */
8210   if (! use_debug_types && die->die_id.die_symbol)
8211     output_die_symbol (die);
8212
8213   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8214                                (unsigned long)die->die_offset,
8215                                dwarf_tag_name (die->die_tag));
8216
8217   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8218     {
8219       const char *name = dwarf_attr_name (a->dw_attr);
8220
8221       switch (AT_class (a))
8222         {
8223         case dw_val_class_addr:
8224           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8225           break;
8226
8227         case dw_val_class_offset:
8228           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8229                                "%s", name);
8230           break;
8231
8232         case dw_val_class_range_list:
8233           {
8234             char *p = strchr (ranges_section_label, '\0');
8235
8236             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8237                      a->dw_attr_val.v.val_offset);
8238             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8239                                    debug_ranges_section, "%s", name);
8240             *p = '\0';
8241           }
8242           break;
8243
8244         case dw_val_class_loc:
8245           size = size_of_locs (AT_loc (a));
8246
8247           /* Output the block length for this list of location operations.  */
8248           if (dwarf_version >= 4)
8249             dw2_asm_output_data_uleb128 (size, "%s", name);
8250           else
8251             dw2_asm_output_data (constant_size (size), size, "%s", name);
8252
8253           output_loc_sequence (AT_loc (a), -1);
8254           break;
8255
8256         case dw_val_class_const:
8257           /* ??? It would be slightly more efficient to use a scheme like is
8258              used for unsigned constants below, but gdb 4.x does not sign
8259              extend.  Gdb 5.x does sign extend.  */
8260           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8261           break;
8262
8263         case dw_val_class_unsigned_const:
8264           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8265                                AT_unsigned (a), "%s", name);
8266           break;
8267
8268         case dw_val_class_const_double:
8269           {
8270             unsigned HOST_WIDE_INT first, second;
8271
8272             if (HOST_BITS_PER_WIDE_INT >= 64)
8273               dw2_asm_output_data (1,
8274                                    2 * HOST_BITS_PER_WIDE_INT
8275                                    / HOST_BITS_PER_CHAR,
8276                                    NULL);
8277
8278             if (WORDS_BIG_ENDIAN)
8279               {
8280                 first = a->dw_attr_val.v.val_double.high;
8281                 second = a->dw_attr_val.v.val_double.low;
8282               }
8283             else
8284               {
8285                 first = a->dw_attr_val.v.val_double.low;
8286                 second = a->dw_attr_val.v.val_double.high;
8287               }
8288
8289             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8290                                  first, name);
8291             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8292                                  second, NULL);
8293           }
8294           break;
8295
8296         case dw_val_class_vec:
8297           {
8298             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8299             unsigned int len = a->dw_attr_val.v.val_vec.length;
8300             unsigned int i;
8301             unsigned char *p;
8302
8303             dw2_asm_output_data (constant_size (len * elt_size),
8304                                  len * elt_size, "%s", name);
8305             if (elt_size > sizeof (HOST_WIDE_INT))
8306               {
8307                 elt_size /= 2;
8308                 len *= 2;
8309               }
8310             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8311                  i < len;
8312                  i++, p += elt_size)
8313               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8314                                    "fp or vector constant word %u", i);
8315             break;
8316           }
8317
8318         case dw_val_class_flag:
8319           if (dwarf_version >= 4)
8320             {
8321               /* Currently all add_AT_flag calls pass in 1 as last argument,
8322                  so DW_FORM_flag_present can be used.  If that ever changes,
8323                  we'll need to use DW_FORM_flag and have some optimization
8324                  in build_abbrev_table that will change those to
8325                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8326                  the same abbrev entry.  */
8327               gcc_assert (AT_flag (a) == 1);
8328               if (flag_debug_asm)
8329                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8330                          ASM_COMMENT_START, name);
8331               break;
8332             }
8333           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8334           break;
8335
8336         case dw_val_class_loc_list:
8337           {
8338             char *sym = AT_loc_list (a)->ll_symbol;
8339
8340             gcc_assert (sym);
8341             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8342                                    "%s", name);
8343           }
8344           break;
8345
8346         case dw_val_class_die_ref:
8347           if (AT_ref_external (a))
8348             {
8349               if (use_debug_types)
8350                 {
8351                   comdat_type_node_ref type_node =
8352                     AT_ref (a)->die_id.die_type_node;
8353
8354                   gcc_assert (type_node);
8355                   output_signature (type_node->signature, name);
8356                 }
8357               else
8358                 {
8359                   char *sym = AT_ref (a)->die_id.die_symbol;
8360                   int size;
8361
8362                   gcc_assert (sym);
8363                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8364                      length, whereas in DWARF3 it's always sized as an
8365                      offset.  */
8366                   if (dwarf_version == 2)
8367                     size = DWARF2_ADDR_SIZE;
8368                   else
8369                     size = DWARF_OFFSET_SIZE;
8370                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8371                                          name);
8372                 }
8373             }
8374           else
8375             {
8376               gcc_assert (AT_ref (a)->die_offset);
8377               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8378                                    "%s", name);
8379             }
8380           break;
8381
8382         case dw_val_class_fde_ref:
8383           {
8384             char l1[20];
8385
8386             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8387                                          a->dw_attr_val.v.val_fde_index * 2);
8388             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8389                                    "%s", name);
8390           }
8391           break;
8392
8393         case dw_val_class_vms_delta:
8394           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8395                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8396                                     "%s", name);
8397           break;
8398
8399         case dw_val_class_lbl_id:
8400           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8401           break;
8402
8403         case dw_val_class_lineptr:
8404           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8405                                  debug_line_section, "%s", name);
8406           break;
8407
8408         case dw_val_class_macptr:
8409           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8410                                  debug_macinfo_section, "%s", name);
8411           break;
8412
8413         case dw_val_class_str:
8414           if (AT_string_form (a) == DW_FORM_strp)
8415             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8416                                    a->dw_attr_val.v.val_str->label,
8417                                    debug_str_section,
8418                                    "%s: \"%s\"", name, AT_string (a));
8419           else
8420             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8421           break;
8422
8423         case dw_val_class_file:
8424           {
8425             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8426
8427             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8428                                  a->dw_attr_val.v.val_file->filename);
8429             break;
8430           }
8431
8432         case dw_val_class_data8:
8433           {
8434             int i;
8435
8436             for (i = 0; i < 8; i++)
8437               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8438                                    i == 0 ? "%s" : NULL, name);
8439             break;
8440           }
8441
8442         default:
8443           gcc_unreachable ();
8444         }
8445     }
8446
8447   FOR_EACH_CHILD (die, c, output_die (c));
8448
8449   /* Add null byte to terminate sibling list.  */
8450   if (die->die_child != NULL)
8451     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8452                          (unsigned long) die->die_offset);
8453 }
8454
8455 /* Output the compilation unit that appears at the beginning of the
8456    .debug_info section, and precedes the DIE descriptions.  */
8457
8458 static void
8459 output_compilation_unit_header (void)
8460 {
8461   int ver = dwarf_version;
8462
8463   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8464     dw2_asm_output_data (4, 0xffffffff,
8465       "Initial length escape value indicating 64-bit DWARF extension");
8466   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8467                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8468                        "Length of Compilation Unit Info");
8469   dw2_asm_output_data (2, ver, "DWARF version number");
8470   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8471                          debug_abbrev_section,
8472                          "Offset Into Abbrev. Section");
8473   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8474 }
8475
8476 /* Output the compilation unit DIE and its children.  */
8477
8478 static void
8479 output_comp_unit (dw_die_ref die, int output_if_empty)
8480 {
8481   const char *secname;
8482   char *oldsym, *tmp;
8483
8484   /* Unless we are outputting main CU, we may throw away empty ones.  */
8485   if (!output_if_empty && die->die_child == NULL)
8486     return;
8487
8488   /* Even if there are no children of this DIE, we must output the information
8489      about the compilation unit.  Otherwise, on an empty translation unit, we
8490      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8491      will then complain when examining the file.  First mark all the DIEs in
8492      this CU so we know which get local refs.  */
8493   mark_dies (die);
8494
8495   build_abbrev_table (die);
8496
8497   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8498   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8499   calc_die_sizes (die);
8500
8501   oldsym = die->die_id.die_symbol;
8502   if (oldsym)
8503     {
8504       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8505
8506       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8507       secname = tmp;
8508       die->die_id.die_symbol = NULL;
8509       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8510     }
8511   else
8512     {
8513       switch_to_section (debug_info_section);
8514       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8515       info_section_emitted = true;
8516     }
8517
8518   /* Output debugging information.  */
8519   output_compilation_unit_header ();
8520   output_die (die);
8521
8522   /* Leave the marks on the main CU, so we can check them in
8523      output_pubnames.  */
8524   if (oldsym)
8525     {
8526       unmark_dies (die);
8527       die->die_id.die_symbol = oldsym;
8528     }
8529 }
8530
8531 /* Output a comdat type unit DIE and its children.  */
8532
8533 static void
8534 output_comdat_type_unit (comdat_type_node *node)
8535 {
8536   const char *secname;
8537   char *tmp;
8538   int i;
8539 #if defined (OBJECT_FORMAT_ELF)
8540   tree comdat_key;
8541 #endif
8542
8543   /* First mark all the DIEs in this CU so we know which get local refs.  */
8544   mark_dies (node->root_die);
8545
8546   build_abbrev_table (node->root_die);
8547
8548   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8549   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8550   calc_die_sizes (node->root_die);
8551
8552 #if defined (OBJECT_FORMAT_ELF)
8553   secname = ".debug_types";
8554   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8555   sprintf (tmp, "wt.");
8556   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8557     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8558   comdat_key = get_identifier (tmp);
8559   targetm.asm_out.named_section (secname,
8560                                  SECTION_DEBUG | SECTION_LINKONCE,
8561                                  comdat_key);
8562 #else
8563   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8564   sprintf (tmp, ".gnu.linkonce.wt.");
8565   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8566     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8567   secname = tmp;
8568   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8569 #endif
8570
8571   /* Output debugging information.  */
8572   output_compilation_unit_header ();
8573   output_signature (node->signature, "Type Signature");
8574   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8575                        "Offset to Type DIE");
8576   output_die (node->root_die);
8577
8578   unmark_dies (node->root_die);
8579 }
8580
8581 /* Return the DWARF2/3 pubname associated with a decl.  */
8582
8583 static const char *
8584 dwarf2_name (tree decl, int scope)
8585 {
8586   if (DECL_NAMELESS (decl))
8587     return NULL;
8588   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8589 }
8590
8591 /* Add a new entry to .debug_pubnames if appropriate.  */
8592
8593 static void
8594 add_pubname_string (const char *str, dw_die_ref die)
8595 {
8596   if (targetm.want_debug_pub_sections)
8597     {
8598       pubname_entry e;
8599
8600       e.die = die;
8601       e.name = xstrdup (str);
8602       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8603     }
8604 }
8605
8606 static void
8607 add_pubname (tree decl, dw_die_ref die)
8608 {
8609   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8610     {
8611       const char *name = dwarf2_name (decl, 1);
8612       if (name)
8613         add_pubname_string (name, die);
8614     }
8615 }
8616
8617 /* Add a new entry to .debug_pubtypes if appropriate.  */
8618
8619 static void
8620 add_pubtype (tree decl, dw_die_ref die)
8621 {
8622   pubname_entry e;
8623
8624   if (!targetm.want_debug_pub_sections)
8625     return;
8626
8627   e.name = NULL;
8628   if ((TREE_PUBLIC (decl)
8629        || is_cu_die (die->die_parent))
8630       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8631     {
8632       e.die = die;
8633       if (TYPE_P (decl))
8634         {
8635           if (TYPE_NAME (decl))
8636             {
8637               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8638                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8639               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8640                        && DECL_NAME (TYPE_NAME (decl)))
8641                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8642               else
8643                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8644             }
8645         }
8646       else
8647         {
8648           e.name = dwarf2_name (decl, 1);
8649           if (e.name)
8650             e.name = xstrdup (e.name);
8651         }
8652
8653       /* If we don't have a name for the type, there's no point in adding
8654          it to the table.  */
8655       if (e.name && e.name[0] != '\0')
8656         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8657     }
8658 }
8659
8660 /* Output the public names table used to speed up access to externally
8661    visible names; or the public types table used to find type definitions.  */
8662
8663 static void
8664 output_pubnames (VEC (pubname_entry, gc) * names)
8665 {
8666   unsigned i;
8667   unsigned long pubnames_length = size_of_pubnames (names);
8668   pubname_ref pub;
8669
8670   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8671     dw2_asm_output_data (4, 0xffffffff,
8672       "Initial length escape value indicating 64-bit DWARF extension");
8673   if (names == pubname_table)
8674     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8675                          "Length of Public Names Info");
8676   else
8677     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8678                          "Length of Public Type Names Info");
8679   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8680   dw2_asm_output_data (2, 2, "DWARF Version");
8681   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8682                          debug_info_section,
8683                          "Offset of Compilation Unit Info");
8684   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8685                        "Compilation Unit Length");
8686
8687   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8688     {
8689       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8690       if (names == pubname_table)
8691         gcc_assert (pub->die->die_mark);
8692
8693       if (names != pubtype_table
8694           || pub->die->die_offset != 0
8695           || !flag_eliminate_unused_debug_types)
8696         {
8697           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8698                                "DIE offset");
8699
8700           dw2_asm_output_nstring (pub->name, -1, "external name");
8701         }
8702     }
8703
8704   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8705 }
8706
8707 /* Output the information that goes into the .debug_aranges table.
8708    Namely, define the beginning and ending address range of the
8709    text section generated for this compilation unit.  */
8710
8711 static void
8712 output_aranges (unsigned long aranges_length)
8713 {
8714   unsigned i;
8715
8716   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8717     dw2_asm_output_data (4, 0xffffffff,
8718       "Initial length escape value indicating 64-bit DWARF extension");
8719   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8720                        "Length of Address Ranges Info");
8721   /* Version number for aranges is still 2, even in DWARF3.  */
8722   dw2_asm_output_data (2, 2, "DWARF Version");
8723   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8724                          debug_info_section,
8725                          "Offset of Compilation Unit Info");
8726   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8727   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8728
8729   /* We need to align to twice the pointer size here.  */
8730   if (DWARF_ARANGES_PAD_SIZE)
8731     {
8732       /* Pad using a 2 byte words so that padding is correct for any
8733          pointer size.  */
8734       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8735                            2 * DWARF2_ADDR_SIZE);
8736       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8737         dw2_asm_output_data (2, 0, NULL);
8738     }
8739
8740   /* It is necessary not to output these entries if the sections were
8741      not used; if the sections were not used, the length will be 0 and
8742      the address may end up as 0 if the section is discarded by ld
8743      --gc-sections, leaving an invalid (0, 0) entry that can be
8744      confused with the terminator.  */
8745   if (text_section_used)
8746     {
8747       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8748       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8749                             text_section_label, "Length");
8750     }
8751   if (cold_text_section_used)
8752     {
8753       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8754                            "Address");
8755       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8756                             cold_text_section_label, "Length");
8757     }
8758
8759   if (have_multiple_function_sections)
8760     {
8761       unsigned fde_idx;
8762       dw_fde_ref fde;
8763
8764       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8765         {
8766           if (!fde->in_std_section)
8767             {
8768               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8769                                    "Address");
8770               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8771                                     fde->dw_fde_begin, "Length");
8772             }
8773           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8774             {
8775               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8776                                    "Address");
8777               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8778                                     fde->dw_fde_second_begin, "Length");
8779             }
8780         }
8781     }
8782
8783   /* Output the terminator words.  */
8784   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8785   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8786 }
8787
8788 /* Add a new entry to .debug_ranges.  Return the offset at which it
8789    was placed.  */
8790
8791 static unsigned int
8792 add_ranges_num (int num)
8793 {
8794   unsigned int in_use = ranges_table_in_use;
8795
8796   if (in_use == ranges_table_allocated)
8797     {
8798       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8799       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8800                                     ranges_table_allocated);
8801       memset (ranges_table + ranges_table_in_use, 0,
8802               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8803     }
8804
8805   ranges_table[in_use].num = num;
8806   ranges_table_in_use = in_use + 1;
8807
8808   return in_use * 2 * DWARF2_ADDR_SIZE;
8809 }
8810
8811 /* Add a new entry to .debug_ranges corresponding to a block, or a
8812    range terminator if BLOCK is NULL.  */
8813
8814 static unsigned int
8815 add_ranges (const_tree block)
8816 {
8817   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8818 }
8819
8820 /* Add a new entry to .debug_ranges corresponding to a pair of
8821    labels.  */
8822
8823 static void
8824 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8825                       bool *added)
8826 {
8827   unsigned int in_use = ranges_by_label_in_use;
8828   unsigned int offset;
8829
8830   if (in_use == ranges_by_label_allocated)
8831     {
8832       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8833       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8834                                        ranges_by_label,
8835                                        ranges_by_label_allocated);
8836       memset (ranges_by_label + ranges_by_label_in_use, 0,
8837               RANGES_TABLE_INCREMENT
8838               * sizeof (struct dw_ranges_by_label_struct));
8839     }
8840
8841   ranges_by_label[in_use].begin = begin;
8842   ranges_by_label[in_use].end = end;
8843   ranges_by_label_in_use = in_use + 1;
8844
8845   offset = add_ranges_num (-(int)in_use - 1);
8846   if (!*added)
8847     {
8848       add_AT_range_list (die, DW_AT_ranges, offset);
8849       *added = true;
8850     }
8851 }
8852
8853 static void
8854 output_ranges (void)
8855 {
8856   unsigned i;
8857   static const char *const start_fmt = "Offset %#x";
8858   const char *fmt = start_fmt;
8859
8860   for (i = 0; i < ranges_table_in_use; i++)
8861     {
8862       int block_num = ranges_table[i].num;
8863
8864       if (block_num > 0)
8865         {
8866           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8867           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8868
8869           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8870           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8871
8872           /* If all code is in the text section, then the compilation
8873              unit base address defaults to DW_AT_low_pc, which is the
8874              base of the text section.  */
8875           if (!have_multiple_function_sections)
8876             {
8877               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8878                                     text_section_label,
8879                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8880               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8881                                     text_section_label, NULL);
8882             }
8883
8884           /* Otherwise, the compilation unit base address is zero,
8885              which allows us to use absolute addresses, and not worry
8886              about whether the target supports cross-section
8887              arithmetic.  */
8888           else
8889             {
8890               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8891                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8892               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8893             }
8894
8895           fmt = NULL;
8896         }
8897
8898       /* Negative block_num stands for an index into ranges_by_label.  */
8899       else if (block_num < 0)
8900         {
8901           int lab_idx = - block_num - 1;
8902
8903           if (!have_multiple_function_sections)
8904             {
8905               gcc_unreachable ();
8906 #if 0
8907               /* If we ever use add_ranges_by_labels () for a single
8908                  function section, all we have to do is to take out
8909                  the #if 0 above.  */
8910               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8911                                     ranges_by_label[lab_idx].begin,
8912                                     text_section_label,
8913                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8914               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8915                                     ranges_by_label[lab_idx].end,
8916                                     text_section_label, NULL);
8917 #endif
8918             }
8919           else
8920             {
8921               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8922                                    ranges_by_label[lab_idx].begin,
8923                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8924               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8925                                    ranges_by_label[lab_idx].end,
8926                                    NULL);
8927             }
8928         }
8929       else
8930         {
8931           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8932           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8933           fmt = start_fmt;
8934         }
8935     }
8936 }
8937
8938 /* Data structure containing information about input files.  */
8939 struct file_info
8940 {
8941   const char *path;     /* Complete file name.  */
8942   const char *fname;    /* File name part.  */
8943   int length;           /* Length of entire string.  */
8944   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8945   int dir_idx;          /* Index in directory table.  */
8946 };
8947
8948 /* Data structure containing information about directories with source
8949    files.  */
8950 struct dir_info
8951 {
8952   const char *path;     /* Path including directory name.  */
8953   int length;           /* Path length.  */
8954   int prefix;           /* Index of directory entry which is a prefix.  */
8955   int count;            /* Number of files in this directory.  */
8956   int dir_idx;          /* Index of directory used as base.  */
8957 };
8958
8959 /* Callback function for file_info comparison.  We sort by looking at
8960    the directories in the path.  */
8961
8962 static int
8963 file_info_cmp (const void *p1, const void *p2)
8964 {
8965   const struct file_info *const s1 = (const struct file_info *) p1;
8966   const struct file_info *const s2 = (const struct file_info *) p2;
8967   const unsigned char *cp1;
8968   const unsigned char *cp2;
8969
8970   /* Take care of file names without directories.  We need to make sure that
8971      we return consistent values to qsort since some will get confused if
8972      we return the same value when identical operands are passed in opposite
8973      orders.  So if neither has a directory, return 0 and otherwise return
8974      1 or -1 depending on which one has the directory.  */
8975   if ((s1->path == s1->fname || s2->path == s2->fname))
8976     return (s2->path == s2->fname) - (s1->path == s1->fname);
8977
8978   cp1 = (const unsigned char *) s1->path;
8979   cp2 = (const unsigned char *) s2->path;
8980
8981   while (1)
8982     {
8983       ++cp1;
8984       ++cp2;
8985       /* Reached the end of the first path?  If so, handle like above.  */
8986       if ((cp1 == (const unsigned char *) s1->fname)
8987           || (cp2 == (const unsigned char *) s2->fname))
8988         return ((cp2 == (const unsigned char *) s2->fname)
8989                 - (cp1 == (const unsigned char *) s1->fname));
8990
8991       /* Character of current path component the same?  */
8992       else if (*cp1 != *cp2)
8993         return *cp1 - *cp2;
8994     }
8995 }
8996
8997 struct file_name_acquire_data
8998 {
8999   struct file_info *files;
9000   int used_files;
9001   int max_files;
9002 };
9003
9004 /* Traversal function for the hash table.  */
9005
9006 static int
9007 file_name_acquire (void ** slot, void *data)
9008 {
9009   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9010   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9011   struct file_info *fi;
9012   const char *f;
9013
9014   gcc_assert (fnad->max_files >= d->emitted_number);
9015
9016   if (! d->emitted_number)
9017     return 1;
9018
9019   gcc_assert (fnad->max_files != fnad->used_files);
9020
9021   fi = fnad->files + fnad->used_files++;
9022
9023   /* Skip all leading "./".  */
9024   f = d->filename;
9025   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9026     f += 2;
9027
9028   /* Create a new array entry.  */
9029   fi->path = f;
9030   fi->length = strlen (f);
9031   fi->file_idx = d;
9032
9033   /* Search for the file name part.  */
9034   f = strrchr (f, DIR_SEPARATOR);
9035 #if defined (DIR_SEPARATOR_2)
9036   {
9037     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9038
9039     if (g != NULL)
9040       {
9041         if (f == NULL || f < g)
9042           f = g;
9043       }
9044   }
9045 #endif
9046
9047   fi->fname = f == NULL ? fi->path : f + 1;
9048   return 1;
9049 }
9050
9051 /* Output the directory table and the file name table.  We try to minimize
9052    the total amount of memory needed.  A heuristic is used to avoid large
9053    slowdowns with many input files.  */
9054
9055 static void
9056 output_file_names (void)
9057 {
9058   struct file_name_acquire_data fnad;
9059   int numfiles;
9060   struct file_info *files;
9061   struct dir_info *dirs;
9062   int *saved;
9063   int *savehere;
9064   int *backmap;
9065   int ndirs;
9066   int idx_offset;
9067   int i;
9068
9069   if (!last_emitted_file)
9070     {
9071       dw2_asm_output_data (1, 0, "End directory table");
9072       dw2_asm_output_data (1, 0, "End file name table");
9073       return;
9074     }
9075
9076   numfiles = last_emitted_file->emitted_number;
9077
9078   /* Allocate the various arrays we need.  */
9079   files = XALLOCAVEC (struct file_info, numfiles);
9080   dirs = XALLOCAVEC (struct dir_info, numfiles);
9081
9082   fnad.files = files;
9083   fnad.used_files = 0;
9084   fnad.max_files = numfiles;
9085   htab_traverse (file_table, file_name_acquire, &fnad);
9086   gcc_assert (fnad.used_files == fnad.max_files);
9087
9088   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9089
9090   /* Find all the different directories used.  */
9091   dirs[0].path = files[0].path;
9092   dirs[0].length = files[0].fname - files[0].path;
9093   dirs[0].prefix = -1;
9094   dirs[0].count = 1;
9095   dirs[0].dir_idx = 0;
9096   files[0].dir_idx = 0;
9097   ndirs = 1;
9098
9099   for (i = 1; i < numfiles; i++)
9100     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9101         && memcmp (dirs[ndirs - 1].path, files[i].path,
9102                    dirs[ndirs - 1].length) == 0)
9103       {
9104         /* Same directory as last entry.  */
9105         files[i].dir_idx = ndirs - 1;
9106         ++dirs[ndirs - 1].count;
9107       }
9108     else
9109       {
9110         int j;
9111
9112         /* This is a new directory.  */
9113         dirs[ndirs].path = files[i].path;
9114         dirs[ndirs].length = files[i].fname - files[i].path;
9115         dirs[ndirs].count = 1;
9116         dirs[ndirs].dir_idx = ndirs;
9117         files[i].dir_idx = ndirs;
9118
9119         /* Search for a prefix.  */
9120         dirs[ndirs].prefix = -1;
9121         for (j = 0; j < ndirs; j++)
9122           if (dirs[j].length < dirs[ndirs].length
9123               && dirs[j].length > 1
9124               && (dirs[ndirs].prefix == -1
9125                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9126               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9127             dirs[ndirs].prefix = j;
9128
9129         ++ndirs;
9130       }
9131
9132   /* Now to the actual work.  We have to find a subset of the directories which
9133      allow expressing the file name using references to the directory table
9134      with the least amount of characters.  We do not do an exhaustive search
9135      where we would have to check out every combination of every single
9136      possible prefix.  Instead we use a heuristic which provides nearly optimal
9137      results in most cases and never is much off.  */
9138   saved = XALLOCAVEC (int, ndirs);
9139   savehere = XALLOCAVEC (int, ndirs);
9140
9141   memset (saved, '\0', ndirs * sizeof (saved[0]));
9142   for (i = 0; i < ndirs; i++)
9143     {
9144       int j;
9145       int total;
9146
9147       /* We can always save some space for the current directory.  But this
9148          does not mean it will be enough to justify adding the directory.  */
9149       savehere[i] = dirs[i].length;
9150       total = (savehere[i] - saved[i]) * dirs[i].count;
9151
9152       for (j = i + 1; j < ndirs; j++)
9153         {
9154           savehere[j] = 0;
9155           if (saved[j] < dirs[i].length)
9156             {
9157               /* Determine whether the dirs[i] path is a prefix of the
9158                  dirs[j] path.  */
9159               int k;
9160
9161               k = dirs[j].prefix;
9162               while (k != -1 && k != (int) i)
9163                 k = dirs[k].prefix;
9164
9165               if (k == (int) i)
9166                 {
9167                   /* Yes it is.  We can possibly save some memory by
9168                      writing the filenames in dirs[j] relative to
9169                      dirs[i].  */
9170                   savehere[j] = dirs[i].length;
9171                   total += (savehere[j] - saved[j]) * dirs[j].count;
9172                 }
9173             }
9174         }
9175
9176       /* Check whether we can save enough to justify adding the dirs[i]
9177          directory.  */
9178       if (total > dirs[i].length + 1)
9179         {
9180           /* It's worthwhile adding.  */
9181           for (j = i; j < ndirs; j++)
9182             if (savehere[j] > 0)
9183               {
9184                 /* Remember how much we saved for this directory so far.  */
9185                 saved[j] = savehere[j];
9186
9187                 /* Remember the prefix directory.  */
9188                 dirs[j].dir_idx = i;
9189               }
9190         }
9191     }
9192
9193   /* Emit the directory name table.  */
9194   idx_offset = dirs[0].length > 0 ? 1 : 0;
9195   for (i = 1 - idx_offset; i < ndirs; i++)
9196     dw2_asm_output_nstring (dirs[i].path,
9197                             dirs[i].length
9198                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9199                             "Directory Entry: %#x", i + idx_offset);
9200
9201   dw2_asm_output_data (1, 0, "End directory table");
9202
9203   /* We have to emit them in the order of emitted_number since that's
9204      used in the debug info generation.  To do this efficiently we
9205      generate a back-mapping of the indices first.  */
9206   backmap = XALLOCAVEC (int, numfiles);
9207   for (i = 0; i < numfiles; i++)
9208     backmap[files[i].file_idx->emitted_number - 1] = i;
9209
9210   /* Now write all the file names.  */
9211   for (i = 0; i < numfiles; i++)
9212     {
9213       int file_idx = backmap[i];
9214       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9215
9216 #ifdef VMS_DEBUGGING_INFO
9217 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9218
9219       /* Setting these fields can lead to debugger miscomparisons,
9220          but VMS Debug requires them to be set correctly.  */
9221
9222       int ver;
9223       long long cdt;
9224       long siz;
9225       int maxfilelen = strlen (files[file_idx].path)
9226                                + dirs[dir_idx].length
9227                                + MAX_VMS_VERSION_LEN + 1;
9228       char *filebuf = XALLOCAVEC (char, maxfilelen);
9229
9230       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9231       snprintf (filebuf, maxfilelen, "%s;%d",
9232                 files[file_idx].path + dirs[dir_idx].length, ver);
9233
9234       dw2_asm_output_nstring
9235         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9236
9237       /* Include directory index.  */
9238       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9239
9240       /* Modification time.  */
9241       dw2_asm_output_data_uleb128
9242         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9243           ? cdt : 0,
9244          NULL);
9245
9246       /* File length in bytes.  */
9247       dw2_asm_output_data_uleb128
9248         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9249           ? siz : 0,
9250          NULL);
9251 #else
9252       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9253                               "File Entry: %#x", (unsigned) i + 1);
9254
9255       /* Include directory index.  */
9256       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9257
9258       /* Modification time.  */
9259       dw2_asm_output_data_uleb128 (0, NULL);
9260
9261       /* File length in bytes.  */
9262       dw2_asm_output_data_uleb128 (0, NULL);
9263 #endif /* VMS_DEBUGGING_INFO */
9264     }
9265
9266   dw2_asm_output_data (1, 0, "End file name table");
9267 }
9268
9269
9270 /* Output one line number table into the .debug_line section.  */
9271
9272 static void
9273 output_one_line_info_table (dw_line_info_table *table)
9274 {
9275   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9276   unsigned int current_line = 1;
9277   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9278   dw_line_info_entry *ent;
9279   size_t i;
9280
9281   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9282     {
9283       switch (ent->opcode)
9284         {
9285         case LI_set_address:
9286           /* ??? Unfortunately, we have little choice here currently, and
9287              must always use the most general form.  GCC does not know the
9288              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9289              ports do have length attributes which will give an upper bound
9290              on the address range.  We could perhaps use length attributes
9291              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9292           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9293
9294           /* This can handle any delta.  This takes
9295              4+DWARF2_ADDR_SIZE bytes.  */
9296           dw2_asm_output_data (1, 0, "set address %s", line_label);
9297           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9298           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9299           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9300           break;
9301
9302         case LI_set_line:
9303           if (ent->val == current_line)
9304             {
9305               /* We still need to start a new row, so output a copy insn.  */
9306               dw2_asm_output_data (1, DW_LNS_copy,
9307                                    "copy line %u", current_line);
9308             }
9309           else
9310             {
9311               int line_offset = ent->val - current_line;
9312               int line_delta = line_offset - DWARF_LINE_BASE;
9313
9314               current_line = ent->val;
9315               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9316                 {
9317                   /* This can handle deltas from -10 to 234, using the current
9318                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9319                      This takes 1 byte.  */
9320                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9321                                        "line %u", current_line);
9322                 }
9323               else
9324                 {
9325                   /* This can handle any delta.  This takes at least 4 bytes,
9326                      depending on the value being encoded.  */
9327                   dw2_asm_output_data (1, DW_LNS_advance_line,
9328                                        "advance to line %u", current_line);
9329                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9330                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9331                 }
9332             }
9333           break;
9334
9335         case LI_set_file:
9336           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9337           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9338           break;
9339
9340         case LI_set_column:
9341           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9342           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9343           break;
9344
9345         case LI_negate_stmt:
9346           current_is_stmt = !current_is_stmt;
9347           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9348                                "is_stmt %d", current_is_stmt);
9349           break;
9350
9351         case LI_set_prologue_end:
9352           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9353                                "set prologue end");
9354           break;
9355           
9356         case LI_set_epilogue_begin:
9357           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9358                                "set epilogue begin");
9359           break;
9360
9361         case LI_set_discriminator:
9362           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9363           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9364           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9365           dw2_asm_output_data_uleb128 (ent->val, NULL);
9366           break;
9367         }
9368     }
9369
9370   /* Emit debug info for the address of the end of the table.  */
9371   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9372   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9373   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9374   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9375
9376   dw2_asm_output_data (1, 0, "end sequence");
9377   dw2_asm_output_data_uleb128 (1, NULL);
9378   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9379 }
9380
9381 /* Output the source line number correspondence information.  This
9382    information goes into the .debug_line section.  */
9383
9384 static void
9385 output_line_info (void)
9386 {
9387   char l1[20], l2[20], p1[20], p2[20];
9388   int ver = dwarf_version;
9389   bool saw_one = false;
9390   int opc;
9391
9392   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9393   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9394   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9395   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9396
9397   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9398     dw2_asm_output_data (4, 0xffffffff,
9399       "Initial length escape value indicating 64-bit DWARF extension");
9400   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9401                         "Length of Source Line Info");
9402   ASM_OUTPUT_LABEL (asm_out_file, l1);
9403
9404   dw2_asm_output_data (2, ver, "DWARF Version");
9405   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9406   ASM_OUTPUT_LABEL (asm_out_file, p1);
9407
9408   /* Define the architecture-dependent minimum instruction length (in bytes).
9409      In this implementation of DWARF, this field is used for information
9410      purposes only.  Since GCC generates assembly language, we have no
9411      a priori knowledge of how many instruction bytes are generated for each
9412      source line, and therefore can use only the DW_LNE_set_address and
9413      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9414      this as '1', which is "correct enough" for all architectures,
9415      and don't let the target override.  */
9416   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9417
9418   if (ver >= 4)
9419     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9420                          "Maximum Operations Per Instruction");
9421   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9422                        "Default is_stmt_start flag");
9423   dw2_asm_output_data (1, DWARF_LINE_BASE,
9424                        "Line Base Value (Special Opcodes)");
9425   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9426                        "Line Range Value (Special Opcodes)");
9427   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9428                        "Special Opcode Base");
9429
9430   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9431     {
9432       int n_op_args;
9433       switch (opc)
9434         {
9435         case DW_LNS_advance_pc:
9436         case DW_LNS_advance_line:
9437         case DW_LNS_set_file:
9438         case DW_LNS_set_column:
9439         case DW_LNS_fixed_advance_pc:
9440         case DW_LNS_set_isa:
9441           n_op_args = 1;
9442           break;
9443         default:
9444           n_op_args = 0;
9445           break;
9446         }
9447
9448       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9449                            opc, n_op_args);
9450     }
9451
9452   /* Write out the information about the files we use.  */
9453   output_file_names ();
9454   ASM_OUTPUT_LABEL (asm_out_file, p2);
9455
9456   if (separate_line_info)
9457     {
9458       dw_line_info_table *table;
9459       size_t i;
9460
9461       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9462         if (table->in_use)
9463           {
9464             output_one_line_info_table (table);
9465             saw_one = true;
9466           }
9467     }
9468   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9469     {
9470       output_one_line_info_table (cold_text_section_line_info);
9471       saw_one = true;
9472     }
9473
9474   /* ??? Some Darwin linkers crash on a .debug_line section with no
9475      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9476      sufficient -- the address column must also be initialized.
9477      Make sure to output at least one set_address/end_sequence pair,
9478      choosing .text since that section is always present.  */
9479   if (text_section_line_info->in_use || !saw_one)
9480     output_one_line_info_table (text_section_line_info);
9481
9482   /* Output the marker for the end of the line number info.  */
9483   ASM_OUTPUT_LABEL (asm_out_file, l2);
9484 }
9485 \f
9486 /* Given a pointer to a tree node for some base type, return a pointer to
9487    a DIE that describes the given type.
9488
9489    This routine must only be called for GCC type nodes that correspond to
9490    Dwarf base (fundamental) types.  */
9491
9492 static dw_die_ref
9493 base_type_die (tree type)
9494 {
9495   dw_die_ref base_type_result;
9496   enum dwarf_type encoding;
9497
9498   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9499     return 0;
9500
9501   /* If this is a subtype that should not be emitted as a subrange type,
9502      use the base type.  See subrange_type_for_debug_p.  */
9503   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9504     type = TREE_TYPE (type);
9505
9506   switch (TREE_CODE (type))
9507     {
9508     case INTEGER_TYPE:
9509       if ((dwarf_version >= 4 || !dwarf_strict)
9510           && TYPE_NAME (type)
9511           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9512           && DECL_IS_BUILTIN (TYPE_NAME (type))
9513           && DECL_NAME (TYPE_NAME (type)))
9514         {
9515           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9516           if (strcmp (name, "char16_t") == 0
9517               || strcmp (name, "char32_t") == 0)
9518             {
9519               encoding = DW_ATE_UTF;
9520               break;
9521             }
9522         }
9523       if (TYPE_STRING_FLAG (type))
9524         {
9525           if (TYPE_UNSIGNED (type))
9526             encoding = DW_ATE_unsigned_char;
9527           else
9528             encoding = DW_ATE_signed_char;
9529         }
9530       else if (TYPE_UNSIGNED (type))
9531         encoding = DW_ATE_unsigned;
9532       else
9533         encoding = DW_ATE_signed;
9534       break;
9535
9536     case REAL_TYPE:
9537       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9538         {
9539           if (dwarf_version >= 3 || !dwarf_strict)
9540             encoding = DW_ATE_decimal_float;
9541           else
9542             encoding = DW_ATE_lo_user;
9543         }
9544       else
9545         encoding = DW_ATE_float;
9546       break;
9547
9548     case FIXED_POINT_TYPE:
9549       if (!(dwarf_version >= 3 || !dwarf_strict))
9550         encoding = DW_ATE_lo_user;
9551       else if (TYPE_UNSIGNED (type))
9552         encoding = DW_ATE_unsigned_fixed;
9553       else
9554         encoding = DW_ATE_signed_fixed;
9555       break;
9556
9557       /* Dwarf2 doesn't know anything about complex ints, so use
9558          a user defined type for it.  */
9559     case COMPLEX_TYPE:
9560       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9561         encoding = DW_ATE_complex_float;
9562       else
9563         encoding = DW_ATE_lo_user;
9564       break;
9565
9566     case BOOLEAN_TYPE:
9567       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9568       encoding = DW_ATE_boolean;
9569       break;
9570
9571     default:
9572       /* No other TREE_CODEs are Dwarf fundamental types.  */
9573       gcc_unreachable ();
9574     }
9575
9576   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9577
9578   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9579                    int_size_in_bytes (type));
9580   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9581
9582   return base_type_result;
9583 }
9584
9585 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9586    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9587
9588 static inline int
9589 is_base_type (tree type)
9590 {
9591   switch (TREE_CODE (type))
9592     {
9593     case ERROR_MARK:
9594     case VOID_TYPE:
9595     case INTEGER_TYPE:
9596     case REAL_TYPE:
9597     case FIXED_POINT_TYPE:
9598     case COMPLEX_TYPE:
9599     case BOOLEAN_TYPE:
9600       return 1;
9601
9602     case ARRAY_TYPE:
9603     case RECORD_TYPE:
9604     case UNION_TYPE:
9605     case QUAL_UNION_TYPE:
9606     case ENUMERAL_TYPE:
9607     case FUNCTION_TYPE:
9608     case METHOD_TYPE:
9609     case POINTER_TYPE:
9610     case REFERENCE_TYPE:
9611     case NULLPTR_TYPE:
9612     case OFFSET_TYPE:
9613     case LANG_TYPE:
9614     case VECTOR_TYPE:
9615       return 0;
9616
9617     default:
9618       gcc_unreachable ();
9619     }
9620
9621   return 0;
9622 }
9623
9624 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9625    node, return the size in bits for the type if it is a constant, or else
9626    return the alignment for the type if the type's size is not constant, or
9627    else return BITS_PER_WORD if the type actually turns out to be an
9628    ERROR_MARK node.  */
9629
9630 static inline unsigned HOST_WIDE_INT
9631 simple_type_size_in_bits (const_tree type)
9632 {
9633   if (TREE_CODE (type) == ERROR_MARK)
9634     return BITS_PER_WORD;
9635   else if (TYPE_SIZE (type) == NULL_TREE)
9636     return 0;
9637   else if (host_integerp (TYPE_SIZE (type), 1))
9638     return tree_low_cst (TYPE_SIZE (type), 1);
9639   else
9640     return TYPE_ALIGN (type);
9641 }
9642
9643 /* Similarly, but return a double_int instead of UHWI.  */
9644
9645 static inline double_int
9646 double_int_type_size_in_bits (const_tree type)
9647 {
9648   if (TREE_CODE (type) == ERROR_MARK)
9649     return uhwi_to_double_int (BITS_PER_WORD);
9650   else if (TYPE_SIZE (type) == NULL_TREE)
9651     return double_int_zero;
9652   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9653     return tree_to_double_int (TYPE_SIZE (type));
9654   else
9655     return uhwi_to_double_int (TYPE_ALIGN (type));
9656 }
9657
9658 /*  Given a pointer to a tree node for a subrange type, return a pointer
9659     to a DIE that describes the given type.  */
9660
9661 static dw_die_ref
9662 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9663 {
9664   dw_die_ref subrange_die;
9665   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9666
9667   if (context_die == NULL)
9668     context_die = comp_unit_die ();
9669
9670   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9671
9672   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9673     {
9674       /* The size of the subrange type and its base type do not match,
9675          so we need to generate a size attribute for the subrange type.  */
9676       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9677     }
9678
9679   if (low)
9680     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9681   if (high)
9682     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9683
9684   return subrange_die;
9685 }
9686
9687 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9688    entry that chains various modifiers in front of the given type.  */
9689
9690 static dw_die_ref
9691 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9692                    dw_die_ref context_die)
9693 {
9694   enum tree_code code = TREE_CODE (type);
9695   dw_die_ref mod_type_die;
9696   dw_die_ref sub_die = NULL;
9697   tree item_type = NULL;
9698   tree qualified_type;
9699   tree name, low, high;
9700
9701   if (code == ERROR_MARK)
9702     return NULL;
9703
9704   /* See if we already have the appropriately qualified variant of
9705      this type.  */
9706   qualified_type
9707     = get_qualified_type (type,
9708                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9709                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9710
9711   if (qualified_type == sizetype
9712       && TYPE_NAME (qualified_type)
9713       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9714     {
9715       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9716
9717       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9718                            && TYPE_PRECISION (t)
9719                            == TYPE_PRECISION (qualified_type)
9720                            && TYPE_UNSIGNED (t)
9721                            == TYPE_UNSIGNED (qualified_type));
9722       qualified_type = t;
9723     }
9724
9725   /* If we do, then we can just use its DIE, if it exists.  */
9726   if (qualified_type)
9727     {
9728       mod_type_die = lookup_type_die (qualified_type);
9729       if (mod_type_die)
9730         return mod_type_die;
9731     }
9732
9733   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9734
9735   /* Handle C typedef types.  */
9736   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9737       && !DECL_ARTIFICIAL (name))
9738     {
9739       tree dtype = TREE_TYPE (name);
9740
9741       if (qualified_type == dtype)
9742         {
9743           /* For a named type, use the typedef.  */
9744           gen_type_die (qualified_type, context_die);
9745           return lookup_type_die (qualified_type);
9746         }
9747       else if (is_const_type < TYPE_READONLY (dtype)
9748                || is_volatile_type < TYPE_VOLATILE (dtype)
9749                || (is_const_type <= TYPE_READONLY (dtype)
9750                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9751                    && DECL_ORIGINAL_TYPE (name) != type))
9752         /* cv-unqualified version of named type.  Just use the unnamed
9753            type to which it refers.  */
9754         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9755                                   is_const_type, is_volatile_type,
9756                                   context_die);
9757       /* Else cv-qualified version of named type; fall through.  */
9758     }
9759
9760   if (is_const_type
9761       /* If both is_const_type and is_volatile_type, prefer the path
9762          which leads to a qualified type.  */
9763       && (!is_volatile_type
9764           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9765           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9766     {
9767       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9768       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9769     }
9770   else if (is_volatile_type)
9771     {
9772       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9773       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9774     }
9775   else if (code == POINTER_TYPE)
9776     {
9777       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9778       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9779                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9780       item_type = TREE_TYPE (type);
9781       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9782         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9783                          TYPE_ADDR_SPACE (item_type));
9784     }
9785   else if (code == REFERENCE_TYPE)
9786     {
9787       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9788         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9789                                 type);
9790       else
9791         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9792       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9793                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9794       item_type = TREE_TYPE (type);
9795       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9796         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9797                          TYPE_ADDR_SPACE (item_type));
9798     }
9799   else if (code == INTEGER_TYPE
9800            && TREE_TYPE (type) != NULL_TREE
9801            && subrange_type_for_debug_p (type, &low, &high))
9802     {
9803       mod_type_die = subrange_type_die (type, low, high, context_die);
9804       item_type = TREE_TYPE (type);
9805     }
9806   else if (is_base_type (type))
9807     mod_type_die = base_type_die (type);
9808   else
9809     {
9810       gen_type_die (type, context_die);
9811
9812       /* We have to get the type_main_variant here (and pass that to the
9813          `lookup_type_die' routine) because the ..._TYPE node we have
9814          might simply be a *copy* of some original type node (where the
9815          copy was created to help us keep track of typedef names) and
9816          that copy might have a different TYPE_UID from the original
9817          ..._TYPE node.  */
9818       if (TREE_CODE (type) != VECTOR_TYPE)
9819         return lookup_type_die (type_main_variant (type));
9820       else
9821         /* Vectors have the debugging information in the type,
9822            not the main variant.  */
9823         return lookup_type_die (type);
9824     }
9825
9826   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9827      don't output a DW_TAG_typedef, since there isn't one in the
9828      user's program; just attach a DW_AT_name to the type.
9829      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9830      if the base type already has the same name.  */
9831   if (name
9832       && ((TREE_CODE (name) != TYPE_DECL
9833            && (qualified_type == TYPE_MAIN_VARIANT (type)
9834                || (!is_const_type && !is_volatile_type)))
9835           || (TREE_CODE (name) == TYPE_DECL
9836               && TREE_TYPE (name) == qualified_type
9837               && DECL_NAME (name))))
9838     {
9839       if (TREE_CODE (name) == TYPE_DECL)
9840         /* Could just call add_name_and_src_coords_attributes here,
9841            but since this is a builtin type it doesn't have any
9842            useful source coordinates anyway.  */
9843         name = DECL_NAME (name);
9844       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9845       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9846       if (TYPE_ARTIFICIAL (type))
9847         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9848     }
9849   /* This probably indicates a bug.  */
9850   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9851     add_name_attribute (mod_type_die, "__unknown__");
9852
9853   if (qualified_type)
9854     equate_type_number_to_die (qualified_type, mod_type_die);
9855
9856   if (item_type)
9857     /* We must do this after the equate_type_number_to_die call, in case
9858        this is a recursive type.  This ensures that the modified_type_die
9859        recursion will terminate even if the type is recursive.  Recursive
9860        types are possible in Ada.  */
9861     sub_die = modified_type_die (item_type,
9862                                  TYPE_READONLY (item_type),
9863                                  TYPE_VOLATILE (item_type),
9864                                  context_die);
9865
9866   if (sub_die != NULL)
9867     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9868
9869   return mod_type_die;
9870 }
9871
9872 /* Generate DIEs for the generic parameters of T.
9873    T must be either a generic type or a generic function.
9874    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9875
9876 static void
9877 gen_generic_params_dies (tree t)
9878 {
9879   tree parms, args;
9880   int parms_num, i;
9881   dw_die_ref die = NULL;
9882
9883   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9884     return;
9885
9886   if (TYPE_P (t))
9887     die = lookup_type_die (t);
9888   else if (DECL_P (t))
9889     die = lookup_decl_die (t);
9890
9891   gcc_assert (die);
9892
9893   parms = lang_hooks.get_innermost_generic_parms (t);
9894   if (!parms)
9895     /* T has no generic parameter. It means T is neither a generic type
9896        or function. End of story.  */
9897     return;
9898
9899   parms_num = TREE_VEC_LENGTH (parms);
9900   args = lang_hooks.get_innermost_generic_args (t);
9901   for (i = 0; i < parms_num; i++)
9902     {
9903       tree parm, arg, arg_pack_elems;
9904
9905       parm = TREE_VEC_ELT (parms, i);
9906       arg = TREE_VEC_ELT (args, i);
9907       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9908       gcc_assert (parm && TREE_VALUE (parm) && arg);
9909
9910       if (parm && TREE_VALUE (parm) && arg)
9911         {
9912           /* If PARM represents a template parameter pack,
9913              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9914              by DW_TAG_template_*_parameter DIEs for the argument
9915              pack elements of ARG. Note that ARG would then be
9916              an argument pack.  */
9917           if (arg_pack_elems)
9918             template_parameter_pack_die (TREE_VALUE (parm),
9919                                          arg_pack_elems,
9920                                          die);
9921           else
9922             generic_parameter_die (TREE_VALUE (parm), arg,
9923                                    true /* Emit DW_AT_name */, die);
9924         }
9925     }
9926 }
9927
9928 /* Create and return a DIE for PARM which should be
9929    the representation of a generic type parameter.
9930    For instance, in the C++ front end, PARM would be a template parameter.
9931    ARG is the argument to PARM.
9932    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9933    name of the PARM.
9934    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9935    as a child node.  */
9936
9937 static dw_die_ref
9938 generic_parameter_die (tree parm, tree arg,
9939                        bool emit_name_p,
9940                        dw_die_ref parent_die)
9941 {
9942   dw_die_ref tmpl_die = NULL;
9943   const char *name = NULL;
9944
9945   if (!parm || !DECL_NAME (parm) || !arg)
9946     return NULL;
9947
9948   /* We support non-type generic parameters and arguments,
9949      type generic parameters and arguments, as well as
9950      generic generic parameters (a.k.a. template template parameters in C++)
9951      and arguments.  */
9952   if (TREE_CODE (parm) == PARM_DECL)
9953     /* PARM is a nontype generic parameter  */
9954     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9955   else if (TREE_CODE (parm) == TYPE_DECL)
9956     /* PARM is a type generic parameter.  */
9957     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9958   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9959     /* PARM is a generic generic parameter.
9960        Its DIE is a GNU extension. It shall have a
9961        DW_AT_name attribute to represent the name of the template template
9962        parameter, and a DW_AT_GNU_template_name attribute to represent the
9963        name of the template template argument.  */
9964     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9965                         parent_die, parm);
9966   else
9967     gcc_unreachable ();
9968
9969   if (tmpl_die)
9970     {
9971       tree tmpl_type;
9972
9973       /* If PARM is a generic parameter pack, it means we are
9974          emitting debug info for a template argument pack element.
9975          In other terms, ARG is a template argument pack element.
9976          In that case, we don't emit any DW_AT_name attribute for
9977          the die.  */
9978       if (emit_name_p)
9979         {
9980           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9981           gcc_assert (name);
9982           add_AT_string (tmpl_die, DW_AT_name, name);
9983         }
9984
9985       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9986         {
9987           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9988              TMPL_DIE should have a child DW_AT_type attribute that is set
9989              to the type of the argument to PARM, which is ARG.
9990              If PARM is a type generic parameter, TMPL_DIE should have a
9991              child DW_AT_type that is set to ARG.  */
9992           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9993           add_type_attribute (tmpl_die, tmpl_type, 0,
9994                               TREE_THIS_VOLATILE (tmpl_type),
9995                               parent_die);
9996         }
9997       else
9998         {
9999           /* So TMPL_DIE is a DIE representing a
10000              a generic generic template parameter, a.k.a template template
10001              parameter in C++ and arg is a template.  */
10002
10003           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10004              to the name of the argument.  */
10005           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10006           if (name)
10007             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10008         }
10009
10010       if (TREE_CODE (parm) == PARM_DECL)
10011         /* So PARM is a non-type generic parameter.
10012            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10013            attribute of TMPL_DIE which value represents the value
10014            of ARG.
10015            We must be careful here:
10016            The value of ARG might reference some function decls.
10017            We might currently be emitting debug info for a generic
10018            type and types are emitted before function decls, we don't
10019            know if the function decls referenced by ARG will actually be
10020            emitted after cgraph computations.
10021            So must defer the generation of the DW_AT_const_value to
10022            after cgraph is ready.  */
10023         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10024     }
10025
10026   return tmpl_die;
10027 }
10028
10029 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10030    PARM_PACK must be a template parameter pack. The returned DIE
10031    will be child DIE of PARENT_DIE.  */
10032
10033 static dw_die_ref
10034 template_parameter_pack_die (tree parm_pack,
10035                              tree parm_pack_args,
10036                              dw_die_ref parent_die)
10037 {
10038   dw_die_ref die;
10039   int j;
10040
10041   gcc_assert (parent_die && parm_pack);
10042
10043   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10044   add_name_and_src_coords_attributes (die, parm_pack);
10045   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10046     generic_parameter_die (parm_pack,
10047                            TREE_VEC_ELT (parm_pack_args, j),
10048                            false /* Don't emit DW_AT_name */,
10049                            die);
10050   return die;
10051 }
10052
10053 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10054    an enumerated type.  */
10055
10056 static inline int
10057 type_is_enum (const_tree type)
10058 {
10059   return TREE_CODE (type) == ENUMERAL_TYPE;
10060 }
10061
10062 /* Return the DBX register number described by a given RTL node.  */
10063
10064 static unsigned int
10065 dbx_reg_number (const_rtx rtl)
10066 {
10067   unsigned regno = REGNO (rtl);
10068
10069   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10070
10071 #ifdef LEAF_REG_REMAP
10072   if (current_function_uses_only_leaf_regs)
10073     {
10074       int leaf_reg = LEAF_REG_REMAP (regno);
10075       if (leaf_reg != -1)
10076         regno = (unsigned) leaf_reg;
10077     }
10078 #endif
10079
10080   return DBX_REGISTER_NUMBER (regno);
10081 }
10082
10083 /* Optionally add a DW_OP_piece term to a location description expression.
10084    DW_OP_piece is only added if the location description expression already
10085    doesn't end with DW_OP_piece.  */
10086
10087 static void
10088 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10089 {
10090   dw_loc_descr_ref loc;
10091
10092   if (*list_head != NULL)
10093     {
10094       /* Find the end of the chain.  */
10095       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10096         ;
10097
10098       if (loc->dw_loc_opc != DW_OP_piece)
10099         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10100     }
10101 }
10102
10103 /* Return a location descriptor that designates a machine register or
10104    zero if there is none.  */
10105
10106 static dw_loc_descr_ref
10107 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10108 {
10109   rtx regs;
10110
10111   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10112     return 0;
10113
10114   /* We only use "frame base" when we're sure we're talking about the
10115      post-prologue local stack frame.  We do this by *not* running
10116      register elimination until this point, and recognizing the special
10117      argument pointer and soft frame pointer rtx's.
10118      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10119   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10120       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10121     {
10122       dw_loc_descr_ref result = NULL;
10123
10124       if (dwarf_version >= 4 || !dwarf_strict)
10125         {
10126           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10127                                        initialized);
10128           if (result)
10129             add_loc_descr (&result,
10130                            new_loc_descr (DW_OP_stack_value, 0, 0));
10131         }
10132       return result;
10133     }
10134
10135   regs = targetm.dwarf_register_span (rtl);
10136
10137   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10138     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10139   else
10140     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10141 }
10142
10143 /* Return a location descriptor that designates a machine register for
10144    a given hard register number.  */
10145
10146 static dw_loc_descr_ref
10147 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10148 {
10149   dw_loc_descr_ref reg_loc_descr;
10150
10151   if (regno <= 31)
10152     reg_loc_descr
10153       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10154   else
10155     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10156
10157   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10158     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10159
10160   return reg_loc_descr;
10161 }
10162
10163 /* Given an RTL of a register, return a location descriptor that
10164    designates a value that spans more than one register.  */
10165
10166 static dw_loc_descr_ref
10167 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10168                              enum var_init_status initialized)
10169 {
10170   int nregs, size, i;
10171   unsigned reg;
10172   dw_loc_descr_ref loc_result = NULL;
10173
10174   reg = REGNO (rtl);
10175 #ifdef LEAF_REG_REMAP
10176   if (current_function_uses_only_leaf_regs)
10177     {
10178       int leaf_reg = LEAF_REG_REMAP (reg);
10179       if (leaf_reg != -1)
10180         reg = (unsigned) leaf_reg;
10181     }
10182 #endif
10183   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10184   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10185
10186   /* Simple, contiguous registers.  */
10187   if (regs == NULL_RTX)
10188     {
10189       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10190
10191       loc_result = NULL;
10192       while (nregs--)
10193         {
10194           dw_loc_descr_ref t;
10195
10196           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10197                                       VAR_INIT_STATUS_INITIALIZED);
10198           add_loc_descr (&loc_result, t);
10199           add_loc_descr_op_piece (&loc_result, size);
10200           ++reg;
10201         }
10202       return loc_result;
10203     }
10204
10205   /* Now onto stupid register sets in non contiguous locations.  */
10206
10207   gcc_assert (GET_CODE (regs) == PARALLEL);
10208
10209   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10210   loc_result = NULL;
10211
10212   for (i = 0; i < XVECLEN (regs, 0); ++i)
10213     {
10214       dw_loc_descr_ref t;
10215
10216       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10217                                   VAR_INIT_STATUS_INITIALIZED);
10218       add_loc_descr (&loc_result, t);
10219       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10220       add_loc_descr_op_piece (&loc_result, size);
10221     }
10222
10223   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10224     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10225   return loc_result;
10226 }
10227
10228 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10229
10230 /* Return a location descriptor that designates a constant i,
10231    as a compound operation from constant (i >> shift), constant shift
10232    and DW_OP_shl.  */
10233
10234 static dw_loc_descr_ref
10235 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10236 {
10237   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10238   add_loc_descr (&ret, int_loc_descriptor (shift));
10239   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10240   return ret;
10241 }
10242
10243 /* Return a location descriptor that designates a constant.  */
10244
10245 static dw_loc_descr_ref
10246 int_loc_descriptor (HOST_WIDE_INT i)
10247 {
10248   enum dwarf_location_atom op;
10249
10250   /* Pick the smallest representation of a constant, rather than just
10251      defaulting to the LEB encoding.  */
10252   if (i >= 0)
10253     {
10254       int clz = clz_hwi (i);
10255       int ctz = ctz_hwi (i);
10256       if (i <= 31)
10257         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10258       else if (i <= 0xff)
10259         op = DW_OP_const1u;
10260       else if (i <= 0xffff)
10261         op = DW_OP_const2u;
10262       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10263                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10264         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10265            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10266            while DW_OP_const4u is 5 bytes.  */
10267         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10268       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10269                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10270         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10271            while DW_OP_const4u is 5 bytes.  */
10272         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10273       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10274         op = DW_OP_const4u;
10275       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10276                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10277         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10278            while DW_OP_constu of constant >= 0x100000000 takes at least
10279            6 bytes.  */
10280         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10281       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10282                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10283                   >= HOST_BITS_PER_WIDE_INT)
10284         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10285            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10286            while DW_OP_constu takes in this case at least 6 bytes.  */
10287         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10288       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10289                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10290                && size_of_uleb128 (i) > 6)
10291         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10292         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10293       else
10294         op = DW_OP_constu;
10295     }
10296   else
10297     {
10298       if (i >= -0x80)
10299         op = DW_OP_const1s;
10300       else if (i >= -0x8000)
10301         op = DW_OP_const2s;
10302       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10303         {
10304           if (size_of_int_loc_descriptor (i) < 5)
10305             {
10306               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10307               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10308               return ret;
10309             }
10310           op = DW_OP_const4s;
10311         }
10312       else
10313         {
10314           if (size_of_int_loc_descriptor (i)
10315               < (unsigned long) 1 + size_of_sleb128 (i))
10316             {
10317               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10318               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10319               return ret;
10320             }
10321           op = DW_OP_consts;
10322         }
10323     }
10324
10325   return new_loc_descr (op, i, 0);
10326 }
10327
10328 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10329    without actually allocating it.  */
10330
10331 static unsigned long
10332 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10333 {
10334   return size_of_int_loc_descriptor (i >> shift)
10335          + size_of_int_loc_descriptor (shift)
10336          + 1;
10337 }
10338
10339 /* Return size_of_locs (int_loc_descriptor (i)) without
10340    actually allocating it.  */
10341
10342 static unsigned long
10343 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10344 {
10345   unsigned long s;
10346
10347   if (i >= 0)
10348     {
10349       int clz, ctz;
10350       if (i <= 31)
10351         return 1;
10352       else if (i <= 0xff)
10353         return 2;
10354       else if (i <= 0xffff)
10355         return 3;
10356       clz = clz_hwi (i);
10357       ctz = ctz_hwi (i);
10358       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10359           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10360         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10361                                                     - clz - 5);
10362       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10363                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10364         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10365                                                     - clz - 8);
10366       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10367         return 5;
10368       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10369       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10370           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10371         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10372                                                     - clz - 8);
10373       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10374                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10375         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10376                                                     - clz - 16);
10377       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10378                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10379                && s > 6)
10380         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10381                                                     - clz - 32);
10382       else
10383         return 1 + s;
10384     }
10385   else
10386     {
10387       if (i >= -0x80)
10388         return 2;
10389       else if (i >= -0x8000)
10390         return 3;
10391       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10392         {
10393           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10394             {
10395               s = size_of_int_loc_descriptor (-i) + 1;
10396               if (s < 5)
10397                 return s;
10398             }
10399           return 5;
10400         }
10401       else
10402         {
10403           unsigned long r = 1 + size_of_sleb128 (i);
10404           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10405             {
10406               s = size_of_int_loc_descriptor (-i) + 1;
10407               if (s < r)
10408                 return s;
10409             }
10410           return r;
10411         }
10412     }
10413 }
10414
10415 /* Return loc description representing "address" of integer value.
10416    This can appear only as toplevel expression.  */
10417
10418 static dw_loc_descr_ref
10419 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10420 {
10421   int litsize;
10422   dw_loc_descr_ref loc_result = NULL;
10423
10424   if (!(dwarf_version >= 4 || !dwarf_strict))
10425     return NULL;
10426
10427   litsize = size_of_int_loc_descriptor (i);
10428   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10429      is more compact.  For DW_OP_stack_value we need:
10430      litsize + 1 (DW_OP_stack_value)
10431      and for DW_OP_implicit_value:
10432      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10433   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10434     {
10435       loc_result = int_loc_descriptor (i);
10436       add_loc_descr (&loc_result,
10437                      new_loc_descr (DW_OP_stack_value, 0, 0));
10438       return loc_result;
10439     }
10440
10441   loc_result = new_loc_descr (DW_OP_implicit_value,
10442                               size, 0);
10443   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10444   loc_result->dw_loc_oprnd2.v.val_int = i;
10445   return loc_result;
10446 }
10447
10448 /* Return a location descriptor that designates a base+offset location.  */
10449
10450 static dw_loc_descr_ref
10451 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10452                  enum var_init_status initialized)
10453 {
10454   unsigned int regno;
10455   dw_loc_descr_ref result;
10456   dw_fde_ref fde = cfun->fde;
10457
10458   /* We only use "frame base" when we're sure we're talking about the
10459      post-prologue local stack frame.  We do this by *not* running
10460      register elimination until this point, and recognizing the special
10461      argument pointer and soft frame pointer rtx's.  */
10462   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10463     {
10464       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10465
10466       if (elim != reg)
10467         {
10468           if (GET_CODE (elim) == PLUS)
10469             {
10470               offset += INTVAL (XEXP (elim, 1));
10471               elim = XEXP (elim, 0);
10472             }
10473           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10474                        && (elim == hard_frame_pointer_rtx
10475                            || elim == stack_pointer_rtx))
10476                       || elim == (frame_pointer_needed
10477                                   ? hard_frame_pointer_rtx
10478                                   : stack_pointer_rtx));
10479
10480           /* If drap register is used to align stack, use frame
10481              pointer + offset to access stack variables.  If stack
10482              is aligned without drap, use stack pointer + offset to
10483              access stack variables.  */
10484           if (crtl->stack_realign_tried
10485               && reg == frame_pointer_rtx)
10486             {
10487               int base_reg
10488                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10489                                       ? HARD_FRAME_POINTER_REGNUM
10490                                       : REGNO (elim));
10491               return new_reg_loc_descr (base_reg, offset);
10492             }
10493
10494           gcc_assert (frame_pointer_fb_offset_valid);
10495           offset += frame_pointer_fb_offset;
10496           return new_loc_descr (DW_OP_fbreg, offset, 0);
10497         }
10498     }
10499
10500   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10501
10502   if (!optimize && fde
10503       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10504     {
10505       /* Use cfa+offset to represent the location of arguments passed
10506          on the stack when drap is used to align stack.
10507          Only do this when not optimizing, for optimized code var-tracking
10508          is supposed to track where the arguments live and the register
10509          used as vdrap or drap in some spot might be used for something
10510          else in other part of the routine.  */
10511       return new_loc_descr (DW_OP_fbreg, offset, 0);
10512     }
10513
10514   if (regno <= 31)
10515     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10516                             offset, 0);
10517   else
10518     result = new_loc_descr (DW_OP_bregx, regno, offset);
10519
10520   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10521     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10522
10523   return result;
10524 }
10525
10526 /* Return true if this RTL expression describes a base+offset calculation.  */
10527
10528 static inline int
10529 is_based_loc (const_rtx rtl)
10530 {
10531   return (GET_CODE (rtl) == PLUS
10532           && ((REG_P (XEXP (rtl, 0))
10533                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10534                && CONST_INT_P (XEXP (rtl, 1)))));
10535 }
10536
10537 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10538    failed.  */
10539
10540 static dw_loc_descr_ref
10541 tls_mem_loc_descriptor (rtx mem)
10542 {
10543   tree base;
10544   dw_loc_descr_ref loc_result;
10545
10546   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10547     return NULL;
10548
10549   base = get_base_address (MEM_EXPR (mem));
10550   if (base == NULL
10551       || TREE_CODE (base) != VAR_DECL
10552       || !DECL_THREAD_LOCAL_P (base))
10553     return NULL;
10554
10555   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10556   if (loc_result == NULL)
10557     return NULL;
10558
10559   if (MEM_OFFSET (mem))
10560     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10561
10562   return loc_result;
10563 }
10564
10565 /* Output debug info about reason why we failed to expand expression as dwarf
10566    expression.  */
10567
10568 static void
10569 expansion_failed (tree expr, rtx rtl, char const *reason)
10570 {
10571   if (dump_file && (dump_flags & TDF_DETAILS))
10572     {
10573       fprintf (dump_file, "Failed to expand as dwarf: ");
10574       if (expr)
10575         print_generic_expr (dump_file, expr, dump_flags);
10576       if (rtl)
10577         {
10578           fprintf (dump_file, "\n");
10579           print_rtl (dump_file, rtl);
10580         }
10581       fprintf (dump_file, "\nReason: %s\n", reason);
10582     }
10583 }
10584
10585 /* Helper function for const_ok_for_output, called either directly
10586    or via for_each_rtx.  */
10587
10588 static int
10589 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10590 {
10591   rtx rtl = *rtlp;
10592
10593   if (GET_CODE (rtl) == UNSPEC)
10594     {
10595       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10596          we can't express it in the debug info.  */
10597 #ifdef ENABLE_CHECKING
10598       /* Don't complain about TLS UNSPECs, those are just too hard to
10599          delegitimize.  */
10600       if (XVECLEN (rtl, 0) != 1
10601           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10602           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10603           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10604           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10605         inform (current_function_decl
10606                 ? DECL_SOURCE_LOCATION (current_function_decl)
10607                 : UNKNOWN_LOCATION,
10608 #if NUM_UNSPEC_VALUES > 0
10609                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10610                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10611                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10612                 XINT (rtl, 1));
10613 #else
10614                 "non-delegitimized UNSPEC %d found in variable location",
10615                 XINT (rtl, 1));
10616 #endif
10617 #endif
10618       expansion_failed (NULL_TREE, rtl,
10619                         "UNSPEC hasn't been delegitimized.\n");
10620       return 1;
10621     }
10622
10623   if (GET_CODE (rtl) != SYMBOL_REF)
10624     return 0;
10625
10626   if (CONSTANT_POOL_ADDRESS_P (rtl))
10627     {
10628       bool marked;
10629       get_pool_constant_mark (rtl, &marked);
10630       /* If all references to this pool constant were optimized away,
10631          it was not output and thus we can't represent it.  */
10632       if (!marked)
10633         {
10634           expansion_failed (NULL_TREE, rtl,
10635                             "Constant was removed from constant pool.\n");
10636           return 1;
10637         }
10638     }
10639
10640   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10641     return 1;
10642
10643   /* Avoid references to external symbols in debug info, on several targets
10644      the linker might even refuse to link when linking a shared library,
10645      and in many other cases the relocations for .debug_info/.debug_loc are
10646      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10647      to be defined within the same shared library or executable are fine.  */
10648   if (SYMBOL_REF_EXTERNAL_P (rtl))
10649     {
10650       tree decl = SYMBOL_REF_DECL (rtl);
10651
10652       if (decl == NULL || !targetm.binds_local_p (decl))
10653         {
10654           expansion_failed (NULL_TREE, rtl,
10655                             "Symbol not defined in current TU.\n");
10656           return 1;
10657         }
10658     }
10659
10660   return 0;
10661 }
10662
10663 /* Return true if constant RTL can be emitted in DW_OP_addr or
10664    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10665    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10666
10667 static bool
10668 const_ok_for_output (rtx rtl)
10669 {
10670   if (GET_CODE (rtl) == SYMBOL_REF)
10671     return const_ok_for_output_1 (&rtl, NULL) == 0;
10672
10673   if (GET_CODE (rtl) == CONST)
10674     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10675
10676   return true;
10677 }
10678
10679 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10680    if possible, NULL otherwise.  */
10681
10682 static dw_die_ref
10683 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10684 {
10685   dw_die_ref type_die;
10686   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10687
10688   if (type == NULL)
10689     return NULL;
10690   switch (TREE_CODE (type))
10691     {
10692     case INTEGER_TYPE:
10693     case REAL_TYPE:
10694       break;
10695     default:
10696       return NULL;
10697     }
10698   type_die = lookup_type_die (type);
10699   if (!type_die)
10700     type_die = modified_type_die (type, false, false, comp_unit_die ());
10701   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10702     return NULL;
10703   return type_die;
10704 }
10705
10706 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10707    type matching MODE, or, if MODE is narrower than or as wide as
10708    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10709    possible.  */
10710
10711 static dw_loc_descr_ref
10712 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10713 {
10714   enum machine_mode outer_mode = mode;
10715   dw_die_ref type_die;
10716   dw_loc_descr_ref cvt;
10717
10718   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10719     {
10720       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10721       return op;
10722     }
10723   type_die = base_type_for_mode (outer_mode, 1);
10724   if (type_die == NULL)
10725     return NULL;
10726   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10727   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10728   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10729   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10730   add_loc_descr (&op, cvt);
10731   return op;
10732 }
10733
10734 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10735
10736 static dw_loc_descr_ref
10737 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10738                         dw_loc_descr_ref op1)
10739 {
10740   dw_loc_descr_ref ret = op0;
10741   add_loc_descr (&ret, op1);
10742   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10743   if (STORE_FLAG_VALUE != 1)
10744     {
10745       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10746       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10747     }
10748   return ret;
10749 }
10750
10751 /* Return location descriptor for signed comparison OP RTL.  */
10752
10753 static dw_loc_descr_ref
10754 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10755                          enum machine_mode mem_mode)
10756 {
10757   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10758   dw_loc_descr_ref op0, op1;
10759   int shift;
10760
10761   if (op_mode == VOIDmode)
10762     op_mode = GET_MODE (XEXP (rtl, 1));
10763   if (op_mode == VOIDmode)
10764     return NULL;
10765
10766   if (dwarf_strict
10767       && (GET_MODE_CLASS (op_mode) != MODE_INT
10768           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10769     return NULL;
10770
10771   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10772                             VAR_INIT_STATUS_INITIALIZED);
10773   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10774                             VAR_INIT_STATUS_INITIALIZED);
10775
10776   if (op0 == NULL || op1 == NULL)
10777     return NULL;
10778
10779   if (GET_MODE_CLASS (op_mode) != MODE_INT
10780       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10781     return compare_loc_descriptor (op, op0, op1);
10782
10783   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10784     {
10785       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10786       dw_loc_descr_ref cvt;
10787
10788       if (type_die == NULL)
10789         return NULL;
10790       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10791       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10792       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10793       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10794       add_loc_descr (&op0, cvt);
10795       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10796       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10797       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10798       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10799       add_loc_descr (&op1, cvt);
10800       return compare_loc_descriptor (op, op0, op1);
10801     }
10802
10803   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10804   /* For eq/ne, if the operands are known to be zero-extended,
10805      there is no need to do the fancy shifting up.  */
10806   if (op == DW_OP_eq || op == DW_OP_ne)
10807     {
10808       dw_loc_descr_ref last0, last1;
10809       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10810         ;
10811       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10812         ;
10813       /* deref_size zero extends, and for constants we can check
10814          whether they are zero extended or not.  */
10815       if (((last0->dw_loc_opc == DW_OP_deref_size
10816             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10817            || (CONST_INT_P (XEXP (rtl, 0))
10818                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10819                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10820           && ((last1->dw_loc_opc == DW_OP_deref_size
10821                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10822               || (CONST_INT_P (XEXP (rtl, 1))
10823                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10824                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10825         return compare_loc_descriptor (op, op0, op1);
10826
10827       /* EQ/NE comparison against constant in narrower type than
10828          DWARF2_ADDR_SIZE can be performed either as
10829          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10830          DW_OP_{eq,ne}
10831          or
10832          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10833          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10834       if (CONST_INT_P (XEXP (rtl, 1))
10835           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10836           && (size_of_int_loc_descriptor (shift) + 1
10837               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10838               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10839                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10840                                                & GET_MODE_MASK (op_mode))))
10841         {
10842           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10843           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10844           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10845                                     & GET_MODE_MASK (op_mode));
10846           return compare_loc_descriptor (op, op0, op1);
10847         }
10848     }
10849   add_loc_descr (&op0, int_loc_descriptor (shift));
10850   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10851   if (CONST_INT_P (XEXP (rtl, 1)))
10852     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10853   else
10854     {
10855       add_loc_descr (&op1, int_loc_descriptor (shift));
10856       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10857     }
10858   return compare_loc_descriptor (op, op0, op1);
10859 }
10860
10861 /* Return location descriptor for unsigned comparison OP RTL.  */
10862
10863 static dw_loc_descr_ref
10864 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10865                          enum machine_mode mem_mode)
10866 {
10867   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10868   dw_loc_descr_ref op0, op1;
10869
10870   if (op_mode == VOIDmode)
10871     op_mode = GET_MODE (XEXP (rtl, 1));
10872   if (op_mode == VOIDmode)
10873     return NULL;
10874   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10875     return NULL;
10876
10877   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10878     return NULL;
10879
10880   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10881                             VAR_INIT_STATUS_INITIALIZED);
10882   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10883                             VAR_INIT_STATUS_INITIALIZED);
10884
10885   if (op0 == NULL || op1 == NULL)
10886     return NULL;
10887
10888   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10889     {
10890       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10891       dw_loc_descr_ref last0, last1;
10892       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10893         ;
10894       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10895         ;
10896       if (CONST_INT_P (XEXP (rtl, 0)))
10897         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10898       /* deref_size zero extends, so no need to mask it again.  */
10899       else if (last0->dw_loc_opc != DW_OP_deref_size
10900                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10901         {
10902           add_loc_descr (&op0, int_loc_descriptor (mask));
10903           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10904         }
10905       if (CONST_INT_P (XEXP (rtl, 1)))
10906         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10907       /* deref_size zero extends, so no need to mask it again.  */
10908       else if (last1->dw_loc_opc != DW_OP_deref_size
10909                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10910         {
10911           add_loc_descr (&op1, int_loc_descriptor (mask));
10912           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10913         }
10914     }
10915   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10916     {
10917       HOST_WIDE_INT bias = 1;
10918       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10919       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10920       if (CONST_INT_P (XEXP (rtl, 1)))
10921         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10922                                   + INTVAL (XEXP (rtl, 1)));
10923       else
10924         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10925                                             bias, 0));
10926     }
10927   return compare_loc_descriptor (op, op0, op1);
10928 }
10929
10930 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10931
10932 static dw_loc_descr_ref
10933 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10934                        enum machine_mode mem_mode)
10935 {
10936   enum dwarf_location_atom op;
10937   dw_loc_descr_ref op0, op1, ret;
10938   dw_loc_descr_ref bra_node, drop_node;
10939
10940   if (dwarf_strict
10941       && (GET_MODE_CLASS (mode) != MODE_INT
10942           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10943     return NULL;
10944
10945   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10946                             VAR_INIT_STATUS_INITIALIZED);
10947   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10948                             VAR_INIT_STATUS_INITIALIZED);
10949
10950   if (op0 == NULL || op1 == NULL)
10951     return NULL;
10952
10953   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10954   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10955   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10956   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10957     {
10958       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10959         {
10960           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10961           add_loc_descr (&op0, int_loc_descriptor (mask));
10962           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10963           add_loc_descr (&op1, int_loc_descriptor (mask));
10964           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10965         }
10966       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10967         {
10968           HOST_WIDE_INT bias = 1;
10969           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10970           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10971           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10972         }
10973     }
10974   else if (GET_MODE_CLASS (mode) == MODE_INT
10975            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10976     {
10977       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10978       add_loc_descr (&op0, int_loc_descriptor (shift));
10979       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10980       add_loc_descr (&op1, int_loc_descriptor (shift));
10981       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10982     }
10983   else if (GET_MODE_CLASS (mode) == MODE_INT
10984            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10985     {
10986       dw_die_ref type_die = base_type_for_mode (mode, 0);
10987       dw_loc_descr_ref cvt;
10988       if (type_die == NULL)
10989         return NULL;
10990       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10991       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10992       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10993       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10994       add_loc_descr (&op0, cvt);
10995       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10996       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10997       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10998       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10999       add_loc_descr (&op1, cvt);
11000     }
11001
11002   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11003     op = DW_OP_lt;
11004   else
11005     op = DW_OP_gt;
11006   ret = op0;
11007   add_loc_descr (&ret, op1);
11008   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11009   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11010   add_loc_descr (&ret, bra_node);
11011   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11012   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11013   add_loc_descr (&ret, drop_node);
11014   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11015   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11016   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11017       && GET_MODE_CLASS (mode) == MODE_INT
11018       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11019     ret = convert_descriptor_to_mode (mode, ret);
11020   return ret;
11021 }
11022
11023 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11024    but after converting arguments to type_die, afterwards
11025    convert back to unsigned.  */
11026
11027 static dw_loc_descr_ref
11028 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11029              enum machine_mode mode, enum machine_mode mem_mode)
11030 {
11031   dw_loc_descr_ref cvt, op0, op1;
11032
11033   if (type_die == NULL)
11034     return NULL;
11035   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11036                             VAR_INIT_STATUS_INITIALIZED);
11037   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11038                             VAR_INIT_STATUS_INITIALIZED);
11039   if (op0 == NULL || op1 == NULL)
11040     return NULL;
11041   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11042   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11043   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11044   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11045   add_loc_descr (&op0, cvt);
11046   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11047   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11048   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11049   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11050   add_loc_descr (&op1, cvt);
11051   add_loc_descr (&op0, op1);
11052   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11053   return convert_descriptor_to_mode (mode, op0);
11054 }
11055
11056 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11057    const0 is DW_OP_lit0 or corresponding typed constant,
11058    const1 is DW_OP_lit1 or corresponding typed constant
11059    and constMSB is constant with just the MSB bit set
11060    for the mode):
11061        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11062    L1: const0 DW_OP_swap
11063    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11064        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11065    L3: DW_OP_drop
11066    L4: DW_OP_nop
11067
11068    CTZ is similar:
11069        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11070    L1: const0 DW_OP_swap
11071    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11072        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11073    L3: DW_OP_drop
11074    L4: DW_OP_nop
11075
11076    FFS is similar:
11077        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11078    L1: const1 DW_OP_swap
11079    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11080        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11081    L3: DW_OP_drop
11082    L4: DW_OP_nop  */
11083
11084 static dw_loc_descr_ref
11085 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11086                     enum machine_mode mem_mode)
11087 {
11088   dw_loc_descr_ref op0, ret, tmp;
11089   HOST_WIDE_INT valv;
11090   dw_loc_descr_ref l1jump, l1label;
11091   dw_loc_descr_ref l2jump, l2label;
11092   dw_loc_descr_ref l3jump, l3label;
11093   dw_loc_descr_ref l4jump, l4label;
11094   rtx msb;
11095
11096   if (GET_MODE_CLASS (mode) != MODE_INT
11097       || GET_MODE (XEXP (rtl, 0)) != mode
11098       || (GET_CODE (rtl) == CLZ
11099           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11100     return NULL;
11101
11102   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11103                             VAR_INIT_STATUS_INITIALIZED);
11104   if (op0 == NULL)
11105     return NULL;
11106   ret = op0;
11107   if (GET_CODE (rtl) == CLZ)
11108     {
11109       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11110         valv = GET_MODE_BITSIZE (mode);
11111     }
11112   else if (GET_CODE (rtl) == FFS)
11113     valv = 0;
11114   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11115     valv = GET_MODE_BITSIZE (mode);
11116   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11117   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11118   add_loc_descr (&ret, l1jump);
11119   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11120   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11121                             VAR_INIT_STATUS_INITIALIZED);
11122   if (tmp == NULL)
11123     return NULL;
11124   add_loc_descr (&ret, tmp);
11125   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11126   add_loc_descr (&ret, l4jump);
11127   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11128                                 ? const1_rtx : const0_rtx,
11129                                 mode, mem_mode,
11130                                 VAR_INIT_STATUS_INITIALIZED);
11131   if (l1label == NULL)
11132     return NULL;
11133   add_loc_descr (&ret, l1label);
11134   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11135   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11136   add_loc_descr (&ret, l2label);
11137   if (GET_CODE (rtl) != CLZ)
11138     msb = const1_rtx;
11139   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11140     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11141                    << (GET_MODE_BITSIZE (mode) - 1));
11142   else
11143     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11144                                   << (GET_MODE_BITSIZE (mode)
11145                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11146   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11147     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11148                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11149                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11150   else
11151     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11152                               VAR_INIT_STATUS_INITIALIZED);
11153   if (tmp == NULL)
11154     return NULL;
11155   add_loc_descr (&ret, tmp);
11156   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11157   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11158   add_loc_descr (&ret, l3jump);
11159   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11160                             VAR_INIT_STATUS_INITIALIZED);
11161   if (tmp == NULL)
11162     return NULL;
11163   add_loc_descr (&ret, tmp);
11164   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11165                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11166   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11167   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11168   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11169   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11170   add_loc_descr (&ret, l2jump);
11171   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11172   add_loc_descr (&ret, l3label);
11173   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11174   add_loc_descr (&ret, l4label);
11175   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11176   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11177   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11178   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11179   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11180   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11181   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11182   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11183   return ret;
11184 }
11185
11186 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11187    const1 is DW_OP_lit1 or corresponding typed constant):
11188        const0 DW_OP_swap
11189    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11190        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11191    L2: DW_OP_drop
11192
11193    PARITY is similar:
11194    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11195        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11196    L2: DW_OP_drop  */
11197
11198 static dw_loc_descr_ref
11199 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11200                          enum machine_mode mem_mode)
11201 {
11202   dw_loc_descr_ref op0, ret, tmp;
11203   dw_loc_descr_ref l1jump, l1label;
11204   dw_loc_descr_ref l2jump, l2label;
11205
11206   if (GET_MODE_CLASS (mode) != MODE_INT
11207       || GET_MODE (XEXP (rtl, 0)) != mode)
11208     return NULL;
11209
11210   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11211                             VAR_INIT_STATUS_INITIALIZED);
11212   if (op0 == NULL)
11213     return NULL;
11214   ret = op0;
11215   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11216                             VAR_INIT_STATUS_INITIALIZED);
11217   if (tmp == NULL)
11218     return NULL;
11219   add_loc_descr (&ret, tmp);
11220   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11221   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11222   add_loc_descr (&ret, l1label);
11223   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11224   add_loc_descr (&ret, l2jump);
11225   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11226   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11227   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11228                             VAR_INIT_STATUS_INITIALIZED);
11229   if (tmp == NULL)
11230     return NULL;
11231   add_loc_descr (&ret, tmp);
11232   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11233   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11234                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11235   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11236   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11237                             VAR_INIT_STATUS_INITIALIZED);
11238   add_loc_descr (&ret, tmp);
11239   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11240   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11241   add_loc_descr (&ret, l1jump);
11242   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11243   add_loc_descr (&ret, l2label);
11244   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11245   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11246   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11247   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11248   return ret;
11249 }
11250
11251 /* BSWAP (constS is initial shift count, either 56 or 24):
11252        constS const0
11253    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11254        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11255        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11256        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11257    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11258
11259 static dw_loc_descr_ref
11260 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11261                       enum machine_mode mem_mode)
11262 {
11263   dw_loc_descr_ref op0, ret, tmp;
11264   dw_loc_descr_ref l1jump, l1label;
11265   dw_loc_descr_ref l2jump, l2label;
11266
11267   if (GET_MODE_CLASS (mode) != MODE_INT
11268       || BITS_PER_UNIT != 8
11269       || (GET_MODE_BITSIZE (mode) != 32
11270           &&  GET_MODE_BITSIZE (mode) != 64))
11271     return NULL;
11272
11273   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11274                             VAR_INIT_STATUS_INITIALIZED);
11275   if (op0 == NULL)
11276     return NULL;
11277
11278   ret = op0;
11279   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11280                             mode, mem_mode,
11281                             VAR_INIT_STATUS_INITIALIZED);
11282   if (tmp == NULL)
11283     return NULL;
11284   add_loc_descr (&ret, tmp);
11285   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11286                             VAR_INIT_STATUS_INITIALIZED);
11287   if (tmp == NULL)
11288     return NULL;
11289   add_loc_descr (&ret, tmp);
11290   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11291   add_loc_descr (&ret, l1label);
11292   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11293                             mode, mem_mode,
11294                             VAR_INIT_STATUS_INITIALIZED);
11295   add_loc_descr (&ret, tmp);
11296   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11297   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11298   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11299   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11300                             VAR_INIT_STATUS_INITIALIZED);
11301   if (tmp == NULL)
11302     return NULL;
11303   add_loc_descr (&ret, tmp);
11304   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11305   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11306   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11307   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11308   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11309   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11310   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11311                             VAR_INIT_STATUS_INITIALIZED);
11312   add_loc_descr (&ret, tmp);
11313   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11314   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11315   add_loc_descr (&ret, l2jump);
11316   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11317                             VAR_INIT_STATUS_INITIALIZED);
11318   add_loc_descr (&ret, tmp);
11319   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11320   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11321   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11322   add_loc_descr (&ret, l1jump);
11323   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11324   add_loc_descr (&ret, l2label);
11325   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11326   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11327   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11328   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11329   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11330   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11331   return ret;
11332 }
11333
11334 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11335    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11336    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11337    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11338
11339    ROTATERT is similar:
11340    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11341    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11342    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11343
11344 static dw_loc_descr_ref
11345 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11346                        enum machine_mode mem_mode)
11347 {
11348   rtx rtlop1 = XEXP (rtl, 1);
11349   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11350   int i;
11351
11352   if (GET_MODE_CLASS (mode) != MODE_INT)
11353     return NULL;
11354
11355   if (GET_MODE (rtlop1) != VOIDmode
11356       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11357     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11358   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11359                             VAR_INIT_STATUS_INITIALIZED);
11360   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11361                             VAR_INIT_STATUS_INITIALIZED);
11362   if (op0 == NULL || op1 == NULL)
11363     return NULL;
11364   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11365     for (i = 0; i < 2; i++)
11366       {
11367         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11368           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11369                                         mode, mem_mode,
11370                                         VAR_INIT_STATUS_INITIALIZED);
11371         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11372           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11373                                    ? DW_OP_const4u
11374                                    : HOST_BITS_PER_WIDE_INT == 64
11375                                    ? DW_OP_const8u : DW_OP_constu,
11376                                    GET_MODE_MASK (mode), 0);
11377         else
11378           mask[i] = NULL;
11379         if (mask[i] == NULL)
11380           return NULL;
11381         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11382       }
11383   ret = op0;
11384   add_loc_descr (&ret, op1);
11385   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11386   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11387   if (GET_CODE (rtl) == ROTATERT)
11388     {
11389       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11390       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11391                                           GET_MODE_BITSIZE (mode), 0));
11392     }
11393   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11394   if (mask[0] != NULL)
11395     add_loc_descr (&ret, mask[0]);
11396   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11397   if (mask[1] != NULL)
11398     {
11399       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11400       add_loc_descr (&ret, mask[1]);
11401       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11402     }
11403   if (GET_CODE (rtl) == ROTATE)
11404     {
11405       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11406       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11407                                           GET_MODE_BITSIZE (mode), 0));
11408     }
11409   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11410   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11411   return ret;
11412 }
11413
11414 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11415    for DEBUG_PARAMETER_REF RTL.  */
11416
11417 static dw_loc_descr_ref
11418 parameter_ref_descriptor (rtx rtl)
11419 {
11420   dw_loc_descr_ref ret;
11421   dw_die_ref ref;
11422
11423   if (dwarf_strict)
11424     return NULL;
11425   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11426   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11427   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11428   if (ref)
11429     {
11430       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11431       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11432       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11433     }
11434   else
11435     {
11436       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11437       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11438     }
11439   return ret;
11440 }
11441
11442 /* Helper function to get mode of MEM's address.  */
11443
11444 enum machine_mode
11445 get_address_mode (rtx mem)
11446 {
11447   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11448   if (mode != VOIDmode)
11449     return mode;
11450   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11451 }
11452
11453 /* The following routine converts the RTL for a variable or parameter
11454    (resident in memory) into an equivalent Dwarf representation of a
11455    mechanism for getting the address of that same variable onto the top of a
11456    hypothetical "address evaluation" stack.
11457
11458    When creating memory location descriptors, we are effectively transforming
11459    the RTL for a memory-resident object into its Dwarf postfix expression
11460    equivalent.  This routine recursively descends an RTL tree, turning
11461    it into Dwarf postfix code as it goes.
11462
11463    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11464
11465    MEM_MODE is the mode of the memory reference, needed to handle some
11466    autoincrement addressing modes.
11467
11468    Return 0 if we can't represent the location.  */
11469
11470 dw_loc_descr_ref
11471 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11472                     enum machine_mode mem_mode,
11473                     enum var_init_status initialized)
11474 {
11475   dw_loc_descr_ref mem_loc_result = NULL;
11476   enum dwarf_location_atom op;
11477   dw_loc_descr_ref op0, op1;
11478
11479   if (mode == VOIDmode)
11480     mode = GET_MODE (rtl);
11481
11482   /* Note that for a dynamically sized array, the location we will generate a
11483      description of here will be the lowest numbered location which is
11484      actually within the array.  That's *not* necessarily the same as the
11485      zeroth element of the array.  */
11486
11487   rtl = targetm.delegitimize_address (rtl);
11488
11489   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11490     return NULL;
11491
11492   switch (GET_CODE (rtl))
11493     {
11494     case POST_INC:
11495     case POST_DEC:
11496     case POST_MODIFY:
11497       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11498
11499     case SUBREG:
11500       /* The case of a subreg may arise when we have a local (register)
11501          variable or a formal (register) parameter which doesn't quite fill
11502          up an entire register.  For now, just assume that it is
11503          legitimate to make the Dwarf info refer to the whole register which
11504          contains the given subreg.  */
11505       if (!subreg_lowpart_p (rtl))
11506         break;
11507       if (GET_MODE_CLASS (mode) == MODE_INT
11508           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11509           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11510 #ifdef POINTERS_EXTEND_UNSIGNED
11511               || (mode == Pmode && mem_mode != VOIDmode)
11512 #endif
11513              )
11514           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11515         {
11516           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11517                                                GET_MODE (SUBREG_REG (rtl)),
11518                                                mem_mode, initialized);
11519           break;
11520         }
11521       if (dwarf_strict)
11522         break;
11523       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11524         break;
11525       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11526           && (GET_MODE_CLASS (mode) != MODE_INT
11527               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11528         break;
11529       else
11530         {
11531           dw_die_ref type_die;
11532           dw_loc_descr_ref cvt;
11533
11534           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11535                                                GET_MODE (SUBREG_REG (rtl)),
11536                                                mem_mode, initialized);
11537           if (mem_loc_result == NULL)
11538             break;
11539           type_die = base_type_for_mode (mode,
11540                                          GET_MODE_CLASS (mode) == MODE_INT);
11541           if (type_die == NULL)
11542             {
11543               mem_loc_result = NULL;
11544               break;
11545             }
11546           if (GET_MODE_SIZE (mode)
11547               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11548             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11549           else
11550             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11551           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11552           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11553           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11554           add_loc_descr (&mem_loc_result, cvt);
11555         }
11556       break;
11557
11558     case REG:
11559       if (GET_MODE_CLASS (mode) != MODE_INT
11560           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11561 #ifdef POINTERS_EXTEND_UNSIGNED
11562               && (mode != Pmode || mem_mode == VOIDmode)
11563 #endif
11564               ))
11565         {
11566           dw_die_ref type_die;
11567
11568           if (dwarf_strict)
11569             break;
11570           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11571             break;
11572           type_die = base_type_for_mode (mode,
11573                                          GET_MODE_CLASS (mode) == MODE_INT);
11574           if (type_die == NULL)
11575             break;
11576           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11577                                           dbx_reg_number (rtl), 0);
11578           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11579           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11580           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11581           break;
11582         }
11583       /* Whenever a register number forms a part of the description of the
11584          method for calculating the (dynamic) address of a memory resident
11585          object, DWARF rules require the register number be referred to as
11586          a "base register".  This distinction is not based in any way upon
11587          what category of register the hardware believes the given register
11588          belongs to.  This is strictly DWARF terminology we're dealing with
11589          here. Note that in cases where the location of a memory-resident
11590          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11591          OP_CONST (0)) the actual DWARF location descriptor that we generate
11592          may just be OP_BASEREG (basereg).  This may look deceptively like
11593          the object in question was allocated to a register (rather than in
11594          memory) so DWARF consumers need to be aware of the subtle
11595          distinction between OP_REG and OP_BASEREG.  */
11596       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11597         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11598       else if (stack_realign_drap
11599                && crtl->drap_reg
11600                && crtl->args.internal_arg_pointer == rtl
11601                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11602         {
11603           /* If RTL is internal_arg_pointer, which has been optimized
11604              out, use DRAP instead.  */
11605           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11606                                             VAR_INIT_STATUS_INITIALIZED);
11607         }
11608       break;
11609
11610     case SIGN_EXTEND:
11611     case ZERO_EXTEND:
11612       if (GET_MODE_CLASS (mode) != MODE_INT)
11613         break;
11614       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11615                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11616       if (op0 == 0)
11617         break;
11618       else if (GET_CODE (rtl) == ZERO_EXTEND
11619                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11620                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11621                   < HOST_BITS_PER_WIDE_INT
11622                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11623                   to expand zero extend as two shifts instead of
11624                   masking.  */
11625                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11626         {
11627           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11628           mem_loc_result = op0;
11629           add_loc_descr (&mem_loc_result,
11630                          int_loc_descriptor (GET_MODE_MASK (imode)));
11631           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11632         }
11633       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11634         {
11635           int shift = DWARF2_ADDR_SIZE
11636                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11637           shift *= BITS_PER_UNIT;
11638           if (GET_CODE (rtl) == SIGN_EXTEND)
11639             op = DW_OP_shra;
11640           else
11641             op = DW_OP_shr;
11642           mem_loc_result = op0;
11643           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11644           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11645           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11646           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11647         }
11648       else if (!dwarf_strict)
11649         {
11650           dw_die_ref type_die1, type_die2;
11651           dw_loc_descr_ref cvt;
11652
11653           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11654                                           GET_CODE (rtl) == ZERO_EXTEND);
11655           if (type_die1 == NULL)
11656             break;
11657           type_die2 = base_type_for_mode (mode, 1);
11658           if (type_die2 == NULL)
11659             break;
11660           mem_loc_result = op0;
11661           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11662           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11663           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11664           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11665           add_loc_descr (&mem_loc_result, cvt);
11666           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11667           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11668           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11669           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11670           add_loc_descr (&mem_loc_result, cvt);
11671         }
11672       break;
11673
11674     case MEM:
11675       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11676                                            get_address_mode (rtl), mode,
11677                                            VAR_INIT_STATUS_INITIALIZED);
11678       if (mem_loc_result == NULL)
11679         mem_loc_result = tls_mem_loc_descriptor (rtl);
11680       if (mem_loc_result != 0)
11681         {
11682           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11683               || GET_MODE_CLASS (mode) != MODE_INT)
11684             {
11685               dw_die_ref type_die;
11686               dw_loc_descr_ref deref;
11687
11688               if (dwarf_strict)
11689                 return NULL;
11690               type_die
11691                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11692               if (type_die == NULL)
11693                 return NULL;
11694               deref = new_loc_descr (DW_OP_GNU_deref_type,
11695                                      GET_MODE_SIZE (mode), 0);
11696               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11697               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11698               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11699               add_loc_descr (&mem_loc_result, deref);
11700             }
11701           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11702             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11703           else
11704             add_loc_descr (&mem_loc_result,
11705                            new_loc_descr (DW_OP_deref_size,
11706                                           GET_MODE_SIZE (mode), 0));
11707         }
11708       else
11709         {
11710           rtx new_rtl = avoid_constant_pool_reference (rtl);
11711           if (new_rtl != rtl)
11712             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
11713         }
11714       break;
11715
11716     case LO_SUM:
11717       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11718
11719     case LABEL_REF:
11720       /* Some ports can transform a symbol ref into a label ref, because
11721          the symbol ref is too far away and has to be dumped into a constant
11722          pool.  */
11723     case CONST:
11724     case SYMBOL_REF:
11725       if (GET_MODE_CLASS (mode) != MODE_INT
11726           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11727 #ifdef POINTERS_EXTEND_UNSIGNED
11728               && (mode != Pmode || mem_mode == VOIDmode)
11729 #endif
11730               ))
11731         break;
11732       if (GET_CODE (rtl) == SYMBOL_REF
11733           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11734         {
11735           dw_loc_descr_ref temp;
11736
11737           /* If this is not defined, we have no way to emit the data.  */
11738           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11739             break;
11740
11741           /* We used to emit DW_OP_addr here, but that's wrong, since
11742              DW_OP_addr should be relocated by the debug info consumer,
11743              while DW_OP_GNU_push_tls_address operand should not.  */
11744           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11745                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11746           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11747           temp->dw_loc_oprnd1.v.val_addr = rtl;
11748           temp->dtprel = true;
11749
11750           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11751           add_loc_descr (&mem_loc_result, temp);
11752
11753           break;
11754         }
11755
11756       if (!const_ok_for_output (rtl))
11757         break;
11758
11759     symref:
11760       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11761       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11762       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11763       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11764       break;
11765
11766     case CONCAT:
11767     case CONCATN:
11768     case VAR_LOCATION:
11769     case DEBUG_IMPLICIT_PTR:
11770       expansion_failed (NULL_TREE, rtl,
11771                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11772       return 0;
11773
11774     case ENTRY_VALUE:
11775       if (dwarf_strict)
11776         return NULL;
11777       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11778         {
11779           if (GET_MODE_CLASS (mode) != MODE_INT
11780               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11781             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11782                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11783           else
11784             op0
11785               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11786                                         VAR_INIT_STATUS_INITIALIZED);
11787         }
11788       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11789                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11790         {
11791           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11792                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11793           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11794             return NULL;
11795         }
11796       else
11797         gcc_unreachable ();
11798       if (op0 == NULL)
11799         return NULL;
11800       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11801       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11802       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11803       break;
11804
11805     case DEBUG_PARAMETER_REF:
11806       mem_loc_result = parameter_ref_descriptor (rtl);
11807       break;
11808
11809     case PRE_MODIFY:
11810       /* Extract the PLUS expression nested inside and fall into
11811          PLUS code below.  */
11812       rtl = XEXP (rtl, 1);
11813       goto plus;
11814
11815     case PRE_INC:
11816     case PRE_DEC:
11817       /* Turn these into a PLUS expression and fall into the PLUS code
11818          below.  */
11819       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11820                           GEN_INT (GET_CODE (rtl) == PRE_INC
11821                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11822                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11823
11824       /* ... fall through ...  */
11825
11826     case PLUS:
11827     plus:
11828       if (is_based_loc (rtl)
11829           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11830           && GET_MODE_CLASS (mode) == MODE_INT)
11831         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11832                                           INTVAL (XEXP (rtl, 1)),
11833                                           VAR_INIT_STATUS_INITIALIZED);
11834       else
11835         {
11836           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11837                                                VAR_INIT_STATUS_INITIALIZED);
11838           if (mem_loc_result == 0)
11839             break;
11840
11841           if (CONST_INT_P (XEXP (rtl, 1))
11842               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11843             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11844           else
11845             {
11846               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11847                                         VAR_INIT_STATUS_INITIALIZED);
11848               if (op1 == 0)
11849                 break;
11850               add_loc_descr (&mem_loc_result, op1);
11851               add_loc_descr (&mem_loc_result,
11852                              new_loc_descr (DW_OP_plus, 0, 0));
11853             }
11854         }
11855       break;
11856
11857     /* If a pseudo-reg is optimized away, it is possible for it to
11858        be replaced with a MEM containing a multiply or shift.  */
11859     case MINUS:
11860       op = DW_OP_minus;
11861       goto do_binop;
11862
11863     case MULT:
11864       op = DW_OP_mul;
11865       goto do_binop;
11866
11867     case DIV:
11868       if (!dwarf_strict
11869           && GET_MODE_CLASS (mode) == MODE_INT
11870           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11871         {
11872           mem_loc_result = typed_binop (DW_OP_div, rtl,
11873                                         base_type_for_mode (mode, 0),
11874                                         mode, mem_mode);
11875           break;
11876         }
11877       op = DW_OP_div;
11878       goto do_binop;
11879
11880     case UMOD:
11881       op = DW_OP_mod;
11882       goto do_binop;
11883
11884     case ASHIFT:
11885       op = DW_OP_shl;
11886       goto do_shift;
11887
11888     case ASHIFTRT:
11889       op = DW_OP_shra;
11890       goto do_shift;
11891
11892     case LSHIFTRT:
11893       op = DW_OP_shr;
11894       goto do_shift;
11895
11896     do_shift:
11897       if (GET_MODE_CLASS (mode) != MODE_INT)
11898         break;
11899       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11900                                 VAR_INIT_STATUS_INITIALIZED);
11901       {
11902         rtx rtlop1 = XEXP (rtl, 1);
11903         if (GET_MODE (rtlop1) != VOIDmode
11904             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11905                < GET_MODE_BITSIZE (mode))
11906           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11907         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11908                                   VAR_INIT_STATUS_INITIALIZED);
11909       }
11910
11911       if (op0 == 0 || op1 == 0)
11912         break;
11913
11914       mem_loc_result = op0;
11915       add_loc_descr (&mem_loc_result, op1);
11916       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11917       break;
11918
11919     case AND:
11920       op = DW_OP_and;
11921       goto do_binop;
11922
11923     case IOR:
11924       op = DW_OP_or;
11925       goto do_binop;
11926
11927     case XOR:
11928       op = DW_OP_xor;
11929       goto do_binop;
11930
11931     do_binop:
11932       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11933                                 VAR_INIT_STATUS_INITIALIZED);
11934       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11935                                 VAR_INIT_STATUS_INITIALIZED);
11936
11937       if (op0 == 0 || op1 == 0)
11938         break;
11939
11940       mem_loc_result = op0;
11941       add_loc_descr (&mem_loc_result, op1);
11942       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11943       break;
11944
11945     case MOD:
11946       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11947         {
11948           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11949                                         base_type_for_mode (mode, 0),
11950                                         mode, mem_mode);
11951           break;
11952         }
11953
11954       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11955                                 VAR_INIT_STATUS_INITIALIZED);
11956       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11957                                 VAR_INIT_STATUS_INITIALIZED);
11958
11959       if (op0 == 0 || op1 == 0)
11960         break;
11961
11962       mem_loc_result = op0;
11963       add_loc_descr (&mem_loc_result, op1);
11964       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11965       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11966       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11967       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11968       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11969       break;
11970
11971     case UDIV:
11972       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11973         {
11974           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11975             {
11976               op = DW_OP_div;
11977               goto do_binop;
11978             }
11979           mem_loc_result = typed_binop (DW_OP_div, rtl,
11980                                         base_type_for_mode (mode, 1),
11981                                         mode, mem_mode);
11982         }
11983       break;
11984
11985     case NOT:
11986       op = DW_OP_not;
11987       goto do_unop;
11988
11989     case ABS:
11990       op = DW_OP_abs;
11991       goto do_unop;
11992
11993     case NEG:
11994       op = DW_OP_neg;
11995       goto do_unop;
11996
11997     do_unop:
11998       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11999                                 VAR_INIT_STATUS_INITIALIZED);
12000
12001       if (op0 == 0)
12002         break;
12003
12004       mem_loc_result = op0;
12005       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12006       break;
12007
12008     case CONST_INT:
12009       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12010 #ifdef POINTERS_EXTEND_UNSIGNED
12011           || (mode == Pmode
12012               && mem_mode != VOIDmode
12013               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12014 #endif
12015           )
12016         {
12017           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12018           break;
12019         }
12020       if (!dwarf_strict
12021           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12022               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12023         {
12024           dw_die_ref type_die = base_type_for_mode (mode, 1);
12025           enum machine_mode amode;
12026           if (type_die == NULL)
12027             return NULL;
12028           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12029                                  MODE_INT, 0);
12030           if (INTVAL (rtl) >= 0
12031               && amode != BLKmode
12032               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12033               /* const DW_OP_GNU_convert <XXX> vs.
12034                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12035               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12036                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12037             {
12038               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12039               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12040               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12041               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12042               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12043               add_loc_descr (&mem_loc_result, op0);
12044               return mem_loc_result;
12045             }
12046           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12047                                           INTVAL (rtl));
12048           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12049           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12050           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12051           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12052             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12053           else
12054             {
12055               mem_loc_result->dw_loc_oprnd2.val_class
12056                 = dw_val_class_const_double;
12057               mem_loc_result->dw_loc_oprnd2.v.val_double
12058                 = shwi_to_double_int (INTVAL (rtl));
12059             }
12060         }
12061       break;
12062
12063     case CONST_DOUBLE:
12064       if (!dwarf_strict)
12065         {
12066           dw_die_ref type_die;
12067
12068           /* Note that a CONST_DOUBLE rtx could represent either an integer
12069              or a floating-point constant.  A CONST_DOUBLE is used whenever
12070              the constant requires more than one word in order to be
12071              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12072           if (mode == VOIDmode
12073               || (GET_MODE (rtl) == VOIDmode
12074                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12075             break;
12076           type_die = base_type_for_mode (mode,
12077                                          GET_MODE_CLASS (mode) == MODE_INT);
12078           if (type_die == NULL)
12079             return NULL;
12080           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12081           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12082           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12083           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12084           if (SCALAR_FLOAT_MODE_P (mode))
12085             {
12086               unsigned int length = GET_MODE_SIZE (mode);
12087               unsigned char *array
12088                   = (unsigned char*) ggc_alloc_atomic (length);
12089
12090               insert_float (rtl, array);
12091               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12092               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12093               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12094               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12095             }
12096           else
12097             {
12098               mem_loc_result->dw_loc_oprnd2.val_class
12099                 = dw_val_class_const_double;
12100               mem_loc_result->dw_loc_oprnd2.v.val_double
12101                 = rtx_to_double_int (rtl);
12102             }
12103         }
12104       break;
12105
12106     case EQ:
12107       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12108       break;
12109
12110     case GE:
12111       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12112       break;
12113
12114     case GT:
12115       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12116       break;
12117
12118     case LE:
12119       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12120       break;
12121
12122     case LT:
12123       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12124       break;
12125
12126     case NE:
12127       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12128       break;
12129
12130     case GEU:
12131       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12132       break;
12133
12134     case GTU:
12135       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12136       break;
12137
12138     case LEU:
12139       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12140       break;
12141
12142     case LTU:
12143       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12144       break;
12145
12146     case UMIN:
12147     case UMAX:
12148       if (GET_MODE_CLASS (mode) != MODE_INT)
12149         break;
12150       /* FALLTHRU */
12151     case SMIN:
12152     case SMAX:
12153       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12154       break;
12155
12156     case ZERO_EXTRACT:
12157     case SIGN_EXTRACT:
12158       if (CONST_INT_P (XEXP (rtl, 1))
12159           && CONST_INT_P (XEXP (rtl, 2))
12160           && ((unsigned) INTVAL (XEXP (rtl, 1))
12161               + (unsigned) INTVAL (XEXP (rtl, 2))
12162               <= GET_MODE_BITSIZE (mode))
12163           && GET_MODE_CLASS (mode) == MODE_INT
12164           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12165           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12166         {
12167           int shift, size;
12168           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12169                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12170           if (op0 == 0)
12171             break;
12172           if (GET_CODE (rtl) == SIGN_EXTRACT)
12173             op = DW_OP_shra;
12174           else
12175             op = DW_OP_shr;
12176           mem_loc_result = op0;
12177           size = INTVAL (XEXP (rtl, 1));
12178           shift = INTVAL (XEXP (rtl, 2));
12179           if (BITS_BIG_ENDIAN)
12180             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12181                     - shift - size;
12182           if (shift + size != (int) DWARF2_ADDR_SIZE)
12183             {
12184               add_loc_descr (&mem_loc_result,
12185                              int_loc_descriptor (DWARF2_ADDR_SIZE
12186                                                  - shift - size));
12187               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12188             }
12189           if (size != (int) DWARF2_ADDR_SIZE)
12190             {
12191               add_loc_descr (&mem_loc_result,
12192                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12193               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12194             }
12195         }
12196       break;
12197
12198     case IF_THEN_ELSE:
12199       {
12200         dw_loc_descr_ref op2, bra_node, drop_node;
12201         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12202                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12203                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12204                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12205         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12206                                   VAR_INIT_STATUS_INITIALIZED);
12207         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12208                                   VAR_INIT_STATUS_INITIALIZED);
12209         if (op0 == NULL || op1 == NULL || op2 == NULL)
12210           break;
12211
12212         mem_loc_result = op1;
12213         add_loc_descr (&mem_loc_result, op2);
12214         add_loc_descr (&mem_loc_result, op0);
12215         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12216         add_loc_descr (&mem_loc_result, bra_node);
12217         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12218         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12219         add_loc_descr (&mem_loc_result, drop_node);
12220         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12221         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12222       }
12223       break;
12224
12225     case FLOAT_EXTEND:
12226     case FLOAT_TRUNCATE:
12227     case FLOAT:
12228     case UNSIGNED_FLOAT:
12229     case FIX:
12230     case UNSIGNED_FIX:
12231       if (!dwarf_strict)
12232         {
12233           dw_die_ref type_die;
12234           dw_loc_descr_ref cvt;
12235
12236           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12237                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12238           if (op0 == NULL)
12239             break;
12240           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12241               && (GET_CODE (rtl) == FLOAT
12242                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12243                      <= DWARF2_ADDR_SIZE))
12244             {
12245               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12246                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12247               if (type_die == NULL)
12248                 break;
12249               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12250               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12251               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12252               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12253               add_loc_descr (&op0, cvt);
12254             }
12255           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12256           if (type_die == NULL)
12257             break;
12258           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12259           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12260           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12261           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12262           add_loc_descr (&op0, cvt);
12263           if (GET_MODE_CLASS (mode) == MODE_INT
12264               && (GET_CODE (rtl) == FIX
12265                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12266             {
12267               op0 = convert_descriptor_to_mode (mode, op0);
12268               if (op0 == NULL)
12269                 break;
12270             }
12271           mem_loc_result = op0;
12272         }
12273       break;
12274
12275     case CLZ:
12276     case CTZ:
12277     case FFS:
12278       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12279       break;
12280
12281     case POPCOUNT:
12282     case PARITY:
12283       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12284       break;
12285
12286     case BSWAP:
12287       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12288       break;
12289
12290     case ROTATE:
12291     case ROTATERT:
12292       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12293       break;
12294
12295     case COMPARE:
12296     case TRUNCATE:
12297       /* In theory, we could implement the above.  */
12298       /* DWARF cannot represent the unsigned compare operations
12299          natively.  */
12300     case SS_MULT:
12301     case US_MULT:
12302     case SS_DIV:
12303     case US_DIV:
12304     case SS_PLUS:
12305     case US_PLUS:
12306     case SS_MINUS:
12307     case US_MINUS:
12308     case SS_NEG:
12309     case US_NEG:
12310     case SS_ABS:
12311     case SS_ASHIFT:
12312     case US_ASHIFT:
12313     case SS_TRUNCATE:
12314     case US_TRUNCATE:
12315     case UNORDERED:
12316     case ORDERED:
12317     case UNEQ:
12318     case UNGE:
12319     case UNGT:
12320     case UNLE:
12321     case UNLT:
12322     case LTGT:
12323     case FRACT_CONVERT:
12324     case UNSIGNED_FRACT_CONVERT:
12325     case SAT_FRACT:
12326     case UNSIGNED_SAT_FRACT:
12327     case SQRT:
12328     case ASM_OPERANDS:
12329     case VEC_MERGE:
12330     case VEC_SELECT:
12331     case VEC_CONCAT:
12332     case VEC_DUPLICATE:
12333     case UNSPEC:
12334     case HIGH:
12335     case FMA:
12336     case STRICT_LOW_PART:
12337     case CONST_VECTOR:
12338     case CONST_FIXED:
12339     case CLRSB:
12340       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12341          can't express it in the debug info.  This can happen e.g. with some
12342          TLS UNSPECs.  */
12343       break;
12344
12345     case CONST_STRING:
12346       resolve_one_addr (&rtl, NULL);
12347       goto symref;
12348
12349     default:
12350 #ifdef ENABLE_CHECKING
12351       print_rtl (stderr, rtl);
12352       gcc_unreachable ();
12353 #else
12354       break;
12355 #endif
12356     }
12357
12358   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12359     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12360
12361   return mem_loc_result;
12362 }
12363
12364 /* Return a descriptor that describes the concatenation of two locations.
12365    This is typically a complex variable.  */
12366
12367 static dw_loc_descr_ref
12368 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12369 {
12370   dw_loc_descr_ref cc_loc_result = NULL;
12371   dw_loc_descr_ref x0_ref
12372     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12373   dw_loc_descr_ref x1_ref
12374     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12375
12376   if (x0_ref == 0 || x1_ref == 0)
12377     return 0;
12378
12379   cc_loc_result = x0_ref;
12380   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12381
12382   add_loc_descr (&cc_loc_result, x1_ref);
12383   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12384
12385   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12386     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12387
12388   return cc_loc_result;
12389 }
12390
12391 /* Return a descriptor that describes the concatenation of N
12392    locations.  */
12393
12394 static dw_loc_descr_ref
12395 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12396 {
12397   unsigned int i;
12398   dw_loc_descr_ref cc_loc_result = NULL;
12399   unsigned int n = XVECLEN (concatn, 0);
12400
12401   for (i = 0; i < n; ++i)
12402     {
12403       dw_loc_descr_ref ref;
12404       rtx x = XVECEXP (concatn, 0, i);
12405
12406       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12407       if (ref == NULL)
12408         return NULL;
12409
12410       add_loc_descr (&cc_loc_result, ref);
12411       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12412     }
12413
12414   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12415     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12416
12417   return cc_loc_result;
12418 }
12419
12420 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12421    for DEBUG_IMPLICIT_PTR RTL.  */
12422
12423 static dw_loc_descr_ref
12424 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12425 {
12426   dw_loc_descr_ref ret;
12427   dw_die_ref ref;
12428
12429   if (dwarf_strict)
12430     return NULL;
12431   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12432               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12433               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12434   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12435   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12436   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12437   if (ref)
12438     {
12439       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12440       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12441       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12442     }
12443   else
12444     {
12445       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12446       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12447     }
12448   return ret;
12449 }
12450
12451 /* Output a proper Dwarf location descriptor for a variable or parameter
12452    which is either allocated in a register or in a memory location.  For a
12453    register, we just generate an OP_REG and the register number.  For a
12454    memory location we provide a Dwarf postfix expression describing how to
12455    generate the (dynamic) address of the object onto the address stack.
12456
12457    MODE is mode of the decl if this loc_descriptor is going to be used in
12458    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12459    allowed, VOIDmode otherwise.
12460
12461    If we don't know how to describe it, return 0.  */
12462
12463 static dw_loc_descr_ref
12464 loc_descriptor (rtx rtl, enum machine_mode mode,
12465                 enum var_init_status initialized)
12466 {
12467   dw_loc_descr_ref loc_result = NULL;
12468
12469   switch (GET_CODE (rtl))
12470     {
12471     case SUBREG:
12472       /* The case of a subreg may arise when we have a local (register)
12473          variable or a formal (register) parameter which doesn't quite fill
12474          up an entire register.  For now, just assume that it is
12475          legitimate to make the Dwarf info refer to the whole register which
12476          contains the given subreg.  */
12477       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12478         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12479       else
12480         goto do_default;
12481       break;
12482
12483     case REG:
12484       loc_result = reg_loc_descriptor (rtl, initialized);
12485       break;
12486
12487     case MEM:
12488       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12489                                        GET_MODE (rtl), initialized);
12490       if (loc_result == NULL)
12491         loc_result = tls_mem_loc_descriptor (rtl);
12492       if (loc_result == NULL)
12493         {
12494           rtx new_rtl = avoid_constant_pool_reference (rtl);
12495           if (new_rtl != rtl)
12496             loc_result = loc_descriptor (new_rtl, mode, initialized);
12497         }
12498       break;
12499
12500     case CONCAT:
12501       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12502                                           initialized);
12503       break;
12504
12505     case CONCATN:
12506       loc_result = concatn_loc_descriptor (rtl, initialized);
12507       break;
12508
12509     case VAR_LOCATION:
12510       /* Single part.  */
12511       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12512         {
12513           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12514           if (GET_CODE (loc) == EXPR_LIST)
12515             loc = XEXP (loc, 0);
12516           loc_result = loc_descriptor (loc, mode, initialized);
12517           break;
12518         }
12519
12520       rtl = XEXP (rtl, 1);
12521       /* FALLTHRU */
12522
12523     case PARALLEL:
12524       {
12525         rtvec par_elems = XVEC (rtl, 0);
12526         int num_elem = GET_NUM_ELEM (par_elems);
12527         enum machine_mode mode;
12528         int i;
12529
12530         /* Create the first one, so we have something to add to.  */
12531         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12532                                      VOIDmode, initialized);
12533         if (loc_result == NULL)
12534           return NULL;
12535         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12536         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12537         for (i = 1; i < num_elem; i++)
12538           {
12539             dw_loc_descr_ref temp;
12540
12541             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12542                                    VOIDmode, initialized);
12543             if (temp == NULL)
12544               return NULL;
12545             add_loc_descr (&loc_result, temp);
12546             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12547             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12548           }
12549       }
12550       break;
12551
12552     case CONST_INT:
12553       if (mode != VOIDmode && mode != BLKmode)
12554         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12555                                                     INTVAL (rtl));
12556       break;
12557
12558     case CONST_DOUBLE:
12559       if (mode == VOIDmode)
12560         mode = GET_MODE (rtl);
12561
12562       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12563         {
12564           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12565
12566           /* Note that a CONST_DOUBLE rtx could represent either an integer
12567              or a floating-point constant.  A CONST_DOUBLE is used whenever
12568              the constant requires more than one word in order to be
12569              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12570           loc_result = new_loc_descr (DW_OP_implicit_value,
12571                                       GET_MODE_SIZE (mode), 0);
12572           if (SCALAR_FLOAT_MODE_P (mode))
12573             {
12574               unsigned int length = GET_MODE_SIZE (mode);
12575               unsigned char *array
12576                   = (unsigned char*) ggc_alloc_atomic (length);
12577
12578               insert_float (rtl, array);
12579               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12580               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12581               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12582               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12583             }
12584           else
12585             {
12586               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12587               loc_result->dw_loc_oprnd2.v.val_double
12588                 = rtx_to_double_int (rtl);
12589             }
12590         }
12591       break;
12592
12593     case CONST_VECTOR:
12594       if (mode == VOIDmode)
12595         mode = GET_MODE (rtl);
12596
12597       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12598         {
12599           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12600           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12601           unsigned char *array = (unsigned char *)
12602             ggc_alloc_atomic (length * elt_size);
12603           unsigned int i;
12604           unsigned char *p;
12605
12606           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12607           switch (GET_MODE_CLASS (mode))
12608             {
12609             case MODE_VECTOR_INT:
12610               for (i = 0, p = array; i < length; i++, p += elt_size)
12611                 {
12612                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12613                   double_int val = rtx_to_double_int (elt);
12614
12615                   if (elt_size <= sizeof (HOST_WIDE_INT))
12616                     insert_int (double_int_to_shwi (val), elt_size, p);
12617                   else
12618                     {
12619                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12620                       insert_double (val, p);
12621                     }
12622                 }
12623               break;
12624
12625             case MODE_VECTOR_FLOAT:
12626               for (i = 0, p = array; i < length; i++, p += elt_size)
12627                 {
12628                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12629                   insert_float (elt, p);
12630                 }
12631               break;
12632
12633             default:
12634               gcc_unreachable ();
12635             }
12636
12637           loc_result = new_loc_descr (DW_OP_implicit_value,
12638                                       length * elt_size, 0);
12639           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12640           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12641           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12642           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12643         }
12644       break;
12645
12646     case CONST:
12647       if (mode == VOIDmode
12648           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12649           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12650           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12651         {
12652           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12653           break;
12654         }
12655       /* FALLTHROUGH */
12656     case SYMBOL_REF:
12657       if (!const_ok_for_output (rtl))
12658         break;
12659     case LABEL_REF:
12660       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12661           && (dwarf_version >= 4 || !dwarf_strict))
12662         {
12663           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12664           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12665           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12666           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12667           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12668         }
12669       break;
12670
12671     case DEBUG_IMPLICIT_PTR:
12672       loc_result = implicit_ptr_descriptor (rtl, 0);
12673       break;
12674
12675     case PLUS:
12676       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12677           && CONST_INT_P (XEXP (rtl, 1)))
12678         {
12679           loc_result
12680             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12681           break;
12682         }
12683       /* FALLTHRU */
12684     do_default:
12685     default:
12686       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12687            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12688            && dwarf_version >= 4)
12689           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12690         {
12691           /* Value expression.  */
12692           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12693           if (loc_result)
12694             add_loc_descr (&loc_result,
12695                            new_loc_descr (DW_OP_stack_value, 0, 0));
12696         }
12697       break;
12698     }
12699
12700   return loc_result;
12701 }
12702
12703 /* We need to figure out what section we should use as the base for the
12704    address ranges where a given location is valid.
12705    1. If this particular DECL has a section associated with it, use that.
12706    2. If this function has a section associated with it, use that.
12707    3. Otherwise, use the text section.
12708    XXX: If you split a variable across multiple sections, we won't notice.  */
12709
12710 static const char *
12711 secname_for_decl (const_tree decl)
12712 {
12713   const char *secname;
12714
12715   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12716     {
12717       tree sectree = DECL_SECTION_NAME (decl);
12718       secname = TREE_STRING_POINTER (sectree);
12719     }
12720   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12721     {
12722       tree sectree = DECL_SECTION_NAME (current_function_decl);
12723       secname = TREE_STRING_POINTER (sectree);
12724     }
12725   else if (cfun && in_cold_section_p)
12726     secname = crtl->subsections.cold_section_label;
12727   else
12728     secname = text_section_label;
12729
12730   return secname;
12731 }
12732
12733 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12734
12735 static bool
12736 decl_by_reference_p (tree decl)
12737 {
12738   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12739            || TREE_CODE (decl) == VAR_DECL)
12740           && DECL_BY_REFERENCE (decl));
12741 }
12742
12743 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12744    for VARLOC.  */
12745
12746 static dw_loc_descr_ref
12747 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12748                enum var_init_status initialized)
12749 {
12750   int have_address = 0;
12751   dw_loc_descr_ref descr;
12752   enum machine_mode mode;
12753
12754   if (want_address != 2)
12755     {
12756       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12757       /* Single part.  */
12758       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12759         {
12760           varloc = PAT_VAR_LOCATION_LOC (varloc);
12761           if (GET_CODE (varloc) == EXPR_LIST)
12762             varloc = XEXP (varloc, 0);
12763           mode = GET_MODE (varloc);
12764           if (MEM_P (varloc))
12765             {
12766               rtx addr = XEXP (varloc, 0);
12767               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12768                                           mode, initialized);
12769               if (descr)
12770                 have_address = 1;
12771               else
12772                 {
12773                   rtx x = avoid_constant_pool_reference (varloc);
12774                   if (x != varloc)
12775                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12776                                                 initialized);
12777                 }
12778             }
12779           else
12780             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12781         }
12782       else
12783         return 0;
12784     }
12785   else
12786     {
12787       if (GET_CODE (varloc) == VAR_LOCATION)
12788         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12789       else
12790         mode = DECL_MODE (loc);
12791       descr = loc_descriptor (varloc, mode, initialized);
12792       have_address = 1;
12793     }
12794
12795   if (!descr)
12796     return 0;
12797
12798   if (want_address == 2 && !have_address
12799       && (dwarf_version >= 4 || !dwarf_strict))
12800     {
12801       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12802         {
12803           expansion_failed (loc, NULL_RTX,
12804                             "DWARF address size mismatch");
12805           return 0;
12806         }
12807       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12808       have_address = 1;
12809     }
12810   /* Show if we can't fill the request for an address.  */
12811   if (want_address && !have_address)
12812     {
12813       expansion_failed (loc, NULL_RTX,
12814                         "Want address and only have value");
12815       return 0;
12816     }
12817
12818   /* If we've got an address and don't want one, dereference.  */
12819   if (!want_address && have_address)
12820     {
12821       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12822       enum dwarf_location_atom op;
12823
12824       if (size > DWARF2_ADDR_SIZE || size == -1)
12825         {
12826           expansion_failed (loc, NULL_RTX,
12827                             "DWARF address size mismatch");
12828           return 0;
12829         }
12830       else if (size == DWARF2_ADDR_SIZE)
12831         op = DW_OP_deref;
12832       else
12833         op = DW_OP_deref_size;
12834
12835       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12836     }
12837
12838   return descr;
12839 }
12840
12841 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12842    if it is not possible.  */
12843
12844 static dw_loc_descr_ref
12845 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12846 {
12847   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12848     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12849   else if (dwarf_version >= 3 || !dwarf_strict)
12850     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12851   else
12852     return NULL;
12853 }
12854
12855 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12856    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12857
12858 static dw_loc_descr_ref
12859 dw_sra_loc_expr (tree decl, rtx loc)
12860 {
12861   rtx p;
12862   unsigned int padsize = 0;
12863   dw_loc_descr_ref descr, *descr_tail;
12864   unsigned HOST_WIDE_INT decl_size;
12865   rtx varloc;
12866   enum var_init_status initialized;
12867
12868   if (DECL_SIZE (decl) == NULL
12869       || !host_integerp (DECL_SIZE (decl), 1))
12870     return NULL;
12871
12872   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12873   descr = NULL;
12874   descr_tail = &descr;
12875
12876   for (p = loc; p; p = XEXP (p, 1))
12877     {
12878       unsigned int bitsize = decl_piece_bitsize (p);
12879       rtx loc_note = *decl_piece_varloc_ptr (p);
12880       dw_loc_descr_ref cur_descr;
12881       dw_loc_descr_ref *tail, last = NULL;
12882       unsigned int opsize = 0;
12883
12884       if (loc_note == NULL_RTX
12885           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12886         {
12887           padsize += bitsize;
12888           continue;
12889         }
12890       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12891       varloc = NOTE_VAR_LOCATION (loc_note);
12892       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12893       if (cur_descr == NULL)
12894         {
12895           padsize += bitsize;
12896           continue;
12897         }
12898
12899       /* Check that cur_descr either doesn't use
12900          DW_OP_*piece operations, or their sum is equal
12901          to bitsize.  Otherwise we can't embed it.  */
12902       for (tail = &cur_descr; *tail != NULL;
12903            tail = &(*tail)->dw_loc_next)
12904         if ((*tail)->dw_loc_opc == DW_OP_piece)
12905           {
12906             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12907                       * BITS_PER_UNIT;
12908             last = *tail;
12909           }
12910         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12911           {
12912             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12913             last = *tail;
12914           }
12915
12916       if (last != NULL && opsize != bitsize)
12917         {
12918           padsize += bitsize;
12919           continue;
12920         }
12921
12922       /* If there is a hole, add DW_OP_*piece after empty DWARF
12923          expression, which means that those bits are optimized out.  */
12924       if (padsize)
12925         {
12926           if (padsize > decl_size)
12927             return NULL;
12928           decl_size -= padsize;
12929           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12930           if (*descr_tail == NULL)
12931             return NULL;
12932           descr_tail = &(*descr_tail)->dw_loc_next;
12933           padsize = 0;
12934         }
12935       *descr_tail = cur_descr;
12936       descr_tail = tail;
12937       if (bitsize > decl_size)
12938         return NULL;
12939       decl_size -= bitsize;
12940       if (last == NULL)
12941         {
12942           HOST_WIDE_INT offset = 0;
12943           if (GET_CODE (varloc) == VAR_LOCATION
12944               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12945             {
12946               varloc = PAT_VAR_LOCATION_LOC (varloc);
12947               if (GET_CODE (varloc) == EXPR_LIST)
12948                 varloc = XEXP (varloc, 0);
12949             }
12950           do 
12951             {
12952               if (GET_CODE (varloc) == CONST
12953                   || GET_CODE (varloc) == SIGN_EXTEND
12954                   || GET_CODE (varloc) == ZERO_EXTEND)
12955                 varloc = XEXP (varloc, 0);
12956               else if (GET_CODE (varloc) == SUBREG)
12957                 varloc = SUBREG_REG (varloc);
12958               else
12959                 break;
12960             }
12961           while (1);
12962           /* DW_OP_bit_size offset should be zero for register
12963              or implicit location descriptions and empty location
12964              descriptions, but for memory addresses needs big endian
12965              adjustment.  */
12966           if (MEM_P (varloc))
12967             {
12968               unsigned HOST_WIDE_INT memsize
12969                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12970               if (memsize != bitsize)
12971                 {
12972                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12973                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12974                     return NULL;
12975                   if (memsize < bitsize)
12976                     return NULL;
12977                   if (BITS_BIG_ENDIAN)
12978                     offset = memsize - bitsize;
12979                 }
12980             }
12981
12982           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12983           if (*descr_tail == NULL)
12984             return NULL;
12985           descr_tail = &(*descr_tail)->dw_loc_next;
12986         }
12987     }
12988
12989   /* If there were any non-empty expressions, add padding till the end of
12990      the decl.  */
12991   if (descr != NULL && decl_size != 0)
12992     {
12993       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12994       if (*descr_tail == NULL)
12995         return NULL;
12996     }
12997   return descr;
12998 }
12999
13000 /* Return the dwarf representation of the location list LOC_LIST of
13001    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13002    function.  */
13003
13004 static dw_loc_list_ref
13005 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13006 {
13007   const char *endname, *secname;
13008   rtx varloc;
13009   enum var_init_status initialized;
13010   struct var_loc_node *node;
13011   dw_loc_descr_ref descr;
13012   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13013   dw_loc_list_ref list = NULL;
13014   dw_loc_list_ref *listp = &list;
13015
13016   /* Now that we know what section we are using for a base,
13017      actually construct the list of locations.
13018      The first location information is what is passed to the
13019      function that creates the location list, and the remaining
13020      locations just get added on to that list.
13021      Note that we only know the start address for a location
13022      (IE location changes), so to build the range, we use
13023      the range [current location start, next location start].
13024      This means we have to special case the last node, and generate
13025      a range of [last location start, end of function label].  */
13026
13027   secname = secname_for_decl (decl);
13028
13029   for (node = loc_list->first; node; node = node->next)
13030     if (GET_CODE (node->loc) == EXPR_LIST
13031         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13032       {
13033         if (GET_CODE (node->loc) == EXPR_LIST)
13034           {
13035             /* This requires DW_OP_{,bit_}piece, which is not usable
13036                inside DWARF expressions.  */
13037             if (want_address != 2)
13038               continue;
13039             descr = dw_sra_loc_expr (decl, node->loc);
13040             if (descr == NULL)
13041               continue;
13042           }
13043         else
13044           {
13045             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13046             varloc = NOTE_VAR_LOCATION (node->loc);
13047             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13048           }
13049         if (descr)
13050           {
13051             bool range_across_switch = false;
13052             /* If section switch happens in between node->label
13053                and node->next->label (or end of function) and
13054                we can't emit it as a single entry list,
13055                emit two ranges, first one ending at the end
13056                of first partition and second one starting at the
13057                beginning of second partition.  */
13058             if (node == loc_list->last_before_switch
13059                 && (node != loc_list->first || loc_list->first->next)
13060                 && current_function_decl)
13061               {
13062                 endname = cfun->fde->dw_fde_end;
13063                 range_across_switch = true;
13064               }
13065             /* The variable has a location between NODE->LABEL and
13066                NODE->NEXT->LABEL.  */
13067             else if (node->next)
13068               endname = node->next->label;
13069             /* If the variable has a location at the last label
13070                it keeps its location until the end of function.  */
13071             else if (!current_function_decl)
13072               endname = text_end_label;
13073             else
13074               {
13075                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13076                                              current_function_funcdef_no);
13077                 endname = ggc_strdup (label_id);
13078               }
13079
13080             *listp = new_loc_list (descr, node->label, endname, secname);
13081             if (TREE_CODE (decl) == PARM_DECL
13082                 && node == loc_list->first
13083                 && GET_CODE (node->loc) == NOTE
13084                 && strcmp (node->label, endname) == 0)
13085               (*listp)->force = true;
13086             listp = &(*listp)->dw_loc_next;
13087
13088             if (range_across_switch)
13089               {
13090                 if (GET_CODE (node->loc) == EXPR_LIST)
13091                   descr = dw_sra_loc_expr (decl, node->loc);
13092                 else
13093                   {
13094                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13095                     varloc = NOTE_VAR_LOCATION (node->loc);
13096                     descr = dw_loc_list_1 (decl, varloc, want_address,
13097                                            initialized);
13098                   }
13099                 gcc_assert (descr);
13100                 /* The variable has a location between NODE->LABEL and
13101                    NODE->NEXT->LABEL.  */
13102                 if (node->next)
13103                   endname = node->next->label;
13104                 else
13105                   endname = cfun->fde->dw_fde_second_end;
13106                 *listp = new_loc_list (descr,
13107                                        cfun->fde->dw_fde_second_begin,
13108                                        endname, secname);
13109                 listp = &(*listp)->dw_loc_next;
13110               }
13111           }
13112       }
13113
13114   /* Try to avoid the overhead of a location list emitting a location
13115      expression instead, but only if we didn't have more than one
13116      location entry in the first place.  If some entries were not
13117      representable, we don't want to pretend a single entry that was
13118      applies to the entire scope in which the variable is
13119      available.  */
13120   if (list && loc_list->first->next)
13121     gen_llsym (list);
13122
13123   return list;
13124 }
13125
13126 /* Return if the loc_list has only single element and thus can be represented
13127    as location description.   */
13128
13129 static bool
13130 single_element_loc_list_p (dw_loc_list_ref list)
13131 {
13132   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13133   return !list->ll_symbol;
13134 }
13135
13136 /* To each location in list LIST add loc descr REF.  */
13137
13138 static void
13139 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13140 {
13141   dw_loc_descr_ref copy;
13142   add_loc_descr (&list->expr, ref);
13143   list = list->dw_loc_next;
13144   while (list)
13145     {
13146       copy = ggc_alloc_dw_loc_descr_node ();
13147       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13148       add_loc_descr (&list->expr, copy);
13149       while (copy->dw_loc_next)
13150         {
13151           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13152           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13153           copy->dw_loc_next = new_copy;
13154           copy = new_copy;
13155         }
13156       list = list->dw_loc_next;
13157     }
13158 }
13159
13160 /* Given two lists RET and LIST
13161    produce location list that is result of adding expression in LIST
13162    to expression in RET on each possition in program.
13163    Might be destructive on both RET and LIST.
13164
13165    TODO: We handle only simple cases of RET or LIST having at most one
13166    element. General case would inolve sorting the lists in program order
13167    and merging them that will need some additional work.
13168    Adding that will improve quality of debug info especially for SRA-ed
13169    structures.  */
13170
13171 static void
13172 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13173 {
13174   if (!list)
13175     return;
13176   if (!*ret)
13177     {
13178       *ret = list;
13179       return;
13180     }
13181   if (!list->dw_loc_next)
13182     {
13183       add_loc_descr_to_each (*ret, list->expr);
13184       return;
13185     }
13186   if (!(*ret)->dw_loc_next)
13187     {
13188       add_loc_descr_to_each (list, (*ret)->expr);
13189       *ret = list;
13190       return;
13191     }
13192   expansion_failed (NULL_TREE, NULL_RTX,
13193                     "Don't know how to merge two non-trivial"
13194                     " location lists.\n");
13195   *ret = NULL;
13196   return;
13197 }
13198
13199 /* LOC is constant expression.  Try a luck, look it up in constant
13200    pool and return its loc_descr of its address.  */
13201
13202 static dw_loc_descr_ref
13203 cst_pool_loc_descr (tree loc)
13204 {
13205   /* Get an RTL for this, if something has been emitted.  */
13206   rtx rtl = lookup_constant_def (loc);
13207
13208   if (!rtl || !MEM_P (rtl))
13209     {
13210       gcc_assert (!rtl);
13211       return 0;
13212     }
13213   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13214
13215   /* TODO: We might get more coverage if we was actually delaying expansion
13216      of all expressions till end of compilation when constant pools are fully
13217      populated.  */
13218   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13219     {
13220       expansion_failed (loc, NULL_RTX,
13221                         "CST value in contant pool but not marked.");
13222       return 0;
13223     }
13224   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13225                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13226 }
13227
13228 /* Return dw_loc_list representing address of addr_expr LOC
13229    by looking for innder INDIRECT_REF expression and turing it
13230    into simple arithmetics.  */
13231
13232 static dw_loc_list_ref
13233 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13234 {
13235   tree obj, offset;
13236   HOST_WIDE_INT bitsize, bitpos, bytepos;
13237   enum machine_mode mode;
13238   int volatilep;
13239   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13240   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13241
13242   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13243                              &bitsize, &bitpos, &offset, &mode,
13244                              &unsignedp, &volatilep, false);
13245   STRIP_NOPS (obj);
13246   if (bitpos % BITS_PER_UNIT)
13247     {
13248       expansion_failed (loc, NULL_RTX, "bitfield access");
13249       return 0;
13250     }
13251   if (!INDIRECT_REF_P (obj))
13252     {
13253       expansion_failed (obj,
13254                         NULL_RTX, "no indirect ref in inner refrence");
13255       return 0;
13256     }
13257   if (!offset && !bitpos)
13258     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13259   else if (toplev
13260            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13261            && (dwarf_version >= 4 || !dwarf_strict))
13262     {
13263       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13264       if (!list_ret)
13265         return 0;
13266       if (offset)
13267         {
13268           /* Variable offset.  */
13269           list_ret1 = loc_list_from_tree (offset, 0);
13270           if (list_ret1 == 0)
13271             return 0;
13272           add_loc_list (&list_ret, list_ret1);
13273           if (!list_ret)
13274             return 0;
13275           add_loc_descr_to_each (list_ret,
13276                                  new_loc_descr (DW_OP_plus, 0, 0));
13277         }
13278       bytepos = bitpos / BITS_PER_UNIT;
13279       if (bytepos > 0)
13280         add_loc_descr_to_each (list_ret,
13281                                new_loc_descr (DW_OP_plus_uconst,
13282                                               bytepos, 0));
13283       else if (bytepos < 0)
13284         loc_list_plus_const (list_ret, bytepos);
13285       add_loc_descr_to_each (list_ret,
13286                              new_loc_descr (DW_OP_stack_value, 0, 0));
13287     }
13288   return list_ret;
13289 }
13290
13291
13292 /* Generate Dwarf location list representing LOC.
13293    If WANT_ADDRESS is false, expression computing LOC will be computed
13294    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13295    if WANT_ADDRESS is 2, expression computing address useable in location
13296      will be returned (i.e. DW_OP_reg can be used
13297      to refer to register values).  */
13298
13299 static dw_loc_list_ref
13300 loc_list_from_tree (tree loc, int want_address)
13301 {
13302   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13303   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13304   int have_address = 0;
13305   enum dwarf_location_atom op;
13306
13307   /* ??? Most of the time we do not take proper care for sign/zero
13308      extending the values properly.  Hopefully this won't be a real
13309      problem...  */
13310
13311   switch (TREE_CODE (loc))
13312     {
13313     case ERROR_MARK:
13314       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13315       return 0;
13316
13317     case PLACEHOLDER_EXPR:
13318       /* This case involves extracting fields from an object to determine the
13319          position of other fields.  We don't try to encode this here.  The
13320          only user of this is Ada, which encodes the needed information using
13321          the names of types.  */
13322       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13323       return 0;
13324
13325     case CALL_EXPR:
13326       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13327       /* There are no opcodes for these operations.  */
13328       return 0;
13329
13330     case PREINCREMENT_EXPR:
13331     case PREDECREMENT_EXPR:
13332     case POSTINCREMENT_EXPR:
13333     case POSTDECREMENT_EXPR:
13334       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13335       /* There are no opcodes for these operations.  */
13336       return 0;
13337
13338     case ADDR_EXPR:
13339       /* If we already want an address, see if there is INDIRECT_REF inside
13340          e.g. for &this->field.  */
13341       if (want_address)
13342         {
13343           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13344                        (loc, want_address == 2);
13345           if (list_ret)
13346             have_address = 1;
13347           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13348                    && (ret = cst_pool_loc_descr (loc)))
13349             have_address = 1;
13350         }
13351         /* Otherwise, process the argument and look for the address.  */
13352       if (!list_ret && !ret)
13353         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13354       else
13355         {
13356           if (want_address)
13357             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13358           return NULL;
13359         }
13360       break;
13361
13362     case VAR_DECL:
13363       if (DECL_THREAD_LOCAL_P (loc))
13364         {
13365           rtx rtl;
13366           enum dwarf_location_atom first_op;
13367           enum dwarf_location_atom second_op;
13368           bool dtprel = false;
13369
13370           if (targetm.have_tls)
13371             {
13372               /* If this is not defined, we have no way to emit the
13373                  data.  */
13374               if (!targetm.asm_out.output_dwarf_dtprel)
13375                 return 0;
13376
13377                /* The way DW_OP_GNU_push_tls_address is specified, we
13378                   can only look up addresses of objects in the current
13379                   module.  We used DW_OP_addr as first op, but that's
13380                   wrong, because DW_OP_addr is relocated by the debug
13381                   info consumer, while DW_OP_GNU_push_tls_address
13382                   operand shouldn't be.  */
13383               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13384                 return 0;
13385               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13386               dtprel = true;
13387               second_op = DW_OP_GNU_push_tls_address;
13388             }
13389           else
13390             {
13391               if (!targetm.emutls.debug_form_tls_address
13392                   || !(dwarf_version >= 3 || !dwarf_strict))
13393                 return 0;
13394               /* We stuffed the control variable into the DECL_VALUE_EXPR
13395                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13396                  no longer appear in gimple code.  We used the control
13397                  variable in specific so that we could pick it up here.  */
13398               loc = DECL_VALUE_EXPR (loc);
13399               first_op = DW_OP_addr;
13400               second_op = DW_OP_form_tls_address;
13401             }
13402
13403           rtl = rtl_for_decl_location (loc);
13404           if (rtl == NULL_RTX)
13405             return 0;
13406
13407           if (!MEM_P (rtl))
13408             return 0;
13409           rtl = XEXP (rtl, 0);
13410           if (! CONSTANT_P (rtl))
13411             return 0;
13412
13413           ret = new_loc_descr (first_op, 0, 0);
13414           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13415           ret->dw_loc_oprnd1.v.val_addr = rtl;
13416           ret->dtprel = dtprel;
13417
13418           ret1 = new_loc_descr (second_op, 0, 0);
13419           add_loc_descr (&ret, ret1);
13420
13421           have_address = 1;
13422           break;
13423         }
13424       /* FALLTHRU */
13425
13426     case PARM_DECL:
13427     case RESULT_DECL:
13428       if (DECL_HAS_VALUE_EXPR_P (loc))
13429         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13430                                    want_address);
13431       /* FALLTHRU */
13432
13433     case FUNCTION_DECL:
13434       {
13435         rtx rtl;
13436         var_loc_list *loc_list = lookup_decl_loc (loc);
13437
13438         if (loc_list && loc_list->first)
13439           {
13440             list_ret = dw_loc_list (loc_list, loc, want_address);
13441             have_address = want_address != 0;
13442             break;
13443           }
13444         rtl = rtl_for_decl_location (loc);
13445         if (rtl == NULL_RTX)
13446           {
13447             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13448             return 0;
13449           }
13450         else if (CONST_INT_P (rtl))
13451           {
13452             HOST_WIDE_INT val = INTVAL (rtl);
13453             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13454               val &= GET_MODE_MASK (DECL_MODE (loc));
13455             ret = int_loc_descriptor (val);
13456           }
13457         else if (GET_CODE (rtl) == CONST_STRING)
13458           {
13459             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13460             return 0;
13461           }
13462         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13463           {
13464             ret = new_loc_descr (DW_OP_addr, 0, 0);
13465             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13466             ret->dw_loc_oprnd1.v.val_addr = rtl;
13467           }
13468         else
13469           {
13470             enum machine_mode mode, mem_mode;
13471
13472             /* Certain constructs can only be represented at top-level.  */
13473             if (want_address == 2)
13474               {
13475                 ret = loc_descriptor (rtl, VOIDmode,
13476                                       VAR_INIT_STATUS_INITIALIZED);
13477                 have_address = 1;
13478               }
13479             else
13480               {
13481                 mode = GET_MODE (rtl);
13482                 mem_mode = VOIDmode;
13483                 if (MEM_P (rtl))
13484                   {
13485                     mem_mode = mode;
13486                     mode = get_address_mode (rtl);
13487                     rtl = XEXP (rtl, 0);
13488                     have_address = 1;
13489                   }
13490                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13491                                           VAR_INIT_STATUS_INITIALIZED);
13492               }
13493             if (!ret)
13494               expansion_failed (loc, rtl,
13495                                 "failed to produce loc descriptor for rtl");
13496           }
13497       }
13498       break;
13499
13500     case MEM_REF:
13501       /* ??? FIXME.  */
13502       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13503         return 0;
13504       /* Fallthru.  */
13505     case INDIRECT_REF:
13506       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13507       have_address = 1;
13508       break;
13509
13510     case COMPOUND_EXPR:
13511       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13512
13513     CASE_CONVERT:
13514     case VIEW_CONVERT_EXPR:
13515     case SAVE_EXPR:
13516     case MODIFY_EXPR:
13517       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13518
13519     case COMPONENT_REF:
13520     case BIT_FIELD_REF:
13521     case ARRAY_REF:
13522     case ARRAY_RANGE_REF:
13523     case REALPART_EXPR:
13524     case IMAGPART_EXPR:
13525       {
13526         tree obj, offset;
13527         HOST_WIDE_INT bitsize, bitpos, bytepos;
13528         enum machine_mode mode;
13529         int volatilep;
13530         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13531
13532         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13533                                    &unsignedp, &volatilep, false);
13534
13535         gcc_assert (obj != loc);
13536
13537         list_ret = loc_list_from_tree (obj,
13538                                        want_address == 2
13539                                        && !bitpos && !offset ? 2 : 1);
13540         /* TODO: We can extract value of the small expression via shifting even
13541            for nonzero bitpos.  */
13542         if (list_ret == 0)
13543           return 0;
13544         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13545           {
13546             expansion_failed (loc, NULL_RTX,
13547                               "bitfield access");
13548             return 0;
13549           }
13550
13551         if (offset != NULL_TREE)
13552           {
13553             /* Variable offset.  */
13554             list_ret1 = loc_list_from_tree (offset, 0);
13555             if (list_ret1 == 0)
13556               return 0;
13557             add_loc_list (&list_ret, list_ret1);
13558             if (!list_ret)
13559               return 0;
13560             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13561           }
13562
13563         bytepos = bitpos / BITS_PER_UNIT;
13564         if (bytepos > 0)
13565           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13566         else if (bytepos < 0)
13567           loc_list_plus_const (list_ret, bytepos);
13568
13569         have_address = 1;
13570         break;
13571       }
13572
13573     case INTEGER_CST:
13574       if ((want_address || !host_integerp (loc, 0))
13575           && (ret = cst_pool_loc_descr (loc)))
13576         have_address = 1;
13577       else if (want_address == 2
13578                && host_integerp (loc, 0)
13579                && (ret = address_of_int_loc_descriptor
13580                            (int_size_in_bytes (TREE_TYPE (loc)),
13581                             tree_low_cst (loc, 0))))
13582         have_address = 1;
13583       else if (host_integerp (loc, 0))
13584         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13585       else
13586         {
13587           expansion_failed (loc, NULL_RTX,
13588                             "Integer operand is not host integer");
13589           return 0;
13590         }
13591       break;
13592
13593     case CONSTRUCTOR:
13594     case REAL_CST:
13595     case STRING_CST:
13596     case COMPLEX_CST:
13597       if ((ret = cst_pool_loc_descr (loc)))
13598         have_address = 1;
13599       else
13600       /* We can construct small constants here using int_loc_descriptor.  */
13601         expansion_failed (loc, NULL_RTX,
13602                           "constructor or constant not in constant pool");
13603       break;
13604
13605     case TRUTH_AND_EXPR:
13606     case TRUTH_ANDIF_EXPR:
13607     case BIT_AND_EXPR:
13608       op = DW_OP_and;
13609       goto do_binop;
13610
13611     case TRUTH_XOR_EXPR:
13612     case BIT_XOR_EXPR:
13613       op = DW_OP_xor;
13614       goto do_binop;
13615
13616     case TRUTH_OR_EXPR:
13617     case TRUTH_ORIF_EXPR:
13618     case BIT_IOR_EXPR:
13619       op = DW_OP_or;
13620       goto do_binop;
13621
13622     case FLOOR_DIV_EXPR:
13623     case CEIL_DIV_EXPR:
13624     case ROUND_DIV_EXPR:
13625     case TRUNC_DIV_EXPR:
13626       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13627         return 0;
13628       op = DW_OP_div;
13629       goto do_binop;
13630
13631     case MINUS_EXPR:
13632       op = DW_OP_minus;
13633       goto do_binop;
13634
13635     case FLOOR_MOD_EXPR:
13636     case CEIL_MOD_EXPR:
13637     case ROUND_MOD_EXPR:
13638     case TRUNC_MOD_EXPR:
13639       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13640         {
13641           op = DW_OP_mod;
13642           goto do_binop;
13643         }
13644       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13645       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13646       if (list_ret == 0 || list_ret1 == 0)
13647         return 0;
13648
13649       add_loc_list (&list_ret, list_ret1);
13650       if (list_ret == 0)
13651         return 0;
13652       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13653       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13654       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13655       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13656       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13657       break;
13658
13659     case MULT_EXPR:
13660       op = DW_OP_mul;
13661       goto do_binop;
13662
13663     case LSHIFT_EXPR:
13664       op = DW_OP_shl;
13665       goto do_binop;
13666
13667     case RSHIFT_EXPR:
13668       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13669       goto do_binop;
13670
13671     case POINTER_PLUS_EXPR:
13672     case PLUS_EXPR:
13673       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13674         {
13675           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13676           if (list_ret == 0)
13677             return 0;
13678
13679           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13680           break;
13681         }
13682
13683       op = DW_OP_plus;
13684       goto do_binop;
13685
13686     case LE_EXPR:
13687       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13688         return 0;
13689
13690       op = DW_OP_le;
13691       goto do_binop;
13692
13693     case GE_EXPR:
13694       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13695         return 0;
13696
13697       op = DW_OP_ge;
13698       goto do_binop;
13699
13700     case LT_EXPR:
13701       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13702         return 0;
13703
13704       op = DW_OP_lt;
13705       goto do_binop;
13706
13707     case GT_EXPR:
13708       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13709         return 0;
13710
13711       op = DW_OP_gt;
13712       goto do_binop;
13713
13714     case EQ_EXPR:
13715       op = DW_OP_eq;
13716       goto do_binop;
13717
13718     case NE_EXPR:
13719       op = DW_OP_ne;
13720       goto do_binop;
13721
13722     do_binop:
13723       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13724       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13725       if (list_ret == 0 || list_ret1 == 0)
13726         return 0;
13727
13728       add_loc_list (&list_ret, list_ret1);
13729       if (list_ret == 0)
13730         return 0;
13731       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13732       break;
13733
13734     case TRUTH_NOT_EXPR:
13735     case BIT_NOT_EXPR:
13736       op = DW_OP_not;
13737       goto do_unop;
13738
13739     case ABS_EXPR:
13740       op = DW_OP_abs;
13741       goto do_unop;
13742
13743     case NEGATE_EXPR:
13744       op = DW_OP_neg;
13745       goto do_unop;
13746
13747     do_unop:
13748       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13749       if (list_ret == 0)
13750         return 0;
13751
13752       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13753       break;
13754
13755     case MIN_EXPR:
13756     case MAX_EXPR:
13757       {
13758         const enum tree_code code =
13759           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13760
13761         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13762                       build2 (code, integer_type_node,
13763                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13764                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13765       }
13766
13767       /* ... fall through ...  */
13768
13769     case COND_EXPR:
13770       {
13771         dw_loc_descr_ref lhs
13772           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13773         dw_loc_list_ref rhs
13774           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13775         dw_loc_descr_ref bra_node, jump_node, tmp;
13776
13777         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13778         if (list_ret == 0 || lhs == 0 || rhs == 0)
13779           return 0;
13780
13781         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13782         add_loc_descr_to_each (list_ret, bra_node);
13783
13784         add_loc_list (&list_ret, rhs);
13785         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13786         add_loc_descr_to_each (list_ret, jump_node);
13787
13788         add_loc_descr_to_each (list_ret, lhs);
13789         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13790         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13791
13792         /* ??? Need a node to point the skip at.  Use a nop.  */
13793         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13794         add_loc_descr_to_each (list_ret, tmp);
13795         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13796         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13797       }
13798       break;
13799
13800     case FIX_TRUNC_EXPR:
13801       return 0;
13802
13803     default:
13804       /* Leave front-end specific codes as simply unknown.  This comes
13805          up, for instance, with the C STMT_EXPR.  */
13806       if ((unsigned int) TREE_CODE (loc)
13807           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13808         {
13809           expansion_failed (loc, NULL_RTX,
13810                             "language specific tree node");
13811           return 0;
13812         }
13813
13814 #ifdef ENABLE_CHECKING
13815       /* Otherwise this is a generic code; we should just lists all of
13816          these explicitly.  We forgot one.  */
13817       gcc_unreachable ();
13818 #else
13819       /* In a release build, we want to degrade gracefully: better to
13820          generate incomplete debugging information than to crash.  */
13821       return NULL;
13822 #endif
13823     }
13824
13825   if (!ret && !list_ret)
13826     return 0;
13827
13828   if (want_address == 2 && !have_address
13829       && (dwarf_version >= 4 || !dwarf_strict))
13830     {
13831       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13832         {
13833           expansion_failed (loc, NULL_RTX,
13834                             "DWARF address size mismatch");
13835           return 0;
13836         }
13837       if (ret)
13838         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13839       else
13840         add_loc_descr_to_each (list_ret,
13841                                new_loc_descr (DW_OP_stack_value, 0, 0));
13842       have_address = 1;
13843     }
13844   /* Show if we can't fill the request for an address.  */
13845   if (want_address && !have_address)
13846     {
13847       expansion_failed (loc, NULL_RTX,
13848                         "Want address and only have value");
13849       return 0;
13850     }
13851
13852   gcc_assert (!ret || !list_ret);
13853
13854   /* If we've got an address and don't want one, dereference.  */
13855   if (!want_address && have_address)
13856     {
13857       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13858
13859       if (size > DWARF2_ADDR_SIZE || size == -1)
13860         {
13861           expansion_failed (loc, NULL_RTX,
13862                             "DWARF address size mismatch");
13863           return 0;
13864         }
13865       else if (size == DWARF2_ADDR_SIZE)
13866         op = DW_OP_deref;
13867       else
13868         op = DW_OP_deref_size;
13869
13870       if (ret)
13871         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13872       else
13873         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13874     }
13875   if (ret)
13876     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13877
13878   return list_ret;
13879 }
13880
13881 /* Same as above but return only single location expression.  */
13882 static dw_loc_descr_ref
13883 loc_descriptor_from_tree (tree loc, int want_address)
13884 {
13885   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13886   if (!ret)
13887     return NULL;
13888   if (ret->dw_loc_next)
13889     {
13890       expansion_failed (loc, NULL_RTX,
13891                         "Location list where only loc descriptor needed");
13892       return NULL;
13893     }
13894   return ret->expr;
13895 }
13896
13897 /* Given a value, round it up to the lowest multiple of `boundary'
13898    which is not less than the value itself.  */
13899
13900 static inline HOST_WIDE_INT
13901 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13902 {
13903   return (((value + boundary - 1) / boundary) * boundary);
13904 }
13905
13906 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13907    pointer to the declared type for the relevant field variable, or return
13908    `integer_type_node' if the given node turns out to be an
13909    ERROR_MARK node.  */
13910
13911 static inline tree
13912 field_type (const_tree decl)
13913 {
13914   tree type;
13915
13916   if (TREE_CODE (decl) == ERROR_MARK)
13917     return integer_type_node;
13918
13919   type = DECL_BIT_FIELD_TYPE (decl);
13920   if (type == NULL_TREE)
13921     type = TREE_TYPE (decl);
13922
13923   return type;
13924 }
13925
13926 /* Given a pointer to a tree node, return the alignment in bits for
13927    it, or else return BITS_PER_WORD if the node actually turns out to
13928    be an ERROR_MARK node.  */
13929
13930 static inline unsigned
13931 simple_type_align_in_bits (const_tree type)
13932 {
13933   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13934 }
13935
13936 static inline unsigned
13937 simple_decl_align_in_bits (const_tree decl)
13938 {
13939   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13940 }
13941
13942 /* Return the result of rounding T up to ALIGN.  */
13943
13944 static inline double_int
13945 round_up_to_align (double_int t, unsigned int align)
13946 {
13947   double_int alignd = uhwi_to_double_int (align);
13948   t = double_int_add (t, alignd);
13949   t = double_int_add (t, double_int_minus_one);
13950   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13951   t = double_int_mul (t, alignd);
13952   return t;
13953 }
13954
13955 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13956    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13957    or return 0 if we are unable to determine what that offset is, either
13958    because the argument turns out to be a pointer to an ERROR_MARK node, or
13959    because the offset is actually variable.  (We can't handle the latter case
13960    just yet).  */
13961
13962 static HOST_WIDE_INT
13963 field_byte_offset (const_tree decl)
13964 {
13965   double_int object_offset_in_bits;
13966   double_int object_offset_in_bytes;
13967   double_int bitpos_int;
13968
13969   if (TREE_CODE (decl) == ERROR_MARK)
13970     return 0;
13971
13972   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13973
13974   /* We cannot yet cope with fields whose positions are variable, so
13975      for now, when we see such things, we simply return 0.  Someday, we may
13976      be able to handle such cases, but it will be damn difficult.  */
13977   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13978     return 0;
13979
13980   bitpos_int = tree_to_double_int (bit_position (decl));
13981
13982 #ifdef PCC_BITFIELD_TYPE_MATTERS
13983   if (PCC_BITFIELD_TYPE_MATTERS)
13984     {
13985       tree type;
13986       tree field_size_tree;
13987       double_int deepest_bitpos;
13988       double_int field_size_in_bits;
13989       unsigned int type_align_in_bits;
13990       unsigned int decl_align_in_bits;
13991       double_int type_size_in_bits;
13992
13993       type = field_type (decl);
13994       type_size_in_bits = double_int_type_size_in_bits (type);
13995       type_align_in_bits = simple_type_align_in_bits (type);
13996
13997       field_size_tree = DECL_SIZE (decl);
13998
13999       /* The size could be unspecified if there was an error, or for
14000          a flexible array member.  */
14001       if (!field_size_tree)
14002         field_size_tree = bitsize_zero_node;
14003
14004       /* If the size of the field is not constant, use the type size.  */
14005       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14006         field_size_in_bits = tree_to_double_int (field_size_tree);
14007       else
14008         field_size_in_bits = type_size_in_bits;
14009
14010       decl_align_in_bits = simple_decl_align_in_bits (decl);
14011
14012       /* The GCC front-end doesn't make any attempt to keep track of the
14013          starting bit offset (relative to the start of the containing
14014          structure type) of the hypothetical "containing object" for a
14015          bit-field.  Thus, when computing the byte offset value for the
14016          start of the "containing object" of a bit-field, we must deduce
14017          this information on our own. This can be rather tricky to do in
14018          some cases.  For example, handling the following structure type
14019          definition when compiling for an i386/i486 target (which only
14020          aligns long long's to 32-bit boundaries) can be very tricky:
14021
14022          struct S { int field1; long long field2:31; };
14023
14024          Fortunately, there is a simple rule-of-thumb which can be used
14025          in such cases.  When compiling for an i386/i486, GCC will
14026          allocate 8 bytes for the structure shown above.  It decides to
14027          do this based upon one simple rule for bit-field allocation.
14028          GCC allocates each "containing object" for each bit-field at
14029          the first (i.e. lowest addressed) legitimate alignment boundary
14030          (based upon the required minimum alignment for the declared
14031          type of the field) which it can possibly use, subject to the
14032          condition that there is still enough available space remaining
14033          in the containing object (when allocated at the selected point)
14034          to fully accommodate all of the bits of the bit-field itself.
14035
14036          This simple rule makes it obvious why GCC allocates 8 bytes for
14037          each object of the structure type shown above.  When looking
14038          for a place to allocate the "containing object" for `field2',
14039          the compiler simply tries to allocate a 64-bit "containing
14040          object" at each successive 32-bit boundary (starting at zero)
14041          until it finds a place to allocate that 64- bit field such that
14042          at least 31 contiguous (and previously unallocated) bits remain
14043          within that selected 64 bit field.  (As it turns out, for the
14044          example above, the compiler finds it is OK to allocate the
14045          "containing object" 64-bit field at bit-offset zero within the
14046          structure type.)
14047
14048          Here we attempt to work backwards from the limited set of facts
14049          we're given, and we try to deduce from those facts, where GCC
14050          must have believed that the containing object started (within
14051          the structure type). The value we deduce is then used (by the
14052          callers of this routine) to generate DW_AT_location and
14053          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14054          the case of DW_AT_location, regular fields as well).  */
14055
14056       /* Figure out the bit-distance from the start of the structure to
14057          the "deepest" bit of the bit-field.  */
14058       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14059
14060       /* This is the tricky part.  Use some fancy footwork to deduce
14061          where the lowest addressed bit of the containing object must
14062          be.  */
14063       object_offset_in_bits
14064         = double_int_sub (deepest_bitpos, type_size_in_bits);
14065
14066       /* Round up to type_align by default.  This works best for
14067          bitfields.  */
14068       object_offset_in_bits
14069         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14070
14071       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14072         {
14073           object_offset_in_bits
14074             = double_int_sub (deepest_bitpos, type_size_in_bits);
14075
14076           /* Round up to decl_align instead.  */
14077           object_offset_in_bits
14078             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14079         }
14080     }
14081   else
14082 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14083     object_offset_in_bits = bitpos_int;
14084
14085   object_offset_in_bytes
14086     = double_int_div (object_offset_in_bits,
14087                       uhwi_to_double_int (BITS_PER_UNIT), true,
14088                       TRUNC_DIV_EXPR);
14089   return double_int_to_shwi (object_offset_in_bytes);
14090 }
14091 \f
14092 /* The following routines define various Dwarf attributes and any data
14093    associated with them.  */
14094
14095 /* Add a location description attribute value to a DIE.
14096
14097    This emits location attributes suitable for whole variables and
14098    whole parameters.  Note that the location attributes for struct fields are
14099    generated by the routine `data_member_location_attribute' below.  */
14100
14101 static inline void
14102 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14103                              dw_loc_list_ref descr)
14104 {
14105   if (descr == 0)
14106     return;
14107   if (single_element_loc_list_p (descr))
14108     add_AT_loc (die, attr_kind, descr->expr);
14109   else
14110     add_AT_loc_list (die, attr_kind, descr);
14111 }
14112
14113 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14114
14115 static void
14116 add_accessibility_attribute (dw_die_ref die, tree decl)
14117 {
14118   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14119      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14120      the default has always been DW_ACCESS_public.  */
14121   if (TREE_PROTECTED (decl))
14122     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14123   else if (TREE_PRIVATE (decl))
14124     {
14125       if (dwarf_version == 2
14126           || die->die_parent == NULL
14127           || die->die_parent->die_tag != DW_TAG_class_type)
14128         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14129     }
14130   else if (dwarf_version > 2
14131            && die->die_parent
14132            && die->die_parent->die_tag == DW_TAG_class_type)
14133     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14134 }
14135
14136 /* Attach the specialized form of location attribute used for data members of
14137    struct and union types.  In the special case of a FIELD_DECL node which
14138    represents a bit-field, the "offset" part of this special location
14139    descriptor must indicate the distance in bytes from the lowest-addressed
14140    byte of the containing struct or union type to the lowest-addressed byte of
14141    the "containing object" for the bit-field.  (See the `field_byte_offset'
14142    function above).
14143
14144    For any given bit-field, the "containing object" is a hypothetical object
14145    (of some integral or enum type) within which the given bit-field lives.  The
14146    type of this hypothetical "containing object" is always the same as the
14147    declared type of the individual bit-field itself (for GCC anyway... the
14148    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14149    bytes) of the hypothetical "containing object" which will be given in the
14150    DW_AT_byte_size attribute for this bit-field.  (See the
14151    `byte_size_attribute' function below.)  It is also used when calculating the
14152    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14153    function below.)  */
14154
14155 static void
14156 add_data_member_location_attribute (dw_die_ref die, tree decl)
14157 {
14158   HOST_WIDE_INT offset;
14159   dw_loc_descr_ref loc_descr = 0;
14160
14161   if (TREE_CODE (decl) == TREE_BINFO)
14162     {
14163       /* We're working on the TAG_inheritance for a base class.  */
14164       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14165         {
14166           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14167              aren't at a fixed offset from all (sub)objects of the same
14168              type.  We need to extract the appropriate offset from our
14169              vtable.  The following dwarf expression means
14170
14171                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14172
14173              This is specific to the V3 ABI, of course.  */
14174
14175           dw_loc_descr_ref tmp;
14176
14177           /* Make a copy of the object address.  */
14178           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14179           add_loc_descr (&loc_descr, tmp);
14180
14181           /* Extract the vtable address.  */
14182           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14183           add_loc_descr (&loc_descr, tmp);
14184
14185           /* Calculate the address of the offset.  */
14186           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14187           gcc_assert (offset < 0);
14188
14189           tmp = int_loc_descriptor (-offset);
14190           add_loc_descr (&loc_descr, tmp);
14191           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14192           add_loc_descr (&loc_descr, tmp);
14193
14194           /* Extract the offset.  */
14195           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14196           add_loc_descr (&loc_descr, tmp);
14197
14198           /* Add it to the object address.  */
14199           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14200           add_loc_descr (&loc_descr, tmp);
14201         }
14202       else
14203         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14204     }
14205   else
14206     offset = field_byte_offset (decl);
14207
14208   if (! loc_descr)
14209     {
14210       if (dwarf_version > 2)
14211         {
14212           /* Don't need to output a location expression, just the constant. */
14213           if (offset < 0)
14214             add_AT_int (die, DW_AT_data_member_location, offset);
14215           else
14216             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14217           return;
14218         }
14219       else
14220         {
14221           enum dwarf_location_atom op;
14222
14223           /* The DWARF2 standard says that we should assume that the structure
14224              address is already on the stack, so we can specify a structure
14225              field address by using DW_OP_plus_uconst.  */
14226
14227 #ifdef MIPS_DEBUGGING_INFO
14228           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14229              operator correctly.  It works only if we leave the offset on the
14230              stack.  */
14231           op = DW_OP_constu;
14232 #else
14233           op = DW_OP_plus_uconst;
14234 #endif
14235
14236           loc_descr = new_loc_descr (op, offset, 0);
14237         }
14238     }
14239
14240   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14241 }
14242
14243 /* Writes integer values to dw_vec_const array.  */
14244
14245 static void
14246 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14247 {
14248   while (size != 0)
14249     {
14250       *dest++ = val & 0xff;
14251       val >>= 8;
14252       --size;
14253     }
14254 }
14255
14256 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14257
14258 static HOST_WIDE_INT
14259 extract_int (const unsigned char *src, unsigned int size)
14260 {
14261   HOST_WIDE_INT val = 0;
14262
14263   src += size;
14264   while (size != 0)
14265     {
14266       val <<= 8;
14267       val |= *--src & 0xff;
14268       --size;
14269     }
14270   return val;
14271 }
14272
14273 /* Writes double_int values to dw_vec_const array.  */
14274
14275 static void
14276 insert_double (double_int val, unsigned char *dest)
14277 {
14278   unsigned char *p0 = dest;
14279   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14280
14281   if (WORDS_BIG_ENDIAN)
14282     {
14283       p0 = p1;
14284       p1 = dest;
14285     }
14286
14287   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14288   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14289 }
14290
14291 /* Writes floating point values to dw_vec_const array.  */
14292
14293 static void
14294 insert_float (const_rtx rtl, unsigned char *array)
14295 {
14296   REAL_VALUE_TYPE rv;
14297   long val[4];
14298   int i;
14299
14300   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14301   real_to_target (val, &rv, GET_MODE (rtl));
14302
14303   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14304   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14305     {
14306       insert_int (val[i], 4, array);
14307       array += 4;
14308     }
14309 }
14310
14311 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14312    does not have a "location" either in memory or in a register.  These
14313    things can arise in GNU C when a constant is passed as an actual parameter
14314    to an inlined function.  They can also arise in C++ where declared
14315    constants do not necessarily get memory "homes".  */
14316
14317 static bool
14318 add_const_value_attribute (dw_die_ref die, rtx rtl)
14319 {
14320   switch (GET_CODE (rtl))
14321     {
14322     case CONST_INT:
14323       {
14324         HOST_WIDE_INT val = INTVAL (rtl);
14325
14326         if (val < 0)
14327           add_AT_int (die, DW_AT_const_value, val);
14328         else
14329           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14330       }
14331       return true;
14332
14333     case CONST_DOUBLE:
14334       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14335          floating-point constant.  A CONST_DOUBLE is used whenever the
14336          constant requires more than one word in order to be adequately
14337          represented.  */
14338       {
14339         enum machine_mode mode = GET_MODE (rtl);
14340
14341         if (SCALAR_FLOAT_MODE_P (mode))
14342           {
14343             unsigned int length = GET_MODE_SIZE (mode);
14344             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14345
14346             insert_float (rtl, array);
14347             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14348           }
14349         else
14350           add_AT_double (die, DW_AT_const_value,
14351                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14352       }
14353       return true;
14354
14355     case CONST_VECTOR:
14356       {
14357         enum machine_mode mode = GET_MODE (rtl);
14358         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14359         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14360         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14361           (length * elt_size);
14362         unsigned int i;
14363         unsigned char *p;
14364
14365         switch (GET_MODE_CLASS (mode))
14366           {
14367           case MODE_VECTOR_INT:
14368             for (i = 0, p = array; i < length; i++, p += elt_size)
14369               {
14370                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14371                 double_int val = rtx_to_double_int (elt);
14372
14373                 if (elt_size <= sizeof (HOST_WIDE_INT))
14374                   insert_int (double_int_to_shwi (val), elt_size, p);
14375                 else
14376                   {
14377                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14378                     insert_double (val, p);
14379                   }
14380               }
14381             break;
14382
14383           case MODE_VECTOR_FLOAT:
14384             for (i = 0, p = array; i < length; i++, p += elt_size)
14385               {
14386                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14387                 insert_float (elt, p);
14388               }
14389             break;
14390
14391           default:
14392             gcc_unreachable ();
14393           }
14394
14395         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14396       }
14397       return true;
14398
14399     case CONST_STRING:
14400       if (dwarf_version >= 4 || !dwarf_strict)
14401         {
14402           dw_loc_descr_ref loc_result;
14403           resolve_one_addr (&rtl, NULL);
14404         rtl_addr:
14405           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14406           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14407           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14408           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14409           add_AT_loc (die, DW_AT_location, loc_result);
14410           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14411           return true;
14412         }
14413       return false;
14414
14415     case CONST:
14416       if (CONSTANT_P (XEXP (rtl, 0)))
14417         return add_const_value_attribute (die, XEXP (rtl, 0));
14418       /* FALLTHROUGH */
14419     case SYMBOL_REF:
14420       if (!const_ok_for_output (rtl))
14421         return false;
14422     case LABEL_REF:
14423       if (dwarf_version >= 4 || !dwarf_strict)
14424         goto rtl_addr;
14425       return false;
14426
14427     case PLUS:
14428       /* In cases where an inlined instance of an inline function is passed
14429          the address of an `auto' variable (which is local to the caller) we
14430          can get a situation where the DECL_RTL of the artificial local
14431          variable (for the inlining) which acts as a stand-in for the
14432          corresponding formal parameter (of the inline function) will look
14433          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14434          exactly a compile-time constant expression, but it isn't the address
14435          of the (artificial) local variable either.  Rather, it represents the
14436          *value* which the artificial local variable always has during its
14437          lifetime.  We currently have no way to represent such quasi-constant
14438          values in Dwarf, so for now we just punt and generate nothing.  */
14439       return false;
14440
14441     case HIGH:
14442     case CONST_FIXED:
14443       return false;
14444
14445     case MEM:
14446       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14447           && MEM_READONLY_P (rtl)
14448           && GET_MODE (rtl) == BLKmode)
14449         {
14450           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14451           return true;
14452         }
14453       return false;
14454
14455     default:
14456       /* No other kinds of rtx should be possible here.  */
14457       gcc_unreachable ();
14458     }
14459   return false;
14460 }
14461
14462 /* Determine whether the evaluation of EXPR references any variables
14463    or functions which aren't otherwise used (and therefore may not be
14464    output).  */
14465 static tree
14466 reference_to_unused (tree * tp, int * walk_subtrees,
14467                      void * data ATTRIBUTE_UNUSED)
14468 {
14469   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14470     *walk_subtrees = 0;
14471
14472   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14473       && ! TREE_ASM_WRITTEN (*tp))
14474     return *tp;
14475   /* ???  The C++ FE emits debug information for using decls, so
14476      putting gcc_unreachable here falls over.  See PR31899.  For now
14477      be conservative.  */
14478   else if (!cgraph_global_info_ready
14479            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14480     return *tp;
14481   else if (TREE_CODE (*tp) == VAR_DECL)
14482     {
14483       struct varpool_node *node = varpool_get_node (*tp);
14484       if (!node || !node->needed)
14485         return *tp;
14486     }
14487   else if (TREE_CODE (*tp) == FUNCTION_DECL
14488            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14489     {
14490       /* The call graph machinery must have finished analyzing,
14491          optimizing and gimplifying the CU by now.
14492          So if *TP has no call graph node associated
14493          to it, it means *TP will not be emitted.  */
14494       if (!cgraph_get_node (*tp))
14495         return *tp;
14496     }
14497   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14498     return *tp;
14499
14500   return NULL_TREE;
14501 }
14502
14503 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14504    for use in a later add_const_value_attribute call.  */
14505
14506 static rtx
14507 rtl_for_decl_init (tree init, tree type)
14508 {
14509   rtx rtl = NULL_RTX;
14510
14511   STRIP_NOPS (init);
14512
14513   /* If a variable is initialized with a string constant without embedded
14514      zeros, build CONST_STRING.  */
14515   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14516     {
14517       tree enttype = TREE_TYPE (type);
14518       tree domain = TYPE_DOMAIN (type);
14519       enum machine_mode mode = TYPE_MODE (enttype);
14520
14521       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14522           && domain
14523           && integer_zerop (TYPE_MIN_VALUE (domain))
14524           && compare_tree_int (TYPE_MAX_VALUE (domain),
14525                                TREE_STRING_LENGTH (init) - 1) == 0
14526           && ((size_t) TREE_STRING_LENGTH (init)
14527               == strlen (TREE_STRING_POINTER (init)) + 1))
14528         {
14529           rtl = gen_rtx_CONST_STRING (VOIDmode,
14530                                       ggc_strdup (TREE_STRING_POINTER (init)));
14531           rtl = gen_rtx_MEM (BLKmode, rtl);
14532           MEM_READONLY_P (rtl) = 1;
14533         }
14534     }
14535   /* Other aggregates, and complex values, could be represented using
14536      CONCAT: FIXME!  */
14537   else if (AGGREGATE_TYPE_P (type)
14538            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14539                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14540            || TREE_CODE (type) == COMPLEX_TYPE)
14541     ;
14542   /* Vectors only work if their mode is supported by the target.
14543      FIXME: generic vectors ought to work too.  */
14544   else if (TREE_CODE (type) == VECTOR_TYPE
14545            && !VECTOR_MODE_P (TYPE_MODE (type)))
14546     ;
14547   /* If the initializer is something that we know will expand into an
14548      immediate RTL constant, expand it now.  We must be careful not to
14549      reference variables which won't be output.  */
14550   else if (initializer_constant_valid_p (init, type)
14551            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14552     {
14553       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14554          possible.  */
14555       if (TREE_CODE (type) == VECTOR_TYPE)
14556         switch (TREE_CODE (init))
14557           {
14558           case VECTOR_CST:
14559             break;
14560           case CONSTRUCTOR:
14561             if (TREE_CONSTANT (init))
14562               {
14563                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14564                 bool constant_p = true;
14565                 tree value;
14566                 unsigned HOST_WIDE_INT ix;
14567
14568                 /* Even when ctor is constant, it might contain non-*_CST
14569                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14570                    belong into VECTOR_CST nodes.  */
14571                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14572                   if (!CONSTANT_CLASS_P (value))
14573                     {
14574                       constant_p = false;
14575                       break;
14576                     }
14577
14578                 if (constant_p)
14579                   {
14580                     init = build_vector_from_ctor (type, elts);
14581                     break;
14582                   }
14583               }
14584             /* FALLTHRU */
14585
14586           default:
14587             return NULL;
14588           }
14589
14590       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14591
14592       /* If expand_expr returns a MEM, it wasn't immediate.  */
14593       gcc_assert (!rtl || !MEM_P (rtl));
14594     }
14595
14596   return rtl;
14597 }
14598
14599 /* Generate RTL for the variable DECL to represent its location.  */
14600
14601 static rtx
14602 rtl_for_decl_location (tree decl)
14603 {
14604   rtx rtl;
14605
14606   /* Here we have to decide where we are going to say the parameter "lives"
14607      (as far as the debugger is concerned).  We only have a couple of
14608      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14609
14610      DECL_RTL normally indicates where the parameter lives during most of the
14611      activation of the function.  If optimization is enabled however, this
14612      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14613      that the parameter doesn't really live anywhere (as far as the code
14614      generation parts of GCC are concerned) during most of the function's
14615      activation.  That will happen (for example) if the parameter is never
14616      referenced within the function.
14617
14618      We could just generate a location descriptor here for all non-NULL
14619      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14620      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14621      where DECL_RTL is NULL or is a pseudo-reg.
14622
14623      Note however that we can only get away with using DECL_INCOMING_RTL as
14624      a backup substitute for DECL_RTL in certain limited cases.  In cases
14625      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14626      we can be sure that the parameter was passed using the same type as it is
14627      declared to have within the function, and that its DECL_INCOMING_RTL
14628      points us to a place where a value of that type is passed.
14629
14630      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14631      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14632      because in these cases DECL_INCOMING_RTL points us to a value of some
14633      type which is *different* from the type of the parameter itself.  Thus,
14634      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14635      such cases, the debugger would end up (for example) trying to fetch a
14636      `float' from a place which actually contains the first part of a
14637      `double'.  That would lead to really incorrect and confusing
14638      output at debug-time.
14639
14640      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14641      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14642      are a couple of exceptions however.  On little-endian machines we can
14643      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14644      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14645      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14646      when (on a little-endian machine) a non-prototyped function has a
14647      parameter declared to be of type `short' or `char'.  In such cases,
14648      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14649      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14650      passed `int' value.  If the debugger then uses that address to fetch
14651      a `short' or a `char' (on a little-endian machine) the result will be
14652      the correct data, so we allow for such exceptional cases below.
14653
14654      Note that our goal here is to describe the place where the given formal
14655      parameter lives during most of the function's activation (i.e. between the
14656      end of the prologue and the start of the epilogue).  We'll do that as best
14657      as we can. Note however that if the given formal parameter is modified
14658      sometime during the execution of the function, then a stack backtrace (at
14659      debug-time) will show the function as having been called with the *new*
14660      value rather than the value which was originally passed in.  This happens
14661      rarely enough that it is not a major problem, but it *is* a problem, and
14662      I'd like to fix it.
14663
14664      A future version of dwarf2out.c may generate two additional attributes for
14665      any given DW_TAG_formal_parameter DIE which will describe the "passed
14666      type" and the "passed location" for the given formal parameter in addition
14667      to the attributes we now generate to indicate the "declared type" and the
14668      "active location" for each parameter.  This additional set of attributes
14669      could be used by debuggers for stack backtraces. Separately, note that
14670      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14671      This happens (for example) for inlined-instances of inline function formal
14672      parameters which are never referenced.  This really shouldn't be
14673      happening.  All PARM_DECL nodes should get valid non-NULL
14674      DECL_INCOMING_RTL values.  FIXME.  */
14675
14676   /* Use DECL_RTL as the "location" unless we find something better.  */
14677   rtl = DECL_RTL_IF_SET (decl);
14678
14679   /* When generating abstract instances, ignore everything except
14680      constants, symbols living in memory, and symbols living in
14681      fixed registers.  */
14682   if (! reload_completed)
14683     {
14684       if (rtl
14685           && (CONSTANT_P (rtl)
14686               || (MEM_P (rtl)
14687                   && CONSTANT_P (XEXP (rtl, 0)))
14688               || (REG_P (rtl)
14689                   && TREE_CODE (decl) == VAR_DECL
14690                   && TREE_STATIC (decl))))
14691         {
14692           rtl = targetm.delegitimize_address (rtl);
14693           return rtl;
14694         }
14695       rtl = NULL_RTX;
14696     }
14697   else if (TREE_CODE (decl) == PARM_DECL)
14698     {
14699       if (rtl == NULL_RTX
14700           || is_pseudo_reg (rtl)
14701           || (MEM_P (rtl)
14702               && is_pseudo_reg (XEXP (rtl, 0))
14703               && DECL_INCOMING_RTL (decl)
14704               && MEM_P (DECL_INCOMING_RTL (decl))
14705               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14706         {
14707           tree declared_type = TREE_TYPE (decl);
14708           tree passed_type = DECL_ARG_TYPE (decl);
14709           enum machine_mode dmode = TYPE_MODE (declared_type);
14710           enum machine_mode pmode = TYPE_MODE (passed_type);
14711
14712           /* This decl represents a formal parameter which was optimized out.
14713              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14714              all cases where (rtl == NULL_RTX) just below.  */
14715           if (dmode == pmode)
14716             rtl = DECL_INCOMING_RTL (decl);
14717           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14718                    && SCALAR_INT_MODE_P (dmode)
14719                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14720                    && DECL_INCOMING_RTL (decl))
14721             {
14722               rtx inc = DECL_INCOMING_RTL (decl);
14723               if (REG_P (inc))
14724                 rtl = inc;
14725               else if (MEM_P (inc))
14726                 {
14727                   if (BYTES_BIG_ENDIAN)
14728                     rtl = adjust_address_nv (inc, dmode,
14729                                              GET_MODE_SIZE (pmode)
14730                                              - GET_MODE_SIZE (dmode));
14731                   else
14732                     rtl = inc;
14733                 }
14734             }
14735         }
14736
14737       /* If the parm was passed in registers, but lives on the stack, then
14738          make a big endian correction if the mode of the type of the
14739          parameter is not the same as the mode of the rtl.  */
14740       /* ??? This is the same series of checks that are made in dbxout.c before
14741          we reach the big endian correction code there.  It isn't clear if all
14742          of these checks are necessary here, but keeping them all is the safe
14743          thing to do.  */
14744       else if (MEM_P (rtl)
14745                && XEXP (rtl, 0) != const0_rtx
14746                && ! CONSTANT_P (XEXP (rtl, 0))
14747                /* Not passed in memory.  */
14748                && !MEM_P (DECL_INCOMING_RTL (decl))
14749                /* Not passed by invisible reference.  */
14750                && (!REG_P (XEXP (rtl, 0))
14751                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14752                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14753 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14754                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14755 #endif
14756                      )
14757                /* Big endian correction check.  */
14758                && BYTES_BIG_ENDIAN
14759                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14760                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14761                    < UNITS_PER_WORD))
14762         {
14763           int offset = (UNITS_PER_WORD
14764                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14765
14766           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14767                              plus_constant (XEXP (rtl, 0), offset));
14768         }
14769     }
14770   else if (TREE_CODE (decl) == VAR_DECL
14771            && rtl
14772            && MEM_P (rtl)
14773            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14774            && BYTES_BIG_ENDIAN)
14775     {
14776       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14777       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14778
14779       /* If a variable is declared "register" yet is smaller than
14780          a register, then if we store the variable to memory, it
14781          looks like we're storing a register-sized value, when in
14782          fact we are not.  We need to adjust the offset of the
14783          storage location to reflect the actual value's bytes,
14784          else gdb will not be able to display it.  */
14785       if (rsize > dsize)
14786         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14787                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14788     }
14789
14790   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14791      and will have been substituted directly into all expressions that use it.
14792      C does not have such a concept, but C++ and other languages do.  */
14793   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14794     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14795
14796   if (rtl)
14797     rtl = targetm.delegitimize_address (rtl);
14798
14799   /* If we don't look past the constant pool, we risk emitting a
14800      reference to a constant pool entry that isn't referenced from
14801      code, and thus is not emitted.  */
14802   if (rtl)
14803     rtl = avoid_constant_pool_reference (rtl);
14804
14805   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14806      in the current CU, resolve_addr will remove the expression referencing
14807      it.  */
14808   if (rtl == NULL_RTX
14809       && TREE_CODE (decl) == VAR_DECL
14810       && !DECL_EXTERNAL (decl)
14811       && TREE_STATIC (decl)
14812       && DECL_NAME (decl)
14813       && !DECL_HARD_REGISTER (decl)
14814       && DECL_MODE (decl) != VOIDmode)
14815     {
14816       rtl = make_decl_rtl_for_debug (decl);
14817       if (!MEM_P (rtl)
14818           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14819           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14820         rtl = NULL_RTX;
14821     }
14822
14823   return rtl;
14824 }
14825
14826 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14827    returned.  If so, the decl for the COMMON block is returned, and the
14828    value is the offset into the common block for the symbol.  */
14829
14830 static tree
14831 fortran_common (tree decl, HOST_WIDE_INT *value)
14832 {
14833   tree val_expr, cvar;
14834   enum machine_mode mode;
14835   HOST_WIDE_INT bitsize, bitpos;
14836   tree offset;
14837   int volatilep = 0, unsignedp = 0;
14838
14839   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14840      it does not have a value (the offset into the common area), or if it
14841      is thread local (as opposed to global) then it isn't common, and shouldn't
14842      be handled as such.  */
14843   if (TREE_CODE (decl) != VAR_DECL
14844       || !TREE_STATIC (decl)
14845       || !DECL_HAS_VALUE_EXPR_P (decl)
14846       || !is_fortran ())
14847     return NULL_TREE;
14848
14849   val_expr = DECL_VALUE_EXPR (decl);
14850   if (TREE_CODE (val_expr) != COMPONENT_REF)
14851     return NULL_TREE;
14852
14853   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14854                               &mode, &unsignedp, &volatilep, true);
14855
14856   if (cvar == NULL_TREE
14857       || TREE_CODE (cvar) != VAR_DECL
14858       || DECL_ARTIFICIAL (cvar)
14859       || !TREE_PUBLIC (cvar))
14860     return NULL_TREE;
14861
14862   *value = 0;
14863   if (offset != NULL)
14864     {
14865       if (!host_integerp (offset, 0))
14866         return NULL_TREE;
14867       *value = tree_low_cst (offset, 0);
14868     }
14869   if (bitpos != 0)
14870     *value += bitpos / BITS_PER_UNIT;
14871
14872   return cvar;
14873 }
14874
14875 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14876    data attribute for a variable or a parameter.  We generate the
14877    DW_AT_const_value attribute only in those cases where the given variable
14878    or parameter does not have a true "location" either in memory or in a
14879    register.  This can happen (for example) when a constant is passed as an
14880    actual argument in a call to an inline function.  (It's possible that
14881    these things can crop up in other ways also.)  Note that one type of
14882    constant value which can be passed into an inlined function is a constant
14883    pointer.  This can happen for example if an actual argument in an inlined
14884    function call evaluates to a compile-time constant address.
14885
14886    CACHE_P is true if it is worth caching the location list for DECL,
14887    so that future calls can reuse it rather than regenerate it from scratch.
14888    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14889    since we will need to refer to them each time the function is inlined.  */
14890
14891 static bool
14892 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14893                                        enum dwarf_attribute attr)
14894 {
14895   rtx rtl;
14896   dw_loc_list_ref list;
14897   var_loc_list *loc_list;
14898   cached_dw_loc_list *cache;
14899   void **slot;
14900
14901   if (TREE_CODE (decl) == ERROR_MARK)
14902     return false;
14903
14904   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14905               || TREE_CODE (decl) == RESULT_DECL);
14906
14907   /* Try to get some constant RTL for this decl, and use that as the value of
14908      the location.  */
14909
14910   rtl = rtl_for_decl_location (decl);
14911   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14912       && add_const_value_attribute (die, rtl))
14913     return true;
14914
14915   /* See if we have single element location list that is equivalent to
14916      a constant value.  That way we are better to use add_const_value_attribute
14917      rather than expanding constant value equivalent.  */
14918   loc_list = lookup_decl_loc (decl);
14919   if (loc_list
14920       && loc_list->first
14921       && loc_list->first->next == NULL
14922       && NOTE_P (loc_list->first->loc)
14923       && NOTE_VAR_LOCATION (loc_list->first->loc)
14924       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14925     {
14926       struct var_loc_node *node;
14927
14928       node = loc_list->first;
14929       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14930       if (GET_CODE (rtl) == EXPR_LIST)
14931         rtl = XEXP (rtl, 0);
14932       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14933           && add_const_value_attribute (die, rtl))
14934          return true;
14935     }
14936   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14937      list several times.  See if we've already cached the contents.  */
14938   list = NULL;
14939   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14940     cache_p = false;
14941   if (cache_p)
14942     {
14943       cache = (cached_dw_loc_list *)
14944         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14945       if (cache)
14946         list = cache->loc_list;
14947     }
14948   if (list == NULL)
14949     {
14950       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14951       /* It is usually worth caching this result if the decl is from
14952          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14953       if (cache_p && list && list->dw_loc_next)
14954         {
14955           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14956                                            DECL_UID (decl), INSERT);
14957           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14958           cache->decl_id = DECL_UID (decl);
14959           cache->loc_list = list;
14960           *slot = cache;
14961         }
14962     }
14963   if (list)
14964     {
14965       add_AT_location_description (die, attr, list);
14966       return true;
14967     }
14968   /* None of that worked, so it must not really have a location;
14969      try adding a constant value attribute from the DECL_INITIAL.  */
14970   return tree_add_const_value_attribute_for_decl (die, decl);
14971 }
14972
14973 /* Add VARIABLE and DIE into deferred locations list.  */
14974
14975 static void
14976 defer_location (tree variable, dw_die_ref die)
14977 {
14978   deferred_locations entry;
14979   entry.variable = variable;
14980   entry.die = die;
14981   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14982 }
14983
14984 /* Helper function for tree_add_const_value_attribute.  Natively encode
14985    initializer INIT into an array.  Return true if successful.  */
14986
14987 static bool
14988 native_encode_initializer (tree init, unsigned char *array, int size)
14989 {
14990   tree type;
14991
14992   if (init == NULL_TREE)
14993     return false;
14994
14995   STRIP_NOPS (init);
14996   switch (TREE_CODE (init))
14997     {
14998     case STRING_CST:
14999       type = TREE_TYPE (init);
15000       if (TREE_CODE (type) == ARRAY_TYPE)
15001         {
15002           tree enttype = TREE_TYPE (type);
15003           enum machine_mode mode = TYPE_MODE (enttype);
15004
15005           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15006             return false;
15007           if (int_size_in_bytes (type) != size)
15008             return false;
15009           if (size > TREE_STRING_LENGTH (init))
15010             {
15011               memcpy (array, TREE_STRING_POINTER (init),
15012                       TREE_STRING_LENGTH (init));
15013               memset (array + TREE_STRING_LENGTH (init),
15014                       '\0', size - TREE_STRING_LENGTH (init));
15015             }
15016           else
15017             memcpy (array, TREE_STRING_POINTER (init), size);
15018           return true;
15019         }
15020       return false;
15021     case CONSTRUCTOR:
15022       type = TREE_TYPE (init);
15023       if (int_size_in_bytes (type) != size)
15024         return false;
15025       if (TREE_CODE (type) == ARRAY_TYPE)
15026         {
15027           HOST_WIDE_INT min_index;
15028           unsigned HOST_WIDE_INT cnt;
15029           int curpos = 0, fieldsize;
15030           constructor_elt *ce;
15031
15032           if (TYPE_DOMAIN (type) == NULL_TREE
15033               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15034             return false;
15035
15036           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15037           if (fieldsize <= 0)
15038             return false;
15039
15040           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15041           memset (array, '\0', size);
15042           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15043             {
15044               tree val = ce->value;
15045               tree index = ce->index;
15046               int pos = curpos;
15047               if (index && TREE_CODE (index) == RANGE_EXPR)
15048                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15049                       * fieldsize;
15050               else if (index)
15051                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15052
15053               if (val)
15054                 {
15055                   STRIP_NOPS (val);
15056                   if (!native_encode_initializer (val, array + pos, fieldsize))
15057                     return false;
15058                 }
15059               curpos = pos + fieldsize;
15060               if (index && TREE_CODE (index) == RANGE_EXPR)
15061                 {
15062                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15063                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15064                   while (count-- > 0)
15065                     {
15066                       if (val)
15067                         memcpy (array + curpos, array + pos, fieldsize);
15068                       curpos += fieldsize;
15069                     }
15070                 }
15071               gcc_assert (curpos <= size);
15072             }
15073           return true;
15074         }
15075       else if (TREE_CODE (type) == RECORD_TYPE
15076                || TREE_CODE (type) == UNION_TYPE)
15077         {
15078           tree field = NULL_TREE;
15079           unsigned HOST_WIDE_INT cnt;
15080           constructor_elt *ce;
15081
15082           if (int_size_in_bytes (type) != size)
15083             return false;
15084
15085           if (TREE_CODE (type) == RECORD_TYPE)
15086             field = TYPE_FIELDS (type);
15087
15088           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15089             {
15090               tree val = ce->value;
15091               int pos, fieldsize;
15092
15093               if (ce->index != 0)
15094                 field = ce->index;
15095
15096               if (val)
15097                 STRIP_NOPS (val);
15098
15099               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15100                 return false;
15101
15102               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15103                   && TYPE_DOMAIN (TREE_TYPE (field))
15104                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15105                 return false;
15106               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15107                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15108                 return false;
15109               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15110               pos = int_byte_position (field);
15111               gcc_assert (pos + fieldsize <= size);
15112               if (val
15113                   && !native_encode_initializer (val, array + pos, fieldsize))
15114                 return false;
15115             }
15116           return true;
15117         }
15118       return false;
15119     case VIEW_CONVERT_EXPR:
15120     case NON_LVALUE_EXPR:
15121       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15122     default:
15123       return native_encode_expr (init, array, size) == size;
15124     }
15125 }
15126
15127 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15128    attribute is the const value T.  */
15129
15130 static bool
15131 tree_add_const_value_attribute (dw_die_ref die, tree t)
15132 {
15133   tree init;
15134   tree type = TREE_TYPE (t);
15135   rtx rtl;
15136
15137   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15138     return false;
15139
15140   init = t;
15141   gcc_assert (!DECL_P (init));
15142
15143   rtl = rtl_for_decl_init (init, type);
15144   if (rtl)
15145     return add_const_value_attribute (die, rtl);
15146   /* If the host and target are sane, try harder.  */
15147   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15148            && initializer_constant_valid_p (init, type))
15149     {
15150       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15151       if (size > 0 && (int) size == size)
15152         {
15153           unsigned char *array = (unsigned char *)
15154             ggc_alloc_cleared_atomic (size);
15155
15156           if (native_encode_initializer (init, array, size))
15157             {
15158               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15159               return true;
15160             }
15161         }
15162     }
15163   return false;
15164 }
15165
15166 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15167    attribute is the const value of T, where T is an integral constant
15168    variable with static storage duration
15169    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15170
15171 static bool
15172 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15173 {
15174
15175   if (!decl
15176       || (TREE_CODE (decl) != VAR_DECL
15177           && TREE_CODE (decl) != CONST_DECL)
15178       || (TREE_CODE (decl) == VAR_DECL
15179           && !TREE_STATIC (decl)))
15180     return false;
15181
15182     if (TREE_READONLY (decl)
15183         && ! TREE_THIS_VOLATILE (decl)
15184         && DECL_INITIAL (decl))
15185       /* OK */;
15186     else
15187       return false;
15188
15189   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15190   if (get_AT (var_die, DW_AT_const_value))
15191     return false;
15192
15193   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15194 }
15195
15196 /* Convert the CFI instructions for the current function into a
15197    location list.  This is used for DW_AT_frame_base when we targeting
15198    a dwarf2 consumer that does not support the dwarf3
15199    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15200    expressions.  */
15201
15202 static dw_loc_list_ref
15203 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15204 {
15205   int ix;
15206   dw_fde_ref fde;
15207   dw_loc_list_ref list, *list_tail;
15208   dw_cfi_ref cfi;
15209   dw_cfa_location last_cfa, next_cfa;
15210   const char *start_label, *last_label, *section;
15211   dw_cfa_location remember;
15212
15213   fde = cfun->fde;
15214   gcc_assert (fde != NULL);
15215
15216   section = secname_for_decl (current_function_decl);
15217   list_tail = &list;
15218   list = NULL;
15219
15220   memset (&next_cfa, 0, sizeof (next_cfa));
15221   next_cfa.reg = INVALID_REGNUM;
15222   remember = next_cfa;
15223
15224   start_label = fde->dw_fde_begin;
15225
15226   /* ??? Bald assumption that the CIE opcode list does not contain
15227      advance opcodes.  */
15228   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15229     lookup_cfa_1 (cfi, &next_cfa, &remember);
15230
15231   last_cfa = next_cfa;
15232   last_label = start_label;
15233
15234   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15235     {
15236       /* If the first partition contained no CFI adjustments, the
15237          CIE opcodes apply to the whole first partition.  */
15238       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15239                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15240       list_tail =&(*list_tail)->dw_loc_next;
15241       start_label = last_label = fde->dw_fde_second_begin;
15242     }
15243
15244   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15245     {
15246       switch (cfi->dw_cfi_opc)
15247         {
15248         case DW_CFA_set_loc:
15249         case DW_CFA_advance_loc1:
15250         case DW_CFA_advance_loc2:
15251         case DW_CFA_advance_loc4:
15252           if (!cfa_equal_p (&last_cfa, &next_cfa))
15253             {
15254               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15255                                          start_label, last_label, section);
15256
15257               list_tail = &(*list_tail)->dw_loc_next;
15258               last_cfa = next_cfa;
15259               start_label = last_label;
15260             }
15261           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15262           break;
15263
15264         case DW_CFA_advance_loc:
15265           /* The encoding is complex enough that we should never emit this.  */
15266           gcc_unreachable ();
15267
15268         default:
15269           lookup_cfa_1 (cfi, &next_cfa, &remember);
15270           break;
15271         }
15272       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15273         {
15274           if (!cfa_equal_p (&last_cfa, &next_cfa))
15275             {
15276               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15277                                          start_label, last_label, section);
15278
15279               list_tail = &(*list_tail)->dw_loc_next;
15280               last_cfa = next_cfa;
15281               start_label = last_label;
15282             }
15283           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15284                                      start_label, fde->dw_fde_end, section);
15285           list_tail = &(*list_tail)->dw_loc_next;
15286           start_label = last_label = fde->dw_fde_second_begin;
15287         }
15288     }
15289
15290   if (!cfa_equal_p (&last_cfa, &next_cfa))
15291     {
15292       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15293                                  start_label, last_label, section);
15294       list_tail = &(*list_tail)->dw_loc_next;
15295       start_label = last_label;
15296     }
15297
15298   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15299                              start_label,
15300                              fde->dw_fde_second_begin
15301                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15302                              section);
15303
15304   if (list && list->dw_loc_next)
15305     gen_llsym (list);
15306
15307   return list;
15308 }
15309
15310 /* Compute a displacement from the "steady-state frame pointer" to the
15311    frame base (often the same as the CFA), and store it in
15312    frame_pointer_fb_offset.  OFFSET is added to the displacement
15313    before the latter is negated.  */
15314
15315 static void
15316 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15317 {
15318   rtx reg, elim;
15319
15320 #ifdef FRAME_POINTER_CFA_OFFSET
15321   reg = frame_pointer_rtx;
15322   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15323 #else
15324   reg = arg_pointer_rtx;
15325   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15326 #endif
15327
15328   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15329   if (GET_CODE (elim) == PLUS)
15330     {
15331       offset += INTVAL (XEXP (elim, 1));
15332       elim = XEXP (elim, 0);
15333     }
15334
15335   frame_pointer_fb_offset = -offset;
15336
15337   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15338      in which to eliminate.  This is because it's stack pointer isn't 
15339      directly accessible as a register within the ISA.  To work around
15340      this, assume that while we cannot provide a proper value for
15341      frame_pointer_fb_offset, we won't need one either.  */
15342   frame_pointer_fb_offset_valid
15343     = ((SUPPORTS_STACK_ALIGNMENT
15344         && (elim == hard_frame_pointer_rtx
15345             || elim == stack_pointer_rtx))
15346        || elim == (frame_pointer_needed
15347                    ? hard_frame_pointer_rtx
15348                    : stack_pointer_rtx));
15349 }
15350
15351 /* Generate a DW_AT_name attribute given some string value to be included as
15352    the value of the attribute.  */
15353
15354 static void
15355 add_name_attribute (dw_die_ref die, const char *name_string)
15356 {
15357   if (name_string != NULL && *name_string != 0)
15358     {
15359       if (demangle_name_func)
15360         name_string = (*demangle_name_func) (name_string);
15361
15362       add_AT_string (die, DW_AT_name, name_string);
15363     }
15364 }
15365
15366 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15367    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15368    of TYPE accordingly.
15369
15370    ??? This is a temporary measure until after we're able to generate
15371    regular DWARF for the complex Ada type system.  */
15372
15373 static void 
15374 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15375                                      dw_die_ref context_die)
15376 {
15377   tree dtype;
15378   dw_die_ref dtype_die;
15379
15380   if (!lang_hooks.types.descriptive_type)
15381     return;
15382
15383   dtype = lang_hooks.types.descriptive_type (type);
15384   if (!dtype)
15385     return;
15386
15387   dtype_die = lookup_type_die (dtype);
15388   if (!dtype_die)
15389     {
15390       gen_type_die (dtype, context_die);
15391       dtype_die = lookup_type_die (dtype);
15392       gcc_assert (dtype_die);
15393     }
15394
15395   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15396 }
15397
15398 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15399
15400 static void
15401 add_comp_dir_attribute (dw_die_ref die)
15402 {
15403   const char *wd = get_src_pwd ();
15404   char *wd1;
15405
15406   if (wd == NULL)
15407     return;
15408
15409   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15410     {
15411       int wdlen;
15412
15413       wdlen = strlen (wd);
15414       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15415       strcpy (wd1, wd);
15416       wd1 [wdlen] = DIR_SEPARATOR;
15417       wd1 [wdlen + 1] = 0;
15418       wd = wd1;
15419     }
15420
15421     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15422 }
15423
15424 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15425    default.  */
15426
15427 static int
15428 lower_bound_default (void)
15429 {
15430   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15431     {
15432     case DW_LANG_C:
15433     case DW_LANG_C89:
15434     case DW_LANG_C99:
15435     case DW_LANG_C_plus_plus:
15436     case DW_LANG_ObjC:
15437     case DW_LANG_ObjC_plus_plus:
15438     case DW_LANG_Java:
15439       return 0;
15440     case DW_LANG_Fortran77:
15441     case DW_LANG_Fortran90:
15442     case DW_LANG_Fortran95:
15443       return 1;
15444     case DW_LANG_UPC:
15445     case DW_LANG_D:
15446     case DW_LANG_Python:
15447       return dwarf_version >= 4 ? 0 : -1;
15448     case DW_LANG_Ada95:
15449     case DW_LANG_Ada83:
15450     case DW_LANG_Cobol74:
15451     case DW_LANG_Cobol85:
15452     case DW_LANG_Pascal83:
15453     case DW_LANG_Modula2:
15454     case DW_LANG_PLI:
15455       return dwarf_version >= 4 ? 1 : -1;
15456     default:
15457       return -1;
15458     }
15459 }
15460
15461 /* Given a tree node describing an array bound (either lower or upper) output
15462    a representation for that bound.  */
15463
15464 static void
15465 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15466 {
15467   switch (TREE_CODE (bound))
15468     {
15469     case ERROR_MARK:
15470       return;
15471
15472     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15473     case INTEGER_CST:
15474       {
15475         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15476         int dflt;
15477
15478         /* Use the default if possible.  */
15479         if (bound_attr == DW_AT_lower_bound
15480             && host_integerp (bound, 0)
15481             && (dflt = lower_bound_default ()) != -1
15482             && tree_low_cst (bound, 0) == dflt)
15483           ;
15484
15485         /* Otherwise represent the bound as an unsigned value with the
15486            precision of its type.  The precision and signedness of the
15487            type will be necessary to re-interpret it unambiguously.  */
15488         else if (prec < HOST_BITS_PER_WIDE_INT)
15489           {
15490             unsigned HOST_WIDE_INT mask
15491               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15492             add_AT_unsigned (subrange_die, bound_attr,
15493                              TREE_INT_CST_LOW (bound) & mask);
15494           }
15495         else if (prec == HOST_BITS_PER_WIDE_INT
15496                  || TREE_INT_CST_HIGH (bound) == 0)
15497           add_AT_unsigned (subrange_die, bound_attr,
15498                            TREE_INT_CST_LOW (bound));
15499         else
15500           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15501                          TREE_INT_CST_LOW (bound));
15502       }
15503       break;
15504
15505     CASE_CONVERT:
15506     case VIEW_CONVERT_EXPR:
15507       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15508       break;
15509
15510     case SAVE_EXPR:
15511       break;
15512
15513     case VAR_DECL:
15514     case PARM_DECL:
15515     case RESULT_DECL:
15516       {
15517         dw_die_ref decl_die = lookup_decl_die (bound);
15518
15519         /* ??? Can this happen, or should the variable have been bound
15520            first?  Probably it can, since I imagine that we try to create
15521            the types of parameters in the order in which they exist in
15522            the list, and won't have created a forward reference to a
15523            later parameter.  */
15524         if (decl_die != NULL)
15525           {
15526             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15527             break;
15528           }
15529       }
15530       /* FALLTHRU */
15531
15532     default:
15533       {
15534         /* Otherwise try to create a stack operation procedure to
15535            evaluate the value of the array bound.  */
15536
15537         dw_die_ref ctx, decl_die;
15538         dw_loc_list_ref list;
15539
15540         list = loc_list_from_tree (bound, 2);
15541         if (list == NULL || single_element_loc_list_p (list))
15542           {
15543             /* If DW_AT_*bound is not a reference nor constant, it is
15544                a DWARF expression rather than location description.
15545                For that loc_list_from_tree (bound, 0) is needed.
15546                If that fails to give a single element list,
15547                fall back to outputting this as a reference anyway.  */
15548             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15549             if (list2 && single_element_loc_list_p (list2))
15550               {
15551                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15552                 break;
15553               }
15554           }
15555         if (list == NULL)
15556           break;
15557
15558         if (current_function_decl == 0)
15559           ctx = comp_unit_die ();
15560         else
15561           ctx = lookup_decl_die (current_function_decl);
15562
15563         decl_die = new_die (DW_TAG_variable, ctx, bound);
15564         add_AT_flag (decl_die, DW_AT_artificial, 1);
15565         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15566         add_AT_location_description (decl_die, DW_AT_location, list);
15567         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15568         break;
15569       }
15570     }
15571 }
15572
15573 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15574    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15575    Note that the block of subscript information for an array type also
15576    includes information about the element type of the given array type.  */
15577
15578 static void
15579 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15580 {
15581   unsigned dimension_number;
15582   tree lower, upper;
15583   dw_die_ref subrange_die;
15584
15585   for (dimension_number = 0;
15586        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15587        type = TREE_TYPE (type), dimension_number++)
15588     {
15589       tree domain = TYPE_DOMAIN (type);
15590
15591       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15592         break;
15593
15594       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15595          and (in GNU C only) variable bounds.  Handle all three forms
15596          here.  */
15597       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15598       if (domain)
15599         {
15600           /* We have an array type with specified bounds.  */
15601           lower = TYPE_MIN_VALUE (domain);
15602           upper = TYPE_MAX_VALUE (domain);
15603
15604           /* Define the index type.  */
15605           if (TREE_TYPE (domain))
15606             {
15607               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15608                  TREE_TYPE field.  We can't emit debug info for this
15609                  because it is an unnamed integral type.  */
15610               if (TREE_CODE (domain) == INTEGER_TYPE
15611                   && TYPE_NAME (domain) == NULL_TREE
15612                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15613                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15614                 ;
15615               else
15616                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15617                                     type_die);
15618             }
15619
15620           /* ??? If upper is NULL, the array has unspecified length,
15621              but it does have a lower bound.  This happens with Fortran
15622                dimension arr(N:*)
15623              Since the debugger is definitely going to need to know N
15624              to produce useful results, go ahead and output the lower
15625              bound solo, and hope the debugger can cope.  */
15626
15627           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15628           if (upper)
15629             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15630         }
15631
15632       /* Otherwise we have an array type with an unspecified length.  The
15633          DWARF-2 spec does not say how to handle this; let's just leave out the
15634          bounds.  */
15635     }
15636 }
15637
15638 static void
15639 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15640 {
15641   unsigned size;
15642
15643   switch (TREE_CODE (tree_node))
15644     {
15645     case ERROR_MARK:
15646       size = 0;
15647       break;
15648     case ENUMERAL_TYPE:
15649     case RECORD_TYPE:
15650     case UNION_TYPE:
15651     case QUAL_UNION_TYPE:
15652       size = int_size_in_bytes (tree_node);
15653       break;
15654     case FIELD_DECL:
15655       /* For a data member of a struct or union, the DW_AT_byte_size is
15656          generally given as the number of bytes normally allocated for an
15657          object of the *declared* type of the member itself.  This is true
15658          even for bit-fields.  */
15659       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15660       break;
15661     default:
15662       gcc_unreachable ();
15663     }
15664
15665   /* Note that `size' might be -1 when we get to this point.  If it is, that
15666      indicates that the byte size of the entity in question is variable.  We
15667      have no good way of expressing this fact in Dwarf at the present time,
15668      so just let the -1 pass on through.  */
15669   add_AT_unsigned (die, DW_AT_byte_size, size);
15670 }
15671
15672 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15673    which specifies the distance in bits from the highest order bit of the
15674    "containing object" for the bit-field to the highest order bit of the
15675    bit-field itself.
15676
15677    For any given bit-field, the "containing object" is a hypothetical object
15678    (of some integral or enum type) within which the given bit-field lives.  The
15679    type of this hypothetical "containing object" is always the same as the
15680    declared type of the individual bit-field itself.  The determination of the
15681    exact location of the "containing object" for a bit-field is rather
15682    complicated.  It's handled by the `field_byte_offset' function (above).
15683
15684    Note that it is the size (in bytes) of the hypothetical "containing object"
15685    which will be given in the DW_AT_byte_size attribute for this bit-field.
15686    (See `byte_size_attribute' above).  */
15687
15688 static inline void
15689 add_bit_offset_attribute (dw_die_ref die, tree decl)
15690 {
15691   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15692   tree type = DECL_BIT_FIELD_TYPE (decl);
15693   HOST_WIDE_INT bitpos_int;
15694   HOST_WIDE_INT highest_order_object_bit_offset;
15695   HOST_WIDE_INT highest_order_field_bit_offset;
15696   HOST_WIDE_INT bit_offset;
15697
15698   /* Must be a field and a bit field.  */
15699   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15700
15701   /* We can't yet handle bit-fields whose offsets are variable, so if we
15702      encounter such things, just return without generating any attribute
15703      whatsoever.  Likewise for variable or too large size.  */
15704   if (! host_integerp (bit_position (decl), 0)
15705       || ! host_integerp (DECL_SIZE (decl), 1))
15706     return;
15707
15708   bitpos_int = int_bit_position (decl);
15709
15710   /* Note that the bit offset is always the distance (in bits) from the
15711      highest-order bit of the "containing object" to the highest-order bit of
15712      the bit-field itself.  Since the "high-order end" of any object or field
15713      is different on big-endian and little-endian machines, the computation
15714      below must take account of these differences.  */
15715   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15716   highest_order_field_bit_offset = bitpos_int;
15717
15718   if (! BYTES_BIG_ENDIAN)
15719     {
15720       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15721       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15722     }
15723
15724   bit_offset
15725     = (! BYTES_BIG_ENDIAN
15726        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15727        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15728
15729   if (bit_offset < 0)
15730     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15731   else
15732     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15733 }
15734
15735 /* For a FIELD_DECL node which represents a bit field, output an attribute
15736    which specifies the length in bits of the given field.  */
15737
15738 static inline void
15739 add_bit_size_attribute (dw_die_ref die, tree decl)
15740 {
15741   /* Must be a field and a bit field.  */
15742   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15743               && DECL_BIT_FIELD_TYPE (decl));
15744
15745   if (host_integerp (DECL_SIZE (decl), 1))
15746     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15747 }
15748
15749 /* If the compiled language is ANSI C, then add a 'prototyped'
15750    attribute, if arg types are given for the parameters of a function.  */
15751
15752 static inline void
15753 add_prototyped_attribute (dw_die_ref die, tree func_type)
15754 {
15755   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15756       && prototype_p (func_type))
15757     add_AT_flag (die, DW_AT_prototyped, 1);
15758 }
15759
15760 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15761    by looking in either the type declaration or object declaration
15762    equate table.  */
15763
15764 static inline dw_die_ref
15765 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15766 {
15767   dw_die_ref origin_die = NULL;
15768
15769   if (TREE_CODE (origin) != FUNCTION_DECL)
15770     {
15771       /* We may have gotten separated from the block for the inlined
15772          function, if we're in an exception handler or some such; make
15773          sure that the abstract function has been written out.
15774
15775          Doing this for nested functions is wrong, however; functions are
15776          distinct units, and our context might not even be inline.  */
15777       tree fn = origin;
15778
15779       if (TYPE_P (fn))
15780         fn = TYPE_STUB_DECL (fn);
15781
15782       fn = decl_function_context (fn);
15783       if (fn)
15784         dwarf2out_abstract_function (fn);
15785     }
15786
15787   if (DECL_P (origin))
15788     origin_die = lookup_decl_die (origin);
15789   else if (TYPE_P (origin))
15790     origin_die = lookup_type_die (origin);
15791
15792   /* XXX: Functions that are never lowered don't always have correct block
15793      trees (in the case of java, they simply have no block tree, in some other
15794      languages).  For these functions, there is nothing we can really do to
15795      output correct debug info for inlined functions in all cases.  Rather
15796      than die, we'll just produce deficient debug info now, in that we will
15797      have variables without a proper abstract origin.  In the future, when all
15798      functions are lowered, we should re-add a gcc_assert (origin_die)
15799      here.  */
15800
15801   if (origin_die)
15802     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15803   return origin_die;
15804 }
15805
15806 /* We do not currently support the pure_virtual attribute.  */
15807
15808 static inline void
15809 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15810 {
15811   if (DECL_VINDEX (func_decl))
15812     {
15813       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15814
15815       if (host_integerp (DECL_VINDEX (func_decl), 0))
15816         add_AT_loc (die, DW_AT_vtable_elem_location,
15817                     new_loc_descr (DW_OP_constu,
15818                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15819                                    0));
15820
15821       /* GNU extension: Record what type this method came from originally.  */
15822       if (debug_info_level > DINFO_LEVEL_TERSE
15823           && DECL_CONTEXT (func_decl))
15824         add_AT_die_ref (die, DW_AT_containing_type,
15825                         lookup_type_die (DECL_CONTEXT (func_decl)));
15826     }
15827 }
15828 \f
15829 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15830    given decl.  This used to be a vendor extension until after DWARF 4
15831    standardized it.  */
15832
15833 static void
15834 add_linkage_attr (dw_die_ref die, tree decl)
15835 {
15836   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15837
15838   /* Mimic what assemble_name_raw does with a leading '*'.  */
15839   if (name[0] == '*')
15840     name = &name[1];
15841
15842   if (dwarf_version >= 4)
15843     add_AT_string (die, DW_AT_linkage_name, name);
15844   else
15845     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15846 }
15847
15848 /* Add source coordinate attributes for the given decl.  */
15849
15850 static void
15851 add_src_coords_attributes (dw_die_ref die, tree decl)
15852 {
15853   expanded_location s;
15854
15855   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15856     return;
15857   s = expand_location (DECL_SOURCE_LOCATION (decl));
15858   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15859   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15860 }
15861
15862 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15863
15864 static void
15865 add_linkage_name (dw_die_ref die, tree decl)
15866 {
15867   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15868        && TREE_PUBLIC (decl)
15869        && !DECL_ABSTRACT (decl)
15870        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15871        && die->die_tag != DW_TAG_member)
15872     {
15873       /* Defer until we have an assembler name set.  */
15874       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15875         {
15876           limbo_die_node *asm_name;
15877
15878           asm_name = ggc_alloc_cleared_limbo_die_node ();
15879           asm_name->die = die;
15880           asm_name->created_for = decl;
15881           asm_name->next = deferred_asm_name;
15882           deferred_asm_name = asm_name;
15883         }
15884       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15885         add_linkage_attr (die, decl);
15886     }
15887 }
15888
15889 /* Add a DW_AT_name attribute and source coordinate attribute for the
15890    given decl, but only if it actually has a name.  */
15891
15892 static void
15893 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15894 {
15895   tree decl_name;
15896
15897   decl_name = DECL_NAME (decl);
15898   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15899     {
15900       const char *name = dwarf2_name (decl, 0);
15901       if (name)
15902         add_name_attribute (die, name);
15903       if (! DECL_ARTIFICIAL (decl))
15904         add_src_coords_attributes (die, decl);
15905
15906       add_linkage_name (die, decl);
15907     }
15908
15909 #ifdef VMS_DEBUGGING_INFO
15910   /* Get the function's name, as described by its RTL.  This may be different
15911      from the DECL_NAME name used in the source file.  */
15912   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15913     {
15914       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15915                    XEXP (DECL_RTL (decl), 0));
15916       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15917     }
15918 #endif /* VMS_DEBUGGING_INFO */
15919 }
15920
15921 #ifdef VMS_DEBUGGING_INFO
15922 /* Output the debug main pointer die for VMS */
15923
15924 void
15925 dwarf2out_vms_debug_main_pointer (void)
15926 {
15927   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15928   dw_die_ref die;
15929
15930   /* Allocate the VMS debug main subprogram die.  */
15931   die = ggc_alloc_cleared_die_node ();
15932   die->die_tag = DW_TAG_subprogram;
15933   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15934   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15935                                current_function_funcdef_no);
15936   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15937
15938   /* Make it the first child of comp_unit_die ().  */
15939   die->die_parent = comp_unit_die ();
15940   if (comp_unit_die ()->die_child)
15941     {
15942       die->die_sib = comp_unit_die ()->die_child->die_sib;
15943       comp_unit_die ()->die_child->die_sib = die;
15944     }
15945   else
15946     {
15947       die->die_sib = die;
15948       comp_unit_die ()->die_child = die;
15949     }
15950 }
15951 #endif /* VMS_DEBUGGING_INFO */
15952
15953 /* Push a new declaration scope.  */
15954
15955 static void
15956 push_decl_scope (tree scope)
15957 {
15958   VEC_safe_push (tree, gc, decl_scope_table, scope);
15959 }
15960
15961 /* Pop a declaration scope.  */
15962
15963 static inline void
15964 pop_decl_scope (void)
15965 {
15966   VEC_pop (tree, decl_scope_table);
15967 }
15968
15969 /* Return the DIE for the scope that immediately contains this type.
15970    Non-named types get global scope.  Named types nested in other
15971    types get their containing scope if it's open, or global scope
15972    otherwise.  All other types (i.e. function-local named types) get
15973    the current active scope.  */
15974
15975 static dw_die_ref
15976 scope_die_for (tree t, dw_die_ref context_die)
15977 {
15978   dw_die_ref scope_die = NULL;
15979   tree containing_scope;
15980   int i;
15981
15982   /* Non-types always go in the current scope.  */
15983   gcc_assert (TYPE_P (t));
15984
15985   containing_scope = TYPE_CONTEXT (t);
15986
15987   /* Use the containing namespace if it was passed in (for a declaration).  */
15988   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15989     {
15990       if (context_die == lookup_decl_die (containing_scope))
15991         /* OK */;
15992       else
15993         containing_scope = NULL_TREE;
15994     }
15995
15996   /* Ignore function type "scopes" from the C frontend.  They mean that
15997      a tagged type is local to a parmlist of a function declarator, but
15998      that isn't useful to DWARF.  */
15999   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16000     containing_scope = NULL_TREE;
16001
16002   if (SCOPE_FILE_SCOPE_P (containing_scope))
16003     scope_die = comp_unit_die ();
16004   else if (TYPE_P (containing_scope))
16005     {
16006       /* For types, we can just look up the appropriate DIE.  But
16007          first we check to see if we're in the middle of emitting it
16008          so we know where the new DIE should go.  */
16009       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16010         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16011           break;
16012
16013       if (i < 0)
16014         {
16015           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16016                       || TREE_ASM_WRITTEN (containing_scope));
16017           /*We are not in the middle of emitting the type
16018             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16019           scope_die = lookup_type_die (containing_scope);
16020
16021           /* If none of the current dies are suitable, we get file scope.  */
16022           if (scope_die == NULL)
16023             scope_die = comp_unit_die ();
16024         }
16025       else
16026         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16027     }
16028   else
16029     scope_die = context_die;
16030
16031   return scope_die;
16032 }
16033
16034 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16035
16036 static inline int
16037 local_scope_p (dw_die_ref context_die)
16038 {
16039   for (; context_die; context_die = context_die->die_parent)
16040     if (context_die->die_tag == DW_TAG_inlined_subroutine
16041         || context_die->die_tag == DW_TAG_subprogram)
16042       return 1;
16043
16044   return 0;
16045 }
16046
16047 /* Returns nonzero if CONTEXT_DIE is a class.  */
16048
16049 static inline int
16050 class_scope_p (dw_die_ref context_die)
16051 {
16052   return (context_die
16053           && (context_die->die_tag == DW_TAG_structure_type
16054               || context_die->die_tag == DW_TAG_class_type
16055               || context_die->die_tag == DW_TAG_interface_type
16056               || context_die->die_tag == DW_TAG_union_type));
16057 }
16058
16059 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16060    whether or not to treat a DIE in this context as a declaration.  */
16061
16062 static inline int
16063 class_or_namespace_scope_p (dw_die_ref context_die)
16064 {
16065   return (class_scope_p (context_die)
16066           || (context_die && context_die->die_tag == DW_TAG_namespace));
16067 }
16068
16069 /* Many forms of DIEs require a "type description" attribute.  This
16070    routine locates the proper "type descriptor" die for the type given
16071    by 'type', and adds a DW_AT_type attribute below the given die.  */
16072
16073 static void
16074 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16075                     int decl_volatile, dw_die_ref context_die)
16076 {
16077   enum tree_code code  = TREE_CODE (type);
16078   dw_die_ref type_die  = NULL;
16079
16080   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16081      or fixed-point type, use the inner type.  This is because we have no
16082      support for unnamed types in base_type_die.  This can happen if this is
16083      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16084   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16085       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16086     type = TREE_TYPE (type), code = TREE_CODE (type);
16087
16088   if (code == ERROR_MARK
16089       /* Handle a special case.  For functions whose return type is void, we
16090          generate *no* type attribute.  (Note that no object may have type
16091          `void', so this only applies to function return types).  */
16092       || code == VOID_TYPE)
16093     return;
16094
16095   type_die = modified_type_die (type,
16096                                 decl_const || TYPE_READONLY (type),
16097                                 decl_volatile || TYPE_VOLATILE (type),
16098                                 context_die);
16099
16100   if (type_die != NULL)
16101     add_AT_die_ref (object_die, DW_AT_type, type_die);
16102 }
16103
16104 /* Given an object die, add the calling convention attribute for the
16105    function call type.  */
16106 static void
16107 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16108 {
16109   enum dwarf_calling_convention value = DW_CC_normal;
16110
16111   value = ((enum dwarf_calling_convention)
16112            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16113
16114   if (is_fortran ()
16115       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16116     {
16117       /* DWARF 2 doesn't provide a way to identify a program's source-level
16118         entry point.  DW_AT_calling_convention attributes are only meant
16119         to describe functions' calling conventions.  However, lacking a
16120         better way to signal the Fortran main program, we used this for 
16121         a long time, following existing custom.  Now, DWARF 4 has 
16122         DW_AT_main_subprogram, which we add below, but some tools still
16123         rely on the old way, which we thus keep.  */
16124       value = DW_CC_program;
16125
16126       if (dwarf_version >= 4 || !dwarf_strict)
16127         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16128     }
16129
16130   /* Only add the attribute if the backend requests it, and
16131      is not DW_CC_normal.  */
16132   if (value && (value != DW_CC_normal))
16133     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16134 }
16135
16136 /* Given a tree pointer to a struct, class, union, or enum type node, return
16137    a pointer to the (string) tag name for the given type, or zero if the type
16138    was declared without a tag.  */
16139
16140 static const char *
16141 type_tag (const_tree type)
16142 {
16143   const char *name = 0;
16144
16145   if (TYPE_NAME (type) != 0)
16146     {
16147       tree t = 0;
16148
16149       /* Find the IDENTIFIER_NODE for the type name.  */
16150       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16151           && !TYPE_NAMELESS (type))
16152         t = TYPE_NAME (type);
16153
16154       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16155          a TYPE_DECL node, regardless of whether or not a `typedef' was
16156          involved.  */
16157       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16158                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16159         {
16160           /* We want to be extra verbose.  Don't call dwarf_name if
16161              DECL_NAME isn't set.  The default hook for decl_printable_name
16162              doesn't like that, and in this context it's correct to return
16163              0, instead of "<anonymous>" or the like.  */
16164           if (DECL_NAME (TYPE_NAME (type))
16165               && !DECL_NAMELESS (TYPE_NAME (type)))
16166             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16167         }
16168
16169       /* Now get the name as a string, or invent one.  */
16170       if (!name && t != 0)
16171         name = IDENTIFIER_POINTER (t);
16172     }
16173
16174   return (name == 0 || *name == '\0') ? 0 : name;
16175 }
16176
16177 /* Return the type associated with a data member, make a special check
16178    for bit field types.  */
16179
16180 static inline tree
16181 member_declared_type (const_tree member)
16182 {
16183   return (DECL_BIT_FIELD_TYPE (member)
16184           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16185 }
16186
16187 /* Get the decl's label, as described by its RTL. This may be different
16188    from the DECL_NAME name used in the source file.  */
16189
16190 #if 0
16191 static const char *
16192 decl_start_label (tree decl)
16193 {
16194   rtx x;
16195   const char *fnname;
16196
16197   x = DECL_RTL (decl);
16198   gcc_assert (MEM_P (x));
16199
16200   x = XEXP (x, 0);
16201   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16202
16203   fnname = XSTR (x, 0);
16204   return fnname;
16205 }
16206 #endif
16207 \f
16208 /* These routines generate the internal representation of the DIE's for
16209    the compilation unit.  Debugging information is collected by walking
16210    the declaration trees passed in from dwarf2out_decl().  */
16211
16212 static void
16213 gen_array_type_die (tree type, dw_die_ref context_die)
16214 {
16215   dw_die_ref scope_die = scope_die_for (type, context_die);
16216   dw_die_ref array_die;
16217
16218   /* GNU compilers represent multidimensional array types as sequences of one
16219      dimensional array types whose element types are themselves array types.
16220      We sometimes squish that down to a single array_type DIE with multiple
16221      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16222      say that we are allowed to do this kind of compression in C, because
16223      there is no difference between an array of arrays and a multidimensional
16224      array.  We don't do this for Ada to remain as close as possible to the
16225      actual representation, which is especially important against the language
16226      flexibilty wrt arrays of variable size.  */
16227
16228   bool collapse_nested_arrays = !is_ada ();
16229   tree element_type;
16230
16231   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16232      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16233   if (TYPE_STRING_FLAG (type)
16234       && TREE_CODE (type) == ARRAY_TYPE
16235       && is_fortran ()
16236       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16237     {
16238       HOST_WIDE_INT size;
16239
16240       array_die = new_die (DW_TAG_string_type, scope_die, type);
16241       add_name_attribute (array_die, type_tag (type));
16242       equate_type_number_to_die (type, array_die);
16243       size = int_size_in_bytes (type);
16244       if (size >= 0)
16245         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16246       else if (TYPE_DOMAIN (type) != NULL_TREE
16247                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16248                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16249         {
16250           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16251           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16252
16253           size = int_size_in_bytes (TREE_TYPE (szdecl));
16254           if (loc && size > 0)
16255             {
16256               add_AT_location_description (array_die, DW_AT_string_length, loc);
16257               if (size != DWARF2_ADDR_SIZE)
16258                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16259             }
16260         }
16261       return;
16262     }
16263
16264   /* ??? The SGI dwarf reader fails for array of array of enum types
16265      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16266      array type comes before the outer array type.  We thus call gen_type_die
16267      before we new_die and must prevent nested array types collapsing for this
16268      target.  */
16269
16270 #ifdef MIPS_DEBUGGING_INFO
16271   gen_type_die (TREE_TYPE (type), context_die);
16272   collapse_nested_arrays = false;
16273 #endif
16274
16275   array_die = new_die (DW_TAG_array_type, scope_die, type);
16276   add_name_attribute (array_die, type_tag (type));
16277   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16278   if (TYPE_ARTIFICIAL (type))
16279     add_AT_flag (array_die, DW_AT_artificial, 1);
16280   equate_type_number_to_die (type, array_die);
16281
16282   if (TREE_CODE (type) == VECTOR_TYPE)
16283     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16284
16285   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16286   if (is_fortran ()
16287       && TREE_CODE (type) == ARRAY_TYPE
16288       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16289       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16290     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16291
16292 #if 0
16293   /* We default the array ordering.  SDB will probably do
16294      the right things even if DW_AT_ordering is not present.  It's not even
16295      an issue until we start to get into multidimensional arrays anyway.  If
16296      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16297      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16298      and when we find out that we need to put these in, we will only do so
16299      for multidimensional arrays.  */
16300   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16301 #endif
16302
16303 #ifdef MIPS_DEBUGGING_INFO
16304   /* The SGI compilers handle arrays of unknown bound by setting
16305      AT_declaration and not emitting any subrange DIEs.  */
16306   if (TREE_CODE (type) == ARRAY_TYPE
16307       && ! TYPE_DOMAIN (type))
16308     add_AT_flag (array_die, DW_AT_declaration, 1);
16309   else
16310 #endif
16311   if (TREE_CODE (type) == VECTOR_TYPE)
16312     {
16313       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16314       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16315       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16316       add_bound_info (subrange_die, DW_AT_upper_bound,
16317                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16318     }
16319   else
16320     add_subscript_info (array_die, type, collapse_nested_arrays);
16321
16322   /* Add representation of the type of the elements of this array type and
16323      emit the corresponding DIE if we haven't done it already.  */
16324   element_type = TREE_TYPE (type);
16325   if (collapse_nested_arrays)
16326     while (TREE_CODE (element_type) == ARRAY_TYPE)
16327       {
16328         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16329           break;
16330         element_type = TREE_TYPE (element_type);
16331       }
16332
16333 #ifndef MIPS_DEBUGGING_INFO
16334   gen_type_die (element_type, context_die);
16335 #endif
16336
16337   add_type_attribute (array_die, element_type, 0, 0, context_die);
16338
16339   if (get_AT (array_die, DW_AT_name))
16340     add_pubtype (type, array_die);
16341 }
16342
16343 static dw_loc_descr_ref
16344 descr_info_loc (tree val, tree base_decl)
16345 {
16346   HOST_WIDE_INT size;
16347   dw_loc_descr_ref loc, loc2;
16348   enum dwarf_location_atom op;
16349
16350   if (val == base_decl)
16351     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16352
16353   switch (TREE_CODE (val))
16354     {
16355     CASE_CONVERT:
16356       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16357     case VAR_DECL:
16358       return loc_descriptor_from_tree (val, 0);
16359     case INTEGER_CST:
16360       if (host_integerp (val, 0))
16361         return int_loc_descriptor (tree_low_cst (val, 0));
16362       break;
16363     case INDIRECT_REF:
16364       size = int_size_in_bytes (TREE_TYPE (val));
16365       if (size < 0)
16366         break;
16367       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16368       if (!loc)
16369         break;
16370       if (size == DWARF2_ADDR_SIZE)
16371         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16372       else
16373         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16374       return loc;
16375     case POINTER_PLUS_EXPR:
16376     case PLUS_EXPR:
16377       if (host_integerp (TREE_OPERAND (val, 1), 1)
16378           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16379              < 16384)
16380         {
16381           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16382           if (!loc)
16383             break;
16384           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16385         }
16386       else
16387         {
16388           op = DW_OP_plus;
16389         do_binop:
16390           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16391           if (!loc)
16392             break;
16393           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16394           if (!loc2)
16395             break;
16396           add_loc_descr (&loc, loc2);
16397           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16398         }
16399       return loc;
16400     case MINUS_EXPR:
16401       op = DW_OP_minus;
16402       goto do_binop;
16403     case MULT_EXPR:
16404       op = DW_OP_mul;
16405       goto do_binop;
16406     case EQ_EXPR:
16407       op = DW_OP_eq;
16408       goto do_binop;
16409     case NE_EXPR:
16410       op = DW_OP_ne;
16411       goto do_binop;
16412     default:
16413       break;
16414     }
16415   return NULL;
16416 }
16417
16418 static void
16419 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16420                       tree val, tree base_decl)
16421 {
16422   dw_loc_descr_ref loc;
16423
16424   if (host_integerp (val, 0))
16425     {
16426       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16427       return;
16428     }
16429
16430   loc = descr_info_loc (val, base_decl);
16431   if (!loc)
16432     return;
16433
16434   add_AT_loc (die, attr, loc);
16435 }
16436
16437 /* This routine generates DIE for array with hidden descriptor, details
16438    are filled into *info by a langhook.  */
16439
16440 static void
16441 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16442                           dw_die_ref context_die)
16443 {
16444   dw_die_ref scope_die = scope_die_for (type, context_die);
16445   dw_die_ref array_die;
16446   int dim;
16447
16448   array_die = new_die (DW_TAG_array_type, scope_die, type);
16449   add_name_attribute (array_die, type_tag (type));
16450   equate_type_number_to_die (type, array_die);
16451
16452   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16453   if (is_fortran ()
16454       && info->ndimensions >= 2)
16455     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16456
16457   if (info->data_location)
16458     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16459                           info->base_decl);
16460   if (info->associated)
16461     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16462                           info->base_decl);
16463   if (info->allocated)
16464     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16465                           info->base_decl);
16466
16467   for (dim = 0; dim < info->ndimensions; dim++)
16468     {
16469       dw_die_ref subrange_die
16470         = new_die (DW_TAG_subrange_type, array_die, NULL);
16471
16472       if (info->dimen[dim].lower_bound)
16473         {
16474           /* If it is the default value, omit it.  */
16475           int dflt;
16476
16477           if (host_integerp (info->dimen[dim].lower_bound, 0)
16478               && (dflt = lower_bound_default ()) != -1
16479               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16480             ;
16481           else
16482             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16483                                   info->dimen[dim].lower_bound,
16484                                   info->base_decl);
16485         }
16486       if (info->dimen[dim].upper_bound)
16487         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16488                               info->dimen[dim].upper_bound,
16489                               info->base_decl);
16490       if (info->dimen[dim].stride)
16491         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16492                               info->dimen[dim].stride,
16493                               info->base_decl);
16494     }
16495
16496   gen_type_die (info->element_type, context_die);
16497   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16498
16499   if (get_AT (array_die, DW_AT_name))
16500     add_pubtype (type, array_die);
16501 }
16502
16503 #if 0
16504 static void
16505 gen_entry_point_die (tree decl, dw_die_ref context_die)
16506 {
16507   tree origin = decl_ultimate_origin (decl);
16508   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16509
16510   if (origin != NULL)
16511     add_abstract_origin_attribute (decl_die, origin);
16512   else
16513     {
16514       add_name_and_src_coords_attributes (decl_die, decl);
16515       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16516                           0, 0, context_die);
16517     }
16518
16519   if (DECL_ABSTRACT (decl))
16520     equate_decl_number_to_die (decl, decl_die);
16521   else
16522     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16523 }
16524 #endif
16525
16526 /* Walk through the list of incomplete types again, trying once more to
16527    emit full debugging info for them.  */
16528
16529 static void
16530 retry_incomplete_types (void)
16531 {
16532   int i;
16533
16534   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16535     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16536                                   DINFO_USAGE_DIR_USE))
16537       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16538 }
16539
16540 /* Determine what tag to use for a record type.  */
16541
16542 static enum dwarf_tag
16543 record_type_tag (tree type)
16544 {
16545   if (! lang_hooks.types.classify_record)
16546     return DW_TAG_structure_type;
16547
16548   switch (lang_hooks.types.classify_record (type))
16549     {
16550     case RECORD_IS_STRUCT:
16551       return DW_TAG_structure_type;
16552
16553     case RECORD_IS_CLASS:
16554       return DW_TAG_class_type;
16555
16556     case RECORD_IS_INTERFACE:
16557       if (dwarf_version >= 3 || !dwarf_strict)
16558         return DW_TAG_interface_type;
16559       return DW_TAG_structure_type;
16560
16561     default:
16562       gcc_unreachable ();
16563     }
16564 }
16565
16566 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16567    include all of the information about the enumeration values also. Each
16568    enumerated type name/value is listed as a child of the enumerated type
16569    DIE.  */
16570
16571 static dw_die_ref
16572 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16573 {
16574   dw_die_ref type_die = lookup_type_die (type);
16575
16576   if (type_die == NULL)
16577     {
16578       type_die = new_die (DW_TAG_enumeration_type,
16579                           scope_die_for (type, context_die), type);
16580       equate_type_number_to_die (type, type_die);
16581       add_name_attribute (type_die, type_tag (type));
16582       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16583       if (TYPE_ARTIFICIAL (type))
16584         add_AT_flag (type_die, DW_AT_artificial, 1);
16585       if (dwarf_version >= 4 || !dwarf_strict)
16586         {
16587           if (ENUM_IS_SCOPED (type))
16588             add_AT_flag (type_die, DW_AT_enum_class, 1);
16589           if (ENUM_IS_OPAQUE (type))
16590             add_AT_flag (type_die, DW_AT_declaration, 1);
16591         }
16592     }
16593   else if (! TYPE_SIZE (type))
16594     return type_die;
16595   else
16596     remove_AT (type_die, DW_AT_declaration);
16597
16598   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16599      given enum type is incomplete, do not generate the DW_AT_byte_size
16600      attribute or the DW_AT_element_list attribute.  */
16601   if (TYPE_SIZE (type))
16602     {
16603       tree link;
16604
16605       TREE_ASM_WRITTEN (type) = 1;
16606       add_byte_size_attribute (type_die, type);
16607       if (TYPE_STUB_DECL (type) != NULL_TREE)
16608         {
16609           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16610           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16611         }
16612
16613       /* If the first reference to this type was as the return type of an
16614          inline function, then it may not have a parent.  Fix this now.  */
16615       if (type_die->die_parent == NULL)
16616         add_child_die (scope_die_for (type, context_die), type_die);
16617
16618       for (link = TYPE_VALUES (type);
16619            link != NULL; link = TREE_CHAIN (link))
16620         {
16621           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16622           tree value = TREE_VALUE (link);
16623
16624           add_name_attribute (enum_die,
16625                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16626
16627           if (TREE_CODE (value) == CONST_DECL)
16628             value = DECL_INITIAL (value);
16629
16630           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16631             /* DWARF2 does not provide a way of indicating whether or
16632                not enumeration constants are signed or unsigned.  GDB
16633                always assumes the values are signed, so we output all
16634                values as if they were signed.  That means that
16635                enumeration constants with very large unsigned values
16636                will appear to have negative values in the debugger.  */
16637             add_AT_int (enum_die, DW_AT_const_value,
16638                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16639         }
16640     }
16641   else
16642     add_AT_flag (type_die, DW_AT_declaration, 1);
16643
16644   if (get_AT (type_die, DW_AT_name))
16645     add_pubtype (type, type_die);
16646
16647   return type_die;
16648 }
16649
16650 /* Generate a DIE to represent either a real live formal parameter decl or to
16651    represent just the type of some formal parameter position in some function
16652    type.
16653
16654    Note that this routine is a bit unusual because its argument may be a
16655    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16656    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16657    node.  If it's the former then this function is being called to output a
16658    DIE to represent a formal parameter object (or some inlining thereof).  If
16659    it's the latter, then this function is only being called to output a
16660    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16661    argument type of some subprogram type.
16662    If EMIT_NAME_P is true, name and source coordinate attributes
16663    are emitted.  */
16664
16665 static dw_die_ref
16666 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16667                           dw_die_ref context_die)
16668 {
16669   tree node_or_origin = node ? node : origin;
16670   tree ultimate_origin;
16671   dw_die_ref parm_die
16672     = new_die (DW_TAG_formal_parameter, context_die, node);
16673
16674   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16675     {
16676     case tcc_declaration:
16677       ultimate_origin = decl_ultimate_origin (node_or_origin);
16678       if (node || ultimate_origin)
16679         origin = ultimate_origin;
16680       if (origin != NULL)
16681         add_abstract_origin_attribute (parm_die, origin);
16682       else if (emit_name_p)
16683         add_name_and_src_coords_attributes (parm_die, node);
16684       if (origin == NULL
16685           || (! DECL_ABSTRACT (node_or_origin)
16686               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16687                                            decl_function_context
16688                                                             (node_or_origin))))
16689         {
16690           tree type = TREE_TYPE (node_or_origin);
16691           if (decl_by_reference_p (node_or_origin))
16692             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16693                                 context_die);
16694           else
16695             add_type_attribute (parm_die, type,
16696                                 TREE_READONLY (node_or_origin),
16697                                 TREE_THIS_VOLATILE (node_or_origin),
16698                                 context_die);
16699         }
16700       if (origin == NULL && DECL_ARTIFICIAL (node))
16701         add_AT_flag (parm_die, DW_AT_artificial, 1);
16702
16703       if (node && node != origin)
16704         equate_decl_number_to_die (node, parm_die);
16705       if (! DECL_ABSTRACT (node_or_origin))
16706         add_location_or_const_value_attribute (parm_die, node_or_origin,
16707                                                node == NULL, DW_AT_location);
16708
16709       break;
16710
16711     case tcc_type:
16712       /* We were called with some kind of a ..._TYPE node.  */
16713       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16714       break;
16715
16716     default:
16717       gcc_unreachable ();
16718     }
16719
16720   return parm_die;
16721 }
16722
16723 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16724    children DW_TAG_formal_parameter DIEs representing the arguments of the
16725    parameter pack.
16726
16727    PARM_PACK must be a function parameter pack.
16728    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16729    must point to the subsequent arguments of the function PACK_ARG belongs to.
16730    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16731    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16732    following the last one for which a DIE was generated.  */
16733
16734 static dw_die_ref
16735 gen_formal_parameter_pack_die  (tree parm_pack,
16736                                 tree pack_arg,
16737                                 dw_die_ref subr_die,
16738                                 tree *next_arg)
16739 {
16740   tree arg;
16741   dw_die_ref parm_pack_die;
16742
16743   gcc_assert (parm_pack
16744               && lang_hooks.function_parameter_pack_p (parm_pack)
16745               && subr_die);
16746
16747   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16748   add_src_coords_attributes (parm_pack_die, parm_pack);
16749
16750   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16751     {
16752       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16753                                                                  parm_pack))
16754         break;
16755       gen_formal_parameter_die (arg, NULL,
16756                                 false /* Don't emit name attribute.  */,
16757                                 parm_pack_die);
16758     }
16759   if (next_arg)
16760     *next_arg = arg;
16761   return parm_pack_die;
16762 }
16763
16764 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16765    at the end of an (ANSI prototyped) formal parameters list.  */
16766
16767 static void
16768 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16769 {
16770   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16771 }
16772
16773 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16774    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16775    parameters as specified in some function type specification (except for
16776    those which appear as part of a function *definition*).  */
16777
16778 static void
16779 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16780 {
16781   tree link;
16782   tree formal_type = NULL;
16783   tree first_parm_type;
16784   tree arg;
16785
16786   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16787     {
16788       arg = DECL_ARGUMENTS (function_or_method_type);
16789       function_or_method_type = TREE_TYPE (function_or_method_type);
16790     }
16791   else
16792     arg = NULL_TREE;
16793
16794   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16795
16796   /* Make our first pass over the list of formal parameter types and output a
16797      DW_TAG_formal_parameter DIE for each one.  */
16798   for (link = first_parm_type; link; )
16799     {
16800       dw_die_ref parm_die;
16801
16802       formal_type = TREE_VALUE (link);
16803       if (formal_type == void_type_node)
16804         break;
16805
16806       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16807       parm_die = gen_formal_parameter_die (formal_type, NULL,
16808                                            true /* Emit name attribute.  */,
16809                                            context_die);
16810       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16811           && link == first_parm_type)
16812         {
16813           add_AT_flag (parm_die, DW_AT_artificial, 1);
16814           if (dwarf_version >= 3 || !dwarf_strict)
16815             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16816         }
16817       else if (arg && DECL_ARTIFICIAL (arg))
16818         add_AT_flag (parm_die, DW_AT_artificial, 1);
16819
16820       link = TREE_CHAIN (link);
16821       if (arg)
16822         arg = DECL_CHAIN (arg);
16823     }
16824
16825   /* If this function type has an ellipsis, add a
16826      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16827   if (formal_type != void_type_node)
16828     gen_unspecified_parameters_die (function_or_method_type, context_die);
16829
16830   /* Make our second (and final) pass over the list of formal parameter types
16831      and output DIEs to represent those types (as necessary).  */
16832   for (link = TYPE_ARG_TYPES (function_or_method_type);
16833        link && TREE_VALUE (link);
16834        link = TREE_CHAIN (link))
16835     gen_type_die (TREE_VALUE (link), context_die);
16836 }
16837
16838 /* We want to generate the DIE for TYPE so that we can generate the
16839    die for MEMBER, which has been defined; we will need to refer back
16840    to the member declaration nested within TYPE.  If we're trying to
16841    generate minimal debug info for TYPE, processing TYPE won't do the
16842    trick; we need to attach the member declaration by hand.  */
16843
16844 static void
16845 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16846 {
16847   gen_type_die (type, context_die);
16848
16849   /* If we're trying to avoid duplicate debug info, we may not have
16850      emitted the member decl for this function.  Emit it now.  */
16851   if (TYPE_STUB_DECL (type)
16852       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16853       && ! lookup_decl_die (member))
16854     {
16855       dw_die_ref type_die;
16856       gcc_assert (!decl_ultimate_origin (member));
16857
16858       push_decl_scope (type);
16859       type_die = lookup_type_die_strip_naming_typedef (type);
16860       if (TREE_CODE (member) == FUNCTION_DECL)
16861         gen_subprogram_die (member, type_die);
16862       else if (TREE_CODE (member) == FIELD_DECL)
16863         {
16864           /* Ignore the nameless fields that are used to skip bits but handle
16865              C++ anonymous unions and structs.  */
16866           if (DECL_NAME (member) != NULL_TREE
16867               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16868               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16869             {
16870               gen_type_die (member_declared_type (member), type_die);
16871               gen_field_die (member, type_die);
16872             }
16873         }
16874       else
16875         gen_variable_die (member, NULL_TREE, type_die);
16876
16877       pop_decl_scope ();
16878     }
16879 }
16880
16881 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16882    may later generate inlined and/or out-of-line instances of.  */
16883
16884 static void
16885 dwarf2out_abstract_function (tree decl)
16886 {
16887   dw_die_ref old_die;
16888   tree save_fn;
16889   tree context;
16890   int was_abstract;
16891   htab_t old_decl_loc_table;
16892   htab_t old_cached_dw_loc_list_table;
16893   int old_call_site_count, old_tail_call_site_count;
16894   struct call_arg_loc_node *old_call_arg_locations;
16895
16896   /* Make sure we have the actual abstract inline, not a clone.  */
16897   decl = DECL_ORIGIN (decl);
16898
16899   old_die = lookup_decl_die (decl);
16900   if (old_die && get_AT (old_die, DW_AT_inline))
16901     /* We've already generated the abstract instance.  */
16902     return;
16903
16904   /* We can be called while recursively when seeing block defining inlined subroutine
16905      DIE.  Be sure to not clobber the outer location table nor use it or we would
16906      get locations in abstract instantces.  */
16907   old_decl_loc_table = decl_loc_table;
16908   decl_loc_table = NULL;
16909   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16910   cached_dw_loc_list_table = NULL;
16911   old_call_arg_locations = call_arg_locations;
16912   call_arg_locations = NULL;
16913   old_call_site_count = call_site_count;
16914   call_site_count = -1;
16915   old_tail_call_site_count = tail_call_site_count;
16916   tail_call_site_count = -1;
16917
16918   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16919      we don't get confused by DECL_ABSTRACT.  */
16920   if (debug_info_level > DINFO_LEVEL_TERSE)
16921     {
16922       context = decl_class_context (decl);
16923       if (context)
16924         gen_type_die_for_member
16925           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16926     }
16927
16928   /* Pretend we've just finished compiling this function.  */
16929   save_fn = current_function_decl;
16930   current_function_decl = decl;
16931   push_cfun (DECL_STRUCT_FUNCTION (decl));
16932
16933   was_abstract = DECL_ABSTRACT (decl);
16934   set_decl_abstract_flags (decl, 1);
16935   dwarf2out_decl (decl);
16936   if (! was_abstract)
16937     set_decl_abstract_flags (decl, 0);
16938
16939   current_function_decl = save_fn;
16940   decl_loc_table = old_decl_loc_table;
16941   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16942   call_arg_locations = old_call_arg_locations;
16943   call_site_count = old_call_site_count;
16944   tail_call_site_count = old_tail_call_site_count;
16945   pop_cfun ();
16946 }
16947
16948 /* Helper function of premark_used_types() which gets called through
16949    htab_traverse.
16950
16951    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16952    marked as unused by prune_unused_types.  */
16953
16954 static int
16955 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16956 {
16957   tree type;
16958   dw_die_ref die;
16959
16960   type = (tree) *slot;
16961   die = lookup_type_die (type);
16962   if (die != NULL)
16963     die->die_perennial_p = 1;
16964   return 1;
16965 }
16966
16967 /* Helper function of premark_types_used_by_global_vars which gets called
16968    through htab_traverse.
16969
16970    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16971    marked as unused by prune_unused_types. The DIE of the type is marked
16972    only if the global variable using the type will actually be emitted.  */
16973
16974 static int
16975 premark_types_used_by_global_vars_helper (void **slot,
16976                                           void *data ATTRIBUTE_UNUSED)
16977 {
16978   struct types_used_by_vars_entry *entry;
16979   dw_die_ref die;
16980
16981   entry = (struct types_used_by_vars_entry *) *slot;
16982   gcc_assert (entry->type != NULL
16983               && entry->var_decl != NULL);
16984   die = lookup_type_die (entry->type);
16985   if (die)
16986     {
16987       /* Ask cgraph if the global variable really is to be emitted.
16988          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16989       struct varpool_node *node = varpool_get_node (entry->var_decl);
16990       if (node && node->needed)
16991         {
16992           die->die_perennial_p = 1;
16993           /* Keep the parent DIEs as well.  */
16994           while ((die = die->die_parent) && die->die_perennial_p == 0)
16995             die->die_perennial_p = 1;
16996         }
16997     }
16998   return 1;
16999 }
17000
17001 /* Mark all members of used_types_hash as perennial.  */
17002
17003 static void
17004 premark_used_types (void)
17005 {
17006   if (cfun && cfun->used_types_hash)
17007     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17008 }
17009
17010 /* Mark all members of types_used_by_vars_entry as perennial.  */
17011
17012 static void
17013 premark_types_used_by_global_vars (void)
17014 {
17015   if (types_used_by_vars_hash)
17016     htab_traverse (types_used_by_vars_hash,
17017                    premark_types_used_by_global_vars_helper, NULL);
17018 }
17019
17020 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17021    for CA_LOC call arg loc node.  */
17022
17023 static dw_die_ref
17024 gen_call_site_die (tree decl, dw_die_ref subr_die,
17025                    struct call_arg_loc_node *ca_loc)
17026 {
17027   dw_die_ref stmt_die = NULL, die;
17028   tree block = ca_loc->block;
17029
17030   while (block
17031          && block != DECL_INITIAL (decl)
17032          && TREE_CODE (block) == BLOCK)
17033     {
17034       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17035         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17036       if (stmt_die)
17037         break;
17038       block = BLOCK_SUPERCONTEXT (block);
17039     }
17040   if (stmt_die == NULL)
17041     stmt_die = subr_die;
17042   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17043   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17044   if (ca_loc->tail_call_p)
17045     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17046   if (ca_loc->symbol_ref)
17047     {
17048       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17049       if (tdie)
17050         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17051       else
17052         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17053     }
17054   return die;
17055 }
17056
17057 /* Generate a DIE to represent a declared function (either file-scope or
17058    block-local).  */
17059
17060 static void
17061 gen_subprogram_die (tree decl, dw_die_ref context_die)
17062 {
17063   tree origin = decl_ultimate_origin (decl);
17064   dw_die_ref subr_die;
17065   tree outer_scope;
17066   dw_die_ref old_die = lookup_decl_die (decl);
17067   int declaration = (current_function_decl != decl
17068                      || class_or_namespace_scope_p (context_die));
17069
17070   premark_used_types ();
17071
17072   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17073      started to generate the abstract instance of an inline, decided to output
17074      its containing class, and proceeded to emit the declaration of the inline
17075      from the member list for the class.  If so, DECLARATION takes priority;
17076      we'll get back to the abstract instance when done with the class.  */
17077
17078   /* The class-scope declaration DIE must be the primary DIE.  */
17079   if (origin && declaration && class_or_namespace_scope_p (context_die))
17080     {
17081       origin = NULL;
17082       gcc_assert (!old_die);
17083     }
17084
17085   /* Now that the C++ front end lazily declares artificial member fns, we
17086      might need to retrofit the declaration into its class.  */
17087   if (!declaration && !origin && !old_die
17088       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17089       && !class_or_namespace_scope_p (context_die)
17090       && debug_info_level > DINFO_LEVEL_TERSE)
17091     old_die = force_decl_die (decl);
17092
17093   if (origin != NULL)
17094     {
17095       gcc_assert (!declaration || local_scope_p (context_die));
17096
17097       /* Fixup die_parent for the abstract instance of a nested
17098          inline function.  */
17099       if (old_die && old_die->die_parent == NULL)
17100         add_child_die (context_die, old_die);
17101
17102       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17103       add_abstract_origin_attribute (subr_die, origin);
17104       /*  This is where the actual code for a cloned function is.
17105           Let's emit linkage name attribute for it.  This helps
17106           debuggers to e.g, set breakpoints into
17107           constructors/destructors when the user asks "break
17108           K::K".  */
17109       add_linkage_name (subr_die, decl);
17110     }
17111   else if (old_die)
17112     {
17113       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17114       struct dwarf_file_data * file_index = lookup_filename (s.file);
17115
17116       if (!get_AT_flag (old_die, DW_AT_declaration)
17117           /* We can have a normal definition following an inline one in the
17118              case of redefinition of GNU C extern inlines.
17119              It seems reasonable to use AT_specification in this case.  */
17120           && !get_AT (old_die, DW_AT_inline))
17121         {
17122           /* Detect and ignore this case, where we are trying to output
17123              something we have already output.  */
17124           return;
17125         }
17126
17127       /* If the definition comes from the same place as the declaration,
17128          maybe use the old DIE.  We always want the DIE for this function
17129          that has the *_pc attributes to be under comp_unit_die so the
17130          debugger can find it.  We also need to do this for abstract
17131          instances of inlines, since the spec requires the out-of-line copy
17132          to have the same parent.  For local class methods, this doesn't
17133          apply; we just use the old DIE.  */
17134       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17135           && (DECL_ARTIFICIAL (decl)
17136               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17137                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17138                       == (unsigned) s.line))))
17139         {
17140           subr_die = old_die;
17141
17142           /* Clear out the declaration attribute and the formal parameters.
17143              Do not remove all children, because it is possible that this
17144              declaration die was forced using force_decl_die(). In such
17145              cases die that forced declaration die (e.g. TAG_imported_module)
17146              is one of the children that we do not want to remove.  */
17147           remove_AT (subr_die, DW_AT_declaration);
17148           remove_AT (subr_die, DW_AT_object_pointer);
17149           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17150         }
17151       else
17152         {
17153           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17154           add_AT_specification (subr_die, old_die);
17155           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17156             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17157           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17158             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17159         }
17160     }
17161   else
17162     {
17163       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17164
17165       if (TREE_PUBLIC (decl))
17166         add_AT_flag (subr_die, DW_AT_external, 1);
17167
17168       add_name_and_src_coords_attributes (subr_die, decl);
17169       if (debug_info_level > DINFO_LEVEL_TERSE)
17170         {
17171           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17172           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17173                               0, 0, context_die);
17174         }
17175
17176       add_pure_or_virtual_attribute (subr_die, decl);
17177       if (DECL_ARTIFICIAL (decl))
17178         add_AT_flag (subr_die, DW_AT_artificial, 1);
17179
17180       add_accessibility_attribute (subr_die, decl);
17181     }
17182
17183   if (declaration)
17184     {
17185       if (!old_die || !get_AT (old_die, DW_AT_inline))
17186         {
17187           add_AT_flag (subr_die, DW_AT_declaration, 1);
17188
17189           /* If this is an explicit function declaration then generate
17190              a DW_AT_explicit attribute.  */
17191           if (lang_hooks.decls.function_decl_explicit_p (decl)
17192               && (dwarf_version >= 3 || !dwarf_strict))
17193             add_AT_flag (subr_die, DW_AT_explicit, 1);
17194
17195           /* The first time we see a member function, it is in the context of
17196              the class to which it belongs.  We make sure of this by emitting
17197              the class first.  The next time is the definition, which is
17198              handled above.  The two may come from the same source text.
17199
17200              Note that force_decl_die() forces function declaration die. It is
17201              later reused to represent definition.  */
17202           equate_decl_number_to_die (decl, subr_die);
17203         }
17204     }
17205   else if (DECL_ABSTRACT (decl))
17206     {
17207       if (DECL_DECLARED_INLINE_P (decl))
17208         {
17209           if (cgraph_function_possibly_inlined_p (decl))
17210             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17211           else
17212             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17213         }
17214       else
17215         {
17216           if (cgraph_function_possibly_inlined_p (decl))
17217             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17218           else
17219             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17220         }
17221
17222       if (DECL_DECLARED_INLINE_P (decl)
17223           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17224         add_AT_flag (subr_die, DW_AT_artificial, 1);
17225
17226       equate_decl_number_to_die (decl, subr_die);
17227     }
17228   else if (!DECL_EXTERNAL (decl))
17229     {
17230       HOST_WIDE_INT cfa_fb_offset;
17231
17232       if (!old_die || !get_AT (old_die, DW_AT_inline))
17233         equate_decl_number_to_die (decl, subr_die);
17234
17235       if (!flag_reorder_blocks_and_partition)
17236         {
17237           dw_fde_ref fde = cfun->fde;
17238           if (fde->dw_fde_begin)
17239             {
17240               /* We have already generated the labels.  */
17241               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17242               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17243             }
17244           else
17245             {
17246               /* Create start/end labels and add the range.  */
17247               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17248               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17249                                            current_function_funcdef_no);
17250               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17251               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17252                                            current_function_funcdef_no);
17253               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17254             }
17255
17256 #if VMS_DEBUGGING_INFO
17257       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17258          Section 2.3 Prologue and Epilogue Attributes:
17259          When a breakpoint is set on entry to a function, it is generally
17260          desirable for execution to be suspended, not on the very first
17261          instruction of the function, but rather at a point after the
17262          function's frame has been set up, after any language defined local
17263          declaration processing has been completed, and before execution of
17264          the first statement of the function begins. Debuggers generally
17265          cannot properly determine where this point is.  Similarly for a
17266          breakpoint set on exit from a function. The prologue and epilogue
17267          attributes allow a compiler to communicate the location(s) to use.  */
17268
17269       {
17270         if (fde->dw_fde_vms_end_prologue)
17271           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17272             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17273
17274         if (fde->dw_fde_vms_begin_epilogue)
17275           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17276             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17277       }
17278 #endif
17279
17280           add_pubname (decl, subr_die);
17281         }
17282       else
17283         {
17284           /* Generate pubnames entries for the split function code ranges.  */
17285           dw_fde_ref fde = cfun->fde;
17286
17287           if (fde->dw_fde_second_begin)
17288             {
17289               if (dwarf_version >= 3 || !dwarf_strict)
17290                 {
17291                   /* We should use ranges for non-contiguous code section 
17292                      addresses.  Use the actual code range for the initial
17293                      section, since the HOT/COLD labels might precede an 
17294                      alignment offset.  */
17295                   bool range_list_added = false;
17296                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17297                                         fde->dw_fde_end, &range_list_added);
17298                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17299                                         fde->dw_fde_second_end,
17300                                         &range_list_added);
17301                   add_pubname (decl, subr_die);
17302                   if (range_list_added)
17303                     add_ranges (NULL);
17304                 }
17305               else
17306                 {
17307                   /* There is no real support in DW2 for this .. so we make
17308                      a work-around.  First, emit the pub name for the segment
17309                      containing the function label.  Then make and emit a
17310                      simplified subprogram DIE for the second segment with the
17311                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17312                      linkage name for the second die so that gdb will find both
17313                      sections when given "b foo".  */
17314                   const char *name = NULL;
17315                   tree decl_name = DECL_NAME (decl);
17316                   dw_die_ref seg_die;
17317
17318                   /* Do the 'primary' section.   */
17319                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17320                                  fde->dw_fde_begin);
17321                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17322                                  fde->dw_fde_end);
17323                   /* Add it.   */
17324                   add_pubname (decl, subr_die);
17325
17326                   /* Build a minimal DIE for the secondary section.  */
17327                   seg_die = new_die (DW_TAG_subprogram,
17328                                      subr_die->die_parent, decl);
17329
17330                   if (TREE_PUBLIC (decl))
17331                     add_AT_flag (seg_die, DW_AT_external, 1);
17332
17333                   if (decl_name != NULL 
17334                       && IDENTIFIER_POINTER (decl_name) != NULL)
17335                     {
17336                       name = dwarf2_name (decl, 1);
17337                       if (! DECL_ARTIFICIAL (decl))
17338                         add_src_coords_attributes (seg_die, decl);
17339
17340                       add_linkage_name (seg_die, decl);
17341                     }
17342                   gcc_assert (name != NULL);
17343                   add_pure_or_virtual_attribute (seg_die, decl);
17344                   if (DECL_ARTIFICIAL (decl))
17345                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17346
17347                   name = concat ("__second_sect_of_", name, NULL); 
17348                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17349                                  fde->dw_fde_second_begin);
17350                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17351                                  fde->dw_fde_second_end);
17352                   add_name_attribute (seg_die, name);
17353                   add_pubname_string (name, seg_die);
17354                 }
17355             }
17356           else
17357             {
17358               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17359               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17360               add_pubname (decl, subr_die);
17361             }
17362         }
17363
17364 #ifdef MIPS_DEBUGGING_INFO
17365       /* Add a reference to the FDE for this routine.  */
17366       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17367 #endif
17368
17369       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17370
17371       /* We define the "frame base" as the function's CFA.  This is more
17372          convenient for several reasons: (1) It's stable across the prologue
17373          and epilogue, which makes it better than just a frame pointer,
17374          (2) With dwarf3, there exists a one-byte encoding that allows us
17375          to reference the .debug_frame data by proxy, but failing that,
17376          (3) We can at least reuse the code inspection and interpretation
17377          code that determines the CFA position at various points in the
17378          function.  */
17379       if (dwarf_version >= 3)
17380         {
17381           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17382           add_AT_loc (subr_die, DW_AT_frame_base, op);
17383         }
17384       else
17385         {
17386           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17387           if (list->dw_loc_next)
17388             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17389           else
17390             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17391         }
17392
17393       /* Compute a displacement from the "steady-state frame pointer" to
17394          the CFA.  The former is what all stack slots and argument slots
17395          will reference in the rtl; the later is what we've told the
17396          debugger about.  We'll need to adjust all frame_base references
17397          by this displacement.  */
17398       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17399
17400       if (cfun->static_chain_decl)
17401         add_AT_location_description (subr_die, DW_AT_static_link,
17402                  loc_list_from_tree (cfun->static_chain_decl, 2));
17403     }
17404
17405   /* Generate child dies for template paramaters.  */
17406   if (debug_info_level > DINFO_LEVEL_TERSE)
17407     gen_generic_params_dies (decl);
17408
17409   /* Now output descriptions of the arguments for this function. This gets
17410      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17411      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17412      `...' at the end of the formal parameter list.  In order to find out if
17413      there was a trailing ellipsis or not, we must instead look at the type
17414      associated with the FUNCTION_DECL.  This will be a node of type
17415      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17416      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17417      an ellipsis at the end.  */
17418
17419   /* In the case where we are describing a mere function declaration, all we
17420      need to do here (and all we *can* do here) is to describe the *types* of
17421      its formal parameters.  */
17422   if (debug_info_level <= DINFO_LEVEL_TERSE)
17423     ;
17424   else if (declaration)
17425     gen_formal_types_die (decl, subr_die);
17426   else
17427     {
17428       /* Generate DIEs to represent all known formal parameters.  */
17429       tree parm = DECL_ARGUMENTS (decl);
17430       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17431       tree generic_decl_parm = generic_decl
17432                                 ? DECL_ARGUMENTS (generic_decl)
17433                                 : NULL;
17434
17435       /* Now we want to walk the list of parameters of the function and
17436          emit their relevant DIEs.
17437
17438          We consider the case of DECL being an instance of a generic function
17439          as well as it being a normal function.
17440
17441          If DECL is an instance of a generic function we walk the
17442          parameters of the generic function declaration _and_ the parameters of
17443          DECL itself. This is useful because we want to emit specific DIEs for
17444          function parameter packs and those are declared as part of the
17445          generic function declaration. In that particular case,
17446          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17447          That DIE has children DIEs representing the set of arguments
17448          of the pack. Note that the set of pack arguments can be empty.
17449          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17450          children DIE.
17451
17452          Otherwise, we just consider the parameters of DECL.  */
17453       while (generic_decl_parm || parm)
17454         {
17455           if (generic_decl_parm
17456               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17457             gen_formal_parameter_pack_die (generic_decl_parm,
17458                                            parm, subr_die,
17459                                            &parm);
17460           else if (parm)
17461             {
17462               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17463
17464               if (parm == DECL_ARGUMENTS (decl)
17465                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17466                   && parm_die
17467                   && (dwarf_version >= 3 || !dwarf_strict))
17468                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17469
17470               parm = DECL_CHAIN (parm);
17471             }
17472
17473           if (generic_decl_parm)
17474             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17475         }
17476
17477       /* Decide whether we need an unspecified_parameters DIE at the end.
17478          There are 2 more cases to do this for: 1) the ansi ... declaration -
17479          this is detectable when the end of the arg list is not a
17480          void_type_node 2) an unprototyped function declaration (not a
17481          definition).  This just means that we have no info about the
17482          parameters at all.  */
17483       if (prototype_p (TREE_TYPE (decl)))
17484         {
17485           /* This is the prototyped case, check for....  */
17486           if (stdarg_p (TREE_TYPE (decl)))
17487             gen_unspecified_parameters_die (decl, subr_die);
17488         }
17489       else if (DECL_INITIAL (decl) == NULL_TREE)
17490         gen_unspecified_parameters_die (decl, subr_die);
17491     }
17492
17493   /* Output Dwarf info for all of the stuff within the body of the function
17494      (if it has one - it may be just a declaration).  */
17495   outer_scope = DECL_INITIAL (decl);
17496
17497   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17498      a function.  This BLOCK actually represents the outermost binding contour
17499      for the function, i.e. the contour in which the function's formal
17500      parameters and labels get declared. Curiously, it appears that the front
17501      end doesn't actually put the PARM_DECL nodes for the current function onto
17502      the BLOCK_VARS list for this outer scope, but are strung off of the
17503      DECL_ARGUMENTS list for the function instead.
17504
17505      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17506      the LABEL_DECL nodes for the function however, and we output DWARF info
17507      for those in decls_for_scope.  Just within the `outer_scope' there will be
17508      a BLOCK node representing the function's outermost pair of curly braces,
17509      and any blocks used for the base and member initializers of a C++
17510      constructor function.  */
17511   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17512     {
17513       int call_site_note_count = 0;
17514       int tail_call_site_note_count = 0;
17515
17516       /* Emit a DW_TAG_variable DIE for a named return value.  */
17517       if (DECL_NAME (DECL_RESULT (decl)))
17518         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17519
17520       current_function_has_inlines = 0;
17521       decls_for_scope (outer_scope, subr_die, 0);
17522
17523       if (call_arg_locations && !dwarf_strict)
17524         {
17525           struct call_arg_loc_node *ca_loc;
17526           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17527             {
17528               dw_die_ref die = NULL;
17529               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17530               rtx arg, next_arg;
17531
17532               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17533                    arg; arg = next_arg)
17534                 {
17535                   dw_loc_descr_ref reg, val;
17536                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17537                   dw_die_ref cdie, tdie = NULL;
17538
17539                   next_arg = XEXP (arg, 1);
17540                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17541                       && next_arg
17542                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17543                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17544                       && REGNO (XEXP (XEXP (arg, 0), 0))
17545                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17546                     next_arg = XEXP (next_arg, 1);
17547                   if (mode == VOIDmode)
17548                     {
17549                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17550                       if (mode == VOIDmode)
17551                         mode = GET_MODE (XEXP (arg, 0));
17552                     }
17553                   if (mode == VOIDmode || mode == BLKmode)
17554                     continue;
17555                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17556                     {
17557                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17558                       tloc = XEXP (XEXP (arg, 0), 1);
17559                       continue;
17560                     }
17561                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17562                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17563                     {
17564                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17565                       tlocc = XEXP (XEXP (arg, 0), 1);
17566                       continue;
17567                     }
17568                   reg = NULL;
17569                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17570                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17571                                               VAR_INIT_STATUS_INITIALIZED);
17572                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17573                     {
17574                       rtx mem = XEXP (XEXP (arg, 0), 0);
17575                       reg = mem_loc_descriptor (XEXP (mem, 0),
17576                                                 get_address_mode (mem),
17577                                                 GET_MODE (mem),
17578                                                 VAR_INIT_STATUS_INITIALIZED);
17579                     }
17580                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17581                            == DEBUG_PARAMETER_REF)
17582                     {
17583                       tree tdecl
17584                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17585                       tdie = lookup_decl_die (tdecl);
17586                       if (tdie == NULL)
17587                         continue;
17588                     }
17589                   else
17590                     continue;
17591                   if (reg == NULL
17592                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17593                          != DEBUG_PARAMETER_REF)
17594                     continue;
17595                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17596                                             VOIDmode,
17597                                             VAR_INIT_STATUS_INITIALIZED);
17598                   if (val == NULL)
17599                     continue;
17600                   if (die == NULL)
17601                     die = gen_call_site_die (decl, subr_die, ca_loc);
17602                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17603                                   NULL_TREE);
17604                   if (reg != NULL)
17605                     add_AT_loc (cdie, DW_AT_location, reg);
17606                   else if (tdie != NULL)
17607                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17608                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17609                   if (next_arg != XEXP (arg, 1))
17610                     {
17611                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17612                       if (mode == VOIDmode)
17613                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17614                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17615                                                             0), 1),
17616                                                 mode, VOIDmode,
17617                                                 VAR_INIT_STATUS_INITIALIZED);
17618                       if (val != NULL)
17619                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17620                     }
17621                 }
17622               if (die == NULL
17623                   && (ca_loc->symbol_ref || tloc))
17624                 die = gen_call_site_die (decl, subr_die, ca_loc);
17625               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17626                 {
17627                   dw_loc_descr_ref tval = NULL;
17628
17629                   if (tloc != NULL_RTX)
17630                     tval = mem_loc_descriptor (tloc,
17631                                                GET_MODE (tloc) == VOIDmode
17632                                                ? Pmode : GET_MODE (tloc),
17633                                                VOIDmode,
17634                                                VAR_INIT_STATUS_INITIALIZED);
17635                   if (tval)
17636                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17637                   else if (tlocc != NULL_RTX)
17638                     {
17639                       tval = mem_loc_descriptor (tlocc,
17640                                                  GET_MODE (tlocc) == VOIDmode
17641                                                  ? Pmode : GET_MODE (tlocc),
17642                                                  VOIDmode,
17643                                                  VAR_INIT_STATUS_INITIALIZED);
17644                       if (tval)
17645                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17646                                     tval);
17647                     }
17648                 }
17649               if (die != NULL)
17650                 {
17651                   call_site_note_count++;
17652                   if (ca_loc->tail_call_p)
17653                     tail_call_site_note_count++;
17654                 }
17655             }
17656         }
17657       call_arg_locations = NULL;
17658       call_arg_loc_last = NULL;
17659       if (tail_call_site_count >= 0
17660           && tail_call_site_count == tail_call_site_note_count
17661           && !dwarf_strict)
17662         {
17663           if (call_site_count >= 0
17664               && call_site_count == call_site_note_count)
17665             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17666           else
17667             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17668         }
17669       call_site_count = -1;
17670       tail_call_site_count = -1;
17671     }
17672   /* Add the calling convention attribute if requested.  */
17673   add_calling_convention_attribute (subr_die, decl);
17674
17675 }
17676
17677 /* Returns a hash value for X (which really is a die_struct).  */
17678
17679 static hashval_t
17680 common_block_die_table_hash (const void *x)
17681 {
17682   const_dw_die_ref d = (const_dw_die_ref) x;
17683   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17684 }
17685
17686 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17687    as decl_id and die_parent of die_struct Y.  */
17688
17689 static int
17690 common_block_die_table_eq (const void *x, const void *y)
17691 {
17692   const_dw_die_ref d = (const_dw_die_ref) x;
17693   const_dw_die_ref e = (const_dw_die_ref) y;
17694   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17695 }
17696
17697 /* Generate a DIE to represent a declared data object.
17698    Either DECL or ORIGIN must be non-null.  */
17699
17700 static void
17701 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17702 {
17703   HOST_WIDE_INT off;
17704   tree com_decl;
17705   tree decl_or_origin = decl ? decl : origin;
17706   tree ultimate_origin;
17707   dw_die_ref var_die;
17708   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17709   dw_die_ref origin_die;
17710   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17711                       || class_or_namespace_scope_p (context_die));
17712   bool specialization_p = false;
17713
17714   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17715   if (decl || ultimate_origin)
17716     origin = ultimate_origin;
17717   com_decl = fortran_common (decl_or_origin, &off);
17718
17719   /* Symbol in common gets emitted as a child of the common block, in the form
17720      of a data member.  */
17721   if (com_decl)
17722     {
17723       dw_die_ref com_die;
17724       dw_loc_list_ref loc;
17725       die_node com_die_arg;
17726
17727       var_die = lookup_decl_die (decl_or_origin);
17728       if (var_die)
17729         {
17730           if (get_AT (var_die, DW_AT_location) == NULL)
17731             {
17732               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17733               if (loc)
17734                 {
17735                   if (off)
17736                     {
17737                       /* Optimize the common case.  */
17738                       if (single_element_loc_list_p (loc)
17739                           && loc->expr->dw_loc_opc == DW_OP_addr
17740                           && loc->expr->dw_loc_next == NULL
17741                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17742                              == SYMBOL_REF)
17743                         loc->expr->dw_loc_oprnd1.v.val_addr
17744                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17745                         else
17746                           loc_list_plus_const (loc, off);
17747                     }
17748                   add_AT_location_description (var_die, DW_AT_location, loc);
17749                   remove_AT (var_die, DW_AT_declaration);
17750                 }
17751             }
17752           return;
17753         }
17754
17755       if (common_block_die_table == NULL)
17756         common_block_die_table
17757           = htab_create_ggc (10, common_block_die_table_hash,
17758                              common_block_die_table_eq, NULL);
17759
17760       com_die_arg.decl_id = DECL_UID (com_decl);
17761       com_die_arg.die_parent = context_die;
17762       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17763       loc = loc_list_from_tree (com_decl, 2);
17764       if (com_die == NULL)
17765         {
17766           const char *cnam
17767             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17768           void **slot;
17769
17770           com_die = new_die (DW_TAG_common_block, context_die, decl);
17771           add_name_and_src_coords_attributes (com_die, com_decl);
17772           if (loc)
17773             {
17774               add_AT_location_description (com_die, DW_AT_location, loc);
17775               /* Avoid sharing the same loc descriptor between
17776                  DW_TAG_common_block and DW_TAG_variable.  */
17777               loc = loc_list_from_tree (com_decl, 2);
17778             }
17779           else if (DECL_EXTERNAL (decl))
17780             add_AT_flag (com_die, DW_AT_declaration, 1);
17781           add_pubname_string (cnam, com_die); /* ??? needed? */
17782           com_die->decl_id = DECL_UID (com_decl);
17783           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17784           *slot = (void *) com_die;
17785         }
17786       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17787         {
17788           add_AT_location_description (com_die, DW_AT_location, loc);
17789           loc = loc_list_from_tree (com_decl, 2);
17790           remove_AT (com_die, DW_AT_declaration);
17791         }
17792       var_die = new_die (DW_TAG_variable, com_die, decl);
17793       add_name_and_src_coords_attributes (var_die, decl);
17794       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17795                           TREE_THIS_VOLATILE (decl), context_die);
17796       add_AT_flag (var_die, DW_AT_external, 1);
17797       if (loc)
17798         {
17799           if (off)
17800             {
17801               /* Optimize the common case.  */
17802               if (single_element_loc_list_p (loc)
17803                   && loc->expr->dw_loc_opc == DW_OP_addr
17804                   && loc->expr->dw_loc_next == NULL
17805                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17806                 loc->expr->dw_loc_oprnd1.v.val_addr
17807                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17808               else
17809                 loc_list_plus_const (loc, off);
17810             }
17811           add_AT_location_description (var_die, DW_AT_location, loc);
17812         }
17813       else if (DECL_EXTERNAL (decl))
17814         add_AT_flag (var_die, DW_AT_declaration, 1);
17815       equate_decl_number_to_die (decl, var_die);
17816       return;
17817     }
17818
17819   /* If the compiler emitted a definition for the DECL declaration
17820      and if we already emitted a DIE for it, don't emit a second
17821      DIE for it again. Allow re-declarations of DECLs that are
17822      inside functions, though.  */
17823   if (old_die && declaration && !local_scope_p (context_die))
17824     return;
17825
17826   /* For static data members, the declaration in the class is supposed
17827      to have DW_TAG_member tag; the specification should still be
17828      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17829   if (declaration && class_scope_p (context_die))
17830     var_die = new_die (DW_TAG_member, context_die, decl);
17831   else
17832     var_die = new_die (DW_TAG_variable, context_die, decl);
17833
17834   origin_die = NULL;
17835   if (origin != NULL)
17836     origin_die = add_abstract_origin_attribute (var_die, origin);
17837
17838   /* Loop unrolling can create multiple blocks that refer to the same
17839      static variable, so we must test for the DW_AT_declaration flag.
17840
17841      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17842      copy decls and set the DECL_ABSTRACT flag on them instead of
17843      sharing them.
17844
17845      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17846
17847      ??? The declare_in_namespace support causes us to get two DIEs for one
17848      variable, both of which are declarations.  We want to avoid considering
17849      one to be a specification, so we must test that this DIE is not a
17850      declaration.  */
17851   else if (old_die && TREE_STATIC (decl) && ! declaration
17852            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17853     {
17854       /* This is a definition of a C++ class level static.  */
17855       add_AT_specification (var_die, old_die);
17856       specialization_p = true;
17857       if (DECL_NAME (decl))
17858         {
17859           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17860           struct dwarf_file_data * file_index = lookup_filename (s.file);
17861
17862           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17863             add_AT_file (var_die, DW_AT_decl_file, file_index);
17864
17865           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17866             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17867
17868           if (old_die->die_tag == DW_TAG_member)
17869             add_linkage_name (var_die, decl);
17870         }
17871     }
17872   else
17873     add_name_and_src_coords_attributes (var_die, decl);
17874
17875   if ((origin == NULL && !specialization_p)
17876       || (origin != NULL
17877           && !DECL_ABSTRACT (decl_or_origin)
17878           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17879                                        decl_function_context
17880                                                         (decl_or_origin))))
17881     {
17882       tree type = TREE_TYPE (decl_or_origin);
17883
17884       if (decl_by_reference_p (decl_or_origin))
17885         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17886       else
17887         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17888                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17889     }
17890
17891   if (origin == NULL && !specialization_p)
17892     {
17893       if (TREE_PUBLIC (decl))
17894         add_AT_flag (var_die, DW_AT_external, 1);
17895
17896       if (DECL_ARTIFICIAL (decl))
17897         add_AT_flag (var_die, DW_AT_artificial, 1);
17898
17899       add_accessibility_attribute (var_die, decl);
17900     }
17901
17902   if (declaration)
17903     add_AT_flag (var_die, DW_AT_declaration, 1);
17904
17905   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17906     equate_decl_number_to_die (decl, var_die);
17907
17908   if (! declaration
17909       && (! DECL_ABSTRACT (decl_or_origin)
17910           /* Local static vars are shared between all clones/inlines,
17911              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17912              already set.  */
17913           || (TREE_CODE (decl_or_origin) == VAR_DECL
17914               && TREE_STATIC (decl_or_origin)
17915               && DECL_RTL_SET_P (decl_or_origin)))
17916       /* When abstract origin already has DW_AT_location attribute, no need
17917          to add it again.  */
17918       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17919     {
17920       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17921           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17922         defer_location (decl_or_origin, var_die);
17923       else
17924         add_location_or_const_value_attribute (var_die, decl_or_origin,
17925                                                decl == NULL, DW_AT_location);
17926       add_pubname (decl_or_origin, var_die);
17927     }
17928   else
17929     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17930 }
17931
17932 /* Generate a DIE to represent a named constant.  */
17933
17934 static void
17935 gen_const_die (tree decl, dw_die_ref context_die)
17936 {
17937   dw_die_ref const_die;
17938   tree type = TREE_TYPE (decl);
17939
17940   const_die = new_die (DW_TAG_constant, context_die, decl);
17941   add_name_and_src_coords_attributes (const_die, decl);
17942   add_type_attribute (const_die, type, 1, 0, context_die);
17943   if (TREE_PUBLIC (decl))
17944     add_AT_flag (const_die, DW_AT_external, 1);
17945   if (DECL_ARTIFICIAL (decl))
17946     add_AT_flag (const_die, DW_AT_artificial, 1);
17947   tree_add_const_value_attribute_for_decl (const_die, decl);
17948 }
17949
17950 /* Generate a DIE to represent a label identifier.  */
17951
17952 static void
17953 gen_label_die (tree decl, dw_die_ref context_die)
17954 {
17955   tree origin = decl_ultimate_origin (decl);
17956   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17957   rtx insn;
17958   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17959
17960   if (origin != NULL)
17961     add_abstract_origin_attribute (lbl_die, origin);
17962   else
17963     add_name_and_src_coords_attributes (lbl_die, decl);
17964
17965   if (DECL_ABSTRACT (decl))
17966     equate_decl_number_to_die (decl, lbl_die);
17967   else
17968     {
17969       insn = DECL_RTL_IF_SET (decl);
17970
17971       /* Deleted labels are programmer specified labels which have been
17972          eliminated because of various optimizations.  We still emit them
17973          here so that it is possible to put breakpoints on them.  */
17974       if (insn
17975           && (LABEL_P (insn)
17976               || ((NOTE_P (insn)
17977                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17978         {
17979           /* When optimization is enabled (via -O) some parts of the compiler
17980              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17981              represent source-level labels which were explicitly declared by
17982              the user.  This really shouldn't be happening though, so catch
17983              it if it ever does happen.  */
17984           gcc_assert (!INSN_DELETED_P (insn));
17985
17986           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17987           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17988         }
17989     }
17990 }
17991
17992 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17993    attributes to the DIE for a block STMT, to describe where the inlined
17994    function was called from.  This is similar to add_src_coords_attributes.  */
17995
17996 static inline void
17997 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17998 {
17999   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18000
18001   if (dwarf_version >= 3 || !dwarf_strict)
18002     {
18003       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18004       add_AT_unsigned (die, DW_AT_call_line, s.line);
18005     }
18006 }
18007
18008
18009 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18010    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18011
18012 static inline void
18013 add_high_low_attributes (tree stmt, dw_die_ref die)
18014 {
18015   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18016
18017   if (BLOCK_FRAGMENT_CHAIN (stmt)
18018       && (dwarf_version >= 3 || !dwarf_strict))
18019     {
18020       tree chain;
18021
18022       if (inlined_function_outer_scope_p (stmt))
18023         {
18024           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18025                                        BLOCK_NUMBER (stmt));
18026           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18027         }
18028
18029       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18030
18031       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18032       do
18033         {
18034           add_ranges (chain);
18035           chain = BLOCK_FRAGMENT_CHAIN (chain);
18036         }
18037       while (chain);
18038       add_ranges (NULL);
18039     }
18040   else
18041     {
18042       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18043                                    BLOCK_NUMBER (stmt));
18044       add_AT_lbl_id (die, DW_AT_low_pc, label);
18045       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18046                                    BLOCK_NUMBER (stmt));
18047       add_AT_lbl_id (die, DW_AT_high_pc, label);
18048     }
18049 }
18050
18051 /* Generate a DIE for a lexical block.  */
18052
18053 static void
18054 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18055 {
18056   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18057
18058   if (call_arg_locations)
18059     {
18060       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18061         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18062                                BLOCK_NUMBER (stmt) + 1);
18063       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18064     }
18065
18066   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18067     add_high_low_attributes (stmt, stmt_die);
18068
18069   decls_for_scope (stmt, stmt_die, depth);
18070 }
18071
18072 /* Generate a DIE for an inlined subprogram.  */
18073
18074 static void
18075 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18076 {
18077   tree decl;
18078
18079   /* The instance of function that is effectively being inlined shall not
18080      be abstract.  */
18081   gcc_assert (! BLOCK_ABSTRACT (stmt));
18082
18083   decl = block_ultimate_origin (stmt);
18084
18085   /* Emit info for the abstract instance first, if we haven't yet.  We
18086      must emit this even if the block is abstract, otherwise when we
18087      emit the block below (or elsewhere), we may end up trying to emit
18088      a die whose origin die hasn't been emitted, and crashing.  */
18089   dwarf2out_abstract_function (decl);
18090
18091   if (! BLOCK_ABSTRACT (stmt))
18092     {
18093       dw_die_ref subr_die
18094         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18095
18096       if (call_arg_locations)
18097         {
18098           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18099             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18100                                    BLOCK_NUMBER (stmt) + 1);
18101           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18102         }
18103       add_abstract_origin_attribute (subr_die, decl);
18104       if (TREE_ASM_WRITTEN (stmt))
18105         add_high_low_attributes (stmt, subr_die);
18106       add_call_src_coords_attributes (stmt, subr_die);
18107
18108       decls_for_scope (stmt, subr_die, depth);
18109       current_function_has_inlines = 1;
18110     }
18111 }
18112
18113 /* Generate a DIE for a field in a record, or structure.  */
18114
18115 static void
18116 gen_field_die (tree decl, dw_die_ref context_die)
18117 {
18118   dw_die_ref decl_die;
18119
18120   if (TREE_TYPE (decl) == error_mark_node)
18121     return;
18122
18123   decl_die = new_die (DW_TAG_member, context_die, decl);
18124   add_name_and_src_coords_attributes (decl_die, decl);
18125   add_type_attribute (decl_die, member_declared_type (decl),
18126                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18127                       context_die);
18128
18129   if (DECL_BIT_FIELD_TYPE (decl))
18130     {
18131       add_byte_size_attribute (decl_die, decl);
18132       add_bit_size_attribute (decl_die, decl);
18133       add_bit_offset_attribute (decl_die, decl);
18134     }
18135
18136   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18137     add_data_member_location_attribute (decl_die, decl);
18138
18139   if (DECL_ARTIFICIAL (decl))
18140     add_AT_flag (decl_die, DW_AT_artificial, 1);
18141
18142   add_accessibility_attribute (decl_die, decl);
18143
18144   /* Equate decl number to die, so that we can look up this decl later on.  */
18145   equate_decl_number_to_die (decl, decl_die);
18146 }
18147
18148 #if 0
18149 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18150    Use modified_type_die instead.
18151    We keep this code here just in case these types of DIEs may be needed to
18152    represent certain things in other languages (e.g. Pascal) someday.  */
18153
18154 static void
18155 gen_pointer_type_die (tree type, dw_die_ref context_die)
18156 {
18157   dw_die_ref ptr_die
18158     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18159
18160   equate_type_number_to_die (type, ptr_die);
18161   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18162   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18163 }
18164
18165 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18166    Use modified_type_die instead.
18167    We keep this code here just in case these types of DIEs may be needed to
18168    represent certain things in other languages (e.g. Pascal) someday.  */
18169
18170 static void
18171 gen_reference_type_die (tree type, dw_die_ref context_die)
18172 {
18173   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18174
18175   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18176     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18177   else
18178     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18179
18180   equate_type_number_to_die (type, ref_die);
18181   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18182   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18183 }
18184 #endif
18185
18186 /* Generate a DIE for a pointer to a member type.  */
18187
18188 static void
18189 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18190 {
18191   dw_die_ref ptr_die
18192     = new_die (DW_TAG_ptr_to_member_type,
18193                scope_die_for (type, context_die), type);
18194
18195   equate_type_number_to_die (type, ptr_die);
18196   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18197                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18198   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18199 }
18200
18201 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18202 DEF_VEC_P(dchar_p);
18203 DEF_VEC_ALLOC_P(dchar_p,heap);
18204
18205 static char *producer_string;
18206
18207 /* Return a heap allocated producer string including command line options
18208    if -grecord-gcc-switches.  */
18209
18210 static char *
18211 gen_producer_string (void)
18212 {
18213   size_t j;
18214   VEC(dchar_p, heap) *switches = NULL;
18215   const char *language_string = lang_hooks.name;
18216   char *producer, *tail;
18217   const char *p;
18218   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18219   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18220
18221   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18222     switch (save_decoded_options[j].opt_index)
18223       {
18224       case OPT_o:
18225       case OPT_d:
18226       case OPT_dumpbase:
18227       case OPT_dumpdir:
18228       case OPT_auxbase:
18229       case OPT_auxbase_strip:
18230       case OPT_quiet:
18231       case OPT_version:
18232       case OPT_v:
18233       case OPT_w:
18234       case OPT_L:
18235       case OPT_D:
18236       case OPT_I:
18237       case OPT_U:
18238       case OPT_SPECIAL_unknown:
18239       case OPT_SPECIAL_ignore:
18240       case OPT_SPECIAL_program_name:
18241       case OPT_SPECIAL_input_file:
18242       case OPT_grecord_gcc_switches:
18243       case OPT_gno_record_gcc_switches:
18244       case OPT__output_pch_:
18245       case OPT_fdiagnostics_show_location_:
18246       case OPT_fdiagnostics_show_option:
18247       case OPT____:
18248       case OPT__sysroot_:
18249       case OPT_nostdinc:
18250       case OPT_nostdinc__:
18251         /* Ignore these.  */
18252         continue;
18253       default:
18254         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18255                              == '-');
18256         switch (save_decoded_options[j].canonical_option[0][1])
18257           {
18258           case 'M':
18259           case 'i':
18260           case 'W':
18261             continue;
18262           case 'f':
18263             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18264                          "dump", 4) == 0)
18265               continue;
18266             break;
18267           default:
18268             break;
18269           }
18270         VEC_safe_push (dchar_p, heap, switches,
18271                        save_decoded_options[j].orig_option_with_args_text);
18272         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18273         break;
18274       }
18275
18276   producer = XNEWVEC (char, plen + 1 + len + 1);
18277   tail = producer;
18278   sprintf (tail, "%s %s", language_string, version_string);
18279   tail += plen;
18280
18281   if (!dwarf_record_gcc_switches)
18282     {
18283 #ifdef MIPS_DEBUGGING_INFO
18284       /* The MIPS/SGI compilers place the 'cc' command line options in the
18285          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18286          if they do not appear in the producer string, the debugger reaches
18287          the conclusion that the object file is stripped and has no debugging
18288          information.  To get the MIPS/SGI debugger to believe that there is
18289          debugging information in the object file, we add a -g to the producer
18290          string.  */
18291       if (debug_info_level > DINFO_LEVEL_TERSE)
18292         {
18293           memcpy (tail, " -g", 3);
18294           tail += 3;
18295         }
18296 #endif
18297     }
18298
18299   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18300     {
18301       len = strlen (p);
18302       *tail = ' ';
18303       memcpy (tail + 1, p, len);
18304       tail += len + 1;
18305     }
18306
18307   *tail = '\0';
18308   VEC_free (dchar_p, heap, switches);
18309   return producer;
18310 }
18311
18312 /* Generate the DIE for the compilation unit.  */
18313
18314 static dw_die_ref
18315 gen_compile_unit_die (const char *filename)
18316 {
18317   dw_die_ref die;
18318   const char *language_string = lang_hooks.name;
18319   int language;
18320
18321   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18322
18323   if (filename)
18324     {
18325       add_name_attribute (die, filename);
18326       /* Don't add cwd for <built-in>.  */
18327       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18328         add_comp_dir_attribute (die);
18329     }
18330
18331   if (producer_string == NULL)
18332     producer_string = gen_producer_string ();
18333   add_AT_string (die, DW_AT_producer, producer_string);
18334
18335   /* If our producer is LTO try to figure out a common language to use
18336      from the global list of translation units.  */
18337   if (strcmp (language_string, "GNU GIMPLE") == 0)
18338     {
18339       unsigned i;
18340       tree t;
18341       const char *common_lang = NULL;
18342
18343       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18344         {
18345           if (!TRANSLATION_UNIT_LANGUAGE (t))
18346             continue;
18347           if (!common_lang)
18348             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18349           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18350             ;
18351           else if (strncmp (common_lang, "GNU C", 5) == 0
18352                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18353             /* Mixing C and C++ is ok, use C++ in that case.  */
18354             common_lang = "GNU C++";
18355           else
18356             {
18357               /* Fall back to C.  */
18358               common_lang = NULL;
18359               break;
18360             }
18361         }
18362
18363       if (common_lang)
18364         language_string = common_lang;
18365     }
18366
18367   language = DW_LANG_C89;
18368   if (strcmp (language_string, "GNU C++") == 0)
18369     language = DW_LANG_C_plus_plus;
18370   else if (strcmp (language_string, "GNU F77") == 0)
18371     language = DW_LANG_Fortran77;
18372   else if (strcmp (language_string, "GNU Pascal") == 0)
18373     language = DW_LANG_Pascal83;
18374   else if (dwarf_version >= 3 || !dwarf_strict)
18375     {
18376       if (strcmp (language_string, "GNU Ada") == 0)
18377         language = DW_LANG_Ada95;
18378       else if (strcmp (language_string, "GNU Fortran") == 0)
18379         language = DW_LANG_Fortran95;
18380       else if (strcmp (language_string, "GNU Java") == 0)
18381         language = DW_LANG_Java;
18382       else if (strcmp (language_string, "GNU Objective-C") == 0)
18383         language = DW_LANG_ObjC;
18384       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18385         language = DW_LANG_ObjC_plus_plus;
18386     }
18387
18388   add_AT_unsigned (die, DW_AT_language, language);
18389
18390   switch (language)
18391     {
18392     case DW_LANG_Fortran77:
18393     case DW_LANG_Fortran90:
18394     case DW_LANG_Fortran95:
18395       /* Fortran has case insensitive identifiers and the front-end
18396          lowercases everything.  */
18397       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18398       break;
18399     default:
18400       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18401       break;
18402     }
18403   return die;
18404 }
18405
18406 /* Generate the DIE for a base class.  */
18407
18408 static void
18409 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18410 {
18411   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18412
18413   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18414   add_data_member_location_attribute (die, binfo);
18415
18416   if (BINFO_VIRTUAL_P (binfo))
18417     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18418
18419   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18420      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18421      the default has always been DW_ACCESS_private.  */
18422   if (access == access_public_node)
18423     {
18424       if (dwarf_version == 2
18425           || context_die->die_tag == DW_TAG_class_type)
18426       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18427     }
18428   else if (access == access_protected_node)
18429     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18430   else if (dwarf_version > 2
18431            && context_die->die_tag != DW_TAG_class_type)
18432     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18433 }
18434
18435 /* Generate a DIE for a class member.  */
18436
18437 static void
18438 gen_member_die (tree type, dw_die_ref context_die)
18439 {
18440   tree member;
18441   tree binfo = TYPE_BINFO (type);
18442   dw_die_ref child;
18443
18444   /* If this is not an incomplete type, output descriptions of each of its
18445      members. Note that as we output the DIEs necessary to represent the
18446      members of this record or union type, we will also be trying to output
18447      DIEs to represent the *types* of those members. However the `type'
18448      function (above) will specifically avoid generating type DIEs for member
18449      types *within* the list of member DIEs for this (containing) type except
18450      for those types (of members) which are explicitly marked as also being
18451      members of this (containing) type themselves.  The g++ front- end can
18452      force any given type to be treated as a member of some other (containing)
18453      type by setting the TYPE_CONTEXT of the given (member) type to point to
18454      the TREE node representing the appropriate (containing) type.  */
18455
18456   /* First output info about the base classes.  */
18457   if (binfo)
18458     {
18459       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18460       int i;
18461       tree base;
18462
18463       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18464         gen_inheritance_die (base,
18465                              (accesses ? VEC_index (tree, accesses, i)
18466                               : access_public_node), context_die);
18467     }
18468
18469   /* Now output info about the data members and type members.  */
18470   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18471     {
18472       /* If we thought we were generating minimal debug info for TYPE
18473          and then changed our minds, some of the member declarations
18474          may have already been defined.  Don't define them again, but
18475          do put them in the right order.  */
18476
18477       child = lookup_decl_die (member);
18478       if (child)
18479         splice_child_die (context_die, child);
18480       else
18481         gen_decl_die (member, NULL, context_die);
18482     }
18483
18484   /* Now output info about the function members (if any).  */
18485   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18486     {
18487       /* Don't include clones in the member list.  */
18488       if (DECL_ABSTRACT_ORIGIN (member))
18489         continue;
18490
18491       child = lookup_decl_die (member);
18492       if (child)
18493         splice_child_die (context_die, child);
18494       else
18495         gen_decl_die (member, NULL, context_die);
18496     }
18497 }
18498
18499 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18500    is set, we pretend that the type was never defined, so we only get the
18501    member DIEs needed by later specification DIEs.  */
18502
18503 static void
18504 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18505                                 enum debug_info_usage usage)
18506 {
18507   dw_die_ref type_die = lookup_type_die (type);
18508   dw_die_ref scope_die = 0;
18509   int nested = 0;
18510   int complete = (TYPE_SIZE (type)
18511                   && (! TYPE_STUB_DECL (type)
18512                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18513   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18514   complete = complete && should_emit_struct_debug (type, usage);
18515
18516   if (type_die && ! complete)
18517     return;
18518
18519   if (TYPE_CONTEXT (type) != NULL_TREE
18520       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18521           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18522     nested = 1;
18523
18524   scope_die = scope_die_for (type, context_die);
18525
18526   if (! type_die || (nested && is_cu_die (scope_die)))
18527     /* First occurrence of type or toplevel definition of nested class.  */
18528     {
18529       dw_die_ref old_die = type_die;
18530
18531       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18532                           ? record_type_tag (type) : DW_TAG_union_type,
18533                           scope_die, type);
18534       equate_type_number_to_die (type, type_die);
18535       if (old_die)
18536         add_AT_specification (type_die, old_die);
18537       else
18538         {
18539           add_name_attribute (type_die, type_tag (type));
18540           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18541           if (TYPE_ARTIFICIAL (type))
18542             add_AT_flag (type_die, DW_AT_artificial, 1);
18543         }
18544     }
18545   else
18546     remove_AT (type_die, DW_AT_declaration);
18547
18548   /* Generate child dies for template paramaters.  */
18549   if (debug_info_level > DINFO_LEVEL_TERSE
18550       && COMPLETE_TYPE_P (type))
18551     schedule_generic_params_dies_gen (type);
18552
18553   /* If this type has been completed, then give it a byte_size attribute and
18554      then give a list of members.  */
18555   if (complete && !ns_decl)
18556     {
18557       /* Prevent infinite recursion in cases where the type of some member of
18558          this type is expressed in terms of this type itself.  */
18559       TREE_ASM_WRITTEN (type) = 1;
18560       add_byte_size_attribute (type_die, type);
18561       if (TYPE_STUB_DECL (type) != NULL_TREE)
18562         {
18563           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18564           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18565         }
18566
18567       /* If the first reference to this type was as the return type of an
18568          inline function, then it may not have a parent.  Fix this now.  */
18569       if (type_die->die_parent == NULL)
18570         add_child_die (scope_die, type_die);
18571
18572       push_decl_scope (type);
18573       gen_member_die (type, type_die);
18574       pop_decl_scope ();
18575
18576       /* GNU extension: Record what type our vtable lives in.  */
18577       if (TYPE_VFIELD (type))
18578         {
18579           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18580
18581           gen_type_die (vtype, context_die);
18582           add_AT_die_ref (type_die, DW_AT_containing_type,
18583                           lookup_type_die (vtype));
18584         }
18585     }
18586   else
18587     {
18588       add_AT_flag (type_die, DW_AT_declaration, 1);
18589
18590       /* We don't need to do this for function-local types.  */
18591       if (TYPE_STUB_DECL (type)
18592           && ! decl_function_context (TYPE_STUB_DECL (type)))
18593         VEC_safe_push (tree, gc, incomplete_types, type);
18594     }
18595
18596   if (get_AT (type_die, DW_AT_name))
18597     add_pubtype (type, type_die);
18598 }
18599
18600 /* Generate a DIE for a subroutine _type_.  */
18601
18602 static void
18603 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18604 {
18605   tree return_type = TREE_TYPE (type);
18606   dw_die_ref subr_die
18607     = new_die (DW_TAG_subroutine_type,
18608                scope_die_for (type, context_die), type);
18609
18610   equate_type_number_to_die (type, subr_die);
18611   add_prototyped_attribute (subr_die, type);
18612   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18613   gen_formal_types_die (type, subr_die);
18614
18615   if (get_AT (subr_die, DW_AT_name))
18616     add_pubtype (type, subr_die);
18617 }
18618
18619 /* Generate a DIE for a type definition.  */
18620
18621 static void
18622 gen_typedef_die (tree decl, dw_die_ref context_die)
18623 {
18624   dw_die_ref type_die;
18625   tree origin;
18626
18627   if (TREE_ASM_WRITTEN (decl))
18628     return;
18629
18630   TREE_ASM_WRITTEN (decl) = 1;
18631   type_die = new_die (DW_TAG_typedef, context_die, decl);
18632   origin = decl_ultimate_origin (decl);
18633   if (origin != NULL)
18634     add_abstract_origin_attribute (type_die, origin);
18635   else
18636     {
18637       tree type;
18638
18639       add_name_and_src_coords_attributes (type_die, decl);
18640       if (DECL_ORIGINAL_TYPE (decl))
18641         {
18642           type = DECL_ORIGINAL_TYPE (decl);
18643
18644           gcc_assert (type != TREE_TYPE (decl));
18645           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18646         }
18647       else
18648         {
18649           type = TREE_TYPE (decl);
18650
18651           if (is_naming_typedef_decl (TYPE_NAME (type)))
18652             {
18653               /* Here, we are in the case of decl being a typedef naming
18654                  an anonymous type, e.g:
18655                      typedef struct {...} foo;
18656                  In that case TREE_TYPE (decl) is not a typedef variant
18657                  type and TYPE_NAME of the anonymous type is set to the
18658                  TYPE_DECL of the typedef. This construct is emitted by
18659                  the C++ FE.
18660
18661                  TYPE is the anonymous struct named by the typedef
18662                  DECL. As we need the DW_AT_type attribute of the
18663                  DW_TAG_typedef to point to the DIE of TYPE, let's
18664                  generate that DIE right away. add_type_attribute
18665                  called below will then pick (via lookup_type_die) that
18666                  anonymous struct DIE.  */
18667               if (!TREE_ASM_WRITTEN (type))
18668                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18669
18670               /* This is a GNU Extension.  We are adding a
18671                  DW_AT_linkage_name attribute to the DIE of the
18672                  anonymous struct TYPE.  The value of that attribute
18673                  is the name of the typedef decl naming the anonymous
18674                  struct.  This greatly eases the work of consumers of
18675                  this debug info.  */
18676               add_linkage_attr (lookup_type_die (type), decl);
18677             }
18678         }
18679
18680       add_type_attribute (type_die, type, TREE_READONLY (decl),
18681                           TREE_THIS_VOLATILE (decl), context_die);
18682
18683       if (is_naming_typedef_decl (decl))
18684         /* We want that all subsequent calls to lookup_type_die with
18685            TYPE in argument yield the DW_TAG_typedef we have just
18686            created.  */
18687         equate_type_number_to_die (type, type_die);
18688
18689       add_accessibility_attribute (type_die, decl);
18690     }
18691
18692   if (DECL_ABSTRACT (decl))
18693     equate_decl_number_to_die (decl, type_die);
18694
18695   if (get_AT (type_die, DW_AT_name))
18696     add_pubtype (decl, type_die);
18697 }
18698
18699 /* Generate a DIE for a struct, class, enum or union type.  */
18700
18701 static void
18702 gen_tagged_type_die (tree type,
18703                      dw_die_ref context_die,
18704                      enum debug_info_usage usage)
18705 {
18706   int need_pop;
18707
18708   if (type == NULL_TREE
18709       || !is_tagged_type (type))
18710     return;
18711
18712   /* If this is a nested type whose containing class hasn't been written
18713      out yet, writing it out will cover this one, too.  This does not apply
18714      to instantiations of member class templates; they need to be added to
18715      the containing class as they are generated.  FIXME: This hurts the
18716      idea of combining type decls from multiple TUs, since we can't predict
18717      what set of template instantiations we'll get.  */
18718   if (TYPE_CONTEXT (type)
18719       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18720       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18721     {
18722       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18723
18724       if (TREE_ASM_WRITTEN (type))
18725         return;
18726
18727       /* If that failed, attach ourselves to the stub.  */
18728       push_decl_scope (TYPE_CONTEXT (type));
18729       context_die = lookup_type_die (TYPE_CONTEXT (type));
18730       need_pop = 1;
18731     }
18732   else if (TYPE_CONTEXT (type) != NULL_TREE
18733            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18734     {
18735       /* If this type is local to a function that hasn't been written
18736          out yet, use a NULL context for now; it will be fixed up in
18737          decls_for_scope.  */
18738       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18739       /* A declaration DIE doesn't count; nested types need to go in the
18740          specification.  */
18741       if (context_die && is_declaration_die (context_die))
18742         context_die = NULL;
18743       need_pop = 0;
18744     }
18745   else
18746     {
18747       context_die = declare_in_namespace (type, context_die);
18748       need_pop = 0;
18749     }
18750
18751   if (TREE_CODE (type) == ENUMERAL_TYPE)
18752     {
18753       /* This might have been written out by the call to
18754          declare_in_namespace.  */
18755       if (!TREE_ASM_WRITTEN (type))
18756         gen_enumeration_type_die (type, context_die);
18757     }
18758   else
18759     gen_struct_or_union_type_die (type, context_die, usage);
18760
18761   if (need_pop)
18762     pop_decl_scope ();
18763
18764   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18765      it up if it is ever completed.  gen_*_type_die will set it for us
18766      when appropriate.  */
18767 }
18768
18769 /* Generate a type description DIE.  */
18770
18771 static void
18772 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18773                          enum debug_info_usage usage)
18774 {
18775   struct array_descr_info info;
18776
18777   if (type == NULL_TREE || type == error_mark_node)
18778     return;
18779
18780   if (TYPE_NAME (type) != NULL_TREE
18781       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18782       && is_redundant_typedef (TYPE_NAME (type))
18783       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18784     /* The DECL of this type is a typedef we don't want to emit debug
18785        info for but we want debug info for its underlying typedef.
18786        This can happen for e.g, the injected-class-name of a C++
18787        type.  */
18788     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18789
18790   /* If TYPE is a typedef type variant, let's generate debug info
18791      for the parent typedef which TYPE is a type of.  */
18792   if (typedef_variant_p (type))
18793     {
18794       if (TREE_ASM_WRITTEN (type))
18795         return;
18796
18797       /* Prevent broken recursion; we can't hand off to the same type.  */
18798       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18799
18800       /* Use the DIE of the containing namespace as the parent DIE of
18801          the type description DIE we want to generate.  */
18802       if (DECL_CONTEXT (TYPE_NAME (type))
18803           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18804         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18805
18806       TREE_ASM_WRITTEN (type) = 1;
18807
18808       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18809       return;
18810     }
18811
18812   /* If type is an anonymous tagged type named by a typedef, let's
18813      generate debug info for the typedef.  */
18814   if (is_naming_typedef_decl (TYPE_NAME (type)))
18815     {
18816       /* Use the DIE of the containing namespace as the parent DIE of
18817          the type description DIE we want to generate.  */
18818       if (DECL_CONTEXT (TYPE_NAME (type))
18819           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18820         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18821       
18822       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18823       return;
18824     }
18825
18826   /* If this is an array type with hidden descriptor, handle it first.  */
18827   if (!TREE_ASM_WRITTEN (type)
18828       && lang_hooks.types.get_array_descr_info
18829       && lang_hooks.types.get_array_descr_info (type, &info)
18830       && (dwarf_version >= 3 || !dwarf_strict))
18831     {
18832       gen_descr_array_type_die (type, &info, context_die);
18833       TREE_ASM_WRITTEN (type) = 1;
18834       return;
18835     }
18836
18837   /* We are going to output a DIE to represent the unqualified version
18838      of this type (i.e. without any const or volatile qualifiers) so
18839      get the main variant (i.e. the unqualified version) of this type
18840      now.  (Vectors are special because the debugging info is in the
18841      cloned type itself).  */
18842   if (TREE_CODE (type) != VECTOR_TYPE)
18843     type = type_main_variant (type);
18844
18845   if (TREE_ASM_WRITTEN (type))
18846     return;
18847
18848   switch (TREE_CODE (type))
18849     {
18850     case ERROR_MARK:
18851       break;
18852
18853     case POINTER_TYPE:
18854     case REFERENCE_TYPE:
18855       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18856          ensures that the gen_type_die recursion will terminate even if the
18857          type is recursive.  Recursive types are possible in Ada.  */
18858       /* ??? We could perhaps do this for all types before the switch
18859          statement.  */
18860       TREE_ASM_WRITTEN (type) = 1;
18861
18862       /* For these types, all that is required is that we output a DIE (or a
18863          set of DIEs) to represent the "basis" type.  */
18864       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18865                                 DINFO_USAGE_IND_USE);
18866       break;
18867
18868     case OFFSET_TYPE:
18869       /* This code is used for C++ pointer-to-data-member types.
18870          Output a description of the relevant class type.  */
18871       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18872                                         DINFO_USAGE_IND_USE);
18873
18874       /* Output a description of the type of the object pointed to.  */
18875       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18876                                         DINFO_USAGE_IND_USE);
18877
18878       /* Now output a DIE to represent this pointer-to-data-member type
18879          itself.  */
18880       gen_ptr_to_mbr_type_die (type, context_die);
18881       break;
18882
18883     case FUNCTION_TYPE:
18884       /* Force out return type (in case it wasn't forced out already).  */
18885       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18886                                         DINFO_USAGE_DIR_USE);
18887       gen_subroutine_type_die (type, context_die);
18888       break;
18889
18890     case METHOD_TYPE:
18891       /* Force out return type (in case it wasn't forced out already).  */
18892       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18893                                         DINFO_USAGE_DIR_USE);
18894       gen_subroutine_type_die (type, context_die);
18895       break;
18896
18897     case ARRAY_TYPE:
18898       gen_array_type_die (type, context_die);
18899       break;
18900
18901     case VECTOR_TYPE:
18902       gen_array_type_die (type, context_die);
18903       break;
18904
18905     case ENUMERAL_TYPE:
18906     case RECORD_TYPE:
18907     case UNION_TYPE:
18908     case QUAL_UNION_TYPE:
18909       gen_tagged_type_die (type, context_die, usage);
18910       return;
18911
18912     case VOID_TYPE:
18913     case INTEGER_TYPE:
18914     case REAL_TYPE:
18915     case FIXED_POINT_TYPE:
18916     case COMPLEX_TYPE:
18917     case BOOLEAN_TYPE:
18918       /* No DIEs needed for fundamental types.  */
18919       break;
18920
18921     case NULLPTR_TYPE:
18922     case LANG_TYPE:
18923       /* Just use DW_TAG_unspecified_type.  */
18924       {
18925         dw_die_ref type_die = lookup_type_die (type);
18926         if (type_die == NULL)
18927           {
18928             tree name = TYPE_NAME (type);
18929             if (TREE_CODE (name) == TYPE_DECL)
18930               name = DECL_NAME (name);
18931             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18932             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18933             equate_type_number_to_die (type, type_die);
18934           }
18935       }
18936       break;
18937
18938     default:
18939       gcc_unreachable ();
18940     }
18941
18942   TREE_ASM_WRITTEN (type) = 1;
18943 }
18944
18945 static void
18946 gen_type_die (tree type, dw_die_ref context_die)
18947 {
18948   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18949 }
18950
18951 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18952    things which are local to the given block.  */
18953
18954 static void
18955 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18956 {
18957   int must_output_die = 0;
18958   bool inlined_func;
18959
18960   /* Ignore blocks that are NULL.  */
18961   if (stmt == NULL_TREE)
18962     return;
18963
18964   inlined_func = inlined_function_outer_scope_p (stmt);
18965
18966   /* If the block is one fragment of a non-contiguous block, do not
18967      process the variables, since they will have been done by the
18968      origin block.  Do process subblocks.  */
18969   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18970     {
18971       tree sub;
18972
18973       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18974         gen_block_die (sub, context_die, depth + 1);
18975
18976       return;
18977     }
18978
18979   /* Determine if we need to output any Dwarf DIEs at all to represent this
18980      block.  */
18981   if (inlined_func)
18982     /* The outer scopes for inlinings *must* always be represented.  We
18983        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18984     must_output_die = 1;
18985   else
18986     {
18987       /* Determine if this block directly contains any "significant"
18988          local declarations which we will need to output DIEs for.  */
18989       if (debug_info_level > DINFO_LEVEL_TERSE)
18990         /* We are not in terse mode so *any* local declaration counts
18991            as being a "significant" one.  */
18992         must_output_die = ((BLOCK_VARS (stmt) != NULL
18993                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18994                            && (TREE_USED (stmt)
18995                                || TREE_ASM_WRITTEN (stmt)
18996                                || BLOCK_ABSTRACT (stmt)));
18997       else if ((TREE_USED (stmt)
18998                 || TREE_ASM_WRITTEN (stmt)
18999                 || BLOCK_ABSTRACT (stmt))
19000                && !dwarf2out_ignore_block (stmt))
19001         must_output_die = 1;
19002     }
19003
19004   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19005      DIE for any block which contains no significant local declarations at
19006      all.  Rather, in such cases we just call `decls_for_scope' so that any
19007      needed Dwarf info for any sub-blocks will get properly generated. Note
19008      that in terse mode, our definition of what constitutes a "significant"
19009      local declaration gets restricted to include only inlined function
19010      instances and local (nested) function definitions.  */
19011   if (must_output_die)
19012     {
19013       if (inlined_func)
19014         {
19015           /* If STMT block is abstract, that means we have been called
19016              indirectly from dwarf2out_abstract_function.
19017              That function rightfully marks the descendent blocks (of
19018              the abstract function it is dealing with) as being abstract,
19019              precisely to prevent us from emitting any
19020              DW_TAG_inlined_subroutine DIE as a descendent
19021              of an abstract function instance. So in that case, we should
19022              not call gen_inlined_subroutine_die.
19023
19024              Later though, when cgraph asks dwarf2out to emit info
19025              for the concrete instance of the function decl into which
19026              the concrete instance of STMT got inlined, the later will lead
19027              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19028           if (! BLOCK_ABSTRACT (stmt))
19029             gen_inlined_subroutine_die (stmt, context_die, depth);
19030         }
19031       else
19032         gen_lexical_block_die (stmt, context_die, depth);
19033     }
19034   else
19035     decls_for_scope (stmt, context_die, depth);
19036 }
19037
19038 /* Process variable DECL (or variable with origin ORIGIN) within
19039    block STMT and add it to CONTEXT_DIE.  */
19040 static void
19041 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19042 {
19043   dw_die_ref die;
19044   tree decl_or_origin = decl ? decl : origin;
19045
19046   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19047     die = lookup_decl_die (decl_or_origin);
19048   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19049            && TYPE_DECL_IS_STUB (decl_or_origin))
19050     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19051   else
19052     die = NULL;
19053
19054   if (die != NULL && die->die_parent == NULL)
19055     add_child_die (context_die, die);
19056   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19057     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19058                                          stmt, context_die);
19059   else
19060     gen_decl_die (decl, origin, context_die);
19061 }
19062
19063 /* Generate all of the decls declared within a given scope and (recursively)
19064    all of its sub-blocks.  */
19065
19066 static void
19067 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19068 {
19069   tree decl;
19070   unsigned int i;
19071   tree subblocks;
19072
19073   /* Ignore NULL blocks.  */
19074   if (stmt == NULL_TREE)
19075     return;
19076
19077   /* Output the DIEs to represent all of the data objects and typedefs
19078      declared directly within this block but not within any nested
19079      sub-blocks.  Also, nested function and tag DIEs have been
19080      generated with a parent of NULL; fix that up now.  */
19081   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19082     process_scope_var (stmt, decl, NULL_TREE, context_die);
19083   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19084     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19085                        context_die);
19086
19087   /* If we're at -g1, we're not interested in subblocks.  */
19088   if (debug_info_level <= DINFO_LEVEL_TERSE)
19089     return;
19090
19091   /* Output the DIEs to represent all sub-blocks (and the items declared
19092      therein) of this block.  */
19093   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19094        subblocks != NULL;
19095        subblocks = BLOCK_CHAIN (subblocks))
19096     gen_block_die (subblocks, context_die, depth + 1);
19097 }
19098
19099 /* Is this a typedef we can avoid emitting?  */
19100
19101 static inline int
19102 is_redundant_typedef (const_tree decl)
19103 {
19104   if (TYPE_DECL_IS_STUB (decl))
19105     return 1;
19106
19107   if (DECL_ARTIFICIAL (decl)
19108       && DECL_CONTEXT (decl)
19109       && is_tagged_type (DECL_CONTEXT (decl))
19110       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19111       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19112     /* Also ignore the artificial member typedef for the class name.  */
19113     return 1;
19114
19115   return 0;
19116 }
19117
19118 /* Return TRUE if TYPE is a typedef that names a type for linkage
19119    purposes. This kind of typedefs is produced by the C++ FE for
19120    constructs like:
19121
19122    typedef struct {...} foo;
19123
19124    In that case, there is no typedef variant type produced for foo.
19125    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19126    struct type.  */
19127
19128 static bool
19129 is_naming_typedef_decl (const_tree decl)
19130 {
19131   if (decl == NULL_TREE
19132       || TREE_CODE (decl) != TYPE_DECL
19133       || !is_tagged_type (TREE_TYPE (decl))
19134       || DECL_IS_BUILTIN (decl)
19135       || is_redundant_typedef (decl)
19136       /* It looks like Ada produces TYPE_DECLs that are very similar
19137          to C++ naming typedefs but that have different
19138          semantics. Let's be specific to c++ for now.  */
19139       || !is_cxx ())
19140     return FALSE;
19141
19142   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19143           && TYPE_NAME (TREE_TYPE (decl)) == decl
19144           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19145               != TYPE_NAME (TREE_TYPE (decl))));
19146 }
19147
19148 /* Returns the DIE for a context.  */
19149
19150 static inline dw_die_ref
19151 get_context_die (tree context)
19152 {
19153   if (context)
19154     {
19155       /* Find die that represents this context.  */
19156       if (TYPE_P (context))
19157         {
19158           context = TYPE_MAIN_VARIANT (context);
19159           return strip_naming_typedef (context, force_type_die (context));
19160         }
19161       else
19162         return force_decl_die (context);
19163     }
19164   return comp_unit_die ();
19165 }
19166
19167 /* Returns the DIE for decl.  A DIE will always be returned.  */
19168
19169 static dw_die_ref
19170 force_decl_die (tree decl)
19171 {
19172   dw_die_ref decl_die;
19173   unsigned saved_external_flag;
19174   tree save_fn = NULL_TREE;
19175   decl_die = lookup_decl_die (decl);
19176   if (!decl_die)
19177     {
19178       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19179
19180       decl_die = lookup_decl_die (decl);
19181       if (decl_die)
19182         return decl_die;
19183
19184       switch (TREE_CODE (decl))
19185         {
19186         case FUNCTION_DECL:
19187           /* Clear current_function_decl, so that gen_subprogram_die thinks
19188              that this is a declaration. At this point, we just want to force
19189              declaration die.  */
19190           save_fn = current_function_decl;
19191           current_function_decl = NULL_TREE;
19192           gen_subprogram_die (decl, context_die);
19193           current_function_decl = save_fn;
19194           break;
19195
19196         case VAR_DECL:
19197           /* Set external flag to force declaration die. Restore it after
19198            gen_decl_die() call.  */
19199           saved_external_flag = DECL_EXTERNAL (decl);
19200           DECL_EXTERNAL (decl) = 1;
19201           gen_decl_die (decl, NULL, context_die);
19202           DECL_EXTERNAL (decl) = saved_external_flag;
19203           break;
19204
19205         case NAMESPACE_DECL:
19206           if (dwarf_version >= 3 || !dwarf_strict)
19207             dwarf2out_decl (decl);
19208           else
19209             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19210             decl_die = comp_unit_die ();
19211           break;
19212
19213         case TRANSLATION_UNIT_DECL:
19214           decl_die = comp_unit_die ();
19215           break;
19216
19217         default:
19218           gcc_unreachable ();
19219         }
19220
19221       /* We should be able to find the DIE now.  */
19222       if (!decl_die)
19223         decl_die = lookup_decl_die (decl);
19224       gcc_assert (decl_die);
19225     }
19226
19227   return decl_die;
19228 }
19229
19230 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19231    always returned.  */
19232
19233 static dw_die_ref
19234 force_type_die (tree type)
19235 {
19236   dw_die_ref type_die;
19237
19238   type_die = lookup_type_die (type);
19239   if (!type_die)
19240     {
19241       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19242
19243       type_die = modified_type_die (type, TYPE_READONLY (type),
19244                                     TYPE_VOLATILE (type), context_die);
19245       gcc_assert (type_die);
19246     }
19247   return type_die;
19248 }
19249
19250 /* Force out any required namespaces to be able to output DECL,
19251    and return the new context_die for it, if it's changed.  */
19252
19253 static dw_die_ref
19254 setup_namespace_context (tree thing, dw_die_ref context_die)
19255 {
19256   tree context = (DECL_P (thing)
19257                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19258   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19259     /* Force out the namespace.  */
19260     context_die = force_decl_die (context);
19261
19262   return context_die;
19263 }
19264
19265 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19266    type) within its namespace, if appropriate.
19267
19268    For compatibility with older debuggers, namespace DIEs only contain
19269    declarations; all definitions are emitted at CU scope.  */
19270
19271 static dw_die_ref
19272 declare_in_namespace (tree thing, dw_die_ref context_die)
19273 {
19274   dw_die_ref ns_context;
19275
19276   if (debug_info_level <= DINFO_LEVEL_TERSE)
19277     return context_die;
19278
19279   /* If this decl is from an inlined function, then don't try to emit it in its
19280      namespace, as we will get confused.  It would have already been emitted
19281      when the abstract instance of the inline function was emitted anyways.  */
19282   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19283     return context_die;
19284
19285   ns_context = setup_namespace_context (thing, context_die);
19286
19287   if (ns_context != context_die)
19288     {
19289       if (is_fortran ())
19290         return ns_context;
19291       if (DECL_P (thing))
19292         gen_decl_die (thing, NULL, ns_context);
19293       else
19294         gen_type_die (thing, ns_context);
19295     }
19296   return context_die;
19297 }
19298
19299 /* Generate a DIE for a namespace or namespace alias.  */
19300
19301 static void
19302 gen_namespace_die (tree decl, dw_die_ref context_die)
19303 {
19304   dw_die_ref namespace_die;
19305
19306   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19307      they are an alias of.  */
19308   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19309     {
19310       /* Output a real namespace or module.  */
19311       context_die = setup_namespace_context (decl, comp_unit_die ());
19312       namespace_die = new_die (is_fortran ()
19313                                ? DW_TAG_module : DW_TAG_namespace,
19314                                context_die, decl);
19315       /* For Fortran modules defined in different CU don't add src coords.  */
19316       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19317         {
19318           const char *name = dwarf2_name (decl, 0);
19319           if (name)
19320             add_name_attribute (namespace_die, name);
19321         }
19322       else
19323         add_name_and_src_coords_attributes (namespace_die, decl);
19324       if (DECL_EXTERNAL (decl))
19325         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19326       equate_decl_number_to_die (decl, namespace_die);
19327     }
19328   else
19329     {
19330       /* Output a namespace alias.  */
19331
19332       /* Force out the namespace we are an alias of, if necessary.  */
19333       dw_die_ref origin_die
19334         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19335
19336       if (DECL_FILE_SCOPE_P (decl)
19337           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19338         context_die = setup_namespace_context (decl, comp_unit_die ());
19339       /* Now create the namespace alias DIE.  */
19340       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19341       add_name_and_src_coords_attributes (namespace_die, decl);
19342       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19343       equate_decl_number_to_die (decl, namespace_die);
19344     }
19345 }
19346
19347 /* Generate Dwarf debug information for a decl described by DECL.
19348    The return value is currently only meaningful for PARM_DECLs,
19349    for all other decls it returns NULL.  */
19350
19351 static dw_die_ref
19352 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19353 {
19354   tree decl_or_origin = decl ? decl : origin;
19355   tree class_origin = NULL, ultimate_origin;
19356
19357   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19358     return NULL;
19359
19360   switch (TREE_CODE (decl_or_origin))
19361     {
19362     case ERROR_MARK:
19363       break;
19364
19365     case CONST_DECL:
19366       if (!is_fortran () && !is_ada ())
19367         {
19368           /* The individual enumerators of an enum type get output when we output
19369              the Dwarf representation of the relevant enum type itself.  */
19370           break;
19371         }
19372
19373       /* Emit its type.  */
19374       gen_type_die (TREE_TYPE (decl), context_die);
19375
19376       /* And its containing namespace.  */
19377       context_die = declare_in_namespace (decl, context_die);
19378
19379       gen_const_die (decl, context_die);
19380       break;
19381
19382     case FUNCTION_DECL:
19383       /* Don't output any DIEs to represent mere function declarations,
19384          unless they are class members or explicit block externs.  */
19385       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19386           && DECL_FILE_SCOPE_P (decl_or_origin)
19387           && (current_function_decl == NULL_TREE
19388               || DECL_ARTIFICIAL (decl_or_origin)))
19389         break;
19390
19391 #if 0
19392       /* FIXME */
19393       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19394          on local redeclarations of global functions.  That seems broken.  */
19395       if (current_function_decl != decl)
19396         /* This is only a declaration.  */;
19397 #endif
19398
19399       /* If we're emitting a clone, emit info for the abstract instance.  */
19400       if (origin || DECL_ORIGIN (decl) != decl)
19401         dwarf2out_abstract_function (origin
19402                                      ? DECL_ORIGIN (origin)
19403                                      : DECL_ABSTRACT_ORIGIN (decl));
19404
19405       /* If we're emitting an out-of-line copy of an inline function,
19406          emit info for the abstract instance and set up to refer to it.  */
19407       else if (cgraph_function_possibly_inlined_p (decl)
19408                && ! DECL_ABSTRACT (decl)
19409                && ! class_or_namespace_scope_p (context_die)
19410                /* dwarf2out_abstract_function won't emit a die if this is just
19411                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19412                   that case, because that works only if we have a die.  */
19413                && DECL_INITIAL (decl) != NULL_TREE)
19414         {
19415           dwarf2out_abstract_function (decl);
19416           set_decl_origin_self (decl);
19417         }
19418
19419       /* Otherwise we're emitting the primary DIE for this decl.  */
19420       else if (debug_info_level > DINFO_LEVEL_TERSE)
19421         {
19422           /* Before we describe the FUNCTION_DECL itself, make sure that we
19423              have its containing type.  */
19424           if (!origin)
19425             origin = decl_class_context (decl);
19426           if (origin != NULL_TREE)
19427             gen_type_die (origin, context_die);
19428
19429           /* And its return type.  */
19430           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19431
19432           /* And its virtual context.  */
19433           if (DECL_VINDEX (decl) != NULL_TREE)
19434             gen_type_die (DECL_CONTEXT (decl), context_die);
19435
19436           /* Make sure we have a member DIE for decl.  */
19437           if (origin != NULL_TREE)
19438             gen_type_die_for_member (origin, decl, context_die);
19439
19440           /* And its containing namespace.  */
19441           context_die = declare_in_namespace (decl, context_die);
19442         }
19443
19444       /* Now output a DIE to represent the function itself.  */
19445       if (decl)
19446         gen_subprogram_die (decl, context_die);
19447       break;
19448
19449     case TYPE_DECL:
19450       /* If we are in terse mode, don't generate any DIEs to represent any
19451          actual typedefs.  */
19452       if (debug_info_level <= DINFO_LEVEL_TERSE)
19453         break;
19454
19455       /* In the special case of a TYPE_DECL node representing the declaration
19456          of some type tag, if the given TYPE_DECL is marked as having been
19457          instantiated from some other (original) TYPE_DECL node (e.g. one which
19458          was generated within the original definition of an inline function) we
19459          used to generate a special (abbreviated) DW_TAG_structure_type,
19460          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19461          should be actually referencing those DIEs, as variable DIEs with that
19462          type would be emitted already in the abstract origin, so it was always
19463          removed during unused type prunning.  Don't add anything in this
19464          case.  */
19465       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19466         break;
19467
19468       if (is_redundant_typedef (decl))
19469         gen_type_die (TREE_TYPE (decl), context_die);
19470       else
19471         /* Output a DIE to represent the typedef itself.  */
19472         gen_typedef_die (decl, context_die);
19473       break;
19474
19475     case LABEL_DECL:
19476       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19477         gen_label_die (decl, context_die);
19478       break;
19479
19480     case VAR_DECL:
19481     case RESULT_DECL:
19482       /* If we are in terse mode, don't generate any DIEs to represent any
19483          variable declarations or definitions.  */
19484       if (debug_info_level <= DINFO_LEVEL_TERSE)
19485         break;
19486
19487       /* Output any DIEs that are needed to specify the type of this data
19488          object.  */
19489       if (decl_by_reference_p (decl_or_origin))
19490         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19491       else
19492         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19493
19494       /* And its containing type.  */
19495       class_origin = decl_class_context (decl_or_origin);
19496       if (class_origin != NULL_TREE)
19497         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19498
19499       /* And its containing namespace.  */
19500       context_die = declare_in_namespace (decl_or_origin, context_die);
19501
19502       /* Now output the DIE to represent the data object itself.  This gets
19503          complicated because of the possibility that the VAR_DECL really
19504          represents an inlined instance of a formal parameter for an inline
19505          function.  */
19506       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19507       if (ultimate_origin != NULL_TREE
19508           && TREE_CODE (ultimate_origin) == PARM_DECL)
19509         gen_formal_parameter_die (decl, origin,
19510                                   true /* Emit name attribute.  */,
19511                                   context_die);
19512       else
19513         gen_variable_die (decl, origin, context_die);
19514       break;
19515
19516     case FIELD_DECL:
19517       /* Ignore the nameless fields that are used to skip bits but handle C++
19518          anonymous unions and structs.  */
19519       if (DECL_NAME (decl) != NULL_TREE
19520           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19521           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19522         {
19523           gen_type_die (member_declared_type (decl), context_die);
19524           gen_field_die (decl, context_die);
19525         }
19526       break;
19527
19528     case PARM_DECL:
19529       if (DECL_BY_REFERENCE (decl_or_origin))
19530         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19531       else
19532         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19533       return gen_formal_parameter_die (decl, origin,
19534                                        true /* Emit name attribute.  */,
19535                                        context_die);
19536
19537     case NAMESPACE_DECL:
19538     case IMPORTED_DECL:
19539       if (dwarf_version >= 3 || !dwarf_strict)
19540         gen_namespace_die (decl, context_die);
19541       break;
19542
19543     default:
19544       /* Probably some frontend-internal decl.  Assume we don't care.  */
19545       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19546       break;
19547     }
19548
19549   return NULL;
19550 }
19551 \f
19552 /* Output debug information for global decl DECL.  Called from toplev.c after
19553    compilation proper has finished.  */
19554
19555 static void
19556 dwarf2out_global_decl (tree decl)
19557 {
19558   /* Output DWARF2 information for file-scope tentative data object
19559      declarations, file-scope (extern) function declarations (which
19560      had no corresponding body) and file-scope tagged type declarations
19561      and definitions which have not yet been forced out.  */
19562   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19563     dwarf2out_decl (decl);
19564 }
19565
19566 /* Output debug information for type decl DECL.  Called from toplev.c
19567    and from language front ends (to record built-in types).  */
19568 static void
19569 dwarf2out_type_decl (tree decl, int local)
19570 {
19571   if (!local)
19572     dwarf2out_decl (decl);
19573 }
19574
19575 /* Output debug information for imported module or decl DECL.
19576    NAME is non-NULL name in the lexical block if the decl has been renamed.
19577    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19578    that DECL belongs to.
19579    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19580 static void
19581 dwarf2out_imported_module_or_decl_1 (tree decl,
19582                                      tree name,
19583                                      tree lexical_block,
19584                                      dw_die_ref lexical_block_die)
19585 {
19586   expanded_location xloc;
19587   dw_die_ref imported_die = NULL;
19588   dw_die_ref at_import_die;
19589
19590   if (TREE_CODE (decl) == IMPORTED_DECL)
19591     {
19592       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19593       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19594       gcc_assert (decl);
19595     }
19596   else
19597     xloc = expand_location (input_location);
19598
19599   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19600     {
19601       at_import_die = force_type_die (TREE_TYPE (decl));
19602       /* For namespace N { typedef void T; } using N::T; base_type_die
19603          returns NULL, but DW_TAG_imported_declaration requires
19604          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19605       if (!at_import_die)
19606         {
19607           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19608           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19609           at_import_die = lookup_type_die (TREE_TYPE (decl));
19610           gcc_assert (at_import_die);
19611         }
19612     }
19613   else
19614     {
19615       at_import_die = lookup_decl_die (decl);
19616       if (!at_import_die)
19617         {
19618           /* If we're trying to avoid duplicate debug info, we may not have
19619              emitted the member decl for this field.  Emit it now.  */
19620           if (TREE_CODE (decl) == FIELD_DECL)
19621             {
19622               tree type = DECL_CONTEXT (decl);
19623
19624               if (TYPE_CONTEXT (type)
19625                   && TYPE_P (TYPE_CONTEXT (type))
19626                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19627                                                 DINFO_USAGE_DIR_USE))
19628                 return;
19629               gen_type_die_for_member (type, decl,
19630                                        get_context_die (TYPE_CONTEXT (type)));
19631             }
19632           at_import_die = force_decl_die (decl);
19633         }
19634     }
19635
19636   if (TREE_CODE (decl) == NAMESPACE_DECL)
19637     {
19638       if (dwarf_version >= 3 || !dwarf_strict)
19639         imported_die = new_die (DW_TAG_imported_module,
19640                                 lexical_block_die,
19641                                 lexical_block);
19642       else
19643         return;
19644     }
19645   else
19646     imported_die = new_die (DW_TAG_imported_declaration,
19647                             lexical_block_die,
19648                             lexical_block);
19649
19650   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19651   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19652   if (name)
19653     add_AT_string (imported_die, DW_AT_name,
19654                    IDENTIFIER_POINTER (name));
19655   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19656 }
19657
19658 /* Output debug information for imported module or decl DECL.
19659    NAME is non-NULL name in context if the decl has been renamed.
19660    CHILD is true if decl is one of the renamed decls as part of
19661    importing whole module.  */
19662
19663 static void
19664 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19665                                    bool child)
19666 {
19667   /* dw_die_ref at_import_die;  */
19668   dw_die_ref scope_die;
19669
19670   if (debug_info_level <= DINFO_LEVEL_TERSE)
19671     return;
19672
19673   gcc_assert (decl);
19674
19675   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19676      We need decl DIE for reference and scope die. First, get DIE for the decl
19677      itself.  */
19678
19679   /* Get the scope die for decl context. Use comp_unit_die for global module
19680      or decl. If die is not found for non globals, force new die.  */
19681   if (context
19682       && TYPE_P (context)
19683       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19684     return;
19685
19686   if (!(dwarf_version >= 3 || !dwarf_strict))
19687     return;
19688
19689   scope_die = get_context_die (context);
19690
19691   if (child)
19692     {
19693       gcc_assert (scope_die->die_child);
19694       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19695       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19696       scope_die = scope_die->die_child;
19697     }
19698
19699   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19700   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19701
19702 }
19703
19704 /* Write the debugging output for DECL.  */
19705
19706 void
19707 dwarf2out_decl (tree decl)
19708 {
19709   dw_die_ref context_die = comp_unit_die ();
19710
19711   switch (TREE_CODE (decl))
19712     {
19713     case ERROR_MARK:
19714       return;
19715
19716     case FUNCTION_DECL:
19717       /* What we would really like to do here is to filter out all mere
19718          file-scope declarations of file-scope functions which are never
19719          referenced later within this translation unit (and keep all of ones
19720          that *are* referenced later on) but we aren't clairvoyant, so we have
19721          no idea which functions will be referenced in the future (i.e. later
19722          on within the current translation unit). So here we just ignore all
19723          file-scope function declarations which are not also definitions.  If
19724          and when the debugger needs to know something about these functions,
19725          it will have to hunt around and find the DWARF information associated
19726          with the definition of the function.
19727
19728          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19729          nodes represent definitions and which ones represent mere
19730          declarations.  We have to check DECL_INITIAL instead. That's because
19731          the C front-end supports some weird semantics for "extern inline"
19732          function definitions.  These can get inlined within the current
19733          translation unit (and thus, we need to generate Dwarf info for their
19734          abstract instances so that the Dwarf info for the concrete inlined
19735          instances can have something to refer to) but the compiler never
19736          generates any out-of-lines instances of such things (despite the fact
19737          that they *are* definitions).
19738
19739          The important point is that the C front-end marks these "extern
19740          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19741          them anyway. Note that the C++ front-end also plays some similar games
19742          for inline function definitions appearing within include files which
19743          also contain `#pragma interface' pragmas.  */
19744       if (DECL_INITIAL (decl) == NULL_TREE)
19745         return;
19746
19747       /* If we're a nested function, initially use a parent of NULL; if we're
19748          a plain function, this will be fixed up in decls_for_scope.  If
19749          we're a method, it will be ignored, since we already have a DIE.  */
19750       if (decl_function_context (decl)
19751           /* But if we're in terse mode, we don't care about scope.  */
19752           && debug_info_level > DINFO_LEVEL_TERSE)
19753         context_die = NULL;
19754       break;
19755
19756     case VAR_DECL:
19757       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19758          declaration and if the declaration was never even referenced from
19759          within this entire compilation unit.  We suppress these DIEs in
19760          order to save space in the .debug section (by eliminating entries
19761          which are probably useless).  Note that we must not suppress
19762          block-local extern declarations (whether used or not) because that
19763          would screw-up the debugger's name lookup mechanism and cause it to
19764          miss things which really ought to be in scope at a given point.  */
19765       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19766         return;
19767
19768       /* For local statics lookup proper context die.  */
19769       if (TREE_STATIC (decl) && decl_function_context (decl))
19770         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19771
19772       /* If we are in terse mode, don't generate any DIEs to represent any
19773          variable declarations or definitions.  */
19774       if (debug_info_level <= DINFO_LEVEL_TERSE)
19775         return;
19776       break;
19777
19778     case CONST_DECL:
19779       if (debug_info_level <= DINFO_LEVEL_TERSE)
19780         return;
19781       if (!is_fortran () && !is_ada ())
19782         return;
19783       if (TREE_STATIC (decl) && decl_function_context (decl))
19784         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19785       break;
19786
19787     case NAMESPACE_DECL:
19788     case IMPORTED_DECL:
19789       if (debug_info_level <= DINFO_LEVEL_TERSE)
19790         return;
19791       if (lookup_decl_die (decl) != NULL)
19792         return;
19793       break;
19794
19795     case TYPE_DECL:
19796       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19797       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19798         return;
19799
19800       /* Don't bother trying to generate any DIEs to represent any of the
19801          normal built-in types for the language we are compiling.  */
19802       if (DECL_IS_BUILTIN (decl))
19803         return;
19804
19805       /* If we are in terse mode, don't generate any DIEs for types.  */
19806       if (debug_info_level <= DINFO_LEVEL_TERSE)
19807         return;
19808
19809       /* If we're a function-scope tag, initially use a parent of NULL;
19810          this will be fixed up in decls_for_scope.  */
19811       if (decl_function_context (decl))
19812         context_die = NULL;
19813
19814       break;
19815
19816     default:
19817       return;
19818     }
19819
19820   gen_decl_die (decl, NULL, context_die);
19821 }
19822
19823 /* Write the debugging output for DECL.  */
19824
19825 static void
19826 dwarf2out_function_decl (tree decl)
19827 {
19828   dwarf2out_decl (decl);
19829   call_arg_locations = NULL;
19830   call_arg_loc_last = NULL;
19831   call_site_count = -1;
19832   tail_call_site_count = -1;
19833   VEC_free (dw_die_ref, heap, block_map);
19834   htab_empty (decl_loc_table);
19835   htab_empty (cached_dw_loc_list_table);
19836 }
19837
19838 /* Output a marker (i.e. a label) for the beginning of the generated code for
19839    a lexical block.  */
19840
19841 static void
19842 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19843                        unsigned int blocknum)
19844 {
19845   switch_to_section (current_function_section ());
19846   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19847 }
19848
19849 /* Output a marker (i.e. a label) for the end of the generated code for a
19850    lexical block.  */
19851
19852 static void
19853 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19854 {
19855   switch_to_section (current_function_section ());
19856   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19857 }
19858
19859 /* Returns nonzero if it is appropriate not to emit any debugging
19860    information for BLOCK, because it doesn't contain any instructions.
19861
19862    Don't allow this for blocks with nested functions or local classes
19863    as we would end up with orphans, and in the presence of scheduling
19864    we may end up calling them anyway.  */
19865
19866 static bool
19867 dwarf2out_ignore_block (const_tree block)
19868 {
19869   tree decl;
19870   unsigned int i;
19871
19872   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19873     if (TREE_CODE (decl) == FUNCTION_DECL
19874         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19875       return 0;
19876   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19877     {
19878       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19879       if (TREE_CODE (decl) == FUNCTION_DECL
19880           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19881       return 0;
19882     }
19883
19884   return 1;
19885 }
19886
19887 /* Hash table routines for file_hash.  */
19888
19889 static int
19890 file_table_eq (const void *p1_p, const void *p2_p)
19891 {
19892   const struct dwarf_file_data *const p1 =
19893     (const struct dwarf_file_data *) p1_p;
19894   const char *const p2 = (const char *) p2_p;
19895   return filename_cmp (p1->filename, p2) == 0;
19896 }
19897
19898 static hashval_t
19899 file_table_hash (const void *p_p)
19900 {
19901   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19902   return htab_hash_string (p->filename);
19903 }
19904
19905 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19906    dwarf2out.c) and return its "index".  The index of each (known) filename is
19907    just a unique number which is associated with only that one filename.  We
19908    need such numbers for the sake of generating labels (in the .debug_sfnames
19909    section) and references to those files numbers (in the .debug_srcinfo
19910    and.debug_macinfo sections).  If the filename given as an argument is not
19911    found in our current list, add it to the list and assign it the next
19912    available unique index number.  In order to speed up searches, we remember
19913    the index of the filename was looked up last.  This handles the majority of
19914    all searches.  */
19915
19916 static struct dwarf_file_data *
19917 lookup_filename (const char *file_name)
19918 {
19919   void ** slot;
19920   struct dwarf_file_data * created;
19921
19922   /* Check to see if the file name that was searched on the previous
19923      call matches this file name.  If so, return the index.  */
19924   if (file_table_last_lookup
19925       && (file_name == file_table_last_lookup->filename
19926           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19927     return file_table_last_lookup;
19928
19929   /* Didn't match the previous lookup, search the table.  */
19930   slot = htab_find_slot_with_hash (file_table, file_name,
19931                                    htab_hash_string (file_name), INSERT);
19932   if (*slot)
19933     return (struct dwarf_file_data *) *slot;
19934
19935   created = ggc_alloc_dwarf_file_data ();
19936   created->filename = file_name;
19937   created->emitted_number = 0;
19938   *slot = created;
19939   return created;
19940 }
19941
19942 /* If the assembler will construct the file table, then translate the compiler
19943    internal file table number into the assembler file table number, and emit
19944    a .file directive if we haven't already emitted one yet.  The file table
19945    numbers are different because we prune debug info for unused variables and
19946    types, which may include filenames.  */
19947
19948 static int
19949 maybe_emit_file (struct dwarf_file_data * fd)
19950 {
19951   if (! fd->emitted_number)
19952     {
19953       if (last_emitted_file)
19954         fd->emitted_number = last_emitted_file->emitted_number + 1;
19955       else
19956         fd->emitted_number = 1;
19957       last_emitted_file = fd;
19958
19959       if (DWARF2_ASM_LINE_DEBUG_INFO)
19960         {
19961           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19962           output_quoted_string (asm_out_file,
19963                                 remap_debug_filename (fd->filename));
19964           fputc ('\n', asm_out_file);
19965         }
19966     }
19967
19968   return fd->emitted_number;
19969 }
19970
19971 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19972    That generation should happen after function debug info has been
19973    generated. The value of the attribute is the constant value of ARG.  */
19974
19975 static void
19976 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19977 {
19978   die_arg_entry entry;
19979
19980   if (!die || !arg)
19981     return;
19982
19983   if (!tmpl_value_parm_die_table)
19984     tmpl_value_parm_die_table
19985       = VEC_alloc (die_arg_entry, gc, 32);
19986
19987   entry.die = die;
19988   entry.arg = arg;
19989   VEC_safe_push (die_arg_entry, gc,
19990                  tmpl_value_parm_die_table,
19991                  &entry);
19992 }
19993
19994 /* Return TRUE if T is an instance of generic type, FALSE
19995    otherwise.  */
19996
19997 static bool
19998 generic_type_p (tree t)
19999 {
20000   if (t == NULL_TREE || !TYPE_P (t))
20001     return false;
20002   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20003 }
20004
20005 /* Schedule the generation of the generic parameter dies for the
20006   instance of generic type T. The proper generation itself is later
20007   done by gen_scheduled_generic_parms_dies. */
20008
20009 static void
20010 schedule_generic_params_dies_gen (tree t)
20011 {
20012   if (!generic_type_p (t))
20013     return;
20014
20015   if (generic_type_instances == NULL)
20016     generic_type_instances = VEC_alloc (tree, gc, 256);
20017
20018   VEC_safe_push (tree, gc, generic_type_instances, t);
20019 }
20020
20021 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20022    by append_entry_to_tmpl_value_parm_die_table. This function must
20023    be called after function DIEs have been generated.  */
20024
20025 static void
20026 gen_remaining_tmpl_value_param_die_attribute (void)
20027 {
20028   if (tmpl_value_parm_die_table)
20029     {
20030       unsigned i;
20031       die_arg_entry *e;
20032
20033       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20034         tree_add_const_value_attribute (e->die, e->arg);
20035     }
20036 }
20037
20038 /* Generate generic parameters DIEs for instances of generic types
20039    that have been previously scheduled by
20040    schedule_generic_params_dies_gen. This function must be called
20041    after all the types of the CU have been laid out.  */
20042
20043 static void
20044 gen_scheduled_generic_parms_dies (void)
20045 {
20046   unsigned i;
20047   tree t;
20048
20049   if (generic_type_instances == NULL)
20050     return;
20051   
20052   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20053     gen_generic_params_dies (t);
20054 }
20055
20056
20057 /* Replace DW_AT_name for the decl with name.  */
20058
20059 static void
20060 dwarf2out_set_name (tree decl, tree name)
20061 {
20062   dw_die_ref die;
20063   dw_attr_ref attr;
20064   const char *dname;
20065
20066   die = TYPE_SYMTAB_DIE (decl);
20067   if (!die)
20068     return;
20069
20070   dname = dwarf2_name (name, 0);
20071   if (!dname)
20072     return;
20073
20074   attr = get_AT (die, DW_AT_name);
20075   if (attr)
20076     {
20077       struct indirect_string_node *node;
20078
20079       node = find_AT_string (dname);
20080       /* replace the string.  */
20081       attr->dw_attr_val.v.val_str = node;
20082     }
20083
20084   else
20085     add_name_attribute (die, dname);
20086 }
20087
20088 /* Called by the final INSN scan whenever we see a var location.  We
20089    use it to drop labels in the right places, and throw the location in
20090    our lookup table.  */
20091
20092 static void
20093 dwarf2out_var_location (rtx loc_note)
20094 {
20095   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20096   struct var_loc_node *newloc;
20097   rtx next_real;
20098   static const char *last_label;
20099   static const char *last_postcall_label;
20100   static bool last_in_cold_section_p;
20101   tree decl;
20102   bool var_loc_p;
20103
20104   if (!NOTE_P (loc_note))
20105     {
20106       if (CALL_P (loc_note))
20107         {
20108           call_site_count++;
20109           if (SIBLING_CALL_P (loc_note))
20110             tail_call_site_count++;
20111         }
20112       return;
20113     }
20114
20115   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20116   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20117     return;
20118
20119   next_real = next_real_insn (loc_note);
20120
20121   /* If there are no instructions which would be affected by this note,
20122      don't do anything.  */
20123   if (var_loc_p
20124       && next_real == NULL_RTX
20125       && !NOTE_DURING_CALL_P (loc_note))
20126     return;
20127
20128   if (next_real == NULL_RTX)
20129     next_real = get_last_insn ();
20130
20131   /* If there were any real insns between note we processed last time
20132      and this note (or if it is the first note), clear
20133      last_{,postcall_}label so that they are not reused this time.  */
20134   if (last_var_location_insn == NULL_RTX
20135       || last_var_location_insn != next_real
20136       || last_in_cold_section_p != in_cold_section_p)
20137     {
20138       last_label = NULL;
20139       last_postcall_label = NULL;
20140     }
20141
20142   if (var_loc_p)
20143     {
20144       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20145       newloc = add_var_loc_to_decl (decl, loc_note,
20146                                     NOTE_DURING_CALL_P (loc_note)
20147                                     ? last_postcall_label : last_label);
20148       if (newloc == NULL)
20149         return;
20150     }
20151   else
20152     {
20153       decl = NULL_TREE;
20154       newloc = NULL;
20155     }
20156
20157   /* If there were no real insns between note we processed last time
20158      and this note, use the label we emitted last time.  Otherwise
20159      create a new label and emit it.  */
20160   if (last_label == NULL)
20161     {
20162       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20163       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20164       loclabel_num++;
20165       last_label = ggc_strdup (loclabel);
20166     }
20167
20168   if (!var_loc_p)
20169     {
20170       struct call_arg_loc_node *ca_loc
20171         = ggc_alloc_cleared_call_arg_loc_node ();
20172       rtx prev = prev_real_insn (loc_note), x;
20173       ca_loc->call_arg_loc_note = loc_note;
20174       ca_loc->next = NULL;
20175       ca_loc->label = last_label;
20176       gcc_assert (prev
20177                   && (CALL_P (prev)
20178                       || (NONJUMP_INSN_P (prev)
20179                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20180                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20181       if (!CALL_P (prev))
20182         prev = XVECEXP (PATTERN (prev), 0, 0);
20183       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20184       x = PATTERN (prev);
20185       if (GET_CODE (x) == PARALLEL)
20186         x = XVECEXP (x, 0, 0);
20187       if (GET_CODE (x) == SET)
20188         x = SET_SRC (x);
20189       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20190         {
20191           x = XEXP (XEXP (x, 0), 0);
20192           if (GET_CODE (x) == SYMBOL_REF
20193               && SYMBOL_REF_DECL (x)
20194               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20195             ca_loc->symbol_ref = x;
20196         }
20197       ca_loc->block = insn_scope (prev);
20198       if (call_arg_locations)
20199         call_arg_loc_last->next = ca_loc;
20200       else
20201         call_arg_locations = ca_loc;
20202       call_arg_loc_last = ca_loc;
20203     }
20204   else if (!NOTE_DURING_CALL_P (loc_note))
20205     newloc->label = last_label;
20206   else
20207     {
20208       if (!last_postcall_label)
20209         {
20210           sprintf (loclabel, "%s-1", last_label);
20211           last_postcall_label = ggc_strdup (loclabel);
20212         }
20213       newloc->label = last_postcall_label;
20214     }
20215
20216   last_var_location_insn = next_real;
20217   last_in_cold_section_p = in_cold_section_p;
20218 }
20219
20220 /* Note in one location list that text section has changed.  */
20221
20222 static int
20223 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20224 {
20225   var_loc_list *list = (var_loc_list *) *slot;
20226   if (list->first)
20227     list->last_before_switch
20228       = list->last->next ? list->last->next : list->last;
20229   return 1;
20230 }
20231
20232 /* Note in all location lists that text section has changed.  */
20233
20234 static void
20235 var_location_switch_text_section (void)
20236 {
20237   if (decl_loc_table == NULL)
20238     return;
20239
20240   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20241 }
20242
20243 /* Create a new line number table.  */
20244
20245 static dw_line_info_table *
20246 new_line_info_table (void)
20247 {
20248   dw_line_info_table *table;
20249
20250   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20251   table->file_num = 1;
20252   table->line_num = 1;
20253   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20254
20255   return table;
20256 }
20257
20258 /* Lookup the "current" table into which we emit line info, so
20259    that we don't have to do it for every source line.  */
20260
20261 static void
20262 set_cur_line_info_table (section *sec)
20263 {
20264   dw_line_info_table *table;
20265
20266   if (sec == text_section)
20267     table = text_section_line_info;
20268   else if (sec == cold_text_section)
20269     {
20270       table = cold_text_section_line_info;
20271       if (!table)
20272         {
20273           cold_text_section_line_info = table = new_line_info_table ();
20274           table->end_label = cold_end_label;
20275         }
20276     }
20277   else
20278     {
20279       const char *end_label;
20280
20281       if (flag_reorder_blocks_and_partition)
20282         {
20283           if (in_cold_section_p)
20284             end_label = crtl->subsections.cold_section_end_label;
20285           else
20286             end_label = crtl->subsections.hot_section_end_label;
20287         }
20288       else
20289         {
20290           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20291           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20292                                        current_function_funcdef_no);
20293           end_label = ggc_strdup (label);
20294         }
20295
20296       table = new_line_info_table ();
20297       table->end_label = end_label;
20298
20299       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20300     }
20301
20302   cur_line_info_table = table;
20303 }
20304
20305
20306 /* We need to reset the locations at the beginning of each
20307    function. We can't do this in the end_function hook, because the
20308    declarations that use the locations won't have been output when
20309    that hook is called.  Also compute have_multiple_function_sections here.  */
20310
20311 static void
20312 dwarf2out_begin_function (tree fun)
20313 {
20314   section *sec = function_section (fun);
20315
20316   if (sec != text_section)
20317     have_multiple_function_sections = true;
20318
20319   if (flag_reorder_blocks_and_partition && !cold_text_section)
20320     {
20321       gcc_assert (current_function_decl == fun);
20322       cold_text_section = unlikely_text_section ();
20323       switch_to_section (cold_text_section);
20324       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20325       switch_to_section (sec);
20326     }
20327
20328   dwarf2out_note_section_used ();
20329   call_site_count = 0;
20330   tail_call_site_count = 0;
20331
20332   set_cur_line_info_table (sec);
20333 }
20334
20335 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20336
20337 static void
20338 push_dw_line_info_entry (dw_line_info_table *table,
20339                          enum dw_line_info_opcode opcode, unsigned int val)
20340 {
20341   dw_line_info_entry e;
20342   e.opcode = opcode;
20343   e.val = val;
20344   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20345 }
20346
20347 /* Output a label to mark the beginning of a source code line entry
20348    and record information relating to this source line, in
20349    'line_info_table' for later output of the .debug_line section.  */
20350 /* ??? The discriminator parameter ought to be unsigned.  */
20351
20352 static void
20353 dwarf2out_source_line (unsigned int line, const char *filename,
20354                        int discriminator, bool is_stmt)
20355 {
20356   unsigned int file_num;
20357   dw_line_info_table *table;
20358
20359   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20360     return;
20361
20362   /* The discriminator column was added in dwarf4.  Simplify the below
20363      by simply removing it if we're not supposed to output it.  */
20364   if (dwarf_version < 4 && dwarf_strict)
20365     discriminator = 0;
20366
20367   table = cur_line_info_table;
20368   file_num = maybe_emit_file (lookup_filename (filename));
20369
20370   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20371      the debugger has used the second (possibly duplicate) line number
20372      at the beginning of the function to mark the end of the prologue.
20373      We could eliminate any other duplicates within the function.  For
20374      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20375      that second line number entry.  */
20376   /* Recall that this end-of-prologue indication is *not* the same thing
20377      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20378      to which the hook corresponds, follows the last insn that was 
20379      emitted by gen_prologue.  What we need is to preceed the first insn
20380      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20381      insn that corresponds to something the user wrote.  These may be
20382      very different locations once scheduling is enabled.  */
20383
20384   if (0 && file_num == table->file_num
20385       && line == table->line_num
20386       && discriminator == table->discrim_num
20387       && is_stmt == table->is_stmt)
20388     return;
20389
20390   switch_to_section (current_function_section ());
20391
20392   /* If requested, emit something human-readable.  */
20393   if (flag_debug_asm)
20394     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20395
20396   if (DWARF2_ASM_LINE_DEBUG_INFO)
20397     {
20398       /* Emit the .loc directive understood by GNU as.  */
20399       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20400       if (is_stmt != table->is_stmt)
20401         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20402       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20403         fprintf (asm_out_file, " discriminator %d", discriminator);
20404       fputc ('\n', asm_out_file);
20405     }
20406   else
20407     {
20408       unsigned int label_num = ++line_info_label_num;
20409
20410       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20411
20412       push_dw_line_info_entry (table, LI_set_address, label_num);
20413       if (file_num != table->file_num)
20414         push_dw_line_info_entry (table, LI_set_file, file_num);
20415       if (discriminator != table->discrim_num)
20416         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20417       if (is_stmt != table->is_stmt)
20418         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20419       push_dw_line_info_entry (table, LI_set_line, line);
20420     }
20421
20422   table->file_num = file_num;
20423   table->line_num = line;
20424   table->discrim_num = discriminator;
20425   table->is_stmt = is_stmt;
20426   table->in_use = true;
20427 }
20428
20429 /* Record the beginning of a new source file.  */
20430
20431 static void
20432 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20433 {
20434   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20435     {
20436       /* Record the beginning of the file for break_out_includes.  */
20437       dw_die_ref bincl_die;
20438
20439       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20440       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20441     }
20442
20443   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20444     {
20445       macinfo_entry e;
20446       e.code = DW_MACINFO_start_file;
20447       e.lineno = lineno;
20448       e.info = xstrdup (filename);
20449       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20450     }
20451 }
20452
20453 /* Record the end of a source file.  */
20454
20455 static void
20456 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20457 {
20458   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20459     /* Record the end of the file for break_out_includes.  */
20460     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20461
20462   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20463     {
20464       macinfo_entry e;
20465       e.code = DW_MACINFO_end_file;
20466       e.lineno = lineno;
20467       e.info = NULL;
20468       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20469     }
20470 }
20471
20472 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20473    the tail part of the directive line, i.e. the part which is past the
20474    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20475
20476 static void
20477 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20478                   const char *buffer ATTRIBUTE_UNUSED)
20479 {
20480   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20481     {
20482       macinfo_entry e;
20483       /* Insert a dummy first entry to be able to optimize the whole
20484          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20485       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20486         {
20487           e.code = 0;
20488           e.lineno = 0;
20489           e.info = NULL;
20490           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20491         }
20492       e.code = DW_MACINFO_define;
20493       e.lineno = lineno;
20494       e.info = xstrdup (buffer);;
20495       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20496     }
20497 }
20498
20499 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20500    the tail part of the directive line, i.e. the part which is past the
20501    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20502
20503 static void
20504 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20505                  const char *buffer ATTRIBUTE_UNUSED)
20506 {
20507   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20508     {
20509       macinfo_entry e;
20510       /* Insert a dummy first entry to be able to optimize the whole
20511          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20512       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20513         {
20514           e.code = 0;
20515           e.lineno = 0;
20516           e.info = NULL;
20517           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20518         }
20519       e.code = DW_MACINFO_undef;
20520       e.lineno = lineno;
20521       e.info = xstrdup (buffer);
20522       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20523     }
20524 }
20525
20526 /* Routines to manipulate hash table of CUs.  */
20527
20528 static hashval_t
20529 htab_macinfo_hash (const void *of)
20530 {
20531   const macinfo_entry *const entry =
20532     (const macinfo_entry *) of;
20533
20534   return htab_hash_string (entry->info);
20535 }
20536
20537 static int
20538 htab_macinfo_eq (const void *of1, const void *of2)
20539 {
20540   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20541   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20542
20543   return !strcmp (entry1->info, entry2->info);
20544 }
20545
20546 /* Output a single .debug_macinfo entry.  */
20547
20548 static void
20549 output_macinfo_op (macinfo_entry *ref)
20550 {
20551   int file_num;
20552   size_t len;
20553   struct indirect_string_node *node;
20554   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20555
20556   switch (ref->code)
20557     {
20558     case DW_MACINFO_start_file:
20559       file_num = maybe_emit_file (lookup_filename (ref->info));
20560       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20561       dw2_asm_output_data_uleb128 (ref->lineno,
20562                                    "Included from line number %lu", 
20563                                    (unsigned long) ref->lineno);
20564       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20565       break;
20566     case DW_MACINFO_end_file:
20567       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20568       break;
20569     case DW_MACINFO_define:
20570     case DW_MACINFO_undef:
20571       len = strlen (ref->info) + 1;
20572       if (!dwarf_strict
20573           && len > DWARF_OFFSET_SIZE
20574           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20575           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20576         {
20577           ref->code = ref->code == DW_MACINFO_define
20578                       ? DW_MACRO_GNU_define_indirect
20579                       : DW_MACRO_GNU_undef_indirect;
20580           output_macinfo_op (ref);
20581           return;
20582         }
20583       dw2_asm_output_data (1, ref->code,
20584                            ref->code == DW_MACINFO_define
20585                            ? "Define macro" : "Undefine macro");
20586       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20587                                    (unsigned long) ref->lineno);
20588       dw2_asm_output_nstring (ref->info, -1, "The macro");
20589       break;
20590     case DW_MACRO_GNU_define_indirect:
20591     case DW_MACRO_GNU_undef_indirect:
20592       node = find_AT_string (ref->info);
20593       if (node->form != DW_FORM_strp)
20594         {
20595           char label[32];
20596           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20597           ++dw2_string_counter;
20598           node->label = xstrdup (label);
20599           node->form = DW_FORM_strp;
20600         }
20601       dw2_asm_output_data (1, ref->code,
20602                            ref->code == DW_MACRO_GNU_define_indirect
20603                            ? "Define macro indirect"
20604                            : "Undefine macro indirect");
20605       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20606                                    (unsigned long) ref->lineno);
20607       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20608                              debug_str_section, "The macro: \"%s\"",
20609                              ref->info);
20610       break;
20611     case DW_MACRO_GNU_transparent_include:
20612       dw2_asm_output_data (1, ref->code, "Transparent include");
20613       ASM_GENERATE_INTERNAL_LABEL (label,
20614                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20615       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20616       break;
20617     default:
20618       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20619                ASM_COMMENT_START, (unsigned long) ref->code);
20620       break;
20621     }
20622 }
20623
20624 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20625    other compilation unit .debug_macinfo sections.  IDX is the first
20626    index of a define/undef, return the number of ops that should be
20627    emitted in a comdat .debug_macinfo section and emit
20628    a DW_MACRO_GNU_transparent_include entry referencing it.
20629    If the define/undef entry should be emitted normally, return 0.  */
20630
20631 static unsigned
20632 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20633                         htab_t *macinfo_htab)
20634 {
20635   macinfo_entry *first, *second, *cur, *inc;
20636   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20637   unsigned char checksum[16];
20638   struct md5_ctx ctx;
20639   char *grp_name, *tail;
20640   const char *base;
20641   unsigned int i, count, encoded_filename_len, linebuf_len;
20642   void **slot;
20643
20644   first = VEC_index (macinfo_entry, macinfo_table, idx);
20645   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20646
20647   /* Optimize only if there are at least two consecutive define/undef ops,
20648      and either all of them are before first DW_MACINFO_start_file
20649      with lineno 0 (i.e. predefined macro block), or all of them are
20650      in some included header file.  */
20651   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20652     return 0;
20653   if (VEC_empty (macinfo_entry, files))
20654     {
20655       if (first->lineno != 0 || second->lineno != 0)
20656         return 0;
20657     }
20658   else if (first->lineno == 0)
20659     return 0;
20660
20661   /* Find the last define/undef entry that can be grouped together
20662      with first and at the same time compute md5 checksum of their
20663      codes, linenumbers and strings.  */
20664   md5_init_ctx (&ctx);
20665   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20666     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20667       break;
20668     else if (first->lineno == 0 && cur->lineno != 0)
20669       break;
20670     else
20671       {
20672         unsigned char code = cur->code;
20673         md5_process_bytes (&code, 1, &ctx);
20674         checksum_uleb128 (cur->lineno, &ctx);
20675         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20676       }
20677   md5_finish_ctx (&ctx, checksum);
20678   count = i - idx;
20679
20680   /* From the containing include filename (if any) pick up just
20681      usable characters from its basename.  */
20682   if (first->lineno == 0)
20683     base = "";
20684   else
20685     base = lbasename (VEC_last (macinfo_entry, files)->info);
20686   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20687     if (ISIDNUM (base[i]) || base[i] == '.')
20688       encoded_filename_len++;
20689   /* Count . at the end.  */
20690   if (encoded_filename_len)
20691     encoded_filename_len++;
20692
20693   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20694   linebuf_len = strlen (linebuf);
20695
20696   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20697   grp_name = XNEWVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20698                       + 16 * 2 + 1);
20699   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20700   tail = grp_name + 4;
20701   if (encoded_filename_len)
20702     {
20703       for (i = 0; base[i]; i++)
20704         if (ISIDNUM (base[i]) || base[i] == '.')
20705           *tail++ = base[i];
20706       *tail++ = '.';
20707     }
20708   memcpy (tail, linebuf, linebuf_len);
20709   tail += linebuf_len;
20710   *tail++ = '.';
20711   for (i = 0; i < 16; i++)
20712     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20713
20714   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20715      in the empty vector entry before the first define/undef.  */
20716   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20717   inc->code = DW_MACRO_GNU_transparent_include;
20718   inc->lineno = 0;
20719   inc->info = grp_name;
20720   if (*macinfo_htab == NULL)
20721     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20722   /* Avoid emitting duplicates.  */
20723   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20724   if (*slot != NULL)
20725     {
20726       free (CONST_CAST (char *, inc->info));
20727       inc->code = 0;
20728       inc->info = NULL;
20729       /* If such an entry has been used before, just emit
20730          a DW_MACRO_GNU_transparent_include op.  */
20731       inc = (macinfo_entry *) *slot;
20732       output_macinfo_op (inc);
20733       /* And clear all macinfo_entry in the range to avoid emitting them
20734          in the second pass.  */
20735       for (i = idx;
20736            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20737            && i < idx + count;
20738            i++)
20739         {
20740           cur->code = 0;
20741           free (CONST_CAST (char *, cur->info));
20742           cur->info = NULL;
20743         }
20744     }
20745   else
20746     {
20747       *slot = inc;
20748       inc->lineno = htab_elements (*macinfo_htab);
20749       output_macinfo_op (inc);
20750     }
20751   return count;
20752 }
20753
20754 /* Output macinfo section(s).  */
20755
20756 static void
20757 output_macinfo (void)
20758 {
20759   unsigned i;
20760   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20761   macinfo_entry *ref;
20762   VEC (macinfo_entry, gc) *files = NULL;
20763   htab_t macinfo_htab = NULL;
20764
20765   if (! length)
20766     return;
20767
20768   /* output_macinfo* uses these interchangeably.  */
20769   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20770               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20771               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20772               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20773
20774   /* For .debug_macro emit the section header.  */
20775   if (!dwarf_strict)
20776     {
20777       dw2_asm_output_data (2, 4, "DWARF macro version number");
20778       if (DWARF_OFFSET_SIZE == 8)
20779         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20780       else
20781         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20782       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20783                              debug_line_section, NULL);
20784     }
20785
20786   /* In the first loop, it emits the primary .debug_macinfo section
20787      and after each emitted op the macinfo_entry is cleared.
20788      If a longer range of define/undef ops can be optimized using
20789      DW_MACRO_GNU_transparent_include, the
20790      DW_MACRO_GNU_transparent_include op is emitted and kept in
20791      the vector before the first define/undef in the range and the
20792      whole range of define/undef ops is not emitted and kept.  */
20793   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20794     {
20795       switch (ref->code)
20796         {
20797         case DW_MACINFO_start_file:
20798           VEC_safe_push (macinfo_entry, gc, files, ref);
20799           break;
20800         case DW_MACINFO_end_file:
20801           if (!VEC_empty (macinfo_entry, files))
20802             {
20803               macinfo_entry *file = VEC_last (macinfo_entry, files);
20804               free (CONST_CAST (char *, file->info));
20805               VEC_pop (macinfo_entry, files);
20806             }
20807           break;
20808         case DW_MACINFO_define:
20809         case DW_MACINFO_undef:
20810           if (!dwarf_strict
20811               && HAVE_COMDAT_GROUP
20812               && VEC_length (macinfo_entry, files) != 1
20813               && i > 0
20814               && i + 1 < length
20815               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20816             {
20817               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20818               if (count)
20819                 {
20820                   i += count - 1;
20821                   continue;
20822                 }
20823             }
20824           break;
20825         case 0:
20826           /* A dummy entry may be inserted at the beginning to be able
20827              to optimize the whole block of predefined macros.  */
20828           if (i == 0)
20829             continue;
20830         default:
20831           break;
20832         }
20833       output_macinfo_op (ref);
20834       /* For DW_MACINFO_start_file ref->info has been copied into files
20835          vector.  */
20836       if (ref->code != DW_MACINFO_start_file)
20837         free (CONST_CAST (char *, ref->info));
20838       ref->info = NULL;
20839       ref->code = 0;
20840     }
20841
20842   if (macinfo_htab == NULL)
20843     return;
20844
20845   htab_delete (macinfo_htab);
20846
20847   /* If any DW_MACRO_GNU_transparent_include were used, on those
20848      DW_MACRO_GNU_transparent_include entries terminate the
20849      current chain and switch to a new comdat .debug_macinfo
20850      section and emit the define/undef entries within it.  */
20851   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20852     switch (ref->code)
20853       {
20854       case 0:
20855         continue;
20856       case DW_MACRO_GNU_transparent_include:
20857         {
20858           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20859           tree comdat_key = get_identifier (ref->info);
20860           /* Terminate the previous .debug_macinfo section.  */
20861           dw2_asm_output_data (1, 0, "End compilation unit");
20862           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20863                                          SECTION_DEBUG
20864                                          | SECTION_LINKONCE,
20865                                          comdat_key);
20866           ASM_GENERATE_INTERNAL_LABEL (label,
20867                                        DEBUG_MACRO_SECTION_LABEL,
20868                                        ref->lineno);
20869           ASM_OUTPUT_LABEL (asm_out_file, label);
20870           ref->code = 0;
20871           free (CONST_CAST (char *, ref->info));
20872           ref->info = NULL;
20873           dw2_asm_output_data (2, 4, "DWARF macro version number");
20874           if (DWARF_OFFSET_SIZE == 8)
20875             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20876           else
20877             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20878         }
20879         break;
20880       case DW_MACINFO_define:
20881       case DW_MACINFO_undef:
20882         output_macinfo_op (ref);
20883         ref->code = 0;
20884         free (CONST_CAST (char *, ref->info));
20885         ref->info = NULL;
20886         break;
20887       default:
20888         gcc_unreachable ();
20889       }
20890 }
20891
20892 /* Set up for Dwarf output at the start of compilation.  */
20893
20894 static void
20895 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20896 {
20897   /* Allocate the file_table.  */
20898   file_table = htab_create_ggc (50, file_table_hash,
20899                                 file_table_eq, NULL);
20900
20901   /* Allocate the decl_die_table.  */
20902   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20903                                     decl_die_table_eq, NULL);
20904
20905   /* Allocate the decl_loc_table.  */
20906   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20907                                     decl_loc_table_eq, NULL);
20908
20909   /* Allocate the cached_dw_loc_list_table.  */
20910   cached_dw_loc_list_table
20911     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20912                        cached_dw_loc_list_table_eq, NULL);
20913
20914   /* Allocate the initial hunk of the decl_scope_table.  */
20915   decl_scope_table = VEC_alloc (tree, gc, 256);
20916
20917   /* Allocate the initial hunk of the abbrev_die_table.  */
20918   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20919     (ABBREV_DIE_TABLE_INCREMENT);
20920   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20921   /* Zero-th entry is allocated, but unused.  */
20922   abbrev_die_table_in_use = 1;
20923
20924   /* Allocate the pubtypes and pubnames vectors.  */
20925   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20926   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20927
20928   incomplete_types = VEC_alloc (tree, gc, 64);
20929
20930   used_rtx_array = VEC_alloc (rtx, gc, 32);
20931
20932   debug_info_section = get_section (DEBUG_INFO_SECTION,
20933                                     SECTION_DEBUG, NULL);
20934   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20935                                       SECTION_DEBUG, NULL);
20936   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20937                                        SECTION_DEBUG, NULL);
20938   debug_macinfo_section = get_section (dwarf_strict
20939                                        ? DEBUG_MACINFO_SECTION
20940                                        : DEBUG_MACRO_SECTION,
20941                                        SECTION_DEBUG, NULL);
20942   debug_line_section = get_section (DEBUG_LINE_SECTION,
20943                                     SECTION_DEBUG, NULL);
20944   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20945                                    SECTION_DEBUG, NULL);
20946   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20947                                         SECTION_DEBUG, NULL);
20948   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20949                                         SECTION_DEBUG, NULL);
20950   debug_str_section = get_section (DEBUG_STR_SECTION,
20951                                    DEBUG_STR_SECTION_FLAGS, NULL);
20952   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20953                                       SECTION_DEBUG, NULL);
20954   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20955                                      SECTION_DEBUG, NULL);
20956
20957   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20958   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20959                                DEBUG_ABBREV_SECTION_LABEL, 0);
20960   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20961   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20962                                COLD_TEXT_SECTION_LABEL, 0);
20963   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20964
20965   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20966                                DEBUG_INFO_SECTION_LABEL, 0);
20967   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20968                                DEBUG_LINE_SECTION_LABEL, 0);
20969   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20970                                DEBUG_RANGES_SECTION_LABEL, 0);
20971   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20972                                dwarf_strict
20973                                ? DEBUG_MACINFO_SECTION_LABEL
20974                                : DEBUG_MACRO_SECTION_LABEL, 0);
20975
20976   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20977     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20978
20979   switch_to_section (text_section);
20980   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20981
20982   /* Make sure the line number table for .text always exists.  */
20983   text_section_line_info = new_line_info_table ();
20984   text_section_line_info->end_label = text_end_label;
20985 }
20986
20987 /* Called before cgraph_optimize starts outputtting functions, variables
20988    and toplevel asms into assembly.  */
20989
20990 static void
20991 dwarf2out_assembly_start (void)
20992 {
20993   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20994       && dwarf2out_do_cfi_asm ()
20995       && (!(flag_unwind_tables || flag_exceptions)
20996           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20997     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20998 }
20999
21000 /* A helper function for dwarf2out_finish called through
21001    htab_traverse.  Emit one queued .debug_str string.  */
21002
21003 static int
21004 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21005 {
21006   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21007
21008   if (node->form == DW_FORM_strp)
21009     {
21010       switch_to_section (debug_str_section);
21011       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21012       assemble_string (node->str, strlen (node->str) + 1);
21013     }
21014
21015   return 1;
21016 }
21017
21018 #if ENABLE_ASSERT_CHECKING
21019 /* Verify that all marks are clear.  */
21020
21021 static void
21022 verify_marks_clear (dw_die_ref die)
21023 {
21024   dw_die_ref c;
21025
21026   gcc_assert (! die->die_mark);
21027   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21028 }
21029 #endif /* ENABLE_ASSERT_CHECKING */
21030
21031 /* Clear the marks for a die and its children.
21032    Be cool if the mark isn't set.  */
21033
21034 static void
21035 prune_unmark_dies (dw_die_ref die)
21036 {
21037   dw_die_ref c;
21038
21039   if (die->die_mark)
21040     die->die_mark = 0;
21041   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21042 }
21043
21044 /* Given DIE that we're marking as used, find any other dies
21045    it references as attributes and mark them as used.  */
21046
21047 static void
21048 prune_unused_types_walk_attribs (dw_die_ref die)
21049 {
21050   dw_attr_ref a;
21051   unsigned ix;
21052
21053   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21054     {
21055       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21056         {
21057           /* A reference to another DIE.
21058              Make sure that it will get emitted.
21059              If it was broken out into a comdat group, don't follow it.  */
21060           if (! use_debug_types
21061               || a->dw_attr == DW_AT_specification
21062               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21063             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21064         }
21065       /* Set the string's refcount to 0 so that prune_unused_types_mark
21066          accounts properly for it.  */
21067       if (AT_class (a) == dw_val_class_str)
21068         a->dw_attr_val.v.val_str->refcount = 0;
21069     }
21070 }
21071
21072 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21073
21074 static void
21075 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21076 {
21077   dw_die_ref c;
21078
21079   if (die == NULL || die->die_child == NULL)
21080     return;
21081   c = die->die_child;
21082   do
21083     {
21084       switch (c->die_tag)
21085         {
21086         case DW_TAG_template_type_param:
21087         case DW_TAG_template_value_param:
21088         case DW_TAG_GNU_template_template_param:
21089         case DW_TAG_GNU_template_parameter_pack:
21090           prune_unused_types_mark (c, 1);
21091           break;
21092         default:
21093           break;
21094         }
21095       c = c->die_sib;
21096     } while (c && c != die->die_child);
21097 }
21098
21099 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21100    to DIE's children.  */
21101
21102 static void
21103 prune_unused_types_mark (dw_die_ref die, int dokids)
21104 {
21105   dw_die_ref c;
21106
21107   if (die->die_mark == 0)
21108     {
21109       /* We haven't done this node yet.  Mark it as used.  */
21110       die->die_mark = 1;
21111       /* If this is the DIE of a generic type instantiation,
21112          mark the children DIEs that describe its generic parms and
21113          args.  */
21114       prune_unused_types_mark_generic_parms_dies (die);
21115
21116       /* We also have to mark its parents as used.
21117          (But we don't want to mark our parents' kids due to this.)  */
21118       if (die->die_parent)
21119         prune_unused_types_mark (die->die_parent, 0);
21120
21121       /* Mark any referenced nodes.  */
21122       prune_unused_types_walk_attribs (die);
21123
21124       /* If this node is a specification,
21125          also mark the definition, if it exists.  */
21126       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21127         prune_unused_types_mark (die->die_definition, 1);
21128     }
21129
21130   if (dokids && die->die_mark != 2)
21131     {
21132       /* We need to walk the children, but haven't done so yet.
21133          Remember that we've walked the kids.  */
21134       die->die_mark = 2;
21135
21136       /* If this is an array type, we need to make sure our
21137          kids get marked, even if they're types.  If we're
21138          breaking out types into comdat sections, do this
21139          for all type definitions.  */
21140       if (die->die_tag == DW_TAG_array_type
21141           || (use_debug_types
21142               && is_type_die (die) && ! is_declaration_die (die)))
21143         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21144       else
21145         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21146     }
21147 }
21148
21149 /* For local classes, look if any static member functions were emitted
21150    and if so, mark them.  */
21151
21152 static void
21153 prune_unused_types_walk_local_classes (dw_die_ref die)
21154 {
21155   dw_die_ref c;
21156
21157   if (die->die_mark == 2)
21158     return;
21159
21160   switch (die->die_tag)
21161     {
21162     case DW_TAG_structure_type:
21163     case DW_TAG_union_type:
21164     case DW_TAG_class_type:
21165       break;
21166
21167     case DW_TAG_subprogram:
21168       if (!get_AT_flag (die, DW_AT_declaration)
21169           || die->die_definition != NULL)
21170         prune_unused_types_mark (die, 1);
21171       return;
21172
21173     default:
21174       return;
21175     }
21176
21177   /* Mark children.  */
21178   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21179 }
21180
21181 /* Walk the tree DIE and mark types that we actually use.  */
21182
21183 static void
21184 prune_unused_types_walk (dw_die_ref die)
21185 {
21186   dw_die_ref c;
21187
21188   /* Don't do anything if this node is already marked and
21189      children have been marked as well.  */
21190   if (die->die_mark == 2)
21191     return;
21192
21193   switch (die->die_tag)
21194     {
21195     case DW_TAG_structure_type:
21196     case DW_TAG_union_type:
21197     case DW_TAG_class_type:
21198       if (die->die_perennial_p)
21199         break;
21200
21201       for (c = die->die_parent; c; c = c->die_parent)
21202         if (c->die_tag == DW_TAG_subprogram)
21203           break;
21204
21205       /* Finding used static member functions inside of classes
21206          is needed just for local classes, because for other classes
21207          static member function DIEs with DW_AT_specification
21208          are emitted outside of the DW_TAG_*_type.  If we ever change
21209          it, we'd need to call this even for non-local classes.  */
21210       if (c)
21211         prune_unused_types_walk_local_classes (die);
21212
21213       /* It's a type node --- don't mark it.  */
21214       return;
21215
21216     case DW_TAG_const_type:
21217     case DW_TAG_packed_type:
21218     case DW_TAG_pointer_type:
21219     case DW_TAG_reference_type:
21220     case DW_TAG_rvalue_reference_type:
21221     case DW_TAG_volatile_type:
21222     case DW_TAG_typedef:
21223     case DW_TAG_array_type:
21224     case DW_TAG_interface_type:
21225     case DW_TAG_friend:
21226     case DW_TAG_variant_part:
21227     case DW_TAG_enumeration_type:
21228     case DW_TAG_subroutine_type:
21229     case DW_TAG_string_type:
21230     case DW_TAG_set_type:
21231     case DW_TAG_subrange_type:
21232     case DW_TAG_ptr_to_member_type:
21233     case DW_TAG_file_type:
21234       if (die->die_perennial_p)
21235         break;
21236
21237       /* It's a type node --- don't mark it.  */
21238       return;
21239
21240     default:
21241       /* Mark everything else.  */
21242       break;
21243   }
21244
21245   if (die->die_mark == 0)
21246     {
21247       die->die_mark = 1;
21248
21249       /* Now, mark any dies referenced from here.  */
21250       prune_unused_types_walk_attribs (die);
21251     }
21252
21253   die->die_mark = 2;
21254
21255   /* Mark children.  */
21256   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21257 }
21258
21259 /* Increment the string counts on strings referred to from DIE's
21260    attributes.  */
21261
21262 static void
21263 prune_unused_types_update_strings (dw_die_ref die)
21264 {
21265   dw_attr_ref a;
21266   unsigned ix;
21267
21268   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21269     if (AT_class (a) == dw_val_class_str)
21270       {
21271         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21272         s->refcount++;
21273         /* Avoid unnecessarily putting strings that are used less than
21274            twice in the hash table.  */
21275         if (s->refcount
21276             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21277           {
21278             void ** slot;
21279             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21280                                              htab_hash_string (s->str),
21281                                              INSERT);
21282             gcc_assert (*slot == NULL);
21283             *slot = s;
21284           }
21285       }
21286 }
21287
21288 /* Remove from the tree DIE any dies that aren't marked.  */
21289
21290 static void
21291 prune_unused_types_prune (dw_die_ref die)
21292 {
21293   dw_die_ref c;
21294
21295   gcc_assert (die->die_mark);
21296   prune_unused_types_update_strings (die);
21297
21298   if (! die->die_child)
21299     return;
21300
21301   c = die->die_child;
21302   do {
21303     dw_die_ref prev = c;
21304     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21305       if (c == die->die_child)
21306         {
21307           /* No marked children between 'prev' and the end of the list.  */
21308           if (prev == c)
21309             /* No marked children at all.  */
21310             die->die_child = NULL;
21311           else
21312             {
21313               prev->die_sib = c->die_sib;
21314               die->die_child = prev;
21315             }
21316           return;
21317         }
21318
21319     if (c != prev->die_sib)
21320       prev->die_sib = c;
21321     prune_unused_types_prune (c);
21322   } while (c != die->die_child);
21323 }
21324
21325 /* Remove dies representing declarations that we never use.  */
21326
21327 static void
21328 prune_unused_types (void)
21329 {
21330   unsigned int i;
21331   limbo_die_node *node;
21332   comdat_type_node *ctnode;
21333   pubname_ref pub;
21334   dw_die_ref base_type;
21335
21336 #if ENABLE_ASSERT_CHECKING
21337   /* All the marks should already be clear.  */
21338   verify_marks_clear (comp_unit_die ());
21339   for (node = limbo_die_list; node; node = node->next)
21340     verify_marks_clear (node->die);
21341   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21342     verify_marks_clear (ctnode->root_die);
21343 #endif /* ENABLE_ASSERT_CHECKING */
21344
21345   /* Mark types that are used in global variables.  */
21346   premark_types_used_by_global_vars ();
21347
21348   /* Set the mark on nodes that are actually used.  */
21349   prune_unused_types_walk (comp_unit_die ());
21350   for (node = limbo_die_list; node; node = node->next)
21351     prune_unused_types_walk (node->die);
21352   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21353     {
21354       prune_unused_types_walk (ctnode->root_die);
21355       prune_unused_types_mark (ctnode->type_die, 1);
21356     }
21357
21358   /* Also set the mark on nodes referenced from the
21359      pubname_table.  */
21360   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21361     prune_unused_types_mark (pub->die, 1);
21362   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21363     prune_unused_types_mark (base_type, 1);
21364
21365   if (debug_str_hash)
21366     htab_empty (debug_str_hash);
21367   prune_unused_types_prune (comp_unit_die ());
21368   for (node = limbo_die_list; node; node = node->next)
21369     prune_unused_types_prune (node->die);
21370   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21371     prune_unused_types_prune (ctnode->root_die);
21372
21373   /* Leave the marks clear.  */
21374   prune_unmark_dies (comp_unit_die ());
21375   for (node = limbo_die_list; node; node = node->next)
21376     prune_unmark_dies (node->die);
21377   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21378     prune_unmark_dies (ctnode->root_die);
21379 }
21380
21381 /* Set the parameter to true if there are any relative pathnames in
21382    the file table.  */
21383 static int
21384 file_table_relative_p (void ** slot, void *param)
21385 {
21386   bool *p = (bool *) param;
21387   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21388   if (!IS_ABSOLUTE_PATH (d->filename))
21389     {
21390       *p = true;
21391       return 0;
21392     }
21393   return 1;
21394 }
21395
21396 /* Routines to manipulate hash table of comdat type units.  */
21397
21398 static hashval_t
21399 htab_ct_hash (const void *of)
21400 {
21401   hashval_t h;
21402   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21403
21404   memcpy (&h, type_node->signature, sizeof (h));
21405   return h;
21406 }
21407
21408 static int
21409 htab_ct_eq (const void *of1, const void *of2)
21410 {
21411   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21412   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21413
21414   return (! memcmp (type_node_1->signature, type_node_2->signature,
21415                     DWARF_TYPE_SIGNATURE_SIZE));
21416 }
21417
21418 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21419    to the location it would have been added, should we know its
21420    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21421    probably improve compactness of debug info, removing equivalent
21422    abbrevs, and hide any differences caused by deferring the
21423    computation of the assembler name, triggered by e.g. PCH.  */
21424
21425 static inline void
21426 move_linkage_attr (dw_die_ref die)
21427 {
21428   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21429   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21430
21431   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21432               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21433
21434   while (--ix > 0)
21435     {
21436       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21437
21438       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21439         break;
21440     }
21441
21442   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21443     {
21444       VEC_pop (dw_attr_node, die->die_attr);
21445       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21446     }
21447 }
21448
21449 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21450    referenced from typed stack ops and count how often they are used.  */
21451
21452 static void
21453 mark_base_types (dw_loc_descr_ref loc)
21454 {
21455   dw_die_ref base_type = NULL;
21456
21457   for (; loc; loc = loc->dw_loc_next)
21458     {
21459       switch (loc->dw_loc_opc)
21460         {
21461         case DW_OP_GNU_regval_type:
21462         case DW_OP_GNU_deref_type:
21463           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21464           break;
21465         case DW_OP_GNU_convert:
21466         case DW_OP_GNU_reinterpret:
21467           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21468             continue;
21469           /* FALLTHRU */
21470         case DW_OP_GNU_const_type:
21471           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21472           break;
21473         case DW_OP_GNU_entry_value:
21474           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21475           continue;
21476         default:
21477           continue;
21478         }
21479       gcc_assert (base_type->die_parent == comp_unit_die ());
21480       if (base_type->die_mark)
21481         base_type->die_mark++;
21482       else
21483         {
21484           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21485           base_type->die_mark = 1;
21486         }
21487     }
21488 }
21489
21490 /* Comparison function for sorting marked base types.  */
21491
21492 static int
21493 base_type_cmp (const void *x, const void *y)
21494 {
21495   dw_die_ref dx = *(const dw_die_ref *) x;
21496   dw_die_ref dy = *(const dw_die_ref *) y;
21497   unsigned int byte_size1, byte_size2;
21498   unsigned int encoding1, encoding2;
21499   if (dx->die_mark > dy->die_mark)
21500     return -1;
21501   if (dx->die_mark < dy->die_mark)
21502     return 1;
21503   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21504   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21505   if (byte_size1 < byte_size2)
21506     return 1;
21507   if (byte_size1 > byte_size2)
21508     return -1;
21509   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21510   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21511   if (encoding1 < encoding2)
21512     return 1;
21513   if (encoding1 > encoding2)
21514     return -1;
21515   return 0;
21516 }
21517
21518 /* Move base types marked by mark_base_types as early as possible
21519    in the CU, sorted by decreasing usage count both to make the
21520    uleb128 references as small as possible and to make sure they
21521    will have die_offset already computed by calc_die_sizes when
21522    sizes of typed stack loc ops is computed.  */
21523
21524 static void
21525 move_marked_base_types (void)
21526 {
21527   unsigned int i;
21528   dw_die_ref base_type, die, c;
21529
21530   if (VEC_empty (dw_die_ref, base_types))
21531     return;
21532
21533   /* Sort by decreasing usage count, they will be added again in that
21534      order later on.  */
21535   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21536   die = comp_unit_die ();
21537   c = die->die_child;
21538   do
21539     {
21540       dw_die_ref prev = c;
21541       c = c->die_sib;
21542       while (c->die_mark)
21543         {
21544           remove_child_with_prev (c, prev);
21545           /* As base types got marked, there must be at least
21546              one node other than DW_TAG_base_type.  */
21547           gcc_assert (c != c->die_sib);
21548           c = c->die_sib;
21549         }
21550     }
21551   while (c != die->die_child);
21552   gcc_assert (die->die_child);
21553   c = die->die_child;
21554   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21555     {
21556       base_type->die_mark = 0;
21557       base_type->die_sib = c->die_sib;
21558       c->die_sib = base_type;
21559       c = base_type;
21560     }
21561 }
21562
21563 /* Helper function for resolve_addr, attempt to resolve
21564    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21565    SYMBOL_REFs refer to variables emitted in the current CU.  */
21566
21567 static int
21568 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21569 {
21570   rtx rtl = *addr;
21571
21572   if (GET_CODE (rtl) == CONST_STRING)
21573     {
21574       size_t len = strlen (XSTR (rtl, 0)) + 1;
21575       tree t = build_string (len, XSTR (rtl, 0));
21576       tree tlen = size_int (len - 1);
21577       TREE_TYPE (t)
21578         = build_array_type (char_type_node, build_index_type (tlen));
21579       rtl = lookup_constant_def (t);
21580       if (!rtl || !MEM_P (rtl))
21581         return 1;
21582       rtl = XEXP (rtl, 0);
21583       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21584       *addr = rtl;
21585       return 0;
21586     }
21587
21588   if (GET_CODE (rtl) == SYMBOL_REF
21589       && SYMBOL_REF_DECL (rtl))
21590     {
21591       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21592         {
21593           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21594             return 1;
21595         }
21596       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21597         return 1;
21598     }
21599
21600   if (GET_CODE (rtl) == CONST
21601       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21602     return 1;
21603
21604   return 0;
21605 }
21606
21607 /* Helper function for resolve_addr, handle one location
21608    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21609    the location list couldn't be resolved.  */
21610
21611 static bool
21612 resolve_addr_in_expr (dw_loc_descr_ref loc)
21613 {
21614   dw_loc_descr_ref keep = NULL;
21615   for (; loc; loc = loc->dw_loc_next)
21616     switch (loc->dw_loc_opc)
21617       {
21618       case DW_OP_addr:
21619         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21620           return false;
21621         break;
21622       case DW_OP_const4u:
21623       case DW_OP_const8u:
21624         if (loc->dtprel
21625             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21626           return false;
21627         break;
21628       case DW_OP_plus_uconst:
21629         if (size_of_loc_descr (loc)
21630             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21631               + 1
21632             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21633           {
21634             dw_loc_descr_ref repl
21635               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21636             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21637             add_loc_descr (&repl, loc->dw_loc_next);
21638             *loc = *repl;
21639           }
21640         break;
21641       case DW_OP_implicit_value:
21642         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21643             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21644           return false;
21645         break;
21646       case DW_OP_GNU_implicit_pointer:
21647       case DW_OP_GNU_parameter_ref:
21648         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21649           {
21650             dw_die_ref ref
21651               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21652             if (ref == NULL)
21653               return false;
21654             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21655             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21656             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21657           }
21658         break;
21659       case DW_OP_GNU_const_type:
21660       case DW_OP_GNU_regval_type:
21661       case DW_OP_GNU_deref_type:
21662       case DW_OP_GNU_convert:
21663       case DW_OP_GNU_reinterpret:
21664         while (loc->dw_loc_next
21665                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21666           {
21667             dw_die_ref base1, base2;
21668             unsigned enc1, enc2, size1, size2;
21669             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21670                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21671               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21672             else if (loc->dw_loc_oprnd1.val_class
21673                      == dw_val_class_unsigned_const)
21674               break;
21675             else
21676               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21677             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21678                 == dw_val_class_unsigned_const)
21679               break;
21680             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21681             gcc_assert (base1->die_tag == DW_TAG_base_type
21682                         && base2->die_tag == DW_TAG_base_type);
21683             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21684             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21685             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21686             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21687             if (size1 == size2
21688                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21689                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21690                      && loc != keep)
21691                     || enc1 == enc2))
21692               {
21693                 /* Optimize away next DW_OP_GNU_convert after
21694                    adjusting LOC's base type die reference.  */
21695                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21696                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21697                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21698                 else
21699                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21700                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21701                 continue;
21702               }
21703             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21704                point typed stack entry.  */
21705             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21706               keep = loc->dw_loc_next;
21707             break;
21708           }
21709         break;
21710       default:
21711         break;
21712       }
21713   return true;
21714 }
21715
21716 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21717    an address in .rodata section if the string literal is emitted there,
21718    or remove the containing location list or replace DW_AT_const_value
21719    with DW_AT_location and empty location expression, if it isn't found
21720    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21721    to something that has been emitted in the current CU.  */
21722
21723 static void
21724 resolve_addr (dw_die_ref die)
21725 {
21726   dw_die_ref c;
21727   dw_attr_ref a;
21728   dw_loc_list_ref *curr, *start, loc;
21729   unsigned ix;
21730
21731   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21732     switch (AT_class (a))
21733       {
21734       case dw_val_class_loc_list:
21735         start = curr = AT_loc_list_ptr (a);
21736         loc = *curr;
21737         gcc_assert (loc);
21738         /* The same list can be referenced more than once.  See if we have
21739            already recorded the result from a previous pass.  */
21740         if (loc->replaced)
21741           *curr = loc->dw_loc_next;
21742         else if (!loc->resolved_addr)
21743           {
21744             /* As things stand, we do not expect or allow one die to
21745                reference a suffix of another die's location list chain.
21746                References must be identical or completely separate.
21747                There is therefore no need to cache the result of this
21748                pass on any list other than the first; doing so
21749                would lead to unnecessary writes.  */
21750             while (*curr)
21751               {
21752                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21753                 if (!resolve_addr_in_expr ((*curr)->expr))
21754                   {
21755                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21756                     if (next && (*curr)->ll_symbol)
21757                       {
21758                         gcc_assert (!next->ll_symbol);
21759                         next->ll_symbol = (*curr)->ll_symbol;
21760                       }
21761                     *curr = next;
21762                   }
21763                 else
21764                   {
21765                     mark_base_types ((*curr)->expr);
21766                     curr = &(*curr)->dw_loc_next;
21767                   }
21768               }
21769             if (loc == *start)
21770               loc->resolved_addr = 1;
21771             else
21772               {
21773                 loc->replaced = 1;
21774                 loc->dw_loc_next = *start;
21775               }
21776           }
21777         if (!*start)
21778           {
21779             remove_AT (die, a->dw_attr);
21780             ix--;
21781           }
21782         break;
21783       case dw_val_class_loc:
21784         if (!resolve_addr_in_expr (AT_loc (a)))
21785           {
21786             remove_AT (die, a->dw_attr);
21787             ix--;
21788           }
21789         else
21790           mark_base_types (AT_loc (a));
21791         break;
21792       case dw_val_class_addr:
21793         if (a->dw_attr == DW_AT_const_value
21794             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21795           {
21796             remove_AT (die, a->dw_attr);
21797             ix--;
21798           }
21799         if (die->die_tag == DW_TAG_GNU_call_site
21800             && a->dw_attr == DW_AT_abstract_origin)
21801           {
21802             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21803             dw_die_ref tdie = lookup_decl_die (tdecl);
21804             if (tdie == NULL
21805                 && DECL_EXTERNAL (tdecl)
21806                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21807               {
21808                 force_decl_die (tdecl);
21809                 tdie = lookup_decl_die (tdecl);
21810               }
21811             if (tdie)
21812               {
21813                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21814                 a->dw_attr_val.v.val_die_ref.die = tdie;
21815                 a->dw_attr_val.v.val_die_ref.external = 0;
21816               }
21817             else
21818               {
21819                 remove_AT (die, a->dw_attr);
21820                 ix--;
21821               }
21822           }
21823         break;
21824       default:
21825         break;
21826       }
21827
21828   FOR_EACH_CHILD (die, c, resolve_addr (c));
21829 }
21830 \f
21831 /* Helper routines for optimize_location_lists.
21832    This pass tries to share identical local lists in .debug_loc
21833    section.  */
21834
21835 /* Iteratively hash operands of LOC opcode.  */
21836
21837 static inline hashval_t
21838 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21839 {
21840   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21841   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21842
21843   switch (loc->dw_loc_opc)
21844     {
21845     case DW_OP_const4u:
21846     case DW_OP_const8u:
21847       if (loc->dtprel)
21848         goto hash_addr;
21849       /* FALLTHRU */
21850     case DW_OP_const1u:
21851     case DW_OP_const1s:
21852     case DW_OP_const2u:
21853     case DW_OP_const2s:
21854     case DW_OP_const4s:
21855     case DW_OP_const8s:
21856     case DW_OP_constu:
21857     case DW_OP_consts:
21858     case DW_OP_pick:
21859     case DW_OP_plus_uconst:
21860     case DW_OP_breg0:
21861     case DW_OP_breg1:
21862     case DW_OP_breg2:
21863     case DW_OP_breg3:
21864     case DW_OP_breg4:
21865     case DW_OP_breg5:
21866     case DW_OP_breg6:
21867     case DW_OP_breg7:
21868     case DW_OP_breg8:
21869     case DW_OP_breg9:
21870     case DW_OP_breg10:
21871     case DW_OP_breg11:
21872     case DW_OP_breg12:
21873     case DW_OP_breg13:
21874     case DW_OP_breg14:
21875     case DW_OP_breg15:
21876     case DW_OP_breg16:
21877     case DW_OP_breg17:
21878     case DW_OP_breg18:
21879     case DW_OP_breg19:
21880     case DW_OP_breg20:
21881     case DW_OP_breg21:
21882     case DW_OP_breg22:
21883     case DW_OP_breg23:
21884     case DW_OP_breg24:
21885     case DW_OP_breg25:
21886     case DW_OP_breg26:
21887     case DW_OP_breg27:
21888     case DW_OP_breg28:
21889     case DW_OP_breg29:
21890     case DW_OP_breg30:
21891     case DW_OP_breg31:
21892     case DW_OP_regx:
21893     case DW_OP_fbreg:
21894     case DW_OP_piece:
21895     case DW_OP_deref_size:
21896     case DW_OP_xderef_size:
21897       hash = iterative_hash_object (val1->v.val_int, hash);
21898       break;
21899     case DW_OP_skip:
21900     case DW_OP_bra:
21901       {
21902         int offset;
21903
21904         gcc_assert (val1->val_class == dw_val_class_loc);
21905         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21906         hash = iterative_hash_object (offset, hash);
21907       }
21908       break;
21909     case DW_OP_implicit_value:
21910       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21911       switch (val2->val_class)
21912         {
21913         case dw_val_class_const:
21914           hash = iterative_hash_object (val2->v.val_int, hash);
21915           break;
21916         case dw_val_class_vec:
21917           {
21918             unsigned int elt_size = val2->v.val_vec.elt_size;
21919             unsigned int len = val2->v.val_vec.length;
21920
21921             hash = iterative_hash_object (elt_size, hash);
21922             hash = iterative_hash_object (len, hash);
21923             hash = iterative_hash (val2->v.val_vec.array,
21924                                    len * elt_size, hash);
21925           }
21926           break;
21927         case dw_val_class_const_double:
21928           hash = iterative_hash_object (val2->v.val_double.low, hash);
21929           hash = iterative_hash_object (val2->v.val_double.high, hash);
21930           break;
21931         case dw_val_class_addr:
21932           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21933           break;
21934         default:
21935           gcc_unreachable ();
21936         }
21937       break;
21938     case DW_OP_bregx:
21939     case DW_OP_bit_piece:
21940       hash = iterative_hash_object (val1->v.val_int, hash);
21941       hash = iterative_hash_object (val2->v.val_int, hash);
21942       break;
21943     case DW_OP_addr:
21944     hash_addr:
21945       if (loc->dtprel)
21946         {
21947           unsigned char dtprel = 0xd1;
21948           hash = iterative_hash_object (dtprel, hash);
21949         }
21950       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21951       break;
21952     case DW_OP_GNU_implicit_pointer:
21953       hash = iterative_hash_object (val2->v.val_int, hash);
21954       break;
21955     case DW_OP_GNU_entry_value:
21956       hash = hash_loc_operands (val1->v.val_loc, hash);
21957       break;
21958     case DW_OP_GNU_regval_type:
21959     case DW_OP_GNU_deref_type:
21960       {
21961         unsigned int byte_size
21962           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21963         unsigned int encoding
21964           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21965         hash = iterative_hash_object (val1->v.val_int, hash);
21966         hash = iterative_hash_object (byte_size, hash);
21967         hash = iterative_hash_object (encoding, hash);
21968       }
21969       break;
21970     case DW_OP_GNU_convert:
21971     case DW_OP_GNU_reinterpret:
21972       if (val1->val_class == dw_val_class_unsigned_const)
21973         {
21974           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21975           break;
21976         }
21977       /* FALLTHRU */
21978     case DW_OP_GNU_const_type:
21979       {
21980         unsigned int byte_size
21981           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21982         unsigned int encoding
21983           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21984         hash = iterative_hash_object (byte_size, hash);
21985         hash = iterative_hash_object (encoding, hash);
21986         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21987           break;
21988         hash = iterative_hash_object (val2->val_class, hash);
21989         switch (val2->val_class)
21990           {
21991           case dw_val_class_const:
21992             hash = iterative_hash_object (val2->v.val_int, hash);
21993             break;
21994           case dw_val_class_vec:
21995             {
21996               unsigned int elt_size = val2->v.val_vec.elt_size;
21997               unsigned int len = val2->v.val_vec.length;
21998
21999               hash = iterative_hash_object (elt_size, hash);
22000               hash = iterative_hash_object (len, hash);
22001               hash = iterative_hash (val2->v.val_vec.array,
22002                                      len * elt_size, hash);
22003             }
22004             break;
22005           case dw_val_class_const_double:
22006             hash = iterative_hash_object (val2->v.val_double.low, hash);
22007             hash = iterative_hash_object (val2->v.val_double.high, hash);
22008             break;
22009           default:
22010             gcc_unreachable ();
22011           }
22012       }
22013       break;
22014
22015     default:
22016       /* Other codes have no operands.  */
22017       break;
22018     }
22019   return hash;
22020 }
22021
22022 /* Iteratively hash the whole DWARF location expression LOC.  */
22023
22024 static inline hashval_t
22025 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22026 {
22027   dw_loc_descr_ref l;
22028   bool sizes_computed = false;
22029   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22030   size_of_locs (loc);
22031
22032   for (l = loc; l != NULL; l = l->dw_loc_next)
22033     {
22034       enum dwarf_location_atom opc = l->dw_loc_opc;
22035       hash = iterative_hash_object (opc, hash);
22036       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22037         {
22038           size_of_locs (loc);
22039           sizes_computed = true;
22040         }
22041       hash = hash_loc_operands (l, hash);
22042     }
22043   return hash;
22044 }
22045
22046 /* Compute hash of the whole location list LIST_HEAD.  */
22047
22048 static inline void
22049 hash_loc_list (dw_loc_list_ref list_head)
22050 {
22051   dw_loc_list_ref curr = list_head;
22052   hashval_t hash = 0;
22053
22054   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22055     {
22056       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22057       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22058       if (curr->section)
22059         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22060                                hash);
22061       hash = hash_locs (curr->expr, hash);
22062     }
22063   list_head->hash = hash;
22064 }
22065
22066 /* Return true if X and Y opcodes have the same operands.  */
22067
22068 static inline bool
22069 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22070 {
22071   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22072   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22073   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22074   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22075
22076   switch (x->dw_loc_opc)
22077     {
22078     case DW_OP_const4u:
22079     case DW_OP_const8u:
22080       if (x->dtprel)
22081         goto hash_addr;
22082       /* FALLTHRU */
22083     case DW_OP_const1u:
22084     case DW_OP_const1s:
22085     case DW_OP_const2u:
22086     case DW_OP_const2s:
22087     case DW_OP_const4s:
22088     case DW_OP_const8s:
22089     case DW_OP_constu:
22090     case DW_OP_consts:
22091     case DW_OP_pick:
22092     case DW_OP_plus_uconst:
22093     case DW_OP_breg0:
22094     case DW_OP_breg1:
22095     case DW_OP_breg2:
22096     case DW_OP_breg3:
22097     case DW_OP_breg4:
22098     case DW_OP_breg5:
22099     case DW_OP_breg6:
22100     case DW_OP_breg7:
22101     case DW_OP_breg8:
22102     case DW_OP_breg9:
22103     case DW_OP_breg10:
22104     case DW_OP_breg11:
22105     case DW_OP_breg12:
22106     case DW_OP_breg13:
22107     case DW_OP_breg14:
22108     case DW_OP_breg15:
22109     case DW_OP_breg16:
22110     case DW_OP_breg17:
22111     case DW_OP_breg18:
22112     case DW_OP_breg19:
22113     case DW_OP_breg20:
22114     case DW_OP_breg21:
22115     case DW_OP_breg22:
22116     case DW_OP_breg23:
22117     case DW_OP_breg24:
22118     case DW_OP_breg25:
22119     case DW_OP_breg26:
22120     case DW_OP_breg27:
22121     case DW_OP_breg28:
22122     case DW_OP_breg29:
22123     case DW_OP_breg30:
22124     case DW_OP_breg31:
22125     case DW_OP_regx:
22126     case DW_OP_fbreg:
22127     case DW_OP_piece:
22128     case DW_OP_deref_size:
22129     case DW_OP_xderef_size:
22130       return valx1->v.val_int == valy1->v.val_int;
22131     case DW_OP_skip:
22132     case DW_OP_bra:
22133       gcc_assert (valx1->val_class == dw_val_class_loc
22134                   && valy1->val_class == dw_val_class_loc
22135                   && x->dw_loc_addr == y->dw_loc_addr);
22136       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22137     case DW_OP_implicit_value:
22138       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22139           || valx2->val_class != valy2->val_class)
22140         return false;
22141       switch (valx2->val_class)
22142         {
22143         case dw_val_class_const:
22144           return valx2->v.val_int == valy2->v.val_int;
22145         case dw_val_class_vec:
22146           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22147                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22148                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22149                             valx2->v.val_vec.elt_size
22150                             * valx2->v.val_vec.length) == 0;
22151         case dw_val_class_const_double:
22152           return valx2->v.val_double.low == valy2->v.val_double.low
22153                  && valx2->v.val_double.high == valy2->v.val_double.high;
22154         case dw_val_class_addr:
22155           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22156         default:
22157           gcc_unreachable ();
22158         }
22159     case DW_OP_bregx:
22160     case DW_OP_bit_piece:
22161       return valx1->v.val_int == valy1->v.val_int
22162              && valx2->v.val_int == valy2->v.val_int;
22163     case DW_OP_addr:
22164     hash_addr:
22165       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22166     case DW_OP_GNU_implicit_pointer:
22167       return valx1->val_class == dw_val_class_die_ref
22168              && valx1->val_class == valy1->val_class
22169              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22170              && valx2->v.val_int == valy2->v.val_int;
22171     case DW_OP_GNU_entry_value:
22172       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22173     case DW_OP_GNU_const_type:
22174       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22175           || valx2->val_class != valy2->val_class)
22176         return false;
22177       switch (valx2->val_class)
22178         {
22179         case dw_val_class_const:
22180           return valx2->v.val_int == valy2->v.val_int;
22181         case dw_val_class_vec:
22182           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22183                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22184                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22185                             valx2->v.val_vec.elt_size
22186                             * valx2->v.val_vec.length) == 0;
22187         case dw_val_class_const_double:
22188           return valx2->v.val_double.low == valy2->v.val_double.low
22189                  && valx2->v.val_double.high == valy2->v.val_double.high;
22190         default:
22191           gcc_unreachable ();
22192         }
22193     case DW_OP_GNU_regval_type:
22194     case DW_OP_GNU_deref_type:
22195       return valx1->v.val_int == valy1->v.val_int
22196              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22197     case DW_OP_GNU_convert:
22198     case DW_OP_GNU_reinterpret:
22199       if (valx1->val_class != valy1->val_class)
22200         return false;
22201       if (valx1->val_class == dw_val_class_unsigned_const)
22202         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22203       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22204     case DW_OP_GNU_parameter_ref:
22205       return valx1->val_class == dw_val_class_die_ref
22206              && valx1->val_class == valy1->val_class
22207              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22208     default:
22209       /* Other codes have no operands.  */
22210       return true;
22211     }
22212 }
22213
22214 /* Return true if DWARF location expressions X and Y are the same.  */
22215
22216 static inline bool
22217 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22218 {
22219   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22220     if (x->dw_loc_opc != y->dw_loc_opc
22221         || x->dtprel != y->dtprel
22222         || !compare_loc_operands (x, y))
22223       break;
22224   return x == NULL && y == NULL;
22225 }
22226
22227 /* Return precomputed hash of location list X.  */
22228
22229 static hashval_t
22230 loc_list_hash (const void *x)
22231 {
22232   return ((const struct dw_loc_list_struct *) x)->hash;
22233 }
22234
22235 /* Return 1 if location lists X and Y are the same.  */
22236
22237 static int
22238 loc_list_eq (const void *x, const void *y)
22239 {
22240   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22241   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22242   if (a == b)
22243     return 1;
22244   if (a->hash != b->hash)
22245     return 0;
22246   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22247     if (strcmp (a->begin, b->begin) != 0
22248         || strcmp (a->end, b->end) != 0
22249         || (a->section == NULL) != (b->section == NULL)
22250         || (a->section && strcmp (a->section, b->section) != 0)
22251         || !compare_locs (a->expr, b->expr))
22252       break;
22253   return a == NULL && b == NULL;
22254 }
22255
22256 /* Recursively optimize location lists referenced from DIE
22257    children and share them whenever possible.  */
22258
22259 static void
22260 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22261 {
22262   dw_die_ref c;
22263   dw_attr_ref a;
22264   unsigned ix;
22265   void **slot;
22266
22267   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22268     if (AT_class (a) == dw_val_class_loc_list)
22269       {
22270         dw_loc_list_ref list = AT_loc_list (a);
22271         /* TODO: perform some optimizations here, before hashing
22272            it and storing into the hash table.  */
22273         hash_loc_list (list);
22274         slot = htab_find_slot_with_hash (htab, list, list->hash,
22275                                          INSERT);
22276         if (*slot == NULL)
22277           *slot = (void *) list;
22278         else
22279           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22280       }
22281
22282   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22283 }
22284
22285 /* Optimize location lists referenced from DIE
22286    children and share them whenever possible.  */
22287
22288 static void
22289 optimize_location_lists (dw_die_ref die)
22290 {
22291   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22292   optimize_location_lists_1 (die, htab);
22293   htab_delete (htab);
22294 }
22295 \f
22296 /* Output stuff that dwarf requires at the end of every file,
22297    and generate the DWARF-2 debugging info.  */
22298
22299 static void
22300 dwarf2out_finish (const char *filename)
22301 {
22302   limbo_die_node *node, *next_node;
22303   comdat_type_node *ctnode;
22304   htab_t comdat_type_table;
22305   unsigned int i;
22306
22307   /* PCH might result in DW_AT_producer string being restored from the
22308      header compilation, fix it up if needed.  */
22309   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22310   if (strcmp (AT_string (producer), producer_string) != 0)
22311     {
22312       struct indirect_string_node *node = find_AT_string (producer_string);
22313       producer->dw_attr_val.v.val_str = node;
22314     }
22315
22316   gen_scheduled_generic_parms_dies ();
22317   gen_remaining_tmpl_value_param_die_attribute ();
22318
22319   /* Add the name for the main input file now.  We delayed this from
22320      dwarf2out_init to avoid complications with PCH.  */
22321   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22322   if (!IS_ABSOLUTE_PATH (filename))
22323     add_comp_dir_attribute (comp_unit_die ());
22324   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22325     {
22326       bool p = false;
22327       htab_traverse (file_table, file_table_relative_p, &p);
22328       if (p)
22329         add_comp_dir_attribute (comp_unit_die ());
22330     }
22331
22332   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22333     {
22334       add_location_or_const_value_attribute (
22335         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22336         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22337         false,
22338         DW_AT_location);
22339     }
22340
22341   /* Traverse the limbo die list, and add parent/child links.  The only
22342      dies without parents that should be here are concrete instances of
22343      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22344      For concrete instances, we can get the parent die from the abstract
22345      instance.  */
22346   for (node = limbo_die_list; node; node = next_node)
22347     {
22348       dw_die_ref die = node->die;
22349       next_node = node->next;
22350
22351       if (die->die_parent == NULL)
22352         {
22353           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22354
22355           if (origin && origin->die_parent)
22356             add_child_die (origin->die_parent, die);
22357           else if (is_cu_die (die))
22358             ;
22359           else if (seen_error ())
22360             /* It's OK to be confused by errors in the input.  */
22361             add_child_die (comp_unit_die (), die);
22362           else
22363             {
22364               /* In certain situations, the lexical block containing a
22365                  nested function can be optimized away, which results
22366                  in the nested function die being orphaned.  Likewise
22367                  with the return type of that nested function.  Force
22368                  this to be a child of the containing function.
22369
22370                  It may happen that even the containing function got fully
22371                  inlined and optimized out.  In that case we are lost and
22372                  assign the empty child.  This should not be big issue as
22373                  the function is likely unreachable too.  */
22374               tree context = NULL_TREE;
22375
22376               gcc_assert (node->created_for);
22377
22378               if (DECL_P (node->created_for))
22379                 context = DECL_CONTEXT (node->created_for);
22380               else if (TYPE_P (node->created_for))
22381                 context = TYPE_CONTEXT (node->created_for);
22382
22383               gcc_assert (context
22384                           && (TREE_CODE (context) == FUNCTION_DECL
22385                               || TREE_CODE (context) == NAMESPACE_DECL));
22386
22387               origin = lookup_decl_die (context);
22388               if (origin)
22389                 add_child_die (origin, die);
22390               else
22391                 add_child_die (comp_unit_die (), die);
22392             }
22393         }
22394     }
22395
22396   limbo_die_list = NULL;
22397
22398 #if ENABLE_ASSERT_CHECKING
22399   {
22400     dw_die_ref die = comp_unit_die (), c;
22401     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22402   }
22403 #endif
22404   resolve_addr (comp_unit_die ());
22405   move_marked_base_types ();
22406
22407   for (node = deferred_asm_name; node; node = node->next)
22408     {
22409       tree decl = node->created_for;
22410       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22411         {
22412           add_linkage_attr (node->die, decl);
22413           move_linkage_attr (node->die);
22414         }
22415     }
22416
22417   deferred_asm_name = NULL;
22418
22419   /* Walk through the list of incomplete types again, trying once more to
22420      emit full debugging info for them.  */
22421   retry_incomplete_types ();
22422
22423   if (flag_eliminate_unused_debug_types)
22424     prune_unused_types ();
22425
22426   /* Generate separate CUs for each of the include files we've seen.
22427      They will go into limbo_die_list.  */
22428   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22429     break_out_includes (comp_unit_die ());
22430
22431   /* Generate separate COMDAT sections for type DIEs. */
22432   if (use_debug_types)
22433     {
22434       break_out_comdat_types (comp_unit_die ());
22435
22436       /* Each new type_unit DIE was added to the limbo die list when created.
22437          Since these have all been added to comdat_type_list, clear the
22438          limbo die list.  */
22439       limbo_die_list = NULL;
22440
22441       /* For each new comdat type unit, copy declarations for incomplete
22442          types to make the new unit self-contained (i.e., no direct
22443          references to the main compile unit).  */
22444       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22445         copy_decls_for_unworthy_types (ctnode->root_die);
22446       copy_decls_for_unworthy_types (comp_unit_die ());
22447
22448       /* In the process of copying declarations from one unit to another,
22449          we may have left some declarations behind that are no longer
22450          referenced.  Prune them.  */
22451       prune_unused_types ();
22452     }
22453
22454   /* Traverse the DIE's and add add sibling attributes to those DIE's
22455      that have children.  */
22456   add_sibling_attributes (comp_unit_die ());
22457   for (node = limbo_die_list; node; node = node->next)
22458     add_sibling_attributes (node->die);
22459   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22460     add_sibling_attributes (ctnode->root_die);
22461
22462   /* Output a terminator label for the .text section.  */
22463   switch_to_section (text_section);
22464   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22465   if (cold_text_section)
22466     {
22467       switch_to_section (cold_text_section);
22468       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22469     }
22470
22471   /* We can only use the low/high_pc attributes if all of the code was
22472      in .text.  */
22473   if (!have_multiple_function_sections 
22474       || (dwarf_version < 3 && dwarf_strict))
22475     {
22476       /* Don't add if the CU has no associated code.  */
22477       if (text_section_used)
22478         {
22479           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22480           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22481         }
22482     }
22483   else
22484     {
22485       unsigned fde_idx;
22486       dw_fde_ref fde;
22487       bool range_list_added = false;
22488
22489       if (text_section_used)
22490         add_ranges_by_labels (comp_unit_die (), text_section_label,
22491                               text_end_label, &range_list_added);
22492       if (cold_text_section_used)
22493         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22494                               cold_end_label, &range_list_added);
22495
22496       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22497         {
22498           if (!fde->in_std_section)
22499             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22500                                   fde->dw_fde_end, &range_list_added);
22501           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22502             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22503                                   fde->dw_fde_second_end, &range_list_added);
22504         }
22505
22506       if (range_list_added)
22507         {
22508           /* We need to give .debug_loc and .debug_ranges an appropriate
22509              "base address".  Use zero so that these addresses become
22510              absolute.  Historically, we've emitted the unexpected
22511              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22512              Emit both to give time for other tools to adapt.  */
22513           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22514           if (! dwarf_strict && dwarf_version < 4)
22515             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22516
22517           add_ranges (NULL);
22518         }
22519     }
22520
22521   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22522     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22523                     debug_line_section_label);
22524
22525   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22526     add_AT_macptr (comp_unit_die (),
22527                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22528                    macinfo_section_label);
22529
22530   if (have_location_lists)
22531     optimize_location_lists (comp_unit_die ());
22532
22533   /* Output all of the compilation units.  We put the main one last so that
22534      the offsets are available to output_pubnames.  */
22535   for (node = limbo_die_list; node; node = node->next)
22536     output_comp_unit (node->die, 0);
22537
22538   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22539   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22540     {
22541       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22542
22543       /* Don't output duplicate types.  */
22544       if (*slot != HTAB_EMPTY_ENTRY)
22545         continue;
22546
22547       /* Add a pointer to the line table for the main compilation unit
22548          so that the debugger can make sense of DW_AT_decl_file
22549          attributes.  */
22550       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22551         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22552                         debug_line_section_label);
22553
22554       output_comdat_type_unit (ctnode);
22555       *slot = ctnode;
22556     }
22557   htab_delete (comdat_type_table);
22558
22559   /* Output the main compilation unit if non-empty or if .debug_macinfo
22560      will be emitted.  */
22561   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22562
22563   /* Output the abbreviation table.  */
22564   if (abbrev_die_table_in_use != 1)
22565     {
22566       switch_to_section (debug_abbrev_section);
22567       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22568       output_abbrev_section ();
22569     }
22570
22571   /* Output location list section if necessary.  */
22572   if (have_location_lists)
22573     {
22574       /* Output the location lists info.  */
22575       switch_to_section (debug_loc_section);
22576       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22577                                    DEBUG_LOC_SECTION_LABEL, 0);
22578       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22579       output_location_lists (comp_unit_die ());
22580     }
22581
22582   /* Output public names table if necessary.  */
22583   if (!VEC_empty (pubname_entry, pubname_table))
22584     {
22585       gcc_assert (info_section_emitted);
22586       switch_to_section (debug_pubnames_section);
22587       output_pubnames (pubname_table);
22588     }
22589
22590   /* Output public types table if necessary.  */
22591   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22592      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22593      simply won't look for the section.  */
22594   if (!VEC_empty (pubname_entry, pubtype_table))
22595     {
22596       bool empty = false;
22597       
22598       if (flag_eliminate_unused_debug_types)
22599         {
22600           /* The pubtypes table might be emptied by pruning unused items.  */
22601           unsigned i;
22602           pubname_ref p;
22603           empty = true;
22604           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22605             if (p->die->die_offset != 0)
22606               {
22607                 empty = false;
22608                 break;
22609               }
22610         }
22611       if (!empty)
22612         {
22613           gcc_assert (info_section_emitted);
22614           switch_to_section (debug_pubtypes_section);
22615           output_pubnames (pubtype_table);
22616         }
22617     }
22618
22619   /* Output the address range information if a CU (.debug_info section)
22620      was emitted.  We output an empty table even if we had no functions
22621      to put in it.  This because the consumer has no way to tell the
22622      difference between an empty table that we omitted and failure to
22623      generate a table that would have contained data.  */
22624   if (info_section_emitted)
22625     {
22626       unsigned long aranges_length = size_of_aranges ();
22627
22628       switch_to_section (debug_aranges_section);
22629       output_aranges (aranges_length);
22630     }
22631
22632   /* Output ranges section if necessary.  */
22633   if (ranges_table_in_use)
22634     {
22635       switch_to_section (debug_ranges_section);
22636       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22637       output_ranges ();
22638     }
22639
22640   /* Output the source line correspondence table.  We must do this
22641      even if there is no line information.  Otherwise, on an empty
22642      translation unit, we will generate a present, but empty,
22643      .debug_info section.  IRIX 6.5 `nm' will then complain when
22644      examining the file.  This is done late so that any filenames
22645      used by the debug_info section are marked as 'used'.  */
22646   switch_to_section (debug_line_section);
22647   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22648   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22649     output_line_info ();
22650
22651   /* Have to end the macro section.  */
22652   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22653     {
22654       switch_to_section (debug_macinfo_section);
22655       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22656       if (!VEC_empty (macinfo_entry, macinfo_table))
22657         output_macinfo ();
22658       dw2_asm_output_data (1, 0, "End compilation unit");
22659     }
22660
22661   /* If we emitted any DW_FORM_strp form attribute, output the string
22662      table too.  */
22663   if (debug_str_hash)
22664     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22665 }
22666
22667 #include "gt-dwarf2out.h"