OSDN Git Service

* config/i386/i386.md (maxmin): New code iterator.
[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           {
7656             int csize = constant_size (AT_unsigned (a));
7657             if (dwarf_version == 3
7658                 && a->dw_attr == DW_AT_data_member_location
7659                 && csize >= 4)
7660               size += size_of_uleb128 (AT_unsigned (a));
7661             else
7662               size += csize;
7663           }
7664           break;
7665         case dw_val_class_const_double:
7666           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7667           if (HOST_BITS_PER_WIDE_INT >= 64)
7668             size++; /* block */
7669           break;
7670         case dw_val_class_vec:
7671           size += constant_size (a->dw_attr_val.v.val_vec.length
7672                                  * a->dw_attr_val.v.val_vec.elt_size)
7673                   + a->dw_attr_val.v.val_vec.length
7674                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7675           break;
7676         case dw_val_class_flag:
7677           if (dwarf_version >= 4)
7678             /* Currently all add_AT_flag calls pass in 1 as last argument,
7679                so DW_FORM_flag_present can be used.  If that ever changes,
7680                we'll need to use DW_FORM_flag and have some optimization
7681                in build_abbrev_table that will change those to
7682                DW_FORM_flag_present if it is set to 1 in all DIEs using
7683                the same abbrev entry.  */
7684             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7685           else
7686             size += 1;
7687           break;
7688         case dw_val_class_die_ref:
7689           if (AT_ref_external (a))
7690             {
7691               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7692                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7693                  is sized by target address length, whereas in DWARF3
7694                  it's always sized as an offset.  */
7695               if (use_debug_types)
7696                 size += DWARF_TYPE_SIGNATURE_SIZE;
7697               else if (dwarf_version == 2)
7698                 size += DWARF2_ADDR_SIZE;
7699               else
7700                 size += DWARF_OFFSET_SIZE;
7701             }
7702           else
7703             size += DWARF_OFFSET_SIZE;
7704           break;
7705         case dw_val_class_fde_ref:
7706           size += DWARF_OFFSET_SIZE;
7707           break;
7708         case dw_val_class_lbl_id:
7709           size += DWARF2_ADDR_SIZE;
7710           break;
7711         case dw_val_class_lineptr:
7712         case dw_val_class_macptr:
7713           size += DWARF_OFFSET_SIZE;
7714           break;
7715         case dw_val_class_str:
7716           if (AT_string_form (a) == DW_FORM_strp)
7717             size += DWARF_OFFSET_SIZE;
7718           else
7719             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7720           break;
7721         case dw_val_class_file:
7722           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7723           break;
7724         case dw_val_class_data8:
7725           size += 8;
7726           break;
7727         case dw_val_class_vms_delta:
7728           size += DWARF_OFFSET_SIZE;
7729           break;
7730         default:
7731           gcc_unreachable ();
7732         }
7733     }
7734
7735   return size;
7736 }
7737
7738 /* Size the debugging information associated with a given DIE.  Visits the
7739    DIE's children recursively.  Updates the global variable next_die_offset, on
7740    each time through.  Uses the current value of next_die_offset to update the
7741    die_offset field in each DIE.  */
7742
7743 static void
7744 calc_die_sizes (dw_die_ref die)
7745 {
7746   dw_die_ref c;
7747
7748   gcc_assert (die->die_offset == 0
7749               || (unsigned long int) die->die_offset == next_die_offset);
7750   die->die_offset = next_die_offset;
7751   next_die_offset += size_of_die (die);
7752
7753   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7754
7755   if (die->die_child != NULL)
7756     /* Count the null byte used to terminate sibling lists.  */
7757     next_die_offset += 1;
7758 }
7759
7760 /* Size just the base type children at the start of the CU.
7761    This is needed because build_abbrev needs to size locs
7762    and sizing of type based stack ops needs to know die_offset
7763    values for the base types.  */
7764
7765 static void
7766 calc_base_type_die_sizes (void)
7767 {
7768   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7769   unsigned int i;
7770   dw_die_ref base_type;
7771 #if ENABLE_ASSERT_CHECKING
7772   dw_die_ref prev = comp_unit_die ()->die_child;
7773 #endif
7774
7775   die_offset += size_of_die (comp_unit_die ());
7776   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7777     {
7778 #if ENABLE_ASSERT_CHECKING
7779       gcc_assert (base_type->die_offset == 0
7780                   && prev->die_sib == base_type
7781                   && base_type->die_child == NULL
7782                   && base_type->die_abbrev);
7783       prev = base_type;
7784 #endif
7785       base_type->die_offset = die_offset;
7786       die_offset += size_of_die (base_type);
7787     }
7788 }
7789
7790 /* Set the marks for a die and its children.  We do this so
7791    that we know whether or not a reference needs to use FORM_ref_addr; only
7792    DIEs in the same CU will be marked.  We used to clear out the offset
7793    and use that as the flag, but ran into ordering problems.  */
7794
7795 static void
7796 mark_dies (dw_die_ref die)
7797 {
7798   dw_die_ref c;
7799
7800   gcc_assert (!die->die_mark);
7801
7802   die->die_mark = 1;
7803   FOR_EACH_CHILD (die, c, mark_dies (c));
7804 }
7805
7806 /* Clear the marks for a die and its children.  */
7807
7808 static void
7809 unmark_dies (dw_die_ref die)
7810 {
7811   dw_die_ref c;
7812
7813   if (! use_debug_types)
7814     gcc_assert (die->die_mark);
7815
7816   die->die_mark = 0;
7817   FOR_EACH_CHILD (die, c, unmark_dies (c));
7818 }
7819
7820 /* Clear the marks for a die, its children and referred dies.  */
7821
7822 static void
7823 unmark_all_dies (dw_die_ref die)
7824 {
7825   dw_die_ref c;
7826   dw_attr_ref a;
7827   unsigned ix;
7828
7829   if (!die->die_mark)
7830     return;
7831   die->die_mark = 0;
7832
7833   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7834
7835   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7836     if (AT_class (a) == dw_val_class_die_ref)
7837       unmark_all_dies (AT_ref (a));
7838 }
7839
7840 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7841    generated for the compilation unit.  */
7842
7843 static unsigned long
7844 size_of_pubnames (VEC (pubname_entry, gc) * names)
7845 {
7846   unsigned long size;
7847   unsigned i;
7848   pubname_ref p;
7849
7850   size = DWARF_PUBNAMES_HEADER_SIZE;
7851   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7852     if (names != pubtype_table
7853         || p->die->die_offset != 0
7854         || !flag_eliminate_unused_debug_types)
7855       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7856
7857   size += DWARF_OFFSET_SIZE;
7858   return size;
7859 }
7860
7861 /* Return the size of the information in the .debug_aranges section.  */
7862
7863 static unsigned long
7864 size_of_aranges (void)
7865 {
7866   unsigned long size;
7867
7868   size = DWARF_ARANGES_HEADER_SIZE;
7869
7870   /* Count the address/length pair for this compilation unit.  */
7871   if (text_section_used)
7872     size += 2 * DWARF2_ADDR_SIZE;
7873   if (cold_text_section_used)
7874     size += 2 * DWARF2_ADDR_SIZE;
7875   if (have_multiple_function_sections)
7876     {
7877       unsigned fde_idx;
7878       dw_fde_ref fde;
7879
7880       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7881         {
7882           if (!fde->in_std_section)
7883             size += 2 * DWARF2_ADDR_SIZE;
7884           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7885             size += 2 * DWARF2_ADDR_SIZE;
7886         }
7887     }
7888
7889   /* Count the two zero words used to terminated the address range table.  */
7890   size += 2 * DWARF2_ADDR_SIZE;
7891   return size;
7892 }
7893 \f
7894 /* Select the encoding of an attribute value.  */
7895
7896 static enum dwarf_form
7897 value_format (dw_attr_ref a)
7898 {
7899   switch (a->dw_attr_val.val_class)
7900     {
7901     case dw_val_class_addr:
7902       /* Only very few attributes allow DW_FORM_addr.  */
7903       switch (a->dw_attr)
7904         {
7905         case DW_AT_low_pc:
7906         case DW_AT_high_pc:
7907         case DW_AT_entry_pc:
7908         case DW_AT_trampoline:
7909           return DW_FORM_addr;
7910         default:
7911           break;
7912         }
7913       switch (DWARF2_ADDR_SIZE)
7914         {
7915         case 1:
7916           return DW_FORM_data1;
7917         case 2:
7918           return DW_FORM_data2;
7919         case 4:
7920           return DW_FORM_data4;
7921         case 8:
7922           return DW_FORM_data8;
7923         default:
7924           gcc_unreachable ();
7925         }
7926     case dw_val_class_range_list:
7927     case dw_val_class_loc_list:
7928       if (dwarf_version >= 4)
7929         return DW_FORM_sec_offset;
7930       /* FALLTHRU */
7931     case dw_val_class_vms_delta:
7932     case dw_val_class_offset:
7933       switch (DWARF_OFFSET_SIZE)
7934         {
7935         case 4:
7936           return DW_FORM_data4;
7937         case 8:
7938           return DW_FORM_data8;
7939         default:
7940           gcc_unreachable ();
7941         }
7942     case dw_val_class_loc:
7943       if (dwarf_version >= 4)
7944         return DW_FORM_exprloc;
7945       switch (constant_size (size_of_locs (AT_loc (a))))
7946         {
7947         case 1:
7948           return DW_FORM_block1;
7949         case 2:
7950           return DW_FORM_block2;
7951         default:
7952           gcc_unreachable ();
7953         }
7954     case dw_val_class_const:
7955       return DW_FORM_sdata;
7956     case dw_val_class_unsigned_const:
7957       switch (constant_size (AT_unsigned (a)))
7958         {
7959         case 1:
7960           return DW_FORM_data1;
7961         case 2:
7962           return DW_FORM_data2;
7963         case 4:
7964           /* In DWARF3 DW_AT_data_member_location with
7965              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7966              constant, so we need to use DW_FORM_udata if we need
7967              a large constant.  */
7968           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7969             return DW_FORM_udata;
7970           return DW_FORM_data4;
7971         case 8:
7972           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7973             return DW_FORM_udata;
7974           return DW_FORM_data8;
7975         default:
7976           gcc_unreachable ();
7977         }
7978     case dw_val_class_const_double:
7979       switch (HOST_BITS_PER_WIDE_INT)
7980         {
7981         case 8:
7982           return DW_FORM_data2;
7983         case 16:
7984           return DW_FORM_data4;
7985         case 32:
7986           return DW_FORM_data8;
7987         case 64:
7988         default:
7989           return DW_FORM_block1;
7990         }
7991     case dw_val_class_vec:
7992       switch (constant_size (a->dw_attr_val.v.val_vec.length
7993                              * a->dw_attr_val.v.val_vec.elt_size))
7994         {
7995         case 1:
7996           return DW_FORM_block1;
7997         case 2:
7998           return DW_FORM_block2;
7999         case 4:
8000           return DW_FORM_block4;
8001         default:
8002           gcc_unreachable ();
8003         }
8004     case dw_val_class_flag:
8005       if (dwarf_version >= 4)
8006         {
8007           /* Currently all add_AT_flag calls pass in 1 as last argument,
8008              so DW_FORM_flag_present can be used.  If that ever changes,
8009              we'll need to use DW_FORM_flag and have some optimization
8010              in build_abbrev_table that will change those to
8011              DW_FORM_flag_present if it is set to 1 in all DIEs using
8012              the same abbrev entry.  */
8013           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8014           return DW_FORM_flag_present;
8015         }
8016       return DW_FORM_flag;
8017     case dw_val_class_die_ref:
8018       if (AT_ref_external (a))
8019         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8020       else
8021         return DW_FORM_ref;
8022     case dw_val_class_fde_ref:
8023       return DW_FORM_data;
8024     case dw_val_class_lbl_id:
8025       return DW_FORM_addr;
8026     case dw_val_class_lineptr:
8027     case dw_val_class_macptr:
8028       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8029     case dw_val_class_str:
8030       return AT_string_form (a);
8031     case dw_val_class_file:
8032       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8033         {
8034         case 1:
8035           return DW_FORM_data1;
8036         case 2:
8037           return DW_FORM_data2;
8038         case 4:
8039           return DW_FORM_data4;
8040         default:
8041           gcc_unreachable ();
8042         }
8043
8044     case dw_val_class_data8:
8045       return DW_FORM_data8;
8046
8047     default:
8048       gcc_unreachable ();
8049     }
8050 }
8051
8052 /* Output the encoding of an attribute value.  */
8053
8054 static void
8055 output_value_format (dw_attr_ref a)
8056 {
8057   enum dwarf_form form = value_format (a);
8058
8059   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8060 }
8061
8062 /* Output the .debug_abbrev section which defines the DIE abbreviation
8063    table.  */
8064
8065 static void
8066 output_abbrev_section (void)
8067 {
8068   unsigned long abbrev_id;
8069
8070   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8071     {
8072       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8073       unsigned ix;
8074       dw_attr_ref a_attr;
8075
8076       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8077       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8078                                    dwarf_tag_name (abbrev->die_tag));
8079
8080       if (abbrev->die_child != NULL)
8081         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8082       else
8083         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8084
8085       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8086            ix++)
8087         {
8088           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8089                                        dwarf_attr_name (a_attr->dw_attr));
8090           output_value_format (a_attr);
8091         }
8092
8093       dw2_asm_output_data (1, 0, NULL);
8094       dw2_asm_output_data (1, 0, NULL);
8095     }
8096
8097   /* Terminate the table.  */
8098   dw2_asm_output_data (1, 0, NULL);
8099 }
8100
8101 /* Output a symbol we can use to refer to this DIE from another CU.  */
8102
8103 static inline void
8104 output_die_symbol (dw_die_ref die)
8105 {
8106   char *sym = die->die_id.die_symbol;
8107
8108   if (sym == 0)
8109     return;
8110
8111   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8112     /* We make these global, not weak; if the target doesn't support
8113        .linkonce, it doesn't support combining the sections, so debugging
8114        will break.  */
8115     targetm.asm_out.globalize_label (asm_out_file, sym);
8116
8117   ASM_OUTPUT_LABEL (asm_out_file, sym);
8118 }
8119
8120 /* Return a new location list, given the begin and end range, and the
8121    expression.  */
8122
8123 static inline dw_loc_list_ref
8124 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8125               const char *section)
8126 {
8127   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8128
8129   retlist->begin = begin;
8130   retlist->end = end;
8131   retlist->expr = expr;
8132   retlist->section = section;
8133
8134   return retlist;
8135 }
8136
8137 /* Generate a new internal symbol for this location list node, if it
8138    hasn't got one yet.  */
8139
8140 static inline void
8141 gen_llsym (dw_loc_list_ref list)
8142 {
8143   gcc_assert (!list->ll_symbol);
8144   list->ll_symbol = gen_internal_sym ("LLST");
8145 }
8146
8147 /* Output the location list given to us.  */
8148
8149 static void
8150 output_loc_list (dw_loc_list_ref list_head)
8151 {
8152   dw_loc_list_ref curr = list_head;
8153
8154   if (list_head->emitted)
8155     return;
8156   list_head->emitted = true;
8157
8158   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8159
8160   /* Walk the location list, and output each range + expression.  */
8161   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8162     {
8163       unsigned long size;
8164       /* Don't output an entry that starts and ends at the same address.  */
8165       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8166         continue;
8167       if (!have_multiple_function_sections)
8168         {
8169           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8170                                 "Location list begin address (%s)",
8171                                 list_head->ll_symbol);
8172           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8173                                 "Location list end address (%s)",
8174                                 list_head->ll_symbol);
8175         }
8176       else
8177         {
8178           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8179                                "Location list begin address (%s)",
8180                                list_head->ll_symbol);
8181           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8182                                "Location list end address (%s)",
8183                                list_head->ll_symbol);
8184         }
8185       size = size_of_locs (curr->expr);
8186
8187       /* Output the block length for this list of location operations.  */
8188       gcc_assert (size <= 0xffff);
8189       dw2_asm_output_data (2, size, "%s", "Location expression size");
8190
8191       output_loc_sequence (curr->expr, -1);
8192     }
8193
8194   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8195                        "Location list terminator begin (%s)",
8196                        list_head->ll_symbol);
8197   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8198                        "Location list terminator end (%s)",
8199                        list_head->ll_symbol);
8200 }
8201
8202 /* Output a type signature.  */
8203
8204 static inline void
8205 output_signature (const char *sig, const char *name)
8206 {
8207   int i;
8208
8209   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8210     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8211 }
8212
8213 /* Output the DIE and its attributes.  Called recursively to generate
8214    the definitions of each child DIE.  */
8215
8216 static void
8217 output_die (dw_die_ref die)
8218 {
8219   dw_attr_ref a;
8220   dw_die_ref c;
8221   unsigned long size;
8222   unsigned ix;
8223
8224   /* If someone in another CU might refer to us, set up a symbol for
8225      them to point to.  */
8226   if (! use_debug_types && die->die_id.die_symbol)
8227     output_die_symbol (die);
8228
8229   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8230                                (unsigned long)die->die_offset,
8231                                dwarf_tag_name (die->die_tag));
8232
8233   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8234     {
8235       const char *name = dwarf_attr_name (a->dw_attr);
8236
8237       switch (AT_class (a))
8238         {
8239         case dw_val_class_addr:
8240           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8241           break;
8242
8243         case dw_val_class_offset:
8244           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8245                                "%s", name);
8246           break;
8247
8248         case dw_val_class_range_list:
8249           {
8250             char *p = strchr (ranges_section_label, '\0');
8251
8252             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8253                      a->dw_attr_val.v.val_offset);
8254             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8255                                    debug_ranges_section, "%s", name);
8256             *p = '\0';
8257           }
8258           break;
8259
8260         case dw_val_class_loc:
8261           size = size_of_locs (AT_loc (a));
8262
8263           /* Output the block length for this list of location operations.  */
8264           if (dwarf_version >= 4)
8265             dw2_asm_output_data_uleb128 (size, "%s", name);
8266           else
8267             dw2_asm_output_data (constant_size (size), size, "%s", name);
8268
8269           output_loc_sequence (AT_loc (a), -1);
8270           break;
8271
8272         case dw_val_class_const:
8273           /* ??? It would be slightly more efficient to use a scheme like is
8274              used for unsigned constants below, but gdb 4.x does not sign
8275              extend.  Gdb 5.x does sign extend.  */
8276           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8277           break;
8278
8279         case dw_val_class_unsigned_const:
8280           {
8281             int csize = constant_size (AT_unsigned (a));
8282             if (dwarf_version == 3
8283                 && a->dw_attr == DW_AT_data_member_location
8284                 && csize >= 4)
8285               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8286             else
8287               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8288           }
8289           break;
8290
8291         case dw_val_class_const_double:
8292           {
8293             unsigned HOST_WIDE_INT first, second;
8294
8295             if (HOST_BITS_PER_WIDE_INT >= 64)
8296               dw2_asm_output_data (1,
8297                                    2 * HOST_BITS_PER_WIDE_INT
8298                                    / HOST_BITS_PER_CHAR,
8299                                    NULL);
8300
8301             if (WORDS_BIG_ENDIAN)
8302               {
8303                 first = a->dw_attr_val.v.val_double.high;
8304                 second = a->dw_attr_val.v.val_double.low;
8305               }
8306             else
8307               {
8308                 first = a->dw_attr_val.v.val_double.low;
8309                 second = a->dw_attr_val.v.val_double.high;
8310               }
8311
8312             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8313                                  first, name);
8314             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8315                                  second, NULL);
8316           }
8317           break;
8318
8319         case dw_val_class_vec:
8320           {
8321             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8322             unsigned int len = a->dw_attr_val.v.val_vec.length;
8323             unsigned int i;
8324             unsigned char *p;
8325
8326             dw2_asm_output_data (constant_size (len * elt_size),
8327                                  len * elt_size, "%s", name);
8328             if (elt_size > sizeof (HOST_WIDE_INT))
8329               {
8330                 elt_size /= 2;
8331                 len *= 2;
8332               }
8333             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8334                  i < len;
8335                  i++, p += elt_size)
8336               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8337                                    "fp or vector constant word %u", i);
8338             break;
8339           }
8340
8341         case dw_val_class_flag:
8342           if (dwarf_version >= 4)
8343             {
8344               /* Currently all add_AT_flag calls pass in 1 as last argument,
8345                  so DW_FORM_flag_present can be used.  If that ever changes,
8346                  we'll need to use DW_FORM_flag and have some optimization
8347                  in build_abbrev_table that will change those to
8348                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8349                  the same abbrev entry.  */
8350               gcc_assert (AT_flag (a) == 1);
8351               if (flag_debug_asm)
8352                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8353                          ASM_COMMENT_START, name);
8354               break;
8355             }
8356           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8357           break;
8358
8359         case dw_val_class_loc_list:
8360           {
8361             char *sym = AT_loc_list (a)->ll_symbol;
8362
8363             gcc_assert (sym);
8364             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8365                                    "%s", name);
8366           }
8367           break;
8368
8369         case dw_val_class_die_ref:
8370           if (AT_ref_external (a))
8371             {
8372               if (use_debug_types)
8373                 {
8374                   comdat_type_node_ref type_node =
8375                     AT_ref (a)->die_id.die_type_node;
8376
8377                   gcc_assert (type_node);
8378                   output_signature (type_node->signature, name);
8379                 }
8380               else
8381                 {
8382                   char *sym = AT_ref (a)->die_id.die_symbol;
8383                   int size;
8384
8385                   gcc_assert (sym);
8386                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8387                      length, whereas in DWARF3 it's always sized as an
8388                      offset.  */
8389                   if (dwarf_version == 2)
8390                     size = DWARF2_ADDR_SIZE;
8391                   else
8392                     size = DWARF_OFFSET_SIZE;
8393                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8394                                          name);
8395                 }
8396             }
8397           else
8398             {
8399               gcc_assert (AT_ref (a)->die_offset);
8400               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8401                                    "%s", name);
8402             }
8403           break;
8404
8405         case dw_val_class_fde_ref:
8406           {
8407             char l1[20];
8408
8409             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8410                                          a->dw_attr_val.v.val_fde_index * 2);
8411             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8412                                    "%s", name);
8413           }
8414           break;
8415
8416         case dw_val_class_vms_delta:
8417           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8418                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8419                                     "%s", name);
8420           break;
8421
8422         case dw_val_class_lbl_id:
8423           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8424           break;
8425
8426         case dw_val_class_lineptr:
8427           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8428                                  debug_line_section, "%s", name);
8429           break;
8430
8431         case dw_val_class_macptr:
8432           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8433                                  debug_macinfo_section, "%s", name);
8434           break;
8435
8436         case dw_val_class_str:
8437           if (AT_string_form (a) == DW_FORM_strp)
8438             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8439                                    a->dw_attr_val.v.val_str->label,
8440                                    debug_str_section,
8441                                    "%s: \"%s\"", name, AT_string (a));
8442           else
8443             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8444           break;
8445
8446         case dw_val_class_file:
8447           {
8448             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8449
8450             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8451                                  a->dw_attr_val.v.val_file->filename);
8452             break;
8453           }
8454
8455         case dw_val_class_data8:
8456           {
8457             int i;
8458
8459             for (i = 0; i < 8; i++)
8460               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8461                                    i == 0 ? "%s" : NULL, name);
8462             break;
8463           }
8464
8465         default:
8466           gcc_unreachable ();
8467         }
8468     }
8469
8470   FOR_EACH_CHILD (die, c, output_die (c));
8471
8472   /* Add null byte to terminate sibling list.  */
8473   if (die->die_child != NULL)
8474     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8475                          (unsigned long) die->die_offset);
8476 }
8477
8478 /* Output the compilation unit that appears at the beginning of the
8479    .debug_info section, and precedes the DIE descriptions.  */
8480
8481 static void
8482 output_compilation_unit_header (void)
8483 {
8484   int ver = dwarf_version;
8485
8486   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8487     dw2_asm_output_data (4, 0xffffffff,
8488       "Initial length escape value indicating 64-bit DWARF extension");
8489   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8490                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8491                        "Length of Compilation Unit Info");
8492   dw2_asm_output_data (2, ver, "DWARF version number");
8493   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8494                          debug_abbrev_section,
8495                          "Offset Into Abbrev. Section");
8496   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8497 }
8498
8499 /* Output the compilation unit DIE and its children.  */
8500
8501 static void
8502 output_comp_unit (dw_die_ref die, int output_if_empty)
8503 {
8504   const char *secname;
8505   char *oldsym, *tmp;
8506
8507   /* Unless we are outputting main CU, we may throw away empty ones.  */
8508   if (!output_if_empty && die->die_child == NULL)
8509     return;
8510
8511   /* Even if there are no children of this DIE, we must output the information
8512      about the compilation unit.  Otherwise, on an empty translation unit, we
8513      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8514      will then complain when examining the file.  First mark all the DIEs in
8515      this CU so we know which get local refs.  */
8516   mark_dies (die);
8517
8518   build_abbrev_table (die);
8519
8520   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8521   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8522   calc_die_sizes (die);
8523
8524   oldsym = die->die_id.die_symbol;
8525   if (oldsym)
8526     {
8527       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8528
8529       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8530       secname = tmp;
8531       die->die_id.die_symbol = NULL;
8532       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8533     }
8534   else
8535     {
8536       switch_to_section (debug_info_section);
8537       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8538       info_section_emitted = true;
8539     }
8540
8541   /* Output debugging information.  */
8542   output_compilation_unit_header ();
8543   output_die (die);
8544
8545   /* Leave the marks on the main CU, so we can check them in
8546      output_pubnames.  */
8547   if (oldsym)
8548     {
8549       unmark_dies (die);
8550       die->die_id.die_symbol = oldsym;
8551     }
8552 }
8553
8554 /* Output a comdat type unit DIE and its children.  */
8555
8556 static void
8557 output_comdat_type_unit (comdat_type_node *node)
8558 {
8559   const char *secname;
8560   char *tmp;
8561   int i;
8562 #if defined (OBJECT_FORMAT_ELF)
8563   tree comdat_key;
8564 #endif
8565
8566   /* First mark all the DIEs in this CU so we know which get local refs.  */
8567   mark_dies (node->root_die);
8568
8569   build_abbrev_table (node->root_die);
8570
8571   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8572   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8573   calc_die_sizes (node->root_die);
8574
8575 #if defined (OBJECT_FORMAT_ELF)
8576   secname = ".debug_types";
8577   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8578   sprintf (tmp, "wt.");
8579   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8580     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8581   comdat_key = get_identifier (tmp);
8582   targetm.asm_out.named_section (secname,
8583                                  SECTION_DEBUG | SECTION_LINKONCE,
8584                                  comdat_key);
8585 #else
8586   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8587   sprintf (tmp, ".gnu.linkonce.wt.");
8588   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8589     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8590   secname = tmp;
8591   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8592 #endif
8593
8594   /* Output debugging information.  */
8595   output_compilation_unit_header ();
8596   output_signature (node->signature, "Type Signature");
8597   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8598                        "Offset to Type DIE");
8599   output_die (node->root_die);
8600
8601   unmark_dies (node->root_die);
8602 }
8603
8604 /* Return the DWARF2/3 pubname associated with a decl.  */
8605
8606 static const char *
8607 dwarf2_name (tree decl, int scope)
8608 {
8609   if (DECL_NAMELESS (decl))
8610     return NULL;
8611   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8612 }
8613
8614 /* Add a new entry to .debug_pubnames if appropriate.  */
8615
8616 static void
8617 add_pubname_string (const char *str, dw_die_ref die)
8618 {
8619   if (targetm.want_debug_pub_sections)
8620     {
8621       pubname_entry e;
8622
8623       e.die = die;
8624       e.name = xstrdup (str);
8625       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8626     }
8627 }
8628
8629 static void
8630 add_pubname (tree decl, dw_die_ref die)
8631 {
8632   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8633     {
8634       const char *name = dwarf2_name (decl, 1);
8635       if (name)
8636         add_pubname_string (name, die);
8637     }
8638 }
8639
8640 /* Add a new entry to .debug_pubtypes if appropriate.  */
8641
8642 static void
8643 add_pubtype (tree decl, dw_die_ref die)
8644 {
8645   pubname_entry e;
8646
8647   if (!targetm.want_debug_pub_sections)
8648     return;
8649
8650   e.name = NULL;
8651   if ((TREE_PUBLIC (decl)
8652        || is_cu_die (die->die_parent))
8653       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8654     {
8655       e.die = die;
8656       if (TYPE_P (decl))
8657         {
8658           if (TYPE_NAME (decl))
8659             {
8660               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8661                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8662               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8663                        && DECL_NAME (TYPE_NAME (decl)))
8664                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8665               else
8666                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8667             }
8668         }
8669       else
8670         {
8671           e.name = dwarf2_name (decl, 1);
8672           if (e.name)
8673             e.name = xstrdup (e.name);
8674         }
8675
8676       /* If we don't have a name for the type, there's no point in adding
8677          it to the table.  */
8678       if (e.name && e.name[0] != '\0')
8679         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8680     }
8681 }
8682
8683 /* Output the public names table used to speed up access to externally
8684    visible names; or the public types table used to find type definitions.  */
8685
8686 static void
8687 output_pubnames (VEC (pubname_entry, gc) * names)
8688 {
8689   unsigned i;
8690   unsigned long pubnames_length = size_of_pubnames (names);
8691   pubname_ref pub;
8692
8693   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8694     dw2_asm_output_data (4, 0xffffffff,
8695       "Initial length escape value indicating 64-bit DWARF extension");
8696   if (names == pubname_table)
8697     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8698                          "Length of Public Names Info");
8699   else
8700     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8701                          "Length of Public Type Names Info");
8702   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8703   dw2_asm_output_data (2, 2, "DWARF Version");
8704   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8705                          debug_info_section,
8706                          "Offset of Compilation Unit Info");
8707   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8708                        "Compilation Unit Length");
8709
8710   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8711     {
8712       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8713       if (names == pubname_table)
8714         gcc_assert (pub->die->die_mark);
8715
8716       if (names != pubtype_table
8717           || pub->die->die_offset != 0
8718           || !flag_eliminate_unused_debug_types)
8719         {
8720           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8721                                "DIE offset");
8722
8723           dw2_asm_output_nstring (pub->name, -1, "external name");
8724         }
8725     }
8726
8727   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8728 }
8729
8730 /* Output the information that goes into the .debug_aranges table.
8731    Namely, define the beginning and ending address range of the
8732    text section generated for this compilation unit.  */
8733
8734 static void
8735 output_aranges (unsigned long aranges_length)
8736 {
8737   unsigned i;
8738
8739   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8740     dw2_asm_output_data (4, 0xffffffff,
8741       "Initial length escape value indicating 64-bit DWARF extension");
8742   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8743                        "Length of Address Ranges Info");
8744   /* Version number for aranges is still 2, even in DWARF3.  */
8745   dw2_asm_output_data (2, 2, "DWARF Version");
8746   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8747                          debug_info_section,
8748                          "Offset of Compilation Unit Info");
8749   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8750   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8751
8752   /* We need to align to twice the pointer size here.  */
8753   if (DWARF_ARANGES_PAD_SIZE)
8754     {
8755       /* Pad using a 2 byte words so that padding is correct for any
8756          pointer size.  */
8757       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8758                            2 * DWARF2_ADDR_SIZE);
8759       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8760         dw2_asm_output_data (2, 0, NULL);
8761     }
8762
8763   /* It is necessary not to output these entries if the sections were
8764      not used; if the sections were not used, the length will be 0 and
8765      the address may end up as 0 if the section is discarded by ld
8766      --gc-sections, leaving an invalid (0, 0) entry that can be
8767      confused with the terminator.  */
8768   if (text_section_used)
8769     {
8770       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8771       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8772                             text_section_label, "Length");
8773     }
8774   if (cold_text_section_used)
8775     {
8776       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8777                            "Address");
8778       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8779                             cold_text_section_label, "Length");
8780     }
8781
8782   if (have_multiple_function_sections)
8783     {
8784       unsigned fde_idx;
8785       dw_fde_ref fde;
8786
8787       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8788         {
8789           if (!fde->in_std_section)
8790             {
8791               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8792                                    "Address");
8793               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8794                                     fde->dw_fde_begin, "Length");
8795             }
8796           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8797             {
8798               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8799                                    "Address");
8800               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8801                                     fde->dw_fde_second_begin, "Length");
8802             }
8803         }
8804     }
8805
8806   /* Output the terminator words.  */
8807   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8808   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8809 }
8810
8811 /* Add a new entry to .debug_ranges.  Return the offset at which it
8812    was placed.  */
8813
8814 static unsigned int
8815 add_ranges_num (int num)
8816 {
8817   unsigned int in_use = ranges_table_in_use;
8818
8819   if (in_use == ranges_table_allocated)
8820     {
8821       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8822       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8823                                     ranges_table_allocated);
8824       memset (ranges_table + ranges_table_in_use, 0,
8825               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8826     }
8827
8828   ranges_table[in_use].num = num;
8829   ranges_table_in_use = in_use + 1;
8830
8831   return in_use * 2 * DWARF2_ADDR_SIZE;
8832 }
8833
8834 /* Add a new entry to .debug_ranges corresponding to a block, or a
8835    range terminator if BLOCK is NULL.  */
8836
8837 static unsigned int
8838 add_ranges (const_tree block)
8839 {
8840   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8841 }
8842
8843 /* Add a new entry to .debug_ranges corresponding to a pair of
8844    labels.  */
8845
8846 static void
8847 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8848                       bool *added)
8849 {
8850   unsigned int in_use = ranges_by_label_in_use;
8851   unsigned int offset;
8852
8853   if (in_use == ranges_by_label_allocated)
8854     {
8855       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8856       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8857                                        ranges_by_label,
8858                                        ranges_by_label_allocated);
8859       memset (ranges_by_label + ranges_by_label_in_use, 0,
8860               RANGES_TABLE_INCREMENT
8861               * sizeof (struct dw_ranges_by_label_struct));
8862     }
8863
8864   ranges_by_label[in_use].begin = begin;
8865   ranges_by_label[in_use].end = end;
8866   ranges_by_label_in_use = in_use + 1;
8867
8868   offset = add_ranges_num (-(int)in_use - 1);
8869   if (!*added)
8870     {
8871       add_AT_range_list (die, DW_AT_ranges, offset);
8872       *added = true;
8873     }
8874 }
8875
8876 static void
8877 output_ranges (void)
8878 {
8879   unsigned i;
8880   static const char *const start_fmt = "Offset %#x";
8881   const char *fmt = start_fmt;
8882
8883   for (i = 0; i < ranges_table_in_use; i++)
8884     {
8885       int block_num = ranges_table[i].num;
8886
8887       if (block_num > 0)
8888         {
8889           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8890           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8891
8892           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8893           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8894
8895           /* If all code is in the text section, then the compilation
8896              unit base address defaults to DW_AT_low_pc, which is the
8897              base of the text section.  */
8898           if (!have_multiple_function_sections)
8899             {
8900               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8901                                     text_section_label,
8902                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8903               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8904                                     text_section_label, NULL);
8905             }
8906
8907           /* Otherwise, the compilation unit base address is zero,
8908              which allows us to use absolute addresses, and not worry
8909              about whether the target supports cross-section
8910              arithmetic.  */
8911           else
8912             {
8913               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8914                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8915               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8916             }
8917
8918           fmt = NULL;
8919         }
8920
8921       /* Negative block_num stands for an index into ranges_by_label.  */
8922       else if (block_num < 0)
8923         {
8924           int lab_idx = - block_num - 1;
8925
8926           if (!have_multiple_function_sections)
8927             {
8928               gcc_unreachable ();
8929 #if 0
8930               /* If we ever use add_ranges_by_labels () for a single
8931                  function section, all we have to do is to take out
8932                  the #if 0 above.  */
8933               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8934                                     ranges_by_label[lab_idx].begin,
8935                                     text_section_label,
8936                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8937               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8938                                     ranges_by_label[lab_idx].end,
8939                                     text_section_label, NULL);
8940 #endif
8941             }
8942           else
8943             {
8944               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8945                                    ranges_by_label[lab_idx].begin,
8946                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8947               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8948                                    ranges_by_label[lab_idx].end,
8949                                    NULL);
8950             }
8951         }
8952       else
8953         {
8954           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8955           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8956           fmt = start_fmt;
8957         }
8958     }
8959 }
8960
8961 /* Data structure containing information about input files.  */
8962 struct file_info
8963 {
8964   const char *path;     /* Complete file name.  */
8965   const char *fname;    /* File name part.  */
8966   int length;           /* Length of entire string.  */
8967   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8968   int dir_idx;          /* Index in directory table.  */
8969 };
8970
8971 /* Data structure containing information about directories with source
8972    files.  */
8973 struct dir_info
8974 {
8975   const char *path;     /* Path including directory name.  */
8976   int length;           /* Path length.  */
8977   int prefix;           /* Index of directory entry which is a prefix.  */
8978   int count;            /* Number of files in this directory.  */
8979   int dir_idx;          /* Index of directory used as base.  */
8980 };
8981
8982 /* Callback function for file_info comparison.  We sort by looking at
8983    the directories in the path.  */
8984
8985 static int
8986 file_info_cmp (const void *p1, const void *p2)
8987 {
8988   const struct file_info *const s1 = (const struct file_info *) p1;
8989   const struct file_info *const s2 = (const struct file_info *) p2;
8990   const unsigned char *cp1;
8991   const unsigned char *cp2;
8992
8993   /* Take care of file names without directories.  We need to make sure that
8994      we return consistent values to qsort since some will get confused if
8995      we return the same value when identical operands are passed in opposite
8996      orders.  So if neither has a directory, return 0 and otherwise return
8997      1 or -1 depending on which one has the directory.  */
8998   if ((s1->path == s1->fname || s2->path == s2->fname))
8999     return (s2->path == s2->fname) - (s1->path == s1->fname);
9000
9001   cp1 = (const unsigned char *) s1->path;
9002   cp2 = (const unsigned char *) s2->path;
9003
9004   while (1)
9005     {
9006       ++cp1;
9007       ++cp2;
9008       /* Reached the end of the first path?  If so, handle like above.  */
9009       if ((cp1 == (const unsigned char *) s1->fname)
9010           || (cp2 == (const unsigned char *) s2->fname))
9011         return ((cp2 == (const unsigned char *) s2->fname)
9012                 - (cp1 == (const unsigned char *) s1->fname));
9013
9014       /* Character of current path component the same?  */
9015       else if (*cp1 != *cp2)
9016         return *cp1 - *cp2;
9017     }
9018 }
9019
9020 struct file_name_acquire_data
9021 {
9022   struct file_info *files;
9023   int used_files;
9024   int max_files;
9025 };
9026
9027 /* Traversal function for the hash table.  */
9028
9029 static int
9030 file_name_acquire (void ** slot, void *data)
9031 {
9032   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9033   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9034   struct file_info *fi;
9035   const char *f;
9036
9037   gcc_assert (fnad->max_files >= d->emitted_number);
9038
9039   if (! d->emitted_number)
9040     return 1;
9041
9042   gcc_assert (fnad->max_files != fnad->used_files);
9043
9044   fi = fnad->files + fnad->used_files++;
9045
9046   /* Skip all leading "./".  */
9047   f = d->filename;
9048   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9049     f += 2;
9050
9051   /* Create a new array entry.  */
9052   fi->path = f;
9053   fi->length = strlen (f);
9054   fi->file_idx = d;
9055
9056   /* Search for the file name part.  */
9057   f = strrchr (f, DIR_SEPARATOR);
9058 #if defined (DIR_SEPARATOR_2)
9059   {
9060     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9061
9062     if (g != NULL)
9063       {
9064         if (f == NULL || f < g)
9065           f = g;
9066       }
9067   }
9068 #endif
9069
9070   fi->fname = f == NULL ? fi->path : f + 1;
9071   return 1;
9072 }
9073
9074 /* Output the directory table and the file name table.  We try to minimize
9075    the total amount of memory needed.  A heuristic is used to avoid large
9076    slowdowns with many input files.  */
9077
9078 static void
9079 output_file_names (void)
9080 {
9081   struct file_name_acquire_data fnad;
9082   int numfiles;
9083   struct file_info *files;
9084   struct dir_info *dirs;
9085   int *saved;
9086   int *savehere;
9087   int *backmap;
9088   int ndirs;
9089   int idx_offset;
9090   int i;
9091
9092   if (!last_emitted_file)
9093     {
9094       dw2_asm_output_data (1, 0, "End directory table");
9095       dw2_asm_output_data (1, 0, "End file name table");
9096       return;
9097     }
9098
9099   numfiles = last_emitted_file->emitted_number;
9100
9101   /* Allocate the various arrays we need.  */
9102   files = XALLOCAVEC (struct file_info, numfiles);
9103   dirs = XALLOCAVEC (struct dir_info, numfiles);
9104
9105   fnad.files = files;
9106   fnad.used_files = 0;
9107   fnad.max_files = numfiles;
9108   htab_traverse (file_table, file_name_acquire, &fnad);
9109   gcc_assert (fnad.used_files == fnad.max_files);
9110
9111   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9112
9113   /* Find all the different directories used.  */
9114   dirs[0].path = files[0].path;
9115   dirs[0].length = files[0].fname - files[0].path;
9116   dirs[0].prefix = -1;
9117   dirs[0].count = 1;
9118   dirs[0].dir_idx = 0;
9119   files[0].dir_idx = 0;
9120   ndirs = 1;
9121
9122   for (i = 1; i < numfiles; i++)
9123     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9124         && memcmp (dirs[ndirs - 1].path, files[i].path,
9125                    dirs[ndirs - 1].length) == 0)
9126       {
9127         /* Same directory as last entry.  */
9128         files[i].dir_idx = ndirs - 1;
9129         ++dirs[ndirs - 1].count;
9130       }
9131     else
9132       {
9133         int j;
9134
9135         /* This is a new directory.  */
9136         dirs[ndirs].path = files[i].path;
9137         dirs[ndirs].length = files[i].fname - files[i].path;
9138         dirs[ndirs].count = 1;
9139         dirs[ndirs].dir_idx = ndirs;
9140         files[i].dir_idx = ndirs;
9141
9142         /* Search for a prefix.  */
9143         dirs[ndirs].prefix = -1;
9144         for (j = 0; j < ndirs; j++)
9145           if (dirs[j].length < dirs[ndirs].length
9146               && dirs[j].length > 1
9147               && (dirs[ndirs].prefix == -1
9148                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9149               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9150             dirs[ndirs].prefix = j;
9151
9152         ++ndirs;
9153       }
9154
9155   /* Now to the actual work.  We have to find a subset of the directories which
9156      allow expressing the file name using references to the directory table
9157      with the least amount of characters.  We do not do an exhaustive search
9158      where we would have to check out every combination of every single
9159      possible prefix.  Instead we use a heuristic which provides nearly optimal
9160      results in most cases and never is much off.  */
9161   saved = XALLOCAVEC (int, ndirs);
9162   savehere = XALLOCAVEC (int, ndirs);
9163
9164   memset (saved, '\0', ndirs * sizeof (saved[0]));
9165   for (i = 0; i < ndirs; i++)
9166     {
9167       int j;
9168       int total;
9169
9170       /* We can always save some space for the current directory.  But this
9171          does not mean it will be enough to justify adding the directory.  */
9172       savehere[i] = dirs[i].length;
9173       total = (savehere[i] - saved[i]) * dirs[i].count;
9174
9175       for (j = i + 1; j < ndirs; j++)
9176         {
9177           savehere[j] = 0;
9178           if (saved[j] < dirs[i].length)
9179             {
9180               /* Determine whether the dirs[i] path is a prefix of the
9181                  dirs[j] path.  */
9182               int k;
9183
9184               k = dirs[j].prefix;
9185               while (k != -1 && k != (int) i)
9186                 k = dirs[k].prefix;
9187
9188               if (k == (int) i)
9189                 {
9190                   /* Yes it is.  We can possibly save some memory by
9191                      writing the filenames in dirs[j] relative to
9192                      dirs[i].  */
9193                   savehere[j] = dirs[i].length;
9194                   total += (savehere[j] - saved[j]) * dirs[j].count;
9195                 }
9196             }
9197         }
9198
9199       /* Check whether we can save enough to justify adding the dirs[i]
9200          directory.  */
9201       if (total > dirs[i].length + 1)
9202         {
9203           /* It's worthwhile adding.  */
9204           for (j = i; j < ndirs; j++)
9205             if (savehere[j] > 0)
9206               {
9207                 /* Remember how much we saved for this directory so far.  */
9208                 saved[j] = savehere[j];
9209
9210                 /* Remember the prefix directory.  */
9211                 dirs[j].dir_idx = i;
9212               }
9213         }
9214     }
9215
9216   /* Emit the directory name table.  */
9217   idx_offset = dirs[0].length > 0 ? 1 : 0;
9218   for (i = 1 - idx_offset; i < ndirs; i++)
9219     dw2_asm_output_nstring (dirs[i].path,
9220                             dirs[i].length
9221                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9222                             "Directory Entry: %#x", i + idx_offset);
9223
9224   dw2_asm_output_data (1, 0, "End directory table");
9225
9226   /* We have to emit them in the order of emitted_number since that's
9227      used in the debug info generation.  To do this efficiently we
9228      generate a back-mapping of the indices first.  */
9229   backmap = XALLOCAVEC (int, numfiles);
9230   for (i = 0; i < numfiles; i++)
9231     backmap[files[i].file_idx->emitted_number - 1] = i;
9232
9233   /* Now write all the file names.  */
9234   for (i = 0; i < numfiles; i++)
9235     {
9236       int file_idx = backmap[i];
9237       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9238
9239 #ifdef VMS_DEBUGGING_INFO
9240 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9241
9242       /* Setting these fields can lead to debugger miscomparisons,
9243          but VMS Debug requires them to be set correctly.  */
9244
9245       int ver;
9246       long long cdt;
9247       long siz;
9248       int maxfilelen = strlen (files[file_idx].path)
9249                                + dirs[dir_idx].length
9250                                + MAX_VMS_VERSION_LEN + 1;
9251       char *filebuf = XALLOCAVEC (char, maxfilelen);
9252
9253       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9254       snprintf (filebuf, maxfilelen, "%s;%d",
9255                 files[file_idx].path + dirs[dir_idx].length, ver);
9256
9257       dw2_asm_output_nstring
9258         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9259
9260       /* Include directory index.  */
9261       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9262
9263       /* Modification time.  */
9264       dw2_asm_output_data_uleb128
9265         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9266           ? cdt : 0,
9267          NULL);
9268
9269       /* File length in bytes.  */
9270       dw2_asm_output_data_uleb128
9271         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9272           ? siz : 0,
9273          NULL);
9274 #else
9275       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9276                               "File Entry: %#x", (unsigned) i + 1);
9277
9278       /* Include directory index.  */
9279       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9280
9281       /* Modification time.  */
9282       dw2_asm_output_data_uleb128 (0, NULL);
9283
9284       /* File length in bytes.  */
9285       dw2_asm_output_data_uleb128 (0, NULL);
9286 #endif /* VMS_DEBUGGING_INFO */
9287     }
9288
9289   dw2_asm_output_data (1, 0, "End file name table");
9290 }
9291
9292
9293 /* Output one line number table into the .debug_line section.  */
9294
9295 static void
9296 output_one_line_info_table (dw_line_info_table *table)
9297 {
9298   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9299   unsigned int current_line = 1;
9300   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9301   dw_line_info_entry *ent;
9302   size_t i;
9303
9304   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9305     {
9306       switch (ent->opcode)
9307         {
9308         case LI_set_address:
9309           /* ??? Unfortunately, we have little choice here currently, and
9310              must always use the most general form.  GCC does not know the
9311              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9312              ports do have length attributes which will give an upper bound
9313              on the address range.  We could perhaps use length attributes
9314              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9315           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9316
9317           /* This can handle any delta.  This takes
9318              4+DWARF2_ADDR_SIZE bytes.  */
9319           dw2_asm_output_data (1, 0, "set address %s", line_label);
9320           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9321           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9322           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9323           break;
9324
9325         case LI_set_line:
9326           if (ent->val == current_line)
9327             {
9328               /* We still need to start a new row, so output a copy insn.  */
9329               dw2_asm_output_data (1, DW_LNS_copy,
9330                                    "copy line %u", current_line);
9331             }
9332           else
9333             {
9334               int line_offset = ent->val - current_line;
9335               int line_delta = line_offset - DWARF_LINE_BASE;
9336
9337               current_line = ent->val;
9338               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9339                 {
9340                   /* This can handle deltas from -10 to 234, using the current
9341                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9342                      This takes 1 byte.  */
9343                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9344                                        "line %u", current_line);
9345                 }
9346               else
9347                 {
9348                   /* This can handle any delta.  This takes at least 4 bytes,
9349                      depending on the value being encoded.  */
9350                   dw2_asm_output_data (1, DW_LNS_advance_line,
9351                                        "advance to line %u", current_line);
9352                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9353                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9354                 }
9355             }
9356           break;
9357
9358         case LI_set_file:
9359           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9360           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9361           break;
9362
9363         case LI_set_column:
9364           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9365           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9366           break;
9367
9368         case LI_negate_stmt:
9369           current_is_stmt = !current_is_stmt;
9370           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9371                                "is_stmt %d", current_is_stmt);
9372           break;
9373
9374         case LI_set_prologue_end:
9375           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9376                                "set prologue end");
9377           break;
9378           
9379         case LI_set_epilogue_begin:
9380           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9381                                "set epilogue begin");
9382           break;
9383
9384         case LI_set_discriminator:
9385           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9386           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9387           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9388           dw2_asm_output_data_uleb128 (ent->val, NULL);
9389           break;
9390         }
9391     }
9392
9393   /* Emit debug info for the address of the end of the table.  */
9394   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9395   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9396   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9397   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9398
9399   dw2_asm_output_data (1, 0, "end sequence");
9400   dw2_asm_output_data_uleb128 (1, NULL);
9401   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9402 }
9403
9404 /* Output the source line number correspondence information.  This
9405    information goes into the .debug_line section.  */
9406
9407 static void
9408 output_line_info (void)
9409 {
9410   char l1[20], l2[20], p1[20], p2[20];
9411   int ver = dwarf_version;
9412   bool saw_one = false;
9413   int opc;
9414
9415   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9416   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9417   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9418   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9419
9420   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9421     dw2_asm_output_data (4, 0xffffffff,
9422       "Initial length escape value indicating 64-bit DWARF extension");
9423   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9424                         "Length of Source Line Info");
9425   ASM_OUTPUT_LABEL (asm_out_file, l1);
9426
9427   dw2_asm_output_data (2, ver, "DWARF Version");
9428   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9429   ASM_OUTPUT_LABEL (asm_out_file, p1);
9430
9431   /* Define the architecture-dependent minimum instruction length (in bytes).
9432      In this implementation of DWARF, this field is used for information
9433      purposes only.  Since GCC generates assembly language, we have no
9434      a priori knowledge of how many instruction bytes are generated for each
9435      source line, and therefore can use only the DW_LNE_set_address and
9436      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9437      this as '1', which is "correct enough" for all architectures,
9438      and don't let the target override.  */
9439   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9440
9441   if (ver >= 4)
9442     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9443                          "Maximum Operations Per Instruction");
9444   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9445                        "Default is_stmt_start flag");
9446   dw2_asm_output_data (1, DWARF_LINE_BASE,
9447                        "Line Base Value (Special Opcodes)");
9448   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9449                        "Line Range Value (Special Opcodes)");
9450   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9451                        "Special Opcode Base");
9452
9453   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9454     {
9455       int n_op_args;
9456       switch (opc)
9457         {
9458         case DW_LNS_advance_pc:
9459         case DW_LNS_advance_line:
9460         case DW_LNS_set_file:
9461         case DW_LNS_set_column:
9462         case DW_LNS_fixed_advance_pc:
9463         case DW_LNS_set_isa:
9464           n_op_args = 1;
9465           break;
9466         default:
9467           n_op_args = 0;
9468           break;
9469         }
9470
9471       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9472                            opc, n_op_args);
9473     }
9474
9475   /* Write out the information about the files we use.  */
9476   output_file_names ();
9477   ASM_OUTPUT_LABEL (asm_out_file, p2);
9478
9479   if (separate_line_info)
9480     {
9481       dw_line_info_table *table;
9482       size_t i;
9483
9484       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9485         if (table->in_use)
9486           {
9487             output_one_line_info_table (table);
9488             saw_one = true;
9489           }
9490     }
9491   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9492     {
9493       output_one_line_info_table (cold_text_section_line_info);
9494       saw_one = true;
9495     }
9496
9497   /* ??? Some Darwin linkers crash on a .debug_line section with no
9498      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9499      sufficient -- the address column must also be initialized.
9500      Make sure to output at least one set_address/end_sequence pair,
9501      choosing .text since that section is always present.  */
9502   if (text_section_line_info->in_use || !saw_one)
9503     output_one_line_info_table (text_section_line_info);
9504
9505   /* Output the marker for the end of the line number info.  */
9506   ASM_OUTPUT_LABEL (asm_out_file, l2);
9507 }
9508 \f
9509 /* Given a pointer to a tree node for some base type, return a pointer to
9510    a DIE that describes the given type.
9511
9512    This routine must only be called for GCC type nodes that correspond to
9513    Dwarf base (fundamental) types.  */
9514
9515 static dw_die_ref
9516 base_type_die (tree type)
9517 {
9518   dw_die_ref base_type_result;
9519   enum dwarf_type encoding;
9520
9521   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9522     return 0;
9523
9524   /* If this is a subtype that should not be emitted as a subrange type,
9525      use the base type.  See subrange_type_for_debug_p.  */
9526   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9527     type = TREE_TYPE (type);
9528
9529   switch (TREE_CODE (type))
9530     {
9531     case INTEGER_TYPE:
9532       if ((dwarf_version >= 4 || !dwarf_strict)
9533           && TYPE_NAME (type)
9534           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9535           && DECL_IS_BUILTIN (TYPE_NAME (type))
9536           && DECL_NAME (TYPE_NAME (type)))
9537         {
9538           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9539           if (strcmp (name, "char16_t") == 0
9540               || strcmp (name, "char32_t") == 0)
9541             {
9542               encoding = DW_ATE_UTF;
9543               break;
9544             }
9545         }
9546       if (TYPE_STRING_FLAG (type))
9547         {
9548           if (TYPE_UNSIGNED (type))
9549             encoding = DW_ATE_unsigned_char;
9550           else
9551             encoding = DW_ATE_signed_char;
9552         }
9553       else if (TYPE_UNSIGNED (type))
9554         encoding = DW_ATE_unsigned;
9555       else
9556         encoding = DW_ATE_signed;
9557       break;
9558
9559     case REAL_TYPE:
9560       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9561         {
9562           if (dwarf_version >= 3 || !dwarf_strict)
9563             encoding = DW_ATE_decimal_float;
9564           else
9565             encoding = DW_ATE_lo_user;
9566         }
9567       else
9568         encoding = DW_ATE_float;
9569       break;
9570
9571     case FIXED_POINT_TYPE:
9572       if (!(dwarf_version >= 3 || !dwarf_strict))
9573         encoding = DW_ATE_lo_user;
9574       else if (TYPE_UNSIGNED (type))
9575         encoding = DW_ATE_unsigned_fixed;
9576       else
9577         encoding = DW_ATE_signed_fixed;
9578       break;
9579
9580       /* Dwarf2 doesn't know anything about complex ints, so use
9581          a user defined type for it.  */
9582     case COMPLEX_TYPE:
9583       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9584         encoding = DW_ATE_complex_float;
9585       else
9586         encoding = DW_ATE_lo_user;
9587       break;
9588
9589     case BOOLEAN_TYPE:
9590       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9591       encoding = DW_ATE_boolean;
9592       break;
9593
9594     default:
9595       /* No other TREE_CODEs are Dwarf fundamental types.  */
9596       gcc_unreachable ();
9597     }
9598
9599   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9600
9601   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9602                    int_size_in_bytes (type));
9603   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9604
9605   return base_type_result;
9606 }
9607
9608 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9609    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9610
9611 static inline int
9612 is_base_type (tree type)
9613 {
9614   switch (TREE_CODE (type))
9615     {
9616     case ERROR_MARK:
9617     case VOID_TYPE:
9618     case INTEGER_TYPE:
9619     case REAL_TYPE:
9620     case FIXED_POINT_TYPE:
9621     case COMPLEX_TYPE:
9622     case BOOLEAN_TYPE:
9623       return 1;
9624
9625     case ARRAY_TYPE:
9626     case RECORD_TYPE:
9627     case UNION_TYPE:
9628     case QUAL_UNION_TYPE:
9629     case ENUMERAL_TYPE:
9630     case FUNCTION_TYPE:
9631     case METHOD_TYPE:
9632     case POINTER_TYPE:
9633     case REFERENCE_TYPE:
9634     case NULLPTR_TYPE:
9635     case OFFSET_TYPE:
9636     case LANG_TYPE:
9637     case VECTOR_TYPE:
9638       return 0;
9639
9640     default:
9641       gcc_unreachable ();
9642     }
9643
9644   return 0;
9645 }
9646
9647 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9648    node, return the size in bits for the type if it is a constant, or else
9649    return the alignment for the type if the type's size is not constant, or
9650    else return BITS_PER_WORD if the type actually turns out to be an
9651    ERROR_MARK node.  */
9652
9653 static inline unsigned HOST_WIDE_INT
9654 simple_type_size_in_bits (const_tree type)
9655 {
9656   if (TREE_CODE (type) == ERROR_MARK)
9657     return BITS_PER_WORD;
9658   else if (TYPE_SIZE (type) == NULL_TREE)
9659     return 0;
9660   else if (host_integerp (TYPE_SIZE (type), 1))
9661     return tree_low_cst (TYPE_SIZE (type), 1);
9662   else
9663     return TYPE_ALIGN (type);
9664 }
9665
9666 /* Similarly, but return a double_int instead of UHWI.  */
9667
9668 static inline double_int
9669 double_int_type_size_in_bits (const_tree type)
9670 {
9671   if (TREE_CODE (type) == ERROR_MARK)
9672     return uhwi_to_double_int (BITS_PER_WORD);
9673   else if (TYPE_SIZE (type) == NULL_TREE)
9674     return double_int_zero;
9675   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9676     return tree_to_double_int (TYPE_SIZE (type));
9677   else
9678     return uhwi_to_double_int (TYPE_ALIGN (type));
9679 }
9680
9681 /*  Given a pointer to a tree node for a subrange type, return a pointer
9682     to a DIE that describes the given type.  */
9683
9684 static dw_die_ref
9685 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9686 {
9687   dw_die_ref subrange_die;
9688   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9689
9690   if (context_die == NULL)
9691     context_die = comp_unit_die ();
9692
9693   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9694
9695   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9696     {
9697       /* The size of the subrange type and its base type do not match,
9698          so we need to generate a size attribute for the subrange type.  */
9699       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9700     }
9701
9702   if (low)
9703     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9704   if (high)
9705     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9706
9707   return subrange_die;
9708 }
9709
9710 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9711    entry that chains various modifiers in front of the given type.  */
9712
9713 static dw_die_ref
9714 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9715                    dw_die_ref context_die)
9716 {
9717   enum tree_code code = TREE_CODE (type);
9718   dw_die_ref mod_type_die;
9719   dw_die_ref sub_die = NULL;
9720   tree item_type = NULL;
9721   tree qualified_type;
9722   tree name, low, high;
9723
9724   if (code == ERROR_MARK)
9725     return NULL;
9726
9727   /* See if we already have the appropriately qualified variant of
9728      this type.  */
9729   qualified_type
9730     = get_qualified_type (type,
9731                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9732                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9733
9734   if (qualified_type == sizetype
9735       && TYPE_NAME (qualified_type)
9736       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9737     {
9738       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9739
9740       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9741                            && TYPE_PRECISION (t)
9742                            == TYPE_PRECISION (qualified_type)
9743                            && TYPE_UNSIGNED (t)
9744                            == TYPE_UNSIGNED (qualified_type));
9745       qualified_type = t;
9746     }
9747
9748   /* If we do, then we can just use its DIE, if it exists.  */
9749   if (qualified_type)
9750     {
9751       mod_type_die = lookup_type_die (qualified_type);
9752       if (mod_type_die)
9753         return mod_type_die;
9754     }
9755
9756   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9757
9758   /* Handle C typedef types.  */
9759   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9760       && !DECL_ARTIFICIAL (name))
9761     {
9762       tree dtype = TREE_TYPE (name);
9763
9764       if (qualified_type == dtype)
9765         {
9766           /* For a named type, use the typedef.  */
9767           gen_type_die (qualified_type, context_die);
9768           return lookup_type_die (qualified_type);
9769         }
9770       else if (is_const_type < TYPE_READONLY (dtype)
9771                || is_volatile_type < TYPE_VOLATILE (dtype)
9772                || (is_const_type <= TYPE_READONLY (dtype)
9773                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9774                    && DECL_ORIGINAL_TYPE (name) != type))
9775         /* cv-unqualified version of named type.  Just use the unnamed
9776            type to which it refers.  */
9777         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9778                                   is_const_type, is_volatile_type,
9779                                   context_die);
9780       /* Else cv-qualified version of named type; fall through.  */
9781     }
9782
9783   if (is_const_type
9784       /* If both is_const_type and is_volatile_type, prefer the path
9785          which leads to a qualified type.  */
9786       && (!is_volatile_type
9787           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9788           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9789     {
9790       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9791       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9792     }
9793   else if (is_volatile_type)
9794     {
9795       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9796       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9797     }
9798   else if (code == POINTER_TYPE)
9799     {
9800       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9801       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9802                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9803       item_type = TREE_TYPE (type);
9804       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9805         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9806                          TYPE_ADDR_SPACE (item_type));
9807     }
9808   else if (code == REFERENCE_TYPE)
9809     {
9810       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9811         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9812                                 type);
9813       else
9814         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9815       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9816                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9817       item_type = TREE_TYPE (type);
9818       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9819         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9820                          TYPE_ADDR_SPACE (item_type));
9821     }
9822   else if (code == INTEGER_TYPE
9823            && TREE_TYPE (type) != NULL_TREE
9824            && subrange_type_for_debug_p (type, &low, &high))
9825     {
9826       mod_type_die = subrange_type_die (type, low, high, context_die);
9827       item_type = TREE_TYPE (type);
9828     }
9829   else if (is_base_type (type))
9830     mod_type_die = base_type_die (type);
9831   else
9832     {
9833       gen_type_die (type, context_die);
9834
9835       /* We have to get the type_main_variant here (and pass that to the
9836          `lookup_type_die' routine) because the ..._TYPE node we have
9837          might simply be a *copy* of some original type node (where the
9838          copy was created to help us keep track of typedef names) and
9839          that copy might have a different TYPE_UID from the original
9840          ..._TYPE node.  */
9841       if (TREE_CODE (type) != VECTOR_TYPE)
9842         return lookup_type_die (type_main_variant (type));
9843       else
9844         /* Vectors have the debugging information in the type,
9845            not the main variant.  */
9846         return lookup_type_die (type);
9847     }
9848
9849   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9850      don't output a DW_TAG_typedef, since there isn't one in the
9851      user's program; just attach a DW_AT_name to the type.
9852      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9853      if the base type already has the same name.  */
9854   if (name
9855       && ((TREE_CODE (name) != TYPE_DECL
9856            && (qualified_type == TYPE_MAIN_VARIANT (type)
9857                || (!is_const_type && !is_volatile_type)))
9858           || (TREE_CODE (name) == TYPE_DECL
9859               && TREE_TYPE (name) == qualified_type
9860               && DECL_NAME (name))))
9861     {
9862       if (TREE_CODE (name) == TYPE_DECL)
9863         /* Could just call add_name_and_src_coords_attributes here,
9864            but since this is a builtin type it doesn't have any
9865            useful source coordinates anyway.  */
9866         name = DECL_NAME (name);
9867       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9868       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9869       if (TYPE_ARTIFICIAL (type))
9870         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9871     }
9872   /* This probably indicates a bug.  */
9873   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9874     add_name_attribute (mod_type_die, "__unknown__");
9875
9876   if (qualified_type)
9877     equate_type_number_to_die (qualified_type, mod_type_die);
9878
9879   if (item_type)
9880     /* We must do this after the equate_type_number_to_die call, in case
9881        this is a recursive type.  This ensures that the modified_type_die
9882        recursion will terminate even if the type is recursive.  Recursive
9883        types are possible in Ada.  */
9884     sub_die = modified_type_die (item_type,
9885                                  TYPE_READONLY (item_type),
9886                                  TYPE_VOLATILE (item_type),
9887                                  context_die);
9888
9889   if (sub_die != NULL)
9890     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9891
9892   return mod_type_die;
9893 }
9894
9895 /* Generate DIEs for the generic parameters of T.
9896    T must be either a generic type or a generic function.
9897    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9898
9899 static void
9900 gen_generic_params_dies (tree t)
9901 {
9902   tree parms, args;
9903   int parms_num, i;
9904   dw_die_ref die = NULL;
9905
9906   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9907     return;
9908
9909   if (TYPE_P (t))
9910     die = lookup_type_die (t);
9911   else if (DECL_P (t))
9912     die = lookup_decl_die (t);
9913
9914   gcc_assert (die);
9915
9916   parms = lang_hooks.get_innermost_generic_parms (t);
9917   if (!parms)
9918     /* T has no generic parameter. It means T is neither a generic type
9919        or function. End of story.  */
9920     return;
9921
9922   parms_num = TREE_VEC_LENGTH (parms);
9923   args = lang_hooks.get_innermost_generic_args (t);
9924   for (i = 0; i < parms_num; i++)
9925     {
9926       tree parm, arg, arg_pack_elems;
9927
9928       parm = TREE_VEC_ELT (parms, i);
9929       arg = TREE_VEC_ELT (args, i);
9930       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9931       gcc_assert (parm && TREE_VALUE (parm) && arg);
9932
9933       if (parm && TREE_VALUE (parm) && arg)
9934         {
9935           /* If PARM represents a template parameter pack,
9936              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9937              by DW_TAG_template_*_parameter DIEs for the argument
9938              pack elements of ARG. Note that ARG would then be
9939              an argument pack.  */
9940           if (arg_pack_elems)
9941             template_parameter_pack_die (TREE_VALUE (parm),
9942                                          arg_pack_elems,
9943                                          die);
9944           else
9945             generic_parameter_die (TREE_VALUE (parm), arg,
9946                                    true /* Emit DW_AT_name */, die);
9947         }
9948     }
9949 }
9950
9951 /* Create and return a DIE for PARM which should be
9952    the representation of a generic type parameter.
9953    For instance, in the C++ front end, PARM would be a template parameter.
9954    ARG is the argument to PARM.
9955    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9956    name of the PARM.
9957    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9958    as a child node.  */
9959
9960 static dw_die_ref
9961 generic_parameter_die (tree parm, tree arg,
9962                        bool emit_name_p,
9963                        dw_die_ref parent_die)
9964 {
9965   dw_die_ref tmpl_die = NULL;
9966   const char *name = NULL;
9967
9968   if (!parm || !DECL_NAME (parm) || !arg)
9969     return NULL;
9970
9971   /* We support non-type generic parameters and arguments,
9972      type generic parameters and arguments, as well as
9973      generic generic parameters (a.k.a. template template parameters in C++)
9974      and arguments.  */
9975   if (TREE_CODE (parm) == PARM_DECL)
9976     /* PARM is a nontype generic parameter  */
9977     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9978   else if (TREE_CODE (parm) == TYPE_DECL)
9979     /* PARM is a type generic parameter.  */
9980     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9981   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9982     /* PARM is a generic generic parameter.
9983        Its DIE is a GNU extension. It shall have a
9984        DW_AT_name attribute to represent the name of the template template
9985        parameter, and a DW_AT_GNU_template_name attribute to represent the
9986        name of the template template argument.  */
9987     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9988                         parent_die, parm);
9989   else
9990     gcc_unreachable ();
9991
9992   if (tmpl_die)
9993     {
9994       tree tmpl_type;
9995
9996       /* If PARM is a generic parameter pack, it means we are
9997          emitting debug info for a template argument pack element.
9998          In other terms, ARG is a template argument pack element.
9999          In that case, we don't emit any DW_AT_name attribute for
10000          the die.  */
10001       if (emit_name_p)
10002         {
10003           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10004           gcc_assert (name);
10005           add_AT_string (tmpl_die, DW_AT_name, name);
10006         }
10007
10008       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10009         {
10010           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10011              TMPL_DIE should have a child DW_AT_type attribute that is set
10012              to the type of the argument to PARM, which is ARG.
10013              If PARM is a type generic parameter, TMPL_DIE should have a
10014              child DW_AT_type that is set to ARG.  */
10015           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10016           add_type_attribute (tmpl_die, tmpl_type, 0,
10017                               TREE_THIS_VOLATILE (tmpl_type),
10018                               parent_die);
10019         }
10020       else
10021         {
10022           /* So TMPL_DIE is a DIE representing a
10023              a generic generic template parameter, a.k.a template template
10024              parameter in C++ and arg is a template.  */
10025
10026           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10027              to the name of the argument.  */
10028           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10029           if (name)
10030             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10031         }
10032
10033       if (TREE_CODE (parm) == PARM_DECL)
10034         /* So PARM is a non-type generic parameter.
10035            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10036            attribute of TMPL_DIE which value represents the value
10037            of ARG.
10038            We must be careful here:
10039            The value of ARG might reference some function decls.
10040            We might currently be emitting debug info for a generic
10041            type and types are emitted before function decls, we don't
10042            know if the function decls referenced by ARG will actually be
10043            emitted after cgraph computations.
10044            So must defer the generation of the DW_AT_const_value to
10045            after cgraph is ready.  */
10046         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10047     }
10048
10049   return tmpl_die;
10050 }
10051
10052 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10053    PARM_PACK must be a template parameter pack. The returned DIE
10054    will be child DIE of PARENT_DIE.  */
10055
10056 static dw_die_ref
10057 template_parameter_pack_die (tree parm_pack,
10058                              tree parm_pack_args,
10059                              dw_die_ref parent_die)
10060 {
10061   dw_die_ref die;
10062   int j;
10063
10064   gcc_assert (parent_die && parm_pack);
10065
10066   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10067   add_name_and_src_coords_attributes (die, parm_pack);
10068   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10069     generic_parameter_die (parm_pack,
10070                            TREE_VEC_ELT (parm_pack_args, j),
10071                            false /* Don't emit DW_AT_name */,
10072                            die);
10073   return die;
10074 }
10075
10076 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10077    an enumerated type.  */
10078
10079 static inline int
10080 type_is_enum (const_tree type)
10081 {
10082   return TREE_CODE (type) == ENUMERAL_TYPE;
10083 }
10084
10085 /* Return the DBX register number described by a given RTL node.  */
10086
10087 static unsigned int
10088 dbx_reg_number (const_rtx rtl)
10089 {
10090   unsigned regno = REGNO (rtl);
10091
10092   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10093
10094 #ifdef LEAF_REG_REMAP
10095   if (current_function_uses_only_leaf_regs)
10096     {
10097       int leaf_reg = LEAF_REG_REMAP (regno);
10098       if (leaf_reg != -1)
10099         regno = (unsigned) leaf_reg;
10100     }
10101 #endif
10102
10103   return DBX_REGISTER_NUMBER (regno);
10104 }
10105
10106 /* Optionally add a DW_OP_piece term to a location description expression.
10107    DW_OP_piece is only added if the location description expression already
10108    doesn't end with DW_OP_piece.  */
10109
10110 static void
10111 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10112 {
10113   dw_loc_descr_ref loc;
10114
10115   if (*list_head != NULL)
10116     {
10117       /* Find the end of the chain.  */
10118       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10119         ;
10120
10121       if (loc->dw_loc_opc != DW_OP_piece)
10122         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10123     }
10124 }
10125
10126 /* Return a location descriptor that designates a machine register or
10127    zero if there is none.  */
10128
10129 static dw_loc_descr_ref
10130 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10131 {
10132   rtx regs;
10133
10134   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10135     return 0;
10136
10137   /* We only use "frame base" when we're sure we're talking about the
10138      post-prologue local stack frame.  We do this by *not* running
10139      register elimination until this point, and recognizing the special
10140      argument pointer and soft frame pointer rtx's.
10141      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10142   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10143       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10144     {
10145       dw_loc_descr_ref result = NULL;
10146
10147       if (dwarf_version >= 4 || !dwarf_strict)
10148         {
10149           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10150                                        initialized);
10151           if (result)
10152             add_loc_descr (&result,
10153                            new_loc_descr (DW_OP_stack_value, 0, 0));
10154         }
10155       return result;
10156     }
10157
10158   regs = targetm.dwarf_register_span (rtl);
10159
10160   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10161     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10162   else
10163     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10164 }
10165
10166 /* Return a location descriptor that designates a machine register for
10167    a given hard register number.  */
10168
10169 static dw_loc_descr_ref
10170 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10171 {
10172   dw_loc_descr_ref reg_loc_descr;
10173
10174   if (regno <= 31)
10175     reg_loc_descr
10176       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10177   else
10178     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10179
10180   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10181     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10182
10183   return reg_loc_descr;
10184 }
10185
10186 /* Given an RTL of a register, return a location descriptor that
10187    designates a value that spans more than one register.  */
10188
10189 static dw_loc_descr_ref
10190 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10191                              enum var_init_status initialized)
10192 {
10193   int nregs, size, i;
10194   unsigned reg;
10195   dw_loc_descr_ref loc_result = NULL;
10196
10197   reg = REGNO (rtl);
10198 #ifdef LEAF_REG_REMAP
10199   if (current_function_uses_only_leaf_regs)
10200     {
10201       int leaf_reg = LEAF_REG_REMAP (reg);
10202       if (leaf_reg != -1)
10203         reg = (unsigned) leaf_reg;
10204     }
10205 #endif
10206   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10207   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10208
10209   /* Simple, contiguous registers.  */
10210   if (regs == NULL_RTX)
10211     {
10212       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10213
10214       loc_result = NULL;
10215       while (nregs--)
10216         {
10217           dw_loc_descr_ref t;
10218
10219           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10220                                       VAR_INIT_STATUS_INITIALIZED);
10221           add_loc_descr (&loc_result, t);
10222           add_loc_descr_op_piece (&loc_result, size);
10223           ++reg;
10224         }
10225       return loc_result;
10226     }
10227
10228   /* Now onto stupid register sets in non contiguous locations.  */
10229
10230   gcc_assert (GET_CODE (regs) == PARALLEL);
10231
10232   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10233   loc_result = NULL;
10234
10235   for (i = 0; i < XVECLEN (regs, 0); ++i)
10236     {
10237       dw_loc_descr_ref t;
10238
10239       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10240                                   VAR_INIT_STATUS_INITIALIZED);
10241       add_loc_descr (&loc_result, t);
10242       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10243       add_loc_descr_op_piece (&loc_result, size);
10244     }
10245
10246   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10247     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10248   return loc_result;
10249 }
10250
10251 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10252
10253 /* Return a location descriptor that designates a constant i,
10254    as a compound operation from constant (i >> shift), constant shift
10255    and DW_OP_shl.  */
10256
10257 static dw_loc_descr_ref
10258 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10259 {
10260   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10261   add_loc_descr (&ret, int_loc_descriptor (shift));
10262   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10263   return ret;
10264 }
10265
10266 /* Return a location descriptor that designates a constant.  */
10267
10268 static dw_loc_descr_ref
10269 int_loc_descriptor (HOST_WIDE_INT i)
10270 {
10271   enum dwarf_location_atom op;
10272
10273   /* Pick the smallest representation of a constant, rather than just
10274      defaulting to the LEB encoding.  */
10275   if (i >= 0)
10276     {
10277       int clz = clz_hwi (i);
10278       int ctz = ctz_hwi (i);
10279       if (i <= 31)
10280         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10281       else if (i <= 0xff)
10282         op = DW_OP_const1u;
10283       else if (i <= 0xffff)
10284         op = DW_OP_const2u;
10285       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10286                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10287         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10288            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10289            while DW_OP_const4u is 5 bytes.  */
10290         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10291       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10292                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10293         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10294            while DW_OP_const4u is 5 bytes.  */
10295         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10296       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10297         op = DW_OP_const4u;
10298       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10299                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10300         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10301            while DW_OP_constu of constant >= 0x100000000 takes at least
10302            6 bytes.  */
10303         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10304       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10305                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10306                   >= HOST_BITS_PER_WIDE_INT)
10307         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10308            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10309            while DW_OP_constu takes in this case at least 6 bytes.  */
10310         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10311       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10312                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10313                && size_of_uleb128 (i) > 6)
10314         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10315         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10316       else
10317         op = DW_OP_constu;
10318     }
10319   else
10320     {
10321       if (i >= -0x80)
10322         op = DW_OP_const1s;
10323       else if (i >= -0x8000)
10324         op = DW_OP_const2s;
10325       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10326         {
10327           if (size_of_int_loc_descriptor (i) < 5)
10328             {
10329               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10330               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10331               return ret;
10332             }
10333           op = DW_OP_const4s;
10334         }
10335       else
10336         {
10337           if (size_of_int_loc_descriptor (i)
10338               < (unsigned long) 1 + size_of_sleb128 (i))
10339             {
10340               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10341               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10342               return ret;
10343             }
10344           op = DW_OP_consts;
10345         }
10346     }
10347
10348   return new_loc_descr (op, i, 0);
10349 }
10350
10351 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10352    without actually allocating it.  */
10353
10354 static unsigned long
10355 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10356 {
10357   return size_of_int_loc_descriptor (i >> shift)
10358          + size_of_int_loc_descriptor (shift)
10359          + 1;
10360 }
10361
10362 /* Return size_of_locs (int_loc_descriptor (i)) without
10363    actually allocating it.  */
10364
10365 static unsigned long
10366 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10367 {
10368   unsigned long s;
10369
10370   if (i >= 0)
10371     {
10372       int clz, ctz;
10373       if (i <= 31)
10374         return 1;
10375       else if (i <= 0xff)
10376         return 2;
10377       else if (i <= 0xffff)
10378         return 3;
10379       clz = clz_hwi (i);
10380       ctz = ctz_hwi (i);
10381       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10382           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10383         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10384                                                     - clz - 5);
10385       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10386                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10387         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10388                                                     - clz - 8);
10389       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10390         return 5;
10391       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10392       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10393           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10394         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10395                                                     - clz - 8);
10396       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10397                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10398         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10399                                                     - clz - 16);
10400       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10401                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10402                && s > 6)
10403         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10404                                                     - clz - 32);
10405       else
10406         return 1 + s;
10407     }
10408   else
10409     {
10410       if (i >= -0x80)
10411         return 2;
10412       else if (i >= -0x8000)
10413         return 3;
10414       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10415         {
10416           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10417             {
10418               s = size_of_int_loc_descriptor (-i) + 1;
10419               if (s < 5)
10420                 return s;
10421             }
10422           return 5;
10423         }
10424       else
10425         {
10426           unsigned long r = 1 + size_of_sleb128 (i);
10427           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10428             {
10429               s = size_of_int_loc_descriptor (-i) + 1;
10430               if (s < r)
10431                 return s;
10432             }
10433           return r;
10434         }
10435     }
10436 }
10437
10438 /* Return loc description representing "address" of integer value.
10439    This can appear only as toplevel expression.  */
10440
10441 static dw_loc_descr_ref
10442 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10443 {
10444   int litsize;
10445   dw_loc_descr_ref loc_result = NULL;
10446
10447   if (!(dwarf_version >= 4 || !dwarf_strict))
10448     return NULL;
10449
10450   litsize = size_of_int_loc_descriptor (i);
10451   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10452      is more compact.  For DW_OP_stack_value we need:
10453      litsize + 1 (DW_OP_stack_value)
10454      and for DW_OP_implicit_value:
10455      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10456   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10457     {
10458       loc_result = int_loc_descriptor (i);
10459       add_loc_descr (&loc_result,
10460                      new_loc_descr (DW_OP_stack_value, 0, 0));
10461       return loc_result;
10462     }
10463
10464   loc_result = new_loc_descr (DW_OP_implicit_value,
10465                               size, 0);
10466   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10467   loc_result->dw_loc_oprnd2.v.val_int = i;
10468   return loc_result;
10469 }
10470
10471 /* Return a location descriptor that designates a base+offset location.  */
10472
10473 static dw_loc_descr_ref
10474 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10475                  enum var_init_status initialized)
10476 {
10477   unsigned int regno;
10478   dw_loc_descr_ref result;
10479   dw_fde_ref fde = cfun->fde;
10480
10481   /* We only use "frame base" when we're sure we're talking about the
10482      post-prologue local stack frame.  We do this by *not* running
10483      register elimination until this point, and recognizing the special
10484      argument pointer and soft frame pointer rtx's.  */
10485   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10486     {
10487       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10488
10489       if (elim != reg)
10490         {
10491           if (GET_CODE (elim) == PLUS)
10492             {
10493               offset += INTVAL (XEXP (elim, 1));
10494               elim = XEXP (elim, 0);
10495             }
10496           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10497                        && (elim == hard_frame_pointer_rtx
10498                            || elim == stack_pointer_rtx))
10499                       || elim == (frame_pointer_needed
10500                                   ? hard_frame_pointer_rtx
10501                                   : stack_pointer_rtx));
10502
10503           /* If drap register is used to align stack, use frame
10504              pointer + offset to access stack variables.  If stack
10505              is aligned without drap, use stack pointer + offset to
10506              access stack variables.  */
10507           if (crtl->stack_realign_tried
10508               && reg == frame_pointer_rtx)
10509             {
10510               int base_reg
10511                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10512                                       ? HARD_FRAME_POINTER_REGNUM
10513                                       : REGNO (elim));
10514               return new_reg_loc_descr (base_reg, offset);
10515             }
10516
10517           gcc_assert (frame_pointer_fb_offset_valid);
10518           offset += frame_pointer_fb_offset;
10519           return new_loc_descr (DW_OP_fbreg, offset, 0);
10520         }
10521     }
10522
10523   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10524
10525   if (!optimize && fde
10526       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10527     {
10528       /* Use cfa+offset to represent the location of arguments passed
10529          on the stack when drap is used to align stack.
10530          Only do this when not optimizing, for optimized code var-tracking
10531          is supposed to track where the arguments live and the register
10532          used as vdrap or drap in some spot might be used for something
10533          else in other part of the routine.  */
10534       return new_loc_descr (DW_OP_fbreg, offset, 0);
10535     }
10536
10537   if (regno <= 31)
10538     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10539                             offset, 0);
10540   else
10541     result = new_loc_descr (DW_OP_bregx, regno, offset);
10542
10543   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10544     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10545
10546   return result;
10547 }
10548
10549 /* Return true if this RTL expression describes a base+offset calculation.  */
10550
10551 static inline int
10552 is_based_loc (const_rtx rtl)
10553 {
10554   return (GET_CODE (rtl) == PLUS
10555           && ((REG_P (XEXP (rtl, 0))
10556                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10557                && CONST_INT_P (XEXP (rtl, 1)))));
10558 }
10559
10560 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10561    failed.  */
10562
10563 static dw_loc_descr_ref
10564 tls_mem_loc_descriptor (rtx mem)
10565 {
10566   tree base;
10567   dw_loc_descr_ref loc_result;
10568
10569   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10570     return NULL;
10571
10572   base = get_base_address (MEM_EXPR (mem));
10573   if (base == NULL
10574       || TREE_CODE (base) != VAR_DECL
10575       || !DECL_THREAD_LOCAL_P (base))
10576     return NULL;
10577
10578   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10579   if (loc_result == NULL)
10580     return NULL;
10581
10582   if (MEM_OFFSET (mem))
10583     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10584
10585   return loc_result;
10586 }
10587
10588 /* Output debug info about reason why we failed to expand expression as dwarf
10589    expression.  */
10590
10591 static void
10592 expansion_failed (tree expr, rtx rtl, char const *reason)
10593 {
10594   if (dump_file && (dump_flags & TDF_DETAILS))
10595     {
10596       fprintf (dump_file, "Failed to expand as dwarf: ");
10597       if (expr)
10598         print_generic_expr (dump_file, expr, dump_flags);
10599       if (rtl)
10600         {
10601           fprintf (dump_file, "\n");
10602           print_rtl (dump_file, rtl);
10603         }
10604       fprintf (dump_file, "\nReason: %s\n", reason);
10605     }
10606 }
10607
10608 /* Helper function for const_ok_for_output, called either directly
10609    or via for_each_rtx.  */
10610
10611 static int
10612 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10613 {
10614   rtx rtl = *rtlp;
10615
10616   if (GET_CODE (rtl) == UNSPEC)
10617     {
10618       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10619          we can't express it in the debug info.  */
10620 #ifdef ENABLE_CHECKING
10621       /* Don't complain about TLS UNSPECs, those are just too hard to
10622          delegitimize.  */
10623       if (XVECLEN (rtl, 0) != 1
10624           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10625           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10626           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10627           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10628         inform (current_function_decl
10629                 ? DECL_SOURCE_LOCATION (current_function_decl)
10630                 : UNKNOWN_LOCATION,
10631 #if NUM_UNSPEC_VALUES > 0
10632                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10633                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10634                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10635                 XINT (rtl, 1));
10636 #else
10637                 "non-delegitimized UNSPEC %d found in variable location",
10638                 XINT (rtl, 1));
10639 #endif
10640 #endif
10641       expansion_failed (NULL_TREE, rtl,
10642                         "UNSPEC hasn't been delegitimized.\n");
10643       return 1;
10644     }
10645
10646   if (GET_CODE (rtl) != SYMBOL_REF)
10647     return 0;
10648
10649   if (CONSTANT_POOL_ADDRESS_P (rtl))
10650     {
10651       bool marked;
10652       get_pool_constant_mark (rtl, &marked);
10653       /* If all references to this pool constant were optimized away,
10654          it was not output and thus we can't represent it.  */
10655       if (!marked)
10656         {
10657           expansion_failed (NULL_TREE, rtl,
10658                             "Constant was removed from constant pool.\n");
10659           return 1;
10660         }
10661     }
10662
10663   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10664     return 1;
10665
10666   /* Avoid references to external symbols in debug info, on several targets
10667      the linker might even refuse to link when linking a shared library,
10668      and in many other cases the relocations for .debug_info/.debug_loc are
10669      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10670      to be defined within the same shared library or executable are fine.  */
10671   if (SYMBOL_REF_EXTERNAL_P (rtl))
10672     {
10673       tree decl = SYMBOL_REF_DECL (rtl);
10674
10675       if (decl == NULL || !targetm.binds_local_p (decl))
10676         {
10677           expansion_failed (NULL_TREE, rtl,
10678                             "Symbol not defined in current TU.\n");
10679           return 1;
10680         }
10681     }
10682
10683   return 0;
10684 }
10685
10686 /* Return true if constant RTL can be emitted in DW_OP_addr or
10687    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10688    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10689
10690 static bool
10691 const_ok_for_output (rtx rtl)
10692 {
10693   if (GET_CODE (rtl) == SYMBOL_REF)
10694     return const_ok_for_output_1 (&rtl, NULL) == 0;
10695
10696   if (GET_CODE (rtl) == CONST)
10697     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10698
10699   return true;
10700 }
10701
10702 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10703    if possible, NULL otherwise.  */
10704
10705 static dw_die_ref
10706 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10707 {
10708   dw_die_ref type_die;
10709   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10710
10711   if (type == NULL)
10712     return NULL;
10713   switch (TREE_CODE (type))
10714     {
10715     case INTEGER_TYPE:
10716     case REAL_TYPE:
10717       break;
10718     default:
10719       return NULL;
10720     }
10721   type_die = lookup_type_die (type);
10722   if (!type_die)
10723     type_die = modified_type_die (type, false, false, comp_unit_die ());
10724   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10725     return NULL;
10726   return type_die;
10727 }
10728
10729 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10730    type matching MODE, or, if MODE is narrower than or as wide as
10731    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10732    possible.  */
10733
10734 static dw_loc_descr_ref
10735 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10736 {
10737   enum machine_mode outer_mode = mode;
10738   dw_die_ref type_die;
10739   dw_loc_descr_ref cvt;
10740
10741   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10742     {
10743       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10744       return op;
10745     }
10746   type_die = base_type_for_mode (outer_mode, 1);
10747   if (type_die == NULL)
10748     return NULL;
10749   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10750   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10751   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10752   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10753   add_loc_descr (&op, cvt);
10754   return op;
10755 }
10756
10757 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10758
10759 static dw_loc_descr_ref
10760 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10761                         dw_loc_descr_ref op1)
10762 {
10763   dw_loc_descr_ref ret = op0;
10764   add_loc_descr (&ret, op1);
10765   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10766   if (STORE_FLAG_VALUE != 1)
10767     {
10768       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10769       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10770     }
10771   return ret;
10772 }
10773
10774 /* Return location descriptor for signed comparison OP RTL.  */
10775
10776 static dw_loc_descr_ref
10777 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10778                          enum machine_mode mem_mode)
10779 {
10780   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10781   dw_loc_descr_ref op0, op1;
10782   int shift;
10783
10784   if (op_mode == VOIDmode)
10785     op_mode = GET_MODE (XEXP (rtl, 1));
10786   if (op_mode == VOIDmode)
10787     return NULL;
10788
10789   if (dwarf_strict
10790       && (GET_MODE_CLASS (op_mode) != MODE_INT
10791           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10792     return NULL;
10793
10794   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10795                             VAR_INIT_STATUS_INITIALIZED);
10796   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10797                             VAR_INIT_STATUS_INITIALIZED);
10798
10799   if (op0 == NULL || op1 == NULL)
10800     return NULL;
10801
10802   if (GET_MODE_CLASS (op_mode) != MODE_INT
10803       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10804     return compare_loc_descriptor (op, op0, op1);
10805
10806   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10807     {
10808       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10809       dw_loc_descr_ref cvt;
10810
10811       if (type_die == NULL)
10812         return NULL;
10813       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10814       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10815       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10816       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10817       add_loc_descr (&op0, cvt);
10818       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10819       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10820       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10821       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10822       add_loc_descr (&op1, cvt);
10823       return compare_loc_descriptor (op, op0, op1);
10824     }
10825
10826   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10827   /* For eq/ne, if the operands are known to be zero-extended,
10828      there is no need to do the fancy shifting up.  */
10829   if (op == DW_OP_eq || op == DW_OP_ne)
10830     {
10831       dw_loc_descr_ref last0, last1;
10832       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10833         ;
10834       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10835         ;
10836       /* deref_size zero extends, and for constants we can check
10837          whether they are zero extended or not.  */
10838       if (((last0->dw_loc_opc == DW_OP_deref_size
10839             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10840            || (CONST_INT_P (XEXP (rtl, 0))
10841                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10842                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10843           && ((last1->dw_loc_opc == DW_OP_deref_size
10844                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10845               || (CONST_INT_P (XEXP (rtl, 1))
10846                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10847                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10848         return compare_loc_descriptor (op, op0, op1);
10849
10850       /* EQ/NE comparison against constant in narrower type than
10851          DWARF2_ADDR_SIZE can be performed either as
10852          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10853          DW_OP_{eq,ne}
10854          or
10855          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10856          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10857       if (CONST_INT_P (XEXP (rtl, 1))
10858           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10859           && (size_of_int_loc_descriptor (shift) + 1
10860               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10861               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10862                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10863                                                & GET_MODE_MASK (op_mode))))
10864         {
10865           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10866           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10867           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10868                                     & GET_MODE_MASK (op_mode));
10869           return compare_loc_descriptor (op, op0, op1);
10870         }
10871     }
10872   add_loc_descr (&op0, int_loc_descriptor (shift));
10873   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10874   if (CONST_INT_P (XEXP (rtl, 1)))
10875     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10876   else
10877     {
10878       add_loc_descr (&op1, int_loc_descriptor (shift));
10879       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10880     }
10881   return compare_loc_descriptor (op, op0, op1);
10882 }
10883
10884 /* Return location descriptor for unsigned comparison OP RTL.  */
10885
10886 static dw_loc_descr_ref
10887 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10888                          enum machine_mode mem_mode)
10889 {
10890   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10891   dw_loc_descr_ref op0, op1;
10892
10893   if (op_mode == VOIDmode)
10894     op_mode = GET_MODE (XEXP (rtl, 1));
10895   if (op_mode == VOIDmode)
10896     return NULL;
10897   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10898     return NULL;
10899
10900   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10901     return NULL;
10902
10903   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10904                             VAR_INIT_STATUS_INITIALIZED);
10905   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10906                             VAR_INIT_STATUS_INITIALIZED);
10907
10908   if (op0 == NULL || op1 == NULL)
10909     return NULL;
10910
10911   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10912     {
10913       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10914       dw_loc_descr_ref last0, last1;
10915       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10916         ;
10917       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10918         ;
10919       if (CONST_INT_P (XEXP (rtl, 0)))
10920         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10921       /* deref_size zero extends, so no need to mask it again.  */
10922       else if (last0->dw_loc_opc != DW_OP_deref_size
10923                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10924         {
10925           add_loc_descr (&op0, int_loc_descriptor (mask));
10926           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10927         }
10928       if (CONST_INT_P (XEXP (rtl, 1)))
10929         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10930       /* deref_size zero extends, so no need to mask it again.  */
10931       else if (last1->dw_loc_opc != DW_OP_deref_size
10932                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10933         {
10934           add_loc_descr (&op1, int_loc_descriptor (mask));
10935           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10936         }
10937     }
10938   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10939     {
10940       HOST_WIDE_INT bias = 1;
10941       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10942       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10943       if (CONST_INT_P (XEXP (rtl, 1)))
10944         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10945                                   + INTVAL (XEXP (rtl, 1)));
10946       else
10947         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10948                                             bias, 0));
10949     }
10950   return compare_loc_descriptor (op, op0, op1);
10951 }
10952
10953 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10954
10955 static dw_loc_descr_ref
10956 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10957                        enum machine_mode mem_mode)
10958 {
10959   enum dwarf_location_atom op;
10960   dw_loc_descr_ref op0, op1, ret;
10961   dw_loc_descr_ref bra_node, drop_node;
10962
10963   if (dwarf_strict
10964       && (GET_MODE_CLASS (mode) != MODE_INT
10965           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10966     return NULL;
10967
10968   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10969                             VAR_INIT_STATUS_INITIALIZED);
10970   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10971                             VAR_INIT_STATUS_INITIALIZED);
10972
10973   if (op0 == NULL || op1 == NULL)
10974     return NULL;
10975
10976   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10977   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10978   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10979   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10980     {
10981       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10982         {
10983           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10984           add_loc_descr (&op0, int_loc_descriptor (mask));
10985           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10986           add_loc_descr (&op1, int_loc_descriptor (mask));
10987           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10988         }
10989       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10990         {
10991           HOST_WIDE_INT bias = 1;
10992           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10993           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10994           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10995         }
10996     }
10997   else if (GET_MODE_CLASS (mode) == MODE_INT
10998            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10999     {
11000       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11001       add_loc_descr (&op0, int_loc_descriptor (shift));
11002       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11003       add_loc_descr (&op1, int_loc_descriptor (shift));
11004       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11005     }
11006   else if (GET_MODE_CLASS (mode) == MODE_INT
11007            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11008     {
11009       dw_die_ref type_die = base_type_for_mode (mode, 0);
11010       dw_loc_descr_ref cvt;
11011       if (type_die == NULL)
11012         return NULL;
11013       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11014       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11015       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11016       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11017       add_loc_descr (&op0, cvt);
11018       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11019       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11020       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11021       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11022       add_loc_descr (&op1, cvt);
11023     }
11024
11025   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11026     op = DW_OP_lt;
11027   else
11028     op = DW_OP_gt;
11029   ret = op0;
11030   add_loc_descr (&ret, op1);
11031   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11032   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11033   add_loc_descr (&ret, bra_node);
11034   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11035   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11036   add_loc_descr (&ret, drop_node);
11037   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11038   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11039   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11040       && GET_MODE_CLASS (mode) == MODE_INT
11041       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11042     ret = convert_descriptor_to_mode (mode, ret);
11043   return ret;
11044 }
11045
11046 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11047    but after converting arguments to type_die, afterwards
11048    convert back to unsigned.  */
11049
11050 static dw_loc_descr_ref
11051 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11052              enum machine_mode mode, enum machine_mode mem_mode)
11053 {
11054   dw_loc_descr_ref cvt, op0, op1;
11055
11056   if (type_die == NULL)
11057     return NULL;
11058   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11059                             VAR_INIT_STATUS_INITIALIZED);
11060   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11061                             VAR_INIT_STATUS_INITIALIZED);
11062   if (op0 == NULL || op1 == NULL)
11063     return NULL;
11064   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11065   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11066   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11067   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11068   add_loc_descr (&op0, cvt);
11069   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11070   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11071   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11072   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11073   add_loc_descr (&op1, cvt);
11074   add_loc_descr (&op0, op1);
11075   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11076   return convert_descriptor_to_mode (mode, op0);
11077 }
11078
11079 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11080    const0 is DW_OP_lit0 or corresponding typed constant,
11081    const1 is DW_OP_lit1 or corresponding typed constant
11082    and constMSB is constant with just the MSB bit set
11083    for the mode):
11084        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11085    L1: const0 DW_OP_swap
11086    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11087        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11088    L3: DW_OP_drop
11089    L4: DW_OP_nop
11090
11091    CTZ is similar:
11092        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11093    L1: const0 DW_OP_swap
11094    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11095        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11096    L3: DW_OP_drop
11097    L4: DW_OP_nop
11098
11099    FFS is similar:
11100        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11101    L1: const1 DW_OP_swap
11102    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11103        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11104    L3: DW_OP_drop
11105    L4: DW_OP_nop  */
11106
11107 static dw_loc_descr_ref
11108 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11109                     enum machine_mode mem_mode)
11110 {
11111   dw_loc_descr_ref op0, ret, tmp;
11112   HOST_WIDE_INT valv;
11113   dw_loc_descr_ref l1jump, l1label;
11114   dw_loc_descr_ref l2jump, l2label;
11115   dw_loc_descr_ref l3jump, l3label;
11116   dw_loc_descr_ref l4jump, l4label;
11117   rtx msb;
11118
11119   if (GET_MODE_CLASS (mode) != MODE_INT
11120       || GET_MODE (XEXP (rtl, 0)) != mode
11121       || (GET_CODE (rtl) == CLZ
11122           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11123     return NULL;
11124
11125   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11126                             VAR_INIT_STATUS_INITIALIZED);
11127   if (op0 == NULL)
11128     return NULL;
11129   ret = op0;
11130   if (GET_CODE (rtl) == CLZ)
11131     {
11132       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11133         valv = GET_MODE_BITSIZE (mode);
11134     }
11135   else if (GET_CODE (rtl) == FFS)
11136     valv = 0;
11137   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11138     valv = GET_MODE_BITSIZE (mode);
11139   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11140   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11141   add_loc_descr (&ret, l1jump);
11142   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11143   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11144                             VAR_INIT_STATUS_INITIALIZED);
11145   if (tmp == NULL)
11146     return NULL;
11147   add_loc_descr (&ret, tmp);
11148   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11149   add_loc_descr (&ret, l4jump);
11150   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11151                                 ? const1_rtx : const0_rtx,
11152                                 mode, mem_mode,
11153                                 VAR_INIT_STATUS_INITIALIZED);
11154   if (l1label == NULL)
11155     return NULL;
11156   add_loc_descr (&ret, l1label);
11157   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11158   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11159   add_loc_descr (&ret, l2label);
11160   if (GET_CODE (rtl) != CLZ)
11161     msb = const1_rtx;
11162   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11163     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11164                    << (GET_MODE_BITSIZE (mode) - 1));
11165   else
11166     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11167                                   << (GET_MODE_BITSIZE (mode)
11168                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11169   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11170     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11171                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11172                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11173   else
11174     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11175                               VAR_INIT_STATUS_INITIALIZED);
11176   if (tmp == NULL)
11177     return NULL;
11178   add_loc_descr (&ret, tmp);
11179   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11180   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11181   add_loc_descr (&ret, l3jump);
11182   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11183                             VAR_INIT_STATUS_INITIALIZED);
11184   if (tmp == NULL)
11185     return NULL;
11186   add_loc_descr (&ret, tmp);
11187   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11188                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11189   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11190   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11191   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11192   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11193   add_loc_descr (&ret, l2jump);
11194   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11195   add_loc_descr (&ret, l3label);
11196   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11197   add_loc_descr (&ret, l4label);
11198   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11199   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11200   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11201   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11202   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11203   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11204   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11205   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11206   return ret;
11207 }
11208
11209 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11210    const1 is DW_OP_lit1 or corresponding typed constant):
11211        const0 DW_OP_swap
11212    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11213        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11214    L2: DW_OP_drop
11215
11216    PARITY is similar:
11217    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11218        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11219    L2: DW_OP_drop  */
11220
11221 static dw_loc_descr_ref
11222 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11223                          enum machine_mode mem_mode)
11224 {
11225   dw_loc_descr_ref op0, ret, tmp;
11226   dw_loc_descr_ref l1jump, l1label;
11227   dw_loc_descr_ref l2jump, l2label;
11228
11229   if (GET_MODE_CLASS (mode) != MODE_INT
11230       || GET_MODE (XEXP (rtl, 0)) != mode)
11231     return NULL;
11232
11233   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11234                             VAR_INIT_STATUS_INITIALIZED);
11235   if (op0 == NULL)
11236     return NULL;
11237   ret = op0;
11238   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11239                             VAR_INIT_STATUS_INITIALIZED);
11240   if (tmp == NULL)
11241     return NULL;
11242   add_loc_descr (&ret, tmp);
11243   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11244   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11245   add_loc_descr (&ret, l1label);
11246   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11247   add_loc_descr (&ret, l2jump);
11248   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11249   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11250   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11251                             VAR_INIT_STATUS_INITIALIZED);
11252   if (tmp == NULL)
11253     return NULL;
11254   add_loc_descr (&ret, tmp);
11255   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11256   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11257                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11258   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11259   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11260                             VAR_INIT_STATUS_INITIALIZED);
11261   add_loc_descr (&ret, tmp);
11262   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11263   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11264   add_loc_descr (&ret, l1jump);
11265   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11266   add_loc_descr (&ret, l2label);
11267   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11268   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11269   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11270   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11271   return ret;
11272 }
11273
11274 /* BSWAP (constS is initial shift count, either 56 or 24):
11275        constS const0
11276    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11277        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11278        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11279        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11280    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11281
11282 static dw_loc_descr_ref
11283 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11284                       enum machine_mode mem_mode)
11285 {
11286   dw_loc_descr_ref op0, ret, tmp;
11287   dw_loc_descr_ref l1jump, l1label;
11288   dw_loc_descr_ref l2jump, l2label;
11289
11290   if (GET_MODE_CLASS (mode) != MODE_INT
11291       || BITS_PER_UNIT != 8
11292       || (GET_MODE_BITSIZE (mode) != 32
11293           &&  GET_MODE_BITSIZE (mode) != 64))
11294     return NULL;
11295
11296   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11297                             VAR_INIT_STATUS_INITIALIZED);
11298   if (op0 == NULL)
11299     return NULL;
11300
11301   ret = op0;
11302   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11303                             mode, mem_mode,
11304                             VAR_INIT_STATUS_INITIALIZED);
11305   if (tmp == NULL)
11306     return NULL;
11307   add_loc_descr (&ret, tmp);
11308   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11309                             VAR_INIT_STATUS_INITIALIZED);
11310   if (tmp == NULL)
11311     return NULL;
11312   add_loc_descr (&ret, tmp);
11313   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11314   add_loc_descr (&ret, l1label);
11315   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11316                             mode, mem_mode,
11317                             VAR_INIT_STATUS_INITIALIZED);
11318   add_loc_descr (&ret, tmp);
11319   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11320   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11321   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11322   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11323                             VAR_INIT_STATUS_INITIALIZED);
11324   if (tmp == NULL)
11325     return NULL;
11326   add_loc_descr (&ret, tmp);
11327   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11328   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11329   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11330   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11331   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11332   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11333   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11334                             VAR_INIT_STATUS_INITIALIZED);
11335   add_loc_descr (&ret, tmp);
11336   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11337   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11338   add_loc_descr (&ret, l2jump);
11339   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11340                             VAR_INIT_STATUS_INITIALIZED);
11341   add_loc_descr (&ret, tmp);
11342   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11343   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11344   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11345   add_loc_descr (&ret, l1jump);
11346   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11347   add_loc_descr (&ret, l2label);
11348   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11349   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11350   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11351   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11352   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11353   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11354   return ret;
11355 }
11356
11357 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11358    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11359    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11360    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11361
11362    ROTATERT is similar:
11363    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11364    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11365    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11366
11367 static dw_loc_descr_ref
11368 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11369                        enum machine_mode mem_mode)
11370 {
11371   rtx rtlop1 = XEXP (rtl, 1);
11372   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11373   int i;
11374
11375   if (GET_MODE_CLASS (mode) != MODE_INT)
11376     return NULL;
11377
11378   if (GET_MODE (rtlop1) != VOIDmode
11379       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11380     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11381   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11382                             VAR_INIT_STATUS_INITIALIZED);
11383   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11384                             VAR_INIT_STATUS_INITIALIZED);
11385   if (op0 == NULL || op1 == NULL)
11386     return NULL;
11387   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11388     for (i = 0; i < 2; i++)
11389       {
11390         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11391           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11392                                         mode, mem_mode,
11393                                         VAR_INIT_STATUS_INITIALIZED);
11394         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11395           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11396                                    ? DW_OP_const4u
11397                                    : HOST_BITS_PER_WIDE_INT == 64
11398                                    ? DW_OP_const8u : DW_OP_constu,
11399                                    GET_MODE_MASK (mode), 0);
11400         else
11401           mask[i] = NULL;
11402         if (mask[i] == NULL)
11403           return NULL;
11404         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11405       }
11406   ret = op0;
11407   add_loc_descr (&ret, op1);
11408   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11409   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11410   if (GET_CODE (rtl) == ROTATERT)
11411     {
11412       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11413       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11414                                           GET_MODE_BITSIZE (mode), 0));
11415     }
11416   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11417   if (mask[0] != NULL)
11418     add_loc_descr (&ret, mask[0]);
11419   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11420   if (mask[1] != NULL)
11421     {
11422       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11423       add_loc_descr (&ret, mask[1]);
11424       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11425     }
11426   if (GET_CODE (rtl) == ROTATE)
11427     {
11428       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11429       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11430                                           GET_MODE_BITSIZE (mode), 0));
11431     }
11432   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11433   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11434   return ret;
11435 }
11436
11437 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11438    for DEBUG_PARAMETER_REF RTL.  */
11439
11440 static dw_loc_descr_ref
11441 parameter_ref_descriptor (rtx rtl)
11442 {
11443   dw_loc_descr_ref ret;
11444   dw_die_ref ref;
11445
11446   if (dwarf_strict)
11447     return NULL;
11448   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11449   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11450   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11451   if (ref)
11452     {
11453       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11454       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11455       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11456     }
11457   else
11458     {
11459       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11460       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11461     }
11462   return ret;
11463 }
11464
11465 /* Helper function to get mode of MEM's address.  */
11466
11467 enum machine_mode
11468 get_address_mode (rtx mem)
11469 {
11470   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11471   if (mode != VOIDmode)
11472     return mode;
11473   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11474 }
11475
11476 /* The following routine converts the RTL for a variable or parameter
11477    (resident in memory) into an equivalent Dwarf representation of a
11478    mechanism for getting the address of that same variable onto the top of a
11479    hypothetical "address evaluation" stack.
11480
11481    When creating memory location descriptors, we are effectively transforming
11482    the RTL for a memory-resident object into its Dwarf postfix expression
11483    equivalent.  This routine recursively descends an RTL tree, turning
11484    it into Dwarf postfix code as it goes.
11485
11486    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11487
11488    MEM_MODE is the mode of the memory reference, needed to handle some
11489    autoincrement addressing modes.
11490
11491    Return 0 if we can't represent the location.  */
11492
11493 dw_loc_descr_ref
11494 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11495                     enum machine_mode mem_mode,
11496                     enum var_init_status initialized)
11497 {
11498   dw_loc_descr_ref mem_loc_result = NULL;
11499   enum dwarf_location_atom op;
11500   dw_loc_descr_ref op0, op1;
11501
11502   if (mode == VOIDmode)
11503     mode = GET_MODE (rtl);
11504
11505   /* Note that for a dynamically sized array, the location we will generate a
11506      description of here will be the lowest numbered location which is
11507      actually within the array.  That's *not* necessarily the same as the
11508      zeroth element of the array.  */
11509
11510   rtl = targetm.delegitimize_address (rtl);
11511
11512   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11513     return NULL;
11514
11515   switch (GET_CODE (rtl))
11516     {
11517     case POST_INC:
11518     case POST_DEC:
11519     case POST_MODIFY:
11520       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11521
11522     case SUBREG:
11523       /* The case of a subreg may arise when we have a local (register)
11524          variable or a formal (register) parameter which doesn't quite fill
11525          up an entire register.  For now, just assume that it is
11526          legitimate to make the Dwarf info refer to the whole register which
11527          contains the given subreg.  */
11528       if (!subreg_lowpart_p (rtl))
11529         break;
11530       if (GET_MODE_CLASS (mode) == MODE_INT
11531           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11532           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11533 #ifdef POINTERS_EXTEND_UNSIGNED
11534               || (mode == Pmode && mem_mode != VOIDmode)
11535 #endif
11536              )
11537           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11538         {
11539           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11540                                                GET_MODE (SUBREG_REG (rtl)),
11541                                                mem_mode, initialized);
11542           break;
11543         }
11544       if (dwarf_strict)
11545         break;
11546       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11547         break;
11548       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11549           && (GET_MODE_CLASS (mode) != MODE_INT
11550               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11551         break;
11552       else
11553         {
11554           dw_die_ref type_die;
11555           dw_loc_descr_ref cvt;
11556
11557           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11558                                                GET_MODE (SUBREG_REG (rtl)),
11559                                                mem_mode, initialized);
11560           if (mem_loc_result == NULL)
11561             break;
11562           type_die = base_type_for_mode (mode,
11563                                          GET_MODE_CLASS (mode) == MODE_INT);
11564           if (type_die == NULL)
11565             {
11566               mem_loc_result = NULL;
11567               break;
11568             }
11569           if (GET_MODE_SIZE (mode)
11570               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11571             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11572           else
11573             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11574           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11575           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11576           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11577           add_loc_descr (&mem_loc_result, cvt);
11578         }
11579       break;
11580
11581     case REG:
11582       if (GET_MODE_CLASS (mode) != MODE_INT
11583           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11584 #ifdef POINTERS_EXTEND_UNSIGNED
11585               && (mode != Pmode || mem_mode == VOIDmode)
11586 #endif
11587               ))
11588         {
11589           dw_die_ref type_die;
11590
11591           if (dwarf_strict)
11592             break;
11593           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11594             break;
11595           type_die = base_type_for_mode (mode,
11596                                          GET_MODE_CLASS (mode) == MODE_INT);
11597           if (type_die == NULL)
11598             break;
11599           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11600                                           dbx_reg_number (rtl), 0);
11601           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11602           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11603           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11604           break;
11605         }
11606       /* Whenever a register number forms a part of the description of the
11607          method for calculating the (dynamic) address of a memory resident
11608          object, DWARF rules require the register number be referred to as
11609          a "base register".  This distinction is not based in any way upon
11610          what category of register the hardware believes the given register
11611          belongs to.  This is strictly DWARF terminology we're dealing with
11612          here. Note that in cases where the location of a memory-resident
11613          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11614          OP_CONST (0)) the actual DWARF location descriptor that we generate
11615          may just be OP_BASEREG (basereg).  This may look deceptively like
11616          the object in question was allocated to a register (rather than in
11617          memory) so DWARF consumers need to be aware of the subtle
11618          distinction between OP_REG and OP_BASEREG.  */
11619       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11620         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11621       else if (stack_realign_drap
11622                && crtl->drap_reg
11623                && crtl->args.internal_arg_pointer == rtl
11624                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11625         {
11626           /* If RTL is internal_arg_pointer, which has been optimized
11627              out, use DRAP instead.  */
11628           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11629                                             VAR_INIT_STATUS_INITIALIZED);
11630         }
11631       break;
11632
11633     case SIGN_EXTEND:
11634     case ZERO_EXTEND:
11635       if (GET_MODE_CLASS (mode) != MODE_INT)
11636         break;
11637       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11638                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11639       if (op0 == 0)
11640         break;
11641       else if (GET_CODE (rtl) == ZERO_EXTEND
11642                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11643                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11644                   < HOST_BITS_PER_WIDE_INT
11645                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11646                   to expand zero extend as two shifts instead of
11647                   masking.  */
11648                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11649         {
11650           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11651           mem_loc_result = op0;
11652           add_loc_descr (&mem_loc_result,
11653                          int_loc_descriptor (GET_MODE_MASK (imode)));
11654           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11655         }
11656       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11657         {
11658           int shift = DWARF2_ADDR_SIZE
11659                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11660           shift *= BITS_PER_UNIT;
11661           if (GET_CODE (rtl) == SIGN_EXTEND)
11662             op = DW_OP_shra;
11663           else
11664             op = DW_OP_shr;
11665           mem_loc_result = op0;
11666           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11667           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11668           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11669           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11670         }
11671       else if (!dwarf_strict)
11672         {
11673           dw_die_ref type_die1, type_die2;
11674           dw_loc_descr_ref cvt;
11675
11676           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11677                                           GET_CODE (rtl) == ZERO_EXTEND);
11678           if (type_die1 == NULL)
11679             break;
11680           type_die2 = base_type_for_mode (mode, 1);
11681           if (type_die2 == NULL)
11682             break;
11683           mem_loc_result = op0;
11684           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11685           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11686           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11687           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11688           add_loc_descr (&mem_loc_result, cvt);
11689           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11690           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11691           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11692           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11693           add_loc_descr (&mem_loc_result, cvt);
11694         }
11695       break;
11696
11697     case MEM:
11698       {
11699         rtx new_rtl = avoid_constant_pool_reference (rtl);
11700         if (new_rtl != rtl)
11701           {
11702             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11703                                                  initialized);
11704             if (mem_loc_result != NULL)
11705               return mem_loc_result;
11706           }
11707       }
11708       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11709                                            get_address_mode (rtl), mode,
11710                                            VAR_INIT_STATUS_INITIALIZED);
11711       if (mem_loc_result == NULL)
11712         mem_loc_result = tls_mem_loc_descriptor (rtl);
11713       if (mem_loc_result != NULL)
11714         {
11715           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11716               || GET_MODE_CLASS (mode) != MODE_INT)
11717             {
11718               dw_die_ref type_die;
11719               dw_loc_descr_ref deref;
11720
11721               if (dwarf_strict)
11722                 return NULL;
11723               type_die
11724                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11725               if (type_die == NULL)
11726                 return NULL;
11727               deref = new_loc_descr (DW_OP_GNU_deref_type,
11728                                      GET_MODE_SIZE (mode), 0);
11729               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11730               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11731               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11732               add_loc_descr (&mem_loc_result, deref);
11733             }
11734           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11735             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11736           else
11737             add_loc_descr (&mem_loc_result,
11738                            new_loc_descr (DW_OP_deref_size,
11739                                           GET_MODE_SIZE (mode), 0));
11740         }
11741       break;
11742
11743     case LO_SUM:
11744       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11745
11746     case LABEL_REF:
11747       /* Some ports can transform a symbol ref into a label ref, because
11748          the symbol ref is too far away and has to be dumped into a constant
11749          pool.  */
11750     case CONST:
11751     case SYMBOL_REF:
11752       if (GET_MODE_CLASS (mode) != MODE_INT
11753           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11754 #ifdef POINTERS_EXTEND_UNSIGNED
11755               && (mode != Pmode || mem_mode == VOIDmode)
11756 #endif
11757               ))
11758         break;
11759       if (GET_CODE (rtl) == SYMBOL_REF
11760           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11761         {
11762           dw_loc_descr_ref temp;
11763
11764           /* If this is not defined, we have no way to emit the data.  */
11765           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11766             break;
11767
11768           /* We used to emit DW_OP_addr here, but that's wrong, since
11769              DW_OP_addr should be relocated by the debug info consumer,
11770              while DW_OP_GNU_push_tls_address operand should not.  */
11771           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11772                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11773           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11774           temp->dw_loc_oprnd1.v.val_addr = rtl;
11775           temp->dtprel = true;
11776
11777           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11778           add_loc_descr (&mem_loc_result, temp);
11779
11780           break;
11781         }
11782
11783       if (!const_ok_for_output (rtl))
11784         break;
11785
11786     symref:
11787       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11788       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11789       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11790       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11791       break;
11792
11793     case CONCAT:
11794     case CONCATN:
11795     case VAR_LOCATION:
11796     case DEBUG_IMPLICIT_PTR:
11797       expansion_failed (NULL_TREE, rtl,
11798                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11799       return 0;
11800
11801     case ENTRY_VALUE:
11802       if (dwarf_strict)
11803         return NULL;
11804       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11805         {
11806           if (GET_MODE_CLASS (mode) != MODE_INT
11807               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11808             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11809                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11810           else
11811             op0
11812               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11813                                         VAR_INIT_STATUS_INITIALIZED);
11814         }
11815       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11816                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11817         {
11818           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11819                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11820           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11821             return NULL;
11822         }
11823       else
11824         gcc_unreachable ();
11825       if (op0 == NULL)
11826         return NULL;
11827       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11828       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11829       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11830       break;
11831
11832     case DEBUG_PARAMETER_REF:
11833       mem_loc_result = parameter_ref_descriptor (rtl);
11834       break;
11835
11836     case PRE_MODIFY:
11837       /* Extract the PLUS expression nested inside and fall into
11838          PLUS code below.  */
11839       rtl = XEXP (rtl, 1);
11840       goto plus;
11841
11842     case PRE_INC:
11843     case PRE_DEC:
11844       /* Turn these into a PLUS expression and fall into the PLUS code
11845          below.  */
11846       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11847                           GEN_INT (GET_CODE (rtl) == PRE_INC
11848                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11849                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11850
11851       /* ... fall through ...  */
11852
11853     case PLUS:
11854     plus:
11855       if (is_based_loc (rtl)
11856           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11857           && GET_MODE_CLASS (mode) == MODE_INT)
11858         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11859                                           INTVAL (XEXP (rtl, 1)),
11860                                           VAR_INIT_STATUS_INITIALIZED);
11861       else
11862         {
11863           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11864                                                VAR_INIT_STATUS_INITIALIZED);
11865           if (mem_loc_result == 0)
11866             break;
11867
11868           if (CONST_INT_P (XEXP (rtl, 1))
11869               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11870             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11871           else
11872             {
11873               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11874                                         VAR_INIT_STATUS_INITIALIZED);
11875               if (op1 == 0)
11876                 break;
11877               add_loc_descr (&mem_loc_result, op1);
11878               add_loc_descr (&mem_loc_result,
11879                              new_loc_descr (DW_OP_plus, 0, 0));
11880             }
11881         }
11882       break;
11883
11884     /* If a pseudo-reg is optimized away, it is possible for it to
11885        be replaced with a MEM containing a multiply or shift.  */
11886     case MINUS:
11887       op = DW_OP_minus;
11888       goto do_binop;
11889
11890     case MULT:
11891       op = DW_OP_mul;
11892       goto do_binop;
11893
11894     case DIV:
11895       if (!dwarf_strict
11896           && GET_MODE_CLASS (mode) == MODE_INT
11897           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11898         {
11899           mem_loc_result = typed_binop (DW_OP_div, rtl,
11900                                         base_type_for_mode (mode, 0),
11901                                         mode, mem_mode);
11902           break;
11903         }
11904       op = DW_OP_div;
11905       goto do_binop;
11906
11907     case UMOD:
11908       op = DW_OP_mod;
11909       goto do_binop;
11910
11911     case ASHIFT:
11912       op = DW_OP_shl;
11913       goto do_shift;
11914
11915     case ASHIFTRT:
11916       op = DW_OP_shra;
11917       goto do_shift;
11918
11919     case LSHIFTRT:
11920       op = DW_OP_shr;
11921       goto do_shift;
11922
11923     do_shift:
11924       if (GET_MODE_CLASS (mode) != MODE_INT)
11925         break;
11926       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11927                                 VAR_INIT_STATUS_INITIALIZED);
11928       {
11929         rtx rtlop1 = XEXP (rtl, 1);
11930         if (GET_MODE (rtlop1) != VOIDmode
11931             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11932                < GET_MODE_BITSIZE (mode))
11933           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11934         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11935                                   VAR_INIT_STATUS_INITIALIZED);
11936       }
11937
11938       if (op0 == 0 || op1 == 0)
11939         break;
11940
11941       mem_loc_result = op0;
11942       add_loc_descr (&mem_loc_result, op1);
11943       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11944       break;
11945
11946     case AND:
11947       op = DW_OP_and;
11948       goto do_binop;
11949
11950     case IOR:
11951       op = DW_OP_or;
11952       goto do_binop;
11953
11954     case XOR:
11955       op = DW_OP_xor;
11956       goto do_binop;
11957
11958     do_binop:
11959       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11960                                 VAR_INIT_STATUS_INITIALIZED);
11961       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11962                                 VAR_INIT_STATUS_INITIALIZED);
11963
11964       if (op0 == 0 || op1 == 0)
11965         break;
11966
11967       mem_loc_result = op0;
11968       add_loc_descr (&mem_loc_result, op1);
11969       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11970       break;
11971
11972     case MOD:
11973       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11974         {
11975           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11976                                         base_type_for_mode (mode, 0),
11977                                         mode, mem_mode);
11978           break;
11979         }
11980
11981       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11982                                 VAR_INIT_STATUS_INITIALIZED);
11983       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11984                                 VAR_INIT_STATUS_INITIALIZED);
11985
11986       if (op0 == 0 || op1 == 0)
11987         break;
11988
11989       mem_loc_result = op0;
11990       add_loc_descr (&mem_loc_result, op1);
11991       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11992       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11993       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11994       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11995       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11996       break;
11997
11998     case UDIV:
11999       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12000         {
12001           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12002             {
12003               op = DW_OP_div;
12004               goto do_binop;
12005             }
12006           mem_loc_result = typed_binop (DW_OP_div, rtl,
12007                                         base_type_for_mode (mode, 1),
12008                                         mode, mem_mode);
12009         }
12010       break;
12011
12012     case NOT:
12013       op = DW_OP_not;
12014       goto do_unop;
12015
12016     case ABS:
12017       op = DW_OP_abs;
12018       goto do_unop;
12019
12020     case NEG:
12021       op = DW_OP_neg;
12022       goto do_unop;
12023
12024     do_unop:
12025       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12026                                 VAR_INIT_STATUS_INITIALIZED);
12027
12028       if (op0 == 0)
12029         break;
12030
12031       mem_loc_result = op0;
12032       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12033       break;
12034
12035     case CONST_INT:
12036       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12037 #ifdef POINTERS_EXTEND_UNSIGNED
12038           || (mode == Pmode
12039               && mem_mode != VOIDmode
12040               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12041 #endif
12042           )
12043         {
12044           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12045           break;
12046         }
12047       if (!dwarf_strict
12048           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12049               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12050         {
12051           dw_die_ref type_die = base_type_for_mode (mode, 1);
12052           enum machine_mode amode;
12053           if (type_die == NULL)
12054             return NULL;
12055           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12056                                  MODE_INT, 0);
12057           if (INTVAL (rtl) >= 0
12058               && amode != BLKmode
12059               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12060               /* const DW_OP_GNU_convert <XXX> vs.
12061                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12062               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12063                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12064             {
12065               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12066               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12067               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12068               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12069               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12070               add_loc_descr (&mem_loc_result, op0);
12071               return mem_loc_result;
12072             }
12073           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12074                                           INTVAL (rtl));
12075           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12076           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12077           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12078           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12079             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12080           else
12081             {
12082               mem_loc_result->dw_loc_oprnd2.val_class
12083                 = dw_val_class_const_double;
12084               mem_loc_result->dw_loc_oprnd2.v.val_double
12085                 = shwi_to_double_int (INTVAL (rtl));
12086             }
12087         }
12088       break;
12089
12090     case CONST_DOUBLE:
12091       if (!dwarf_strict)
12092         {
12093           dw_die_ref type_die;
12094
12095           /* Note that a CONST_DOUBLE rtx could represent either an integer
12096              or a floating-point constant.  A CONST_DOUBLE is used whenever
12097              the constant requires more than one word in order to be
12098              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12099           if (mode == VOIDmode
12100               || (GET_MODE (rtl) == VOIDmode
12101                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12102             break;
12103           type_die = base_type_for_mode (mode,
12104                                          GET_MODE_CLASS (mode) == MODE_INT);
12105           if (type_die == NULL)
12106             return NULL;
12107           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12108           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12109           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12110           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12111           if (SCALAR_FLOAT_MODE_P (mode))
12112             {
12113               unsigned int length = GET_MODE_SIZE (mode);
12114               unsigned char *array
12115                   = (unsigned char*) ggc_alloc_atomic (length);
12116
12117               insert_float (rtl, array);
12118               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12119               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12120               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12121               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12122             }
12123           else
12124             {
12125               mem_loc_result->dw_loc_oprnd2.val_class
12126                 = dw_val_class_const_double;
12127               mem_loc_result->dw_loc_oprnd2.v.val_double
12128                 = rtx_to_double_int (rtl);
12129             }
12130         }
12131       break;
12132
12133     case EQ:
12134       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12135       break;
12136
12137     case GE:
12138       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12139       break;
12140
12141     case GT:
12142       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12143       break;
12144
12145     case LE:
12146       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12147       break;
12148
12149     case LT:
12150       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12151       break;
12152
12153     case NE:
12154       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12155       break;
12156
12157     case GEU:
12158       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12159       break;
12160
12161     case GTU:
12162       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12163       break;
12164
12165     case LEU:
12166       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12167       break;
12168
12169     case LTU:
12170       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12171       break;
12172
12173     case UMIN:
12174     case UMAX:
12175       if (GET_MODE_CLASS (mode) != MODE_INT)
12176         break;
12177       /* FALLTHRU */
12178     case SMIN:
12179     case SMAX:
12180       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12181       break;
12182
12183     case ZERO_EXTRACT:
12184     case SIGN_EXTRACT:
12185       if (CONST_INT_P (XEXP (rtl, 1))
12186           && CONST_INT_P (XEXP (rtl, 2))
12187           && ((unsigned) INTVAL (XEXP (rtl, 1))
12188               + (unsigned) INTVAL (XEXP (rtl, 2))
12189               <= GET_MODE_BITSIZE (mode))
12190           && GET_MODE_CLASS (mode) == MODE_INT
12191           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12192           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12193         {
12194           int shift, size;
12195           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12196                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12197           if (op0 == 0)
12198             break;
12199           if (GET_CODE (rtl) == SIGN_EXTRACT)
12200             op = DW_OP_shra;
12201           else
12202             op = DW_OP_shr;
12203           mem_loc_result = op0;
12204           size = INTVAL (XEXP (rtl, 1));
12205           shift = INTVAL (XEXP (rtl, 2));
12206           if (BITS_BIG_ENDIAN)
12207             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12208                     - shift - size;
12209           if (shift + size != (int) DWARF2_ADDR_SIZE)
12210             {
12211               add_loc_descr (&mem_loc_result,
12212                              int_loc_descriptor (DWARF2_ADDR_SIZE
12213                                                  - shift - size));
12214               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12215             }
12216           if (size != (int) DWARF2_ADDR_SIZE)
12217             {
12218               add_loc_descr (&mem_loc_result,
12219                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12220               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12221             }
12222         }
12223       break;
12224
12225     case IF_THEN_ELSE:
12226       {
12227         dw_loc_descr_ref op2, bra_node, drop_node;
12228         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12229                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12230                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12231                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12232         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12233                                   VAR_INIT_STATUS_INITIALIZED);
12234         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12235                                   VAR_INIT_STATUS_INITIALIZED);
12236         if (op0 == NULL || op1 == NULL || op2 == NULL)
12237           break;
12238
12239         mem_loc_result = op1;
12240         add_loc_descr (&mem_loc_result, op2);
12241         add_loc_descr (&mem_loc_result, op0);
12242         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12243         add_loc_descr (&mem_loc_result, bra_node);
12244         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12245         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12246         add_loc_descr (&mem_loc_result, drop_node);
12247         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12248         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12249       }
12250       break;
12251
12252     case FLOAT_EXTEND:
12253     case FLOAT_TRUNCATE:
12254     case FLOAT:
12255     case UNSIGNED_FLOAT:
12256     case FIX:
12257     case UNSIGNED_FIX:
12258       if (!dwarf_strict)
12259         {
12260           dw_die_ref type_die;
12261           dw_loc_descr_ref cvt;
12262
12263           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12264                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12265           if (op0 == NULL)
12266             break;
12267           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12268               && (GET_CODE (rtl) == FLOAT
12269                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12270                      <= DWARF2_ADDR_SIZE))
12271             {
12272               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12273                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12274               if (type_die == NULL)
12275                 break;
12276               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12277               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12278               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12279               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12280               add_loc_descr (&op0, cvt);
12281             }
12282           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12283           if (type_die == NULL)
12284             break;
12285           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12286           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12287           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12288           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12289           add_loc_descr (&op0, cvt);
12290           if (GET_MODE_CLASS (mode) == MODE_INT
12291               && (GET_CODE (rtl) == FIX
12292                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12293             {
12294               op0 = convert_descriptor_to_mode (mode, op0);
12295               if (op0 == NULL)
12296                 break;
12297             }
12298           mem_loc_result = op0;
12299         }
12300       break;
12301
12302     case CLZ:
12303     case CTZ:
12304     case FFS:
12305       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12306       break;
12307
12308     case POPCOUNT:
12309     case PARITY:
12310       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12311       break;
12312
12313     case BSWAP:
12314       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12315       break;
12316
12317     case ROTATE:
12318     case ROTATERT:
12319       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12320       break;
12321
12322     case COMPARE:
12323     case TRUNCATE:
12324       /* In theory, we could implement the above.  */
12325       /* DWARF cannot represent the unsigned compare operations
12326          natively.  */
12327     case SS_MULT:
12328     case US_MULT:
12329     case SS_DIV:
12330     case US_DIV:
12331     case SS_PLUS:
12332     case US_PLUS:
12333     case SS_MINUS:
12334     case US_MINUS:
12335     case SS_NEG:
12336     case US_NEG:
12337     case SS_ABS:
12338     case SS_ASHIFT:
12339     case US_ASHIFT:
12340     case SS_TRUNCATE:
12341     case US_TRUNCATE:
12342     case UNORDERED:
12343     case ORDERED:
12344     case UNEQ:
12345     case UNGE:
12346     case UNGT:
12347     case UNLE:
12348     case UNLT:
12349     case LTGT:
12350     case FRACT_CONVERT:
12351     case UNSIGNED_FRACT_CONVERT:
12352     case SAT_FRACT:
12353     case UNSIGNED_SAT_FRACT:
12354     case SQRT:
12355     case ASM_OPERANDS:
12356     case VEC_MERGE:
12357     case VEC_SELECT:
12358     case VEC_CONCAT:
12359     case VEC_DUPLICATE:
12360     case UNSPEC:
12361     case HIGH:
12362     case FMA:
12363     case STRICT_LOW_PART:
12364     case CONST_VECTOR:
12365     case CONST_FIXED:
12366     case CLRSB:
12367       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12368          can't express it in the debug info.  This can happen e.g. with some
12369          TLS UNSPECs.  */
12370       break;
12371
12372     case CONST_STRING:
12373       resolve_one_addr (&rtl, NULL);
12374       goto symref;
12375
12376     default:
12377 #ifdef ENABLE_CHECKING
12378       print_rtl (stderr, rtl);
12379       gcc_unreachable ();
12380 #else
12381       break;
12382 #endif
12383     }
12384
12385   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12386     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12387
12388   return mem_loc_result;
12389 }
12390
12391 /* Return a descriptor that describes the concatenation of two locations.
12392    This is typically a complex variable.  */
12393
12394 static dw_loc_descr_ref
12395 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12396 {
12397   dw_loc_descr_ref cc_loc_result = NULL;
12398   dw_loc_descr_ref x0_ref
12399     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12400   dw_loc_descr_ref x1_ref
12401     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12402
12403   if (x0_ref == 0 || x1_ref == 0)
12404     return 0;
12405
12406   cc_loc_result = x0_ref;
12407   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12408
12409   add_loc_descr (&cc_loc_result, x1_ref);
12410   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12411
12412   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12413     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12414
12415   return cc_loc_result;
12416 }
12417
12418 /* Return a descriptor that describes the concatenation of N
12419    locations.  */
12420
12421 static dw_loc_descr_ref
12422 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12423 {
12424   unsigned int i;
12425   dw_loc_descr_ref cc_loc_result = NULL;
12426   unsigned int n = XVECLEN (concatn, 0);
12427
12428   for (i = 0; i < n; ++i)
12429     {
12430       dw_loc_descr_ref ref;
12431       rtx x = XVECEXP (concatn, 0, i);
12432
12433       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12434       if (ref == NULL)
12435         return NULL;
12436
12437       add_loc_descr (&cc_loc_result, ref);
12438       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12439     }
12440
12441   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12442     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12443
12444   return cc_loc_result;
12445 }
12446
12447 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12448    for DEBUG_IMPLICIT_PTR RTL.  */
12449
12450 static dw_loc_descr_ref
12451 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12452 {
12453   dw_loc_descr_ref ret;
12454   dw_die_ref ref;
12455
12456   if (dwarf_strict)
12457     return NULL;
12458   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12459               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12460               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12461   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12462   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12463   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12464   if (ref)
12465     {
12466       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12467       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12468       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12469     }
12470   else
12471     {
12472       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12473       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12474     }
12475   return ret;
12476 }
12477
12478 /* Output a proper Dwarf location descriptor for a variable or parameter
12479    which is either allocated in a register or in a memory location.  For a
12480    register, we just generate an OP_REG and the register number.  For a
12481    memory location we provide a Dwarf postfix expression describing how to
12482    generate the (dynamic) address of the object onto the address stack.
12483
12484    MODE is mode of the decl if this loc_descriptor is going to be used in
12485    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12486    allowed, VOIDmode otherwise.
12487
12488    If we don't know how to describe it, return 0.  */
12489
12490 static dw_loc_descr_ref
12491 loc_descriptor (rtx rtl, enum machine_mode mode,
12492                 enum var_init_status initialized)
12493 {
12494   dw_loc_descr_ref loc_result = NULL;
12495
12496   switch (GET_CODE (rtl))
12497     {
12498     case SUBREG:
12499       /* The case of a subreg may arise when we have a local (register)
12500          variable or a formal (register) parameter which doesn't quite fill
12501          up an entire register.  For now, just assume that it is
12502          legitimate to make the Dwarf info refer to the whole register which
12503          contains the given subreg.  */
12504       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12505         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12506       else
12507         goto do_default;
12508       break;
12509
12510     case REG:
12511       loc_result = reg_loc_descriptor (rtl, initialized);
12512       break;
12513
12514     case MEM:
12515       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12516                                        GET_MODE (rtl), initialized);
12517       if (loc_result == NULL)
12518         loc_result = tls_mem_loc_descriptor (rtl);
12519       if (loc_result == NULL)
12520         {
12521           rtx new_rtl = avoid_constant_pool_reference (rtl);
12522           if (new_rtl != rtl)
12523             loc_result = loc_descriptor (new_rtl, mode, initialized);
12524         }
12525       break;
12526
12527     case CONCAT:
12528       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12529                                           initialized);
12530       break;
12531
12532     case CONCATN:
12533       loc_result = concatn_loc_descriptor (rtl, initialized);
12534       break;
12535
12536     case VAR_LOCATION:
12537       /* Single part.  */
12538       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12539         {
12540           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12541           if (GET_CODE (loc) == EXPR_LIST)
12542             loc = XEXP (loc, 0);
12543           loc_result = loc_descriptor (loc, mode, initialized);
12544           break;
12545         }
12546
12547       rtl = XEXP (rtl, 1);
12548       /* FALLTHRU */
12549
12550     case PARALLEL:
12551       {
12552         rtvec par_elems = XVEC (rtl, 0);
12553         int num_elem = GET_NUM_ELEM (par_elems);
12554         enum machine_mode mode;
12555         int i;
12556
12557         /* Create the first one, so we have something to add to.  */
12558         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12559                                      VOIDmode, initialized);
12560         if (loc_result == NULL)
12561           return NULL;
12562         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12563         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12564         for (i = 1; i < num_elem; i++)
12565           {
12566             dw_loc_descr_ref temp;
12567
12568             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12569                                    VOIDmode, initialized);
12570             if (temp == NULL)
12571               return NULL;
12572             add_loc_descr (&loc_result, temp);
12573             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12574             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12575           }
12576       }
12577       break;
12578
12579     case CONST_INT:
12580       if (mode != VOIDmode && mode != BLKmode)
12581         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12582                                                     INTVAL (rtl));
12583       break;
12584
12585     case CONST_DOUBLE:
12586       if (mode == VOIDmode)
12587         mode = GET_MODE (rtl);
12588
12589       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12590         {
12591           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12592
12593           /* Note that a CONST_DOUBLE rtx could represent either an integer
12594              or a floating-point constant.  A CONST_DOUBLE is used whenever
12595              the constant requires more than one word in order to be
12596              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12597           loc_result = new_loc_descr (DW_OP_implicit_value,
12598                                       GET_MODE_SIZE (mode), 0);
12599           if (SCALAR_FLOAT_MODE_P (mode))
12600             {
12601               unsigned int length = GET_MODE_SIZE (mode);
12602               unsigned char *array
12603                   = (unsigned char*) ggc_alloc_atomic (length);
12604
12605               insert_float (rtl, array);
12606               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12607               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12608               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12609               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12610             }
12611           else
12612             {
12613               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12614               loc_result->dw_loc_oprnd2.v.val_double
12615                 = rtx_to_double_int (rtl);
12616             }
12617         }
12618       break;
12619
12620     case CONST_VECTOR:
12621       if (mode == VOIDmode)
12622         mode = GET_MODE (rtl);
12623
12624       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12625         {
12626           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12627           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12628           unsigned char *array = (unsigned char *)
12629             ggc_alloc_atomic (length * elt_size);
12630           unsigned int i;
12631           unsigned char *p;
12632
12633           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12634           switch (GET_MODE_CLASS (mode))
12635             {
12636             case MODE_VECTOR_INT:
12637               for (i = 0, p = array; i < length; i++, p += elt_size)
12638                 {
12639                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12640                   double_int val = rtx_to_double_int (elt);
12641
12642                   if (elt_size <= sizeof (HOST_WIDE_INT))
12643                     insert_int (double_int_to_shwi (val), elt_size, p);
12644                   else
12645                     {
12646                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12647                       insert_double (val, p);
12648                     }
12649                 }
12650               break;
12651
12652             case MODE_VECTOR_FLOAT:
12653               for (i = 0, p = array; i < length; i++, p += elt_size)
12654                 {
12655                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12656                   insert_float (elt, p);
12657                 }
12658               break;
12659
12660             default:
12661               gcc_unreachable ();
12662             }
12663
12664           loc_result = new_loc_descr (DW_OP_implicit_value,
12665                                       length * elt_size, 0);
12666           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12667           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12668           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12669           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12670         }
12671       break;
12672
12673     case CONST:
12674       if (mode == VOIDmode
12675           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12676           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12677           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12678         {
12679           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12680           break;
12681         }
12682       /* FALLTHROUGH */
12683     case SYMBOL_REF:
12684       if (!const_ok_for_output (rtl))
12685         break;
12686     case LABEL_REF:
12687       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12688           && (dwarf_version >= 4 || !dwarf_strict))
12689         {
12690           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12691           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12692           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12693           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12694           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12695         }
12696       break;
12697
12698     case DEBUG_IMPLICIT_PTR:
12699       loc_result = implicit_ptr_descriptor (rtl, 0);
12700       break;
12701
12702     case PLUS:
12703       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12704           && CONST_INT_P (XEXP (rtl, 1)))
12705         {
12706           loc_result
12707             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12708           break;
12709         }
12710       /* FALLTHRU */
12711     do_default:
12712     default:
12713       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12714            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12715            && dwarf_version >= 4)
12716           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12717         {
12718           /* Value expression.  */
12719           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12720           if (loc_result)
12721             add_loc_descr (&loc_result,
12722                            new_loc_descr (DW_OP_stack_value, 0, 0));
12723         }
12724       break;
12725     }
12726
12727   return loc_result;
12728 }
12729
12730 /* We need to figure out what section we should use as the base for the
12731    address ranges where a given location is valid.
12732    1. If this particular DECL has a section associated with it, use that.
12733    2. If this function has a section associated with it, use that.
12734    3. Otherwise, use the text section.
12735    XXX: If you split a variable across multiple sections, we won't notice.  */
12736
12737 static const char *
12738 secname_for_decl (const_tree decl)
12739 {
12740   const char *secname;
12741
12742   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12743     {
12744       tree sectree = DECL_SECTION_NAME (decl);
12745       secname = TREE_STRING_POINTER (sectree);
12746     }
12747   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12748     {
12749       tree sectree = DECL_SECTION_NAME (current_function_decl);
12750       secname = TREE_STRING_POINTER (sectree);
12751     }
12752   else if (cfun && in_cold_section_p)
12753     secname = crtl->subsections.cold_section_label;
12754   else
12755     secname = text_section_label;
12756
12757   return secname;
12758 }
12759
12760 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12761
12762 static bool
12763 decl_by_reference_p (tree decl)
12764 {
12765   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12766            || TREE_CODE (decl) == VAR_DECL)
12767           && DECL_BY_REFERENCE (decl));
12768 }
12769
12770 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12771    for VARLOC.  */
12772
12773 static dw_loc_descr_ref
12774 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12775                enum var_init_status initialized)
12776 {
12777   int have_address = 0;
12778   dw_loc_descr_ref descr;
12779   enum machine_mode mode;
12780
12781   if (want_address != 2)
12782     {
12783       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12784       /* Single part.  */
12785       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12786         {
12787           varloc = PAT_VAR_LOCATION_LOC (varloc);
12788           if (GET_CODE (varloc) == EXPR_LIST)
12789             varloc = XEXP (varloc, 0);
12790           mode = GET_MODE (varloc);
12791           if (MEM_P (varloc))
12792             {
12793               rtx addr = XEXP (varloc, 0);
12794               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12795                                           mode, initialized);
12796               if (descr)
12797                 have_address = 1;
12798               else
12799                 {
12800                   rtx x = avoid_constant_pool_reference (varloc);
12801                   if (x != varloc)
12802                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12803                                                 initialized);
12804                 }
12805             }
12806           else
12807             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12808         }
12809       else
12810         return 0;
12811     }
12812   else
12813     {
12814       if (GET_CODE (varloc) == VAR_LOCATION)
12815         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12816       else
12817         mode = DECL_MODE (loc);
12818       descr = loc_descriptor (varloc, mode, initialized);
12819       have_address = 1;
12820     }
12821
12822   if (!descr)
12823     return 0;
12824
12825   if (want_address == 2 && !have_address
12826       && (dwarf_version >= 4 || !dwarf_strict))
12827     {
12828       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12829         {
12830           expansion_failed (loc, NULL_RTX,
12831                             "DWARF address size mismatch");
12832           return 0;
12833         }
12834       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12835       have_address = 1;
12836     }
12837   /* Show if we can't fill the request for an address.  */
12838   if (want_address && !have_address)
12839     {
12840       expansion_failed (loc, NULL_RTX,
12841                         "Want address and only have value");
12842       return 0;
12843     }
12844
12845   /* If we've got an address and don't want one, dereference.  */
12846   if (!want_address && have_address)
12847     {
12848       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12849       enum dwarf_location_atom op;
12850
12851       if (size > DWARF2_ADDR_SIZE || size == -1)
12852         {
12853           expansion_failed (loc, NULL_RTX,
12854                             "DWARF address size mismatch");
12855           return 0;
12856         }
12857       else if (size == DWARF2_ADDR_SIZE)
12858         op = DW_OP_deref;
12859       else
12860         op = DW_OP_deref_size;
12861
12862       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12863     }
12864
12865   return descr;
12866 }
12867
12868 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12869    if it is not possible.  */
12870
12871 static dw_loc_descr_ref
12872 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12873 {
12874   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12875     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12876   else if (dwarf_version >= 3 || !dwarf_strict)
12877     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12878   else
12879     return NULL;
12880 }
12881
12882 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12883    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12884
12885 static dw_loc_descr_ref
12886 dw_sra_loc_expr (tree decl, rtx loc)
12887 {
12888   rtx p;
12889   unsigned int padsize = 0;
12890   dw_loc_descr_ref descr, *descr_tail;
12891   unsigned HOST_WIDE_INT decl_size;
12892   rtx varloc;
12893   enum var_init_status initialized;
12894
12895   if (DECL_SIZE (decl) == NULL
12896       || !host_integerp (DECL_SIZE (decl), 1))
12897     return NULL;
12898
12899   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12900   descr = NULL;
12901   descr_tail = &descr;
12902
12903   for (p = loc; p; p = XEXP (p, 1))
12904     {
12905       unsigned int bitsize = decl_piece_bitsize (p);
12906       rtx loc_note = *decl_piece_varloc_ptr (p);
12907       dw_loc_descr_ref cur_descr;
12908       dw_loc_descr_ref *tail, last = NULL;
12909       unsigned int opsize = 0;
12910
12911       if (loc_note == NULL_RTX
12912           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12913         {
12914           padsize += bitsize;
12915           continue;
12916         }
12917       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12918       varloc = NOTE_VAR_LOCATION (loc_note);
12919       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12920       if (cur_descr == NULL)
12921         {
12922           padsize += bitsize;
12923           continue;
12924         }
12925
12926       /* Check that cur_descr either doesn't use
12927          DW_OP_*piece operations, or their sum is equal
12928          to bitsize.  Otherwise we can't embed it.  */
12929       for (tail = &cur_descr; *tail != NULL;
12930            tail = &(*tail)->dw_loc_next)
12931         if ((*tail)->dw_loc_opc == DW_OP_piece)
12932           {
12933             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12934                       * BITS_PER_UNIT;
12935             last = *tail;
12936           }
12937         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12938           {
12939             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12940             last = *tail;
12941           }
12942
12943       if (last != NULL && opsize != bitsize)
12944         {
12945           padsize += bitsize;
12946           continue;
12947         }
12948
12949       /* If there is a hole, add DW_OP_*piece after empty DWARF
12950          expression, which means that those bits are optimized out.  */
12951       if (padsize)
12952         {
12953           if (padsize > decl_size)
12954             return NULL;
12955           decl_size -= padsize;
12956           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12957           if (*descr_tail == NULL)
12958             return NULL;
12959           descr_tail = &(*descr_tail)->dw_loc_next;
12960           padsize = 0;
12961         }
12962       *descr_tail = cur_descr;
12963       descr_tail = tail;
12964       if (bitsize > decl_size)
12965         return NULL;
12966       decl_size -= bitsize;
12967       if (last == NULL)
12968         {
12969           HOST_WIDE_INT offset = 0;
12970           if (GET_CODE (varloc) == VAR_LOCATION
12971               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12972             {
12973               varloc = PAT_VAR_LOCATION_LOC (varloc);
12974               if (GET_CODE (varloc) == EXPR_LIST)
12975                 varloc = XEXP (varloc, 0);
12976             }
12977           do 
12978             {
12979               if (GET_CODE (varloc) == CONST
12980                   || GET_CODE (varloc) == SIGN_EXTEND
12981                   || GET_CODE (varloc) == ZERO_EXTEND)
12982                 varloc = XEXP (varloc, 0);
12983               else if (GET_CODE (varloc) == SUBREG)
12984                 varloc = SUBREG_REG (varloc);
12985               else
12986                 break;
12987             }
12988           while (1);
12989           /* DW_OP_bit_size offset should be zero for register
12990              or implicit location descriptions and empty location
12991              descriptions, but for memory addresses needs big endian
12992              adjustment.  */
12993           if (MEM_P (varloc))
12994             {
12995               unsigned HOST_WIDE_INT memsize
12996                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12997               if (memsize != bitsize)
12998                 {
12999                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13000                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13001                     return NULL;
13002                   if (memsize < bitsize)
13003                     return NULL;
13004                   if (BITS_BIG_ENDIAN)
13005                     offset = memsize - bitsize;
13006                 }
13007             }
13008
13009           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13010           if (*descr_tail == NULL)
13011             return NULL;
13012           descr_tail = &(*descr_tail)->dw_loc_next;
13013         }
13014     }
13015
13016   /* If there were any non-empty expressions, add padding till the end of
13017      the decl.  */
13018   if (descr != NULL && decl_size != 0)
13019     {
13020       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13021       if (*descr_tail == NULL)
13022         return NULL;
13023     }
13024   return descr;
13025 }
13026
13027 /* Return the dwarf representation of the location list LOC_LIST of
13028    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13029    function.  */
13030
13031 static dw_loc_list_ref
13032 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13033 {
13034   const char *endname, *secname;
13035   rtx varloc;
13036   enum var_init_status initialized;
13037   struct var_loc_node *node;
13038   dw_loc_descr_ref descr;
13039   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13040   dw_loc_list_ref list = NULL;
13041   dw_loc_list_ref *listp = &list;
13042
13043   /* Now that we know what section we are using for a base,
13044      actually construct the list of locations.
13045      The first location information is what is passed to the
13046      function that creates the location list, and the remaining
13047      locations just get added on to that list.
13048      Note that we only know the start address for a location
13049      (IE location changes), so to build the range, we use
13050      the range [current location start, next location start].
13051      This means we have to special case the last node, and generate
13052      a range of [last location start, end of function label].  */
13053
13054   secname = secname_for_decl (decl);
13055
13056   for (node = loc_list->first; node; node = node->next)
13057     if (GET_CODE (node->loc) == EXPR_LIST
13058         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13059       {
13060         if (GET_CODE (node->loc) == EXPR_LIST)
13061           {
13062             /* This requires DW_OP_{,bit_}piece, which is not usable
13063                inside DWARF expressions.  */
13064             if (want_address != 2)
13065               continue;
13066             descr = dw_sra_loc_expr (decl, node->loc);
13067             if (descr == NULL)
13068               continue;
13069           }
13070         else
13071           {
13072             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13073             varloc = NOTE_VAR_LOCATION (node->loc);
13074             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13075           }
13076         if (descr)
13077           {
13078             bool range_across_switch = false;
13079             /* If section switch happens in between node->label
13080                and node->next->label (or end of function) and
13081                we can't emit it as a single entry list,
13082                emit two ranges, first one ending at the end
13083                of first partition and second one starting at the
13084                beginning of second partition.  */
13085             if (node == loc_list->last_before_switch
13086                 && (node != loc_list->first || loc_list->first->next)
13087                 && current_function_decl)
13088               {
13089                 endname = cfun->fde->dw_fde_end;
13090                 range_across_switch = true;
13091               }
13092             /* The variable has a location between NODE->LABEL and
13093                NODE->NEXT->LABEL.  */
13094             else if (node->next)
13095               endname = node->next->label;
13096             /* If the variable has a location at the last label
13097                it keeps its location until the end of function.  */
13098             else if (!current_function_decl)
13099               endname = text_end_label;
13100             else
13101               {
13102                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13103                                              current_function_funcdef_no);
13104                 endname = ggc_strdup (label_id);
13105               }
13106
13107             *listp = new_loc_list (descr, node->label, endname, secname);
13108             if (TREE_CODE (decl) == PARM_DECL
13109                 && node == loc_list->first
13110                 && GET_CODE (node->loc) == NOTE
13111                 && strcmp (node->label, endname) == 0)
13112               (*listp)->force = true;
13113             listp = &(*listp)->dw_loc_next;
13114
13115             if (range_across_switch)
13116               {
13117                 if (GET_CODE (node->loc) == EXPR_LIST)
13118                   descr = dw_sra_loc_expr (decl, node->loc);
13119                 else
13120                   {
13121                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13122                     varloc = NOTE_VAR_LOCATION (node->loc);
13123                     descr = dw_loc_list_1 (decl, varloc, want_address,
13124                                            initialized);
13125                   }
13126                 gcc_assert (descr);
13127                 /* The variable has a location between NODE->LABEL and
13128                    NODE->NEXT->LABEL.  */
13129                 if (node->next)
13130                   endname = node->next->label;
13131                 else
13132                   endname = cfun->fde->dw_fde_second_end;
13133                 *listp = new_loc_list (descr,
13134                                        cfun->fde->dw_fde_second_begin,
13135                                        endname, secname);
13136                 listp = &(*listp)->dw_loc_next;
13137               }
13138           }
13139       }
13140
13141   /* Try to avoid the overhead of a location list emitting a location
13142      expression instead, but only if we didn't have more than one
13143      location entry in the first place.  If some entries were not
13144      representable, we don't want to pretend a single entry that was
13145      applies to the entire scope in which the variable is
13146      available.  */
13147   if (list && loc_list->first->next)
13148     gen_llsym (list);
13149
13150   return list;
13151 }
13152
13153 /* Return if the loc_list has only single element and thus can be represented
13154    as location description.   */
13155
13156 static bool
13157 single_element_loc_list_p (dw_loc_list_ref list)
13158 {
13159   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13160   return !list->ll_symbol;
13161 }
13162
13163 /* To each location in list LIST add loc descr REF.  */
13164
13165 static void
13166 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13167 {
13168   dw_loc_descr_ref copy;
13169   add_loc_descr (&list->expr, ref);
13170   list = list->dw_loc_next;
13171   while (list)
13172     {
13173       copy = ggc_alloc_dw_loc_descr_node ();
13174       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13175       add_loc_descr (&list->expr, copy);
13176       while (copy->dw_loc_next)
13177         {
13178           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13179           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13180           copy->dw_loc_next = new_copy;
13181           copy = new_copy;
13182         }
13183       list = list->dw_loc_next;
13184     }
13185 }
13186
13187 /* Given two lists RET and LIST
13188    produce location list that is result of adding expression in LIST
13189    to expression in RET on each possition in program.
13190    Might be destructive on both RET and LIST.
13191
13192    TODO: We handle only simple cases of RET or LIST having at most one
13193    element. General case would inolve sorting the lists in program order
13194    and merging them that will need some additional work.
13195    Adding that will improve quality of debug info especially for SRA-ed
13196    structures.  */
13197
13198 static void
13199 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13200 {
13201   if (!list)
13202     return;
13203   if (!*ret)
13204     {
13205       *ret = list;
13206       return;
13207     }
13208   if (!list->dw_loc_next)
13209     {
13210       add_loc_descr_to_each (*ret, list->expr);
13211       return;
13212     }
13213   if (!(*ret)->dw_loc_next)
13214     {
13215       add_loc_descr_to_each (list, (*ret)->expr);
13216       *ret = list;
13217       return;
13218     }
13219   expansion_failed (NULL_TREE, NULL_RTX,
13220                     "Don't know how to merge two non-trivial"
13221                     " location lists.\n");
13222   *ret = NULL;
13223   return;
13224 }
13225
13226 /* LOC is constant expression.  Try a luck, look it up in constant
13227    pool and return its loc_descr of its address.  */
13228
13229 static dw_loc_descr_ref
13230 cst_pool_loc_descr (tree loc)
13231 {
13232   /* Get an RTL for this, if something has been emitted.  */
13233   rtx rtl = lookup_constant_def (loc);
13234
13235   if (!rtl || !MEM_P (rtl))
13236     {
13237       gcc_assert (!rtl);
13238       return 0;
13239     }
13240   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13241
13242   /* TODO: We might get more coverage if we was actually delaying expansion
13243      of all expressions till end of compilation when constant pools are fully
13244      populated.  */
13245   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13246     {
13247       expansion_failed (loc, NULL_RTX,
13248                         "CST value in contant pool but not marked.");
13249       return 0;
13250     }
13251   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13252                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13253 }
13254
13255 /* Return dw_loc_list representing address of addr_expr LOC
13256    by looking for innder INDIRECT_REF expression and turing it
13257    into simple arithmetics.  */
13258
13259 static dw_loc_list_ref
13260 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13261 {
13262   tree obj, offset;
13263   HOST_WIDE_INT bitsize, bitpos, bytepos;
13264   enum machine_mode mode;
13265   int volatilep;
13266   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13267   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13268
13269   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13270                              &bitsize, &bitpos, &offset, &mode,
13271                              &unsignedp, &volatilep, false);
13272   STRIP_NOPS (obj);
13273   if (bitpos % BITS_PER_UNIT)
13274     {
13275       expansion_failed (loc, NULL_RTX, "bitfield access");
13276       return 0;
13277     }
13278   if (!INDIRECT_REF_P (obj))
13279     {
13280       expansion_failed (obj,
13281                         NULL_RTX, "no indirect ref in inner refrence");
13282       return 0;
13283     }
13284   if (!offset && !bitpos)
13285     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13286   else if (toplev
13287            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13288            && (dwarf_version >= 4 || !dwarf_strict))
13289     {
13290       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13291       if (!list_ret)
13292         return 0;
13293       if (offset)
13294         {
13295           /* Variable offset.  */
13296           list_ret1 = loc_list_from_tree (offset, 0);
13297           if (list_ret1 == 0)
13298             return 0;
13299           add_loc_list (&list_ret, list_ret1);
13300           if (!list_ret)
13301             return 0;
13302           add_loc_descr_to_each (list_ret,
13303                                  new_loc_descr (DW_OP_plus, 0, 0));
13304         }
13305       bytepos = bitpos / BITS_PER_UNIT;
13306       if (bytepos > 0)
13307         add_loc_descr_to_each (list_ret,
13308                                new_loc_descr (DW_OP_plus_uconst,
13309                                               bytepos, 0));
13310       else if (bytepos < 0)
13311         loc_list_plus_const (list_ret, bytepos);
13312       add_loc_descr_to_each (list_ret,
13313                              new_loc_descr (DW_OP_stack_value, 0, 0));
13314     }
13315   return list_ret;
13316 }
13317
13318
13319 /* Generate Dwarf location list representing LOC.
13320    If WANT_ADDRESS is false, expression computing LOC will be computed
13321    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13322    if WANT_ADDRESS is 2, expression computing address useable in location
13323      will be returned (i.e. DW_OP_reg can be used
13324      to refer to register values).  */
13325
13326 static dw_loc_list_ref
13327 loc_list_from_tree (tree loc, int want_address)
13328 {
13329   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13330   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13331   int have_address = 0;
13332   enum dwarf_location_atom op;
13333
13334   /* ??? Most of the time we do not take proper care for sign/zero
13335      extending the values properly.  Hopefully this won't be a real
13336      problem...  */
13337
13338   switch (TREE_CODE (loc))
13339     {
13340     case ERROR_MARK:
13341       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13342       return 0;
13343
13344     case PLACEHOLDER_EXPR:
13345       /* This case involves extracting fields from an object to determine the
13346          position of other fields.  We don't try to encode this here.  The
13347          only user of this is Ada, which encodes the needed information using
13348          the names of types.  */
13349       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13350       return 0;
13351
13352     case CALL_EXPR:
13353       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13354       /* There are no opcodes for these operations.  */
13355       return 0;
13356
13357     case PREINCREMENT_EXPR:
13358     case PREDECREMENT_EXPR:
13359     case POSTINCREMENT_EXPR:
13360     case POSTDECREMENT_EXPR:
13361       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13362       /* There are no opcodes for these operations.  */
13363       return 0;
13364
13365     case ADDR_EXPR:
13366       /* If we already want an address, see if there is INDIRECT_REF inside
13367          e.g. for &this->field.  */
13368       if (want_address)
13369         {
13370           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13371                        (loc, want_address == 2);
13372           if (list_ret)
13373             have_address = 1;
13374           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13375                    && (ret = cst_pool_loc_descr (loc)))
13376             have_address = 1;
13377         }
13378         /* Otherwise, process the argument and look for the address.  */
13379       if (!list_ret && !ret)
13380         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13381       else
13382         {
13383           if (want_address)
13384             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13385           return NULL;
13386         }
13387       break;
13388
13389     case VAR_DECL:
13390       if (DECL_THREAD_LOCAL_P (loc))
13391         {
13392           rtx rtl;
13393           enum dwarf_location_atom first_op;
13394           enum dwarf_location_atom second_op;
13395           bool dtprel = false;
13396
13397           if (targetm.have_tls)
13398             {
13399               /* If this is not defined, we have no way to emit the
13400                  data.  */
13401               if (!targetm.asm_out.output_dwarf_dtprel)
13402                 return 0;
13403
13404                /* The way DW_OP_GNU_push_tls_address is specified, we
13405                   can only look up addresses of objects in the current
13406                   module.  We used DW_OP_addr as first op, but that's
13407                   wrong, because DW_OP_addr is relocated by the debug
13408                   info consumer, while DW_OP_GNU_push_tls_address
13409                   operand shouldn't be.  */
13410               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13411                 return 0;
13412               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13413               dtprel = true;
13414               second_op = DW_OP_GNU_push_tls_address;
13415             }
13416           else
13417             {
13418               if (!targetm.emutls.debug_form_tls_address
13419                   || !(dwarf_version >= 3 || !dwarf_strict))
13420                 return 0;
13421               /* We stuffed the control variable into the DECL_VALUE_EXPR
13422                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13423                  no longer appear in gimple code.  We used the control
13424                  variable in specific so that we could pick it up here.  */
13425               loc = DECL_VALUE_EXPR (loc);
13426               first_op = DW_OP_addr;
13427               second_op = DW_OP_form_tls_address;
13428             }
13429
13430           rtl = rtl_for_decl_location (loc);
13431           if (rtl == NULL_RTX)
13432             return 0;
13433
13434           if (!MEM_P (rtl))
13435             return 0;
13436           rtl = XEXP (rtl, 0);
13437           if (! CONSTANT_P (rtl))
13438             return 0;
13439
13440           ret = new_loc_descr (first_op, 0, 0);
13441           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13442           ret->dw_loc_oprnd1.v.val_addr = rtl;
13443           ret->dtprel = dtprel;
13444
13445           ret1 = new_loc_descr (second_op, 0, 0);
13446           add_loc_descr (&ret, ret1);
13447
13448           have_address = 1;
13449           break;
13450         }
13451       /* FALLTHRU */
13452
13453     case PARM_DECL:
13454     case RESULT_DECL:
13455       if (DECL_HAS_VALUE_EXPR_P (loc))
13456         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13457                                    want_address);
13458       /* FALLTHRU */
13459
13460     case FUNCTION_DECL:
13461       {
13462         rtx rtl;
13463         var_loc_list *loc_list = lookup_decl_loc (loc);
13464
13465         if (loc_list && loc_list->first)
13466           {
13467             list_ret = dw_loc_list (loc_list, loc, want_address);
13468             have_address = want_address != 0;
13469             break;
13470           }
13471         rtl = rtl_for_decl_location (loc);
13472         if (rtl == NULL_RTX)
13473           {
13474             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13475             return 0;
13476           }
13477         else if (CONST_INT_P (rtl))
13478           {
13479             HOST_WIDE_INT val = INTVAL (rtl);
13480             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13481               val &= GET_MODE_MASK (DECL_MODE (loc));
13482             ret = int_loc_descriptor (val);
13483           }
13484         else if (GET_CODE (rtl) == CONST_STRING)
13485           {
13486             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13487             return 0;
13488           }
13489         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13490           {
13491             ret = new_loc_descr (DW_OP_addr, 0, 0);
13492             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13493             ret->dw_loc_oprnd1.v.val_addr = rtl;
13494           }
13495         else
13496           {
13497             enum machine_mode mode, mem_mode;
13498
13499             /* Certain constructs can only be represented at top-level.  */
13500             if (want_address == 2)
13501               {
13502                 ret = loc_descriptor (rtl, VOIDmode,
13503                                       VAR_INIT_STATUS_INITIALIZED);
13504                 have_address = 1;
13505               }
13506             else
13507               {
13508                 mode = GET_MODE (rtl);
13509                 mem_mode = VOIDmode;
13510                 if (MEM_P (rtl))
13511                   {
13512                     mem_mode = mode;
13513                     mode = get_address_mode (rtl);
13514                     rtl = XEXP (rtl, 0);
13515                     have_address = 1;
13516                   }
13517                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13518                                           VAR_INIT_STATUS_INITIALIZED);
13519               }
13520             if (!ret)
13521               expansion_failed (loc, rtl,
13522                                 "failed to produce loc descriptor for rtl");
13523           }
13524       }
13525       break;
13526
13527     case MEM_REF:
13528       /* ??? FIXME.  */
13529       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13530         return 0;
13531       /* Fallthru.  */
13532     case INDIRECT_REF:
13533       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13534       have_address = 1;
13535       break;
13536
13537     case COMPOUND_EXPR:
13538       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13539
13540     CASE_CONVERT:
13541     case VIEW_CONVERT_EXPR:
13542     case SAVE_EXPR:
13543     case MODIFY_EXPR:
13544       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13545
13546     case COMPONENT_REF:
13547     case BIT_FIELD_REF:
13548     case ARRAY_REF:
13549     case ARRAY_RANGE_REF:
13550     case REALPART_EXPR:
13551     case IMAGPART_EXPR:
13552       {
13553         tree obj, offset;
13554         HOST_WIDE_INT bitsize, bitpos, bytepos;
13555         enum machine_mode mode;
13556         int volatilep;
13557         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13558
13559         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13560                                    &unsignedp, &volatilep, false);
13561
13562         gcc_assert (obj != loc);
13563
13564         list_ret = loc_list_from_tree (obj,
13565                                        want_address == 2
13566                                        && !bitpos && !offset ? 2 : 1);
13567         /* TODO: We can extract value of the small expression via shifting even
13568            for nonzero bitpos.  */
13569         if (list_ret == 0)
13570           return 0;
13571         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13572           {
13573             expansion_failed (loc, NULL_RTX,
13574                               "bitfield access");
13575             return 0;
13576           }
13577
13578         if (offset != NULL_TREE)
13579           {
13580             /* Variable offset.  */
13581             list_ret1 = loc_list_from_tree (offset, 0);
13582             if (list_ret1 == 0)
13583               return 0;
13584             add_loc_list (&list_ret, list_ret1);
13585             if (!list_ret)
13586               return 0;
13587             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13588           }
13589
13590         bytepos = bitpos / BITS_PER_UNIT;
13591         if (bytepos > 0)
13592           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13593         else if (bytepos < 0)
13594           loc_list_plus_const (list_ret, bytepos);
13595
13596         have_address = 1;
13597         break;
13598       }
13599
13600     case INTEGER_CST:
13601       if ((want_address || !host_integerp (loc, 0))
13602           && (ret = cst_pool_loc_descr (loc)))
13603         have_address = 1;
13604       else if (want_address == 2
13605                && host_integerp (loc, 0)
13606                && (ret = address_of_int_loc_descriptor
13607                            (int_size_in_bytes (TREE_TYPE (loc)),
13608                             tree_low_cst (loc, 0))))
13609         have_address = 1;
13610       else if (host_integerp (loc, 0))
13611         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13612       else
13613         {
13614           expansion_failed (loc, NULL_RTX,
13615                             "Integer operand is not host integer");
13616           return 0;
13617         }
13618       break;
13619
13620     case CONSTRUCTOR:
13621     case REAL_CST:
13622     case STRING_CST:
13623     case COMPLEX_CST:
13624       if ((ret = cst_pool_loc_descr (loc)))
13625         have_address = 1;
13626       else
13627       /* We can construct small constants here using int_loc_descriptor.  */
13628         expansion_failed (loc, NULL_RTX,
13629                           "constructor or constant not in constant pool");
13630       break;
13631
13632     case TRUTH_AND_EXPR:
13633     case TRUTH_ANDIF_EXPR:
13634     case BIT_AND_EXPR:
13635       op = DW_OP_and;
13636       goto do_binop;
13637
13638     case TRUTH_XOR_EXPR:
13639     case BIT_XOR_EXPR:
13640       op = DW_OP_xor;
13641       goto do_binop;
13642
13643     case TRUTH_OR_EXPR:
13644     case TRUTH_ORIF_EXPR:
13645     case BIT_IOR_EXPR:
13646       op = DW_OP_or;
13647       goto do_binop;
13648
13649     case FLOOR_DIV_EXPR:
13650     case CEIL_DIV_EXPR:
13651     case ROUND_DIV_EXPR:
13652     case TRUNC_DIV_EXPR:
13653       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13654         return 0;
13655       op = DW_OP_div;
13656       goto do_binop;
13657
13658     case MINUS_EXPR:
13659       op = DW_OP_minus;
13660       goto do_binop;
13661
13662     case FLOOR_MOD_EXPR:
13663     case CEIL_MOD_EXPR:
13664     case ROUND_MOD_EXPR:
13665     case TRUNC_MOD_EXPR:
13666       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13667         {
13668           op = DW_OP_mod;
13669           goto do_binop;
13670         }
13671       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13672       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13673       if (list_ret == 0 || list_ret1 == 0)
13674         return 0;
13675
13676       add_loc_list (&list_ret, list_ret1);
13677       if (list_ret == 0)
13678         return 0;
13679       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13680       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13681       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13682       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13683       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13684       break;
13685
13686     case MULT_EXPR:
13687       op = DW_OP_mul;
13688       goto do_binop;
13689
13690     case LSHIFT_EXPR:
13691       op = DW_OP_shl;
13692       goto do_binop;
13693
13694     case RSHIFT_EXPR:
13695       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13696       goto do_binop;
13697
13698     case POINTER_PLUS_EXPR:
13699     case PLUS_EXPR:
13700       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13701         {
13702           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13703           if (list_ret == 0)
13704             return 0;
13705
13706           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13707           break;
13708         }
13709
13710       op = DW_OP_plus;
13711       goto do_binop;
13712
13713     case LE_EXPR:
13714       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13715         return 0;
13716
13717       op = DW_OP_le;
13718       goto do_binop;
13719
13720     case GE_EXPR:
13721       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13722         return 0;
13723
13724       op = DW_OP_ge;
13725       goto do_binop;
13726
13727     case LT_EXPR:
13728       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13729         return 0;
13730
13731       op = DW_OP_lt;
13732       goto do_binop;
13733
13734     case GT_EXPR:
13735       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13736         return 0;
13737
13738       op = DW_OP_gt;
13739       goto do_binop;
13740
13741     case EQ_EXPR:
13742       op = DW_OP_eq;
13743       goto do_binop;
13744
13745     case NE_EXPR:
13746       op = DW_OP_ne;
13747       goto do_binop;
13748
13749     do_binop:
13750       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13751       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13752       if (list_ret == 0 || list_ret1 == 0)
13753         return 0;
13754
13755       add_loc_list (&list_ret, list_ret1);
13756       if (list_ret == 0)
13757         return 0;
13758       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13759       break;
13760
13761     case TRUTH_NOT_EXPR:
13762     case BIT_NOT_EXPR:
13763       op = DW_OP_not;
13764       goto do_unop;
13765
13766     case ABS_EXPR:
13767       op = DW_OP_abs;
13768       goto do_unop;
13769
13770     case NEGATE_EXPR:
13771       op = DW_OP_neg;
13772       goto do_unop;
13773
13774     do_unop:
13775       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13776       if (list_ret == 0)
13777         return 0;
13778
13779       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13780       break;
13781
13782     case MIN_EXPR:
13783     case MAX_EXPR:
13784       {
13785         const enum tree_code code =
13786           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13787
13788         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13789                       build2 (code, integer_type_node,
13790                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13791                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13792       }
13793
13794       /* ... fall through ...  */
13795
13796     case COND_EXPR:
13797       {
13798         dw_loc_descr_ref lhs
13799           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13800         dw_loc_list_ref rhs
13801           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13802         dw_loc_descr_ref bra_node, jump_node, tmp;
13803
13804         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13805         if (list_ret == 0 || lhs == 0 || rhs == 0)
13806           return 0;
13807
13808         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13809         add_loc_descr_to_each (list_ret, bra_node);
13810
13811         add_loc_list (&list_ret, rhs);
13812         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13813         add_loc_descr_to_each (list_ret, jump_node);
13814
13815         add_loc_descr_to_each (list_ret, lhs);
13816         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13817         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13818
13819         /* ??? Need a node to point the skip at.  Use a nop.  */
13820         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13821         add_loc_descr_to_each (list_ret, tmp);
13822         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13823         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13824       }
13825       break;
13826
13827     case FIX_TRUNC_EXPR:
13828       return 0;
13829
13830     default:
13831       /* Leave front-end specific codes as simply unknown.  This comes
13832          up, for instance, with the C STMT_EXPR.  */
13833       if ((unsigned int) TREE_CODE (loc)
13834           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13835         {
13836           expansion_failed (loc, NULL_RTX,
13837                             "language specific tree node");
13838           return 0;
13839         }
13840
13841 #ifdef ENABLE_CHECKING
13842       /* Otherwise this is a generic code; we should just lists all of
13843          these explicitly.  We forgot one.  */
13844       gcc_unreachable ();
13845 #else
13846       /* In a release build, we want to degrade gracefully: better to
13847          generate incomplete debugging information than to crash.  */
13848       return NULL;
13849 #endif
13850     }
13851
13852   if (!ret && !list_ret)
13853     return 0;
13854
13855   if (want_address == 2 && !have_address
13856       && (dwarf_version >= 4 || !dwarf_strict))
13857     {
13858       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13859         {
13860           expansion_failed (loc, NULL_RTX,
13861                             "DWARF address size mismatch");
13862           return 0;
13863         }
13864       if (ret)
13865         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13866       else
13867         add_loc_descr_to_each (list_ret,
13868                                new_loc_descr (DW_OP_stack_value, 0, 0));
13869       have_address = 1;
13870     }
13871   /* Show if we can't fill the request for an address.  */
13872   if (want_address && !have_address)
13873     {
13874       expansion_failed (loc, NULL_RTX,
13875                         "Want address and only have value");
13876       return 0;
13877     }
13878
13879   gcc_assert (!ret || !list_ret);
13880
13881   /* If we've got an address and don't want one, dereference.  */
13882   if (!want_address && have_address)
13883     {
13884       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13885
13886       if (size > DWARF2_ADDR_SIZE || size == -1)
13887         {
13888           expansion_failed (loc, NULL_RTX,
13889                             "DWARF address size mismatch");
13890           return 0;
13891         }
13892       else if (size == DWARF2_ADDR_SIZE)
13893         op = DW_OP_deref;
13894       else
13895         op = DW_OP_deref_size;
13896
13897       if (ret)
13898         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13899       else
13900         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13901     }
13902   if (ret)
13903     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13904
13905   return list_ret;
13906 }
13907
13908 /* Same as above but return only single location expression.  */
13909 static dw_loc_descr_ref
13910 loc_descriptor_from_tree (tree loc, int want_address)
13911 {
13912   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13913   if (!ret)
13914     return NULL;
13915   if (ret->dw_loc_next)
13916     {
13917       expansion_failed (loc, NULL_RTX,
13918                         "Location list where only loc descriptor needed");
13919       return NULL;
13920     }
13921   return ret->expr;
13922 }
13923
13924 /* Given a value, round it up to the lowest multiple of `boundary'
13925    which is not less than the value itself.  */
13926
13927 static inline HOST_WIDE_INT
13928 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13929 {
13930   return (((value + boundary - 1) / boundary) * boundary);
13931 }
13932
13933 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13934    pointer to the declared type for the relevant field variable, or return
13935    `integer_type_node' if the given node turns out to be an
13936    ERROR_MARK node.  */
13937
13938 static inline tree
13939 field_type (const_tree decl)
13940 {
13941   tree type;
13942
13943   if (TREE_CODE (decl) == ERROR_MARK)
13944     return integer_type_node;
13945
13946   type = DECL_BIT_FIELD_TYPE (decl);
13947   if (type == NULL_TREE)
13948     type = TREE_TYPE (decl);
13949
13950   return type;
13951 }
13952
13953 /* Given a pointer to a tree node, return the alignment in bits for
13954    it, or else return BITS_PER_WORD if the node actually turns out to
13955    be an ERROR_MARK node.  */
13956
13957 static inline unsigned
13958 simple_type_align_in_bits (const_tree type)
13959 {
13960   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13961 }
13962
13963 static inline unsigned
13964 simple_decl_align_in_bits (const_tree decl)
13965 {
13966   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13967 }
13968
13969 /* Return the result of rounding T up to ALIGN.  */
13970
13971 static inline double_int
13972 round_up_to_align (double_int t, unsigned int align)
13973 {
13974   double_int alignd = uhwi_to_double_int (align);
13975   t = double_int_add (t, alignd);
13976   t = double_int_add (t, double_int_minus_one);
13977   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13978   t = double_int_mul (t, alignd);
13979   return t;
13980 }
13981
13982 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13983    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13984    or return 0 if we are unable to determine what that offset is, either
13985    because the argument turns out to be a pointer to an ERROR_MARK node, or
13986    because the offset is actually variable.  (We can't handle the latter case
13987    just yet).  */
13988
13989 static HOST_WIDE_INT
13990 field_byte_offset (const_tree decl)
13991 {
13992   double_int object_offset_in_bits;
13993   double_int object_offset_in_bytes;
13994   double_int bitpos_int;
13995
13996   if (TREE_CODE (decl) == ERROR_MARK)
13997     return 0;
13998
13999   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14000
14001   /* We cannot yet cope with fields whose positions are variable, so
14002      for now, when we see such things, we simply return 0.  Someday, we may
14003      be able to handle such cases, but it will be damn difficult.  */
14004   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14005     return 0;
14006
14007   bitpos_int = tree_to_double_int (bit_position (decl));
14008
14009 #ifdef PCC_BITFIELD_TYPE_MATTERS
14010   if (PCC_BITFIELD_TYPE_MATTERS)
14011     {
14012       tree type;
14013       tree field_size_tree;
14014       double_int deepest_bitpos;
14015       double_int field_size_in_bits;
14016       unsigned int type_align_in_bits;
14017       unsigned int decl_align_in_bits;
14018       double_int type_size_in_bits;
14019
14020       type = field_type (decl);
14021       type_size_in_bits = double_int_type_size_in_bits (type);
14022       type_align_in_bits = simple_type_align_in_bits (type);
14023
14024       field_size_tree = DECL_SIZE (decl);
14025
14026       /* The size could be unspecified if there was an error, or for
14027          a flexible array member.  */
14028       if (!field_size_tree)
14029         field_size_tree = bitsize_zero_node;
14030
14031       /* If the size of the field is not constant, use the type size.  */
14032       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14033         field_size_in_bits = tree_to_double_int (field_size_tree);
14034       else
14035         field_size_in_bits = type_size_in_bits;
14036
14037       decl_align_in_bits = simple_decl_align_in_bits (decl);
14038
14039       /* The GCC front-end doesn't make any attempt to keep track of the
14040          starting bit offset (relative to the start of the containing
14041          structure type) of the hypothetical "containing object" for a
14042          bit-field.  Thus, when computing the byte offset value for the
14043          start of the "containing object" of a bit-field, we must deduce
14044          this information on our own. This can be rather tricky to do in
14045          some cases.  For example, handling the following structure type
14046          definition when compiling for an i386/i486 target (which only
14047          aligns long long's to 32-bit boundaries) can be very tricky:
14048
14049          struct S { int field1; long long field2:31; };
14050
14051          Fortunately, there is a simple rule-of-thumb which can be used
14052          in such cases.  When compiling for an i386/i486, GCC will
14053          allocate 8 bytes for the structure shown above.  It decides to
14054          do this based upon one simple rule for bit-field allocation.
14055          GCC allocates each "containing object" for each bit-field at
14056          the first (i.e. lowest addressed) legitimate alignment boundary
14057          (based upon the required minimum alignment for the declared
14058          type of the field) which it can possibly use, subject to the
14059          condition that there is still enough available space remaining
14060          in the containing object (when allocated at the selected point)
14061          to fully accommodate all of the bits of the bit-field itself.
14062
14063          This simple rule makes it obvious why GCC allocates 8 bytes for
14064          each object of the structure type shown above.  When looking
14065          for a place to allocate the "containing object" for `field2',
14066          the compiler simply tries to allocate a 64-bit "containing
14067          object" at each successive 32-bit boundary (starting at zero)
14068          until it finds a place to allocate that 64- bit field such that
14069          at least 31 contiguous (and previously unallocated) bits remain
14070          within that selected 64 bit field.  (As it turns out, for the
14071          example above, the compiler finds it is OK to allocate the
14072          "containing object" 64-bit field at bit-offset zero within the
14073          structure type.)
14074
14075          Here we attempt to work backwards from the limited set of facts
14076          we're given, and we try to deduce from those facts, where GCC
14077          must have believed that the containing object started (within
14078          the structure type). The value we deduce is then used (by the
14079          callers of this routine) to generate DW_AT_location and
14080          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14081          the case of DW_AT_location, regular fields as well).  */
14082
14083       /* Figure out the bit-distance from the start of the structure to
14084          the "deepest" bit of the bit-field.  */
14085       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14086
14087       /* This is the tricky part.  Use some fancy footwork to deduce
14088          where the lowest addressed bit of the containing object must
14089          be.  */
14090       object_offset_in_bits
14091         = double_int_sub (deepest_bitpos, type_size_in_bits);
14092
14093       /* Round up to type_align by default.  This works best for
14094          bitfields.  */
14095       object_offset_in_bits
14096         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14097
14098       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14099         {
14100           object_offset_in_bits
14101             = double_int_sub (deepest_bitpos, type_size_in_bits);
14102
14103           /* Round up to decl_align instead.  */
14104           object_offset_in_bits
14105             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14106         }
14107     }
14108   else
14109 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14110     object_offset_in_bits = bitpos_int;
14111
14112   object_offset_in_bytes
14113     = double_int_div (object_offset_in_bits,
14114                       uhwi_to_double_int (BITS_PER_UNIT), true,
14115                       TRUNC_DIV_EXPR);
14116   return double_int_to_shwi (object_offset_in_bytes);
14117 }
14118 \f
14119 /* The following routines define various Dwarf attributes and any data
14120    associated with them.  */
14121
14122 /* Add a location description attribute value to a DIE.
14123
14124    This emits location attributes suitable for whole variables and
14125    whole parameters.  Note that the location attributes for struct fields are
14126    generated by the routine `data_member_location_attribute' below.  */
14127
14128 static inline void
14129 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14130                              dw_loc_list_ref descr)
14131 {
14132   if (descr == 0)
14133     return;
14134   if (single_element_loc_list_p (descr))
14135     add_AT_loc (die, attr_kind, descr->expr);
14136   else
14137     add_AT_loc_list (die, attr_kind, descr);
14138 }
14139
14140 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14141
14142 static void
14143 add_accessibility_attribute (dw_die_ref die, tree decl)
14144 {
14145   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14146      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14147      the default has always been DW_ACCESS_public.  */
14148   if (TREE_PROTECTED (decl))
14149     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14150   else if (TREE_PRIVATE (decl))
14151     {
14152       if (dwarf_version == 2
14153           || die->die_parent == NULL
14154           || die->die_parent->die_tag != DW_TAG_class_type)
14155         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14156     }
14157   else if (dwarf_version > 2
14158            && die->die_parent
14159            && die->die_parent->die_tag == DW_TAG_class_type)
14160     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14161 }
14162
14163 /* Attach the specialized form of location attribute used for data members of
14164    struct and union types.  In the special case of a FIELD_DECL node which
14165    represents a bit-field, the "offset" part of this special location
14166    descriptor must indicate the distance in bytes from the lowest-addressed
14167    byte of the containing struct or union type to the lowest-addressed byte of
14168    the "containing object" for the bit-field.  (See the `field_byte_offset'
14169    function above).
14170
14171    For any given bit-field, the "containing object" is a hypothetical object
14172    (of some integral or enum type) within which the given bit-field lives.  The
14173    type of this hypothetical "containing object" is always the same as the
14174    declared type of the individual bit-field itself (for GCC anyway... the
14175    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14176    bytes) of the hypothetical "containing object" which will be given in the
14177    DW_AT_byte_size attribute for this bit-field.  (See the
14178    `byte_size_attribute' function below.)  It is also used when calculating the
14179    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14180    function below.)  */
14181
14182 static void
14183 add_data_member_location_attribute (dw_die_ref die, tree decl)
14184 {
14185   HOST_WIDE_INT offset;
14186   dw_loc_descr_ref loc_descr = 0;
14187
14188   if (TREE_CODE (decl) == TREE_BINFO)
14189     {
14190       /* We're working on the TAG_inheritance for a base class.  */
14191       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14192         {
14193           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14194              aren't at a fixed offset from all (sub)objects of the same
14195              type.  We need to extract the appropriate offset from our
14196              vtable.  The following dwarf expression means
14197
14198                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14199
14200              This is specific to the V3 ABI, of course.  */
14201
14202           dw_loc_descr_ref tmp;
14203
14204           /* Make a copy of the object address.  */
14205           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14206           add_loc_descr (&loc_descr, tmp);
14207
14208           /* Extract the vtable address.  */
14209           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14210           add_loc_descr (&loc_descr, tmp);
14211
14212           /* Calculate the address of the offset.  */
14213           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14214           gcc_assert (offset < 0);
14215
14216           tmp = int_loc_descriptor (-offset);
14217           add_loc_descr (&loc_descr, tmp);
14218           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14219           add_loc_descr (&loc_descr, tmp);
14220
14221           /* Extract the offset.  */
14222           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14223           add_loc_descr (&loc_descr, tmp);
14224
14225           /* Add it to the object address.  */
14226           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14227           add_loc_descr (&loc_descr, tmp);
14228         }
14229       else
14230         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14231     }
14232   else
14233     offset = field_byte_offset (decl);
14234
14235   if (! loc_descr)
14236     {
14237       if (dwarf_version > 2)
14238         {
14239           /* Don't need to output a location expression, just the constant. */
14240           if (offset < 0)
14241             add_AT_int (die, DW_AT_data_member_location, offset);
14242           else
14243             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14244           return;
14245         }
14246       else
14247         {
14248           enum dwarf_location_atom op;
14249
14250           /* The DWARF2 standard says that we should assume that the structure
14251              address is already on the stack, so we can specify a structure
14252              field address by using DW_OP_plus_uconst.  */
14253
14254 #ifdef MIPS_DEBUGGING_INFO
14255           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14256              operator correctly.  It works only if we leave the offset on the
14257              stack.  */
14258           op = DW_OP_constu;
14259 #else
14260           op = DW_OP_plus_uconst;
14261 #endif
14262
14263           loc_descr = new_loc_descr (op, offset, 0);
14264         }
14265     }
14266
14267   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14268 }
14269
14270 /* Writes integer values to dw_vec_const array.  */
14271
14272 static void
14273 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14274 {
14275   while (size != 0)
14276     {
14277       *dest++ = val & 0xff;
14278       val >>= 8;
14279       --size;
14280     }
14281 }
14282
14283 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14284
14285 static HOST_WIDE_INT
14286 extract_int (const unsigned char *src, unsigned int size)
14287 {
14288   HOST_WIDE_INT val = 0;
14289
14290   src += size;
14291   while (size != 0)
14292     {
14293       val <<= 8;
14294       val |= *--src & 0xff;
14295       --size;
14296     }
14297   return val;
14298 }
14299
14300 /* Writes double_int values to dw_vec_const array.  */
14301
14302 static void
14303 insert_double (double_int val, unsigned char *dest)
14304 {
14305   unsigned char *p0 = dest;
14306   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14307
14308   if (WORDS_BIG_ENDIAN)
14309     {
14310       p0 = p1;
14311       p1 = dest;
14312     }
14313
14314   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14315   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14316 }
14317
14318 /* Writes floating point values to dw_vec_const array.  */
14319
14320 static void
14321 insert_float (const_rtx rtl, unsigned char *array)
14322 {
14323   REAL_VALUE_TYPE rv;
14324   long val[4];
14325   int i;
14326
14327   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14328   real_to_target (val, &rv, GET_MODE (rtl));
14329
14330   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14331   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14332     {
14333       insert_int (val[i], 4, array);
14334       array += 4;
14335     }
14336 }
14337
14338 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14339    does not have a "location" either in memory or in a register.  These
14340    things can arise in GNU C when a constant is passed as an actual parameter
14341    to an inlined function.  They can also arise in C++ where declared
14342    constants do not necessarily get memory "homes".  */
14343
14344 static bool
14345 add_const_value_attribute (dw_die_ref die, rtx rtl)
14346 {
14347   switch (GET_CODE (rtl))
14348     {
14349     case CONST_INT:
14350       {
14351         HOST_WIDE_INT val = INTVAL (rtl);
14352
14353         if (val < 0)
14354           add_AT_int (die, DW_AT_const_value, val);
14355         else
14356           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14357       }
14358       return true;
14359
14360     case CONST_DOUBLE:
14361       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14362          floating-point constant.  A CONST_DOUBLE is used whenever the
14363          constant requires more than one word in order to be adequately
14364          represented.  */
14365       {
14366         enum machine_mode mode = GET_MODE (rtl);
14367
14368         if (SCALAR_FLOAT_MODE_P (mode))
14369           {
14370             unsigned int length = GET_MODE_SIZE (mode);
14371             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14372
14373             insert_float (rtl, array);
14374             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14375           }
14376         else
14377           add_AT_double (die, DW_AT_const_value,
14378                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14379       }
14380       return true;
14381
14382     case CONST_VECTOR:
14383       {
14384         enum machine_mode mode = GET_MODE (rtl);
14385         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14386         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14387         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14388           (length * elt_size);
14389         unsigned int i;
14390         unsigned char *p;
14391
14392         switch (GET_MODE_CLASS (mode))
14393           {
14394           case MODE_VECTOR_INT:
14395             for (i = 0, p = array; i < length; i++, p += elt_size)
14396               {
14397                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14398                 double_int val = rtx_to_double_int (elt);
14399
14400                 if (elt_size <= sizeof (HOST_WIDE_INT))
14401                   insert_int (double_int_to_shwi (val), elt_size, p);
14402                 else
14403                   {
14404                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14405                     insert_double (val, p);
14406                   }
14407               }
14408             break;
14409
14410           case MODE_VECTOR_FLOAT:
14411             for (i = 0, p = array; i < length; i++, p += elt_size)
14412               {
14413                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14414                 insert_float (elt, p);
14415               }
14416             break;
14417
14418           default:
14419             gcc_unreachable ();
14420           }
14421
14422         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14423       }
14424       return true;
14425
14426     case CONST_STRING:
14427       if (dwarf_version >= 4 || !dwarf_strict)
14428         {
14429           dw_loc_descr_ref loc_result;
14430           resolve_one_addr (&rtl, NULL);
14431         rtl_addr:
14432           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14433           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14434           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14435           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14436           add_AT_loc (die, DW_AT_location, loc_result);
14437           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14438           return true;
14439         }
14440       return false;
14441
14442     case CONST:
14443       if (CONSTANT_P (XEXP (rtl, 0)))
14444         return add_const_value_attribute (die, XEXP (rtl, 0));
14445       /* FALLTHROUGH */
14446     case SYMBOL_REF:
14447       if (!const_ok_for_output (rtl))
14448         return false;
14449     case LABEL_REF:
14450       if (dwarf_version >= 4 || !dwarf_strict)
14451         goto rtl_addr;
14452       return false;
14453
14454     case PLUS:
14455       /* In cases where an inlined instance of an inline function is passed
14456          the address of an `auto' variable (which is local to the caller) we
14457          can get a situation where the DECL_RTL of the artificial local
14458          variable (for the inlining) which acts as a stand-in for the
14459          corresponding formal parameter (of the inline function) will look
14460          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14461          exactly a compile-time constant expression, but it isn't the address
14462          of the (artificial) local variable either.  Rather, it represents the
14463          *value* which the artificial local variable always has during its
14464          lifetime.  We currently have no way to represent such quasi-constant
14465          values in Dwarf, so for now we just punt and generate nothing.  */
14466       return false;
14467
14468     case HIGH:
14469     case CONST_FIXED:
14470       return false;
14471
14472     case MEM:
14473       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14474           && MEM_READONLY_P (rtl)
14475           && GET_MODE (rtl) == BLKmode)
14476         {
14477           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14478           return true;
14479         }
14480       return false;
14481
14482     default:
14483       /* No other kinds of rtx should be possible here.  */
14484       gcc_unreachable ();
14485     }
14486   return false;
14487 }
14488
14489 /* Determine whether the evaluation of EXPR references any variables
14490    or functions which aren't otherwise used (and therefore may not be
14491    output).  */
14492 static tree
14493 reference_to_unused (tree * tp, int * walk_subtrees,
14494                      void * data ATTRIBUTE_UNUSED)
14495 {
14496   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14497     *walk_subtrees = 0;
14498
14499   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14500       && ! TREE_ASM_WRITTEN (*tp))
14501     return *tp;
14502   /* ???  The C++ FE emits debug information for using decls, so
14503      putting gcc_unreachable here falls over.  See PR31899.  For now
14504      be conservative.  */
14505   else if (!cgraph_global_info_ready
14506            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14507     return *tp;
14508   else if (TREE_CODE (*tp) == VAR_DECL)
14509     {
14510       struct varpool_node *node = varpool_get_node (*tp);
14511       if (!node || !node->needed)
14512         return *tp;
14513     }
14514   else if (TREE_CODE (*tp) == FUNCTION_DECL
14515            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14516     {
14517       /* The call graph machinery must have finished analyzing,
14518          optimizing and gimplifying the CU by now.
14519          So if *TP has no call graph node associated
14520          to it, it means *TP will not be emitted.  */
14521       if (!cgraph_get_node (*tp))
14522         return *tp;
14523     }
14524   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14525     return *tp;
14526
14527   return NULL_TREE;
14528 }
14529
14530 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14531    for use in a later add_const_value_attribute call.  */
14532
14533 static rtx
14534 rtl_for_decl_init (tree init, tree type)
14535 {
14536   rtx rtl = NULL_RTX;
14537
14538   STRIP_NOPS (init);
14539
14540   /* If a variable is initialized with a string constant without embedded
14541      zeros, build CONST_STRING.  */
14542   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14543     {
14544       tree enttype = TREE_TYPE (type);
14545       tree domain = TYPE_DOMAIN (type);
14546       enum machine_mode mode = TYPE_MODE (enttype);
14547
14548       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14549           && domain
14550           && integer_zerop (TYPE_MIN_VALUE (domain))
14551           && compare_tree_int (TYPE_MAX_VALUE (domain),
14552                                TREE_STRING_LENGTH (init) - 1) == 0
14553           && ((size_t) TREE_STRING_LENGTH (init)
14554               == strlen (TREE_STRING_POINTER (init)) + 1))
14555         {
14556           rtl = gen_rtx_CONST_STRING (VOIDmode,
14557                                       ggc_strdup (TREE_STRING_POINTER (init)));
14558           rtl = gen_rtx_MEM (BLKmode, rtl);
14559           MEM_READONLY_P (rtl) = 1;
14560         }
14561     }
14562   /* Other aggregates, and complex values, could be represented using
14563      CONCAT: FIXME!  */
14564   else if (AGGREGATE_TYPE_P (type)
14565            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14566                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14567            || TREE_CODE (type) == COMPLEX_TYPE)
14568     ;
14569   /* Vectors only work if their mode is supported by the target.
14570      FIXME: generic vectors ought to work too.  */
14571   else if (TREE_CODE (type) == VECTOR_TYPE
14572            && !VECTOR_MODE_P (TYPE_MODE (type)))
14573     ;
14574   /* If the initializer is something that we know will expand into an
14575      immediate RTL constant, expand it now.  We must be careful not to
14576      reference variables which won't be output.  */
14577   else if (initializer_constant_valid_p (init, type)
14578            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14579     {
14580       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14581          possible.  */
14582       if (TREE_CODE (type) == VECTOR_TYPE)
14583         switch (TREE_CODE (init))
14584           {
14585           case VECTOR_CST:
14586             break;
14587           case CONSTRUCTOR:
14588             if (TREE_CONSTANT (init))
14589               {
14590                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14591                 bool constant_p = true;
14592                 tree value;
14593                 unsigned HOST_WIDE_INT ix;
14594
14595                 /* Even when ctor is constant, it might contain non-*_CST
14596                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14597                    belong into VECTOR_CST nodes.  */
14598                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14599                   if (!CONSTANT_CLASS_P (value))
14600                     {
14601                       constant_p = false;
14602                       break;
14603                     }
14604
14605                 if (constant_p)
14606                   {
14607                     init = build_vector_from_ctor (type, elts);
14608                     break;
14609                   }
14610               }
14611             /* FALLTHRU */
14612
14613           default:
14614             return NULL;
14615           }
14616
14617       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14618
14619       /* If expand_expr returns a MEM, it wasn't immediate.  */
14620       gcc_assert (!rtl || !MEM_P (rtl));
14621     }
14622
14623   return rtl;
14624 }
14625
14626 /* Generate RTL for the variable DECL to represent its location.  */
14627
14628 static rtx
14629 rtl_for_decl_location (tree decl)
14630 {
14631   rtx rtl;
14632
14633   /* Here we have to decide where we are going to say the parameter "lives"
14634      (as far as the debugger is concerned).  We only have a couple of
14635      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14636
14637      DECL_RTL normally indicates where the parameter lives during most of the
14638      activation of the function.  If optimization is enabled however, this
14639      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14640      that the parameter doesn't really live anywhere (as far as the code
14641      generation parts of GCC are concerned) during most of the function's
14642      activation.  That will happen (for example) if the parameter is never
14643      referenced within the function.
14644
14645      We could just generate a location descriptor here for all non-NULL
14646      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14647      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14648      where DECL_RTL is NULL or is a pseudo-reg.
14649
14650      Note however that we can only get away with using DECL_INCOMING_RTL as
14651      a backup substitute for DECL_RTL in certain limited cases.  In cases
14652      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14653      we can be sure that the parameter was passed using the same type as it is
14654      declared to have within the function, and that its DECL_INCOMING_RTL
14655      points us to a place where a value of that type is passed.
14656
14657      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14658      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14659      because in these cases DECL_INCOMING_RTL points us to a value of some
14660      type which is *different* from the type of the parameter itself.  Thus,
14661      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14662      such cases, the debugger would end up (for example) trying to fetch a
14663      `float' from a place which actually contains the first part of a
14664      `double'.  That would lead to really incorrect and confusing
14665      output at debug-time.
14666
14667      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14668      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14669      are a couple of exceptions however.  On little-endian machines we can
14670      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14671      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14672      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14673      when (on a little-endian machine) a non-prototyped function has a
14674      parameter declared to be of type `short' or `char'.  In such cases,
14675      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14676      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14677      passed `int' value.  If the debugger then uses that address to fetch
14678      a `short' or a `char' (on a little-endian machine) the result will be
14679      the correct data, so we allow for such exceptional cases below.
14680
14681      Note that our goal here is to describe the place where the given formal
14682      parameter lives during most of the function's activation (i.e. between the
14683      end of the prologue and the start of the epilogue).  We'll do that as best
14684      as we can. Note however that if the given formal parameter is modified
14685      sometime during the execution of the function, then a stack backtrace (at
14686      debug-time) will show the function as having been called with the *new*
14687      value rather than the value which was originally passed in.  This happens
14688      rarely enough that it is not a major problem, but it *is* a problem, and
14689      I'd like to fix it.
14690
14691      A future version of dwarf2out.c may generate two additional attributes for
14692      any given DW_TAG_formal_parameter DIE which will describe the "passed
14693      type" and the "passed location" for the given formal parameter in addition
14694      to the attributes we now generate to indicate the "declared type" and the
14695      "active location" for each parameter.  This additional set of attributes
14696      could be used by debuggers for stack backtraces. Separately, note that
14697      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14698      This happens (for example) for inlined-instances of inline function formal
14699      parameters which are never referenced.  This really shouldn't be
14700      happening.  All PARM_DECL nodes should get valid non-NULL
14701      DECL_INCOMING_RTL values.  FIXME.  */
14702
14703   /* Use DECL_RTL as the "location" unless we find something better.  */
14704   rtl = DECL_RTL_IF_SET (decl);
14705
14706   /* When generating abstract instances, ignore everything except
14707      constants, symbols living in memory, and symbols living in
14708      fixed registers.  */
14709   if (! reload_completed)
14710     {
14711       if (rtl
14712           && (CONSTANT_P (rtl)
14713               || (MEM_P (rtl)
14714                   && CONSTANT_P (XEXP (rtl, 0)))
14715               || (REG_P (rtl)
14716                   && TREE_CODE (decl) == VAR_DECL
14717                   && TREE_STATIC (decl))))
14718         {
14719           rtl = targetm.delegitimize_address (rtl);
14720           return rtl;
14721         }
14722       rtl = NULL_RTX;
14723     }
14724   else if (TREE_CODE (decl) == PARM_DECL)
14725     {
14726       if (rtl == NULL_RTX
14727           || is_pseudo_reg (rtl)
14728           || (MEM_P (rtl)
14729               && is_pseudo_reg (XEXP (rtl, 0))
14730               && DECL_INCOMING_RTL (decl)
14731               && MEM_P (DECL_INCOMING_RTL (decl))
14732               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14733         {
14734           tree declared_type = TREE_TYPE (decl);
14735           tree passed_type = DECL_ARG_TYPE (decl);
14736           enum machine_mode dmode = TYPE_MODE (declared_type);
14737           enum machine_mode pmode = TYPE_MODE (passed_type);
14738
14739           /* This decl represents a formal parameter which was optimized out.
14740              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14741              all cases where (rtl == NULL_RTX) just below.  */
14742           if (dmode == pmode)
14743             rtl = DECL_INCOMING_RTL (decl);
14744           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14745                    && SCALAR_INT_MODE_P (dmode)
14746                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14747                    && DECL_INCOMING_RTL (decl))
14748             {
14749               rtx inc = DECL_INCOMING_RTL (decl);
14750               if (REG_P (inc))
14751                 rtl = inc;
14752               else if (MEM_P (inc))
14753                 {
14754                   if (BYTES_BIG_ENDIAN)
14755                     rtl = adjust_address_nv (inc, dmode,
14756                                              GET_MODE_SIZE (pmode)
14757                                              - GET_MODE_SIZE (dmode));
14758                   else
14759                     rtl = inc;
14760                 }
14761             }
14762         }
14763
14764       /* If the parm was passed in registers, but lives on the stack, then
14765          make a big endian correction if the mode of the type of the
14766          parameter is not the same as the mode of the rtl.  */
14767       /* ??? This is the same series of checks that are made in dbxout.c before
14768          we reach the big endian correction code there.  It isn't clear if all
14769          of these checks are necessary here, but keeping them all is the safe
14770          thing to do.  */
14771       else if (MEM_P (rtl)
14772                && XEXP (rtl, 0) != const0_rtx
14773                && ! CONSTANT_P (XEXP (rtl, 0))
14774                /* Not passed in memory.  */
14775                && !MEM_P (DECL_INCOMING_RTL (decl))
14776                /* Not passed by invisible reference.  */
14777                && (!REG_P (XEXP (rtl, 0))
14778                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14779                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14780 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14781                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14782 #endif
14783                      )
14784                /* Big endian correction check.  */
14785                && BYTES_BIG_ENDIAN
14786                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14787                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14788                    < UNITS_PER_WORD))
14789         {
14790           int offset = (UNITS_PER_WORD
14791                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14792
14793           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14794                              plus_constant (XEXP (rtl, 0), offset));
14795         }
14796     }
14797   else if (TREE_CODE (decl) == VAR_DECL
14798            && rtl
14799            && MEM_P (rtl)
14800            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14801            && BYTES_BIG_ENDIAN)
14802     {
14803       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14804       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14805
14806       /* If a variable is declared "register" yet is smaller than
14807          a register, then if we store the variable to memory, it
14808          looks like we're storing a register-sized value, when in
14809          fact we are not.  We need to adjust the offset of the
14810          storage location to reflect the actual value's bytes,
14811          else gdb will not be able to display it.  */
14812       if (rsize > dsize)
14813         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14814                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14815     }
14816
14817   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14818      and will have been substituted directly into all expressions that use it.
14819      C does not have such a concept, but C++ and other languages do.  */
14820   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14821     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14822
14823   if (rtl)
14824     rtl = targetm.delegitimize_address (rtl);
14825
14826   /* If we don't look past the constant pool, we risk emitting a
14827      reference to a constant pool entry that isn't referenced from
14828      code, and thus is not emitted.  */
14829   if (rtl)
14830     rtl = avoid_constant_pool_reference (rtl);
14831
14832   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14833      in the current CU, resolve_addr will remove the expression referencing
14834      it.  */
14835   if (rtl == NULL_RTX
14836       && TREE_CODE (decl) == VAR_DECL
14837       && !DECL_EXTERNAL (decl)
14838       && TREE_STATIC (decl)
14839       && DECL_NAME (decl)
14840       && !DECL_HARD_REGISTER (decl)
14841       && DECL_MODE (decl) != VOIDmode)
14842     {
14843       rtl = make_decl_rtl_for_debug (decl);
14844       if (!MEM_P (rtl)
14845           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14846           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14847         rtl = NULL_RTX;
14848     }
14849
14850   return rtl;
14851 }
14852
14853 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14854    returned.  If so, the decl for the COMMON block is returned, and the
14855    value is the offset into the common block for the symbol.  */
14856
14857 static tree
14858 fortran_common (tree decl, HOST_WIDE_INT *value)
14859 {
14860   tree val_expr, cvar;
14861   enum machine_mode mode;
14862   HOST_WIDE_INT bitsize, bitpos;
14863   tree offset;
14864   int volatilep = 0, unsignedp = 0;
14865
14866   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14867      it does not have a value (the offset into the common area), or if it
14868      is thread local (as opposed to global) then it isn't common, and shouldn't
14869      be handled as such.  */
14870   if (TREE_CODE (decl) != VAR_DECL
14871       || !TREE_STATIC (decl)
14872       || !DECL_HAS_VALUE_EXPR_P (decl)
14873       || !is_fortran ())
14874     return NULL_TREE;
14875
14876   val_expr = DECL_VALUE_EXPR (decl);
14877   if (TREE_CODE (val_expr) != COMPONENT_REF)
14878     return NULL_TREE;
14879
14880   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14881                               &mode, &unsignedp, &volatilep, true);
14882
14883   if (cvar == NULL_TREE
14884       || TREE_CODE (cvar) != VAR_DECL
14885       || DECL_ARTIFICIAL (cvar)
14886       || !TREE_PUBLIC (cvar))
14887     return NULL_TREE;
14888
14889   *value = 0;
14890   if (offset != NULL)
14891     {
14892       if (!host_integerp (offset, 0))
14893         return NULL_TREE;
14894       *value = tree_low_cst (offset, 0);
14895     }
14896   if (bitpos != 0)
14897     *value += bitpos / BITS_PER_UNIT;
14898
14899   return cvar;
14900 }
14901
14902 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14903    data attribute for a variable or a parameter.  We generate the
14904    DW_AT_const_value attribute only in those cases where the given variable
14905    or parameter does not have a true "location" either in memory or in a
14906    register.  This can happen (for example) when a constant is passed as an
14907    actual argument in a call to an inline function.  (It's possible that
14908    these things can crop up in other ways also.)  Note that one type of
14909    constant value which can be passed into an inlined function is a constant
14910    pointer.  This can happen for example if an actual argument in an inlined
14911    function call evaluates to a compile-time constant address.
14912
14913    CACHE_P is true if it is worth caching the location list for DECL,
14914    so that future calls can reuse it rather than regenerate it from scratch.
14915    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14916    since we will need to refer to them each time the function is inlined.  */
14917
14918 static bool
14919 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14920                                        enum dwarf_attribute attr)
14921 {
14922   rtx rtl;
14923   dw_loc_list_ref list;
14924   var_loc_list *loc_list;
14925   cached_dw_loc_list *cache;
14926   void **slot;
14927
14928   if (TREE_CODE (decl) == ERROR_MARK)
14929     return false;
14930
14931   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14932               || TREE_CODE (decl) == RESULT_DECL);
14933
14934   /* Try to get some constant RTL for this decl, and use that as the value of
14935      the location.  */
14936
14937   rtl = rtl_for_decl_location (decl);
14938   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14939       && add_const_value_attribute (die, rtl))
14940     return true;
14941
14942   /* See if we have single element location list that is equivalent to
14943      a constant value.  That way we are better to use add_const_value_attribute
14944      rather than expanding constant value equivalent.  */
14945   loc_list = lookup_decl_loc (decl);
14946   if (loc_list
14947       && loc_list->first
14948       && loc_list->first->next == NULL
14949       && NOTE_P (loc_list->first->loc)
14950       && NOTE_VAR_LOCATION (loc_list->first->loc)
14951       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14952     {
14953       struct var_loc_node *node;
14954
14955       node = loc_list->first;
14956       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14957       if (GET_CODE (rtl) == EXPR_LIST)
14958         rtl = XEXP (rtl, 0);
14959       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14960           && add_const_value_attribute (die, rtl))
14961          return true;
14962     }
14963   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14964      list several times.  See if we've already cached the contents.  */
14965   list = NULL;
14966   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14967     cache_p = false;
14968   if (cache_p)
14969     {
14970       cache = (cached_dw_loc_list *)
14971         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14972       if (cache)
14973         list = cache->loc_list;
14974     }
14975   if (list == NULL)
14976     {
14977       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14978       /* It is usually worth caching this result if the decl is from
14979          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14980       if (cache_p && list && list->dw_loc_next)
14981         {
14982           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14983                                            DECL_UID (decl), INSERT);
14984           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14985           cache->decl_id = DECL_UID (decl);
14986           cache->loc_list = list;
14987           *slot = cache;
14988         }
14989     }
14990   if (list)
14991     {
14992       add_AT_location_description (die, attr, list);
14993       return true;
14994     }
14995   /* None of that worked, so it must not really have a location;
14996      try adding a constant value attribute from the DECL_INITIAL.  */
14997   return tree_add_const_value_attribute_for_decl (die, decl);
14998 }
14999
15000 /* Add VARIABLE and DIE into deferred locations list.  */
15001
15002 static void
15003 defer_location (tree variable, dw_die_ref die)
15004 {
15005   deferred_locations entry;
15006   entry.variable = variable;
15007   entry.die = die;
15008   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15009 }
15010
15011 /* Helper function for tree_add_const_value_attribute.  Natively encode
15012    initializer INIT into an array.  Return true if successful.  */
15013
15014 static bool
15015 native_encode_initializer (tree init, unsigned char *array, int size)
15016 {
15017   tree type;
15018
15019   if (init == NULL_TREE)
15020     return false;
15021
15022   STRIP_NOPS (init);
15023   switch (TREE_CODE (init))
15024     {
15025     case STRING_CST:
15026       type = TREE_TYPE (init);
15027       if (TREE_CODE (type) == ARRAY_TYPE)
15028         {
15029           tree enttype = TREE_TYPE (type);
15030           enum machine_mode mode = TYPE_MODE (enttype);
15031
15032           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15033             return false;
15034           if (int_size_in_bytes (type) != size)
15035             return false;
15036           if (size > TREE_STRING_LENGTH (init))
15037             {
15038               memcpy (array, TREE_STRING_POINTER (init),
15039                       TREE_STRING_LENGTH (init));
15040               memset (array + TREE_STRING_LENGTH (init),
15041                       '\0', size - TREE_STRING_LENGTH (init));
15042             }
15043           else
15044             memcpy (array, TREE_STRING_POINTER (init), size);
15045           return true;
15046         }
15047       return false;
15048     case CONSTRUCTOR:
15049       type = TREE_TYPE (init);
15050       if (int_size_in_bytes (type) != size)
15051         return false;
15052       if (TREE_CODE (type) == ARRAY_TYPE)
15053         {
15054           HOST_WIDE_INT min_index;
15055           unsigned HOST_WIDE_INT cnt;
15056           int curpos = 0, fieldsize;
15057           constructor_elt *ce;
15058
15059           if (TYPE_DOMAIN (type) == NULL_TREE
15060               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15061             return false;
15062
15063           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15064           if (fieldsize <= 0)
15065             return false;
15066
15067           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15068           memset (array, '\0', size);
15069           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15070             {
15071               tree val = ce->value;
15072               tree index = ce->index;
15073               int pos = curpos;
15074               if (index && TREE_CODE (index) == RANGE_EXPR)
15075                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15076                       * fieldsize;
15077               else if (index)
15078                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15079
15080               if (val)
15081                 {
15082                   STRIP_NOPS (val);
15083                   if (!native_encode_initializer (val, array + pos, fieldsize))
15084                     return false;
15085                 }
15086               curpos = pos + fieldsize;
15087               if (index && TREE_CODE (index) == RANGE_EXPR)
15088                 {
15089                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15090                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15091                   while (count-- > 0)
15092                     {
15093                       if (val)
15094                         memcpy (array + curpos, array + pos, fieldsize);
15095                       curpos += fieldsize;
15096                     }
15097                 }
15098               gcc_assert (curpos <= size);
15099             }
15100           return true;
15101         }
15102       else if (TREE_CODE (type) == RECORD_TYPE
15103                || TREE_CODE (type) == UNION_TYPE)
15104         {
15105           tree field = NULL_TREE;
15106           unsigned HOST_WIDE_INT cnt;
15107           constructor_elt *ce;
15108
15109           if (int_size_in_bytes (type) != size)
15110             return false;
15111
15112           if (TREE_CODE (type) == RECORD_TYPE)
15113             field = TYPE_FIELDS (type);
15114
15115           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15116             {
15117               tree val = ce->value;
15118               int pos, fieldsize;
15119
15120               if (ce->index != 0)
15121                 field = ce->index;
15122
15123               if (val)
15124                 STRIP_NOPS (val);
15125
15126               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15127                 return false;
15128
15129               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15130                   && TYPE_DOMAIN (TREE_TYPE (field))
15131                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15132                 return false;
15133               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15134                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15135                 return false;
15136               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15137               pos = int_byte_position (field);
15138               gcc_assert (pos + fieldsize <= size);
15139               if (val
15140                   && !native_encode_initializer (val, array + pos, fieldsize))
15141                 return false;
15142             }
15143           return true;
15144         }
15145       return false;
15146     case VIEW_CONVERT_EXPR:
15147     case NON_LVALUE_EXPR:
15148       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15149     default:
15150       return native_encode_expr (init, array, size) == size;
15151     }
15152 }
15153
15154 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15155    attribute is the const value T.  */
15156
15157 static bool
15158 tree_add_const_value_attribute (dw_die_ref die, tree t)
15159 {
15160   tree init;
15161   tree type = TREE_TYPE (t);
15162   rtx rtl;
15163
15164   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15165     return false;
15166
15167   init = t;
15168   gcc_assert (!DECL_P (init));
15169
15170   rtl = rtl_for_decl_init (init, type);
15171   if (rtl)
15172     return add_const_value_attribute (die, rtl);
15173   /* If the host and target are sane, try harder.  */
15174   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15175            && initializer_constant_valid_p (init, type))
15176     {
15177       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15178       if (size > 0 && (int) size == size)
15179         {
15180           unsigned char *array = (unsigned char *)
15181             ggc_alloc_cleared_atomic (size);
15182
15183           if (native_encode_initializer (init, array, size))
15184             {
15185               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15186               return true;
15187             }
15188         }
15189     }
15190   return false;
15191 }
15192
15193 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15194    attribute is the const value of T, where T is an integral constant
15195    variable with static storage duration
15196    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15197
15198 static bool
15199 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15200 {
15201
15202   if (!decl
15203       || (TREE_CODE (decl) != VAR_DECL
15204           && TREE_CODE (decl) != CONST_DECL)
15205       || (TREE_CODE (decl) == VAR_DECL
15206           && !TREE_STATIC (decl)))
15207     return false;
15208
15209     if (TREE_READONLY (decl)
15210         && ! TREE_THIS_VOLATILE (decl)
15211         && DECL_INITIAL (decl))
15212       /* OK */;
15213     else
15214       return false;
15215
15216   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15217   if (get_AT (var_die, DW_AT_const_value))
15218     return false;
15219
15220   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15221 }
15222
15223 /* Convert the CFI instructions for the current function into a
15224    location list.  This is used for DW_AT_frame_base when we targeting
15225    a dwarf2 consumer that does not support the dwarf3
15226    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15227    expressions.  */
15228
15229 static dw_loc_list_ref
15230 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15231 {
15232   int ix;
15233   dw_fde_ref fde;
15234   dw_loc_list_ref list, *list_tail;
15235   dw_cfi_ref cfi;
15236   dw_cfa_location last_cfa, next_cfa;
15237   const char *start_label, *last_label, *section;
15238   dw_cfa_location remember;
15239
15240   fde = cfun->fde;
15241   gcc_assert (fde != NULL);
15242
15243   section = secname_for_decl (current_function_decl);
15244   list_tail = &list;
15245   list = NULL;
15246
15247   memset (&next_cfa, 0, sizeof (next_cfa));
15248   next_cfa.reg = INVALID_REGNUM;
15249   remember = next_cfa;
15250
15251   start_label = fde->dw_fde_begin;
15252
15253   /* ??? Bald assumption that the CIE opcode list does not contain
15254      advance opcodes.  */
15255   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15256     lookup_cfa_1 (cfi, &next_cfa, &remember);
15257
15258   last_cfa = next_cfa;
15259   last_label = start_label;
15260
15261   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15262     {
15263       /* If the first partition contained no CFI adjustments, the
15264          CIE opcodes apply to the whole first partition.  */
15265       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15266                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15267       list_tail =&(*list_tail)->dw_loc_next;
15268       start_label = last_label = fde->dw_fde_second_begin;
15269     }
15270
15271   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15272     {
15273       switch (cfi->dw_cfi_opc)
15274         {
15275         case DW_CFA_set_loc:
15276         case DW_CFA_advance_loc1:
15277         case DW_CFA_advance_loc2:
15278         case DW_CFA_advance_loc4:
15279           if (!cfa_equal_p (&last_cfa, &next_cfa))
15280             {
15281               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15282                                          start_label, last_label, section);
15283
15284               list_tail = &(*list_tail)->dw_loc_next;
15285               last_cfa = next_cfa;
15286               start_label = last_label;
15287             }
15288           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15289           break;
15290
15291         case DW_CFA_advance_loc:
15292           /* The encoding is complex enough that we should never emit this.  */
15293           gcc_unreachable ();
15294
15295         default:
15296           lookup_cfa_1 (cfi, &next_cfa, &remember);
15297           break;
15298         }
15299       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15300         {
15301           if (!cfa_equal_p (&last_cfa, &next_cfa))
15302             {
15303               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15304                                          start_label, last_label, section);
15305
15306               list_tail = &(*list_tail)->dw_loc_next;
15307               last_cfa = next_cfa;
15308               start_label = last_label;
15309             }
15310           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15311                                      start_label, fde->dw_fde_end, section);
15312           list_tail = &(*list_tail)->dw_loc_next;
15313           start_label = last_label = fde->dw_fde_second_begin;
15314         }
15315     }
15316
15317   if (!cfa_equal_p (&last_cfa, &next_cfa))
15318     {
15319       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15320                                  start_label, last_label, section);
15321       list_tail = &(*list_tail)->dw_loc_next;
15322       start_label = last_label;
15323     }
15324
15325   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15326                              start_label,
15327                              fde->dw_fde_second_begin
15328                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15329                              section);
15330
15331   if (list && list->dw_loc_next)
15332     gen_llsym (list);
15333
15334   return list;
15335 }
15336
15337 /* Compute a displacement from the "steady-state frame pointer" to the
15338    frame base (often the same as the CFA), and store it in
15339    frame_pointer_fb_offset.  OFFSET is added to the displacement
15340    before the latter is negated.  */
15341
15342 static void
15343 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15344 {
15345   rtx reg, elim;
15346
15347 #ifdef FRAME_POINTER_CFA_OFFSET
15348   reg = frame_pointer_rtx;
15349   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15350 #else
15351   reg = arg_pointer_rtx;
15352   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15353 #endif
15354
15355   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15356   if (GET_CODE (elim) == PLUS)
15357     {
15358       offset += INTVAL (XEXP (elim, 1));
15359       elim = XEXP (elim, 0);
15360     }
15361
15362   frame_pointer_fb_offset = -offset;
15363
15364   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15365      in which to eliminate.  This is because it's stack pointer isn't 
15366      directly accessible as a register within the ISA.  To work around
15367      this, assume that while we cannot provide a proper value for
15368      frame_pointer_fb_offset, we won't need one either.  */
15369   frame_pointer_fb_offset_valid
15370     = ((SUPPORTS_STACK_ALIGNMENT
15371         && (elim == hard_frame_pointer_rtx
15372             || elim == stack_pointer_rtx))
15373        || elim == (frame_pointer_needed
15374                    ? hard_frame_pointer_rtx
15375                    : stack_pointer_rtx));
15376 }
15377
15378 /* Generate a DW_AT_name attribute given some string value to be included as
15379    the value of the attribute.  */
15380
15381 static void
15382 add_name_attribute (dw_die_ref die, const char *name_string)
15383 {
15384   if (name_string != NULL && *name_string != 0)
15385     {
15386       if (demangle_name_func)
15387         name_string = (*demangle_name_func) (name_string);
15388
15389       add_AT_string (die, DW_AT_name, name_string);
15390     }
15391 }
15392
15393 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15394    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15395    of TYPE accordingly.
15396
15397    ??? This is a temporary measure until after we're able to generate
15398    regular DWARF for the complex Ada type system.  */
15399
15400 static void 
15401 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15402                                      dw_die_ref context_die)
15403 {
15404   tree dtype;
15405   dw_die_ref dtype_die;
15406
15407   if (!lang_hooks.types.descriptive_type)
15408     return;
15409
15410   dtype = lang_hooks.types.descriptive_type (type);
15411   if (!dtype)
15412     return;
15413
15414   dtype_die = lookup_type_die (dtype);
15415   if (!dtype_die)
15416     {
15417       gen_type_die (dtype, context_die);
15418       dtype_die = lookup_type_die (dtype);
15419       gcc_assert (dtype_die);
15420     }
15421
15422   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15423 }
15424
15425 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15426
15427 static void
15428 add_comp_dir_attribute (dw_die_ref die)
15429 {
15430   const char *wd = get_src_pwd ();
15431   char *wd1;
15432
15433   if (wd == NULL)
15434     return;
15435
15436   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15437     {
15438       int wdlen;
15439
15440       wdlen = strlen (wd);
15441       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15442       strcpy (wd1, wd);
15443       wd1 [wdlen] = DIR_SEPARATOR;
15444       wd1 [wdlen + 1] = 0;
15445       wd = wd1;
15446     }
15447
15448     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15449 }
15450
15451 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15452    default.  */
15453
15454 static int
15455 lower_bound_default (void)
15456 {
15457   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15458     {
15459     case DW_LANG_C:
15460     case DW_LANG_C89:
15461     case DW_LANG_C99:
15462     case DW_LANG_C_plus_plus:
15463     case DW_LANG_ObjC:
15464     case DW_LANG_ObjC_plus_plus:
15465     case DW_LANG_Java:
15466       return 0;
15467     case DW_LANG_Fortran77:
15468     case DW_LANG_Fortran90:
15469     case DW_LANG_Fortran95:
15470       return 1;
15471     case DW_LANG_UPC:
15472     case DW_LANG_D:
15473     case DW_LANG_Python:
15474       return dwarf_version >= 4 ? 0 : -1;
15475     case DW_LANG_Ada95:
15476     case DW_LANG_Ada83:
15477     case DW_LANG_Cobol74:
15478     case DW_LANG_Cobol85:
15479     case DW_LANG_Pascal83:
15480     case DW_LANG_Modula2:
15481     case DW_LANG_PLI:
15482       return dwarf_version >= 4 ? 1 : -1;
15483     default:
15484       return -1;
15485     }
15486 }
15487
15488 /* Given a tree node describing an array bound (either lower or upper) output
15489    a representation for that bound.  */
15490
15491 static void
15492 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15493 {
15494   switch (TREE_CODE (bound))
15495     {
15496     case ERROR_MARK:
15497       return;
15498
15499     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15500     case INTEGER_CST:
15501       {
15502         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15503         int dflt;
15504
15505         /* Use the default if possible.  */
15506         if (bound_attr == DW_AT_lower_bound
15507             && host_integerp (bound, 0)
15508             && (dflt = lower_bound_default ()) != -1
15509             && tree_low_cst (bound, 0) == dflt)
15510           ;
15511
15512         /* Otherwise represent the bound as an unsigned value with the
15513            precision of its type.  The precision and signedness of the
15514            type will be necessary to re-interpret it unambiguously.  */
15515         else if (prec < HOST_BITS_PER_WIDE_INT)
15516           {
15517             unsigned HOST_WIDE_INT mask
15518               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15519             add_AT_unsigned (subrange_die, bound_attr,
15520                              TREE_INT_CST_LOW (bound) & mask);
15521           }
15522         else if (prec == HOST_BITS_PER_WIDE_INT
15523                  || TREE_INT_CST_HIGH (bound) == 0)
15524           add_AT_unsigned (subrange_die, bound_attr,
15525                            TREE_INT_CST_LOW (bound));
15526         else
15527           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15528                          TREE_INT_CST_LOW (bound));
15529       }
15530       break;
15531
15532     CASE_CONVERT:
15533     case VIEW_CONVERT_EXPR:
15534       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15535       break;
15536
15537     case SAVE_EXPR:
15538       break;
15539
15540     case VAR_DECL:
15541     case PARM_DECL:
15542     case RESULT_DECL:
15543       {
15544         dw_die_ref decl_die = lookup_decl_die (bound);
15545
15546         /* ??? Can this happen, or should the variable have been bound
15547            first?  Probably it can, since I imagine that we try to create
15548            the types of parameters in the order in which they exist in
15549            the list, and won't have created a forward reference to a
15550            later parameter.  */
15551         if (decl_die != NULL)
15552           {
15553             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15554             break;
15555           }
15556       }
15557       /* FALLTHRU */
15558
15559     default:
15560       {
15561         /* Otherwise try to create a stack operation procedure to
15562            evaluate the value of the array bound.  */
15563
15564         dw_die_ref ctx, decl_die;
15565         dw_loc_list_ref list;
15566
15567         list = loc_list_from_tree (bound, 2);
15568         if (list == NULL || single_element_loc_list_p (list))
15569           {
15570             /* If DW_AT_*bound is not a reference nor constant, it is
15571                a DWARF expression rather than location description.
15572                For that loc_list_from_tree (bound, 0) is needed.
15573                If that fails to give a single element list,
15574                fall back to outputting this as a reference anyway.  */
15575             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15576             if (list2 && single_element_loc_list_p (list2))
15577               {
15578                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15579                 break;
15580               }
15581           }
15582         if (list == NULL)
15583           break;
15584
15585         if (current_function_decl == 0)
15586           ctx = comp_unit_die ();
15587         else
15588           ctx = lookup_decl_die (current_function_decl);
15589
15590         decl_die = new_die (DW_TAG_variable, ctx, bound);
15591         add_AT_flag (decl_die, DW_AT_artificial, 1);
15592         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15593         add_AT_location_description (decl_die, DW_AT_location, list);
15594         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15595         break;
15596       }
15597     }
15598 }
15599
15600 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15601    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15602    Note that the block of subscript information for an array type also
15603    includes information about the element type of the given array type.  */
15604
15605 static void
15606 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15607 {
15608   unsigned dimension_number;
15609   tree lower, upper;
15610   dw_die_ref subrange_die;
15611
15612   for (dimension_number = 0;
15613        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15614        type = TREE_TYPE (type), dimension_number++)
15615     {
15616       tree domain = TYPE_DOMAIN (type);
15617
15618       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15619         break;
15620
15621       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15622          and (in GNU C only) variable bounds.  Handle all three forms
15623          here.  */
15624       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15625       if (domain)
15626         {
15627           /* We have an array type with specified bounds.  */
15628           lower = TYPE_MIN_VALUE (domain);
15629           upper = TYPE_MAX_VALUE (domain);
15630
15631           /* Define the index type.  */
15632           if (TREE_TYPE (domain))
15633             {
15634               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15635                  TREE_TYPE field.  We can't emit debug info for this
15636                  because it is an unnamed integral type.  */
15637               if (TREE_CODE (domain) == INTEGER_TYPE
15638                   && TYPE_NAME (domain) == NULL_TREE
15639                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15640                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15641                 ;
15642               else
15643                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15644                                     type_die);
15645             }
15646
15647           /* ??? If upper is NULL, the array has unspecified length,
15648              but it does have a lower bound.  This happens with Fortran
15649                dimension arr(N:*)
15650              Since the debugger is definitely going to need to know N
15651              to produce useful results, go ahead and output the lower
15652              bound solo, and hope the debugger can cope.  */
15653
15654           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15655           if (upper)
15656             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15657         }
15658
15659       /* Otherwise we have an array type with an unspecified length.  The
15660          DWARF-2 spec does not say how to handle this; let's just leave out the
15661          bounds.  */
15662     }
15663 }
15664
15665 static void
15666 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15667 {
15668   unsigned size;
15669
15670   switch (TREE_CODE (tree_node))
15671     {
15672     case ERROR_MARK:
15673       size = 0;
15674       break;
15675     case ENUMERAL_TYPE:
15676     case RECORD_TYPE:
15677     case UNION_TYPE:
15678     case QUAL_UNION_TYPE:
15679       size = int_size_in_bytes (tree_node);
15680       break;
15681     case FIELD_DECL:
15682       /* For a data member of a struct or union, the DW_AT_byte_size is
15683          generally given as the number of bytes normally allocated for an
15684          object of the *declared* type of the member itself.  This is true
15685          even for bit-fields.  */
15686       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15687       break;
15688     default:
15689       gcc_unreachable ();
15690     }
15691
15692   /* Note that `size' might be -1 when we get to this point.  If it is, that
15693      indicates that the byte size of the entity in question is variable.  We
15694      have no good way of expressing this fact in Dwarf at the present time,
15695      so just let the -1 pass on through.  */
15696   add_AT_unsigned (die, DW_AT_byte_size, size);
15697 }
15698
15699 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15700    which specifies the distance in bits from the highest order bit of the
15701    "containing object" for the bit-field to the highest order bit of the
15702    bit-field itself.
15703
15704    For any given bit-field, the "containing object" is a hypothetical object
15705    (of some integral or enum type) within which the given bit-field lives.  The
15706    type of this hypothetical "containing object" is always the same as the
15707    declared type of the individual bit-field itself.  The determination of the
15708    exact location of the "containing object" for a bit-field is rather
15709    complicated.  It's handled by the `field_byte_offset' function (above).
15710
15711    Note that it is the size (in bytes) of the hypothetical "containing object"
15712    which will be given in the DW_AT_byte_size attribute for this bit-field.
15713    (See `byte_size_attribute' above).  */
15714
15715 static inline void
15716 add_bit_offset_attribute (dw_die_ref die, tree decl)
15717 {
15718   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15719   tree type = DECL_BIT_FIELD_TYPE (decl);
15720   HOST_WIDE_INT bitpos_int;
15721   HOST_WIDE_INT highest_order_object_bit_offset;
15722   HOST_WIDE_INT highest_order_field_bit_offset;
15723   HOST_WIDE_INT bit_offset;
15724
15725   /* Must be a field and a bit field.  */
15726   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15727
15728   /* We can't yet handle bit-fields whose offsets are variable, so if we
15729      encounter such things, just return without generating any attribute
15730      whatsoever.  Likewise for variable or too large size.  */
15731   if (! host_integerp (bit_position (decl), 0)
15732       || ! host_integerp (DECL_SIZE (decl), 1))
15733     return;
15734
15735   bitpos_int = int_bit_position (decl);
15736
15737   /* Note that the bit offset is always the distance (in bits) from the
15738      highest-order bit of the "containing object" to the highest-order bit of
15739      the bit-field itself.  Since the "high-order end" of any object or field
15740      is different on big-endian and little-endian machines, the computation
15741      below must take account of these differences.  */
15742   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15743   highest_order_field_bit_offset = bitpos_int;
15744
15745   if (! BYTES_BIG_ENDIAN)
15746     {
15747       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15748       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15749     }
15750
15751   bit_offset
15752     = (! BYTES_BIG_ENDIAN
15753        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15754        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15755
15756   if (bit_offset < 0)
15757     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15758   else
15759     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15760 }
15761
15762 /* For a FIELD_DECL node which represents a bit field, output an attribute
15763    which specifies the length in bits of the given field.  */
15764
15765 static inline void
15766 add_bit_size_attribute (dw_die_ref die, tree decl)
15767 {
15768   /* Must be a field and a bit field.  */
15769   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15770               && DECL_BIT_FIELD_TYPE (decl));
15771
15772   if (host_integerp (DECL_SIZE (decl), 1))
15773     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15774 }
15775
15776 /* If the compiled language is ANSI C, then add a 'prototyped'
15777    attribute, if arg types are given for the parameters of a function.  */
15778
15779 static inline void
15780 add_prototyped_attribute (dw_die_ref die, tree func_type)
15781 {
15782   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15783       && prototype_p (func_type))
15784     add_AT_flag (die, DW_AT_prototyped, 1);
15785 }
15786
15787 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15788    by looking in either the type declaration or object declaration
15789    equate table.  */
15790
15791 static inline dw_die_ref
15792 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15793 {
15794   dw_die_ref origin_die = NULL;
15795
15796   if (TREE_CODE (origin) != FUNCTION_DECL)
15797     {
15798       /* We may have gotten separated from the block for the inlined
15799          function, if we're in an exception handler or some such; make
15800          sure that the abstract function has been written out.
15801
15802          Doing this for nested functions is wrong, however; functions are
15803          distinct units, and our context might not even be inline.  */
15804       tree fn = origin;
15805
15806       if (TYPE_P (fn))
15807         fn = TYPE_STUB_DECL (fn);
15808
15809       fn = decl_function_context (fn);
15810       if (fn)
15811         dwarf2out_abstract_function (fn);
15812     }
15813
15814   if (DECL_P (origin))
15815     origin_die = lookup_decl_die (origin);
15816   else if (TYPE_P (origin))
15817     origin_die = lookup_type_die (origin);
15818
15819   /* XXX: Functions that are never lowered don't always have correct block
15820      trees (in the case of java, they simply have no block tree, in some other
15821      languages).  For these functions, there is nothing we can really do to
15822      output correct debug info for inlined functions in all cases.  Rather
15823      than die, we'll just produce deficient debug info now, in that we will
15824      have variables without a proper abstract origin.  In the future, when all
15825      functions are lowered, we should re-add a gcc_assert (origin_die)
15826      here.  */
15827
15828   if (origin_die)
15829     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15830   return origin_die;
15831 }
15832
15833 /* We do not currently support the pure_virtual attribute.  */
15834
15835 static inline void
15836 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15837 {
15838   if (DECL_VINDEX (func_decl))
15839     {
15840       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15841
15842       if (host_integerp (DECL_VINDEX (func_decl), 0))
15843         add_AT_loc (die, DW_AT_vtable_elem_location,
15844                     new_loc_descr (DW_OP_constu,
15845                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15846                                    0));
15847
15848       /* GNU extension: Record what type this method came from originally.  */
15849       if (debug_info_level > DINFO_LEVEL_TERSE
15850           && DECL_CONTEXT (func_decl))
15851         add_AT_die_ref (die, DW_AT_containing_type,
15852                         lookup_type_die (DECL_CONTEXT (func_decl)));
15853     }
15854 }
15855 \f
15856 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15857    given decl.  This used to be a vendor extension until after DWARF 4
15858    standardized it.  */
15859
15860 static void
15861 add_linkage_attr (dw_die_ref die, tree decl)
15862 {
15863   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15864
15865   /* Mimic what assemble_name_raw does with a leading '*'.  */
15866   if (name[0] == '*')
15867     name = &name[1];
15868
15869   if (dwarf_version >= 4)
15870     add_AT_string (die, DW_AT_linkage_name, name);
15871   else
15872     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15873 }
15874
15875 /* Add source coordinate attributes for the given decl.  */
15876
15877 static void
15878 add_src_coords_attributes (dw_die_ref die, tree decl)
15879 {
15880   expanded_location s;
15881
15882   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15883     return;
15884   s = expand_location (DECL_SOURCE_LOCATION (decl));
15885   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15886   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15887 }
15888
15889 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15890
15891 static void
15892 add_linkage_name (dw_die_ref die, tree decl)
15893 {
15894   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15895        && TREE_PUBLIC (decl)
15896        && !DECL_ABSTRACT (decl)
15897        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15898        && die->die_tag != DW_TAG_member)
15899     {
15900       /* Defer until we have an assembler name set.  */
15901       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15902         {
15903           limbo_die_node *asm_name;
15904
15905           asm_name = ggc_alloc_cleared_limbo_die_node ();
15906           asm_name->die = die;
15907           asm_name->created_for = decl;
15908           asm_name->next = deferred_asm_name;
15909           deferred_asm_name = asm_name;
15910         }
15911       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15912         add_linkage_attr (die, decl);
15913     }
15914 }
15915
15916 /* Add a DW_AT_name attribute and source coordinate attribute for the
15917    given decl, but only if it actually has a name.  */
15918
15919 static void
15920 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15921 {
15922   tree decl_name;
15923
15924   decl_name = DECL_NAME (decl);
15925   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15926     {
15927       const char *name = dwarf2_name (decl, 0);
15928       if (name)
15929         add_name_attribute (die, name);
15930       if (! DECL_ARTIFICIAL (decl))
15931         add_src_coords_attributes (die, decl);
15932
15933       add_linkage_name (die, decl);
15934     }
15935
15936 #ifdef VMS_DEBUGGING_INFO
15937   /* Get the function's name, as described by its RTL.  This may be different
15938      from the DECL_NAME name used in the source file.  */
15939   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15940     {
15941       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15942                    XEXP (DECL_RTL (decl), 0));
15943       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15944     }
15945 #endif /* VMS_DEBUGGING_INFO */
15946 }
15947
15948 #ifdef VMS_DEBUGGING_INFO
15949 /* Output the debug main pointer die for VMS */
15950
15951 void
15952 dwarf2out_vms_debug_main_pointer (void)
15953 {
15954   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15955   dw_die_ref die;
15956
15957   /* Allocate the VMS debug main subprogram die.  */
15958   die = ggc_alloc_cleared_die_node ();
15959   die->die_tag = DW_TAG_subprogram;
15960   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15961   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15962                                current_function_funcdef_no);
15963   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15964
15965   /* Make it the first child of comp_unit_die ().  */
15966   die->die_parent = comp_unit_die ();
15967   if (comp_unit_die ()->die_child)
15968     {
15969       die->die_sib = comp_unit_die ()->die_child->die_sib;
15970       comp_unit_die ()->die_child->die_sib = die;
15971     }
15972   else
15973     {
15974       die->die_sib = die;
15975       comp_unit_die ()->die_child = die;
15976     }
15977 }
15978 #endif /* VMS_DEBUGGING_INFO */
15979
15980 /* Push a new declaration scope.  */
15981
15982 static void
15983 push_decl_scope (tree scope)
15984 {
15985   VEC_safe_push (tree, gc, decl_scope_table, scope);
15986 }
15987
15988 /* Pop a declaration scope.  */
15989
15990 static inline void
15991 pop_decl_scope (void)
15992 {
15993   VEC_pop (tree, decl_scope_table);
15994 }
15995
15996 /* Return the DIE for the scope that immediately contains this type.
15997    Non-named types get global scope.  Named types nested in other
15998    types get their containing scope if it's open, or global scope
15999    otherwise.  All other types (i.e. function-local named types) get
16000    the current active scope.  */
16001
16002 static dw_die_ref
16003 scope_die_for (tree t, dw_die_ref context_die)
16004 {
16005   dw_die_ref scope_die = NULL;
16006   tree containing_scope;
16007   int i;
16008
16009   /* Non-types always go in the current scope.  */
16010   gcc_assert (TYPE_P (t));
16011
16012   containing_scope = TYPE_CONTEXT (t);
16013
16014   /* Use the containing namespace if it was passed in (for a declaration).  */
16015   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16016     {
16017       if (context_die == lookup_decl_die (containing_scope))
16018         /* OK */;
16019       else
16020         containing_scope = NULL_TREE;
16021     }
16022
16023   /* Ignore function type "scopes" from the C frontend.  They mean that
16024      a tagged type is local to a parmlist of a function declarator, but
16025      that isn't useful to DWARF.  */
16026   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16027     containing_scope = NULL_TREE;
16028
16029   if (SCOPE_FILE_SCOPE_P (containing_scope))
16030     scope_die = comp_unit_die ();
16031   else if (TYPE_P (containing_scope))
16032     {
16033       /* For types, we can just look up the appropriate DIE.  But
16034          first we check to see if we're in the middle of emitting it
16035          so we know where the new DIE should go.  */
16036       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16037         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16038           break;
16039
16040       if (i < 0)
16041         {
16042           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16043                       || TREE_ASM_WRITTEN (containing_scope));
16044           /*We are not in the middle of emitting the type
16045             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16046           scope_die = lookup_type_die (containing_scope);
16047
16048           /* If none of the current dies are suitable, we get file scope.  */
16049           if (scope_die == NULL)
16050             scope_die = comp_unit_die ();
16051         }
16052       else
16053         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16054     }
16055   else
16056     scope_die = context_die;
16057
16058   return scope_die;
16059 }
16060
16061 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16062
16063 static inline int
16064 local_scope_p (dw_die_ref context_die)
16065 {
16066   for (; context_die; context_die = context_die->die_parent)
16067     if (context_die->die_tag == DW_TAG_inlined_subroutine
16068         || context_die->die_tag == DW_TAG_subprogram)
16069       return 1;
16070
16071   return 0;
16072 }
16073
16074 /* Returns nonzero if CONTEXT_DIE is a class.  */
16075
16076 static inline int
16077 class_scope_p (dw_die_ref context_die)
16078 {
16079   return (context_die
16080           && (context_die->die_tag == DW_TAG_structure_type
16081               || context_die->die_tag == DW_TAG_class_type
16082               || context_die->die_tag == DW_TAG_interface_type
16083               || context_die->die_tag == DW_TAG_union_type));
16084 }
16085
16086 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16087    whether or not to treat a DIE in this context as a declaration.  */
16088
16089 static inline int
16090 class_or_namespace_scope_p (dw_die_ref context_die)
16091 {
16092   return (class_scope_p (context_die)
16093           || (context_die && context_die->die_tag == DW_TAG_namespace));
16094 }
16095
16096 /* Many forms of DIEs require a "type description" attribute.  This
16097    routine locates the proper "type descriptor" die for the type given
16098    by 'type', and adds a DW_AT_type attribute below the given die.  */
16099
16100 static void
16101 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16102                     int decl_volatile, dw_die_ref context_die)
16103 {
16104   enum tree_code code  = TREE_CODE (type);
16105   dw_die_ref type_die  = NULL;
16106
16107   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16108      or fixed-point type, use the inner type.  This is because we have no
16109      support for unnamed types in base_type_die.  This can happen if this is
16110      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16111   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16112       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16113     type = TREE_TYPE (type), code = TREE_CODE (type);
16114
16115   if (code == ERROR_MARK
16116       /* Handle a special case.  For functions whose return type is void, we
16117          generate *no* type attribute.  (Note that no object may have type
16118          `void', so this only applies to function return types).  */
16119       || code == VOID_TYPE)
16120     return;
16121
16122   type_die = modified_type_die (type,
16123                                 decl_const || TYPE_READONLY (type),
16124                                 decl_volatile || TYPE_VOLATILE (type),
16125                                 context_die);
16126
16127   if (type_die != NULL)
16128     add_AT_die_ref (object_die, DW_AT_type, type_die);
16129 }
16130
16131 /* Given an object die, add the calling convention attribute for the
16132    function call type.  */
16133 static void
16134 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16135 {
16136   enum dwarf_calling_convention value = DW_CC_normal;
16137
16138   value = ((enum dwarf_calling_convention)
16139            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16140
16141   if (is_fortran ()
16142       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16143     {
16144       /* DWARF 2 doesn't provide a way to identify a program's source-level
16145         entry point.  DW_AT_calling_convention attributes are only meant
16146         to describe functions' calling conventions.  However, lacking a
16147         better way to signal the Fortran main program, we used this for 
16148         a long time, following existing custom.  Now, DWARF 4 has 
16149         DW_AT_main_subprogram, which we add below, but some tools still
16150         rely on the old way, which we thus keep.  */
16151       value = DW_CC_program;
16152
16153       if (dwarf_version >= 4 || !dwarf_strict)
16154         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16155     }
16156
16157   /* Only add the attribute if the backend requests it, and
16158      is not DW_CC_normal.  */
16159   if (value && (value != DW_CC_normal))
16160     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16161 }
16162
16163 /* Given a tree pointer to a struct, class, union, or enum type node, return
16164    a pointer to the (string) tag name for the given type, or zero if the type
16165    was declared without a tag.  */
16166
16167 static const char *
16168 type_tag (const_tree type)
16169 {
16170   const char *name = 0;
16171
16172   if (TYPE_NAME (type) != 0)
16173     {
16174       tree t = 0;
16175
16176       /* Find the IDENTIFIER_NODE for the type name.  */
16177       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16178           && !TYPE_NAMELESS (type))
16179         t = TYPE_NAME (type);
16180
16181       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16182          a TYPE_DECL node, regardless of whether or not a `typedef' was
16183          involved.  */
16184       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16185                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16186         {
16187           /* We want to be extra verbose.  Don't call dwarf_name if
16188              DECL_NAME isn't set.  The default hook for decl_printable_name
16189              doesn't like that, and in this context it's correct to return
16190              0, instead of "<anonymous>" or the like.  */
16191           if (DECL_NAME (TYPE_NAME (type))
16192               && !DECL_NAMELESS (TYPE_NAME (type)))
16193             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16194         }
16195
16196       /* Now get the name as a string, or invent one.  */
16197       if (!name && t != 0)
16198         name = IDENTIFIER_POINTER (t);
16199     }
16200
16201   return (name == 0 || *name == '\0') ? 0 : name;
16202 }
16203
16204 /* Return the type associated with a data member, make a special check
16205    for bit field types.  */
16206
16207 static inline tree
16208 member_declared_type (const_tree member)
16209 {
16210   return (DECL_BIT_FIELD_TYPE (member)
16211           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16212 }
16213
16214 /* Get the decl's label, as described by its RTL. This may be different
16215    from the DECL_NAME name used in the source file.  */
16216
16217 #if 0
16218 static const char *
16219 decl_start_label (tree decl)
16220 {
16221   rtx x;
16222   const char *fnname;
16223
16224   x = DECL_RTL (decl);
16225   gcc_assert (MEM_P (x));
16226
16227   x = XEXP (x, 0);
16228   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16229
16230   fnname = XSTR (x, 0);
16231   return fnname;
16232 }
16233 #endif
16234 \f
16235 /* These routines generate the internal representation of the DIE's for
16236    the compilation unit.  Debugging information is collected by walking
16237    the declaration trees passed in from dwarf2out_decl().  */
16238
16239 static void
16240 gen_array_type_die (tree type, dw_die_ref context_die)
16241 {
16242   dw_die_ref scope_die = scope_die_for (type, context_die);
16243   dw_die_ref array_die;
16244
16245   /* GNU compilers represent multidimensional array types as sequences of one
16246      dimensional array types whose element types are themselves array types.
16247      We sometimes squish that down to a single array_type DIE with multiple
16248      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16249      say that we are allowed to do this kind of compression in C, because
16250      there is no difference between an array of arrays and a multidimensional
16251      array.  We don't do this for Ada to remain as close as possible to the
16252      actual representation, which is especially important against the language
16253      flexibilty wrt arrays of variable size.  */
16254
16255   bool collapse_nested_arrays = !is_ada ();
16256   tree element_type;
16257
16258   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16259      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16260   if (TYPE_STRING_FLAG (type)
16261       && TREE_CODE (type) == ARRAY_TYPE
16262       && is_fortran ()
16263       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16264     {
16265       HOST_WIDE_INT size;
16266
16267       array_die = new_die (DW_TAG_string_type, scope_die, type);
16268       add_name_attribute (array_die, type_tag (type));
16269       equate_type_number_to_die (type, array_die);
16270       size = int_size_in_bytes (type);
16271       if (size >= 0)
16272         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16273       else if (TYPE_DOMAIN (type) != NULL_TREE
16274                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16275                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16276         {
16277           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16278           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16279
16280           size = int_size_in_bytes (TREE_TYPE (szdecl));
16281           if (loc && size > 0)
16282             {
16283               add_AT_location_description (array_die, DW_AT_string_length, loc);
16284               if (size != DWARF2_ADDR_SIZE)
16285                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16286             }
16287         }
16288       return;
16289     }
16290
16291   /* ??? The SGI dwarf reader fails for array of array of enum types
16292      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16293      array type comes before the outer array type.  We thus call gen_type_die
16294      before we new_die and must prevent nested array types collapsing for this
16295      target.  */
16296
16297 #ifdef MIPS_DEBUGGING_INFO
16298   gen_type_die (TREE_TYPE (type), context_die);
16299   collapse_nested_arrays = false;
16300 #endif
16301
16302   array_die = new_die (DW_TAG_array_type, scope_die, type);
16303   add_name_attribute (array_die, type_tag (type));
16304   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16305   if (TYPE_ARTIFICIAL (type))
16306     add_AT_flag (array_die, DW_AT_artificial, 1);
16307   equate_type_number_to_die (type, array_die);
16308
16309   if (TREE_CODE (type) == VECTOR_TYPE)
16310     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16311
16312   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16313   if (is_fortran ()
16314       && TREE_CODE (type) == ARRAY_TYPE
16315       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16316       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16317     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16318
16319 #if 0
16320   /* We default the array ordering.  SDB will probably do
16321      the right things even if DW_AT_ordering is not present.  It's not even
16322      an issue until we start to get into multidimensional arrays anyway.  If
16323      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16324      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16325      and when we find out that we need to put these in, we will only do so
16326      for multidimensional arrays.  */
16327   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16328 #endif
16329
16330 #ifdef MIPS_DEBUGGING_INFO
16331   /* The SGI compilers handle arrays of unknown bound by setting
16332      AT_declaration and not emitting any subrange DIEs.  */
16333   if (TREE_CODE (type) == ARRAY_TYPE
16334       && ! TYPE_DOMAIN (type))
16335     add_AT_flag (array_die, DW_AT_declaration, 1);
16336   else
16337 #endif
16338   if (TREE_CODE (type) == VECTOR_TYPE)
16339     {
16340       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16341       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16342       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16343       add_bound_info (subrange_die, DW_AT_upper_bound,
16344                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16345     }
16346   else
16347     add_subscript_info (array_die, type, collapse_nested_arrays);
16348
16349   /* Add representation of the type of the elements of this array type and
16350      emit the corresponding DIE if we haven't done it already.  */
16351   element_type = TREE_TYPE (type);
16352   if (collapse_nested_arrays)
16353     while (TREE_CODE (element_type) == ARRAY_TYPE)
16354       {
16355         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16356           break;
16357         element_type = TREE_TYPE (element_type);
16358       }
16359
16360 #ifndef MIPS_DEBUGGING_INFO
16361   gen_type_die (element_type, context_die);
16362 #endif
16363
16364   add_type_attribute (array_die, element_type, 0, 0, context_die);
16365
16366   if (get_AT (array_die, DW_AT_name))
16367     add_pubtype (type, array_die);
16368 }
16369
16370 static dw_loc_descr_ref
16371 descr_info_loc (tree val, tree base_decl)
16372 {
16373   HOST_WIDE_INT size;
16374   dw_loc_descr_ref loc, loc2;
16375   enum dwarf_location_atom op;
16376
16377   if (val == base_decl)
16378     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16379
16380   switch (TREE_CODE (val))
16381     {
16382     CASE_CONVERT:
16383       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16384     case VAR_DECL:
16385       return loc_descriptor_from_tree (val, 0);
16386     case INTEGER_CST:
16387       if (host_integerp (val, 0))
16388         return int_loc_descriptor (tree_low_cst (val, 0));
16389       break;
16390     case INDIRECT_REF:
16391       size = int_size_in_bytes (TREE_TYPE (val));
16392       if (size < 0)
16393         break;
16394       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16395       if (!loc)
16396         break;
16397       if (size == DWARF2_ADDR_SIZE)
16398         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16399       else
16400         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16401       return loc;
16402     case POINTER_PLUS_EXPR:
16403     case PLUS_EXPR:
16404       if (host_integerp (TREE_OPERAND (val, 1), 1)
16405           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16406              < 16384)
16407         {
16408           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16409           if (!loc)
16410             break;
16411           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16412         }
16413       else
16414         {
16415           op = DW_OP_plus;
16416         do_binop:
16417           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16418           if (!loc)
16419             break;
16420           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16421           if (!loc2)
16422             break;
16423           add_loc_descr (&loc, loc2);
16424           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16425         }
16426       return loc;
16427     case MINUS_EXPR:
16428       op = DW_OP_minus;
16429       goto do_binop;
16430     case MULT_EXPR:
16431       op = DW_OP_mul;
16432       goto do_binop;
16433     case EQ_EXPR:
16434       op = DW_OP_eq;
16435       goto do_binop;
16436     case NE_EXPR:
16437       op = DW_OP_ne;
16438       goto do_binop;
16439     default:
16440       break;
16441     }
16442   return NULL;
16443 }
16444
16445 static void
16446 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16447                       tree val, tree base_decl)
16448 {
16449   dw_loc_descr_ref loc;
16450
16451   if (host_integerp (val, 0))
16452     {
16453       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16454       return;
16455     }
16456
16457   loc = descr_info_loc (val, base_decl);
16458   if (!loc)
16459     return;
16460
16461   add_AT_loc (die, attr, loc);
16462 }
16463
16464 /* This routine generates DIE for array with hidden descriptor, details
16465    are filled into *info by a langhook.  */
16466
16467 static void
16468 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16469                           dw_die_ref context_die)
16470 {
16471   dw_die_ref scope_die = scope_die_for (type, context_die);
16472   dw_die_ref array_die;
16473   int dim;
16474
16475   array_die = new_die (DW_TAG_array_type, scope_die, type);
16476   add_name_attribute (array_die, type_tag (type));
16477   equate_type_number_to_die (type, array_die);
16478
16479   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16480   if (is_fortran ()
16481       && info->ndimensions >= 2)
16482     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16483
16484   if (info->data_location)
16485     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16486                           info->base_decl);
16487   if (info->associated)
16488     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16489                           info->base_decl);
16490   if (info->allocated)
16491     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16492                           info->base_decl);
16493
16494   for (dim = 0; dim < info->ndimensions; dim++)
16495     {
16496       dw_die_ref subrange_die
16497         = new_die (DW_TAG_subrange_type, array_die, NULL);
16498
16499       if (info->dimen[dim].lower_bound)
16500         {
16501           /* If it is the default value, omit it.  */
16502           int dflt;
16503
16504           if (host_integerp (info->dimen[dim].lower_bound, 0)
16505               && (dflt = lower_bound_default ()) != -1
16506               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16507             ;
16508           else
16509             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16510                                   info->dimen[dim].lower_bound,
16511                                   info->base_decl);
16512         }
16513       if (info->dimen[dim].upper_bound)
16514         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16515                               info->dimen[dim].upper_bound,
16516                               info->base_decl);
16517       if (info->dimen[dim].stride)
16518         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16519                               info->dimen[dim].stride,
16520                               info->base_decl);
16521     }
16522
16523   gen_type_die (info->element_type, context_die);
16524   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16525
16526   if (get_AT (array_die, DW_AT_name))
16527     add_pubtype (type, array_die);
16528 }
16529
16530 #if 0
16531 static void
16532 gen_entry_point_die (tree decl, dw_die_ref context_die)
16533 {
16534   tree origin = decl_ultimate_origin (decl);
16535   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16536
16537   if (origin != NULL)
16538     add_abstract_origin_attribute (decl_die, origin);
16539   else
16540     {
16541       add_name_and_src_coords_attributes (decl_die, decl);
16542       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16543                           0, 0, context_die);
16544     }
16545
16546   if (DECL_ABSTRACT (decl))
16547     equate_decl_number_to_die (decl, decl_die);
16548   else
16549     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16550 }
16551 #endif
16552
16553 /* Walk through the list of incomplete types again, trying once more to
16554    emit full debugging info for them.  */
16555
16556 static void
16557 retry_incomplete_types (void)
16558 {
16559   int i;
16560
16561   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16562     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16563                                   DINFO_USAGE_DIR_USE))
16564       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16565 }
16566
16567 /* Determine what tag to use for a record type.  */
16568
16569 static enum dwarf_tag
16570 record_type_tag (tree type)
16571 {
16572   if (! lang_hooks.types.classify_record)
16573     return DW_TAG_structure_type;
16574
16575   switch (lang_hooks.types.classify_record (type))
16576     {
16577     case RECORD_IS_STRUCT:
16578       return DW_TAG_structure_type;
16579
16580     case RECORD_IS_CLASS:
16581       return DW_TAG_class_type;
16582
16583     case RECORD_IS_INTERFACE:
16584       if (dwarf_version >= 3 || !dwarf_strict)
16585         return DW_TAG_interface_type;
16586       return DW_TAG_structure_type;
16587
16588     default:
16589       gcc_unreachable ();
16590     }
16591 }
16592
16593 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16594    include all of the information about the enumeration values also. Each
16595    enumerated type name/value is listed as a child of the enumerated type
16596    DIE.  */
16597
16598 static dw_die_ref
16599 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16600 {
16601   dw_die_ref type_die = lookup_type_die (type);
16602
16603   if (type_die == NULL)
16604     {
16605       type_die = new_die (DW_TAG_enumeration_type,
16606                           scope_die_for (type, context_die), type);
16607       equate_type_number_to_die (type, type_die);
16608       add_name_attribute (type_die, type_tag (type));
16609       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16610       if (TYPE_ARTIFICIAL (type))
16611         add_AT_flag (type_die, DW_AT_artificial, 1);
16612       if (dwarf_version >= 4 || !dwarf_strict)
16613         {
16614           if (ENUM_IS_SCOPED (type))
16615             add_AT_flag (type_die, DW_AT_enum_class, 1);
16616           if (ENUM_IS_OPAQUE (type))
16617             add_AT_flag (type_die, DW_AT_declaration, 1);
16618         }
16619     }
16620   else if (! TYPE_SIZE (type))
16621     return type_die;
16622   else
16623     remove_AT (type_die, DW_AT_declaration);
16624
16625   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16626      given enum type is incomplete, do not generate the DW_AT_byte_size
16627      attribute or the DW_AT_element_list attribute.  */
16628   if (TYPE_SIZE (type))
16629     {
16630       tree link;
16631
16632       TREE_ASM_WRITTEN (type) = 1;
16633       add_byte_size_attribute (type_die, type);
16634       if (TYPE_STUB_DECL (type) != NULL_TREE)
16635         {
16636           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16637           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16638         }
16639
16640       /* If the first reference to this type was as the return type of an
16641          inline function, then it may not have a parent.  Fix this now.  */
16642       if (type_die->die_parent == NULL)
16643         add_child_die (scope_die_for (type, context_die), type_die);
16644
16645       for (link = TYPE_VALUES (type);
16646            link != NULL; link = TREE_CHAIN (link))
16647         {
16648           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16649           tree value = TREE_VALUE (link);
16650
16651           add_name_attribute (enum_die,
16652                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16653
16654           if (TREE_CODE (value) == CONST_DECL)
16655             value = DECL_INITIAL (value);
16656
16657           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16658             /* DWARF2 does not provide a way of indicating whether or
16659                not enumeration constants are signed or unsigned.  GDB
16660                always assumes the values are signed, so we output all
16661                values as if they were signed.  That means that
16662                enumeration constants with very large unsigned values
16663                will appear to have negative values in the debugger.  */
16664             add_AT_int (enum_die, DW_AT_const_value,
16665                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16666         }
16667     }
16668   else
16669     add_AT_flag (type_die, DW_AT_declaration, 1);
16670
16671   if (get_AT (type_die, DW_AT_name))
16672     add_pubtype (type, type_die);
16673
16674   return type_die;
16675 }
16676
16677 /* Generate a DIE to represent either a real live formal parameter decl or to
16678    represent just the type of some formal parameter position in some function
16679    type.
16680
16681    Note that this routine is a bit unusual because its argument may be a
16682    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16683    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16684    node.  If it's the former then this function is being called to output a
16685    DIE to represent a formal parameter object (or some inlining thereof).  If
16686    it's the latter, then this function is only being called to output a
16687    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16688    argument type of some subprogram type.
16689    If EMIT_NAME_P is true, name and source coordinate attributes
16690    are emitted.  */
16691
16692 static dw_die_ref
16693 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16694                           dw_die_ref context_die)
16695 {
16696   tree node_or_origin = node ? node : origin;
16697   tree ultimate_origin;
16698   dw_die_ref parm_die
16699     = new_die (DW_TAG_formal_parameter, context_die, node);
16700
16701   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16702     {
16703     case tcc_declaration:
16704       ultimate_origin = decl_ultimate_origin (node_or_origin);
16705       if (node || ultimate_origin)
16706         origin = ultimate_origin;
16707       if (origin != NULL)
16708         add_abstract_origin_attribute (parm_die, origin);
16709       else if (emit_name_p)
16710         add_name_and_src_coords_attributes (parm_die, node);
16711       if (origin == NULL
16712           || (! DECL_ABSTRACT (node_or_origin)
16713               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16714                                            decl_function_context
16715                                                             (node_or_origin))))
16716         {
16717           tree type = TREE_TYPE (node_or_origin);
16718           if (decl_by_reference_p (node_or_origin))
16719             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16720                                 context_die);
16721           else
16722             add_type_attribute (parm_die, type,
16723                                 TREE_READONLY (node_or_origin),
16724                                 TREE_THIS_VOLATILE (node_or_origin),
16725                                 context_die);
16726         }
16727       if (origin == NULL && DECL_ARTIFICIAL (node))
16728         add_AT_flag (parm_die, DW_AT_artificial, 1);
16729
16730       if (node && node != origin)
16731         equate_decl_number_to_die (node, parm_die);
16732       if (! DECL_ABSTRACT (node_or_origin))
16733         add_location_or_const_value_attribute (parm_die, node_or_origin,
16734                                                node == NULL, DW_AT_location);
16735
16736       break;
16737
16738     case tcc_type:
16739       /* We were called with some kind of a ..._TYPE node.  */
16740       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16741       break;
16742
16743     default:
16744       gcc_unreachable ();
16745     }
16746
16747   return parm_die;
16748 }
16749
16750 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16751    children DW_TAG_formal_parameter DIEs representing the arguments of the
16752    parameter pack.
16753
16754    PARM_PACK must be a function parameter pack.
16755    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16756    must point to the subsequent arguments of the function PACK_ARG belongs to.
16757    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16758    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16759    following the last one for which a DIE was generated.  */
16760
16761 static dw_die_ref
16762 gen_formal_parameter_pack_die  (tree parm_pack,
16763                                 tree pack_arg,
16764                                 dw_die_ref subr_die,
16765                                 tree *next_arg)
16766 {
16767   tree arg;
16768   dw_die_ref parm_pack_die;
16769
16770   gcc_assert (parm_pack
16771               && lang_hooks.function_parameter_pack_p (parm_pack)
16772               && subr_die);
16773
16774   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16775   add_src_coords_attributes (parm_pack_die, parm_pack);
16776
16777   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16778     {
16779       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16780                                                                  parm_pack))
16781         break;
16782       gen_formal_parameter_die (arg, NULL,
16783                                 false /* Don't emit name attribute.  */,
16784                                 parm_pack_die);
16785     }
16786   if (next_arg)
16787     *next_arg = arg;
16788   return parm_pack_die;
16789 }
16790
16791 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16792    at the end of an (ANSI prototyped) formal parameters list.  */
16793
16794 static void
16795 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16796 {
16797   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16798 }
16799
16800 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16801    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16802    parameters as specified in some function type specification (except for
16803    those which appear as part of a function *definition*).  */
16804
16805 static void
16806 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16807 {
16808   tree link;
16809   tree formal_type = NULL;
16810   tree first_parm_type;
16811   tree arg;
16812
16813   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16814     {
16815       arg = DECL_ARGUMENTS (function_or_method_type);
16816       function_or_method_type = TREE_TYPE (function_or_method_type);
16817     }
16818   else
16819     arg = NULL_TREE;
16820
16821   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16822
16823   /* Make our first pass over the list of formal parameter types and output a
16824      DW_TAG_formal_parameter DIE for each one.  */
16825   for (link = first_parm_type; link; )
16826     {
16827       dw_die_ref parm_die;
16828
16829       formal_type = TREE_VALUE (link);
16830       if (formal_type == void_type_node)
16831         break;
16832
16833       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16834       parm_die = gen_formal_parameter_die (formal_type, NULL,
16835                                            true /* Emit name attribute.  */,
16836                                            context_die);
16837       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16838           && link == first_parm_type)
16839         {
16840           add_AT_flag (parm_die, DW_AT_artificial, 1);
16841           if (dwarf_version >= 3 || !dwarf_strict)
16842             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16843         }
16844       else if (arg && DECL_ARTIFICIAL (arg))
16845         add_AT_flag (parm_die, DW_AT_artificial, 1);
16846
16847       link = TREE_CHAIN (link);
16848       if (arg)
16849         arg = DECL_CHAIN (arg);
16850     }
16851
16852   /* If this function type has an ellipsis, add a
16853      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16854   if (formal_type != void_type_node)
16855     gen_unspecified_parameters_die (function_or_method_type, context_die);
16856
16857   /* Make our second (and final) pass over the list of formal parameter types
16858      and output DIEs to represent those types (as necessary).  */
16859   for (link = TYPE_ARG_TYPES (function_or_method_type);
16860        link && TREE_VALUE (link);
16861        link = TREE_CHAIN (link))
16862     gen_type_die (TREE_VALUE (link), context_die);
16863 }
16864
16865 /* We want to generate the DIE for TYPE so that we can generate the
16866    die for MEMBER, which has been defined; we will need to refer back
16867    to the member declaration nested within TYPE.  If we're trying to
16868    generate minimal debug info for TYPE, processing TYPE won't do the
16869    trick; we need to attach the member declaration by hand.  */
16870
16871 static void
16872 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16873 {
16874   gen_type_die (type, context_die);
16875
16876   /* If we're trying to avoid duplicate debug info, we may not have
16877      emitted the member decl for this function.  Emit it now.  */
16878   if (TYPE_STUB_DECL (type)
16879       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16880       && ! lookup_decl_die (member))
16881     {
16882       dw_die_ref type_die;
16883       gcc_assert (!decl_ultimate_origin (member));
16884
16885       push_decl_scope (type);
16886       type_die = lookup_type_die_strip_naming_typedef (type);
16887       if (TREE_CODE (member) == FUNCTION_DECL)
16888         gen_subprogram_die (member, type_die);
16889       else if (TREE_CODE (member) == FIELD_DECL)
16890         {
16891           /* Ignore the nameless fields that are used to skip bits but handle
16892              C++ anonymous unions and structs.  */
16893           if (DECL_NAME (member) != NULL_TREE
16894               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16895               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16896             {
16897               gen_type_die (member_declared_type (member), type_die);
16898               gen_field_die (member, type_die);
16899             }
16900         }
16901       else
16902         gen_variable_die (member, NULL_TREE, type_die);
16903
16904       pop_decl_scope ();
16905     }
16906 }
16907
16908 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16909    may later generate inlined and/or out-of-line instances of.  */
16910
16911 static void
16912 dwarf2out_abstract_function (tree decl)
16913 {
16914   dw_die_ref old_die;
16915   tree save_fn;
16916   tree context;
16917   int was_abstract;
16918   htab_t old_decl_loc_table;
16919   htab_t old_cached_dw_loc_list_table;
16920   int old_call_site_count, old_tail_call_site_count;
16921   struct call_arg_loc_node *old_call_arg_locations;
16922
16923   /* Make sure we have the actual abstract inline, not a clone.  */
16924   decl = DECL_ORIGIN (decl);
16925
16926   old_die = lookup_decl_die (decl);
16927   if (old_die && get_AT (old_die, DW_AT_inline))
16928     /* We've already generated the abstract instance.  */
16929     return;
16930
16931   /* We can be called while recursively when seeing block defining inlined subroutine
16932      DIE.  Be sure to not clobber the outer location table nor use it or we would
16933      get locations in abstract instantces.  */
16934   old_decl_loc_table = decl_loc_table;
16935   decl_loc_table = NULL;
16936   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16937   cached_dw_loc_list_table = NULL;
16938   old_call_arg_locations = call_arg_locations;
16939   call_arg_locations = NULL;
16940   old_call_site_count = call_site_count;
16941   call_site_count = -1;
16942   old_tail_call_site_count = tail_call_site_count;
16943   tail_call_site_count = -1;
16944
16945   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16946      we don't get confused by DECL_ABSTRACT.  */
16947   if (debug_info_level > DINFO_LEVEL_TERSE)
16948     {
16949       context = decl_class_context (decl);
16950       if (context)
16951         gen_type_die_for_member
16952           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16953     }
16954
16955   /* Pretend we've just finished compiling this function.  */
16956   save_fn = current_function_decl;
16957   current_function_decl = decl;
16958   push_cfun (DECL_STRUCT_FUNCTION (decl));
16959
16960   was_abstract = DECL_ABSTRACT (decl);
16961   set_decl_abstract_flags (decl, 1);
16962   dwarf2out_decl (decl);
16963   if (! was_abstract)
16964     set_decl_abstract_flags (decl, 0);
16965
16966   current_function_decl = save_fn;
16967   decl_loc_table = old_decl_loc_table;
16968   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16969   call_arg_locations = old_call_arg_locations;
16970   call_site_count = old_call_site_count;
16971   tail_call_site_count = old_tail_call_site_count;
16972   pop_cfun ();
16973 }
16974
16975 /* Helper function of premark_used_types() which gets called through
16976    htab_traverse.
16977
16978    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16979    marked as unused by prune_unused_types.  */
16980
16981 static int
16982 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16983 {
16984   tree type;
16985   dw_die_ref die;
16986
16987   type = (tree) *slot;
16988   die = lookup_type_die (type);
16989   if (die != NULL)
16990     die->die_perennial_p = 1;
16991   return 1;
16992 }
16993
16994 /* Helper function of premark_types_used_by_global_vars which gets called
16995    through htab_traverse.
16996
16997    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16998    marked as unused by prune_unused_types. The DIE of the type is marked
16999    only if the global variable using the type will actually be emitted.  */
17000
17001 static int
17002 premark_types_used_by_global_vars_helper (void **slot,
17003                                           void *data ATTRIBUTE_UNUSED)
17004 {
17005   struct types_used_by_vars_entry *entry;
17006   dw_die_ref die;
17007
17008   entry = (struct types_used_by_vars_entry *) *slot;
17009   gcc_assert (entry->type != NULL
17010               && entry->var_decl != NULL);
17011   die = lookup_type_die (entry->type);
17012   if (die)
17013     {
17014       /* Ask cgraph if the global variable really is to be emitted.
17015          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17016       struct varpool_node *node = varpool_get_node (entry->var_decl);
17017       if (node && node->needed)
17018         {
17019           die->die_perennial_p = 1;
17020           /* Keep the parent DIEs as well.  */
17021           while ((die = die->die_parent) && die->die_perennial_p == 0)
17022             die->die_perennial_p = 1;
17023         }
17024     }
17025   return 1;
17026 }
17027
17028 /* Mark all members of used_types_hash as perennial.  */
17029
17030 static void
17031 premark_used_types (void)
17032 {
17033   if (cfun && cfun->used_types_hash)
17034     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17035 }
17036
17037 /* Mark all members of types_used_by_vars_entry as perennial.  */
17038
17039 static void
17040 premark_types_used_by_global_vars (void)
17041 {
17042   if (types_used_by_vars_hash)
17043     htab_traverse (types_used_by_vars_hash,
17044                    premark_types_used_by_global_vars_helper, NULL);
17045 }
17046
17047 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17048    for CA_LOC call arg loc node.  */
17049
17050 static dw_die_ref
17051 gen_call_site_die (tree decl, dw_die_ref subr_die,
17052                    struct call_arg_loc_node *ca_loc)
17053 {
17054   dw_die_ref stmt_die = NULL, die;
17055   tree block = ca_loc->block;
17056
17057   while (block
17058          && block != DECL_INITIAL (decl)
17059          && TREE_CODE (block) == BLOCK)
17060     {
17061       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17062         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17063       if (stmt_die)
17064         break;
17065       block = BLOCK_SUPERCONTEXT (block);
17066     }
17067   if (stmt_die == NULL)
17068     stmt_die = subr_die;
17069   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17070   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17071   if (ca_loc->tail_call_p)
17072     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17073   if (ca_loc->symbol_ref)
17074     {
17075       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17076       if (tdie)
17077         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17078       else
17079         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17080     }
17081   return die;
17082 }
17083
17084 /* Generate a DIE to represent a declared function (either file-scope or
17085    block-local).  */
17086
17087 static void
17088 gen_subprogram_die (tree decl, dw_die_ref context_die)
17089 {
17090   tree origin = decl_ultimate_origin (decl);
17091   dw_die_ref subr_die;
17092   tree outer_scope;
17093   dw_die_ref old_die = lookup_decl_die (decl);
17094   int declaration = (current_function_decl != decl
17095                      || class_or_namespace_scope_p (context_die));
17096
17097   premark_used_types ();
17098
17099   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17100      started to generate the abstract instance of an inline, decided to output
17101      its containing class, and proceeded to emit the declaration of the inline
17102      from the member list for the class.  If so, DECLARATION takes priority;
17103      we'll get back to the abstract instance when done with the class.  */
17104
17105   /* The class-scope declaration DIE must be the primary DIE.  */
17106   if (origin && declaration && class_or_namespace_scope_p (context_die))
17107     {
17108       origin = NULL;
17109       gcc_assert (!old_die);
17110     }
17111
17112   /* Now that the C++ front end lazily declares artificial member fns, we
17113      might need to retrofit the declaration into its class.  */
17114   if (!declaration && !origin && !old_die
17115       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17116       && !class_or_namespace_scope_p (context_die)
17117       && debug_info_level > DINFO_LEVEL_TERSE)
17118     old_die = force_decl_die (decl);
17119
17120   if (origin != NULL)
17121     {
17122       gcc_assert (!declaration || local_scope_p (context_die));
17123
17124       /* Fixup die_parent for the abstract instance of a nested
17125          inline function.  */
17126       if (old_die && old_die->die_parent == NULL)
17127         add_child_die (context_die, old_die);
17128
17129       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17130       add_abstract_origin_attribute (subr_die, origin);
17131       /*  This is where the actual code for a cloned function is.
17132           Let's emit linkage name attribute for it.  This helps
17133           debuggers to e.g, set breakpoints into
17134           constructors/destructors when the user asks "break
17135           K::K".  */
17136       add_linkage_name (subr_die, decl);
17137     }
17138   else if (old_die)
17139     {
17140       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17141       struct dwarf_file_data * file_index = lookup_filename (s.file);
17142
17143       if (!get_AT_flag (old_die, DW_AT_declaration)
17144           /* We can have a normal definition following an inline one in the
17145              case of redefinition of GNU C extern inlines.
17146              It seems reasonable to use AT_specification in this case.  */
17147           && !get_AT (old_die, DW_AT_inline))
17148         {
17149           /* Detect and ignore this case, where we are trying to output
17150              something we have already output.  */
17151           return;
17152         }
17153
17154       /* If the definition comes from the same place as the declaration,
17155          maybe use the old DIE.  We always want the DIE for this function
17156          that has the *_pc attributes to be under comp_unit_die so the
17157          debugger can find it.  We also need to do this for abstract
17158          instances of inlines, since the spec requires the out-of-line copy
17159          to have the same parent.  For local class methods, this doesn't
17160          apply; we just use the old DIE.  */
17161       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17162           && (DECL_ARTIFICIAL (decl)
17163               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17164                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17165                       == (unsigned) s.line))))
17166         {
17167           subr_die = old_die;
17168
17169           /* Clear out the declaration attribute and the formal parameters.
17170              Do not remove all children, because it is possible that this
17171              declaration die was forced using force_decl_die(). In such
17172              cases die that forced declaration die (e.g. TAG_imported_module)
17173              is one of the children that we do not want to remove.  */
17174           remove_AT (subr_die, DW_AT_declaration);
17175           remove_AT (subr_die, DW_AT_object_pointer);
17176           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17177         }
17178       else
17179         {
17180           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17181           add_AT_specification (subr_die, old_die);
17182           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17183             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17184           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17185             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17186         }
17187     }
17188   else
17189     {
17190       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17191
17192       if (TREE_PUBLIC (decl))
17193         add_AT_flag (subr_die, DW_AT_external, 1);
17194
17195       add_name_and_src_coords_attributes (subr_die, decl);
17196       if (debug_info_level > DINFO_LEVEL_TERSE)
17197         {
17198           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17199           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17200                               0, 0, context_die);
17201         }
17202
17203       add_pure_or_virtual_attribute (subr_die, decl);
17204       if (DECL_ARTIFICIAL (decl))
17205         add_AT_flag (subr_die, DW_AT_artificial, 1);
17206
17207       add_accessibility_attribute (subr_die, decl);
17208     }
17209
17210   if (declaration)
17211     {
17212       if (!old_die || !get_AT (old_die, DW_AT_inline))
17213         {
17214           add_AT_flag (subr_die, DW_AT_declaration, 1);
17215
17216           /* If this is an explicit function declaration then generate
17217              a DW_AT_explicit attribute.  */
17218           if (lang_hooks.decls.function_decl_explicit_p (decl)
17219               && (dwarf_version >= 3 || !dwarf_strict))
17220             add_AT_flag (subr_die, DW_AT_explicit, 1);
17221
17222           /* The first time we see a member function, it is in the context of
17223              the class to which it belongs.  We make sure of this by emitting
17224              the class first.  The next time is the definition, which is
17225              handled above.  The two may come from the same source text.
17226
17227              Note that force_decl_die() forces function declaration die. It is
17228              later reused to represent definition.  */
17229           equate_decl_number_to_die (decl, subr_die);
17230         }
17231     }
17232   else if (DECL_ABSTRACT (decl))
17233     {
17234       if (DECL_DECLARED_INLINE_P (decl))
17235         {
17236           if (cgraph_function_possibly_inlined_p (decl))
17237             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17238           else
17239             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17240         }
17241       else
17242         {
17243           if (cgraph_function_possibly_inlined_p (decl))
17244             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17245           else
17246             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17247         }
17248
17249       if (DECL_DECLARED_INLINE_P (decl)
17250           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17251         add_AT_flag (subr_die, DW_AT_artificial, 1);
17252
17253       equate_decl_number_to_die (decl, subr_die);
17254     }
17255   else if (!DECL_EXTERNAL (decl))
17256     {
17257       HOST_WIDE_INT cfa_fb_offset;
17258
17259       if (!old_die || !get_AT (old_die, DW_AT_inline))
17260         equate_decl_number_to_die (decl, subr_die);
17261
17262       if (!flag_reorder_blocks_and_partition)
17263         {
17264           dw_fde_ref fde = cfun->fde;
17265           if (fde->dw_fde_begin)
17266             {
17267               /* We have already generated the labels.  */
17268               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17269               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17270             }
17271           else
17272             {
17273               /* Create start/end labels and add the range.  */
17274               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17275               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17276                                            current_function_funcdef_no);
17277               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17278               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17279                                            current_function_funcdef_no);
17280               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17281             }
17282
17283 #if VMS_DEBUGGING_INFO
17284       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17285          Section 2.3 Prologue and Epilogue Attributes:
17286          When a breakpoint is set on entry to a function, it is generally
17287          desirable for execution to be suspended, not on the very first
17288          instruction of the function, but rather at a point after the
17289          function's frame has been set up, after any language defined local
17290          declaration processing has been completed, and before execution of
17291          the first statement of the function begins. Debuggers generally
17292          cannot properly determine where this point is.  Similarly for a
17293          breakpoint set on exit from a function. The prologue and epilogue
17294          attributes allow a compiler to communicate the location(s) to use.  */
17295
17296       {
17297         if (fde->dw_fde_vms_end_prologue)
17298           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17299             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17300
17301         if (fde->dw_fde_vms_begin_epilogue)
17302           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17303             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17304       }
17305 #endif
17306
17307           add_pubname (decl, subr_die);
17308         }
17309       else
17310         {
17311           /* Generate pubnames entries for the split function code ranges.  */
17312           dw_fde_ref fde = cfun->fde;
17313
17314           if (fde->dw_fde_second_begin)
17315             {
17316               if (dwarf_version >= 3 || !dwarf_strict)
17317                 {
17318                   /* We should use ranges for non-contiguous code section 
17319                      addresses.  Use the actual code range for the initial
17320                      section, since the HOT/COLD labels might precede an 
17321                      alignment offset.  */
17322                   bool range_list_added = false;
17323                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17324                                         fde->dw_fde_end, &range_list_added);
17325                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17326                                         fde->dw_fde_second_end,
17327                                         &range_list_added);
17328                   add_pubname (decl, subr_die);
17329                   if (range_list_added)
17330                     add_ranges (NULL);
17331                 }
17332               else
17333                 {
17334                   /* There is no real support in DW2 for this .. so we make
17335                      a work-around.  First, emit the pub name for the segment
17336                      containing the function label.  Then make and emit a
17337                      simplified subprogram DIE for the second segment with the
17338                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17339                      linkage name for the second die so that gdb will find both
17340                      sections when given "b foo".  */
17341                   const char *name = NULL;
17342                   tree decl_name = DECL_NAME (decl);
17343                   dw_die_ref seg_die;
17344
17345                   /* Do the 'primary' section.   */
17346                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17347                                  fde->dw_fde_begin);
17348                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17349                                  fde->dw_fde_end);
17350                   /* Add it.   */
17351                   add_pubname (decl, subr_die);
17352
17353                   /* Build a minimal DIE for the secondary section.  */
17354                   seg_die = new_die (DW_TAG_subprogram,
17355                                      subr_die->die_parent, decl);
17356
17357                   if (TREE_PUBLIC (decl))
17358                     add_AT_flag (seg_die, DW_AT_external, 1);
17359
17360                   if (decl_name != NULL 
17361                       && IDENTIFIER_POINTER (decl_name) != NULL)
17362                     {
17363                       name = dwarf2_name (decl, 1);
17364                       if (! DECL_ARTIFICIAL (decl))
17365                         add_src_coords_attributes (seg_die, decl);
17366
17367                       add_linkage_name (seg_die, decl);
17368                     }
17369                   gcc_assert (name != NULL);
17370                   add_pure_or_virtual_attribute (seg_die, decl);
17371                   if (DECL_ARTIFICIAL (decl))
17372                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17373
17374                   name = concat ("__second_sect_of_", name, NULL); 
17375                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17376                                  fde->dw_fde_second_begin);
17377                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17378                                  fde->dw_fde_second_end);
17379                   add_name_attribute (seg_die, name);
17380                   add_pubname_string (name, seg_die);
17381                 }
17382             }
17383           else
17384             {
17385               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17386               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17387               add_pubname (decl, subr_die);
17388             }
17389         }
17390
17391 #ifdef MIPS_DEBUGGING_INFO
17392       /* Add a reference to the FDE for this routine.  */
17393       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17394 #endif
17395
17396       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17397
17398       /* We define the "frame base" as the function's CFA.  This is more
17399          convenient for several reasons: (1) It's stable across the prologue
17400          and epilogue, which makes it better than just a frame pointer,
17401          (2) With dwarf3, there exists a one-byte encoding that allows us
17402          to reference the .debug_frame data by proxy, but failing that,
17403          (3) We can at least reuse the code inspection and interpretation
17404          code that determines the CFA position at various points in the
17405          function.  */
17406       if (dwarf_version >= 3)
17407         {
17408           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17409           add_AT_loc (subr_die, DW_AT_frame_base, op);
17410         }
17411       else
17412         {
17413           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17414           if (list->dw_loc_next)
17415             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17416           else
17417             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17418         }
17419
17420       /* Compute a displacement from the "steady-state frame pointer" to
17421          the CFA.  The former is what all stack slots and argument slots
17422          will reference in the rtl; the later is what we've told the
17423          debugger about.  We'll need to adjust all frame_base references
17424          by this displacement.  */
17425       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17426
17427       if (cfun->static_chain_decl)
17428         add_AT_location_description (subr_die, DW_AT_static_link,
17429                  loc_list_from_tree (cfun->static_chain_decl, 2));
17430     }
17431
17432   /* Generate child dies for template paramaters.  */
17433   if (debug_info_level > DINFO_LEVEL_TERSE)
17434     gen_generic_params_dies (decl);
17435
17436   /* Now output descriptions of the arguments for this function. This gets
17437      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17438      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17439      `...' at the end of the formal parameter list.  In order to find out if
17440      there was a trailing ellipsis or not, we must instead look at the type
17441      associated with the FUNCTION_DECL.  This will be a node of type
17442      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17443      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17444      an ellipsis at the end.  */
17445
17446   /* In the case where we are describing a mere function declaration, all we
17447      need to do here (and all we *can* do here) is to describe the *types* of
17448      its formal parameters.  */
17449   if (debug_info_level <= DINFO_LEVEL_TERSE)
17450     ;
17451   else if (declaration)
17452     gen_formal_types_die (decl, subr_die);
17453   else
17454     {
17455       /* Generate DIEs to represent all known formal parameters.  */
17456       tree parm = DECL_ARGUMENTS (decl);
17457       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17458       tree generic_decl_parm = generic_decl
17459                                 ? DECL_ARGUMENTS (generic_decl)
17460                                 : NULL;
17461
17462       /* Now we want to walk the list of parameters of the function and
17463          emit their relevant DIEs.
17464
17465          We consider the case of DECL being an instance of a generic function
17466          as well as it being a normal function.
17467
17468          If DECL is an instance of a generic function we walk the
17469          parameters of the generic function declaration _and_ the parameters of
17470          DECL itself. This is useful because we want to emit specific DIEs for
17471          function parameter packs and those are declared as part of the
17472          generic function declaration. In that particular case,
17473          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17474          That DIE has children DIEs representing the set of arguments
17475          of the pack. Note that the set of pack arguments can be empty.
17476          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17477          children DIE.
17478
17479          Otherwise, we just consider the parameters of DECL.  */
17480       while (generic_decl_parm || parm)
17481         {
17482           if (generic_decl_parm
17483               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17484             gen_formal_parameter_pack_die (generic_decl_parm,
17485                                            parm, subr_die,
17486                                            &parm);
17487           else if (parm)
17488             {
17489               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17490
17491               if (parm == DECL_ARGUMENTS (decl)
17492                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17493                   && parm_die
17494                   && (dwarf_version >= 3 || !dwarf_strict))
17495                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17496
17497               parm = DECL_CHAIN (parm);
17498             }
17499
17500           if (generic_decl_parm)
17501             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17502         }
17503
17504       /* Decide whether we need an unspecified_parameters DIE at the end.
17505          There are 2 more cases to do this for: 1) the ansi ... declaration -
17506          this is detectable when the end of the arg list is not a
17507          void_type_node 2) an unprototyped function declaration (not a
17508          definition).  This just means that we have no info about the
17509          parameters at all.  */
17510       if (prototype_p (TREE_TYPE (decl)))
17511         {
17512           /* This is the prototyped case, check for....  */
17513           if (stdarg_p (TREE_TYPE (decl)))
17514             gen_unspecified_parameters_die (decl, subr_die);
17515         }
17516       else if (DECL_INITIAL (decl) == NULL_TREE)
17517         gen_unspecified_parameters_die (decl, subr_die);
17518     }
17519
17520   /* Output Dwarf info for all of the stuff within the body of the function
17521      (if it has one - it may be just a declaration).  */
17522   outer_scope = DECL_INITIAL (decl);
17523
17524   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17525      a function.  This BLOCK actually represents the outermost binding contour
17526      for the function, i.e. the contour in which the function's formal
17527      parameters and labels get declared. Curiously, it appears that the front
17528      end doesn't actually put the PARM_DECL nodes for the current function onto
17529      the BLOCK_VARS list for this outer scope, but are strung off of the
17530      DECL_ARGUMENTS list for the function instead.
17531
17532      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17533      the LABEL_DECL nodes for the function however, and we output DWARF info
17534      for those in decls_for_scope.  Just within the `outer_scope' there will be
17535      a BLOCK node representing the function's outermost pair of curly braces,
17536      and any blocks used for the base and member initializers of a C++
17537      constructor function.  */
17538   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17539     {
17540       int call_site_note_count = 0;
17541       int tail_call_site_note_count = 0;
17542
17543       /* Emit a DW_TAG_variable DIE for a named return value.  */
17544       if (DECL_NAME (DECL_RESULT (decl)))
17545         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17546
17547       current_function_has_inlines = 0;
17548       decls_for_scope (outer_scope, subr_die, 0);
17549
17550       if (call_arg_locations && !dwarf_strict)
17551         {
17552           struct call_arg_loc_node *ca_loc;
17553           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17554             {
17555               dw_die_ref die = NULL;
17556               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17557               rtx arg, next_arg;
17558
17559               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17560                    arg; arg = next_arg)
17561                 {
17562                   dw_loc_descr_ref reg, val;
17563                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17564                   dw_die_ref cdie, tdie = NULL;
17565
17566                   next_arg = XEXP (arg, 1);
17567                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17568                       && next_arg
17569                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17570                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17571                       && REGNO (XEXP (XEXP (arg, 0), 0))
17572                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17573                     next_arg = XEXP (next_arg, 1);
17574                   if (mode == VOIDmode)
17575                     {
17576                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17577                       if (mode == VOIDmode)
17578                         mode = GET_MODE (XEXP (arg, 0));
17579                     }
17580                   if (mode == VOIDmode || mode == BLKmode)
17581                     continue;
17582                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17583                     {
17584                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17585                       tloc = XEXP (XEXP (arg, 0), 1);
17586                       continue;
17587                     }
17588                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17589                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17590                     {
17591                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17592                       tlocc = XEXP (XEXP (arg, 0), 1);
17593                       continue;
17594                     }
17595                   reg = NULL;
17596                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17597                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17598                                               VAR_INIT_STATUS_INITIALIZED);
17599                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17600                     {
17601                       rtx mem = XEXP (XEXP (arg, 0), 0);
17602                       reg = mem_loc_descriptor (XEXP (mem, 0),
17603                                                 get_address_mode (mem),
17604                                                 GET_MODE (mem),
17605                                                 VAR_INIT_STATUS_INITIALIZED);
17606                     }
17607                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17608                            == DEBUG_PARAMETER_REF)
17609                     {
17610                       tree tdecl
17611                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17612                       tdie = lookup_decl_die (tdecl);
17613                       if (tdie == NULL)
17614                         continue;
17615                     }
17616                   else
17617                     continue;
17618                   if (reg == NULL
17619                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17620                          != DEBUG_PARAMETER_REF)
17621                     continue;
17622                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17623                                             VOIDmode,
17624                                             VAR_INIT_STATUS_INITIALIZED);
17625                   if (val == NULL)
17626                     continue;
17627                   if (die == NULL)
17628                     die = gen_call_site_die (decl, subr_die, ca_loc);
17629                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17630                                   NULL_TREE);
17631                   if (reg != NULL)
17632                     add_AT_loc (cdie, DW_AT_location, reg);
17633                   else if (tdie != NULL)
17634                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17635                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17636                   if (next_arg != XEXP (arg, 1))
17637                     {
17638                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17639                       if (mode == VOIDmode)
17640                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17641                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17642                                                             0), 1),
17643                                                 mode, VOIDmode,
17644                                                 VAR_INIT_STATUS_INITIALIZED);
17645                       if (val != NULL)
17646                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17647                     }
17648                 }
17649               if (die == NULL
17650                   && (ca_loc->symbol_ref || tloc))
17651                 die = gen_call_site_die (decl, subr_die, ca_loc);
17652               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17653                 {
17654                   dw_loc_descr_ref tval = NULL;
17655
17656                   if (tloc != NULL_RTX)
17657                     tval = mem_loc_descriptor (tloc,
17658                                                GET_MODE (tloc) == VOIDmode
17659                                                ? Pmode : GET_MODE (tloc),
17660                                                VOIDmode,
17661                                                VAR_INIT_STATUS_INITIALIZED);
17662                   if (tval)
17663                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17664                   else if (tlocc != NULL_RTX)
17665                     {
17666                       tval = mem_loc_descriptor (tlocc,
17667                                                  GET_MODE (tlocc) == VOIDmode
17668                                                  ? Pmode : GET_MODE (tlocc),
17669                                                  VOIDmode,
17670                                                  VAR_INIT_STATUS_INITIALIZED);
17671                       if (tval)
17672                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17673                                     tval);
17674                     }
17675                 }
17676               if (die != NULL)
17677                 {
17678                   call_site_note_count++;
17679                   if (ca_loc->tail_call_p)
17680                     tail_call_site_note_count++;
17681                 }
17682             }
17683         }
17684       call_arg_locations = NULL;
17685       call_arg_loc_last = NULL;
17686       if (tail_call_site_count >= 0
17687           && tail_call_site_count == tail_call_site_note_count
17688           && !dwarf_strict)
17689         {
17690           if (call_site_count >= 0
17691               && call_site_count == call_site_note_count)
17692             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17693           else
17694             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17695         }
17696       call_site_count = -1;
17697       tail_call_site_count = -1;
17698     }
17699   /* Add the calling convention attribute if requested.  */
17700   add_calling_convention_attribute (subr_die, decl);
17701
17702 }
17703
17704 /* Returns a hash value for X (which really is a die_struct).  */
17705
17706 static hashval_t
17707 common_block_die_table_hash (const void *x)
17708 {
17709   const_dw_die_ref d = (const_dw_die_ref) x;
17710   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17711 }
17712
17713 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17714    as decl_id and die_parent of die_struct Y.  */
17715
17716 static int
17717 common_block_die_table_eq (const void *x, const void *y)
17718 {
17719   const_dw_die_ref d = (const_dw_die_ref) x;
17720   const_dw_die_ref e = (const_dw_die_ref) y;
17721   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17722 }
17723
17724 /* Generate a DIE to represent a declared data object.
17725    Either DECL or ORIGIN must be non-null.  */
17726
17727 static void
17728 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17729 {
17730   HOST_WIDE_INT off;
17731   tree com_decl;
17732   tree decl_or_origin = decl ? decl : origin;
17733   tree ultimate_origin;
17734   dw_die_ref var_die;
17735   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17736   dw_die_ref origin_die;
17737   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17738                       || class_or_namespace_scope_p (context_die));
17739   bool specialization_p = false;
17740
17741   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17742   if (decl || ultimate_origin)
17743     origin = ultimate_origin;
17744   com_decl = fortran_common (decl_or_origin, &off);
17745
17746   /* Symbol in common gets emitted as a child of the common block, in the form
17747      of a data member.  */
17748   if (com_decl)
17749     {
17750       dw_die_ref com_die;
17751       dw_loc_list_ref loc;
17752       die_node com_die_arg;
17753
17754       var_die = lookup_decl_die (decl_or_origin);
17755       if (var_die)
17756         {
17757           if (get_AT (var_die, DW_AT_location) == NULL)
17758             {
17759               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17760               if (loc)
17761                 {
17762                   if (off)
17763                     {
17764                       /* Optimize the common case.  */
17765                       if (single_element_loc_list_p (loc)
17766                           && loc->expr->dw_loc_opc == DW_OP_addr
17767                           && loc->expr->dw_loc_next == NULL
17768                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17769                              == SYMBOL_REF)
17770                         loc->expr->dw_loc_oprnd1.v.val_addr
17771                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17772                         else
17773                           loc_list_plus_const (loc, off);
17774                     }
17775                   add_AT_location_description (var_die, DW_AT_location, loc);
17776                   remove_AT (var_die, DW_AT_declaration);
17777                 }
17778             }
17779           return;
17780         }
17781
17782       if (common_block_die_table == NULL)
17783         common_block_die_table
17784           = htab_create_ggc (10, common_block_die_table_hash,
17785                              common_block_die_table_eq, NULL);
17786
17787       com_die_arg.decl_id = DECL_UID (com_decl);
17788       com_die_arg.die_parent = context_die;
17789       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17790       loc = loc_list_from_tree (com_decl, 2);
17791       if (com_die == NULL)
17792         {
17793           const char *cnam
17794             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17795           void **slot;
17796
17797           com_die = new_die (DW_TAG_common_block, context_die, decl);
17798           add_name_and_src_coords_attributes (com_die, com_decl);
17799           if (loc)
17800             {
17801               add_AT_location_description (com_die, DW_AT_location, loc);
17802               /* Avoid sharing the same loc descriptor between
17803                  DW_TAG_common_block and DW_TAG_variable.  */
17804               loc = loc_list_from_tree (com_decl, 2);
17805             }
17806           else if (DECL_EXTERNAL (decl))
17807             add_AT_flag (com_die, DW_AT_declaration, 1);
17808           add_pubname_string (cnam, com_die); /* ??? needed? */
17809           com_die->decl_id = DECL_UID (com_decl);
17810           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17811           *slot = (void *) com_die;
17812         }
17813       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17814         {
17815           add_AT_location_description (com_die, DW_AT_location, loc);
17816           loc = loc_list_from_tree (com_decl, 2);
17817           remove_AT (com_die, DW_AT_declaration);
17818         }
17819       var_die = new_die (DW_TAG_variable, com_die, decl);
17820       add_name_and_src_coords_attributes (var_die, decl);
17821       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17822                           TREE_THIS_VOLATILE (decl), context_die);
17823       add_AT_flag (var_die, DW_AT_external, 1);
17824       if (loc)
17825         {
17826           if (off)
17827             {
17828               /* Optimize the common case.  */
17829               if (single_element_loc_list_p (loc)
17830                   && loc->expr->dw_loc_opc == DW_OP_addr
17831                   && loc->expr->dw_loc_next == NULL
17832                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17833                 loc->expr->dw_loc_oprnd1.v.val_addr
17834                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17835               else
17836                 loc_list_plus_const (loc, off);
17837             }
17838           add_AT_location_description (var_die, DW_AT_location, loc);
17839         }
17840       else if (DECL_EXTERNAL (decl))
17841         add_AT_flag (var_die, DW_AT_declaration, 1);
17842       equate_decl_number_to_die (decl, var_die);
17843       return;
17844     }
17845
17846   /* If the compiler emitted a definition for the DECL declaration
17847      and if we already emitted a DIE for it, don't emit a second
17848      DIE for it again. Allow re-declarations of DECLs that are
17849      inside functions, though.  */
17850   if (old_die && declaration && !local_scope_p (context_die))
17851     return;
17852
17853   /* For static data members, the declaration in the class is supposed
17854      to have DW_TAG_member tag; the specification should still be
17855      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17856   if (declaration && class_scope_p (context_die))
17857     var_die = new_die (DW_TAG_member, context_die, decl);
17858   else
17859     var_die = new_die (DW_TAG_variable, context_die, decl);
17860
17861   origin_die = NULL;
17862   if (origin != NULL)
17863     origin_die = add_abstract_origin_attribute (var_die, origin);
17864
17865   /* Loop unrolling can create multiple blocks that refer to the same
17866      static variable, so we must test for the DW_AT_declaration flag.
17867
17868      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17869      copy decls and set the DECL_ABSTRACT flag on them instead of
17870      sharing them.
17871
17872      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17873
17874      ??? The declare_in_namespace support causes us to get two DIEs for one
17875      variable, both of which are declarations.  We want to avoid considering
17876      one to be a specification, so we must test that this DIE is not a
17877      declaration.  */
17878   else if (old_die && TREE_STATIC (decl) && ! declaration
17879            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17880     {
17881       /* This is a definition of a C++ class level static.  */
17882       add_AT_specification (var_die, old_die);
17883       specialization_p = true;
17884       if (DECL_NAME (decl))
17885         {
17886           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17887           struct dwarf_file_data * file_index = lookup_filename (s.file);
17888
17889           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17890             add_AT_file (var_die, DW_AT_decl_file, file_index);
17891
17892           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17893             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17894
17895           if (old_die->die_tag == DW_TAG_member)
17896             add_linkage_name (var_die, decl);
17897         }
17898     }
17899   else
17900     add_name_and_src_coords_attributes (var_die, decl);
17901
17902   if ((origin == NULL && !specialization_p)
17903       || (origin != NULL
17904           && !DECL_ABSTRACT (decl_or_origin)
17905           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17906                                        decl_function_context
17907                                                         (decl_or_origin))))
17908     {
17909       tree type = TREE_TYPE (decl_or_origin);
17910
17911       if (decl_by_reference_p (decl_or_origin))
17912         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17913       else
17914         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17915                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17916     }
17917
17918   if (origin == NULL && !specialization_p)
17919     {
17920       if (TREE_PUBLIC (decl))
17921         add_AT_flag (var_die, DW_AT_external, 1);
17922
17923       if (DECL_ARTIFICIAL (decl))
17924         add_AT_flag (var_die, DW_AT_artificial, 1);
17925
17926       add_accessibility_attribute (var_die, decl);
17927     }
17928
17929   if (declaration)
17930     add_AT_flag (var_die, DW_AT_declaration, 1);
17931
17932   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17933     equate_decl_number_to_die (decl, var_die);
17934
17935   if (! declaration
17936       && (! DECL_ABSTRACT (decl_or_origin)
17937           /* Local static vars are shared between all clones/inlines,
17938              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17939              already set.  */
17940           || (TREE_CODE (decl_or_origin) == VAR_DECL
17941               && TREE_STATIC (decl_or_origin)
17942               && DECL_RTL_SET_P (decl_or_origin)))
17943       /* When abstract origin already has DW_AT_location attribute, no need
17944          to add it again.  */
17945       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17946     {
17947       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17948           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17949         defer_location (decl_or_origin, var_die);
17950       else
17951         add_location_or_const_value_attribute (var_die, decl_or_origin,
17952                                                decl == NULL, DW_AT_location);
17953       add_pubname (decl_or_origin, var_die);
17954     }
17955   else
17956     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17957 }
17958
17959 /* Generate a DIE to represent a named constant.  */
17960
17961 static void
17962 gen_const_die (tree decl, dw_die_ref context_die)
17963 {
17964   dw_die_ref const_die;
17965   tree type = TREE_TYPE (decl);
17966
17967   const_die = new_die (DW_TAG_constant, context_die, decl);
17968   add_name_and_src_coords_attributes (const_die, decl);
17969   add_type_attribute (const_die, type, 1, 0, context_die);
17970   if (TREE_PUBLIC (decl))
17971     add_AT_flag (const_die, DW_AT_external, 1);
17972   if (DECL_ARTIFICIAL (decl))
17973     add_AT_flag (const_die, DW_AT_artificial, 1);
17974   tree_add_const_value_attribute_for_decl (const_die, decl);
17975 }
17976
17977 /* Generate a DIE to represent a label identifier.  */
17978
17979 static void
17980 gen_label_die (tree decl, dw_die_ref context_die)
17981 {
17982   tree origin = decl_ultimate_origin (decl);
17983   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17984   rtx insn;
17985   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17986
17987   if (origin != NULL)
17988     add_abstract_origin_attribute (lbl_die, origin);
17989   else
17990     add_name_and_src_coords_attributes (lbl_die, decl);
17991
17992   if (DECL_ABSTRACT (decl))
17993     equate_decl_number_to_die (decl, lbl_die);
17994   else
17995     {
17996       insn = DECL_RTL_IF_SET (decl);
17997
17998       /* Deleted labels are programmer specified labels which have been
17999          eliminated because of various optimizations.  We still emit them
18000          here so that it is possible to put breakpoints on them.  */
18001       if (insn
18002           && (LABEL_P (insn)
18003               || ((NOTE_P (insn)
18004                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18005         {
18006           /* When optimization is enabled (via -O) some parts of the compiler
18007              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18008              represent source-level labels which were explicitly declared by
18009              the user.  This really shouldn't be happening though, so catch
18010              it if it ever does happen.  */
18011           gcc_assert (!INSN_DELETED_P (insn));
18012
18013           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18014           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18015         }
18016     }
18017 }
18018
18019 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18020    attributes to the DIE for a block STMT, to describe where the inlined
18021    function was called from.  This is similar to add_src_coords_attributes.  */
18022
18023 static inline void
18024 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18025 {
18026   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18027
18028   if (dwarf_version >= 3 || !dwarf_strict)
18029     {
18030       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18031       add_AT_unsigned (die, DW_AT_call_line, s.line);
18032     }
18033 }
18034
18035
18036 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18037    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18038
18039 static inline void
18040 add_high_low_attributes (tree stmt, dw_die_ref die)
18041 {
18042   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18043
18044   if (BLOCK_FRAGMENT_CHAIN (stmt)
18045       && (dwarf_version >= 3 || !dwarf_strict))
18046     {
18047       tree chain;
18048
18049       if (inlined_function_outer_scope_p (stmt))
18050         {
18051           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18052                                        BLOCK_NUMBER (stmt));
18053           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18054         }
18055
18056       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18057
18058       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18059       do
18060         {
18061           add_ranges (chain);
18062           chain = BLOCK_FRAGMENT_CHAIN (chain);
18063         }
18064       while (chain);
18065       add_ranges (NULL);
18066     }
18067   else
18068     {
18069       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18070                                    BLOCK_NUMBER (stmt));
18071       add_AT_lbl_id (die, DW_AT_low_pc, label);
18072       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18073                                    BLOCK_NUMBER (stmt));
18074       add_AT_lbl_id (die, DW_AT_high_pc, label);
18075     }
18076 }
18077
18078 /* Generate a DIE for a lexical block.  */
18079
18080 static void
18081 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18082 {
18083   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18084
18085   if (call_arg_locations)
18086     {
18087       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18088         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18089                                BLOCK_NUMBER (stmt) + 1);
18090       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18091     }
18092
18093   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18094     add_high_low_attributes (stmt, stmt_die);
18095
18096   decls_for_scope (stmt, stmt_die, depth);
18097 }
18098
18099 /* Generate a DIE for an inlined subprogram.  */
18100
18101 static void
18102 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18103 {
18104   tree decl;
18105
18106   /* The instance of function that is effectively being inlined shall not
18107      be abstract.  */
18108   gcc_assert (! BLOCK_ABSTRACT (stmt));
18109
18110   decl = block_ultimate_origin (stmt);
18111
18112   /* Emit info for the abstract instance first, if we haven't yet.  We
18113      must emit this even if the block is abstract, otherwise when we
18114      emit the block below (or elsewhere), we may end up trying to emit
18115      a die whose origin die hasn't been emitted, and crashing.  */
18116   dwarf2out_abstract_function (decl);
18117
18118   if (! BLOCK_ABSTRACT (stmt))
18119     {
18120       dw_die_ref subr_die
18121         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18122
18123       if (call_arg_locations)
18124         {
18125           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18126             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18127                                    BLOCK_NUMBER (stmt) + 1);
18128           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18129         }
18130       add_abstract_origin_attribute (subr_die, decl);
18131       if (TREE_ASM_WRITTEN (stmt))
18132         add_high_low_attributes (stmt, subr_die);
18133       add_call_src_coords_attributes (stmt, subr_die);
18134
18135       decls_for_scope (stmt, subr_die, depth);
18136       current_function_has_inlines = 1;
18137     }
18138 }
18139
18140 /* Generate a DIE for a field in a record, or structure.  */
18141
18142 static void
18143 gen_field_die (tree decl, dw_die_ref context_die)
18144 {
18145   dw_die_ref decl_die;
18146
18147   if (TREE_TYPE (decl) == error_mark_node)
18148     return;
18149
18150   decl_die = new_die (DW_TAG_member, context_die, decl);
18151   add_name_and_src_coords_attributes (decl_die, decl);
18152   add_type_attribute (decl_die, member_declared_type (decl),
18153                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18154                       context_die);
18155
18156   if (DECL_BIT_FIELD_TYPE (decl))
18157     {
18158       add_byte_size_attribute (decl_die, decl);
18159       add_bit_size_attribute (decl_die, decl);
18160       add_bit_offset_attribute (decl_die, decl);
18161     }
18162
18163   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18164     add_data_member_location_attribute (decl_die, decl);
18165
18166   if (DECL_ARTIFICIAL (decl))
18167     add_AT_flag (decl_die, DW_AT_artificial, 1);
18168
18169   add_accessibility_attribute (decl_die, decl);
18170
18171   /* Equate decl number to die, so that we can look up this decl later on.  */
18172   equate_decl_number_to_die (decl, decl_die);
18173 }
18174
18175 #if 0
18176 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18177    Use modified_type_die instead.
18178    We keep this code here just in case these types of DIEs may be needed to
18179    represent certain things in other languages (e.g. Pascal) someday.  */
18180
18181 static void
18182 gen_pointer_type_die (tree type, dw_die_ref context_die)
18183 {
18184   dw_die_ref ptr_die
18185     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18186
18187   equate_type_number_to_die (type, ptr_die);
18188   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18189   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18190 }
18191
18192 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18193    Use modified_type_die instead.
18194    We keep this code here just in case these types of DIEs may be needed to
18195    represent certain things in other languages (e.g. Pascal) someday.  */
18196
18197 static void
18198 gen_reference_type_die (tree type, dw_die_ref context_die)
18199 {
18200   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18201
18202   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18203     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18204   else
18205     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18206
18207   equate_type_number_to_die (type, ref_die);
18208   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18209   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18210 }
18211 #endif
18212
18213 /* Generate a DIE for a pointer to a member type.  */
18214
18215 static void
18216 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18217 {
18218   dw_die_ref ptr_die
18219     = new_die (DW_TAG_ptr_to_member_type,
18220                scope_die_for (type, context_die), type);
18221
18222   equate_type_number_to_die (type, ptr_die);
18223   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18224                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18225   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18226 }
18227
18228 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18229 DEF_VEC_P(dchar_p);
18230 DEF_VEC_ALLOC_P(dchar_p,heap);
18231
18232 static char *producer_string;
18233
18234 /* Return a heap allocated producer string including command line options
18235    if -grecord-gcc-switches.  */
18236
18237 static char *
18238 gen_producer_string (void)
18239 {
18240   size_t j;
18241   VEC(dchar_p, heap) *switches = NULL;
18242   const char *language_string = lang_hooks.name;
18243   char *producer, *tail;
18244   const char *p;
18245   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18246   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18247
18248   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18249     switch (save_decoded_options[j].opt_index)
18250       {
18251       case OPT_o:
18252       case OPT_d:
18253       case OPT_dumpbase:
18254       case OPT_dumpdir:
18255       case OPT_auxbase:
18256       case OPT_auxbase_strip:
18257       case OPT_quiet:
18258       case OPT_version:
18259       case OPT_v:
18260       case OPT_w:
18261       case OPT_L:
18262       case OPT_D:
18263       case OPT_I:
18264       case OPT_U:
18265       case OPT_SPECIAL_unknown:
18266       case OPT_SPECIAL_ignore:
18267       case OPT_SPECIAL_program_name:
18268       case OPT_SPECIAL_input_file:
18269       case OPT_grecord_gcc_switches:
18270       case OPT_gno_record_gcc_switches:
18271       case OPT__output_pch_:
18272       case OPT_fdiagnostics_show_location_:
18273       case OPT_fdiagnostics_show_option:
18274       case OPT_fverbose_asm:
18275       case OPT____:
18276       case OPT__sysroot_:
18277       case OPT_nostdinc:
18278       case OPT_nostdinc__:
18279         /* Ignore these.  */
18280         continue;
18281       default:
18282         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18283                              == '-');
18284         switch (save_decoded_options[j].canonical_option[0][1])
18285           {
18286           case 'M':
18287           case 'i':
18288           case 'W':
18289             continue;
18290           case 'f':
18291             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18292                          "dump", 4) == 0)
18293               continue;
18294             break;
18295           default:
18296             break;
18297           }
18298         VEC_safe_push (dchar_p, heap, switches,
18299                        save_decoded_options[j].orig_option_with_args_text);
18300         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18301         break;
18302       }
18303
18304   producer = XNEWVEC (char, plen + 1 + len + 1);
18305   tail = producer;
18306   sprintf (tail, "%s %s", language_string, version_string);
18307   tail += plen;
18308
18309   if (!dwarf_record_gcc_switches)
18310     {
18311 #ifdef MIPS_DEBUGGING_INFO
18312       /* The MIPS/SGI compilers place the 'cc' command line options in the
18313          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18314          if they do not appear in the producer string, the debugger reaches
18315          the conclusion that the object file is stripped and has no debugging
18316          information.  To get the MIPS/SGI debugger to believe that there is
18317          debugging information in the object file, we add a -g to the producer
18318          string.  */
18319       if (debug_info_level > DINFO_LEVEL_TERSE)
18320         {
18321           memcpy (tail, " -g", 3);
18322           tail += 3;
18323         }
18324 #endif
18325     }
18326
18327   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18328     {
18329       len = strlen (p);
18330       *tail = ' ';
18331       memcpy (tail + 1, p, len);
18332       tail += len + 1;
18333     }
18334
18335   *tail = '\0';
18336   VEC_free (dchar_p, heap, switches);
18337   return producer;
18338 }
18339
18340 /* Generate the DIE for the compilation unit.  */
18341
18342 static dw_die_ref
18343 gen_compile_unit_die (const char *filename)
18344 {
18345   dw_die_ref die;
18346   const char *language_string = lang_hooks.name;
18347   int language;
18348
18349   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18350
18351   if (filename)
18352     {
18353       add_name_attribute (die, filename);
18354       /* Don't add cwd for <built-in>.  */
18355       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18356         add_comp_dir_attribute (die);
18357     }
18358
18359   if (producer_string == NULL)
18360     producer_string = gen_producer_string ();
18361   add_AT_string (die, DW_AT_producer, producer_string);
18362
18363   /* If our producer is LTO try to figure out a common language to use
18364      from the global list of translation units.  */
18365   if (strcmp (language_string, "GNU GIMPLE") == 0)
18366     {
18367       unsigned i;
18368       tree t;
18369       const char *common_lang = NULL;
18370
18371       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18372         {
18373           if (!TRANSLATION_UNIT_LANGUAGE (t))
18374             continue;
18375           if (!common_lang)
18376             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18377           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18378             ;
18379           else if (strncmp (common_lang, "GNU C", 5) == 0
18380                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18381             /* Mixing C and C++ is ok, use C++ in that case.  */
18382             common_lang = "GNU C++";
18383           else
18384             {
18385               /* Fall back to C.  */
18386               common_lang = NULL;
18387               break;
18388             }
18389         }
18390
18391       if (common_lang)
18392         language_string = common_lang;
18393     }
18394
18395   language = DW_LANG_C89;
18396   if (strcmp (language_string, "GNU C++") == 0)
18397     language = DW_LANG_C_plus_plus;
18398   else if (strcmp (language_string, "GNU F77") == 0)
18399     language = DW_LANG_Fortran77;
18400   else if (strcmp (language_string, "GNU Pascal") == 0)
18401     language = DW_LANG_Pascal83;
18402   else if (dwarf_version >= 3 || !dwarf_strict)
18403     {
18404       if (strcmp (language_string, "GNU Ada") == 0)
18405         language = DW_LANG_Ada95;
18406       else if (strcmp (language_string, "GNU Fortran") == 0)
18407         language = DW_LANG_Fortran95;
18408       else if (strcmp (language_string, "GNU Java") == 0)
18409         language = DW_LANG_Java;
18410       else if (strcmp (language_string, "GNU Objective-C") == 0)
18411         language = DW_LANG_ObjC;
18412       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18413         language = DW_LANG_ObjC_plus_plus;
18414     }
18415
18416   add_AT_unsigned (die, DW_AT_language, language);
18417
18418   switch (language)
18419     {
18420     case DW_LANG_Fortran77:
18421     case DW_LANG_Fortran90:
18422     case DW_LANG_Fortran95:
18423       /* Fortran has case insensitive identifiers and the front-end
18424          lowercases everything.  */
18425       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18426       break;
18427     default:
18428       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18429       break;
18430     }
18431   return die;
18432 }
18433
18434 /* Generate the DIE for a base class.  */
18435
18436 static void
18437 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18438 {
18439   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18440
18441   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18442   add_data_member_location_attribute (die, binfo);
18443
18444   if (BINFO_VIRTUAL_P (binfo))
18445     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18446
18447   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18448      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18449      the default has always been DW_ACCESS_private.  */
18450   if (access == access_public_node)
18451     {
18452       if (dwarf_version == 2
18453           || context_die->die_tag == DW_TAG_class_type)
18454       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18455     }
18456   else if (access == access_protected_node)
18457     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18458   else if (dwarf_version > 2
18459            && context_die->die_tag != DW_TAG_class_type)
18460     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18461 }
18462
18463 /* Generate a DIE for a class member.  */
18464
18465 static void
18466 gen_member_die (tree type, dw_die_ref context_die)
18467 {
18468   tree member;
18469   tree binfo = TYPE_BINFO (type);
18470   dw_die_ref child;
18471
18472   /* If this is not an incomplete type, output descriptions of each of its
18473      members. Note that as we output the DIEs necessary to represent the
18474      members of this record or union type, we will also be trying to output
18475      DIEs to represent the *types* of those members. However the `type'
18476      function (above) will specifically avoid generating type DIEs for member
18477      types *within* the list of member DIEs for this (containing) type except
18478      for those types (of members) which are explicitly marked as also being
18479      members of this (containing) type themselves.  The g++ front- end can
18480      force any given type to be treated as a member of some other (containing)
18481      type by setting the TYPE_CONTEXT of the given (member) type to point to
18482      the TREE node representing the appropriate (containing) type.  */
18483
18484   /* First output info about the base classes.  */
18485   if (binfo)
18486     {
18487       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18488       int i;
18489       tree base;
18490
18491       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18492         gen_inheritance_die (base,
18493                              (accesses ? VEC_index (tree, accesses, i)
18494                               : access_public_node), context_die);
18495     }
18496
18497   /* Now output info about the data members and type members.  */
18498   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18499     {
18500       /* If we thought we were generating minimal debug info for TYPE
18501          and then changed our minds, some of the member declarations
18502          may have already been defined.  Don't define them again, but
18503          do put them in the right order.  */
18504
18505       child = lookup_decl_die (member);
18506       if (child)
18507         splice_child_die (context_die, child);
18508       else
18509         gen_decl_die (member, NULL, context_die);
18510     }
18511
18512   /* Now output info about the function members (if any).  */
18513   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18514     {
18515       /* Don't include clones in the member list.  */
18516       if (DECL_ABSTRACT_ORIGIN (member))
18517         continue;
18518
18519       child = lookup_decl_die (member);
18520       if (child)
18521         splice_child_die (context_die, child);
18522       else
18523         gen_decl_die (member, NULL, context_die);
18524     }
18525 }
18526
18527 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18528    is set, we pretend that the type was never defined, so we only get the
18529    member DIEs needed by later specification DIEs.  */
18530
18531 static void
18532 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18533                                 enum debug_info_usage usage)
18534 {
18535   dw_die_ref type_die = lookup_type_die (type);
18536   dw_die_ref scope_die = 0;
18537   int nested = 0;
18538   int complete = (TYPE_SIZE (type)
18539                   && (! TYPE_STUB_DECL (type)
18540                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18541   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18542   complete = complete && should_emit_struct_debug (type, usage);
18543
18544   if (type_die && ! complete)
18545     return;
18546
18547   if (TYPE_CONTEXT (type) != NULL_TREE
18548       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18549           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18550     nested = 1;
18551
18552   scope_die = scope_die_for (type, context_die);
18553
18554   if (! type_die || (nested && is_cu_die (scope_die)))
18555     /* First occurrence of type or toplevel definition of nested class.  */
18556     {
18557       dw_die_ref old_die = type_die;
18558
18559       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18560                           ? record_type_tag (type) : DW_TAG_union_type,
18561                           scope_die, type);
18562       equate_type_number_to_die (type, type_die);
18563       if (old_die)
18564         add_AT_specification (type_die, old_die);
18565       else
18566         {
18567           add_name_attribute (type_die, type_tag (type));
18568           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18569           if (TYPE_ARTIFICIAL (type))
18570             add_AT_flag (type_die, DW_AT_artificial, 1);
18571         }
18572     }
18573   else
18574     remove_AT (type_die, DW_AT_declaration);
18575
18576   /* Generate child dies for template paramaters.  */
18577   if (debug_info_level > DINFO_LEVEL_TERSE
18578       && COMPLETE_TYPE_P (type))
18579     schedule_generic_params_dies_gen (type);
18580
18581   /* If this type has been completed, then give it a byte_size attribute and
18582      then give a list of members.  */
18583   if (complete && !ns_decl)
18584     {
18585       /* Prevent infinite recursion in cases where the type of some member of
18586          this type is expressed in terms of this type itself.  */
18587       TREE_ASM_WRITTEN (type) = 1;
18588       add_byte_size_attribute (type_die, type);
18589       if (TYPE_STUB_DECL (type) != NULL_TREE)
18590         {
18591           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18592           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18593         }
18594
18595       /* If the first reference to this type was as the return type of an
18596          inline function, then it may not have a parent.  Fix this now.  */
18597       if (type_die->die_parent == NULL)
18598         add_child_die (scope_die, type_die);
18599
18600       push_decl_scope (type);
18601       gen_member_die (type, type_die);
18602       pop_decl_scope ();
18603
18604       /* GNU extension: Record what type our vtable lives in.  */
18605       if (TYPE_VFIELD (type))
18606         {
18607           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18608
18609           gen_type_die (vtype, context_die);
18610           add_AT_die_ref (type_die, DW_AT_containing_type,
18611                           lookup_type_die (vtype));
18612         }
18613     }
18614   else
18615     {
18616       add_AT_flag (type_die, DW_AT_declaration, 1);
18617
18618       /* We don't need to do this for function-local types.  */
18619       if (TYPE_STUB_DECL (type)
18620           && ! decl_function_context (TYPE_STUB_DECL (type)))
18621         VEC_safe_push (tree, gc, incomplete_types, type);
18622     }
18623
18624   if (get_AT (type_die, DW_AT_name))
18625     add_pubtype (type, type_die);
18626 }
18627
18628 /* Generate a DIE for a subroutine _type_.  */
18629
18630 static void
18631 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18632 {
18633   tree return_type = TREE_TYPE (type);
18634   dw_die_ref subr_die
18635     = new_die (DW_TAG_subroutine_type,
18636                scope_die_for (type, context_die), type);
18637
18638   equate_type_number_to_die (type, subr_die);
18639   add_prototyped_attribute (subr_die, type);
18640   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18641   gen_formal_types_die (type, subr_die);
18642
18643   if (get_AT (subr_die, DW_AT_name))
18644     add_pubtype (type, subr_die);
18645 }
18646
18647 /* Generate a DIE for a type definition.  */
18648
18649 static void
18650 gen_typedef_die (tree decl, dw_die_ref context_die)
18651 {
18652   dw_die_ref type_die;
18653   tree origin;
18654
18655   if (TREE_ASM_WRITTEN (decl))
18656     return;
18657
18658   TREE_ASM_WRITTEN (decl) = 1;
18659   type_die = new_die (DW_TAG_typedef, context_die, decl);
18660   origin = decl_ultimate_origin (decl);
18661   if (origin != NULL)
18662     add_abstract_origin_attribute (type_die, origin);
18663   else
18664     {
18665       tree type;
18666
18667       add_name_and_src_coords_attributes (type_die, decl);
18668       if (DECL_ORIGINAL_TYPE (decl))
18669         {
18670           type = DECL_ORIGINAL_TYPE (decl);
18671
18672           gcc_assert (type != TREE_TYPE (decl));
18673           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18674         }
18675       else
18676         {
18677           type = TREE_TYPE (decl);
18678
18679           if (is_naming_typedef_decl (TYPE_NAME (type)))
18680             {
18681               /* Here, we are in the case of decl being a typedef naming
18682                  an anonymous type, e.g:
18683                      typedef struct {...} foo;
18684                  In that case TREE_TYPE (decl) is not a typedef variant
18685                  type and TYPE_NAME of the anonymous type is set to the
18686                  TYPE_DECL of the typedef. This construct is emitted by
18687                  the C++ FE.
18688
18689                  TYPE is the anonymous struct named by the typedef
18690                  DECL. As we need the DW_AT_type attribute of the
18691                  DW_TAG_typedef to point to the DIE of TYPE, let's
18692                  generate that DIE right away. add_type_attribute
18693                  called below will then pick (via lookup_type_die) that
18694                  anonymous struct DIE.  */
18695               if (!TREE_ASM_WRITTEN (type))
18696                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18697
18698               /* This is a GNU Extension.  We are adding a
18699                  DW_AT_linkage_name attribute to the DIE of the
18700                  anonymous struct TYPE.  The value of that attribute
18701                  is the name of the typedef decl naming the anonymous
18702                  struct.  This greatly eases the work of consumers of
18703                  this debug info.  */
18704               add_linkage_attr (lookup_type_die (type), decl);
18705             }
18706         }
18707
18708       add_type_attribute (type_die, type, TREE_READONLY (decl),
18709                           TREE_THIS_VOLATILE (decl), context_die);
18710
18711       if (is_naming_typedef_decl (decl))
18712         /* We want that all subsequent calls to lookup_type_die with
18713            TYPE in argument yield the DW_TAG_typedef we have just
18714            created.  */
18715         equate_type_number_to_die (type, type_die);
18716
18717       add_accessibility_attribute (type_die, decl);
18718     }
18719
18720   if (DECL_ABSTRACT (decl))
18721     equate_decl_number_to_die (decl, type_die);
18722
18723   if (get_AT (type_die, DW_AT_name))
18724     add_pubtype (decl, type_die);
18725 }
18726
18727 /* Generate a DIE for a struct, class, enum or union type.  */
18728
18729 static void
18730 gen_tagged_type_die (tree type,
18731                      dw_die_ref context_die,
18732                      enum debug_info_usage usage)
18733 {
18734   int need_pop;
18735
18736   if (type == NULL_TREE
18737       || !is_tagged_type (type))
18738     return;
18739
18740   /* If this is a nested type whose containing class hasn't been written
18741      out yet, writing it out will cover this one, too.  This does not apply
18742      to instantiations of member class templates; they need to be added to
18743      the containing class as they are generated.  FIXME: This hurts the
18744      idea of combining type decls from multiple TUs, since we can't predict
18745      what set of template instantiations we'll get.  */
18746   if (TYPE_CONTEXT (type)
18747       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18748       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18749     {
18750       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18751
18752       if (TREE_ASM_WRITTEN (type))
18753         return;
18754
18755       /* If that failed, attach ourselves to the stub.  */
18756       push_decl_scope (TYPE_CONTEXT (type));
18757       context_die = lookup_type_die (TYPE_CONTEXT (type));
18758       need_pop = 1;
18759     }
18760   else if (TYPE_CONTEXT (type) != NULL_TREE
18761            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18762     {
18763       /* If this type is local to a function that hasn't been written
18764          out yet, use a NULL context for now; it will be fixed up in
18765          decls_for_scope.  */
18766       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18767       /* A declaration DIE doesn't count; nested types need to go in the
18768          specification.  */
18769       if (context_die && is_declaration_die (context_die))
18770         context_die = NULL;
18771       need_pop = 0;
18772     }
18773   else
18774     {
18775       context_die = declare_in_namespace (type, context_die);
18776       need_pop = 0;
18777     }
18778
18779   if (TREE_CODE (type) == ENUMERAL_TYPE)
18780     {
18781       /* This might have been written out by the call to
18782          declare_in_namespace.  */
18783       if (!TREE_ASM_WRITTEN (type))
18784         gen_enumeration_type_die (type, context_die);
18785     }
18786   else
18787     gen_struct_or_union_type_die (type, context_die, usage);
18788
18789   if (need_pop)
18790     pop_decl_scope ();
18791
18792   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18793      it up if it is ever completed.  gen_*_type_die will set it for us
18794      when appropriate.  */
18795 }
18796
18797 /* Generate a type description DIE.  */
18798
18799 static void
18800 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18801                          enum debug_info_usage usage)
18802 {
18803   struct array_descr_info info;
18804
18805   if (type == NULL_TREE || type == error_mark_node)
18806     return;
18807
18808   if (TYPE_NAME (type) != NULL_TREE
18809       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18810       && is_redundant_typedef (TYPE_NAME (type))
18811       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18812     /* The DECL of this type is a typedef we don't want to emit debug
18813        info for but we want debug info for its underlying typedef.
18814        This can happen for e.g, the injected-class-name of a C++
18815        type.  */
18816     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18817
18818   /* If TYPE is a typedef type variant, let's generate debug info
18819      for the parent typedef which TYPE is a type of.  */
18820   if (typedef_variant_p (type))
18821     {
18822       if (TREE_ASM_WRITTEN (type))
18823         return;
18824
18825       /* Prevent broken recursion; we can't hand off to the same type.  */
18826       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18827
18828       /* Use the DIE of the containing namespace as the parent DIE of
18829          the type description DIE we want to generate.  */
18830       if (DECL_CONTEXT (TYPE_NAME (type))
18831           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18832         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18833
18834       TREE_ASM_WRITTEN (type) = 1;
18835
18836       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18837       return;
18838     }
18839
18840   /* If type is an anonymous tagged type named by a typedef, let's
18841      generate debug info for the typedef.  */
18842   if (is_naming_typedef_decl (TYPE_NAME (type)))
18843     {
18844       /* Use the DIE of the containing namespace as the parent DIE of
18845          the type description DIE we want to generate.  */
18846       if (DECL_CONTEXT (TYPE_NAME (type))
18847           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18848         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18849       
18850       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18851       return;
18852     }
18853
18854   /* If this is an array type with hidden descriptor, handle it first.  */
18855   if (!TREE_ASM_WRITTEN (type)
18856       && lang_hooks.types.get_array_descr_info
18857       && lang_hooks.types.get_array_descr_info (type, &info)
18858       && (dwarf_version >= 3 || !dwarf_strict))
18859     {
18860       gen_descr_array_type_die (type, &info, context_die);
18861       TREE_ASM_WRITTEN (type) = 1;
18862       return;
18863     }
18864
18865   /* We are going to output a DIE to represent the unqualified version
18866      of this type (i.e. without any const or volatile qualifiers) so
18867      get the main variant (i.e. the unqualified version) of this type
18868      now.  (Vectors are special because the debugging info is in the
18869      cloned type itself).  */
18870   if (TREE_CODE (type) != VECTOR_TYPE)
18871     type = type_main_variant (type);
18872
18873   if (TREE_ASM_WRITTEN (type))
18874     return;
18875
18876   switch (TREE_CODE (type))
18877     {
18878     case ERROR_MARK:
18879       break;
18880
18881     case POINTER_TYPE:
18882     case REFERENCE_TYPE:
18883       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18884          ensures that the gen_type_die recursion will terminate even if the
18885          type is recursive.  Recursive types are possible in Ada.  */
18886       /* ??? We could perhaps do this for all types before the switch
18887          statement.  */
18888       TREE_ASM_WRITTEN (type) = 1;
18889
18890       /* For these types, all that is required is that we output a DIE (or a
18891          set of DIEs) to represent the "basis" type.  */
18892       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18893                                 DINFO_USAGE_IND_USE);
18894       break;
18895
18896     case OFFSET_TYPE:
18897       /* This code is used for C++ pointer-to-data-member types.
18898          Output a description of the relevant class type.  */
18899       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18900                                         DINFO_USAGE_IND_USE);
18901
18902       /* Output a description of the type of the object pointed to.  */
18903       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18904                                         DINFO_USAGE_IND_USE);
18905
18906       /* Now output a DIE to represent this pointer-to-data-member type
18907          itself.  */
18908       gen_ptr_to_mbr_type_die (type, context_die);
18909       break;
18910
18911     case FUNCTION_TYPE:
18912       /* Force out return type (in case it wasn't forced out already).  */
18913       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18914                                         DINFO_USAGE_DIR_USE);
18915       gen_subroutine_type_die (type, context_die);
18916       break;
18917
18918     case METHOD_TYPE:
18919       /* Force out return type (in case it wasn't forced out already).  */
18920       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18921                                         DINFO_USAGE_DIR_USE);
18922       gen_subroutine_type_die (type, context_die);
18923       break;
18924
18925     case ARRAY_TYPE:
18926       gen_array_type_die (type, context_die);
18927       break;
18928
18929     case VECTOR_TYPE:
18930       gen_array_type_die (type, context_die);
18931       break;
18932
18933     case ENUMERAL_TYPE:
18934     case RECORD_TYPE:
18935     case UNION_TYPE:
18936     case QUAL_UNION_TYPE:
18937       gen_tagged_type_die (type, context_die, usage);
18938       return;
18939
18940     case VOID_TYPE:
18941     case INTEGER_TYPE:
18942     case REAL_TYPE:
18943     case FIXED_POINT_TYPE:
18944     case COMPLEX_TYPE:
18945     case BOOLEAN_TYPE:
18946       /* No DIEs needed for fundamental types.  */
18947       break;
18948
18949     case NULLPTR_TYPE:
18950     case LANG_TYPE:
18951       /* Just use DW_TAG_unspecified_type.  */
18952       {
18953         dw_die_ref type_die = lookup_type_die (type);
18954         if (type_die == NULL)
18955           {
18956             tree name = TYPE_NAME (type);
18957             if (TREE_CODE (name) == TYPE_DECL)
18958               name = DECL_NAME (name);
18959             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18960             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18961             equate_type_number_to_die (type, type_die);
18962           }
18963       }
18964       break;
18965
18966     default:
18967       gcc_unreachable ();
18968     }
18969
18970   TREE_ASM_WRITTEN (type) = 1;
18971 }
18972
18973 static void
18974 gen_type_die (tree type, dw_die_ref context_die)
18975 {
18976   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18977 }
18978
18979 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18980    things which are local to the given block.  */
18981
18982 static void
18983 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18984 {
18985   int must_output_die = 0;
18986   bool inlined_func;
18987
18988   /* Ignore blocks that are NULL.  */
18989   if (stmt == NULL_TREE)
18990     return;
18991
18992   inlined_func = inlined_function_outer_scope_p (stmt);
18993
18994   /* If the block is one fragment of a non-contiguous block, do not
18995      process the variables, since they will have been done by the
18996      origin block.  Do process subblocks.  */
18997   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18998     {
18999       tree sub;
19000
19001       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19002         gen_block_die (sub, context_die, depth + 1);
19003
19004       return;
19005     }
19006
19007   /* Determine if we need to output any Dwarf DIEs at all to represent this
19008      block.  */
19009   if (inlined_func)
19010     /* The outer scopes for inlinings *must* always be represented.  We
19011        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19012     must_output_die = 1;
19013   else
19014     {
19015       /* Determine if this block directly contains any "significant"
19016          local declarations which we will need to output DIEs for.  */
19017       if (debug_info_level > DINFO_LEVEL_TERSE)
19018         /* We are not in terse mode so *any* local declaration counts
19019            as being a "significant" one.  */
19020         must_output_die = ((BLOCK_VARS (stmt) != NULL
19021                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19022                            && (TREE_USED (stmt)
19023                                || TREE_ASM_WRITTEN (stmt)
19024                                || BLOCK_ABSTRACT (stmt)));
19025       else if ((TREE_USED (stmt)
19026                 || TREE_ASM_WRITTEN (stmt)
19027                 || BLOCK_ABSTRACT (stmt))
19028                && !dwarf2out_ignore_block (stmt))
19029         must_output_die = 1;
19030     }
19031
19032   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19033      DIE for any block which contains no significant local declarations at
19034      all.  Rather, in such cases we just call `decls_for_scope' so that any
19035      needed Dwarf info for any sub-blocks will get properly generated. Note
19036      that in terse mode, our definition of what constitutes a "significant"
19037      local declaration gets restricted to include only inlined function
19038      instances and local (nested) function definitions.  */
19039   if (must_output_die)
19040     {
19041       if (inlined_func)
19042         {
19043           /* If STMT block is abstract, that means we have been called
19044              indirectly from dwarf2out_abstract_function.
19045              That function rightfully marks the descendent blocks (of
19046              the abstract function it is dealing with) as being abstract,
19047              precisely to prevent us from emitting any
19048              DW_TAG_inlined_subroutine DIE as a descendent
19049              of an abstract function instance. So in that case, we should
19050              not call gen_inlined_subroutine_die.
19051
19052              Later though, when cgraph asks dwarf2out to emit info
19053              for the concrete instance of the function decl into which
19054              the concrete instance of STMT got inlined, the later will lead
19055              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19056           if (! BLOCK_ABSTRACT (stmt))
19057             gen_inlined_subroutine_die (stmt, context_die, depth);
19058         }
19059       else
19060         gen_lexical_block_die (stmt, context_die, depth);
19061     }
19062   else
19063     decls_for_scope (stmt, context_die, depth);
19064 }
19065
19066 /* Process variable DECL (or variable with origin ORIGIN) within
19067    block STMT and add it to CONTEXT_DIE.  */
19068 static void
19069 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19070 {
19071   dw_die_ref die;
19072   tree decl_or_origin = decl ? decl : origin;
19073
19074   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19075     die = lookup_decl_die (decl_or_origin);
19076   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19077            && TYPE_DECL_IS_STUB (decl_or_origin))
19078     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19079   else
19080     die = NULL;
19081
19082   if (die != NULL && die->die_parent == NULL)
19083     add_child_die (context_die, die);
19084   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19085     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19086                                          stmt, context_die);
19087   else
19088     gen_decl_die (decl, origin, context_die);
19089 }
19090
19091 /* Generate all of the decls declared within a given scope and (recursively)
19092    all of its sub-blocks.  */
19093
19094 static void
19095 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19096 {
19097   tree decl;
19098   unsigned int i;
19099   tree subblocks;
19100
19101   /* Ignore NULL blocks.  */
19102   if (stmt == NULL_TREE)
19103     return;
19104
19105   /* Output the DIEs to represent all of the data objects and typedefs
19106      declared directly within this block but not within any nested
19107      sub-blocks.  Also, nested function and tag DIEs have been
19108      generated with a parent of NULL; fix that up now.  */
19109   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19110     process_scope_var (stmt, decl, NULL_TREE, context_die);
19111   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19112     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19113                        context_die);
19114
19115   /* If we're at -g1, we're not interested in subblocks.  */
19116   if (debug_info_level <= DINFO_LEVEL_TERSE)
19117     return;
19118
19119   /* Output the DIEs to represent all sub-blocks (and the items declared
19120      therein) of this block.  */
19121   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19122        subblocks != NULL;
19123        subblocks = BLOCK_CHAIN (subblocks))
19124     gen_block_die (subblocks, context_die, depth + 1);
19125 }
19126
19127 /* Is this a typedef we can avoid emitting?  */
19128
19129 static inline int
19130 is_redundant_typedef (const_tree decl)
19131 {
19132   if (TYPE_DECL_IS_STUB (decl))
19133     return 1;
19134
19135   if (DECL_ARTIFICIAL (decl)
19136       && DECL_CONTEXT (decl)
19137       && is_tagged_type (DECL_CONTEXT (decl))
19138       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19139       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19140     /* Also ignore the artificial member typedef for the class name.  */
19141     return 1;
19142
19143   return 0;
19144 }
19145
19146 /* Return TRUE if TYPE is a typedef that names a type for linkage
19147    purposes. This kind of typedefs is produced by the C++ FE for
19148    constructs like:
19149
19150    typedef struct {...} foo;
19151
19152    In that case, there is no typedef variant type produced for foo.
19153    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19154    struct type.  */
19155
19156 static bool
19157 is_naming_typedef_decl (const_tree decl)
19158 {
19159   if (decl == NULL_TREE
19160       || TREE_CODE (decl) != TYPE_DECL
19161       || !is_tagged_type (TREE_TYPE (decl))
19162       || DECL_IS_BUILTIN (decl)
19163       || is_redundant_typedef (decl)
19164       /* It looks like Ada produces TYPE_DECLs that are very similar
19165          to C++ naming typedefs but that have different
19166          semantics. Let's be specific to c++ for now.  */
19167       || !is_cxx ())
19168     return FALSE;
19169
19170   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19171           && TYPE_NAME (TREE_TYPE (decl)) == decl
19172           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19173               != TYPE_NAME (TREE_TYPE (decl))));
19174 }
19175
19176 /* Returns the DIE for a context.  */
19177
19178 static inline dw_die_ref
19179 get_context_die (tree context)
19180 {
19181   if (context)
19182     {
19183       /* Find die that represents this context.  */
19184       if (TYPE_P (context))
19185         {
19186           context = TYPE_MAIN_VARIANT (context);
19187           return strip_naming_typedef (context, force_type_die (context));
19188         }
19189       else
19190         return force_decl_die (context);
19191     }
19192   return comp_unit_die ();
19193 }
19194
19195 /* Returns the DIE for decl.  A DIE will always be returned.  */
19196
19197 static dw_die_ref
19198 force_decl_die (tree decl)
19199 {
19200   dw_die_ref decl_die;
19201   unsigned saved_external_flag;
19202   tree save_fn = NULL_TREE;
19203   decl_die = lookup_decl_die (decl);
19204   if (!decl_die)
19205     {
19206       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19207
19208       decl_die = lookup_decl_die (decl);
19209       if (decl_die)
19210         return decl_die;
19211
19212       switch (TREE_CODE (decl))
19213         {
19214         case FUNCTION_DECL:
19215           /* Clear current_function_decl, so that gen_subprogram_die thinks
19216              that this is a declaration. At this point, we just want to force
19217              declaration die.  */
19218           save_fn = current_function_decl;
19219           current_function_decl = NULL_TREE;
19220           gen_subprogram_die (decl, context_die);
19221           current_function_decl = save_fn;
19222           break;
19223
19224         case VAR_DECL:
19225           /* Set external flag to force declaration die. Restore it after
19226            gen_decl_die() call.  */
19227           saved_external_flag = DECL_EXTERNAL (decl);
19228           DECL_EXTERNAL (decl) = 1;
19229           gen_decl_die (decl, NULL, context_die);
19230           DECL_EXTERNAL (decl) = saved_external_flag;
19231           break;
19232
19233         case NAMESPACE_DECL:
19234           if (dwarf_version >= 3 || !dwarf_strict)
19235             dwarf2out_decl (decl);
19236           else
19237             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19238             decl_die = comp_unit_die ();
19239           break;
19240
19241         case TRANSLATION_UNIT_DECL:
19242           decl_die = comp_unit_die ();
19243           break;
19244
19245         default:
19246           gcc_unreachable ();
19247         }
19248
19249       /* We should be able to find the DIE now.  */
19250       if (!decl_die)
19251         decl_die = lookup_decl_die (decl);
19252       gcc_assert (decl_die);
19253     }
19254
19255   return decl_die;
19256 }
19257
19258 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19259    always returned.  */
19260
19261 static dw_die_ref
19262 force_type_die (tree type)
19263 {
19264   dw_die_ref type_die;
19265
19266   type_die = lookup_type_die (type);
19267   if (!type_die)
19268     {
19269       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19270
19271       type_die = modified_type_die (type, TYPE_READONLY (type),
19272                                     TYPE_VOLATILE (type), context_die);
19273       gcc_assert (type_die);
19274     }
19275   return type_die;
19276 }
19277
19278 /* Force out any required namespaces to be able to output DECL,
19279    and return the new context_die for it, if it's changed.  */
19280
19281 static dw_die_ref
19282 setup_namespace_context (tree thing, dw_die_ref context_die)
19283 {
19284   tree context = (DECL_P (thing)
19285                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19286   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19287     /* Force out the namespace.  */
19288     context_die = force_decl_die (context);
19289
19290   return context_die;
19291 }
19292
19293 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19294    type) within its namespace, if appropriate.
19295
19296    For compatibility with older debuggers, namespace DIEs only contain
19297    declarations; all definitions are emitted at CU scope.  */
19298
19299 static dw_die_ref
19300 declare_in_namespace (tree thing, dw_die_ref context_die)
19301 {
19302   dw_die_ref ns_context;
19303
19304   if (debug_info_level <= DINFO_LEVEL_TERSE)
19305     return context_die;
19306
19307   /* If this decl is from an inlined function, then don't try to emit it in its
19308      namespace, as we will get confused.  It would have already been emitted
19309      when the abstract instance of the inline function was emitted anyways.  */
19310   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19311     return context_die;
19312
19313   ns_context = setup_namespace_context (thing, context_die);
19314
19315   if (ns_context != context_die)
19316     {
19317       if (is_fortran ())
19318         return ns_context;
19319       if (DECL_P (thing))
19320         gen_decl_die (thing, NULL, ns_context);
19321       else
19322         gen_type_die (thing, ns_context);
19323     }
19324   return context_die;
19325 }
19326
19327 /* Generate a DIE for a namespace or namespace alias.  */
19328
19329 static void
19330 gen_namespace_die (tree decl, dw_die_ref context_die)
19331 {
19332   dw_die_ref namespace_die;
19333
19334   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19335      they are an alias of.  */
19336   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19337     {
19338       /* Output a real namespace or module.  */
19339       context_die = setup_namespace_context (decl, comp_unit_die ());
19340       namespace_die = new_die (is_fortran ()
19341                                ? DW_TAG_module : DW_TAG_namespace,
19342                                context_die, decl);
19343       /* For Fortran modules defined in different CU don't add src coords.  */
19344       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19345         {
19346           const char *name = dwarf2_name (decl, 0);
19347           if (name)
19348             add_name_attribute (namespace_die, name);
19349         }
19350       else
19351         add_name_and_src_coords_attributes (namespace_die, decl);
19352       if (DECL_EXTERNAL (decl))
19353         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19354       equate_decl_number_to_die (decl, namespace_die);
19355     }
19356   else
19357     {
19358       /* Output a namespace alias.  */
19359
19360       /* Force out the namespace we are an alias of, if necessary.  */
19361       dw_die_ref origin_die
19362         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19363
19364       if (DECL_FILE_SCOPE_P (decl)
19365           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19366         context_die = setup_namespace_context (decl, comp_unit_die ());
19367       /* Now create the namespace alias DIE.  */
19368       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19369       add_name_and_src_coords_attributes (namespace_die, decl);
19370       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19371       equate_decl_number_to_die (decl, namespace_die);
19372     }
19373 }
19374
19375 /* Generate Dwarf debug information for a decl described by DECL.
19376    The return value is currently only meaningful for PARM_DECLs,
19377    for all other decls it returns NULL.  */
19378
19379 static dw_die_ref
19380 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19381 {
19382   tree decl_or_origin = decl ? decl : origin;
19383   tree class_origin = NULL, ultimate_origin;
19384
19385   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19386     return NULL;
19387
19388   switch (TREE_CODE (decl_or_origin))
19389     {
19390     case ERROR_MARK:
19391       break;
19392
19393     case CONST_DECL:
19394       if (!is_fortran () && !is_ada ())
19395         {
19396           /* The individual enumerators of an enum type get output when we output
19397              the Dwarf representation of the relevant enum type itself.  */
19398           break;
19399         }
19400
19401       /* Emit its type.  */
19402       gen_type_die (TREE_TYPE (decl), context_die);
19403
19404       /* And its containing namespace.  */
19405       context_die = declare_in_namespace (decl, context_die);
19406
19407       gen_const_die (decl, context_die);
19408       break;
19409
19410     case FUNCTION_DECL:
19411       /* Don't output any DIEs to represent mere function declarations,
19412          unless they are class members or explicit block externs.  */
19413       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19414           && DECL_FILE_SCOPE_P (decl_or_origin)
19415           && (current_function_decl == NULL_TREE
19416               || DECL_ARTIFICIAL (decl_or_origin)))
19417         break;
19418
19419 #if 0
19420       /* FIXME */
19421       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19422          on local redeclarations of global functions.  That seems broken.  */
19423       if (current_function_decl != decl)
19424         /* This is only a declaration.  */;
19425 #endif
19426
19427       /* If we're emitting a clone, emit info for the abstract instance.  */
19428       if (origin || DECL_ORIGIN (decl) != decl)
19429         dwarf2out_abstract_function (origin
19430                                      ? DECL_ORIGIN (origin)
19431                                      : DECL_ABSTRACT_ORIGIN (decl));
19432
19433       /* If we're emitting an out-of-line copy of an inline function,
19434          emit info for the abstract instance and set up to refer to it.  */
19435       else if (cgraph_function_possibly_inlined_p (decl)
19436                && ! DECL_ABSTRACT (decl)
19437                && ! class_or_namespace_scope_p (context_die)
19438                /* dwarf2out_abstract_function won't emit a die if this is just
19439                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19440                   that case, because that works only if we have a die.  */
19441                && DECL_INITIAL (decl) != NULL_TREE)
19442         {
19443           dwarf2out_abstract_function (decl);
19444           set_decl_origin_self (decl);
19445         }
19446
19447       /* Otherwise we're emitting the primary DIE for this decl.  */
19448       else if (debug_info_level > DINFO_LEVEL_TERSE)
19449         {
19450           /* Before we describe the FUNCTION_DECL itself, make sure that we
19451              have its containing type.  */
19452           if (!origin)
19453             origin = decl_class_context (decl);
19454           if (origin != NULL_TREE)
19455             gen_type_die (origin, context_die);
19456
19457           /* And its return type.  */
19458           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19459
19460           /* And its virtual context.  */
19461           if (DECL_VINDEX (decl) != NULL_TREE)
19462             gen_type_die (DECL_CONTEXT (decl), context_die);
19463
19464           /* Make sure we have a member DIE for decl.  */
19465           if (origin != NULL_TREE)
19466             gen_type_die_for_member (origin, decl, context_die);
19467
19468           /* And its containing namespace.  */
19469           context_die = declare_in_namespace (decl, context_die);
19470         }
19471
19472       /* Now output a DIE to represent the function itself.  */
19473       if (decl)
19474         gen_subprogram_die (decl, context_die);
19475       break;
19476
19477     case TYPE_DECL:
19478       /* If we are in terse mode, don't generate any DIEs to represent any
19479          actual typedefs.  */
19480       if (debug_info_level <= DINFO_LEVEL_TERSE)
19481         break;
19482
19483       /* In the special case of a TYPE_DECL node representing the declaration
19484          of some type tag, if the given TYPE_DECL is marked as having been
19485          instantiated from some other (original) TYPE_DECL node (e.g. one which
19486          was generated within the original definition of an inline function) we
19487          used to generate a special (abbreviated) DW_TAG_structure_type,
19488          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19489          should be actually referencing those DIEs, as variable DIEs with that
19490          type would be emitted already in the abstract origin, so it was always
19491          removed during unused type prunning.  Don't add anything in this
19492          case.  */
19493       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19494         break;
19495
19496       if (is_redundant_typedef (decl))
19497         gen_type_die (TREE_TYPE (decl), context_die);
19498       else
19499         /* Output a DIE to represent the typedef itself.  */
19500         gen_typedef_die (decl, context_die);
19501       break;
19502
19503     case LABEL_DECL:
19504       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19505         gen_label_die (decl, context_die);
19506       break;
19507
19508     case VAR_DECL:
19509     case RESULT_DECL:
19510       /* If we are in terse mode, don't generate any DIEs to represent any
19511          variable declarations or definitions.  */
19512       if (debug_info_level <= DINFO_LEVEL_TERSE)
19513         break;
19514
19515       /* Output any DIEs that are needed to specify the type of this data
19516          object.  */
19517       if (decl_by_reference_p (decl_or_origin))
19518         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19519       else
19520         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19521
19522       /* And its containing type.  */
19523       class_origin = decl_class_context (decl_or_origin);
19524       if (class_origin != NULL_TREE)
19525         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19526
19527       /* And its containing namespace.  */
19528       context_die = declare_in_namespace (decl_or_origin, context_die);
19529
19530       /* Now output the DIE to represent the data object itself.  This gets
19531          complicated because of the possibility that the VAR_DECL really
19532          represents an inlined instance of a formal parameter for an inline
19533          function.  */
19534       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19535       if (ultimate_origin != NULL_TREE
19536           && TREE_CODE (ultimate_origin) == PARM_DECL)
19537         gen_formal_parameter_die (decl, origin,
19538                                   true /* Emit name attribute.  */,
19539                                   context_die);
19540       else
19541         gen_variable_die (decl, origin, context_die);
19542       break;
19543
19544     case FIELD_DECL:
19545       /* Ignore the nameless fields that are used to skip bits but handle C++
19546          anonymous unions and structs.  */
19547       if (DECL_NAME (decl) != NULL_TREE
19548           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19549           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19550         {
19551           gen_type_die (member_declared_type (decl), context_die);
19552           gen_field_die (decl, context_die);
19553         }
19554       break;
19555
19556     case PARM_DECL:
19557       if (DECL_BY_REFERENCE (decl_or_origin))
19558         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19559       else
19560         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19561       return gen_formal_parameter_die (decl, origin,
19562                                        true /* Emit name attribute.  */,
19563                                        context_die);
19564
19565     case NAMESPACE_DECL:
19566     case IMPORTED_DECL:
19567       if (dwarf_version >= 3 || !dwarf_strict)
19568         gen_namespace_die (decl, context_die);
19569       break;
19570
19571     default:
19572       /* Probably some frontend-internal decl.  Assume we don't care.  */
19573       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19574       break;
19575     }
19576
19577   return NULL;
19578 }
19579 \f
19580 /* Output debug information for global decl DECL.  Called from toplev.c after
19581    compilation proper has finished.  */
19582
19583 static void
19584 dwarf2out_global_decl (tree decl)
19585 {
19586   /* Output DWARF2 information for file-scope tentative data object
19587      declarations, file-scope (extern) function declarations (which
19588      had no corresponding body) and file-scope tagged type declarations
19589      and definitions which have not yet been forced out.  */
19590   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19591     dwarf2out_decl (decl);
19592 }
19593
19594 /* Output debug information for type decl DECL.  Called from toplev.c
19595    and from language front ends (to record built-in types).  */
19596 static void
19597 dwarf2out_type_decl (tree decl, int local)
19598 {
19599   if (!local)
19600     dwarf2out_decl (decl);
19601 }
19602
19603 /* Output debug information for imported module or decl DECL.
19604    NAME is non-NULL name in the lexical block if the decl has been renamed.
19605    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19606    that DECL belongs to.
19607    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19608 static void
19609 dwarf2out_imported_module_or_decl_1 (tree decl,
19610                                      tree name,
19611                                      tree lexical_block,
19612                                      dw_die_ref lexical_block_die)
19613 {
19614   expanded_location xloc;
19615   dw_die_ref imported_die = NULL;
19616   dw_die_ref at_import_die;
19617
19618   if (TREE_CODE (decl) == IMPORTED_DECL)
19619     {
19620       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19621       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19622       gcc_assert (decl);
19623     }
19624   else
19625     xloc = expand_location (input_location);
19626
19627   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19628     {
19629       at_import_die = force_type_die (TREE_TYPE (decl));
19630       /* For namespace N { typedef void T; } using N::T; base_type_die
19631          returns NULL, but DW_TAG_imported_declaration requires
19632          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19633       if (!at_import_die)
19634         {
19635           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19636           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19637           at_import_die = lookup_type_die (TREE_TYPE (decl));
19638           gcc_assert (at_import_die);
19639         }
19640     }
19641   else
19642     {
19643       at_import_die = lookup_decl_die (decl);
19644       if (!at_import_die)
19645         {
19646           /* If we're trying to avoid duplicate debug info, we may not have
19647              emitted the member decl for this field.  Emit it now.  */
19648           if (TREE_CODE (decl) == FIELD_DECL)
19649             {
19650               tree type = DECL_CONTEXT (decl);
19651
19652               if (TYPE_CONTEXT (type)
19653                   && TYPE_P (TYPE_CONTEXT (type))
19654                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19655                                                 DINFO_USAGE_DIR_USE))
19656                 return;
19657               gen_type_die_for_member (type, decl,
19658                                        get_context_die (TYPE_CONTEXT (type)));
19659             }
19660           at_import_die = force_decl_die (decl);
19661         }
19662     }
19663
19664   if (TREE_CODE (decl) == NAMESPACE_DECL)
19665     {
19666       if (dwarf_version >= 3 || !dwarf_strict)
19667         imported_die = new_die (DW_TAG_imported_module,
19668                                 lexical_block_die,
19669                                 lexical_block);
19670       else
19671         return;
19672     }
19673   else
19674     imported_die = new_die (DW_TAG_imported_declaration,
19675                             lexical_block_die,
19676                             lexical_block);
19677
19678   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19679   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19680   if (name)
19681     add_AT_string (imported_die, DW_AT_name,
19682                    IDENTIFIER_POINTER (name));
19683   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19684 }
19685
19686 /* Output debug information for imported module or decl DECL.
19687    NAME is non-NULL name in context if the decl has been renamed.
19688    CHILD is true if decl is one of the renamed decls as part of
19689    importing whole module.  */
19690
19691 static void
19692 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19693                                    bool child)
19694 {
19695   /* dw_die_ref at_import_die;  */
19696   dw_die_ref scope_die;
19697
19698   if (debug_info_level <= DINFO_LEVEL_TERSE)
19699     return;
19700
19701   gcc_assert (decl);
19702
19703   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19704      We need decl DIE for reference and scope die. First, get DIE for the decl
19705      itself.  */
19706
19707   /* Get the scope die for decl context. Use comp_unit_die for global module
19708      or decl. If die is not found for non globals, force new die.  */
19709   if (context
19710       && TYPE_P (context)
19711       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19712     return;
19713
19714   if (!(dwarf_version >= 3 || !dwarf_strict))
19715     return;
19716
19717   scope_die = get_context_die (context);
19718
19719   if (child)
19720     {
19721       gcc_assert (scope_die->die_child);
19722       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19723       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19724       scope_die = scope_die->die_child;
19725     }
19726
19727   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19728   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19729
19730 }
19731
19732 /* Write the debugging output for DECL.  */
19733
19734 void
19735 dwarf2out_decl (tree decl)
19736 {
19737   dw_die_ref context_die = comp_unit_die ();
19738
19739   switch (TREE_CODE (decl))
19740     {
19741     case ERROR_MARK:
19742       return;
19743
19744     case FUNCTION_DECL:
19745       /* What we would really like to do here is to filter out all mere
19746          file-scope declarations of file-scope functions which are never
19747          referenced later within this translation unit (and keep all of ones
19748          that *are* referenced later on) but we aren't clairvoyant, so we have
19749          no idea which functions will be referenced in the future (i.e. later
19750          on within the current translation unit). So here we just ignore all
19751          file-scope function declarations which are not also definitions.  If
19752          and when the debugger needs to know something about these functions,
19753          it will have to hunt around and find the DWARF information associated
19754          with the definition of the function.
19755
19756          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19757          nodes represent definitions and which ones represent mere
19758          declarations.  We have to check DECL_INITIAL instead. That's because
19759          the C front-end supports some weird semantics for "extern inline"
19760          function definitions.  These can get inlined within the current
19761          translation unit (and thus, we need to generate Dwarf info for their
19762          abstract instances so that the Dwarf info for the concrete inlined
19763          instances can have something to refer to) but the compiler never
19764          generates any out-of-lines instances of such things (despite the fact
19765          that they *are* definitions).
19766
19767          The important point is that the C front-end marks these "extern
19768          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19769          them anyway. Note that the C++ front-end also plays some similar games
19770          for inline function definitions appearing within include files which
19771          also contain `#pragma interface' pragmas.  */
19772       if (DECL_INITIAL (decl) == NULL_TREE)
19773         return;
19774
19775       /* If we're a nested function, initially use a parent of NULL; if we're
19776          a plain function, this will be fixed up in decls_for_scope.  If
19777          we're a method, it will be ignored, since we already have a DIE.  */
19778       if (decl_function_context (decl)
19779           /* But if we're in terse mode, we don't care about scope.  */
19780           && debug_info_level > DINFO_LEVEL_TERSE)
19781         context_die = NULL;
19782       break;
19783
19784     case VAR_DECL:
19785       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19786          declaration and if the declaration was never even referenced from
19787          within this entire compilation unit.  We suppress these DIEs in
19788          order to save space in the .debug section (by eliminating entries
19789          which are probably useless).  Note that we must not suppress
19790          block-local extern declarations (whether used or not) because that
19791          would screw-up the debugger's name lookup mechanism and cause it to
19792          miss things which really ought to be in scope at a given point.  */
19793       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19794         return;
19795
19796       /* For local statics lookup proper context die.  */
19797       if (TREE_STATIC (decl) && decl_function_context (decl))
19798         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19799
19800       /* If we are in terse mode, don't generate any DIEs to represent any
19801          variable declarations or definitions.  */
19802       if (debug_info_level <= DINFO_LEVEL_TERSE)
19803         return;
19804       break;
19805
19806     case CONST_DECL:
19807       if (debug_info_level <= DINFO_LEVEL_TERSE)
19808         return;
19809       if (!is_fortran () && !is_ada ())
19810         return;
19811       if (TREE_STATIC (decl) && decl_function_context (decl))
19812         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19813       break;
19814
19815     case NAMESPACE_DECL:
19816     case IMPORTED_DECL:
19817       if (debug_info_level <= DINFO_LEVEL_TERSE)
19818         return;
19819       if (lookup_decl_die (decl) != NULL)
19820         return;
19821       break;
19822
19823     case TYPE_DECL:
19824       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19825       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19826         return;
19827
19828       /* Don't bother trying to generate any DIEs to represent any of the
19829          normal built-in types for the language we are compiling.  */
19830       if (DECL_IS_BUILTIN (decl))
19831         return;
19832
19833       /* If we are in terse mode, don't generate any DIEs for types.  */
19834       if (debug_info_level <= DINFO_LEVEL_TERSE)
19835         return;
19836
19837       /* If we're a function-scope tag, initially use a parent of NULL;
19838          this will be fixed up in decls_for_scope.  */
19839       if (decl_function_context (decl))
19840         context_die = NULL;
19841
19842       break;
19843
19844     default:
19845       return;
19846     }
19847
19848   gen_decl_die (decl, NULL, context_die);
19849 }
19850
19851 /* Write the debugging output for DECL.  */
19852
19853 static void
19854 dwarf2out_function_decl (tree decl)
19855 {
19856   dwarf2out_decl (decl);
19857   call_arg_locations = NULL;
19858   call_arg_loc_last = NULL;
19859   call_site_count = -1;
19860   tail_call_site_count = -1;
19861   VEC_free (dw_die_ref, heap, block_map);
19862   htab_empty (decl_loc_table);
19863   htab_empty (cached_dw_loc_list_table);
19864 }
19865
19866 /* Output a marker (i.e. a label) for the beginning of the generated code for
19867    a lexical block.  */
19868
19869 static void
19870 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19871                        unsigned int blocknum)
19872 {
19873   switch_to_section (current_function_section ());
19874   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19875 }
19876
19877 /* Output a marker (i.e. a label) for the end of the generated code for a
19878    lexical block.  */
19879
19880 static void
19881 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19882 {
19883   switch_to_section (current_function_section ());
19884   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19885 }
19886
19887 /* Returns nonzero if it is appropriate not to emit any debugging
19888    information for BLOCK, because it doesn't contain any instructions.
19889
19890    Don't allow this for blocks with nested functions or local classes
19891    as we would end up with orphans, and in the presence of scheduling
19892    we may end up calling them anyway.  */
19893
19894 static bool
19895 dwarf2out_ignore_block (const_tree block)
19896 {
19897   tree decl;
19898   unsigned int i;
19899
19900   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19901     if (TREE_CODE (decl) == FUNCTION_DECL
19902         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19903       return 0;
19904   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19905     {
19906       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19907       if (TREE_CODE (decl) == FUNCTION_DECL
19908           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19909       return 0;
19910     }
19911
19912   return 1;
19913 }
19914
19915 /* Hash table routines for file_hash.  */
19916
19917 static int
19918 file_table_eq (const void *p1_p, const void *p2_p)
19919 {
19920   const struct dwarf_file_data *const p1 =
19921     (const struct dwarf_file_data *) p1_p;
19922   const char *const p2 = (const char *) p2_p;
19923   return filename_cmp (p1->filename, p2) == 0;
19924 }
19925
19926 static hashval_t
19927 file_table_hash (const void *p_p)
19928 {
19929   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19930   return htab_hash_string (p->filename);
19931 }
19932
19933 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19934    dwarf2out.c) and return its "index".  The index of each (known) filename is
19935    just a unique number which is associated with only that one filename.  We
19936    need such numbers for the sake of generating labels (in the .debug_sfnames
19937    section) and references to those files numbers (in the .debug_srcinfo
19938    and.debug_macinfo sections).  If the filename given as an argument is not
19939    found in our current list, add it to the list and assign it the next
19940    available unique index number.  In order to speed up searches, we remember
19941    the index of the filename was looked up last.  This handles the majority of
19942    all searches.  */
19943
19944 static struct dwarf_file_data *
19945 lookup_filename (const char *file_name)
19946 {
19947   void ** slot;
19948   struct dwarf_file_data * created;
19949
19950   /* Check to see if the file name that was searched on the previous
19951      call matches this file name.  If so, return the index.  */
19952   if (file_table_last_lookup
19953       && (file_name == file_table_last_lookup->filename
19954           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19955     return file_table_last_lookup;
19956
19957   /* Didn't match the previous lookup, search the table.  */
19958   slot = htab_find_slot_with_hash (file_table, file_name,
19959                                    htab_hash_string (file_name), INSERT);
19960   if (*slot)
19961     return (struct dwarf_file_data *) *slot;
19962
19963   created = ggc_alloc_dwarf_file_data ();
19964   created->filename = file_name;
19965   created->emitted_number = 0;
19966   *slot = created;
19967   return created;
19968 }
19969
19970 /* If the assembler will construct the file table, then translate the compiler
19971    internal file table number into the assembler file table number, and emit
19972    a .file directive if we haven't already emitted one yet.  The file table
19973    numbers are different because we prune debug info for unused variables and
19974    types, which may include filenames.  */
19975
19976 static int
19977 maybe_emit_file (struct dwarf_file_data * fd)
19978 {
19979   if (! fd->emitted_number)
19980     {
19981       if (last_emitted_file)
19982         fd->emitted_number = last_emitted_file->emitted_number + 1;
19983       else
19984         fd->emitted_number = 1;
19985       last_emitted_file = fd;
19986
19987       if (DWARF2_ASM_LINE_DEBUG_INFO)
19988         {
19989           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19990           output_quoted_string (asm_out_file,
19991                                 remap_debug_filename (fd->filename));
19992           fputc ('\n', asm_out_file);
19993         }
19994     }
19995
19996   return fd->emitted_number;
19997 }
19998
19999 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20000    That generation should happen after function debug info has been
20001    generated. The value of the attribute is the constant value of ARG.  */
20002
20003 static void
20004 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20005 {
20006   die_arg_entry entry;
20007
20008   if (!die || !arg)
20009     return;
20010
20011   if (!tmpl_value_parm_die_table)
20012     tmpl_value_parm_die_table
20013       = VEC_alloc (die_arg_entry, gc, 32);
20014
20015   entry.die = die;
20016   entry.arg = arg;
20017   VEC_safe_push (die_arg_entry, gc,
20018                  tmpl_value_parm_die_table,
20019                  &entry);
20020 }
20021
20022 /* Return TRUE if T is an instance of generic type, FALSE
20023    otherwise.  */
20024
20025 static bool
20026 generic_type_p (tree t)
20027 {
20028   if (t == NULL_TREE || !TYPE_P (t))
20029     return false;
20030   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20031 }
20032
20033 /* Schedule the generation of the generic parameter dies for the
20034   instance of generic type T. The proper generation itself is later
20035   done by gen_scheduled_generic_parms_dies. */
20036
20037 static void
20038 schedule_generic_params_dies_gen (tree t)
20039 {
20040   if (!generic_type_p (t))
20041     return;
20042
20043   if (generic_type_instances == NULL)
20044     generic_type_instances = VEC_alloc (tree, gc, 256);
20045
20046   VEC_safe_push (tree, gc, generic_type_instances, t);
20047 }
20048
20049 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20050    by append_entry_to_tmpl_value_parm_die_table. This function must
20051    be called after function DIEs have been generated.  */
20052
20053 static void
20054 gen_remaining_tmpl_value_param_die_attribute (void)
20055 {
20056   if (tmpl_value_parm_die_table)
20057     {
20058       unsigned i;
20059       die_arg_entry *e;
20060
20061       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20062         tree_add_const_value_attribute (e->die, e->arg);
20063     }
20064 }
20065
20066 /* Generate generic parameters DIEs for instances of generic types
20067    that have been previously scheduled by
20068    schedule_generic_params_dies_gen. This function must be called
20069    after all the types of the CU have been laid out.  */
20070
20071 static void
20072 gen_scheduled_generic_parms_dies (void)
20073 {
20074   unsigned i;
20075   tree t;
20076
20077   if (generic_type_instances == NULL)
20078     return;
20079   
20080   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20081     gen_generic_params_dies (t);
20082 }
20083
20084
20085 /* Replace DW_AT_name for the decl with name.  */
20086
20087 static void
20088 dwarf2out_set_name (tree decl, tree name)
20089 {
20090   dw_die_ref die;
20091   dw_attr_ref attr;
20092   const char *dname;
20093
20094   die = TYPE_SYMTAB_DIE (decl);
20095   if (!die)
20096     return;
20097
20098   dname = dwarf2_name (name, 0);
20099   if (!dname)
20100     return;
20101
20102   attr = get_AT (die, DW_AT_name);
20103   if (attr)
20104     {
20105       struct indirect_string_node *node;
20106
20107       node = find_AT_string (dname);
20108       /* replace the string.  */
20109       attr->dw_attr_val.v.val_str = node;
20110     }
20111
20112   else
20113     add_name_attribute (die, dname);
20114 }
20115
20116 /* Called by the final INSN scan whenever we see a var location.  We
20117    use it to drop labels in the right places, and throw the location in
20118    our lookup table.  */
20119
20120 static void
20121 dwarf2out_var_location (rtx loc_note)
20122 {
20123   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20124   struct var_loc_node *newloc;
20125   rtx next_real;
20126   static const char *last_label;
20127   static const char *last_postcall_label;
20128   static bool last_in_cold_section_p;
20129   tree decl;
20130   bool var_loc_p;
20131
20132   if (!NOTE_P (loc_note))
20133     {
20134       if (CALL_P (loc_note))
20135         {
20136           call_site_count++;
20137           if (SIBLING_CALL_P (loc_note))
20138             tail_call_site_count++;
20139         }
20140       return;
20141     }
20142
20143   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20144   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20145     return;
20146
20147   next_real = next_real_insn (loc_note);
20148
20149   /* If there are no instructions which would be affected by this note,
20150      don't do anything.  */
20151   if (var_loc_p
20152       && next_real == NULL_RTX
20153       && !NOTE_DURING_CALL_P (loc_note))
20154     return;
20155
20156   if (next_real == NULL_RTX)
20157     next_real = get_last_insn ();
20158
20159   /* If there were any real insns between note we processed last time
20160      and this note (or if it is the first note), clear
20161      last_{,postcall_}label so that they are not reused this time.  */
20162   if (last_var_location_insn == NULL_RTX
20163       || last_var_location_insn != next_real
20164       || last_in_cold_section_p != in_cold_section_p)
20165     {
20166       last_label = NULL;
20167       last_postcall_label = NULL;
20168     }
20169
20170   if (var_loc_p)
20171     {
20172       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20173       newloc = add_var_loc_to_decl (decl, loc_note,
20174                                     NOTE_DURING_CALL_P (loc_note)
20175                                     ? last_postcall_label : last_label);
20176       if (newloc == NULL)
20177         return;
20178     }
20179   else
20180     {
20181       decl = NULL_TREE;
20182       newloc = NULL;
20183     }
20184
20185   /* If there were no real insns between note we processed last time
20186      and this note, use the label we emitted last time.  Otherwise
20187      create a new label and emit it.  */
20188   if (last_label == NULL)
20189     {
20190       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20191       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20192       loclabel_num++;
20193       last_label = ggc_strdup (loclabel);
20194     }
20195
20196   if (!var_loc_p)
20197     {
20198       struct call_arg_loc_node *ca_loc
20199         = ggc_alloc_cleared_call_arg_loc_node ();
20200       rtx prev = prev_real_insn (loc_note), x;
20201       ca_loc->call_arg_loc_note = loc_note;
20202       ca_loc->next = NULL;
20203       ca_loc->label = last_label;
20204       gcc_assert (prev
20205                   && (CALL_P (prev)
20206                       || (NONJUMP_INSN_P (prev)
20207                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20208                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20209       if (!CALL_P (prev))
20210         prev = XVECEXP (PATTERN (prev), 0, 0);
20211       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20212       x = PATTERN (prev);
20213       if (GET_CODE (x) == PARALLEL)
20214         x = XVECEXP (x, 0, 0);
20215       if (GET_CODE (x) == SET)
20216         x = SET_SRC (x);
20217       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20218         {
20219           x = XEXP (XEXP (x, 0), 0);
20220           if (GET_CODE (x) == SYMBOL_REF
20221               && SYMBOL_REF_DECL (x)
20222               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20223             ca_loc->symbol_ref = x;
20224         }
20225       ca_loc->block = insn_scope (prev);
20226       if (call_arg_locations)
20227         call_arg_loc_last->next = ca_loc;
20228       else
20229         call_arg_locations = ca_loc;
20230       call_arg_loc_last = ca_loc;
20231     }
20232   else if (!NOTE_DURING_CALL_P (loc_note))
20233     newloc->label = last_label;
20234   else
20235     {
20236       if (!last_postcall_label)
20237         {
20238           sprintf (loclabel, "%s-1", last_label);
20239           last_postcall_label = ggc_strdup (loclabel);
20240         }
20241       newloc->label = last_postcall_label;
20242     }
20243
20244   last_var_location_insn = next_real;
20245   last_in_cold_section_p = in_cold_section_p;
20246 }
20247
20248 /* Note in one location list that text section has changed.  */
20249
20250 static int
20251 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20252 {
20253   var_loc_list *list = (var_loc_list *) *slot;
20254   if (list->first)
20255     list->last_before_switch
20256       = list->last->next ? list->last->next : list->last;
20257   return 1;
20258 }
20259
20260 /* Note in all location lists that text section has changed.  */
20261
20262 static void
20263 var_location_switch_text_section (void)
20264 {
20265   if (decl_loc_table == NULL)
20266     return;
20267
20268   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20269 }
20270
20271 /* Create a new line number table.  */
20272
20273 static dw_line_info_table *
20274 new_line_info_table (void)
20275 {
20276   dw_line_info_table *table;
20277
20278   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20279   table->file_num = 1;
20280   table->line_num = 1;
20281   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20282
20283   return table;
20284 }
20285
20286 /* Lookup the "current" table into which we emit line info, so
20287    that we don't have to do it for every source line.  */
20288
20289 static void
20290 set_cur_line_info_table (section *sec)
20291 {
20292   dw_line_info_table *table;
20293
20294   if (sec == text_section)
20295     table = text_section_line_info;
20296   else if (sec == cold_text_section)
20297     {
20298       table = cold_text_section_line_info;
20299       if (!table)
20300         {
20301           cold_text_section_line_info = table = new_line_info_table ();
20302           table->end_label = cold_end_label;
20303         }
20304     }
20305   else
20306     {
20307       const char *end_label;
20308
20309       if (flag_reorder_blocks_and_partition)
20310         {
20311           if (in_cold_section_p)
20312             end_label = crtl->subsections.cold_section_end_label;
20313           else
20314             end_label = crtl->subsections.hot_section_end_label;
20315         }
20316       else
20317         {
20318           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20319           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20320                                        current_function_funcdef_no);
20321           end_label = ggc_strdup (label);
20322         }
20323
20324       table = new_line_info_table ();
20325       table->end_label = end_label;
20326
20327       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20328     }
20329
20330   cur_line_info_table = table;
20331 }
20332
20333
20334 /* We need to reset the locations at the beginning of each
20335    function. We can't do this in the end_function hook, because the
20336    declarations that use the locations won't have been output when
20337    that hook is called.  Also compute have_multiple_function_sections here.  */
20338
20339 static void
20340 dwarf2out_begin_function (tree fun)
20341 {
20342   section *sec = function_section (fun);
20343
20344   if (sec != text_section)
20345     have_multiple_function_sections = true;
20346
20347   if (flag_reorder_blocks_and_partition && !cold_text_section)
20348     {
20349       gcc_assert (current_function_decl == fun);
20350       cold_text_section = unlikely_text_section ();
20351       switch_to_section (cold_text_section);
20352       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20353       switch_to_section (sec);
20354     }
20355
20356   dwarf2out_note_section_used ();
20357   call_site_count = 0;
20358   tail_call_site_count = 0;
20359
20360   set_cur_line_info_table (sec);
20361 }
20362
20363 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20364
20365 static void
20366 push_dw_line_info_entry (dw_line_info_table *table,
20367                          enum dw_line_info_opcode opcode, unsigned int val)
20368 {
20369   dw_line_info_entry e;
20370   e.opcode = opcode;
20371   e.val = val;
20372   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20373 }
20374
20375 /* Output a label to mark the beginning of a source code line entry
20376    and record information relating to this source line, in
20377    'line_info_table' for later output of the .debug_line section.  */
20378 /* ??? The discriminator parameter ought to be unsigned.  */
20379
20380 static void
20381 dwarf2out_source_line (unsigned int line, const char *filename,
20382                        int discriminator, bool is_stmt)
20383 {
20384   unsigned int file_num;
20385   dw_line_info_table *table;
20386
20387   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20388     return;
20389
20390   /* The discriminator column was added in dwarf4.  Simplify the below
20391      by simply removing it if we're not supposed to output it.  */
20392   if (dwarf_version < 4 && dwarf_strict)
20393     discriminator = 0;
20394
20395   table = cur_line_info_table;
20396   file_num = maybe_emit_file (lookup_filename (filename));
20397
20398   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20399      the debugger has used the second (possibly duplicate) line number
20400      at the beginning of the function to mark the end of the prologue.
20401      We could eliminate any other duplicates within the function.  For
20402      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20403      that second line number entry.  */
20404   /* Recall that this end-of-prologue indication is *not* the same thing
20405      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20406      to which the hook corresponds, follows the last insn that was 
20407      emitted by gen_prologue.  What we need is to preceed the first insn
20408      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20409      insn that corresponds to something the user wrote.  These may be
20410      very different locations once scheduling is enabled.  */
20411
20412   if (0 && file_num == table->file_num
20413       && line == table->line_num
20414       && discriminator == table->discrim_num
20415       && is_stmt == table->is_stmt)
20416     return;
20417
20418   switch_to_section (current_function_section ());
20419
20420   /* If requested, emit something human-readable.  */
20421   if (flag_debug_asm)
20422     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20423
20424   if (DWARF2_ASM_LINE_DEBUG_INFO)
20425     {
20426       /* Emit the .loc directive understood by GNU as.  */
20427       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20428       if (is_stmt != table->is_stmt)
20429         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20430       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20431         fprintf (asm_out_file, " discriminator %d", discriminator);
20432       fputc ('\n', asm_out_file);
20433     }
20434   else
20435     {
20436       unsigned int label_num = ++line_info_label_num;
20437
20438       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20439
20440       push_dw_line_info_entry (table, LI_set_address, label_num);
20441       if (file_num != table->file_num)
20442         push_dw_line_info_entry (table, LI_set_file, file_num);
20443       if (discriminator != table->discrim_num)
20444         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20445       if (is_stmt != table->is_stmt)
20446         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20447       push_dw_line_info_entry (table, LI_set_line, line);
20448     }
20449
20450   table->file_num = file_num;
20451   table->line_num = line;
20452   table->discrim_num = discriminator;
20453   table->is_stmt = is_stmt;
20454   table->in_use = true;
20455 }
20456
20457 /* Record the beginning of a new source file.  */
20458
20459 static void
20460 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20461 {
20462   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20463     {
20464       /* Record the beginning of the file for break_out_includes.  */
20465       dw_die_ref bincl_die;
20466
20467       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20468       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20469     }
20470
20471   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20472     {
20473       macinfo_entry e;
20474       e.code = DW_MACINFO_start_file;
20475       e.lineno = lineno;
20476       e.info = xstrdup (filename);
20477       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20478     }
20479 }
20480
20481 /* Record the end of a source file.  */
20482
20483 static void
20484 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20485 {
20486   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20487     /* Record the end of the file for break_out_includes.  */
20488     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20489
20490   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20491     {
20492       macinfo_entry e;
20493       e.code = DW_MACINFO_end_file;
20494       e.lineno = lineno;
20495       e.info = NULL;
20496       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20497     }
20498 }
20499
20500 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20501    the tail part of the directive line, i.e. the part which is past the
20502    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20503
20504 static void
20505 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20506                   const char *buffer ATTRIBUTE_UNUSED)
20507 {
20508   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20509     {
20510       macinfo_entry e;
20511       /* Insert a dummy first entry to be able to optimize the whole
20512          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20513       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20514         {
20515           e.code = 0;
20516           e.lineno = 0;
20517           e.info = NULL;
20518           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20519         }
20520       e.code = DW_MACINFO_define;
20521       e.lineno = lineno;
20522       e.info = xstrdup (buffer);;
20523       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20524     }
20525 }
20526
20527 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20528    the tail part of the directive line, i.e. the part which is past the
20529    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20530
20531 static void
20532 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20533                  const char *buffer ATTRIBUTE_UNUSED)
20534 {
20535   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20536     {
20537       macinfo_entry e;
20538       /* Insert a dummy first entry to be able to optimize the whole
20539          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20540       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20541         {
20542           e.code = 0;
20543           e.lineno = 0;
20544           e.info = NULL;
20545           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20546         }
20547       e.code = DW_MACINFO_undef;
20548       e.lineno = lineno;
20549       e.info = xstrdup (buffer);
20550       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20551     }
20552 }
20553
20554 /* Routines to manipulate hash table of CUs.  */
20555
20556 static hashval_t
20557 htab_macinfo_hash (const void *of)
20558 {
20559   const macinfo_entry *const entry =
20560     (const macinfo_entry *) of;
20561
20562   return htab_hash_string (entry->info);
20563 }
20564
20565 static int
20566 htab_macinfo_eq (const void *of1, const void *of2)
20567 {
20568   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20569   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20570
20571   return !strcmp (entry1->info, entry2->info);
20572 }
20573
20574 /* Output a single .debug_macinfo entry.  */
20575
20576 static void
20577 output_macinfo_op (macinfo_entry *ref)
20578 {
20579   int file_num;
20580   size_t len;
20581   struct indirect_string_node *node;
20582   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20583   struct dwarf_file_data *fd;
20584
20585   switch (ref->code)
20586     {
20587     case DW_MACINFO_start_file:
20588       fd = lookup_filename (ref->info);
20589       if (fd->filename == ref->info)
20590         fd->filename = ggc_strdup (fd->filename);
20591       file_num = maybe_emit_file (fd);
20592       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20593       dw2_asm_output_data_uleb128 (ref->lineno,
20594                                    "Included from line number %lu", 
20595                                    (unsigned long) ref->lineno);
20596       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20597       break;
20598     case DW_MACINFO_end_file:
20599       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20600       break;
20601     case DW_MACINFO_define:
20602     case DW_MACINFO_undef:
20603       len = strlen (ref->info) + 1;
20604       if (!dwarf_strict
20605           && len > DWARF_OFFSET_SIZE
20606           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20607           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20608         {
20609           ref->code = ref->code == DW_MACINFO_define
20610                       ? DW_MACRO_GNU_define_indirect
20611                       : DW_MACRO_GNU_undef_indirect;
20612           output_macinfo_op (ref);
20613           return;
20614         }
20615       dw2_asm_output_data (1, ref->code,
20616                            ref->code == DW_MACINFO_define
20617                            ? "Define macro" : "Undefine macro");
20618       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20619                                    (unsigned long) ref->lineno);
20620       dw2_asm_output_nstring (ref->info, -1, "The macro");
20621       break;
20622     case DW_MACRO_GNU_define_indirect:
20623     case DW_MACRO_GNU_undef_indirect:
20624       node = find_AT_string (ref->info);
20625       if (node->form != DW_FORM_strp)
20626         {
20627           char label[32];
20628           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20629           ++dw2_string_counter;
20630           node->label = xstrdup (label);
20631           node->form = DW_FORM_strp;
20632         }
20633       dw2_asm_output_data (1, ref->code,
20634                            ref->code == DW_MACRO_GNU_define_indirect
20635                            ? "Define macro indirect"
20636                            : "Undefine macro indirect");
20637       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20638                                    (unsigned long) ref->lineno);
20639       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20640                              debug_str_section, "The macro: \"%s\"",
20641                              ref->info);
20642       break;
20643     case DW_MACRO_GNU_transparent_include:
20644       dw2_asm_output_data (1, ref->code, "Transparent include");
20645       ASM_GENERATE_INTERNAL_LABEL (label,
20646                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20647       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20648       break;
20649     default:
20650       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20651                ASM_COMMENT_START, (unsigned long) ref->code);
20652       break;
20653     }
20654 }
20655
20656 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20657    other compilation unit .debug_macinfo sections.  IDX is the first
20658    index of a define/undef, return the number of ops that should be
20659    emitted in a comdat .debug_macinfo section and emit
20660    a DW_MACRO_GNU_transparent_include entry referencing it.
20661    If the define/undef entry should be emitted normally, return 0.  */
20662
20663 static unsigned
20664 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20665                         htab_t *macinfo_htab)
20666 {
20667   macinfo_entry *first, *second, *cur, *inc;
20668   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20669   unsigned char checksum[16];
20670   struct md5_ctx ctx;
20671   char *grp_name, *tail;
20672   const char *base;
20673   unsigned int i, count, encoded_filename_len, linebuf_len;
20674   void **slot;
20675
20676   first = VEC_index (macinfo_entry, macinfo_table, idx);
20677   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20678
20679   /* Optimize only if there are at least two consecutive define/undef ops,
20680      and either all of them are before first DW_MACINFO_start_file
20681      with lineno 0 (i.e. predefined macro block), or all of them are
20682      in some included header file.  */
20683   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20684     return 0;
20685   if (VEC_empty (macinfo_entry, files))
20686     {
20687       if (first->lineno != 0 || second->lineno != 0)
20688         return 0;
20689     }
20690   else if (first->lineno == 0)
20691     return 0;
20692
20693   /* Find the last define/undef entry that can be grouped together
20694      with first and at the same time compute md5 checksum of their
20695      codes, linenumbers and strings.  */
20696   md5_init_ctx (&ctx);
20697   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20698     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20699       break;
20700     else if (first->lineno == 0 && cur->lineno != 0)
20701       break;
20702     else
20703       {
20704         unsigned char code = cur->code;
20705         md5_process_bytes (&code, 1, &ctx);
20706         checksum_uleb128 (cur->lineno, &ctx);
20707         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20708       }
20709   md5_finish_ctx (&ctx, checksum);
20710   count = i - idx;
20711
20712   /* From the containing include filename (if any) pick up just
20713      usable characters from its basename.  */
20714   if (first->lineno == 0)
20715     base = "";
20716   else
20717     base = lbasename (VEC_last (macinfo_entry, files)->info);
20718   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20719     if (ISIDNUM (base[i]) || base[i] == '.')
20720       encoded_filename_len++;
20721   /* Count . at the end.  */
20722   if (encoded_filename_len)
20723     encoded_filename_len++;
20724
20725   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20726   linebuf_len = strlen (linebuf);
20727
20728   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20729   grp_name = XNEWVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20730                       + 16 * 2 + 1);
20731   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20732   tail = grp_name + 4;
20733   if (encoded_filename_len)
20734     {
20735       for (i = 0; base[i]; i++)
20736         if (ISIDNUM (base[i]) || base[i] == '.')
20737           *tail++ = base[i];
20738       *tail++ = '.';
20739     }
20740   memcpy (tail, linebuf, linebuf_len);
20741   tail += linebuf_len;
20742   *tail++ = '.';
20743   for (i = 0; i < 16; i++)
20744     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20745
20746   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20747      in the empty vector entry before the first define/undef.  */
20748   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20749   inc->code = DW_MACRO_GNU_transparent_include;
20750   inc->lineno = 0;
20751   inc->info = grp_name;
20752   if (*macinfo_htab == NULL)
20753     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20754   /* Avoid emitting duplicates.  */
20755   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20756   if (*slot != NULL)
20757     {
20758       free (CONST_CAST (char *, inc->info));
20759       inc->code = 0;
20760       inc->info = NULL;
20761       /* If such an entry has been used before, just emit
20762          a DW_MACRO_GNU_transparent_include op.  */
20763       inc = (macinfo_entry *) *slot;
20764       output_macinfo_op (inc);
20765       /* And clear all macinfo_entry in the range to avoid emitting them
20766          in the second pass.  */
20767       for (i = idx;
20768            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20769            && i < idx + count;
20770            i++)
20771         {
20772           cur->code = 0;
20773           free (CONST_CAST (char *, cur->info));
20774           cur->info = NULL;
20775         }
20776     }
20777   else
20778     {
20779       *slot = inc;
20780       inc->lineno = htab_elements (*macinfo_htab);
20781       output_macinfo_op (inc);
20782     }
20783   return count;
20784 }
20785
20786 /* Output macinfo section(s).  */
20787
20788 static void
20789 output_macinfo (void)
20790 {
20791   unsigned i;
20792   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20793   macinfo_entry *ref;
20794   VEC (macinfo_entry, gc) *files = NULL;
20795   htab_t macinfo_htab = NULL;
20796
20797   if (! length)
20798     return;
20799
20800   /* output_macinfo* uses these interchangeably.  */
20801   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20802               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20803               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20804               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20805
20806   /* For .debug_macro emit the section header.  */
20807   if (!dwarf_strict)
20808     {
20809       dw2_asm_output_data (2, 4, "DWARF macro version number");
20810       if (DWARF_OFFSET_SIZE == 8)
20811         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20812       else
20813         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20814       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20815                              debug_line_section, NULL);
20816     }
20817
20818   /* In the first loop, it emits the primary .debug_macinfo section
20819      and after each emitted op the macinfo_entry is cleared.
20820      If a longer range of define/undef ops can be optimized using
20821      DW_MACRO_GNU_transparent_include, the
20822      DW_MACRO_GNU_transparent_include op is emitted and kept in
20823      the vector before the first define/undef in the range and the
20824      whole range of define/undef ops is not emitted and kept.  */
20825   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20826     {
20827       switch (ref->code)
20828         {
20829         case DW_MACINFO_start_file:
20830           VEC_safe_push (macinfo_entry, gc, files, ref);
20831           break;
20832         case DW_MACINFO_end_file:
20833           if (!VEC_empty (macinfo_entry, files))
20834             {
20835               macinfo_entry *file = VEC_last (macinfo_entry, files);
20836               free (CONST_CAST (char *, file->info));
20837               VEC_pop (macinfo_entry, files);
20838             }
20839           break;
20840         case DW_MACINFO_define:
20841         case DW_MACINFO_undef:
20842           if (!dwarf_strict
20843               && HAVE_COMDAT_GROUP
20844               && VEC_length (macinfo_entry, files) != 1
20845               && i > 0
20846               && i + 1 < length
20847               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20848             {
20849               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20850               if (count)
20851                 {
20852                   i += count - 1;
20853                   continue;
20854                 }
20855             }
20856           break;
20857         case 0:
20858           /* A dummy entry may be inserted at the beginning to be able
20859              to optimize the whole block of predefined macros.  */
20860           if (i == 0)
20861             continue;
20862         default:
20863           break;
20864         }
20865       output_macinfo_op (ref);
20866       /* For DW_MACINFO_start_file ref->info has been copied into files
20867          vector.  */
20868       if (ref->code != DW_MACINFO_start_file)
20869         free (CONST_CAST (char *, ref->info));
20870       ref->info = NULL;
20871       ref->code = 0;
20872     }
20873
20874   if (macinfo_htab == NULL)
20875     return;
20876
20877   htab_delete (macinfo_htab);
20878
20879   /* If any DW_MACRO_GNU_transparent_include were used, on those
20880      DW_MACRO_GNU_transparent_include entries terminate the
20881      current chain and switch to a new comdat .debug_macinfo
20882      section and emit the define/undef entries within it.  */
20883   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20884     switch (ref->code)
20885       {
20886       case 0:
20887         continue;
20888       case DW_MACRO_GNU_transparent_include:
20889         {
20890           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20891           tree comdat_key = get_identifier (ref->info);
20892           /* Terminate the previous .debug_macinfo section.  */
20893           dw2_asm_output_data (1, 0, "End compilation unit");
20894           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20895                                          SECTION_DEBUG
20896                                          | SECTION_LINKONCE,
20897                                          comdat_key);
20898           ASM_GENERATE_INTERNAL_LABEL (label,
20899                                        DEBUG_MACRO_SECTION_LABEL,
20900                                        ref->lineno);
20901           ASM_OUTPUT_LABEL (asm_out_file, label);
20902           ref->code = 0;
20903           free (CONST_CAST (char *, ref->info));
20904           ref->info = NULL;
20905           dw2_asm_output_data (2, 4, "DWARF macro version number");
20906           if (DWARF_OFFSET_SIZE == 8)
20907             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20908           else
20909             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20910         }
20911         break;
20912       case DW_MACINFO_define:
20913       case DW_MACINFO_undef:
20914         output_macinfo_op (ref);
20915         ref->code = 0;
20916         free (CONST_CAST (char *, ref->info));
20917         ref->info = NULL;
20918         break;
20919       default:
20920         gcc_unreachable ();
20921       }
20922 }
20923
20924 /* Set up for Dwarf output at the start of compilation.  */
20925
20926 static void
20927 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20928 {
20929   /* Allocate the file_table.  */
20930   file_table = htab_create_ggc (50, file_table_hash,
20931                                 file_table_eq, NULL);
20932
20933   /* Allocate the decl_die_table.  */
20934   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20935                                     decl_die_table_eq, NULL);
20936
20937   /* Allocate the decl_loc_table.  */
20938   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20939                                     decl_loc_table_eq, NULL);
20940
20941   /* Allocate the cached_dw_loc_list_table.  */
20942   cached_dw_loc_list_table
20943     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20944                        cached_dw_loc_list_table_eq, NULL);
20945
20946   /* Allocate the initial hunk of the decl_scope_table.  */
20947   decl_scope_table = VEC_alloc (tree, gc, 256);
20948
20949   /* Allocate the initial hunk of the abbrev_die_table.  */
20950   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20951     (ABBREV_DIE_TABLE_INCREMENT);
20952   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20953   /* Zero-th entry is allocated, but unused.  */
20954   abbrev_die_table_in_use = 1;
20955
20956   /* Allocate the pubtypes and pubnames vectors.  */
20957   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20958   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20959
20960   incomplete_types = VEC_alloc (tree, gc, 64);
20961
20962   used_rtx_array = VEC_alloc (rtx, gc, 32);
20963
20964   debug_info_section = get_section (DEBUG_INFO_SECTION,
20965                                     SECTION_DEBUG, NULL);
20966   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20967                                       SECTION_DEBUG, NULL);
20968   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20969                                        SECTION_DEBUG, NULL);
20970   debug_macinfo_section = get_section (dwarf_strict
20971                                        ? DEBUG_MACINFO_SECTION
20972                                        : DEBUG_MACRO_SECTION,
20973                                        SECTION_DEBUG, NULL);
20974   debug_line_section = get_section (DEBUG_LINE_SECTION,
20975                                     SECTION_DEBUG, NULL);
20976   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20977                                    SECTION_DEBUG, NULL);
20978   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20979                                         SECTION_DEBUG, NULL);
20980   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20981                                         SECTION_DEBUG, NULL);
20982   debug_str_section = get_section (DEBUG_STR_SECTION,
20983                                    DEBUG_STR_SECTION_FLAGS, NULL);
20984   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20985                                       SECTION_DEBUG, NULL);
20986   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20987                                      SECTION_DEBUG, NULL);
20988
20989   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20990   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20991                                DEBUG_ABBREV_SECTION_LABEL, 0);
20992   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20993   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20994                                COLD_TEXT_SECTION_LABEL, 0);
20995   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20996
20997   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20998                                DEBUG_INFO_SECTION_LABEL, 0);
20999   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21000                                DEBUG_LINE_SECTION_LABEL, 0);
21001   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21002                                DEBUG_RANGES_SECTION_LABEL, 0);
21003   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21004                                dwarf_strict
21005                                ? DEBUG_MACINFO_SECTION_LABEL
21006                                : DEBUG_MACRO_SECTION_LABEL, 0);
21007
21008   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21009     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21010
21011   switch_to_section (text_section);
21012   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21013
21014   /* Make sure the line number table for .text always exists.  */
21015   text_section_line_info = new_line_info_table ();
21016   text_section_line_info->end_label = text_end_label;
21017 }
21018
21019 /* Called before cgraph_optimize starts outputtting functions, variables
21020    and toplevel asms into assembly.  */
21021
21022 static void
21023 dwarf2out_assembly_start (void)
21024 {
21025   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21026       && dwarf2out_do_cfi_asm ()
21027       && (!(flag_unwind_tables || flag_exceptions)
21028           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21029     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21030 }
21031
21032 /* A helper function for dwarf2out_finish called through
21033    htab_traverse.  Emit one queued .debug_str string.  */
21034
21035 static int
21036 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21037 {
21038   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21039
21040   if (node->form == DW_FORM_strp)
21041     {
21042       switch_to_section (debug_str_section);
21043       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21044       assemble_string (node->str, strlen (node->str) + 1);
21045     }
21046
21047   return 1;
21048 }
21049
21050 #if ENABLE_ASSERT_CHECKING
21051 /* Verify that all marks are clear.  */
21052
21053 static void
21054 verify_marks_clear (dw_die_ref die)
21055 {
21056   dw_die_ref c;
21057
21058   gcc_assert (! die->die_mark);
21059   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21060 }
21061 #endif /* ENABLE_ASSERT_CHECKING */
21062
21063 /* Clear the marks for a die and its children.
21064    Be cool if the mark isn't set.  */
21065
21066 static void
21067 prune_unmark_dies (dw_die_ref die)
21068 {
21069   dw_die_ref c;
21070
21071   if (die->die_mark)
21072     die->die_mark = 0;
21073   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21074 }
21075
21076 /* Given DIE that we're marking as used, find any other dies
21077    it references as attributes and mark them as used.  */
21078
21079 static void
21080 prune_unused_types_walk_attribs (dw_die_ref die)
21081 {
21082   dw_attr_ref a;
21083   unsigned ix;
21084
21085   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21086     {
21087       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21088         {
21089           /* A reference to another DIE.
21090              Make sure that it will get emitted.
21091              If it was broken out into a comdat group, don't follow it.  */
21092           if (! use_debug_types
21093               || a->dw_attr == DW_AT_specification
21094               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21095             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21096         }
21097       /* Set the string's refcount to 0 so that prune_unused_types_mark
21098          accounts properly for it.  */
21099       if (AT_class (a) == dw_val_class_str)
21100         a->dw_attr_val.v.val_str->refcount = 0;
21101     }
21102 }
21103
21104 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21105
21106 static void
21107 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21108 {
21109   dw_die_ref c;
21110
21111   if (die == NULL || die->die_child == NULL)
21112     return;
21113   c = die->die_child;
21114   do
21115     {
21116       switch (c->die_tag)
21117         {
21118         case DW_TAG_template_type_param:
21119         case DW_TAG_template_value_param:
21120         case DW_TAG_GNU_template_template_param:
21121         case DW_TAG_GNU_template_parameter_pack:
21122           prune_unused_types_mark (c, 1);
21123           break;
21124         default:
21125           break;
21126         }
21127       c = c->die_sib;
21128     } while (c && c != die->die_child);
21129 }
21130
21131 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21132    to DIE's children.  */
21133
21134 static void
21135 prune_unused_types_mark (dw_die_ref die, int dokids)
21136 {
21137   dw_die_ref c;
21138
21139   if (die->die_mark == 0)
21140     {
21141       /* We haven't done this node yet.  Mark it as used.  */
21142       die->die_mark = 1;
21143       /* If this is the DIE of a generic type instantiation,
21144          mark the children DIEs that describe its generic parms and
21145          args.  */
21146       prune_unused_types_mark_generic_parms_dies (die);
21147
21148       /* We also have to mark its parents as used.
21149          (But we don't want to mark our parents' kids due to this.)  */
21150       if (die->die_parent)
21151         prune_unused_types_mark (die->die_parent, 0);
21152
21153       /* Mark any referenced nodes.  */
21154       prune_unused_types_walk_attribs (die);
21155
21156       /* If this node is a specification,
21157          also mark the definition, if it exists.  */
21158       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21159         prune_unused_types_mark (die->die_definition, 1);
21160     }
21161
21162   if (dokids && die->die_mark != 2)
21163     {
21164       /* We need to walk the children, but haven't done so yet.
21165          Remember that we've walked the kids.  */
21166       die->die_mark = 2;
21167
21168       /* If this is an array type, we need to make sure our
21169          kids get marked, even if they're types.  If we're
21170          breaking out types into comdat sections, do this
21171          for all type definitions.  */
21172       if (die->die_tag == DW_TAG_array_type
21173           || (use_debug_types
21174               && is_type_die (die) && ! is_declaration_die (die)))
21175         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21176       else
21177         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21178     }
21179 }
21180
21181 /* For local classes, look if any static member functions were emitted
21182    and if so, mark them.  */
21183
21184 static void
21185 prune_unused_types_walk_local_classes (dw_die_ref die)
21186 {
21187   dw_die_ref c;
21188
21189   if (die->die_mark == 2)
21190     return;
21191
21192   switch (die->die_tag)
21193     {
21194     case DW_TAG_structure_type:
21195     case DW_TAG_union_type:
21196     case DW_TAG_class_type:
21197       break;
21198
21199     case DW_TAG_subprogram:
21200       if (!get_AT_flag (die, DW_AT_declaration)
21201           || die->die_definition != NULL)
21202         prune_unused_types_mark (die, 1);
21203       return;
21204
21205     default:
21206       return;
21207     }
21208
21209   /* Mark children.  */
21210   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21211 }
21212
21213 /* Walk the tree DIE and mark types that we actually use.  */
21214
21215 static void
21216 prune_unused_types_walk (dw_die_ref die)
21217 {
21218   dw_die_ref c;
21219
21220   /* Don't do anything if this node is already marked and
21221      children have been marked as well.  */
21222   if (die->die_mark == 2)
21223     return;
21224
21225   switch (die->die_tag)
21226     {
21227     case DW_TAG_structure_type:
21228     case DW_TAG_union_type:
21229     case DW_TAG_class_type:
21230       if (die->die_perennial_p)
21231         break;
21232
21233       for (c = die->die_parent; c; c = c->die_parent)
21234         if (c->die_tag == DW_TAG_subprogram)
21235           break;
21236
21237       /* Finding used static member functions inside of classes
21238          is needed just for local classes, because for other classes
21239          static member function DIEs with DW_AT_specification
21240          are emitted outside of the DW_TAG_*_type.  If we ever change
21241          it, we'd need to call this even for non-local classes.  */
21242       if (c)
21243         prune_unused_types_walk_local_classes (die);
21244
21245       /* It's a type node --- don't mark it.  */
21246       return;
21247
21248     case DW_TAG_const_type:
21249     case DW_TAG_packed_type:
21250     case DW_TAG_pointer_type:
21251     case DW_TAG_reference_type:
21252     case DW_TAG_rvalue_reference_type:
21253     case DW_TAG_volatile_type:
21254     case DW_TAG_typedef:
21255     case DW_TAG_array_type:
21256     case DW_TAG_interface_type:
21257     case DW_TAG_friend:
21258     case DW_TAG_variant_part:
21259     case DW_TAG_enumeration_type:
21260     case DW_TAG_subroutine_type:
21261     case DW_TAG_string_type:
21262     case DW_TAG_set_type:
21263     case DW_TAG_subrange_type:
21264     case DW_TAG_ptr_to_member_type:
21265     case DW_TAG_file_type:
21266       if (die->die_perennial_p)
21267         break;
21268
21269       /* It's a type node --- don't mark it.  */
21270       return;
21271
21272     default:
21273       /* Mark everything else.  */
21274       break;
21275   }
21276
21277   if (die->die_mark == 0)
21278     {
21279       die->die_mark = 1;
21280
21281       /* Now, mark any dies referenced from here.  */
21282       prune_unused_types_walk_attribs (die);
21283     }
21284
21285   die->die_mark = 2;
21286
21287   /* Mark children.  */
21288   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21289 }
21290
21291 /* Increment the string counts on strings referred to from DIE's
21292    attributes.  */
21293
21294 static void
21295 prune_unused_types_update_strings (dw_die_ref die)
21296 {
21297   dw_attr_ref a;
21298   unsigned ix;
21299
21300   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21301     if (AT_class (a) == dw_val_class_str)
21302       {
21303         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21304         s->refcount++;
21305         /* Avoid unnecessarily putting strings that are used less than
21306            twice in the hash table.  */
21307         if (s->refcount
21308             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21309           {
21310             void ** slot;
21311             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21312                                              htab_hash_string (s->str),
21313                                              INSERT);
21314             gcc_assert (*slot == NULL);
21315             *slot = s;
21316           }
21317       }
21318 }
21319
21320 /* Remove from the tree DIE any dies that aren't marked.  */
21321
21322 static void
21323 prune_unused_types_prune (dw_die_ref die)
21324 {
21325   dw_die_ref c;
21326
21327   gcc_assert (die->die_mark);
21328   prune_unused_types_update_strings (die);
21329
21330   if (! die->die_child)
21331     return;
21332
21333   c = die->die_child;
21334   do {
21335     dw_die_ref prev = c;
21336     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21337       if (c == die->die_child)
21338         {
21339           /* No marked children between 'prev' and the end of the list.  */
21340           if (prev == c)
21341             /* No marked children at all.  */
21342             die->die_child = NULL;
21343           else
21344             {
21345               prev->die_sib = c->die_sib;
21346               die->die_child = prev;
21347             }
21348           return;
21349         }
21350
21351     if (c != prev->die_sib)
21352       prev->die_sib = c;
21353     prune_unused_types_prune (c);
21354   } while (c != die->die_child);
21355 }
21356
21357 /* Remove dies representing declarations that we never use.  */
21358
21359 static void
21360 prune_unused_types (void)
21361 {
21362   unsigned int i;
21363   limbo_die_node *node;
21364   comdat_type_node *ctnode;
21365   pubname_ref pub;
21366   dw_die_ref base_type;
21367
21368 #if ENABLE_ASSERT_CHECKING
21369   /* All the marks should already be clear.  */
21370   verify_marks_clear (comp_unit_die ());
21371   for (node = limbo_die_list; node; node = node->next)
21372     verify_marks_clear (node->die);
21373   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21374     verify_marks_clear (ctnode->root_die);
21375 #endif /* ENABLE_ASSERT_CHECKING */
21376
21377   /* Mark types that are used in global variables.  */
21378   premark_types_used_by_global_vars ();
21379
21380   /* Set the mark on nodes that are actually used.  */
21381   prune_unused_types_walk (comp_unit_die ());
21382   for (node = limbo_die_list; node; node = node->next)
21383     prune_unused_types_walk (node->die);
21384   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21385     {
21386       prune_unused_types_walk (ctnode->root_die);
21387       prune_unused_types_mark (ctnode->type_die, 1);
21388     }
21389
21390   /* Also set the mark on nodes referenced from the
21391      pubname_table.  */
21392   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21393     prune_unused_types_mark (pub->die, 1);
21394   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21395     prune_unused_types_mark (base_type, 1);
21396
21397   if (debug_str_hash)
21398     htab_empty (debug_str_hash);
21399   prune_unused_types_prune (comp_unit_die ());
21400   for (node = limbo_die_list; node; node = node->next)
21401     prune_unused_types_prune (node->die);
21402   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21403     prune_unused_types_prune (ctnode->root_die);
21404
21405   /* Leave the marks clear.  */
21406   prune_unmark_dies (comp_unit_die ());
21407   for (node = limbo_die_list; node; node = node->next)
21408     prune_unmark_dies (node->die);
21409   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21410     prune_unmark_dies (ctnode->root_die);
21411 }
21412
21413 /* Set the parameter to true if there are any relative pathnames in
21414    the file table.  */
21415 static int
21416 file_table_relative_p (void ** slot, void *param)
21417 {
21418   bool *p = (bool *) param;
21419   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21420   if (!IS_ABSOLUTE_PATH (d->filename))
21421     {
21422       *p = true;
21423       return 0;
21424     }
21425   return 1;
21426 }
21427
21428 /* Routines to manipulate hash table of comdat type units.  */
21429
21430 static hashval_t
21431 htab_ct_hash (const void *of)
21432 {
21433   hashval_t h;
21434   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21435
21436   memcpy (&h, type_node->signature, sizeof (h));
21437   return h;
21438 }
21439
21440 static int
21441 htab_ct_eq (const void *of1, const void *of2)
21442 {
21443   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21444   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21445
21446   return (! memcmp (type_node_1->signature, type_node_2->signature,
21447                     DWARF_TYPE_SIGNATURE_SIZE));
21448 }
21449
21450 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21451    to the location it would have been added, should we know its
21452    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21453    probably improve compactness of debug info, removing equivalent
21454    abbrevs, and hide any differences caused by deferring the
21455    computation of the assembler name, triggered by e.g. PCH.  */
21456
21457 static inline void
21458 move_linkage_attr (dw_die_ref die)
21459 {
21460   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21461   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21462
21463   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21464               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21465
21466   while (--ix > 0)
21467     {
21468       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21469
21470       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21471         break;
21472     }
21473
21474   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21475     {
21476       VEC_pop (dw_attr_node, die->die_attr);
21477       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21478     }
21479 }
21480
21481 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21482    referenced from typed stack ops and count how often they are used.  */
21483
21484 static void
21485 mark_base_types (dw_loc_descr_ref loc)
21486 {
21487   dw_die_ref base_type = NULL;
21488
21489   for (; loc; loc = loc->dw_loc_next)
21490     {
21491       switch (loc->dw_loc_opc)
21492         {
21493         case DW_OP_GNU_regval_type:
21494         case DW_OP_GNU_deref_type:
21495           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21496           break;
21497         case DW_OP_GNU_convert:
21498         case DW_OP_GNU_reinterpret:
21499           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21500             continue;
21501           /* FALLTHRU */
21502         case DW_OP_GNU_const_type:
21503           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21504           break;
21505         case DW_OP_GNU_entry_value:
21506           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21507           continue;
21508         default:
21509           continue;
21510         }
21511       gcc_assert (base_type->die_parent == comp_unit_die ());
21512       if (base_type->die_mark)
21513         base_type->die_mark++;
21514       else
21515         {
21516           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21517           base_type->die_mark = 1;
21518         }
21519     }
21520 }
21521
21522 /* Comparison function for sorting marked base types.  */
21523
21524 static int
21525 base_type_cmp (const void *x, const void *y)
21526 {
21527   dw_die_ref dx = *(const dw_die_ref *) x;
21528   dw_die_ref dy = *(const dw_die_ref *) y;
21529   unsigned int byte_size1, byte_size2;
21530   unsigned int encoding1, encoding2;
21531   if (dx->die_mark > dy->die_mark)
21532     return -1;
21533   if (dx->die_mark < dy->die_mark)
21534     return 1;
21535   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21536   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21537   if (byte_size1 < byte_size2)
21538     return 1;
21539   if (byte_size1 > byte_size2)
21540     return -1;
21541   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21542   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21543   if (encoding1 < encoding2)
21544     return 1;
21545   if (encoding1 > encoding2)
21546     return -1;
21547   return 0;
21548 }
21549
21550 /* Move base types marked by mark_base_types as early as possible
21551    in the CU, sorted by decreasing usage count both to make the
21552    uleb128 references as small as possible and to make sure they
21553    will have die_offset already computed by calc_die_sizes when
21554    sizes of typed stack loc ops is computed.  */
21555
21556 static void
21557 move_marked_base_types (void)
21558 {
21559   unsigned int i;
21560   dw_die_ref base_type, die, c;
21561
21562   if (VEC_empty (dw_die_ref, base_types))
21563     return;
21564
21565   /* Sort by decreasing usage count, they will be added again in that
21566      order later on.  */
21567   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21568   die = comp_unit_die ();
21569   c = die->die_child;
21570   do
21571     {
21572       dw_die_ref prev = c;
21573       c = c->die_sib;
21574       while (c->die_mark)
21575         {
21576           remove_child_with_prev (c, prev);
21577           /* As base types got marked, there must be at least
21578              one node other than DW_TAG_base_type.  */
21579           gcc_assert (c != c->die_sib);
21580           c = c->die_sib;
21581         }
21582     }
21583   while (c != die->die_child);
21584   gcc_assert (die->die_child);
21585   c = die->die_child;
21586   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21587     {
21588       base_type->die_mark = 0;
21589       base_type->die_sib = c->die_sib;
21590       c->die_sib = base_type;
21591       c = base_type;
21592     }
21593 }
21594
21595 /* Helper function for resolve_addr, attempt to resolve
21596    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21597    SYMBOL_REFs refer to variables emitted in the current CU.  */
21598
21599 static int
21600 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21601 {
21602   rtx rtl = *addr;
21603
21604   if (GET_CODE (rtl) == CONST_STRING)
21605     {
21606       size_t len = strlen (XSTR (rtl, 0)) + 1;
21607       tree t = build_string (len, XSTR (rtl, 0));
21608       tree tlen = size_int (len - 1);
21609       TREE_TYPE (t)
21610         = build_array_type (char_type_node, build_index_type (tlen));
21611       rtl = lookup_constant_def (t);
21612       if (!rtl || !MEM_P (rtl))
21613         return 1;
21614       rtl = XEXP (rtl, 0);
21615       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21616       *addr = rtl;
21617       return 0;
21618     }
21619
21620   if (GET_CODE (rtl) == SYMBOL_REF
21621       && SYMBOL_REF_DECL (rtl))
21622     {
21623       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21624         {
21625           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21626             return 1;
21627         }
21628       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21629         return 1;
21630     }
21631
21632   if (GET_CODE (rtl) == CONST
21633       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21634     return 1;
21635
21636   return 0;
21637 }
21638
21639 /* Helper function for resolve_addr, handle one location
21640    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21641    the location list couldn't be resolved.  */
21642
21643 static bool
21644 resolve_addr_in_expr (dw_loc_descr_ref loc)
21645 {
21646   dw_loc_descr_ref keep = NULL;
21647   for (; loc; loc = loc->dw_loc_next)
21648     switch (loc->dw_loc_opc)
21649       {
21650       case DW_OP_addr:
21651         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21652           return false;
21653         break;
21654       case DW_OP_const4u:
21655       case DW_OP_const8u:
21656         if (loc->dtprel
21657             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21658           return false;
21659         break;
21660       case DW_OP_plus_uconst:
21661         if (size_of_loc_descr (loc)
21662             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21663               + 1
21664             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21665           {
21666             dw_loc_descr_ref repl
21667               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21668             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21669             add_loc_descr (&repl, loc->dw_loc_next);
21670             *loc = *repl;
21671           }
21672         break;
21673       case DW_OP_implicit_value:
21674         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21675             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21676           return false;
21677         break;
21678       case DW_OP_GNU_implicit_pointer:
21679       case DW_OP_GNU_parameter_ref:
21680         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21681           {
21682             dw_die_ref ref
21683               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21684             if (ref == NULL)
21685               return false;
21686             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21687             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21688             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21689           }
21690         break;
21691       case DW_OP_GNU_const_type:
21692       case DW_OP_GNU_regval_type:
21693       case DW_OP_GNU_deref_type:
21694       case DW_OP_GNU_convert:
21695       case DW_OP_GNU_reinterpret:
21696         while (loc->dw_loc_next
21697                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21698           {
21699             dw_die_ref base1, base2;
21700             unsigned enc1, enc2, size1, size2;
21701             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21702                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21703               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21704             else if (loc->dw_loc_oprnd1.val_class
21705                      == dw_val_class_unsigned_const)
21706               break;
21707             else
21708               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21709             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21710                 == dw_val_class_unsigned_const)
21711               break;
21712             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21713             gcc_assert (base1->die_tag == DW_TAG_base_type
21714                         && base2->die_tag == DW_TAG_base_type);
21715             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21716             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21717             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21718             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21719             if (size1 == size2
21720                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21721                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21722                      && loc != keep)
21723                     || enc1 == enc2))
21724               {
21725                 /* Optimize away next DW_OP_GNU_convert after
21726                    adjusting LOC's base type die reference.  */
21727                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21728                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21729                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21730                 else
21731                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21732                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21733                 continue;
21734               }
21735             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21736                point typed stack entry.  */
21737             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21738               keep = loc->dw_loc_next;
21739             break;
21740           }
21741         break;
21742       default:
21743         break;
21744       }
21745   return true;
21746 }
21747
21748 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21749    an address in .rodata section if the string literal is emitted there,
21750    or remove the containing location list or replace DW_AT_const_value
21751    with DW_AT_location and empty location expression, if it isn't found
21752    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21753    to something that has been emitted in the current CU.  */
21754
21755 static void
21756 resolve_addr (dw_die_ref die)
21757 {
21758   dw_die_ref c;
21759   dw_attr_ref a;
21760   dw_loc_list_ref *curr, *start, loc;
21761   unsigned ix;
21762
21763   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21764     switch (AT_class (a))
21765       {
21766       case dw_val_class_loc_list:
21767         start = curr = AT_loc_list_ptr (a);
21768         loc = *curr;
21769         gcc_assert (loc);
21770         /* The same list can be referenced more than once.  See if we have
21771            already recorded the result from a previous pass.  */
21772         if (loc->replaced)
21773           *curr = loc->dw_loc_next;
21774         else if (!loc->resolved_addr)
21775           {
21776             /* As things stand, we do not expect or allow one die to
21777                reference a suffix of another die's location list chain.
21778                References must be identical or completely separate.
21779                There is therefore no need to cache the result of this
21780                pass on any list other than the first; doing so
21781                would lead to unnecessary writes.  */
21782             while (*curr)
21783               {
21784                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21785                 if (!resolve_addr_in_expr ((*curr)->expr))
21786                   {
21787                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21788                     if (next && (*curr)->ll_symbol)
21789                       {
21790                         gcc_assert (!next->ll_symbol);
21791                         next->ll_symbol = (*curr)->ll_symbol;
21792                       }
21793                     *curr = next;
21794                   }
21795                 else
21796                   {
21797                     mark_base_types ((*curr)->expr);
21798                     curr = &(*curr)->dw_loc_next;
21799                   }
21800               }
21801             if (loc == *start)
21802               loc->resolved_addr = 1;
21803             else
21804               {
21805                 loc->replaced = 1;
21806                 loc->dw_loc_next = *start;
21807               }
21808           }
21809         if (!*start)
21810           {
21811             remove_AT (die, a->dw_attr);
21812             ix--;
21813           }
21814         break;
21815       case dw_val_class_loc:
21816         {
21817           dw_loc_descr_ref l = AT_loc (a);
21818           /* For -gdwarf-2 don't attempt to optimize
21819              DW_AT_data_member_location containing
21820              DW_OP_plus_uconst - older consumers might
21821              rely on it being that op instead of a more complex,
21822              but shorter, location description.  */
21823           if ((dwarf_version > 2
21824                || a->dw_attr != DW_AT_data_member_location
21825                || l == NULL
21826                || l->dw_loc_opc != DW_OP_plus_uconst
21827                || l->dw_loc_next != NULL)
21828               && !resolve_addr_in_expr (l))
21829             {
21830               remove_AT (die, a->dw_attr);
21831               ix--;
21832             }
21833           else
21834             mark_base_types (l);
21835         }
21836         break;
21837       case dw_val_class_addr:
21838         if (a->dw_attr == DW_AT_const_value
21839             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21840           {
21841             remove_AT (die, a->dw_attr);
21842             ix--;
21843           }
21844         if (die->die_tag == DW_TAG_GNU_call_site
21845             && a->dw_attr == DW_AT_abstract_origin)
21846           {
21847             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21848             dw_die_ref tdie = lookup_decl_die (tdecl);
21849             if (tdie == NULL
21850                 && DECL_EXTERNAL (tdecl)
21851                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21852               {
21853                 force_decl_die (tdecl);
21854                 tdie = lookup_decl_die (tdecl);
21855               }
21856             if (tdie)
21857               {
21858                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21859                 a->dw_attr_val.v.val_die_ref.die = tdie;
21860                 a->dw_attr_val.v.val_die_ref.external = 0;
21861               }
21862             else
21863               {
21864                 remove_AT (die, a->dw_attr);
21865                 ix--;
21866               }
21867           }
21868         break;
21869       default:
21870         break;
21871       }
21872
21873   FOR_EACH_CHILD (die, c, resolve_addr (c));
21874 }
21875 \f
21876 /* Helper routines for optimize_location_lists.
21877    This pass tries to share identical local lists in .debug_loc
21878    section.  */
21879
21880 /* Iteratively hash operands of LOC opcode.  */
21881
21882 static inline hashval_t
21883 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21884 {
21885   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21886   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21887
21888   switch (loc->dw_loc_opc)
21889     {
21890     case DW_OP_const4u:
21891     case DW_OP_const8u:
21892       if (loc->dtprel)
21893         goto hash_addr;
21894       /* FALLTHRU */
21895     case DW_OP_const1u:
21896     case DW_OP_const1s:
21897     case DW_OP_const2u:
21898     case DW_OP_const2s:
21899     case DW_OP_const4s:
21900     case DW_OP_const8s:
21901     case DW_OP_constu:
21902     case DW_OP_consts:
21903     case DW_OP_pick:
21904     case DW_OP_plus_uconst:
21905     case DW_OP_breg0:
21906     case DW_OP_breg1:
21907     case DW_OP_breg2:
21908     case DW_OP_breg3:
21909     case DW_OP_breg4:
21910     case DW_OP_breg5:
21911     case DW_OP_breg6:
21912     case DW_OP_breg7:
21913     case DW_OP_breg8:
21914     case DW_OP_breg9:
21915     case DW_OP_breg10:
21916     case DW_OP_breg11:
21917     case DW_OP_breg12:
21918     case DW_OP_breg13:
21919     case DW_OP_breg14:
21920     case DW_OP_breg15:
21921     case DW_OP_breg16:
21922     case DW_OP_breg17:
21923     case DW_OP_breg18:
21924     case DW_OP_breg19:
21925     case DW_OP_breg20:
21926     case DW_OP_breg21:
21927     case DW_OP_breg22:
21928     case DW_OP_breg23:
21929     case DW_OP_breg24:
21930     case DW_OP_breg25:
21931     case DW_OP_breg26:
21932     case DW_OP_breg27:
21933     case DW_OP_breg28:
21934     case DW_OP_breg29:
21935     case DW_OP_breg30:
21936     case DW_OP_breg31:
21937     case DW_OP_regx:
21938     case DW_OP_fbreg:
21939     case DW_OP_piece:
21940     case DW_OP_deref_size:
21941     case DW_OP_xderef_size:
21942       hash = iterative_hash_object (val1->v.val_int, hash);
21943       break;
21944     case DW_OP_skip:
21945     case DW_OP_bra:
21946       {
21947         int offset;
21948
21949         gcc_assert (val1->val_class == dw_val_class_loc);
21950         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21951         hash = iterative_hash_object (offset, hash);
21952       }
21953       break;
21954     case DW_OP_implicit_value:
21955       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21956       switch (val2->val_class)
21957         {
21958         case dw_val_class_const:
21959           hash = iterative_hash_object (val2->v.val_int, hash);
21960           break;
21961         case dw_val_class_vec:
21962           {
21963             unsigned int elt_size = val2->v.val_vec.elt_size;
21964             unsigned int len = val2->v.val_vec.length;
21965
21966             hash = iterative_hash_object (elt_size, hash);
21967             hash = iterative_hash_object (len, hash);
21968             hash = iterative_hash (val2->v.val_vec.array,
21969                                    len * elt_size, hash);
21970           }
21971           break;
21972         case dw_val_class_const_double:
21973           hash = iterative_hash_object (val2->v.val_double.low, hash);
21974           hash = iterative_hash_object (val2->v.val_double.high, hash);
21975           break;
21976         case dw_val_class_addr:
21977           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21978           break;
21979         default:
21980           gcc_unreachable ();
21981         }
21982       break;
21983     case DW_OP_bregx:
21984     case DW_OP_bit_piece:
21985       hash = iterative_hash_object (val1->v.val_int, hash);
21986       hash = iterative_hash_object (val2->v.val_int, hash);
21987       break;
21988     case DW_OP_addr:
21989     hash_addr:
21990       if (loc->dtprel)
21991         {
21992           unsigned char dtprel = 0xd1;
21993           hash = iterative_hash_object (dtprel, hash);
21994         }
21995       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21996       break;
21997     case DW_OP_GNU_implicit_pointer:
21998       hash = iterative_hash_object (val2->v.val_int, hash);
21999       break;
22000     case DW_OP_GNU_entry_value:
22001       hash = hash_loc_operands (val1->v.val_loc, hash);
22002       break;
22003     case DW_OP_GNU_regval_type:
22004     case DW_OP_GNU_deref_type:
22005       {
22006         unsigned int byte_size
22007           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22008         unsigned int encoding
22009           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22010         hash = iterative_hash_object (val1->v.val_int, hash);
22011         hash = iterative_hash_object (byte_size, hash);
22012         hash = iterative_hash_object (encoding, hash);
22013       }
22014       break;
22015     case DW_OP_GNU_convert:
22016     case DW_OP_GNU_reinterpret:
22017       if (val1->val_class == dw_val_class_unsigned_const)
22018         {
22019           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22020           break;
22021         }
22022       /* FALLTHRU */
22023     case DW_OP_GNU_const_type:
22024       {
22025         unsigned int byte_size
22026           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22027         unsigned int encoding
22028           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22029         hash = iterative_hash_object (byte_size, hash);
22030         hash = iterative_hash_object (encoding, hash);
22031         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22032           break;
22033         hash = iterative_hash_object (val2->val_class, hash);
22034         switch (val2->val_class)
22035           {
22036           case dw_val_class_const:
22037             hash = iterative_hash_object (val2->v.val_int, hash);
22038             break;
22039           case dw_val_class_vec:
22040             {
22041               unsigned int elt_size = val2->v.val_vec.elt_size;
22042               unsigned int len = val2->v.val_vec.length;
22043
22044               hash = iterative_hash_object (elt_size, hash);
22045               hash = iterative_hash_object (len, hash);
22046               hash = iterative_hash (val2->v.val_vec.array,
22047                                      len * elt_size, hash);
22048             }
22049             break;
22050           case dw_val_class_const_double:
22051             hash = iterative_hash_object (val2->v.val_double.low, hash);
22052             hash = iterative_hash_object (val2->v.val_double.high, hash);
22053             break;
22054           default:
22055             gcc_unreachable ();
22056           }
22057       }
22058       break;
22059
22060     default:
22061       /* Other codes have no operands.  */
22062       break;
22063     }
22064   return hash;
22065 }
22066
22067 /* Iteratively hash the whole DWARF location expression LOC.  */
22068
22069 static inline hashval_t
22070 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22071 {
22072   dw_loc_descr_ref l;
22073   bool sizes_computed = false;
22074   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22075   size_of_locs (loc);
22076
22077   for (l = loc; l != NULL; l = l->dw_loc_next)
22078     {
22079       enum dwarf_location_atom opc = l->dw_loc_opc;
22080       hash = iterative_hash_object (opc, hash);
22081       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22082         {
22083           size_of_locs (loc);
22084           sizes_computed = true;
22085         }
22086       hash = hash_loc_operands (l, hash);
22087     }
22088   return hash;
22089 }
22090
22091 /* Compute hash of the whole location list LIST_HEAD.  */
22092
22093 static inline void
22094 hash_loc_list (dw_loc_list_ref list_head)
22095 {
22096   dw_loc_list_ref curr = list_head;
22097   hashval_t hash = 0;
22098
22099   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22100     {
22101       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22102       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22103       if (curr->section)
22104         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22105                                hash);
22106       hash = hash_locs (curr->expr, hash);
22107     }
22108   list_head->hash = hash;
22109 }
22110
22111 /* Return true if X and Y opcodes have the same operands.  */
22112
22113 static inline bool
22114 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22115 {
22116   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22117   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22118   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22119   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22120
22121   switch (x->dw_loc_opc)
22122     {
22123     case DW_OP_const4u:
22124     case DW_OP_const8u:
22125       if (x->dtprel)
22126         goto hash_addr;
22127       /* FALLTHRU */
22128     case DW_OP_const1u:
22129     case DW_OP_const1s:
22130     case DW_OP_const2u:
22131     case DW_OP_const2s:
22132     case DW_OP_const4s:
22133     case DW_OP_const8s:
22134     case DW_OP_constu:
22135     case DW_OP_consts:
22136     case DW_OP_pick:
22137     case DW_OP_plus_uconst:
22138     case DW_OP_breg0:
22139     case DW_OP_breg1:
22140     case DW_OP_breg2:
22141     case DW_OP_breg3:
22142     case DW_OP_breg4:
22143     case DW_OP_breg5:
22144     case DW_OP_breg6:
22145     case DW_OP_breg7:
22146     case DW_OP_breg8:
22147     case DW_OP_breg9:
22148     case DW_OP_breg10:
22149     case DW_OP_breg11:
22150     case DW_OP_breg12:
22151     case DW_OP_breg13:
22152     case DW_OP_breg14:
22153     case DW_OP_breg15:
22154     case DW_OP_breg16:
22155     case DW_OP_breg17:
22156     case DW_OP_breg18:
22157     case DW_OP_breg19:
22158     case DW_OP_breg20:
22159     case DW_OP_breg21:
22160     case DW_OP_breg22:
22161     case DW_OP_breg23:
22162     case DW_OP_breg24:
22163     case DW_OP_breg25:
22164     case DW_OP_breg26:
22165     case DW_OP_breg27:
22166     case DW_OP_breg28:
22167     case DW_OP_breg29:
22168     case DW_OP_breg30:
22169     case DW_OP_breg31:
22170     case DW_OP_regx:
22171     case DW_OP_fbreg:
22172     case DW_OP_piece:
22173     case DW_OP_deref_size:
22174     case DW_OP_xderef_size:
22175       return valx1->v.val_int == valy1->v.val_int;
22176     case DW_OP_skip:
22177     case DW_OP_bra:
22178       gcc_assert (valx1->val_class == dw_val_class_loc
22179                   && valy1->val_class == dw_val_class_loc
22180                   && x->dw_loc_addr == y->dw_loc_addr);
22181       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22182     case DW_OP_implicit_value:
22183       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22184           || valx2->val_class != valy2->val_class)
22185         return false;
22186       switch (valx2->val_class)
22187         {
22188         case dw_val_class_const:
22189           return valx2->v.val_int == valy2->v.val_int;
22190         case dw_val_class_vec:
22191           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22192                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22193                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22194                             valx2->v.val_vec.elt_size
22195                             * valx2->v.val_vec.length) == 0;
22196         case dw_val_class_const_double:
22197           return valx2->v.val_double.low == valy2->v.val_double.low
22198                  && valx2->v.val_double.high == valy2->v.val_double.high;
22199         case dw_val_class_addr:
22200           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22201         default:
22202           gcc_unreachable ();
22203         }
22204     case DW_OP_bregx:
22205     case DW_OP_bit_piece:
22206       return valx1->v.val_int == valy1->v.val_int
22207              && valx2->v.val_int == valy2->v.val_int;
22208     case DW_OP_addr:
22209     hash_addr:
22210       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22211     case DW_OP_GNU_implicit_pointer:
22212       return valx1->val_class == dw_val_class_die_ref
22213              && valx1->val_class == valy1->val_class
22214              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22215              && valx2->v.val_int == valy2->v.val_int;
22216     case DW_OP_GNU_entry_value:
22217       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22218     case DW_OP_GNU_const_type:
22219       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22220           || valx2->val_class != valy2->val_class)
22221         return false;
22222       switch (valx2->val_class)
22223         {
22224         case dw_val_class_const:
22225           return valx2->v.val_int == valy2->v.val_int;
22226         case dw_val_class_vec:
22227           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22228                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22229                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22230                             valx2->v.val_vec.elt_size
22231                             * valx2->v.val_vec.length) == 0;
22232         case dw_val_class_const_double:
22233           return valx2->v.val_double.low == valy2->v.val_double.low
22234                  && valx2->v.val_double.high == valy2->v.val_double.high;
22235         default:
22236           gcc_unreachable ();
22237         }
22238     case DW_OP_GNU_regval_type:
22239     case DW_OP_GNU_deref_type:
22240       return valx1->v.val_int == valy1->v.val_int
22241              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22242     case DW_OP_GNU_convert:
22243     case DW_OP_GNU_reinterpret:
22244       if (valx1->val_class != valy1->val_class)
22245         return false;
22246       if (valx1->val_class == dw_val_class_unsigned_const)
22247         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22248       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22249     case DW_OP_GNU_parameter_ref:
22250       return valx1->val_class == dw_val_class_die_ref
22251              && valx1->val_class == valy1->val_class
22252              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22253     default:
22254       /* Other codes have no operands.  */
22255       return true;
22256     }
22257 }
22258
22259 /* Return true if DWARF location expressions X and Y are the same.  */
22260
22261 static inline bool
22262 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22263 {
22264   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22265     if (x->dw_loc_opc != y->dw_loc_opc
22266         || x->dtprel != y->dtprel
22267         || !compare_loc_operands (x, y))
22268       break;
22269   return x == NULL && y == NULL;
22270 }
22271
22272 /* Return precomputed hash of location list X.  */
22273
22274 static hashval_t
22275 loc_list_hash (const void *x)
22276 {
22277   return ((const struct dw_loc_list_struct *) x)->hash;
22278 }
22279
22280 /* Return 1 if location lists X and Y are the same.  */
22281
22282 static int
22283 loc_list_eq (const void *x, const void *y)
22284 {
22285   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22286   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22287   if (a == b)
22288     return 1;
22289   if (a->hash != b->hash)
22290     return 0;
22291   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22292     if (strcmp (a->begin, b->begin) != 0
22293         || strcmp (a->end, b->end) != 0
22294         || (a->section == NULL) != (b->section == NULL)
22295         || (a->section && strcmp (a->section, b->section) != 0)
22296         || !compare_locs (a->expr, b->expr))
22297       break;
22298   return a == NULL && b == NULL;
22299 }
22300
22301 /* Recursively optimize location lists referenced from DIE
22302    children and share them whenever possible.  */
22303
22304 static void
22305 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22306 {
22307   dw_die_ref c;
22308   dw_attr_ref a;
22309   unsigned ix;
22310   void **slot;
22311
22312   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22313     if (AT_class (a) == dw_val_class_loc_list)
22314       {
22315         dw_loc_list_ref list = AT_loc_list (a);
22316         /* TODO: perform some optimizations here, before hashing
22317            it and storing into the hash table.  */
22318         hash_loc_list (list);
22319         slot = htab_find_slot_with_hash (htab, list, list->hash,
22320                                          INSERT);
22321         if (*slot == NULL)
22322           *slot = (void *) list;
22323         else
22324           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22325       }
22326
22327   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22328 }
22329
22330 /* Optimize location lists referenced from DIE
22331    children and share them whenever possible.  */
22332
22333 static void
22334 optimize_location_lists (dw_die_ref die)
22335 {
22336   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22337   optimize_location_lists_1 (die, htab);
22338   htab_delete (htab);
22339 }
22340 \f
22341 /* Output stuff that dwarf requires at the end of every file,
22342    and generate the DWARF-2 debugging info.  */
22343
22344 static void
22345 dwarf2out_finish (const char *filename)
22346 {
22347   limbo_die_node *node, *next_node;
22348   comdat_type_node *ctnode;
22349   htab_t comdat_type_table;
22350   unsigned int i;
22351
22352   /* PCH might result in DW_AT_producer string being restored from the
22353      header compilation, fix it up if needed.  */
22354   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22355   if (strcmp (AT_string (producer), producer_string) != 0)
22356     {
22357       struct indirect_string_node *node = find_AT_string (producer_string);
22358       producer->dw_attr_val.v.val_str = node;
22359     }
22360
22361   gen_scheduled_generic_parms_dies ();
22362   gen_remaining_tmpl_value_param_die_attribute ();
22363
22364   /* Add the name for the main input file now.  We delayed this from
22365      dwarf2out_init to avoid complications with PCH.  */
22366   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22367   if (!IS_ABSOLUTE_PATH (filename))
22368     add_comp_dir_attribute (comp_unit_die ());
22369   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22370     {
22371       bool p = false;
22372       htab_traverse (file_table, file_table_relative_p, &p);
22373       if (p)
22374         add_comp_dir_attribute (comp_unit_die ());
22375     }
22376
22377   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22378     {
22379       add_location_or_const_value_attribute (
22380         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22381         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22382         false,
22383         DW_AT_location);
22384     }
22385
22386   /* Traverse the limbo die list, and add parent/child links.  The only
22387      dies without parents that should be here are concrete instances of
22388      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22389      For concrete instances, we can get the parent die from the abstract
22390      instance.  */
22391   for (node = limbo_die_list; node; node = next_node)
22392     {
22393       dw_die_ref die = node->die;
22394       next_node = node->next;
22395
22396       if (die->die_parent == NULL)
22397         {
22398           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22399
22400           if (origin && origin->die_parent)
22401             add_child_die (origin->die_parent, die);
22402           else if (is_cu_die (die))
22403             ;
22404           else if (seen_error ())
22405             /* It's OK to be confused by errors in the input.  */
22406             add_child_die (comp_unit_die (), die);
22407           else
22408             {
22409               /* In certain situations, the lexical block containing a
22410                  nested function can be optimized away, which results
22411                  in the nested function die being orphaned.  Likewise
22412                  with the return type of that nested function.  Force
22413                  this to be a child of the containing function.
22414
22415                  It may happen that even the containing function got fully
22416                  inlined and optimized out.  In that case we are lost and
22417                  assign the empty child.  This should not be big issue as
22418                  the function is likely unreachable too.  */
22419               tree context = NULL_TREE;
22420
22421               gcc_assert (node->created_for);
22422
22423               if (DECL_P (node->created_for))
22424                 context = DECL_CONTEXT (node->created_for);
22425               else if (TYPE_P (node->created_for))
22426                 context = TYPE_CONTEXT (node->created_for);
22427
22428               gcc_assert (context
22429                           && (TREE_CODE (context) == FUNCTION_DECL
22430                               || TREE_CODE (context) == NAMESPACE_DECL));
22431
22432               origin = lookup_decl_die (context);
22433               if (origin)
22434                 add_child_die (origin, die);
22435               else
22436                 add_child_die (comp_unit_die (), die);
22437             }
22438         }
22439     }
22440
22441   limbo_die_list = NULL;
22442
22443 #if ENABLE_ASSERT_CHECKING
22444   {
22445     dw_die_ref die = comp_unit_die (), c;
22446     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22447   }
22448 #endif
22449   resolve_addr (comp_unit_die ());
22450   move_marked_base_types ();
22451
22452   for (node = deferred_asm_name; node; node = node->next)
22453     {
22454       tree decl = node->created_for;
22455       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22456         {
22457           add_linkage_attr (node->die, decl);
22458           move_linkage_attr (node->die);
22459         }
22460     }
22461
22462   deferred_asm_name = NULL;
22463
22464   /* Walk through the list of incomplete types again, trying once more to
22465      emit full debugging info for them.  */
22466   retry_incomplete_types ();
22467
22468   if (flag_eliminate_unused_debug_types)
22469     prune_unused_types ();
22470
22471   /* Generate separate CUs for each of the include files we've seen.
22472      They will go into limbo_die_list.  */
22473   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22474     break_out_includes (comp_unit_die ());
22475
22476   /* Generate separate COMDAT sections for type DIEs. */
22477   if (use_debug_types)
22478     {
22479       break_out_comdat_types (comp_unit_die ());
22480
22481       /* Each new type_unit DIE was added to the limbo die list when created.
22482          Since these have all been added to comdat_type_list, clear the
22483          limbo die list.  */
22484       limbo_die_list = NULL;
22485
22486       /* For each new comdat type unit, copy declarations for incomplete
22487          types to make the new unit self-contained (i.e., no direct
22488          references to the main compile unit).  */
22489       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22490         copy_decls_for_unworthy_types (ctnode->root_die);
22491       copy_decls_for_unworthy_types (comp_unit_die ());
22492
22493       /* In the process of copying declarations from one unit to another,
22494          we may have left some declarations behind that are no longer
22495          referenced.  Prune them.  */
22496       prune_unused_types ();
22497     }
22498
22499   /* Traverse the DIE's and add add sibling attributes to those DIE's
22500      that have children.  */
22501   add_sibling_attributes (comp_unit_die ());
22502   for (node = limbo_die_list; node; node = node->next)
22503     add_sibling_attributes (node->die);
22504   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22505     add_sibling_attributes (ctnode->root_die);
22506
22507   /* Output a terminator label for the .text section.  */
22508   switch_to_section (text_section);
22509   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22510   if (cold_text_section)
22511     {
22512       switch_to_section (cold_text_section);
22513       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22514     }
22515
22516   /* We can only use the low/high_pc attributes if all of the code was
22517      in .text.  */
22518   if (!have_multiple_function_sections 
22519       || (dwarf_version < 3 && dwarf_strict))
22520     {
22521       /* Don't add if the CU has no associated code.  */
22522       if (text_section_used)
22523         {
22524           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22525           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22526         }
22527     }
22528   else
22529     {
22530       unsigned fde_idx;
22531       dw_fde_ref fde;
22532       bool range_list_added = false;
22533
22534       if (text_section_used)
22535         add_ranges_by_labels (comp_unit_die (), text_section_label,
22536                               text_end_label, &range_list_added);
22537       if (cold_text_section_used)
22538         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22539                               cold_end_label, &range_list_added);
22540
22541       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22542         {
22543           if (!fde->in_std_section)
22544             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22545                                   fde->dw_fde_end, &range_list_added);
22546           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22547             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22548                                   fde->dw_fde_second_end, &range_list_added);
22549         }
22550
22551       if (range_list_added)
22552         {
22553           /* We need to give .debug_loc and .debug_ranges an appropriate
22554              "base address".  Use zero so that these addresses become
22555              absolute.  Historically, we've emitted the unexpected
22556              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22557              Emit both to give time for other tools to adapt.  */
22558           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22559           if (! dwarf_strict && dwarf_version < 4)
22560             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22561
22562           add_ranges (NULL);
22563         }
22564     }
22565
22566   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22567     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22568                     debug_line_section_label);
22569
22570   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22571     add_AT_macptr (comp_unit_die (),
22572                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22573                    macinfo_section_label);
22574
22575   if (have_location_lists)
22576     optimize_location_lists (comp_unit_die ());
22577
22578   /* Output all of the compilation units.  We put the main one last so that
22579      the offsets are available to output_pubnames.  */
22580   for (node = limbo_die_list; node; node = node->next)
22581     output_comp_unit (node->die, 0);
22582
22583   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22584   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22585     {
22586       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22587
22588       /* Don't output duplicate types.  */
22589       if (*slot != HTAB_EMPTY_ENTRY)
22590         continue;
22591
22592       /* Add a pointer to the line table for the main compilation unit
22593          so that the debugger can make sense of DW_AT_decl_file
22594          attributes.  */
22595       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22596         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22597                         debug_line_section_label);
22598
22599       output_comdat_type_unit (ctnode);
22600       *slot = ctnode;
22601     }
22602   htab_delete (comdat_type_table);
22603
22604   /* Output the main compilation unit if non-empty or if .debug_macinfo
22605      will be emitted.  */
22606   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22607
22608   /* Output the abbreviation table.  */
22609   if (abbrev_die_table_in_use != 1)
22610     {
22611       switch_to_section (debug_abbrev_section);
22612       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22613       output_abbrev_section ();
22614     }
22615
22616   /* Output location list section if necessary.  */
22617   if (have_location_lists)
22618     {
22619       /* Output the location lists info.  */
22620       switch_to_section (debug_loc_section);
22621       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22622                                    DEBUG_LOC_SECTION_LABEL, 0);
22623       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22624       output_location_lists (comp_unit_die ());
22625     }
22626
22627   /* Output public names table if necessary.  */
22628   if (!VEC_empty (pubname_entry, pubname_table))
22629     {
22630       gcc_assert (info_section_emitted);
22631       switch_to_section (debug_pubnames_section);
22632       output_pubnames (pubname_table);
22633     }
22634
22635   /* Output public types table if necessary.  */
22636   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22637      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22638      simply won't look for the section.  */
22639   if (!VEC_empty (pubname_entry, pubtype_table))
22640     {
22641       bool empty = false;
22642       
22643       if (flag_eliminate_unused_debug_types)
22644         {
22645           /* The pubtypes table might be emptied by pruning unused items.  */
22646           unsigned i;
22647           pubname_ref p;
22648           empty = true;
22649           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22650             if (p->die->die_offset != 0)
22651               {
22652                 empty = false;
22653                 break;
22654               }
22655         }
22656       if (!empty)
22657         {
22658           gcc_assert (info_section_emitted);
22659           switch_to_section (debug_pubtypes_section);
22660           output_pubnames (pubtype_table);
22661         }
22662     }
22663
22664   /* Output the address range information if a CU (.debug_info section)
22665      was emitted.  We output an empty table even if we had no functions
22666      to put in it.  This because the consumer has no way to tell the
22667      difference between an empty table that we omitted and failure to
22668      generate a table that would have contained data.  */
22669   if (info_section_emitted)
22670     {
22671       unsigned long aranges_length = size_of_aranges ();
22672
22673       switch_to_section (debug_aranges_section);
22674       output_aranges (aranges_length);
22675     }
22676
22677   /* Output ranges section if necessary.  */
22678   if (ranges_table_in_use)
22679     {
22680       switch_to_section (debug_ranges_section);
22681       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22682       output_ranges ();
22683     }
22684
22685   /* Have to end the macro section.  */
22686   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22687     {
22688       switch_to_section (debug_macinfo_section);
22689       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22690       if (!VEC_empty (macinfo_entry, macinfo_table))
22691         output_macinfo ();
22692       dw2_asm_output_data (1, 0, "End compilation unit");
22693     }
22694
22695   /* Output the source line correspondence table.  We must do this
22696      even if there is no line information.  Otherwise, on an empty
22697      translation unit, we will generate a present, but empty,
22698      .debug_info section.  IRIX 6.5 `nm' will then complain when
22699      examining the file.  This is done late so that any filenames
22700      used by the debug_info section are marked as 'used'.  */
22701   switch_to_section (debug_line_section);
22702   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22703   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22704     output_line_info ();
22705
22706   /* If we emitted any DW_FORM_strp form attribute, output the string
22707      table too.  */
22708   if (debug_str_hash)
22709     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22710 }
22711
22712 #include "gt-dwarf2out.h"