OSDN Git Service

2012-01-08 Jonathan Wakely <jwakely.gcc@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97 #include "opts.h"
98
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_insn;
101 static rtx cached_next_real_insn;
102
103 #ifdef VMS_DEBUGGING_INFO
104 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105
106 /* Define this macro to be a nonzero value if the directory specifications
107     which are output in the debug info should end with a separator.  */
108 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
109 /* Define this macro to evaluate to a nonzero value if GCC should refrain
110    from generating indirect strings in DWARF2 debug information, for instance
111    if your target is stuck with an old version of GDB that is unable to
112    process them properly or uses VMS Debug.  */
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
114 #else
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
116 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
117 #endif
118
119 /* ??? Poison these here until it can be done generically.  They've been
120    totally replaced in this file; make sure it stays that way.  */
121 #undef DWARF2_UNWIND_INFO
122 #undef DWARF2_FRAME_INFO
123 #if (GCC_VERSION >= 3000)
124  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
125 #endif
126
127 /* The size of the target's pointer type.  */
128 #ifndef PTR_SIZE
129 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
130 #endif
131
132 /* Array of RTXes referenced by the debugging information, which therefore
133    must be kept around forever.  */
134 static GTY(()) VEC(rtx,gc) *used_rtx_array;
135
136 /* A pointer to the base of a list of incomplete types which might be
137    completed at some later time.  incomplete_types_list needs to be a
138    VEC(tree,gc) because we want to tell the garbage collector about
139    it.  */
140 static GTY(()) VEC(tree,gc) *incomplete_types;
141
142 /* A pointer to the base of a table of references to declaration
143    scopes.  This table is a display which tracks the nesting
144    of declaration scopes at the current scope and containing
145    scopes.  This table is used to find the proper place to
146    define type declaration DIE's.  */
147 static GTY(()) VEC(tree,gc) *decl_scope_table;
148
149 /* Pointers to various DWARF2 sections.  */
150 static GTY(()) section *debug_info_section;
151 static GTY(()) section *debug_abbrev_section;
152 static GTY(()) section *debug_aranges_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_loc_section;
156 static GTY(()) section *debug_pubnames_section;
157 static GTY(()) section *debug_pubtypes_section;
158 static GTY(()) section *debug_str_section;
159 static GTY(()) section *debug_ranges_section;
160 static GTY(()) section *debug_frame_section;
161
162 /* Maximum size (in bytes) of an artificially generated label.  */
163 #define MAX_ARTIFICIAL_LABEL_BYTES      30
164
165 /* According to the (draft) DWARF 3 specification, the initial length
166    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
167    bytes are 0xffffffff, followed by the length stored in the next 8
168    bytes.
169
170    However, the SGI/MIPS ABI uses an initial length which is equal to
171    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
172
173 #ifndef DWARF_INITIAL_LENGTH_SIZE
174 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
175 #endif
176
177 /* Round SIZE up to the nearest BOUNDARY.  */
178 #define DWARF_ROUND(SIZE,BOUNDARY) \
179   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
180
181 /* CIE identifier.  */
182 #if HOST_BITS_PER_WIDE_INT >= 64
183 #define DWARF_CIE_ID \
184   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
185 #else
186 #define DWARF_CIE_ID DW_CIE_ID
187 #endif
188
189 DEF_VEC_P (dw_fde_ref);
190 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
191
192 /* A vector for a table that contains frame description
193    information for each routine.  */
194 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
195
196 struct GTY(()) indirect_string_node {
197   const char *str;
198   unsigned int refcount;
199   enum dwarf_form form;
200   char *label;
201 };
202
203 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
204
205 static GTY(()) int dw2_string_counter;
206
207 /* True if the compilation unit places functions in more than one section.  */
208 static GTY(()) bool have_multiple_function_sections = false;
209
210 /* Whether the default text and cold text sections have been used at all.  */
211
212 static GTY(()) bool text_section_used = false;
213 static GTY(()) bool cold_text_section_used = false;
214
215 /* The default cold text section.  */
216 static GTY(()) section *cold_text_section;
217
218 /* Forward declarations for functions defined in this file.  */
219
220 static char *stripattributes (const char *);
221 static void output_call_frame_info (int);
222 static void dwarf2out_note_section_used (void);
223
224 /* Personality decl of current unit.  Used only when assembler does not support
225    personality CFI.  */
226 static GTY(()) rtx current_unit_personality;
227
228 /* Data and reference forms for relocatable data.  */
229 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
230 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
231
232 #ifndef DEBUG_FRAME_SECTION
233 #define DEBUG_FRAME_SECTION     ".debug_frame"
234 #endif
235
236 #ifndef FUNC_BEGIN_LABEL
237 #define FUNC_BEGIN_LABEL        "LFB"
238 #endif
239
240 #ifndef FUNC_END_LABEL
241 #define FUNC_END_LABEL          "LFE"
242 #endif
243
244 #ifndef PROLOGUE_END_LABEL
245 #define PROLOGUE_END_LABEL      "LPE"
246 #endif
247
248 #ifndef EPILOGUE_BEGIN_LABEL
249 #define EPILOGUE_BEGIN_LABEL    "LEB"
250 #endif
251
252 #ifndef FRAME_BEGIN_LABEL
253 #define FRAME_BEGIN_LABEL       "Lframe"
254 #endif
255 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
256 #define CIE_END_LABEL           "LECIE"
257 #define FDE_LABEL               "LSFDE"
258 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
259 #define FDE_END_LABEL           "LEFDE"
260 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
261 #define LINE_NUMBER_END_LABEL   "LELT"
262 #define LN_PROLOG_AS_LABEL      "LASLTP"
263 #define LN_PROLOG_END_LABEL     "LELTP"
264 #define DIE_LABEL_PREFIX        "DW"
265 \f
266 /* Match the base name of a file to the base name of a compilation unit. */
267
268 static int
269 matches_main_base (const char *path)
270 {
271   /* Cache the last query. */
272   static const char *last_path = NULL;
273   static int last_match = 0;
274   if (path != last_path)
275     {
276       const char *base;
277       int length = base_of_path (path, &base);
278       last_path = path;
279       last_match = (length == main_input_baselength
280                     && memcmp (base, main_input_basename, length) == 0);
281     }
282   return last_match;
283 }
284
285 #ifdef DEBUG_DEBUG_STRUCT
286
287 static int
288 dump_struct_debug (tree type, enum debug_info_usage usage,
289                    enum debug_struct_file criterion, int generic,
290                    int matches, int result)
291 {
292   /* Find the type name. */
293   tree type_decl = TYPE_STUB_DECL (type);
294   tree t = type_decl;
295   const char *name = 0;
296   if (TREE_CODE (t) == TYPE_DECL)
297     t = DECL_NAME (t);
298   if (t)
299     name = IDENTIFIER_POINTER (t);
300
301   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
302            criterion,
303            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
304            matches ? "bas" : "hdr",
305            generic ? "gen" : "ord",
306            usage == DINFO_USAGE_DFN ? ";" :
307              usage == DINFO_USAGE_DIR_USE ? "." : "*",
308            result,
309            (void*) type_decl, name);
310   return result;
311 }
312 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
313   dump_struct_debug (type, usage, criterion, generic, matches, result)
314
315 #else
316
317 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
318   (result)
319
320 #endif
321
322 static bool
323 should_emit_struct_debug (tree type, enum debug_info_usage usage)
324 {
325   enum debug_struct_file criterion;
326   tree type_decl;
327   bool generic = lang_hooks.types.generic_p (type);
328
329   if (generic)
330     criterion = debug_struct_generic[usage];
331   else
332     criterion = debug_struct_ordinary[usage];
333
334   if (criterion == DINFO_STRUCT_FILE_NONE)
335     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
336   if (criterion == DINFO_STRUCT_FILE_ANY)
337     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
338
339   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
340
341   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
342     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343
344   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
345     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
346   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
347 }
348 \f
349 /* Return a pointer to a copy of the section string name S with all
350    attributes stripped off, and an asterisk prepended (for assemble_name).  */
351
352 static inline char *
353 stripattributes (const char *s)
354 {
355   char *stripped = XNEWVEC (char, strlen (s) + 2);
356   char *p = stripped;
357
358   *p++ = '*';
359
360   while (*s && *s != ',')
361     *p++ = *s++;
362
363   *p = '\0';
364   return stripped;
365 }
366
367 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
368    switch to the data section instead, and write out a synthetic start label
369    for collect2 the first time around.  */
370
371 static void
372 switch_to_eh_frame_section (bool back)
373 {
374   tree label;
375
376 #ifdef EH_FRAME_SECTION_NAME
377   if (eh_frame_section == 0)
378     {
379       int flags;
380
381       if (EH_TABLES_CAN_BE_READ_ONLY)
382         {
383           int fde_encoding;
384           int per_encoding;
385           int lsda_encoding;
386
387           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
388                                                        /*global=*/0);
389           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
390                                                        /*global=*/1);
391           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
392                                                         /*global=*/0);
393           flags = ((! flag_pic
394                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
395                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
396                         && (per_encoding & 0x70) != DW_EH_PE_absptr
397                         && (per_encoding & 0x70) != DW_EH_PE_aligned
398                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
399                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
400                    ? 0 : SECTION_WRITE);
401         }
402       else
403         flags = SECTION_WRITE;
404       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
405     }
406 #endif /* EH_FRAME_SECTION_NAME */
407
408   if (eh_frame_section)
409     switch_to_section (eh_frame_section);
410   else
411     {
412       /* We have no special eh_frame section.  Put the information in
413          the data section and emit special labels to guide collect2.  */
414       switch_to_section (data_section);
415
416       if (!back)
417         {
418           label = get_file_function_name ("F");
419           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
420           targetm.asm_out.globalize_label (asm_out_file,
421                                            IDENTIFIER_POINTER (label));
422           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
423         }
424     }
425 }
426
427 /* Switch [BACK] to the eh or debug frame table section, depending on
428    FOR_EH.  */
429
430 static void
431 switch_to_frame_table_section (int for_eh, bool back)
432 {
433   if (for_eh)
434     switch_to_eh_frame_section (back);
435   else
436     {
437       if (!debug_frame_section)
438         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
439                                            SECTION_DEBUG, NULL);
440       switch_to_section (debug_frame_section);
441     }
442 }
443
444 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
445
446 enum dw_cfi_oprnd_type
447 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
448 {
449   switch (cfi)
450     {
451     case DW_CFA_nop:
452     case DW_CFA_GNU_window_save:
453     case DW_CFA_remember_state:
454     case DW_CFA_restore_state:
455       return dw_cfi_oprnd_unused;
456
457     case DW_CFA_set_loc:
458     case DW_CFA_advance_loc1:
459     case DW_CFA_advance_loc2:
460     case DW_CFA_advance_loc4:
461     case DW_CFA_MIPS_advance_loc8:
462       return dw_cfi_oprnd_addr;
463
464     case DW_CFA_offset:
465     case DW_CFA_offset_extended:
466     case DW_CFA_def_cfa:
467     case DW_CFA_offset_extended_sf:
468     case DW_CFA_def_cfa_sf:
469     case DW_CFA_restore:
470     case DW_CFA_restore_extended:
471     case DW_CFA_undefined:
472     case DW_CFA_same_value:
473     case DW_CFA_def_cfa_register:
474     case DW_CFA_register:
475     case DW_CFA_expression:
476       return dw_cfi_oprnd_reg_num;
477
478     case DW_CFA_def_cfa_offset:
479     case DW_CFA_GNU_args_size:
480     case DW_CFA_def_cfa_offset_sf:
481       return dw_cfi_oprnd_offset;
482
483     case DW_CFA_def_cfa_expression:
484       return dw_cfi_oprnd_loc;
485
486     default:
487       gcc_unreachable ();
488     }
489 }
490
491 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
492
493 enum dw_cfi_oprnd_type
494 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
495 {
496   switch (cfi)
497     {
498     case DW_CFA_def_cfa:
499     case DW_CFA_def_cfa_sf:
500     case DW_CFA_offset:
501     case DW_CFA_offset_extended_sf:
502     case DW_CFA_offset_extended:
503       return dw_cfi_oprnd_offset;
504
505     case DW_CFA_register:
506       return dw_cfi_oprnd_reg_num;
507
508     case DW_CFA_expression:
509       return dw_cfi_oprnd_loc;
510
511     default:
512       return dw_cfi_oprnd_unused;
513     }
514 }
515
516 /* Output one FDE.  */
517
518 static void
519 output_fde (dw_fde_ref fde, bool for_eh, bool second,
520             char *section_start_label, int fde_encoding, char *augmentation,
521             bool any_lsda_needed, int lsda_encoding)
522 {
523   const char *begin, *end;
524   static unsigned int j;
525   char l1[20], l2[20];
526
527   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
528                                      /* empty */ 0);
529   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
530                                   for_eh + j);
531   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
532   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
533   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
534     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
535                          " indicating 64-bit DWARF extension");
536   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
537                         "FDE Length");
538   ASM_OUTPUT_LABEL (asm_out_file, l1);
539
540   if (for_eh)
541     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
542   else
543     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
544                            debug_frame_section, "FDE CIE offset");
545
546   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
547   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
548
549   if (for_eh)
550     {
551       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
552       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
553       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
554                                        "FDE initial location");
555       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
556                             end, begin, "FDE address range");
557     }
558   else
559     {
560       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
561       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
562     }
563
564   if (augmentation[0])
565     {
566       if (any_lsda_needed)
567         {
568           int size = size_of_encoded_value (lsda_encoding);
569
570           if (lsda_encoding == DW_EH_PE_aligned)
571             {
572               int offset = (  4         /* Length */
573                             + 4         /* CIE offset */
574                             + 2 * size_of_encoded_value (fde_encoding)
575                             + 1         /* Augmentation size */ );
576               int pad = -offset & (PTR_SIZE - 1);
577
578               size += pad;
579               gcc_assert (size_of_uleb128 (size) == 1);
580             }
581
582           dw2_asm_output_data_uleb128 (size, "Augmentation size");
583
584           if (fde->uses_eh_lsda)
585             {
586               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
587                                            fde->funcdef_number);
588               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
589                                                gen_rtx_SYMBOL_REF (Pmode, l1),
590                                                false,
591                                                "Language Specific Data Area");
592             }
593           else
594             {
595               if (lsda_encoding == DW_EH_PE_aligned)
596                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
597               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
598                                    "Language Specific Data Area (none)");
599             }
600         }
601       else
602         dw2_asm_output_data_uleb128 (0, "Augmentation size");
603     }
604
605   /* Loop through the Call Frame Instructions associated with this FDE.  */
606   fde->dw_fde_current_label = begin;
607   {
608     size_t from, until, i;
609
610     from = 0;
611     until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
612
613     if (fde->dw_fde_second_begin == NULL)
614       ;
615     else if (!second)
616       until = fde->dw_fde_switch_cfi_index;
617     else
618       from = fde->dw_fde_switch_cfi_index;
619
620     for (i = from; i < until; i++)
621       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
622   }
623
624   /* If we are to emit a ref/link from function bodies to their frame tables,
625      do it now.  This is typically performed to make sure that tables
626      associated with functions are dragged with them and not discarded in
627      garbage collecting links. We need to do this on a per function basis to
628      cope with -ffunction-sections.  */
629
630 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
631   /* Switch to the function section, emit the ref to the tables, and
632      switch *back* into the table section.  */
633   switch_to_section (function_section (fde->decl));
634   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
635   switch_to_frame_table_section (for_eh, true);
636 #endif
637
638   /* Pad the FDE out to an address sized boundary.  */
639   ASM_OUTPUT_ALIGN (asm_out_file,
640                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
641   ASM_OUTPUT_LABEL (asm_out_file, l2);
642
643   j += 2;
644 }
645
646 /* Return true if frame description entry FDE is needed for EH.  */
647
648 static bool
649 fde_needed_for_eh_p (dw_fde_ref fde)
650 {
651   if (flag_asynchronous_unwind_tables)
652     return true;
653
654   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
655     return true;
656
657   if (fde->uses_eh_lsda)
658     return true;
659
660   /* If exceptions are enabled, we have collected nothrow info.  */
661   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
662     return false;
663
664   return true;
665 }
666
667 /* Output the call frame information used to record information
668    that relates to calculating the frame pointer, and records the
669    location of saved registers.  */
670
671 static void
672 output_call_frame_info (int for_eh)
673 {
674   unsigned int i;
675   dw_fde_ref fde;
676   dw_cfi_ref cfi;
677   char l1[20], l2[20], section_start_label[20];
678   bool any_lsda_needed = false;
679   char augmentation[6];
680   int augmentation_size;
681   int fde_encoding = DW_EH_PE_absptr;
682   int per_encoding = DW_EH_PE_absptr;
683   int lsda_encoding = DW_EH_PE_absptr;
684   int return_reg;
685   rtx personality = NULL;
686   int dw_cie_version;
687
688   /* Don't emit a CIE if there won't be any FDEs.  */
689   if (fde_vec == NULL)
690     return;
691
692   /* Nothing to do if the assembler's doing it all.  */
693   if (dwarf2out_do_cfi_asm ())
694     return;
695
696   /* If we don't have any functions we'll want to unwind out of, don't emit
697      any EH unwind information.  If we make FDEs linkonce, we may have to
698      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
699      want to avoid having an FDE kept around when the function it refers to
700      is discarded.  Example where this matters: a primary function template
701      in C++ requires EH information, an explicit specialization doesn't.  */
702   if (for_eh)
703     {
704       bool any_eh_needed = false;
705
706       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
707         {
708           if (fde->uses_eh_lsda)
709             any_eh_needed = any_lsda_needed = true;
710           else if (fde_needed_for_eh_p (fde))
711             any_eh_needed = true;
712           else if (TARGET_USES_WEAK_UNWIND_INFO)
713             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
714         }
715
716       if (!any_eh_needed)
717         return;
718     }
719
720   /* We're going to be generating comments, so turn on app.  */
721   if (flag_debug_asm)
722     app_enable ();
723
724   /* Switch to the proper frame section, first time.  */
725   switch_to_frame_table_section (for_eh, false);
726
727   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
728   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
729
730   /* Output the CIE.  */
731   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
732   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
733   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
734     dw2_asm_output_data (4, 0xffffffff,
735       "Initial length escape value indicating 64-bit DWARF extension");
736   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
737                         "Length of Common Information Entry");
738   ASM_OUTPUT_LABEL (asm_out_file, l1);
739
740   /* Now that the CIE pointer is PC-relative for EH,
741      use 0 to identify the CIE.  */
742   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
743                        (for_eh ? 0 : DWARF_CIE_ID),
744                        "CIE Identifier Tag");
745
746   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
747      use CIE version 1, unless that would produce incorrect results
748      due to overflowing the return register column.  */
749   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
750   dw_cie_version = 1;
751   if (return_reg >= 256 || dwarf_version > 2)
752     dw_cie_version = 3;
753   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
754
755   augmentation[0] = 0;
756   augmentation_size = 0;
757
758   personality = current_unit_personality;
759   if (for_eh)
760     {
761       char *p;
762
763       /* Augmentation:
764          z      Indicates that a uleb128 is present to size the
765                 augmentation section.
766          L      Indicates the encoding (and thus presence) of
767                 an LSDA pointer in the FDE augmentation.
768          R      Indicates a non-default pointer encoding for
769                 FDE code pointers.
770          P      Indicates the presence of an encoding + language
771                 personality routine in the CIE augmentation.  */
772
773       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
774       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
775       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
776
777       p = augmentation + 1;
778       if (personality)
779         {
780           *p++ = 'P';
781           augmentation_size += 1 + size_of_encoded_value (per_encoding);
782           assemble_external_libcall (personality);
783         }
784       if (any_lsda_needed)
785         {
786           *p++ = 'L';
787           augmentation_size += 1;
788         }
789       if (fde_encoding != DW_EH_PE_absptr)
790         {
791           *p++ = 'R';
792           augmentation_size += 1;
793         }
794       if (p > augmentation + 1)
795         {
796           augmentation[0] = 'z';
797           *p = '\0';
798         }
799
800       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
801       if (personality && per_encoding == DW_EH_PE_aligned)
802         {
803           int offset = (  4             /* Length */
804                         + 4             /* CIE Id */
805                         + 1             /* CIE version */
806                         + strlen (augmentation) + 1     /* Augmentation */
807                         + size_of_uleb128 (1)           /* Code alignment */
808                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
809                         + 1             /* RA column */
810                         + 1             /* Augmentation size */
811                         + 1             /* Personality encoding */ );
812           int pad = -offset & (PTR_SIZE - 1);
813
814           augmentation_size += pad;
815
816           /* Augmentations should be small, so there's scarce need to
817              iterate for a solution.  Die if we exceed one uleb128 byte.  */
818           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
819         }
820     }
821
822   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
823   if (dw_cie_version >= 4)
824     {
825       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
826       dw2_asm_output_data (1, 0, "CIE Segment Size");
827     }
828   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
829   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
830                                "CIE Data Alignment Factor");
831
832   if (dw_cie_version == 1)
833     dw2_asm_output_data (1, return_reg, "CIE RA Column");
834   else
835     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
836
837   if (augmentation[0])
838     {
839       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
840       if (personality)
841         {
842           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
843                                eh_data_format_name (per_encoding));
844           dw2_asm_output_encoded_addr_rtx (per_encoding,
845                                            personality,
846                                            true, NULL);
847         }
848
849       if (any_lsda_needed)
850         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
851                              eh_data_format_name (lsda_encoding));
852
853       if (fde_encoding != DW_EH_PE_absptr)
854         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
855                              eh_data_format_name (fde_encoding));
856     }
857
858   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
859     output_cfi (cfi, NULL, for_eh);
860
861   /* Pad the CIE out to an address sized boundary.  */
862   ASM_OUTPUT_ALIGN (asm_out_file,
863                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
864   ASM_OUTPUT_LABEL (asm_out_file, l2);
865
866   /* Loop through all of the FDE's.  */
867   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
868     {
869       unsigned int k;
870
871       /* Don't emit EH unwind info for leaf functions that don't need it.  */
872       if (for_eh && !fde_needed_for_eh_p (fde))
873         continue;
874
875       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
876         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
877                     augmentation, any_lsda_needed, lsda_encoding);
878     }
879
880   if (for_eh && targetm.terminate_dw2_eh_frame_info)
881     dw2_asm_output_data (4, 0, "End of Table");
882 #ifdef MIPS_DEBUGGING_INFO
883   /* Work around Irix 6 assembler bug whereby labels at the end of a section
884      get a value of 0.  Putting .align 0 after the label fixes it.  */
885   ASM_OUTPUT_ALIGN (asm_out_file, 0);
886 #endif
887
888   /* Turn off app to make assembly quicker.  */
889   if (flag_debug_asm)
890     app_disable ();
891 }
892
893 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
894
895 static void
896 dwarf2out_do_cfi_startproc (bool second)
897 {
898   int enc;
899   rtx ref;
900   rtx personality = get_personality_function (current_function_decl);
901
902   fprintf (asm_out_file, "\t.cfi_startproc\n");
903
904   if (personality)
905     {
906       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
907       ref = personality;
908
909       /* ??? The GAS support isn't entirely consistent.  We have to
910          handle indirect support ourselves, but PC-relative is done
911          in the assembler.  Further, the assembler can't handle any
912          of the weirder relocation types.  */
913       if (enc & DW_EH_PE_indirect)
914         ref = dw2_force_const_mem (ref, true);
915
916       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
917       output_addr_const (asm_out_file, ref);
918       fputc ('\n', asm_out_file);
919     }
920
921   if (crtl->uses_eh_lsda)
922     {
923       char lab[20];
924
925       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
926       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
927                                    current_function_funcdef_no);
928       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
929       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930
931       if (enc & DW_EH_PE_indirect)
932         ref = dw2_force_const_mem (ref, true);
933
934       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
935       output_addr_const (asm_out_file, ref);
936       fputc ('\n', asm_out_file);
937     }
938 }
939
940 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
941    this allocation may be done before pass_final.  */
942
943 dw_fde_ref
944 dwarf2out_alloc_current_fde (void)
945 {
946   dw_fde_ref fde;
947
948   fde = ggc_alloc_cleared_dw_fde_node ();
949   fde->decl = current_function_decl;
950   fde->funcdef_number = current_function_funcdef_no;
951   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
952   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
953   fde->uses_eh_lsda = crtl->uses_eh_lsda;
954   fde->nothrow = crtl->nothrow;
955   fde->drap_reg = INVALID_REGNUM;
956   fde->vdrap_reg = INVALID_REGNUM;
957
958   /* Record the FDE associated with this function.  */
959   cfun->fde = fde;
960   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
961
962   return fde;
963 }
964
965 /* Output a marker (i.e. a label) for the beginning of a function, before
966    the prologue.  */
967
968 void
969 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
970                           const char *file ATTRIBUTE_UNUSED)
971 {
972   char label[MAX_ARTIFICIAL_LABEL_BYTES];
973   char * dup_label;
974   dw_fde_ref fde;
975   section *fnsec;
976   bool do_frame;
977
978   current_function_func_begin_label = NULL;
979
980   do_frame = dwarf2out_do_frame ();
981
982   /* ??? current_function_func_begin_label is also used by except.c for
983      call-site information.  We must emit this label if it might be used.  */
984   if (!do_frame
985       && (!flag_exceptions
986           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
987     return;
988
989   fnsec = function_section (current_function_decl);
990   switch_to_section (fnsec);
991   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
992                                current_function_funcdef_no);
993   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
994                           current_function_funcdef_no);
995   dup_label = xstrdup (label);
996   current_function_func_begin_label = dup_label;
997
998   /* We can elide the fde allocation if we're not emitting debug info.  */
999   if (!do_frame)
1000     return;
1001
1002   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1003      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1004      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1005      do so now.  */
1006   fde = cfun->fde;
1007   if (fde == NULL)
1008     fde = dwarf2out_alloc_current_fde ();
1009
1010   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1011   fde->dw_fde_begin = dup_label;
1012   fde->dw_fde_current_label = dup_label;
1013   fde->in_std_section = (fnsec == text_section
1014                          || (cold_text_section && fnsec == cold_text_section));
1015
1016   /* We only want to output line number information for the genuine dwarf2
1017      prologue case, not the eh frame case.  */
1018 #ifdef DWARF2_DEBUGGING_INFO
1019   if (file)
1020     dwarf2out_source_line (line, file, 0, true);
1021 #endif
1022
1023   if (dwarf2out_do_cfi_asm ())
1024     dwarf2out_do_cfi_startproc (false);
1025   else
1026     {
1027       rtx personality = get_personality_function (current_function_decl);
1028       if (!current_unit_personality)
1029         current_unit_personality = personality;
1030
1031       /* We cannot keep a current personality per function as without CFI
1032          asm, at the point where we emit the CFI data, there is no current
1033          function anymore.  */
1034       if (personality && current_unit_personality != personality)
1035         sorry ("multiple EH personalities are supported only with assemblers "
1036                "supporting .cfi_personality directive");
1037     }
1038 }
1039
1040 /* Output a marker (i.e. a label) for the end of the generated code
1041    for a function prologue.  This gets called *after* the prologue code has
1042    been generated.  */
1043
1044 void
1045 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1046                         const char *file ATTRIBUTE_UNUSED)
1047 {
1048   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049
1050   /* Output a label to mark the endpoint of the code generated for this
1051      function.  */
1052   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1053                                current_function_funcdef_no);
1054   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1055                           current_function_funcdef_no);
1056   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1057 }
1058
1059 /* Output a marker (i.e. a label) for the beginning of the generated code
1060    for a function epilogue.  This gets called *before* the prologue code has
1061    been generated.  */
1062
1063 void
1064 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1065                           const char *file ATTRIBUTE_UNUSED)
1066 {
1067   dw_fde_ref fde = cfun->fde;
1068   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069
1070   if (fde->dw_fde_vms_begin_epilogue)
1071     return;
1072
1073   /* Output a label to mark the endpoint of the code generated for this
1074      function.  */
1075   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1076                                current_function_funcdef_no);
1077   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1078                           current_function_funcdef_no);
1079   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1080 }
1081
1082 /* Output a marker (i.e. a label) for the absolute end of the generated code
1083    for a function definition.  This gets called *after* the epilogue code has
1084    been generated.  */
1085
1086 void
1087 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1088                         const char *file ATTRIBUTE_UNUSED)
1089 {
1090   dw_fde_ref fde;
1091   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092
1093   last_var_location_insn = NULL_RTX;
1094   cached_next_real_insn = NULL_RTX;
1095
1096   if (dwarf2out_do_cfi_asm ())
1097     fprintf (asm_out_file, "\t.cfi_endproc\n");
1098
1099   /* Output a label to mark the endpoint of the code generated for this
1100      function.  */
1101   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1102                                current_function_funcdef_no);
1103   ASM_OUTPUT_LABEL (asm_out_file, label);
1104   fde = cfun->fde;
1105   gcc_assert (fde != NULL);
1106   if (fde->dw_fde_second_begin == NULL)
1107     fde->dw_fde_end = xstrdup (label);
1108 }
1109
1110 void
1111 dwarf2out_frame_finish (void)
1112 {
1113   /* Output call frame information.  */
1114   if (targetm.debug_unwind_info () == UI_DWARF2)
1115     output_call_frame_info (0);
1116
1117   /* Output another copy for the unwinder.  */
1118   if ((flag_unwind_tables || flag_exceptions)
1119       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1120     output_call_frame_info (1);
1121 }
1122
1123 /* Note that the current function section is being used for code.  */
1124
1125 static void
1126 dwarf2out_note_section_used (void)
1127 {
1128   section *sec = current_function_section ();
1129   if (sec == text_section)
1130     text_section_used = true;
1131   else if (sec == cold_text_section)
1132     cold_text_section_used = true;
1133 }
1134
1135 static void var_location_switch_text_section (void);
1136 static void set_cur_line_info_table (section *);
1137
1138 void
1139 dwarf2out_switch_text_section (void)
1140 {
1141   section *sect;
1142   dw_fde_ref fde = cfun->fde;
1143
1144   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1145
1146   if (!in_cold_section_p)
1147     {
1148       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1149       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1150       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1151     }
1152   else
1153     {
1154       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1155       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1156       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1157     }
1158   have_multiple_function_sections = true;
1159
1160   /* There is no need to mark used sections when not debugging.  */
1161   if (cold_text_section != NULL)
1162     dwarf2out_note_section_used ();
1163
1164   if (dwarf2out_do_cfi_asm ())
1165     fprintf (asm_out_file, "\t.cfi_endproc\n");
1166
1167   /* Now do the real section switch.  */
1168   sect = current_function_section ();
1169   switch_to_section (sect);
1170
1171   fde->second_in_std_section
1172     = (sect == text_section
1173        || (cold_text_section && sect == cold_text_section));
1174
1175   if (dwarf2out_do_cfi_asm ())
1176     dwarf2out_do_cfi_startproc (true);
1177
1178   var_location_switch_text_section ();
1179
1180   set_cur_line_info_table (sect);
1181 }
1182 \f
1183 /* And now, the subset of the debugging information support code necessary
1184    for emitting location expressions.  */
1185
1186 /* Data about a single source file.  */
1187 struct GTY(()) dwarf_file_data {
1188   const char * filename;
1189   int emitted_number;
1190 };
1191
1192 typedef struct GTY(()) deferred_locations_struct
1193 {
1194   tree variable;
1195   dw_die_ref die;
1196 } deferred_locations;
1197
1198 DEF_VEC_O(deferred_locations);
1199 DEF_VEC_ALLOC_O(deferred_locations,gc);
1200
1201 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1202
1203 DEF_VEC_P(dw_die_ref);
1204 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1205
1206 /* Location lists are ranges + location descriptions for that range,
1207    so you can track variables that are in different places over
1208    their entire life.  */
1209 typedef struct GTY(()) dw_loc_list_struct {
1210   dw_loc_list_ref dw_loc_next;
1211   const char *begin; /* Label for begin address of range */
1212   const char *end;  /* Label for end address of range */
1213   char *ll_symbol; /* Label for beginning of location list.
1214                       Only on head of list */
1215   const char *section; /* Section this loclist is relative to */
1216   dw_loc_descr_ref expr;
1217   hashval_t hash;
1218   /* True if all addresses in this and subsequent lists are known to be
1219      resolved.  */
1220   bool resolved_addr;
1221   /* True if this list has been replaced by dw_loc_next.  */
1222   bool replaced;
1223   bool emitted;
1224   /* True if the range should be emitted even if begin and end
1225      are the same.  */
1226   bool force;
1227 } dw_loc_list_node;
1228
1229 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1230
1231 /* Convert a DWARF stack opcode into its string name.  */
1232
1233 static const char *
1234 dwarf_stack_op_name (unsigned int op)
1235 {
1236   switch (op)
1237     {
1238     case DW_OP_addr:
1239       return "DW_OP_addr";
1240     case DW_OP_deref:
1241       return "DW_OP_deref";
1242     case DW_OP_const1u:
1243       return "DW_OP_const1u";
1244     case DW_OP_const1s:
1245       return "DW_OP_const1s";
1246     case DW_OP_const2u:
1247       return "DW_OP_const2u";
1248     case DW_OP_const2s:
1249       return "DW_OP_const2s";
1250     case DW_OP_const4u:
1251       return "DW_OP_const4u";
1252     case DW_OP_const4s:
1253       return "DW_OP_const4s";
1254     case DW_OP_const8u:
1255       return "DW_OP_const8u";
1256     case DW_OP_const8s:
1257       return "DW_OP_const8s";
1258     case DW_OP_constu:
1259       return "DW_OP_constu";
1260     case DW_OP_consts:
1261       return "DW_OP_consts";
1262     case DW_OP_dup:
1263       return "DW_OP_dup";
1264     case DW_OP_drop:
1265       return "DW_OP_drop";
1266     case DW_OP_over:
1267       return "DW_OP_over";
1268     case DW_OP_pick:
1269       return "DW_OP_pick";
1270     case DW_OP_swap:
1271       return "DW_OP_swap";
1272     case DW_OP_rot:
1273       return "DW_OP_rot";
1274     case DW_OP_xderef:
1275       return "DW_OP_xderef";
1276     case DW_OP_abs:
1277       return "DW_OP_abs";
1278     case DW_OP_and:
1279       return "DW_OP_and";
1280     case DW_OP_div:
1281       return "DW_OP_div";
1282     case DW_OP_minus:
1283       return "DW_OP_minus";
1284     case DW_OP_mod:
1285       return "DW_OP_mod";
1286     case DW_OP_mul:
1287       return "DW_OP_mul";
1288     case DW_OP_neg:
1289       return "DW_OP_neg";
1290     case DW_OP_not:
1291       return "DW_OP_not";
1292     case DW_OP_or:
1293       return "DW_OP_or";
1294     case DW_OP_plus:
1295       return "DW_OP_plus";
1296     case DW_OP_plus_uconst:
1297       return "DW_OP_plus_uconst";
1298     case DW_OP_shl:
1299       return "DW_OP_shl";
1300     case DW_OP_shr:
1301       return "DW_OP_shr";
1302     case DW_OP_shra:
1303       return "DW_OP_shra";
1304     case DW_OP_xor:
1305       return "DW_OP_xor";
1306     case DW_OP_bra:
1307       return "DW_OP_bra";
1308     case DW_OP_eq:
1309       return "DW_OP_eq";
1310     case DW_OP_ge:
1311       return "DW_OP_ge";
1312     case DW_OP_gt:
1313       return "DW_OP_gt";
1314     case DW_OP_le:
1315       return "DW_OP_le";
1316     case DW_OP_lt:
1317       return "DW_OP_lt";
1318     case DW_OP_ne:
1319       return "DW_OP_ne";
1320     case DW_OP_skip:
1321       return "DW_OP_skip";
1322     case DW_OP_lit0:
1323       return "DW_OP_lit0";
1324     case DW_OP_lit1:
1325       return "DW_OP_lit1";
1326     case DW_OP_lit2:
1327       return "DW_OP_lit2";
1328     case DW_OP_lit3:
1329       return "DW_OP_lit3";
1330     case DW_OP_lit4:
1331       return "DW_OP_lit4";
1332     case DW_OP_lit5:
1333       return "DW_OP_lit5";
1334     case DW_OP_lit6:
1335       return "DW_OP_lit6";
1336     case DW_OP_lit7:
1337       return "DW_OP_lit7";
1338     case DW_OP_lit8:
1339       return "DW_OP_lit8";
1340     case DW_OP_lit9:
1341       return "DW_OP_lit9";
1342     case DW_OP_lit10:
1343       return "DW_OP_lit10";
1344     case DW_OP_lit11:
1345       return "DW_OP_lit11";
1346     case DW_OP_lit12:
1347       return "DW_OP_lit12";
1348     case DW_OP_lit13:
1349       return "DW_OP_lit13";
1350     case DW_OP_lit14:
1351       return "DW_OP_lit14";
1352     case DW_OP_lit15:
1353       return "DW_OP_lit15";
1354     case DW_OP_lit16:
1355       return "DW_OP_lit16";
1356     case DW_OP_lit17:
1357       return "DW_OP_lit17";
1358     case DW_OP_lit18:
1359       return "DW_OP_lit18";
1360     case DW_OP_lit19:
1361       return "DW_OP_lit19";
1362     case DW_OP_lit20:
1363       return "DW_OP_lit20";
1364     case DW_OP_lit21:
1365       return "DW_OP_lit21";
1366     case DW_OP_lit22:
1367       return "DW_OP_lit22";
1368     case DW_OP_lit23:
1369       return "DW_OP_lit23";
1370     case DW_OP_lit24:
1371       return "DW_OP_lit24";
1372     case DW_OP_lit25:
1373       return "DW_OP_lit25";
1374     case DW_OP_lit26:
1375       return "DW_OP_lit26";
1376     case DW_OP_lit27:
1377       return "DW_OP_lit27";
1378     case DW_OP_lit28:
1379       return "DW_OP_lit28";
1380     case DW_OP_lit29:
1381       return "DW_OP_lit29";
1382     case DW_OP_lit30:
1383       return "DW_OP_lit30";
1384     case DW_OP_lit31:
1385       return "DW_OP_lit31";
1386     case DW_OP_reg0:
1387       return "DW_OP_reg0";
1388     case DW_OP_reg1:
1389       return "DW_OP_reg1";
1390     case DW_OP_reg2:
1391       return "DW_OP_reg2";
1392     case DW_OP_reg3:
1393       return "DW_OP_reg3";
1394     case DW_OP_reg4:
1395       return "DW_OP_reg4";
1396     case DW_OP_reg5:
1397       return "DW_OP_reg5";
1398     case DW_OP_reg6:
1399       return "DW_OP_reg6";
1400     case DW_OP_reg7:
1401       return "DW_OP_reg7";
1402     case DW_OP_reg8:
1403       return "DW_OP_reg8";
1404     case DW_OP_reg9:
1405       return "DW_OP_reg9";
1406     case DW_OP_reg10:
1407       return "DW_OP_reg10";
1408     case DW_OP_reg11:
1409       return "DW_OP_reg11";
1410     case DW_OP_reg12:
1411       return "DW_OP_reg12";
1412     case DW_OP_reg13:
1413       return "DW_OP_reg13";
1414     case DW_OP_reg14:
1415       return "DW_OP_reg14";
1416     case DW_OP_reg15:
1417       return "DW_OP_reg15";
1418     case DW_OP_reg16:
1419       return "DW_OP_reg16";
1420     case DW_OP_reg17:
1421       return "DW_OP_reg17";
1422     case DW_OP_reg18:
1423       return "DW_OP_reg18";
1424     case DW_OP_reg19:
1425       return "DW_OP_reg19";
1426     case DW_OP_reg20:
1427       return "DW_OP_reg20";
1428     case DW_OP_reg21:
1429       return "DW_OP_reg21";
1430     case DW_OP_reg22:
1431       return "DW_OP_reg22";
1432     case DW_OP_reg23:
1433       return "DW_OP_reg23";
1434     case DW_OP_reg24:
1435       return "DW_OP_reg24";
1436     case DW_OP_reg25:
1437       return "DW_OP_reg25";
1438     case DW_OP_reg26:
1439       return "DW_OP_reg26";
1440     case DW_OP_reg27:
1441       return "DW_OP_reg27";
1442     case DW_OP_reg28:
1443       return "DW_OP_reg28";
1444     case DW_OP_reg29:
1445       return "DW_OP_reg29";
1446     case DW_OP_reg30:
1447       return "DW_OP_reg30";
1448     case DW_OP_reg31:
1449       return "DW_OP_reg31";
1450     case DW_OP_breg0:
1451       return "DW_OP_breg0";
1452     case DW_OP_breg1:
1453       return "DW_OP_breg1";
1454     case DW_OP_breg2:
1455       return "DW_OP_breg2";
1456     case DW_OP_breg3:
1457       return "DW_OP_breg3";
1458     case DW_OP_breg4:
1459       return "DW_OP_breg4";
1460     case DW_OP_breg5:
1461       return "DW_OP_breg5";
1462     case DW_OP_breg6:
1463       return "DW_OP_breg6";
1464     case DW_OP_breg7:
1465       return "DW_OP_breg7";
1466     case DW_OP_breg8:
1467       return "DW_OP_breg8";
1468     case DW_OP_breg9:
1469       return "DW_OP_breg9";
1470     case DW_OP_breg10:
1471       return "DW_OP_breg10";
1472     case DW_OP_breg11:
1473       return "DW_OP_breg11";
1474     case DW_OP_breg12:
1475       return "DW_OP_breg12";
1476     case DW_OP_breg13:
1477       return "DW_OP_breg13";
1478     case DW_OP_breg14:
1479       return "DW_OP_breg14";
1480     case DW_OP_breg15:
1481       return "DW_OP_breg15";
1482     case DW_OP_breg16:
1483       return "DW_OP_breg16";
1484     case DW_OP_breg17:
1485       return "DW_OP_breg17";
1486     case DW_OP_breg18:
1487       return "DW_OP_breg18";
1488     case DW_OP_breg19:
1489       return "DW_OP_breg19";
1490     case DW_OP_breg20:
1491       return "DW_OP_breg20";
1492     case DW_OP_breg21:
1493       return "DW_OP_breg21";
1494     case DW_OP_breg22:
1495       return "DW_OP_breg22";
1496     case DW_OP_breg23:
1497       return "DW_OP_breg23";
1498     case DW_OP_breg24:
1499       return "DW_OP_breg24";
1500     case DW_OP_breg25:
1501       return "DW_OP_breg25";
1502     case DW_OP_breg26:
1503       return "DW_OP_breg26";
1504     case DW_OP_breg27:
1505       return "DW_OP_breg27";
1506     case DW_OP_breg28:
1507       return "DW_OP_breg28";
1508     case DW_OP_breg29:
1509       return "DW_OP_breg29";
1510     case DW_OP_breg30:
1511       return "DW_OP_breg30";
1512     case DW_OP_breg31:
1513       return "DW_OP_breg31";
1514     case DW_OP_regx:
1515       return "DW_OP_regx";
1516     case DW_OP_fbreg:
1517       return "DW_OP_fbreg";
1518     case DW_OP_bregx:
1519       return "DW_OP_bregx";
1520     case DW_OP_piece:
1521       return "DW_OP_piece";
1522     case DW_OP_deref_size:
1523       return "DW_OP_deref_size";
1524     case DW_OP_xderef_size:
1525       return "DW_OP_xderef_size";
1526     case DW_OP_nop:
1527       return "DW_OP_nop";
1528
1529     case DW_OP_push_object_address:
1530       return "DW_OP_push_object_address";
1531     case DW_OP_call2:
1532       return "DW_OP_call2";
1533     case DW_OP_call4:
1534       return "DW_OP_call4";
1535     case DW_OP_call_ref:
1536       return "DW_OP_call_ref";
1537     case DW_OP_implicit_value:
1538       return "DW_OP_implicit_value";
1539     case DW_OP_stack_value:
1540       return "DW_OP_stack_value";
1541     case DW_OP_form_tls_address:
1542       return "DW_OP_form_tls_address";
1543     case DW_OP_call_frame_cfa:
1544       return "DW_OP_call_frame_cfa";
1545     case DW_OP_bit_piece:
1546       return "DW_OP_bit_piece";
1547
1548     case DW_OP_GNU_push_tls_address:
1549       return "DW_OP_GNU_push_tls_address";
1550     case DW_OP_GNU_uninit:
1551       return "DW_OP_GNU_uninit";
1552     case DW_OP_GNU_encoded_addr:
1553       return "DW_OP_GNU_encoded_addr";
1554     case DW_OP_GNU_implicit_pointer:
1555       return "DW_OP_GNU_implicit_pointer";
1556     case DW_OP_GNU_entry_value:
1557       return "DW_OP_GNU_entry_value";
1558     case DW_OP_GNU_const_type:
1559       return "DW_OP_GNU_const_type";
1560     case DW_OP_GNU_regval_type:
1561       return "DW_OP_GNU_regval_type";
1562     case DW_OP_GNU_deref_type:
1563       return "DW_OP_GNU_deref_type";
1564     case DW_OP_GNU_convert:
1565       return "DW_OP_GNU_convert";
1566     case DW_OP_GNU_reinterpret:
1567       return "DW_OP_GNU_reinterpret";
1568     case DW_OP_GNU_parameter_ref:
1569       return "DW_OP_GNU_parameter_ref";
1570
1571     default:
1572       return "OP_<unknown>";
1573     }
1574 }
1575
1576 /* Return a pointer to a newly allocated location description.  Location
1577    descriptions are simple expression terms that can be strung
1578    together to form more complicated location (address) descriptions.  */
1579
1580 static inline dw_loc_descr_ref
1581 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1582                unsigned HOST_WIDE_INT oprnd2)
1583 {
1584   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1585
1586   descr->dw_loc_opc = op;
1587   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1588   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1589   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1590   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1591
1592   return descr;
1593 }
1594
1595 /* Return a pointer to a newly allocated location description for
1596    REG and OFFSET.  */
1597
1598 static inline dw_loc_descr_ref
1599 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1600 {
1601   if (reg <= 31)
1602     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1603                           offset, 0);
1604   else
1605     return new_loc_descr (DW_OP_bregx, reg, offset);
1606 }
1607
1608 /* Add a location description term to a location description expression.  */
1609
1610 static inline void
1611 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1612 {
1613   dw_loc_descr_ref *d;
1614
1615   /* Find the end of the chain.  */
1616   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1617     ;
1618
1619   *d = descr;
1620 }
1621
1622 /* Compare two location operands for exact equality.  */
1623
1624 static bool
1625 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1626 {
1627   if (a->val_class != b->val_class)
1628     return false;
1629   switch (a->val_class)
1630     {
1631     case dw_val_class_none:
1632       return true;
1633     case dw_val_class_addr:
1634       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1635
1636     case dw_val_class_offset:
1637     case dw_val_class_unsigned_const:
1638     case dw_val_class_const:
1639     case dw_val_class_range_list:
1640     case dw_val_class_lineptr:
1641     case dw_val_class_macptr:
1642       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1643       return a->v.val_unsigned == b->v.val_unsigned;
1644
1645     case dw_val_class_loc:
1646       return a->v.val_loc == b->v.val_loc;
1647     case dw_val_class_loc_list:
1648       return a->v.val_loc_list == b->v.val_loc_list;
1649     case dw_val_class_die_ref:
1650       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1651     case dw_val_class_fde_ref:
1652       return a->v.val_fde_index == b->v.val_fde_index;
1653     case dw_val_class_lbl_id:
1654       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1655     case dw_val_class_str:
1656       return a->v.val_str == b->v.val_str;
1657     case dw_val_class_flag:
1658       return a->v.val_flag == b->v.val_flag;
1659     case dw_val_class_file:
1660       return a->v.val_file == b->v.val_file;
1661     case dw_val_class_decl_ref:
1662       return a->v.val_decl_ref == b->v.val_decl_ref;
1663     
1664     case dw_val_class_const_double:
1665       return (a->v.val_double.high == b->v.val_double.high
1666               && a->v.val_double.low == b->v.val_double.low);
1667
1668     case dw_val_class_vec:
1669       {
1670         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1671         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1672
1673         return (a_len == b_len
1674                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1675       }
1676
1677     case dw_val_class_data8:
1678       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1679
1680     case dw_val_class_vms_delta:
1681       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1682               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1683     }
1684   gcc_unreachable ();
1685 }
1686
1687 /* Compare two location atoms for exact equality.  */
1688
1689 static bool
1690 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1691 {
1692   if (a->dw_loc_opc != b->dw_loc_opc)
1693     return false;
1694
1695   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1696      address size, but since we always allocate cleared storage it
1697      should be zero for other types of locations.  */
1698   if (a->dtprel != b->dtprel)
1699     return false;
1700
1701   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1702           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1703 }
1704
1705 /* Compare two complete location expressions for exact equality.  */
1706
1707 bool
1708 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1709 {
1710   while (1)
1711     {
1712       if (a == b)
1713         return true;
1714       if (a == NULL || b == NULL)
1715         return false;
1716       if (!loc_descr_equal_p_1 (a, b))
1717         return false;
1718
1719       a = a->dw_loc_next;
1720       b = b->dw_loc_next;
1721     }
1722 }
1723
1724
1725 /* Add a constant OFFSET to a location expression.  */
1726
1727 static void
1728 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1729 {
1730   dw_loc_descr_ref loc;
1731   HOST_WIDE_INT *p;
1732
1733   gcc_assert (*list_head != NULL);
1734
1735   if (!offset)
1736     return;
1737
1738   /* Find the end of the chain.  */
1739   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1740     ;
1741
1742   p = NULL;
1743   if (loc->dw_loc_opc == DW_OP_fbreg
1744       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1745     p = &loc->dw_loc_oprnd1.v.val_int;
1746   else if (loc->dw_loc_opc == DW_OP_bregx)
1747     p = &loc->dw_loc_oprnd2.v.val_int;
1748
1749   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1750      offset.  Don't optimize if an signed integer overflow would happen.  */
1751   if (p != NULL
1752       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1753           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1754     *p += offset;
1755
1756   else if (offset > 0)
1757     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1758
1759   else
1760     {
1761       loc->dw_loc_next = int_loc_descriptor (-offset);
1762       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1763     }
1764 }
1765
1766 /* Add a constant OFFSET to a location list.  */
1767
1768 static void
1769 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1770 {
1771   dw_loc_list_ref d;
1772   for (d = list_head; d != NULL; d = d->dw_loc_next)
1773     loc_descr_plus_const (&d->expr, offset);
1774 }
1775
1776 #define DWARF_REF_SIZE  \
1777   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1778
1779 static unsigned long int get_base_type_offset (dw_die_ref);
1780
1781 /* Return the size of a location descriptor.  */
1782
1783 static unsigned long
1784 size_of_loc_descr (dw_loc_descr_ref loc)
1785 {
1786   unsigned long size = 1;
1787
1788   switch (loc->dw_loc_opc)
1789     {
1790     case DW_OP_addr:
1791       size += DWARF2_ADDR_SIZE;
1792       break;
1793     case DW_OP_const1u:
1794     case DW_OP_const1s:
1795       size += 1;
1796       break;
1797     case DW_OP_const2u:
1798     case DW_OP_const2s:
1799       size += 2;
1800       break;
1801     case DW_OP_const4u:
1802     case DW_OP_const4s:
1803       size += 4;
1804       break;
1805     case DW_OP_const8u:
1806     case DW_OP_const8s:
1807       size += 8;
1808       break;
1809     case DW_OP_constu:
1810       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1811       break;
1812     case DW_OP_consts:
1813       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1814       break;
1815     case DW_OP_pick:
1816       size += 1;
1817       break;
1818     case DW_OP_plus_uconst:
1819       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1820       break;
1821     case DW_OP_skip:
1822     case DW_OP_bra:
1823       size += 2;
1824       break;
1825     case DW_OP_breg0:
1826     case DW_OP_breg1:
1827     case DW_OP_breg2:
1828     case DW_OP_breg3:
1829     case DW_OP_breg4:
1830     case DW_OP_breg5:
1831     case DW_OP_breg6:
1832     case DW_OP_breg7:
1833     case DW_OP_breg8:
1834     case DW_OP_breg9:
1835     case DW_OP_breg10:
1836     case DW_OP_breg11:
1837     case DW_OP_breg12:
1838     case DW_OP_breg13:
1839     case DW_OP_breg14:
1840     case DW_OP_breg15:
1841     case DW_OP_breg16:
1842     case DW_OP_breg17:
1843     case DW_OP_breg18:
1844     case DW_OP_breg19:
1845     case DW_OP_breg20:
1846     case DW_OP_breg21:
1847     case DW_OP_breg22:
1848     case DW_OP_breg23:
1849     case DW_OP_breg24:
1850     case DW_OP_breg25:
1851     case DW_OP_breg26:
1852     case DW_OP_breg27:
1853     case DW_OP_breg28:
1854     case DW_OP_breg29:
1855     case DW_OP_breg30:
1856     case DW_OP_breg31:
1857       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1858       break;
1859     case DW_OP_regx:
1860       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1861       break;
1862     case DW_OP_fbreg:
1863       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1864       break;
1865     case DW_OP_bregx:
1866       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1867       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1868       break;
1869     case DW_OP_piece:
1870       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871       break;
1872     case DW_OP_bit_piece:
1873       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1874       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1875       break;
1876     case DW_OP_deref_size:
1877     case DW_OP_xderef_size:
1878       size += 1;
1879       break;
1880     case DW_OP_call2:
1881       size += 2;
1882       break;
1883     case DW_OP_call4:
1884       size += 4;
1885       break;
1886     case DW_OP_call_ref:
1887       size += DWARF_REF_SIZE;
1888       break;
1889     case DW_OP_implicit_value:
1890       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1891               + loc->dw_loc_oprnd1.v.val_unsigned;
1892       break;
1893     case DW_OP_GNU_implicit_pointer:
1894       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1895       break;
1896     case DW_OP_GNU_entry_value:
1897       {
1898         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1899         size += size_of_uleb128 (op_size) + op_size;
1900         break;
1901       }
1902     case DW_OP_GNU_const_type:
1903       {
1904         unsigned long o
1905           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1906         size += size_of_uleb128 (o) + 1;
1907         switch (loc->dw_loc_oprnd2.val_class)
1908           {
1909           case dw_val_class_vec:
1910             size += loc->dw_loc_oprnd2.v.val_vec.length
1911                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1912             break;
1913           case dw_val_class_const:
1914             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1915             break;
1916           case dw_val_class_const_double:
1917             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1918             break;
1919           default:
1920             gcc_unreachable ();
1921           }
1922         break;
1923       }
1924     case DW_OP_GNU_regval_type:
1925       {
1926         unsigned long o
1927           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1928         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1929                 + size_of_uleb128 (o);
1930       }
1931       break;
1932     case DW_OP_GNU_deref_type:
1933       {
1934         unsigned long o
1935           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1936         size += 1 + size_of_uleb128 (o);
1937       }
1938       break;
1939     case DW_OP_GNU_convert:
1940     case DW_OP_GNU_reinterpret:
1941       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1942         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1943       else
1944         {
1945           unsigned long o
1946             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1947           size += size_of_uleb128 (o);
1948         }
1949       break;
1950     case DW_OP_GNU_parameter_ref:
1951       size += 4;
1952       break;
1953     default:
1954       break;
1955     }
1956
1957   return size;
1958 }
1959
1960 /* Return the size of a series of location descriptors.  */
1961
1962 unsigned long
1963 size_of_locs (dw_loc_descr_ref loc)
1964 {
1965   dw_loc_descr_ref l;
1966   unsigned long size;
1967
1968   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1969      field, to avoid writing to a PCH file.  */
1970   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1971     {
1972       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1973         break;
1974       size += size_of_loc_descr (l);
1975     }
1976   if (! l)
1977     return size;
1978
1979   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1980     {
1981       l->dw_loc_addr = size;
1982       size += size_of_loc_descr (l);
1983     }
1984
1985   return size;
1986 }
1987
1988 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1989 static void get_ref_die_offset_label (char *, dw_die_ref);
1990 static unsigned long int get_ref_die_offset (dw_die_ref);
1991
1992 /* Output location description stack opcode's operands (if any).
1993    The for_eh_or_skip parameter controls whether register numbers are
1994    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1995    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1996    info).  This should be suppressed for the cases that have not been converted
1997    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1998
1999 static void
2000 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2001 {
2002   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2003   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2004
2005   switch (loc->dw_loc_opc)
2006     {
2007 #ifdef DWARF2_DEBUGGING_INFO
2008     case DW_OP_const2u:
2009     case DW_OP_const2s:
2010       dw2_asm_output_data (2, val1->v.val_int, NULL);
2011       break;
2012     case DW_OP_const4u:
2013       if (loc->dtprel)
2014         {
2015           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2016           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2017                                                val1->v.val_addr);
2018           fputc ('\n', asm_out_file);
2019           break;
2020         }
2021       /* FALLTHRU */
2022     case DW_OP_const4s:
2023       dw2_asm_output_data (4, val1->v.val_int, NULL);
2024       break;
2025     case DW_OP_const8u:
2026       if (loc->dtprel)
2027         {
2028           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2029           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2030                                                val1->v.val_addr);
2031           fputc ('\n', asm_out_file);
2032           break;
2033         }
2034       /* FALLTHRU */
2035     case DW_OP_const8s:
2036       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2037       dw2_asm_output_data (8, val1->v.val_int, NULL);
2038       break;
2039     case DW_OP_skip:
2040     case DW_OP_bra:
2041       {
2042         int offset;
2043
2044         gcc_assert (val1->val_class == dw_val_class_loc);
2045         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2046
2047         dw2_asm_output_data (2, offset, NULL);
2048       }
2049       break;
2050     case DW_OP_implicit_value:
2051       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2052       switch (val2->val_class)
2053         {
2054         case dw_val_class_const:
2055           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2056           break;
2057         case dw_val_class_vec:
2058           {
2059             unsigned int elt_size = val2->v.val_vec.elt_size;
2060             unsigned int len = val2->v.val_vec.length;
2061             unsigned int i;
2062             unsigned char *p;
2063
2064             if (elt_size > sizeof (HOST_WIDE_INT))
2065               {
2066                 elt_size /= 2;
2067                 len *= 2;
2068               }
2069             for (i = 0, p = val2->v.val_vec.array;
2070                  i < len;
2071                  i++, p += elt_size)
2072               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2073                                    "fp or vector constant word %u", i);
2074           }
2075           break;
2076         case dw_val_class_const_double:
2077           {
2078             unsigned HOST_WIDE_INT first, second;
2079
2080             if (WORDS_BIG_ENDIAN)
2081               {
2082                 first = val2->v.val_double.high;
2083                 second = val2->v.val_double.low;
2084               }
2085             else
2086               {
2087                 first = val2->v.val_double.low;
2088                 second = val2->v.val_double.high;
2089               }
2090             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2091                                  first, NULL);
2092             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2093                                  second, NULL);
2094           }
2095           break;
2096         case dw_val_class_addr:
2097           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2098           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2099           break;
2100         default:
2101           gcc_unreachable ();
2102         }
2103       break;
2104 #else
2105     case DW_OP_const2u:
2106     case DW_OP_const2s:
2107     case DW_OP_const4u:
2108     case DW_OP_const4s:
2109     case DW_OP_const8u:
2110     case DW_OP_const8s:
2111     case DW_OP_skip:
2112     case DW_OP_bra:
2113     case DW_OP_implicit_value:
2114       /* We currently don't make any attempt to make sure these are
2115          aligned properly like we do for the main unwind info, so
2116          don't support emitting things larger than a byte if we're
2117          only doing unwinding.  */
2118       gcc_unreachable ();
2119 #endif
2120     case DW_OP_const1u:
2121     case DW_OP_const1s:
2122       dw2_asm_output_data (1, val1->v.val_int, NULL);
2123       break;
2124     case DW_OP_constu:
2125       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2126       break;
2127     case DW_OP_consts:
2128       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2129       break;
2130     case DW_OP_pick:
2131       dw2_asm_output_data (1, val1->v.val_int, NULL);
2132       break;
2133     case DW_OP_plus_uconst:
2134       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2135       break;
2136     case DW_OP_breg0:
2137     case DW_OP_breg1:
2138     case DW_OP_breg2:
2139     case DW_OP_breg3:
2140     case DW_OP_breg4:
2141     case DW_OP_breg5:
2142     case DW_OP_breg6:
2143     case DW_OP_breg7:
2144     case DW_OP_breg8:
2145     case DW_OP_breg9:
2146     case DW_OP_breg10:
2147     case DW_OP_breg11:
2148     case DW_OP_breg12:
2149     case DW_OP_breg13:
2150     case DW_OP_breg14:
2151     case DW_OP_breg15:
2152     case DW_OP_breg16:
2153     case DW_OP_breg17:
2154     case DW_OP_breg18:
2155     case DW_OP_breg19:
2156     case DW_OP_breg20:
2157     case DW_OP_breg21:
2158     case DW_OP_breg22:
2159     case DW_OP_breg23:
2160     case DW_OP_breg24:
2161     case DW_OP_breg25:
2162     case DW_OP_breg26:
2163     case DW_OP_breg27:
2164     case DW_OP_breg28:
2165     case DW_OP_breg29:
2166     case DW_OP_breg30:
2167     case DW_OP_breg31:
2168       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2169       break;
2170     case DW_OP_regx:
2171       {
2172         unsigned r = val1->v.val_unsigned;
2173         if (for_eh_or_skip >= 0)
2174           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2175         gcc_assert (size_of_uleb128 (r) 
2176                     == size_of_uleb128 (val1->v.val_unsigned));
2177         dw2_asm_output_data_uleb128 (r, NULL);  
2178       }
2179       break;
2180     case DW_OP_fbreg:
2181       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2182       break;
2183     case DW_OP_bregx:
2184       {
2185         unsigned r = val1->v.val_unsigned;
2186         if (for_eh_or_skip >= 0)
2187           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2188         gcc_assert (size_of_uleb128 (r) 
2189                     == size_of_uleb128 (val1->v.val_unsigned));
2190         dw2_asm_output_data_uleb128 (r, NULL);  
2191         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2192       }
2193       break;
2194     case DW_OP_piece:
2195       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2196       break;
2197     case DW_OP_bit_piece:
2198       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2199       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2200       break;
2201     case DW_OP_deref_size:
2202     case DW_OP_xderef_size:
2203       dw2_asm_output_data (1, val1->v.val_int, NULL);
2204       break;
2205
2206     case DW_OP_addr:
2207       if (loc->dtprel)
2208         {
2209           if (targetm.asm_out.output_dwarf_dtprel)
2210             {
2211               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2212                                                    DWARF2_ADDR_SIZE,
2213                                                    val1->v.val_addr);
2214               fputc ('\n', asm_out_file);
2215             }
2216           else
2217             gcc_unreachable ();
2218         }
2219       else
2220         {
2221 #ifdef DWARF2_DEBUGGING_INFO
2222           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2223 #else
2224           gcc_unreachable ();
2225 #endif
2226         }
2227       break;
2228
2229     case DW_OP_GNU_implicit_pointer:
2230       {
2231         char label[MAX_ARTIFICIAL_LABEL_BYTES
2232                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2233         gcc_assert (val1->val_class == dw_val_class_die_ref);
2234         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2235         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2236         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2237       }
2238       break;
2239
2240     case DW_OP_GNU_entry_value:
2241       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2242       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2243       break;
2244
2245     case DW_OP_GNU_const_type:
2246       {
2247         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2248         gcc_assert (o);
2249         dw2_asm_output_data_uleb128 (o, NULL);
2250         switch (val2->val_class)
2251           {
2252           case dw_val_class_const:
2253             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2254             dw2_asm_output_data (1, l, NULL);
2255             dw2_asm_output_data (l, val2->v.val_int, NULL);
2256             break;
2257           case dw_val_class_vec:
2258             {
2259               unsigned int elt_size = val2->v.val_vec.elt_size;
2260               unsigned int len = val2->v.val_vec.length;
2261               unsigned int i;
2262               unsigned char *p;
2263
2264               l = len * elt_size;
2265               dw2_asm_output_data (1, l, NULL);
2266               if (elt_size > sizeof (HOST_WIDE_INT))
2267                 {
2268                   elt_size /= 2;
2269                   len *= 2;
2270                 }
2271               for (i = 0, p = val2->v.val_vec.array;
2272                    i < len;
2273                    i++, p += elt_size)
2274                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2275                                      "fp or vector constant word %u", i);
2276             }
2277             break;
2278           case dw_val_class_const_double:
2279             {
2280               unsigned HOST_WIDE_INT first, second;
2281               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2282
2283               dw2_asm_output_data (1, 2 * l, NULL);
2284               if (WORDS_BIG_ENDIAN)
2285                 {
2286                   first = val2->v.val_double.high;
2287                   second = val2->v.val_double.low;
2288                 }
2289               else
2290                 {
2291                   first = val2->v.val_double.low;
2292                   second = val2->v.val_double.high;
2293                 }
2294               dw2_asm_output_data (l, first, NULL);
2295               dw2_asm_output_data (l, second, NULL);
2296             }
2297             break;
2298           default:
2299             gcc_unreachable ();
2300           }
2301       }
2302       break;
2303     case DW_OP_GNU_regval_type:
2304       {
2305         unsigned r = val1->v.val_unsigned;
2306         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2307         gcc_assert (o);
2308         if (for_eh_or_skip >= 0)
2309           {
2310             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2311             gcc_assert (size_of_uleb128 (r)
2312                         == size_of_uleb128 (val1->v.val_unsigned));
2313           }
2314         dw2_asm_output_data_uleb128 (r, NULL);
2315         dw2_asm_output_data_uleb128 (o, NULL);
2316       }
2317       break;
2318     case DW_OP_GNU_deref_type:
2319       {
2320         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2321         gcc_assert (o);
2322         dw2_asm_output_data (1, val1->v.val_int, NULL);
2323         dw2_asm_output_data_uleb128 (o, NULL);
2324       }
2325       break;
2326     case DW_OP_GNU_convert:
2327     case DW_OP_GNU_reinterpret:
2328       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2329         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2330       else
2331         {
2332           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2333           gcc_assert (o);
2334           dw2_asm_output_data_uleb128 (o, NULL);
2335         }
2336       break;
2337
2338     case DW_OP_GNU_parameter_ref:
2339       {
2340         unsigned long o;
2341         gcc_assert (val1->val_class == dw_val_class_die_ref);
2342         o = get_ref_die_offset (val1->v.val_die_ref.die);
2343         dw2_asm_output_data (4, o, NULL);
2344       }
2345       break;
2346
2347     default:
2348       /* Other codes have no operands.  */
2349       break;
2350     }
2351 }
2352
2353 /* Output a sequence of location operations.  
2354    The for_eh_or_skip parameter controls whether register numbers are
2355    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2356    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2357    info).  This should be suppressed for the cases that have not been converted
2358    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2359
2360 void
2361 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2362 {
2363   for (; loc != NULL; loc = loc->dw_loc_next)
2364     {
2365       enum dwarf_location_atom opc = loc->dw_loc_opc;
2366       /* Output the opcode.  */
2367       if (for_eh_or_skip >= 0 
2368           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2369         {
2370           unsigned r = (opc - DW_OP_breg0);
2371           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2372           gcc_assert (r <= 31);
2373           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2374         }
2375       else if (for_eh_or_skip >= 0 
2376                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2377         {
2378           unsigned r = (opc - DW_OP_reg0);
2379           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2380           gcc_assert (r <= 31);
2381           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2382         }
2383
2384       dw2_asm_output_data (1, opc,
2385                              "%s", dwarf_stack_op_name (opc));
2386
2387       /* Output the operand(s) (if any).  */
2388       output_loc_operands (loc, for_eh_or_skip);
2389     }
2390 }
2391
2392 /* Output location description stack opcode's operands (if any).
2393    The output is single bytes on a line, suitable for .cfi_escape.  */
2394
2395 static void
2396 output_loc_operands_raw (dw_loc_descr_ref loc)
2397 {
2398   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2399   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2400
2401   switch (loc->dw_loc_opc)
2402     {
2403     case DW_OP_addr:
2404     case DW_OP_implicit_value:
2405       /* We cannot output addresses in .cfi_escape, only bytes.  */
2406       gcc_unreachable ();
2407
2408     case DW_OP_const1u:
2409     case DW_OP_const1s:
2410     case DW_OP_pick:
2411     case DW_OP_deref_size:
2412     case DW_OP_xderef_size:
2413       fputc (',', asm_out_file);
2414       dw2_asm_output_data_raw (1, val1->v.val_int);
2415       break;
2416
2417     case DW_OP_const2u:
2418     case DW_OP_const2s:
2419       fputc (',', asm_out_file);
2420       dw2_asm_output_data_raw (2, val1->v.val_int);
2421       break;
2422
2423     case DW_OP_const4u:
2424     case DW_OP_const4s:
2425       fputc (',', asm_out_file);
2426       dw2_asm_output_data_raw (4, val1->v.val_int);
2427       break;
2428
2429     case DW_OP_const8u:
2430     case DW_OP_const8s:
2431       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2432       fputc (',', asm_out_file);
2433       dw2_asm_output_data_raw (8, val1->v.val_int);
2434       break;
2435
2436     case DW_OP_skip:
2437     case DW_OP_bra:
2438       {
2439         int offset;
2440
2441         gcc_assert (val1->val_class == dw_val_class_loc);
2442         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2443
2444         fputc (',', asm_out_file);
2445         dw2_asm_output_data_raw (2, offset);
2446       }
2447       break;
2448
2449     case DW_OP_regx:
2450       {
2451         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2452         gcc_assert (size_of_uleb128 (r) 
2453                     == size_of_uleb128 (val1->v.val_unsigned));
2454         fputc (',', asm_out_file);
2455         dw2_asm_output_data_uleb128_raw (r);
2456       }
2457       break;
2458       
2459     case DW_OP_constu:
2460     case DW_OP_plus_uconst:
2461     case DW_OP_piece:
2462       fputc (',', asm_out_file);
2463       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2464       break;
2465
2466     case DW_OP_bit_piece:
2467       fputc (',', asm_out_file);
2468       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2469       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2470       break;
2471
2472     case DW_OP_consts:
2473     case DW_OP_breg0:
2474     case DW_OP_breg1:
2475     case DW_OP_breg2:
2476     case DW_OP_breg3:
2477     case DW_OP_breg4:
2478     case DW_OP_breg5:
2479     case DW_OP_breg6:
2480     case DW_OP_breg7:
2481     case DW_OP_breg8:
2482     case DW_OP_breg9:
2483     case DW_OP_breg10:
2484     case DW_OP_breg11:
2485     case DW_OP_breg12:
2486     case DW_OP_breg13:
2487     case DW_OP_breg14:
2488     case DW_OP_breg15:
2489     case DW_OP_breg16:
2490     case DW_OP_breg17:
2491     case DW_OP_breg18:
2492     case DW_OP_breg19:
2493     case DW_OP_breg20:
2494     case DW_OP_breg21:
2495     case DW_OP_breg22:
2496     case DW_OP_breg23:
2497     case DW_OP_breg24:
2498     case DW_OP_breg25:
2499     case DW_OP_breg26:
2500     case DW_OP_breg27:
2501     case DW_OP_breg28:
2502     case DW_OP_breg29:
2503     case DW_OP_breg30:
2504     case DW_OP_breg31:
2505     case DW_OP_fbreg:
2506       fputc (',', asm_out_file);
2507       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2508       break;
2509
2510     case DW_OP_bregx:
2511       {
2512         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2513         gcc_assert (size_of_uleb128 (r) 
2514                     == size_of_uleb128 (val1->v.val_unsigned));
2515         fputc (',', asm_out_file);
2516         dw2_asm_output_data_uleb128_raw (r);
2517         fputc (',', asm_out_file);
2518         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2519       }
2520       break;
2521
2522     case DW_OP_GNU_implicit_pointer:
2523     case DW_OP_GNU_entry_value:
2524     case DW_OP_GNU_const_type:
2525     case DW_OP_GNU_regval_type:
2526     case DW_OP_GNU_deref_type:
2527     case DW_OP_GNU_convert:
2528     case DW_OP_GNU_reinterpret:
2529     case DW_OP_GNU_parameter_ref:
2530       gcc_unreachable ();
2531       break;
2532
2533     default:
2534       /* Other codes have no operands.  */
2535       break;
2536     }
2537 }
2538
2539 void
2540 output_loc_sequence_raw (dw_loc_descr_ref loc)
2541 {
2542   while (1)
2543     {
2544       enum dwarf_location_atom opc = loc->dw_loc_opc;
2545       /* Output the opcode.  */
2546       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2547         {
2548           unsigned r = (opc - DW_OP_breg0);
2549           r = DWARF2_FRAME_REG_OUT (r, 1);
2550           gcc_assert (r <= 31);
2551           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2552         }
2553       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2554         {
2555           unsigned r = (opc - DW_OP_reg0);
2556           r = DWARF2_FRAME_REG_OUT (r, 1);
2557           gcc_assert (r <= 31);
2558           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2559         }
2560       /* Output the opcode.  */
2561       fprintf (asm_out_file, "%#x", opc);
2562       output_loc_operands_raw (loc);
2563
2564       if (!loc->dw_loc_next)
2565         break;
2566       loc = loc->dw_loc_next;
2567
2568       fputc (',', asm_out_file);
2569     }
2570 }
2571
2572 /* This function builds a dwarf location descriptor sequence from a
2573    dw_cfa_location, adding the given OFFSET to the result of the
2574    expression.  */
2575
2576 struct dw_loc_descr_struct *
2577 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2578 {
2579   struct dw_loc_descr_struct *head, *tmp;
2580
2581   offset += cfa->offset;
2582
2583   if (cfa->indirect)
2584     {
2585       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2586       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2587       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2588       add_loc_descr (&head, tmp);
2589       if (offset != 0)
2590         {
2591           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2592           add_loc_descr (&head, tmp);
2593         }
2594     }
2595   else
2596     head = new_reg_loc_descr (cfa->reg, offset);
2597
2598   return head;
2599 }
2600
2601 /* This function builds a dwarf location descriptor sequence for
2602    the address at OFFSET from the CFA when stack is aligned to
2603    ALIGNMENT byte.  */
2604
2605 struct dw_loc_descr_struct *
2606 build_cfa_aligned_loc (dw_cfa_location *cfa,
2607                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2608 {
2609   struct dw_loc_descr_struct *head;
2610   unsigned int dwarf_fp
2611     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2612
2613   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2614   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2615     {
2616       head = new_reg_loc_descr (dwarf_fp, 0);
2617       add_loc_descr (&head, int_loc_descriptor (alignment));
2618       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2619       loc_descr_plus_const (&head, offset);
2620     }
2621   else
2622     head = new_reg_loc_descr (dwarf_fp, offset);
2623   return head;
2624 }
2625 \f
2626 /* And now, the support for symbolic debugging information.  */
2627
2628 /* .debug_str support.  */
2629 static int output_indirect_string (void **, void *);
2630
2631 static void dwarf2out_init (const char *);
2632 static void dwarf2out_finish (const char *);
2633 static void dwarf2out_assembly_start (void);
2634 static void dwarf2out_define (unsigned int, const char *);
2635 static void dwarf2out_undef (unsigned int, const char *);
2636 static void dwarf2out_start_source_file (unsigned, const char *);
2637 static void dwarf2out_end_source_file (unsigned);
2638 static void dwarf2out_function_decl (tree);
2639 static void dwarf2out_begin_block (unsigned, unsigned);
2640 static void dwarf2out_end_block (unsigned, unsigned);
2641 static bool dwarf2out_ignore_block (const_tree);
2642 static void dwarf2out_global_decl (tree);
2643 static void dwarf2out_type_decl (tree, int);
2644 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2645 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2646                                                  dw_die_ref);
2647 static void dwarf2out_abstract_function (tree);
2648 static void dwarf2out_var_location (rtx);
2649 static void dwarf2out_begin_function (tree);
2650 static void dwarf2out_set_name (tree, tree);
2651
2652 /* The debug hooks structure.  */
2653
2654 const struct gcc_debug_hooks dwarf2_debug_hooks =
2655 {
2656   dwarf2out_init,
2657   dwarf2out_finish,
2658   dwarf2out_assembly_start,
2659   dwarf2out_define,
2660   dwarf2out_undef,
2661   dwarf2out_start_source_file,
2662   dwarf2out_end_source_file,
2663   dwarf2out_begin_block,
2664   dwarf2out_end_block,
2665   dwarf2out_ignore_block,
2666   dwarf2out_source_line,
2667   dwarf2out_begin_prologue,
2668 #if VMS_DEBUGGING_INFO
2669   dwarf2out_vms_end_prologue,
2670   dwarf2out_vms_begin_epilogue,
2671 #else
2672   debug_nothing_int_charstar,
2673   debug_nothing_int_charstar,
2674 #endif
2675   dwarf2out_end_epilogue,
2676   dwarf2out_begin_function,
2677   debug_nothing_int,            /* end_function */
2678   dwarf2out_function_decl,      /* function_decl */
2679   dwarf2out_global_decl,
2680   dwarf2out_type_decl,          /* type_decl */
2681   dwarf2out_imported_module_or_decl,
2682   debug_nothing_tree,           /* deferred_inline_function */
2683   /* The DWARF 2 backend tries to reduce debugging bloat by not
2684      emitting the abstract description of inline functions until
2685      something tries to reference them.  */
2686   dwarf2out_abstract_function,  /* outlining_inline_function */
2687   debug_nothing_rtx,            /* label */
2688   debug_nothing_int,            /* handle_pch */
2689   dwarf2out_var_location,
2690   dwarf2out_switch_text_section,
2691   dwarf2out_set_name,
2692   1,                            /* start_end_main_source_file */
2693   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2694 };
2695 \f
2696 /* NOTE: In the comments in this file, many references are made to
2697    "Debugging Information Entries".  This term is abbreviated as `DIE'
2698    throughout the remainder of this file.  */
2699
2700 /* An internal representation of the DWARF output is built, and then
2701    walked to generate the DWARF debugging info.  The walk of the internal
2702    representation is done after the entire program has been compiled.
2703    The types below are used to describe the internal representation.  */
2704
2705 /* Whether to put type DIEs into their own section .debug_types instead
2706    of making them part of the .debug_info section.  Only supported for
2707    Dwarf V4 or higher and the user didn't disable them through
2708    -fno-debug-types-section.  It is more efficient to put them in a
2709    separate comdat sections since the linker will then be able to
2710    remove duplicates.  But not all tools support .debug_types sections
2711    yet.  */
2712
2713 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2714
2715 /* Various DIE's use offsets relative to the beginning of the
2716    .debug_info section to refer to each other.  */
2717
2718 typedef long int dw_offset;
2719
2720 /* Define typedefs here to avoid circular dependencies.  */
2721
2722 typedef struct dw_attr_struct *dw_attr_ref;
2723 typedef struct dw_line_info_struct *dw_line_info_ref;
2724 typedef struct pubname_struct *pubname_ref;
2725 typedef struct dw_ranges_struct *dw_ranges_ref;
2726 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2727 typedef struct comdat_type_struct *comdat_type_node_ref;
2728
2729 /* The entries in the line_info table more-or-less mirror the opcodes
2730    that are used in the real dwarf line table.  Arrays of these entries
2731    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2732    supported.  */
2733
2734 enum dw_line_info_opcode {
2735   /* Emit DW_LNE_set_address; the operand is the label index.  */
2736   LI_set_address,
2737
2738   /* Emit a row to the matrix with the given line.  This may be done
2739      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2740      special opcodes.  */
2741   LI_set_line,
2742
2743   /* Emit a DW_LNS_set_file.  */
2744   LI_set_file,
2745
2746   /* Emit a DW_LNS_set_column.  */
2747   LI_set_column,
2748
2749   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2750   LI_negate_stmt,
2751
2752   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2753   LI_set_prologue_end,
2754   LI_set_epilogue_begin,
2755
2756   /* Emit a DW_LNE_set_discriminator.  */
2757   LI_set_discriminator
2758 };
2759
2760 typedef struct GTY(()) dw_line_info_struct {
2761   enum dw_line_info_opcode opcode;
2762   unsigned int val;
2763 } dw_line_info_entry;
2764
2765 DEF_VEC_O(dw_line_info_entry);
2766 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2767
2768 typedef struct GTY(()) dw_line_info_table_struct {
2769   /* The label that marks the end of this section.  */
2770   const char *end_label;
2771
2772   /* The values for the last row of the matrix, as collected in the table.
2773      These are used to minimize the changes to the next row.  */
2774   unsigned int file_num;
2775   unsigned int line_num;
2776   unsigned int column_num;
2777   int discrim_num;
2778   bool is_stmt;
2779   bool in_use;
2780
2781   VEC(dw_line_info_entry, gc) *entries;
2782 } dw_line_info_table;
2783
2784 typedef dw_line_info_table *dw_line_info_table_p;
2785
2786 DEF_VEC_P(dw_line_info_table_p);
2787 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2788
2789 /* Each DIE attribute has a field specifying the attribute kind,
2790    a link to the next attribute in the chain, and an attribute value.
2791    Attributes are typically linked below the DIE they modify.  */
2792
2793 typedef struct GTY(()) dw_attr_struct {
2794   enum dwarf_attribute dw_attr;
2795   dw_val_node dw_attr_val;
2796 }
2797 dw_attr_node;
2798
2799 DEF_VEC_O(dw_attr_node);
2800 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2801
2802 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2803    The children of each node form a circular list linked by
2804    die_sib.  die_child points to the node *before* the "first" child node.  */
2805
2806 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2807   union die_symbol_or_type_node
2808     {
2809       char * GTY ((tag ("0"))) die_symbol;
2810       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2811     }
2812   GTY ((desc ("use_debug_types"))) die_id;
2813   VEC(dw_attr_node,gc) * die_attr;
2814   dw_die_ref die_parent;
2815   dw_die_ref die_child;
2816   dw_die_ref die_sib;
2817   dw_die_ref die_definition; /* ref from a specification to its definition */
2818   dw_offset die_offset;
2819   unsigned long die_abbrev;
2820   int die_mark;
2821   /* Die is used and must not be pruned as unused.  */
2822   int die_perennial_p;
2823   unsigned int decl_id;
2824   enum dwarf_tag die_tag;
2825 }
2826 die_node;
2827
2828 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2829 #define FOR_EACH_CHILD(die, c, expr) do {       \
2830   c = die->die_child;                           \
2831   if (c) do {                                   \
2832     c = c->die_sib;                             \
2833     expr;                                       \
2834   } while (c != die->die_child);                \
2835 } while (0)
2836
2837 /* The pubname structure */
2838
2839 typedef struct GTY(()) pubname_struct {
2840   dw_die_ref die;
2841   const char *name;
2842 }
2843 pubname_entry;
2844
2845 DEF_VEC_O(pubname_entry);
2846 DEF_VEC_ALLOC_O(pubname_entry, gc);
2847
2848 struct GTY(()) dw_ranges_struct {
2849   /* If this is positive, it's a block number, otherwise it's a
2850      bitwise-negated index into dw_ranges_by_label.  */
2851   int num;
2852 };
2853
2854 /* A structure to hold a macinfo entry.  */
2855
2856 typedef struct GTY(()) macinfo_struct {
2857   unsigned char code;
2858   unsigned HOST_WIDE_INT lineno;
2859   const char *info;
2860 }
2861 macinfo_entry;
2862
2863 DEF_VEC_O(macinfo_entry);
2864 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2865
2866 struct GTY(()) dw_ranges_by_label_struct {
2867   const char *begin;
2868   const char *end;
2869 };
2870
2871 /* The comdat type node structure.  */
2872 typedef struct GTY(()) comdat_type_struct
2873 {
2874   dw_die_ref root_die;
2875   dw_die_ref type_die;
2876   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2877   struct comdat_type_struct *next;
2878 }
2879 comdat_type_node;
2880
2881 /* The limbo die list structure.  */
2882 typedef struct GTY(()) limbo_die_struct {
2883   dw_die_ref die;
2884   tree created_for;
2885   struct limbo_die_struct *next;
2886 }
2887 limbo_die_node;
2888
2889 typedef struct skeleton_chain_struct
2890 {
2891   dw_die_ref old_die;
2892   dw_die_ref new_die;
2893   struct skeleton_chain_struct *parent;
2894 }
2895 skeleton_chain_node;
2896
2897 /* Define a macro which returns nonzero for a TYPE_DECL which was
2898    implicitly generated for a type.
2899
2900    Note that, unlike the C front-end (which generates a NULL named
2901    TYPE_DECL node for each complete tagged type, each array type,
2902    and each function type node created) the C++ front-end generates
2903    a _named_ TYPE_DECL node for each tagged type node created.
2904    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2905    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2906    front-end, but for each type, tagged or not.  */
2907
2908 #define TYPE_DECL_IS_STUB(decl)                         \
2909   (DECL_NAME (decl) == NULL_TREE                        \
2910    || (DECL_ARTIFICIAL (decl)                           \
2911        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2912            /* This is necessary for stub decls that     \
2913               appear in nested inline functions.  */    \
2914            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2915                && (decl_ultimate_origin (decl)          \
2916                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2917
2918 /* Information concerning the compilation unit's programming
2919    language, and compiler version.  */
2920
2921 /* Fixed size portion of the DWARF compilation unit header.  */
2922 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2923   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2924
2925 /* Fixed size portion of the DWARF comdat type unit header.  */
2926 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2927   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2928    + DWARF_OFFSET_SIZE)
2929
2930 /* Fixed size portion of public names info.  */
2931 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2932
2933 /* Fixed size portion of the address range info.  */
2934 #define DWARF_ARANGES_HEADER_SIZE                                       \
2935   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2936                 DWARF2_ADDR_SIZE * 2)                                   \
2937    - DWARF_INITIAL_LENGTH_SIZE)
2938
2939 /* Size of padding portion in the address range info.  It must be
2940    aligned to twice the pointer size.  */
2941 #define DWARF_ARANGES_PAD_SIZE \
2942   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2943                 DWARF2_ADDR_SIZE * 2)                              \
2944    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2945
2946 /* Use assembler line directives if available.  */
2947 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2948 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2949 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2950 #else
2951 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2952 #endif
2953 #endif
2954
2955 /* Minimum line offset in a special line info. opcode.
2956    This value was chosen to give a reasonable range of values.  */
2957 #define DWARF_LINE_BASE  -10
2958
2959 /* First special line opcode - leave room for the standard opcodes.  */
2960 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2961
2962 /* Range of line offsets in a special line info. opcode.  */
2963 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2964
2965 /* Flag that indicates the initial value of the is_stmt_start flag.
2966    In the present implementation, we do not mark any lines as
2967    the beginning of a source statement, because that information
2968    is not made available by the GCC front-end.  */
2969 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2970
2971 /* Maximum number of operations per instruction bundle.  */
2972 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2973 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2974 #endif
2975
2976 /* This location is used by calc_die_sizes() to keep track
2977    the offset of each DIE within the .debug_info section.  */
2978 static unsigned long next_die_offset;
2979
2980 /* Record the root of the DIE's built for the current compilation unit.  */
2981 static GTY(()) dw_die_ref single_comp_unit_die;
2982
2983 /* A list of type DIEs that have been separated into comdat sections.  */
2984 static GTY(()) comdat_type_node *comdat_type_list;
2985
2986 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2987 static GTY(()) limbo_die_node *limbo_die_list;
2988
2989 /* A list of DIEs for which we may have to generate
2990    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2991 static GTY(()) limbo_die_node *deferred_asm_name;
2992
2993 /* Filenames referenced by this compilation unit.  */
2994 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2995
2996 /* A hash table of references to DIE's that describe declarations.
2997    The key is a DECL_UID() which is a unique number identifying each decl.  */
2998 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2999
3000 /* A hash table of references to DIE's that describe COMMON blocks.
3001    The key is DECL_UID() ^ die_parent.  */
3002 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3003
3004 typedef struct GTY(()) die_arg_entry_struct {
3005     dw_die_ref die;
3006     tree arg;
3007 } die_arg_entry;
3008
3009 DEF_VEC_O(die_arg_entry);
3010 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3011
3012 /* Node of the variable location list.  */
3013 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3014   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3015      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3016      in mode of the EXPR_LIST node and first EXPR_LIST operand
3017      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3018      location or NULL for padding.  For larger bitsizes,
3019      mode is 0 and first operand is a CONCAT with bitsize
3020      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3021      NULL as second operand.  */
3022   rtx GTY (()) loc;
3023   const char * GTY (()) label;
3024   struct var_loc_node * GTY (()) next;
3025 };
3026
3027 /* Variable location list.  */
3028 struct GTY (()) var_loc_list_def {
3029   struct var_loc_node * GTY (()) first;
3030
3031   /* Pointer to the last but one or last element of the
3032      chained list.  If the list is empty, both first and
3033      last are NULL, if the list contains just one node
3034      or the last node certainly is not redundant, it points
3035      to the last node, otherwise points to the last but one.
3036      Do not mark it for GC because it is marked through the chain.  */
3037   struct var_loc_node * GTY ((skip ("%h"))) last;
3038
3039   /* Pointer to the last element before section switch,
3040      if NULL, either sections weren't switched or first
3041      is after section switch.  */
3042   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3043
3044   /* DECL_UID of the variable decl.  */
3045   unsigned int decl_id;
3046 };
3047 typedef struct var_loc_list_def var_loc_list;
3048
3049 /* Call argument location list.  */
3050 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3051   rtx GTY (()) call_arg_loc_note;
3052   const char * GTY (()) label;
3053   tree GTY (()) block;
3054   bool tail_call_p;
3055   rtx GTY (()) symbol_ref;
3056   struct call_arg_loc_node * GTY (()) next;
3057 };
3058
3059
3060 /* Table of decl location linked lists.  */
3061 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3062
3063 /* Head and tail of call_arg_loc chain.  */
3064 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3065 static struct call_arg_loc_node *call_arg_loc_last;
3066
3067 /* Number of call sites in the current function.  */
3068 static int call_site_count = -1;
3069 /* Number of tail call sites in the current function.  */
3070 static int tail_call_site_count = -1;
3071
3072 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3073    DIEs.  */
3074 static VEC (dw_die_ref, heap) *block_map;
3075
3076 /* A cached location list.  */
3077 struct GTY (()) cached_dw_loc_list_def {
3078   /* The DECL_UID of the decl that this entry describes.  */
3079   unsigned int decl_id;
3080
3081   /* The cached location list.  */
3082   dw_loc_list_ref loc_list;
3083 };
3084 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3085
3086 /* Table of cached location lists.  */
3087 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3088
3089 /* A pointer to the base of a list of references to DIE's that
3090    are uniquely identified by their tag, presence/absence of
3091    children DIE's, and list of attribute/value pairs.  */
3092 static GTY((length ("abbrev_die_table_allocated")))
3093   dw_die_ref *abbrev_die_table;
3094
3095 /* Number of elements currently allocated for abbrev_die_table.  */
3096 static GTY(()) unsigned abbrev_die_table_allocated;
3097
3098 /* Number of elements in type_die_table currently in use.  */
3099 static GTY(()) unsigned abbrev_die_table_in_use;
3100
3101 /* Size (in elements) of increments by which we may expand the
3102    abbrev_die_table.  */
3103 #define ABBREV_DIE_TABLE_INCREMENT 256
3104
3105 /* A global counter for generating labels for line number data.  */
3106 static unsigned int line_info_label_num;
3107
3108 /* The current table to which we should emit line number information
3109    for the current function.  This will be set up at the beginning of
3110    assembly for the function.  */
3111 static dw_line_info_table *cur_line_info_table;
3112
3113 /* The two default tables of line number info.  */
3114 static GTY(()) dw_line_info_table *text_section_line_info;
3115 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3116
3117 /* The set of all non-default tables of line number info.  */
3118 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3119
3120 /* A flag to tell pubnames/types export if there is an info section to
3121    refer to.  */
3122 static bool info_section_emitted;
3123
3124 /* A pointer to the base of a table that contains a list of publicly
3125    accessible names.  */
3126 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3127
3128 /* A pointer to the base of a table that contains a list of publicly
3129    accessible types.  */
3130 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3131
3132 /* A pointer to the base of a table that contains a list of macro
3133    defines/undefines (and file start/end markers).  */
3134 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3135
3136 /* Array of dies for which we should generate .debug_ranges info.  */
3137 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3138
3139 /* Number of elements currently allocated for ranges_table.  */
3140 static GTY(()) unsigned ranges_table_allocated;
3141
3142 /* Number of elements in ranges_table currently in use.  */
3143 static GTY(()) unsigned ranges_table_in_use;
3144
3145 /* Array of pairs of labels referenced in ranges_table.  */
3146 static GTY ((length ("ranges_by_label_allocated")))
3147      dw_ranges_by_label_ref ranges_by_label;
3148
3149 /* Number of elements currently allocated for ranges_by_label.  */
3150 static GTY(()) unsigned ranges_by_label_allocated;
3151
3152 /* Number of elements in ranges_by_label currently in use.  */
3153 static GTY(()) unsigned ranges_by_label_in_use;
3154
3155 /* Size (in elements) of increments by which we may expand the
3156    ranges_table.  */
3157 #define RANGES_TABLE_INCREMENT 64
3158
3159 /* Whether we have location lists that need outputting */
3160 static GTY(()) bool have_location_lists;
3161
3162 /* Unique label counter.  */
3163 static GTY(()) unsigned int loclabel_num;
3164
3165 /* Unique label counter for point-of-call tables.  */
3166 static GTY(()) unsigned int poc_label_num;
3167
3168 /* Record whether the function being analyzed contains inlined functions.  */
3169 static int current_function_has_inlines;
3170
3171 /* The last file entry emitted by maybe_emit_file().  */
3172 static GTY(()) struct dwarf_file_data * last_emitted_file;
3173
3174 /* Number of internal labels generated by gen_internal_sym().  */
3175 static GTY(()) int label_num;
3176
3177 /* Cached result of previous call to lookup_filename.  */
3178 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3179
3180 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3181
3182 /* Instances of generic types for which we need to generate debug
3183    info that describe their generic parameters and arguments. That
3184    generation needs to happen once all types are properly laid out so
3185    we do it at the end of compilation.  */
3186 static GTY(()) VEC(tree,gc) *generic_type_instances;
3187
3188 /* Offset from the "steady-state frame pointer" to the frame base,
3189    within the current function.  */
3190 static HOST_WIDE_INT frame_pointer_fb_offset;
3191 static bool frame_pointer_fb_offset_valid;
3192
3193 static VEC (dw_die_ref, heap) *base_types;
3194
3195 /* Forward declarations for functions defined in this file.  */
3196
3197 static int is_pseudo_reg (const_rtx);
3198 static tree type_main_variant (tree);
3199 static int is_tagged_type (const_tree);
3200 static const char *dwarf_tag_name (unsigned);
3201 static const char *dwarf_attr_name (unsigned);
3202 static const char *dwarf_form_name (unsigned);
3203 static tree decl_ultimate_origin (const_tree);
3204 static tree decl_class_context (tree);
3205 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3206 static inline enum dw_val_class AT_class (dw_attr_ref);
3207 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3208 static inline unsigned AT_flag (dw_attr_ref);
3209 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3210 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3211 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3212 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3213 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3214                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3215 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3216                                unsigned int, unsigned char *);
3217 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3218 static hashval_t debug_str_do_hash (const void *);
3219 static int debug_str_eq (const void *, const void *);
3220 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3221 static inline const char *AT_string (dw_attr_ref);
3222 static enum dwarf_form AT_string_form (dw_attr_ref);
3223 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3224 static void add_AT_specification (dw_die_ref, dw_die_ref);
3225 static inline dw_die_ref AT_ref (dw_attr_ref);
3226 static inline int AT_ref_external (dw_attr_ref);
3227 static inline void set_AT_ref_external (dw_attr_ref, int);
3228 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3229 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3230 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3231 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3232                              dw_loc_list_ref);
3233 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3234 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3235 static inline rtx AT_addr (dw_attr_ref);
3236 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3237 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3238 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3239 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3240                            unsigned HOST_WIDE_INT);
3241 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3242                                unsigned long);
3243 static inline const char *AT_lbl (dw_attr_ref);
3244 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3245 static const char *get_AT_low_pc (dw_die_ref);
3246 static const char *get_AT_hi_pc (dw_die_ref);
3247 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3248 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3249 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3250 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3251 static bool is_cxx (void);
3252 static bool is_fortran (void);
3253 static bool is_ada (void);
3254 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3255 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3256 static void add_child_die (dw_die_ref, dw_die_ref);
3257 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3258 static dw_die_ref lookup_type_die (tree);
3259 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3260 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3261 static void equate_type_number_to_die (tree, dw_die_ref);
3262 static hashval_t decl_die_table_hash (const void *);
3263 static int decl_die_table_eq (const void *, const void *);
3264 static dw_die_ref lookup_decl_die (tree);
3265 static hashval_t common_block_die_table_hash (const void *);
3266 static int common_block_die_table_eq (const void *, const void *);
3267 static hashval_t decl_loc_table_hash (const void *);
3268 static int decl_loc_table_eq (const void *, const void *);
3269 static var_loc_list *lookup_decl_loc (const_tree);
3270 static void equate_decl_number_to_die (tree, dw_die_ref);
3271 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3272 static void print_spaces (FILE *);
3273 static void print_die (dw_die_ref, FILE *);
3274 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3275 static dw_die_ref pop_compile_unit (dw_die_ref);
3276 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3277 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3278 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3279 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3280 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3281 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3282 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3283                                    struct md5_ctx *, int *);
3284 struct checksum_attributes;
3285 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3286 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3287 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3288 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3289 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3290 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3291 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3292 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3293 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3294 static void compute_section_prefix (dw_die_ref);
3295 static int is_type_die (dw_die_ref);
3296 static int is_comdat_die (dw_die_ref);
3297 static int is_symbol_die (dw_die_ref);
3298 static void assign_symbol_names (dw_die_ref);
3299 static void break_out_includes (dw_die_ref);
3300 static int is_declaration_die (dw_die_ref);
3301 static int should_move_die_to_comdat (dw_die_ref);
3302 static dw_die_ref clone_as_declaration (dw_die_ref);
3303 static dw_die_ref clone_die (dw_die_ref);
3304 static dw_die_ref clone_tree (dw_die_ref);
3305 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3306 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3307 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3308 static dw_die_ref generate_skeleton (dw_die_ref);
3309 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3310                                                          dw_die_ref);
3311 static void break_out_comdat_types (dw_die_ref);
3312 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3313 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3314 static void copy_decls_for_unworthy_types (dw_die_ref);
3315
3316 static hashval_t htab_cu_hash (const void *);
3317 static int htab_cu_eq (const void *, const void *);
3318 static void htab_cu_del (void *);
3319 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3320 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3321 static void add_sibling_attributes (dw_die_ref);
3322 static void build_abbrev_table (dw_die_ref);
3323 static void output_location_lists (dw_die_ref);
3324 static int constant_size (unsigned HOST_WIDE_INT);
3325 static unsigned long size_of_die (dw_die_ref);
3326 static void calc_die_sizes (dw_die_ref);
3327 static void calc_base_type_die_sizes (void);
3328 static void mark_dies (dw_die_ref);
3329 static void unmark_dies (dw_die_ref);
3330 static void unmark_all_dies (dw_die_ref);
3331 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3332 static unsigned long size_of_aranges (void);
3333 static enum dwarf_form value_format (dw_attr_ref);
3334 static void output_value_format (dw_attr_ref);
3335 static void output_abbrev_section (void);
3336 static void output_die_symbol (dw_die_ref);
3337 static void output_die (dw_die_ref);
3338 static void output_compilation_unit_header (void);
3339 static void output_comp_unit (dw_die_ref, int);
3340 static void output_comdat_type_unit (comdat_type_node *);
3341 static const char *dwarf2_name (tree, int);
3342 static void add_pubname (tree, dw_die_ref);
3343 static void add_pubname_string (const char *, dw_die_ref);
3344 static void add_pubtype (tree, dw_die_ref);
3345 static void output_pubnames (VEC (pubname_entry,gc) *);
3346 static void output_aranges (unsigned long);
3347 static unsigned int add_ranges_num (int);
3348 static unsigned int add_ranges (const_tree);
3349 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3350                                   bool *);
3351 static void output_ranges (void);
3352 static dw_line_info_table *new_line_info_table (void);
3353 static void output_line_info (void);
3354 static void output_file_names (void);
3355 static dw_die_ref base_type_die (tree);
3356 static int is_base_type (tree);
3357 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3358 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3359 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3360 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3361 static int type_is_enum (const_tree);
3362 static unsigned int dbx_reg_number (const_rtx);
3363 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3364 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3365 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3366                                                 enum var_init_status);
3367 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3368                                                      enum var_init_status);
3369 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3370                                          enum var_init_status);
3371 static int is_based_loc (const_rtx);
3372 static int resolve_one_addr (rtx *, void *);
3373 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3374                                                enum var_init_status);
3375 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3376                                         enum var_init_status);
3377 static dw_loc_list_ref loc_list_from_tree (tree, int);
3378 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3379 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3380 static tree field_type (const_tree);
3381 static unsigned int simple_type_align_in_bits (const_tree);
3382 static unsigned int simple_decl_align_in_bits (const_tree);
3383 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3384 static HOST_WIDE_INT field_byte_offset (const_tree);
3385 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3386                                          dw_loc_list_ref);
3387 static void add_data_member_location_attribute (dw_die_ref, tree);
3388 static bool add_const_value_attribute (dw_die_ref, rtx);
3389 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3390 static void insert_double (double_int, unsigned char *);
3391 static void insert_float (const_rtx, unsigned char *);
3392 static rtx rtl_for_decl_location (tree);
3393 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3394                                                    enum dwarf_attribute);
3395 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3396 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3397 static void add_name_attribute (dw_die_ref, const char *);
3398 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3399 static void add_comp_dir_attribute (dw_die_ref);
3400 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3401 static void add_subscript_info (dw_die_ref, tree, bool);
3402 static void add_byte_size_attribute (dw_die_ref, tree);
3403 static void add_bit_offset_attribute (dw_die_ref, tree);
3404 static void add_bit_size_attribute (dw_die_ref, tree);
3405 static void add_prototyped_attribute (dw_die_ref, tree);
3406 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3407 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3408 static void add_src_coords_attributes (dw_die_ref, tree);
3409 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3410 static void push_decl_scope (tree);
3411 static void pop_decl_scope (void);
3412 static dw_die_ref scope_die_for (tree, dw_die_ref);
3413 static inline int local_scope_p (dw_die_ref);
3414 static inline int class_scope_p (dw_die_ref);
3415 static inline int class_or_namespace_scope_p (dw_die_ref);
3416 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3417 static void add_calling_convention_attribute (dw_die_ref, tree);
3418 static const char *type_tag (const_tree);
3419 static tree member_declared_type (const_tree);
3420 #if 0
3421 static const char *decl_start_label (tree);
3422 #endif
3423 static void gen_array_type_die (tree, dw_die_ref);
3424 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3425 #if 0
3426 static void gen_entry_point_die (tree, dw_die_ref);
3427 #endif
3428 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3429 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3430 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3431 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3432 static void gen_formal_types_die (tree, dw_die_ref);
3433 static void gen_subprogram_die (tree, dw_die_ref);
3434 static void gen_variable_die (tree, tree, dw_die_ref);
3435 static void gen_const_die (tree, dw_die_ref);
3436 static void gen_label_die (tree, dw_die_ref);
3437 static void gen_lexical_block_die (tree, dw_die_ref, int);
3438 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3439 static void gen_field_die (tree, dw_die_ref);
3440 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3441 static dw_die_ref gen_compile_unit_die (const char *);
3442 static void gen_inheritance_die (tree, tree, dw_die_ref);
3443 static void gen_member_die (tree, dw_die_ref);
3444 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3445                                                 enum debug_info_usage);
3446 static void gen_subroutine_type_die (tree, dw_die_ref);
3447 static void gen_typedef_die (tree, dw_die_ref);
3448 static void gen_type_die (tree, dw_die_ref);
3449 static void gen_block_die (tree, dw_die_ref, int);
3450 static void decls_for_scope (tree, dw_die_ref, int);
3451 static inline int is_redundant_typedef (const_tree);
3452 static bool is_naming_typedef_decl (const_tree);
3453 static inline dw_die_ref get_context_die (tree);
3454 static void gen_namespace_die (tree, dw_die_ref);
3455 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3456 static dw_die_ref force_decl_die (tree);
3457 static dw_die_ref force_type_die (tree);
3458 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3459 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3460 static struct dwarf_file_data * lookup_filename (const char *);
3461 static void retry_incomplete_types (void);
3462 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3463 static void gen_generic_params_dies (tree);
3464 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3465 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3466 static void splice_child_die (dw_die_ref, dw_die_ref);
3467 static int file_info_cmp (const void *, const void *);
3468 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3469                                      const char *, const char *);
3470 static void output_loc_list (dw_loc_list_ref);
3471 static char *gen_internal_sym (const char *);
3472
3473 static void prune_unmark_dies (dw_die_ref);
3474 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3475 static void prune_unused_types_mark (dw_die_ref, int);
3476 static void prune_unused_types_walk (dw_die_ref);
3477 static void prune_unused_types_walk_attribs (dw_die_ref);
3478 static void prune_unused_types_prune (dw_die_ref);
3479 static void prune_unused_types (void);
3480 static int maybe_emit_file (struct dwarf_file_data *fd);
3481 static inline const char *AT_vms_delta1 (dw_attr_ref);
3482 static inline const char *AT_vms_delta2 (dw_attr_ref);
3483 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3484                                      const char *, const char *);
3485 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3486 static void gen_remaining_tmpl_value_param_die_attribute (void);
3487 static bool generic_type_p (tree);
3488 static void schedule_generic_params_dies_gen (tree t);
3489 static void gen_scheduled_generic_parms_dies (void);
3490
3491 /* Section names used to hold DWARF debugging information.  */
3492 #ifndef DEBUG_INFO_SECTION
3493 #define DEBUG_INFO_SECTION      ".debug_info"
3494 #endif
3495 #ifndef DEBUG_ABBREV_SECTION
3496 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_ARANGES_SECTION
3499 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3500 #endif
3501 #ifndef DEBUG_MACINFO_SECTION
3502 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3503 #endif
3504 #ifndef DEBUG_MACRO_SECTION
3505 #define DEBUG_MACRO_SECTION     ".debug_macro"
3506 #endif
3507 #ifndef DEBUG_LINE_SECTION
3508 #define DEBUG_LINE_SECTION      ".debug_line"
3509 #endif
3510 #ifndef DEBUG_LOC_SECTION
3511 #define DEBUG_LOC_SECTION       ".debug_loc"
3512 #endif
3513 #ifndef DEBUG_PUBNAMES_SECTION
3514 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3515 #endif
3516 #ifndef DEBUG_PUBTYPES_SECTION
3517 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3518 #endif
3519 #ifndef DEBUG_STR_SECTION
3520 #define DEBUG_STR_SECTION       ".debug_str"
3521 #endif
3522 #ifndef DEBUG_RANGES_SECTION
3523 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3524 #endif
3525
3526 /* Standard ELF section names for compiled code and data.  */
3527 #ifndef TEXT_SECTION_NAME
3528 #define TEXT_SECTION_NAME       ".text"
3529 #endif
3530
3531 /* Section flags for .debug_str section.  */
3532 #define DEBUG_STR_SECTION_FLAGS \
3533   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3534    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3535    : SECTION_DEBUG)
3536
3537 /* Labels we insert at beginning sections we can reference instead of
3538    the section names themselves.  */
3539
3540 #ifndef TEXT_SECTION_LABEL
3541 #define TEXT_SECTION_LABEL              "Ltext"
3542 #endif
3543 #ifndef COLD_TEXT_SECTION_LABEL
3544 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3545 #endif
3546 #ifndef DEBUG_LINE_SECTION_LABEL
3547 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3548 #endif
3549 #ifndef DEBUG_INFO_SECTION_LABEL
3550 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3551 #endif
3552 #ifndef DEBUG_ABBREV_SECTION_LABEL
3553 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3554 #endif
3555 #ifndef DEBUG_LOC_SECTION_LABEL
3556 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3557 #endif
3558 #ifndef DEBUG_RANGES_SECTION_LABEL
3559 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3560 #endif
3561 #ifndef DEBUG_MACINFO_SECTION_LABEL
3562 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3563 #endif
3564 #ifndef DEBUG_MACRO_SECTION_LABEL
3565 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3566 #endif
3567
3568
3569 /* Definitions of defaults for formats and names of various special
3570    (artificial) labels which may be generated within this file (when the -g
3571    options is used and DWARF2_DEBUGGING_INFO is in effect.
3572    If necessary, these may be overridden from within the tm.h file, but
3573    typically, overriding these defaults is unnecessary.  */
3574
3575 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3576 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3585
3586 #ifndef TEXT_END_LABEL
3587 #define TEXT_END_LABEL          "Letext"
3588 #endif
3589 #ifndef COLD_END_LABEL
3590 #define COLD_END_LABEL          "Letext_cold"
3591 #endif
3592 #ifndef BLOCK_BEGIN_LABEL
3593 #define BLOCK_BEGIN_LABEL       "LBB"
3594 #endif
3595 #ifndef BLOCK_END_LABEL
3596 #define BLOCK_END_LABEL         "LBE"
3597 #endif
3598 #ifndef LINE_CODE_LABEL
3599 #define LINE_CODE_LABEL         "LM"
3600 #endif
3601
3602 \f
3603 /* Return the root of the DIE's built for the current compilation unit.  */
3604 static dw_die_ref
3605 comp_unit_die (void)
3606 {
3607   if (!single_comp_unit_die)
3608     single_comp_unit_die = gen_compile_unit_die (NULL);
3609   return single_comp_unit_die;
3610 }
3611
3612 /* We allow a language front-end to designate a function that is to be
3613    called to "demangle" any name before it is put into a DIE.  */
3614
3615 static const char *(*demangle_name_func) (const char *);
3616
3617 void
3618 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3619 {
3620   demangle_name_func = func;
3621 }
3622
3623 /* Test if rtl node points to a pseudo register.  */
3624
3625 static inline int
3626 is_pseudo_reg (const_rtx rtl)
3627 {
3628   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3629           || (GET_CODE (rtl) == SUBREG
3630               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3631 }
3632
3633 /* Return a reference to a type, with its const and volatile qualifiers
3634    removed.  */
3635
3636 static inline tree
3637 type_main_variant (tree type)
3638 {
3639   type = TYPE_MAIN_VARIANT (type);
3640
3641   /* ??? There really should be only one main variant among any group of
3642      variants of a given type (and all of the MAIN_VARIANT values for all
3643      members of the group should point to that one type) but sometimes the C
3644      front-end messes this up for array types, so we work around that bug
3645      here.  */
3646   if (TREE_CODE (type) == ARRAY_TYPE)
3647     while (type != TYPE_MAIN_VARIANT (type))
3648       type = TYPE_MAIN_VARIANT (type);
3649
3650   return type;
3651 }
3652
3653 /* Return nonzero if the given type node represents a tagged type.  */
3654
3655 static inline int
3656 is_tagged_type (const_tree type)
3657 {
3658   enum tree_code code = TREE_CODE (type);
3659
3660   return (code == RECORD_TYPE || code == UNION_TYPE
3661           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3662 }
3663
3664 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3665
3666 static void
3667 get_ref_die_offset_label (char *label, dw_die_ref ref)
3668 {
3669   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3670 }
3671
3672 /* Return die_offset of a DIE reference to a base type.  */
3673
3674 static unsigned long int
3675 get_base_type_offset (dw_die_ref ref)
3676 {
3677   if (ref->die_offset)
3678     return ref->die_offset;
3679   if (comp_unit_die ()->die_abbrev)
3680     {
3681       calc_base_type_die_sizes ();
3682       gcc_assert (ref->die_offset);
3683     }
3684   return ref->die_offset;
3685 }
3686
3687 /* Return die_offset of a DIE reference other than base type.  */
3688
3689 static unsigned long int
3690 get_ref_die_offset (dw_die_ref ref)
3691 {
3692   gcc_assert (ref->die_offset);
3693   return ref->die_offset;
3694 }
3695
3696 /* Convert a DIE tag into its string name.  */
3697
3698 static const char *
3699 dwarf_tag_name (unsigned int tag)
3700 {
3701   switch (tag)
3702     {
3703     case DW_TAG_padding:
3704       return "DW_TAG_padding";
3705     case DW_TAG_array_type:
3706       return "DW_TAG_array_type";
3707     case DW_TAG_class_type:
3708       return "DW_TAG_class_type";
3709     case DW_TAG_entry_point:
3710       return "DW_TAG_entry_point";
3711     case DW_TAG_enumeration_type:
3712       return "DW_TAG_enumeration_type";
3713     case DW_TAG_formal_parameter:
3714       return "DW_TAG_formal_parameter";
3715     case DW_TAG_imported_declaration:
3716       return "DW_TAG_imported_declaration";
3717     case DW_TAG_label:
3718       return "DW_TAG_label";
3719     case DW_TAG_lexical_block:
3720       return "DW_TAG_lexical_block";
3721     case DW_TAG_member:
3722       return "DW_TAG_member";
3723     case DW_TAG_pointer_type:
3724       return "DW_TAG_pointer_type";
3725     case DW_TAG_reference_type:
3726       return "DW_TAG_reference_type";
3727     case DW_TAG_compile_unit:
3728       return "DW_TAG_compile_unit";
3729     case DW_TAG_string_type:
3730       return "DW_TAG_string_type";
3731     case DW_TAG_structure_type:
3732       return "DW_TAG_structure_type";
3733     case DW_TAG_subroutine_type:
3734       return "DW_TAG_subroutine_type";
3735     case DW_TAG_typedef:
3736       return "DW_TAG_typedef";
3737     case DW_TAG_union_type:
3738       return "DW_TAG_union_type";
3739     case DW_TAG_unspecified_parameters:
3740       return "DW_TAG_unspecified_parameters";
3741     case DW_TAG_variant:
3742       return "DW_TAG_variant";
3743     case DW_TAG_common_block:
3744       return "DW_TAG_common_block";
3745     case DW_TAG_common_inclusion:
3746       return "DW_TAG_common_inclusion";
3747     case DW_TAG_inheritance:
3748       return "DW_TAG_inheritance";
3749     case DW_TAG_inlined_subroutine:
3750       return "DW_TAG_inlined_subroutine";
3751     case DW_TAG_module:
3752       return "DW_TAG_module";
3753     case DW_TAG_ptr_to_member_type:
3754       return "DW_TAG_ptr_to_member_type";
3755     case DW_TAG_set_type:
3756       return "DW_TAG_set_type";
3757     case DW_TAG_subrange_type:
3758       return "DW_TAG_subrange_type";
3759     case DW_TAG_with_stmt:
3760       return "DW_TAG_with_stmt";
3761     case DW_TAG_access_declaration:
3762       return "DW_TAG_access_declaration";
3763     case DW_TAG_base_type:
3764       return "DW_TAG_base_type";
3765     case DW_TAG_catch_block:
3766       return "DW_TAG_catch_block";
3767     case DW_TAG_const_type:
3768       return "DW_TAG_const_type";
3769     case DW_TAG_constant:
3770       return "DW_TAG_constant";
3771     case DW_TAG_enumerator:
3772       return "DW_TAG_enumerator";
3773     case DW_TAG_file_type:
3774       return "DW_TAG_file_type";
3775     case DW_TAG_friend:
3776       return "DW_TAG_friend";
3777     case DW_TAG_namelist:
3778       return "DW_TAG_namelist";
3779     case DW_TAG_namelist_item:
3780       return "DW_TAG_namelist_item";
3781     case DW_TAG_packed_type:
3782       return "DW_TAG_packed_type";
3783     case DW_TAG_subprogram:
3784       return "DW_TAG_subprogram";
3785     case DW_TAG_template_type_param:
3786       return "DW_TAG_template_type_param";
3787     case DW_TAG_template_value_param:
3788       return "DW_TAG_template_value_param";
3789     case DW_TAG_thrown_type:
3790       return "DW_TAG_thrown_type";
3791     case DW_TAG_try_block:
3792       return "DW_TAG_try_block";
3793     case DW_TAG_variant_part:
3794       return "DW_TAG_variant_part";
3795     case DW_TAG_variable:
3796       return "DW_TAG_variable";
3797     case DW_TAG_volatile_type:
3798       return "DW_TAG_volatile_type";
3799     case DW_TAG_dwarf_procedure:
3800       return "DW_TAG_dwarf_procedure";
3801     case DW_TAG_restrict_type:
3802       return "DW_TAG_restrict_type";
3803     case DW_TAG_interface_type:
3804       return "DW_TAG_interface_type";
3805     case DW_TAG_namespace:
3806       return "DW_TAG_namespace";
3807     case DW_TAG_imported_module:
3808       return "DW_TAG_imported_module";
3809     case DW_TAG_unspecified_type:
3810       return "DW_TAG_unspecified_type";
3811     case DW_TAG_partial_unit:
3812       return "DW_TAG_partial_unit";
3813     case DW_TAG_imported_unit:
3814       return "DW_TAG_imported_unit";
3815     case DW_TAG_condition:
3816       return "DW_TAG_condition";
3817     case DW_TAG_shared_type:
3818       return "DW_TAG_shared_type";
3819     case DW_TAG_type_unit:
3820       return "DW_TAG_type_unit";
3821     case DW_TAG_rvalue_reference_type:
3822       return "DW_TAG_rvalue_reference_type";
3823     case DW_TAG_template_alias:
3824       return "DW_TAG_template_alias";
3825     case DW_TAG_GNU_template_parameter_pack:
3826       return "DW_TAG_GNU_template_parameter_pack";
3827     case DW_TAG_GNU_formal_parameter_pack:
3828       return "DW_TAG_GNU_formal_parameter_pack";
3829     case DW_TAG_MIPS_loop:
3830       return "DW_TAG_MIPS_loop";
3831     case DW_TAG_format_label:
3832       return "DW_TAG_format_label";
3833     case DW_TAG_function_template:
3834       return "DW_TAG_function_template";
3835     case DW_TAG_class_template:
3836       return "DW_TAG_class_template";
3837     case DW_TAG_GNU_BINCL:
3838       return "DW_TAG_GNU_BINCL";
3839     case DW_TAG_GNU_EINCL:
3840       return "DW_TAG_GNU_EINCL";
3841     case DW_TAG_GNU_template_template_param:
3842       return "DW_TAG_GNU_template_template_param";
3843     case DW_TAG_GNU_call_site:
3844       return "DW_TAG_GNU_call_site";
3845     case DW_TAG_GNU_call_site_parameter:
3846       return "DW_TAG_GNU_call_site_parameter";
3847     default:
3848       return "DW_TAG_<unknown>";
3849     }
3850 }
3851
3852 /* Convert a DWARF attribute code into its string name.  */
3853
3854 static const char *
3855 dwarf_attr_name (unsigned int attr)
3856 {
3857   switch (attr)
3858     {
3859     case DW_AT_sibling:
3860       return "DW_AT_sibling";
3861     case DW_AT_location:
3862       return "DW_AT_location";
3863     case DW_AT_name:
3864       return "DW_AT_name";
3865     case DW_AT_ordering:
3866       return "DW_AT_ordering";
3867     case DW_AT_subscr_data:
3868       return "DW_AT_subscr_data";
3869     case DW_AT_byte_size:
3870       return "DW_AT_byte_size";
3871     case DW_AT_bit_offset:
3872       return "DW_AT_bit_offset";
3873     case DW_AT_bit_size:
3874       return "DW_AT_bit_size";
3875     case DW_AT_element_list:
3876       return "DW_AT_element_list";
3877     case DW_AT_stmt_list:
3878       return "DW_AT_stmt_list";
3879     case DW_AT_low_pc:
3880       return "DW_AT_low_pc";
3881     case DW_AT_high_pc:
3882       return "DW_AT_high_pc";
3883     case DW_AT_language:
3884       return "DW_AT_language";
3885     case DW_AT_member:
3886       return "DW_AT_member";
3887     case DW_AT_discr:
3888       return "DW_AT_discr";
3889     case DW_AT_discr_value:
3890       return "DW_AT_discr_value";
3891     case DW_AT_visibility:
3892       return "DW_AT_visibility";
3893     case DW_AT_import:
3894       return "DW_AT_import";
3895     case DW_AT_string_length:
3896       return "DW_AT_string_length";
3897     case DW_AT_common_reference:
3898       return "DW_AT_common_reference";
3899     case DW_AT_comp_dir:
3900       return "DW_AT_comp_dir";
3901     case DW_AT_const_value:
3902       return "DW_AT_const_value";
3903     case DW_AT_containing_type:
3904       return "DW_AT_containing_type";
3905     case DW_AT_default_value:
3906       return "DW_AT_default_value";
3907     case DW_AT_inline:
3908       return "DW_AT_inline";
3909     case DW_AT_is_optional:
3910       return "DW_AT_is_optional";
3911     case DW_AT_lower_bound:
3912       return "DW_AT_lower_bound";
3913     case DW_AT_producer:
3914       return "DW_AT_producer";
3915     case DW_AT_prototyped:
3916       return "DW_AT_prototyped";
3917     case DW_AT_return_addr:
3918       return "DW_AT_return_addr";
3919     case DW_AT_start_scope:
3920       return "DW_AT_start_scope";
3921     case DW_AT_bit_stride:
3922       return "DW_AT_bit_stride";
3923     case DW_AT_upper_bound:
3924       return "DW_AT_upper_bound";
3925     case DW_AT_abstract_origin:
3926       return "DW_AT_abstract_origin";
3927     case DW_AT_accessibility:
3928       return "DW_AT_accessibility";
3929     case DW_AT_address_class:
3930       return "DW_AT_address_class";
3931     case DW_AT_artificial:
3932       return "DW_AT_artificial";
3933     case DW_AT_base_types:
3934       return "DW_AT_base_types";
3935     case DW_AT_calling_convention:
3936       return "DW_AT_calling_convention";
3937     case DW_AT_count:
3938       return "DW_AT_count";
3939     case DW_AT_data_member_location:
3940       return "DW_AT_data_member_location";
3941     case DW_AT_decl_column:
3942       return "DW_AT_decl_column";
3943     case DW_AT_decl_file:
3944       return "DW_AT_decl_file";
3945     case DW_AT_decl_line:
3946       return "DW_AT_decl_line";
3947     case DW_AT_declaration:
3948       return "DW_AT_declaration";
3949     case DW_AT_discr_list:
3950       return "DW_AT_discr_list";
3951     case DW_AT_encoding:
3952       return "DW_AT_encoding";
3953     case DW_AT_external:
3954       return "DW_AT_external";
3955     case DW_AT_explicit:
3956       return "DW_AT_explicit";
3957     case DW_AT_frame_base:
3958       return "DW_AT_frame_base";
3959     case DW_AT_friend:
3960       return "DW_AT_friend";
3961     case DW_AT_identifier_case:
3962       return "DW_AT_identifier_case";
3963     case DW_AT_macro_info:
3964       return "DW_AT_macro_info";
3965     case DW_AT_namelist_items:
3966       return "DW_AT_namelist_items";
3967     case DW_AT_priority:
3968       return "DW_AT_priority";
3969     case DW_AT_segment:
3970       return "DW_AT_segment";
3971     case DW_AT_specification:
3972       return "DW_AT_specification";
3973     case DW_AT_static_link:
3974       return "DW_AT_static_link";
3975     case DW_AT_type:
3976       return "DW_AT_type";
3977     case DW_AT_use_location:
3978       return "DW_AT_use_location";
3979     case DW_AT_variable_parameter:
3980       return "DW_AT_variable_parameter";
3981     case DW_AT_virtuality:
3982       return "DW_AT_virtuality";
3983     case DW_AT_vtable_elem_location:
3984       return "DW_AT_vtable_elem_location";
3985
3986     case DW_AT_allocated:
3987       return "DW_AT_allocated";
3988     case DW_AT_associated:
3989       return "DW_AT_associated";
3990     case DW_AT_data_location:
3991       return "DW_AT_data_location";
3992     case DW_AT_byte_stride:
3993       return "DW_AT_byte_stride";
3994     case DW_AT_entry_pc:
3995       return "DW_AT_entry_pc";
3996     case DW_AT_use_UTF8:
3997       return "DW_AT_use_UTF8";
3998     case DW_AT_extension:
3999       return "DW_AT_extension";
4000     case DW_AT_ranges:
4001       return "DW_AT_ranges";
4002     case DW_AT_trampoline:
4003       return "DW_AT_trampoline";
4004     case DW_AT_call_column:
4005       return "DW_AT_call_column";
4006     case DW_AT_call_file:
4007       return "DW_AT_call_file";
4008     case DW_AT_call_line:
4009       return "DW_AT_call_line";
4010     case DW_AT_object_pointer:
4011       return "DW_AT_object_pointer";
4012
4013     case DW_AT_signature:
4014       return "DW_AT_signature";
4015     case DW_AT_main_subprogram:
4016       return "DW_AT_main_subprogram";
4017     case DW_AT_data_bit_offset:
4018       return "DW_AT_data_bit_offset";
4019     case DW_AT_const_expr:
4020       return "DW_AT_const_expr";
4021     case DW_AT_enum_class:
4022       return "DW_AT_enum_class";
4023     case DW_AT_linkage_name:
4024       return "DW_AT_linkage_name";
4025
4026     case DW_AT_MIPS_fde:
4027       return "DW_AT_MIPS_fde";
4028     case DW_AT_MIPS_loop_begin:
4029       return "DW_AT_MIPS_loop_begin";
4030     case DW_AT_MIPS_tail_loop_begin:
4031       return "DW_AT_MIPS_tail_loop_begin";
4032     case DW_AT_MIPS_epilog_begin:
4033       return "DW_AT_MIPS_epilog_begin";
4034 #if VMS_DEBUGGING_INFO
4035     case DW_AT_HP_prologue:
4036       return "DW_AT_HP_prologue";
4037 #else
4038     case DW_AT_MIPS_loop_unroll_factor:
4039       return "DW_AT_MIPS_loop_unroll_factor";
4040 #endif
4041     case DW_AT_MIPS_software_pipeline_depth:
4042       return "DW_AT_MIPS_software_pipeline_depth";
4043     case DW_AT_MIPS_linkage_name:
4044       return "DW_AT_MIPS_linkage_name";
4045 #if VMS_DEBUGGING_INFO
4046     case DW_AT_HP_epilogue:
4047       return "DW_AT_HP_epilogue";
4048 #else
4049     case DW_AT_MIPS_stride:
4050       return "DW_AT_MIPS_stride";
4051 #endif
4052     case DW_AT_MIPS_abstract_name:
4053       return "DW_AT_MIPS_abstract_name";
4054     case DW_AT_MIPS_clone_origin:
4055       return "DW_AT_MIPS_clone_origin";
4056     case DW_AT_MIPS_has_inlines:
4057       return "DW_AT_MIPS_has_inlines";
4058
4059     case DW_AT_sf_names:
4060       return "DW_AT_sf_names";
4061     case DW_AT_src_info:
4062       return "DW_AT_src_info";
4063     case DW_AT_mac_info:
4064       return "DW_AT_mac_info";
4065     case DW_AT_src_coords:
4066       return "DW_AT_src_coords";
4067     case DW_AT_body_begin:
4068       return "DW_AT_body_begin";
4069     case DW_AT_body_end:
4070       return "DW_AT_body_end";
4071
4072     case DW_AT_GNU_vector:
4073       return "DW_AT_GNU_vector";
4074     case DW_AT_GNU_guarded_by:
4075       return "DW_AT_GNU_guarded_by";
4076     case DW_AT_GNU_pt_guarded_by:
4077       return "DW_AT_GNU_pt_guarded_by";
4078     case DW_AT_GNU_guarded:
4079       return "DW_AT_GNU_guarded";
4080     case DW_AT_GNU_pt_guarded:
4081       return "DW_AT_GNU_pt_guarded";
4082     case DW_AT_GNU_locks_excluded:
4083       return "DW_AT_GNU_locks_excluded";
4084     case DW_AT_GNU_exclusive_locks_required:
4085       return "DW_AT_GNU_exclusive_locks_required";
4086     case DW_AT_GNU_shared_locks_required:
4087       return "DW_AT_GNU_shared_locks_required";
4088     case DW_AT_GNU_odr_signature:
4089       return "DW_AT_GNU_odr_signature";
4090     case DW_AT_GNU_template_name:
4091       return "DW_AT_GNU_template_name";
4092     case DW_AT_GNU_call_site_value:
4093       return "DW_AT_GNU_call_site_value";
4094     case DW_AT_GNU_call_site_data_value:
4095       return "DW_AT_GNU_call_site_data_value";
4096     case DW_AT_GNU_call_site_target:
4097       return "DW_AT_GNU_call_site_target";
4098     case DW_AT_GNU_call_site_target_clobbered:
4099       return "DW_AT_GNU_call_site_target_clobbered";
4100     case DW_AT_GNU_tail_call:
4101       return "DW_AT_GNU_tail_call";
4102     case DW_AT_GNU_all_tail_call_sites:
4103       return "DW_AT_GNU_all_tail_call_sites";
4104     case DW_AT_GNU_all_call_sites:
4105       return "DW_AT_GNU_all_call_sites";
4106     case DW_AT_GNU_all_source_call_sites:
4107       return "DW_AT_GNU_all_source_call_sites";
4108     case DW_AT_GNU_macros:
4109       return "DW_AT_GNU_macros";
4110
4111     case DW_AT_GNAT_descriptive_type:
4112       return "DW_AT_GNAT_descriptive_type";
4113
4114     case DW_AT_VMS_rtnbeg_pd_address:
4115       return "DW_AT_VMS_rtnbeg_pd_address";
4116
4117     default:
4118       return "DW_AT_<unknown>";
4119     }
4120 }
4121
4122 /* Convert a DWARF value form code into its string name.  */
4123
4124 static const char *
4125 dwarf_form_name (unsigned int form)
4126 {
4127   switch (form)
4128     {
4129     case DW_FORM_addr:
4130       return "DW_FORM_addr";
4131     case DW_FORM_block2:
4132       return "DW_FORM_block2";
4133     case DW_FORM_block4:
4134       return "DW_FORM_block4";
4135     case DW_FORM_data2:
4136       return "DW_FORM_data2";
4137     case DW_FORM_data4:
4138       return "DW_FORM_data4";
4139     case DW_FORM_data8:
4140       return "DW_FORM_data8";
4141     case DW_FORM_string:
4142       return "DW_FORM_string";
4143     case DW_FORM_block:
4144       return "DW_FORM_block";
4145     case DW_FORM_block1:
4146       return "DW_FORM_block1";
4147     case DW_FORM_data1:
4148       return "DW_FORM_data1";
4149     case DW_FORM_flag:
4150       return "DW_FORM_flag";
4151     case DW_FORM_sdata:
4152       return "DW_FORM_sdata";
4153     case DW_FORM_strp:
4154       return "DW_FORM_strp";
4155     case DW_FORM_udata:
4156       return "DW_FORM_udata";
4157     case DW_FORM_ref_addr:
4158       return "DW_FORM_ref_addr";
4159     case DW_FORM_ref1:
4160       return "DW_FORM_ref1";
4161     case DW_FORM_ref2:
4162       return "DW_FORM_ref2";
4163     case DW_FORM_ref4:
4164       return "DW_FORM_ref4";
4165     case DW_FORM_ref8:
4166       return "DW_FORM_ref8";
4167     case DW_FORM_ref_udata:
4168       return "DW_FORM_ref_udata";
4169     case DW_FORM_indirect:
4170       return "DW_FORM_indirect";
4171     case DW_FORM_sec_offset:
4172       return "DW_FORM_sec_offset";
4173     case DW_FORM_exprloc:
4174       return "DW_FORM_exprloc";
4175     case DW_FORM_flag_present:
4176       return "DW_FORM_flag_present";
4177     case DW_FORM_ref_sig8:
4178       return "DW_FORM_ref_sig8";
4179     default:
4180       return "DW_FORM_<unknown>";
4181     }
4182 }
4183 \f
4184 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4185    instance of an inlined instance of a decl which is local to an inline
4186    function, so we have to trace all of the way back through the origin chain
4187    to find out what sort of node actually served as the original seed for the
4188    given block.  */
4189
4190 static tree
4191 decl_ultimate_origin (const_tree decl)
4192 {
4193   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4194     return NULL_TREE;
4195
4196   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4197      nodes in the function to point to themselves; ignore that if
4198      we're trying to output the abstract instance of this function.  */
4199   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4200     return NULL_TREE;
4201
4202   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4203      most distant ancestor, this should never happen.  */
4204   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4205
4206   return DECL_ABSTRACT_ORIGIN (decl);
4207 }
4208
4209 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4210    of a virtual function may refer to a base class, so we check the 'this'
4211    parameter.  */
4212
4213 static tree
4214 decl_class_context (tree decl)
4215 {
4216   tree context = NULL_TREE;
4217
4218   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4219     context = DECL_CONTEXT (decl);
4220   else
4221     context = TYPE_MAIN_VARIANT
4222       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4223
4224   if (context && !TYPE_P (context))
4225     context = NULL_TREE;
4226
4227   return context;
4228 }
4229 \f
4230 /* Add an attribute/value pair to a DIE.  */
4231
4232 static inline void
4233 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4234 {
4235   /* Maybe this should be an assert?  */
4236   if (die == NULL)
4237     return;
4238
4239   if (die->die_attr == NULL)
4240     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4241   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4242 }
4243
4244 static inline enum dw_val_class
4245 AT_class (dw_attr_ref a)
4246 {
4247   return a->dw_attr_val.val_class;
4248 }
4249
4250 /* Add a flag value attribute to a DIE.  */
4251
4252 static inline void
4253 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4254 {
4255   dw_attr_node attr;
4256
4257   attr.dw_attr = attr_kind;
4258   attr.dw_attr_val.val_class = dw_val_class_flag;
4259   attr.dw_attr_val.v.val_flag = flag;
4260   add_dwarf_attr (die, &attr);
4261 }
4262
4263 static inline unsigned
4264 AT_flag (dw_attr_ref a)
4265 {
4266   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4267   return a->dw_attr_val.v.val_flag;
4268 }
4269
4270 /* Add a signed integer attribute value to a DIE.  */
4271
4272 static inline void
4273 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4274 {
4275   dw_attr_node attr;
4276
4277   attr.dw_attr = attr_kind;
4278   attr.dw_attr_val.val_class = dw_val_class_const;
4279   attr.dw_attr_val.v.val_int = int_val;
4280   add_dwarf_attr (die, &attr);
4281 }
4282
4283 static inline HOST_WIDE_INT
4284 AT_int (dw_attr_ref a)
4285 {
4286   gcc_assert (a && AT_class (a) == dw_val_class_const);
4287   return a->dw_attr_val.v.val_int;
4288 }
4289
4290 /* Add an unsigned integer attribute value to a DIE.  */
4291
4292 static inline void
4293 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4294                  unsigned HOST_WIDE_INT unsigned_val)
4295 {
4296   dw_attr_node attr;
4297
4298   attr.dw_attr = attr_kind;
4299   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4300   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4301   add_dwarf_attr (die, &attr);
4302 }
4303
4304 static inline unsigned HOST_WIDE_INT
4305 AT_unsigned (dw_attr_ref a)
4306 {
4307   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4308   return a->dw_attr_val.v.val_unsigned;
4309 }
4310
4311 /* Add an unsigned double integer attribute value to a DIE.  */
4312
4313 static inline void
4314 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4315                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4316 {
4317   dw_attr_node attr;
4318
4319   attr.dw_attr = attr_kind;
4320   attr.dw_attr_val.val_class = dw_val_class_const_double;
4321   attr.dw_attr_val.v.val_double.high = high;
4322   attr.dw_attr_val.v.val_double.low = low;
4323   add_dwarf_attr (die, &attr);
4324 }
4325
4326 /* Add a floating point attribute value to a DIE and return it.  */
4327
4328 static inline void
4329 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4330             unsigned int length, unsigned int elt_size, unsigned char *array)
4331 {
4332   dw_attr_node attr;
4333
4334   attr.dw_attr = attr_kind;
4335   attr.dw_attr_val.val_class = dw_val_class_vec;
4336   attr.dw_attr_val.v.val_vec.length = length;
4337   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4338   attr.dw_attr_val.v.val_vec.array = array;
4339   add_dwarf_attr (die, &attr);
4340 }
4341
4342 /* Add an 8-byte data attribute value to a DIE.  */
4343
4344 static inline void
4345 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4346               unsigned char data8[8])
4347 {
4348   dw_attr_node attr;
4349
4350   attr.dw_attr = attr_kind;
4351   attr.dw_attr_val.val_class = dw_val_class_data8;
4352   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4353   add_dwarf_attr (die, &attr);
4354 }
4355
4356 /* Hash and equality functions for debug_str_hash.  */
4357
4358 static hashval_t
4359 debug_str_do_hash (const void *x)
4360 {
4361   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4362 }
4363
4364 static int
4365 debug_str_eq (const void *x1, const void *x2)
4366 {
4367   return strcmp ((((const struct indirect_string_node *)x1)->str),
4368                  (const char *)x2) == 0;
4369 }
4370
4371 /* Add STR to the indirect string hash table.  */
4372
4373 static struct indirect_string_node *
4374 find_AT_string (const char *str)
4375 {
4376   struct indirect_string_node *node;
4377   void **slot;
4378
4379   if (! debug_str_hash)
4380     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4381                                       debug_str_eq, NULL);
4382
4383   slot = htab_find_slot_with_hash (debug_str_hash, str,
4384                                    htab_hash_string (str), INSERT);
4385   if (*slot == NULL)
4386     {
4387       node = ggc_alloc_cleared_indirect_string_node ();
4388       node->str = ggc_strdup (str);
4389       *slot = node;
4390     }
4391   else
4392     node = (struct indirect_string_node *) *slot;
4393
4394   node->refcount++;
4395   return node;
4396 }
4397
4398 /* Add a string attribute value to a DIE.  */
4399
4400 static inline void
4401 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4402 {
4403   dw_attr_node attr;
4404   struct indirect_string_node *node;
4405
4406   node = find_AT_string (str);
4407
4408   attr.dw_attr = attr_kind;
4409   attr.dw_attr_val.val_class = dw_val_class_str;
4410   attr.dw_attr_val.v.val_str = node;
4411   add_dwarf_attr (die, &attr);
4412 }
4413
4414 static inline const char *
4415 AT_string (dw_attr_ref a)
4416 {
4417   gcc_assert (a && AT_class (a) == dw_val_class_str);
4418   return a->dw_attr_val.v.val_str->str;
4419 }
4420
4421 /* Find out whether a string should be output inline in DIE
4422    or out-of-line in .debug_str section.  */
4423
4424 static enum dwarf_form
4425 AT_string_form (dw_attr_ref a)
4426 {
4427   struct indirect_string_node *node;
4428   unsigned int len;
4429   char label[32];
4430
4431   gcc_assert (a && AT_class (a) == dw_val_class_str);
4432
4433   node = a->dw_attr_val.v.val_str;
4434   if (node->form)
4435     return node->form;
4436
4437   len = strlen (node->str) + 1;
4438
4439   /* If the string is shorter or equal to the size of the reference, it is
4440      always better to put it inline.  */
4441   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4442     return node->form = DW_FORM_string;
4443
4444   /* If we cannot expect the linker to merge strings in .debug_str
4445      section, only put it into .debug_str if it is worth even in this
4446      single module.  */
4447   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4448       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4449       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4450     return node->form = DW_FORM_string;
4451
4452   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4453   ++dw2_string_counter;
4454   node->label = xstrdup (label);
4455
4456   return node->form = DW_FORM_strp;
4457 }
4458
4459 /* Add a DIE reference attribute value to a DIE.  */
4460
4461 static inline void
4462 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4463 {
4464   dw_attr_node attr;
4465
4466 #ifdef ENABLE_CHECKING
4467   gcc_assert (targ_die != NULL);
4468 #else
4469   /* With LTO we can end up trying to reference something we didn't create
4470      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4471   if (targ_die == NULL)
4472     return;
4473 #endif
4474
4475   attr.dw_attr = attr_kind;
4476   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4477   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4478   attr.dw_attr_val.v.val_die_ref.external = 0;
4479   add_dwarf_attr (die, &attr);
4480 }
4481
4482 /* Add an AT_specification attribute to a DIE, and also make the back
4483    pointer from the specification to the definition.  */
4484
4485 static inline void
4486 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4487 {
4488   add_AT_die_ref (die, DW_AT_specification, targ_die);
4489   gcc_assert (!targ_die->die_definition);
4490   targ_die->die_definition = die;
4491 }
4492
4493 static inline dw_die_ref
4494 AT_ref (dw_attr_ref a)
4495 {
4496   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4497   return a->dw_attr_val.v.val_die_ref.die;
4498 }
4499
4500 static inline int
4501 AT_ref_external (dw_attr_ref a)
4502 {
4503   if (a && AT_class (a) == dw_val_class_die_ref)
4504     return a->dw_attr_val.v.val_die_ref.external;
4505
4506   return 0;
4507 }
4508
4509 static inline void
4510 set_AT_ref_external (dw_attr_ref a, int i)
4511 {
4512   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4513   a->dw_attr_val.v.val_die_ref.external = i;
4514 }
4515
4516 /* Add an FDE reference attribute value to a DIE.  */
4517
4518 static inline void
4519 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4520 {
4521   dw_attr_node attr;
4522
4523   attr.dw_attr = attr_kind;
4524   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4525   attr.dw_attr_val.v.val_fde_index = targ_fde;
4526   add_dwarf_attr (die, &attr);
4527 }
4528
4529 /* Add a location description attribute value to a DIE.  */
4530
4531 static inline void
4532 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4533 {
4534   dw_attr_node attr;
4535
4536   attr.dw_attr = attr_kind;
4537   attr.dw_attr_val.val_class = dw_val_class_loc;
4538   attr.dw_attr_val.v.val_loc = loc;
4539   add_dwarf_attr (die, &attr);
4540 }
4541
4542 static inline dw_loc_descr_ref
4543 AT_loc (dw_attr_ref a)
4544 {
4545   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4546   return a->dw_attr_val.v.val_loc;
4547 }
4548
4549 static inline void
4550 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4551 {
4552   dw_attr_node attr;
4553
4554   attr.dw_attr = attr_kind;
4555   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4556   attr.dw_attr_val.v.val_loc_list = loc_list;
4557   add_dwarf_attr (die, &attr);
4558   have_location_lists = true;
4559 }
4560
4561 static inline dw_loc_list_ref
4562 AT_loc_list (dw_attr_ref a)
4563 {
4564   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4565   return a->dw_attr_val.v.val_loc_list;
4566 }
4567
4568 static inline dw_loc_list_ref *
4569 AT_loc_list_ptr (dw_attr_ref a)
4570 {
4571   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4572   return &a->dw_attr_val.v.val_loc_list;
4573 }
4574
4575 /* Add an address constant attribute value to a DIE.  */
4576
4577 static inline void
4578 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4579 {
4580   dw_attr_node attr;
4581
4582   attr.dw_attr = attr_kind;
4583   attr.dw_attr_val.val_class = dw_val_class_addr;
4584   attr.dw_attr_val.v.val_addr = addr;
4585   add_dwarf_attr (die, &attr);
4586 }
4587
4588 /* Get the RTX from to an address DIE attribute.  */
4589
4590 static inline rtx
4591 AT_addr (dw_attr_ref a)
4592 {
4593   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4594   return a->dw_attr_val.v.val_addr;
4595 }
4596
4597 /* Add a file attribute value to a DIE.  */
4598
4599 static inline void
4600 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4601              struct dwarf_file_data *fd)
4602 {
4603   dw_attr_node attr;
4604
4605   attr.dw_attr = attr_kind;
4606   attr.dw_attr_val.val_class = dw_val_class_file;
4607   attr.dw_attr_val.v.val_file = fd;
4608   add_dwarf_attr (die, &attr);
4609 }
4610
4611 /* Get the dwarf_file_data from a file DIE attribute.  */
4612
4613 static inline struct dwarf_file_data *
4614 AT_file (dw_attr_ref a)
4615 {
4616   gcc_assert (a && AT_class (a) == dw_val_class_file);
4617   return a->dw_attr_val.v.val_file;
4618 }
4619
4620 /* Add a vms delta attribute value to a DIE.  */
4621
4622 static inline void
4623 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4624                   const char *lbl1, const char *lbl2)
4625 {
4626   dw_attr_node attr;
4627
4628   attr.dw_attr = attr_kind;
4629   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4630   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4631   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4632   add_dwarf_attr (die, &attr);
4633 }
4634
4635 /* Add a label identifier attribute value to a DIE.  */
4636
4637 static inline void
4638 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4639 {
4640   dw_attr_node attr;
4641
4642   attr.dw_attr = attr_kind;
4643   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4644   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4645   add_dwarf_attr (die, &attr);
4646 }
4647
4648 /* Add a section offset attribute value to a DIE, an offset into the
4649    debug_line section.  */
4650
4651 static inline void
4652 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4653                 const char *label)
4654 {
4655   dw_attr_node attr;
4656
4657   attr.dw_attr = attr_kind;
4658   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4659   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4660   add_dwarf_attr (die, &attr);
4661 }
4662
4663 /* Add a section offset attribute value to a DIE, an offset into the
4664    debug_macinfo section.  */
4665
4666 static inline void
4667 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4668                const char *label)
4669 {
4670   dw_attr_node attr;
4671
4672   attr.dw_attr = attr_kind;
4673   attr.dw_attr_val.val_class = dw_val_class_macptr;
4674   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4675   add_dwarf_attr (die, &attr);
4676 }
4677
4678 /* Add an offset attribute value to a DIE.  */
4679
4680 static inline void
4681 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4682                unsigned HOST_WIDE_INT offset)
4683 {
4684   dw_attr_node attr;
4685
4686   attr.dw_attr = attr_kind;
4687   attr.dw_attr_val.val_class = dw_val_class_offset;
4688   attr.dw_attr_val.v.val_offset = offset;
4689   add_dwarf_attr (die, &attr);
4690 }
4691
4692 /* Add an range_list attribute value to a DIE.  */
4693
4694 static void
4695 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4696                    long unsigned int offset)
4697 {
4698   dw_attr_node attr;
4699
4700   attr.dw_attr = attr_kind;
4701   attr.dw_attr_val.val_class = dw_val_class_range_list;
4702   attr.dw_attr_val.v.val_offset = offset;
4703   add_dwarf_attr (die, &attr);
4704 }
4705
4706 /* Return the start label of a delta attribute.  */
4707
4708 static inline const char *
4709 AT_vms_delta1 (dw_attr_ref a)
4710 {
4711   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4712   return a->dw_attr_val.v.val_vms_delta.lbl1;
4713 }
4714
4715 /* Return the end label of a delta attribute.  */
4716
4717 static inline const char *
4718 AT_vms_delta2 (dw_attr_ref a)
4719 {
4720   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4721   return a->dw_attr_val.v.val_vms_delta.lbl2;
4722 }
4723
4724 static inline const char *
4725 AT_lbl (dw_attr_ref a)
4726 {
4727   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4728                     || AT_class (a) == dw_val_class_lineptr
4729                     || AT_class (a) == dw_val_class_macptr));
4730   return a->dw_attr_val.v.val_lbl_id;
4731 }
4732
4733 /* Get the attribute of type attr_kind.  */
4734
4735 static dw_attr_ref
4736 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4737 {
4738   dw_attr_ref a;
4739   unsigned ix;
4740   dw_die_ref spec = NULL;
4741
4742   if (! die)
4743     return NULL;
4744
4745   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4746     if (a->dw_attr == attr_kind)
4747       return a;
4748     else if (a->dw_attr == DW_AT_specification
4749              || a->dw_attr == DW_AT_abstract_origin)
4750       spec = AT_ref (a);
4751
4752   if (spec)
4753     return get_AT (spec, attr_kind);
4754
4755   return NULL;
4756 }
4757
4758 /* Return the "low pc" attribute value, typically associated with a subprogram
4759    DIE.  Return null if the "low pc" attribute is either not present, or if it
4760    cannot be represented as an assembler label identifier.  */
4761
4762 static inline const char *
4763 get_AT_low_pc (dw_die_ref die)
4764 {
4765   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4766
4767   return a ? AT_lbl (a) : NULL;
4768 }
4769
4770 /* Return the "high pc" attribute value, typically associated with a subprogram
4771    DIE.  Return null if the "high pc" attribute is either not present, or if it
4772    cannot be represented as an assembler label identifier.  */
4773
4774 static inline const char *
4775 get_AT_hi_pc (dw_die_ref die)
4776 {
4777   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4778
4779   return a ? AT_lbl (a) : NULL;
4780 }
4781
4782 /* Return the value of the string attribute designated by ATTR_KIND, or
4783    NULL if it is not present.  */
4784
4785 static inline const char *
4786 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4787 {
4788   dw_attr_ref a = get_AT (die, attr_kind);
4789
4790   return a ? AT_string (a) : NULL;
4791 }
4792
4793 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4794    if it is not present.  */
4795
4796 static inline int
4797 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4798 {
4799   dw_attr_ref a = get_AT (die, attr_kind);
4800
4801   return a ? AT_flag (a) : 0;
4802 }
4803
4804 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4805    if it is not present.  */
4806
4807 static inline unsigned
4808 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4809 {
4810   dw_attr_ref a = get_AT (die, attr_kind);
4811
4812   return a ? AT_unsigned (a) : 0;
4813 }
4814
4815 static inline dw_die_ref
4816 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4817 {
4818   dw_attr_ref a = get_AT (die, attr_kind);
4819
4820   return a ? AT_ref (a) : NULL;
4821 }
4822
4823 static inline struct dwarf_file_data *
4824 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4825 {
4826   dw_attr_ref a = get_AT (die, attr_kind);
4827
4828   return a ? AT_file (a) : NULL;
4829 }
4830
4831 /* Return TRUE if the language is C++.  */
4832
4833 static inline bool
4834 is_cxx (void)
4835 {
4836   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4837
4838   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4839 }
4840
4841 /* Return TRUE if the language is Fortran.  */
4842
4843 static inline bool
4844 is_fortran (void)
4845 {
4846   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4847
4848   return (lang == DW_LANG_Fortran77
4849           || lang == DW_LANG_Fortran90
4850           || lang == DW_LANG_Fortran95);
4851 }
4852
4853 /* Return TRUE if the language is Ada.  */
4854
4855 static inline bool
4856 is_ada (void)
4857 {
4858   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4859
4860   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4861 }
4862
4863 /* Remove the specified attribute if present.  */
4864
4865 static void
4866 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4867 {
4868   dw_attr_ref a;
4869   unsigned ix;
4870
4871   if (! die)
4872     return;
4873
4874   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4875     if (a->dw_attr == attr_kind)
4876       {
4877         if (AT_class (a) == dw_val_class_str)
4878           if (a->dw_attr_val.v.val_str->refcount)
4879             a->dw_attr_val.v.val_str->refcount--;
4880
4881         /* VEC_ordered_remove should help reduce the number of abbrevs
4882            that are needed.  */
4883         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4884         return;
4885       }
4886 }
4887
4888 /* Remove CHILD from its parent.  PREV must have the property that
4889    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4890
4891 static void
4892 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4893 {
4894   gcc_assert (child->die_parent == prev->die_parent);
4895   gcc_assert (prev->die_sib == child);
4896   if (prev == child)
4897     {
4898       gcc_assert (child->die_parent->die_child == child);
4899       prev = NULL;
4900     }
4901   else
4902     prev->die_sib = child->die_sib;
4903   if (child->die_parent->die_child == child)
4904     child->die_parent->die_child = prev;
4905 }
4906
4907 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4908    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4909
4910 static void
4911 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4912 {
4913   dw_die_ref parent = old_child->die_parent;
4914
4915   gcc_assert (parent == prev->die_parent);
4916   gcc_assert (prev->die_sib == old_child);
4917
4918   new_child->die_parent = parent;
4919   if (prev == old_child)
4920     {
4921       gcc_assert (parent->die_child == old_child);
4922       new_child->die_sib = new_child;
4923     }
4924   else
4925     {
4926       prev->die_sib = new_child;
4927       new_child->die_sib = old_child->die_sib;
4928     }
4929   if (old_child->die_parent->die_child == old_child)
4930     old_child->die_parent->die_child = new_child;
4931 }
4932
4933 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4934
4935 static void
4936 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4937 {
4938   dw_die_ref c;
4939   new_parent->die_child = old_parent->die_child;
4940   old_parent->die_child = NULL;
4941   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4942 }
4943
4944 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4945    matches TAG.  */
4946
4947 static void
4948 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4949 {
4950   dw_die_ref c;
4951
4952   c = die->die_child;
4953   if (c) do {
4954     dw_die_ref prev = c;
4955     c = c->die_sib;
4956     while (c->die_tag == tag)
4957       {
4958         remove_child_with_prev (c, prev);
4959         /* Might have removed every child.  */
4960         if (c == c->die_sib)
4961           return;
4962         c = c->die_sib;
4963       }
4964   } while (c != die->die_child);
4965 }
4966
4967 /* Add a CHILD_DIE as the last child of DIE.  */
4968
4969 static void
4970 add_child_die (dw_die_ref die, dw_die_ref child_die)
4971 {
4972   /* FIXME this should probably be an assert.  */
4973   if (! die || ! child_die)
4974     return;
4975   gcc_assert (die != child_die);
4976
4977   child_die->die_parent = die;
4978   if (die->die_child)
4979     {
4980       child_die->die_sib = die->die_child->die_sib;
4981       die->die_child->die_sib = child_die;
4982     }
4983   else
4984     child_die->die_sib = child_die;
4985   die->die_child = child_die;
4986 }
4987
4988 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4989    is the specification, to the end of PARENT's list of children.
4990    This is done by removing and re-adding it.  */
4991
4992 static void
4993 splice_child_die (dw_die_ref parent, dw_die_ref child)
4994 {
4995   dw_die_ref p;
4996
4997   /* We want the declaration DIE from inside the class, not the
4998      specification DIE at toplevel.  */
4999   if (child->die_parent != parent)
5000     {
5001       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5002
5003       if (tmp)
5004         child = tmp;
5005     }
5006
5007   gcc_assert (child->die_parent == parent
5008               || (child->die_parent
5009                   == get_AT_ref (parent, DW_AT_specification)));
5010
5011   for (p = child->die_parent->die_child; ; p = p->die_sib)
5012     if (p->die_sib == child)
5013       {
5014         remove_child_with_prev (child, p);
5015         break;
5016       }
5017
5018   add_child_die (parent, child);
5019 }
5020
5021 /* Return a pointer to a newly created DIE node.  */
5022
5023 static inline dw_die_ref
5024 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5025 {
5026   dw_die_ref die = ggc_alloc_cleared_die_node ();
5027
5028   die->die_tag = tag_value;
5029
5030   if (parent_die != NULL)
5031     add_child_die (parent_die, die);
5032   else
5033     {
5034       limbo_die_node *limbo_node;
5035
5036       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5037       limbo_node->die = die;
5038       limbo_node->created_for = t;
5039       limbo_node->next = limbo_die_list;
5040       limbo_die_list = limbo_node;
5041     }
5042
5043   return die;
5044 }
5045
5046 /* Return the DIE associated with the given type specifier.  */
5047
5048 static inline dw_die_ref
5049 lookup_type_die (tree type)
5050 {
5051   return TYPE_SYMTAB_DIE (type);
5052 }
5053
5054 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5055    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5056    anonymous type instead the one of the naming typedef.  */
5057
5058 static inline dw_die_ref
5059 strip_naming_typedef (tree type, dw_die_ref type_die)
5060 {
5061   if (type
5062       && TREE_CODE (type) == RECORD_TYPE
5063       && type_die
5064       && type_die->die_tag == DW_TAG_typedef
5065       && is_naming_typedef_decl (TYPE_NAME (type)))
5066     type_die = get_AT_ref (type_die, DW_AT_type);
5067   return type_die;
5068 }
5069
5070 /* Like lookup_type_die, but if type is an anonymous type named by a
5071    typedef[1], return the DIE of the anonymous type instead the one of
5072    the naming typedef.  This is because in gen_typedef_die, we did
5073    equate the anonymous struct named by the typedef with the DIE of
5074    the naming typedef. So by default, lookup_type_die on an anonymous
5075    struct yields the DIE of the naming typedef.
5076
5077    [1]: Read the comment of is_naming_typedef_decl to learn about what
5078    a naming typedef is.  */
5079
5080 static inline dw_die_ref
5081 lookup_type_die_strip_naming_typedef (tree type)
5082 {
5083   dw_die_ref die = lookup_type_die (type);
5084   return strip_naming_typedef (type, die);
5085 }
5086
5087 /* Equate a DIE to a given type specifier.  */
5088
5089 static inline void
5090 equate_type_number_to_die (tree type, dw_die_ref type_die)
5091 {
5092   TYPE_SYMTAB_DIE (type) = type_die;
5093 }
5094
5095 /* Returns a hash value for X (which really is a die_struct).  */
5096
5097 static hashval_t
5098 decl_die_table_hash (const void *x)
5099 {
5100   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5101 }
5102
5103 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5104
5105 static int
5106 decl_die_table_eq (const void *x, const void *y)
5107 {
5108   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5109 }
5110
5111 /* Return the DIE associated with a given declaration.  */
5112
5113 static inline dw_die_ref
5114 lookup_decl_die (tree decl)
5115 {
5116   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5117 }
5118
5119 /* Returns a hash value for X (which really is a var_loc_list).  */
5120
5121 static hashval_t
5122 decl_loc_table_hash (const void *x)
5123 {
5124   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5125 }
5126
5127 /* Return nonzero if decl_id of var_loc_list X is the same as
5128    UID of decl *Y.  */
5129
5130 static int
5131 decl_loc_table_eq (const void *x, const void *y)
5132 {
5133   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5134 }
5135
5136 /* Return the var_loc list associated with a given declaration.  */
5137
5138 static inline var_loc_list *
5139 lookup_decl_loc (const_tree decl)
5140 {
5141   if (!decl_loc_table)
5142     return NULL;
5143   return (var_loc_list *)
5144     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5145 }
5146
5147 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5148
5149 static hashval_t
5150 cached_dw_loc_list_table_hash (const void *x)
5151 {
5152   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5153 }
5154
5155 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5156    UID of decl *Y.  */
5157
5158 static int
5159 cached_dw_loc_list_table_eq (const void *x, const void *y)
5160 {
5161   return (((const cached_dw_loc_list *) x)->decl_id
5162           == DECL_UID ((const_tree) y));
5163 }
5164
5165 /* Equate a DIE to a particular declaration.  */
5166
5167 static void
5168 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5169 {
5170   unsigned int decl_id = DECL_UID (decl);
5171   void **slot;
5172
5173   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5174   *slot = decl_die;
5175   decl_die->decl_id = decl_id;
5176 }
5177
5178 /* Return how many bits covers PIECE EXPR_LIST.  */
5179
5180 static int
5181 decl_piece_bitsize (rtx piece)
5182 {
5183   int ret = (int) GET_MODE (piece);
5184   if (ret)
5185     return ret;
5186   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5187               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5188   return INTVAL (XEXP (XEXP (piece, 0), 0));
5189 }
5190
5191 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5192
5193 static rtx *
5194 decl_piece_varloc_ptr (rtx piece)
5195 {
5196   if ((int) GET_MODE (piece))
5197     return &XEXP (piece, 0);
5198   else
5199     return &XEXP (XEXP (piece, 0), 1);
5200 }
5201
5202 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5203    Next is the chain of following piece nodes.  */
5204
5205 static rtx
5206 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5207 {
5208   if (bitsize <= (int) MAX_MACHINE_MODE)
5209     return alloc_EXPR_LIST (bitsize, loc_note, next);
5210   else
5211     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5212                                                GEN_INT (bitsize),
5213                                                loc_note), next);
5214 }
5215
5216 /* Return rtx that should be stored into loc field for
5217    LOC_NOTE and BITPOS/BITSIZE.  */
5218
5219 static rtx
5220 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5221                       HOST_WIDE_INT bitsize)
5222 {
5223   if (bitsize != -1)
5224     {
5225       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5226       if (bitpos != 0)
5227         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5228     }
5229   return loc_note;
5230 }
5231
5232 /* This function either modifies location piece list *DEST in
5233    place (if SRC and INNER is NULL), or copies location piece list
5234    *SRC to *DEST while modifying it.  Location BITPOS is modified
5235    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5236    not copied and if needed some padding around it is added.
5237    When modifying in place, DEST should point to EXPR_LIST where
5238    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5239    to the start of the whole list and INNER points to the EXPR_LIST
5240    where earlier pieces cover PIECE_BITPOS bits.  */
5241
5242 static void
5243 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5244                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5245                    HOST_WIDE_INT bitsize, rtx loc_note)
5246 {
5247   int diff;
5248   bool copy = inner != NULL;
5249
5250   if (copy)
5251     {
5252       /* First copy all nodes preceeding the current bitpos.  */
5253       while (src != inner)
5254         {
5255           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5256                                    decl_piece_bitsize (*src), NULL_RTX);
5257           dest = &XEXP (*dest, 1);
5258           src = &XEXP (*src, 1);
5259         }
5260     }
5261   /* Add padding if needed.  */
5262   if (bitpos != piece_bitpos)
5263     {
5264       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5265                                copy ? NULL_RTX : *dest);
5266       dest = &XEXP (*dest, 1);
5267     }
5268   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5269     {
5270       gcc_assert (!copy);
5271       /* A piece with correct bitpos and bitsize already exist,
5272          just update the location for it and return.  */
5273       *decl_piece_varloc_ptr (*dest) = loc_note;
5274       return;
5275     }
5276   /* Add the piece that changed.  */
5277   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5278   dest = &XEXP (*dest, 1);
5279   /* Skip over pieces that overlap it.  */
5280   diff = bitpos - piece_bitpos + bitsize;
5281   if (!copy)
5282     src = dest;
5283   while (diff > 0 && *src)
5284     {
5285       rtx piece = *src;
5286       diff -= decl_piece_bitsize (piece);
5287       if (copy)
5288         src = &XEXP (piece, 1);
5289       else
5290         {
5291           *src = XEXP (piece, 1);
5292           free_EXPR_LIST_node (piece);
5293         }
5294     }
5295   /* Add padding if needed.  */
5296   if (diff < 0 && *src)
5297     {
5298       if (!copy)
5299         dest = src;
5300       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5301       dest = &XEXP (*dest, 1);
5302     }
5303   if (!copy)
5304     return;
5305   /* Finally copy all nodes following it.  */
5306   while (*src)
5307     {
5308       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5309                                decl_piece_bitsize (*src), NULL_RTX);
5310       dest = &XEXP (*dest, 1);
5311       src = &XEXP (*src, 1);
5312     }
5313 }
5314
5315 /* Add a variable location node to the linked list for DECL.  */
5316
5317 static struct var_loc_node *
5318 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5319 {
5320   unsigned int decl_id;
5321   var_loc_list *temp;
5322   void **slot;
5323   struct var_loc_node *loc = NULL;
5324   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5325
5326   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5327     {
5328       tree realdecl = DECL_DEBUG_EXPR (decl);
5329       if (realdecl && handled_component_p (realdecl))
5330         {
5331           HOST_WIDE_INT maxsize;
5332           tree innerdecl;
5333           innerdecl
5334             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5335           if (!DECL_P (innerdecl)
5336               || DECL_IGNORED_P (innerdecl)
5337               || TREE_STATIC (innerdecl)
5338               || bitsize <= 0
5339               || bitpos + bitsize > 256
5340               || bitsize != maxsize)
5341             return NULL;
5342           decl = innerdecl;
5343         }
5344     }
5345
5346   decl_id = DECL_UID (decl);
5347   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5348   if (*slot == NULL)
5349     {
5350       temp = ggc_alloc_cleared_var_loc_list ();
5351       temp->decl_id = decl_id;
5352       *slot = temp;
5353     }
5354   else
5355     temp = (var_loc_list *) *slot;
5356
5357   /* For PARM_DECLs try to keep around the original incoming value,
5358      even if that means we'll emit a zero-range .debug_loc entry.  */
5359   if (temp->last
5360       && temp->first == temp->last
5361       && TREE_CODE (decl) == PARM_DECL
5362       && GET_CODE (temp->first->loc) == NOTE
5363       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5364       && DECL_INCOMING_RTL (decl)
5365       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5366       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5367          == GET_CODE (DECL_INCOMING_RTL (decl))
5368       && prev_real_insn (temp->first->loc) == NULL_RTX
5369       && (bitsize != -1
5370           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5371                            NOTE_VAR_LOCATION_LOC (loc_note))
5372           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5373               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5374     {
5375       loc = ggc_alloc_cleared_var_loc_node ();
5376       temp->first->next = loc;
5377       temp->last = loc;
5378       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5379     }
5380   else if (temp->last)
5381     {
5382       struct var_loc_node *last = temp->last, *unused = NULL;
5383       rtx *piece_loc = NULL, last_loc_note;
5384       int piece_bitpos = 0;
5385       if (last->next)
5386         {
5387           last = last->next;
5388           gcc_assert (last->next == NULL);
5389         }
5390       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5391         {
5392           piece_loc = &last->loc;
5393           do
5394             {
5395               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5396               if (piece_bitpos + cur_bitsize > bitpos)
5397                 break;
5398               piece_bitpos += cur_bitsize;
5399               piece_loc = &XEXP (*piece_loc, 1);
5400             }
5401           while (*piece_loc);
5402         }
5403       /* TEMP->LAST here is either pointer to the last but one or
5404          last element in the chained list, LAST is pointer to the
5405          last element.  */
5406       if (label && strcmp (last->label, label) == 0)
5407         {
5408           /* For SRA optimized variables if there weren't any real
5409              insns since last note, just modify the last node.  */
5410           if (piece_loc != NULL)
5411             {
5412               adjust_piece_list (piece_loc, NULL, NULL,
5413                                  bitpos, piece_bitpos, bitsize, loc_note);
5414               return NULL;
5415             }
5416           /* If the last note doesn't cover any instructions, remove it.  */
5417           if (temp->last != last)
5418             {
5419               temp->last->next = NULL;
5420               unused = last;
5421               last = temp->last;
5422               gcc_assert (strcmp (last->label, label) != 0);
5423             }
5424           else
5425             {
5426               gcc_assert (temp->first == temp->last
5427                           || (temp->first->next == temp->last
5428                               && TREE_CODE (decl) == PARM_DECL));
5429               memset (temp->last, '\0', sizeof (*temp->last));
5430               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5431               return temp->last;
5432             }
5433         }
5434       if (bitsize == -1 && NOTE_P (last->loc))
5435         last_loc_note = last->loc;
5436       else if (piece_loc != NULL
5437                && *piece_loc != NULL_RTX
5438                && piece_bitpos == bitpos
5439                && decl_piece_bitsize (*piece_loc) == bitsize)
5440         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5441       else
5442         last_loc_note = NULL_RTX;
5443       /* If the current location is the same as the end of the list,
5444          and either both or neither of the locations is uninitialized,
5445          we have nothing to do.  */
5446       if (last_loc_note == NULL_RTX
5447           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5448                             NOTE_VAR_LOCATION_LOC (loc_note)))
5449           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5450                != NOTE_VAR_LOCATION_STATUS (loc_note))
5451               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5452                    == VAR_INIT_STATUS_UNINITIALIZED)
5453                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5454                       == VAR_INIT_STATUS_UNINITIALIZED))))
5455         {
5456           /* Add LOC to the end of list and update LAST.  If the last
5457              element of the list has been removed above, reuse its
5458              memory for the new node, otherwise allocate a new one.  */
5459           if (unused)
5460             {
5461               loc = unused;
5462               memset (loc, '\0', sizeof (*loc));
5463             }
5464           else
5465             loc = ggc_alloc_cleared_var_loc_node ();
5466           if (bitsize == -1 || piece_loc == NULL)
5467             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5468           else
5469             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5470                                bitpos, piece_bitpos, bitsize, loc_note);
5471           last->next = loc;
5472           /* Ensure TEMP->LAST will point either to the new last but one
5473              element of the chain, or to the last element in it.  */
5474           if (last != temp->last)
5475             temp->last = last;
5476         }
5477       else if (unused)
5478         ggc_free (unused);
5479     }
5480   else
5481     {
5482       loc = ggc_alloc_cleared_var_loc_node ();
5483       temp->first = loc;
5484       temp->last = loc;
5485       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5486     }
5487   return loc;
5488 }
5489 \f
5490 /* Keep track of the number of spaces used to indent the
5491    output of the debugging routines that print the structure of
5492    the DIE internal representation.  */
5493 static int print_indent;
5494
5495 /* Indent the line the number of spaces given by print_indent.  */
5496
5497 static inline void
5498 print_spaces (FILE *outfile)
5499 {
5500   fprintf (outfile, "%*s", print_indent, "");
5501 }
5502
5503 /* Print a type signature in hex.  */
5504
5505 static inline void
5506 print_signature (FILE *outfile, char *sig)
5507 {
5508   int i;
5509
5510   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5511     fprintf (outfile, "%02x", sig[i] & 0xff);
5512 }
5513
5514 /* Print the information associated with a given DIE, and its children.
5515    This routine is a debugging aid only.  */
5516
5517 static void
5518 print_die (dw_die_ref die, FILE *outfile)
5519 {
5520   dw_attr_ref a;
5521   dw_die_ref c;
5522   unsigned ix;
5523
5524   print_spaces (outfile);
5525   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5526            die->die_offset, dwarf_tag_name (die->die_tag),
5527            (void*) die);
5528   print_spaces (outfile);
5529   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5530   fprintf (outfile, " offset: %ld", die->die_offset);
5531   fprintf (outfile, " mark: %d\n", die->die_mark);
5532
5533   if (use_debug_types && die->die_id.die_type_node)
5534     {
5535       print_spaces (outfile);
5536       fprintf (outfile, "  signature: ");
5537       print_signature (outfile, die->die_id.die_type_node->signature);
5538       fprintf (outfile, "\n");
5539     }
5540
5541   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5542     {
5543       print_spaces (outfile);
5544       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5545
5546       switch (AT_class (a))
5547         {
5548         case dw_val_class_addr:
5549           fprintf (outfile, "address");
5550           break;
5551         case dw_val_class_offset:
5552           fprintf (outfile, "offset");
5553           break;
5554         case dw_val_class_loc:
5555           fprintf (outfile, "location descriptor");
5556           break;
5557         case dw_val_class_loc_list:
5558           fprintf (outfile, "location list -> label:%s",
5559                    AT_loc_list (a)->ll_symbol);
5560           break;
5561         case dw_val_class_range_list:
5562           fprintf (outfile, "range list");
5563           break;
5564         case dw_val_class_const:
5565           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5566           break;
5567         case dw_val_class_unsigned_const:
5568           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5569           break;
5570         case dw_val_class_const_double:
5571           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5572                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5573                    a->dw_attr_val.v.val_double.high,
5574                    a->dw_attr_val.v.val_double.low);
5575           break;
5576         case dw_val_class_vec:
5577           fprintf (outfile, "floating-point or vector constant");
5578           break;
5579         case dw_val_class_flag:
5580           fprintf (outfile, "%u", AT_flag (a));
5581           break;
5582         case dw_val_class_die_ref:
5583           if (AT_ref (a) != NULL)
5584             {
5585               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5586                 {
5587                   fprintf (outfile, "die -> signature: ");
5588                   print_signature (outfile,
5589                                    AT_ref (a)->die_id.die_type_node->signature);
5590                 }
5591               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5592                 fprintf (outfile, "die -> label: %s",
5593                          AT_ref (a)->die_id.die_symbol);
5594               else
5595                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5596               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5597             }
5598           else
5599             fprintf (outfile, "die -> <null>");
5600           break;
5601         case dw_val_class_vms_delta:
5602           fprintf (outfile, "delta: @slotcount(%s-%s)",
5603                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5604           break;
5605         case dw_val_class_lbl_id:
5606         case dw_val_class_lineptr:
5607         case dw_val_class_macptr:
5608           fprintf (outfile, "label: %s", AT_lbl (a));
5609           break;
5610         case dw_val_class_str:
5611           if (AT_string (a) != NULL)
5612             fprintf (outfile, "\"%s\"", AT_string (a));
5613           else
5614             fprintf (outfile, "<null>");
5615           break;
5616         case dw_val_class_file:
5617           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5618                    AT_file (a)->emitted_number);
5619           break;
5620         case dw_val_class_data8:
5621           {
5622             int i;
5623
5624             for (i = 0; i < 8; i++)
5625               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5626             break;
5627           }
5628         default:
5629           break;
5630         }
5631
5632       fprintf (outfile, "\n");
5633     }
5634
5635   if (die->die_child != NULL)
5636     {
5637       print_indent += 4;
5638       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5639       print_indent -= 4;
5640     }
5641   if (print_indent == 0)
5642     fprintf (outfile, "\n");
5643 }
5644
5645 /* Print the information collected for a given DIE.  */
5646
5647 DEBUG_FUNCTION void
5648 debug_dwarf_die (dw_die_ref die)
5649 {
5650   print_die (die, stderr);
5651 }
5652
5653 /* Print all DWARF information collected for the compilation unit.
5654    This routine is a debugging aid only.  */
5655
5656 DEBUG_FUNCTION void
5657 debug_dwarf (void)
5658 {
5659   print_indent = 0;
5660   print_die (comp_unit_die (), stderr);
5661 }
5662 \f
5663 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5664    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5665    DIE that marks the start of the DIEs for this include file.  */
5666
5667 static dw_die_ref
5668 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5669 {
5670   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5671   dw_die_ref new_unit = gen_compile_unit_die (filename);
5672
5673   new_unit->die_sib = old_unit;
5674   return new_unit;
5675 }
5676
5677 /* Close an include-file CU and reopen the enclosing one.  */
5678
5679 static dw_die_ref
5680 pop_compile_unit (dw_die_ref old_unit)
5681 {
5682   dw_die_ref new_unit = old_unit->die_sib;
5683
5684   old_unit->die_sib = NULL;
5685   return new_unit;
5686 }
5687
5688 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5689 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5690
5691 /* Calculate the checksum of a location expression.  */
5692
5693 static inline void
5694 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5695 {
5696   int tem;
5697
5698   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5699   CHECKSUM (tem);
5700   CHECKSUM (loc->dw_loc_oprnd1);
5701   CHECKSUM (loc->dw_loc_oprnd2);
5702 }
5703
5704 /* Calculate the checksum of an attribute.  */
5705
5706 static void
5707 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5708 {
5709   dw_loc_descr_ref loc;
5710   rtx r;
5711
5712   CHECKSUM (at->dw_attr);
5713
5714   /* We don't care that this was compiled with a different compiler
5715      snapshot; if the output is the same, that's what matters.  */
5716   if (at->dw_attr == DW_AT_producer)
5717     return;
5718
5719   switch (AT_class (at))
5720     {
5721     case dw_val_class_const:
5722       CHECKSUM (at->dw_attr_val.v.val_int);
5723       break;
5724     case dw_val_class_unsigned_const:
5725       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5726       break;
5727     case dw_val_class_const_double:
5728       CHECKSUM (at->dw_attr_val.v.val_double);
5729       break;
5730     case dw_val_class_vec:
5731       CHECKSUM (at->dw_attr_val.v.val_vec);
5732       break;
5733     case dw_val_class_flag:
5734       CHECKSUM (at->dw_attr_val.v.val_flag);
5735       break;
5736     case dw_val_class_str:
5737       CHECKSUM_STRING (AT_string (at));
5738       break;
5739
5740     case dw_val_class_addr:
5741       r = AT_addr (at);
5742       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5743       CHECKSUM_STRING (XSTR (r, 0));
5744       break;
5745
5746     case dw_val_class_offset:
5747       CHECKSUM (at->dw_attr_val.v.val_offset);
5748       break;
5749
5750     case dw_val_class_loc:
5751       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5752         loc_checksum (loc, ctx);
5753       break;
5754
5755     case dw_val_class_die_ref:
5756       die_checksum (AT_ref (at), ctx, mark);
5757       break;
5758
5759     case dw_val_class_fde_ref:
5760     case dw_val_class_vms_delta:
5761     case dw_val_class_lbl_id:
5762     case dw_val_class_lineptr:
5763     case dw_val_class_macptr:
5764       break;
5765
5766     case dw_val_class_file:
5767       CHECKSUM_STRING (AT_file (at)->filename);
5768       break;
5769
5770     case dw_val_class_data8:
5771       CHECKSUM (at->dw_attr_val.v.val_data8);
5772       break;
5773
5774     default:
5775       break;
5776     }
5777 }
5778
5779 /* Calculate the checksum of a DIE.  */
5780
5781 static void
5782 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5783 {
5784   dw_die_ref c;
5785   dw_attr_ref a;
5786   unsigned ix;
5787
5788   /* To avoid infinite recursion.  */
5789   if (die->die_mark)
5790     {
5791       CHECKSUM (die->die_mark);
5792       return;
5793     }
5794   die->die_mark = ++(*mark);
5795
5796   CHECKSUM (die->die_tag);
5797
5798   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5799     attr_checksum (a, ctx, mark);
5800
5801   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5802 }
5803
5804 #undef CHECKSUM
5805 #undef CHECKSUM_STRING
5806
5807 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5808 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5809 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5810 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5811 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5812 #define CHECKSUM_ATTR(FOO) \
5813   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5814
5815 /* Calculate the checksum of a number in signed LEB128 format.  */
5816
5817 static void
5818 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5819 {
5820   unsigned char byte;
5821   bool more;
5822
5823   while (1)
5824     {
5825       byte = (value & 0x7f);
5826       value >>= 7;
5827       more = !((value == 0 && (byte & 0x40) == 0)
5828                 || (value == -1 && (byte & 0x40) != 0));
5829       if (more)
5830         byte |= 0x80;
5831       CHECKSUM (byte);
5832       if (!more)
5833         break;
5834     }
5835 }
5836
5837 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5838
5839 static void
5840 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5841 {
5842   while (1)
5843     {
5844       unsigned char byte = (value & 0x7f);
5845       value >>= 7;
5846       if (value != 0)
5847         /* More bytes to follow.  */
5848         byte |= 0x80;
5849       CHECKSUM (byte);
5850       if (value == 0)
5851         break;
5852     }
5853 }
5854
5855 /* Checksum the context of the DIE.  This adds the names of any
5856    surrounding namespaces or structures to the checksum.  */
5857
5858 static void
5859 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5860 {
5861   const char *name;
5862   dw_die_ref spec;
5863   int tag = die->die_tag;
5864
5865   if (tag != DW_TAG_namespace
5866       && tag != DW_TAG_structure_type
5867       && tag != DW_TAG_class_type)
5868     return;
5869
5870   name = get_AT_string (die, DW_AT_name);
5871
5872   spec = get_AT_ref (die, DW_AT_specification);
5873   if (spec != NULL)
5874     die = spec;
5875
5876   if (die->die_parent != NULL)
5877     checksum_die_context (die->die_parent, ctx);
5878
5879   CHECKSUM_ULEB128 ('C');
5880   CHECKSUM_ULEB128 (tag);
5881   if (name != NULL)
5882     CHECKSUM_STRING (name);
5883 }
5884
5885 /* Calculate the checksum of a location expression.  */
5886
5887 static inline void
5888 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5889 {
5890   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5891      were emitted as a DW_FORM_sdata instead of a location expression.  */
5892   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5893     {
5894       CHECKSUM_ULEB128 (DW_FORM_sdata);
5895       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5896       return;
5897     }
5898
5899   /* Otherwise, just checksum the raw location expression.  */
5900   while (loc != NULL)
5901     {
5902       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5903       CHECKSUM (loc->dw_loc_oprnd1);
5904       CHECKSUM (loc->dw_loc_oprnd2);
5905       loc = loc->dw_loc_next;
5906     }
5907 }
5908
5909 /* Calculate the checksum of an attribute.  */
5910
5911 static void
5912 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5913                        struct md5_ctx *ctx, int *mark)
5914 {
5915   dw_loc_descr_ref loc;
5916   rtx r;
5917
5918   if (AT_class (at) == dw_val_class_die_ref)
5919     {
5920       dw_die_ref target_die = AT_ref (at);
5921
5922       /* For pointer and reference types, we checksum only the (qualified)
5923          name of the target type (if there is a name).  For friend entries,
5924          we checksum only the (qualified) name of the target type or function.
5925          This allows the checksum to remain the same whether the target type
5926          is complete or not.  */
5927       if ((at->dw_attr == DW_AT_type
5928            && (tag == DW_TAG_pointer_type
5929                || tag == DW_TAG_reference_type
5930                || tag == DW_TAG_rvalue_reference_type
5931                || tag == DW_TAG_ptr_to_member_type))
5932           || (at->dw_attr == DW_AT_friend
5933               && tag == DW_TAG_friend))
5934         {
5935           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5936
5937           if (name_attr != NULL)
5938             {
5939               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5940
5941               if (decl == NULL)
5942                 decl = target_die;
5943               CHECKSUM_ULEB128 ('N');
5944               CHECKSUM_ULEB128 (at->dw_attr);
5945               if (decl->die_parent != NULL)
5946                 checksum_die_context (decl->die_parent, ctx);
5947               CHECKSUM_ULEB128 ('E');
5948               CHECKSUM_STRING (AT_string (name_attr));
5949               return;
5950             }
5951         }
5952
5953       /* For all other references to another DIE, we check to see if the
5954          target DIE has already been visited.  If it has, we emit a
5955          backward reference; if not, we descend recursively.  */
5956       if (target_die->die_mark > 0)
5957         {
5958           CHECKSUM_ULEB128 ('R');
5959           CHECKSUM_ULEB128 (at->dw_attr);
5960           CHECKSUM_ULEB128 (target_die->die_mark);
5961         }
5962       else
5963         {
5964           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5965
5966           if (decl == NULL)
5967             decl = target_die;
5968           target_die->die_mark = ++(*mark);
5969           CHECKSUM_ULEB128 ('T');
5970           CHECKSUM_ULEB128 (at->dw_attr);
5971           if (decl->die_parent != NULL)
5972             checksum_die_context (decl->die_parent, ctx);
5973           die_checksum_ordered (target_die, ctx, mark);
5974         }
5975       return;
5976     }
5977
5978   CHECKSUM_ULEB128 ('A');
5979   CHECKSUM_ULEB128 (at->dw_attr);
5980
5981   switch (AT_class (at))
5982     {
5983     case dw_val_class_const:
5984       CHECKSUM_ULEB128 (DW_FORM_sdata);
5985       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5986       break;
5987
5988     case dw_val_class_unsigned_const:
5989       CHECKSUM_ULEB128 (DW_FORM_sdata);
5990       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5991       break;
5992
5993     case dw_val_class_const_double:
5994       CHECKSUM_ULEB128 (DW_FORM_block);
5995       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5996       CHECKSUM (at->dw_attr_val.v.val_double);
5997       break;
5998
5999     case dw_val_class_vec:
6000       CHECKSUM_ULEB128 (DW_FORM_block);
6001       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6002       CHECKSUM (at->dw_attr_val.v.val_vec);
6003       break;
6004
6005     case dw_val_class_flag:
6006       CHECKSUM_ULEB128 (DW_FORM_flag);
6007       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6008       break;
6009
6010     case dw_val_class_str:
6011       CHECKSUM_ULEB128 (DW_FORM_string);
6012       CHECKSUM_STRING (AT_string (at));
6013       break;
6014
6015     case dw_val_class_addr:
6016       r = AT_addr (at);
6017       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6018       CHECKSUM_ULEB128 (DW_FORM_string);
6019       CHECKSUM_STRING (XSTR (r, 0));
6020       break;
6021
6022     case dw_val_class_offset:
6023       CHECKSUM_ULEB128 (DW_FORM_sdata);
6024       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6025       break;
6026
6027     case dw_val_class_loc:
6028       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6029         loc_checksum_ordered (loc, ctx);
6030       break;
6031
6032     case dw_val_class_fde_ref:
6033     case dw_val_class_lbl_id:
6034     case dw_val_class_lineptr:
6035     case dw_val_class_macptr:
6036       break;
6037
6038     case dw_val_class_file:
6039       CHECKSUM_ULEB128 (DW_FORM_string);
6040       CHECKSUM_STRING (AT_file (at)->filename);
6041       break;
6042
6043     case dw_val_class_data8:
6044       CHECKSUM (at->dw_attr_val.v.val_data8);
6045       break;
6046
6047     default:
6048       break;
6049     }
6050 }
6051
6052 struct checksum_attributes
6053 {
6054   dw_attr_ref at_name;
6055   dw_attr_ref at_type;
6056   dw_attr_ref at_friend;
6057   dw_attr_ref at_accessibility;
6058   dw_attr_ref at_address_class;
6059   dw_attr_ref at_allocated;
6060   dw_attr_ref at_artificial;
6061   dw_attr_ref at_associated;
6062   dw_attr_ref at_binary_scale;
6063   dw_attr_ref at_bit_offset;
6064   dw_attr_ref at_bit_size;
6065   dw_attr_ref at_bit_stride;
6066   dw_attr_ref at_byte_size;
6067   dw_attr_ref at_byte_stride;
6068   dw_attr_ref at_const_value;
6069   dw_attr_ref at_containing_type;
6070   dw_attr_ref at_count;
6071   dw_attr_ref at_data_location;
6072   dw_attr_ref at_data_member_location;
6073   dw_attr_ref at_decimal_scale;
6074   dw_attr_ref at_decimal_sign;
6075   dw_attr_ref at_default_value;
6076   dw_attr_ref at_digit_count;
6077   dw_attr_ref at_discr;
6078   dw_attr_ref at_discr_list;
6079   dw_attr_ref at_discr_value;
6080   dw_attr_ref at_encoding;
6081   dw_attr_ref at_endianity;
6082   dw_attr_ref at_explicit;
6083   dw_attr_ref at_is_optional;
6084   dw_attr_ref at_location;
6085   dw_attr_ref at_lower_bound;
6086   dw_attr_ref at_mutable;
6087   dw_attr_ref at_ordering;
6088   dw_attr_ref at_picture_string;
6089   dw_attr_ref at_prototyped;
6090   dw_attr_ref at_small;
6091   dw_attr_ref at_segment;
6092   dw_attr_ref at_string_length;
6093   dw_attr_ref at_threads_scaled;
6094   dw_attr_ref at_upper_bound;
6095   dw_attr_ref at_use_location;
6096   dw_attr_ref at_use_UTF8;
6097   dw_attr_ref at_variable_parameter;
6098   dw_attr_ref at_virtuality;
6099   dw_attr_ref at_visibility;
6100   dw_attr_ref at_vtable_elem_location;
6101 };
6102
6103 /* Collect the attributes that we will want to use for the checksum.  */
6104
6105 static void
6106 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6107 {
6108   dw_attr_ref a;
6109   unsigned ix;
6110
6111   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6112     {
6113       switch (a->dw_attr)
6114         {
6115         case DW_AT_name:
6116           attrs->at_name = a;
6117           break;
6118         case DW_AT_type:
6119           attrs->at_type = a;
6120           break;
6121         case DW_AT_friend:
6122           attrs->at_friend = a;
6123           break;
6124         case DW_AT_accessibility:
6125           attrs->at_accessibility = a;
6126           break;
6127         case DW_AT_address_class:
6128           attrs->at_address_class = a;
6129           break;
6130         case DW_AT_allocated:
6131           attrs->at_allocated = a;
6132           break;
6133         case DW_AT_artificial:
6134           attrs->at_artificial = a;
6135           break;
6136         case DW_AT_associated:
6137           attrs->at_associated = a;
6138           break;
6139         case DW_AT_binary_scale:
6140           attrs->at_binary_scale = a;
6141           break;
6142         case DW_AT_bit_offset:
6143           attrs->at_bit_offset = a;
6144           break;
6145         case DW_AT_bit_size:
6146           attrs->at_bit_size = a;
6147           break;
6148         case DW_AT_bit_stride:
6149           attrs->at_bit_stride = a;
6150           break;
6151         case DW_AT_byte_size:
6152           attrs->at_byte_size = a;
6153           break;
6154         case DW_AT_byte_stride:
6155           attrs->at_byte_stride = a;
6156           break;
6157         case DW_AT_const_value:
6158           attrs->at_const_value = a;
6159           break;
6160         case DW_AT_containing_type:
6161           attrs->at_containing_type = a;
6162           break;
6163         case DW_AT_count:
6164           attrs->at_count = a;
6165           break;
6166         case DW_AT_data_location:
6167           attrs->at_data_location = a;
6168           break;
6169         case DW_AT_data_member_location:
6170           attrs->at_data_member_location = a;
6171           break;
6172         case DW_AT_decimal_scale:
6173           attrs->at_decimal_scale = a;
6174           break;
6175         case DW_AT_decimal_sign:
6176           attrs->at_decimal_sign = a;
6177           break;
6178         case DW_AT_default_value:
6179           attrs->at_default_value = a;
6180           break;
6181         case DW_AT_digit_count:
6182           attrs->at_digit_count = a;
6183           break;
6184         case DW_AT_discr:
6185           attrs->at_discr = a;
6186           break;
6187         case DW_AT_discr_list:
6188           attrs->at_discr_list = a;
6189           break;
6190         case DW_AT_discr_value:
6191           attrs->at_discr_value = a;
6192           break;
6193         case DW_AT_encoding:
6194           attrs->at_encoding = a;
6195           break;
6196         case DW_AT_endianity:
6197           attrs->at_endianity = a;
6198           break;
6199         case DW_AT_explicit:
6200           attrs->at_explicit = a;
6201           break;
6202         case DW_AT_is_optional:
6203           attrs->at_is_optional = a;
6204           break;
6205         case DW_AT_location:
6206           attrs->at_location = a;
6207           break;
6208         case DW_AT_lower_bound:
6209           attrs->at_lower_bound = a;
6210           break;
6211         case DW_AT_mutable:
6212           attrs->at_mutable = a;
6213           break;
6214         case DW_AT_ordering:
6215           attrs->at_ordering = a;
6216           break;
6217         case DW_AT_picture_string:
6218           attrs->at_picture_string = a;
6219           break;
6220         case DW_AT_prototyped:
6221           attrs->at_prototyped = a;
6222           break;
6223         case DW_AT_small:
6224           attrs->at_small = a;
6225           break;
6226         case DW_AT_segment:
6227           attrs->at_segment = a;
6228           break;
6229         case DW_AT_string_length:
6230           attrs->at_string_length = a;
6231           break;
6232         case DW_AT_threads_scaled:
6233           attrs->at_threads_scaled = a;
6234           break;
6235         case DW_AT_upper_bound:
6236           attrs->at_upper_bound = a;
6237           break;
6238         case DW_AT_use_location:
6239           attrs->at_use_location = a;
6240           break;
6241         case DW_AT_use_UTF8:
6242           attrs->at_use_UTF8 = a;
6243           break;
6244         case DW_AT_variable_parameter:
6245           attrs->at_variable_parameter = a;
6246           break;
6247         case DW_AT_virtuality:
6248           attrs->at_virtuality = a;
6249           break;
6250         case DW_AT_visibility:
6251           attrs->at_visibility = a;
6252           break;
6253         case DW_AT_vtable_elem_location:
6254           attrs->at_vtable_elem_location = a;
6255           break;
6256         default:
6257           break;
6258         }
6259     }
6260 }
6261
6262 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6263
6264 static void
6265 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6266 {
6267   dw_die_ref c;
6268   dw_die_ref decl;
6269   struct checksum_attributes attrs;
6270
6271   CHECKSUM_ULEB128 ('D');
6272   CHECKSUM_ULEB128 (die->die_tag);
6273
6274   memset (&attrs, 0, sizeof (attrs));
6275
6276   decl = get_AT_ref (die, DW_AT_specification);
6277   if (decl != NULL)
6278     collect_checksum_attributes (&attrs, decl);
6279   collect_checksum_attributes (&attrs, die);
6280
6281   CHECKSUM_ATTR (attrs.at_name);
6282   CHECKSUM_ATTR (attrs.at_accessibility);
6283   CHECKSUM_ATTR (attrs.at_address_class);
6284   CHECKSUM_ATTR (attrs.at_allocated);
6285   CHECKSUM_ATTR (attrs.at_artificial);
6286   CHECKSUM_ATTR (attrs.at_associated);
6287   CHECKSUM_ATTR (attrs.at_binary_scale);
6288   CHECKSUM_ATTR (attrs.at_bit_offset);
6289   CHECKSUM_ATTR (attrs.at_bit_size);
6290   CHECKSUM_ATTR (attrs.at_bit_stride);
6291   CHECKSUM_ATTR (attrs.at_byte_size);
6292   CHECKSUM_ATTR (attrs.at_byte_stride);
6293   CHECKSUM_ATTR (attrs.at_const_value);
6294   CHECKSUM_ATTR (attrs.at_containing_type);
6295   CHECKSUM_ATTR (attrs.at_count);
6296   CHECKSUM_ATTR (attrs.at_data_location);
6297   CHECKSUM_ATTR (attrs.at_data_member_location);
6298   CHECKSUM_ATTR (attrs.at_decimal_scale);
6299   CHECKSUM_ATTR (attrs.at_decimal_sign);
6300   CHECKSUM_ATTR (attrs.at_default_value);
6301   CHECKSUM_ATTR (attrs.at_digit_count);
6302   CHECKSUM_ATTR (attrs.at_discr);
6303   CHECKSUM_ATTR (attrs.at_discr_list);
6304   CHECKSUM_ATTR (attrs.at_discr_value);
6305   CHECKSUM_ATTR (attrs.at_encoding);
6306   CHECKSUM_ATTR (attrs.at_endianity);
6307   CHECKSUM_ATTR (attrs.at_explicit);
6308   CHECKSUM_ATTR (attrs.at_is_optional);
6309   CHECKSUM_ATTR (attrs.at_location);
6310   CHECKSUM_ATTR (attrs.at_lower_bound);
6311   CHECKSUM_ATTR (attrs.at_mutable);
6312   CHECKSUM_ATTR (attrs.at_ordering);
6313   CHECKSUM_ATTR (attrs.at_picture_string);
6314   CHECKSUM_ATTR (attrs.at_prototyped);
6315   CHECKSUM_ATTR (attrs.at_small);
6316   CHECKSUM_ATTR (attrs.at_segment);
6317   CHECKSUM_ATTR (attrs.at_string_length);
6318   CHECKSUM_ATTR (attrs.at_threads_scaled);
6319   CHECKSUM_ATTR (attrs.at_upper_bound);
6320   CHECKSUM_ATTR (attrs.at_use_location);
6321   CHECKSUM_ATTR (attrs.at_use_UTF8);
6322   CHECKSUM_ATTR (attrs.at_variable_parameter);
6323   CHECKSUM_ATTR (attrs.at_virtuality);
6324   CHECKSUM_ATTR (attrs.at_visibility);
6325   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6326   CHECKSUM_ATTR (attrs.at_type);
6327   CHECKSUM_ATTR (attrs.at_friend);
6328
6329   /* Checksum the child DIEs, except for nested types and member functions.  */
6330   c = die->die_child;
6331   if (c) do {
6332     dw_attr_ref name_attr;
6333
6334     c = c->die_sib;
6335     name_attr = get_AT (c, DW_AT_name);
6336     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6337         && name_attr != NULL)
6338       {
6339         CHECKSUM_ULEB128 ('S');
6340         CHECKSUM_ULEB128 (c->die_tag);
6341         CHECKSUM_STRING (AT_string (name_attr));
6342       }
6343     else
6344       {
6345         /* Mark this DIE so it gets processed when unmarking.  */
6346         if (c->die_mark == 0)
6347           c->die_mark = -1;
6348         die_checksum_ordered (c, ctx, mark);
6349       }
6350   } while (c != die->die_child);
6351
6352   CHECKSUM_ULEB128 (0);
6353 }
6354
6355 #undef CHECKSUM
6356 #undef CHECKSUM_STRING
6357 #undef CHECKSUM_ATTR
6358 #undef CHECKSUM_LEB128
6359 #undef CHECKSUM_ULEB128
6360
6361 /* Generate the type signature for DIE.  This is computed by generating an
6362    MD5 checksum over the DIE's tag, its relevant attributes, and its
6363    children.  Attributes that are references to other DIEs are processed
6364    by recursion, using the MARK field to prevent infinite recursion.
6365    If the DIE is nested inside a namespace or another type, we also
6366    need to include that context in the signature.  The lower 64 bits
6367    of the resulting MD5 checksum comprise the signature.  */
6368
6369 static void
6370 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6371 {
6372   int mark;
6373   const char *name;
6374   unsigned char checksum[16];
6375   struct md5_ctx ctx;
6376   dw_die_ref decl;
6377
6378   name = get_AT_string (die, DW_AT_name);
6379   decl = get_AT_ref (die, DW_AT_specification);
6380
6381   /* First, compute a signature for just the type name (and its surrounding
6382      context, if any.  This is stored in the type unit DIE for link-time
6383      ODR (one-definition rule) checking.  */
6384
6385   if (is_cxx() && name != NULL)
6386     {
6387       md5_init_ctx (&ctx);
6388
6389       /* Checksum the names of surrounding namespaces and structures.  */
6390       if (decl != NULL && decl->die_parent != NULL)
6391         checksum_die_context (decl->die_parent, &ctx);
6392
6393       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6394       md5_process_bytes (name, strlen (name) + 1, &ctx);
6395       md5_finish_ctx (&ctx, checksum);
6396
6397       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6398     }
6399
6400   /* Next, compute the complete type signature.  */
6401
6402   md5_init_ctx (&ctx);
6403   mark = 1;
6404   die->die_mark = mark;
6405
6406   /* Checksum the names of surrounding namespaces and structures.  */
6407   if (decl != NULL && decl->die_parent != NULL)
6408     checksum_die_context (decl->die_parent, &ctx);
6409
6410   /* Checksum the DIE and its children.  */
6411   die_checksum_ordered (die, &ctx, &mark);
6412   unmark_all_dies (die);
6413   md5_finish_ctx (&ctx, checksum);
6414
6415   /* Store the signature in the type node and link the type DIE and the
6416      type node together.  */
6417   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6418           DWARF_TYPE_SIGNATURE_SIZE);
6419   die->die_id.die_type_node = type_node;
6420   type_node->type_die = die;
6421
6422   /* If the DIE is a specification, link its declaration to the type node
6423      as well.  */
6424   if (decl != NULL)
6425     decl->die_id.die_type_node = type_node;
6426 }
6427
6428 /* Do the location expressions look same?  */
6429 static inline int
6430 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6431 {
6432   return loc1->dw_loc_opc == loc2->dw_loc_opc
6433          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6434          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6435 }
6436
6437 /* Do the values look the same?  */
6438 static int
6439 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6440 {
6441   dw_loc_descr_ref loc1, loc2;
6442   rtx r1, r2;
6443
6444   if (v1->val_class != v2->val_class)
6445     return 0;
6446
6447   switch (v1->val_class)
6448     {
6449     case dw_val_class_const:
6450       return v1->v.val_int == v2->v.val_int;
6451     case dw_val_class_unsigned_const:
6452       return v1->v.val_unsigned == v2->v.val_unsigned;
6453     case dw_val_class_const_double:
6454       return v1->v.val_double.high == v2->v.val_double.high
6455              && v1->v.val_double.low == v2->v.val_double.low;
6456     case dw_val_class_vec:
6457       if (v1->v.val_vec.length != v2->v.val_vec.length
6458           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6459         return 0;
6460       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6461                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6462         return 0;
6463       return 1;
6464     case dw_val_class_flag:
6465       return v1->v.val_flag == v2->v.val_flag;
6466     case dw_val_class_str:
6467       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6468
6469     case dw_val_class_addr:
6470       r1 = v1->v.val_addr;
6471       r2 = v2->v.val_addr;
6472       if (GET_CODE (r1) != GET_CODE (r2))
6473         return 0;
6474       return !rtx_equal_p (r1, r2);
6475
6476     case dw_val_class_offset:
6477       return v1->v.val_offset == v2->v.val_offset;
6478
6479     case dw_val_class_loc:
6480       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6481            loc1 && loc2;
6482            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6483         if (!same_loc_p (loc1, loc2, mark))
6484           return 0;
6485       return !loc1 && !loc2;
6486
6487     case dw_val_class_die_ref:
6488       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6489
6490     case dw_val_class_fde_ref:
6491     case dw_val_class_vms_delta:
6492     case dw_val_class_lbl_id:
6493     case dw_val_class_lineptr:
6494     case dw_val_class_macptr:
6495       return 1;
6496
6497     case dw_val_class_file:
6498       return v1->v.val_file == v2->v.val_file;
6499
6500     case dw_val_class_data8:
6501       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6502
6503     default:
6504       return 1;
6505     }
6506 }
6507
6508 /* Do the attributes look the same?  */
6509
6510 static int
6511 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6512 {
6513   if (at1->dw_attr != at2->dw_attr)
6514     return 0;
6515
6516   /* We don't care that this was compiled with a different compiler
6517      snapshot; if the output is the same, that's what matters. */
6518   if (at1->dw_attr == DW_AT_producer)
6519     return 1;
6520
6521   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6522 }
6523
6524 /* Do the dies look the same?  */
6525
6526 static int
6527 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6528 {
6529   dw_die_ref c1, c2;
6530   dw_attr_ref a1;
6531   unsigned ix;
6532
6533   /* To avoid infinite recursion.  */
6534   if (die1->die_mark)
6535     return die1->die_mark == die2->die_mark;
6536   die1->die_mark = die2->die_mark = ++(*mark);
6537
6538   if (die1->die_tag != die2->die_tag)
6539     return 0;
6540
6541   if (VEC_length (dw_attr_node, die1->die_attr)
6542       != VEC_length (dw_attr_node, die2->die_attr))
6543     return 0;
6544
6545   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6546     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6547       return 0;
6548
6549   c1 = die1->die_child;
6550   c2 = die2->die_child;
6551   if (! c1)
6552     {
6553       if (c2)
6554         return 0;
6555     }
6556   else
6557     for (;;)
6558       {
6559         if (!same_die_p (c1, c2, mark))
6560           return 0;
6561         c1 = c1->die_sib;
6562         c2 = c2->die_sib;
6563         if (c1 == die1->die_child)
6564           {
6565             if (c2 == die2->die_child)
6566               break;
6567             else
6568               return 0;
6569           }
6570     }
6571
6572   return 1;
6573 }
6574
6575 /* Do the dies look the same?  Wrapper around same_die_p.  */
6576
6577 static int
6578 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6579 {
6580   int mark = 0;
6581   int ret = same_die_p (die1, die2, &mark);
6582
6583   unmark_all_dies (die1);
6584   unmark_all_dies (die2);
6585
6586   return ret;
6587 }
6588
6589 /* The prefix to attach to symbols on DIEs in the current comdat debug
6590    info section.  */
6591 static char *comdat_symbol_id;
6592
6593 /* The index of the current symbol within the current comdat CU.  */
6594 static unsigned int comdat_symbol_number;
6595
6596 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6597    children, and set comdat_symbol_id accordingly.  */
6598
6599 static void
6600 compute_section_prefix (dw_die_ref unit_die)
6601 {
6602   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6603   const char *base = die_name ? lbasename (die_name) : "anonymous";
6604   char *name = XALLOCAVEC (char, strlen (base) + 64);
6605   char *p;
6606   int i, mark;
6607   unsigned char checksum[16];
6608   struct md5_ctx ctx;
6609
6610   /* Compute the checksum of the DIE, then append part of it as hex digits to
6611      the name filename of the unit.  */
6612
6613   md5_init_ctx (&ctx);
6614   mark = 0;
6615   die_checksum (unit_die, &ctx, &mark);
6616   unmark_all_dies (unit_die);
6617   md5_finish_ctx (&ctx, checksum);
6618
6619   sprintf (name, "%s.", base);
6620   clean_symbol_name (name);
6621
6622   p = name + strlen (name);
6623   for (i = 0; i < 4; i++)
6624     {
6625       sprintf (p, "%.2x", checksum[i]);
6626       p += 2;
6627     }
6628
6629   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6630   comdat_symbol_number = 0;
6631 }
6632
6633 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6634
6635 static int
6636 is_type_die (dw_die_ref die)
6637 {
6638   switch (die->die_tag)
6639     {
6640     case DW_TAG_array_type:
6641     case DW_TAG_class_type:
6642     case DW_TAG_interface_type:
6643     case DW_TAG_enumeration_type:
6644     case DW_TAG_pointer_type:
6645     case DW_TAG_reference_type:
6646     case DW_TAG_rvalue_reference_type:
6647     case DW_TAG_string_type:
6648     case DW_TAG_structure_type:
6649     case DW_TAG_subroutine_type:
6650     case DW_TAG_union_type:
6651     case DW_TAG_ptr_to_member_type:
6652     case DW_TAG_set_type:
6653     case DW_TAG_subrange_type:
6654     case DW_TAG_base_type:
6655     case DW_TAG_const_type:
6656     case DW_TAG_file_type:
6657     case DW_TAG_packed_type:
6658     case DW_TAG_volatile_type:
6659     case DW_TAG_typedef:
6660       return 1;
6661     default:
6662       return 0;
6663     }
6664 }
6665
6666 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6667    Basically, we want to choose the bits that are likely to be shared between
6668    compilations (types) and leave out the bits that are specific to individual
6669    compilations (functions).  */
6670
6671 static int
6672 is_comdat_die (dw_die_ref c)
6673 {
6674   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6675      we do for stabs.  The advantage is a greater likelihood of sharing between
6676      objects that don't include headers in the same order (and therefore would
6677      put the base types in a different comdat).  jason 8/28/00 */
6678
6679   if (c->die_tag == DW_TAG_base_type)
6680     return 0;
6681
6682   if (c->die_tag == DW_TAG_pointer_type
6683       || c->die_tag == DW_TAG_reference_type
6684       || c->die_tag == DW_TAG_rvalue_reference_type
6685       || c->die_tag == DW_TAG_const_type
6686       || c->die_tag == DW_TAG_volatile_type)
6687     {
6688       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6689
6690       return t ? is_comdat_die (t) : 0;
6691     }
6692
6693   return is_type_die (c);
6694 }
6695
6696 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6697    compilation unit.  */
6698
6699 static int
6700 is_symbol_die (dw_die_ref c)
6701 {
6702   return (is_type_die (c)
6703           || is_declaration_die (c)
6704           || c->die_tag == DW_TAG_namespace
6705           || c->die_tag == DW_TAG_module);
6706 }
6707
6708 /* Returns true iff C is a compile-unit DIE.  */
6709
6710 static inline bool
6711 is_cu_die (dw_die_ref c)
6712 {
6713   return c && c->die_tag == DW_TAG_compile_unit;
6714 }
6715
6716 static char *
6717 gen_internal_sym (const char *prefix)
6718 {
6719   char buf[256];
6720
6721   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6722   return xstrdup (buf);
6723 }
6724
6725 /* Assign symbols to all worthy DIEs under DIE.  */
6726
6727 static void
6728 assign_symbol_names (dw_die_ref die)
6729 {
6730   dw_die_ref c;
6731
6732   if (is_symbol_die (die))
6733     {
6734       if (comdat_symbol_id)
6735         {
6736           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6737
6738           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6739                    comdat_symbol_id, comdat_symbol_number++);
6740           die->die_id.die_symbol = xstrdup (p);
6741         }
6742       else
6743         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6744     }
6745
6746   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6747 }
6748
6749 struct cu_hash_table_entry
6750 {
6751   dw_die_ref cu;
6752   unsigned min_comdat_num, max_comdat_num;
6753   struct cu_hash_table_entry *next;
6754 };
6755
6756 /* Routines to manipulate hash table of CUs.  */
6757 static hashval_t
6758 htab_cu_hash (const void *of)
6759 {
6760   const struct cu_hash_table_entry *const entry =
6761     (const struct cu_hash_table_entry *) of;
6762
6763   return htab_hash_string (entry->cu->die_id.die_symbol);
6764 }
6765
6766 static int
6767 htab_cu_eq (const void *of1, const void *of2)
6768 {
6769   const struct cu_hash_table_entry *const entry1 =
6770     (const struct cu_hash_table_entry *) of1;
6771   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6772
6773   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6774 }
6775
6776 static void
6777 htab_cu_del (void *what)
6778 {
6779   struct cu_hash_table_entry *next,
6780     *entry = (struct cu_hash_table_entry *) what;
6781
6782   while (entry)
6783     {
6784       next = entry->next;
6785       free (entry);
6786       entry = next;
6787     }
6788 }
6789
6790 /* Check whether we have already seen this CU and set up SYM_NUM
6791    accordingly.  */
6792 static int
6793 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6794 {
6795   struct cu_hash_table_entry dummy;
6796   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6797
6798   dummy.max_comdat_num = 0;
6799
6800   slot = (struct cu_hash_table_entry **)
6801     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6802         INSERT);
6803   entry = *slot;
6804
6805   for (; entry; last = entry, entry = entry->next)
6806     {
6807       if (same_die_p_wrap (cu, entry->cu))
6808         break;
6809     }
6810
6811   if (entry)
6812     {
6813       *sym_num = entry->min_comdat_num;
6814       return 1;
6815     }
6816
6817   entry = XCNEW (struct cu_hash_table_entry);
6818   entry->cu = cu;
6819   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6820   entry->next = *slot;
6821   *slot = entry;
6822
6823   return 0;
6824 }
6825
6826 /* Record SYM_NUM to record of CU in HTABLE.  */
6827 static void
6828 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6829 {
6830   struct cu_hash_table_entry **slot, *entry;
6831
6832   slot = (struct cu_hash_table_entry **)
6833     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6834         NO_INSERT);
6835   entry = *slot;
6836
6837   entry->max_comdat_num = sym_num;
6838 }
6839
6840 /* Traverse the DIE (which is always comp_unit_die), and set up
6841    additional compilation units for each of the include files we see
6842    bracketed by BINCL/EINCL.  */
6843
6844 static void
6845 break_out_includes (dw_die_ref die)
6846 {
6847   dw_die_ref c;
6848   dw_die_ref unit = NULL;
6849   limbo_die_node *node, **pnode;
6850   htab_t cu_hash_table;
6851
6852   c = die->die_child;
6853   if (c) do {
6854     dw_die_ref prev = c;
6855     c = c->die_sib;
6856     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6857            || (unit && is_comdat_die (c)))
6858       {
6859         dw_die_ref next = c->die_sib;
6860
6861         /* This DIE is for a secondary CU; remove it from the main one.  */
6862         remove_child_with_prev (c, prev);
6863
6864         if (c->die_tag == DW_TAG_GNU_BINCL)
6865           unit = push_new_compile_unit (unit, c);
6866         else if (c->die_tag == DW_TAG_GNU_EINCL)
6867           unit = pop_compile_unit (unit);
6868         else
6869           add_child_die (unit, c);
6870         c = next;
6871         if (c == die->die_child)
6872           break;
6873       }
6874   } while (c != die->die_child);
6875
6876 #if 0
6877   /* We can only use this in debugging, since the frontend doesn't check
6878      to make sure that we leave every include file we enter.  */
6879   gcc_assert (!unit);
6880 #endif
6881
6882   assign_symbol_names (die);
6883   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6884   for (node = limbo_die_list, pnode = &limbo_die_list;
6885        node;
6886        node = node->next)
6887     {
6888       int is_dupl;
6889
6890       compute_section_prefix (node->die);
6891       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6892                         &comdat_symbol_number);
6893       assign_symbol_names (node->die);
6894       if (is_dupl)
6895         *pnode = node->next;
6896       else
6897         {
6898           pnode = &node->next;
6899           record_comdat_symbol_number (node->die, cu_hash_table,
6900                 comdat_symbol_number);
6901         }
6902     }
6903   htab_delete (cu_hash_table);
6904 }
6905
6906 /* Return non-zero if this DIE is a declaration.  */
6907
6908 static int
6909 is_declaration_die (dw_die_ref die)
6910 {
6911   dw_attr_ref a;
6912   unsigned ix;
6913
6914   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6915     if (a->dw_attr == DW_AT_declaration)
6916       return 1;
6917
6918   return 0;
6919 }
6920
6921 /* Return non-zero if this DIE is nested inside a subprogram.  */
6922
6923 static int
6924 is_nested_in_subprogram (dw_die_ref die)
6925 {
6926   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6927
6928   if (decl == NULL)
6929     decl = die;
6930   return local_scope_p (decl);
6931 }
6932
6933 /* Return non-zero if this DIE contains a defining declaration of a
6934    subprogram.  */
6935
6936 static int
6937 contains_subprogram_definition (dw_die_ref die)
6938 {
6939   dw_die_ref c;
6940
6941   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6942     return 1;
6943   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6944   return 0;
6945 }
6946
6947 /* Return non-zero if this is a type DIE that should be moved to a
6948    COMDAT .debug_types section.  */
6949
6950 static int
6951 should_move_die_to_comdat (dw_die_ref die)
6952 {
6953   switch (die->die_tag)
6954     {
6955     case DW_TAG_class_type:
6956     case DW_TAG_structure_type:
6957     case DW_TAG_enumeration_type:
6958     case DW_TAG_union_type:
6959       /* Don't move declarations, inlined instances, or types nested in a
6960          subprogram.  */
6961       if (is_declaration_die (die)
6962           || get_AT (die, DW_AT_abstract_origin)
6963           || is_nested_in_subprogram (die))
6964         return 0;
6965       /* A type definition should never contain a subprogram definition.  */
6966       gcc_assert (!contains_subprogram_definition (die));
6967       return 1;
6968     case DW_TAG_array_type:
6969     case DW_TAG_interface_type:
6970     case DW_TAG_pointer_type:
6971     case DW_TAG_reference_type:
6972     case DW_TAG_rvalue_reference_type:
6973     case DW_TAG_string_type:
6974     case DW_TAG_subroutine_type:
6975     case DW_TAG_ptr_to_member_type:
6976     case DW_TAG_set_type:
6977     case DW_TAG_subrange_type:
6978     case DW_TAG_base_type:
6979     case DW_TAG_const_type:
6980     case DW_TAG_file_type:
6981     case DW_TAG_packed_type:
6982     case DW_TAG_volatile_type:
6983     case DW_TAG_typedef:
6984     default:
6985       return 0;
6986     }
6987 }
6988
6989 /* Make a clone of DIE.  */
6990
6991 static dw_die_ref
6992 clone_die (dw_die_ref die)
6993 {
6994   dw_die_ref clone;
6995   dw_attr_ref a;
6996   unsigned ix;
6997
6998   clone = ggc_alloc_cleared_die_node ();
6999   clone->die_tag = die->die_tag;
7000
7001   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7002     add_dwarf_attr (clone, a);
7003
7004   return clone;
7005 }
7006
7007 /* Make a clone of the tree rooted at DIE.  */
7008
7009 static dw_die_ref
7010 clone_tree (dw_die_ref die)
7011 {
7012   dw_die_ref c;
7013   dw_die_ref clone = clone_die (die);
7014
7015   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7016
7017   return clone;
7018 }
7019
7020 /* Make a clone of DIE as a declaration.  */
7021
7022 static dw_die_ref
7023 clone_as_declaration (dw_die_ref die)
7024 {
7025   dw_die_ref clone;
7026   dw_die_ref decl;
7027   dw_attr_ref a;
7028   unsigned ix;
7029
7030   /* If the DIE is already a declaration, just clone it.  */
7031   if (is_declaration_die (die))
7032     return clone_die (die);
7033
7034   /* If the DIE is a specification, just clone its declaration DIE.  */
7035   decl = get_AT_ref (die, DW_AT_specification);
7036   if (decl != NULL)
7037     return clone_die (decl);
7038
7039   clone = ggc_alloc_cleared_die_node ();
7040   clone->die_tag = die->die_tag;
7041
7042   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7043     {
7044       /* We don't want to copy over all attributes.
7045          For example we don't want DW_AT_byte_size because otherwise we will no
7046          longer have a declaration and GDB will treat it as a definition.  */
7047
7048       switch (a->dw_attr)
7049         {
7050         case DW_AT_artificial:
7051         case DW_AT_containing_type:
7052         case DW_AT_external:
7053         case DW_AT_name:
7054         case DW_AT_type:
7055         case DW_AT_virtuality:
7056         case DW_AT_linkage_name:
7057         case DW_AT_MIPS_linkage_name:
7058           add_dwarf_attr (clone, a);
7059           break;
7060         case DW_AT_byte_size:
7061         default:
7062           break;
7063         }
7064     }
7065
7066   if (die->die_id.die_type_node)
7067     add_AT_die_ref (clone, DW_AT_signature, die);
7068
7069   add_AT_flag (clone, DW_AT_declaration, 1);
7070   return clone;
7071 }
7072
7073 /* Copy the declaration context to the new compile unit DIE.  This includes
7074    any surrounding namespace or type declarations.  If the DIE has an
7075    AT_specification attribute, it also includes attributes and children
7076    attached to the specification.  */
7077
7078 static void
7079 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7080 {
7081   dw_die_ref decl;
7082   dw_die_ref new_decl;
7083
7084   decl = get_AT_ref (die, DW_AT_specification);
7085   if (decl == NULL)
7086     decl = die;
7087   else
7088     {
7089       unsigned ix;
7090       dw_die_ref c;
7091       dw_attr_ref a;
7092
7093       /* Copy the type node pointer from the new DIE to the original
7094          declaration DIE so we can forward references later.  */
7095       decl->die_id.die_type_node = die->die_id.die_type_node;
7096
7097       remove_AT (die, DW_AT_specification);
7098
7099       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7100         {
7101           if (a->dw_attr != DW_AT_name
7102               && a->dw_attr != DW_AT_declaration
7103               && a->dw_attr != DW_AT_external)
7104             add_dwarf_attr (die, a);
7105         }
7106
7107       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7108     }
7109
7110   if (decl->die_parent != NULL
7111       && decl->die_parent->die_tag != DW_TAG_compile_unit
7112       && decl->die_parent->die_tag != DW_TAG_type_unit)
7113     {
7114       new_decl = copy_ancestor_tree (unit, decl, NULL);
7115       if (new_decl != NULL)
7116         {
7117           remove_AT (new_decl, DW_AT_signature);
7118           add_AT_specification (die, new_decl);
7119         }
7120     }
7121 }
7122
7123 /* Generate the skeleton ancestor tree for the given NODE, then clone
7124    the DIE and add the clone into the tree.  */
7125
7126 static void
7127 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7128 {
7129   if (node->new_die != NULL)
7130     return;
7131
7132   node->new_die = clone_as_declaration (node->old_die);
7133
7134   if (node->parent != NULL)
7135     {
7136       generate_skeleton_ancestor_tree (node->parent);
7137       add_child_die (node->parent->new_die, node->new_die);
7138     }
7139 }
7140
7141 /* Generate a skeleton tree of DIEs containing any declarations that are
7142    found in the original tree.  We traverse the tree looking for declaration
7143    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7144
7145 static void
7146 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7147 {
7148   skeleton_chain_node node;
7149   dw_die_ref c;
7150   dw_die_ref first;
7151   dw_die_ref prev = NULL;
7152   dw_die_ref next = NULL;
7153
7154   node.parent = parent;
7155
7156   first = c = parent->old_die->die_child;
7157   if (c)
7158     next = c->die_sib;
7159   if (c) do {
7160     if (prev == NULL || prev->die_sib == c)
7161       prev = c;
7162     c = next;
7163     next = (c == first ? NULL : c->die_sib);
7164     node.old_die = c;
7165     node.new_die = NULL;
7166     if (is_declaration_die (c))
7167       {
7168         /* Clone the existing DIE, move the original to the skeleton
7169            tree (which is in the main CU), and put the clone, with
7170            all the original's children, where the original came from.  */
7171         dw_die_ref clone = clone_die (c);
7172         move_all_children (c, clone);
7173
7174         replace_child (c, clone, prev);
7175         generate_skeleton_ancestor_tree (parent);
7176         add_child_die (parent->new_die, c);
7177         node.new_die = c;
7178         c = clone;
7179       }
7180     generate_skeleton_bottom_up (&node);
7181   } while (next != NULL);
7182 }
7183
7184 /* Wrapper function for generate_skeleton_bottom_up.  */
7185
7186 static dw_die_ref
7187 generate_skeleton (dw_die_ref die)
7188 {
7189   skeleton_chain_node node;
7190
7191   node.old_die = die;
7192   node.new_die = NULL;
7193   node.parent = NULL;
7194
7195   /* If this type definition is nested inside another type,
7196      always leave at least a declaration in its place.  */
7197   if (die->die_parent != NULL && is_type_die (die->die_parent))
7198     node.new_die = clone_as_declaration (die);
7199
7200   generate_skeleton_bottom_up (&node);
7201   return node.new_die;
7202 }
7203
7204 /* Remove the DIE from its parent, possibly replacing it with a cloned
7205    declaration.  The original DIE will be moved to a new compile unit
7206    so that existing references to it follow it to the new location.  If
7207    any of the original DIE's descendants is a declaration, we need to
7208    replace the original DIE with a skeleton tree and move the
7209    declarations back into the skeleton tree.  */
7210
7211 static dw_die_ref
7212 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7213 {
7214   dw_die_ref skeleton;
7215
7216   skeleton = generate_skeleton (child);
7217   if (skeleton == NULL)
7218     remove_child_with_prev (child, prev);
7219   else
7220     {
7221       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7222       replace_child (child, skeleton, prev);
7223     }
7224
7225   return skeleton;
7226 }
7227
7228 /* Traverse the DIE and set up additional .debug_types sections for each
7229    type worthy of being placed in a COMDAT section.  */
7230
7231 static void
7232 break_out_comdat_types (dw_die_ref die)
7233 {
7234   dw_die_ref c;
7235   dw_die_ref first;
7236   dw_die_ref prev = NULL;
7237   dw_die_ref next = NULL;
7238   dw_die_ref unit = NULL;
7239
7240   first = c = die->die_child;
7241   if (c)
7242     next = c->die_sib;
7243   if (c) do {
7244     if (prev == NULL || prev->die_sib == c)
7245       prev = c;
7246     c = next;
7247     next = (c == first ? NULL : c->die_sib);
7248     if (should_move_die_to_comdat (c))
7249       {
7250         dw_die_ref replacement;
7251         comdat_type_node_ref type_node;
7252
7253         /* Create a new type unit DIE as the root for the new tree, and
7254            add it to the list of comdat types.  */
7255         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7256         add_AT_unsigned (unit, DW_AT_language,
7257                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7258         type_node = ggc_alloc_cleared_comdat_type_node ();
7259         type_node->root_die = unit;
7260         type_node->next = comdat_type_list;
7261         comdat_type_list = type_node;
7262
7263         /* Generate the type signature.  */
7264         generate_type_signature (c, type_node);
7265
7266         /* Copy the declaration context, attributes, and children of the
7267            declaration into the new compile unit DIE.  */
7268         copy_declaration_context (unit, c);
7269
7270         /* Remove this DIE from the main CU.  */
7271         replacement = remove_child_or_replace_with_skeleton (c, prev);
7272
7273         /* Break out nested types into their own type units.  */
7274         break_out_comdat_types (c);
7275
7276         /* Add the DIE to the new compunit.  */
7277         add_child_die (unit, c);
7278
7279         if (replacement != NULL)
7280           c = replacement;
7281       }
7282     else if (c->die_tag == DW_TAG_namespace
7283              || c->die_tag == DW_TAG_class_type
7284              || c->die_tag == DW_TAG_structure_type
7285              || c->die_tag == DW_TAG_union_type)
7286       {
7287         /* Look for nested types that can be broken out.  */
7288         break_out_comdat_types (c);
7289       }
7290   } while (next != NULL);
7291 }
7292
7293 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7294
7295 struct decl_table_entry
7296 {
7297   dw_die_ref orig;
7298   dw_die_ref copy;
7299 };
7300
7301 /* Routines to manipulate hash table of copied declarations.  */
7302
7303 static hashval_t
7304 htab_decl_hash (const void *of)
7305 {
7306   const struct decl_table_entry *const entry =
7307     (const struct decl_table_entry *) of;
7308
7309   return htab_hash_pointer (entry->orig);
7310 }
7311
7312 static int
7313 htab_decl_eq (const void *of1, const void *of2)
7314 {
7315   const struct decl_table_entry *const entry1 =
7316     (const struct decl_table_entry *) of1;
7317   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7318
7319   return entry1->orig == entry2;
7320 }
7321
7322 static void
7323 htab_decl_del (void *what)
7324 {
7325   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7326
7327   free (entry);
7328 }
7329
7330 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7331    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7332    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7333    to check if the ancestor has already been copied into UNIT.  */
7334
7335 static dw_die_ref
7336 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7337 {
7338   dw_die_ref parent = die->die_parent;
7339   dw_die_ref new_parent = unit;
7340   dw_die_ref copy;
7341   void **slot = NULL;
7342   struct decl_table_entry *entry = NULL;
7343
7344   if (decl_table)
7345     {
7346       /* Check if the entry has already been copied to UNIT.  */
7347       slot = htab_find_slot_with_hash (decl_table, die,
7348                                        htab_hash_pointer (die), INSERT);
7349       if (*slot != HTAB_EMPTY_ENTRY)
7350         {
7351           entry = (struct decl_table_entry *) *slot;
7352           return entry->copy;
7353         }
7354
7355       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7356       entry = XCNEW (struct decl_table_entry);
7357       entry->orig = die;
7358       entry->copy = NULL;
7359       *slot = entry;
7360     }
7361
7362   if (parent != NULL)
7363     {
7364       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7365       if (spec != NULL)
7366         parent = spec;
7367       if (parent->die_tag != DW_TAG_compile_unit
7368           && parent->die_tag != DW_TAG_type_unit)
7369         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7370     }
7371
7372   copy = clone_as_declaration (die);
7373   add_child_die (new_parent, copy);
7374
7375   if (decl_table != NULL)
7376     {
7377       /* Record the pointer to the copy.  */
7378       entry->copy = copy;
7379     }
7380
7381   return copy;
7382 }
7383
7384 /* Walk the DIE and its children, looking for references to incomplete
7385    or trivial types that are unmarked (i.e., that are not in the current
7386    type_unit).  */
7387
7388 static void
7389 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7390 {
7391   dw_die_ref c;
7392   dw_attr_ref a;
7393   unsigned ix;
7394
7395   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7396     {
7397       if (AT_class (a) == dw_val_class_die_ref)
7398         {
7399           dw_die_ref targ = AT_ref (a);
7400           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7401           void **slot;
7402           struct decl_table_entry *entry;
7403
7404           if (targ->die_mark != 0 || type_node != NULL)
7405             continue;
7406
7407           slot = htab_find_slot_with_hash (decl_table, targ,
7408                                            htab_hash_pointer (targ), INSERT);
7409
7410           if (*slot != HTAB_EMPTY_ENTRY)
7411             {
7412               /* TARG has already been copied, so we just need to
7413                  modify the reference to point to the copy.  */
7414               entry = (struct decl_table_entry *) *slot;
7415               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7416             }
7417           else
7418             {
7419               dw_die_ref parent = unit;
7420               dw_die_ref copy = clone_tree (targ);
7421
7422               /* Make sure the cloned tree is marked as part of the
7423                  type unit.  */
7424               mark_dies (copy);
7425
7426               /* Record in DECL_TABLE that TARG has been copied.
7427                  Need to do this now, before the recursive call,
7428                  because DECL_TABLE may be expanded and SLOT
7429                  would no longer be a valid pointer.  */
7430               entry = XCNEW (struct decl_table_entry);
7431               entry->orig = targ;
7432               entry->copy = copy;
7433               *slot = entry;
7434
7435               /* If TARG has surrounding context, copy its ancestor tree
7436                  into the new type unit.  */
7437               if (targ->die_parent != NULL
7438                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7439                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7440                 parent = copy_ancestor_tree (unit, targ->die_parent,
7441                                              decl_table);
7442
7443               add_child_die (parent, copy);
7444               a->dw_attr_val.v.val_die_ref.die = copy;
7445
7446               /* Make sure the newly-copied DIE is walked.  If it was
7447                  installed in a previously-added context, it won't
7448                  get visited otherwise.  */
7449               if (parent != unit)
7450                 {
7451                   /* Find the highest point of the newly-added tree,
7452                      mark each node along the way, and walk from there.  */
7453                   parent->die_mark = 1;
7454                   while (parent->die_parent
7455                          && parent->die_parent->die_mark == 0)
7456                     {
7457                       parent = parent->die_parent;
7458                       parent->die_mark = 1;
7459                     }
7460                   copy_decls_walk (unit, parent, decl_table);
7461                 }
7462             }
7463         }
7464     }
7465
7466   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7467 }
7468
7469 /* Copy declarations for "unworthy" types into the new comdat section.
7470    Incomplete types, modified types, and certain other types aren't broken
7471    out into comdat sections of their own, so they don't have a signature,
7472    and we need to copy the declaration into the same section so that we
7473    don't have an external reference.  */
7474
7475 static void
7476 copy_decls_for_unworthy_types (dw_die_ref unit)
7477 {
7478   htab_t decl_table;
7479
7480   mark_dies (unit);
7481   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7482   copy_decls_walk (unit, unit, decl_table);
7483   htab_delete (decl_table);
7484   unmark_dies (unit);
7485 }
7486
7487 /* Traverse the DIE and add a sibling attribute if it may have the
7488    effect of speeding up access to siblings.  To save some space,
7489    avoid generating sibling attributes for DIE's without children.  */
7490
7491 static void
7492 add_sibling_attributes (dw_die_ref die)
7493 {
7494   dw_die_ref c;
7495
7496   if (! die->die_child)
7497     return;
7498
7499   if (die->die_parent && die != die->die_parent->die_child)
7500     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7501
7502   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7503 }
7504
7505 /* Output all location lists for the DIE and its children.  */
7506
7507 static void
7508 output_location_lists (dw_die_ref die)
7509 {
7510   dw_die_ref c;
7511   dw_attr_ref a;
7512   unsigned ix;
7513
7514   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7515     if (AT_class (a) == dw_val_class_loc_list)
7516       output_loc_list (AT_loc_list (a));
7517
7518   FOR_EACH_CHILD (die, c, output_location_lists (c));
7519 }
7520
7521 /* The format of each DIE (and its attribute value pairs) is encoded in an
7522    abbreviation table.  This routine builds the abbreviation table and assigns
7523    a unique abbreviation id for each abbreviation entry.  The children of each
7524    die are visited recursively.  */
7525
7526 static void
7527 build_abbrev_table (dw_die_ref die)
7528 {
7529   unsigned long abbrev_id;
7530   unsigned int n_alloc;
7531   dw_die_ref c;
7532   dw_attr_ref a;
7533   unsigned ix;
7534
7535   /* Scan the DIE references, and mark as external any that refer to
7536      DIEs from other CUs (i.e. those which are not marked).  */
7537   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7538     if (AT_class (a) == dw_val_class_die_ref
7539         && AT_ref (a)->die_mark == 0)
7540       {
7541         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7542         set_AT_ref_external (a, 1);
7543       }
7544
7545   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7546     {
7547       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7548       dw_attr_ref die_a, abbrev_a;
7549       unsigned ix;
7550       bool ok = true;
7551
7552       if (abbrev->die_tag != die->die_tag)
7553         continue;
7554       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7555         continue;
7556
7557       if (VEC_length (dw_attr_node, abbrev->die_attr)
7558           != VEC_length (dw_attr_node, die->die_attr))
7559         continue;
7560
7561       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7562         {
7563           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7564           if ((abbrev_a->dw_attr != die_a->dw_attr)
7565               || (value_format (abbrev_a) != value_format (die_a)))
7566             {
7567               ok = false;
7568               break;
7569             }
7570         }
7571       if (ok)
7572         break;
7573     }
7574
7575   if (abbrev_id >= abbrev_die_table_in_use)
7576     {
7577       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7578         {
7579           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7580           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7581                                             n_alloc);
7582
7583           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7584                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7585           abbrev_die_table_allocated = n_alloc;
7586         }
7587
7588       ++abbrev_die_table_in_use;
7589       abbrev_die_table[abbrev_id] = die;
7590     }
7591
7592   die->die_abbrev = abbrev_id;
7593   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7594 }
7595 \f
7596 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7597
7598 static int
7599 constant_size (unsigned HOST_WIDE_INT value)
7600 {
7601   int log;
7602
7603   if (value == 0)
7604     log = 0;
7605   else
7606     log = floor_log2 (value);
7607
7608   log = log / 8;
7609   log = 1 << (floor_log2 (log) + 1);
7610
7611   return log;
7612 }
7613
7614 /* Return the size of a DIE as it is represented in the
7615    .debug_info section.  */
7616
7617 static unsigned long
7618 size_of_die (dw_die_ref die)
7619 {
7620   unsigned long size = 0;
7621   dw_attr_ref a;
7622   unsigned ix;
7623
7624   size += size_of_uleb128 (die->die_abbrev);
7625   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7626     {
7627       switch (AT_class (a))
7628         {
7629         case dw_val_class_addr:
7630           size += DWARF2_ADDR_SIZE;
7631           break;
7632         case dw_val_class_offset:
7633           size += DWARF_OFFSET_SIZE;
7634           break;
7635         case dw_val_class_loc:
7636           {
7637             unsigned long lsize = size_of_locs (AT_loc (a));
7638
7639             /* Block length.  */
7640             if (dwarf_version >= 4)
7641               size += size_of_uleb128 (lsize);
7642             else
7643               size += constant_size (lsize);
7644             size += lsize;
7645           }
7646           break;
7647         case dw_val_class_loc_list:
7648           size += DWARF_OFFSET_SIZE;
7649           break;
7650         case dw_val_class_range_list:
7651           size += DWARF_OFFSET_SIZE;
7652           break;
7653         case dw_val_class_const:
7654           size += size_of_sleb128 (AT_int (a));
7655           break;
7656         case dw_val_class_unsigned_const:
7657           {
7658             int csize = constant_size (AT_unsigned (a));
7659             if (dwarf_version == 3
7660                 && a->dw_attr == DW_AT_data_member_location
7661                 && csize >= 4)
7662               size += size_of_uleb128 (AT_unsigned (a));
7663             else
7664               size += csize;
7665           }
7666           break;
7667         case dw_val_class_const_double:
7668           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7669           if (HOST_BITS_PER_WIDE_INT >= 64)
7670             size++; /* block */
7671           break;
7672         case dw_val_class_vec:
7673           size += constant_size (a->dw_attr_val.v.val_vec.length
7674                                  * a->dw_attr_val.v.val_vec.elt_size)
7675                   + a->dw_attr_val.v.val_vec.length
7676                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7677           break;
7678         case dw_val_class_flag:
7679           if (dwarf_version >= 4)
7680             /* Currently all add_AT_flag calls pass in 1 as last argument,
7681                so DW_FORM_flag_present can be used.  If that ever changes,
7682                we'll need to use DW_FORM_flag and have some optimization
7683                in build_abbrev_table that will change those to
7684                DW_FORM_flag_present if it is set to 1 in all DIEs using
7685                the same abbrev entry.  */
7686             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7687           else
7688             size += 1;
7689           break;
7690         case dw_val_class_die_ref:
7691           if (AT_ref_external (a))
7692             {
7693               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7694                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7695                  is sized by target address length, whereas in DWARF3
7696                  it's always sized as an offset.  */
7697               if (use_debug_types)
7698                 size += DWARF_TYPE_SIGNATURE_SIZE;
7699               else if (dwarf_version == 2)
7700                 size += DWARF2_ADDR_SIZE;
7701               else
7702                 size += DWARF_OFFSET_SIZE;
7703             }
7704           else
7705             size += DWARF_OFFSET_SIZE;
7706           break;
7707         case dw_val_class_fde_ref:
7708           size += DWARF_OFFSET_SIZE;
7709           break;
7710         case dw_val_class_lbl_id:
7711           size += DWARF2_ADDR_SIZE;
7712           break;
7713         case dw_val_class_lineptr:
7714         case dw_val_class_macptr:
7715           size += DWARF_OFFSET_SIZE;
7716           break;
7717         case dw_val_class_str:
7718           if (AT_string_form (a) == DW_FORM_strp)
7719             size += DWARF_OFFSET_SIZE;
7720           else
7721             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7722           break;
7723         case dw_val_class_file:
7724           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7725           break;
7726         case dw_val_class_data8:
7727           size += 8;
7728           break;
7729         case dw_val_class_vms_delta:
7730           size += DWARF_OFFSET_SIZE;
7731           break;
7732         default:
7733           gcc_unreachable ();
7734         }
7735     }
7736
7737   return size;
7738 }
7739
7740 /* Size the debugging information associated with a given DIE.  Visits the
7741    DIE's children recursively.  Updates the global variable next_die_offset, on
7742    each time through.  Uses the current value of next_die_offset to update the
7743    die_offset field in each DIE.  */
7744
7745 static void
7746 calc_die_sizes (dw_die_ref die)
7747 {
7748   dw_die_ref c;
7749
7750   gcc_assert (die->die_offset == 0
7751               || (unsigned long int) die->die_offset == next_die_offset);
7752   die->die_offset = next_die_offset;
7753   next_die_offset += size_of_die (die);
7754
7755   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7756
7757   if (die->die_child != NULL)
7758     /* Count the null byte used to terminate sibling lists.  */
7759     next_die_offset += 1;
7760 }
7761
7762 /* Size just the base type children at the start of the CU.
7763    This is needed because build_abbrev needs to size locs
7764    and sizing of type based stack ops needs to know die_offset
7765    values for the base types.  */
7766
7767 static void
7768 calc_base_type_die_sizes (void)
7769 {
7770   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7771   unsigned int i;
7772   dw_die_ref base_type;
7773 #if ENABLE_ASSERT_CHECKING
7774   dw_die_ref prev = comp_unit_die ()->die_child;
7775 #endif
7776
7777   die_offset += size_of_die (comp_unit_die ());
7778   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7779     {
7780 #if ENABLE_ASSERT_CHECKING
7781       gcc_assert (base_type->die_offset == 0
7782                   && prev->die_sib == base_type
7783                   && base_type->die_child == NULL
7784                   && base_type->die_abbrev);
7785       prev = base_type;
7786 #endif
7787       base_type->die_offset = die_offset;
7788       die_offset += size_of_die (base_type);
7789     }
7790 }
7791
7792 /* Set the marks for a die and its children.  We do this so
7793    that we know whether or not a reference needs to use FORM_ref_addr; only
7794    DIEs in the same CU will be marked.  We used to clear out the offset
7795    and use that as the flag, but ran into ordering problems.  */
7796
7797 static void
7798 mark_dies (dw_die_ref die)
7799 {
7800   dw_die_ref c;
7801
7802   gcc_assert (!die->die_mark);
7803
7804   die->die_mark = 1;
7805   FOR_EACH_CHILD (die, c, mark_dies (c));
7806 }
7807
7808 /* Clear the marks for a die and its children.  */
7809
7810 static void
7811 unmark_dies (dw_die_ref die)
7812 {
7813   dw_die_ref c;
7814
7815   if (! use_debug_types)
7816     gcc_assert (die->die_mark);
7817
7818   die->die_mark = 0;
7819   FOR_EACH_CHILD (die, c, unmark_dies (c));
7820 }
7821
7822 /* Clear the marks for a die, its children and referred dies.  */
7823
7824 static void
7825 unmark_all_dies (dw_die_ref die)
7826 {
7827   dw_die_ref c;
7828   dw_attr_ref a;
7829   unsigned ix;
7830
7831   if (!die->die_mark)
7832     return;
7833   die->die_mark = 0;
7834
7835   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7836
7837   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7838     if (AT_class (a) == dw_val_class_die_ref)
7839       unmark_all_dies (AT_ref (a));
7840 }
7841
7842 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7843    generated for the compilation unit.  */
7844
7845 static unsigned long
7846 size_of_pubnames (VEC (pubname_entry, gc) * names)
7847 {
7848   unsigned long size;
7849   unsigned i;
7850   pubname_ref p;
7851
7852   size = DWARF_PUBNAMES_HEADER_SIZE;
7853   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7854     if (names != pubtype_table
7855         || p->die->die_offset != 0
7856         || !flag_eliminate_unused_debug_types)
7857       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7858
7859   size += DWARF_OFFSET_SIZE;
7860   return size;
7861 }
7862
7863 /* Return the size of the information in the .debug_aranges section.  */
7864
7865 static unsigned long
7866 size_of_aranges (void)
7867 {
7868   unsigned long size;
7869
7870   size = DWARF_ARANGES_HEADER_SIZE;
7871
7872   /* Count the address/length pair for this compilation unit.  */
7873   if (text_section_used)
7874     size += 2 * DWARF2_ADDR_SIZE;
7875   if (cold_text_section_used)
7876     size += 2 * DWARF2_ADDR_SIZE;
7877   if (have_multiple_function_sections)
7878     {
7879       unsigned fde_idx;
7880       dw_fde_ref fde;
7881
7882       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7883         {
7884           if (!fde->in_std_section)
7885             size += 2 * DWARF2_ADDR_SIZE;
7886           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7887             size += 2 * DWARF2_ADDR_SIZE;
7888         }
7889     }
7890
7891   /* Count the two zero words used to terminated the address range table.  */
7892   size += 2 * DWARF2_ADDR_SIZE;
7893   return size;
7894 }
7895 \f
7896 /* Select the encoding of an attribute value.  */
7897
7898 static enum dwarf_form
7899 value_format (dw_attr_ref a)
7900 {
7901   switch (a->dw_attr_val.val_class)
7902     {
7903     case dw_val_class_addr:
7904       /* Only very few attributes allow DW_FORM_addr.  */
7905       switch (a->dw_attr)
7906         {
7907         case DW_AT_low_pc:
7908         case DW_AT_high_pc:
7909         case DW_AT_entry_pc:
7910         case DW_AT_trampoline:
7911           return DW_FORM_addr;
7912         default:
7913           break;
7914         }
7915       switch (DWARF2_ADDR_SIZE)
7916         {
7917         case 1:
7918           return DW_FORM_data1;
7919         case 2:
7920           return DW_FORM_data2;
7921         case 4:
7922           return DW_FORM_data4;
7923         case 8:
7924           return DW_FORM_data8;
7925         default:
7926           gcc_unreachable ();
7927         }
7928     case dw_val_class_range_list:
7929     case dw_val_class_loc_list:
7930       if (dwarf_version >= 4)
7931         return DW_FORM_sec_offset;
7932       /* FALLTHRU */
7933     case dw_val_class_vms_delta:
7934     case dw_val_class_offset:
7935       switch (DWARF_OFFSET_SIZE)
7936         {
7937         case 4:
7938           return DW_FORM_data4;
7939         case 8:
7940           return DW_FORM_data8;
7941         default:
7942           gcc_unreachable ();
7943         }
7944     case dw_val_class_loc:
7945       if (dwarf_version >= 4)
7946         return DW_FORM_exprloc;
7947       switch (constant_size (size_of_locs (AT_loc (a))))
7948         {
7949         case 1:
7950           return DW_FORM_block1;
7951         case 2:
7952           return DW_FORM_block2;
7953         default:
7954           gcc_unreachable ();
7955         }
7956     case dw_val_class_const:
7957       return DW_FORM_sdata;
7958     case dw_val_class_unsigned_const:
7959       switch (constant_size (AT_unsigned (a)))
7960         {
7961         case 1:
7962           return DW_FORM_data1;
7963         case 2:
7964           return DW_FORM_data2;
7965         case 4:
7966           /* In DWARF3 DW_AT_data_member_location with
7967              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7968              constant, so we need to use DW_FORM_udata if we need
7969              a large constant.  */
7970           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7971             return DW_FORM_udata;
7972           return DW_FORM_data4;
7973         case 8:
7974           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7975             return DW_FORM_udata;
7976           return DW_FORM_data8;
7977         default:
7978           gcc_unreachable ();
7979         }
7980     case dw_val_class_const_double:
7981       switch (HOST_BITS_PER_WIDE_INT)
7982         {
7983         case 8:
7984           return DW_FORM_data2;
7985         case 16:
7986           return DW_FORM_data4;
7987         case 32:
7988           return DW_FORM_data8;
7989         case 64:
7990         default:
7991           return DW_FORM_block1;
7992         }
7993     case dw_val_class_vec:
7994       switch (constant_size (a->dw_attr_val.v.val_vec.length
7995                              * a->dw_attr_val.v.val_vec.elt_size))
7996         {
7997         case 1:
7998           return DW_FORM_block1;
7999         case 2:
8000           return DW_FORM_block2;
8001         case 4:
8002           return DW_FORM_block4;
8003         default:
8004           gcc_unreachable ();
8005         }
8006     case dw_val_class_flag:
8007       if (dwarf_version >= 4)
8008         {
8009           /* Currently all add_AT_flag calls pass in 1 as last argument,
8010              so DW_FORM_flag_present can be used.  If that ever changes,
8011              we'll need to use DW_FORM_flag and have some optimization
8012              in build_abbrev_table that will change those to
8013              DW_FORM_flag_present if it is set to 1 in all DIEs using
8014              the same abbrev entry.  */
8015           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8016           return DW_FORM_flag_present;
8017         }
8018       return DW_FORM_flag;
8019     case dw_val_class_die_ref:
8020       if (AT_ref_external (a))
8021         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8022       else
8023         return DW_FORM_ref;
8024     case dw_val_class_fde_ref:
8025       return DW_FORM_data;
8026     case dw_val_class_lbl_id:
8027       return DW_FORM_addr;
8028     case dw_val_class_lineptr:
8029     case dw_val_class_macptr:
8030       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8031     case dw_val_class_str:
8032       return AT_string_form (a);
8033     case dw_val_class_file:
8034       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8035         {
8036         case 1:
8037           return DW_FORM_data1;
8038         case 2:
8039           return DW_FORM_data2;
8040         case 4:
8041           return DW_FORM_data4;
8042         default:
8043           gcc_unreachable ();
8044         }
8045
8046     case dw_val_class_data8:
8047       return DW_FORM_data8;
8048
8049     default:
8050       gcc_unreachable ();
8051     }
8052 }
8053
8054 /* Output the encoding of an attribute value.  */
8055
8056 static void
8057 output_value_format (dw_attr_ref a)
8058 {
8059   enum dwarf_form form = value_format (a);
8060
8061   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8062 }
8063
8064 /* Output the .debug_abbrev section which defines the DIE abbreviation
8065    table.  */
8066
8067 static void
8068 output_abbrev_section (void)
8069 {
8070   unsigned long abbrev_id;
8071
8072   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8073     {
8074       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8075       unsigned ix;
8076       dw_attr_ref a_attr;
8077
8078       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8079       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8080                                    dwarf_tag_name (abbrev->die_tag));
8081
8082       if (abbrev->die_child != NULL)
8083         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8084       else
8085         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8086
8087       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8088            ix++)
8089         {
8090           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8091                                        dwarf_attr_name (a_attr->dw_attr));
8092           output_value_format (a_attr);
8093         }
8094
8095       dw2_asm_output_data (1, 0, NULL);
8096       dw2_asm_output_data (1, 0, NULL);
8097     }
8098
8099   /* Terminate the table.  */
8100   dw2_asm_output_data (1, 0, NULL);
8101 }
8102
8103 /* Output a symbol we can use to refer to this DIE from another CU.  */
8104
8105 static inline void
8106 output_die_symbol (dw_die_ref die)
8107 {
8108   char *sym = die->die_id.die_symbol;
8109
8110   if (sym == 0)
8111     return;
8112
8113   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8114     /* We make these global, not weak; if the target doesn't support
8115        .linkonce, it doesn't support combining the sections, so debugging
8116        will break.  */
8117     targetm.asm_out.globalize_label (asm_out_file, sym);
8118
8119   ASM_OUTPUT_LABEL (asm_out_file, sym);
8120 }
8121
8122 /* Return a new location list, given the begin and end range, and the
8123    expression.  */
8124
8125 static inline dw_loc_list_ref
8126 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8127               const char *section)
8128 {
8129   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8130
8131   retlist->begin = begin;
8132   retlist->end = end;
8133   retlist->expr = expr;
8134   retlist->section = section;
8135
8136   return retlist;
8137 }
8138
8139 /* Generate a new internal symbol for this location list node, if it
8140    hasn't got one yet.  */
8141
8142 static inline void
8143 gen_llsym (dw_loc_list_ref list)
8144 {
8145   gcc_assert (!list->ll_symbol);
8146   list->ll_symbol = gen_internal_sym ("LLST");
8147 }
8148
8149 /* Output the location list given to us.  */
8150
8151 static void
8152 output_loc_list (dw_loc_list_ref list_head)
8153 {
8154   dw_loc_list_ref curr = list_head;
8155
8156   if (list_head->emitted)
8157     return;
8158   list_head->emitted = true;
8159
8160   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8161
8162   /* Walk the location list, and output each range + expression.  */
8163   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8164     {
8165       unsigned long size;
8166       /* Don't output an entry that starts and ends at the same address.  */
8167       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8168         continue;
8169       size = size_of_locs (curr->expr);
8170       /* If the expression is too large, drop it on the floor.  We could
8171          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8172          in the expression, but >= 64KB expressions for a single value
8173          in a single range are unlikely very useful.  */
8174       if (size > 0xffff)
8175         continue;
8176       if (!have_multiple_function_sections)
8177         {
8178           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8179                                 "Location list begin address (%s)",
8180                                 list_head->ll_symbol);
8181           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8182                                 "Location list end address (%s)",
8183                                 list_head->ll_symbol);
8184         }
8185       else
8186         {
8187           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8188                                "Location list begin address (%s)",
8189                                list_head->ll_symbol);
8190           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8191                                "Location list end address (%s)",
8192                                list_head->ll_symbol);
8193         }
8194
8195       /* Output the block length for this list of location operations.  */
8196       gcc_assert (size <= 0xffff);
8197       dw2_asm_output_data (2, size, "%s", "Location expression size");
8198
8199       output_loc_sequence (curr->expr, -1);
8200     }
8201
8202   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8203                        "Location list terminator begin (%s)",
8204                        list_head->ll_symbol);
8205   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8206                        "Location list terminator end (%s)",
8207                        list_head->ll_symbol);
8208 }
8209
8210 /* Output a type signature.  */
8211
8212 static inline void
8213 output_signature (const char *sig, const char *name)
8214 {
8215   int i;
8216
8217   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8218     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8219 }
8220
8221 /* Output the DIE and its attributes.  Called recursively to generate
8222    the definitions of each child DIE.  */
8223
8224 static void
8225 output_die (dw_die_ref die)
8226 {
8227   dw_attr_ref a;
8228   dw_die_ref c;
8229   unsigned long size;
8230   unsigned ix;
8231
8232   /* If someone in another CU might refer to us, set up a symbol for
8233      them to point to.  */
8234   if (! use_debug_types && die->die_id.die_symbol)
8235     output_die_symbol (die);
8236
8237   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8238                                (unsigned long)die->die_offset,
8239                                dwarf_tag_name (die->die_tag));
8240
8241   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8242     {
8243       const char *name = dwarf_attr_name (a->dw_attr);
8244
8245       switch (AT_class (a))
8246         {
8247         case dw_val_class_addr:
8248           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8249           break;
8250
8251         case dw_val_class_offset:
8252           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8253                                "%s", name);
8254           break;
8255
8256         case dw_val_class_range_list:
8257           {
8258             char *p = strchr (ranges_section_label, '\0');
8259
8260             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8261                      a->dw_attr_val.v.val_offset);
8262             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8263                                    debug_ranges_section, "%s", name);
8264             *p = '\0';
8265           }
8266           break;
8267
8268         case dw_val_class_loc:
8269           size = size_of_locs (AT_loc (a));
8270
8271           /* Output the block length for this list of location operations.  */
8272           if (dwarf_version >= 4)
8273             dw2_asm_output_data_uleb128 (size, "%s", name);
8274           else
8275             dw2_asm_output_data (constant_size (size), size, "%s", name);
8276
8277           output_loc_sequence (AT_loc (a), -1);
8278           break;
8279
8280         case dw_val_class_const:
8281           /* ??? It would be slightly more efficient to use a scheme like is
8282              used for unsigned constants below, but gdb 4.x does not sign
8283              extend.  Gdb 5.x does sign extend.  */
8284           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8285           break;
8286
8287         case dw_val_class_unsigned_const:
8288           {
8289             int csize = constant_size (AT_unsigned (a));
8290             if (dwarf_version == 3
8291                 && a->dw_attr == DW_AT_data_member_location
8292                 && csize >= 4)
8293               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8294             else
8295               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8296           }
8297           break;
8298
8299         case dw_val_class_const_double:
8300           {
8301             unsigned HOST_WIDE_INT first, second;
8302
8303             if (HOST_BITS_PER_WIDE_INT >= 64)
8304               dw2_asm_output_data (1,
8305                                    2 * HOST_BITS_PER_WIDE_INT
8306                                    / HOST_BITS_PER_CHAR,
8307                                    NULL);
8308
8309             if (WORDS_BIG_ENDIAN)
8310               {
8311                 first = a->dw_attr_val.v.val_double.high;
8312                 second = a->dw_attr_val.v.val_double.low;
8313               }
8314             else
8315               {
8316                 first = a->dw_attr_val.v.val_double.low;
8317                 second = a->dw_attr_val.v.val_double.high;
8318               }
8319
8320             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8321                                  first, name);
8322             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8323                                  second, NULL);
8324           }
8325           break;
8326
8327         case dw_val_class_vec:
8328           {
8329             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8330             unsigned int len = a->dw_attr_val.v.val_vec.length;
8331             unsigned int i;
8332             unsigned char *p;
8333
8334             dw2_asm_output_data (constant_size (len * elt_size),
8335                                  len * elt_size, "%s", name);
8336             if (elt_size > sizeof (HOST_WIDE_INT))
8337               {
8338                 elt_size /= 2;
8339                 len *= 2;
8340               }
8341             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8342                  i < len;
8343                  i++, p += elt_size)
8344               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8345                                    "fp or vector constant word %u", i);
8346             break;
8347           }
8348
8349         case dw_val_class_flag:
8350           if (dwarf_version >= 4)
8351             {
8352               /* Currently all add_AT_flag calls pass in 1 as last argument,
8353                  so DW_FORM_flag_present can be used.  If that ever changes,
8354                  we'll need to use DW_FORM_flag and have some optimization
8355                  in build_abbrev_table that will change those to
8356                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8357                  the same abbrev entry.  */
8358               gcc_assert (AT_flag (a) == 1);
8359               if (flag_debug_asm)
8360                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8361                          ASM_COMMENT_START, name);
8362               break;
8363             }
8364           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8365           break;
8366
8367         case dw_val_class_loc_list:
8368           {
8369             char *sym = AT_loc_list (a)->ll_symbol;
8370
8371             gcc_assert (sym);
8372             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8373                                    "%s", name);
8374           }
8375           break;
8376
8377         case dw_val_class_die_ref:
8378           if (AT_ref_external (a))
8379             {
8380               if (use_debug_types)
8381                 {
8382                   comdat_type_node_ref type_node =
8383                     AT_ref (a)->die_id.die_type_node;
8384
8385                   gcc_assert (type_node);
8386                   output_signature (type_node->signature, name);
8387                 }
8388               else
8389                 {
8390                   char *sym = AT_ref (a)->die_id.die_symbol;
8391                   int size;
8392
8393                   gcc_assert (sym);
8394                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8395                      length, whereas in DWARF3 it's always sized as an
8396                      offset.  */
8397                   if (dwarf_version == 2)
8398                     size = DWARF2_ADDR_SIZE;
8399                   else
8400                     size = DWARF_OFFSET_SIZE;
8401                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8402                                          name);
8403                 }
8404             }
8405           else
8406             {
8407               gcc_assert (AT_ref (a)->die_offset);
8408               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8409                                    "%s", name);
8410             }
8411           break;
8412
8413         case dw_val_class_fde_ref:
8414           {
8415             char l1[20];
8416
8417             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8418                                          a->dw_attr_val.v.val_fde_index * 2);
8419             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8420                                    "%s", name);
8421           }
8422           break;
8423
8424         case dw_val_class_vms_delta:
8425           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8426                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8427                                     "%s", name);
8428           break;
8429
8430         case dw_val_class_lbl_id:
8431           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8432           break;
8433
8434         case dw_val_class_lineptr:
8435           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8436                                  debug_line_section, "%s", name);
8437           break;
8438
8439         case dw_val_class_macptr:
8440           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8441                                  debug_macinfo_section, "%s", name);
8442           break;
8443
8444         case dw_val_class_str:
8445           if (AT_string_form (a) == DW_FORM_strp)
8446             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8447                                    a->dw_attr_val.v.val_str->label,
8448                                    debug_str_section,
8449                                    "%s: \"%s\"", name, AT_string (a));
8450           else
8451             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8452           break;
8453
8454         case dw_val_class_file:
8455           {
8456             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8457
8458             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8459                                  a->dw_attr_val.v.val_file->filename);
8460             break;
8461           }
8462
8463         case dw_val_class_data8:
8464           {
8465             int i;
8466
8467             for (i = 0; i < 8; i++)
8468               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8469                                    i == 0 ? "%s" : NULL, name);
8470             break;
8471           }
8472
8473         default:
8474           gcc_unreachable ();
8475         }
8476     }
8477
8478   FOR_EACH_CHILD (die, c, output_die (c));
8479
8480   /* Add null byte to terminate sibling list.  */
8481   if (die->die_child != NULL)
8482     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8483                          (unsigned long) die->die_offset);
8484 }
8485
8486 /* Output the compilation unit that appears at the beginning of the
8487    .debug_info section, and precedes the DIE descriptions.  */
8488
8489 static void
8490 output_compilation_unit_header (void)
8491 {
8492   int ver = dwarf_version;
8493
8494   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8495     dw2_asm_output_data (4, 0xffffffff,
8496       "Initial length escape value indicating 64-bit DWARF extension");
8497   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8498                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8499                        "Length of Compilation Unit Info");
8500   dw2_asm_output_data (2, ver, "DWARF version number");
8501   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8502                          debug_abbrev_section,
8503                          "Offset Into Abbrev. Section");
8504   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8505 }
8506
8507 /* Output the compilation unit DIE and its children.  */
8508
8509 static void
8510 output_comp_unit (dw_die_ref die, int output_if_empty)
8511 {
8512   const char *secname;
8513   char *oldsym, *tmp;
8514
8515   /* Unless we are outputting main CU, we may throw away empty ones.  */
8516   if (!output_if_empty && die->die_child == NULL)
8517     return;
8518
8519   /* Even if there are no children of this DIE, we must output the information
8520      about the compilation unit.  Otherwise, on an empty translation unit, we
8521      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8522      will then complain when examining the file.  First mark all the DIEs in
8523      this CU so we know which get local refs.  */
8524   mark_dies (die);
8525
8526   build_abbrev_table (die);
8527
8528   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8529   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8530   calc_die_sizes (die);
8531
8532   oldsym = die->die_id.die_symbol;
8533   if (oldsym)
8534     {
8535       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8536
8537       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8538       secname = tmp;
8539       die->die_id.die_symbol = NULL;
8540       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8541     }
8542   else
8543     {
8544       switch_to_section (debug_info_section);
8545       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8546       info_section_emitted = true;
8547     }
8548
8549   /* Output debugging information.  */
8550   output_compilation_unit_header ();
8551   output_die (die);
8552
8553   /* Leave the marks on the main CU, so we can check them in
8554      output_pubnames.  */
8555   if (oldsym)
8556     {
8557       unmark_dies (die);
8558       die->die_id.die_symbol = oldsym;
8559     }
8560 }
8561
8562 /* Output a comdat type unit DIE and its children.  */
8563
8564 static void
8565 output_comdat_type_unit (comdat_type_node *node)
8566 {
8567   const char *secname;
8568   char *tmp;
8569   int i;
8570 #if defined (OBJECT_FORMAT_ELF)
8571   tree comdat_key;
8572 #endif
8573
8574   /* First mark all the DIEs in this CU so we know which get local refs.  */
8575   mark_dies (node->root_die);
8576
8577   build_abbrev_table (node->root_die);
8578
8579   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8580   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8581   calc_die_sizes (node->root_die);
8582
8583 #if defined (OBJECT_FORMAT_ELF)
8584   secname = ".debug_types";
8585   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8586   sprintf (tmp, "wt.");
8587   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8588     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8589   comdat_key = get_identifier (tmp);
8590   targetm.asm_out.named_section (secname,
8591                                  SECTION_DEBUG | SECTION_LINKONCE,
8592                                  comdat_key);
8593 #else
8594   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8595   sprintf (tmp, ".gnu.linkonce.wt.");
8596   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8597     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8598   secname = tmp;
8599   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8600 #endif
8601
8602   /* Output debugging information.  */
8603   output_compilation_unit_header ();
8604   output_signature (node->signature, "Type Signature");
8605   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8606                        "Offset to Type DIE");
8607   output_die (node->root_die);
8608
8609   unmark_dies (node->root_die);
8610 }
8611
8612 /* Return the DWARF2/3 pubname associated with a decl.  */
8613
8614 static const char *
8615 dwarf2_name (tree decl, int scope)
8616 {
8617   if (DECL_NAMELESS (decl))
8618     return NULL;
8619   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8620 }
8621
8622 /* Add a new entry to .debug_pubnames if appropriate.  */
8623
8624 static void
8625 add_pubname_string (const char *str, dw_die_ref die)
8626 {
8627   if (targetm.want_debug_pub_sections)
8628     {
8629       pubname_entry e;
8630
8631       e.die = die;
8632       e.name = xstrdup (str);
8633       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8634     }
8635 }
8636
8637 static void
8638 add_pubname (tree decl, dw_die_ref die)
8639 {
8640   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8641     {
8642       const char *name = dwarf2_name (decl, 1);
8643       if (name)
8644         add_pubname_string (name, die);
8645     }
8646 }
8647
8648 /* Add a new entry to .debug_pubtypes if appropriate.  */
8649
8650 static void
8651 add_pubtype (tree decl, dw_die_ref die)
8652 {
8653   pubname_entry e;
8654
8655   if (!targetm.want_debug_pub_sections)
8656     return;
8657
8658   e.name = NULL;
8659   if ((TREE_PUBLIC (decl)
8660        || is_cu_die (die->die_parent))
8661       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8662     {
8663       e.die = die;
8664       if (TYPE_P (decl))
8665         {
8666           if (TYPE_NAME (decl))
8667             {
8668               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8669                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8670               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8671                        && DECL_NAME (TYPE_NAME (decl)))
8672                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8673               else
8674                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8675             }
8676         }
8677       else
8678         {
8679           e.name = dwarf2_name (decl, 1);
8680           if (e.name)
8681             e.name = xstrdup (e.name);
8682         }
8683
8684       /* If we don't have a name for the type, there's no point in adding
8685          it to the table.  */
8686       if (e.name && e.name[0] != '\0')
8687         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8688     }
8689 }
8690
8691 /* Output the public names table used to speed up access to externally
8692    visible names; or the public types table used to find type definitions.  */
8693
8694 static void
8695 output_pubnames (VEC (pubname_entry, gc) * names)
8696 {
8697   unsigned i;
8698   unsigned long pubnames_length = size_of_pubnames (names);
8699   pubname_ref pub;
8700
8701   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8702     dw2_asm_output_data (4, 0xffffffff,
8703       "Initial length escape value indicating 64-bit DWARF extension");
8704   if (names == pubname_table)
8705     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8706                          "Length of Public Names Info");
8707   else
8708     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8709                          "Length of Public Type Names Info");
8710   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8711   dw2_asm_output_data (2, 2, "DWARF Version");
8712   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8713                          debug_info_section,
8714                          "Offset of Compilation Unit Info");
8715   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8716                        "Compilation Unit Length");
8717
8718   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8719     {
8720       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8721       if (names == pubname_table)
8722         gcc_assert (pub->die->die_mark);
8723
8724       if (names != pubtype_table
8725           || pub->die->die_offset != 0
8726           || !flag_eliminate_unused_debug_types)
8727         {
8728           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8729                                "DIE offset");
8730
8731           dw2_asm_output_nstring (pub->name, -1, "external name");
8732         }
8733     }
8734
8735   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8736 }
8737
8738 /* Output the information that goes into the .debug_aranges table.
8739    Namely, define the beginning and ending address range of the
8740    text section generated for this compilation unit.  */
8741
8742 static void
8743 output_aranges (unsigned long aranges_length)
8744 {
8745   unsigned i;
8746
8747   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8748     dw2_asm_output_data (4, 0xffffffff,
8749       "Initial length escape value indicating 64-bit DWARF extension");
8750   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8751                        "Length of Address Ranges Info");
8752   /* Version number for aranges is still 2, even in DWARF3.  */
8753   dw2_asm_output_data (2, 2, "DWARF Version");
8754   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8755                          debug_info_section,
8756                          "Offset of Compilation Unit Info");
8757   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8758   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8759
8760   /* We need to align to twice the pointer size here.  */
8761   if (DWARF_ARANGES_PAD_SIZE)
8762     {
8763       /* Pad using a 2 byte words so that padding is correct for any
8764          pointer size.  */
8765       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8766                            2 * DWARF2_ADDR_SIZE);
8767       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8768         dw2_asm_output_data (2, 0, NULL);
8769     }
8770
8771   /* It is necessary not to output these entries if the sections were
8772      not used; if the sections were not used, the length will be 0 and
8773      the address may end up as 0 if the section is discarded by ld
8774      --gc-sections, leaving an invalid (0, 0) entry that can be
8775      confused with the terminator.  */
8776   if (text_section_used)
8777     {
8778       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8779       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8780                             text_section_label, "Length");
8781     }
8782   if (cold_text_section_used)
8783     {
8784       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8785                            "Address");
8786       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8787                             cold_text_section_label, "Length");
8788     }
8789
8790   if (have_multiple_function_sections)
8791     {
8792       unsigned fde_idx;
8793       dw_fde_ref fde;
8794
8795       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8796         {
8797           if (!fde->in_std_section)
8798             {
8799               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8800                                    "Address");
8801               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8802                                     fde->dw_fde_begin, "Length");
8803             }
8804           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8805             {
8806               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8807                                    "Address");
8808               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8809                                     fde->dw_fde_second_begin, "Length");
8810             }
8811         }
8812     }
8813
8814   /* Output the terminator words.  */
8815   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8816   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8817 }
8818
8819 /* Add a new entry to .debug_ranges.  Return the offset at which it
8820    was placed.  */
8821
8822 static unsigned int
8823 add_ranges_num (int num)
8824 {
8825   unsigned int in_use = ranges_table_in_use;
8826
8827   if (in_use == ranges_table_allocated)
8828     {
8829       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8830       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8831                                     ranges_table_allocated);
8832       memset (ranges_table + ranges_table_in_use, 0,
8833               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8834     }
8835
8836   ranges_table[in_use].num = num;
8837   ranges_table_in_use = in_use + 1;
8838
8839   return in_use * 2 * DWARF2_ADDR_SIZE;
8840 }
8841
8842 /* Add a new entry to .debug_ranges corresponding to a block, or a
8843    range terminator if BLOCK is NULL.  */
8844
8845 static unsigned int
8846 add_ranges (const_tree block)
8847 {
8848   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8849 }
8850
8851 /* Add a new entry to .debug_ranges corresponding to a pair of
8852    labels.  */
8853
8854 static void
8855 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8856                       bool *added)
8857 {
8858   unsigned int in_use = ranges_by_label_in_use;
8859   unsigned int offset;
8860
8861   if (in_use == ranges_by_label_allocated)
8862     {
8863       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8864       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8865                                        ranges_by_label,
8866                                        ranges_by_label_allocated);
8867       memset (ranges_by_label + ranges_by_label_in_use, 0,
8868               RANGES_TABLE_INCREMENT
8869               * sizeof (struct dw_ranges_by_label_struct));
8870     }
8871
8872   ranges_by_label[in_use].begin = begin;
8873   ranges_by_label[in_use].end = end;
8874   ranges_by_label_in_use = in_use + 1;
8875
8876   offset = add_ranges_num (-(int)in_use - 1);
8877   if (!*added)
8878     {
8879       add_AT_range_list (die, DW_AT_ranges, offset);
8880       *added = true;
8881     }
8882 }
8883
8884 static void
8885 output_ranges (void)
8886 {
8887   unsigned i;
8888   static const char *const start_fmt = "Offset %#x";
8889   const char *fmt = start_fmt;
8890
8891   for (i = 0; i < ranges_table_in_use; i++)
8892     {
8893       int block_num = ranges_table[i].num;
8894
8895       if (block_num > 0)
8896         {
8897           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8898           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8899
8900           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8901           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8902
8903           /* If all code is in the text section, then the compilation
8904              unit base address defaults to DW_AT_low_pc, which is the
8905              base of the text section.  */
8906           if (!have_multiple_function_sections)
8907             {
8908               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8909                                     text_section_label,
8910                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8911               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8912                                     text_section_label, NULL);
8913             }
8914
8915           /* Otherwise, the compilation unit base address is zero,
8916              which allows us to use absolute addresses, and not worry
8917              about whether the target supports cross-section
8918              arithmetic.  */
8919           else
8920             {
8921               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8922                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8923               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8924             }
8925
8926           fmt = NULL;
8927         }
8928
8929       /* Negative block_num stands for an index into ranges_by_label.  */
8930       else if (block_num < 0)
8931         {
8932           int lab_idx = - block_num - 1;
8933
8934           if (!have_multiple_function_sections)
8935             {
8936               gcc_unreachable ();
8937 #if 0
8938               /* If we ever use add_ranges_by_labels () for a single
8939                  function section, all we have to do is to take out
8940                  the #if 0 above.  */
8941               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8942                                     ranges_by_label[lab_idx].begin,
8943                                     text_section_label,
8944                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8945               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8946                                     ranges_by_label[lab_idx].end,
8947                                     text_section_label, NULL);
8948 #endif
8949             }
8950           else
8951             {
8952               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8953                                    ranges_by_label[lab_idx].begin,
8954                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8955               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8956                                    ranges_by_label[lab_idx].end,
8957                                    NULL);
8958             }
8959         }
8960       else
8961         {
8962           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8963           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8964           fmt = start_fmt;
8965         }
8966     }
8967 }
8968
8969 /* Data structure containing information about input files.  */
8970 struct file_info
8971 {
8972   const char *path;     /* Complete file name.  */
8973   const char *fname;    /* File name part.  */
8974   int length;           /* Length of entire string.  */
8975   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8976   int dir_idx;          /* Index in directory table.  */
8977 };
8978
8979 /* Data structure containing information about directories with source
8980    files.  */
8981 struct dir_info
8982 {
8983   const char *path;     /* Path including directory name.  */
8984   int length;           /* Path length.  */
8985   int prefix;           /* Index of directory entry which is a prefix.  */
8986   int count;            /* Number of files in this directory.  */
8987   int dir_idx;          /* Index of directory used as base.  */
8988 };
8989
8990 /* Callback function for file_info comparison.  We sort by looking at
8991    the directories in the path.  */
8992
8993 static int
8994 file_info_cmp (const void *p1, const void *p2)
8995 {
8996   const struct file_info *const s1 = (const struct file_info *) p1;
8997   const struct file_info *const s2 = (const struct file_info *) p2;
8998   const unsigned char *cp1;
8999   const unsigned char *cp2;
9000
9001   /* Take care of file names without directories.  We need to make sure that
9002      we return consistent values to qsort since some will get confused if
9003      we return the same value when identical operands are passed in opposite
9004      orders.  So if neither has a directory, return 0 and otherwise return
9005      1 or -1 depending on which one has the directory.  */
9006   if ((s1->path == s1->fname || s2->path == s2->fname))
9007     return (s2->path == s2->fname) - (s1->path == s1->fname);
9008
9009   cp1 = (const unsigned char *) s1->path;
9010   cp2 = (const unsigned char *) s2->path;
9011
9012   while (1)
9013     {
9014       ++cp1;
9015       ++cp2;
9016       /* Reached the end of the first path?  If so, handle like above.  */
9017       if ((cp1 == (const unsigned char *) s1->fname)
9018           || (cp2 == (const unsigned char *) s2->fname))
9019         return ((cp2 == (const unsigned char *) s2->fname)
9020                 - (cp1 == (const unsigned char *) s1->fname));
9021
9022       /* Character of current path component the same?  */
9023       else if (*cp1 != *cp2)
9024         return *cp1 - *cp2;
9025     }
9026 }
9027
9028 struct file_name_acquire_data
9029 {
9030   struct file_info *files;
9031   int used_files;
9032   int max_files;
9033 };
9034
9035 /* Traversal function for the hash table.  */
9036
9037 static int
9038 file_name_acquire (void ** slot, void *data)
9039 {
9040   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9041   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9042   struct file_info *fi;
9043   const char *f;
9044
9045   gcc_assert (fnad->max_files >= d->emitted_number);
9046
9047   if (! d->emitted_number)
9048     return 1;
9049
9050   gcc_assert (fnad->max_files != fnad->used_files);
9051
9052   fi = fnad->files + fnad->used_files++;
9053
9054   /* Skip all leading "./".  */
9055   f = d->filename;
9056   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9057     f += 2;
9058
9059   /* Create a new array entry.  */
9060   fi->path = f;
9061   fi->length = strlen (f);
9062   fi->file_idx = d;
9063
9064   /* Search for the file name part.  */
9065   f = strrchr (f, DIR_SEPARATOR);
9066 #if defined (DIR_SEPARATOR_2)
9067   {
9068     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9069
9070     if (g != NULL)
9071       {
9072         if (f == NULL || f < g)
9073           f = g;
9074       }
9075   }
9076 #endif
9077
9078   fi->fname = f == NULL ? fi->path : f + 1;
9079   return 1;
9080 }
9081
9082 /* Output the directory table and the file name table.  We try to minimize
9083    the total amount of memory needed.  A heuristic is used to avoid large
9084    slowdowns with many input files.  */
9085
9086 static void
9087 output_file_names (void)
9088 {
9089   struct file_name_acquire_data fnad;
9090   int numfiles;
9091   struct file_info *files;
9092   struct dir_info *dirs;
9093   int *saved;
9094   int *savehere;
9095   int *backmap;
9096   int ndirs;
9097   int idx_offset;
9098   int i;
9099
9100   if (!last_emitted_file)
9101     {
9102       dw2_asm_output_data (1, 0, "End directory table");
9103       dw2_asm_output_data (1, 0, "End file name table");
9104       return;
9105     }
9106
9107   numfiles = last_emitted_file->emitted_number;
9108
9109   /* Allocate the various arrays we need.  */
9110   files = XALLOCAVEC (struct file_info, numfiles);
9111   dirs = XALLOCAVEC (struct dir_info, numfiles);
9112
9113   fnad.files = files;
9114   fnad.used_files = 0;
9115   fnad.max_files = numfiles;
9116   htab_traverse (file_table, file_name_acquire, &fnad);
9117   gcc_assert (fnad.used_files == fnad.max_files);
9118
9119   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9120
9121   /* Find all the different directories used.  */
9122   dirs[0].path = files[0].path;
9123   dirs[0].length = files[0].fname - files[0].path;
9124   dirs[0].prefix = -1;
9125   dirs[0].count = 1;
9126   dirs[0].dir_idx = 0;
9127   files[0].dir_idx = 0;
9128   ndirs = 1;
9129
9130   for (i = 1; i < numfiles; i++)
9131     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9132         && memcmp (dirs[ndirs - 1].path, files[i].path,
9133                    dirs[ndirs - 1].length) == 0)
9134       {
9135         /* Same directory as last entry.  */
9136         files[i].dir_idx = ndirs - 1;
9137         ++dirs[ndirs - 1].count;
9138       }
9139     else
9140       {
9141         int j;
9142
9143         /* This is a new directory.  */
9144         dirs[ndirs].path = files[i].path;
9145         dirs[ndirs].length = files[i].fname - files[i].path;
9146         dirs[ndirs].count = 1;
9147         dirs[ndirs].dir_idx = ndirs;
9148         files[i].dir_idx = ndirs;
9149
9150         /* Search for a prefix.  */
9151         dirs[ndirs].prefix = -1;
9152         for (j = 0; j < ndirs; j++)
9153           if (dirs[j].length < dirs[ndirs].length
9154               && dirs[j].length > 1
9155               && (dirs[ndirs].prefix == -1
9156                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9157               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9158             dirs[ndirs].prefix = j;
9159
9160         ++ndirs;
9161       }
9162
9163   /* Now to the actual work.  We have to find a subset of the directories which
9164      allow expressing the file name using references to the directory table
9165      with the least amount of characters.  We do not do an exhaustive search
9166      where we would have to check out every combination of every single
9167      possible prefix.  Instead we use a heuristic which provides nearly optimal
9168      results in most cases and never is much off.  */
9169   saved = XALLOCAVEC (int, ndirs);
9170   savehere = XALLOCAVEC (int, ndirs);
9171
9172   memset (saved, '\0', ndirs * sizeof (saved[0]));
9173   for (i = 0; i < ndirs; i++)
9174     {
9175       int j;
9176       int total;
9177
9178       /* We can always save some space for the current directory.  But this
9179          does not mean it will be enough to justify adding the directory.  */
9180       savehere[i] = dirs[i].length;
9181       total = (savehere[i] - saved[i]) * dirs[i].count;
9182
9183       for (j = i + 1; j < ndirs; j++)
9184         {
9185           savehere[j] = 0;
9186           if (saved[j] < dirs[i].length)
9187             {
9188               /* Determine whether the dirs[i] path is a prefix of the
9189                  dirs[j] path.  */
9190               int k;
9191
9192               k = dirs[j].prefix;
9193               while (k != -1 && k != (int) i)
9194                 k = dirs[k].prefix;
9195
9196               if (k == (int) i)
9197                 {
9198                   /* Yes it is.  We can possibly save some memory by
9199                      writing the filenames in dirs[j] relative to
9200                      dirs[i].  */
9201                   savehere[j] = dirs[i].length;
9202                   total += (savehere[j] - saved[j]) * dirs[j].count;
9203                 }
9204             }
9205         }
9206
9207       /* Check whether we can save enough to justify adding the dirs[i]
9208          directory.  */
9209       if (total > dirs[i].length + 1)
9210         {
9211           /* It's worthwhile adding.  */
9212           for (j = i; j < ndirs; j++)
9213             if (savehere[j] > 0)
9214               {
9215                 /* Remember how much we saved for this directory so far.  */
9216                 saved[j] = savehere[j];
9217
9218                 /* Remember the prefix directory.  */
9219                 dirs[j].dir_idx = i;
9220               }
9221         }
9222     }
9223
9224   /* Emit the directory name table.  */
9225   idx_offset = dirs[0].length > 0 ? 1 : 0;
9226   for (i = 1 - idx_offset; i < ndirs; i++)
9227     dw2_asm_output_nstring (dirs[i].path,
9228                             dirs[i].length
9229                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9230                             "Directory Entry: %#x", i + idx_offset);
9231
9232   dw2_asm_output_data (1, 0, "End directory table");
9233
9234   /* We have to emit them in the order of emitted_number since that's
9235      used in the debug info generation.  To do this efficiently we
9236      generate a back-mapping of the indices first.  */
9237   backmap = XALLOCAVEC (int, numfiles);
9238   for (i = 0; i < numfiles; i++)
9239     backmap[files[i].file_idx->emitted_number - 1] = i;
9240
9241   /* Now write all the file names.  */
9242   for (i = 0; i < numfiles; i++)
9243     {
9244       int file_idx = backmap[i];
9245       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9246
9247 #ifdef VMS_DEBUGGING_INFO
9248 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9249
9250       /* Setting these fields can lead to debugger miscomparisons,
9251          but VMS Debug requires them to be set correctly.  */
9252
9253       int ver;
9254       long long cdt;
9255       long siz;
9256       int maxfilelen = strlen (files[file_idx].path)
9257                                + dirs[dir_idx].length
9258                                + MAX_VMS_VERSION_LEN + 1;
9259       char *filebuf = XALLOCAVEC (char, maxfilelen);
9260
9261       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9262       snprintf (filebuf, maxfilelen, "%s;%d",
9263                 files[file_idx].path + dirs[dir_idx].length, ver);
9264
9265       dw2_asm_output_nstring
9266         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9267
9268       /* Include directory index.  */
9269       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9270
9271       /* Modification time.  */
9272       dw2_asm_output_data_uleb128
9273         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9274           ? cdt : 0,
9275          NULL);
9276
9277       /* File length in bytes.  */
9278       dw2_asm_output_data_uleb128
9279         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9280           ? siz : 0,
9281          NULL);
9282 #else
9283       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9284                               "File Entry: %#x", (unsigned) i + 1);
9285
9286       /* Include directory index.  */
9287       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9288
9289       /* Modification time.  */
9290       dw2_asm_output_data_uleb128 (0, NULL);
9291
9292       /* File length in bytes.  */
9293       dw2_asm_output_data_uleb128 (0, NULL);
9294 #endif /* VMS_DEBUGGING_INFO */
9295     }
9296
9297   dw2_asm_output_data (1, 0, "End file name table");
9298 }
9299
9300
9301 /* Output one line number table into the .debug_line section.  */
9302
9303 static void
9304 output_one_line_info_table (dw_line_info_table *table)
9305 {
9306   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9307   unsigned int current_line = 1;
9308   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9309   dw_line_info_entry *ent;
9310   size_t i;
9311
9312   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9313     {
9314       switch (ent->opcode)
9315         {
9316         case LI_set_address:
9317           /* ??? Unfortunately, we have little choice here currently, and
9318              must always use the most general form.  GCC does not know the
9319              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9320              ports do have length attributes which will give an upper bound
9321              on the address range.  We could perhaps use length attributes
9322              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9323           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9324
9325           /* This can handle any delta.  This takes
9326              4+DWARF2_ADDR_SIZE bytes.  */
9327           dw2_asm_output_data (1, 0, "set address %s", line_label);
9328           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9329           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9330           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9331           break;
9332
9333         case LI_set_line:
9334           if (ent->val == current_line)
9335             {
9336               /* We still need to start a new row, so output a copy insn.  */
9337               dw2_asm_output_data (1, DW_LNS_copy,
9338                                    "copy line %u", current_line);
9339             }
9340           else
9341             {
9342               int line_offset = ent->val - current_line;
9343               int line_delta = line_offset - DWARF_LINE_BASE;
9344
9345               current_line = ent->val;
9346               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9347                 {
9348                   /* This can handle deltas from -10 to 234, using the current
9349                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9350                      This takes 1 byte.  */
9351                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9352                                        "line %u", current_line);
9353                 }
9354               else
9355                 {
9356                   /* This can handle any delta.  This takes at least 4 bytes,
9357                      depending on the value being encoded.  */
9358                   dw2_asm_output_data (1, DW_LNS_advance_line,
9359                                        "advance to line %u", current_line);
9360                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9361                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9362                 }
9363             }
9364           break;
9365
9366         case LI_set_file:
9367           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9368           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9369           break;
9370
9371         case LI_set_column:
9372           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9373           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9374           break;
9375
9376         case LI_negate_stmt:
9377           current_is_stmt = !current_is_stmt;
9378           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9379                                "is_stmt %d", current_is_stmt);
9380           break;
9381
9382         case LI_set_prologue_end:
9383           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9384                                "set prologue end");
9385           break;
9386           
9387         case LI_set_epilogue_begin:
9388           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9389                                "set epilogue begin");
9390           break;
9391
9392         case LI_set_discriminator:
9393           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9394           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9395           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9396           dw2_asm_output_data_uleb128 (ent->val, NULL);
9397           break;
9398         }
9399     }
9400
9401   /* Emit debug info for the address of the end of the table.  */
9402   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9403   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9404   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9405   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9406
9407   dw2_asm_output_data (1, 0, "end sequence");
9408   dw2_asm_output_data_uleb128 (1, NULL);
9409   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9410 }
9411
9412 /* Output the source line number correspondence information.  This
9413    information goes into the .debug_line section.  */
9414
9415 static void
9416 output_line_info (void)
9417 {
9418   char l1[20], l2[20], p1[20], p2[20];
9419   int ver = dwarf_version;
9420   bool saw_one = false;
9421   int opc;
9422
9423   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9424   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9425   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9426   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9427
9428   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9429     dw2_asm_output_data (4, 0xffffffff,
9430       "Initial length escape value indicating 64-bit DWARF extension");
9431   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9432                         "Length of Source Line Info");
9433   ASM_OUTPUT_LABEL (asm_out_file, l1);
9434
9435   dw2_asm_output_data (2, ver, "DWARF Version");
9436   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9437   ASM_OUTPUT_LABEL (asm_out_file, p1);
9438
9439   /* Define the architecture-dependent minimum instruction length (in bytes).
9440      In this implementation of DWARF, this field is used for information
9441      purposes only.  Since GCC generates assembly language, we have no
9442      a priori knowledge of how many instruction bytes are generated for each
9443      source line, and therefore can use only the DW_LNE_set_address and
9444      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9445      this as '1', which is "correct enough" for all architectures,
9446      and don't let the target override.  */
9447   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9448
9449   if (ver >= 4)
9450     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9451                          "Maximum Operations Per Instruction");
9452   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9453                        "Default is_stmt_start flag");
9454   dw2_asm_output_data (1, DWARF_LINE_BASE,
9455                        "Line Base Value (Special Opcodes)");
9456   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9457                        "Line Range Value (Special Opcodes)");
9458   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9459                        "Special Opcode Base");
9460
9461   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9462     {
9463       int n_op_args;
9464       switch (opc)
9465         {
9466         case DW_LNS_advance_pc:
9467         case DW_LNS_advance_line:
9468         case DW_LNS_set_file:
9469         case DW_LNS_set_column:
9470         case DW_LNS_fixed_advance_pc:
9471         case DW_LNS_set_isa:
9472           n_op_args = 1;
9473           break;
9474         default:
9475           n_op_args = 0;
9476           break;
9477         }
9478
9479       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9480                            opc, n_op_args);
9481     }
9482
9483   /* Write out the information about the files we use.  */
9484   output_file_names ();
9485   ASM_OUTPUT_LABEL (asm_out_file, p2);
9486
9487   if (separate_line_info)
9488     {
9489       dw_line_info_table *table;
9490       size_t i;
9491
9492       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9493         if (table->in_use)
9494           {
9495             output_one_line_info_table (table);
9496             saw_one = true;
9497           }
9498     }
9499   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9500     {
9501       output_one_line_info_table (cold_text_section_line_info);
9502       saw_one = true;
9503     }
9504
9505   /* ??? Some Darwin linkers crash on a .debug_line section with no
9506      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9507      sufficient -- the address column must also be initialized.
9508      Make sure to output at least one set_address/end_sequence pair,
9509      choosing .text since that section is always present.  */
9510   if (text_section_line_info->in_use || !saw_one)
9511     output_one_line_info_table (text_section_line_info);
9512
9513   /* Output the marker for the end of the line number info.  */
9514   ASM_OUTPUT_LABEL (asm_out_file, l2);
9515 }
9516 \f
9517 /* Given a pointer to a tree node for some base type, return a pointer to
9518    a DIE that describes the given type.
9519
9520    This routine must only be called for GCC type nodes that correspond to
9521    Dwarf base (fundamental) types.  */
9522
9523 static dw_die_ref
9524 base_type_die (tree type)
9525 {
9526   dw_die_ref base_type_result;
9527   enum dwarf_type encoding;
9528
9529   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9530     return 0;
9531
9532   /* If this is a subtype that should not be emitted as a subrange type,
9533      use the base type.  See subrange_type_for_debug_p.  */
9534   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9535     type = TREE_TYPE (type);
9536
9537   switch (TREE_CODE (type))
9538     {
9539     case INTEGER_TYPE:
9540       if ((dwarf_version >= 4 || !dwarf_strict)
9541           && TYPE_NAME (type)
9542           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9543           && DECL_IS_BUILTIN (TYPE_NAME (type))
9544           && DECL_NAME (TYPE_NAME (type)))
9545         {
9546           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9547           if (strcmp (name, "char16_t") == 0
9548               || strcmp (name, "char32_t") == 0)
9549             {
9550               encoding = DW_ATE_UTF;
9551               break;
9552             }
9553         }
9554       if (TYPE_STRING_FLAG (type))
9555         {
9556           if (TYPE_UNSIGNED (type))
9557             encoding = DW_ATE_unsigned_char;
9558           else
9559             encoding = DW_ATE_signed_char;
9560         }
9561       else if (TYPE_UNSIGNED (type))
9562         encoding = DW_ATE_unsigned;
9563       else
9564         encoding = DW_ATE_signed;
9565       break;
9566
9567     case REAL_TYPE:
9568       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9569         {
9570           if (dwarf_version >= 3 || !dwarf_strict)
9571             encoding = DW_ATE_decimal_float;
9572           else
9573             encoding = DW_ATE_lo_user;
9574         }
9575       else
9576         encoding = DW_ATE_float;
9577       break;
9578
9579     case FIXED_POINT_TYPE:
9580       if (!(dwarf_version >= 3 || !dwarf_strict))
9581         encoding = DW_ATE_lo_user;
9582       else if (TYPE_UNSIGNED (type))
9583         encoding = DW_ATE_unsigned_fixed;
9584       else
9585         encoding = DW_ATE_signed_fixed;
9586       break;
9587
9588       /* Dwarf2 doesn't know anything about complex ints, so use
9589          a user defined type for it.  */
9590     case COMPLEX_TYPE:
9591       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9592         encoding = DW_ATE_complex_float;
9593       else
9594         encoding = DW_ATE_lo_user;
9595       break;
9596
9597     case BOOLEAN_TYPE:
9598       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9599       encoding = DW_ATE_boolean;
9600       break;
9601
9602     default:
9603       /* No other TREE_CODEs are Dwarf fundamental types.  */
9604       gcc_unreachable ();
9605     }
9606
9607   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9608
9609   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9610                    int_size_in_bytes (type));
9611   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9612
9613   return base_type_result;
9614 }
9615
9616 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9617    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9618
9619 static inline int
9620 is_base_type (tree type)
9621 {
9622   switch (TREE_CODE (type))
9623     {
9624     case ERROR_MARK:
9625     case VOID_TYPE:
9626     case INTEGER_TYPE:
9627     case REAL_TYPE:
9628     case FIXED_POINT_TYPE:
9629     case COMPLEX_TYPE:
9630     case BOOLEAN_TYPE:
9631       return 1;
9632
9633     case ARRAY_TYPE:
9634     case RECORD_TYPE:
9635     case UNION_TYPE:
9636     case QUAL_UNION_TYPE:
9637     case ENUMERAL_TYPE:
9638     case FUNCTION_TYPE:
9639     case METHOD_TYPE:
9640     case POINTER_TYPE:
9641     case REFERENCE_TYPE:
9642     case NULLPTR_TYPE:
9643     case OFFSET_TYPE:
9644     case LANG_TYPE:
9645     case VECTOR_TYPE:
9646       return 0;
9647
9648     default:
9649       gcc_unreachable ();
9650     }
9651
9652   return 0;
9653 }
9654
9655 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9656    node, return the size in bits for the type if it is a constant, or else
9657    return the alignment for the type if the type's size is not constant, or
9658    else return BITS_PER_WORD if the type actually turns out to be an
9659    ERROR_MARK node.  */
9660
9661 static inline unsigned HOST_WIDE_INT
9662 simple_type_size_in_bits (const_tree type)
9663 {
9664   if (TREE_CODE (type) == ERROR_MARK)
9665     return BITS_PER_WORD;
9666   else if (TYPE_SIZE (type) == NULL_TREE)
9667     return 0;
9668   else if (host_integerp (TYPE_SIZE (type), 1))
9669     return tree_low_cst (TYPE_SIZE (type), 1);
9670   else
9671     return TYPE_ALIGN (type);
9672 }
9673
9674 /* Similarly, but return a double_int instead of UHWI.  */
9675
9676 static inline double_int
9677 double_int_type_size_in_bits (const_tree type)
9678 {
9679   if (TREE_CODE (type) == ERROR_MARK)
9680     return uhwi_to_double_int (BITS_PER_WORD);
9681   else if (TYPE_SIZE (type) == NULL_TREE)
9682     return double_int_zero;
9683   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9684     return tree_to_double_int (TYPE_SIZE (type));
9685   else
9686     return uhwi_to_double_int (TYPE_ALIGN (type));
9687 }
9688
9689 /*  Given a pointer to a tree node for a subrange type, return a pointer
9690     to a DIE that describes the given type.  */
9691
9692 static dw_die_ref
9693 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9694 {
9695   dw_die_ref subrange_die;
9696   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9697
9698   if (context_die == NULL)
9699     context_die = comp_unit_die ();
9700
9701   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9702
9703   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9704     {
9705       /* The size of the subrange type and its base type do not match,
9706          so we need to generate a size attribute for the subrange type.  */
9707       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9708     }
9709
9710   if (low)
9711     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9712   if (high)
9713     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9714
9715   return subrange_die;
9716 }
9717
9718 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9719    entry that chains various modifiers in front of the given type.  */
9720
9721 static dw_die_ref
9722 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9723                    dw_die_ref context_die)
9724 {
9725   enum tree_code code = TREE_CODE (type);
9726   dw_die_ref mod_type_die;
9727   dw_die_ref sub_die = NULL;
9728   tree item_type = NULL;
9729   tree qualified_type;
9730   tree name, low, high;
9731
9732   if (code == ERROR_MARK)
9733     return NULL;
9734
9735   /* See if we already have the appropriately qualified variant of
9736      this type.  */
9737   qualified_type
9738     = get_qualified_type (type,
9739                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9740                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9741
9742   if (qualified_type == sizetype
9743       && TYPE_NAME (qualified_type)
9744       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9745     {
9746       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9747
9748       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9749                            && TYPE_PRECISION (t)
9750                            == TYPE_PRECISION (qualified_type)
9751                            && TYPE_UNSIGNED (t)
9752                            == TYPE_UNSIGNED (qualified_type));
9753       qualified_type = t;
9754     }
9755
9756   /* If we do, then we can just use its DIE, if it exists.  */
9757   if (qualified_type)
9758     {
9759       mod_type_die = lookup_type_die (qualified_type);
9760       if (mod_type_die)
9761         return mod_type_die;
9762     }
9763
9764   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9765
9766   /* Handle C typedef types.  */
9767   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9768       && !DECL_ARTIFICIAL (name))
9769     {
9770       tree dtype = TREE_TYPE (name);
9771
9772       if (qualified_type == dtype)
9773         {
9774           /* For a named type, use the typedef.  */
9775           gen_type_die (qualified_type, context_die);
9776           return lookup_type_die (qualified_type);
9777         }
9778       else if (is_const_type < TYPE_READONLY (dtype)
9779                || is_volatile_type < TYPE_VOLATILE (dtype)
9780                || (is_const_type <= TYPE_READONLY (dtype)
9781                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9782                    && DECL_ORIGINAL_TYPE (name) != type))
9783         /* cv-unqualified version of named type.  Just use the unnamed
9784            type to which it refers.  */
9785         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9786                                   is_const_type, is_volatile_type,
9787                                   context_die);
9788       /* Else cv-qualified version of named type; fall through.  */
9789     }
9790
9791   if (is_const_type
9792       /* If both is_const_type and is_volatile_type, prefer the path
9793          which leads to a qualified type.  */
9794       && (!is_volatile_type
9795           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9796           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9797     {
9798       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9799       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9800     }
9801   else if (is_volatile_type)
9802     {
9803       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9804       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9805     }
9806   else if (code == POINTER_TYPE)
9807     {
9808       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9809       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9810                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9811       item_type = TREE_TYPE (type);
9812       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9813         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9814                          TYPE_ADDR_SPACE (item_type));
9815     }
9816   else if (code == REFERENCE_TYPE)
9817     {
9818       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9819         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9820                                 type);
9821       else
9822         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9823       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9824                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9825       item_type = TREE_TYPE (type);
9826       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9827         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9828                          TYPE_ADDR_SPACE (item_type));
9829     }
9830   else if (code == INTEGER_TYPE
9831            && TREE_TYPE (type) != NULL_TREE
9832            && subrange_type_for_debug_p (type, &low, &high))
9833     {
9834       mod_type_die = subrange_type_die (type, low, high, context_die);
9835       item_type = TREE_TYPE (type);
9836     }
9837   else if (is_base_type (type))
9838     mod_type_die = base_type_die (type);
9839   else
9840     {
9841       gen_type_die (type, context_die);
9842
9843       /* We have to get the type_main_variant here (and pass that to the
9844          `lookup_type_die' routine) because the ..._TYPE node we have
9845          might simply be a *copy* of some original type node (where the
9846          copy was created to help us keep track of typedef names) and
9847          that copy might have a different TYPE_UID from the original
9848          ..._TYPE node.  */
9849       if (TREE_CODE (type) != VECTOR_TYPE)
9850         return lookup_type_die (type_main_variant (type));
9851       else
9852         /* Vectors have the debugging information in the type,
9853            not the main variant.  */
9854         return lookup_type_die (type);
9855     }
9856
9857   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9858      don't output a DW_TAG_typedef, since there isn't one in the
9859      user's program; just attach a DW_AT_name to the type.
9860      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9861      if the base type already has the same name.  */
9862   if (name
9863       && ((TREE_CODE (name) != TYPE_DECL
9864            && (qualified_type == TYPE_MAIN_VARIANT (type)
9865                || (!is_const_type && !is_volatile_type)))
9866           || (TREE_CODE (name) == TYPE_DECL
9867               && TREE_TYPE (name) == qualified_type
9868               && DECL_NAME (name))))
9869     {
9870       if (TREE_CODE (name) == TYPE_DECL)
9871         /* Could just call add_name_and_src_coords_attributes here,
9872            but since this is a builtin type it doesn't have any
9873            useful source coordinates anyway.  */
9874         name = DECL_NAME (name);
9875       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9876       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9877       if (TYPE_ARTIFICIAL (type))
9878         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9879     }
9880   /* This probably indicates a bug.  */
9881   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9882     {
9883       name = TYPE_NAME (type);
9884       if (name
9885           && TREE_CODE (name) == TYPE_DECL)
9886         name = DECL_NAME (name);
9887       add_name_attribute (mod_type_die,
9888                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9889     }
9890
9891   if (qualified_type)
9892     equate_type_number_to_die (qualified_type, mod_type_die);
9893
9894   if (item_type)
9895     /* We must do this after the equate_type_number_to_die call, in case
9896        this is a recursive type.  This ensures that the modified_type_die
9897        recursion will terminate even if the type is recursive.  Recursive
9898        types are possible in Ada.  */
9899     sub_die = modified_type_die (item_type,
9900                                  TYPE_READONLY (item_type),
9901                                  TYPE_VOLATILE (item_type),
9902                                  context_die);
9903
9904   if (sub_die != NULL)
9905     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9906
9907   return mod_type_die;
9908 }
9909
9910 /* Generate DIEs for the generic parameters of T.
9911    T must be either a generic type or a generic function.
9912    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9913
9914 static void
9915 gen_generic_params_dies (tree t)
9916 {
9917   tree parms, args;
9918   int parms_num, i;
9919   dw_die_ref die = NULL;
9920
9921   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9922     return;
9923
9924   if (TYPE_P (t))
9925     die = lookup_type_die (t);
9926   else if (DECL_P (t))
9927     die = lookup_decl_die (t);
9928
9929   gcc_assert (die);
9930
9931   parms = lang_hooks.get_innermost_generic_parms (t);
9932   if (!parms)
9933     /* T has no generic parameter. It means T is neither a generic type
9934        or function. End of story.  */
9935     return;
9936
9937   parms_num = TREE_VEC_LENGTH (parms);
9938   args = lang_hooks.get_innermost_generic_args (t);
9939   for (i = 0; i < parms_num; i++)
9940     {
9941       tree parm, arg, arg_pack_elems;
9942
9943       parm = TREE_VEC_ELT (parms, i);
9944       arg = TREE_VEC_ELT (args, i);
9945       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9946       gcc_assert (parm && TREE_VALUE (parm) && arg);
9947
9948       if (parm && TREE_VALUE (parm) && arg)
9949         {
9950           /* If PARM represents a template parameter pack,
9951              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9952              by DW_TAG_template_*_parameter DIEs for the argument
9953              pack elements of ARG. Note that ARG would then be
9954              an argument pack.  */
9955           if (arg_pack_elems)
9956             template_parameter_pack_die (TREE_VALUE (parm),
9957                                          arg_pack_elems,
9958                                          die);
9959           else
9960             generic_parameter_die (TREE_VALUE (parm), arg,
9961                                    true /* Emit DW_AT_name */, die);
9962         }
9963     }
9964 }
9965
9966 /* Create and return a DIE for PARM which should be
9967    the representation of a generic type parameter.
9968    For instance, in the C++ front end, PARM would be a template parameter.
9969    ARG is the argument to PARM.
9970    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9971    name of the PARM.
9972    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9973    as a child node.  */
9974
9975 static dw_die_ref
9976 generic_parameter_die (tree parm, tree arg,
9977                        bool emit_name_p,
9978                        dw_die_ref parent_die)
9979 {
9980   dw_die_ref tmpl_die = NULL;
9981   const char *name = NULL;
9982
9983   if (!parm || !DECL_NAME (parm) || !arg)
9984     return NULL;
9985
9986   /* We support non-type generic parameters and arguments,
9987      type generic parameters and arguments, as well as
9988      generic generic parameters (a.k.a. template template parameters in C++)
9989      and arguments.  */
9990   if (TREE_CODE (parm) == PARM_DECL)
9991     /* PARM is a nontype generic parameter  */
9992     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9993   else if (TREE_CODE (parm) == TYPE_DECL)
9994     /* PARM is a type generic parameter.  */
9995     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9996   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9997     /* PARM is a generic generic parameter.
9998        Its DIE is a GNU extension. It shall have a
9999        DW_AT_name attribute to represent the name of the template template
10000        parameter, and a DW_AT_GNU_template_name attribute to represent the
10001        name of the template template argument.  */
10002     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10003                         parent_die, parm);
10004   else
10005     gcc_unreachable ();
10006
10007   if (tmpl_die)
10008     {
10009       tree tmpl_type;
10010
10011       /* If PARM is a generic parameter pack, it means we are
10012          emitting debug info for a template argument pack element.
10013          In other terms, ARG is a template argument pack element.
10014          In that case, we don't emit any DW_AT_name attribute for
10015          the die.  */
10016       if (emit_name_p)
10017         {
10018           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10019           gcc_assert (name);
10020           add_AT_string (tmpl_die, DW_AT_name, name);
10021         }
10022
10023       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10024         {
10025           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10026              TMPL_DIE should have a child DW_AT_type attribute that is set
10027              to the type of the argument to PARM, which is ARG.
10028              If PARM is a type generic parameter, TMPL_DIE should have a
10029              child DW_AT_type that is set to ARG.  */
10030           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10031           add_type_attribute (tmpl_die, tmpl_type, 0,
10032                               TREE_THIS_VOLATILE (tmpl_type),
10033                               parent_die);
10034         }
10035       else
10036         {
10037           /* So TMPL_DIE is a DIE representing a
10038              a generic generic template parameter, a.k.a template template
10039              parameter in C++ and arg is a template.  */
10040
10041           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10042              to the name of the argument.  */
10043           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10044           if (name)
10045             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10046         }
10047
10048       if (TREE_CODE (parm) == PARM_DECL)
10049         /* So PARM is a non-type generic parameter.
10050            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10051            attribute of TMPL_DIE which value represents the value
10052            of ARG.
10053            We must be careful here:
10054            The value of ARG might reference some function decls.
10055            We might currently be emitting debug info for a generic
10056            type and types are emitted before function decls, we don't
10057            know if the function decls referenced by ARG will actually be
10058            emitted after cgraph computations.
10059            So must defer the generation of the DW_AT_const_value to
10060            after cgraph is ready.  */
10061         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10062     }
10063
10064   return tmpl_die;
10065 }
10066
10067 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10068    PARM_PACK must be a template parameter pack. The returned DIE
10069    will be child DIE of PARENT_DIE.  */
10070
10071 static dw_die_ref
10072 template_parameter_pack_die (tree parm_pack,
10073                              tree parm_pack_args,
10074                              dw_die_ref parent_die)
10075 {
10076   dw_die_ref die;
10077   int j;
10078
10079   gcc_assert (parent_die && parm_pack);
10080
10081   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10082   add_name_and_src_coords_attributes (die, parm_pack);
10083   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10084     generic_parameter_die (parm_pack,
10085                            TREE_VEC_ELT (parm_pack_args, j),
10086                            false /* Don't emit DW_AT_name */,
10087                            die);
10088   return die;
10089 }
10090
10091 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10092    an enumerated type.  */
10093
10094 static inline int
10095 type_is_enum (const_tree type)
10096 {
10097   return TREE_CODE (type) == ENUMERAL_TYPE;
10098 }
10099
10100 /* Return the DBX register number described by a given RTL node.  */
10101
10102 static unsigned int
10103 dbx_reg_number (const_rtx rtl)
10104 {
10105   unsigned regno = REGNO (rtl);
10106
10107   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10108
10109 #ifdef LEAF_REG_REMAP
10110   if (current_function_uses_only_leaf_regs)
10111     {
10112       int leaf_reg = LEAF_REG_REMAP (regno);
10113       if (leaf_reg != -1)
10114         regno = (unsigned) leaf_reg;
10115     }
10116 #endif
10117
10118   return DBX_REGISTER_NUMBER (regno);
10119 }
10120
10121 /* Optionally add a DW_OP_piece term to a location description expression.
10122    DW_OP_piece is only added if the location description expression already
10123    doesn't end with DW_OP_piece.  */
10124
10125 static void
10126 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10127 {
10128   dw_loc_descr_ref loc;
10129
10130   if (*list_head != NULL)
10131     {
10132       /* Find the end of the chain.  */
10133       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10134         ;
10135
10136       if (loc->dw_loc_opc != DW_OP_piece)
10137         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10138     }
10139 }
10140
10141 /* Return a location descriptor that designates a machine register or
10142    zero if there is none.  */
10143
10144 static dw_loc_descr_ref
10145 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10146 {
10147   rtx regs;
10148
10149   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10150     return 0;
10151
10152   /* We only use "frame base" when we're sure we're talking about the
10153      post-prologue local stack frame.  We do this by *not* running
10154      register elimination until this point, and recognizing the special
10155      argument pointer and soft frame pointer rtx's.
10156      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10157   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10158       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10159     {
10160       dw_loc_descr_ref result = NULL;
10161
10162       if (dwarf_version >= 4 || !dwarf_strict)
10163         {
10164           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10165                                        initialized);
10166           if (result)
10167             add_loc_descr (&result,
10168                            new_loc_descr (DW_OP_stack_value, 0, 0));
10169         }
10170       return result;
10171     }
10172
10173   regs = targetm.dwarf_register_span (rtl);
10174
10175   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10176     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10177   else
10178     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10179 }
10180
10181 /* Return a location descriptor that designates a machine register for
10182    a given hard register number.  */
10183
10184 static dw_loc_descr_ref
10185 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10186 {
10187   dw_loc_descr_ref reg_loc_descr;
10188
10189   if (regno <= 31)
10190     reg_loc_descr
10191       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10192   else
10193     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10194
10195   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10196     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10197
10198   return reg_loc_descr;
10199 }
10200
10201 /* Given an RTL of a register, return a location descriptor that
10202    designates a value that spans more than one register.  */
10203
10204 static dw_loc_descr_ref
10205 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10206                              enum var_init_status initialized)
10207 {
10208   int nregs, size, i;
10209   unsigned reg;
10210   dw_loc_descr_ref loc_result = NULL;
10211
10212   reg = REGNO (rtl);
10213 #ifdef LEAF_REG_REMAP
10214   if (current_function_uses_only_leaf_regs)
10215     {
10216       int leaf_reg = LEAF_REG_REMAP (reg);
10217       if (leaf_reg != -1)
10218         reg = (unsigned) leaf_reg;
10219     }
10220 #endif
10221   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10222   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10223
10224   /* Simple, contiguous registers.  */
10225   if (regs == NULL_RTX)
10226     {
10227       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10228
10229       loc_result = NULL;
10230       while (nregs--)
10231         {
10232           dw_loc_descr_ref t;
10233
10234           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10235                                       VAR_INIT_STATUS_INITIALIZED);
10236           add_loc_descr (&loc_result, t);
10237           add_loc_descr_op_piece (&loc_result, size);
10238           ++reg;
10239         }
10240       return loc_result;
10241     }
10242
10243   /* Now onto stupid register sets in non contiguous locations.  */
10244
10245   gcc_assert (GET_CODE (regs) == PARALLEL);
10246
10247   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10248   loc_result = NULL;
10249
10250   for (i = 0; i < XVECLEN (regs, 0); ++i)
10251     {
10252       dw_loc_descr_ref t;
10253
10254       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10255                                   VAR_INIT_STATUS_INITIALIZED);
10256       add_loc_descr (&loc_result, t);
10257       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10258       add_loc_descr_op_piece (&loc_result, size);
10259     }
10260
10261   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10262     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10263   return loc_result;
10264 }
10265
10266 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10267
10268 /* Return a location descriptor that designates a constant i,
10269    as a compound operation from constant (i >> shift), constant shift
10270    and DW_OP_shl.  */
10271
10272 static dw_loc_descr_ref
10273 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10274 {
10275   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10276   add_loc_descr (&ret, int_loc_descriptor (shift));
10277   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10278   return ret;
10279 }
10280
10281 /* Return a location descriptor that designates a constant.  */
10282
10283 static dw_loc_descr_ref
10284 int_loc_descriptor (HOST_WIDE_INT i)
10285 {
10286   enum dwarf_location_atom op;
10287
10288   /* Pick the smallest representation of a constant, rather than just
10289      defaulting to the LEB encoding.  */
10290   if (i >= 0)
10291     {
10292       int clz = clz_hwi (i);
10293       int ctz = ctz_hwi (i);
10294       if (i <= 31)
10295         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10296       else if (i <= 0xff)
10297         op = DW_OP_const1u;
10298       else if (i <= 0xffff)
10299         op = DW_OP_const2u;
10300       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10301                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10302         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10303            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10304            while DW_OP_const4u is 5 bytes.  */
10305         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10306       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10307                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10308         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10309            while DW_OP_const4u is 5 bytes.  */
10310         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10311       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10312         op = DW_OP_const4u;
10313       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10314                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10315         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10316            while DW_OP_constu of constant >= 0x100000000 takes at least
10317            6 bytes.  */
10318         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10319       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10320                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10321                   >= HOST_BITS_PER_WIDE_INT)
10322         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10323            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10324            while DW_OP_constu takes in this case at least 6 bytes.  */
10325         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10326       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10327                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10328                && size_of_uleb128 (i) > 6)
10329         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10330         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10331       else
10332         op = DW_OP_constu;
10333     }
10334   else
10335     {
10336       if (i >= -0x80)
10337         op = DW_OP_const1s;
10338       else if (i >= -0x8000)
10339         op = DW_OP_const2s;
10340       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10341         {
10342           if (size_of_int_loc_descriptor (i) < 5)
10343             {
10344               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10345               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10346               return ret;
10347             }
10348           op = DW_OP_const4s;
10349         }
10350       else
10351         {
10352           if (size_of_int_loc_descriptor (i)
10353               < (unsigned long) 1 + size_of_sleb128 (i))
10354             {
10355               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10356               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10357               return ret;
10358             }
10359           op = DW_OP_consts;
10360         }
10361     }
10362
10363   return new_loc_descr (op, i, 0);
10364 }
10365
10366 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10367    without actually allocating it.  */
10368
10369 static unsigned long
10370 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10371 {
10372   return size_of_int_loc_descriptor (i >> shift)
10373          + size_of_int_loc_descriptor (shift)
10374          + 1;
10375 }
10376
10377 /* Return size_of_locs (int_loc_descriptor (i)) without
10378    actually allocating it.  */
10379
10380 static unsigned long
10381 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10382 {
10383   unsigned long s;
10384
10385   if (i >= 0)
10386     {
10387       int clz, ctz;
10388       if (i <= 31)
10389         return 1;
10390       else if (i <= 0xff)
10391         return 2;
10392       else if (i <= 0xffff)
10393         return 3;
10394       clz = clz_hwi (i);
10395       ctz = ctz_hwi (i);
10396       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10397           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10398         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10399                                                     - clz - 5);
10400       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10401                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10402         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10403                                                     - clz - 8);
10404       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10405         return 5;
10406       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10407       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10408           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10409         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10410                                                     - clz - 8);
10411       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10412                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10413         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10414                                                     - clz - 16);
10415       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10416                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10417                && s > 6)
10418         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10419                                                     - clz - 32);
10420       else
10421         return 1 + s;
10422     }
10423   else
10424     {
10425       if (i >= -0x80)
10426         return 2;
10427       else if (i >= -0x8000)
10428         return 3;
10429       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10430         {
10431           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10432             {
10433               s = size_of_int_loc_descriptor (-i) + 1;
10434               if (s < 5)
10435                 return s;
10436             }
10437           return 5;
10438         }
10439       else
10440         {
10441           unsigned long r = 1 + size_of_sleb128 (i);
10442           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10443             {
10444               s = size_of_int_loc_descriptor (-i) + 1;
10445               if (s < r)
10446                 return s;
10447             }
10448           return r;
10449         }
10450     }
10451 }
10452
10453 /* Return loc description representing "address" of integer value.
10454    This can appear only as toplevel expression.  */
10455
10456 static dw_loc_descr_ref
10457 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10458 {
10459   int litsize;
10460   dw_loc_descr_ref loc_result = NULL;
10461
10462   if (!(dwarf_version >= 4 || !dwarf_strict))
10463     return NULL;
10464
10465   litsize = size_of_int_loc_descriptor (i);
10466   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10467      is more compact.  For DW_OP_stack_value we need:
10468      litsize + 1 (DW_OP_stack_value)
10469      and for DW_OP_implicit_value:
10470      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10471   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10472     {
10473       loc_result = int_loc_descriptor (i);
10474       add_loc_descr (&loc_result,
10475                      new_loc_descr (DW_OP_stack_value, 0, 0));
10476       return loc_result;
10477     }
10478
10479   loc_result = new_loc_descr (DW_OP_implicit_value,
10480                               size, 0);
10481   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10482   loc_result->dw_loc_oprnd2.v.val_int = i;
10483   return loc_result;
10484 }
10485
10486 /* Return a location descriptor that designates a base+offset location.  */
10487
10488 static dw_loc_descr_ref
10489 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10490                  enum var_init_status initialized)
10491 {
10492   unsigned int regno;
10493   dw_loc_descr_ref result;
10494   dw_fde_ref fde = cfun->fde;
10495
10496   /* We only use "frame base" when we're sure we're talking about the
10497      post-prologue local stack frame.  We do this by *not* running
10498      register elimination until this point, and recognizing the special
10499      argument pointer and soft frame pointer rtx's.  */
10500   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10501     {
10502       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10503
10504       if (elim != reg)
10505         {
10506           if (GET_CODE (elim) == PLUS)
10507             {
10508               offset += INTVAL (XEXP (elim, 1));
10509               elim = XEXP (elim, 0);
10510             }
10511           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10512                        && (elim == hard_frame_pointer_rtx
10513                            || elim == stack_pointer_rtx))
10514                       || elim == (frame_pointer_needed
10515                                   ? hard_frame_pointer_rtx
10516                                   : stack_pointer_rtx));
10517
10518           /* If drap register is used to align stack, use frame
10519              pointer + offset to access stack variables.  If stack
10520              is aligned without drap, use stack pointer + offset to
10521              access stack variables.  */
10522           if (crtl->stack_realign_tried
10523               && reg == frame_pointer_rtx)
10524             {
10525               int base_reg
10526                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10527                                       ? HARD_FRAME_POINTER_REGNUM
10528                                       : REGNO (elim));
10529               return new_reg_loc_descr (base_reg, offset);
10530             }
10531
10532           gcc_assert (frame_pointer_fb_offset_valid);
10533           offset += frame_pointer_fb_offset;
10534           return new_loc_descr (DW_OP_fbreg, offset, 0);
10535         }
10536     }
10537
10538   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10539
10540   if (!optimize && fde
10541       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10542     {
10543       /* Use cfa+offset to represent the location of arguments passed
10544          on the stack when drap is used to align stack.
10545          Only do this when not optimizing, for optimized code var-tracking
10546          is supposed to track where the arguments live and the register
10547          used as vdrap or drap in some spot might be used for something
10548          else in other part of the routine.  */
10549       return new_loc_descr (DW_OP_fbreg, offset, 0);
10550     }
10551
10552   if (regno <= 31)
10553     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10554                             offset, 0);
10555   else
10556     result = new_loc_descr (DW_OP_bregx, regno, offset);
10557
10558   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10559     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10560
10561   return result;
10562 }
10563
10564 /* Return true if this RTL expression describes a base+offset calculation.  */
10565
10566 static inline int
10567 is_based_loc (const_rtx rtl)
10568 {
10569   return (GET_CODE (rtl) == PLUS
10570           && ((REG_P (XEXP (rtl, 0))
10571                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10572                && CONST_INT_P (XEXP (rtl, 1)))));
10573 }
10574
10575 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10576    failed.  */
10577
10578 static dw_loc_descr_ref
10579 tls_mem_loc_descriptor (rtx mem)
10580 {
10581   tree base;
10582   dw_loc_descr_ref loc_result;
10583
10584   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10585     return NULL;
10586
10587   base = get_base_address (MEM_EXPR (mem));
10588   if (base == NULL
10589       || TREE_CODE (base) != VAR_DECL
10590       || !DECL_THREAD_LOCAL_P (base))
10591     return NULL;
10592
10593   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10594   if (loc_result == NULL)
10595     return NULL;
10596
10597   if (MEM_OFFSET (mem))
10598     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10599
10600   return loc_result;
10601 }
10602
10603 /* Output debug info about reason why we failed to expand expression as dwarf
10604    expression.  */
10605
10606 static void
10607 expansion_failed (tree expr, rtx rtl, char const *reason)
10608 {
10609   if (dump_file && (dump_flags & TDF_DETAILS))
10610     {
10611       fprintf (dump_file, "Failed to expand as dwarf: ");
10612       if (expr)
10613         print_generic_expr (dump_file, expr, dump_flags);
10614       if (rtl)
10615         {
10616           fprintf (dump_file, "\n");
10617           print_rtl (dump_file, rtl);
10618         }
10619       fprintf (dump_file, "\nReason: %s\n", reason);
10620     }
10621 }
10622
10623 /* Helper function for const_ok_for_output, called either directly
10624    or via for_each_rtx.  */
10625
10626 static int
10627 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10628 {
10629   rtx rtl = *rtlp;
10630
10631   if (GET_CODE (rtl) == UNSPEC)
10632     {
10633       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10634          we can't express it in the debug info.  */
10635 #ifdef ENABLE_CHECKING
10636       /* Don't complain about TLS UNSPECs, those are just too hard to
10637          delegitimize.  */
10638       if (XVECLEN (rtl, 0) != 1
10639           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10640           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10641           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10642           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10643         inform (current_function_decl
10644                 ? DECL_SOURCE_LOCATION (current_function_decl)
10645                 : UNKNOWN_LOCATION,
10646 #if NUM_UNSPEC_VALUES > 0
10647                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10648                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10649                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10650                 XINT (rtl, 1));
10651 #else
10652                 "non-delegitimized UNSPEC %d found in variable location",
10653                 XINT (rtl, 1));
10654 #endif
10655 #endif
10656       expansion_failed (NULL_TREE, rtl,
10657                         "UNSPEC hasn't been delegitimized.\n");
10658       return 1;
10659     }
10660
10661   if (GET_CODE (rtl) != SYMBOL_REF)
10662     return 0;
10663
10664   if (CONSTANT_POOL_ADDRESS_P (rtl))
10665     {
10666       bool marked;
10667       get_pool_constant_mark (rtl, &marked);
10668       /* If all references to this pool constant were optimized away,
10669          it was not output and thus we can't represent it.  */
10670       if (!marked)
10671         {
10672           expansion_failed (NULL_TREE, rtl,
10673                             "Constant was removed from constant pool.\n");
10674           return 1;
10675         }
10676     }
10677
10678   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10679     return 1;
10680
10681   /* Avoid references to external symbols in debug info, on several targets
10682      the linker might even refuse to link when linking a shared library,
10683      and in many other cases the relocations for .debug_info/.debug_loc are
10684      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10685      to be defined within the same shared library or executable are fine.  */
10686   if (SYMBOL_REF_EXTERNAL_P (rtl))
10687     {
10688       tree decl = SYMBOL_REF_DECL (rtl);
10689
10690       if (decl == NULL || !targetm.binds_local_p (decl))
10691         {
10692           expansion_failed (NULL_TREE, rtl,
10693                             "Symbol not defined in current TU.\n");
10694           return 1;
10695         }
10696     }
10697
10698   return 0;
10699 }
10700
10701 /* Return true if constant RTL can be emitted in DW_OP_addr or
10702    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10703    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10704
10705 static bool
10706 const_ok_for_output (rtx rtl)
10707 {
10708   if (GET_CODE (rtl) == SYMBOL_REF)
10709     return const_ok_for_output_1 (&rtl, NULL) == 0;
10710
10711   if (GET_CODE (rtl) == CONST)
10712     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10713
10714   return true;
10715 }
10716
10717 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10718    if possible, NULL otherwise.  */
10719
10720 static dw_die_ref
10721 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10722 {
10723   dw_die_ref type_die;
10724   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10725
10726   if (type == NULL)
10727     return NULL;
10728   switch (TREE_CODE (type))
10729     {
10730     case INTEGER_TYPE:
10731     case REAL_TYPE:
10732       break;
10733     default:
10734       return NULL;
10735     }
10736   type_die = lookup_type_die (type);
10737   if (!type_die)
10738     type_die = modified_type_die (type, false, false, comp_unit_die ());
10739   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10740     return NULL;
10741   return type_die;
10742 }
10743
10744 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10745    type matching MODE, or, if MODE is narrower than or as wide as
10746    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10747    possible.  */
10748
10749 static dw_loc_descr_ref
10750 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10751 {
10752   enum machine_mode outer_mode = mode;
10753   dw_die_ref type_die;
10754   dw_loc_descr_ref cvt;
10755
10756   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10757     {
10758       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10759       return op;
10760     }
10761   type_die = base_type_for_mode (outer_mode, 1);
10762   if (type_die == NULL)
10763     return NULL;
10764   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10765   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10766   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10767   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10768   add_loc_descr (&op, cvt);
10769   return op;
10770 }
10771
10772 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10773
10774 static dw_loc_descr_ref
10775 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10776                         dw_loc_descr_ref op1)
10777 {
10778   dw_loc_descr_ref ret = op0;
10779   add_loc_descr (&ret, op1);
10780   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10781   if (STORE_FLAG_VALUE != 1)
10782     {
10783       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10784       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10785     }
10786   return ret;
10787 }
10788
10789 /* Return location descriptor for signed comparison OP RTL.  */
10790
10791 static dw_loc_descr_ref
10792 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10793                          enum machine_mode mem_mode)
10794 {
10795   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10796   dw_loc_descr_ref op0, op1;
10797   int shift;
10798
10799   if (op_mode == VOIDmode)
10800     op_mode = GET_MODE (XEXP (rtl, 1));
10801   if (op_mode == VOIDmode)
10802     return NULL;
10803
10804   if (dwarf_strict
10805       && (GET_MODE_CLASS (op_mode) != MODE_INT
10806           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10807     return NULL;
10808
10809   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10810                             VAR_INIT_STATUS_INITIALIZED);
10811   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10812                             VAR_INIT_STATUS_INITIALIZED);
10813
10814   if (op0 == NULL || op1 == NULL)
10815     return NULL;
10816
10817   if (GET_MODE_CLASS (op_mode) != MODE_INT
10818       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10819     return compare_loc_descriptor (op, op0, op1);
10820
10821   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10822     {
10823       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10824       dw_loc_descr_ref cvt;
10825
10826       if (type_die == NULL)
10827         return NULL;
10828       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10829       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10830       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10831       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10832       add_loc_descr (&op0, cvt);
10833       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10834       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10835       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10836       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10837       add_loc_descr (&op1, cvt);
10838       return compare_loc_descriptor (op, op0, op1);
10839     }
10840
10841   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10842   /* For eq/ne, if the operands are known to be zero-extended,
10843      there is no need to do the fancy shifting up.  */
10844   if (op == DW_OP_eq || op == DW_OP_ne)
10845     {
10846       dw_loc_descr_ref last0, last1;
10847       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10848         ;
10849       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10850         ;
10851       /* deref_size zero extends, and for constants we can check
10852          whether they are zero extended or not.  */
10853       if (((last0->dw_loc_opc == DW_OP_deref_size
10854             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10855            || (CONST_INT_P (XEXP (rtl, 0))
10856                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10857                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10858           && ((last1->dw_loc_opc == DW_OP_deref_size
10859                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10860               || (CONST_INT_P (XEXP (rtl, 1))
10861                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10862                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10863         return compare_loc_descriptor (op, op0, op1);
10864
10865       /* EQ/NE comparison against constant in narrower type than
10866          DWARF2_ADDR_SIZE can be performed either as
10867          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10868          DW_OP_{eq,ne}
10869          or
10870          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10871          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10872       if (CONST_INT_P (XEXP (rtl, 1))
10873           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10874           && (size_of_int_loc_descriptor (shift) + 1
10875               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10876               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10877                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10878                                                & GET_MODE_MASK (op_mode))))
10879         {
10880           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10881           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10882           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10883                                     & GET_MODE_MASK (op_mode));
10884           return compare_loc_descriptor (op, op0, op1);
10885         }
10886     }
10887   add_loc_descr (&op0, int_loc_descriptor (shift));
10888   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10889   if (CONST_INT_P (XEXP (rtl, 1)))
10890     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10891   else
10892     {
10893       add_loc_descr (&op1, int_loc_descriptor (shift));
10894       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10895     }
10896   return compare_loc_descriptor (op, op0, op1);
10897 }
10898
10899 /* Return location descriptor for unsigned comparison OP RTL.  */
10900
10901 static dw_loc_descr_ref
10902 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10903                          enum machine_mode mem_mode)
10904 {
10905   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10906   dw_loc_descr_ref op0, op1;
10907
10908   if (op_mode == VOIDmode)
10909     op_mode = GET_MODE (XEXP (rtl, 1));
10910   if (op_mode == VOIDmode)
10911     return NULL;
10912   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10913     return NULL;
10914
10915   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10916     return NULL;
10917
10918   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10919                             VAR_INIT_STATUS_INITIALIZED);
10920   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10921                             VAR_INIT_STATUS_INITIALIZED);
10922
10923   if (op0 == NULL || op1 == NULL)
10924     return NULL;
10925
10926   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10927     {
10928       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10929       dw_loc_descr_ref last0, last1;
10930       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10931         ;
10932       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10933         ;
10934       if (CONST_INT_P (XEXP (rtl, 0)))
10935         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10936       /* deref_size zero extends, so no need to mask it again.  */
10937       else if (last0->dw_loc_opc != DW_OP_deref_size
10938                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10939         {
10940           add_loc_descr (&op0, int_loc_descriptor (mask));
10941           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10942         }
10943       if (CONST_INT_P (XEXP (rtl, 1)))
10944         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10945       /* deref_size zero extends, so no need to mask it again.  */
10946       else if (last1->dw_loc_opc != DW_OP_deref_size
10947                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10948         {
10949           add_loc_descr (&op1, int_loc_descriptor (mask));
10950           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10951         }
10952     }
10953   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10954     {
10955       HOST_WIDE_INT bias = 1;
10956       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10957       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10958       if (CONST_INT_P (XEXP (rtl, 1)))
10959         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10960                                   + INTVAL (XEXP (rtl, 1)));
10961       else
10962         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10963                                             bias, 0));
10964     }
10965   return compare_loc_descriptor (op, op0, op1);
10966 }
10967
10968 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10969
10970 static dw_loc_descr_ref
10971 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10972                        enum machine_mode mem_mode)
10973 {
10974   enum dwarf_location_atom op;
10975   dw_loc_descr_ref op0, op1, ret;
10976   dw_loc_descr_ref bra_node, drop_node;
10977
10978   if (dwarf_strict
10979       && (GET_MODE_CLASS (mode) != MODE_INT
10980           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10981     return NULL;
10982
10983   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10984                             VAR_INIT_STATUS_INITIALIZED);
10985   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10986                             VAR_INIT_STATUS_INITIALIZED);
10987
10988   if (op0 == NULL || op1 == NULL)
10989     return NULL;
10990
10991   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10992   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10993   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10994   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10995     {
10996       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10997         {
10998           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10999           add_loc_descr (&op0, int_loc_descriptor (mask));
11000           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11001           add_loc_descr (&op1, int_loc_descriptor (mask));
11002           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11003         }
11004       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11005         {
11006           HOST_WIDE_INT bias = 1;
11007           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11008           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11009           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11010         }
11011     }
11012   else if (GET_MODE_CLASS (mode) == MODE_INT
11013            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11014     {
11015       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11016       add_loc_descr (&op0, int_loc_descriptor (shift));
11017       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11018       add_loc_descr (&op1, int_loc_descriptor (shift));
11019       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11020     }
11021   else if (GET_MODE_CLASS (mode) == MODE_INT
11022            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11023     {
11024       dw_die_ref type_die = base_type_for_mode (mode, 0);
11025       dw_loc_descr_ref cvt;
11026       if (type_die == NULL)
11027         return NULL;
11028       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11029       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11030       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11031       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11032       add_loc_descr (&op0, cvt);
11033       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11034       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11035       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11036       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11037       add_loc_descr (&op1, cvt);
11038     }
11039
11040   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11041     op = DW_OP_lt;
11042   else
11043     op = DW_OP_gt;
11044   ret = op0;
11045   add_loc_descr (&ret, op1);
11046   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11047   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11048   add_loc_descr (&ret, bra_node);
11049   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11050   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11051   add_loc_descr (&ret, drop_node);
11052   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11053   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11054   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11055       && GET_MODE_CLASS (mode) == MODE_INT
11056       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11057     ret = convert_descriptor_to_mode (mode, ret);
11058   return ret;
11059 }
11060
11061 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11062    but after converting arguments to type_die, afterwards
11063    convert back to unsigned.  */
11064
11065 static dw_loc_descr_ref
11066 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11067              enum machine_mode mode, enum machine_mode mem_mode)
11068 {
11069   dw_loc_descr_ref cvt, op0, op1;
11070
11071   if (type_die == NULL)
11072     return NULL;
11073   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11074                             VAR_INIT_STATUS_INITIALIZED);
11075   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11076                             VAR_INIT_STATUS_INITIALIZED);
11077   if (op0 == NULL || op1 == NULL)
11078     return NULL;
11079   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11080   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11081   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11082   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11083   add_loc_descr (&op0, cvt);
11084   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11085   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11086   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11087   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11088   add_loc_descr (&op1, cvt);
11089   add_loc_descr (&op0, op1);
11090   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11091   return convert_descriptor_to_mode (mode, op0);
11092 }
11093
11094 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11095    const0 is DW_OP_lit0 or corresponding typed constant,
11096    const1 is DW_OP_lit1 or corresponding typed constant
11097    and constMSB is constant with just the MSB bit set
11098    for the mode):
11099        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11100    L1: const0 DW_OP_swap
11101    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11102        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11103    L3: DW_OP_drop
11104    L4: DW_OP_nop
11105
11106    CTZ is similar:
11107        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11108    L1: const0 DW_OP_swap
11109    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11110        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11111    L3: DW_OP_drop
11112    L4: DW_OP_nop
11113
11114    FFS is similar:
11115        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11116    L1: const1 DW_OP_swap
11117    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11118        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11119    L3: DW_OP_drop
11120    L4: DW_OP_nop  */
11121
11122 static dw_loc_descr_ref
11123 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11124                     enum machine_mode mem_mode)
11125 {
11126   dw_loc_descr_ref op0, ret, tmp;
11127   HOST_WIDE_INT valv;
11128   dw_loc_descr_ref l1jump, l1label;
11129   dw_loc_descr_ref l2jump, l2label;
11130   dw_loc_descr_ref l3jump, l3label;
11131   dw_loc_descr_ref l4jump, l4label;
11132   rtx msb;
11133
11134   if (GET_MODE_CLASS (mode) != MODE_INT
11135       || GET_MODE (XEXP (rtl, 0)) != mode
11136       || (GET_CODE (rtl) == CLZ
11137           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11138     return NULL;
11139
11140   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11141                             VAR_INIT_STATUS_INITIALIZED);
11142   if (op0 == NULL)
11143     return NULL;
11144   ret = op0;
11145   if (GET_CODE (rtl) == CLZ)
11146     {
11147       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11148         valv = GET_MODE_BITSIZE (mode);
11149     }
11150   else if (GET_CODE (rtl) == FFS)
11151     valv = 0;
11152   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11153     valv = GET_MODE_BITSIZE (mode);
11154   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11155   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11156   add_loc_descr (&ret, l1jump);
11157   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11158   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11159                             VAR_INIT_STATUS_INITIALIZED);
11160   if (tmp == NULL)
11161     return NULL;
11162   add_loc_descr (&ret, tmp);
11163   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11164   add_loc_descr (&ret, l4jump);
11165   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11166                                 ? const1_rtx : const0_rtx,
11167                                 mode, mem_mode,
11168                                 VAR_INIT_STATUS_INITIALIZED);
11169   if (l1label == NULL)
11170     return NULL;
11171   add_loc_descr (&ret, l1label);
11172   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11173   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11174   add_loc_descr (&ret, l2label);
11175   if (GET_CODE (rtl) != CLZ)
11176     msb = const1_rtx;
11177   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11178     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11179                    << (GET_MODE_BITSIZE (mode) - 1));
11180   else
11181     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11182                                   << (GET_MODE_BITSIZE (mode)
11183                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11184   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11185     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11186                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11187                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11188   else
11189     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11190                               VAR_INIT_STATUS_INITIALIZED);
11191   if (tmp == NULL)
11192     return NULL;
11193   add_loc_descr (&ret, tmp);
11194   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11195   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11196   add_loc_descr (&ret, l3jump);
11197   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11198                             VAR_INIT_STATUS_INITIALIZED);
11199   if (tmp == NULL)
11200     return NULL;
11201   add_loc_descr (&ret, tmp);
11202   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11203                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11204   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11205   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11206   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11207   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11208   add_loc_descr (&ret, l2jump);
11209   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11210   add_loc_descr (&ret, l3label);
11211   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11212   add_loc_descr (&ret, l4label);
11213   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11214   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11215   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11216   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11217   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11218   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11219   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11220   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11221   return ret;
11222 }
11223
11224 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11225    const1 is DW_OP_lit1 or corresponding typed constant):
11226        const0 DW_OP_swap
11227    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11228        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11229    L2: DW_OP_drop
11230
11231    PARITY is similar:
11232    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11233        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11234    L2: DW_OP_drop  */
11235
11236 static dw_loc_descr_ref
11237 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11238                          enum machine_mode mem_mode)
11239 {
11240   dw_loc_descr_ref op0, ret, tmp;
11241   dw_loc_descr_ref l1jump, l1label;
11242   dw_loc_descr_ref l2jump, l2label;
11243
11244   if (GET_MODE_CLASS (mode) != MODE_INT
11245       || GET_MODE (XEXP (rtl, 0)) != mode)
11246     return NULL;
11247
11248   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11249                             VAR_INIT_STATUS_INITIALIZED);
11250   if (op0 == NULL)
11251     return NULL;
11252   ret = op0;
11253   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11254                             VAR_INIT_STATUS_INITIALIZED);
11255   if (tmp == NULL)
11256     return NULL;
11257   add_loc_descr (&ret, tmp);
11258   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11259   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11260   add_loc_descr (&ret, l1label);
11261   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11262   add_loc_descr (&ret, l2jump);
11263   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11264   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11265   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11266                             VAR_INIT_STATUS_INITIALIZED);
11267   if (tmp == NULL)
11268     return NULL;
11269   add_loc_descr (&ret, tmp);
11270   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11271   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11272                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11273   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11274   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11275                             VAR_INIT_STATUS_INITIALIZED);
11276   add_loc_descr (&ret, tmp);
11277   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11278   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11279   add_loc_descr (&ret, l1jump);
11280   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11281   add_loc_descr (&ret, l2label);
11282   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11283   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11284   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11285   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11286   return ret;
11287 }
11288
11289 /* BSWAP (constS is initial shift count, either 56 or 24):
11290        constS const0
11291    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11292        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11293        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11294        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11295    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11296
11297 static dw_loc_descr_ref
11298 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11299                       enum machine_mode mem_mode)
11300 {
11301   dw_loc_descr_ref op0, ret, tmp;
11302   dw_loc_descr_ref l1jump, l1label;
11303   dw_loc_descr_ref l2jump, l2label;
11304
11305   if (GET_MODE_CLASS (mode) != MODE_INT
11306       || BITS_PER_UNIT != 8
11307       || (GET_MODE_BITSIZE (mode) != 32
11308           &&  GET_MODE_BITSIZE (mode) != 64))
11309     return NULL;
11310
11311   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11312                             VAR_INIT_STATUS_INITIALIZED);
11313   if (op0 == NULL)
11314     return NULL;
11315
11316   ret = op0;
11317   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11318                             mode, mem_mode,
11319                             VAR_INIT_STATUS_INITIALIZED);
11320   if (tmp == NULL)
11321     return NULL;
11322   add_loc_descr (&ret, tmp);
11323   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11324                             VAR_INIT_STATUS_INITIALIZED);
11325   if (tmp == NULL)
11326     return NULL;
11327   add_loc_descr (&ret, tmp);
11328   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11329   add_loc_descr (&ret, l1label);
11330   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11331                             mode, mem_mode,
11332                             VAR_INIT_STATUS_INITIALIZED);
11333   add_loc_descr (&ret, tmp);
11334   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11335   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11336   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11337   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11338                             VAR_INIT_STATUS_INITIALIZED);
11339   if (tmp == NULL)
11340     return NULL;
11341   add_loc_descr (&ret, tmp);
11342   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11343   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11344   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11345   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11346   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11347   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11348   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11349                             VAR_INIT_STATUS_INITIALIZED);
11350   add_loc_descr (&ret, tmp);
11351   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11352   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11353   add_loc_descr (&ret, l2jump);
11354   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11355                             VAR_INIT_STATUS_INITIALIZED);
11356   add_loc_descr (&ret, tmp);
11357   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11358   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11359   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11360   add_loc_descr (&ret, l1jump);
11361   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11362   add_loc_descr (&ret, l2label);
11363   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11364   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11365   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11366   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11367   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11368   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11369   return ret;
11370 }
11371
11372 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11373    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11374    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11375    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11376
11377    ROTATERT is similar:
11378    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11379    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11380    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11381
11382 static dw_loc_descr_ref
11383 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11384                        enum machine_mode mem_mode)
11385 {
11386   rtx rtlop1 = XEXP (rtl, 1);
11387   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11388   int i;
11389
11390   if (GET_MODE_CLASS (mode) != MODE_INT)
11391     return NULL;
11392
11393   if (GET_MODE (rtlop1) != VOIDmode
11394       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11395     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11396   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11397                             VAR_INIT_STATUS_INITIALIZED);
11398   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11399                             VAR_INIT_STATUS_INITIALIZED);
11400   if (op0 == NULL || op1 == NULL)
11401     return NULL;
11402   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11403     for (i = 0; i < 2; i++)
11404       {
11405         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11406           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11407                                         mode, mem_mode,
11408                                         VAR_INIT_STATUS_INITIALIZED);
11409         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11410           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11411                                    ? DW_OP_const4u
11412                                    : HOST_BITS_PER_WIDE_INT == 64
11413                                    ? DW_OP_const8u : DW_OP_constu,
11414                                    GET_MODE_MASK (mode), 0);
11415         else
11416           mask[i] = NULL;
11417         if (mask[i] == NULL)
11418           return NULL;
11419         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11420       }
11421   ret = op0;
11422   add_loc_descr (&ret, op1);
11423   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11424   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11425   if (GET_CODE (rtl) == ROTATERT)
11426     {
11427       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11428       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11429                                           GET_MODE_BITSIZE (mode), 0));
11430     }
11431   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11432   if (mask[0] != NULL)
11433     add_loc_descr (&ret, mask[0]);
11434   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11435   if (mask[1] != NULL)
11436     {
11437       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11438       add_loc_descr (&ret, mask[1]);
11439       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11440     }
11441   if (GET_CODE (rtl) == ROTATE)
11442     {
11443       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11444       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11445                                           GET_MODE_BITSIZE (mode), 0));
11446     }
11447   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11448   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11449   return ret;
11450 }
11451
11452 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11453    for DEBUG_PARAMETER_REF RTL.  */
11454
11455 static dw_loc_descr_ref
11456 parameter_ref_descriptor (rtx rtl)
11457 {
11458   dw_loc_descr_ref ret;
11459   dw_die_ref ref;
11460
11461   if (dwarf_strict)
11462     return NULL;
11463   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11464   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11465   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11466   if (ref)
11467     {
11468       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11469       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11470       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11471     }
11472   else
11473     {
11474       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11475       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11476     }
11477   return ret;
11478 }
11479
11480 /* Helper function to get mode of MEM's address.  */
11481
11482 enum machine_mode
11483 get_address_mode (rtx mem)
11484 {
11485   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11486   if (mode != VOIDmode)
11487     return mode;
11488   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11489 }
11490
11491 /* The following routine converts the RTL for a variable or parameter
11492    (resident in memory) into an equivalent Dwarf representation of a
11493    mechanism for getting the address of that same variable onto the top of a
11494    hypothetical "address evaluation" stack.
11495
11496    When creating memory location descriptors, we are effectively transforming
11497    the RTL for a memory-resident object into its Dwarf postfix expression
11498    equivalent.  This routine recursively descends an RTL tree, turning
11499    it into Dwarf postfix code as it goes.
11500
11501    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11502
11503    MEM_MODE is the mode of the memory reference, needed to handle some
11504    autoincrement addressing modes.
11505
11506    Return 0 if we can't represent the location.  */
11507
11508 dw_loc_descr_ref
11509 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11510                     enum machine_mode mem_mode,
11511                     enum var_init_status initialized)
11512 {
11513   dw_loc_descr_ref mem_loc_result = NULL;
11514   enum dwarf_location_atom op;
11515   dw_loc_descr_ref op0, op1;
11516
11517   if (mode == VOIDmode)
11518     mode = GET_MODE (rtl);
11519
11520   /* Note that for a dynamically sized array, the location we will generate a
11521      description of here will be the lowest numbered location which is
11522      actually within the array.  That's *not* necessarily the same as the
11523      zeroth element of the array.  */
11524
11525   rtl = targetm.delegitimize_address (rtl);
11526
11527   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11528     return NULL;
11529
11530   switch (GET_CODE (rtl))
11531     {
11532     case POST_INC:
11533     case POST_DEC:
11534     case POST_MODIFY:
11535       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11536
11537     case SUBREG:
11538       /* The case of a subreg may arise when we have a local (register)
11539          variable or a formal (register) parameter which doesn't quite fill
11540          up an entire register.  For now, just assume that it is
11541          legitimate to make the Dwarf info refer to the whole register which
11542          contains the given subreg.  */
11543       if (!subreg_lowpart_p (rtl))
11544         break;
11545       if (GET_MODE_CLASS (mode) == MODE_INT
11546           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11547           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11548 #ifdef POINTERS_EXTEND_UNSIGNED
11549               || (mode == Pmode && mem_mode != VOIDmode)
11550 #endif
11551              )
11552           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11553         {
11554           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11555                                                GET_MODE (SUBREG_REG (rtl)),
11556                                                mem_mode, initialized);
11557           break;
11558         }
11559       if (dwarf_strict)
11560         break;
11561       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11562         break;
11563       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11564           && (GET_MODE_CLASS (mode) != MODE_INT
11565               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11566         break;
11567       else
11568         {
11569           dw_die_ref type_die;
11570           dw_loc_descr_ref cvt;
11571
11572           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11573                                                GET_MODE (SUBREG_REG (rtl)),
11574                                                mem_mode, initialized);
11575           if (mem_loc_result == NULL)
11576             break;
11577           type_die = base_type_for_mode (mode,
11578                                          GET_MODE_CLASS (mode) == MODE_INT);
11579           if (type_die == NULL)
11580             {
11581               mem_loc_result = NULL;
11582               break;
11583             }
11584           if (GET_MODE_SIZE (mode)
11585               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11586             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11587           else
11588             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11589           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11590           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11591           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11592           add_loc_descr (&mem_loc_result, cvt);
11593         }
11594       break;
11595
11596     case REG:
11597       if (GET_MODE_CLASS (mode) != MODE_INT
11598           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11599 #ifdef POINTERS_EXTEND_UNSIGNED
11600               && (mode != Pmode || mem_mode == VOIDmode)
11601 #endif
11602               ))
11603         {
11604           dw_die_ref type_die;
11605
11606           if (dwarf_strict)
11607             break;
11608           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11609             break;
11610           type_die = base_type_for_mode (mode,
11611                                          GET_MODE_CLASS (mode) == MODE_INT);
11612           if (type_die == NULL)
11613             break;
11614           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11615                                           dbx_reg_number (rtl), 0);
11616           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11617           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11618           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11619           break;
11620         }
11621       /* Whenever a register number forms a part of the description of the
11622          method for calculating the (dynamic) address of a memory resident
11623          object, DWARF rules require the register number be referred to as
11624          a "base register".  This distinction is not based in any way upon
11625          what category of register the hardware believes the given register
11626          belongs to.  This is strictly DWARF terminology we're dealing with
11627          here. Note that in cases where the location of a memory-resident
11628          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11629          OP_CONST (0)) the actual DWARF location descriptor that we generate
11630          may just be OP_BASEREG (basereg).  This may look deceptively like
11631          the object in question was allocated to a register (rather than in
11632          memory) so DWARF consumers need to be aware of the subtle
11633          distinction between OP_REG and OP_BASEREG.  */
11634       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11635         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11636       else if (stack_realign_drap
11637                && crtl->drap_reg
11638                && crtl->args.internal_arg_pointer == rtl
11639                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11640         {
11641           /* If RTL is internal_arg_pointer, which has been optimized
11642              out, use DRAP instead.  */
11643           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11644                                             VAR_INIT_STATUS_INITIALIZED);
11645         }
11646       break;
11647
11648     case SIGN_EXTEND:
11649     case ZERO_EXTEND:
11650       if (GET_MODE_CLASS (mode) != MODE_INT)
11651         break;
11652       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11653                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11654       if (op0 == 0)
11655         break;
11656       else if (GET_CODE (rtl) == ZERO_EXTEND
11657                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11658                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11659                   < HOST_BITS_PER_WIDE_INT
11660                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11661                   to expand zero extend as two shifts instead of
11662                   masking.  */
11663                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11664         {
11665           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11666           mem_loc_result = op0;
11667           add_loc_descr (&mem_loc_result,
11668                          int_loc_descriptor (GET_MODE_MASK (imode)));
11669           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11670         }
11671       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11672         {
11673           int shift = DWARF2_ADDR_SIZE
11674                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11675           shift *= BITS_PER_UNIT;
11676           if (GET_CODE (rtl) == SIGN_EXTEND)
11677             op = DW_OP_shra;
11678           else
11679             op = DW_OP_shr;
11680           mem_loc_result = op0;
11681           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11682           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11683           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11684           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11685         }
11686       else if (!dwarf_strict)
11687         {
11688           dw_die_ref type_die1, type_die2;
11689           dw_loc_descr_ref cvt;
11690
11691           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11692                                           GET_CODE (rtl) == ZERO_EXTEND);
11693           if (type_die1 == NULL)
11694             break;
11695           type_die2 = base_type_for_mode (mode, 1);
11696           if (type_die2 == NULL)
11697             break;
11698           mem_loc_result = op0;
11699           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11700           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11701           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11702           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11703           add_loc_descr (&mem_loc_result, cvt);
11704           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11705           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11706           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11707           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11708           add_loc_descr (&mem_loc_result, cvt);
11709         }
11710       break;
11711
11712     case MEM:
11713       {
11714         rtx new_rtl = avoid_constant_pool_reference (rtl);
11715         if (new_rtl != rtl)
11716           {
11717             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11718                                                  initialized);
11719             if (mem_loc_result != NULL)
11720               return mem_loc_result;
11721           }
11722       }
11723       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11724                                            get_address_mode (rtl), mode,
11725                                            VAR_INIT_STATUS_INITIALIZED);
11726       if (mem_loc_result == NULL)
11727         mem_loc_result = tls_mem_loc_descriptor (rtl);
11728       if (mem_loc_result != NULL)
11729         {
11730           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11731               || GET_MODE_CLASS (mode) != MODE_INT)
11732             {
11733               dw_die_ref type_die;
11734               dw_loc_descr_ref deref;
11735
11736               if (dwarf_strict)
11737                 return NULL;
11738               type_die
11739                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11740               if (type_die == NULL)
11741                 return NULL;
11742               deref = new_loc_descr (DW_OP_GNU_deref_type,
11743                                      GET_MODE_SIZE (mode), 0);
11744               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11745               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11746               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11747               add_loc_descr (&mem_loc_result, deref);
11748             }
11749           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11750             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11751           else
11752             add_loc_descr (&mem_loc_result,
11753                            new_loc_descr (DW_OP_deref_size,
11754                                           GET_MODE_SIZE (mode), 0));
11755         }
11756       break;
11757
11758     case LO_SUM:
11759       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11760
11761     case LABEL_REF:
11762       /* Some ports can transform a symbol ref into a label ref, because
11763          the symbol ref is too far away and has to be dumped into a constant
11764          pool.  */
11765     case CONST:
11766     case SYMBOL_REF:
11767       if (GET_MODE_CLASS (mode) != MODE_INT
11768           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11769 #ifdef POINTERS_EXTEND_UNSIGNED
11770               && (mode != Pmode || mem_mode == VOIDmode)
11771 #endif
11772               ))
11773         break;
11774       if (GET_CODE (rtl) == SYMBOL_REF
11775           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11776         {
11777           dw_loc_descr_ref temp;
11778
11779           /* If this is not defined, we have no way to emit the data.  */
11780           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11781             break;
11782
11783           /* We used to emit DW_OP_addr here, but that's wrong, since
11784              DW_OP_addr should be relocated by the debug info consumer,
11785              while DW_OP_GNU_push_tls_address operand should not.  */
11786           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11787                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11788           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11789           temp->dw_loc_oprnd1.v.val_addr = rtl;
11790           temp->dtprel = true;
11791
11792           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11793           add_loc_descr (&mem_loc_result, temp);
11794
11795           break;
11796         }
11797
11798       if (!const_ok_for_output (rtl))
11799         break;
11800
11801     symref:
11802       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11803       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11804       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11805       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11806       break;
11807
11808     case CONCAT:
11809     case CONCATN:
11810     case VAR_LOCATION:
11811     case DEBUG_IMPLICIT_PTR:
11812       expansion_failed (NULL_TREE, rtl,
11813                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11814       return 0;
11815
11816     case ENTRY_VALUE:
11817       if (dwarf_strict)
11818         return NULL;
11819       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11820         {
11821           if (GET_MODE_CLASS (mode) != MODE_INT
11822               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11823             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11824                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11825           else
11826             op0
11827               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11828                                         VAR_INIT_STATUS_INITIALIZED);
11829         }
11830       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11831                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11832         {
11833           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11834                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11835           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11836             return NULL;
11837         }
11838       else
11839         gcc_unreachable ();
11840       if (op0 == NULL)
11841         return NULL;
11842       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11843       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11844       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11845       break;
11846
11847     case DEBUG_PARAMETER_REF:
11848       mem_loc_result = parameter_ref_descriptor (rtl);
11849       break;
11850
11851     case PRE_MODIFY:
11852       /* Extract the PLUS expression nested inside and fall into
11853          PLUS code below.  */
11854       rtl = XEXP (rtl, 1);
11855       goto plus;
11856
11857     case PRE_INC:
11858     case PRE_DEC:
11859       /* Turn these into a PLUS expression and fall into the PLUS code
11860          below.  */
11861       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11862                           GEN_INT (GET_CODE (rtl) == PRE_INC
11863                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11864                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11865
11866       /* ... fall through ...  */
11867
11868     case PLUS:
11869     plus:
11870       if (is_based_loc (rtl)
11871           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11872           && GET_MODE_CLASS (mode) == MODE_INT)
11873         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11874                                           INTVAL (XEXP (rtl, 1)),
11875                                           VAR_INIT_STATUS_INITIALIZED);
11876       else
11877         {
11878           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11879                                                VAR_INIT_STATUS_INITIALIZED);
11880           if (mem_loc_result == 0)
11881             break;
11882
11883           if (CONST_INT_P (XEXP (rtl, 1))
11884               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11885             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11886           else
11887             {
11888               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11889                                         VAR_INIT_STATUS_INITIALIZED);
11890               if (op1 == 0)
11891                 break;
11892               add_loc_descr (&mem_loc_result, op1);
11893               add_loc_descr (&mem_loc_result,
11894                              new_loc_descr (DW_OP_plus, 0, 0));
11895             }
11896         }
11897       break;
11898
11899     /* If a pseudo-reg is optimized away, it is possible for it to
11900        be replaced with a MEM containing a multiply or shift.  */
11901     case MINUS:
11902       op = DW_OP_minus;
11903       goto do_binop;
11904
11905     case MULT:
11906       op = DW_OP_mul;
11907       goto do_binop;
11908
11909     case DIV:
11910       if (!dwarf_strict
11911           && GET_MODE_CLASS (mode) == MODE_INT
11912           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11913         {
11914           mem_loc_result = typed_binop (DW_OP_div, rtl,
11915                                         base_type_for_mode (mode, 0),
11916                                         mode, mem_mode);
11917           break;
11918         }
11919       op = DW_OP_div;
11920       goto do_binop;
11921
11922     case UMOD:
11923       op = DW_OP_mod;
11924       goto do_binop;
11925
11926     case ASHIFT:
11927       op = DW_OP_shl;
11928       goto do_shift;
11929
11930     case ASHIFTRT:
11931       op = DW_OP_shra;
11932       goto do_shift;
11933
11934     case LSHIFTRT:
11935       op = DW_OP_shr;
11936       goto do_shift;
11937
11938     do_shift:
11939       if (GET_MODE_CLASS (mode) != MODE_INT)
11940         break;
11941       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11942                                 VAR_INIT_STATUS_INITIALIZED);
11943       {
11944         rtx rtlop1 = XEXP (rtl, 1);
11945         if (GET_MODE (rtlop1) != VOIDmode
11946             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11947                < GET_MODE_BITSIZE (mode))
11948           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11949         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11950                                   VAR_INIT_STATUS_INITIALIZED);
11951       }
11952
11953       if (op0 == 0 || op1 == 0)
11954         break;
11955
11956       mem_loc_result = op0;
11957       add_loc_descr (&mem_loc_result, op1);
11958       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11959       break;
11960
11961     case AND:
11962       op = DW_OP_and;
11963       goto do_binop;
11964
11965     case IOR:
11966       op = DW_OP_or;
11967       goto do_binop;
11968
11969     case XOR:
11970       op = DW_OP_xor;
11971       goto do_binop;
11972
11973     do_binop:
11974       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11975                                 VAR_INIT_STATUS_INITIALIZED);
11976       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11977                                 VAR_INIT_STATUS_INITIALIZED);
11978
11979       if (op0 == 0 || op1 == 0)
11980         break;
11981
11982       mem_loc_result = op0;
11983       add_loc_descr (&mem_loc_result, op1);
11984       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11985       break;
11986
11987     case MOD:
11988       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11989         {
11990           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11991                                         base_type_for_mode (mode, 0),
11992                                         mode, mem_mode);
11993           break;
11994         }
11995
11996       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11997                                 VAR_INIT_STATUS_INITIALIZED);
11998       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11999                                 VAR_INIT_STATUS_INITIALIZED);
12000
12001       if (op0 == 0 || op1 == 0)
12002         break;
12003
12004       mem_loc_result = op0;
12005       add_loc_descr (&mem_loc_result, op1);
12006       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12007       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12008       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12009       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12010       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12011       break;
12012
12013     case UDIV:
12014       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12015         {
12016           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12017             {
12018               op = DW_OP_div;
12019               goto do_binop;
12020             }
12021           mem_loc_result = typed_binop (DW_OP_div, rtl,
12022                                         base_type_for_mode (mode, 1),
12023                                         mode, mem_mode);
12024         }
12025       break;
12026
12027     case NOT:
12028       op = DW_OP_not;
12029       goto do_unop;
12030
12031     case ABS:
12032       op = DW_OP_abs;
12033       goto do_unop;
12034
12035     case NEG:
12036       op = DW_OP_neg;
12037       goto do_unop;
12038
12039     do_unop:
12040       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12041                                 VAR_INIT_STATUS_INITIALIZED);
12042
12043       if (op0 == 0)
12044         break;
12045
12046       mem_loc_result = op0;
12047       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12048       break;
12049
12050     case CONST_INT:
12051       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12052 #ifdef POINTERS_EXTEND_UNSIGNED
12053           || (mode == Pmode
12054               && mem_mode != VOIDmode
12055               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12056 #endif
12057           )
12058         {
12059           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12060           break;
12061         }
12062       if (!dwarf_strict
12063           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12064               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12065         {
12066           dw_die_ref type_die = base_type_for_mode (mode, 1);
12067           enum machine_mode amode;
12068           if (type_die == NULL)
12069             return NULL;
12070           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12071                                  MODE_INT, 0);
12072           if (INTVAL (rtl) >= 0
12073               && amode != BLKmode
12074               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12075               /* const DW_OP_GNU_convert <XXX> vs.
12076                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12077               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12078                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12079             {
12080               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12081               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12082               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12083               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12084               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12085               add_loc_descr (&mem_loc_result, op0);
12086               return mem_loc_result;
12087             }
12088           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12089                                           INTVAL (rtl));
12090           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12091           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12092           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12093           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12094             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12095           else
12096             {
12097               mem_loc_result->dw_loc_oprnd2.val_class
12098                 = dw_val_class_const_double;
12099               mem_loc_result->dw_loc_oprnd2.v.val_double
12100                 = shwi_to_double_int (INTVAL (rtl));
12101             }
12102         }
12103       break;
12104
12105     case CONST_DOUBLE:
12106       if (!dwarf_strict)
12107         {
12108           dw_die_ref type_die;
12109
12110           /* Note that a CONST_DOUBLE rtx could represent either an integer
12111              or a floating-point constant.  A CONST_DOUBLE is used whenever
12112              the constant requires more than one word in order to be
12113              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12114           if (mode == VOIDmode
12115               || (GET_MODE (rtl) == VOIDmode
12116                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12117             break;
12118           type_die = base_type_for_mode (mode,
12119                                          GET_MODE_CLASS (mode) == MODE_INT);
12120           if (type_die == NULL)
12121             return NULL;
12122           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12123           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12124           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12125           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12126           if (SCALAR_FLOAT_MODE_P (mode))
12127             {
12128               unsigned int length = GET_MODE_SIZE (mode);
12129               unsigned char *array
12130                   = (unsigned char*) ggc_alloc_atomic (length);
12131
12132               insert_float (rtl, array);
12133               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12134               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12135               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12136               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12137             }
12138           else
12139             {
12140               mem_loc_result->dw_loc_oprnd2.val_class
12141                 = dw_val_class_const_double;
12142               mem_loc_result->dw_loc_oprnd2.v.val_double
12143                 = rtx_to_double_int (rtl);
12144             }
12145         }
12146       break;
12147
12148     case EQ:
12149       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12150       break;
12151
12152     case GE:
12153       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12154       break;
12155
12156     case GT:
12157       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12158       break;
12159
12160     case LE:
12161       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12162       break;
12163
12164     case LT:
12165       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12166       break;
12167
12168     case NE:
12169       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12170       break;
12171
12172     case GEU:
12173       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12174       break;
12175
12176     case GTU:
12177       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12178       break;
12179
12180     case LEU:
12181       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12182       break;
12183
12184     case LTU:
12185       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12186       break;
12187
12188     case UMIN:
12189     case UMAX:
12190       if (GET_MODE_CLASS (mode) != MODE_INT)
12191         break;
12192       /* FALLTHRU */
12193     case SMIN:
12194     case SMAX:
12195       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12196       break;
12197
12198     case ZERO_EXTRACT:
12199     case SIGN_EXTRACT:
12200       if (CONST_INT_P (XEXP (rtl, 1))
12201           && CONST_INT_P (XEXP (rtl, 2))
12202           && ((unsigned) INTVAL (XEXP (rtl, 1))
12203               + (unsigned) INTVAL (XEXP (rtl, 2))
12204               <= GET_MODE_BITSIZE (mode))
12205           && GET_MODE_CLASS (mode) == MODE_INT
12206           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12207           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12208         {
12209           int shift, size;
12210           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12211                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12212           if (op0 == 0)
12213             break;
12214           if (GET_CODE (rtl) == SIGN_EXTRACT)
12215             op = DW_OP_shra;
12216           else
12217             op = DW_OP_shr;
12218           mem_loc_result = op0;
12219           size = INTVAL (XEXP (rtl, 1));
12220           shift = INTVAL (XEXP (rtl, 2));
12221           if (BITS_BIG_ENDIAN)
12222             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12223                     - shift - size;
12224           if (shift + size != (int) DWARF2_ADDR_SIZE)
12225             {
12226               add_loc_descr (&mem_loc_result,
12227                              int_loc_descriptor (DWARF2_ADDR_SIZE
12228                                                  - shift - size));
12229               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12230             }
12231           if (size != (int) DWARF2_ADDR_SIZE)
12232             {
12233               add_loc_descr (&mem_loc_result,
12234                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12235               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12236             }
12237         }
12238       break;
12239
12240     case IF_THEN_ELSE:
12241       {
12242         dw_loc_descr_ref op2, bra_node, drop_node;
12243         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12244                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12245                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12246                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12247         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12248                                   VAR_INIT_STATUS_INITIALIZED);
12249         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12250                                   VAR_INIT_STATUS_INITIALIZED);
12251         if (op0 == NULL || op1 == NULL || op2 == NULL)
12252           break;
12253
12254         mem_loc_result = op1;
12255         add_loc_descr (&mem_loc_result, op2);
12256         add_loc_descr (&mem_loc_result, op0);
12257         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12258         add_loc_descr (&mem_loc_result, bra_node);
12259         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12260         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12261         add_loc_descr (&mem_loc_result, drop_node);
12262         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12263         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12264       }
12265       break;
12266
12267     case FLOAT_EXTEND:
12268     case FLOAT_TRUNCATE:
12269     case FLOAT:
12270     case UNSIGNED_FLOAT:
12271     case FIX:
12272     case UNSIGNED_FIX:
12273       if (!dwarf_strict)
12274         {
12275           dw_die_ref type_die;
12276           dw_loc_descr_ref cvt;
12277
12278           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12279                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12280           if (op0 == NULL)
12281             break;
12282           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12283               && (GET_CODE (rtl) == FLOAT
12284                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12285                      <= DWARF2_ADDR_SIZE))
12286             {
12287               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12288                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12289               if (type_die == NULL)
12290                 break;
12291               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12292               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12293               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12294               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12295               add_loc_descr (&op0, cvt);
12296             }
12297           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12298           if (type_die == NULL)
12299             break;
12300           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12301           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12302           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12303           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12304           add_loc_descr (&op0, cvt);
12305           if (GET_MODE_CLASS (mode) == MODE_INT
12306               && (GET_CODE (rtl) == FIX
12307                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12308             {
12309               op0 = convert_descriptor_to_mode (mode, op0);
12310               if (op0 == NULL)
12311                 break;
12312             }
12313           mem_loc_result = op0;
12314         }
12315       break;
12316
12317     case CLZ:
12318     case CTZ:
12319     case FFS:
12320       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12321       break;
12322
12323     case POPCOUNT:
12324     case PARITY:
12325       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12326       break;
12327
12328     case BSWAP:
12329       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12330       break;
12331
12332     case ROTATE:
12333     case ROTATERT:
12334       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12335       break;
12336
12337     case COMPARE:
12338     case TRUNCATE:
12339       /* In theory, we could implement the above.  */
12340       /* DWARF cannot represent the unsigned compare operations
12341          natively.  */
12342     case SS_MULT:
12343     case US_MULT:
12344     case SS_DIV:
12345     case US_DIV:
12346     case SS_PLUS:
12347     case US_PLUS:
12348     case SS_MINUS:
12349     case US_MINUS:
12350     case SS_NEG:
12351     case US_NEG:
12352     case SS_ABS:
12353     case SS_ASHIFT:
12354     case US_ASHIFT:
12355     case SS_TRUNCATE:
12356     case US_TRUNCATE:
12357     case UNORDERED:
12358     case ORDERED:
12359     case UNEQ:
12360     case UNGE:
12361     case UNGT:
12362     case UNLE:
12363     case UNLT:
12364     case LTGT:
12365     case FRACT_CONVERT:
12366     case UNSIGNED_FRACT_CONVERT:
12367     case SAT_FRACT:
12368     case UNSIGNED_SAT_FRACT:
12369     case SQRT:
12370     case ASM_OPERANDS:
12371     case VEC_MERGE:
12372     case VEC_SELECT:
12373     case VEC_CONCAT:
12374     case VEC_DUPLICATE:
12375     case UNSPEC:
12376     case HIGH:
12377     case FMA:
12378     case STRICT_LOW_PART:
12379     case CONST_VECTOR:
12380     case CONST_FIXED:
12381     case CLRSB:
12382       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12383          can't express it in the debug info.  This can happen e.g. with some
12384          TLS UNSPECs.  */
12385       break;
12386
12387     case CONST_STRING:
12388       resolve_one_addr (&rtl, NULL);
12389       goto symref;
12390
12391     default:
12392 #ifdef ENABLE_CHECKING
12393       print_rtl (stderr, rtl);
12394       gcc_unreachable ();
12395 #else
12396       break;
12397 #endif
12398     }
12399
12400   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12401     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12402
12403   return mem_loc_result;
12404 }
12405
12406 /* Return a descriptor that describes the concatenation of two locations.
12407    This is typically a complex variable.  */
12408
12409 static dw_loc_descr_ref
12410 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12411 {
12412   dw_loc_descr_ref cc_loc_result = NULL;
12413   dw_loc_descr_ref x0_ref
12414     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12415   dw_loc_descr_ref x1_ref
12416     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12417
12418   if (x0_ref == 0 || x1_ref == 0)
12419     return 0;
12420
12421   cc_loc_result = x0_ref;
12422   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12423
12424   add_loc_descr (&cc_loc_result, x1_ref);
12425   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12426
12427   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12428     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12429
12430   return cc_loc_result;
12431 }
12432
12433 /* Return a descriptor that describes the concatenation of N
12434    locations.  */
12435
12436 static dw_loc_descr_ref
12437 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12438 {
12439   unsigned int i;
12440   dw_loc_descr_ref cc_loc_result = NULL;
12441   unsigned int n = XVECLEN (concatn, 0);
12442
12443   for (i = 0; i < n; ++i)
12444     {
12445       dw_loc_descr_ref ref;
12446       rtx x = XVECEXP (concatn, 0, i);
12447
12448       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12449       if (ref == NULL)
12450         return NULL;
12451
12452       add_loc_descr (&cc_loc_result, ref);
12453       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12454     }
12455
12456   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12457     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12458
12459   return cc_loc_result;
12460 }
12461
12462 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12463    for DEBUG_IMPLICIT_PTR RTL.  */
12464
12465 static dw_loc_descr_ref
12466 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12467 {
12468   dw_loc_descr_ref ret;
12469   dw_die_ref ref;
12470
12471   if (dwarf_strict)
12472     return NULL;
12473   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12474               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12475               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12476   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12477   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12478   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12479   if (ref)
12480     {
12481       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12482       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12483       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12484     }
12485   else
12486     {
12487       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12488       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12489     }
12490   return ret;
12491 }
12492
12493 /* Output a proper Dwarf location descriptor for a variable or parameter
12494    which is either allocated in a register or in a memory location.  For a
12495    register, we just generate an OP_REG and the register number.  For a
12496    memory location we provide a Dwarf postfix expression describing how to
12497    generate the (dynamic) address of the object onto the address stack.
12498
12499    MODE is mode of the decl if this loc_descriptor is going to be used in
12500    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12501    allowed, VOIDmode otherwise.
12502
12503    If we don't know how to describe it, return 0.  */
12504
12505 static dw_loc_descr_ref
12506 loc_descriptor (rtx rtl, enum machine_mode mode,
12507                 enum var_init_status initialized)
12508 {
12509   dw_loc_descr_ref loc_result = NULL;
12510
12511   switch (GET_CODE (rtl))
12512     {
12513     case SUBREG:
12514       /* The case of a subreg may arise when we have a local (register)
12515          variable or a formal (register) parameter which doesn't quite fill
12516          up an entire register.  For now, just assume that it is
12517          legitimate to make the Dwarf info refer to the whole register which
12518          contains the given subreg.  */
12519       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12520         loc_result = loc_descriptor (SUBREG_REG (rtl),
12521                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12522       else
12523         goto do_default;
12524       break;
12525
12526     case REG:
12527       loc_result = reg_loc_descriptor (rtl, initialized);
12528       break;
12529
12530     case MEM:
12531       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12532                                        GET_MODE (rtl), initialized);
12533       if (loc_result == NULL)
12534         loc_result = tls_mem_loc_descriptor (rtl);
12535       if (loc_result == NULL)
12536         {
12537           rtx new_rtl = avoid_constant_pool_reference (rtl);
12538           if (new_rtl != rtl)
12539             loc_result = loc_descriptor (new_rtl, mode, initialized);
12540         }
12541       break;
12542
12543     case CONCAT:
12544       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12545                                           initialized);
12546       break;
12547
12548     case CONCATN:
12549       loc_result = concatn_loc_descriptor (rtl, initialized);
12550       break;
12551
12552     case VAR_LOCATION:
12553       /* Single part.  */
12554       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12555         {
12556           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12557           if (GET_CODE (loc) == EXPR_LIST)
12558             loc = XEXP (loc, 0);
12559           loc_result = loc_descriptor (loc, mode, initialized);
12560           break;
12561         }
12562
12563       rtl = XEXP (rtl, 1);
12564       /* FALLTHRU */
12565
12566     case PARALLEL:
12567       {
12568         rtvec par_elems = XVEC (rtl, 0);
12569         int num_elem = GET_NUM_ELEM (par_elems);
12570         enum machine_mode mode;
12571         int i;
12572
12573         /* Create the first one, so we have something to add to.  */
12574         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12575                                      VOIDmode, initialized);
12576         if (loc_result == NULL)
12577           return NULL;
12578         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12579         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12580         for (i = 1; i < num_elem; i++)
12581           {
12582             dw_loc_descr_ref temp;
12583
12584             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12585                                    VOIDmode, initialized);
12586             if (temp == NULL)
12587               return NULL;
12588             add_loc_descr (&loc_result, temp);
12589             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12590             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12591           }
12592       }
12593       break;
12594
12595     case CONST_INT:
12596       if (mode != VOIDmode && mode != BLKmode)
12597         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12598                                                     INTVAL (rtl));
12599       break;
12600
12601     case CONST_DOUBLE:
12602       if (mode == VOIDmode)
12603         mode = GET_MODE (rtl);
12604
12605       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12606         {
12607           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12608
12609           /* Note that a CONST_DOUBLE rtx could represent either an integer
12610              or a floating-point constant.  A CONST_DOUBLE is used whenever
12611              the constant requires more than one word in order to be
12612              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12613           loc_result = new_loc_descr (DW_OP_implicit_value,
12614                                       GET_MODE_SIZE (mode), 0);
12615           if (SCALAR_FLOAT_MODE_P (mode))
12616             {
12617               unsigned int length = GET_MODE_SIZE (mode);
12618               unsigned char *array
12619                   = (unsigned char*) ggc_alloc_atomic (length);
12620
12621               insert_float (rtl, array);
12622               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12623               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12624               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12625               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12626             }
12627           else
12628             {
12629               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12630               loc_result->dw_loc_oprnd2.v.val_double
12631                 = rtx_to_double_int (rtl);
12632             }
12633         }
12634       break;
12635
12636     case CONST_VECTOR:
12637       if (mode == VOIDmode)
12638         mode = GET_MODE (rtl);
12639
12640       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12641         {
12642           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12643           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12644           unsigned char *array = (unsigned char *)
12645             ggc_alloc_atomic (length * elt_size);
12646           unsigned int i;
12647           unsigned char *p;
12648
12649           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12650           switch (GET_MODE_CLASS (mode))
12651             {
12652             case MODE_VECTOR_INT:
12653               for (i = 0, p = array; i < length; i++, p += elt_size)
12654                 {
12655                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12656                   double_int val = rtx_to_double_int (elt);
12657
12658                   if (elt_size <= sizeof (HOST_WIDE_INT))
12659                     insert_int (double_int_to_shwi (val), elt_size, p);
12660                   else
12661                     {
12662                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12663                       insert_double (val, p);
12664                     }
12665                 }
12666               break;
12667
12668             case MODE_VECTOR_FLOAT:
12669               for (i = 0, p = array; i < length; i++, p += elt_size)
12670                 {
12671                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12672                   insert_float (elt, p);
12673                 }
12674               break;
12675
12676             default:
12677               gcc_unreachable ();
12678             }
12679
12680           loc_result = new_loc_descr (DW_OP_implicit_value,
12681                                       length * elt_size, 0);
12682           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12683           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12684           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12685           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12686         }
12687       break;
12688
12689     case CONST:
12690       if (mode == VOIDmode
12691           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12692           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12693           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12694         {
12695           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12696           break;
12697         }
12698       /* FALLTHROUGH */
12699     case SYMBOL_REF:
12700       if (!const_ok_for_output (rtl))
12701         break;
12702     case LABEL_REF:
12703       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12704           && (dwarf_version >= 4 || !dwarf_strict))
12705         {
12706           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12707           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12708           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12709           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12710           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12711         }
12712       break;
12713
12714     case DEBUG_IMPLICIT_PTR:
12715       loc_result = implicit_ptr_descriptor (rtl, 0);
12716       break;
12717
12718     case PLUS:
12719       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12720           && CONST_INT_P (XEXP (rtl, 1)))
12721         {
12722           loc_result
12723             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12724           break;
12725         }
12726       /* FALLTHRU */
12727     do_default:
12728     default:
12729       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12730            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12731            && dwarf_version >= 4)
12732           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12733         {
12734           /* Value expression.  */
12735           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12736           if (loc_result)
12737             add_loc_descr (&loc_result,
12738                            new_loc_descr (DW_OP_stack_value, 0, 0));
12739         }
12740       break;
12741     }
12742
12743   return loc_result;
12744 }
12745
12746 /* We need to figure out what section we should use as the base for the
12747    address ranges where a given location is valid.
12748    1. If this particular DECL has a section associated with it, use that.
12749    2. If this function has a section associated with it, use that.
12750    3. Otherwise, use the text section.
12751    XXX: If you split a variable across multiple sections, we won't notice.  */
12752
12753 static const char *
12754 secname_for_decl (const_tree decl)
12755 {
12756   const char *secname;
12757
12758   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12759     {
12760       tree sectree = DECL_SECTION_NAME (decl);
12761       secname = TREE_STRING_POINTER (sectree);
12762     }
12763   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12764     {
12765       tree sectree = DECL_SECTION_NAME (current_function_decl);
12766       secname = TREE_STRING_POINTER (sectree);
12767     }
12768   else if (cfun && in_cold_section_p)
12769     secname = crtl->subsections.cold_section_label;
12770   else
12771     secname = text_section_label;
12772
12773   return secname;
12774 }
12775
12776 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12777
12778 static bool
12779 decl_by_reference_p (tree decl)
12780 {
12781   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12782            || TREE_CODE (decl) == VAR_DECL)
12783           && DECL_BY_REFERENCE (decl));
12784 }
12785
12786 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12787    for VARLOC.  */
12788
12789 static dw_loc_descr_ref
12790 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12791                enum var_init_status initialized)
12792 {
12793   int have_address = 0;
12794   dw_loc_descr_ref descr;
12795   enum machine_mode mode;
12796
12797   if (want_address != 2)
12798     {
12799       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12800       /* Single part.  */
12801       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12802         {
12803           varloc = PAT_VAR_LOCATION_LOC (varloc);
12804           if (GET_CODE (varloc) == EXPR_LIST)
12805             varloc = XEXP (varloc, 0);
12806           mode = GET_MODE (varloc);
12807           if (MEM_P (varloc))
12808             {
12809               rtx addr = XEXP (varloc, 0);
12810               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12811                                           mode, initialized);
12812               if (descr)
12813                 have_address = 1;
12814               else
12815                 {
12816                   rtx x = avoid_constant_pool_reference (varloc);
12817                   if (x != varloc)
12818                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12819                                                 initialized);
12820                 }
12821             }
12822           else
12823             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12824         }
12825       else
12826         return 0;
12827     }
12828   else
12829     {
12830       if (GET_CODE (varloc) == VAR_LOCATION)
12831         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12832       else
12833         mode = DECL_MODE (loc);
12834       descr = loc_descriptor (varloc, mode, initialized);
12835       have_address = 1;
12836     }
12837
12838   if (!descr)
12839     return 0;
12840
12841   if (want_address == 2 && !have_address
12842       && (dwarf_version >= 4 || !dwarf_strict))
12843     {
12844       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12845         {
12846           expansion_failed (loc, NULL_RTX,
12847                             "DWARF address size mismatch");
12848           return 0;
12849         }
12850       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12851       have_address = 1;
12852     }
12853   /* Show if we can't fill the request for an address.  */
12854   if (want_address && !have_address)
12855     {
12856       expansion_failed (loc, NULL_RTX,
12857                         "Want address and only have value");
12858       return 0;
12859     }
12860
12861   /* If we've got an address and don't want one, dereference.  */
12862   if (!want_address && have_address)
12863     {
12864       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12865       enum dwarf_location_atom op;
12866
12867       if (size > DWARF2_ADDR_SIZE || size == -1)
12868         {
12869           expansion_failed (loc, NULL_RTX,
12870                             "DWARF address size mismatch");
12871           return 0;
12872         }
12873       else if (size == DWARF2_ADDR_SIZE)
12874         op = DW_OP_deref;
12875       else
12876         op = DW_OP_deref_size;
12877
12878       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12879     }
12880
12881   return descr;
12882 }
12883
12884 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12885    if it is not possible.  */
12886
12887 static dw_loc_descr_ref
12888 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12889 {
12890   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12891     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12892   else if (dwarf_version >= 3 || !dwarf_strict)
12893     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12894   else
12895     return NULL;
12896 }
12897
12898 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12899    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12900
12901 static dw_loc_descr_ref
12902 dw_sra_loc_expr (tree decl, rtx loc)
12903 {
12904   rtx p;
12905   unsigned int padsize = 0;
12906   dw_loc_descr_ref descr, *descr_tail;
12907   unsigned HOST_WIDE_INT decl_size;
12908   rtx varloc;
12909   enum var_init_status initialized;
12910
12911   if (DECL_SIZE (decl) == NULL
12912       || !host_integerp (DECL_SIZE (decl), 1))
12913     return NULL;
12914
12915   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12916   descr = NULL;
12917   descr_tail = &descr;
12918
12919   for (p = loc; p; p = XEXP (p, 1))
12920     {
12921       unsigned int bitsize = decl_piece_bitsize (p);
12922       rtx loc_note = *decl_piece_varloc_ptr (p);
12923       dw_loc_descr_ref cur_descr;
12924       dw_loc_descr_ref *tail, last = NULL;
12925       unsigned int opsize = 0;
12926
12927       if (loc_note == NULL_RTX
12928           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12929         {
12930           padsize += bitsize;
12931           continue;
12932         }
12933       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12934       varloc = NOTE_VAR_LOCATION (loc_note);
12935       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12936       if (cur_descr == NULL)
12937         {
12938           padsize += bitsize;
12939           continue;
12940         }
12941
12942       /* Check that cur_descr either doesn't use
12943          DW_OP_*piece operations, or their sum is equal
12944          to bitsize.  Otherwise we can't embed it.  */
12945       for (tail = &cur_descr; *tail != NULL;
12946            tail = &(*tail)->dw_loc_next)
12947         if ((*tail)->dw_loc_opc == DW_OP_piece)
12948           {
12949             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12950                       * BITS_PER_UNIT;
12951             last = *tail;
12952           }
12953         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12954           {
12955             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12956             last = *tail;
12957           }
12958
12959       if (last != NULL && opsize != bitsize)
12960         {
12961           padsize += bitsize;
12962           continue;
12963         }
12964
12965       /* If there is a hole, add DW_OP_*piece after empty DWARF
12966          expression, which means that those bits are optimized out.  */
12967       if (padsize)
12968         {
12969           if (padsize > decl_size)
12970             return NULL;
12971           decl_size -= padsize;
12972           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12973           if (*descr_tail == NULL)
12974             return NULL;
12975           descr_tail = &(*descr_tail)->dw_loc_next;
12976           padsize = 0;
12977         }
12978       *descr_tail = cur_descr;
12979       descr_tail = tail;
12980       if (bitsize > decl_size)
12981         return NULL;
12982       decl_size -= bitsize;
12983       if (last == NULL)
12984         {
12985           HOST_WIDE_INT offset = 0;
12986           if (GET_CODE (varloc) == VAR_LOCATION
12987               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12988             {
12989               varloc = PAT_VAR_LOCATION_LOC (varloc);
12990               if (GET_CODE (varloc) == EXPR_LIST)
12991                 varloc = XEXP (varloc, 0);
12992             }
12993           do 
12994             {
12995               if (GET_CODE (varloc) == CONST
12996                   || GET_CODE (varloc) == SIGN_EXTEND
12997                   || GET_CODE (varloc) == ZERO_EXTEND)
12998                 varloc = XEXP (varloc, 0);
12999               else if (GET_CODE (varloc) == SUBREG)
13000                 varloc = SUBREG_REG (varloc);
13001               else
13002                 break;
13003             }
13004           while (1);
13005           /* DW_OP_bit_size offset should be zero for register
13006              or implicit location descriptions and empty location
13007              descriptions, but for memory addresses needs big endian
13008              adjustment.  */
13009           if (MEM_P (varloc))
13010             {
13011               unsigned HOST_WIDE_INT memsize
13012                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13013               if (memsize != bitsize)
13014                 {
13015                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13016                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13017                     return NULL;
13018                   if (memsize < bitsize)
13019                     return NULL;
13020                   if (BITS_BIG_ENDIAN)
13021                     offset = memsize - bitsize;
13022                 }
13023             }
13024
13025           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13026           if (*descr_tail == NULL)
13027             return NULL;
13028           descr_tail = &(*descr_tail)->dw_loc_next;
13029         }
13030     }
13031
13032   /* If there were any non-empty expressions, add padding till the end of
13033      the decl.  */
13034   if (descr != NULL && decl_size != 0)
13035     {
13036       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13037       if (*descr_tail == NULL)
13038         return NULL;
13039     }
13040   return descr;
13041 }
13042
13043 /* Return the dwarf representation of the location list LOC_LIST of
13044    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13045    function.  */
13046
13047 static dw_loc_list_ref
13048 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13049 {
13050   const char *endname, *secname;
13051   rtx varloc;
13052   enum var_init_status initialized;
13053   struct var_loc_node *node;
13054   dw_loc_descr_ref descr;
13055   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13056   dw_loc_list_ref list = NULL;
13057   dw_loc_list_ref *listp = &list;
13058
13059   /* Now that we know what section we are using for a base,
13060      actually construct the list of locations.
13061      The first location information is what is passed to the
13062      function that creates the location list, and the remaining
13063      locations just get added on to that list.
13064      Note that we only know the start address for a location
13065      (IE location changes), so to build the range, we use
13066      the range [current location start, next location start].
13067      This means we have to special case the last node, and generate
13068      a range of [last location start, end of function label].  */
13069
13070   secname = secname_for_decl (decl);
13071
13072   for (node = loc_list->first; node; node = node->next)
13073     if (GET_CODE (node->loc) == EXPR_LIST
13074         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13075       {
13076         if (GET_CODE (node->loc) == EXPR_LIST)
13077           {
13078             /* This requires DW_OP_{,bit_}piece, which is not usable
13079                inside DWARF expressions.  */
13080             if (want_address != 2)
13081               continue;
13082             descr = dw_sra_loc_expr (decl, node->loc);
13083             if (descr == NULL)
13084               continue;
13085           }
13086         else
13087           {
13088             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13089             varloc = NOTE_VAR_LOCATION (node->loc);
13090             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13091           }
13092         if (descr)
13093           {
13094             bool range_across_switch = false;
13095             /* If section switch happens in between node->label
13096                and node->next->label (or end of function) and
13097                we can't emit it as a single entry list,
13098                emit two ranges, first one ending at the end
13099                of first partition and second one starting at the
13100                beginning of second partition.  */
13101             if (node == loc_list->last_before_switch
13102                 && (node != loc_list->first || loc_list->first->next)
13103                 && current_function_decl)
13104               {
13105                 endname = cfun->fde->dw_fde_end;
13106                 range_across_switch = true;
13107               }
13108             /* The variable has a location between NODE->LABEL and
13109                NODE->NEXT->LABEL.  */
13110             else if (node->next)
13111               endname = node->next->label;
13112             /* If the variable has a location at the last label
13113                it keeps its location until the end of function.  */
13114             else if (!current_function_decl)
13115               endname = text_end_label;
13116             else
13117               {
13118                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13119                                              current_function_funcdef_no);
13120                 endname = ggc_strdup (label_id);
13121               }
13122
13123             *listp = new_loc_list (descr, node->label, endname, secname);
13124             if (TREE_CODE (decl) == PARM_DECL
13125                 && node == loc_list->first
13126                 && GET_CODE (node->loc) == NOTE
13127                 && strcmp (node->label, endname) == 0)
13128               (*listp)->force = true;
13129             listp = &(*listp)->dw_loc_next;
13130
13131             if (range_across_switch)
13132               {
13133                 if (GET_CODE (node->loc) == EXPR_LIST)
13134                   descr = dw_sra_loc_expr (decl, node->loc);
13135                 else
13136                   {
13137                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13138                     varloc = NOTE_VAR_LOCATION (node->loc);
13139                     descr = dw_loc_list_1 (decl, varloc, want_address,
13140                                            initialized);
13141                   }
13142                 gcc_assert (descr);
13143                 /* The variable has a location between NODE->LABEL and
13144                    NODE->NEXT->LABEL.  */
13145                 if (node->next)
13146                   endname = node->next->label;
13147                 else
13148                   endname = cfun->fde->dw_fde_second_end;
13149                 *listp = new_loc_list (descr,
13150                                        cfun->fde->dw_fde_second_begin,
13151                                        endname, secname);
13152                 listp = &(*listp)->dw_loc_next;
13153               }
13154           }
13155       }
13156
13157   /* Try to avoid the overhead of a location list emitting a location
13158      expression instead, but only if we didn't have more than one
13159      location entry in the first place.  If some entries were not
13160      representable, we don't want to pretend a single entry that was
13161      applies to the entire scope in which the variable is
13162      available.  */
13163   if (list && loc_list->first->next)
13164     gen_llsym (list);
13165
13166   return list;
13167 }
13168
13169 /* Return if the loc_list has only single element and thus can be represented
13170    as location description.   */
13171
13172 static bool
13173 single_element_loc_list_p (dw_loc_list_ref list)
13174 {
13175   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13176   return !list->ll_symbol;
13177 }
13178
13179 /* To each location in list LIST add loc descr REF.  */
13180
13181 static void
13182 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13183 {
13184   dw_loc_descr_ref copy;
13185   add_loc_descr (&list->expr, ref);
13186   list = list->dw_loc_next;
13187   while (list)
13188     {
13189       copy = ggc_alloc_dw_loc_descr_node ();
13190       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13191       add_loc_descr (&list->expr, copy);
13192       while (copy->dw_loc_next)
13193         {
13194           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13195           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13196           copy->dw_loc_next = new_copy;
13197           copy = new_copy;
13198         }
13199       list = list->dw_loc_next;
13200     }
13201 }
13202
13203 /* Given two lists RET and LIST
13204    produce location list that is result of adding expression in LIST
13205    to expression in RET on each possition in program.
13206    Might be destructive on both RET and LIST.
13207
13208    TODO: We handle only simple cases of RET or LIST having at most one
13209    element. General case would inolve sorting the lists in program order
13210    and merging them that will need some additional work.
13211    Adding that will improve quality of debug info especially for SRA-ed
13212    structures.  */
13213
13214 static void
13215 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13216 {
13217   if (!list)
13218     return;
13219   if (!*ret)
13220     {
13221       *ret = list;
13222       return;
13223     }
13224   if (!list->dw_loc_next)
13225     {
13226       add_loc_descr_to_each (*ret, list->expr);
13227       return;
13228     }
13229   if (!(*ret)->dw_loc_next)
13230     {
13231       add_loc_descr_to_each (list, (*ret)->expr);
13232       *ret = list;
13233       return;
13234     }
13235   expansion_failed (NULL_TREE, NULL_RTX,
13236                     "Don't know how to merge two non-trivial"
13237                     " location lists.\n");
13238   *ret = NULL;
13239   return;
13240 }
13241
13242 /* LOC is constant expression.  Try a luck, look it up in constant
13243    pool and return its loc_descr of its address.  */
13244
13245 static dw_loc_descr_ref
13246 cst_pool_loc_descr (tree loc)
13247 {
13248   /* Get an RTL for this, if something has been emitted.  */
13249   rtx rtl = lookup_constant_def (loc);
13250
13251   if (!rtl || !MEM_P (rtl))
13252     {
13253       gcc_assert (!rtl);
13254       return 0;
13255     }
13256   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13257
13258   /* TODO: We might get more coverage if we was actually delaying expansion
13259      of all expressions till end of compilation when constant pools are fully
13260      populated.  */
13261   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13262     {
13263       expansion_failed (loc, NULL_RTX,
13264                         "CST value in contant pool but not marked.");
13265       return 0;
13266     }
13267   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13268                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13269 }
13270
13271 /* Return dw_loc_list representing address of addr_expr LOC
13272    by looking for innder INDIRECT_REF expression and turing it
13273    into simple arithmetics.  */
13274
13275 static dw_loc_list_ref
13276 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13277 {
13278   tree obj, offset;
13279   HOST_WIDE_INT bitsize, bitpos, bytepos;
13280   enum machine_mode mode;
13281   int volatilep;
13282   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13283   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13284
13285   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13286                              &bitsize, &bitpos, &offset, &mode,
13287                              &unsignedp, &volatilep, false);
13288   STRIP_NOPS (obj);
13289   if (bitpos % BITS_PER_UNIT)
13290     {
13291       expansion_failed (loc, NULL_RTX, "bitfield access");
13292       return 0;
13293     }
13294   if (!INDIRECT_REF_P (obj))
13295     {
13296       expansion_failed (obj,
13297                         NULL_RTX, "no indirect ref in inner refrence");
13298       return 0;
13299     }
13300   if (!offset && !bitpos)
13301     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13302   else if (toplev
13303            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13304            && (dwarf_version >= 4 || !dwarf_strict))
13305     {
13306       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13307       if (!list_ret)
13308         return 0;
13309       if (offset)
13310         {
13311           /* Variable offset.  */
13312           list_ret1 = loc_list_from_tree (offset, 0);
13313           if (list_ret1 == 0)
13314             return 0;
13315           add_loc_list (&list_ret, list_ret1);
13316           if (!list_ret)
13317             return 0;
13318           add_loc_descr_to_each (list_ret,
13319                                  new_loc_descr (DW_OP_plus, 0, 0));
13320         }
13321       bytepos = bitpos / BITS_PER_UNIT;
13322       if (bytepos > 0)
13323         add_loc_descr_to_each (list_ret,
13324                                new_loc_descr (DW_OP_plus_uconst,
13325                                               bytepos, 0));
13326       else if (bytepos < 0)
13327         loc_list_plus_const (list_ret, bytepos);
13328       add_loc_descr_to_each (list_ret,
13329                              new_loc_descr (DW_OP_stack_value, 0, 0));
13330     }
13331   return list_ret;
13332 }
13333
13334
13335 /* Generate Dwarf location list representing LOC.
13336    If WANT_ADDRESS is false, expression computing LOC will be computed
13337    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13338    if WANT_ADDRESS is 2, expression computing address useable in location
13339      will be returned (i.e. DW_OP_reg can be used
13340      to refer to register values).  */
13341
13342 static dw_loc_list_ref
13343 loc_list_from_tree (tree loc, int want_address)
13344 {
13345   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13346   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13347   int have_address = 0;
13348   enum dwarf_location_atom op;
13349
13350   /* ??? Most of the time we do not take proper care for sign/zero
13351      extending the values properly.  Hopefully this won't be a real
13352      problem...  */
13353
13354   switch (TREE_CODE (loc))
13355     {
13356     case ERROR_MARK:
13357       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13358       return 0;
13359
13360     case PLACEHOLDER_EXPR:
13361       /* This case involves extracting fields from an object to determine the
13362          position of other fields.  We don't try to encode this here.  The
13363          only user of this is Ada, which encodes the needed information using
13364          the names of types.  */
13365       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13366       return 0;
13367
13368     case CALL_EXPR:
13369       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13370       /* There are no opcodes for these operations.  */
13371       return 0;
13372
13373     case PREINCREMENT_EXPR:
13374     case PREDECREMENT_EXPR:
13375     case POSTINCREMENT_EXPR:
13376     case POSTDECREMENT_EXPR:
13377       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13378       /* There are no opcodes for these operations.  */
13379       return 0;
13380
13381     case ADDR_EXPR:
13382       /* If we already want an address, see if there is INDIRECT_REF inside
13383          e.g. for &this->field.  */
13384       if (want_address)
13385         {
13386           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13387                        (loc, want_address == 2);
13388           if (list_ret)
13389             have_address = 1;
13390           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13391                    && (ret = cst_pool_loc_descr (loc)))
13392             have_address = 1;
13393         }
13394         /* Otherwise, process the argument and look for the address.  */
13395       if (!list_ret && !ret)
13396         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13397       else
13398         {
13399           if (want_address)
13400             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13401           return NULL;
13402         }
13403       break;
13404
13405     case VAR_DECL:
13406       if (DECL_THREAD_LOCAL_P (loc))
13407         {
13408           rtx rtl;
13409           enum dwarf_location_atom first_op;
13410           enum dwarf_location_atom second_op;
13411           bool dtprel = false;
13412
13413           if (targetm.have_tls)
13414             {
13415               /* If this is not defined, we have no way to emit the
13416                  data.  */
13417               if (!targetm.asm_out.output_dwarf_dtprel)
13418                 return 0;
13419
13420                /* The way DW_OP_GNU_push_tls_address is specified, we
13421                   can only look up addresses of objects in the current
13422                   module.  We used DW_OP_addr as first op, but that's
13423                   wrong, because DW_OP_addr is relocated by the debug
13424                   info consumer, while DW_OP_GNU_push_tls_address
13425                   operand shouldn't be.  */
13426               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13427                 return 0;
13428               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13429               dtprel = true;
13430               second_op = DW_OP_GNU_push_tls_address;
13431             }
13432           else
13433             {
13434               if (!targetm.emutls.debug_form_tls_address
13435                   || !(dwarf_version >= 3 || !dwarf_strict))
13436                 return 0;
13437               /* We stuffed the control variable into the DECL_VALUE_EXPR
13438                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13439                  no longer appear in gimple code.  We used the control
13440                  variable in specific so that we could pick it up here.  */
13441               loc = DECL_VALUE_EXPR (loc);
13442               first_op = DW_OP_addr;
13443               second_op = DW_OP_form_tls_address;
13444             }
13445
13446           rtl = rtl_for_decl_location (loc);
13447           if (rtl == NULL_RTX)
13448             return 0;
13449
13450           if (!MEM_P (rtl))
13451             return 0;
13452           rtl = XEXP (rtl, 0);
13453           if (! CONSTANT_P (rtl))
13454             return 0;
13455
13456           ret = new_loc_descr (first_op, 0, 0);
13457           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13458           ret->dw_loc_oprnd1.v.val_addr = rtl;
13459           ret->dtprel = dtprel;
13460
13461           ret1 = new_loc_descr (second_op, 0, 0);
13462           add_loc_descr (&ret, ret1);
13463
13464           have_address = 1;
13465           break;
13466         }
13467       /* FALLTHRU */
13468
13469     case PARM_DECL:
13470     case RESULT_DECL:
13471       if (DECL_HAS_VALUE_EXPR_P (loc))
13472         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13473                                    want_address);
13474       /* FALLTHRU */
13475
13476     case FUNCTION_DECL:
13477       {
13478         rtx rtl;
13479         var_loc_list *loc_list = lookup_decl_loc (loc);
13480
13481         if (loc_list && loc_list->first)
13482           {
13483             list_ret = dw_loc_list (loc_list, loc, want_address);
13484             have_address = want_address != 0;
13485             break;
13486           }
13487         rtl = rtl_for_decl_location (loc);
13488         if (rtl == NULL_RTX)
13489           {
13490             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13491             return 0;
13492           }
13493         else if (CONST_INT_P (rtl))
13494           {
13495             HOST_WIDE_INT val = INTVAL (rtl);
13496             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13497               val &= GET_MODE_MASK (DECL_MODE (loc));
13498             ret = int_loc_descriptor (val);
13499           }
13500         else if (GET_CODE (rtl) == CONST_STRING)
13501           {
13502             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13503             return 0;
13504           }
13505         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13506           {
13507             ret = new_loc_descr (DW_OP_addr, 0, 0);
13508             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13509             ret->dw_loc_oprnd1.v.val_addr = rtl;
13510           }
13511         else
13512           {
13513             enum machine_mode mode, mem_mode;
13514
13515             /* Certain constructs can only be represented at top-level.  */
13516             if (want_address == 2)
13517               {
13518                 ret = loc_descriptor (rtl, VOIDmode,
13519                                       VAR_INIT_STATUS_INITIALIZED);
13520                 have_address = 1;
13521               }
13522             else
13523               {
13524                 mode = GET_MODE (rtl);
13525                 mem_mode = VOIDmode;
13526                 if (MEM_P (rtl))
13527                   {
13528                     mem_mode = mode;
13529                     mode = get_address_mode (rtl);
13530                     rtl = XEXP (rtl, 0);
13531                     have_address = 1;
13532                   }
13533                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13534                                           VAR_INIT_STATUS_INITIALIZED);
13535               }
13536             if (!ret)
13537               expansion_failed (loc, rtl,
13538                                 "failed to produce loc descriptor for rtl");
13539           }
13540       }
13541       break;
13542
13543     case MEM_REF:
13544       /* ??? FIXME.  */
13545       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13546         return 0;
13547       /* Fallthru.  */
13548     case INDIRECT_REF:
13549       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13550       have_address = 1;
13551       break;
13552
13553     case COMPOUND_EXPR:
13554       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13555
13556     CASE_CONVERT:
13557     case VIEW_CONVERT_EXPR:
13558     case SAVE_EXPR:
13559     case MODIFY_EXPR:
13560       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13561
13562     case COMPONENT_REF:
13563     case BIT_FIELD_REF:
13564     case ARRAY_REF:
13565     case ARRAY_RANGE_REF:
13566     case REALPART_EXPR:
13567     case IMAGPART_EXPR:
13568       {
13569         tree obj, offset;
13570         HOST_WIDE_INT bitsize, bitpos, bytepos;
13571         enum machine_mode mode;
13572         int volatilep;
13573         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13574
13575         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13576                                    &unsignedp, &volatilep, false);
13577
13578         gcc_assert (obj != loc);
13579
13580         list_ret = loc_list_from_tree (obj,
13581                                        want_address == 2
13582                                        && !bitpos && !offset ? 2 : 1);
13583         /* TODO: We can extract value of the small expression via shifting even
13584            for nonzero bitpos.  */
13585         if (list_ret == 0)
13586           return 0;
13587         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13588           {
13589             expansion_failed (loc, NULL_RTX,
13590                               "bitfield access");
13591             return 0;
13592           }
13593
13594         if (offset != NULL_TREE)
13595           {
13596             /* Variable offset.  */
13597             list_ret1 = loc_list_from_tree (offset, 0);
13598             if (list_ret1 == 0)
13599               return 0;
13600             add_loc_list (&list_ret, list_ret1);
13601             if (!list_ret)
13602               return 0;
13603             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13604           }
13605
13606         bytepos = bitpos / BITS_PER_UNIT;
13607         if (bytepos > 0)
13608           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13609         else if (bytepos < 0)
13610           loc_list_plus_const (list_ret, bytepos);
13611
13612         have_address = 1;
13613         break;
13614       }
13615
13616     case INTEGER_CST:
13617       if ((want_address || !host_integerp (loc, 0))
13618           && (ret = cst_pool_loc_descr (loc)))
13619         have_address = 1;
13620       else if (want_address == 2
13621                && host_integerp (loc, 0)
13622                && (ret = address_of_int_loc_descriptor
13623                            (int_size_in_bytes (TREE_TYPE (loc)),
13624                             tree_low_cst (loc, 0))))
13625         have_address = 1;
13626       else if (host_integerp (loc, 0))
13627         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13628       else
13629         {
13630           expansion_failed (loc, NULL_RTX,
13631                             "Integer operand is not host integer");
13632           return 0;
13633         }
13634       break;
13635
13636     case CONSTRUCTOR:
13637     case REAL_CST:
13638     case STRING_CST:
13639     case COMPLEX_CST:
13640       if ((ret = cst_pool_loc_descr (loc)))
13641         have_address = 1;
13642       else
13643       /* We can construct small constants here using int_loc_descriptor.  */
13644         expansion_failed (loc, NULL_RTX,
13645                           "constructor or constant not in constant pool");
13646       break;
13647
13648     case TRUTH_AND_EXPR:
13649     case TRUTH_ANDIF_EXPR:
13650     case BIT_AND_EXPR:
13651       op = DW_OP_and;
13652       goto do_binop;
13653
13654     case TRUTH_XOR_EXPR:
13655     case BIT_XOR_EXPR:
13656       op = DW_OP_xor;
13657       goto do_binop;
13658
13659     case TRUTH_OR_EXPR:
13660     case TRUTH_ORIF_EXPR:
13661     case BIT_IOR_EXPR:
13662       op = DW_OP_or;
13663       goto do_binop;
13664
13665     case FLOOR_DIV_EXPR:
13666     case CEIL_DIV_EXPR:
13667     case ROUND_DIV_EXPR:
13668     case TRUNC_DIV_EXPR:
13669       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13670         return 0;
13671       op = DW_OP_div;
13672       goto do_binop;
13673
13674     case MINUS_EXPR:
13675       op = DW_OP_minus;
13676       goto do_binop;
13677
13678     case FLOOR_MOD_EXPR:
13679     case CEIL_MOD_EXPR:
13680     case ROUND_MOD_EXPR:
13681     case TRUNC_MOD_EXPR:
13682       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13683         {
13684           op = DW_OP_mod;
13685           goto do_binop;
13686         }
13687       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13688       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13689       if (list_ret == 0 || list_ret1 == 0)
13690         return 0;
13691
13692       add_loc_list (&list_ret, list_ret1);
13693       if (list_ret == 0)
13694         return 0;
13695       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13696       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13697       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13698       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13699       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13700       break;
13701
13702     case MULT_EXPR:
13703       op = DW_OP_mul;
13704       goto do_binop;
13705
13706     case LSHIFT_EXPR:
13707       op = DW_OP_shl;
13708       goto do_binop;
13709
13710     case RSHIFT_EXPR:
13711       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13712       goto do_binop;
13713
13714     case POINTER_PLUS_EXPR:
13715     case PLUS_EXPR:
13716       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13717         {
13718           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13719           if (list_ret == 0)
13720             return 0;
13721
13722           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13723           break;
13724         }
13725
13726       op = DW_OP_plus;
13727       goto do_binop;
13728
13729     case LE_EXPR:
13730       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13731         return 0;
13732
13733       op = DW_OP_le;
13734       goto do_binop;
13735
13736     case GE_EXPR:
13737       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13738         return 0;
13739
13740       op = DW_OP_ge;
13741       goto do_binop;
13742
13743     case LT_EXPR:
13744       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13745         return 0;
13746
13747       op = DW_OP_lt;
13748       goto do_binop;
13749
13750     case GT_EXPR:
13751       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13752         return 0;
13753
13754       op = DW_OP_gt;
13755       goto do_binop;
13756
13757     case EQ_EXPR:
13758       op = DW_OP_eq;
13759       goto do_binop;
13760
13761     case NE_EXPR:
13762       op = DW_OP_ne;
13763       goto do_binop;
13764
13765     do_binop:
13766       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13767       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13768       if (list_ret == 0 || list_ret1 == 0)
13769         return 0;
13770
13771       add_loc_list (&list_ret, list_ret1);
13772       if (list_ret == 0)
13773         return 0;
13774       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13775       break;
13776
13777     case TRUTH_NOT_EXPR:
13778     case BIT_NOT_EXPR:
13779       op = DW_OP_not;
13780       goto do_unop;
13781
13782     case ABS_EXPR:
13783       op = DW_OP_abs;
13784       goto do_unop;
13785
13786     case NEGATE_EXPR:
13787       op = DW_OP_neg;
13788       goto do_unop;
13789
13790     do_unop:
13791       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13792       if (list_ret == 0)
13793         return 0;
13794
13795       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13796       break;
13797
13798     case MIN_EXPR:
13799     case MAX_EXPR:
13800       {
13801         const enum tree_code code =
13802           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13803
13804         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13805                       build2 (code, integer_type_node,
13806                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13807                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13808       }
13809
13810       /* ... fall through ...  */
13811
13812     case COND_EXPR:
13813       {
13814         dw_loc_descr_ref lhs
13815           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13816         dw_loc_list_ref rhs
13817           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13818         dw_loc_descr_ref bra_node, jump_node, tmp;
13819
13820         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13821         if (list_ret == 0 || lhs == 0 || rhs == 0)
13822           return 0;
13823
13824         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13825         add_loc_descr_to_each (list_ret, bra_node);
13826
13827         add_loc_list (&list_ret, rhs);
13828         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13829         add_loc_descr_to_each (list_ret, jump_node);
13830
13831         add_loc_descr_to_each (list_ret, lhs);
13832         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13833         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13834
13835         /* ??? Need a node to point the skip at.  Use a nop.  */
13836         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13837         add_loc_descr_to_each (list_ret, tmp);
13838         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13839         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13840       }
13841       break;
13842
13843     case FIX_TRUNC_EXPR:
13844       return 0;
13845
13846     default:
13847       /* Leave front-end specific codes as simply unknown.  This comes
13848          up, for instance, with the C STMT_EXPR.  */
13849       if ((unsigned int) TREE_CODE (loc)
13850           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13851         {
13852           expansion_failed (loc, NULL_RTX,
13853                             "language specific tree node");
13854           return 0;
13855         }
13856
13857 #ifdef ENABLE_CHECKING
13858       /* Otherwise this is a generic code; we should just lists all of
13859          these explicitly.  We forgot one.  */
13860       gcc_unreachable ();
13861 #else
13862       /* In a release build, we want to degrade gracefully: better to
13863          generate incomplete debugging information than to crash.  */
13864       return NULL;
13865 #endif
13866     }
13867
13868   if (!ret && !list_ret)
13869     return 0;
13870
13871   if (want_address == 2 && !have_address
13872       && (dwarf_version >= 4 || !dwarf_strict))
13873     {
13874       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13875         {
13876           expansion_failed (loc, NULL_RTX,
13877                             "DWARF address size mismatch");
13878           return 0;
13879         }
13880       if (ret)
13881         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13882       else
13883         add_loc_descr_to_each (list_ret,
13884                                new_loc_descr (DW_OP_stack_value, 0, 0));
13885       have_address = 1;
13886     }
13887   /* Show if we can't fill the request for an address.  */
13888   if (want_address && !have_address)
13889     {
13890       expansion_failed (loc, NULL_RTX,
13891                         "Want address and only have value");
13892       return 0;
13893     }
13894
13895   gcc_assert (!ret || !list_ret);
13896
13897   /* If we've got an address and don't want one, dereference.  */
13898   if (!want_address && have_address)
13899     {
13900       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13901
13902       if (size > DWARF2_ADDR_SIZE || size == -1)
13903         {
13904           expansion_failed (loc, NULL_RTX,
13905                             "DWARF address size mismatch");
13906           return 0;
13907         }
13908       else if (size == DWARF2_ADDR_SIZE)
13909         op = DW_OP_deref;
13910       else
13911         op = DW_OP_deref_size;
13912
13913       if (ret)
13914         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13915       else
13916         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13917     }
13918   if (ret)
13919     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13920
13921   return list_ret;
13922 }
13923
13924 /* Same as above but return only single location expression.  */
13925 static dw_loc_descr_ref
13926 loc_descriptor_from_tree (tree loc, int want_address)
13927 {
13928   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13929   if (!ret)
13930     return NULL;
13931   if (ret->dw_loc_next)
13932     {
13933       expansion_failed (loc, NULL_RTX,
13934                         "Location list where only loc descriptor needed");
13935       return NULL;
13936     }
13937   return ret->expr;
13938 }
13939
13940 /* Given a value, round it up to the lowest multiple of `boundary'
13941    which is not less than the value itself.  */
13942
13943 static inline HOST_WIDE_INT
13944 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13945 {
13946   return (((value + boundary - 1) / boundary) * boundary);
13947 }
13948
13949 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13950    pointer to the declared type for the relevant field variable, or return
13951    `integer_type_node' if the given node turns out to be an
13952    ERROR_MARK node.  */
13953
13954 static inline tree
13955 field_type (const_tree decl)
13956 {
13957   tree type;
13958
13959   if (TREE_CODE (decl) == ERROR_MARK)
13960     return integer_type_node;
13961
13962   type = DECL_BIT_FIELD_TYPE (decl);
13963   if (type == NULL_TREE)
13964     type = TREE_TYPE (decl);
13965
13966   return type;
13967 }
13968
13969 /* Given a pointer to a tree node, return the alignment in bits for
13970    it, or else return BITS_PER_WORD if the node actually turns out to
13971    be an ERROR_MARK node.  */
13972
13973 static inline unsigned
13974 simple_type_align_in_bits (const_tree type)
13975 {
13976   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13977 }
13978
13979 static inline unsigned
13980 simple_decl_align_in_bits (const_tree decl)
13981 {
13982   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13983 }
13984
13985 /* Return the result of rounding T up to ALIGN.  */
13986
13987 static inline double_int
13988 round_up_to_align (double_int t, unsigned int align)
13989 {
13990   double_int alignd = uhwi_to_double_int (align);
13991   t = double_int_add (t, alignd);
13992   t = double_int_add (t, double_int_minus_one);
13993   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13994   t = double_int_mul (t, alignd);
13995   return t;
13996 }
13997
13998 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13999    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14000    or return 0 if we are unable to determine what that offset is, either
14001    because the argument turns out to be a pointer to an ERROR_MARK node, or
14002    because the offset is actually variable.  (We can't handle the latter case
14003    just yet).  */
14004
14005 static HOST_WIDE_INT
14006 field_byte_offset (const_tree decl)
14007 {
14008   double_int object_offset_in_bits;
14009   double_int object_offset_in_bytes;
14010   double_int bitpos_int;
14011
14012   if (TREE_CODE (decl) == ERROR_MARK)
14013     return 0;
14014
14015   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14016
14017   /* We cannot yet cope with fields whose positions are variable, so
14018      for now, when we see such things, we simply return 0.  Someday, we may
14019      be able to handle such cases, but it will be damn difficult.  */
14020   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14021     return 0;
14022
14023   bitpos_int = tree_to_double_int (bit_position (decl));
14024
14025 #ifdef PCC_BITFIELD_TYPE_MATTERS
14026   if (PCC_BITFIELD_TYPE_MATTERS)
14027     {
14028       tree type;
14029       tree field_size_tree;
14030       double_int deepest_bitpos;
14031       double_int field_size_in_bits;
14032       unsigned int type_align_in_bits;
14033       unsigned int decl_align_in_bits;
14034       double_int type_size_in_bits;
14035
14036       type = field_type (decl);
14037       type_size_in_bits = double_int_type_size_in_bits (type);
14038       type_align_in_bits = simple_type_align_in_bits (type);
14039
14040       field_size_tree = DECL_SIZE (decl);
14041
14042       /* The size could be unspecified if there was an error, or for
14043          a flexible array member.  */
14044       if (!field_size_tree)
14045         field_size_tree = bitsize_zero_node;
14046
14047       /* If the size of the field is not constant, use the type size.  */
14048       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14049         field_size_in_bits = tree_to_double_int (field_size_tree);
14050       else
14051         field_size_in_bits = type_size_in_bits;
14052
14053       decl_align_in_bits = simple_decl_align_in_bits (decl);
14054
14055       /* The GCC front-end doesn't make any attempt to keep track of the
14056          starting bit offset (relative to the start of the containing
14057          structure type) of the hypothetical "containing object" for a
14058          bit-field.  Thus, when computing the byte offset value for the
14059          start of the "containing object" of a bit-field, we must deduce
14060          this information on our own. This can be rather tricky to do in
14061          some cases.  For example, handling the following structure type
14062          definition when compiling for an i386/i486 target (which only
14063          aligns long long's to 32-bit boundaries) can be very tricky:
14064
14065          struct S { int field1; long long field2:31; };
14066
14067          Fortunately, there is a simple rule-of-thumb which can be used
14068          in such cases.  When compiling for an i386/i486, GCC will
14069          allocate 8 bytes for the structure shown above.  It decides to
14070          do this based upon one simple rule for bit-field allocation.
14071          GCC allocates each "containing object" for each bit-field at
14072          the first (i.e. lowest addressed) legitimate alignment boundary
14073          (based upon the required minimum alignment for the declared
14074          type of the field) which it can possibly use, subject to the
14075          condition that there is still enough available space remaining
14076          in the containing object (when allocated at the selected point)
14077          to fully accommodate all of the bits of the bit-field itself.
14078
14079          This simple rule makes it obvious why GCC allocates 8 bytes for
14080          each object of the structure type shown above.  When looking
14081          for a place to allocate the "containing object" for `field2',
14082          the compiler simply tries to allocate a 64-bit "containing
14083          object" at each successive 32-bit boundary (starting at zero)
14084          until it finds a place to allocate that 64- bit field such that
14085          at least 31 contiguous (and previously unallocated) bits remain
14086          within that selected 64 bit field.  (As it turns out, for the
14087          example above, the compiler finds it is OK to allocate the
14088          "containing object" 64-bit field at bit-offset zero within the
14089          structure type.)
14090
14091          Here we attempt to work backwards from the limited set of facts
14092          we're given, and we try to deduce from those facts, where GCC
14093          must have believed that the containing object started (within
14094          the structure type). The value we deduce is then used (by the
14095          callers of this routine) to generate DW_AT_location and
14096          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14097          the case of DW_AT_location, regular fields as well).  */
14098
14099       /* Figure out the bit-distance from the start of the structure to
14100          the "deepest" bit of the bit-field.  */
14101       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14102
14103       /* This is the tricky part.  Use some fancy footwork to deduce
14104          where the lowest addressed bit of the containing object must
14105          be.  */
14106       object_offset_in_bits
14107         = double_int_sub (deepest_bitpos, type_size_in_bits);
14108
14109       /* Round up to type_align by default.  This works best for
14110          bitfields.  */
14111       object_offset_in_bits
14112         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14113
14114       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14115         {
14116           object_offset_in_bits
14117             = double_int_sub (deepest_bitpos, type_size_in_bits);
14118
14119           /* Round up to decl_align instead.  */
14120           object_offset_in_bits
14121             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14122         }
14123     }
14124   else
14125 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14126     object_offset_in_bits = bitpos_int;
14127
14128   object_offset_in_bytes
14129     = double_int_div (object_offset_in_bits,
14130                       uhwi_to_double_int (BITS_PER_UNIT), true,
14131                       TRUNC_DIV_EXPR);
14132   return double_int_to_shwi (object_offset_in_bytes);
14133 }
14134 \f
14135 /* The following routines define various Dwarf attributes and any data
14136    associated with them.  */
14137
14138 /* Add a location description attribute value to a DIE.
14139
14140    This emits location attributes suitable for whole variables and
14141    whole parameters.  Note that the location attributes for struct fields are
14142    generated by the routine `data_member_location_attribute' below.  */
14143
14144 static inline void
14145 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14146                              dw_loc_list_ref descr)
14147 {
14148   if (descr == 0)
14149     return;
14150   if (single_element_loc_list_p (descr))
14151     add_AT_loc (die, attr_kind, descr->expr);
14152   else
14153     add_AT_loc_list (die, attr_kind, descr);
14154 }
14155
14156 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14157
14158 static void
14159 add_accessibility_attribute (dw_die_ref die, tree decl)
14160 {
14161   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14162      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14163      the default has always been DW_ACCESS_public.  */
14164   if (TREE_PROTECTED (decl))
14165     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14166   else if (TREE_PRIVATE (decl))
14167     {
14168       if (dwarf_version == 2
14169           || die->die_parent == NULL
14170           || die->die_parent->die_tag != DW_TAG_class_type)
14171         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14172     }
14173   else if (dwarf_version > 2
14174            && die->die_parent
14175            && die->die_parent->die_tag == DW_TAG_class_type)
14176     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14177 }
14178
14179 /* Attach the specialized form of location attribute used for data members of
14180    struct and union types.  In the special case of a FIELD_DECL node which
14181    represents a bit-field, the "offset" part of this special location
14182    descriptor must indicate the distance in bytes from the lowest-addressed
14183    byte of the containing struct or union type to the lowest-addressed byte of
14184    the "containing object" for the bit-field.  (See the `field_byte_offset'
14185    function above).
14186
14187    For any given bit-field, the "containing object" is a hypothetical object
14188    (of some integral or enum type) within which the given bit-field lives.  The
14189    type of this hypothetical "containing object" is always the same as the
14190    declared type of the individual bit-field itself (for GCC anyway... the
14191    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14192    bytes) of the hypothetical "containing object" which will be given in the
14193    DW_AT_byte_size attribute for this bit-field.  (See the
14194    `byte_size_attribute' function below.)  It is also used when calculating the
14195    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14196    function below.)  */
14197
14198 static void
14199 add_data_member_location_attribute (dw_die_ref die, tree decl)
14200 {
14201   HOST_WIDE_INT offset;
14202   dw_loc_descr_ref loc_descr = 0;
14203
14204   if (TREE_CODE (decl) == TREE_BINFO)
14205     {
14206       /* We're working on the TAG_inheritance for a base class.  */
14207       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14208         {
14209           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14210              aren't at a fixed offset from all (sub)objects of the same
14211              type.  We need to extract the appropriate offset from our
14212              vtable.  The following dwarf expression means
14213
14214                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14215
14216              This is specific to the V3 ABI, of course.  */
14217
14218           dw_loc_descr_ref tmp;
14219
14220           /* Make a copy of the object address.  */
14221           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14222           add_loc_descr (&loc_descr, tmp);
14223
14224           /* Extract the vtable address.  */
14225           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14226           add_loc_descr (&loc_descr, tmp);
14227
14228           /* Calculate the address of the offset.  */
14229           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14230           gcc_assert (offset < 0);
14231
14232           tmp = int_loc_descriptor (-offset);
14233           add_loc_descr (&loc_descr, tmp);
14234           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14235           add_loc_descr (&loc_descr, tmp);
14236
14237           /* Extract the offset.  */
14238           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14239           add_loc_descr (&loc_descr, tmp);
14240
14241           /* Add it to the object address.  */
14242           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14243           add_loc_descr (&loc_descr, tmp);
14244         }
14245       else
14246         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14247     }
14248   else
14249     offset = field_byte_offset (decl);
14250
14251   if (! loc_descr)
14252     {
14253       if (dwarf_version > 2)
14254         {
14255           /* Don't need to output a location expression, just the constant. */
14256           if (offset < 0)
14257             add_AT_int (die, DW_AT_data_member_location, offset);
14258           else
14259             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14260           return;
14261         }
14262       else
14263         {
14264           enum dwarf_location_atom op;
14265
14266           /* The DWARF2 standard says that we should assume that the structure
14267              address is already on the stack, so we can specify a structure
14268              field address by using DW_OP_plus_uconst.  */
14269
14270 #ifdef MIPS_DEBUGGING_INFO
14271           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14272              operator correctly.  It works only if we leave the offset on the
14273              stack.  */
14274           op = DW_OP_constu;
14275 #else
14276           op = DW_OP_plus_uconst;
14277 #endif
14278
14279           loc_descr = new_loc_descr (op, offset, 0);
14280         }
14281     }
14282
14283   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14284 }
14285
14286 /* Writes integer values to dw_vec_const array.  */
14287
14288 static void
14289 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14290 {
14291   while (size != 0)
14292     {
14293       *dest++ = val & 0xff;
14294       val >>= 8;
14295       --size;
14296     }
14297 }
14298
14299 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14300
14301 static HOST_WIDE_INT
14302 extract_int (const unsigned char *src, unsigned int size)
14303 {
14304   HOST_WIDE_INT val = 0;
14305
14306   src += size;
14307   while (size != 0)
14308     {
14309       val <<= 8;
14310       val |= *--src & 0xff;
14311       --size;
14312     }
14313   return val;
14314 }
14315
14316 /* Writes double_int values to dw_vec_const array.  */
14317
14318 static void
14319 insert_double (double_int val, unsigned char *dest)
14320 {
14321   unsigned char *p0 = dest;
14322   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14323
14324   if (WORDS_BIG_ENDIAN)
14325     {
14326       p0 = p1;
14327       p1 = dest;
14328     }
14329
14330   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14331   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14332 }
14333
14334 /* Writes floating point values to dw_vec_const array.  */
14335
14336 static void
14337 insert_float (const_rtx rtl, unsigned char *array)
14338 {
14339   REAL_VALUE_TYPE rv;
14340   long val[4];
14341   int i;
14342
14343   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14344   real_to_target (val, &rv, GET_MODE (rtl));
14345
14346   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14347   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14348     {
14349       insert_int (val[i], 4, array);
14350       array += 4;
14351     }
14352 }
14353
14354 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14355    does not have a "location" either in memory or in a register.  These
14356    things can arise in GNU C when a constant is passed as an actual parameter
14357    to an inlined function.  They can also arise in C++ where declared
14358    constants do not necessarily get memory "homes".  */
14359
14360 static bool
14361 add_const_value_attribute (dw_die_ref die, rtx rtl)
14362 {
14363   switch (GET_CODE (rtl))
14364     {
14365     case CONST_INT:
14366       {
14367         HOST_WIDE_INT val = INTVAL (rtl);
14368
14369         if (val < 0)
14370           add_AT_int (die, DW_AT_const_value, val);
14371         else
14372           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14373       }
14374       return true;
14375
14376     case CONST_DOUBLE:
14377       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14378          floating-point constant.  A CONST_DOUBLE is used whenever the
14379          constant requires more than one word in order to be adequately
14380          represented.  */
14381       {
14382         enum machine_mode mode = GET_MODE (rtl);
14383
14384         if (SCALAR_FLOAT_MODE_P (mode))
14385           {
14386             unsigned int length = GET_MODE_SIZE (mode);
14387             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14388
14389             insert_float (rtl, array);
14390             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14391           }
14392         else
14393           add_AT_double (die, DW_AT_const_value,
14394                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14395       }
14396       return true;
14397
14398     case CONST_VECTOR:
14399       {
14400         enum machine_mode mode = GET_MODE (rtl);
14401         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14402         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14403         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14404           (length * elt_size);
14405         unsigned int i;
14406         unsigned char *p;
14407
14408         switch (GET_MODE_CLASS (mode))
14409           {
14410           case MODE_VECTOR_INT:
14411             for (i = 0, p = array; i < length; i++, p += elt_size)
14412               {
14413                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14414                 double_int val = rtx_to_double_int (elt);
14415
14416                 if (elt_size <= sizeof (HOST_WIDE_INT))
14417                   insert_int (double_int_to_shwi (val), elt_size, p);
14418                 else
14419                   {
14420                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14421                     insert_double (val, p);
14422                   }
14423               }
14424             break;
14425
14426           case MODE_VECTOR_FLOAT:
14427             for (i = 0, p = array; i < length; i++, p += elt_size)
14428               {
14429                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14430                 insert_float (elt, p);
14431               }
14432             break;
14433
14434           default:
14435             gcc_unreachable ();
14436           }
14437
14438         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14439       }
14440       return true;
14441
14442     case CONST_STRING:
14443       if (dwarf_version >= 4 || !dwarf_strict)
14444         {
14445           dw_loc_descr_ref loc_result;
14446           resolve_one_addr (&rtl, NULL);
14447         rtl_addr:
14448           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14449           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14450           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14451           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14452           add_AT_loc (die, DW_AT_location, loc_result);
14453           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14454           return true;
14455         }
14456       return false;
14457
14458     case CONST:
14459       if (CONSTANT_P (XEXP (rtl, 0)))
14460         return add_const_value_attribute (die, XEXP (rtl, 0));
14461       /* FALLTHROUGH */
14462     case SYMBOL_REF:
14463       if (!const_ok_for_output (rtl))
14464         return false;
14465     case LABEL_REF:
14466       if (dwarf_version >= 4 || !dwarf_strict)
14467         goto rtl_addr;
14468       return false;
14469
14470     case PLUS:
14471       /* In cases where an inlined instance of an inline function is passed
14472          the address of an `auto' variable (which is local to the caller) we
14473          can get a situation where the DECL_RTL of the artificial local
14474          variable (for the inlining) which acts as a stand-in for the
14475          corresponding formal parameter (of the inline function) will look
14476          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14477          exactly a compile-time constant expression, but it isn't the address
14478          of the (artificial) local variable either.  Rather, it represents the
14479          *value* which the artificial local variable always has during its
14480          lifetime.  We currently have no way to represent such quasi-constant
14481          values in Dwarf, so for now we just punt and generate nothing.  */
14482       return false;
14483
14484     case HIGH:
14485     case CONST_FIXED:
14486       return false;
14487
14488     case MEM:
14489       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14490           && MEM_READONLY_P (rtl)
14491           && GET_MODE (rtl) == BLKmode)
14492         {
14493           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14494           return true;
14495         }
14496       return false;
14497
14498     default:
14499       /* No other kinds of rtx should be possible here.  */
14500       gcc_unreachable ();
14501     }
14502   return false;
14503 }
14504
14505 /* Determine whether the evaluation of EXPR references any variables
14506    or functions which aren't otherwise used (and therefore may not be
14507    output).  */
14508 static tree
14509 reference_to_unused (tree * tp, int * walk_subtrees,
14510                      void * data ATTRIBUTE_UNUSED)
14511 {
14512   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14513     *walk_subtrees = 0;
14514
14515   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14516       && ! TREE_ASM_WRITTEN (*tp))
14517     return *tp;
14518   /* ???  The C++ FE emits debug information for using decls, so
14519      putting gcc_unreachable here falls over.  See PR31899.  For now
14520      be conservative.  */
14521   else if (!cgraph_global_info_ready
14522            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14523     return *tp;
14524   else if (TREE_CODE (*tp) == VAR_DECL)
14525     {
14526       struct varpool_node *node = varpool_get_node (*tp);
14527       if (!node || !node->needed)
14528         return *tp;
14529     }
14530   else if (TREE_CODE (*tp) == FUNCTION_DECL
14531            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14532     {
14533       /* The call graph machinery must have finished analyzing,
14534          optimizing and gimplifying the CU by now.
14535          So if *TP has no call graph node associated
14536          to it, it means *TP will not be emitted.  */
14537       if (!cgraph_get_node (*tp))
14538         return *tp;
14539     }
14540   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14541     return *tp;
14542
14543   return NULL_TREE;
14544 }
14545
14546 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14547    for use in a later add_const_value_attribute call.  */
14548
14549 static rtx
14550 rtl_for_decl_init (tree init, tree type)
14551 {
14552   rtx rtl = NULL_RTX;
14553
14554   STRIP_NOPS (init);
14555
14556   /* If a variable is initialized with a string constant without embedded
14557      zeros, build CONST_STRING.  */
14558   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14559     {
14560       tree enttype = TREE_TYPE (type);
14561       tree domain = TYPE_DOMAIN (type);
14562       enum machine_mode mode = TYPE_MODE (enttype);
14563
14564       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14565           && domain
14566           && integer_zerop (TYPE_MIN_VALUE (domain))
14567           && compare_tree_int (TYPE_MAX_VALUE (domain),
14568                                TREE_STRING_LENGTH (init) - 1) == 0
14569           && ((size_t) TREE_STRING_LENGTH (init)
14570               == strlen (TREE_STRING_POINTER (init)) + 1))
14571         {
14572           rtl = gen_rtx_CONST_STRING (VOIDmode,
14573                                       ggc_strdup (TREE_STRING_POINTER (init)));
14574           rtl = gen_rtx_MEM (BLKmode, rtl);
14575           MEM_READONLY_P (rtl) = 1;
14576         }
14577     }
14578   /* Other aggregates, and complex values, could be represented using
14579      CONCAT: FIXME!  */
14580   else if (AGGREGATE_TYPE_P (type)
14581            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14582                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14583            || TREE_CODE (type) == COMPLEX_TYPE)
14584     ;
14585   /* Vectors only work if their mode is supported by the target.
14586      FIXME: generic vectors ought to work too.  */
14587   else if (TREE_CODE (type) == VECTOR_TYPE
14588            && !VECTOR_MODE_P (TYPE_MODE (type)))
14589     ;
14590   /* If the initializer is something that we know will expand into an
14591      immediate RTL constant, expand it now.  We must be careful not to
14592      reference variables which won't be output.  */
14593   else if (initializer_constant_valid_p (init, type)
14594            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14595     {
14596       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14597          possible.  */
14598       if (TREE_CODE (type) == VECTOR_TYPE)
14599         switch (TREE_CODE (init))
14600           {
14601           case VECTOR_CST:
14602             break;
14603           case CONSTRUCTOR:
14604             if (TREE_CONSTANT (init))
14605               {
14606                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14607                 bool constant_p = true;
14608                 tree value;
14609                 unsigned HOST_WIDE_INT ix;
14610
14611                 /* Even when ctor is constant, it might contain non-*_CST
14612                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14613                    belong into VECTOR_CST nodes.  */
14614                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14615                   if (!CONSTANT_CLASS_P (value))
14616                     {
14617                       constant_p = false;
14618                       break;
14619                     }
14620
14621                 if (constant_p)
14622                   {
14623                     init = build_vector_from_ctor (type, elts);
14624                     break;
14625                   }
14626               }
14627             /* FALLTHRU */
14628
14629           default:
14630             return NULL;
14631           }
14632
14633       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14634
14635       /* If expand_expr returns a MEM, it wasn't immediate.  */
14636       gcc_assert (!rtl || !MEM_P (rtl));
14637     }
14638
14639   return rtl;
14640 }
14641
14642 /* Generate RTL for the variable DECL to represent its location.  */
14643
14644 static rtx
14645 rtl_for_decl_location (tree decl)
14646 {
14647   rtx rtl;
14648
14649   /* Here we have to decide where we are going to say the parameter "lives"
14650      (as far as the debugger is concerned).  We only have a couple of
14651      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14652
14653      DECL_RTL normally indicates where the parameter lives during most of the
14654      activation of the function.  If optimization is enabled however, this
14655      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14656      that the parameter doesn't really live anywhere (as far as the code
14657      generation parts of GCC are concerned) during most of the function's
14658      activation.  That will happen (for example) if the parameter is never
14659      referenced within the function.
14660
14661      We could just generate a location descriptor here for all non-NULL
14662      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14663      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14664      where DECL_RTL is NULL or is a pseudo-reg.
14665
14666      Note however that we can only get away with using DECL_INCOMING_RTL as
14667      a backup substitute for DECL_RTL in certain limited cases.  In cases
14668      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14669      we can be sure that the parameter was passed using the same type as it is
14670      declared to have within the function, and that its DECL_INCOMING_RTL
14671      points us to a place where a value of that type is passed.
14672
14673      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14674      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14675      because in these cases DECL_INCOMING_RTL points us to a value of some
14676      type which is *different* from the type of the parameter itself.  Thus,
14677      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14678      such cases, the debugger would end up (for example) trying to fetch a
14679      `float' from a place which actually contains the first part of a
14680      `double'.  That would lead to really incorrect and confusing
14681      output at debug-time.
14682
14683      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14684      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14685      are a couple of exceptions however.  On little-endian machines we can
14686      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14687      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14688      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14689      when (on a little-endian machine) a non-prototyped function has a
14690      parameter declared to be of type `short' or `char'.  In such cases,
14691      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14692      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14693      passed `int' value.  If the debugger then uses that address to fetch
14694      a `short' or a `char' (on a little-endian machine) the result will be
14695      the correct data, so we allow for such exceptional cases below.
14696
14697      Note that our goal here is to describe the place where the given formal
14698      parameter lives during most of the function's activation (i.e. between the
14699      end of the prologue and the start of the epilogue).  We'll do that as best
14700      as we can. Note however that if the given formal parameter is modified
14701      sometime during the execution of the function, then a stack backtrace (at
14702      debug-time) will show the function as having been called with the *new*
14703      value rather than the value which was originally passed in.  This happens
14704      rarely enough that it is not a major problem, but it *is* a problem, and
14705      I'd like to fix it.
14706
14707      A future version of dwarf2out.c may generate two additional attributes for
14708      any given DW_TAG_formal_parameter DIE which will describe the "passed
14709      type" and the "passed location" for the given formal parameter in addition
14710      to the attributes we now generate to indicate the "declared type" and the
14711      "active location" for each parameter.  This additional set of attributes
14712      could be used by debuggers for stack backtraces. Separately, note that
14713      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14714      This happens (for example) for inlined-instances of inline function formal
14715      parameters which are never referenced.  This really shouldn't be
14716      happening.  All PARM_DECL nodes should get valid non-NULL
14717      DECL_INCOMING_RTL values.  FIXME.  */
14718
14719   /* Use DECL_RTL as the "location" unless we find something better.  */
14720   rtl = DECL_RTL_IF_SET (decl);
14721
14722   /* When generating abstract instances, ignore everything except
14723      constants, symbols living in memory, and symbols living in
14724      fixed registers.  */
14725   if (! reload_completed)
14726     {
14727       if (rtl
14728           && (CONSTANT_P (rtl)
14729               || (MEM_P (rtl)
14730                   && CONSTANT_P (XEXP (rtl, 0)))
14731               || (REG_P (rtl)
14732                   && TREE_CODE (decl) == VAR_DECL
14733                   && TREE_STATIC (decl))))
14734         {
14735           rtl = targetm.delegitimize_address (rtl);
14736           return rtl;
14737         }
14738       rtl = NULL_RTX;
14739     }
14740   else if (TREE_CODE (decl) == PARM_DECL)
14741     {
14742       if (rtl == NULL_RTX
14743           || is_pseudo_reg (rtl)
14744           || (MEM_P (rtl)
14745               && is_pseudo_reg (XEXP (rtl, 0))
14746               && DECL_INCOMING_RTL (decl)
14747               && MEM_P (DECL_INCOMING_RTL (decl))
14748               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14749         {
14750           tree declared_type = TREE_TYPE (decl);
14751           tree passed_type = DECL_ARG_TYPE (decl);
14752           enum machine_mode dmode = TYPE_MODE (declared_type);
14753           enum machine_mode pmode = TYPE_MODE (passed_type);
14754
14755           /* This decl represents a formal parameter which was optimized out.
14756              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14757              all cases where (rtl == NULL_RTX) just below.  */
14758           if (dmode == pmode)
14759             rtl = DECL_INCOMING_RTL (decl);
14760           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14761                    && SCALAR_INT_MODE_P (dmode)
14762                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14763                    && DECL_INCOMING_RTL (decl))
14764             {
14765               rtx inc = DECL_INCOMING_RTL (decl);
14766               if (REG_P (inc))
14767                 rtl = inc;
14768               else if (MEM_P (inc))
14769                 {
14770                   if (BYTES_BIG_ENDIAN)
14771                     rtl = adjust_address_nv (inc, dmode,
14772                                              GET_MODE_SIZE (pmode)
14773                                              - GET_MODE_SIZE (dmode));
14774                   else
14775                     rtl = inc;
14776                 }
14777             }
14778         }
14779
14780       /* If the parm was passed in registers, but lives on the stack, then
14781          make a big endian correction if the mode of the type of the
14782          parameter is not the same as the mode of the rtl.  */
14783       /* ??? This is the same series of checks that are made in dbxout.c before
14784          we reach the big endian correction code there.  It isn't clear if all
14785          of these checks are necessary here, but keeping them all is the safe
14786          thing to do.  */
14787       else if (MEM_P (rtl)
14788                && XEXP (rtl, 0) != const0_rtx
14789                && ! CONSTANT_P (XEXP (rtl, 0))
14790                /* Not passed in memory.  */
14791                && !MEM_P (DECL_INCOMING_RTL (decl))
14792                /* Not passed by invisible reference.  */
14793                && (!REG_P (XEXP (rtl, 0))
14794                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14795                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14796 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14797                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14798 #endif
14799                      )
14800                /* Big endian correction check.  */
14801                && BYTES_BIG_ENDIAN
14802                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14803                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14804                    < UNITS_PER_WORD))
14805         {
14806           int offset = (UNITS_PER_WORD
14807                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14808
14809           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14810                              plus_constant (XEXP (rtl, 0), offset));
14811         }
14812     }
14813   else if (TREE_CODE (decl) == VAR_DECL
14814            && rtl
14815            && MEM_P (rtl)
14816            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14817            && BYTES_BIG_ENDIAN)
14818     {
14819       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14820       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14821
14822       /* If a variable is declared "register" yet is smaller than
14823          a register, then if we store the variable to memory, it
14824          looks like we're storing a register-sized value, when in
14825          fact we are not.  We need to adjust the offset of the
14826          storage location to reflect the actual value's bytes,
14827          else gdb will not be able to display it.  */
14828       if (rsize > dsize)
14829         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14830                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14831     }
14832
14833   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14834      and will have been substituted directly into all expressions that use it.
14835      C does not have such a concept, but C++ and other languages do.  */
14836   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14837     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14838
14839   if (rtl)
14840     rtl = targetm.delegitimize_address (rtl);
14841
14842   /* If we don't look past the constant pool, we risk emitting a
14843      reference to a constant pool entry that isn't referenced from
14844      code, and thus is not emitted.  */
14845   if (rtl)
14846     rtl = avoid_constant_pool_reference (rtl);
14847
14848   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14849      in the current CU, resolve_addr will remove the expression referencing
14850      it.  */
14851   if (rtl == NULL_RTX
14852       && TREE_CODE (decl) == VAR_DECL
14853       && !DECL_EXTERNAL (decl)
14854       && TREE_STATIC (decl)
14855       && DECL_NAME (decl)
14856       && !DECL_HARD_REGISTER (decl)
14857       && DECL_MODE (decl) != VOIDmode)
14858     {
14859       rtl = make_decl_rtl_for_debug (decl);
14860       if (!MEM_P (rtl)
14861           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14862           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14863         rtl = NULL_RTX;
14864     }
14865
14866   return rtl;
14867 }
14868
14869 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14870    returned.  If so, the decl for the COMMON block is returned, and the
14871    value is the offset into the common block for the symbol.  */
14872
14873 static tree
14874 fortran_common (tree decl, HOST_WIDE_INT *value)
14875 {
14876   tree val_expr, cvar;
14877   enum machine_mode mode;
14878   HOST_WIDE_INT bitsize, bitpos;
14879   tree offset;
14880   int volatilep = 0, unsignedp = 0;
14881
14882   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14883      it does not have a value (the offset into the common area), or if it
14884      is thread local (as opposed to global) then it isn't common, and shouldn't
14885      be handled as such.  */
14886   if (TREE_CODE (decl) != VAR_DECL
14887       || !TREE_STATIC (decl)
14888       || !DECL_HAS_VALUE_EXPR_P (decl)
14889       || !is_fortran ())
14890     return NULL_TREE;
14891
14892   val_expr = DECL_VALUE_EXPR (decl);
14893   if (TREE_CODE (val_expr) != COMPONENT_REF)
14894     return NULL_TREE;
14895
14896   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14897                               &mode, &unsignedp, &volatilep, true);
14898
14899   if (cvar == NULL_TREE
14900       || TREE_CODE (cvar) != VAR_DECL
14901       || DECL_ARTIFICIAL (cvar)
14902       || !TREE_PUBLIC (cvar))
14903     return NULL_TREE;
14904
14905   *value = 0;
14906   if (offset != NULL)
14907     {
14908       if (!host_integerp (offset, 0))
14909         return NULL_TREE;
14910       *value = tree_low_cst (offset, 0);
14911     }
14912   if (bitpos != 0)
14913     *value += bitpos / BITS_PER_UNIT;
14914
14915   return cvar;
14916 }
14917
14918 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14919    data attribute for a variable or a parameter.  We generate the
14920    DW_AT_const_value attribute only in those cases where the given variable
14921    or parameter does not have a true "location" either in memory or in a
14922    register.  This can happen (for example) when a constant is passed as an
14923    actual argument in a call to an inline function.  (It's possible that
14924    these things can crop up in other ways also.)  Note that one type of
14925    constant value which can be passed into an inlined function is a constant
14926    pointer.  This can happen for example if an actual argument in an inlined
14927    function call evaluates to a compile-time constant address.
14928
14929    CACHE_P is true if it is worth caching the location list for DECL,
14930    so that future calls can reuse it rather than regenerate it from scratch.
14931    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14932    since we will need to refer to them each time the function is inlined.  */
14933
14934 static bool
14935 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14936                                        enum dwarf_attribute attr)
14937 {
14938   rtx rtl;
14939   dw_loc_list_ref list;
14940   var_loc_list *loc_list;
14941   cached_dw_loc_list *cache;
14942   void **slot;
14943
14944   if (TREE_CODE (decl) == ERROR_MARK)
14945     return false;
14946
14947   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14948               || TREE_CODE (decl) == RESULT_DECL);
14949
14950   /* Try to get some constant RTL for this decl, and use that as the value of
14951      the location.  */
14952
14953   rtl = rtl_for_decl_location (decl);
14954   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14955       && add_const_value_attribute (die, rtl))
14956     return true;
14957
14958   /* See if we have single element location list that is equivalent to
14959      a constant value.  That way we are better to use add_const_value_attribute
14960      rather than expanding constant value equivalent.  */
14961   loc_list = lookup_decl_loc (decl);
14962   if (loc_list
14963       && loc_list->first
14964       && loc_list->first->next == NULL
14965       && NOTE_P (loc_list->first->loc)
14966       && NOTE_VAR_LOCATION (loc_list->first->loc)
14967       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14968     {
14969       struct var_loc_node *node;
14970
14971       node = loc_list->first;
14972       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14973       if (GET_CODE (rtl) == EXPR_LIST)
14974         rtl = XEXP (rtl, 0);
14975       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14976           && add_const_value_attribute (die, rtl))
14977          return true;
14978     }
14979   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14980      list several times.  See if we've already cached the contents.  */
14981   list = NULL;
14982   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14983     cache_p = false;
14984   if (cache_p)
14985     {
14986       cache = (cached_dw_loc_list *)
14987         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14988       if (cache)
14989         list = cache->loc_list;
14990     }
14991   if (list == NULL)
14992     {
14993       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14994       /* It is usually worth caching this result if the decl is from
14995          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14996       if (cache_p && list && list->dw_loc_next)
14997         {
14998           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14999                                            DECL_UID (decl), INSERT);
15000           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15001           cache->decl_id = DECL_UID (decl);
15002           cache->loc_list = list;
15003           *slot = cache;
15004         }
15005     }
15006   if (list)
15007     {
15008       add_AT_location_description (die, attr, list);
15009       return true;
15010     }
15011   /* None of that worked, so it must not really have a location;
15012      try adding a constant value attribute from the DECL_INITIAL.  */
15013   return tree_add_const_value_attribute_for_decl (die, decl);
15014 }
15015
15016 /* Add VARIABLE and DIE into deferred locations list.  */
15017
15018 static void
15019 defer_location (tree variable, dw_die_ref die)
15020 {
15021   deferred_locations entry;
15022   entry.variable = variable;
15023   entry.die = die;
15024   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15025 }
15026
15027 /* Helper function for tree_add_const_value_attribute.  Natively encode
15028    initializer INIT into an array.  Return true if successful.  */
15029
15030 static bool
15031 native_encode_initializer (tree init, unsigned char *array, int size)
15032 {
15033   tree type;
15034
15035   if (init == NULL_TREE)
15036     return false;
15037
15038   STRIP_NOPS (init);
15039   switch (TREE_CODE (init))
15040     {
15041     case STRING_CST:
15042       type = TREE_TYPE (init);
15043       if (TREE_CODE (type) == ARRAY_TYPE)
15044         {
15045           tree enttype = TREE_TYPE (type);
15046           enum machine_mode mode = TYPE_MODE (enttype);
15047
15048           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15049             return false;
15050           if (int_size_in_bytes (type) != size)
15051             return false;
15052           if (size > TREE_STRING_LENGTH (init))
15053             {
15054               memcpy (array, TREE_STRING_POINTER (init),
15055                       TREE_STRING_LENGTH (init));
15056               memset (array + TREE_STRING_LENGTH (init),
15057                       '\0', size - TREE_STRING_LENGTH (init));
15058             }
15059           else
15060             memcpy (array, TREE_STRING_POINTER (init), size);
15061           return true;
15062         }
15063       return false;
15064     case CONSTRUCTOR:
15065       type = TREE_TYPE (init);
15066       if (int_size_in_bytes (type) != size)
15067         return false;
15068       if (TREE_CODE (type) == ARRAY_TYPE)
15069         {
15070           HOST_WIDE_INT min_index;
15071           unsigned HOST_WIDE_INT cnt;
15072           int curpos = 0, fieldsize;
15073           constructor_elt *ce;
15074
15075           if (TYPE_DOMAIN (type) == NULL_TREE
15076               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15077             return false;
15078
15079           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15080           if (fieldsize <= 0)
15081             return false;
15082
15083           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15084           memset (array, '\0', size);
15085           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15086             {
15087               tree val = ce->value;
15088               tree index = ce->index;
15089               int pos = curpos;
15090               if (index && TREE_CODE (index) == RANGE_EXPR)
15091                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15092                       * fieldsize;
15093               else if (index)
15094                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15095
15096               if (val)
15097                 {
15098                   STRIP_NOPS (val);
15099                   if (!native_encode_initializer (val, array + pos, fieldsize))
15100                     return false;
15101                 }
15102               curpos = pos + fieldsize;
15103               if (index && TREE_CODE (index) == RANGE_EXPR)
15104                 {
15105                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15106                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15107                   while (count-- > 0)
15108                     {
15109                       if (val)
15110                         memcpy (array + curpos, array + pos, fieldsize);
15111                       curpos += fieldsize;
15112                     }
15113                 }
15114               gcc_assert (curpos <= size);
15115             }
15116           return true;
15117         }
15118       else if (TREE_CODE (type) == RECORD_TYPE
15119                || TREE_CODE (type) == UNION_TYPE)
15120         {
15121           tree field = NULL_TREE;
15122           unsigned HOST_WIDE_INT cnt;
15123           constructor_elt *ce;
15124
15125           if (int_size_in_bytes (type) != size)
15126             return false;
15127
15128           if (TREE_CODE (type) == RECORD_TYPE)
15129             field = TYPE_FIELDS (type);
15130
15131           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15132             {
15133               tree val = ce->value;
15134               int pos, fieldsize;
15135
15136               if (ce->index != 0)
15137                 field = ce->index;
15138
15139               if (val)
15140                 STRIP_NOPS (val);
15141
15142               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15143                 return false;
15144
15145               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15146                   && TYPE_DOMAIN (TREE_TYPE (field))
15147                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15148                 return false;
15149               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15150                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15151                 return false;
15152               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15153               pos = int_byte_position (field);
15154               gcc_assert (pos + fieldsize <= size);
15155               if (val
15156                   && !native_encode_initializer (val, array + pos, fieldsize))
15157                 return false;
15158             }
15159           return true;
15160         }
15161       return false;
15162     case VIEW_CONVERT_EXPR:
15163     case NON_LVALUE_EXPR:
15164       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15165     default:
15166       return native_encode_expr (init, array, size) == size;
15167     }
15168 }
15169
15170 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15171    attribute is the const value T.  */
15172
15173 static bool
15174 tree_add_const_value_attribute (dw_die_ref die, tree t)
15175 {
15176   tree init;
15177   tree type = TREE_TYPE (t);
15178   rtx rtl;
15179
15180   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15181     return false;
15182
15183   init = t;
15184   gcc_assert (!DECL_P (init));
15185
15186   rtl = rtl_for_decl_init (init, type);
15187   if (rtl)
15188     return add_const_value_attribute (die, rtl);
15189   /* If the host and target are sane, try harder.  */
15190   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15191            && initializer_constant_valid_p (init, type))
15192     {
15193       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15194       if (size > 0 && (int) size == size)
15195         {
15196           unsigned char *array = (unsigned char *)
15197             ggc_alloc_cleared_atomic (size);
15198
15199           if (native_encode_initializer (init, array, size))
15200             {
15201               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15202               return true;
15203             }
15204         }
15205     }
15206   return false;
15207 }
15208
15209 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15210    attribute is the const value of T, where T is an integral constant
15211    variable with static storage duration
15212    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15213
15214 static bool
15215 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15216 {
15217
15218   if (!decl
15219       || (TREE_CODE (decl) != VAR_DECL
15220           && TREE_CODE (decl) != CONST_DECL)
15221       || (TREE_CODE (decl) == VAR_DECL
15222           && !TREE_STATIC (decl)))
15223     return false;
15224
15225     if (TREE_READONLY (decl)
15226         && ! TREE_THIS_VOLATILE (decl)
15227         && DECL_INITIAL (decl))
15228       /* OK */;
15229     else
15230       return false;
15231
15232   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15233   if (get_AT (var_die, DW_AT_const_value))
15234     return false;
15235
15236   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15237 }
15238
15239 /* Convert the CFI instructions for the current function into a
15240    location list.  This is used for DW_AT_frame_base when we targeting
15241    a dwarf2 consumer that does not support the dwarf3
15242    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15243    expressions.  */
15244
15245 static dw_loc_list_ref
15246 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15247 {
15248   int ix;
15249   dw_fde_ref fde;
15250   dw_loc_list_ref list, *list_tail;
15251   dw_cfi_ref cfi;
15252   dw_cfa_location last_cfa, next_cfa;
15253   const char *start_label, *last_label, *section;
15254   dw_cfa_location remember;
15255
15256   fde = cfun->fde;
15257   gcc_assert (fde != NULL);
15258
15259   section = secname_for_decl (current_function_decl);
15260   list_tail = &list;
15261   list = NULL;
15262
15263   memset (&next_cfa, 0, sizeof (next_cfa));
15264   next_cfa.reg = INVALID_REGNUM;
15265   remember = next_cfa;
15266
15267   start_label = fde->dw_fde_begin;
15268
15269   /* ??? Bald assumption that the CIE opcode list does not contain
15270      advance opcodes.  */
15271   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15272     lookup_cfa_1 (cfi, &next_cfa, &remember);
15273
15274   last_cfa = next_cfa;
15275   last_label = start_label;
15276
15277   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15278     {
15279       /* If the first partition contained no CFI adjustments, the
15280          CIE opcodes apply to the whole first partition.  */
15281       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15282                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15283       list_tail =&(*list_tail)->dw_loc_next;
15284       start_label = last_label = fde->dw_fde_second_begin;
15285     }
15286
15287   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15288     {
15289       switch (cfi->dw_cfi_opc)
15290         {
15291         case DW_CFA_set_loc:
15292         case DW_CFA_advance_loc1:
15293         case DW_CFA_advance_loc2:
15294         case DW_CFA_advance_loc4:
15295           if (!cfa_equal_p (&last_cfa, &next_cfa))
15296             {
15297               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15298                                          start_label, last_label, section);
15299
15300               list_tail = &(*list_tail)->dw_loc_next;
15301               last_cfa = next_cfa;
15302               start_label = last_label;
15303             }
15304           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15305           break;
15306
15307         case DW_CFA_advance_loc:
15308           /* The encoding is complex enough that we should never emit this.  */
15309           gcc_unreachable ();
15310
15311         default:
15312           lookup_cfa_1 (cfi, &next_cfa, &remember);
15313           break;
15314         }
15315       if (ix + 1 == fde->dw_fde_switch_cfi_index)
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
15322               list_tail = &(*list_tail)->dw_loc_next;
15323               last_cfa = next_cfa;
15324               start_label = last_label;
15325             }
15326           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15327                                      start_label, fde->dw_fde_end, section);
15328           list_tail = &(*list_tail)->dw_loc_next;
15329           start_label = last_label = fde->dw_fde_second_begin;
15330         }
15331     }
15332
15333   if (!cfa_equal_p (&last_cfa, &next_cfa))
15334     {
15335       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15336                                  start_label, last_label, section);
15337       list_tail = &(*list_tail)->dw_loc_next;
15338       start_label = last_label;
15339     }
15340
15341   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15342                              start_label,
15343                              fde->dw_fde_second_begin
15344                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15345                              section);
15346
15347   if (list && list->dw_loc_next)
15348     gen_llsym (list);
15349
15350   return list;
15351 }
15352
15353 /* Compute a displacement from the "steady-state frame pointer" to the
15354    frame base (often the same as the CFA), and store it in
15355    frame_pointer_fb_offset.  OFFSET is added to the displacement
15356    before the latter is negated.  */
15357
15358 static void
15359 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15360 {
15361   rtx reg, elim;
15362
15363 #ifdef FRAME_POINTER_CFA_OFFSET
15364   reg = frame_pointer_rtx;
15365   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15366 #else
15367   reg = arg_pointer_rtx;
15368   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15369 #endif
15370
15371   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15372   if (GET_CODE (elim) == PLUS)
15373     {
15374       offset += INTVAL (XEXP (elim, 1));
15375       elim = XEXP (elim, 0);
15376     }
15377
15378   frame_pointer_fb_offset = -offset;
15379
15380   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15381      in which to eliminate.  This is because it's stack pointer isn't 
15382      directly accessible as a register within the ISA.  To work around
15383      this, assume that while we cannot provide a proper value for
15384      frame_pointer_fb_offset, we won't need one either.  */
15385   frame_pointer_fb_offset_valid
15386     = ((SUPPORTS_STACK_ALIGNMENT
15387         && (elim == hard_frame_pointer_rtx
15388             || elim == stack_pointer_rtx))
15389        || elim == (frame_pointer_needed
15390                    ? hard_frame_pointer_rtx
15391                    : stack_pointer_rtx));
15392 }
15393
15394 /* Generate a DW_AT_name attribute given some string value to be included as
15395    the value of the attribute.  */
15396
15397 static void
15398 add_name_attribute (dw_die_ref die, const char *name_string)
15399 {
15400   if (name_string != NULL && *name_string != 0)
15401     {
15402       if (demangle_name_func)
15403         name_string = (*demangle_name_func) (name_string);
15404
15405       add_AT_string (die, DW_AT_name, name_string);
15406     }
15407 }
15408
15409 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15410    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15411    of TYPE accordingly.
15412
15413    ??? This is a temporary measure until after we're able to generate
15414    regular DWARF for the complex Ada type system.  */
15415
15416 static void 
15417 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15418                                      dw_die_ref context_die)
15419 {
15420   tree dtype;
15421   dw_die_ref dtype_die;
15422
15423   if (!lang_hooks.types.descriptive_type)
15424     return;
15425
15426   dtype = lang_hooks.types.descriptive_type (type);
15427   if (!dtype)
15428     return;
15429
15430   dtype_die = lookup_type_die (dtype);
15431   if (!dtype_die)
15432     {
15433       /* The descriptive type indirectly references TYPE if this is also the
15434          case for TYPE itself.  Do not deal with the circularity here.  */
15435       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
15436       gen_type_die (dtype, context_die);
15437       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 0;
15438       dtype_die = lookup_type_die (dtype);
15439       gcc_assert (dtype_die);
15440     }
15441
15442   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15443 }
15444
15445 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15446
15447 static void
15448 add_comp_dir_attribute (dw_die_ref die)
15449 {
15450   const char *wd = get_src_pwd ();
15451   char *wd1;
15452
15453   if (wd == NULL)
15454     return;
15455
15456   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15457     {
15458       int wdlen;
15459
15460       wdlen = strlen (wd);
15461       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15462       strcpy (wd1, wd);
15463       wd1 [wdlen] = DIR_SEPARATOR;
15464       wd1 [wdlen + 1] = 0;
15465       wd = wd1;
15466     }
15467
15468     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15469 }
15470
15471 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15472    default.  */
15473
15474 static int
15475 lower_bound_default (void)
15476 {
15477   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15478     {
15479     case DW_LANG_C:
15480     case DW_LANG_C89:
15481     case DW_LANG_C99:
15482     case DW_LANG_C_plus_plus:
15483     case DW_LANG_ObjC:
15484     case DW_LANG_ObjC_plus_plus:
15485     case DW_LANG_Java:
15486       return 0;
15487     case DW_LANG_Fortran77:
15488     case DW_LANG_Fortran90:
15489     case DW_LANG_Fortran95:
15490       return 1;
15491     case DW_LANG_UPC:
15492     case DW_LANG_D:
15493     case DW_LANG_Python:
15494       return dwarf_version >= 4 ? 0 : -1;
15495     case DW_LANG_Ada95:
15496     case DW_LANG_Ada83:
15497     case DW_LANG_Cobol74:
15498     case DW_LANG_Cobol85:
15499     case DW_LANG_Pascal83:
15500     case DW_LANG_Modula2:
15501     case DW_LANG_PLI:
15502       return dwarf_version >= 4 ? 1 : -1;
15503     default:
15504       return -1;
15505     }
15506 }
15507
15508 /* Given a tree node describing an array bound (either lower or upper) output
15509    a representation for that bound.  */
15510
15511 static void
15512 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15513 {
15514   switch (TREE_CODE (bound))
15515     {
15516     case ERROR_MARK:
15517       return;
15518
15519     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15520     case INTEGER_CST:
15521       {
15522         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15523         int dflt;
15524
15525         /* Use the default if possible.  */
15526         if (bound_attr == DW_AT_lower_bound
15527             && host_integerp (bound, 0)
15528             && (dflt = lower_bound_default ()) != -1
15529             && tree_low_cst (bound, 0) == dflt)
15530           ;
15531
15532         /* Otherwise represent the bound as an unsigned value with the
15533            precision of its type.  The precision and signedness of the
15534            type will be necessary to re-interpret it unambiguously.  */
15535         else if (prec < HOST_BITS_PER_WIDE_INT)
15536           {
15537             unsigned HOST_WIDE_INT mask
15538               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15539             add_AT_unsigned (subrange_die, bound_attr,
15540                              TREE_INT_CST_LOW (bound) & mask);
15541           }
15542         else if (prec == HOST_BITS_PER_WIDE_INT
15543                  || TREE_INT_CST_HIGH (bound) == 0)
15544           add_AT_unsigned (subrange_die, bound_attr,
15545                            TREE_INT_CST_LOW (bound));
15546         else
15547           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15548                          TREE_INT_CST_LOW (bound));
15549       }
15550       break;
15551
15552     CASE_CONVERT:
15553     case VIEW_CONVERT_EXPR:
15554       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15555       break;
15556
15557     case SAVE_EXPR:
15558       break;
15559
15560     case VAR_DECL:
15561     case PARM_DECL:
15562     case RESULT_DECL:
15563       {
15564         dw_die_ref decl_die = lookup_decl_die (bound);
15565
15566         /* ??? Can this happen, or should the variable have been bound
15567            first?  Probably it can, since I imagine that we try to create
15568            the types of parameters in the order in which they exist in
15569            the list, and won't have created a forward reference to a
15570            later parameter.  */
15571         if (decl_die != NULL)
15572           {
15573             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15574             break;
15575           }
15576       }
15577       /* FALLTHRU */
15578
15579     default:
15580       {
15581         /* Otherwise try to create a stack operation procedure to
15582            evaluate the value of the array bound.  */
15583
15584         dw_die_ref ctx, decl_die;
15585         dw_loc_list_ref list;
15586
15587         list = loc_list_from_tree (bound, 2);
15588         if (list == NULL || single_element_loc_list_p (list))
15589           {
15590             /* If DW_AT_*bound is not a reference nor constant, it is
15591                a DWARF expression rather than location description.
15592                For that loc_list_from_tree (bound, 0) is needed.
15593                If that fails to give a single element list,
15594                fall back to outputting this as a reference anyway.  */
15595             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15596             if (list2 && single_element_loc_list_p (list2))
15597               {
15598                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15599                 break;
15600               }
15601           }
15602         if (list == NULL)
15603           break;
15604
15605         if (current_function_decl == 0)
15606           ctx = comp_unit_die ();
15607         else
15608           ctx = lookup_decl_die (current_function_decl);
15609
15610         decl_die = new_die (DW_TAG_variable, ctx, bound);
15611         add_AT_flag (decl_die, DW_AT_artificial, 1);
15612         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15613         add_AT_location_description (decl_die, DW_AT_location, list);
15614         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15615         break;
15616       }
15617     }
15618 }
15619
15620 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15621    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15622    Note that the block of subscript information for an array type also
15623    includes information about the element type of the given array type.  */
15624
15625 static void
15626 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15627 {
15628   unsigned dimension_number;
15629   tree lower, upper;
15630   dw_die_ref subrange_die;
15631
15632   for (dimension_number = 0;
15633        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15634        type = TREE_TYPE (type), dimension_number++)
15635     {
15636       tree domain = TYPE_DOMAIN (type);
15637
15638       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15639         break;
15640
15641       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15642          and (in GNU C only) variable bounds.  Handle all three forms
15643          here.  */
15644       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15645       if (domain)
15646         {
15647           /* We have an array type with specified bounds.  */
15648           lower = TYPE_MIN_VALUE (domain);
15649           upper = TYPE_MAX_VALUE (domain);
15650
15651           /* Define the index type.  */
15652           if (TREE_TYPE (domain))
15653             {
15654               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15655                  TREE_TYPE field.  We can't emit debug info for this
15656                  because it is an unnamed integral type.  */
15657               if (TREE_CODE (domain) == INTEGER_TYPE
15658                   && TYPE_NAME (domain) == NULL_TREE
15659                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15660                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15661                 ;
15662               else
15663                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15664                                     type_die);
15665             }
15666
15667           /* ??? If upper is NULL, the array has unspecified length,
15668              but it does have a lower bound.  This happens with Fortran
15669                dimension arr(N:*)
15670              Since the debugger is definitely going to need to know N
15671              to produce useful results, go ahead and output the lower
15672              bound solo, and hope the debugger can cope.  */
15673
15674           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15675           if (upper)
15676             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15677         }
15678
15679       /* Otherwise we have an array type with an unspecified length.  The
15680          DWARF-2 spec does not say how to handle this; let's just leave out the
15681          bounds.  */
15682     }
15683 }
15684
15685 static void
15686 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15687 {
15688   unsigned size;
15689
15690   switch (TREE_CODE (tree_node))
15691     {
15692     case ERROR_MARK:
15693       size = 0;
15694       break;
15695     case ENUMERAL_TYPE:
15696     case RECORD_TYPE:
15697     case UNION_TYPE:
15698     case QUAL_UNION_TYPE:
15699       size = int_size_in_bytes (tree_node);
15700       break;
15701     case FIELD_DECL:
15702       /* For a data member of a struct or union, the DW_AT_byte_size is
15703          generally given as the number of bytes normally allocated for an
15704          object of the *declared* type of the member itself.  This is true
15705          even for bit-fields.  */
15706       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15707       break;
15708     default:
15709       gcc_unreachable ();
15710     }
15711
15712   /* Note that `size' might be -1 when we get to this point.  If it is, that
15713      indicates that the byte size of the entity in question is variable.  We
15714      have no good way of expressing this fact in Dwarf at the present time,
15715      so just let the -1 pass on through.  */
15716   add_AT_unsigned (die, DW_AT_byte_size, size);
15717 }
15718
15719 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15720    which specifies the distance in bits from the highest order bit of the
15721    "containing object" for the bit-field to the highest order bit of the
15722    bit-field itself.
15723
15724    For any given bit-field, the "containing object" is a hypothetical object
15725    (of some integral or enum type) within which the given bit-field lives.  The
15726    type of this hypothetical "containing object" is always the same as the
15727    declared type of the individual bit-field itself.  The determination of the
15728    exact location of the "containing object" for a bit-field is rather
15729    complicated.  It's handled by the `field_byte_offset' function (above).
15730
15731    Note that it is the size (in bytes) of the hypothetical "containing object"
15732    which will be given in the DW_AT_byte_size attribute for this bit-field.
15733    (See `byte_size_attribute' above).  */
15734
15735 static inline void
15736 add_bit_offset_attribute (dw_die_ref die, tree decl)
15737 {
15738   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15739   tree type = DECL_BIT_FIELD_TYPE (decl);
15740   HOST_WIDE_INT bitpos_int;
15741   HOST_WIDE_INT highest_order_object_bit_offset;
15742   HOST_WIDE_INT highest_order_field_bit_offset;
15743   HOST_WIDE_INT bit_offset;
15744
15745   /* Must be a field and a bit field.  */
15746   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15747
15748   /* We can't yet handle bit-fields whose offsets are variable, so if we
15749      encounter such things, just return without generating any attribute
15750      whatsoever.  Likewise for variable or too large size.  */
15751   if (! host_integerp (bit_position (decl), 0)
15752       || ! host_integerp (DECL_SIZE (decl), 1))
15753     return;
15754
15755   bitpos_int = int_bit_position (decl);
15756
15757   /* Note that the bit offset is always the distance (in bits) from the
15758      highest-order bit of the "containing object" to the highest-order bit of
15759      the bit-field itself.  Since the "high-order end" of any object or field
15760      is different on big-endian and little-endian machines, the computation
15761      below must take account of these differences.  */
15762   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15763   highest_order_field_bit_offset = bitpos_int;
15764
15765   if (! BYTES_BIG_ENDIAN)
15766     {
15767       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15768       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15769     }
15770
15771   bit_offset
15772     = (! BYTES_BIG_ENDIAN
15773        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15774        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15775
15776   if (bit_offset < 0)
15777     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15778   else
15779     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15780 }
15781
15782 /* For a FIELD_DECL node which represents a bit field, output an attribute
15783    which specifies the length in bits of the given field.  */
15784
15785 static inline void
15786 add_bit_size_attribute (dw_die_ref die, tree decl)
15787 {
15788   /* Must be a field and a bit field.  */
15789   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15790               && DECL_BIT_FIELD_TYPE (decl));
15791
15792   if (host_integerp (DECL_SIZE (decl), 1))
15793     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15794 }
15795
15796 /* If the compiled language is ANSI C, then add a 'prototyped'
15797    attribute, if arg types are given for the parameters of a function.  */
15798
15799 static inline void
15800 add_prototyped_attribute (dw_die_ref die, tree func_type)
15801 {
15802   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15803       && prototype_p (func_type))
15804     add_AT_flag (die, DW_AT_prototyped, 1);
15805 }
15806
15807 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15808    by looking in either the type declaration or object declaration
15809    equate table.  */
15810
15811 static inline dw_die_ref
15812 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15813 {
15814   dw_die_ref origin_die = NULL;
15815
15816   if (TREE_CODE (origin) != FUNCTION_DECL)
15817     {
15818       /* We may have gotten separated from the block for the inlined
15819          function, if we're in an exception handler or some such; make
15820          sure that the abstract function has been written out.
15821
15822          Doing this for nested functions is wrong, however; functions are
15823          distinct units, and our context might not even be inline.  */
15824       tree fn = origin;
15825
15826       if (TYPE_P (fn))
15827         fn = TYPE_STUB_DECL (fn);
15828
15829       fn = decl_function_context (fn);
15830       if (fn)
15831         dwarf2out_abstract_function (fn);
15832     }
15833
15834   if (DECL_P (origin))
15835     origin_die = lookup_decl_die (origin);
15836   else if (TYPE_P (origin))
15837     origin_die = lookup_type_die (origin);
15838
15839   /* XXX: Functions that are never lowered don't always have correct block
15840      trees (in the case of java, they simply have no block tree, in some other
15841      languages).  For these functions, there is nothing we can really do to
15842      output correct debug info for inlined functions in all cases.  Rather
15843      than die, we'll just produce deficient debug info now, in that we will
15844      have variables without a proper abstract origin.  In the future, when all
15845      functions are lowered, we should re-add a gcc_assert (origin_die)
15846      here.  */
15847
15848   if (origin_die)
15849     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15850   return origin_die;
15851 }
15852
15853 /* We do not currently support the pure_virtual attribute.  */
15854
15855 static inline void
15856 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15857 {
15858   if (DECL_VINDEX (func_decl))
15859     {
15860       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15861
15862       if (host_integerp (DECL_VINDEX (func_decl), 0))
15863         add_AT_loc (die, DW_AT_vtable_elem_location,
15864                     new_loc_descr (DW_OP_constu,
15865                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15866                                    0));
15867
15868       /* GNU extension: Record what type this method came from originally.  */
15869       if (debug_info_level > DINFO_LEVEL_TERSE
15870           && DECL_CONTEXT (func_decl))
15871         add_AT_die_ref (die, DW_AT_containing_type,
15872                         lookup_type_die (DECL_CONTEXT (func_decl)));
15873     }
15874 }
15875 \f
15876 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15877    given decl.  This used to be a vendor extension until after DWARF 4
15878    standardized it.  */
15879
15880 static void
15881 add_linkage_attr (dw_die_ref die, tree decl)
15882 {
15883   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15884
15885   /* Mimic what assemble_name_raw does with a leading '*'.  */
15886   if (name[0] == '*')
15887     name = &name[1];
15888
15889   if (dwarf_version >= 4)
15890     add_AT_string (die, DW_AT_linkage_name, name);
15891   else
15892     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15893 }
15894
15895 /* Add source coordinate attributes for the given decl.  */
15896
15897 static void
15898 add_src_coords_attributes (dw_die_ref die, tree decl)
15899 {
15900   expanded_location s;
15901
15902   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15903     return;
15904   s = expand_location (DECL_SOURCE_LOCATION (decl));
15905   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15906   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15907 }
15908
15909 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15910
15911 static void
15912 add_linkage_name (dw_die_ref die, tree decl)
15913 {
15914   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15915        && TREE_PUBLIC (decl)
15916        && !DECL_ABSTRACT (decl)
15917        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15918        && die->die_tag != DW_TAG_member)
15919     {
15920       /* Defer until we have an assembler name set.  */
15921       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15922         {
15923           limbo_die_node *asm_name;
15924
15925           asm_name = ggc_alloc_cleared_limbo_die_node ();
15926           asm_name->die = die;
15927           asm_name->created_for = decl;
15928           asm_name->next = deferred_asm_name;
15929           deferred_asm_name = asm_name;
15930         }
15931       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15932         add_linkage_attr (die, decl);
15933     }
15934 }
15935
15936 /* Add a DW_AT_name attribute and source coordinate attribute for the
15937    given decl, but only if it actually has a name.  */
15938
15939 static void
15940 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15941 {
15942   tree decl_name;
15943
15944   decl_name = DECL_NAME (decl);
15945   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15946     {
15947       const char *name = dwarf2_name (decl, 0);
15948       if (name)
15949         add_name_attribute (die, name);
15950       if (! DECL_ARTIFICIAL (decl))
15951         add_src_coords_attributes (die, decl);
15952
15953       add_linkage_name (die, decl);
15954     }
15955
15956 #ifdef VMS_DEBUGGING_INFO
15957   /* Get the function's name, as described by its RTL.  This may be different
15958      from the DECL_NAME name used in the source file.  */
15959   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15960     {
15961       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15962                    XEXP (DECL_RTL (decl), 0));
15963       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15964     }
15965 #endif /* VMS_DEBUGGING_INFO */
15966 }
15967
15968 #ifdef VMS_DEBUGGING_INFO
15969 /* Output the debug main pointer die for VMS */
15970
15971 void
15972 dwarf2out_vms_debug_main_pointer (void)
15973 {
15974   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15975   dw_die_ref die;
15976
15977   /* Allocate the VMS debug main subprogram die.  */
15978   die = ggc_alloc_cleared_die_node ();
15979   die->die_tag = DW_TAG_subprogram;
15980   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15981   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15982                                current_function_funcdef_no);
15983   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15984
15985   /* Make it the first child of comp_unit_die ().  */
15986   die->die_parent = comp_unit_die ();
15987   if (comp_unit_die ()->die_child)
15988     {
15989       die->die_sib = comp_unit_die ()->die_child->die_sib;
15990       comp_unit_die ()->die_child->die_sib = die;
15991     }
15992   else
15993     {
15994       die->die_sib = die;
15995       comp_unit_die ()->die_child = die;
15996     }
15997 }
15998 #endif /* VMS_DEBUGGING_INFO */
15999
16000 /* Push a new declaration scope.  */
16001
16002 static void
16003 push_decl_scope (tree scope)
16004 {
16005   VEC_safe_push (tree, gc, decl_scope_table, scope);
16006 }
16007
16008 /* Pop a declaration scope.  */
16009
16010 static inline void
16011 pop_decl_scope (void)
16012 {
16013   VEC_pop (tree, decl_scope_table);
16014 }
16015
16016 /* Return the DIE for the scope that immediately contains this type.
16017    Non-named types get global scope.  Named types nested in other
16018    types get their containing scope if it's open, or global scope
16019    otherwise.  All other types (i.e. function-local named types) get
16020    the current active scope.  */
16021
16022 static dw_die_ref
16023 scope_die_for (tree t, dw_die_ref context_die)
16024 {
16025   dw_die_ref scope_die = NULL;
16026   tree containing_scope;
16027   int i;
16028
16029   /* Non-types always go in the current scope.  */
16030   gcc_assert (TYPE_P (t));
16031
16032   containing_scope = TYPE_CONTEXT (t);
16033
16034   /* Use the containing namespace if it was passed in (for a declaration).  */
16035   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16036     {
16037       if (context_die == lookup_decl_die (containing_scope))
16038         /* OK */;
16039       else
16040         containing_scope = NULL_TREE;
16041     }
16042
16043   /* Ignore function type "scopes" from the C frontend.  They mean that
16044      a tagged type is local to a parmlist of a function declarator, but
16045      that isn't useful to DWARF.  */
16046   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16047     containing_scope = NULL_TREE;
16048
16049   if (SCOPE_FILE_SCOPE_P (containing_scope))
16050     scope_die = comp_unit_die ();
16051   else if (TYPE_P (containing_scope))
16052     {
16053       /* For types, we can just look up the appropriate DIE.  But
16054          first we check to see if we're in the middle of emitting it
16055          so we know where the new DIE should go.  */
16056       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16057         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16058           break;
16059
16060       if (i < 0)
16061         {
16062           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16063                       || TREE_ASM_WRITTEN (containing_scope));
16064           /*We are not in the middle of emitting the type
16065             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16066           scope_die = lookup_type_die (containing_scope);
16067
16068           /* If none of the current dies are suitable, we get file scope.  */
16069           if (scope_die == NULL)
16070             scope_die = comp_unit_die ();
16071         }
16072       else
16073         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16074     }
16075   else
16076     scope_die = context_die;
16077
16078   return scope_die;
16079 }
16080
16081 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16082
16083 static inline int
16084 local_scope_p (dw_die_ref context_die)
16085 {
16086   for (; context_die; context_die = context_die->die_parent)
16087     if (context_die->die_tag == DW_TAG_inlined_subroutine
16088         || context_die->die_tag == DW_TAG_subprogram)
16089       return 1;
16090
16091   return 0;
16092 }
16093
16094 /* Returns nonzero if CONTEXT_DIE is a class.  */
16095
16096 static inline int
16097 class_scope_p (dw_die_ref context_die)
16098 {
16099   return (context_die
16100           && (context_die->die_tag == DW_TAG_structure_type
16101               || context_die->die_tag == DW_TAG_class_type
16102               || context_die->die_tag == DW_TAG_interface_type
16103               || context_die->die_tag == DW_TAG_union_type));
16104 }
16105
16106 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16107    whether or not to treat a DIE in this context as a declaration.  */
16108
16109 static inline int
16110 class_or_namespace_scope_p (dw_die_ref context_die)
16111 {
16112   return (class_scope_p (context_die)
16113           || (context_die && context_die->die_tag == DW_TAG_namespace));
16114 }
16115
16116 /* Many forms of DIEs require a "type description" attribute.  This
16117    routine locates the proper "type descriptor" die for the type given
16118    by 'type', and adds a DW_AT_type attribute below the given die.  */
16119
16120 static void
16121 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16122                     int decl_volatile, dw_die_ref context_die)
16123 {
16124   enum tree_code code  = TREE_CODE (type);
16125   dw_die_ref type_die  = NULL;
16126
16127   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16128      or fixed-point type, use the inner type.  This is because we have no
16129      support for unnamed types in base_type_die.  This can happen if this is
16130      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16131   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16132       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16133     type = TREE_TYPE (type), code = TREE_CODE (type);
16134
16135   if (code == ERROR_MARK
16136       /* Handle a special case.  For functions whose return type is void, we
16137          generate *no* type attribute.  (Note that no object may have type
16138          `void', so this only applies to function return types).  */
16139       || code == VOID_TYPE)
16140     return;
16141
16142   type_die = modified_type_die (type,
16143                                 decl_const || TYPE_READONLY (type),
16144                                 decl_volatile || TYPE_VOLATILE (type),
16145                                 context_die);
16146
16147   if (type_die != NULL)
16148     add_AT_die_ref (object_die, DW_AT_type, type_die);
16149 }
16150
16151 /* Given an object die, add the calling convention attribute for the
16152    function call type.  */
16153 static void
16154 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16155 {
16156   enum dwarf_calling_convention value = DW_CC_normal;
16157
16158   value = ((enum dwarf_calling_convention)
16159            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16160
16161   if (is_fortran ()
16162       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16163     {
16164       /* DWARF 2 doesn't provide a way to identify a program's source-level
16165         entry point.  DW_AT_calling_convention attributes are only meant
16166         to describe functions' calling conventions.  However, lacking a
16167         better way to signal the Fortran main program, we used this for 
16168         a long time, following existing custom.  Now, DWARF 4 has 
16169         DW_AT_main_subprogram, which we add below, but some tools still
16170         rely on the old way, which we thus keep.  */
16171       value = DW_CC_program;
16172
16173       if (dwarf_version >= 4 || !dwarf_strict)
16174         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16175     }
16176
16177   /* Only add the attribute if the backend requests it, and
16178      is not DW_CC_normal.  */
16179   if (value && (value != DW_CC_normal))
16180     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16181 }
16182
16183 /* Given a tree pointer to a struct, class, union, or enum type node, return
16184    a pointer to the (string) tag name for the given type, or zero if the type
16185    was declared without a tag.  */
16186
16187 static const char *
16188 type_tag (const_tree type)
16189 {
16190   const char *name = 0;
16191
16192   if (TYPE_NAME (type) != 0)
16193     {
16194       tree t = 0;
16195
16196       /* Find the IDENTIFIER_NODE for the type name.  */
16197       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16198           && !TYPE_NAMELESS (type))
16199         t = TYPE_NAME (type);
16200
16201       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16202          a TYPE_DECL node, regardless of whether or not a `typedef' was
16203          involved.  */
16204       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16205                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16206         {
16207           /* We want to be extra verbose.  Don't call dwarf_name if
16208              DECL_NAME isn't set.  The default hook for decl_printable_name
16209              doesn't like that, and in this context it's correct to return
16210              0, instead of "<anonymous>" or the like.  */
16211           if (DECL_NAME (TYPE_NAME (type))
16212               && !DECL_NAMELESS (TYPE_NAME (type)))
16213             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16214         }
16215
16216       /* Now get the name as a string, or invent one.  */
16217       if (!name && t != 0)
16218         name = IDENTIFIER_POINTER (t);
16219     }
16220
16221   return (name == 0 || *name == '\0') ? 0 : name;
16222 }
16223
16224 /* Return the type associated with a data member, make a special check
16225    for bit field types.  */
16226
16227 static inline tree
16228 member_declared_type (const_tree member)
16229 {
16230   return (DECL_BIT_FIELD_TYPE (member)
16231           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16232 }
16233
16234 /* Get the decl's label, as described by its RTL. This may be different
16235    from the DECL_NAME name used in the source file.  */
16236
16237 #if 0
16238 static const char *
16239 decl_start_label (tree decl)
16240 {
16241   rtx x;
16242   const char *fnname;
16243
16244   x = DECL_RTL (decl);
16245   gcc_assert (MEM_P (x));
16246
16247   x = XEXP (x, 0);
16248   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16249
16250   fnname = XSTR (x, 0);
16251   return fnname;
16252 }
16253 #endif
16254 \f
16255 /* These routines generate the internal representation of the DIE's for
16256    the compilation unit.  Debugging information is collected by walking
16257    the declaration trees passed in from dwarf2out_decl().  */
16258
16259 static void
16260 gen_array_type_die (tree type, dw_die_ref context_die)
16261 {
16262   dw_die_ref scope_die = scope_die_for (type, context_die);
16263   dw_die_ref array_die;
16264
16265   /* GNU compilers represent multidimensional array types as sequences of one
16266      dimensional array types whose element types are themselves array types.
16267      We sometimes squish that down to a single array_type DIE with multiple
16268      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16269      say that we are allowed to do this kind of compression in C, because
16270      there is no difference between an array of arrays and a multidimensional
16271      array.  We don't do this for Ada to remain as close as possible to the
16272      actual representation, which is especially important against the language
16273      flexibilty wrt arrays of variable size.  */
16274
16275   bool collapse_nested_arrays = !is_ada ();
16276   tree element_type;
16277
16278   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16279      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16280   if (TYPE_STRING_FLAG (type)
16281       && TREE_CODE (type) == ARRAY_TYPE
16282       && is_fortran ()
16283       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16284     {
16285       HOST_WIDE_INT size;
16286
16287       array_die = new_die (DW_TAG_string_type, scope_die, type);
16288       add_name_attribute (array_die, type_tag (type));
16289       equate_type_number_to_die (type, array_die);
16290       size = int_size_in_bytes (type);
16291       if (size >= 0)
16292         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16293       else if (TYPE_DOMAIN (type) != NULL_TREE
16294                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16295                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16296         {
16297           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16298           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16299
16300           size = int_size_in_bytes (TREE_TYPE (szdecl));
16301           if (loc && size > 0)
16302             {
16303               add_AT_location_description (array_die, DW_AT_string_length, loc);
16304               if (size != DWARF2_ADDR_SIZE)
16305                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16306             }
16307         }
16308       return;
16309     }
16310
16311   /* ??? The SGI dwarf reader fails for array of array of enum types
16312      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16313      array type comes before the outer array type.  We thus call gen_type_die
16314      before we new_die and must prevent nested array types collapsing for this
16315      target.  */
16316
16317 #ifdef MIPS_DEBUGGING_INFO
16318   gen_type_die (TREE_TYPE (type), context_die);
16319   collapse_nested_arrays = false;
16320 #endif
16321
16322   array_die = new_die (DW_TAG_array_type, scope_die, type);
16323   add_name_attribute (array_die, type_tag (type));
16324   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16325   if (TYPE_ARTIFICIAL (type))
16326     add_AT_flag (array_die, DW_AT_artificial, 1);
16327   equate_type_number_to_die (type, array_die);
16328
16329   if (TREE_CODE (type) == VECTOR_TYPE)
16330     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16331
16332   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16333   if (is_fortran ()
16334       && TREE_CODE (type) == ARRAY_TYPE
16335       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16336       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16337     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16338
16339 #if 0
16340   /* We default the array ordering.  SDB will probably do
16341      the right things even if DW_AT_ordering is not present.  It's not even
16342      an issue until we start to get into multidimensional arrays anyway.  If
16343      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16344      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16345      and when we find out that we need to put these in, we will only do so
16346      for multidimensional arrays.  */
16347   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16348 #endif
16349
16350 #ifdef MIPS_DEBUGGING_INFO
16351   /* The SGI compilers handle arrays of unknown bound by setting
16352      AT_declaration and not emitting any subrange DIEs.  */
16353   if (TREE_CODE (type) == ARRAY_TYPE
16354       && ! TYPE_DOMAIN (type))
16355     add_AT_flag (array_die, DW_AT_declaration, 1);
16356   else
16357 #endif
16358   if (TREE_CODE (type) == VECTOR_TYPE)
16359     {
16360       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16361       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16362       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16363       add_bound_info (subrange_die, DW_AT_upper_bound,
16364                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16365     }
16366   else
16367     add_subscript_info (array_die, type, collapse_nested_arrays);
16368
16369   /* Add representation of the type of the elements of this array type and
16370      emit the corresponding DIE if we haven't done it already.  */
16371   element_type = TREE_TYPE (type);
16372   if (collapse_nested_arrays)
16373     while (TREE_CODE (element_type) == ARRAY_TYPE)
16374       {
16375         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16376           break;
16377         element_type = TREE_TYPE (element_type);
16378       }
16379
16380 #ifndef MIPS_DEBUGGING_INFO
16381   gen_type_die (element_type, context_die);
16382 #endif
16383
16384   add_type_attribute (array_die, element_type, 0, 0, context_die);
16385
16386   if (get_AT (array_die, DW_AT_name))
16387     add_pubtype (type, array_die);
16388 }
16389
16390 static dw_loc_descr_ref
16391 descr_info_loc (tree val, tree base_decl)
16392 {
16393   HOST_WIDE_INT size;
16394   dw_loc_descr_ref loc, loc2;
16395   enum dwarf_location_atom op;
16396
16397   if (val == base_decl)
16398     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16399
16400   switch (TREE_CODE (val))
16401     {
16402     CASE_CONVERT:
16403       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16404     case VAR_DECL:
16405       return loc_descriptor_from_tree (val, 0);
16406     case INTEGER_CST:
16407       if (host_integerp (val, 0))
16408         return int_loc_descriptor (tree_low_cst (val, 0));
16409       break;
16410     case INDIRECT_REF:
16411       size = int_size_in_bytes (TREE_TYPE (val));
16412       if (size < 0)
16413         break;
16414       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16415       if (!loc)
16416         break;
16417       if (size == DWARF2_ADDR_SIZE)
16418         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16419       else
16420         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16421       return loc;
16422     case POINTER_PLUS_EXPR:
16423     case PLUS_EXPR:
16424       if (host_integerp (TREE_OPERAND (val, 1), 1)
16425           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16426              < 16384)
16427         {
16428           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16429           if (!loc)
16430             break;
16431           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16432         }
16433       else
16434         {
16435           op = DW_OP_plus;
16436         do_binop:
16437           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16438           if (!loc)
16439             break;
16440           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16441           if (!loc2)
16442             break;
16443           add_loc_descr (&loc, loc2);
16444           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16445         }
16446       return loc;
16447     case MINUS_EXPR:
16448       op = DW_OP_minus;
16449       goto do_binop;
16450     case MULT_EXPR:
16451       op = DW_OP_mul;
16452       goto do_binop;
16453     case EQ_EXPR:
16454       op = DW_OP_eq;
16455       goto do_binop;
16456     case NE_EXPR:
16457       op = DW_OP_ne;
16458       goto do_binop;
16459     default:
16460       break;
16461     }
16462   return NULL;
16463 }
16464
16465 static void
16466 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16467                       tree val, tree base_decl)
16468 {
16469   dw_loc_descr_ref loc;
16470
16471   if (host_integerp (val, 0))
16472     {
16473       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16474       return;
16475     }
16476
16477   loc = descr_info_loc (val, base_decl);
16478   if (!loc)
16479     return;
16480
16481   add_AT_loc (die, attr, loc);
16482 }
16483
16484 /* This routine generates DIE for array with hidden descriptor, details
16485    are filled into *info by a langhook.  */
16486
16487 static void
16488 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16489                           dw_die_ref context_die)
16490 {
16491   dw_die_ref scope_die = scope_die_for (type, context_die);
16492   dw_die_ref array_die;
16493   int dim;
16494
16495   array_die = new_die (DW_TAG_array_type, scope_die, type);
16496   add_name_attribute (array_die, type_tag (type));
16497   equate_type_number_to_die (type, array_die);
16498
16499   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16500   if (is_fortran ()
16501       && info->ndimensions >= 2)
16502     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16503
16504   if (info->data_location)
16505     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16506                           info->base_decl);
16507   if (info->associated)
16508     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16509                           info->base_decl);
16510   if (info->allocated)
16511     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16512                           info->base_decl);
16513
16514   for (dim = 0; dim < info->ndimensions; dim++)
16515     {
16516       dw_die_ref subrange_die
16517         = new_die (DW_TAG_subrange_type, array_die, NULL);
16518
16519       if (info->dimen[dim].lower_bound)
16520         {
16521           /* If it is the default value, omit it.  */
16522           int dflt;
16523
16524           if (host_integerp (info->dimen[dim].lower_bound, 0)
16525               && (dflt = lower_bound_default ()) != -1
16526               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16527             ;
16528           else
16529             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16530                                   info->dimen[dim].lower_bound,
16531                                   info->base_decl);
16532         }
16533       if (info->dimen[dim].upper_bound)
16534         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16535                               info->dimen[dim].upper_bound,
16536                               info->base_decl);
16537       if (info->dimen[dim].stride)
16538         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16539                               info->dimen[dim].stride,
16540                               info->base_decl);
16541     }
16542
16543   gen_type_die (info->element_type, context_die);
16544   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16545
16546   if (get_AT (array_die, DW_AT_name))
16547     add_pubtype (type, array_die);
16548 }
16549
16550 #if 0
16551 static void
16552 gen_entry_point_die (tree decl, dw_die_ref context_die)
16553 {
16554   tree origin = decl_ultimate_origin (decl);
16555   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16556
16557   if (origin != NULL)
16558     add_abstract_origin_attribute (decl_die, origin);
16559   else
16560     {
16561       add_name_and_src_coords_attributes (decl_die, decl);
16562       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16563                           0, 0, context_die);
16564     }
16565
16566   if (DECL_ABSTRACT (decl))
16567     equate_decl_number_to_die (decl, decl_die);
16568   else
16569     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16570 }
16571 #endif
16572
16573 /* Walk through the list of incomplete types again, trying once more to
16574    emit full debugging info for them.  */
16575
16576 static void
16577 retry_incomplete_types (void)
16578 {
16579   int i;
16580
16581   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16582     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16583                                   DINFO_USAGE_DIR_USE))
16584       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16585 }
16586
16587 /* Determine what tag to use for a record type.  */
16588
16589 static enum dwarf_tag
16590 record_type_tag (tree type)
16591 {
16592   if (! lang_hooks.types.classify_record)
16593     return DW_TAG_structure_type;
16594
16595   switch (lang_hooks.types.classify_record (type))
16596     {
16597     case RECORD_IS_STRUCT:
16598       return DW_TAG_structure_type;
16599
16600     case RECORD_IS_CLASS:
16601       return DW_TAG_class_type;
16602
16603     case RECORD_IS_INTERFACE:
16604       if (dwarf_version >= 3 || !dwarf_strict)
16605         return DW_TAG_interface_type;
16606       return DW_TAG_structure_type;
16607
16608     default:
16609       gcc_unreachable ();
16610     }
16611 }
16612
16613 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16614    include all of the information about the enumeration values also. Each
16615    enumerated type name/value is listed as a child of the enumerated type
16616    DIE.  */
16617
16618 static dw_die_ref
16619 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16620 {
16621   dw_die_ref type_die = lookup_type_die (type);
16622
16623   if (type_die == NULL)
16624     {
16625       type_die = new_die (DW_TAG_enumeration_type,
16626                           scope_die_for (type, context_die), type);
16627       equate_type_number_to_die (type, type_die);
16628       add_name_attribute (type_die, type_tag (type));
16629       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16630       if (TYPE_ARTIFICIAL (type))
16631         add_AT_flag (type_die, DW_AT_artificial, 1);
16632       if (dwarf_version >= 4 || !dwarf_strict)
16633         {
16634           if (ENUM_IS_SCOPED (type))
16635             add_AT_flag (type_die, DW_AT_enum_class, 1);
16636           if (ENUM_IS_OPAQUE (type))
16637             add_AT_flag (type_die, DW_AT_declaration, 1);
16638         }
16639     }
16640   else if (! TYPE_SIZE (type))
16641     return type_die;
16642   else
16643     remove_AT (type_die, DW_AT_declaration);
16644
16645   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16646      given enum type is incomplete, do not generate the DW_AT_byte_size
16647      attribute or the DW_AT_element_list attribute.  */
16648   if (TYPE_SIZE (type))
16649     {
16650       tree link;
16651
16652       TREE_ASM_WRITTEN (type) = 1;
16653       add_byte_size_attribute (type_die, type);
16654       if (TYPE_STUB_DECL (type) != NULL_TREE)
16655         {
16656           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16657           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16658         }
16659
16660       /* If the first reference to this type was as the return type of an
16661          inline function, then it may not have a parent.  Fix this now.  */
16662       if (type_die->die_parent == NULL)
16663         add_child_die (scope_die_for (type, context_die), type_die);
16664
16665       for (link = TYPE_VALUES (type);
16666            link != NULL; link = TREE_CHAIN (link))
16667         {
16668           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16669           tree value = TREE_VALUE (link);
16670
16671           add_name_attribute (enum_die,
16672                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16673
16674           if (TREE_CODE (value) == CONST_DECL)
16675             value = DECL_INITIAL (value);
16676
16677           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16678             /* DWARF2 does not provide a way of indicating whether or
16679                not enumeration constants are signed or unsigned.  GDB
16680                always assumes the values are signed, so we output all
16681                values as if they were signed.  That means that
16682                enumeration constants with very large unsigned values
16683                will appear to have negative values in the debugger.  */
16684             add_AT_int (enum_die, DW_AT_const_value,
16685                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16686         }
16687     }
16688   else
16689     add_AT_flag (type_die, DW_AT_declaration, 1);
16690
16691   if (get_AT (type_die, DW_AT_name))
16692     add_pubtype (type, type_die);
16693
16694   return type_die;
16695 }
16696
16697 /* Generate a DIE to represent either a real live formal parameter decl or to
16698    represent just the type of some formal parameter position in some function
16699    type.
16700
16701    Note that this routine is a bit unusual because its argument may be a
16702    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16703    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16704    node.  If it's the former then this function is being called to output a
16705    DIE to represent a formal parameter object (or some inlining thereof).  If
16706    it's the latter, then this function is only being called to output a
16707    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16708    argument type of some subprogram type.
16709    If EMIT_NAME_P is true, name and source coordinate attributes
16710    are emitted.  */
16711
16712 static dw_die_ref
16713 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16714                           dw_die_ref context_die)
16715 {
16716   tree node_or_origin = node ? node : origin;
16717   tree ultimate_origin;
16718   dw_die_ref parm_die
16719     = new_die (DW_TAG_formal_parameter, context_die, node);
16720
16721   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16722     {
16723     case tcc_declaration:
16724       ultimate_origin = decl_ultimate_origin (node_or_origin);
16725       if (node || ultimate_origin)
16726         origin = ultimate_origin;
16727       if (origin != NULL)
16728         add_abstract_origin_attribute (parm_die, origin);
16729       else if (emit_name_p)
16730         add_name_and_src_coords_attributes (parm_die, node);
16731       if (origin == NULL
16732           || (! DECL_ABSTRACT (node_or_origin)
16733               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16734                                            decl_function_context
16735                                                             (node_or_origin))))
16736         {
16737           tree type = TREE_TYPE (node_or_origin);
16738           if (decl_by_reference_p (node_or_origin))
16739             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16740                                 context_die);
16741           else
16742             add_type_attribute (parm_die, type,
16743                                 TREE_READONLY (node_or_origin),
16744                                 TREE_THIS_VOLATILE (node_or_origin),
16745                                 context_die);
16746         }
16747       if (origin == NULL && DECL_ARTIFICIAL (node))
16748         add_AT_flag (parm_die, DW_AT_artificial, 1);
16749
16750       if (node && node != origin)
16751         equate_decl_number_to_die (node, parm_die);
16752       if (! DECL_ABSTRACT (node_or_origin))
16753         add_location_or_const_value_attribute (parm_die, node_or_origin,
16754                                                node == NULL, DW_AT_location);
16755
16756       break;
16757
16758     case tcc_type:
16759       /* We were called with some kind of a ..._TYPE node.  */
16760       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16761       break;
16762
16763     default:
16764       gcc_unreachable ();
16765     }
16766
16767   return parm_die;
16768 }
16769
16770 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16771    children DW_TAG_formal_parameter DIEs representing the arguments of the
16772    parameter pack.
16773
16774    PARM_PACK must be a function parameter pack.
16775    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16776    must point to the subsequent arguments of the function PACK_ARG belongs to.
16777    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16778    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16779    following the last one for which a DIE was generated.  */
16780
16781 static dw_die_ref
16782 gen_formal_parameter_pack_die  (tree parm_pack,
16783                                 tree pack_arg,
16784                                 dw_die_ref subr_die,
16785                                 tree *next_arg)
16786 {
16787   tree arg;
16788   dw_die_ref parm_pack_die;
16789
16790   gcc_assert (parm_pack
16791               && lang_hooks.function_parameter_pack_p (parm_pack)
16792               && subr_die);
16793
16794   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16795   add_src_coords_attributes (parm_pack_die, parm_pack);
16796
16797   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16798     {
16799       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16800                                                                  parm_pack))
16801         break;
16802       gen_formal_parameter_die (arg, NULL,
16803                                 false /* Don't emit name attribute.  */,
16804                                 parm_pack_die);
16805     }
16806   if (next_arg)
16807     *next_arg = arg;
16808   return parm_pack_die;
16809 }
16810
16811 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16812    at the end of an (ANSI prototyped) formal parameters list.  */
16813
16814 static void
16815 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16816 {
16817   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16818 }
16819
16820 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16821    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16822    parameters as specified in some function type specification (except for
16823    those which appear as part of a function *definition*).  */
16824
16825 static void
16826 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16827 {
16828   tree link;
16829   tree formal_type = NULL;
16830   tree first_parm_type;
16831   tree arg;
16832
16833   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16834     {
16835       arg = DECL_ARGUMENTS (function_or_method_type);
16836       function_or_method_type = TREE_TYPE (function_or_method_type);
16837     }
16838   else
16839     arg = NULL_TREE;
16840
16841   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16842
16843   /* Make our first pass over the list of formal parameter types and output a
16844      DW_TAG_formal_parameter DIE for each one.  */
16845   for (link = first_parm_type; link; )
16846     {
16847       dw_die_ref parm_die;
16848
16849       formal_type = TREE_VALUE (link);
16850       if (formal_type == void_type_node)
16851         break;
16852
16853       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16854       parm_die = gen_formal_parameter_die (formal_type, NULL,
16855                                            true /* Emit name attribute.  */,
16856                                            context_die);
16857       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16858           && link == first_parm_type)
16859         {
16860           add_AT_flag (parm_die, DW_AT_artificial, 1);
16861           if (dwarf_version >= 3 || !dwarf_strict)
16862             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16863         }
16864       else if (arg && DECL_ARTIFICIAL (arg))
16865         add_AT_flag (parm_die, DW_AT_artificial, 1);
16866
16867       link = TREE_CHAIN (link);
16868       if (arg)
16869         arg = DECL_CHAIN (arg);
16870     }
16871
16872   /* If this function type has an ellipsis, add a
16873      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16874   if (formal_type != void_type_node)
16875     gen_unspecified_parameters_die (function_or_method_type, context_die);
16876
16877   /* Make our second (and final) pass over the list of formal parameter types
16878      and output DIEs to represent those types (as necessary).  */
16879   for (link = TYPE_ARG_TYPES (function_or_method_type);
16880        link && TREE_VALUE (link);
16881        link = TREE_CHAIN (link))
16882     gen_type_die (TREE_VALUE (link), context_die);
16883 }
16884
16885 /* We want to generate the DIE for TYPE so that we can generate the
16886    die for MEMBER, which has been defined; we will need to refer back
16887    to the member declaration nested within TYPE.  If we're trying to
16888    generate minimal debug info for TYPE, processing TYPE won't do the
16889    trick; we need to attach the member declaration by hand.  */
16890
16891 static void
16892 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16893 {
16894   gen_type_die (type, context_die);
16895
16896   /* If we're trying to avoid duplicate debug info, we may not have
16897      emitted the member decl for this function.  Emit it now.  */
16898   if (TYPE_STUB_DECL (type)
16899       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16900       && ! lookup_decl_die (member))
16901     {
16902       dw_die_ref type_die;
16903       gcc_assert (!decl_ultimate_origin (member));
16904
16905       push_decl_scope (type);
16906       type_die = lookup_type_die_strip_naming_typedef (type);
16907       if (TREE_CODE (member) == FUNCTION_DECL)
16908         gen_subprogram_die (member, type_die);
16909       else if (TREE_CODE (member) == FIELD_DECL)
16910         {
16911           /* Ignore the nameless fields that are used to skip bits but handle
16912              C++ anonymous unions and structs.  */
16913           if (DECL_NAME (member) != NULL_TREE
16914               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16915               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16916             {
16917               gen_type_die (member_declared_type (member), type_die);
16918               gen_field_die (member, type_die);
16919             }
16920         }
16921       else
16922         gen_variable_die (member, NULL_TREE, type_die);
16923
16924       pop_decl_scope ();
16925     }
16926 }
16927
16928 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16929    may later generate inlined and/or out-of-line instances of.  */
16930
16931 static void
16932 dwarf2out_abstract_function (tree decl)
16933 {
16934   dw_die_ref old_die;
16935   tree save_fn;
16936   tree context;
16937   int was_abstract;
16938   htab_t old_decl_loc_table;
16939   htab_t old_cached_dw_loc_list_table;
16940   int old_call_site_count, old_tail_call_site_count;
16941   struct call_arg_loc_node *old_call_arg_locations;
16942
16943   /* Make sure we have the actual abstract inline, not a clone.  */
16944   decl = DECL_ORIGIN (decl);
16945
16946   old_die = lookup_decl_die (decl);
16947   if (old_die && get_AT (old_die, DW_AT_inline))
16948     /* We've already generated the abstract instance.  */
16949     return;
16950
16951   /* We can be called while recursively when seeing block defining inlined subroutine
16952      DIE.  Be sure to not clobber the outer location table nor use it or we would
16953      get locations in abstract instantces.  */
16954   old_decl_loc_table = decl_loc_table;
16955   decl_loc_table = NULL;
16956   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16957   cached_dw_loc_list_table = NULL;
16958   old_call_arg_locations = call_arg_locations;
16959   call_arg_locations = NULL;
16960   old_call_site_count = call_site_count;
16961   call_site_count = -1;
16962   old_tail_call_site_count = tail_call_site_count;
16963   tail_call_site_count = -1;
16964
16965   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16966      we don't get confused by DECL_ABSTRACT.  */
16967   if (debug_info_level > DINFO_LEVEL_TERSE)
16968     {
16969       context = decl_class_context (decl);
16970       if (context)
16971         gen_type_die_for_member
16972           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16973     }
16974
16975   /* Pretend we've just finished compiling this function.  */
16976   save_fn = current_function_decl;
16977   current_function_decl = decl;
16978   push_cfun (DECL_STRUCT_FUNCTION (decl));
16979
16980   was_abstract = DECL_ABSTRACT (decl);
16981   set_decl_abstract_flags (decl, 1);
16982   dwarf2out_decl (decl);
16983   if (! was_abstract)
16984     set_decl_abstract_flags (decl, 0);
16985
16986   current_function_decl = save_fn;
16987   decl_loc_table = old_decl_loc_table;
16988   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16989   call_arg_locations = old_call_arg_locations;
16990   call_site_count = old_call_site_count;
16991   tail_call_site_count = old_tail_call_site_count;
16992   pop_cfun ();
16993 }
16994
16995 /* Helper function of premark_used_types() which gets called through
16996    htab_traverse.
16997
16998    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16999    marked as unused by prune_unused_types.  */
17000
17001 static int
17002 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17003 {
17004   tree type;
17005   dw_die_ref die;
17006
17007   type = (tree) *slot;
17008   die = lookup_type_die (type);
17009   if (die != NULL)
17010     die->die_perennial_p = 1;
17011   return 1;
17012 }
17013
17014 /* Helper function of premark_types_used_by_global_vars which gets called
17015    through htab_traverse.
17016
17017    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17018    marked as unused by prune_unused_types. The DIE of the type is marked
17019    only if the global variable using the type will actually be emitted.  */
17020
17021 static int
17022 premark_types_used_by_global_vars_helper (void **slot,
17023                                           void *data ATTRIBUTE_UNUSED)
17024 {
17025   struct types_used_by_vars_entry *entry;
17026   dw_die_ref die;
17027
17028   entry = (struct types_used_by_vars_entry *) *slot;
17029   gcc_assert (entry->type != NULL
17030               && entry->var_decl != NULL);
17031   die = lookup_type_die (entry->type);
17032   if (die)
17033     {
17034       /* Ask cgraph if the global variable really is to be emitted.
17035          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17036       struct varpool_node *node = varpool_get_node (entry->var_decl);
17037       if (node && node->needed)
17038         {
17039           die->die_perennial_p = 1;
17040           /* Keep the parent DIEs as well.  */
17041           while ((die = die->die_parent) && die->die_perennial_p == 0)
17042             die->die_perennial_p = 1;
17043         }
17044     }
17045   return 1;
17046 }
17047
17048 /* Mark all members of used_types_hash as perennial.  */
17049
17050 static void
17051 premark_used_types (void)
17052 {
17053   if (cfun && cfun->used_types_hash)
17054     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17055 }
17056
17057 /* Mark all members of types_used_by_vars_entry as perennial.  */
17058
17059 static void
17060 premark_types_used_by_global_vars (void)
17061 {
17062   if (types_used_by_vars_hash)
17063     htab_traverse (types_used_by_vars_hash,
17064                    premark_types_used_by_global_vars_helper, NULL);
17065 }
17066
17067 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17068    for CA_LOC call arg loc node.  */
17069
17070 static dw_die_ref
17071 gen_call_site_die (tree decl, dw_die_ref subr_die,
17072                    struct call_arg_loc_node *ca_loc)
17073 {
17074   dw_die_ref stmt_die = NULL, die;
17075   tree block = ca_loc->block;
17076
17077   while (block
17078          && block != DECL_INITIAL (decl)
17079          && TREE_CODE (block) == BLOCK)
17080     {
17081       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17082         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17083       if (stmt_die)
17084         break;
17085       block = BLOCK_SUPERCONTEXT (block);
17086     }
17087   if (stmt_die == NULL)
17088     stmt_die = subr_die;
17089   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17090   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17091   if (ca_loc->tail_call_p)
17092     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17093   if (ca_loc->symbol_ref)
17094     {
17095       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17096       if (tdie)
17097         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17098       else
17099         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17100     }
17101   return die;
17102 }
17103
17104 /* Generate a DIE to represent a declared function (either file-scope or
17105    block-local).  */
17106
17107 static void
17108 gen_subprogram_die (tree decl, dw_die_ref context_die)
17109 {
17110   tree origin = decl_ultimate_origin (decl);
17111   dw_die_ref subr_die;
17112   tree outer_scope;
17113   dw_die_ref old_die = lookup_decl_die (decl);
17114   int declaration = (current_function_decl != decl
17115                      || class_or_namespace_scope_p (context_die));
17116
17117   premark_used_types ();
17118
17119   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17120      started to generate the abstract instance of an inline, decided to output
17121      its containing class, and proceeded to emit the declaration of the inline
17122      from the member list for the class.  If so, DECLARATION takes priority;
17123      we'll get back to the abstract instance when done with the class.  */
17124
17125   /* The class-scope declaration DIE must be the primary DIE.  */
17126   if (origin && declaration && class_or_namespace_scope_p (context_die))
17127     {
17128       origin = NULL;
17129       gcc_assert (!old_die);
17130     }
17131
17132   /* Now that the C++ front end lazily declares artificial member fns, we
17133      might need to retrofit the declaration into its class.  */
17134   if (!declaration && !origin && !old_die
17135       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17136       && !class_or_namespace_scope_p (context_die)
17137       && debug_info_level > DINFO_LEVEL_TERSE)
17138     old_die = force_decl_die (decl);
17139
17140   if (origin != NULL)
17141     {
17142       gcc_assert (!declaration || local_scope_p (context_die));
17143
17144       /* Fixup die_parent for the abstract instance of a nested
17145          inline function.  */
17146       if (old_die && old_die->die_parent == NULL)
17147         add_child_die (context_die, old_die);
17148
17149       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17150       add_abstract_origin_attribute (subr_die, origin);
17151       /*  This is where the actual code for a cloned function is.
17152           Let's emit linkage name attribute for it.  This helps
17153           debuggers to e.g, set breakpoints into
17154           constructors/destructors when the user asks "break
17155           K::K".  */
17156       add_linkage_name (subr_die, decl);
17157     }
17158   else if (old_die)
17159     {
17160       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17161       struct dwarf_file_data * file_index = lookup_filename (s.file);
17162
17163       if (!get_AT_flag (old_die, DW_AT_declaration)
17164           /* We can have a normal definition following an inline one in the
17165              case of redefinition of GNU C extern inlines.
17166              It seems reasonable to use AT_specification in this case.  */
17167           && !get_AT (old_die, DW_AT_inline))
17168         {
17169           /* Detect and ignore this case, where we are trying to output
17170              something we have already output.  */
17171           return;
17172         }
17173
17174       /* If the definition comes from the same place as the declaration,
17175          maybe use the old DIE.  We always want the DIE for this function
17176          that has the *_pc attributes to be under comp_unit_die so the
17177          debugger can find it.  We also need to do this for abstract
17178          instances of inlines, since the spec requires the out-of-line copy
17179          to have the same parent.  For local class methods, this doesn't
17180          apply; we just use the old DIE.  */
17181       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17182           && (DECL_ARTIFICIAL (decl)
17183               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17184                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17185                       == (unsigned) s.line))))
17186         {
17187           subr_die = old_die;
17188
17189           /* Clear out the declaration attribute and the formal parameters.
17190              Do not remove all children, because it is possible that this
17191              declaration die was forced using force_decl_die(). In such
17192              cases die that forced declaration die (e.g. TAG_imported_module)
17193              is one of the children that we do not want to remove.  */
17194           remove_AT (subr_die, DW_AT_declaration);
17195           remove_AT (subr_die, DW_AT_object_pointer);
17196           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17197         }
17198       else
17199         {
17200           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17201           add_AT_specification (subr_die, old_die);
17202           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17203             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17204           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17205             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17206         }
17207     }
17208   else
17209     {
17210       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17211
17212       if (TREE_PUBLIC (decl))
17213         add_AT_flag (subr_die, DW_AT_external, 1);
17214
17215       add_name_and_src_coords_attributes (subr_die, decl);
17216       if (debug_info_level > DINFO_LEVEL_TERSE)
17217         {
17218           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17219           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17220                               0, 0, context_die);
17221         }
17222
17223       add_pure_or_virtual_attribute (subr_die, decl);
17224       if (DECL_ARTIFICIAL (decl))
17225         add_AT_flag (subr_die, DW_AT_artificial, 1);
17226
17227       add_accessibility_attribute (subr_die, decl);
17228     }
17229
17230   if (declaration)
17231     {
17232       if (!old_die || !get_AT (old_die, DW_AT_inline))
17233         {
17234           add_AT_flag (subr_die, DW_AT_declaration, 1);
17235
17236           /* If this is an explicit function declaration then generate
17237              a DW_AT_explicit attribute.  */
17238           if (lang_hooks.decls.function_decl_explicit_p (decl)
17239               && (dwarf_version >= 3 || !dwarf_strict))
17240             add_AT_flag (subr_die, DW_AT_explicit, 1);
17241
17242           /* The first time we see a member function, it is in the context of
17243              the class to which it belongs.  We make sure of this by emitting
17244              the class first.  The next time is the definition, which is
17245              handled above.  The two may come from the same source text.
17246
17247              Note that force_decl_die() forces function declaration die. It is
17248              later reused to represent definition.  */
17249           equate_decl_number_to_die (decl, subr_die);
17250         }
17251     }
17252   else if (DECL_ABSTRACT (decl))
17253     {
17254       if (DECL_DECLARED_INLINE_P (decl))
17255         {
17256           if (cgraph_function_possibly_inlined_p (decl))
17257             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17258           else
17259             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17260         }
17261       else
17262         {
17263           if (cgraph_function_possibly_inlined_p (decl))
17264             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17265           else
17266             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17267         }
17268
17269       if (DECL_DECLARED_INLINE_P (decl)
17270           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17271         add_AT_flag (subr_die, DW_AT_artificial, 1);
17272
17273       equate_decl_number_to_die (decl, subr_die);
17274     }
17275   else if (!DECL_EXTERNAL (decl))
17276     {
17277       HOST_WIDE_INT cfa_fb_offset;
17278
17279       if (!old_die || !get_AT (old_die, DW_AT_inline))
17280         equate_decl_number_to_die (decl, subr_die);
17281
17282       if (!flag_reorder_blocks_and_partition)
17283         {
17284           dw_fde_ref fde = cfun->fde;
17285           if (fde->dw_fde_begin)
17286             {
17287               /* We have already generated the labels.  */
17288               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17289               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17290             }
17291           else
17292             {
17293               /* Create start/end labels and add the range.  */
17294               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17295               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17296                                            current_function_funcdef_no);
17297               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17298               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17299                                            current_function_funcdef_no);
17300               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17301             }
17302
17303 #if VMS_DEBUGGING_INFO
17304       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17305          Section 2.3 Prologue and Epilogue Attributes:
17306          When a breakpoint is set on entry to a function, it is generally
17307          desirable for execution to be suspended, not on the very first
17308          instruction of the function, but rather at a point after the
17309          function's frame has been set up, after any language defined local
17310          declaration processing has been completed, and before execution of
17311          the first statement of the function begins. Debuggers generally
17312          cannot properly determine where this point is.  Similarly for a
17313          breakpoint set on exit from a function. The prologue and epilogue
17314          attributes allow a compiler to communicate the location(s) to use.  */
17315
17316       {
17317         if (fde->dw_fde_vms_end_prologue)
17318           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17319             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17320
17321         if (fde->dw_fde_vms_begin_epilogue)
17322           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17323             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17324       }
17325 #endif
17326
17327           add_pubname (decl, subr_die);
17328         }
17329       else
17330         {
17331           /* Generate pubnames entries for the split function code ranges.  */
17332           dw_fde_ref fde = cfun->fde;
17333
17334           if (fde->dw_fde_second_begin)
17335             {
17336               if (dwarf_version >= 3 || !dwarf_strict)
17337                 {
17338                   /* We should use ranges for non-contiguous code section 
17339                      addresses.  Use the actual code range for the initial
17340                      section, since the HOT/COLD labels might precede an 
17341                      alignment offset.  */
17342                   bool range_list_added = false;
17343                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17344                                         fde->dw_fde_end, &range_list_added);
17345                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17346                                         fde->dw_fde_second_end,
17347                                         &range_list_added);
17348                   add_pubname (decl, subr_die);
17349                   if (range_list_added)
17350                     add_ranges (NULL);
17351                 }
17352               else
17353                 {
17354                   /* There is no real support in DW2 for this .. so we make
17355                      a work-around.  First, emit the pub name for the segment
17356                      containing the function label.  Then make and emit a
17357                      simplified subprogram DIE for the second segment with the
17358                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17359                      linkage name for the second die so that gdb will find both
17360                      sections when given "b foo".  */
17361                   const char *name = NULL;
17362                   tree decl_name = DECL_NAME (decl);
17363                   dw_die_ref seg_die;
17364
17365                   /* Do the 'primary' section.   */
17366                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17367                                  fde->dw_fde_begin);
17368                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17369                                  fde->dw_fde_end);
17370                   /* Add it.   */
17371                   add_pubname (decl, subr_die);
17372
17373                   /* Build a minimal DIE for the secondary section.  */
17374                   seg_die = new_die (DW_TAG_subprogram,
17375                                      subr_die->die_parent, decl);
17376
17377                   if (TREE_PUBLIC (decl))
17378                     add_AT_flag (seg_die, DW_AT_external, 1);
17379
17380                   if (decl_name != NULL 
17381                       && IDENTIFIER_POINTER (decl_name) != NULL)
17382                     {
17383                       name = dwarf2_name (decl, 1);
17384                       if (! DECL_ARTIFICIAL (decl))
17385                         add_src_coords_attributes (seg_die, decl);
17386
17387                       add_linkage_name (seg_die, decl);
17388                     }
17389                   gcc_assert (name != NULL);
17390                   add_pure_or_virtual_attribute (seg_die, decl);
17391                   if (DECL_ARTIFICIAL (decl))
17392                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17393
17394                   name = concat ("__second_sect_of_", name, NULL); 
17395                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17396                                  fde->dw_fde_second_begin);
17397                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17398                                  fde->dw_fde_second_end);
17399                   add_name_attribute (seg_die, name);
17400                   add_pubname_string (name, seg_die);
17401                 }
17402             }
17403           else
17404             {
17405               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17406               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17407               add_pubname (decl, subr_die);
17408             }
17409         }
17410
17411 #ifdef MIPS_DEBUGGING_INFO
17412       /* Add a reference to the FDE for this routine.  */
17413       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17414 #endif
17415
17416       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17417
17418       /* We define the "frame base" as the function's CFA.  This is more
17419          convenient for several reasons: (1) It's stable across the prologue
17420          and epilogue, which makes it better than just a frame pointer,
17421          (2) With dwarf3, there exists a one-byte encoding that allows us
17422          to reference the .debug_frame data by proxy, but failing that,
17423          (3) We can at least reuse the code inspection and interpretation
17424          code that determines the CFA position at various points in the
17425          function.  */
17426       if (dwarf_version >= 3)
17427         {
17428           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17429           add_AT_loc (subr_die, DW_AT_frame_base, op);
17430         }
17431       else
17432         {
17433           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17434           if (list->dw_loc_next)
17435             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17436           else
17437             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17438         }
17439
17440       /* Compute a displacement from the "steady-state frame pointer" to
17441          the CFA.  The former is what all stack slots and argument slots
17442          will reference in the rtl; the later is what we've told the
17443          debugger about.  We'll need to adjust all frame_base references
17444          by this displacement.  */
17445       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17446
17447       if (cfun->static_chain_decl)
17448         add_AT_location_description (subr_die, DW_AT_static_link,
17449                  loc_list_from_tree (cfun->static_chain_decl, 2));
17450     }
17451
17452   /* Generate child dies for template paramaters.  */
17453   if (debug_info_level > DINFO_LEVEL_TERSE)
17454     gen_generic_params_dies (decl);
17455
17456   /* Now output descriptions of the arguments for this function. This gets
17457      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17458      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17459      `...' at the end of the formal parameter list.  In order to find out if
17460      there was a trailing ellipsis or not, we must instead look at the type
17461      associated with the FUNCTION_DECL.  This will be a node of type
17462      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17463      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17464      an ellipsis at the end.  */
17465
17466   /* In the case where we are describing a mere function declaration, all we
17467      need to do here (and all we *can* do here) is to describe the *types* of
17468      its formal parameters.  */
17469   if (debug_info_level <= DINFO_LEVEL_TERSE)
17470     ;
17471   else if (declaration)
17472     gen_formal_types_die (decl, subr_die);
17473   else
17474     {
17475       /* Generate DIEs to represent all known formal parameters.  */
17476       tree parm = DECL_ARGUMENTS (decl);
17477       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17478       tree generic_decl_parm = generic_decl
17479                                 ? DECL_ARGUMENTS (generic_decl)
17480                                 : NULL;
17481
17482       /* Now we want to walk the list of parameters of the function and
17483          emit their relevant DIEs.
17484
17485          We consider the case of DECL being an instance of a generic function
17486          as well as it being a normal function.
17487
17488          If DECL is an instance of a generic function we walk the
17489          parameters of the generic function declaration _and_ the parameters of
17490          DECL itself. This is useful because we want to emit specific DIEs for
17491          function parameter packs and those are declared as part of the
17492          generic function declaration. In that particular case,
17493          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17494          That DIE has children DIEs representing the set of arguments
17495          of the pack. Note that the set of pack arguments can be empty.
17496          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17497          children DIE.
17498
17499          Otherwise, we just consider the parameters of DECL.  */
17500       while (generic_decl_parm || parm)
17501         {
17502           if (generic_decl_parm
17503               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17504             gen_formal_parameter_pack_die (generic_decl_parm,
17505                                            parm, subr_die,
17506                                            &parm);
17507           else if (parm)
17508             {
17509               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17510
17511               if (parm == DECL_ARGUMENTS (decl)
17512                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17513                   && parm_die
17514                   && (dwarf_version >= 3 || !dwarf_strict))
17515                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17516
17517               parm = DECL_CHAIN (parm);
17518             }
17519
17520           if (generic_decl_parm)
17521             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17522         }
17523
17524       /* Decide whether we need an unspecified_parameters DIE at the end.
17525          There are 2 more cases to do this for: 1) the ansi ... declaration -
17526          this is detectable when the end of the arg list is not a
17527          void_type_node 2) an unprototyped function declaration (not a
17528          definition).  This just means that we have no info about the
17529          parameters at all.  */
17530       if (prototype_p (TREE_TYPE (decl)))
17531         {
17532           /* This is the prototyped case, check for....  */
17533           if (stdarg_p (TREE_TYPE (decl)))
17534             gen_unspecified_parameters_die (decl, subr_die);
17535         }
17536       else if (DECL_INITIAL (decl) == NULL_TREE)
17537         gen_unspecified_parameters_die (decl, subr_die);
17538     }
17539
17540   /* Output Dwarf info for all of the stuff within the body of the function
17541      (if it has one - it may be just a declaration).  */
17542   outer_scope = DECL_INITIAL (decl);
17543
17544   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17545      a function.  This BLOCK actually represents the outermost binding contour
17546      for the function, i.e. the contour in which the function's formal
17547      parameters and labels get declared. Curiously, it appears that the front
17548      end doesn't actually put the PARM_DECL nodes for the current function onto
17549      the BLOCK_VARS list for this outer scope, but are strung off of the
17550      DECL_ARGUMENTS list for the function instead.
17551
17552      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17553      the LABEL_DECL nodes for the function however, and we output DWARF info
17554      for those in decls_for_scope.  Just within the `outer_scope' there will be
17555      a BLOCK node representing the function's outermost pair of curly braces,
17556      and any blocks used for the base and member initializers of a C++
17557      constructor function.  */
17558   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17559     {
17560       int call_site_note_count = 0;
17561       int tail_call_site_note_count = 0;
17562
17563       /* Emit a DW_TAG_variable DIE for a named return value.  */
17564       if (DECL_NAME (DECL_RESULT (decl)))
17565         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17566
17567       current_function_has_inlines = 0;
17568       decls_for_scope (outer_scope, subr_die, 0);
17569
17570       if (call_arg_locations && !dwarf_strict)
17571         {
17572           struct call_arg_loc_node *ca_loc;
17573           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17574             {
17575               dw_die_ref die = NULL;
17576               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17577               rtx arg, next_arg;
17578
17579               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17580                    arg; arg = next_arg)
17581                 {
17582                   dw_loc_descr_ref reg, val;
17583                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17584                   dw_die_ref cdie, tdie = NULL;
17585
17586                   next_arg = XEXP (arg, 1);
17587                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17588                       && next_arg
17589                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17590                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17591                       && REGNO (XEXP (XEXP (arg, 0), 0))
17592                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17593                     next_arg = XEXP (next_arg, 1);
17594                   if (mode == VOIDmode)
17595                     {
17596                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17597                       if (mode == VOIDmode)
17598                         mode = GET_MODE (XEXP (arg, 0));
17599                     }
17600                   if (mode == VOIDmode || mode == BLKmode)
17601                     continue;
17602                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17603                     {
17604                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17605                       tloc = XEXP (XEXP (arg, 0), 1);
17606                       continue;
17607                     }
17608                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17609                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17610                     {
17611                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17612                       tlocc = XEXP (XEXP (arg, 0), 1);
17613                       continue;
17614                     }
17615                   reg = NULL;
17616                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17617                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17618                                               VAR_INIT_STATUS_INITIALIZED);
17619                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17620                     {
17621                       rtx mem = XEXP (XEXP (arg, 0), 0);
17622                       reg = mem_loc_descriptor (XEXP (mem, 0),
17623                                                 get_address_mode (mem),
17624                                                 GET_MODE (mem),
17625                                                 VAR_INIT_STATUS_INITIALIZED);
17626                     }
17627                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17628                            == DEBUG_PARAMETER_REF)
17629                     {
17630                       tree tdecl
17631                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17632                       tdie = lookup_decl_die (tdecl);
17633                       if (tdie == NULL)
17634                         continue;
17635                     }
17636                   else
17637                     continue;
17638                   if (reg == NULL
17639                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17640                          != DEBUG_PARAMETER_REF)
17641                     continue;
17642                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17643                                             VOIDmode,
17644                                             VAR_INIT_STATUS_INITIALIZED);
17645                   if (val == NULL)
17646                     continue;
17647                   if (die == NULL)
17648                     die = gen_call_site_die (decl, subr_die, ca_loc);
17649                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17650                                   NULL_TREE);
17651                   if (reg != NULL)
17652                     add_AT_loc (cdie, DW_AT_location, reg);
17653                   else if (tdie != NULL)
17654                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17655                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17656                   if (next_arg != XEXP (arg, 1))
17657                     {
17658                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17659                       if (mode == VOIDmode)
17660                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17661                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17662                                                             0), 1),
17663                                                 mode, VOIDmode,
17664                                                 VAR_INIT_STATUS_INITIALIZED);
17665                       if (val != NULL)
17666                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17667                     }
17668                 }
17669               if (die == NULL
17670                   && (ca_loc->symbol_ref || tloc))
17671                 die = gen_call_site_die (decl, subr_die, ca_loc);
17672               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17673                 {
17674                   dw_loc_descr_ref tval = NULL;
17675
17676                   if (tloc != NULL_RTX)
17677                     tval = mem_loc_descriptor (tloc,
17678                                                GET_MODE (tloc) == VOIDmode
17679                                                ? Pmode : GET_MODE (tloc),
17680                                                VOIDmode,
17681                                                VAR_INIT_STATUS_INITIALIZED);
17682                   if (tval)
17683                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17684                   else if (tlocc != NULL_RTX)
17685                     {
17686                       tval = mem_loc_descriptor (tlocc,
17687                                                  GET_MODE (tlocc) == VOIDmode
17688                                                  ? Pmode : GET_MODE (tlocc),
17689                                                  VOIDmode,
17690                                                  VAR_INIT_STATUS_INITIALIZED);
17691                       if (tval)
17692                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17693                                     tval);
17694                     }
17695                 }
17696               if (die != NULL)
17697                 {
17698                   call_site_note_count++;
17699                   if (ca_loc->tail_call_p)
17700                     tail_call_site_note_count++;
17701                 }
17702             }
17703         }
17704       call_arg_locations = NULL;
17705       call_arg_loc_last = NULL;
17706       if (tail_call_site_count >= 0
17707           && tail_call_site_count == tail_call_site_note_count
17708           && !dwarf_strict)
17709         {
17710           if (call_site_count >= 0
17711               && call_site_count == call_site_note_count)
17712             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17713           else
17714             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17715         }
17716       call_site_count = -1;
17717       tail_call_site_count = -1;
17718     }
17719   /* Add the calling convention attribute if requested.  */
17720   add_calling_convention_attribute (subr_die, decl);
17721
17722 }
17723
17724 /* Returns a hash value for X (which really is a die_struct).  */
17725
17726 static hashval_t
17727 common_block_die_table_hash (const void *x)
17728 {
17729   const_dw_die_ref d = (const_dw_die_ref) x;
17730   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17731 }
17732
17733 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17734    as decl_id and die_parent of die_struct Y.  */
17735
17736 static int
17737 common_block_die_table_eq (const void *x, const void *y)
17738 {
17739   const_dw_die_ref d = (const_dw_die_ref) x;
17740   const_dw_die_ref e = (const_dw_die_ref) y;
17741   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17742 }
17743
17744 /* Generate a DIE to represent a declared data object.
17745    Either DECL or ORIGIN must be non-null.  */
17746
17747 static void
17748 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17749 {
17750   HOST_WIDE_INT off;
17751   tree com_decl;
17752   tree decl_or_origin = decl ? decl : origin;
17753   tree ultimate_origin;
17754   dw_die_ref var_die;
17755   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17756   dw_die_ref origin_die;
17757   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17758                       || class_or_namespace_scope_p (context_die));
17759   bool specialization_p = false;
17760
17761   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17762   if (decl || ultimate_origin)
17763     origin = ultimate_origin;
17764   com_decl = fortran_common (decl_or_origin, &off);
17765
17766   /* Symbol in common gets emitted as a child of the common block, in the form
17767      of a data member.  */
17768   if (com_decl)
17769     {
17770       dw_die_ref com_die;
17771       dw_loc_list_ref loc;
17772       die_node com_die_arg;
17773
17774       var_die = lookup_decl_die (decl_or_origin);
17775       if (var_die)
17776         {
17777           if (get_AT (var_die, DW_AT_location) == NULL)
17778             {
17779               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17780               if (loc)
17781                 {
17782                   if (off)
17783                     {
17784                       /* Optimize the common case.  */
17785                       if (single_element_loc_list_p (loc)
17786                           && loc->expr->dw_loc_opc == DW_OP_addr
17787                           && loc->expr->dw_loc_next == NULL
17788                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17789                              == SYMBOL_REF)
17790                         loc->expr->dw_loc_oprnd1.v.val_addr
17791                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17792                         else
17793                           loc_list_plus_const (loc, off);
17794                     }
17795                   add_AT_location_description (var_die, DW_AT_location, loc);
17796                   remove_AT (var_die, DW_AT_declaration);
17797                 }
17798             }
17799           return;
17800         }
17801
17802       if (common_block_die_table == NULL)
17803         common_block_die_table
17804           = htab_create_ggc (10, common_block_die_table_hash,
17805                              common_block_die_table_eq, NULL);
17806
17807       com_die_arg.decl_id = DECL_UID (com_decl);
17808       com_die_arg.die_parent = context_die;
17809       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17810       loc = loc_list_from_tree (com_decl, 2);
17811       if (com_die == NULL)
17812         {
17813           const char *cnam
17814             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17815           void **slot;
17816
17817           com_die = new_die (DW_TAG_common_block, context_die, decl);
17818           add_name_and_src_coords_attributes (com_die, com_decl);
17819           if (loc)
17820             {
17821               add_AT_location_description (com_die, DW_AT_location, loc);
17822               /* Avoid sharing the same loc descriptor between
17823                  DW_TAG_common_block and DW_TAG_variable.  */
17824               loc = loc_list_from_tree (com_decl, 2);
17825             }
17826           else if (DECL_EXTERNAL (decl))
17827             add_AT_flag (com_die, DW_AT_declaration, 1);
17828           add_pubname_string (cnam, com_die); /* ??? needed? */
17829           com_die->decl_id = DECL_UID (com_decl);
17830           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17831           *slot = (void *) com_die;
17832         }
17833       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17834         {
17835           add_AT_location_description (com_die, DW_AT_location, loc);
17836           loc = loc_list_from_tree (com_decl, 2);
17837           remove_AT (com_die, DW_AT_declaration);
17838         }
17839       var_die = new_die (DW_TAG_variable, com_die, decl);
17840       add_name_and_src_coords_attributes (var_die, decl);
17841       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17842                           TREE_THIS_VOLATILE (decl), context_die);
17843       add_AT_flag (var_die, DW_AT_external, 1);
17844       if (loc)
17845         {
17846           if (off)
17847             {
17848               /* Optimize the common case.  */
17849               if (single_element_loc_list_p (loc)
17850                   && loc->expr->dw_loc_opc == DW_OP_addr
17851                   && loc->expr->dw_loc_next == NULL
17852                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17853                 loc->expr->dw_loc_oprnd1.v.val_addr
17854                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17855               else
17856                 loc_list_plus_const (loc, off);
17857             }
17858           add_AT_location_description (var_die, DW_AT_location, loc);
17859         }
17860       else if (DECL_EXTERNAL (decl))
17861         add_AT_flag (var_die, DW_AT_declaration, 1);
17862       equate_decl_number_to_die (decl, var_die);
17863       return;
17864     }
17865
17866   /* If the compiler emitted a definition for the DECL declaration
17867      and if we already emitted a DIE for it, don't emit a second
17868      DIE for it again. Allow re-declarations of DECLs that are
17869      inside functions, though.  */
17870   if (old_die && declaration && !local_scope_p (context_die))
17871     return;
17872
17873   /* For static data members, the declaration in the class is supposed
17874      to have DW_TAG_member tag; the specification should still be
17875      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17876   if (declaration && class_scope_p (context_die))
17877     var_die = new_die (DW_TAG_member, context_die, decl);
17878   else
17879     var_die = new_die (DW_TAG_variable, context_die, decl);
17880
17881   origin_die = NULL;
17882   if (origin != NULL)
17883     origin_die = add_abstract_origin_attribute (var_die, origin);
17884
17885   /* Loop unrolling can create multiple blocks that refer to the same
17886      static variable, so we must test for the DW_AT_declaration flag.
17887
17888      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17889      copy decls and set the DECL_ABSTRACT flag on them instead of
17890      sharing them.
17891
17892      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17893
17894      ??? The declare_in_namespace support causes us to get two DIEs for one
17895      variable, both of which are declarations.  We want to avoid considering
17896      one to be a specification, so we must test that this DIE is not a
17897      declaration.  */
17898   else if (old_die && TREE_STATIC (decl) && ! declaration
17899            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17900     {
17901       /* This is a definition of a C++ class level static.  */
17902       add_AT_specification (var_die, old_die);
17903       specialization_p = true;
17904       if (DECL_NAME (decl))
17905         {
17906           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17907           struct dwarf_file_data * file_index = lookup_filename (s.file);
17908
17909           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17910             add_AT_file (var_die, DW_AT_decl_file, file_index);
17911
17912           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17913             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17914
17915           if (old_die->die_tag == DW_TAG_member)
17916             add_linkage_name (var_die, decl);
17917         }
17918     }
17919   else
17920     add_name_and_src_coords_attributes (var_die, decl);
17921
17922   if ((origin == NULL && !specialization_p)
17923       || (origin != NULL
17924           && !DECL_ABSTRACT (decl_or_origin)
17925           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17926                                        decl_function_context
17927                                                         (decl_or_origin))))
17928     {
17929       tree type = TREE_TYPE (decl_or_origin);
17930
17931       if (decl_by_reference_p (decl_or_origin))
17932         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17933       else
17934         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17935                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17936     }
17937
17938   if (origin == NULL && !specialization_p)
17939     {
17940       if (TREE_PUBLIC (decl))
17941         add_AT_flag (var_die, DW_AT_external, 1);
17942
17943       if (DECL_ARTIFICIAL (decl))
17944         add_AT_flag (var_die, DW_AT_artificial, 1);
17945
17946       add_accessibility_attribute (var_die, decl);
17947     }
17948
17949   if (declaration)
17950     add_AT_flag (var_die, DW_AT_declaration, 1);
17951
17952   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17953     equate_decl_number_to_die (decl, var_die);
17954
17955   if (! declaration
17956       && (! DECL_ABSTRACT (decl_or_origin)
17957           /* Local static vars are shared between all clones/inlines,
17958              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17959              already set.  */
17960           || (TREE_CODE (decl_or_origin) == VAR_DECL
17961               && TREE_STATIC (decl_or_origin)
17962               && DECL_RTL_SET_P (decl_or_origin)))
17963       /* When abstract origin already has DW_AT_location attribute, no need
17964          to add it again.  */
17965       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17966     {
17967       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17968           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17969         defer_location (decl_or_origin, var_die);
17970       else
17971         add_location_or_const_value_attribute (var_die, decl_or_origin,
17972                                                decl == NULL, DW_AT_location);
17973       add_pubname (decl_or_origin, var_die);
17974     }
17975   else
17976     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17977 }
17978
17979 /* Generate a DIE to represent a named constant.  */
17980
17981 static void
17982 gen_const_die (tree decl, dw_die_ref context_die)
17983 {
17984   dw_die_ref const_die;
17985   tree type = TREE_TYPE (decl);
17986
17987   const_die = new_die (DW_TAG_constant, context_die, decl);
17988   add_name_and_src_coords_attributes (const_die, decl);
17989   add_type_attribute (const_die, type, 1, 0, context_die);
17990   if (TREE_PUBLIC (decl))
17991     add_AT_flag (const_die, DW_AT_external, 1);
17992   if (DECL_ARTIFICIAL (decl))
17993     add_AT_flag (const_die, DW_AT_artificial, 1);
17994   tree_add_const_value_attribute_for_decl (const_die, decl);
17995 }
17996
17997 /* Generate a DIE to represent a label identifier.  */
17998
17999 static void
18000 gen_label_die (tree decl, dw_die_ref context_die)
18001 {
18002   tree origin = decl_ultimate_origin (decl);
18003   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18004   rtx insn;
18005   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18006
18007   if (origin != NULL)
18008     add_abstract_origin_attribute (lbl_die, origin);
18009   else
18010     add_name_and_src_coords_attributes (lbl_die, decl);
18011
18012   if (DECL_ABSTRACT (decl))
18013     equate_decl_number_to_die (decl, lbl_die);
18014   else
18015     {
18016       insn = DECL_RTL_IF_SET (decl);
18017
18018       /* Deleted labels are programmer specified labels which have been
18019          eliminated because of various optimizations.  We still emit them
18020          here so that it is possible to put breakpoints on them.  */
18021       if (insn
18022           && (LABEL_P (insn)
18023               || ((NOTE_P (insn)
18024                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18025         {
18026           /* When optimization is enabled (via -O) some parts of the compiler
18027              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18028              represent source-level labels which were explicitly declared by
18029              the user.  This really shouldn't be happening though, so catch
18030              it if it ever does happen.  */
18031           gcc_assert (!INSN_DELETED_P (insn));
18032
18033           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18034           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18035         }
18036       else if (insn
18037                && NOTE_P (insn)
18038                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18039                && CODE_LABEL_NUMBER (insn) != -1)
18040         {
18041           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18042           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18043         }
18044     }
18045 }
18046
18047 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18048    attributes to the DIE for a block STMT, to describe where the inlined
18049    function was called from.  This is similar to add_src_coords_attributes.  */
18050
18051 static inline void
18052 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18053 {
18054   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18055
18056   if (dwarf_version >= 3 || !dwarf_strict)
18057     {
18058       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18059       add_AT_unsigned (die, DW_AT_call_line, s.line);
18060     }
18061 }
18062
18063
18064 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18065    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18066
18067 static inline void
18068 add_high_low_attributes (tree stmt, dw_die_ref die)
18069 {
18070   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18071
18072   if (BLOCK_FRAGMENT_CHAIN (stmt)
18073       && (dwarf_version >= 3 || !dwarf_strict))
18074     {
18075       tree chain;
18076
18077       if (inlined_function_outer_scope_p (stmt))
18078         {
18079           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18080                                        BLOCK_NUMBER (stmt));
18081           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18082         }
18083
18084       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18085
18086       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18087       do
18088         {
18089           add_ranges (chain);
18090           chain = BLOCK_FRAGMENT_CHAIN (chain);
18091         }
18092       while (chain);
18093       add_ranges (NULL);
18094     }
18095   else
18096     {
18097       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18098                                    BLOCK_NUMBER (stmt));
18099       add_AT_lbl_id (die, DW_AT_low_pc, label);
18100       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18101                                    BLOCK_NUMBER (stmt));
18102       add_AT_lbl_id (die, DW_AT_high_pc, label);
18103     }
18104 }
18105
18106 /* Generate a DIE for a lexical block.  */
18107
18108 static void
18109 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18110 {
18111   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18112
18113   if (call_arg_locations)
18114     {
18115       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18116         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18117                                BLOCK_NUMBER (stmt) + 1);
18118       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18119     }
18120
18121   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18122     add_high_low_attributes (stmt, stmt_die);
18123
18124   decls_for_scope (stmt, stmt_die, depth);
18125 }
18126
18127 /* Generate a DIE for an inlined subprogram.  */
18128
18129 static void
18130 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18131 {
18132   tree decl;
18133
18134   /* The instance of function that is effectively being inlined shall not
18135      be abstract.  */
18136   gcc_assert (! BLOCK_ABSTRACT (stmt));
18137
18138   decl = block_ultimate_origin (stmt);
18139
18140   /* Emit info for the abstract instance first, if we haven't yet.  We
18141      must emit this even if the block is abstract, otherwise when we
18142      emit the block below (or elsewhere), we may end up trying to emit
18143      a die whose origin die hasn't been emitted, and crashing.  */
18144   dwarf2out_abstract_function (decl);
18145
18146   if (! BLOCK_ABSTRACT (stmt))
18147     {
18148       dw_die_ref subr_die
18149         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18150
18151       if (call_arg_locations)
18152         {
18153           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18154             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18155                                    BLOCK_NUMBER (stmt) + 1);
18156           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18157         }
18158       add_abstract_origin_attribute (subr_die, decl);
18159       if (TREE_ASM_WRITTEN (stmt))
18160         add_high_low_attributes (stmt, subr_die);
18161       add_call_src_coords_attributes (stmt, subr_die);
18162
18163       decls_for_scope (stmt, subr_die, depth);
18164       current_function_has_inlines = 1;
18165     }
18166 }
18167
18168 /* Generate a DIE for a field in a record, or structure.  */
18169
18170 static void
18171 gen_field_die (tree decl, dw_die_ref context_die)
18172 {
18173   dw_die_ref decl_die;
18174
18175   if (TREE_TYPE (decl) == error_mark_node)
18176     return;
18177
18178   decl_die = new_die (DW_TAG_member, context_die, decl);
18179   add_name_and_src_coords_attributes (decl_die, decl);
18180   add_type_attribute (decl_die, member_declared_type (decl),
18181                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18182                       context_die);
18183
18184   if (DECL_BIT_FIELD_TYPE (decl))
18185     {
18186       add_byte_size_attribute (decl_die, decl);
18187       add_bit_size_attribute (decl_die, decl);
18188       add_bit_offset_attribute (decl_die, decl);
18189     }
18190
18191   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18192     add_data_member_location_attribute (decl_die, decl);
18193
18194   if (DECL_ARTIFICIAL (decl))
18195     add_AT_flag (decl_die, DW_AT_artificial, 1);
18196
18197   add_accessibility_attribute (decl_die, decl);
18198
18199   /* Equate decl number to die, so that we can look up this decl later on.  */
18200   equate_decl_number_to_die (decl, decl_die);
18201 }
18202
18203 #if 0
18204 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18205    Use modified_type_die instead.
18206    We keep this code here just in case these types of DIEs may be needed to
18207    represent certain things in other languages (e.g. Pascal) someday.  */
18208
18209 static void
18210 gen_pointer_type_die (tree type, dw_die_ref context_die)
18211 {
18212   dw_die_ref ptr_die
18213     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18214
18215   equate_type_number_to_die (type, ptr_die);
18216   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18217   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18218 }
18219
18220 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18221    Use modified_type_die instead.
18222    We keep this code here just in case these types of DIEs may be needed to
18223    represent certain things in other languages (e.g. Pascal) someday.  */
18224
18225 static void
18226 gen_reference_type_die (tree type, dw_die_ref context_die)
18227 {
18228   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18229
18230   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18231     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18232   else
18233     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18234
18235   equate_type_number_to_die (type, ref_die);
18236   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18237   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18238 }
18239 #endif
18240
18241 /* Generate a DIE for a pointer to a member type.  */
18242
18243 static void
18244 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18245 {
18246   dw_die_ref ptr_die
18247     = new_die (DW_TAG_ptr_to_member_type,
18248                scope_die_for (type, context_die), type);
18249
18250   equate_type_number_to_die (type, ptr_die);
18251   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18252                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18253   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18254 }
18255
18256 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18257 DEF_VEC_P(dchar_p);
18258 DEF_VEC_ALLOC_P(dchar_p,heap);
18259
18260 static char *producer_string;
18261
18262 /* Return a heap allocated producer string including command line options
18263    if -grecord-gcc-switches.  */
18264
18265 static char *
18266 gen_producer_string (void)
18267 {
18268   size_t j;
18269   VEC(dchar_p, heap) *switches = NULL;
18270   const char *language_string = lang_hooks.name;
18271   char *producer, *tail;
18272   const char *p;
18273   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18274   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18275
18276   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18277     switch (save_decoded_options[j].opt_index)
18278       {
18279       case OPT_o:
18280       case OPT_d:
18281       case OPT_dumpbase:
18282       case OPT_dumpdir:
18283       case OPT_auxbase:
18284       case OPT_auxbase_strip:
18285       case OPT_quiet:
18286       case OPT_version:
18287       case OPT_v:
18288       case OPT_w:
18289       case OPT_L:
18290       case OPT_D:
18291       case OPT_I:
18292       case OPT_U:
18293       case OPT_SPECIAL_unknown:
18294       case OPT_SPECIAL_ignore:
18295       case OPT_SPECIAL_program_name:
18296       case OPT_SPECIAL_input_file:
18297       case OPT_grecord_gcc_switches:
18298       case OPT_gno_record_gcc_switches:
18299       case OPT__output_pch_:
18300       case OPT_fdiagnostics_show_location_:
18301       case OPT_fdiagnostics_show_option:
18302       case OPT_fverbose_asm:
18303       case OPT____:
18304       case OPT__sysroot_:
18305       case OPT_nostdinc:
18306       case OPT_nostdinc__:
18307         /* Ignore these.  */
18308         continue;
18309       default:
18310         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18311                              == '-');
18312         switch (save_decoded_options[j].canonical_option[0][1])
18313           {
18314           case 'M':
18315           case 'i':
18316           case 'W':
18317             continue;
18318           case 'f':
18319             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18320                          "dump", 4) == 0)
18321               continue;
18322             break;
18323           default:
18324             break;
18325           }
18326         VEC_safe_push (dchar_p, heap, switches,
18327                        save_decoded_options[j].orig_option_with_args_text);
18328         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18329         break;
18330       }
18331
18332   producer = XNEWVEC (char, plen + 1 + len + 1);
18333   tail = producer;
18334   sprintf (tail, "%s %s", language_string, version_string);
18335   tail += plen;
18336
18337   if (!dwarf_record_gcc_switches)
18338     {
18339 #ifdef MIPS_DEBUGGING_INFO
18340       /* The MIPS/SGI compilers place the 'cc' command line options in the
18341          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18342          if they do not appear in the producer string, the debugger reaches
18343          the conclusion that the object file is stripped and has no debugging
18344          information.  To get the MIPS/SGI debugger to believe that there is
18345          debugging information in the object file, we add a -g to the producer
18346          string.  */
18347       if (debug_info_level > DINFO_LEVEL_TERSE)
18348         {
18349           memcpy (tail, " -g", 3);
18350           tail += 3;
18351         }
18352 #endif
18353     }
18354
18355   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18356     {
18357       len = strlen (p);
18358       *tail = ' ';
18359       memcpy (tail + 1, p, len);
18360       tail += len + 1;
18361     }
18362
18363   *tail = '\0';
18364   VEC_free (dchar_p, heap, switches);
18365   return producer;
18366 }
18367
18368 /* Generate the DIE for the compilation unit.  */
18369
18370 static dw_die_ref
18371 gen_compile_unit_die (const char *filename)
18372 {
18373   dw_die_ref die;
18374   const char *language_string = lang_hooks.name;
18375   int language;
18376
18377   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18378
18379   if (filename)
18380     {
18381       add_name_attribute (die, filename);
18382       /* Don't add cwd for <built-in>.  */
18383       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18384         add_comp_dir_attribute (die);
18385     }
18386
18387   if (producer_string == NULL)
18388     producer_string = gen_producer_string ();
18389   add_AT_string (die, DW_AT_producer, producer_string);
18390
18391   /* If our producer is LTO try to figure out a common language to use
18392      from the global list of translation units.  */
18393   if (strcmp (language_string, "GNU GIMPLE") == 0)
18394     {
18395       unsigned i;
18396       tree t;
18397       const char *common_lang = NULL;
18398
18399       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18400         {
18401           if (!TRANSLATION_UNIT_LANGUAGE (t))
18402             continue;
18403           if (!common_lang)
18404             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18405           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18406             ;
18407           else if (strncmp (common_lang, "GNU C", 5) == 0
18408                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18409             /* Mixing C and C++ is ok, use C++ in that case.  */
18410             common_lang = "GNU C++";
18411           else
18412             {
18413               /* Fall back to C.  */
18414               common_lang = NULL;
18415               break;
18416             }
18417         }
18418
18419       if (common_lang)
18420         language_string = common_lang;
18421     }
18422
18423   language = DW_LANG_C89;
18424   if (strcmp (language_string, "GNU C++") == 0)
18425     language = DW_LANG_C_plus_plus;
18426   else if (strcmp (language_string, "GNU F77") == 0)
18427     language = DW_LANG_Fortran77;
18428   else if (strcmp (language_string, "GNU Pascal") == 0)
18429     language = DW_LANG_Pascal83;
18430   else if (dwarf_version >= 3 || !dwarf_strict)
18431     {
18432       if (strcmp (language_string, "GNU Ada") == 0)
18433         language = DW_LANG_Ada95;
18434       else if (strcmp (language_string, "GNU Fortran") == 0)
18435         language = DW_LANG_Fortran95;
18436       else if (strcmp (language_string, "GNU Java") == 0)
18437         language = DW_LANG_Java;
18438       else if (strcmp (language_string, "GNU Objective-C") == 0)
18439         language = DW_LANG_ObjC;
18440       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18441         language = DW_LANG_ObjC_plus_plus;
18442       else if (dwarf_version >= 5 || !dwarf_strict)
18443         {
18444           if (strcmp (language_string, "GNU Go") == 0)
18445             language = DW_LANG_Go;
18446         }
18447     }
18448
18449   add_AT_unsigned (die, DW_AT_language, language);
18450
18451   switch (language)
18452     {
18453     case DW_LANG_Fortran77:
18454     case DW_LANG_Fortran90:
18455     case DW_LANG_Fortran95:
18456       /* Fortran has case insensitive identifiers and the front-end
18457          lowercases everything.  */
18458       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18459       break;
18460     default:
18461       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18462       break;
18463     }
18464   return die;
18465 }
18466
18467 /* Generate the DIE for a base class.  */
18468
18469 static void
18470 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18471 {
18472   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18473
18474   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18475   add_data_member_location_attribute (die, binfo);
18476
18477   if (BINFO_VIRTUAL_P (binfo))
18478     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18479
18480   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18481      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18482      the default has always been DW_ACCESS_private.  */
18483   if (access == access_public_node)
18484     {
18485       if (dwarf_version == 2
18486           || context_die->die_tag == DW_TAG_class_type)
18487       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18488     }
18489   else if (access == access_protected_node)
18490     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18491   else if (dwarf_version > 2
18492            && context_die->die_tag != DW_TAG_class_type)
18493     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18494 }
18495
18496 /* Generate a DIE for a class member.  */
18497
18498 static void
18499 gen_member_die (tree type, dw_die_ref context_die)
18500 {
18501   tree member;
18502   tree binfo = TYPE_BINFO (type);
18503   dw_die_ref child;
18504
18505   /* If this is not an incomplete type, output descriptions of each of its
18506      members. Note that as we output the DIEs necessary to represent the
18507      members of this record or union type, we will also be trying to output
18508      DIEs to represent the *types* of those members. However the `type'
18509      function (above) will specifically avoid generating type DIEs for member
18510      types *within* the list of member DIEs for this (containing) type except
18511      for those types (of members) which are explicitly marked as also being
18512      members of this (containing) type themselves.  The g++ front- end can
18513      force any given type to be treated as a member of some other (containing)
18514      type by setting the TYPE_CONTEXT of the given (member) type to point to
18515      the TREE node representing the appropriate (containing) type.  */
18516
18517   /* First output info about the base classes.  */
18518   if (binfo)
18519     {
18520       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18521       int i;
18522       tree base;
18523
18524       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18525         gen_inheritance_die (base,
18526                              (accesses ? VEC_index (tree, accesses, i)
18527                               : access_public_node), context_die);
18528     }
18529
18530   /* Now output info about the data members and type members.  */
18531   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18532     {
18533       /* If we thought we were generating minimal debug info for TYPE
18534          and then changed our minds, some of the member declarations
18535          may have already been defined.  Don't define them again, but
18536          do put them in the right order.  */
18537
18538       child = lookup_decl_die (member);
18539       if (child)
18540         splice_child_die (context_die, child);
18541       else
18542         gen_decl_die (member, NULL, context_die);
18543     }
18544
18545   /* Now output info about the function members (if any).  */
18546   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18547     {
18548       /* Don't include clones in the member list.  */
18549       if (DECL_ABSTRACT_ORIGIN (member))
18550         continue;
18551
18552       child = lookup_decl_die (member);
18553       if (child)
18554         splice_child_die (context_die, child);
18555       else
18556         gen_decl_die (member, NULL, context_die);
18557     }
18558 }
18559
18560 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18561    is set, we pretend that the type was never defined, so we only get the
18562    member DIEs needed by later specification DIEs.  */
18563
18564 static void
18565 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18566                                 enum debug_info_usage usage)
18567 {
18568   dw_die_ref type_die = lookup_type_die (type);
18569   dw_die_ref scope_die = 0;
18570   int nested = 0;
18571   int complete = (TYPE_SIZE (type)
18572                   && (! TYPE_STUB_DECL (type)
18573                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18574   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18575   complete = complete && should_emit_struct_debug (type, usage);
18576
18577   if (type_die && ! complete)
18578     return;
18579
18580   if (TYPE_CONTEXT (type) != NULL_TREE
18581       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18582           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18583     nested = 1;
18584
18585   scope_die = scope_die_for (type, context_die);
18586
18587   if (! type_die || (nested && is_cu_die (scope_die)))
18588     /* First occurrence of type or toplevel definition of nested class.  */
18589     {
18590       dw_die_ref old_die = type_die;
18591
18592       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18593                           ? record_type_tag (type) : DW_TAG_union_type,
18594                           scope_die, type);
18595       equate_type_number_to_die (type, type_die);
18596       if (old_die)
18597         add_AT_specification (type_die, old_die);
18598       else
18599         {
18600           add_name_attribute (type_die, type_tag (type));
18601           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18602           if (TYPE_ARTIFICIAL (type))
18603             add_AT_flag (type_die, DW_AT_artificial, 1);
18604         }
18605     }
18606   else
18607     remove_AT (type_die, DW_AT_declaration);
18608
18609   /* Generate child dies for template paramaters.  */
18610   if (debug_info_level > DINFO_LEVEL_TERSE
18611       && COMPLETE_TYPE_P (type))
18612     schedule_generic_params_dies_gen (type);
18613
18614   /* If this type has been completed, then give it a byte_size attribute and
18615      then give a list of members.  */
18616   if (complete && !ns_decl)
18617     {
18618       /* Prevent infinite recursion in cases where the type of some member of
18619          this type is expressed in terms of this type itself.  */
18620       TREE_ASM_WRITTEN (type) = 1;
18621       add_byte_size_attribute (type_die, type);
18622       if (TYPE_STUB_DECL (type) != NULL_TREE)
18623         {
18624           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18625           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18626         }
18627
18628       /* If the first reference to this type was as the return type of an
18629          inline function, then it may not have a parent.  Fix this now.  */
18630       if (type_die->die_parent == NULL)
18631         add_child_die (scope_die, type_die);
18632
18633       push_decl_scope (type);
18634       gen_member_die (type, type_die);
18635       pop_decl_scope ();
18636
18637       /* GNU extension: Record what type our vtable lives in.  */
18638       if (TYPE_VFIELD (type))
18639         {
18640           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18641
18642           gen_type_die (vtype, context_die);
18643           add_AT_die_ref (type_die, DW_AT_containing_type,
18644                           lookup_type_die (vtype));
18645         }
18646     }
18647   else
18648     {
18649       add_AT_flag (type_die, DW_AT_declaration, 1);
18650
18651       /* We don't need to do this for function-local types.  */
18652       if (TYPE_STUB_DECL (type)
18653           && ! decl_function_context (TYPE_STUB_DECL (type)))
18654         VEC_safe_push (tree, gc, incomplete_types, type);
18655     }
18656
18657   if (get_AT (type_die, DW_AT_name))
18658     add_pubtype (type, type_die);
18659 }
18660
18661 /* Generate a DIE for a subroutine _type_.  */
18662
18663 static void
18664 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18665 {
18666   tree return_type = TREE_TYPE (type);
18667   dw_die_ref subr_die
18668     = new_die (DW_TAG_subroutine_type,
18669                scope_die_for (type, context_die), type);
18670
18671   equate_type_number_to_die (type, subr_die);
18672   add_prototyped_attribute (subr_die, type);
18673   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18674   gen_formal_types_die (type, subr_die);
18675
18676   if (get_AT (subr_die, DW_AT_name))
18677     add_pubtype (type, subr_die);
18678 }
18679
18680 /* Generate a DIE for a type definition.  */
18681
18682 static void
18683 gen_typedef_die (tree decl, dw_die_ref context_die)
18684 {
18685   dw_die_ref type_die;
18686   tree origin;
18687
18688   if (TREE_ASM_WRITTEN (decl))
18689     return;
18690
18691   TREE_ASM_WRITTEN (decl) = 1;
18692   type_die = new_die (DW_TAG_typedef, context_die, decl);
18693   origin = decl_ultimate_origin (decl);
18694   if (origin != NULL)
18695     add_abstract_origin_attribute (type_die, origin);
18696   else
18697     {
18698       tree type;
18699
18700       add_name_and_src_coords_attributes (type_die, decl);
18701       if (DECL_ORIGINAL_TYPE (decl))
18702         {
18703           type = DECL_ORIGINAL_TYPE (decl);
18704
18705           gcc_assert (type != TREE_TYPE (decl));
18706           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18707         }
18708       else
18709         {
18710           type = TREE_TYPE (decl);
18711
18712           if (is_naming_typedef_decl (TYPE_NAME (type)))
18713             {
18714               /* Here, we are in the case of decl being a typedef naming
18715                  an anonymous type, e.g:
18716                      typedef struct {...} foo;
18717                  In that case TREE_TYPE (decl) is not a typedef variant
18718                  type and TYPE_NAME of the anonymous type is set to the
18719                  TYPE_DECL of the typedef. This construct is emitted by
18720                  the C++ FE.
18721
18722                  TYPE is the anonymous struct named by the typedef
18723                  DECL. As we need the DW_AT_type attribute of the
18724                  DW_TAG_typedef to point to the DIE of TYPE, let's
18725                  generate that DIE right away. add_type_attribute
18726                  called below will then pick (via lookup_type_die) that
18727                  anonymous struct DIE.  */
18728               if (!TREE_ASM_WRITTEN (type))
18729                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18730
18731               /* This is a GNU Extension.  We are adding a
18732                  DW_AT_linkage_name attribute to the DIE of the
18733                  anonymous struct TYPE.  The value of that attribute
18734                  is the name of the typedef decl naming the anonymous
18735                  struct.  This greatly eases the work of consumers of
18736                  this debug info.  */
18737               add_linkage_attr (lookup_type_die (type), decl);
18738             }
18739         }
18740
18741       add_type_attribute (type_die, type, TREE_READONLY (decl),
18742                           TREE_THIS_VOLATILE (decl), context_die);
18743
18744       if (is_naming_typedef_decl (decl))
18745         /* We want that all subsequent calls to lookup_type_die with
18746            TYPE in argument yield the DW_TAG_typedef we have just
18747            created.  */
18748         equate_type_number_to_die (type, type_die);
18749
18750       add_accessibility_attribute (type_die, decl);
18751     }
18752
18753   if (DECL_ABSTRACT (decl))
18754     equate_decl_number_to_die (decl, type_die);
18755
18756   if (get_AT (type_die, DW_AT_name))
18757     add_pubtype (decl, type_die);
18758 }
18759
18760 /* Generate a DIE for a struct, class, enum or union type.  */
18761
18762 static void
18763 gen_tagged_type_die (tree type,
18764                      dw_die_ref context_die,
18765                      enum debug_info_usage usage)
18766 {
18767   int need_pop;
18768
18769   if (type == NULL_TREE
18770       || !is_tagged_type (type))
18771     return;
18772
18773   /* If this is a nested type whose containing class hasn't been written
18774      out yet, writing it out will cover this one, too.  This does not apply
18775      to instantiations of member class templates; they need to be added to
18776      the containing class as they are generated.  FIXME: This hurts the
18777      idea of combining type decls from multiple TUs, since we can't predict
18778      what set of template instantiations we'll get.  */
18779   if (TYPE_CONTEXT (type)
18780       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18781       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18782     {
18783       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18784
18785       if (TREE_ASM_WRITTEN (type))
18786         return;
18787
18788       /* If that failed, attach ourselves to the stub.  */
18789       push_decl_scope (TYPE_CONTEXT (type));
18790       context_die = lookup_type_die (TYPE_CONTEXT (type));
18791       need_pop = 1;
18792     }
18793   else if (TYPE_CONTEXT (type) != NULL_TREE
18794            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18795     {
18796       /* If this type is local to a function that hasn't been written
18797          out yet, use a NULL context for now; it will be fixed up in
18798          decls_for_scope.  */
18799       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18800       /* A declaration DIE doesn't count; nested types need to go in the
18801          specification.  */
18802       if (context_die && is_declaration_die (context_die))
18803         context_die = NULL;
18804       need_pop = 0;
18805     }
18806   else
18807     {
18808       context_die = declare_in_namespace (type, context_die);
18809       need_pop = 0;
18810     }
18811
18812   if (TREE_CODE (type) == ENUMERAL_TYPE)
18813     {
18814       /* This might have been written out by the call to
18815          declare_in_namespace.  */
18816       if (!TREE_ASM_WRITTEN (type))
18817         gen_enumeration_type_die (type, context_die);
18818     }
18819   else
18820     gen_struct_or_union_type_die (type, context_die, usage);
18821
18822   if (need_pop)
18823     pop_decl_scope ();
18824
18825   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18826      it up if it is ever completed.  gen_*_type_die will set it for us
18827      when appropriate.  */
18828 }
18829
18830 /* Generate a type description DIE.  */
18831
18832 static void
18833 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18834                          enum debug_info_usage usage)
18835 {
18836   struct array_descr_info info;
18837
18838   if (type == NULL_TREE || type == error_mark_node)
18839     return;
18840
18841   if (TYPE_NAME (type) != NULL_TREE
18842       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18843       && is_redundant_typedef (TYPE_NAME (type))
18844       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18845     /* The DECL of this type is a typedef we don't want to emit debug
18846        info for but we want debug info for its underlying typedef.
18847        This can happen for e.g, the injected-class-name of a C++
18848        type.  */
18849     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18850
18851   /* If TYPE is a typedef type variant, let's generate debug info
18852      for the parent typedef which TYPE is a type of.  */
18853   if (typedef_variant_p (type))
18854     {
18855       if (TREE_ASM_WRITTEN (type))
18856         return;
18857
18858       /* Prevent broken recursion; we can't hand off to the same type.  */
18859       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18860
18861       /* Use the DIE of the containing namespace as the parent DIE of
18862          the type description DIE we want to generate.  */
18863       if (DECL_FILE_SCOPE_P (TYPE_NAME (type))
18864           || (DECL_CONTEXT (TYPE_NAME (type))
18865               && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL))
18866         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18867
18868       TREE_ASM_WRITTEN (type) = 1;
18869
18870       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18871       return;
18872     }
18873
18874   /* If type is an anonymous tagged type named by a typedef, let's
18875      generate debug info for the typedef.  */
18876   if (is_naming_typedef_decl (TYPE_NAME (type)))
18877     {
18878       /* Use the DIE of the containing namespace as the parent DIE of
18879          the type description DIE we want to generate.  */
18880       if (DECL_CONTEXT (TYPE_NAME (type))
18881           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18882         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18883       
18884       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18885       return;
18886     }
18887
18888   /* If this is an array type with hidden descriptor, handle it first.  */
18889   if (!TREE_ASM_WRITTEN (type)
18890       && lang_hooks.types.get_array_descr_info
18891       && lang_hooks.types.get_array_descr_info (type, &info)
18892       && (dwarf_version >= 3 || !dwarf_strict))
18893     {
18894       gen_descr_array_type_die (type, &info, context_die);
18895       TREE_ASM_WRITTEN (type) = 1;
18896       return;
18897     }
18898
18899   /* We are going to output a DIE to represent the unqualified version
18900      of this type (i.e. without any const or volatile qualifiers) so
18901      get the main variant (i.e. the unqualified version) of this type
18902      now.  (Vectors are special because the debugging info is in the
18903      cloned type itself).  */
18904   if (TREE_CODE (type) != VECTOR_TYPE)
18905     type = type_main_variant (type);
18906
18907   if (TREE_ASM_WRITTEN (type))
18908     return;
18909
18910   switch (TREE_CODE (type))
18911     {
18912     case ERROR_MARK:
18913       break;
18914
18915     case POINTER_TYPE:
18916     case REFERENCE_TYPE:
18917       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18918          ensures that the gen_type_die recursion will terminate even if the
18919          type is recursive.  Recursive types are possible in Ada.  */
18920       /* ??? We could perhaps do this for all types before the switch
18921          statement.  */
18922       TREE_ASM_WRITTEN (type) = 1;
18923
18924       /* For these types, all that is required is that we output a DIE (or a
18925          set of DIEs) to represent the "basis" type.  */
18926       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18927                                 DINFO_USAGE_IND_USE);
18928       break;
18929
18930     case OFFSET_TYPE:
18931       /* This code is used for C++ pointer-to-data-member types.
18932          Output a description of the relevant class type.  */
18933       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18934                                         DINFO_USAGE_IND_USE);
18935
18936       /* Output a description of the type of the object pointed to.  */
18937       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18938                                         DINFO_USAGE_IND_USE);
18939
18940       /* Now output a DIE to represent this pointer-to-data-member type
18941          itself.  */
18942       gen_ptr_to_mbr_type_die (type, context_die);
18943       break;
18944
18945     case FUNCTION_TYPE:
18946       /* Force out return type (in case it wasn't forced out already).  */
18947       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18948                                         DINFO_USAGE_DIR_USE);
18949       gen_subroutine_type_die (type, context_die);
18950       break;
18951
18952     case METHOD_TYPE:
18953       /* Force out return type (in case it wasn't forced out already).  */
18954       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18955                                         DINFO_USAGE_DIR_USE);
18956       gen_subroutine_type_die (type, context_die);
18957       break;
18958
18959     case ARRAY_TYPE:
18960       gen_array_type_die (type, context_die);
18961       break;
18962
18963     case VECTOR_TYPE:
18964       gen_array_type_die (type, context_die);
18965       break;
18966
18967     case ENUMERAL_TYPE:
18968     case RECORD_TYPE:
18969     case UNION_TYPE:
18970     case QUAL_UNION_TYPE:
18971       gen_tagged_type_die (type, context_die, usage);
18972       return;
18973
18974     case VOID_TYPE:
18975     case INTEGER_TYPE:
18976     case REAL_TYPE:
18977     case FIXED_POINT_TYPE:
18978     case COMPLEX_TYPE:
18979     case BOOLEAN_TYPE:
18980       /* No DIEs needed for fundamental types.  */
18981       break;
18982
18983     case NULLPTR_TYPE:
18984     case LANG_TYPE:
18985       /* Just use DW_TAG_unspecified_type.  */
18986       {
18987         dw_die_ref type_die = lookup_type_die (type);
18988         if (type_die == NULL)
18989           {
18990             tree name = TYPE_NAME (type);
18991             if (TREE_CODE (name) == TYPE_DECL)
18992               name = DECL_NAME (name);
18993             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18994             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18995             equate_type_number_to_die (type, type_die);
18996           }
18997       }
18998       break;
18999
19000     default:
19001       gcc_unreachable ();
19002     }
19003
19004   TREE_ASM_WRITTEN (type) = 1;
19005 }
19006
19007 static void
19008 gen_type_die (tree type, dw_die_ref context_die)
19009 {
19010   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19011 }
19012
19013 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19014    things which are local to the given block.  */
19015
19016 static void
19017 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19018 {
19019   int must_output_die = 0;
19020   bool inlined_func;
19021
19022   /* Ignore blocks that are NULL.  */
19023   if (stmt == NULL_TREE)
19024     return;
19025
19026   inlined_func = inlined_function_outer_scope_p (stmt);
19027
19028   /* If the block is one fragment of a non-contiguous block, do not
19029      process the variables, since they will have been done by the
19030      origin block.  Do process subblocks.  */
19031   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19032     {
19033       tree sub;
19034
19035       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19036         gen_block_die (sub, context_die, depth + 1);
19037
19038       return;
19039     }
19040
19041   /* Determine if we need to output any Dwarf DIEs at all to represent this
19042      block.  */
19043   if (inlined_func)
19044     /* The outer scopes for inlinings *must* always be represented.  We
19045        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19046     must_output_die = 1;
19047   else
19048     {
19049       /* Determine if this block directly contains any "significant"
19050          local declarations which we will need to output DIEs for.  */
19051       if (debug_info_level > DINFO_LEVEL_TERSE)
19052         /* We are not in terse mode so *any* local declaration counts
19053            as being a "significant" one.  */
19054         must_output_die = ((BLOCK_VARS (stmt) != NULL
19055                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19056                            && (TREE_USED (stmt)
19057                                || TREE_ASM_WRITTEN (stmt)
19058                                || BLOCK_ABSTRACT (stmt)));
19059       else if ((TREE_USED (stmt)
19060                 || TREE_ASM_WRITTEN (stmt)
19061                 || BLOCK_ABSTRACT (stmt))
19062                && !dwarf2out_ignore_block (stmt))
19063         must_output_die = 1;
19064     }
19065
19066   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19067      DIE for any block which contains no significant local declarations at
19068      all.  Rather, in such cases we just call `decls_for_scope' so that any
19069      needed Dwarf info for any sub-blocks will get properly generated. Note
19070      that in terse mode, our definition of what constitutes a "significant"
19071      local declaration gets restricted to include only inlined function
19072      instances and local (nested) function definitions.  */
19073   if (must_output_die)
19074     {
19075       if (inlined_func)
19076         {
19077           /* If STMT block is abstract, that means we have been called
19078              indirectly from dwarf2out_abstract_function.
19079              That function rightfully marks the descendent blocks (of
19080              the abstract function it is dealing with) as being abstract,
19081              precisely to prevent us from emitting any
19082              DW_TAG_inlined_subroutine DIE as a descendent
19083              of an abstract function instance. So in that case, we should
19084              not call gen_inlined_subroutine_die.
19085
19086              Later though, when cgraph asks dwarf2out to emit info
19087              for the concrete instance of the function decl into which
19088              the concrete instance of STMT got inlined, the later will lead
19089              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19090           if (! BLOCK_ABSTRACT (stmt))
19091             gen_inlined_subroutine_die (stmt, context_die, depth);
19092         }
19093       else
19094         gen_lexical_block_die (stmt, context_die, depth);
19095     }
19096   else
19097     decls_for_scope (stmt, context_die, depth);
19098 }
19099
19100 /* Process variable DECL (or variable with origin ORIGIN) within
19101    block STMT and add it to CONTEXT_DIE.  */
19102 static void
19103 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19104 {
19105   dw_die_ref die;
19106   tree decl_or_origin = decl ? decl : origin;
19107
19108   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19109     die = lookup_decl_die (decl_or_origin);
19110   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19111            && TYPE_DECL_IS_STUB (decl_or_origin))
19112     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19113   else
19114     die = NULL;
19115
19116   if (die != NULL && die->die_parent == NULL)
19117     add_child_die (context_die, die);
19118   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19119     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19120                                          stmt, context_die);
19121   else
19122     gen_decl_die (decl, origin, context_die);
19123 }
19124
19125 /* Generate all of the decls declared within a given scope and (recursively)
19126    all of its sub-blocks.  */
19127
19128 static void
19129 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19130 {
19131   tree decl;
19132   unsigned int i;
19133   tree subblocks;
19134
19135   /* Ignore NULL blocks.  */
19136   if (stmt == NULL_TREE)
19137     return;
19138
19139   /* Output the DIEs to represent all of the data objects and typedefs
19140      declared directly within this block but not within any nested
19141      sub-blocks.  Also, nested function and tag DIEs have been
19142      generated with a parent of NULL; fix that up now.  */
19143   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19144     process_scope_var (stmt, decl, NULL_TREE, context_die);
19145   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19146     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19147                        context_die);
19148
19149   /* If we're at -g1, we're not interested in subblocks.  */
19150   if (debug_info_level <= DINFO_LEVEL_TERSE)
19151     return;
19152
19153   /* Output the DIEs to represent all sub-blocks (and the items declared
19154      therein) of this block.  */
19155   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19156        subblocks != NULL;
19157        subblocks = BLOCK_CHAIN (subblocks))
19158     gen_block_die (subblocks, context_die, depth + 1);
19159 }
19160
19161 /* Is this a typedef we can avoid emitting?  */
19162
19163 static inline int
19164 is_redundant_typedef (const_tree decl)
19165 {
19166   if (TYPE_DECL_IS_STUB (decl))
19167     return 1;
19168
19169   if (DECL_ARTIFICIAL (decl)
19170       && DECL_CONTEXT (decl)
19171       && is_tagged_type (DECL_CONTEXT (decl))
19172       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19173       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19174     /* Also ignore the artificial member typedef for the class name.  */
19175     return 1;
19176
19177   return 0;
19178 }
19179
19180 /* Return TRUE if TYPE is a typedef that names a type for linkage
19181    purposes. This kind of typedefs is produced by the C++ FE for
19182    constructs like:
19183
19184    typedef struct {...} foo;
19185
19186    In that case, there is no typedef variant type produced for foo.
19187    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19188    struct type.  */
19189
19190 static bool
19191 is_naming_typedef_decl (const_tree decl)
19192 {
19193   if (decl == NULL_TREE
19194       || TREE_CODE (decl) != TYPE_DECL
19195       || !is_tagged_type (TREE_TYPE (decl))
19196       || DECL_IS_BUILTIN (decl)
19197       || is_redundant_typedef (decl)
19198       /* It looks like Ada produces TYPE_DECLs that are very similar
19199          to C++ naming typedefs but that have different
19200          semantics. Let's be specific to c++ for now.  */
19201       || !is_cxx ())
19202     return FALSE;
19203
19204   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19205           && TYPE_NAME (TREE_TYPE (decl)) == decl
19206           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19207               != TYPE_NAME (TREE_TYPE (decl))));
19208 }
19209
19210 /* Returns the DIE for a context.  */
19211
19212 static inline dw_die_ref
19213 get_context_die (tree context)
19214 {
19215   if (context)
19216     {
19217       /* Find die that represents this context.  */
19218       if (TYPE_P (context))
19219         {
19220           context = TYPE_MAIN_VARIANT (context);
19221           return strip_naming_typedef (context, force_type_die (context));
19222         }
19223       else
19224         return force_decl_die (context);
19225     }
19226   return comp_unit_die ();
19227 }
19228
19229 /* Returns the DIE for decl.  A DIE will always be returned.  */
19230
19231 static dw_die_ref
19232 force_decl_die (tree decl)
19233 {
19234   dw_die_ref decl_die;
19235   unsigned saved_external_flag;
19236   tree save_fn = NULL_TREE;
19237   decl_die = lookup_decl_die (decl);
19238   if (!decl_die)
19239     {
19240       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19241
19242       decl_die = lookup_decl_die (decl);
19243       if (decl_die)
19244         return decl_die;
19245
19246       switch (TREE_CODE (decl))
19247         {
19248         case FUNCTION_DECL:
19249           /* Clear current_function_decl, so that gen_subprogram_die thinks
19250              that this is a declaration. At this point, we just want to force
19251              declaration die.  */
19252           save_fn = current_function_decl;
19253           current_function_decl = NULL_TREE;
19254           gen_subprogram_die (decl, context_die);
19255           current_function_decl = save_fn;
19256           break;
19257
19258         case VAR_DECL:
19259           /* Set external flag to force declaration die. Restore it after
19260            gen_decl_die() call.  */
19261           saved_external_flag = DECL_EXTERNAL (decl);
19262           DECL_EXTERNAL (decl) = 1;
19263           gen_decl_die (decl, NULL, context_die);
19264           DECL_EXTERNAL (decl) = saved_external_flag;
19265           break;
19266
19267         case NAMESPACE_DECL:
19268           if (dwarf_version >= 3 || !dwarf_strict)
19269             dwarf2out_decl (decl);
19270           else
19271             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19272             decl_die = comp_unit_die ();
19273           break;
19274
19275         case TRANSLATION_UNIT_DECL:
19276           decl_die = comp_unit_die ();
19277           break;
19278
19279         default:
19280           gcc_unreachable ();
19281         }
19282
19283       /* We should be able to find the DIE now.  */
19284       if (!decl_die)
19285         decl_die = lookup_decl_die (decl);
19286       gcc_assert (decl_die);
19287     }
19288
19289   return decl_die;
19290 }
19291
19292 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19293    always returned.  */
19294
19295 static dw_die_ref
19296 force_type_die (tree type)
19297 {
19298   dw_die_ref type_die;
19299
19300   type_die = lookup_type_die (type);
19301   if (!type_die)
19302     {
19303       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19304
19305       type_die = modified_type_die (type, TYPE_READONLY (type),
19306                                     TYPE_VOLATILE (type), context_die);
19307       gcc_assert (type_die);
19308     }
19309   return type_die;
19310 }
19311
19312 /* Force out any required namespaces to be able to output DECL,
19313    and return the new context_die for it, if it's changed.  */
19314
19315 static dw_die_ref
19316 setup_namespace_context (tree thing, dw_die_ref context_die)
19317 {
19318   tree context = (DECL_P (thing)
19319                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19320   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19321     /* Force out the namespace.  */
19322     context_die = force_decl_die (context);
19323
19324   return context_die;
19325 }
19326
19327 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19328    type) within its namespace, if appropriate.
19329
19330    For compatibility with older debuggers, namespace DIEs only contain
19331    declarations; all definitions are emitted at CU scope.  */
19332
19333 static dw_die_ref
19334 declare_in_namespace (tree thing, dw_die_ref context_die)
19335 {
19336   dw_die_ref ns_context;
19337
19338   if (debug_info_level <= DINFO_LEVEL_TERSE)
19339     return context_die;
19340
19341   /* If this decl is from an inlined function, then don't try to emit it in its
19342      namespace, as we will get confused.  It would have already been emitted
19343      when the abstract instance of the inline function was emitted anyways.  */
19344   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19345     return context_die;
19346
19347   ns_context = setup_namespace_context (thing, context_die);
19348
19349   if (ns_context != context_die)
19350     {
19351       if (is_fortran ())
19352         return ns_context;
19353       if (DECL_P (thing))
19354         gen_decl_die (thing, NULL, ns_context);
19355       else
19356         gen_type_die (thing, ns_context);
19357     }
19358   return context_die;
19359 }
19360
19361 /* Generate a DIE for a namespace or namespace alias.  */
19362
19363 static void
19364 gen_namespace_die (tree decl, dw_die_ref context_die)
19365 {
19366   dw_die_ref namespace_die;
19367
19368   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19369      they are an alias of.  */
19370   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19371     {
19372       /* Output a real namespace or module.  */
19373       context_die = setup_namespace_context (decl, comp_unit_die ());
19374       namespace_die = new_die (is_fortran ()
19375                                ? DW_TAG_module : DW_TAG_namespace,
19376                                context_die, decl);
19377       /* For Fortran modules defined in different CU don't add src coords.  */
19378       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19379         {
19380           const char *name = dwarf2_name (decl, 0);
19381           if (name)
19382             add_name_attribute (namespace_die, name);
19383         }
19384       else
19385         add_name_and_src_coords_attributes (namespace_die, decl);
19386       if (DECL_EXTERNAL (decl))
19387         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19388       equate_decl_number_to_die (decl, namespace_die);
19389     }
19390   else
19391     {
19392       /* Output a namespace alias.  */
19393
19394       /* Force out the namespace we are an alias of, if necessary.  */
19395       dw_die_ref origin_die
19396         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19397
19398       if (DECL_FILE_SCOPE_P (decl)
19399           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19400         context_die = setup_namespace_context (decl, comp_unit_die ());
19401       /* Now create the namespace alias DIE.  */
19402       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19403       add_name_and_src_coords_attributes (namespace_die, decl);
19404       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19405       equate_decl_number_to_die (decl, namespace_die);
19406     }
19407 }
19408
19409 /* Generate Dwarf debug information for a decl described by DECL.
19410    The return value is currently only meaningful for PARM_DECLs,
19411    for all other decls it returns NULL.  */
19412
19413 static dw_die_ref
19414 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19415 {
19416   tree decl_or_origin = decl ? decl : origin;
19417   tree class_origin = NULL, ultimate_origin;
19418
19419   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19420     return NULL;
19421
19422   switch (TREE_CODE (decl_or_origin))
19423     {
19424     case ERROR_MARK:
19425       break;
19426
19427     case CONST_DECL:
19428       if (!is_fortran () && !is_ada ())
19429         {
19430           /* The individual enumerators of an enum type get output when we output
19431              the Dwarf representation of the relevant enum type itself.  */
19432           break;
19433         }
19434
19435       /* Emit its type.  */
19436       gen_type_die (TREE_TYPE (decl), context_die);
19437
19438       /* And its containing namespace.  */
19439       context_die = declare_in_namespace (decl, context_die);
19440
19441       gen_const_die (decl, context_die);
19442       break;
19443
19444     case FUNCTION_DECL:
19445       /* Don't output any DIEs to represent mere function declarations,
19446          unless they are class members or explicit block externs.  */
19447       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19448           && DECL_FILE_SCOPE_P (decl_or_origin)
19449           && (current_function_decl == NULL_TREE
19450               || DECL_ARTIFICIAL (decl_or_origin)))
19451         break;
19452
19453 #if 0
19454       /* FIXME */
19455       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19456          on local redeclarations of global functions.  That seems broken.  */
19457       if (current_function_decl != decl)
19458         /* This is only a declaration.  */;
19459 #endif
19460
19461       /* If we're emitting a clone, emit info for the abstract instance.  */
19462       if (origin || DECL_ORIGIN (decl) != decl)
19463         dwarf2out_abstract_function (origin
19464                                      ? DECL_ORIGIN (origin)
19465                                      : DECL_ABSTRACT_ORIGIN (decl));
19466
19467       /* If we're emitting an out-of-line copy of an inline function,
19468          emit info for the abstract instance and set up to refer to it.  */
19469       else if (cgraph_function_possibly_inlined_p (decl)
19470                && ! DECL_ABSTRACT (decl)
19471                && ! class_or_namespace_scope_p (context_die)
19472                /* dwarf2out_abstract_function won't emit a die if this is just
19473                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19474                   that case, because that works only if we have a die.  */
19475                && DECL_INITIAL (decl) != NULL_TREE)
19476         {
19477           dwarf2out_abstract_function (decl);
19478           set_decl_origin_self (decl);
19479         }
19480
19481       /* Otherwise we're emitting the primary DIE for this decl.  */
19482       else if (debug_info_level > DINFO_LEVEL_TERSE)
19483         {
19484           /* Before we describe the FUNCTION_DECL itself, make sure that we
19485              have its containing type.  */
19486           if (!origin)
19487             origin = decl_class_context (decl);
19488           if (origin != NULL_TREE)
19489             gen_type_die (origin, context_die);
19490
19491           /* And its return type.  */
19492           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19493
19494           /* And its virtual context.  */
19495           if (DECL_VINDEX (decl) != NULL_TREE)
19496             gen_type_die (DECL_CONTEXT (decl), context_die);
19497
19498           /* Make sure we have a member DIE for decl.  */
19499           if (origin != NULL_TREE)
19500             gen_type_die_for_member (origin, decl, context_die);
19501
19502           /* And its containing namespace.  */
19503           context_die = declare_in_namespace (decl, context_die);
19504         }
19505
19506       /* Now output a DIE to represent the function itself.  */
19507       if (decl)
19508         gen_subprogram_die (decl, context_die);
19509       break;
19510
19511     case TYPE_DECL:
19512       /* If we are in terse mode, don't generate any DIEs to represent any
19513          actual typedefs.  */
19514       if (debug_info_level <= DINFO_LEVEL_TERSE)
19515         break;
19516
19517       /* In the special case of a TYPE_DECL node representing the declaration
19518          of some type tag, if the given TYPE_DECL is marked as having been
19519          instantiated from some other (original) TYPE_DECL node (e.g. one which
19520          was generated within the original definition of an inline function) we
19521          used to generate a special (abbreviated) DW_TAG_structure_type,
19522          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19523          should be actually referencing those DIEs, as variable DIEs with that
19524          type would be emitted already in the abstract origin, so it was always
19525          removed during unused type prunning.  Don't add anything in this
19526          case.  */
19527       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19528         break;
19529
19530       if (is_redundant_typedef (decl))
19531         gen_type_die (TREE_TYPE (decl), context_die);
19532       else
19533         /* Output a DIE to represent the typedef itself.  */
19534         gen_typedef_die (decl, context_die);
19535       break;
19536
19537     case LABEL_DECL:
19538       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19539         gen_label_die (decl, context_die);
19540       break;
19541
19542     case VAR_DECL:
19543     case RESULT_DECL:
19544       /* If we are in terse mode, don't generate any DIEs to represent any
19545          variable declarations or definitions.  */
19546       if (debug_info_level <= DINFO_LEVEL_TERSE)
19547         break;
19548
19549       /* Output any DIEs that are needed to specify the type of this data
19550          object.  */
19551       if (decl_by_reference_p (decl_or_origin))
19552         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19553       else
19554         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19555
19556       /* And its containing type.  */
19557       class_origin = decl_class_context (decl_or_origin);
19558       if (class_origin != NULL_TREE)
19559         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19560
19561       /* And its containing namespace.  */
19562       context_die = declare_in_namespace (decl_or_origin, context_die);
19563
19564       /* Now output the DIE to represent the data object itself.  This gets
19565          complicated because of the possibility that the VAR_DECL really
19566          represents an inlined instance of a formal parameter for an inline
19567          function.  */
19568       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19569       if (ultimate_origin != NULL_TREE
19570           && TREE_CODE (ultimate_origin) == PARM_DECL)
19571         gen_formal_parameter_die (decl, origin,
19572                                   true /* Emit name attribute.  */,
19573                                   context_die);
19574       else
19575         gen_variable_die (decl, origin, context_die);
19576       break;
19577
19578     case FIELD_DECL:
19579       /* Ignore the nameless fields that are used to skip bits but handle C++
19580          anonymous unions and structs.  */
19581       if (DECL_NAME (decl) != NULL_TREE
19582           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19583           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19584         {
19585           gen_type_die (member_declared_type (decl), context_die);
19586           gen_field_die (decl, context_die);
19587         }
19588       break;
19589
19590     case PARM_DECL:
19591       if (DECL_BY_REFERENCE (decl_or_origin))
19592         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19593       else
19594         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19595       return gen_formal_parameter_die (decl, origin,
19596                                        true /* Emit name attribute.  */,
19597                                        context_die);
19598
19599     case NAMESPACE_DECL:
19600     case IMPORTED_DECL:
19601       if (dwarf_version >= 3 || !dwarf_strict)
19602         gen_namespace_die (decl, context_die);
19603       break;
19604
19605     default:
19606       /* Probably some frontend-internal decl.  Assume we don't care.  */
19607       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19608       break;
19609     }
19610
19611   return NULL;
19612 }
19613 \f
19614 /* Output debug information for global decl DECL.  Called from toplev.c after
19615    compilation proper has finished.  */
19616
19617 static void
19618 dwarf2out_global_decl (tree decl)
19619 {
19620   /* Output DWARF2 information for file-scope tentative data object
19621      declarations, file-scope (extern) function declarations (which
19622      had no corresponding body) and file-scope tagged type declarations
19623      and definitions which have not yet been forced out.  */
19624   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19625     dwarf2out_decl (decl);
19626 }
19627
19628 /* Output debug information for type decl DECL.  Called from toplev.c
19629    and from language front ends (to record built-in types).  */
19630 static void
19631 dwarf2out_type_decl (tree decl, int local)
19632 {
19633   if (!local)
19634     dwarf2out_decl (decl);
19635 }
19636
19637 /* Output debug information for imported module or decl DECL.
19638    NAME is non-NULL name in the lexical block if the decl has been renamed.
19639    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19640    that DECL belongs to.
19641    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19642 static void
19643 dwarf2out_imported_module_or_decl_1 (tree decl,
19644                                      tree name,
19645                                      tree lexical_block,
19646                                      dw_die_ref lexical_block_die)
19647 {
19648   expanded_location xloc;
19649   dw_die_ref imported_die = NULL;
19650   dw_die_ref at_import_die;
19651
19652   if (TREE_CODE (decl) == IMPORTED_DECL)
19653     {
19654       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19655       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19656       gcc_assert (decl);
19657     }
19658   else
19659     xloc = expand_location (input_location);
19660
19661   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19662     {
19663       at_import_die = force_type_die (TREE_TYPE (decl));
19664       /* For namespace N { typedef void T; } using N::T; base_type_die
19665          returns NULL, but DW_TAG_imported_declaration requires
19666          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19667       if (!at_import_die)
19668         {
19669           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19670           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19671           at_import_die = lookup_type_die (TREE_TYPE (decl));
19672           gcc_assert (at_import_die);
19673         }
19674     }
19675   else
19676     {
19677       at_import_die = lookup_decl_die (decl);
19678       if (!at_import_die)
19679         {
19680           /* If we're trying to avoid duplicate debug info, we may not have
19681              emitted the member decl for this field.  Emit it now.  */
19682           if (TREE_CODE (decl) == FIELD_DECL)
19683             {
19684               tree type = DECL_CONTEXT (decl);
19685
19686               if (TYPE_CONTEXT (type)
19687                   && TYPE_P (TYPE_CONTEXT (type))
19688                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19689                                                 DINFO_USAGE_DIR_USE))
19690                 return;
19691               gen_type_die_for_member (type, decl,
19692                                        get_context_die (TYPE_CONTEXT (type)));
19693             }
19694           at_import_die = force_decl_die (decl);
19695         }
19696     }
19697
19698   if (TREE_CODE (decl) == NAMESPACE_DECL)
19699     {
19700       if (dwarf_version >= 3 || !dwarf_strict)
19701         imported_die = new_die (DW_TAG_imported_module,
19702                                 lexical_block_die,
19703                                 lexical_block);
19704       else
19705         return;
19706     }
19707   else
19708     imported_die = new_die (DW_TAG_imported_declaration,
19709                             lexical_block_die,
19710                             lexical_block);
19711
19712   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19713   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19714   if (name)
19715     add_AT_string (imported_die, DW_AT_name,
19716                    IDENTIFIER_POINTER (name));
19717   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19718 }
19719
19720 /* Output debug information for imported module or decl DECL.
19721    NAME is non-NULL name in context if the decl has been renamed.
19722    CHILD is true if decl is one of the renamed decls as part of
19723    importing whole module.  */
19724
19725 static void
19726 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19727                                    bool child)
19728 {
19729   /* dw_die_ref at_import_die;  */
19730   dw_die_ref scope_die;
19731
19732   if (debug_info_level <= DINFO_LEVEL_TERSE)
19733     return;
19734
19735   gcc_assert (decl);
19736
19737   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19738      We need decl DIE for reference and scope die. First, get DIE for the decl
19739      itself.  */
19740
19741   /* Get the scope die for decl context. Use comp_unit_die for global module
19742      or decl. If die is not found for non globals, force new die.  */
19743   if (context
19744       && TYPE_P (context)
19745       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19746     return;
19747
19748   if (!(dwarf_version >= 3 || !dwarf_strict))
19749     return;
19750
19751   scope_die = get_context_die (context);
19752
19753   if (child)
19754     {
19755       gcc_assert (scope_die->die_child);
19756       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19757       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19758       scope_die = scope_die->die_child;
19759     }
19760
19761   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19762   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19763
19764 }
19765
19766 /* Write the debugging output for DECL.  */
19767
19768 void
19769 dwarf2out_decl (tree decl)
19770 {
19771   dw_die_ref context_die = comp_unit_die ();
19772
19773   switch (TREE_CODE (decl))
19774     {
19775     case ERROR_MARK:
19776       return;
19777
19778     case FUNCTION_DECL:
19779       /* What we would really like to do here is to filter out all mere
19780          file-scope declarations of file-scope functions which are never
19781          referenced later within this translation unit (and keep all of ones
19782          that *are* referenced later on) but we aren't clairvoyant, so we have
19783          no idea which functions will be referenced in the future (i.e. later
19784          on within the current translation unit). So here we just ignore all
19785          file-scope function declarations which are not also definitions.  If
19786          and when the debugger needs to know something about these functions,
19787          it will have to hunt around and find the DWARF information associated
19788          with the definition of the function.
19789
19790          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19791          nodes represent definitions and which ones represent mere
19792          declarations.  We have to check DECL_INITIAL instead. That's because
19793          the C front-end supports some weird semantics for "extern inline"
19794          function definitions.  These can get inlined within the current
19795          translation unit (and thus, we need to generate Dwarf info for their
19796          abstract instances so that the Dwarf info for the concrete inlined
19797          instances can have something to refer to) but the compiler never
19798          generates any out-of-lines instances of such things (despite the fact
19799          that they *are* definitions).
19800
19801          The important point is that the C front-end marks these "extern
19802          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19803          them anyway. Note that the C++ front-end also plays some similar games
19804          for inline function definitions appearing within include files which
19805          also contain `#pragma interface' pragmas.  */
19806       if (DECL_INITIAL (decl) == NULL_TREE)
19807         return;
19808
19809       /* If we're a nested function, initially use a parent of NULL; if we're
19810          a plain function, this will be fixed up in decls_for_scope.  If
19811          we're a method, it will be ignored, since we already have a DIE.  */
19812       if (decl_function_context (decl)
19813           /* But if we're in terse mode, we don't care about scope.  */
19814           && debug_info_level > DINFO_LEVEL_TERSE)
19815         context_die = NULL;
19816       break;
19817
19818     case VAR_DECL:
19819       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19820          declaration and if the declaration was never even referenced from
19821          within this entire compilation unit.  We suppress these DIEs in
19822          order to save space in the .debug section (by eliminating entries
19823          which are probably useless).  Note that we must not suppress
19824          block-local extern declarations (whether used or not) because that
19825          would screw-up the debugger's name lookup mechanism and cause it to
19826          miss things which really ought to be in scope at a given point.  */
19827       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19828         return;
19829
19830       /* For local statics lookup proper context die.  */
19831       if (TREE_STATIC (decl) && decl_function_context (decl))
19832         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19833
19834       /* If we are in terse mode, don't generate any DIEs to represent any
19835          variable declarations or definitions.  */
19836       if (debug_info_level <= DINFO_LEVEL_TERSE)
19837         return;
19838       break;
19839
19840     case CONST_DECL:
19841       if (debug_info_level <= DINFO_LEVEL_TERSE)
19842         return;
19843       if (!is_fortran () && !is_ada ())
19844         return;
19845       if (TREE_STATIC (decl) && decl_function_context (decl))
19846         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19847       break;
19848
19849     case NAMESPACE_DECL:
19850     case IMPORTED_DECL:
19851       if (debug_info_level <= DINFO_LEVEL_TERSE)
19852         return;
19853       if (lookup_decl_die (decl) != NULL)
19854         return;
19855       break;
19856
19857     case TYPE_DECL:
19858       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19859       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19860         return;
19861
19862       /* Don't bother trying to generate any DIEs to represent any of the
19863          normal built-in types for the language we are compiling.  */
19864       if (DECL_IS_BUILTIN (decl))
19865         return;
19866
19867       /* If we are in terse mode, don't generate any DIEs for types.  */
19868       if (debug_info_level <= DINFO_LEVEL_TERSE)
19869         return;
19870
19871       /* If we're a function-scope tag, initially use a parent of NULL;
19872          this will be fixed up in decls_for_scope.  */
19873       if (decl_function_context (decl))
19874         context_die = NULL;
19875
19876       break;
19877
19878     default:
19879       return;
19880     }
19881
19882   gen_decl_die (decl, NULL, context_die);
19883 }
19884
19885 /* Write the debugging output for DECL.  */
19886
19887 static void
19888 dwarf2out_function_decl (tree decl)
19889 {
19890   dwarf2out_decl (decl);
19891   call_arg_locations = NULL;
19892   call_arg_loc_last = NULL;
19893   call_site_count = -1;
19894   tail_call_site_count = -1;
19895   VEC_free (dw_die_ref, heap, block_map);
19896   htab_empty (decl_loc_table);
19897   htab_empty (cached_dw_loc_list_table);
19898 }
19899
19900 /* Output a marker (i.e. a label) for the beginning of the generated code for
19901    a lexical block.  */
19902
19903 static void
19904 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19905                        unsigned int blocknum)
19906 {
19907   switch_to_section (current_function_section ());
19908   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19909 }
19910
19911 /* Output a marker (i.e. a label) for the end of the generated code for a
19912    lexical block.  */
19913
19914 static void
19915 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19916 {
19917   switch_to_section (current_function_section ());
19918   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19919 }
19920
19921 /* Returns nonzero if it is appropriate not to emit any debugging
19922    information for BLOCK, because it doesn't contain any instructions.
19923
19924    Don't allow this for blocks with nested functions or local classes
19925    as we would end up with orphans, and in the presence of scheduling
19926    we may end up calling them anyway.  */
19927
19928 static bool
19929 dwarf2out_ignore_block (const_tree block)
19930 {
19931   tree decl;
19932   unsigned int i;
19933
19934   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19935     if (TREE_CODE (decl) == FUNCTION_DECL
19936         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19937       return 0;
19938   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19939     {
19940       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19941       if (TREE_CODE (decl) == FUNCTION_DECL
19942           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19943       return 0;
19944     }
19945
19946   return 1;
19947 }
19948
19949 /* Hash table routines for file_hash.  */
19950
19951 static int
19952 file_table_eq (const void *p1_p, const void *p2_p)
19953 {
19954   const struct dwarf_file_data *const p1 =
19955     (const struct dwarf_file_data *) p1_p;
19956   const char *const p2 = (const char *) p2_p;
19957   return filename_cmp (p1->filename, p2) == 0;
19958 }
19959
19960 static hashval_t
19961 file_table_hash (const void *p_p)
19962 {
19963   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19964   return htab_hash_string (p->filename);
19965 }
19966
19967 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19968    dwarf2out.c) and return its "index".  The index of each (known) filename is
19969    just a unique number which is associated with only that one filename.  We
19970    need such numbers for the sake of generating labels (in the .debug_sfnames
19971    section) and references to those files numbers (in the .debug_srcinfo
19972    and.debug_macinfo sections).  If the filename given as an argument is not
19973    found in our current list, add it to the list and assign it the next
19974    available unique index number.  In order to speed up searches, we remember
19975    the index of the filename was looked up last.  This handles the majority of
19976    all searches.  */
19977
19978 static struct dwarf_file_data *
19979 lookup_filename (const char *file_name)
19980 {
19981   void ** slot;
19982   struct dwarf_file_data * created;
19983
19984   /* Check to see if the file name that was searched on the previous
19985      call matches this file name.  If so, return the index.  */
19986   if (file_table_last_lookup
19987       && (file_name == file_table_last_lookup->filename
19988           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19989     return file_table_last_lookup;
19990
19991   /* Didn't match the previous lookup, search the table.  */
19992   slot = htab_find_slot_with_hash (file_table, file_name,
19993                                    htab_hash_string (file_name), INSERT);
19994   if (*slot)
19995     return (struct dwarf_file_data *) *slot;
19996
19997   created = ggc_alloc_dwarf_file_data ();
19998   created->filename = file_name;
19999   created->emitted_number = 0;
20000   *slot = created;
20001   return created;
20002 }
20003
20004 /* If the assembler will construct the file table, then translate the compiler
20005    internal file table number into the assembler file table number, and emit
20006    a .file directive if we haven't already emitted one yet.  The file table
20007    numbers are different because we prune debug info for unused variables and
20008    types, which may include filenames.  */
20009
20010 static int
20011 maybe_emit_file (struct dwarf_file_data * fd)
20012 {
20013   if (! fd->emitted_number)
20014     {
20015       if (last_emitted_file)
20016         fd->emitted_number = last_emitted_file->emitted_number + 1;
20017       else
20018         fd->emitted_number = 1;
20019       last_emitted_file = fd;
20020
20021       if (DWARF2_ASM_LINE_DEBUG_INFO)
20022         {
20023           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20024           output_quoted_string (asm_out_file,
20025                                 remap_debug_filename (fd->filename));
20026           fputc ('\n', asm_out_file);
20027         }
20028     }
20029
20030   return fd->emitted_number;
20031 }
20032
20033 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20034    That generation should happen after function debug info has been
20035    generated. The value of the attribute is the constant value of ARG.  */
20036
20037 static void
20038 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20039 {
20040   die_arg_entry entry;
20041
20042   if (!die || !arg)
20043     return;
20044
20045   if (!tmpl_value_parm_die_table)
20046     tmpl_value_parm_die_table
20047       = VEC_alloc (die_arg_entry, gc, 32);
20048
20049   entry.die = die;
20050   entry.arg = arg;
20051   VEC_safe_push (die_arg_entry, gc,
20052                  tmpl_value_parm_die_table,
20053                  &entry);
20054 }
20055
20056 /* Return TRUE if T is an instance of generic type, FALSE
20057    otherwise.  */
20058
20059 static bool
20060 generic_type_p (tree t)
20061 {
20062   if (t == NULL_TREE || !TYPE_P (t))
20063     return false;
20064   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20065 }
20066
20067 /* Schedule the generation of the generic parameter dies for the
20068   instance of generic type T. The proper generation itself is later
20069   done by gen_scheduled_generic_parms_dies. */
20070
20071 static void
20072 schedule_generic_params_dies_gen (tree t)
20073 {
20074   if (!generic_type_p (t))
20075     return;
20076
20077   if (generic_type_instances == NULL)
20078     generic_type_instances = VEC_alloc (tree, gc, 256);
20079
20080   VEC_safe_push (tree, gc, generic_type_instances, t);
20081 }
20082
20083 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20084    by append_entry_to_tmpl_value_parm_die_table. This function must
20085    be called after function DIEs have been generated.  */
20086
20087 static void
20088 gen_remaining_tmpl_value_param_die_attribute (void)
20089 {
20090   if (tmpl_value_parm_die_table)
20091     {
20092       unsigned i;
20093       die_arg_entry *e;
20094
20095       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20096         tree_add_const_value_attribute (e->die, e->arg);
20097     }
20098 }
20099
20100 /* Generate generic parameters DIEs for instances of generic types
20101    that have been previously scheduled by
20102    schedule_generic_params_dies_gen. This function must be called
20103    after all the types of the CU have been laid out.  */
20104
20105 static void
20106 gen_scheduled_generic_parms_dies (void)
20107 {
20108   unsigned i;
20109   tree t;
20110
20111   if (generic_type_instances == NULL)
20112     return;
20113   
20114   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20115     gen_generic_params_dies (t);
20116 }
20117
20118
20119 /* Replace DW_AT_name for the decl with name.  */
20120
20121 static void
20122 dwarf2out_set_name (tree decl, tree name)
20123 {
20124   dw_die_ref die;
20125   dw_attr_ref attr;
20126   const char *dname;
20127
20128   die = TYPE_SYMTAB_DIE (decl);
20129   if (!die)
20130     return;
20131
20132   dname = dwarf2_name (name, 0);
20133   if (!dname)
20134     return;
20135
20136   attr = get_AT (die, DW_AT_name);
20137   if (attr)
20138     {
20139       struct indirect_string_node *node;
20140
20141       node = find_AT_string (dname);
20142       /* replace the string.  */
20143       attr->dw_attr_val.v.val_str = node;
20144     }
20145
20146   else
20147     add_name_attribute (die, dname);
20148 }
20149
20150 /* Called by the final INSN scan whenever we see a var location.  We
20151    use it to drop labels in the right places, and throw the location in
20152    our lookup table.  */
20153
20154 static void
20155 dwarf2out_var_location (rtx loc_note)
20156 {
20157   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20158   struct var_loc_node *newloc;
20159   rtx next_real, next_note;
20160   static const char *last_label;
20161   static const char *last_postcall_label;
20162   static bool last_in_cold_section_p;
20163   static rtx expected_next_loc_note;
20164   tree decl;
20165   bool var_loc_p;
20166
20167   if (!NOTE_P (loc_note))
20168     {
20169       if (CALL_P (loc_note))
20170         {
20171           call_site_count++;
20172           if (SIBLING_CALL_P (loc_note))
20173             tail_call_site_count++;
20174         }
20175       return;
20176     }
20177
20178   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20179   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20180     return;
20181
20182   /* Optimize processing a large consecutive sequence of location
20183      notes so we don't spend too much time in next_real_insn.  If the
20184      next insn is another location note, remember the next_real_insn
20185      calculation for next time.  */
20186   next_real = cached_next_real_insn;
20187   if (next_real)
20188     {
20189       if (expected_next_loc_note != loc_note)
20190         next_real = NULL_RTX;
20191     }
20192
20193   next_note = NEXT_INSN (loc_note);
20194   if (! next_note
20195       || INSN_DELETED_P (next_note)
20196       || GET_CODE (next_note) != NOTE
20197       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20198           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20199     next_note = NULL_RTX;
20200
20201   if (! next_real)
20202     next_real = next_real_insn (loc_note);
20203
20204   if (next_note)
20205     {
20206       expected_next_loc_note = next_note;
20207       cached_next_real_insn = next_real;
20208     }
20209   else
20210     cached_next_real_insn = NULL_RTX;
20211
20212   /* If there are no instructions which would be affected by this note,
20213      don't do anything.  */
20214   if (var_loc_p
20215       && next_real == NULL_RTX
20216       && !NOTE_DURING_CALL_P (loc_note))
20217     return;
20218
20219   if (next_real == NULL_RTX)
20220     next_real = get_last_insn ();
20221
20222   /* If there were any real insns between note we processed last time
20223      and this note (or if it is the first note), clear
20224      last_{,postcall_}label so that they are not reused this time.  */
20225   if (last_var_location_insn == NULL_RTX
20226       || last_var_location_insn != next_real
20227       || last_in_cold_section_p != in_cold_section_p)
20228     {
20229       last_label = NULL;
20230       last_postcall_label = NULL;
20231     }
20232
20233   if (var_loc_p)
20234     {
20235       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20236       newloc = add_var_loc_to_decl (decl, loc_note,
20237                                     NOTE_DURING_CALL_P (loc_note)
20238                                     ? last_postcall_label : last_label);
20239       if (newloc == NULL)
20240         return;
20241     }
20242   else
20243     {
20244       decl = NULL_TREE;
20245       newloc = NULL;
20246     }
20247
20248   /* If there were no real insns between note we processed last time
20249      and this note, use the label we emitted last time.  Otherwise
20250      create a new label and emit it.  */
20251   if (last_label == NULL)
20252     {
20253       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20254       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20255       loclabel_num++;
20256       last_label = ggc_strdup (loclabel);
20257     }
20258
20259   if (!var_loc_p)
20260     {
20261       struct call_arg_loc_node *ca_loc
20262         = ggc_alloc_cleared_call_arg_loc_node ();
20263       rtx prev = prev_real_insn (loc_note), x;
20264       ca_loc->call_arg_loc_note = loc_note;
20265       ca_loc->next = NULL;
20266       ca_loc->label = last_label;
20267       gcc_assert (prev
20268                   && (CALL_P (prev)
20269                       || (NONJUMP_INSN_P (prev)
20270                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20271                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20272       if (!CALL_P (prev))
20273         prev = XVECEXP (PATTERN (prev), 0, 0);
20274       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20275       x = PATTERN (prev);
20276       if (GET_CODE (x) == PARALLEL)
20277         x = XVECEXP (x, 0, 0);
20278       if (GET_CODE (x) == SET)
20279         x = SET_SRC (x);
20280       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20281         {
20282           x = XEXP (XEXP (x, 0), 0);
20283           if (GET_CODE (x) == SYMBOL_REF
20284               && SYMBOL_REF_DECL (x)
20285               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20286             ca_loc->symbol_ref = x;
20287         }
20288       ca_loc->block = insn_scope (prev);
20289       if (call_arg_locations)
20290         call_arg_loc_last->next = ca_loc;
20291       else
20292         call_arg_locations = ca_loc;
20293       call_arg_loc_last = ca_loc;
20294     }
20295   else if (!NOTE_DURING_CALL_P (loc_note))
20296     newloc->label = last_label;
20297   else
20298     {
20299       if (!last_postcall_label)
20300         {
20301           sprintf (loclabel, "%s-1", last_label);
20302           last_postcall_label = ggc_strdup (loclabel);
20303         }
20304       newloc->label = last_postcall_label;
20305     }
20306
20307   last_var_location_insn = next_real;
20308   last_in_cold_section_p = in_cold_section_p;
20309 }
20310
20311 /* Note in one location list that text section has changed.  */
20312
20313 static int
20314 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20315 {
20316   var_loc_list *list = (var_loc_list *) *slot;
20317   if (list->first)
20318     list->last_before_switch
20319       = list->last->next ? list->last->next : list->last;
20320   return 1;
20321 }
20322
20323 /* Note in all location lists that text section has changed.  */
20324
20325 static void
20326 var_location_switch_text_section (void)
20327 {
20328   if (decl_loc_table == NULL)
20329     return;
20330
20331   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20332 }
20333
20334 /* Create a new line number table.  */
20335
20336 static dw_line_info_table *
20337 new_line_info_table (void)
20338 {
20339   dw_line_info_table *table;
20340
20341   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20342   table->file_num = 1;
20343   table->line_num = 1;
20344   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20345
20346   return table;
20347 }
20348
20349 /* Lookup the "current" table into which we emit line info, so
20350    that we don't have to do it for every source line.  */
20351
20352 static void
20353 set_cur_line_info_table (section *sec)
20354 {
20355   dw_line_info_table *table;
20356
20357   if (sec == text_section)
20358     table = text_section_line_info;
20359   else if (sec == cold_text_section)
20360     {
20361       table = cold_text_section_line_info;
20362       if (!table)
20363         {
20364           cold_text_section_line_info = table = new_line_info_table ();
20365           table->end_label = cold_end_label;
20366         }
20367     }
20368   else
20369     {
20370       const char *end_label;
20371
20372       if (flag_reorder_blocks_and_partition)
20373         {
20374           if (in_cold_section_p)
20375             end_label = crtl->subsections.cold_section_end_label;
20376           else
20377             end_label = crtl->subsections.hot_section_end_label;
20378         }
20379       else
20380         {
20381           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20382           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20383                                        current_function_funcdef_no);
20384           end_label = ggc_strdup (label);
20385         }
20386
20387       table = new_line_info_table ();
20388       table->end_label = end_label;
20389
20390       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20391     }
20392
20393   if (DWARF2_ASM_LINE_DEBUG_INFO)
20394     table->is_stmt = (cur_line_info_table
20395                       ? cur_line_info_table->is_stmt
20396                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20397   cur_line_info_table = table;
20398 }
20399
20400
20401 /* We need to reset the locations at the beginning of each
20402    function. We can't do this in the end_function hook, because the
20403    declarations that use the locations won't have been output when
20404    that hook is called.  Also compute have_multiple_function_sections here.  */
20405
20406 static void
20407 dwarf2out_begin_function (tree fun)
20408 {
20409   section *sec = function_section (fun);
20410
20411   if (sec != text_section)
20412     have_multiple_function_sections = true;
20413
20414   if (flag_reorder_blocks_and_partition && !cold_text_section)
20415     {
20416       gcc_assert (current_function_decl == fun);
20417       cold_text_section = unlikely_text_section ();
20418       switch_to_section (cold_text_section);
20419       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20420       switch_to_section (sec);
20421     }
20422
20423   dwarf2out_note_section_used ();
20424   call_site_count = 0;
20425   tail_call_site_count = 0;
20426
20427   set_cur_line_info_table (sec);
20428 }
20429
20430 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20431
20432 static void
20433 push_dw_line_info_entry (dw_line_info_table *table,
20434                          enum dw_line_info_opcode opcode, unsigned int val)
20435 {
20436   dw_line_info_entry e;
20437   e.opcode = opcode;
20438   e.val = val;
20439   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20440 }
20441
20442 /* Output a label to mark the beginning of a source code line entry
20443    and record information relating to this source line, in
20444    'line_info_table' for later output of the .debug_line section.  */
20445 /* ??? The discriminator parameter ought to be unsigned.  */
20446
20447 static void
20448 dwarf2out_source_line (unsigned int line, const char *filename,
20449                        int discriminator, bool is_stmt)
20450 {
20451   unsigned int file_num;
20452   dw_line_info_table *table;
20453
20454   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20455     return;
20456
20457   /* The discriminator column was added in dwarf4.  Simplify the below
20458      by simply removing it if we're not supposed to output it.  */
20459   if (dwarf_version < 4 && dwarf_strict)
20460     discriminator = 0;
20461
20462   table = cur_line_info_table;
20463   file_num = maybe_emit_file (lookup_filename (filename));
20464
20465   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20466      the debugger has used the second (possibly duplicate) line number
20467      at the beginning of the function to mark the end of the prologue.
20468      We could eliminate any other duplicates within the function.  For
20469      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20470      that second line number entry.  */
20471   /* Recall that this end-of-prologue indication is *not* the same thing
20472      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20473      to which the hook corresponds, follows the last insn that was 
20474      emitted by gen_prologue.  What we need is to preceed the first insn
20475      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20476      insn that corresponds to something the user wrote.  These may be
20477      very different locations once scheduling is enabled.  */
20478
20479   if (0 && file_num == table->file_num
20480       && line == table->line_num
20481       && discriminator == table->discrim_num
20482       && is_stmt == table->is_stmt)
20483     return;
20484
20485   switch_to_section (current_function_section ());
20486
20487   /* If requested, emit something human-readable.  */
20488   if (flag_debug_asm)
20489     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20490
20491   if (DWARF2_ASM_LINE_DEBUG_INFO)
20492     {
20493       /* Emit the .loc directive understood by GNU as.  */
20494       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20495          file_num, line, is_stmt, discriminator */
20496       fputs ("\t.loc ", asm_out_file);
20497       fprint_ul (asm_out_file, file_num);
20498       putc (' ', asm_out_file);
20499       fprint_ul (asm_out_file, line);
20500       putc (' ', asm_out_file);
20501       putc ('0', asm_out_file);
20502
20503       if (is_stmt != table->is_stmt)
20504         {
20505           fputs (" is_stmt ", asm_out_file);
20506           putc (is_stmt ? '1' : '0', asm_out_file);
20507         }
20508       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20509         {
20510           gcc_assert (discriminator > 0);
20511           fputs (" discriminator ", asm_out_file);
20512           fprint_ul (asm_out_file, (unsigned long) discriminator);
20513         }
20514       putc ('\n', asm_out_file);
20515     }
20516   else
20517     {
20518       unsigned int label_num = ++line_info_label_num;
20519
20520       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20521
20522       push_dw_line_info_entry (table, LI_set_address, label_num);
20523       if (file_num != table->file_num)
20524         push_dw_line_info_entry (table, LI_set_file, file_num);
20525       if (discriminator != table->discrim_num)
20526         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20527       if (is_stmt != table->is_stmt)
20528         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20529       push_dw_line_info_entry (table, LI_set_line, line);
20530     }
20531
20532   table->file_num = file_num;
20533   table->line_num = line;
20534   table->discrim_num = discriminator;
20535   table->is_stmt = is_stmt;
20536   table->in_use = true;
20537 }
20538
20539 /* Record the beginning of a new source file.  */
20540
20541 static void
20542 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20543 {
20544   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20545     {
20546       /* Record the beginning of the file for break_out_includes.  */
20547       dw_die_ref bincl_die;
20548
20549       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20550       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20551     }
20552
20553   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20554     {
20555       macinfo_entry e;
20556       e.code = DW_MACINFO_start_file;
20557       e.lineno = lineno;
20558       e.info = ggc_strdup (filename);
20559       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20560     }
20561 }
20562
20563 /* Record the end of a source file.  */
20564
20565 static void
20566 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20567 {
20568   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20569     /* Record the end of the file for break_out_includes.  */
20570     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20571
20572   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20573     {
20574       macinfo_entry e;
20575       e.code = DW_MACINFO_end_file;
20576       e.lineno = lineno;
20577       e.info = NULL;
20578       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20579     }
20580 }
20581
20582 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20583    the tail part of the directive line, i.e. the part which is past the
20584    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20585
20586 static void
20587 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20588                   const char *buffer ATTRIBUTE_UNUSED)
20589 {
20590   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20591     {
20592       macinfo_entry e;
20593       /* Insert a dummy first entry to be able to optimize the whole
20594          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20595       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20596         {
20597           e.code = 0;
20598           e.lineno = 0;
20599           e.info = NULL;
20600           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20601         }
20602       e.code = DW_MACINFO_define;
20603       e.lineno = lineno;
20604       e.info = ggc_strdup (buffer);
20605       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20606     }
20607 }
20608
20609 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20610    the tail part of the directive line, i.e. the part which is past the
20611    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20612
20613 static void
20614 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20615                  const char *buffer ATTRIBUTE_UNUSED)
20616 {
20617   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20618     {
20619       macinfo_entry e;
20620       /* Insert a dummy first entry to be able to optimize the whole
20621          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20622       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20623         {
20624           e.code = 0;
20625           e.lineno = 0;
20626           e.info = NULL;
20627           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20628         }
20629       e.code = DW_MACINFO_undef;
20630       e.lineno = lineno;
20631       e.info = ggc_strdup (buffer);
20632       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20633     }
20634 }
20635
20636 /* Routines to manipulate hash table of CUs.  */
20637
20638 static hashval_t
20639 htab_macinfo_hash (const void *of)
20640 {
20641   const macinfo_entry *const entry =
20642     (const macinfo_entry *) of;
20643
20644   return htab_hash_string (entry->info);
20645 }
20646
20647 static int
20648 htab_macinfo_eq (const void *of1, const void *of2)
20649 {
20650   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20651   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20652
20653   return !strcmp (entry1->info, entry2->info);
20654 }
20655
20656 /* Output a single .debug_macinfo entry.  */
20657
20658 static void
20659 output_macinfo_op (macinfo_entry *ref)
20660 {
20661   int file_num;
20662   size_t len;
20663   struct indirect_string_node *node;
20664   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20665   struct dwarf_file_data *fd;
20666
20667   switch (ref->code)
20668     {
20669     case DW_MACINFO_start_file:
20670       fd = lookup_filename (ref->info);
20671       file_num = maybe_emit_file (fd);
20672       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20673       dw2_asm_output_data_uleb128 (ref->lineno,
20674                                    "Included from line number %lu", 
20675                                    (unsigned long) ref->lineno);
20676       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20677       break;
20678     case DW_MACINFO_end_file:
20679       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20680       break;
20681     case DW_MACINFO_define:
20682     case DW_MACINFO_undef:
20683       len = strlen (ref->info) + 1;
20684       if (!dwarf_strict
20685           && len > DWARF_OFFSET_SIZE
20686           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20687           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20688         {
20689           ref->code = ref->code == DW_MACINFO_define
20690                       ? DW_MACRO_GNU_define_indirect
20691                       : DW_MACRO_GNU_undef_indirect;
20692           output_macinfo_op (ref);
20693           return;
20694         }
20695       dw2_asm_output_data (1, ref->code,
20696                            ref->code == DW_MACINFO_define
20697                            ? "Define macro" : "Undefine macro");
20698       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20699                                    (unsigned long) ref->lineno);
20700       dw2_asm_output_nstring (ref->info, -1, "The macro");
20701       break;
20702     case DW_MACRO_GNU_define_indirect:
20703     case DW_MACRO_GNU_undef_indirect:
20704       node = find_AT_string (ref->info);
20705       if (node->form != DW_FORM_strp)
20706         {
20707           char label[32];
20708           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20709           ++dw2_string_counter;
20710           node->label = xstrdup (label);
20711           node->form = DW_FORM_strp;
20712         }
20713       dw2_asm_output_data (1, ref->code,
20714                            ref->code == DW_MACRO_GNU_define_indirect
20715                            ? "Define macro indirect"
20716                            : "Undefine macro indirect");
20717       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20718                                    (unsigned long) ref->lineno);
20719       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20720                              debug_str_section, "The macro: \"%s\"",
20721                              ref->info);
20722       break;
20723     case DW_MACRO_GNU_transparent_include:
20724       dw2_asm_output_data (1, ref->code, "Transparent include");
20725       ASM_GENERATE_INTERNAL_LABEL (label,
20726                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20727       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20728       break;
20729     default:
20730       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20731                ASM_COMMENT_START, (unsigned long) ref->code);
20732       break;
20733     }
20734 }
20735
20736 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20737    other compilation unit .debug_macinfo sections.  IDX is the first
20738    index of a define/undef, return the number of ops that should be
20739    emitted in a comdat .debug_macinfo section and emit
20740    a DW_MACRO_GNU_transparent_include entry referencing it.
20741    If the define/undef entry should be emitted normally, return 0.  */
20742
20743 static unsigned
20744 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20745                         htab_t *macinfo_htab)
20746 {
20747   macinfo_entry *first, *second, *cur, *inc;
20748   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20749   unsigned char checksum[16];
20750   struct md5_ctx ctx;
20751   char *grp_name, *tail;
20752   const char *base;
20753   unsigned int i, count, encoded_filename_len, linebuf_len;
20754   void **slot;
20755
20756   first = VEC_index (macinfo_entry, macinfo_table, idx);
20757   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20758
20759   /* Optimize only if there are at least two consecutive define/undef ops,
20760      and either all of them are before first DW_MACINFO_start_file
20761      with lineno 0 (i.e. predefined macro block), or all of them are
20762      in some included header file.  */
20763   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20764     return 0;
20765   if (VEC_empty (macinfo_entry, files))
20766     {
20767       if (first->lineno != 0 || second->lineno != 0)
20768         return 0;
20769     }
20770   else if (first->lineno == 0)
20771     return 0;
20772
20773   /* Find the last define/undef entry that can be grouped together
20774      with first and at the same time compute md5 checksum of their
20775      codes, linenumbers and strings.  */
20776   md5_init_ctx (&ctx);
20777   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20778     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20779       break;
20780     else if (first->lineno == 0 && cur->lineno != 0)
20781       break;
20782     else
20783       {
20784         unsigned char code = cur->code;
20785         md5_process_bytes (&code, 1, &ctx);
20786         checksum_uleb128 (cur->lineno, &ctx);
20787         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20788       }
20789   md5_finish_ctx (&ctx, checksum);
20790   count = i - idx;
20791
20792   /* From the containing include filename (if any) pick up just
20793      usable characters from its basename.  */
20794   if (first->lineno == 0)
20795     base = "";
20796   else
20797     base = lbasename (VEC_last (macinfo_entry, files)->info);
20798   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20799     if (ISIDNUM (base[i]) || base[i] == '.')
20800       encoded_filename_len++;
20801   /* Count . at the end.  */
20802   if (encoded_filename_len)
20803     encoded_filename_len++;
20804
20805   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20806   linebuf_len = strlen (linebuf);
20807
20808   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20809   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20810                          + 16 * 2 + 1);
20811   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20812   tail = grp_name + 4;
20813   if (encoded_filename_len)
20814     {
20815       for (i = 0; base[i]; i++)
20816         if (ISIDNUM (base[i]) || base[i] == '.')
20817           *tail++ = base[i];
20818       *tail++ = '.';
20819     }
20820   memcpy (tail, linebuf, linebuf_len);
20821   tail += linebuf_len;
20822   *tail++ = '.';
20823   for (i = 0; i < 16; i++)
20824     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20825
20826   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20827      in the empty vector entry before the first define/undef.  */
20828   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20829   inc->code = DW_MACRO_GNU_transparent_include;
20830   inc->lineno = 0;
20831   inc->info = ggc_strdup (grp_name);
20832   if (*macinfo_htab == NULL)
20833     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20834   /* Avoid emitting duplicates.  */
20835   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20836   if (*slot != NULL)
20837     {
20838       inc->code = 0;
20839       inc->info = NULL;
20840       /* If such an entry has been used before, just emit
20841          a DW_MACRO_GNU_transparent_include op.  */
20842       inc = (macinfo_entry *) *slot;
20843       output_macinfo_op (inc);
20844       /* And clear all macinfo_entry in the range to avoid emitting them
20845          in the second pass.  */
20846       for (i = idx;
20847            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20848            && i < idx + count;
20849            i++)
20850         {
20851           cur->code = 0;
20852           cur->info = NULL;
20853         }
20854     }
20855   else
20856     {
20857       *slot = inc;
20858       inc->lineno = htab_elements (*macinfo_htab);
20859       output_macinfo_op (inc);
20860     }
20861   return count;
20862 }
20863
20864 /* Output macinfo section(s).  */
20865
20866 static void
20867 output_macinfo (void)
20868 {
20869   unsigned i;
20870   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20871   macinfo_entry *ref;
20872   VEC (macinfo_entry, gc) *files = NULL;
20873   htab_t macinfo_htab = NULL;
20874
20875   if (! length)
20876     return;
20877
20878   /* output_macinfo* uses these interchangeably.  */
20879   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20880               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20881               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20882               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20883
20884   /* For .debug_macro emit the section header.  */
20885   if (!dwarf_strict)
20886     {
20887       dw2_asm_output_data (2, 4, "DWARF macro version number");
20888       if (DWARF_OFFSET_SIZE == 8)
20889         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20890       else
20891         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20892       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20893                              debug_line_section, NULL);
20894     }
20895
20896   /* In the first loop, it emits the primary .debug_macinfo section
20897      and after each emitted op the macinfo_entry is cleared.
20898      If a longer range of define/undef ops can be optimized using
20899      DW_MACRO_GNU_transparent_include, the
20900      DW_MACRO_GNU_transparent_include op is emitted and kept in
20901      the vector before the first define/undef in the range and the
20902      whole range of define/undef ops is not emitted and kept.  */
20903   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20904     {
20905       switch (ref->code)
20906         {
20907         case DW_MACINFO_start_file:
20908           VEC_safe_push (macinfo_entry, gc, files, ref);
20909           break;
20910         case DW_MACINFO_end_file:
20911           if (!VEC_empty (macinfo_entry, files))
20912             VEC_pop (macinfo_entry, files);
20913           break;
20914         case DW_MACINFO_define:
20915         case DW_MACINFO_undef:
20916           if (!dwarf_strict
20917               && HAVE_COMDAT_GROUP
20918               && VEC_length (macinfo_entry, files) != 1
20919               && i > 0
20920               && i + 1 < length
20921               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20922             {
20923               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20924               if (count)
20925                 {
20926                   i += count - 1;
20927                   continue;
20928                 }
20929             }
20930           break;
20931         case 0:
20932           /* A dummy entry may be inserted at the beginning to be able
20933              to optimize the whole block of predefined macros.  */
20934           if (i == 0)
20935             continue;
20936         default:
20937           break;
20938         }
20939       output_macinfo_op (ref);
20940       ref->info = NULL;
20941       ref->code = 0;
20942     }
20943
20944   if (macinfo_htab == NULL)
20945     return;
20946
20947   htab_delete (macinfo_htab);
20948
20949   /* If any DW_MACRO_GNU_transparent_include were used, on those
20950      DW_MACRO_GNU_transparent_include entries terminate the
20951      current chain and switch to a new comdat .debug_macinfo
20952      section and emit the define/undef entries within it.  */
20953   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20954     switch (ref->code)
20955       {
20956       case 0:
20957         continue;
20958       case DW_MACRO_GNU_transparent_include:
20959         {
20960           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20961           tree comdat_key = get_identifier (ref->info);
20962           /* Terminate the previous .debug_macinfo section.  */
20963           dw2_asm_output_data (1, 0, "End compilation unit");
20964           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20965                                          SECTION_DEBUG
20966                                          | SECTION_LINKONCE,
20967                                          comdat_key);
20968           ASM_GENERATE_INTERNAL_LABEL (label,
20969                                        DEBUG_MACRO_SECTION_LABEL,
20970                                        ref->lineno);
20971           ASM_OUTPUT_LABEL (asm_out_file, label);
20972           ref->code = 0;
20973           ref->info = NULL;
20974           dw2_asm_output_data (2, 4, "DWARF macro version number");
20975           if (DWARF_OFFSET_SIZE == 8)
20976             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20977           else
20978             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20979         }
20980         break;
20981       case DW_MACINFO_define:
20982       case DW_MACINFO_undef:
20983         output_macinfo_op (ref);
20984         ref->code = 0;
20985         ref->info = NULL;
20986         break;
20987       default:
20988         gcc_unreachable ();
20989       }
20990 }
20991
20992 /* Set up for Dwarf output at the start of compilation.  */
20993
20994 static void
20995 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20996 {
20997   /* Allocate the file_table.  */
20998   file_table = htab_create_ggc (50, file_table_hash,
20999                                 file_table_eq, NULL);
21000
21001   /* Allocate the decl_die_table.  */
21002   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21003                                     decl_die_table_eq, NULL);
21004
21005   /* Allocate the decl_loc_table.  */
21006   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21007                                     decl_loc_table_eq, NULL);
21008
21009   /* Allocate the cached_dw_loc_list_table.  */
21010   cached_dw_loc_list_table
21011     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21012                        cached_dw_loc_list_table_eq, NULL);
21013
21014   /* Allocate the initial hunk of the decl_scope_table.  */
21015   decl_scope_table = VEC_alloc (tree, gc, 256);
21016
21017   /* Allocate the initial hunk of the abbrev_die_table.  */
21018   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21019     (ABBREV_DIE_TABLE_INCREMENT);
21020   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21021   /* Zero-th entry is allocated, but unused.  */
21022   abbrev_die_table_in_use = 1;
21023
21024   /* Allocate the pubtypes and pubnames vectors.  */
21025   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21026   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21027
21028   incomplete_types = VEC_alloc (tree, gc, 64);
21029
21030   used_rtx_array = VEC_alloc (rtx, gc, 32);
21031
21032   debug_info_section = get_section (DEBUG_INFO_SECTION,
21033                                     SECTION_DEBUG, NULL);
21034   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21035                                       SECTION_DEBUG, NULL);
21036   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21037                                        SECTION_DEBUG, NULL);
21038   debug_macinfo_section = get_section (dwarf_strict
21039                                        ? DEBUG_MACINFO_SECTION
21040                                        : DEBUG_MACRO_SECTION,
21041                                        SECTION_DEBUG, NULL);
21042   debug_line_section = get_section (DEBUG_LINE_SECTION,
21043                                     SECTION_DEBUG, NULL);
21044   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21045                                    SECTION_DEBUG, NULL);
21046   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21047                                         SECTION_DEBUG, NULL);
21048   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21049                                         SECTION_DEBUG, NULL);
21050   debug_str_section = get_section (DEBUG_STR_SECTION,
21051                                    DEBUG_STR_SECTION_FLAGS, NULL);
21052   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21053                                       SECTION_DEBUG, NULL);
21054   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21055                                      SECTION_DEBUG, NULL);
21056
21057   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21058   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21059                                DEBUG_ABBREV_SECTION_LABEL, 0);
21060   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21061   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21062                                COLD_TEXT_SECTION_LABEL, 0);
21063   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21064
21065   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21066                                DEBUG_INFO_SECTION_LABEL, 0);
21067   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21068                                DEBUG_LINE_SECTION_LABEL, 0);
21069   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21070                                DEBUG_RANGES_SECTION_LABEL, 0);
21071   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21072                                dwarf_strict
21073                                ? DEBUG_MACINFO_SECTION_LABEL
21074                                : DEBUG_MACRO_SECTION_LABEL, 0);
21075
21076   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21077     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21078
21079   switch_to_section (text_section);
21080   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21081
21082   /* Make sure the line number table for .text always exists.  */
21083   text_section_line_info = new_line_info_table ();
21084   text_section_line_info->end_label = text_end_label;
21085 }
21086
21087 /* Called before cgraph_optimize starts outputtting functions, variables
21088    and toplevel asms into assembly.  */
21089
21090 static void
21091 dwarf2out_assembly_start (void)
21092 {
21093   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21094       && dwarf2out_do_cfi_asm ()
21095       && (!(flag_unwind_tables || flag_exceptions)
21096           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21097     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21098 }
21099
21100 /* A helper function for dwarf2out_finish called through
21101    htab_traverse.  Emit one queued .debug_str string.  */
21102
21103 static int
21104 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21105 {
21106   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21107
21108   if (node->form == DW_FORM_strp)
21109     {
21110       switch_to_section (debug_str_section);
21111       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21112       assemble_string (node->str, strlen (node->str) + 1);
21113     }
21114
21115   return 1;
21116 }
21117
21118 #if ENABLE_ASSERT_CHECKING
21119 /* Verify that all marks are clear.  */
21120
21121 static void
21122 verify_marks_clear (dw_die_ref die)
21123 {
21124   dw_die_ref c;
21125
21126   gcc_assert (! die->die_mark);
21127   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21128 }
21129 #endif /* ENABLE_ASSERT_CHECKING */
21130
21131 /* Clear the marks for a die and its children.
21132    Be cool if the mark isn't set.  */
21133
21134 static void
21135 prune_unmark_dies (dw_die_ref die)
21136 {
21137   dw_die_ref c;
21138
21139   if (die->die_mark)
21140     die->die_mark = 0;
21141   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21142 }
21143
21144 /* Given DIE that we're marking as used, find any other dies
21145    it references as attributes and mark them as used.  */
21146
21147 static void
21148 prune_unused_types_walk_attribs (dw_die_ref die)
21149 {
21150   dw_attr_ref a;
21151   unsigned ix;
21152
21153   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21154     {
21155       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21156         {
21157           /* A reference to another DIE.
21158              Make sure that it will get emitted.
21159              If it was broken out into a comdat group, don't follow it.  */
21160           if (! use_debug_types
21161               || a->dw_attr == DW_AT_specification
21162               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21163             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21164         }
21165       /* Set the string's refcount to 0 so that prune_unused_types_mark
21166          accounts properly for it.  */
21167       if (AT_class (a) == dw_val_class_str)
21168         a->dw_attr_val.v.val_str->refcount = 0;
21169     }
21170 }
21171
21172 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21173
21174 static void
21175 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21176 {
21177   dw_die_ref c;
21178
21179   if (die == NULL || die->die_child == NULL)
21180     return;
21181   c = die->die_child;
21182   do
21183     {
21184       switch (c->die_tag)
21185         {
21186         case DW_TAG_template_type_param:
21187         case DW_TAG_template_value_param:
21188         case DW_TAG_GNU_template_template_param:
21189         case DW_TAG_GNU_template_parameter_pack:
21190           prune_unused_types_mark (c, 1);
21191           break;
21192         default:
21193           break;
21194         }
21195       c = c->die_sib;
21196     } while (c && c != die->die_child);
21197 }
21198
21199 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21200    to DIE's children.  */
21201
21202 static void
21203 prune_unused_types_mark (dw_die_ref die, int dokids)
21204 {
21205   dw_die_ref c;
21206
21207   if (die->die_mark == 0)
21208     {
21209       /* We haven't done this node yet.  Mark it as used.  */
21210       die->die_mark = 1;
21211       /* If this is the DIE of a generic type instantiation,
21212          mark the children DIEs that describe its generic parms and
21213          args.  */
21214       prune_unused_types_mark_generic_parms_dies (die);
21215
21216       /* We also have to mark its parents as used.
21217          (But we don't want to mark our parents' kids due to this.)  */
21218       if (die->die_parent)
21219         prune_unused_types_mark (die->die_parent, 0);
21220
21221       /* Mark any referenced nodes.  */
21222       prune_unused_types_walk_attribs (die);
21223
21224       /* If this node is a specification,
21225          also mark the definition, if it exists.  */
21226       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21227         prune_unused_types_mark (die->die_definition, 1);
21228     }
21229
21230   if (dokids && die->die_mark != 2)
21231     {
21232       /* We need to walk the children, but haven't done so yet.
21233          Remember that we've walked the kids.  */
21234       die->die_mark = 2;
21235
21236       /* If this is an array type, we need to make sure our
21237          kids get marked, even if they're types.  If we're
21238          breaking out types into comdat sections, do this
21239          for all type definitions.  */
21240       if (die->die_tag == DW_TAG_array_type
21241           || (use_debug_types
21242               && is_type_die (die) && ! is_declaration_die (die)))
21243         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21244       else
21245         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21246     }
21247 }
21248
21249 /* For local classes, look if any static member functions were emitted
21250    and if so, mark them.  */
21251
21252 static void
21253 prune_unused_types_walk_local_classes (dw_die_ref die)
21254 {
21255   dw_die_ref c;
21256
21257   if (die->die_mark == 2)
21258     return;
21259
21260   switch (die->die_tag)
21261     {
21262     case DW_TAG_structure_type:
21263     case DW_TAG_union_type:
21264     case DW_TAG_class_type:
21265       break;
21266
21267     case DW_TAG_subprogram:
21268       if (!get_AT_flag (die, DW_AT_declaration)
21269           || die->die_definition != NULL)
21270         prune_unused_types_mark (die, 1);
21271       return;
21272
21273     default:
21274       return;
21275     }
21276
21277   /* Mark children.  */
21278   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21279 }
21280
21281 /* Walk the tree DIE and mark types that we actually use.  */
21282
21283 static void
21284 prune_unused_types_walk (dw_die_ref die)
21285 {
21286   dw_die_ref c;
21287
21288   /* Don't do anything if this node is already marked and
21289      children have been marked as well.  */
21290   if (die->die_mark == 2)
21291     return;
21292
21293   switch (die->die_tag)
21294     {
21295     case DW_TAG_structure_type:
21296     case DW_TAG_union_type:
21297     case DW_TAG_class_type:
21298       if (die->die_perennial_p)
21299         break;
21300
21301       for (c = die->die_parent; c; c = c->die_parent)
21302         if (c->die_tag == DW_TAG_subprogram)
21303           break;
21304
21305       /* Finding used static member functions inside of classes
21306          is needed just for local classes, because for other classes
21307          static member function DIEs with DW_AT_specification
21308          are emitted outside of the DW_TAG_*_type.  If we ever change
21309          it, we'd need to call this even for non-local classes.  */
21310       if (c)
21311         prune_unused_types_walk_local_classes (die);
21312
21313       /* It's a type node --- don't mark it.  */
21314       return;
21315
21316     case DW_TAG_const_type:
21317     case DW_TAG_packed_type:
21318     case DW_TAG_pointer_type:
21319     case DW_TAG_reference_type:
21320     case DW_TAG_rvalue_reference_type:
21321     case DW_TAG_volatile_type:
21322     case DW_TAG_typedef:
21323     case DW_TAG_array_type:
21324     case DW_TAG_interface_type:
21325     case DW_TAG_friend:
21326     case DW_TAG_variant_part:
21327     case DW_TAG_enumeration_type:
21328     case DW_TAG_subroutine_type:
21329     case DW_TAG_string_type:
21330     case DW_TAG_set_type:
21331     case DW_TAG_subrange_type:
21332     case DW_TAG_ptr_to_member_type:
21333     case DW_TAG_file_type:
21334       if (die->die_perennial_p)
21335         break;
21336
21337       /* It's a type node --- don't mark it.  */
21338       return;
21339
21340     default:
21341       /* Mark everything else.  */
21342       break;
21343   }
21344
21345   if (die->die_mark == 0)
21346     {
21347       die->die_mark = 1;
21348
21349       /* Now, mark any dies referenced from here.  */
21350       prune_unused_types_walk_attribs (die);
21351     }
21352
21353   die->die_mark = 2;
21354
21355   /* Mark children.  */
21356   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21357 }
21358
21359 /* Increment the string counts on strings referred to from DIE's
21360    attributes.  */
21361
21362 static void
21363 prune_unused_types_update_strings (dw_die_ref die)
21364 {
21365   dw_attr_ref a;
21366   unsigned ix;
21367
21368   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21369     if (AT_class (a) == dw_val_class_str)
21370       {
21371         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21372         s->refcount++;
21373         /* Avoid unnecessarily putting strings that are used less than
21374            twice in the hash table.  */
21375         if (s->refcount
21376             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21377           {
21378             void ** slot;
21379             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21380                                              htab_hash_string (s->str),
21381                                              INSERT);
21382             gcc_assert (*slot == NULL);
21383             *slot = s;
21384           }
21385       }
21386 }
21387
21388 /* Remove from the tree DIE any dies that aren't marked.  */
21389
21390 static void
21391 prune_unused_types_prune (dw_die_ref die)
21392 {
21393   dw_die_ref c;
21394
21395   gcc_assert (die->die_mark);
21396   prune_unused_types_update_strings (die);
21397
21398   if (! die->die_child)
21399     return;
21400
21401   c = die->die_child;
21402   do {
21403     dw_die_ref prev = c;
21404     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21405       if (c == die->die_child)
21406         {
21407           /* No marked children between 'prev' and the end of the list.  */
21408           if (prev == c)
21409             /* No marked children at all.  */
21410             die->die_child = NULL;
21411           else
21412             {
21413               prev->die_sib = c->die_sib;
21414               die->die_child = prev;
21415             }
21416           return;
21417         }
21418
21419     if (c != prev->die_sib)
21420       prev->die_sib = c;
21421     prune_unused_types_prune (c);
21422   } while (c != die->die_child);
21423 }
21424
21425 /* Remove dies representing declarations that we never use.  */
21426
21427 static void
21428 prune_unused_types (void)
21429 {
21430   unsigned int i;
21431   limbo_die_node *node;
21432   comdat_type_node *ctnode;
21433   pubname_ref pub;
21434   dw_die_ref base_type;
21435
21436 #if ENABLE_ASSERT_CHECKING
21437   /* All the marks should already be clear.  */
21438   verify_marks_clear (comp_unit_die ());
21439   for (node = limbo_die_list; node; node = node->next)
21440     verify_marks_clear (node->die);
21441   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21442     verify_marks_clear (ctnode->root_die);
21443 #endif /* ENABLE_ASSERT_CHECKING */
21444
21445   /* Mark types that are used in global variables.  */
21446   premark_types_used_by_global_vars ();
21447
21448   /* Set the mark on nodes that are actually used.  */
21449   prune_unused_types_walk (comp_unit_die ());
21450   for (node = limbo_die_list; node; node = node->next)
21451     prune_unused_types_walk (node->die);
21452   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21453     {
21454       prune_unused_types_walk (ctnode->root_die);
21455       prune_unused_types_mark (ctnode->type_die, 1);
21456     }
21457
21458   /* Also set the mark on nodes referenced from the
21459      pubname_table.  */
21460   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21461     prune_unused_types_mark (pub->die, 1);
21462   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21463     prune_unused_types_mark (base_type, 1);
21464
21465   if (debug_str_hash)
21466     htab_empty (debug_str_hash);
21467   prune_unused_types_prune (comp_unit_die ());
21468   for (node = limbo_die_list; node; node = node->next)
21469     prune_unused_types_prune (node->die);
21470   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21471     prune_unused_types_prune (ctnode->root_die);
21472
21473   /* Leave the marks clear.  */
21474   prune_unmark_dies (comp_unit_die ());
21475   for (node = limbo_die_list; node; node = node->next)
21476     prune_unmark_dies (node->die);
21477   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21478     prune_unmark_dies (ctnode->root_die);
21479 }
21480
21481 /* Set the parameter to true if there are any relative pathnames in
21482    the file table.  */
21483 static int
21484 file_table_relative_p (void ** slot, void *param)
21485 {
21486   bool *p = (bool *) param;
21487   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21488   if (!IS_ABSOLUTE_PATH (d->filename))
21489     {
21490       *p = true;
21491       return 0;
21492     }
21493   return 1;
21494 }
21495
21496 /* Routines to manipulate hash table of comdat type units.  */
21497
21498 static hashval_t
21499 htab_ct_hash (const void *of)
21500 {
21501   hashval_t h;
21502   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21503
21504   memcpy (&h, type_node->signature, sizeof (h));
21505   return h;
21506 }
21507
21508 static int
21509 htab_ct_eq (const void *of1, const void *of2)
21510 {
21511   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21512   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21513
21514   return (! memcmp (type_node_1->signature, type_node_2->signature,
21515                     DWARF_TYPE_SIGNATURE_SIZE));
21516 }
21517
21518 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21519    to the location it would have been added, should we know its
21520    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21521    probably improve compactness of debug info, removing equivalent
21522    abbrevs, and hide any differences caused by deferring the
21523    computation of the assembler name, triggered by e.g. PCH.  */
21524
21525 static inline void
21526 move_linkage_attr (dw_die_ref die)
21527 {
21528   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21529   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21530
21531   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21532               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21533
21534   while (--ix > 0)
21535     {
21536       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21537
21538       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21539         break;
21540     }
21541
21542   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21543     {
21544       VEC_pop (dw_attr_node, die->die_attr);
21545       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21546     }
21547 }
21548
21549 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21550    referenced from typed stack ops and count how often they are used.  */
21551
21552 static void
21553 mark_base_types (dw_loc_descr_ref loc)
21554 {
21555   dw_die_ref base_type = NULL;
21556
21557   for (; loc; loc = loc->dw_loc_next)
21558     {
21559       switch (loc->dw_loc_opc)
21560         {
21561         case DW_OP_GNU_regval_type:
21562         case DW_OP_GNU_deref_type:
21563           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21564           break;
21565         case DW_OP_GNU_convert:
21566         case DW_OP_GNU_reinterpret:
21567           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21568             continue;
21569           /* FALLTHRU */
21570         case DW_OP_GNU_const_type:
21571           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21572           break;
21573         case DW_OP_GNU_entry_value:
21574           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21575           continue;
21576         default:
21577           continue;
21578         }
21579       gcc_assert (base_type->die_parent == comp_unit_die ());
21580       if (base_type->die_mark)
21581         base_type->die_mark++;
21582       else
21583         {
21584           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21585           base_type->die_mark = 1;
21586         }
21587     }
21588 }
21589
21590 /* Comparison function for sorting marked base types.  */
21591
21592 static int
21593 base_type_cmp (const void *x, const void *y)
21594 {
21595   dw_die_ref dx = *(const dw_die_ref *) x;
21596   dw_die_ref dy = *(const dw_die_ref *) y;
21597   unsigned int byte_size1, byte_size2;
21598   unsigned int encoding1, encoding2;
21599   if (dx->die_mark > dy->die_mark)
21600     return -1;
21601   if (dx->die_mark < dy->die_mark)
21602     return 1;
21603   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21604   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21605   if (byte_size1 < byte_size2)
21606     return 1;
21607   if (byte_size1 > byte_size2)
21608     return -1;
21609   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21610   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21611   if (encoding1 < encoding2)
21612     return 1;
21613   if (encoding1 > encoding2)
21614     return -1;
21615   return 0;
21616 }
21617
21618 /* Move base types marked by mark_base_types as early as possible
21619    in the CU, sorted by decreasing usage count both to make the
21620    uleb128 references as small as possible and to make sure they
21621    will have die_offset already computed by calc_die_sizes when
21622    sizes of typed stack loc ops is computed.  */
21623
21624 static void
21625 move_marked_base_types (void)
21626 {
21627   unsigned int i;
21628   dw_die_ref base_type, die, c;
21629
21630   if (VEC_empty (dw_die_ref, base_types))
21631     return;
21632
21633   /* Sort by decreasing usage count, they will be added again in that
21634      order later on.  */
21635   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21636   die = comp_unit_die ();
21637   c = die->die_child;
21638   do
21639     {
21640       dw_die_ref prev = c;
21641       c = c->die_sib;
21642       while (c->die_mark)
21643         {
21644           remove_child_with_prev (c, prev);
21645           /* As base types got marked, there must be at least
21646              one node other than DW_TAG_base_type.  */
21647           gcc_assert (c != c->die_sib);
21648           c = c->die_sib;
21649         }
21650     }
21651   while (c != die->die_child);
21652   gcc_assert (die->die_child);
21653   c = die->die_child;
21654   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21655     {
21656       base_type->die_mark = 0;
21657       base_type->die_sib = c->die_sib;
21658       c->die_sib = base_type;
21659       c = base_type;
21660     }
21661 }
21662
21663 /* Helper function for resolve_addr, attempt to resolve
21664    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21665    SYMBOL_REFs refer to variables emitted in the current CU.  */
21666
21667 static int
21668 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21669 {
21670   rtx rtl = *addr;
21671
21672   if (GET_CODE (rtl) == CONST_STRING)
21673     {
21674       size_t len = strlen (XSTR (rtl, 0)) + 1;
21675       tree t = build_string (len, XSTR (rtl, 0));
21676       tree tlen = size_int (len - 1);
21677       TREE_TYPE (t)
21678         = build_array_type (char_type_node, build_index_type (tlen));
21679       rtl = lookup_constant_def (t);
21680       if (!rtl || !MEM_P (rtl))
21681         return 1;
21682       rtl = XEXP (rtl, 0);
21683       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21684       *addr = rtl;
21685       return 0;
21686     }
21687
21688   if (GET_CODE (rtl) == SYMBOL_REF
21689       && SYMBOL_REF_DECL (rtl))
21690     {
21691       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21692         {
21693           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21694             return 1;
21695         }
21696       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21697         return 1;
21698     }
21699
21700   if (GET_CODE (rtl) == CONST
21701       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21702     return 1;
21703
21704   return 0;
21705 }
21706
21707 /* Helper function for resolve_addr, handle one location
21708    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21709    the location list couldn't be resolved.  */
21710
21711 static bool
21712 resolve_addr_in_expr (dw_loc_descr_ref loc)
21713 {
21714   dw_loc_descr_ref keep = NULL;
21715   for (; loc; loc = loc->dw_loc_next)
21716     switch (loc->dw_loc_opc)
21717       {
21718       case DW_OP_addr:
21719         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21720           return false;
21721         break;
21722       case DW_OP_const4u:
21723       case DW_OP_const8u:
21724         if (loc->dtprel
21725             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21726           return false;
21727         break;
21728       case DW_OP_plus_uconst:
21729         if (size_of_loc_descr (loc)
21730             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21731               + 1
21732             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21733           {
21734             dw_loc_descr_ref repl
21735               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21736             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21737             add_loc_descr (&repl, loc->dw_loc_next);
21738             *loc = *repl;
21739           }
21740         break;
21741       case DW_OP_implicit_value:
21742         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21743             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21744           return false;
21745         break;
21746       case DW_OP_GNU_implicit_pointer:
21747       case DW_OP_GNU_parameter_ref:
21748         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21749           {
21750             dw_die_ref ref
21751               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21752             if (ref == NULL)
21753               return false;
21754             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21755             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21756             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21757           }
21758         break;
21759       case DW_OP_GNU_const_type:
21760       case DW_OP_GNU_regval_type:
21761       case DW_OP_GNU_deref_type:
21762       case DW_OP_GNU_convert:
21763       case DW_OP_GNU_reinterpret:
21764         while (loc->dw_loc_next
21765                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21766           {
21767             dw_die_ref base1, base2;
21768             unsigned enc1, enc2, size1, size2;
21769             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21770                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21771               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21772             else if (loc->dw_loc_oprnd1.val_class
21773                      == dw_val_class_unsigned_const)
21774               break;
21775             else
21776               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21777             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21778                 == dw_val_class_unsigned_const)
21779               break;
21780             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21781             gcc_assert (base1->die_tag == DW_TAG_base_type
21782                         && base2->die_tag == DW_TAG_base_type);
21783             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21784             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21785             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21786             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21787             if (size1 == size2
21788                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21789                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21790                      && loc != keep)
21791                     || enc1 == enc2))
21792               {
21793                 /* Optimize away next DW_OP_GNU_convert after
21794                    adjusting LOC's base type die reference.  */
21795                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21796                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21797                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21798                 else
21799                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21800                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21801                 continue;
21802               }
21803             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21804                point typed stack entry.  */
21805             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21806               keep = loc->dw_loc_next;
21807             break;
21808           }
21809         break;
21810       default:
21811         break;
21812       }
21813   return true;
21814 }
21815
21816 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21817    an address in .rodata section if the string literal is emitted there,
21818    or remove the containing location list or replace DW_AT_const_value
21819    with DW_AT_location and empty location expression, if it isn't found
21820    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21821    to something that has been emitted in the current CU.  */
21822
21823 static void
21824 resolve_addr (dw_die_ref die)
21825 {
21826   dw_die_ref c;
21827   dw_attr_ref a;
21828   dw_loc_list_ref *curr, *start, loc;
21829   unsigned ix;
21830
21831   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21832     switch (AT_class (a))
21833       {
21834       case dw_val_class_loc_list:
21835         start = curr = AT_loc_list_ptr (a);
21836         loc = *curr;
21837         gcc_assert (loc);
21838         /* The same list can be referenced more than once.  See if we have
21839            already recorded the result from a previous pass.  */
21840         if (loc->replaced)
21841           *curr = loc->dw_loc_next;
21842         else if (!loc->resolved_addr)
21843           {
21844             /* As things stand, we do not expect or allow one die to
21845                reference a suffix of another die's location list chain.
21846                References must be identical or completely separate.
21847                There is therefore no need to cache the result of this
21848                pass on any list other than the first; doing so
21849                would lead to unnecessary writes.  */
21850             while (*curr)
21851               {
21852                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21853                 if (!resolve_addr_in_expr ((*curr)->expr))
21854                   {
21855                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21856                     if (next && (*curr)->ll_symbol)
21857                       {
21858                         gcc_assert (!next->ll_symbol);
21859                         next->ll_symbol = (*curr)->ll_symbol;
21860                       }
21861                     *curr = next;
21862                   }
21863                 else
21864                   {
21865                     mark_base_types ((*curr)->expr);
21866                     curr = &(*curr)->dw_loc_next;
21867                   }
21868               }
21869             if (loc == *start)
21870               loc->resolved_addr = 1;
21871             else
21872               {
21873                 loc->replaced = 1;
21874                 loc->dw_loc_next = *start;
21875               }
21876           }
21877         if (!*start)
21878           {
21879             remove_AT (die, a->dw_attr);
21880             ix--;
21881           }
21882         break;
21883       case dw_val_class_loc:
21884         {
21885           dw_loc_descr_ref l = AT_loc (a);
21886           /* For -gdwarf-2 don't attempt to optimize
21887              DW_AT_data_member_location containing
21888              DW_OP_plus_uconst - older consumers might
21889              rely on it being that op instead of a more complex,
21890              but shorter, location description.  */
21891           if ((dwarf_version > 2
21892                || a->dw_attr != DW_AT_data_member_location
21893                || l == NULL
21894                || l->dw_loc_opc != DW_OP_plus_uconst
21895                || l->dw_loc_next != NULL)
21896               && !resolve_addr_in_expr (l))
21897             {
21898               remove_AT (die, a->dw_attr);
21899               ix--;
21900             }
21901           else
21902             mark_base_types (l);
21903         }
21904         break;
21905       case dw_val_class_addr:
21906         if (a->dw_attr == DW_AT_const_value
21907             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21908           {
21909             remove_AT (die, a->dw_attr);
21910             ix--;
21911           }
21912         if (die->die_tag == DW_TAG_GNU_call_site
21913             && a->dw_attr == DW_AT_abstract_origin)
21914           {
21915             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21916             dw_die_ref tdie = lookup_decl_die (tdecl);
21917             if (tdie == NULL
21918                 && DECL_EXTERNAL (tdecl)
21919                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21920               {
21921                 force_decl_die (tdecl);
21922                 tdie = lookup_decl_die (tdecl);
21923               }
21924             if (tdie)
21925               {
21926                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21927                 a->dw_attr_val.v.val_die_ref.die = tdie;
21928                 a->dw_attr_val.v.val_die_ref.external = 0;
21929               }
21930             else
21931               {
21932                 remove_AT (die, a->dw_attr);
21933                 ix--;
21934               }
21935           }
21936         break;
21937       default:
21938         break;
21939       }
21940
21941   FOR_EACH_CHILD (die, c, resolve_addr (c));
21942 }
21943 \f
21944 /* Helper routines for optimize_location_lists.
21945    This pass tries to share identical local lists in .debug_loc
21946    section.  */
21947
21948 /* Iteratively hash operands of LOC opcode.  */
21949
21950 static inline hashval_t
21951 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21952 {
21953   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21954   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21955
21956   switch (loc->dw_loc_opc)
21957     {
21958     case DW_OP_const4u:
21959     case DW_OP_const8u:
21960       if (loc->dtprel)
21961         goto hash_addr;
21962       /* FALLTHRU */
21963     case DW_OP_const1u:
21964     case DW_OP_const1s:
21965     case DW_OP_const2u:
21966     case DW_OP_const2s:
21967     case DW_OP_const4s:
21968     case DW_OP_const8s:
21969     case DW_OP_constu:
21970     case DW_OP_consts:
21971     case DW_OP_pick:
21972     case DW_OP_plus_uconst:
21973     case DW_OP_breg0:
21974     case DW_OP_breg1:
21975     case DW_OP_breg2:
21976     case DW_OP_breg3:
21977     case DW_OP_breg4:
21978     case DW_OP_breg5:
21979     case DW_OP_breg6:
21980     case DW_OP_breg7:
21981     case DW_OP_breg8:
21982     case DW_OP_breg9:
21983     case DW_OP_breg10:
21984     case DW_OP_breg11:
21985     case DW_OP_breg12:
21986     case DW_OP_breg13:
21987     case DW_OP_breg14:
21988     case DW_OP_breg15:
21989     case DW_OP_breg16:
21990     case DW_OP_breg17:
21991     case DW_OP_breg18:
21992     case DW_OP_breg19:
21993     case DW_OP_breg20:
21994     case DW_OP_breg21:
21995     case DW_OP_breg22:
21996     case DW_OP_breg23:
21997     case DW_OP_breg24:
21998     case DW_OP_breg25:
21999     case DW_OP_breg26:
22000     case DW_OP_breg27:
22001     case DW_OP_breg28:
22002     case DW_OP_breg29:
22003     case DW_OP_breg30:
22004     case DW_OP_breg31:
22005     case DW_OP_regx:
22006     case DW_OP_fbreg:
22007     case DW_OP_piece:
22008     case DW_OP_deref_size:
22009     case DW_OP_xderef_size:
22010       hash = iterative_hash_object (val1->v.val_int, hash);
22011       break;
22012     case DW_OP_skip:
22013     case DW_OP_bra:
22014       {
22015         int offset;
22016
22017         gcc_assert (val1->val_class == dw_val_class_loc);
22018         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22019         hash = iterative_hash_object (offset, hash);
22020       }
22021       break;
22022     case DW_OP_implicit_value:
22023       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22024       switch (val2->val_class)
22025         {
22026         case dw_val_class_const:
22027           hash = iterative_hash_object (val2->v.val_int, hash);
22028           break;
22029         case dw_val_class_vec:
22030           {
22031             unsigned int elt_size = val2->v.val_vec.elt_size;
22032             unsigned int len = val2->v.val_vec.length;
22033
22034             hash = iterative_hash_object (elt_size, hash);
22035             hash = iterative_hash_object (len, hash);
22036             hash = iterative_hash (val2->v.val_vec.array,
22037                                    len * elt_size, hash);
22038           }
22039           break;
22040         case dw_val_class_const_double:
22041           hash = iterative_hash_object (val2->v.val_double.low, hash);
22042           hash = iterative_hash_object (val2->v.val_double.high, hash);
22043           break;
22044         case dw_val_class_addr:
22045           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22046           break;
22047         default:
22048           gcc_unreachable ();
22049         }
22050       break;
22051     case DW_OP_bregx:
22052     case DW_OP_bit_piece:
22053       hash = iterative_hash_object (val1->v.val_int, hash);
22054       hash = iterative_hash_object (val2->v.val_int, hash);
22055       break;
22056     case DW_OP_addr:
22057     hash_addr:
22058       if (loc->dtprel)
22059         {
22060           unsigned char dtprel = 0xd1;
22061           hash = iterative_hash_object (dtprel, hash);
22062         }
22063       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22064       break;
22065     case DW_OP_GNU_implicit_pointer:
22066       hash = iterative_hash_object (val2->v.val_int, hash);
22067       break;
22068     case DW_OP_GNU_entry_value:
22069       hash = hash_loc_operands (val1->v.val_loc, hash);
22070       break;
22071     case DW_OP_GNU_regval_type:
22072     case DW_OP_GNU_deref_type:
22073       {
22074         unsigned int byte_size
22075           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22076         unsigned int encoding
22077           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22078         hash = iterative_hash_object (val1->v.val_int, hash);
22079         hash = iterative_hash_object (byte_size, hash);
22080         hash = iterative_hash_object (encoding, hash);
22081       }
22082       break;
22083     case DW_OP_GNU_convert:
22084     case DW_OP_GNU_reinterpret:
22085       if (val1->val_class == dw_val_class_unsigned_const)
22086         {
22087           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22088           break;
22089         }
22090       /* FALLTHRU */
22091     case DW_OP_GNU_const_type:
22092       {
22093         unsigned int byte_size
22094           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22095         unsigned int encoding
22096           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22097         hash = iterative_hash_object (byte_size, hash);
22098         hash = iterative_hash_object (encoding, hash);
22099         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22100           break;
22101         hash = iterative_hash_object (val2->val_class, hash);
22102         switch (val2->val_class)
22103           {
22104           case dw_val_class_const:
22105             hash = iterative_hash_object (val2->v.val_int, hash);
22106             break;
22107           case dw_val_class_vec:
22108             {
22109               unsigned int elt_size = val2->v.val_vec.elt_size;
22110               unsigned int len = val2->v.val_vec.length;
22111
22112               hash = iterative_hash_object (elt_size, hash);
22113               hash = iterative_hash_object (len, hash);
22114               hash = iterative_hash (val2->v.val_vec.array,
22115                                      len * elt_size, hash);
22116             }
22117             break;
22118           case dw_val_class_const_double:
22119             hash = iterative_hash_object (val2->v.val_double.low, hash);
22120             hash = iterative_hash_object (val2->v.val_double.high, hash);
22121             break;
22122           default:
22123             gcc_unreachable ();
22124           }
22125       }
22126       break;
22127
22128     default:
22129       /* Other codes have no operands.  */
22130       break;
22131     }
22132   return hash;
22133 }
22134
22135 /* Iteratively hash the whole DWARF location expression LOC.  */
22136
22137 static inline hashval_t
22138 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22139 {
22140   dw_loc_descr_ref l;
22141   bool sizes_computed = false;
22142   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22143   size_of_locs (loc);
22144
22145   for (l = loc; l != NULL; l = l->dw_loc_next)
22146     {
22147       enum dwarf_location_atom opc = l->dw_loc_opc;
22148       hash = iterative_hash_object (opc, hash);
22149       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22150         {
22151           size_of_locs (loc);
22152           sizes_computed = true;
22153         }
22154       hash = hash_loc_operands (l, hash);
22155     }
22156   return hash;
22157 }
22158
22159 /* Compute hash of the whole location list LIST_HEAD.  */
22160
22161 static inline void
22162 hash_loc_list (dw_loc_list_ref list_head)
22163 {
22164   dw_loc_list_ref curr = list_head;
22165   hashval_t hash = 0;
22166
22167   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22168     {
22169       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22170       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22171       if (curr->section)
22172         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22173                                hash);
22174       hash = hash_locs (curr->expr, hash);
22175     }
22176   list_head->hash = hash;
22177 }
22178
22179 /* Return true if X and Y opcodes have the same operands.  */
22180
22181 static inline bool
22182 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22183 {
22184   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22185   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22186   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22187   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22188
22189   switch (x->dw_loc_opc)
22190     {
22191     case DW_OP_const4u:
22192     case DW_OP_const8u:
22193       if (x->dtprel)
22194         goto hash_addr;
22195       /* FALLTHRU */
22196     case DW_OP_const1u:
22197     case DW_OP_const1s:
22198     case DW_OP_const2u:
22199     case DW_OP_const2s:
22200     case DW_OP_const4s:
22201     case DW_OP_const8s:
22202     case DW_OP_constu:
22203     case DW_OP_consts:
22204     case DW_OP_pick:
22205     case DW_OP_plus_uconst:
22206     case DW_OP_breg0:
22207     case DW_OP_breg1:
22208     case DW_OP_breg2:
22209     case DW_OP_breg3:
22210     case DW_OP_breg4:
22211     case DW_OP_breg5:
22212     case DW_OP_breg6:
22213     case DW_OP_breg7:
22214     case DW_OP_breg8:
22215     case DW_OP_breg9:
22216     case DW_OP_breg10:
22217     case DW_OP_breg11:
22218     case DW_OP_breg12:
22219     case DW_OP_breg13:
22220     case DW_OP_breg14:
22221     case DW_OP_breg15:
22222     case DW_OP_breg16:
22223     case DW_OP_breg17:
22224     case DW_OP_breg18:
22225     case DW_OP_breg19:
22226     case DW_OP_breg20:
22227     case DW_OP_breg21:
22228     case DW_OP_breg22:
22229     case DW_OP_breg23:
22230     case DW_OP_breg24:
22231     case DW_OP_breg25:
22232     case DW_OP_breg26:
22233     case DW_OP_breg27:
22234     case DW_OP_breg28:
22235     case DW_OP_breg29:
22236     case DW_OP_breg30:
22237     case DW_OP_breg31:
22238     case DW_OP_regx:
22239     case DW_OP_fbreg:
22240     case DW_OP_piece:
22241     case DW_OP_deref_size:
22242     case DW_OP_xderef_size:
22243       return valx1->v.val_int == valy1->v.val_int;
22244     case DW_OP_skip:
22245     case DW_OP_bra:
22246       gcc_assert (valx1->val_class == dw_val_class_loc
22247                   && valy1->val_class == dw_val_class_loc
22248                   && x->dw_loc_addr == y->dw_loc_addr);
22249       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22250     case DW_OP_implicit_value:
22251       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22252           || valx2->val_class != valy2->val_class)
22253         return false;
22254       switch (valx2->val_class)
22255         {
22256         case dw_val_class_const:
22257           return valx2->v.val_int == valy2->v.val_int;
22258         case dw_val_class_vec:
22259           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22260                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22261                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22262                             valx2->v.val_vec.elt_size
22263                             * valx2->v.val_vec.length) == 0;
22264         case dw_val_class_const_double:
22265           return valx2->v.val_double.low == valy2->v.val_double.low
22266                  && valx2->v.val_double.high == valy2->v.val_double.high;
22267         case dw_val_class_addr:
22268           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22269         default:
22270           gcc_unreachable ();
22271         }
22272     case DW_OP_bregx:
22273     case DW_OP_bit_piece:
22274       return valx1->v.val_int == valy1->v.val_int
22275              && valx2->v.val_int == valy2->v.val_int;
22276     case DW_OP_addr:
22277     hash_addr:
22278       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22279     case DW_OP_GNU_implicit_pointer:
22280       return valx1->val_class == dw_val_class_die_ref
22281              && valx1->val_class == valy1->val_class
22282              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22283              && valx2->v.val_int == valy2->v.val_int;
22284     case DW_OP_GNU_entry_value:
22285       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22286     case DW_OP_GNU_const_type:
22287       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22288           || valx2->val_class != valy2->val_class)
22289         return false;
22290       switch (valx2->val_class)
22291         {
22292         case dw_val_class_const:
22293           return valx2->v.val_int == valy2->v.val_int;
22294         case dw_val_class_vec:
22295           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22296                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22297                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22298                             valx2->v.val_vec.elt_size
22299                             * valx2->v.val_vec.length) == 0;
22300         case dw_val_class_const_double:
22301           return valx2->v.val_double.low == valy2->v.val_double.low
22302                  && valx2->v.val_double.high == valy2->v.val_double.high;
22303         default:
22304           gcc_unreachable ();
22305         }
22306     case DW_OP_GNU_regval_type:
22307     case DW_OP_GNU_deref_type:
22308       return valx1->v.val_int == valy1->v.val_int
22309              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22310     case DW_OP_GNU_convert:
22311     case DW_OP_GNU_reinterpret:
22312       if (valx1->val_class != valy1->val_class)
22313         return false;
22314       if (valx1->val_class == dw_val_class_unsigned_const)
22315         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22316       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22317     case DW_OP_GNU_parameter_ref:
22318       return valx1->val_class == dw_val_class_die_ref
22319              && valx1->val_class == valy1->val_class
22320              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22321     default:
22322       /* Other codes have no operands.  */
22323       return true;
22324     }
22325 }
22326
22327 /* Return true if DWARF location expressions X and Y are the same.  */
22328
22329 static inline bool
22330 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22331 {
22332   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22333     if (x->dw_loc_opc != y->dw_loc_opc
22334         || x->dtprel != y->dtprel
22335         || !compare_loc_operands (x, y))
22336       break;
22337   return x == NULL && y == NULL;
22338 }
22339
22340 /* Return precomputed hash of location list X.  */
22341
22342 static hashval_t
22343 loc_list_hash (const void *x)
22344 {
22345   return ((const struct dw_loc_list_struct *) x)->hash;
22346 }
22347
22348 /* Return 1 if location lists X and Y are the same.  */
22349
22350 static int
22351 loc_list_eq (const void *x, const void *y)
22352 {
22353   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22354   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22355   if (a == b)
22356     return 1;
22357   if (a->hash != b->hash)
22358     return 0;
22359   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22360     if (strcmp (a->begin, b->begin) != 0
22361         || strcmp (a->end, b->end) != 0
22362         || (a->section == NULL) != (b->section == NULL)
22363         || (a->section && strcmp (a->section, b->section) != 0)
22364         || !compare_locs (a->expr, b->expr))
22365       break;
22366   return a == NULL && b == NULL;
22367 }
22368
22369 /* Recursively optimize location lists referenced from DIE
22370    children and share them whenever possible.  */
22371
22372 static void
22373 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22374 {
22375   dw_die_ref c;
22376   dw_attr_ref a;
22377   unsigned ix;
22378   void **slot;
22379
22380   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22381     if (AT_class (a) == dw_val_class_loc_list)
22382       {
22383         dw_loc_list_ref list = AT_loc_list (a);
22384         /* TODO: perform some optimizations here, before hashing
22385            it and storing into the hash table.  */
22386         hash_loc_list (list);
22387         slot = htab_find_slot_with_hash (htab, list, list->hash,
22388                                          INSERT);
22389         if (*slot == NULL)
22390           *slot = (void *) list;
22391         else
22392           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22393       }
22394
22395   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22396 }
22397
22398 /* Optimize location lists referenced from DIE
22399    children and share them whenever possible.  */
22400
22401 static void
22402 optimize_location_lists (dw_die_ref die)
22403 {
22404   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22405   optimize_location_lists_1 (die, htab);
22406   htab_delete (htab);
22407 }
22408 \f
22409 /* Output stuff that dwarf requires at the end of every file,
22410    and generate the DWARF-2 debugging info.  */
22411
22412 static void
22413 dwarf2out_finish (const char *filename)
22414 {
22415   limbo_die_node *node, *next_node;
22416   comdat_type_node *ctnode;
22417   htab_t comdat_type_table;
22418   unsigned int i;
22419
22420   /* PCH might result in DW_AT_producer string being restored from the
22421      header compilation, fix it up if needed.  */
22422   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22423   if (strcmp (AT_string (producer), producer_string) != 0)
22424     {
22425       struct indirect_string_node *node = find_AT_string (producer_string);
22426       producer->dw_attr_val.v.val_str = node;
22427     }
22428
22429   gen_scheduled_generic_parms_dies ();
22430   gen_remaining_tmpl_value_param_die_attribute ();
22431
22432   /* Add the name for the main input file now.  We delayed this from
22433      dwarf2out_init to avoid complications with PCH.  */
22434   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22435   if (!IS_ABSOLUTE_PATH (filename))
22436     add_comp_dir_attribute (comp_unit_die ());
22437   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22438     {
22439       bool p = false;
22440       htab_traverse (file_table, file_table_relative_p, &p);
22441       if (p)
22442         add_comp_dir_attribute (comp_unit_die ());
22443     }
22444
22445   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22446     {
22447       add_location_or_const_value_attribute (
22448         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22449         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22450         false,
22451         DW_AT_location);
22452     }
22453
22454   /* Traverse the limbo die list, and add parent/child links.  The only
22455      dies without parents that should be here are concrete instances of
22456      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22457      For concrete instances, we can get the parent die from the abstract
22458      instance.  */
22459   for (node = limbo_die_list; node; node = next_node)
22460     {
22461       dw_die_ref die = node->die;
22462       next_node = node->next;
22463
22464       if (die->die_parent == NULL)
22465         {
22466           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22467
22468           if (origin && origin->die_parent)
22469             add_child_die (origin->die_parent, die);
22470           else if (is_cu_die (die))
22471             ;
22472           else if (seen_error ())
22473             /* It's OK to be confused by errors in the input.  */
22474             add_child_die (comp_unit_die (), die);
22475           else
22476             {
22477               /* In certain situations, the lexical block containing a
22478                  nested function can be optimized away, which results
22479                  in the nested function die being orphaned.  Likewise
22480                  with the return type of that nested function.  Force
22481                  this to be a child of the containing function.
22482
22483                  It may happen that even the containing function got fully
22484                  inlined and optimized out.  In that case we are lost and
22485                  assign the empty child.  This should not be big issue as
22486                  the function is likely unreachable too.  */
22487               tree context = NULL_TREE;
22488
22489               gcc_assert (node->created_for);
22490
22491               if (DECL_P (node->created_for))
22492                 context = DECL_CONTEXT (node->created_for);
22493               else if (TYPE_P (node->created_for))
22494                 context = TYPE_CONTEXT (node->created_for);
22495
22496               origin = get_context_die (context);
22497               add_child_die (origin, die);
22498             }
22499         }
22500     }
22501
22502   limbo_die_list = NULL;
22503
22504 #if ENABLE_ASSERT_CHECKING
22505   {
22506     dw_die_ref die = comp_unit_die (), c;
22507     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22508   }
22509 #endif
22510   resolve_addr (comp_unit_die ());
22511   move_marked_base_types ();
22512
22513   for (node = deferred_asm_name; node; node = node->next)
22514     {
22515       tree decl = node->created_for;
22516       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22517         {
22518           add_linkage_attr (node->die, decl);
22519           move_linkage_attr (node->die);
22520         }
22521     }
22522
22523   deferred_asm_name = NULL;
22524
22525   /* Walk through the list of incomplete types again, trying once more to
22526      emit full debugging info for them.  */
22527   retry_incomplete_types ();
22528
22529   if (flag_eliminate_unused_debug_types)
22530     prune_unused_types ();
22531
22532   /* Generate separate CUs for each of the include files we've seen.
22533      They will go into limbo_die_list.  */
22534   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22535     break_out_includes (comp_unit_die ());
22536
22537   /* Generate separate COMDAT sections for type DIEs. */
22538   if (use_debug_types)
22539     {
22540       break_out_comdat_types (comp_unit_die ());
22541
22542       /* Each new type_unit DIE was added to the limbo die list when created.
22543          Since these have all been added to comdat_type_list, clear the
22544          limbo die list.  */
22545       limbo_die_list = NULL;
22546
22547       /* For each new comdat type unit, copy declarations for incomplete
22548          types to make the new unit self-contained (i.e., no direct
22549          references to the main compile unit).  */
22550       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22551         copy_decls_for_unworthy_types (ctnode->root_die);
22552       copy_decls_for_unworthy_types (comp_unit_die ());
22553
22554       /* In the process of copying declarations from one unit to another,
22555          we may have left some declarations behind that are no longer
22556          referenced.  Prune them.  */
22557       prune_unused_types ();
22558     }
22559
22560   /* Traverse the DIE's and add add sibling attributes to those DIE's
22561      that have children.  */
22562   add_sibling_attributes (comp_unit_die ());
22563   for (node = limbo_die_list; node; node = node->next)
22564     add_sibling_attributes (node->die);
22565   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22566     add_sibling_attributes (ctnode->root_die);
22567
22568   /* Output a terminator label for the .text section.  */
22569   switch_to_section (text_section);
22570   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22571   if (cold_text_section)
22572     {
22573       switch_to_section (cold_text_section);
22574       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22575     }
22576
22577   /* We can only use the low/high_pc attributes if all of the code was
22578      in .text.  */
22579   if (!have_multiple_function_sections 
22580       || (dwarf_version < 3 && dwarf_strict))
22581     {
22582       /* Don't add if the CU has no associated code.  */
22583       if (text_section_used)
22584         {
22585           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22586           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22587         }
22588     }
22589   else
22590     {
22591       unsigned fde_idx;
22592       dw_fde_ref fde;
22593       bool range_list_added = false;
22594
22595       if (text_section_used)
22596         add_ranges_by_labels (comp_unit_die (), text_section_label,
22597                               text_end_label, &range_list_added);
22598       if (cold_text_section_used)
22599         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22600                               cold_end_label, &range_list_added);
22601
22602       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22603         {
22604           if (!fde->in_std_section)
22605             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22606                                   fde->dw_fde_end, &range_list_added);
22607           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22608             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22609                                   fde->dw_fde_second_end, &range_list_added);
22610         }
22611
22612       if (range_list_added)
22613         {
22614           /* We need to give .debug_loc and .debug_ranges an appropriate
22615              "base address".  Use zero so that these addresses become
22616              absolute.  Historically, we've emitted the unexpected
22617              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22618              Emit both to give time for other tools to adapt.  */
22619           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22620           if (! dwarf_strict && dwarf_version < 4)
22621             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22622
22623           add_ranges (NULL);
22624         }
22625     }
22626
22627   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22628     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22629                     debug_line_section_label);
22630
22631   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22632     add_AT_macptr (comp_unit_die (),
22633                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22634                    macinfo_section_label);
22635
22636   if (have_location_lists)
22637     optimize_location_lists (comp_unit_die ());
22638
22639   /* Output all of the compilation units.  We put the main one last so that
22640      the offsets are available to output_pubnames.  */
22641   for (node = limbo_die_list; node; node = node->next)
22642     output_comp_unit (node->die, 0);
22643
22644   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22645   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22646     {
22647       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22648
22649       /* Don't output duplicate types.  */
22650       if (*slot != HTAB_EMPTY_ENTRY)
22651         continue;
22652
22653       /* Add a pointer to the line table for the main compilation unit
22654          so that the debugger can make sense of DW_AT_decl_file
22655          attributes.  */
22656       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22657         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22658                         debug_line_section_label);
22659
22660       output_comdat_type_unit (ctnode);
22661       *slot = ctnode;
22662     }
22663   htab_delete (comdat_type_table);
22664
22665   /* Output the main compilation unit if non-empty or if .debug_macinfo
22666      will be emitted.  */
22667   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22668
22669   /* Output the abbreviation table.  */
22670   if (abbrev_die_table_in_use != 1)
22671     {
22672       switch_to_section (debug_abbrev_section);
22673       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22674       output_abbrev_section ();
22675     }
22676
22677   /* Output location list section if necessary.  */
22678   if (have_location_lists)
22679     {
22680       /* Output the location lists info.  */
22681       switch_to_section (debug_loc_section);
22682       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22683                                    DEBUG_LOC_SECTION_LABEL, 0);
22684       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22685       output_location_lists (comp_unit_die ());
22686     }
22687
22688   /* Output public names table if necessary.  */
22689   if (!VEC_empty (pubname_entry, pubname_table))
22690     {
22691       gcc_assert (info_section_emitted);
22692       switch_to_section (debug_pubnames_section);
22693       output_pubnames (pubname_table);
22694     }
22695
22696   /* Output public types table if necessary.  */
22697   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22698      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22699      simply won't look for the section.  */
22700   if (!VEC_empty (pubname_entry, pubtype_table))
22701     {
22702       bool empty = false;
22703       
22704       if (flag_eliminate_unused_debug_types)
22705         {
22706           /* The pubtypes table might be emptied by pruning unused items.  */
22707           unsigned i;
22708           pubname_ref p;
22709           empty = true;
22710           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22711             if (p->die->die_offset != 0)
22712               {
22713                 empty = false;
22714                 break;
22715               }
22716         }
22717       if (!empty)
22718         {
22719           gcc_assert (info_section_emitted);
22720           switch_to_section (debug_pubtypes_section);
22721           output_pubnames (pubtype_table);
22722         }
22723     }
22724
22725   /* Output the address range information if a CU (.debug_info section)
22726      was emitted.  We output an empty table even if we had no functions
22727      to put in it.  This because the consumer has no way to tell the
22728      difference between an empty table that we omitted and failure to
22729      generate a table that would have contained data.  */
22730   if (info_section_emitted)
22731     {
22732       unsigned long aranges_length = size_of_aranges ();
22733
22734       switch_to_section (debug_aranges_section);
22735       output_aranges (aranges_length);
22736     }
22737
22738   /* Output ranges section if necessary.  */
22739   if (ranges_table_in_use)
22740     {
22741       switch_to_section (debug_ranges_section);
22742       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22743       output_ranges ();
22744     }
22745
22746   /* Have to end the macro section.  */
22747   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22748     {
22749       switch_to_section (debug_macinfo_section);
22750       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22751       if (!VEC_empty (macinfo_entry, macinfo_table))
22752         output_macinfo ();
22753       dw2_asm_output_data (1, 0, "End compilation unit");
22754     }
22755
22756   /* Output the source line correspondence table.  We must do this
22757      even if there is no line information.  Otherwise, on an empty
22758      translation unit, we will generate a present, but empty,
22759      .debug_info section.  IRIX 6.5 `nm' will then complain when
22760      examining the file.  This is done late so that any filenames
22761      used by the debug_info section are marked as 'used'.  */
22762   switch_to_section (debug_line_section);
22763   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22764   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22765     output_line_info ();
22766
22767   /* If we emitted any DW_FORM_strp form attribute, output the string
22768      table too.  */
22769   if (debug_str_hash)
22770     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22771 }
22772
22773 #include "gt-dwarf2out.h"