OSDN Git Service

PR target/51871
[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 dw_die_ref 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                                                          dw_die_ref);
3312 static void break_out_comdat_types (dw_die_ref);
3313 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3314 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3315 static void copy_decls_for_unworthy_types (dw_die_ref);
3316
3317 static hashval_t htab_cu_hash (const void *);
3318 static int htab_cu_eq (const void *, const void *);
3319 static void htab_cu_del (void *);
3320 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3321 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3322 static void add_sibling_attributes (dw_die_ref);
3323 static void build_abbrev_table (dw_die_ref);
3324 static void output_location_lists (dw_die_ref);
3325 static int constant_size (unsigned HOST_WIDE_INT);
3326 static unsigned long size_of_die (dw_die_ref);
3327 static void calc_die_sizes (dw_die_ref);
3328 static void calc_base_type_die_sizes (void);
3329 static void mark_dies (dw_die_ref);
3330 static void unmark_dies (dw_die_ref);
3331 static void unmark_all_dies (dw_die_ref);
3332 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3333 static unsigned long size_of_aranges (void);
3334 static enum dwarf_form value_format (dw_attr_ref);
3335 static void output_value_format (dw_attr_ref);
3336 static void output_abbrev_section (void);
3337 static void output_die_symbol (dw_die_ref);
3338 static void output_die (dw_die_ref);
3339 static void output_compilation_unit_header (void);
3340 static void output_comp_unit (dw_die_ref, int);
3341 static void output_comdat_type_unit (comdat_type_node *);
3342 static const char *dwarf2_name (tree, int);
3343 static void add_pubname (tree, dw_die_ref);
3344 static void add_pubname_string (const char *, dw_die_ref);
3345 static void add_pubtype (tree, dw_die_ref);
3346 static void output_pubnames (VEC (pubname_entry,gc) *);
3347 static void output_aranges (unsigned long);
3348 static unsigned int add_ranges_num (int);
3349 static unsigned int add_ranges (const_tree);
3350 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3351                                   bool *);
3352 static void output_ranges (void);
3353 static dw_line_info_table *new_line_info_table (void);
3354 static void output_line_info (void);
3355 static void output_file_names (void);
3356 static dw_die_ref base_type_die (tree);
3357 static int is_base_type (tree);
3358 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3359 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3360 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3361 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3362 static int type_is_enum (const_tree);
3363 static unsigned int dbx_reg_number (const_rtx);
3364 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3365 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3366 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3367                                                 enum var_init_status);
3368 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3369                                                      enum var_init_status);
3370 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3371                                          enum var_init_status);
3372 static int is_based_loc (const_rtx);
3373 static int resolve_one_addr (rtx *, void *);
3374 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3375                                                enum var_init_status);
3376 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3377                                         enum var_init_status);
3378 static dw_loc_list_ref loc_list_from_tree (tree, int);
3379 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3380 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3381 static tree field_type (const_tree);
3382 static unsigned int simple_type_align_in_bits (const_tree);
3383 static unsigned int simple_decl_align_in_bits (const_tree);
3384 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3385 static HOST_WIDE_INT field_byte_offset (const_tree);
3386 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3387                                          dw_loc_list_ref);
3388 static void add_data_member_location_attribute (dw_die_ref, tree);
3389 static bool add_const_value_attribute (dw_die_ref, rtx);
3390 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3391 static void insert_double (double_int, unsigned char *);
3392 static void insert_float (const_rtx, unsigned char *);
3393 static rtx rtl_for_decl_location (tree);
3394 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3395                                                    enum dwarf_attribute);
3396 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3397 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3398 static void add_name_attribute (dw_die_ref, const char *);
3399 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3400 static void add_comp_dir_attribute (dw_die_ref);
3401 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3402 static void add_subscript_info (dw_die_ref, tree, bool);
3403 static void add_byte_size_attribute (dw_die_ref, tree);
3404 static void add_bit_offset_attribute (dw_die_ref, tree);
3405 static void add_bit_size_attribute (dw_die_ref, tree);
3406 static void add_prototyped_attribute (dw_die_ref, tree);
3407 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3408 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3409 static void add_src_coords_attributes (dw_die_ref, tree);
3410 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3411 static void push_decl_scope (tree);
3412 static void pop_decl_scope (void);
3413 static dw_die_ref scope_die_for (tree, dw_die_ref);
3414 static inline int local_scope_p (dw_die_ref);
3415 static inline int class_scope_p (dw_die_ref);
3416 static inline int class_or_namespace_scope_p (dw_die_ref);
3417 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3418 static void add_calling_convention_attribute (dw_die_ref, tree);
3419 static const char *type_tag (const_tree);
3420 static tree member_declared_type (const_tree);
3421 #if 0
3422 static const char *decl_start_label (tree);
3423 #endif
3424 static void gen_array_type_die (tree, dw_die_ref);
3425 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3426 #if 0
3427 static void gen_entry_point_die (tree, dw_die_ref);
3428 #endif
3429 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3430 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3431 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3432 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3433 static void gen_formal_types_die (tree, dw_die_ref);
3434 static void gen_subprogram_die (tree, dw_die_ref);
3435 static void gen_variable_die (tree, tree, dw_die_ref);
3436 static void gen_const_die (tree, dw_die_ref);
3437 static void gen_label_die (tree, dw_die_ref);
3438 static void gen_lexical_block_die (tree, dw_die_ref, int);
3439 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3440 static void gen_field_die (tree, dw_die_ref);
3441 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3442 static dw_die_ref gen_compile_unit_die (const char *);
3443 static void gen_inheritance_die (tree, tree, dw_die_ref);
3444 static void gen_member_die (tree, dw_die_ref);
3445 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3446                                                 enum debug_info_usage);
3447 static void gen_subroutine_type_die (tree, dw_die_ref);
3448 static void gen_typedef_die (tree, dw_die_ref);
3449 static void gen_type_die (tree, dw_die_ref);
3450 static void gen_block_die (tree, dw_die_ref, int);
3451 static void decls_for_scope (tree, dw_die_ref, int);
3452 static inline int is_redundant_typedef (const_tree);
3453 static bool is_naming_typedef_decl (const_tree);
3454 static inline dw_die_ref get_context_die (tree);
3455 static void gen_namespace_die (tree, dw_die_ref);
3456 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3457 static dw_die_ref force_decl_die (tree);
3458 static dw_die_ref force_type_die (tree);
3459 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3460 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3461 static struct dwarf_file_data * lookup_filename (const char *);
3462 static void retry_incomplete_types (void);
3463 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3464 static void gen_generic_params_dies (tree);
3465 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3466 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3467 static void splice_child_die (dw_die_ref, dw_die_ref);
3468 static int file_info_cmp (const void *, const void *);
3469 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3470                                      const char *, const char *);
3471 static void output_loc_list (dw_loc_list_ref);
3472 static char *gen_internal_sym (const char *);
3473
3474 static void prune_unmark_dies (dw_die_ref);
3475 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3476 static void prune_unused_types_mark (dw_die_ref, int);
3477 static void prune_unused_types_walk (dw_die_ref);
3478 static void prune_unused_types_walk_attribs (dw_die_ref);
3479 static void prune_unused_types_prune (dw_die_ref);
3480 static void prune_unused_types (void);
3481 static int maybe_emit_file (struct dwarf_file_data *fd);
3482 static inline const char *AT_vms_delta1 (dw_attr_ref);
3483 static inline const char *AT_vms_delta2 (dw_attr_ref);
3484 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3485                                      const char *, const char *);
3486 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3487 static void gen_remaining_tmpl_value_param_die_attribute (void);
3488 static bool generic_type_p (tree);
3489 static void schedule_generic_params_dies_gen (tree t);
3490 static void gen_scheduled_generic_parms_dies (void);
3491
3492 /* Section names used to hold DWARF debugging information.  */
3493 #ifndef DEBUG_INFO_SECTION
3494 #define DEBUG_INFO_SECTION      ".debug_info"
3495 #endif
3496 #ifndef DEBUG_ABBREV_SECTION
3497 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3498 #endif
3499 #ifndef DEBUG_ARANGES_SECTION
3500 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3501 #endif
3502 #ifndef DEBUG_MACINFO_SECTION
3503 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3504 #endif
3505 #ifndef DEBUG_MACRO_SECTION
3506 #define DEBUG_MACRO_SECTION     ".debug_macro"
3507 #endif
3508 #ifndef DEBUG_LINE_SECTION
3509 #define DEBUG_LINE_SECTION      ".debug_line"
3510 #endif
3511 #ifndef DEBUG_LOC_SECTION
3512 #define DEBUG_LOC_SECTION       ".debug_loc"
3513 #endif
3514 #ifndef DEBUG_PUBNAMES_SECTION
3515 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3516 #endif
3517 #ifndef DEBUG_PUBTYPES_SECTION
3518 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3519 #endif
3520 #ifndef DEBUG_STR_SECTION
3521 #define DEBUG_STR_SECTION       ".debug_str"
3522 #endif
3523 #ifndef DEBUG_RANGES_SECTION
3524 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3525 #endif
3526
3527 /* Standard ELF section names for compiled code and data.  */
3528 #ifndef TEXT_SECTION_NAME
3529 #define TEXT_SECTION_NAME       ".text"
3530 #endif
3531
3532 /* Section flags for .debug_str section.  */
3533 #define DEBUG_STR_SECTION_FLAGS \
3534   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3535    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3536    : SECTION_DEBUG)
3537
3538 /* Labels we insert at beginning sections we can reference instead of
3539    the section names themselves.  */
3540
3541 #ifndef TEXT_SECTION_LABEL
3542 #define TEXT_SECTION_LABEL              "Ltext"
3543 #endif
3544 #ifndef COLD_TEXT_SECTION_LABEL
3545 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3546 #endif
3547 #ifndef DEBUG_LINE_SECTION_LABEL
3548 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3549 #endif
3550 #ifndef DEBUG_INFO_SECTION_LABEL
3551 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3552 #endif
3553 #ifndef DEBUG_ABBREV_SECTION_LABEL
3554 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3555 #endif
3556 #ifndef DEBUG_LOC_SECTION_LABEL
3557 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3558 #endif
3559 #ifndef DEBUG_RANGES_SECTION_LABEL
3560 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3561 #endif
3562 #ifndef DEBUG_MACINFO_SECTION_LABEL
3563 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3564 #endif
3565 #ifndef DEBUG_MACRO_SECTION_LABEL
3566 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3567 #endif
3568
3569
3570 /* Definitions of defaults for formats and names of various special
3571    (artificial) labels which may be generated within this file (when the -g
3572    options is used and DWARF2_DEBUGGING_INFO is in effect.
3573    If necessary, these may be overridden from within the tm.h file, but
3574    typically, overriding these defaults is unnecessary.  */
3575
3576 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3577 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3578 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3579 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3580 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3581 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3582 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3583 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3585 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3586
3587 #ifndef TEXT_END_LABEL
3588 #define TEXT_END_LABEL          "Letext"
3589 #endif
3590 #ifndef COLD_END_LABEL
3591 #define COLD_END_LABEL          "Letext_cold"
3592 #endif
3593 #ifndef BLOCK_BEGIN_LABEL
3594 #define BLOCK_BEGIN_LABEL       "LBB"
3595 #endif
3596 #ifndef BLOCK_END_LABEL
3597 #define BLOCK_END_LABEL         "LBE"
3598 #endif
3599 #ifndef LINE_CODE_LABEL
3600 #define LINE_CODE_LABEL         "LM"
3601 #endif
3602
3603 \f
3604 /* Return the root of the DIE's built for the current compilation unit.  */
3605 static dw_die_ref
3606 comp_unit_die (void)
3607 {
3608   if (!single_comp_unit_die)
3609     single_comp_unit_die = gen_compile_unit_die (NULL);
3610   return single_comp_unit_die;
3611 }
3612
3613 /* We allow a language front-end to designate a function that is to be
3614    called to "demangle" any name before it is put into a DIE.  */
3615
3616 static const char *(*demangle_name_func) (const char *);
3617
3618 void
3619 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3620 {
3621   demangle_name_func = func;
3622 }
3623
3624 /* Test if rtl node points to a pseudo register.  */
3625
3626 static inline int
3627 is_pseudo_reg (const_rtx rtl)
3628 {
3629   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3630           || (GET_CODE (rtl) == SUBREG
3631               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3632 }
3633
3634 /* Return a reference to a type, with its const and volatile qualifiers
3635    removed.  */
3636
3637 static inline tree
3638 type_main_variant (tree type)
3639 {
3640   type = TYPE_MAIN_VARIANT (type);
3641
3642   /* ??? There really should be only one main variant among any group of
3643      variants of a given type (and all of the MAIN_VARIANT values for all
3644      members of the group should point to that one type) but sometimes the C
3645      front-end messes this up for array types, so we work around that bug
3646      here.  */
3647   if (TREE_CODE (type) == ARRAY_TYPE)
3648     while (type != TYPE_MAIN_VARIANT (type))
3649       type = TYPE_MAIN_VARIANT (type);
3650
3651   return type;
3652 }
3653
3654 /* Return nonzero if the given type node represents a tagged type.  */
3655
3656 static inline int
3657 is_tagged_type (const_tree type)
3658 {
3659   enum tree_code code = TREE_CODE (type);
3660
3661   return (code == RECORD_TYPE || code == UNION_TYPE
3662           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3663 }
3664
3665 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3666
3667 static void
3668 get_ref_die_offset_label (char *label, dw_die_ref ref)
3669 {
3670   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3671 }
3672
3673 /* Return die_offset of a DIE reference to a base type.  */
3674
3675 static unsigned long int
3676 get_base_type_offset (dw_die_ref ref)
3677 {
3678   if (ref->die_offset)
3679     return ref->die_offset;
3680   if (comp_unit_die ()->die_abbrev)
3681     {
3682       calc_base_type_die_sizes ();
3683       gcc_assert (ref->die_offset);
3684     }
3685   return ref->die_offset;
3686 }
3687
3688 /* Return die_offset of a DIE reference other than base type.  */
3689
3690 static unsigned long int
3691 get_ref_die_offset (dw_die_ref ref)
3692 {
3693   gcc_assert (ref->die_offset);
3694   return ref->die_offset;
3695 }
3696
3697 /* Convert a DIE tag into its string name.  */
3698
3699 static const char *
3700 dwarf_tag_name (unsigned int tag)
3701 {
3702   switch (tag)
3703     {
3704     case DW_TAG_padding:
3705       return "DW_TAG_padding";
3706     case DW_TAG_array_type:
3707       return "DW_TAG_array_type";
3708     case DW_TAG_class_type:
3709       return "DW_TAG_class_type";
3710     case DW_TAG_entry_point:
3711       return "DW_TAG_entry_point";
3712     case DW_TAG_enumeration_type:
3713       return "DW_TAG_enumeration_type";
3714     case DW_TAG_formal_parameter:
3715       return "DW_TAG_formal_parameter";
3716     case DW_TAG_imported_declaration:
3717       return "DW_TAG_imported_declaration";
3718     case DW_TAG_label:
3719       return "DW_TAG_label";
3720     case DW_TAG_lexical_block:
3721       return "DW_TAG_lexical_block";
3722     case DW_TAG_member:
3723       return "DW_TAG_member";
3724     case DW_TAG_pointer_type:
3725       return "DW_TAG_pointer_type";
3726     case DW_TAG_reference_type:
3727       return "DW_TAG_reference_type";
3728     case DW_TAG_compile_unit:
3729       return "DW_TAG_compile_unit";
3730     case DW_TAG_string_type:
3731       return "DW_TAG_string_type";
3732     case DW_TAG_structure_type:
3733       return "DW_TAG_structure_type";
3734     case DW_TAG_subroutine_type:
3735       return "DW_TAG_subroutine_type";
3736     case DW_TAG_typedef:
3737       return "DW_TAG_typedef";
3738     case DW_TAG_union_type:
3739       return "DW_TAG_union_type";
3740     case DW_TAG_unspecified_parameters:
3741       return "DW_TAG_unspecified_parameters";
3742     case DW_TAG_variant:
3743       return "DW_TAG_variant";
3744     case DW_TAG_common_block:
3745       return "DW_TAG_common_block";
3746     case DW_TAG_common_inclusion:
3747       return "DW_TAG_common_inclusion";
3748     case DW_TAG_inheritance:
3749       return "DW_TAG_inheritance";
3750     case DW_TAG_inlined_subroutine:
3751       return "DW_TAG_inlined_subroutine";
3752     case DW_TAG_module:
3753       return "DW_TAG_module";
3754     case DW_TAG_ptr_to_member_type:
3755       return "DW_TAG_ptr_to_member_type";
3756     case DW_TAG_set_type:
3757       return "DW_TAG_set_type";
3758     case DW_TAG_subrange_type:
3759       return "DW_TAG_subrange_type";
3760     case DW_TAG_with_stmt:
3761       return "DW_TAG_with_stmt";
3762     case DW_TAG_access_declaration:
3763       return "DW_TAG_access_declaration";
3764     case DW_TAG_base_type:
3765       return "DW_TAG_base_type";
3766     case DW_TAG_catch_block:
3767       return "DW_TAG_catch_block";
3768     case DW_TAG_const_type:
3769       return "DW_TAG_const_type";
3770     case DW_TAG_constant:
3771       return "DW_TAG_constant";
3772     case DW_TAG_enumerator:
3773       return "DW_TAG_enumerator";
3774     case DW_TAG_file_type:
3775       return "DW_TAG_file_type";
3776     case DW_TAG_friend:
3777       return "DW_TAG_friend";
3778     case DW_TAG_namelist:
3779       return "DW_TAG_namelist";
3780     case DW_TAG_namelist_item:
3781       return "DW_TAG_namelist_item";
3782     case DW_TAG_packed_type:
3783       return "DW_TAG_packed_type";
3784     case DW_TAG_subprogram:
3785       return "DW_TAG_subprogram";
3786     case DW_TAG_template_type_param:
3787       return "DW_TAG_template_type_param";
3788     case DW_TAG_template_value_param:
3789       return "DW_TAG_template_value_param";
3790     case DW_TAG_thrown_type:
3791       return "DW_TAG_thrown_type";
3792     case DW_TAG_try_block:
3793       return "DW_TAG_try_block";
3794     case DW_TAG_variant_part:
3795       return "DW_TAG_variant_part";
3796     case DW_TAG_variable:
3797       return "DW_TAG_variable";
3798     case DW_TAG_volatile_type:
3799       return "DW_TAG_volatile_type";
3800     case DW_TAG_dwarf_procedure:
3801       return "DW_TAG_dwarf_procedure";
3802     case DW_TAG_restrict_type:
3803       return "DW_TAG_restrict_type";
3804     case DW_TAG_interface_type:
3805       return "DW_TAG_interface_type";
3806     case DW_TAG_namespace:
3807       return "DW_TAG_namespace";
3808     case DW_TAG_imported_module:
3809       return "DW_TAG_imported_module";
3810     case DW_TAG_unspecified_type:
3811       return "DW_TAG_unspecified_type";
3812     case DW_TAG_partial_unit:
3813       return "DW_TAG_partial_unit";
3814     case DW_TAG_imported_unit:
3815       return "DW_TAG_imported_unit";
3816     case DW_TAG_condition:
3817       return "DW_TAG_condition";
3818     case DW_TAG_shared_type:
3819       return "DW_TAG_shared_type";
3820     case DW_TAG_type_unit:
3821       return "DW_TAG_type_unit";
3822     case DW_TAG_rvalue_reference_type:
3823       return "DW_TAG_rvalue_reference_type";
3824     case DW_TAG_template_alias:
3825       return "DW_TAG_template_alias";
3826     case DW_TAG_GNU_template_parameter_pack:
3827       return "DW_TAG_GNU_template_parameter_pack";
3828     case DW_TAG_GNU_formal_parameter_pack:
3829       return "DW_TAG_GNU_formal_parameter_pack";
3830     case DW_TAG_MIPS_loop:
3831       return "DW_TAG_MIPS_loop";
3832     case DW_TAG_format_label:
3833       return "DW_TAG_format_label";
3834     case DW_TAG_function_template:
3835       return "DW_TAG_function_template";
3836     case DW_TAG_class_template:
3837       return "DW_TAG_class_template";
3838     case DW_TAG_GNU_BINCL:
3839       return "DW_TAG_GNU_BINCL";
3840     case DW_TAG_GNU_EINCL:
3841       return "DW_TAG_GNU_EINCL";
3842     case DW_TAG_GNU_template_template_param:
3843       return "DW_TAG_GNU_template_template_param";
3844     case DW_TAG_GNU_call_site:
3845       return "DW_TAG_GNU_call_site";
3846     case DW_TAG_GNU_call_site_parameter:
3847       return "DW_TAG_GNU_call_site_parameter";
3848     default:
3849       return "DW_TAG_<unknown>";
3850     }
3851 }
3852
3853 /* Convert a DWARF attribute code into its string name.  */
3854
3855 static const char *
3856 dwarf_attr_name (unsigned int attr)
3857 {
3858   switch (attr)
3859     {
3860     case DW_AT_sibling:
3861       return "DW_AT_sibling";
3862     case DW_AT_location:
3863       return "DW_AT_location";
3864     case DW_AT_name:
3865       return "DW_AT_name";
3866     case DW_AT_ordering:
3867       return "DW_AT_ordering";
3868     case DW_AT_subscr_data:
3869       return "DW_AT_subscr_data";
3870     case DW_AT_byte_size:
3871       return "DW_AT_byte_size";
3872     case DW_AT_bit_offset:
3873       return "DW_AT_bit_offset";
3874     case DW_AT_bit_size:
3875       return "DW_AT_bit_size";
3876     case DW_AT_element_list:
3877       return "DW_AT_element_list";
3878     case DW_AT_stmt_list:
3879       return "DW_AT_stmt_list";
3880     case DW_AT_low_pc:
3881       return "DW_AT_low_pc";
3882     case DW_AT_high_pc:
3883       return "DW_AT_high_pc";
3884     case DW_AT_language:
3885       return "DW_AT_language";
3886     case DW_AT_member:
3887       return "DW_AT_member";
3888     case DW_AT_discr:
3889       return "DW_AT_discr";
3890     case DW_AT_discr_value:
3891       return "DW_AT_discr_value";
3892     case DW_AT_visibility:
3893       return "DW_AT_visibility";
3894     case DW_AT_import:
3895       return "DW_AT_import";
3896     case DW_AT_string_length:
3897       return "DW_AT_string_length";
3898     case DW_AT_common_reference:
3899       return "DW_AT_common_reference";
3900     case DW_AT_comp_dir:
3901       return "DW_AT_comp_dir";
3902     case DW_AT_const_value:
3903       return "DW_AT_const_value";
3904     case DW_AT_containing_type:
3905       return "DW_AT_containing_type";
3906     case DW_AT_default_value:
3907       return "DW_AT_default_value";
3908     case DW_AT_inline:
3909       return "DW_AT_inline";
3910     case DW_AT_is_optional:
3911       return "DW_AT_is_optional";
3912     case DW_AT_lower_bound:
3913       return "DW_AT_lower_bound";
3914     case DW_AT_producer:
3915       return "DW_AT_producer";
3916     case DW_AT_prototyped:
3917       return "DW_AT_prototyped";
3918     case DW_AT_return_addr:
3919       return "DW_AT_return_addr";
3920     case DW_AT_start_scope:
3921       return "DW_AT_start_scope";
3922     case DW_AT_bit_stride:
3923       return "DW_AT_bit_stride";
3924     case DW_AT_upper_bound:
3925       return "DW_AT_upper_bound";
3926     case DW_AT_abstract_origin:
3927       return "DW_AT_abstract_origin";
3928     case DW_AT_accessibility:
3929       return "DW_AT_accessibility";
3930     case DW_AT_address_class:
3931       return "DW_AT_address_class";
3932     case DW_AT_artificial:
3933       return "DW_AT_artificial";
3934     case DW_AT_base_types:
3935       return "DW_AT_base_types";
3936     case DW_AT_calling_convention:
3937       return "DW_AT_calling_convention";
3938     case DW_AT_count:
3939       return "DW_AT_count";
3940     case DW_AT_data_member_location:
3941       return "DW_AT_data_member_location";
3942     case DW_AT_decl_column:
3943       return "DW_AT_decl_column";
3944     case DW_AT_decl_file:
3945       return "DW_AT_decl_file";
3946     case DW_AT_decl_line:
3947       return "DW_AT_decl_line";
3948     case DW_AT_declaration:
3949       return "DW_AT_declaration";
3950     case DW_AT_discr_list:
3951       return "DW_AT_discr_list";
3952     case DW_AT_encoding:
3953       return "DW_AT_encoding";
3954     case DW_AT_external:
3955       return "DW_AT_external";
3956     case DW_AT_explicit:
3957       return "DW_AT_explicit";
3958     case DW_AT_frame_base:
3959       return "DW_AT_frame_base";
3960     case DW_AT_friend:
3961       return "DW_AT_friend";
3962     case DW_AT_identifier_case:
3963       return "DW_AT_identifier_case";
3964     case DW_AT_macro_info:
3965       return "DW_AT_macro_info";
3966     case DW_AT_namelist_items:
3967       return "DW_AT_namelist_items";
3968     case DW_AT_priority:
3969       return "DW_AT_priority";
3970     case DW_AT_segment:
3971       return "DW_AT_segment";
3972     case DW_AT_specification:
3973       return "DW_AT_specification";
3974     case DW_AT_static_link:
3975       return "DW_AT_static_link";
3976     case DW_AT_type:
3977       return "DW_AT_type";
3978     case DW_AT_use_location:
3979       return "DW_AT_use_location";
3980     case DW_AT_variable_parameter:
3981       return "DW_AT_variable_parameter";
3982     case DW_AT_virtuality:
3983       return "DW_AT_virtuality";
3984     case DW_AT_vtable_elem_location:
3985       return "DW_AT_vtable_elem_location";
3986
3987     case DW_AT_allocated:
3988       return "DW_AT_allocated";
3989     case DW_AT_associated:
3990       return "DW_AT_associated";
3991     case DW_AT_data_location:
3992       return "DW_AT_data_location";
3993     case DW_AT_byte_stride:
3994       return "DW_AT_byte_stride";
3995     case DW_AT_entry_pc:
3996       return "DW_AT_entry_pc";
3997     case DW_AT_use_UTF8:
3998       return "DW_AT_use_UTF8";
3999     case DW_AT_extension:
4000       return "DW_AT_extension";
4001     case DW_AT_ranges:
4002       return "DW_AT_ranges";
4003     case DW_AT_trampoline:
4004       return "DW_AT_trampoline";
4005     case DW_AT_call_column:
4006       return "DW_AT_call_column";
4007     case DW_AT_call_file:
4008       return "DW_AT_call_file";
4009     case DW_AT_call_line:
4010       return "DW_AT_call_line";
4011     case DW_AT_object_pointer:
4012       return "DW_AT_object_pointer";
4013
4014     case DW_AT_signature:
4015       return "DW_AT_signature";
4016     case DW_AT_main_subprogram:
4017       return "DW_AT_main_subprogram";
4018     case DW_AT_data_bit_offset:
4019       return "DW_AT_data_bit_offset";
4020     case DW_AT_const_expr:
4021       return "DW_AT_const_expr";
4022     case DW_AT_enum_class:
4023       return "DW_AT_enum_class";
4024     case DW_AT_linkage_name:
4025       return "DW_AT_linkage_name";
4026
4027     case DW_AT_MIPS_fde:
4028       return "DW_AT_MIPS_fde";
4029     case DW_AT_MIPS_loop_begin:
4030       return "DW_AT_MIPS_loop_begin";
4031     case DW_AT_MIPS_tail_loop_begin:
4032       return "DW_AT_MIPS_tail_loop_begin";
4033     case DW_AT_MIPS_epilog_begin:
4034       return "DW_AT_MIPS_epilog_begin";
4035 #if VMS_DEBUGGING_INFO
4036     case DW_AT_HP_prologue:
4037       return "DW_AT_HP_prologue";
4038 #else
4039     case DW_AT_MIPS_loop_unroll_factor:
4040       return "DW_AT_MIPS_loop_unroll_factor";
4041 #endif
4042     case DW_AT_MIPS_software_pipeline_depth:
4043       return "DW_AT_MIPS_software_pipeline_depth";
4044     case DW_AT_MIPS_linkage_name:
4045       return "DW_AT_MIPS_linkage_name";
4046 #if VMS_DEBUGGING_INFO
4047     case DW_AT_HP_epilogue:
4048       return "DW_AT_HP_epilogue";
4049 #else
4050     case DW_AT_MIPS_stride:
4051       return "DW_AT_MIPS_stride";
4052 #endif
4053     case DW_AT_MIPS_abstract_name:
4054       return "DW_AT_MIPS_abstract_name";
4055     case DW_AT_MIPS_clone_origin:
4056       return "DW_AT_MIPS_clone_origin";
4057     case DW_AT_MIPS_has_inlines:
4058       return "DW_AT_MIPS_has_inlines";
4059
4060     case DW_AT_sf_names:
4061       return "DW_AT_sf_names";
4062     case DW_AT_src_info:
4063       return "DW_AT_src_info";
4064     case DW_AT_mac_info:
4065       return "DW_AT_mac_info";
4066     case DW_AT_src_coords:
4067       return "DW_AT_src_coords";
4068     case DW_AT_body_begin:
4069       return "DW_AT_body_begin";
4070     case DW_AT_body_end:
4071       return "DW_AT_body_end";
4072
4073     case DW_AT_GNU_vector:
4074       return "DW_AT_GNU_vector";
4075     case DW_AT_GNU_guarded_by:
4076       return "DW_AT_GNU_guarded_by";
4077     case DW_AT_GNU_pt_guarded_by:
4078       return "DW_AT_GNU_pt_guarded_by";
4079     case DW_AT_GNU_guarded:
4080       return "DW_AT_GNU_guarded";
4081     case DW_AT_GNU_pt_guarded:
4082       return "DW_AT_GNU_pt_guarded";
4083     case DW_AT_GNU_locks_excluded:
4084       return "DW_AT_GNU_locks_excluded";
4085     case DW_AT_GNU_exclusive_locks_required:
4086       return "DW_AT_GNU_exclusive_locks_required";
4087     case DW_AT_GNU_shared_locks_required:
4088       return "DW_AT_GNU_shared_locks_required";
4089     case DW_AT_GNU_odr_signature:
4090       return "DW_AT_GNU_odr_signature";
4091     case DW_AT_GNU_template_name:
4092       return "DW_AT_GNU_template_name";
4093     case DW_AT_GNU_call_site_value:
4094       return "DW_AT_GNU_call_site_value";
4095     case DW_AT_GNU_call_site_data_value:
4096       return "DW_AT_GNU_call_site_data_value";
4097     case DW_AT_GNU_call_site_target:
4098       return "DW_AT_GNU_call_site_target";
4099     case DW_AT_GNU_call_site_target_clobbered:
4100       return "DW_AT_GNU_call_site_target_clobbered";
4101     case DW_AT_GNU_tail_call:
4102       return "DW_AT_GNU_tail_call";
4103     case DW_AT_GNU_all_tail_call_sites:
4104       return "DW_AT_GNU_all_tail_call_sites";
4105     case DW_AT_GNU_all_call_sites:
4106       return "DW_AT_GNU_all_call_sites";
4107     case DW_AT_GNU_all_source_call_sites:
4108       return "DW_AT_GNU_all_source_call_sites";
4109     case DW_AT_GNU_macros:
4110       return "DW_AT_GNU_macros";
4111
4112     case DW_AT_GNAT_descriptive_type:
4113       return "DW_AT_GNAT_descriptive_type";
4114
4115     case DW_AT_VMS_rtnbeg_pd_address:
4116       return "DW_AT_VMS_rtnbeg_pd_address";
4117
4118     default:
4119       return "DW_AT_<unknown>";
4120     }
4121 }
4122
4123 /* Convert a DWARF value form code into its string name.  */
4124
4125 static const char *
4126 dwarf_form_name (unsigned int form)
4127 {
4128   switch (form)
4129     {
4130     case DW_FORM_addr:
4131       return "DW_FORM_addr";
4132     case DW_FORM_block2:
4133       return "DW_FORM_block2";
4134     case DW_FORM_block4:
4135       return "DW_FORM_block4";
4136     case DW_FORM_data2:
4137       return "DW_FORM_data2";
4138     case DW_FORM_data4:
4139       return "DW_FORM_data4";
4140     case DW_FORM_data8:
4141       return "DW_FORM_data8";
4142     case DW_FORM_string:
4143       return "DW_FORM_string";
4144     case DW_FORM_block:
4145       return "DW_FORM_block";
4146     case DW_FORM_block1:
4147       return "DW_FORM_block1";
4148     case DW_FORM_data1:
4149       return "DW_FORM_data1";
4150     case DW_FORM_flag:
4151       return "DW_FORM_flag";
4152     case DW_FORM_sdata:
4153       return "DW_FORM_sdata";
4154     case DW_FORM_strp:
4155       return "DW_FORM_strp";
4156     case DW_FORM_udata:
4157       return "DW_FORM_udata";
4158     case DW_FORM_ref_addr:
4159       return "DW_FORM_ref_addr";
4160     case DW_FORM_ref1:
4161       return "DW_FORM_ref1";
4162     case DW_FORM_ref2:
4163       return "DW_FORM_ref2";
4164     case DW_FORM_ref4:
4165       return "DW_FORM_ref4";
4166     case DW_FORM_ref8:
4167       return "DW_FORM_ref8";
4168     case DW_FORM_ref_udata:
4169       return "DW_FORM_ref_udata";
4170     case DW_FORM_indirect:
4171       return "DW_FORM_indirect";
4172     case DW_FORM_sec_offset:
4173       return "DW_FORM_sec_offset";
4174     case DW_FORM_exprloc:
4175       return "DW_FORM_exprloc";
4176     case DW_FORM_flag_present:
4177       return "DW_FORM_flag_present";
4178     case DW_FORM_ref_sig8:
4179       return "DW_FORM_ref_sig8";
4180     default:
4181       return "DW_FORM_<unknown>";
4182     }
4183 }
4184 \f
4185 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4186    instance of an inlined instance of a decl which is local to an inline
4187    function, so we have to trace all of the way back through the origin chain
4188    to find out what sort of node actually served as the original seed for the
4189    given block.  */
4190
4191 static tree
4192 decl_ultimate_origin (const_tree decl)
4193 {
4194   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4195     return NULL_TREE;
4196
4197   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4198      nodes in the function to point to themselves; ignore that if
4199      we're trying to output the abstract instance of this function.  */
4200   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4201     return NULL_TREE;
4202
4203   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4204      most distant ancestor, this should never happen.  */
4205   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4206
4207   return DECL_ABSTRACT_ORIGIN (decl);
4208 }
4209
4210 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4211    of a virtual function may refer to a base class, so we check the 'this'
4212    parameter.  */
4213
4214 static tree
4215 decl_class_context (tree decl)
4216 {
4217   tree context = NULL_TREE;
4218
4219   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4220     context = DECL_CONTEXT (decl);
4221   else
4222     context = TYPE_MAIN_VARIANT
4223       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4224
4225   if (context && !TYPE_P (context))
4226     context = NULL_TREE;
4227
4228   return context;
4229 }
4230 \f
4231 /* Add an attribute/value pair to a DIE.  */
4232
4233 static inline void
4234 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4235 {
4236   /* Maybe this should be an assert?  */
4237   if (die == NULL)
4238     return;
4239
4240   if (die->die_attr == NULL)
4241     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4242   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4243 }
4244
4245 static inline enum dw_val_class
4246 AT_class (dw_attr_ref a)
4247 {
4248   return a->dw_attr_val.val_class;
4249 }
4250
4251 /* Add a flag value attribute to a DIE.  */
4252
4253 static inline void
4254 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4255 {
4256   dw_attr_node attr;
4257
4258   attr.dw_attr = attr_kind;
4259   attr.dw_attr_val.val_class = dw_val_class_flag;
4260   attr.dw_attr_val.v.val_flag = flag;
4261   add_dwarf_attr (die, &attr);
4262 }
4263
4264 static inline unsigned
4265 AT_flag (dw_attr_ref a)
4266 {
4267   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4268   return a->dw_attr_val.v.val_flag;
4269 }
4270
4271 /* Add a signed integer attribute value to a DIE.  */
4272
4273 static inline void
4274 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4275 {
4276   dw_attr_node attr;
4277
4278   attr.dw_attr = attr_kind;
4279   attr.dw_attr_val.val_class = dw_val_class_const;
4280   attr.dw_attr_val.v.val_int = int_val;
4281   add_dwarf_attr (die, &attr);
4282 }
4283
4284 static inline HOST_WIDE_INT
4285 AT_int (dw_attr_ref a)
4286 {
4287   gcc_assert (a && AT_class (a) == dw_val_class_const);
4288   return a->dw_attr_val.v.val_int;
4289 }
4290
4291 /* Add an unsigned integer attribute value to a DIE.  */
4292
4293 static inline void
4294 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4295                  unsigned HOST_WIDE_INT unsigned_val)
4296 {
4297   dw_attr_node attr;
4298
4299   attr.dw_attr = attr_kind;
4300   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4301   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4302   add_dwarf_attr (die, &attr);
4303 }
4304
4305 static inline unsigned HOST_WIDE_INT
4306 AT_unsigned (dw_attr_ref a)
4307 {
4308   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4309   return a->dw_attr_val.v.val_unsigned;
4310 }
4311
4312 /* Add an unsigned double integer attribute value to a DIE.  */
4313
4314 static inline void
4315 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4316                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4317 {
4318   dw_attr_node attr;
4319
4320   attr.dw_attr = attr_kind;
4321   attr.dw_attr_val.val_class = dw_val_class_const_double;
4322   attr.dw_attr_val.v.val_double.high = high;
4323   attr.dw_attr_val.v.val_double.low = low;
4324   add_dwarf_attr (die, &attr);
4325 }
4326
4327 /* Add a floating point attribute value to a DIE and return it.  */
4328
4329 static inline void
4330 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4331             unsigned int length, unsigned int elt_size, unsigned char *array)
4332 {
4333   dw_attr_node attr;
4334
4335   attr.dw_attr = attr_kind;
4336   attr.dw_attr_val.val_class = dw_val_class_vec;
4337   attr.dw_attr_val.v.val_vec.length = length;
4338   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4339   attr.dw_attr_val.v.val_vec.array = array;
4340   add_dwarf_attr (die, &attr);
4341 }
4342
4343 /* Add an 8-byte data attribute value to a DIE.  */
4344
4345 static inline void
4346 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4347               unsigned char data8[8])
4348 {
4349   dw_attr_node attr;
4350
4351   attr.dw_attr = attr_kind;
4352   attr.dw_attr_val.val_class = dw_val_class_data8;
4353   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4354   add_dwarf_attr (die, &attr);
4355 }
4356
4357 /* Hash and equality functions for debug_str_hash.  */
4358
4359 static hashval_t
4360 debug_str_do_hash (const void *x)
4361 {
4362   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4363 }
4364
4365 static int
4366 debug_str_eq (const void *x1, const void *x2)
4367 {
4368   return strcmp ((((const struct indirect_string_node *)x1)->str),
4369                  (const char *)x2) == 0;
4370 }
4371
4372 /* Add STR to the indirect string hash table.  */
4373
4374 static struct indirect_string_node *
4375 find_AT_string (const char *str)
4376 {
4377   struct indirect_string_node *node;
4378   void **slot;
4379
4380   if (! debug_str_hash)
4381     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4382                                       debug_str_eq, NULL);
4383
4384   slot = htab_find_slot_with_hash (debug_str_hash, str,
4385                                    htab_hash_string (str), INSERT);
4386   if (*slot == NULL)
4387     {
4388       node = ggc_alloc_cleared_indirect_string_node ();
4389       node->str = ggc_strdup (str);
4390       *slot = node;
4391     }
4392   else
4393     node = (struct indirect_string_node *) *slot;
4394
4395   node->refcount++;
4396   return node;
4397 }
4398
4399 /* Add a string attribute value to a DIE.  */
4400
4401 static inline void
4402 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4403 {
4404   dw_attr_node attr;
4405   struct indirect_string_node *node;
4406
4407   node = find_AT_string (str);
4408
4409   attr.dw_attr = attr_kind;
4410   attr.dw_attr_val.val_class = dw_val_class_str;
4411   attr.dw_attr_val.v.val_str = node;
4412   add_dwarf_attr (die, &attr);
4413 }
4414
4415 static inline const char *
4416 AT_string (dw_attr_ref a)
4417 {
4418   gcc_assert (a && AT_class (a) == dw_val_class_str);
4419   return a->dw_attr_val.v.val_str->str;
4420 }
4421
4422 /* Find out whether a string should be output inline in DIE
4423    or out-of-line in .debug_str section.  */
4424
4425 static enum dwarf_form
4426 AT_string_form (dw_attr_ref a)
4427 {
4428   struct indirect_string_node *node;
4429   unsigned int len;
4430   char label[32];
4431
4432   gcc_assert (a && AT_class (a) == dw_val_class_str);
4433
4434   node = a->dw_attr_val.v.val_str;
4435   if (node->form)
4436     return node->form;
4437
4438   len = strlen (node->str) + 1;
4439
4440   /* If the string is shorter or equal to the size of the reference, it is
4441      always better to put it inline.  */
4442   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4443     return node->form = DW_FORM_string;
4444
4445   /* If we cannot expect the linker to merge strings in .debug_str
4446      section, only put it into .debug_str if it is worth even in this
4447      single module.  */
4448   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4449       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4450       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4451     return node->form = DW_FORM_string;
4452
4453   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4454   ++dw2_string_counter;
4455   node->label = xstrdup (label);
4456
4457   return node->form = DW_FORM_strp;
4458 }
4459
4460 /* Add a DIE reference attribute value to a DIE.  */
4461
4462 static inline void
4463 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4464 {
4465   dw_attr_node attr;
4466
4467 #ifdef ENABLE_CHECKING
4468   gcc_assert (targ_die != NULL);
4469 #else
4470   /* With LTO we can end up trying to reference something we didn't create
4471      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4472   if (targ_die == NULL)
4473     return;
4474 #endif
4475
4476   attr.dw_attr = attr_kind;
4477   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4478   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4479   attr.dw_attr_val.v.val_die_ref.external = 0;
4480   add_dwarf_attr (die, &attr);
4481 }
4482
4483 /* Add an AT_specification attribute to a DIE, and also make the back
4484    pointer from the specification to the definition.  */
4485
4486 static inline void
4487 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4488 {
4489   add_AT_die_ref (die, DW_AT_specification, targ_die);
4490   gcc_assert (!targ_die->die_definition);
4491   targ_die->die_definition = die;
4492 }
4493
4494 static inline dw_die_ref
4495 AT_ref (dw_attr_ref a)
4496 {
4497   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4498   return a->dw_attr_val.v.val_die_ref.die;
4499 }
4500
4501 static inline int
4502 AT_ref_external (dw_attr_ref a)
4503 {
4504   if (a && AT_class (a) == dw_val_class_die_ref)
4505     return a->dw_attr_val.v.val_die_ref.external;
4506
4507   return 0;
4508 }
4509
4510 static inline void
4511 set_AT_ref_external (dw_attr_ref a, int i)
4512 {
4513   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4514   a->dw_attr_val.v.val_die_ref.external = i;
4515 }
4516
4517 /* Add an FDE reference attribute value to a DIE.  */
4518
4519 static inline void
4520 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4521 {
4522   dw_attr_node attr;
4523
4524   attr.dw_attr = attr_kind;
4525   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4526   attr.dw_attr_val.v.val_fde_index = targ_fde;
4527   add_dwarf_attr (die, &attr);
4528 }
4529
4530 /* Add a location description attribute value to a DIE.  */
4531
4532 static inline void
4533 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4534 {
4535   dw_attr_node attr;
4536
4537   attr.dw_attr = attr_kind;
4538   attr.dw_attr_val.val_class = dw_val_class_loc;
4539   attr.dw_attr_val.v.val_loc = loc;
4540   add_dwarf_attr (die, &attr);
4541 }
4542
4543 static inline dw_loc_descr_ref
4544 AT_loc (dw_attr_ref a)
4545 {
4546   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4547   return a->dw_attr_val.v.val_loc;
4548 }
4549
4550 static inline void
4551 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4552 {
4553   dw_attr_node attr;
4554
4555   attr.dw_attr = attr_kind;
4556   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4557   attr.dw_attr_val.v.val_loc_list = loc_list;
4558   add_dwarf_attr (die, &attr);
4559   have_location_lists = true;
4560 }
4561
4562 static inline dw_loc_list_ref
4563 AT_loc_list (dw_attr_ref a)
4564 {
4565   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4566   return a->dw_attr_val.v.val_loc_list;
4567 }
4568
4569 static inline dw_loc_list_ref *
4570 AT_loc_list_ptr (dw_attr_ref a)
4571 {
4572   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4573   return &a->dw_attr_val.v.val_loc_list;
4574 }
4575
4576 /* Add an address constant attribute value to a DIE.  */
4577
4578 static inline void
4579 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4580 {
4581   dw_attr_node attr;
4582
4583   attr.dw_attr = attr_kind;
4584   attr.dw_attr_val.val_class = dw_val_class_addr;
4585   attr.dw_attr_val.v.val_addr = addr;
4586   add_dwarf_attr (die, &attr);
4587 }
4588
4589 /* Get the RTX from to an address DIE attribute.  */
4590
4591 static inline rtx
4592 AT_addr (dw_attr_ref a)
4593 {
4594   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4595   return a->dw_attr_val.v.val_addr;
4596 }
4597
4598 /* Add a file attribute value to a DIE.  */
4599
4600 static inline void
4601 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4602              struct dwarf_file_data *fd)
4603 {
4604   dw_attr_node attr;
4605
4606   attr.dw_attr = attr_kind;
4607   attr.dw_attr_val.val_class = dw_val_class_file;
4608   attr.dw_attr_val.v.val_file = fd;
4609   add_dwarf_attr (die, &attr);
4610 }
4611
4612 /* Get the dwarf_file_data from a file DIE attribute.  */
4613
4614 static inline struct dwarf_file_data *
4615 AT_file (dw_attr_ref a)
4616 {
4617   gcc_assert (a && AT_class (a) == dw_val_class_file);
4618   return a->dw_attr_val.v.val_file;
4619 }
4620
4621 /* Add a vms delta attribute value to a DIE.  */
4622
4623 static inline void
4624 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4625                   const char *lbl1, const char *lbl2)
4626 {
4627   dw_attr_node attr;
4628
4629   attr.dw_attr = attr_kind;
4630   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4631   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4632   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4633   add_dwarf_attr (die, &attr);
4634 }
4635
4636 /* Add a label identifier attribute value to a DIE.  */
4637
4638 static inline void
4639 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4640 {
4641   dw_attr_node attr;
4642
4643   attr.dw_attr = attr_kind;
4644   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4645   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4646   add_dwarf_attr (die, &attr);
4647 }
4648
4649 /* Add a section offset attribute value to a DIE, an offset into the
4650    debug_line section.  */
4651
4652 static inline void
4653 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4654                 const char *label)
4655 {
4656   dw_attr_node attr;
4657
4658   attr.dw_attr = attr_kind;
4659   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4660   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4661   add_dwarf_attr (die, &attr);
4662 }
4663
4664 /* Add a section offset attribute value to a DIE, an offset into the
4665    debug_macinfo section.  */
4666
4667 static inline void
4668 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4669                const char *label)
4670 {
4671   dw_attr_node attr;
4672
4673   attr.dw_attr = attr_kind;
4674   attr.dw_attr_val.val_class = dw_val_class_macptr;
4675   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4676   add_dwarf_attr (die, &attr);
4677 }
4678
4679 /* Add an offset attribute value to a DIE.  */
4680
4681 static inline void
4682 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4683                unsigned HOST_WIDE_INT offset)
4684 {
4685   dw_attr_node attr;
4686
4687   attr.dw_attr = attr_kind;
4688   attr.dw_attr_val.val_class = dw_val_class_offset;
4689   attr.dw_attr_val.v.val_offset = offset;
4690   add_dwarf_attr (die, &attr);
4691 }
4692
4693 /* Add an range_list attribute value to a DIE.  */
4694
4695 static void
4696 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4697                    long unsigned int offset)
4698 {
4699   dw_attr_node attr;
4700
4701   attr.dw_attr = attr_kind;
4702   attr.dw_attr_val.val_class = dw_val_class_range_list;
4703   attr.dw_attr_val.v.val_offset = offset;
4704   add_dwarf_attr (die, &attr);
4705 }
4706
4707 /* Return the start label of a delta attribute.  */
4708
4709 static inline const char *
4710 AT_vms_delta1 (dw_attr_ref a)
4711 {
4712   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4713   return a->dw_attr_val.v.val_vms_delta.lbl1;
4714 }
4715
4716 /* Return the end label of a delta attribute.  */
4717
4718 static inline const char *
4719 AT_vms_delta2 (dw_attr_ref a)
4720 {
4721   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4722   return a->dw_attr_val.v.val_vms_delta.lbl2;
4723 }
4724
4725 static inline const char *
4726 AT_lbl (dw_attr_ref a)
4727 {
4728   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4729                     || AT_class (a) == dw_val_class_lineptr
4730                     || AT_class (a) == dw_val_class_macptr));
4731   return a->dw_attr_val.v.val_lbl_id;
4732 }
4733
4734 /* Get the attribute of type attr_kind.  */
4735
4736 static dw_attr_ref
4737 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4738 {
4739   dw_attr_ref a;
4740   unsigned ix;
4741   dw_die_ref spec = NULL;
4742
4743   if (! die)
4744     return NULL;
4745
4746   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4747     if (a->dw_attr == attr_kind)
4748       return a;
4749     else if (a->dw_attr == DW_AT_specification
4750              || a->dw_attr == DW_AT_abstract_origin)
4751       spec = AT_ref (a);
4752
4753   if (spec)
4754     return get_AT (spec, attr_kind);
4755
4756   return NULL;
4757 }
4758
4759 /* Return the "low pc" attribute value, typically associated with a subprogram
4760    DIE.  Return null if the "low pc" attribute is either not present, or if it
4761    cannot be represented as an assembler label identifier.  */
4762
4763 static inline const char *
4764 get_AT_low_pc (dw_die_ref die)
4765 {
4766   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4767
4768   return a ? AT_lbl (a) : NULL;
4769 }
4770
4771 /* Return the "high pc" attribute value, typically associated with a subprogram
4772    DIE.  Return null if the "high pc" attribute is either not present, or if it
4773    cannot be represented as an assembler label identifier.  */
4774
4775 static inline const char *
4776 get_AT_hi_pc (dw_die_ref die)
4777 {
4778   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4779
4780   return a ? AT_lbl (a) : NULL;
4781 }
4782
4783 /* Return the value of the string attribute designated by ATTR_KIND, or
4784    NULL if it is not present.  */
4785
4786 static inline const char *
4787 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4788 {
4789   dw_attr_ref a = get_AT (die, attr_kind);
4790
4791   return a ? AT_string (a) : NULL;
4792 }
4793
4794 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4795    if it is not present.  */
4796
4797 static inline int
4798 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4799 {
4800   dw_attr_ref a = get_AT (die, attr_kind);
4801
4802   return a ? AT_flag (a) : 0;
4803 }
4804
4805 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4806    if it is not present.  */
4807
4808 static inline unsigned
4809 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4810 {
4811   dw_attr_ref a = get_AT (die, attr_kind);
4812
4813   return a ? AT_unsigned (a) : 0;
4814 }
4815
4816 static inline dw_die_ref
4817 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4818 {
4819   dw_attr_ref a = get_AT (die, attr_kind);
4820
4821   return a ? AT_ref (a) : NULL;
4822 }
4823
4824 static inline struct dwarf_file_data *
4825 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4826 {
4827   dw_attr_ref a = get_AT (die, attr_kind);
4828
4829   return a ? AT_file (a) : NULL;
4830 }
4831
4832 /* Return TRUE if the language is C++.  */
4833
4834 static inline bool
4835 is_cxx (void)
4836 {
4837   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4838
4839   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4840 }
4841
4842 /* Return TRUE if the language is Fortran.  */
4843
4844 static inline bool
4845 is_fortran (void)
4846 {
4847   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4848
4849   return (lang == DW_LANG_Fortran77
4850           || lang == DW_LANG_Fortran90
4851           || lang == DW_LANG_Fortran95);
4852 }
4853
4854 /* Return TRUE if the language is Ada.  */
4855
4856 static inline bool
4857 is_ada (void)
4858 {
4859   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4860
4861   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4862 }
4863
4864 /* Remove the specified attribute if present.  */
4865
4866 static void
4867 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4868 {
4869   dw_attr_ref a;
4870   unsigned ix;
4871
4872   if (! die)
4873     return;
4874
4875   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4876     if (a->dw_attr == attr_kind)
4877       {
4878         if (AT_class (a) == dw_val_class_str)
4879           if (a->dw_attr_val.v.val_str->refcount)
4880             a->dw_attr_val.v.val_str->refcount--;
4881
4882         /* VEC_ordered_remove should help reduce the number of abbrevs
4883            that are needed.  */
4884         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4885         return;
4886       }
4887 }
4888
4889 /* Remove CHILD from its parent.  PREV must have the property that
4890    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4891
4892 static void
4893 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4894 {
4895   gcc_assert (child->die_parent == prev->die_parent);
4896   gcc_assert (prev->die_sib == child);
4897   if (prev == child)
4898     {
4899       gcc_assert (child->die_parent->die_child == child);
4900       prev = NULL;
4901     }
4902   else
4903     prev->die_sib = child->die_sib;
4904   if (child->die_parent->die_child == child)
4905     child->die_parent->die_child = prev;
4906 }
4907
4908 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4909    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4910
4911 static void
4912 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4913 {
4914   dw_die_ref parent = old_child->die_parent;
4915
4916   gcc_assert (parent == prev->die_parent);
4917   gcc_assert (prev->die_sib == old_child);
4918
4919   new_child->die_parent = parent;
4920   if (prev == old_child)
4921     {
4922       gcc_assert (parent->die_child == old_child);
4923       new_child->die_sib = new_child;
4924     }
4925   else
4926     {
4927       prev->die_sib = new_child;
4928       new_child->die_sib = old_child->die_sib;
4929     }
4930   if (old_child->die_parent->die_child == old_child)
4931     old_child->die_parent->die_child = new_child;
4932 }
4933
4934 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4935
4936 static void
4937 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4938 {
4939   dw_die_ref c;
4940   new_parent->die_child = old_parent->die_child;
4941   old_parent->die_child = NULL;
4942   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4943 }
4944
4945 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4946    matches TAG.  */
4947
4948 static void
4949 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4950 {
4951   dw_die_ref c;
4952
4953   c = die->die_child;
4954   if (c) do {
4955     dw_die_ref prev = c;
4956     c = c->die_sib;
4957     while (c->die_tag == tag)
4958       {
4959         remove_child_with_prev (c, prev);
4960         /* Might have removed every child.  */
4961         if (c == c->die_sib)
4962           return;
4963         c = c->die_sib;
4964       }
4965   } while (c != die->die_child);
4966 }
4967
4968 /* Add a CHILD_DIE as the last child of DIE.  */
4969
4970 static void
4971 add_child_die (dw_die_ref die, dw_die_ref child_die)
4972 {
4973   /* FIXME this should probably be an assert.  */
4974   if (! die || ! child_die)
4975     return;
4976   gcc_assert (die != child_die);
4977
4978   child_die->die_parent = die;
4979   if (die->die_child)
4980     {
4981       child_die->die_sib = die->die_child->die_sib;
4982       die->die_child->die_sib = child_die;
4983     }
4984   else
4985     child_die->die_sib = child_die;
4986   die->die_child = child_die;
4987 }
4988
4989 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4990    is the specification, to the end of PARENT's list of children.
4991    This is done by removing and re-adding it.  */
4992
4993 static void
4994 splice_child_die (dw_die_ref parent, dw_die_ref child)
4995 {
4996   dw_die_ref p;
4997
4998   /* We want the declaration DIE from inside the class, not the
4999      specification DIE at toplevel.  */
5000   if (child->die_parent != parent)
5001     {
5002       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5003
5004       if (tmp)
5005         child = tmp;
5006     }
5007
5008   gcc_assert (child->die_parent == parent
5009               || (child->die_parent
5010                   == get_AT_ref (parent, DW_AT_specification)));
5011
5012   for (p = child->die_parent->die_child; ; p = p->die_sib)
5013     if (p->die_sib == child)
5014       {
5015         remove_child_with_prev (child, p);
5016         break;
5017       }
5018
5019   add_child_die (parent, child);
5020 }
5021
5022 /* Return a pointer to a newly created DIE node.  */
5023
5024 static inline dw_die_ref
5025 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5026 {
5027   dw_die_ref die = ggc_alloc_cleared_die_node ();
5028
5029   die->die_tag = tag_value;
5030
5031   if (parent_die != NULL)
5032     add_child_die (parent_die, die);
5033   else
5034     {
5035       limbo_die_node *limbo_node;
5036
5037       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5038       limbo_node->die = die;
5039       limbo_node->created_for = t;
5040       limbo_node->next = limbo_die_list;
5041       limbo_die_list = limbo_node;
5042     }
5043
5044   return die;
5045 }
5046
5047 /* Return the DIE associated with the given type specifier.  */
5048
5049 static inline dw_die_ref
5050 lookup_type_die (tree type)
5051 {
5052   return TYPE_SYMTAB_DIE (type);
5053 }
5054
5055 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5056    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5057    anonymous type instead the one of the naming typedef.  */
5058
5059 static inline dw_die_ref
5060 strip_naming_typedef (tree type, dw_die_ref type_die)
5061 {
5062   if (type
5063       && TREE_CODE (type) == RECORD_TYPE
5064       && type_die
5065       && type_die->die_tag == DW_TAG_typedef
5066       && is_naming_typedef_decl (TYPE_NAME (type)))
5067     type_die = get_AT_ref (type_die, DW_AT_type);
5068   return type_die;
5069 }
5070
5071 /* Like lookup_type_die, but if type is an anonymous type named by a
5072    typedef[1], return the DIE of the anonymous type instead the one of
5073    the naming typedef.  This is because in gen_typedef_die, we did
5074    equate the anonymous struct named by the typedef with the DIE of
5075    the naming typedef. So by default, lookup_type_die on an anonymous
5076    struct yields the DIE of the naming typedef.
5077
5078    [1]: Read the comment of is_naming_typedef_decl to learn about what
5079    a naming typedef is.  */
5080
5081 static inline dw_die_ref
5082 lookup_type_die_strip_naming_typedef (tree type)
5083 {
5084   dw_die_ref die = lookup_type_die (type);
5085   return strip_naming_typedef (type, die);
5086 }
5087
5088 /* Equate a DIE to a given type specifier.  */
5089
5090 static inline void
5091 equate_type_number_to_die (tree type, dw_die_ref type_die)
5092 {
5093   TYPE_SYMTAB_DIE (type) = type_die;
5094 }
5095
5096 /* Returns a hash value for X (which really is a die_struct).  */
5097
5098 static hashval_t
5099 decl_die_table_hash (const void *x)
5100 {
5101   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5102 }
5103
5104 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5105
5106 static int
5107 decl_die_table_eq (const void *x, const void *y)
5108 {
5109   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5110 }
5111
5112 /* Return the DIE associated with a given declaration.  */
5113
5114 static inline dw_die_ref
5115 lookup_decl_die (tree decl)
5116 {
5117   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5118 }
5119
5120 /* Returns a hash value for X (which really is a var_loc_list).  */
5121
5122 static hashval_t
5123 decl_loc_table_hash (const void *x)
5124 {
5125   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5126 }
5127
5128 /* Return nonzero if decl_id of var_loc_list X is the same as
5129    UID of decl *Y.  */
5130
5131 static int
5132 decl_loc_table_eq (const void *x, const void *y)
5133 {
5134   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5135 }
5136
5137 /* Return the var_loc list associated with a given declaration.  */
5138
5139 static inline var_loc_list *
5140 lookup_decl_loc (const_tree decl)
5141 {
5142   if (!decl_loc_table)
5143     return NULL;
5144   return (var_loc_list *)
5145     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5146 }
5147
5148 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5149
5150 static hashval_t
5151 cached_dw_loc_list_table_hash (const void *x)
5152 {
5153   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5154 }
5155
5156 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5157    UID of decl *Y.  */
5158
5159 static int
5160 cached_dw_loc_list_table_eq (const void *x, const void *y)
5161 {
5162   return (((const cached_dw_loc_list *) x)->decl_id
5163           == DECL_UID ((const_tree) y));
5164 }
5165
5166 /* Equate a DIE to a particular declaration.  */
5167
5168 static void
5169 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5170 {
5171   unsigned int decl_id = DECL_UID (decl);
5172   void **slot;
5173
5174   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5175   *slot = decl_die;
5176   decl_die->decl_id = decl_id;
5177 }
5178
5179 /* Return how many bits covers PIECE EXPR_LIST.  */
5180
5181 static int
5182 decl_piece_bitsize (rtx piece)
5183 {
5184   int ret = (int) GET_MODE (piece);
5185   if (ret)
5186     return ret;
5187   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5188               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5189   return INTVAL (XEXP (XEXP (piece, 0), 0));
5190 }
5191
5192 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5193
5194 static rtx *
5195 decl_piece_varloc_ptr (rtx piece)
5196 {
5197   if ((int) GET_MODE (piece))
5198     return &XEXP (piece, 0);
5199   else
5200     return &XEXP (XEXP (piece, 0), 1);
5201 }
5202
5203 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5204    Next is the chain of following piece nodes.  */
5205
5206 static rtx
5207 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5208 {
5209   if (bitsize <= (int) MAX_MACHINE_MODE)
5210     return alloc_EXPR_LIST (bitsize, loc_note, next);
5211   else
5212     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5213                                                GEN_INT (bitsize),
5214                                                loc_note), next);
5215 }
5216
5217 /* Return rtx that should be stored into loc field for
5218    LOC_NOTE and BITPOS/BITSIZE.  */
5219
5220 static rtx
5221 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5222                       HOST_WIDE_INT bitsize)
5223 {
5224   if (bitsize != -1)
5225     {
5226       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5227       if (bitpos != 0)
5228         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5229     }
5230   return loc_note;
5231 }
5232
5233 /* This function either modifies location piece list *DEST in
5234    place (if SRC and INNER is NULL), or copies location piece list
5235    *SRC to *DEST while modifying it.  Location BITPOS is modified
5236    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5237    not copied and if needed some padding around it is added.
5238    When modifying in place, DEST should point to EXPR_LIST where
5239    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5240    to the start of the whole list and INNER points to the EXPR_LIST
5241    where earlier pieces cover PIECE_BITPOS bits.  */
5242
5243 static void
5244 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5245                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5246                    HOST_WIDE_INT bitsize, rtx loc_note)
5247 {
5248   int diff;
5249   bool copy = inner != NULL;
5250
5251   if (copy)
5252     {
5253       /* First copy all nodes preceeding the current bitpos.  */
5254       while (src != inner)
5255         {
5256           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5257                                    decl_piece_bitsize (*src), NULL_RTX);
5258           dest = &XEXP (*dest, 1);
5259           src = &XEXP (*src, 1);
5260         }
5261     }
5262   /* Add padding if needed.  */
5263   if (bitpos != piece_bitpos)
5264     {
5265       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5266                                copy ? NULL_RTX : *dest);
5267       dest = &XEXP (*dest, 1);
5268     }
5269   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5270     {
5271       gcc_assert (!copy);
5272       /* A piece with correct bitpos and bitsize already exist,
5273          just update the location for it and return.  */
5274       *decl_piece_varloc_ptr (*dest) = loc_note;
5275       return;
5276     }
5277   /* Add the piece that changed.  */
5278   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5279   dest = &XEXP (*dest, 1);
5280   /* Skip over pieces that overlap it.  */
5281   diff = bitpos - piece_bitpos + bitsize;
5282   if (!copy)
5283     src = dest;
5284   while (diff > 0 && *src)
5285     {
5286       rtx piece = *src;
5287       diff -= decl_piece_bitsize (piece);
5288       if (copy)
5289         src = &XEXP (piece, 1);
5290       else
5291         {
5292           *src = XEXP (piece, 1);
5293           free_EXPR_LIST_node (piece);
5294         }
5295     }
5296   /* Add padding if needed.  */
5297   if (diff < 0 && *src)
5298     {
5299       if (!copy)
5300         dest = src;
5301       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5302       dest = &XEXP (*dest, 1);
5303     }
5304   if (!copy)
5305     return;
5306   /* Finally copy all nodes following it.  */
5307   while (*src)
5308     {
5309       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5310                                decl_piece_bitsize (*src), NULL_RTX);
5311       dest = &XEXP (*dest, 1);
5312       src = &XEXP (*src, 1);
5313     }
5314 }
5315
5316 /* Add a variable location node to the linked list for DECL.  */
5317
5318 static struct var_loc_node *
5319 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5320 {
5321   unsigned int decl_id;
5322   var_loc_list *temp;
5323   void **slot;
5324   struct var_loc_node *loc = NULL;
5325   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5326
5327   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5328     {
5329       tree realdecl = DECL_DEBUG_EXPR (decl);
5330       if (realdecl && handled_component_p (realdecl))
5331         {
5332           HOST_WIDE_INT maxsize;
5333           tree innerdecl;
5334           innerdecl
5335             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5336           if (!DECL_P (innerdecl)
5337               || DECL_IGNORED_P (innerdecl)
5338               || TREE_STATIC (innerdecl)
5339               || bitsize <= 0
5340               || bitpos + bitsize > 256
5341               || bitsize != maxsize)
5342             return NULL;
5343           decl = innerdecl;
5344         }
5345     }
5346
5347   decl_id = DECL_UID (decl);
5348   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5349   if (*slot == NULL)
5350     {
5351       temp = ggc_alloc_cleared_var_loc_list ();
5352       temp->decl_id = decl_id;
5353       *slot = temp;
5354     }
5355   else
5356     temp = (var_loc_list *) *slot;
5357
5358   /* For PARM_DECLs try to keep around the original incoming value,
5359      even if that means we'll emit a zero-range .debug_loc entry.  */
5360   if (temp->last
5361       && temp->first == temp->last
5362       && TREE_CODE (decl) == PARM_DECL
5363       && GET_CODE (temp->first->loc) == NOTE
5364       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5365       && DECL_INCOMING_RTL (decl)
5366       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5367       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5368          == GET_CODE (DECL_INCOMING_RTL (decl))
5369       && prev_real_insn (temp->first->loc) == NULL_RTX
5370       && (bitsize != -1
5371           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5372                            NOTE_VAR_LOCATION_LOC (loc_note))
5373           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5374               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5375     {
5376       loc = ggc_alloc_cleared_var_loc_node ();
5377       temp->first->next = loc;
5378       temp->last = loc;
5379       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5380     }
5381   else if (temp->last)
5382     {
5383       struct var_loc_node *last = temp->last, *unused = NULL;
5384       rtx *piece_loc = NULL, last_loc_note;
5385       int piece_bitpos = 0;
5386       if (last->next)
5387         {
5388           last = last->next;
5389           gcc_assert (last->next == NULL);
5390         }
5391       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5392         {
5393           piece_loc = &last->loc;
5394           do
5395             {
5396               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5397               if (piece_bitpos + cur_bitsize > bitpos)
5398                 break;
5399               piece_bitpos += cur_bitsize;
5400               piece_loc = &XEXP (*piece_loc, 1);
5401             }
5402           while (*piece_loc);
5403         }
5404       /* TEMP->LAST here is either pointer to the last but one or
5405          last element in the chained list, LAST is pointer to the
5406          last element.  */
5407       if (label && strcmp (last->label, label) == 0)
5408         {
5409           /* For SRA optimized variables if there weren't any real
5410              insns since last note, just modify the last node.  */
5411           if (piece_loc != NULL)
5412             {
5413               adjust_piece_list (piece_loc, NULL, NULL,
5414                                  bitpos, piece_bitpos, bitsize, loc_note);
5415               return NULL;
5416             }
5417           /* If the last note doesn't cover any instructions, remove it.  */
5418           if (temp->last != last)
5419             {
5420               temp->last->next = NULL;
5421               unused = last;
5422               last = temp->last;
5423               gcc_assert (strcmp (last->label, label) != 0);
5424             }
5425           else
5426             {
5427               gcc_assert (temp->first == temp->last
5428                           || (temp->first->next == temp->last
5429                               && TREE_CODE (decl) == PARM_DECL));
5430               memset (temp->last, '\0', sizeof (*temp->last));
5431               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5432               return temp->last;
5433             }
5434         }
5435       if (bitsize == -1 && NOTE_P (last->loc))
5436         last_loc_note = last->loc;
5437       else if (piece_loc != NULL
5438                && *piece_loc != NULL_RTX
5439                && piece_bitpos == bitpos
5440                && decl_piece_bitsize (*piece_loc) == bitsize)
5441         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5442       else
5443         last_loc_note = NULL_RTX;
5444       /* If the current location is the same as the end of the list,
5445          and either both or neither of the locations is uninitialized,
5446          we have nothing to do.  */
5447       if (last_loc_note == NULL_RTX
5448           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5449                             NOTE_VAR_LOCATION_LOC (loc_note)))
5450           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5451                != NOTE_VAR_LOCATION_STATUS (loc_note))
5452               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5453                    == VAR_INIT_STATUS_UNINITIALIZED)
5454                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5455                       == VAR_INIT_STATUS_UNINITIALIZED))))
5456         {
5457           /* Add LOC to the end of list and update LAST.  If the last
5458              element of the list has been removed above, reuse its
5459              memory for the new node, otherwise allocate a new one.  */
5460           if (unused)
5461             {
5462               loc = unused;
5463               memset (loc, '\0', sizeof (*loc));
5464             }
5465           else
5466             loc = ggc_alloc_cleared_var_loc_node ();
5467           if (bitsize == -1 || piece_loc == NULL)
5468             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5469           else
5470             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5471                                bitpos, piece_bitpos, bitsize, loc_note);
5472           last->next = loc;
5473           /* Ensure TEMP->LAST will point either to the new last but one
5474              element of the chain, or to the last element in it.  */
5475           if (last != temp->last)
5476             temp->last = last;
5477         }
5478       else if (unused)
5479         ggc_free (unused);
5480     }
5481   else
5482     {
5483       loc = ggc_alloc_cleared_var_loc_node ();
5484       temp->first = loc;
5485       temp->last = loc;
5486       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5487     }
5488   return loc;
5489 }
5490 \f
5491 /* Keep track of the number of spaces used to indent the
5492    output of the debugging routines that print the structure of
5493    the DIE internal representation.  */
5494 static int print_indent;
5495
5496 /* Indent the line the number of spaces given by print_indent.  */
5497
5498 static inline void
5499 print_spaces (FILE *outfile)
5500 {
5501   fprintf (outfile, "%*s", print_indent, "");
5502 }
5503
5504 /* Print a type signature in hex.  */
5505
5506 static inline void
5507 print_signature (FILE *outfile, char *sig)
5508 {
5509   int i;
5510
5511   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5512     fprintf (outfile, "%02x", sig[i] & 0xff);
5513 }
5514
5515 /* Print the information associated with a given DIE, and its children.
5516    This routine is a debugging aid only.  */
5517
5518 static void
5519 print_die (dw_die_ref die, FILE *outfile)
5520 {
5521   dw_attr_ref a;
5522   dw_die_ref c;
5523   unsigned ix;
5524
5525   print_spaces (outfile);
5526   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5527            die->die_offset, dwarf_tag_name (die->die_tag),
5528            (void*) die);
5529   print_spaces (outfile);
5530   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5531   fprintf (outfile, " offset: %ld", die->die_offset);
5532   fprintf (outfile, " mark: %d\n", die->die_mark);
5533
5534   if (use_debug_types && die->die_id.die_type_node)
5535     {
5536       print_spaces (outfile);
5537       fprintf (outfile, "  signature: ");
5538       print_signature (outfile, die->die_id.die_type_node->signature);
5539       fprintf (outfile, "\n");
5540     }
5541
5542   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5543     {
5544       print_spaces (outfile);
5545       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5546
5547       switch (AT_class (a))
5548         {
5549         case dw_val_class_addr:
5550           fprintf (outfile, "address");
5551           break;
5552         case dw_val_class_offset:
5553           fprintf (outfile, "offset");
5554           break;
5555         case dw_val_class_loc:
5556           fprintf (outfile, "location descriptor");
5557           break;
5558         case dw_val_class_loc_list:
5559           fprintf (outfile, "location list -> label:%s",
5560                    AT_loc_list (a)->ll_symbol);
5561           break;
5562         case dw_val_class_range_list:
5563           fprintf (outfile, "range list");
5564           break;
5565         case dw_val_class_const:
5566           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5567           break;
5568         case dw_val_class_unsigned_const:
5569           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5570           break;
5571         case dw_val_class_const_double:
5572           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5573                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5574                    a->dw_attr_val.v.val_double.high,
5575                    a->dw_attr_val.v.val_double.low);
5576           break;
5577         case dw_val_class_vec:
5578           fprintf (outfile, "floating-point or vector constant");
5579           break;
5580         case dw_val_class_flag:
5581           fprintf (outfile, "%u", AT_flag (a));
5582           break;
5583         case dw_val_class_die_ref:
5584           if (AT_ref (a) != NULL)
5585             {
5586               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5587                 {
5588                   fprintf (outfile, "die -> signature: ");
5589                   print_signature (outfile,
5590                                    AT_ref (a)->die_id.die_type_node->signature);
5591                 }
5592               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5593                 fprintf (outfile, "die -> label: %s",
5594                          AT_ref (a)->die_id.die_symbol);
5595               else
5596                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5597               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5598             }
5599           else
5600             fprintf (outfile, "die -> <null>");
5601           break;
5602         case dw_val_class_vms_delta:
5603           fprintf (outfile, "delta: @slotcount(%s-%s)",
5604                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5605           break;
5606         case dw_val_class_lbl_id:
5607         case dw_val_class_lineptr:
5608         case dw_val_class_macptr:
5609           fprintf (outfile, "label: %s", AT_lbl (a));
5610           break;
5611         case dw_val_class_str:
5612           if (AT_string (a) != NULL)
5613             fprintf (outfile, "\"%s\"", AT_string (a));
5614           else
5615             fprintf (outfile, "<null>");
5616           break;
5617         case dw_val_class_file:
5618           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5619                    AT_file (a)->emitted_number);
5620           break;
5621         case dw_val_class_data8:
5622           {
5623             int i;
5624
5625             for (i = 0; i < 8; i++)
5626               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5627             break;
5628           }
5629         default:
5630           break;
5631         }
5632
5633       fprintf (outfile, "\n");
5634     }
5635
5636   if (die->die_child != NULL)
5637     {
5638       print_indent += 4;
5639       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5640       print_indent -= 4;
5641     }
5642   if (print_indent == 0)
5643     fprintf (outfile, "\n");
5644 }
5645
5646 /* Print the information collected for a given DIE.  */
5647
5648 DEBUG_FUNCTION void
5649 debug_dwarf_die (dw_die_ref die)
5650 {
5651   print_die (die, stderr);
5652 }
5653
5654 /* Print all DWARF information collected for the compilation unit.
5655    This routine is a debugging aid only.  */
5656
5657 DEBUG_FUNCTION void
5658 debug_dwarf (void)
5659 {
5660   print_indent = 0;
5661   print_die (comp_unit_die (), stderr);
5662 }
5663 \f
5664 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5665    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5666    DIE that marks the start of the DIEs for this include file.  */
5667
5668 static dw_die_ref
5669 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5670 {
5671   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5672   dw_die_ref new_unit = gen_compile_unit_die (filename);
5673
5674   new_unit->die_sib = old_unit;
5675   return new_unit;
5676 }
5677
5678 /* Close an include-file CU and reopen the enclosing one.  */
5679
5680 static dw_die_ref
5681 pop_compile_unit (dw_die_ref old_unit)
5682 {
5683   dw_die_ref new_unit = old_unit->die_sib;
5684
5685   old_unit->die_sib = NULL;
5686   return new_unit;
5687 }
5688
5689 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5690 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5691
5692 /* Calculate the checksum of a location expression.  */
5693
5694 static inline void
5695 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5696 {
5697   int tem;
5698
5699   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5700   CHECKSUM (tem);
5701   CHECKSUM (loc->dw_loc_oprnd1);
5702   CHECKSUM (loc->dw_loc_oprnd2);
5703 }
5704
5705 /* Calculate the checksum of an attribute.  */
5706
5707 static void
5708 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5709 {
5710   dw_loc_descr_ref loc;
5711   rtx r;
5712
5713   CHECKSUM (at->dw_attr);
5714
5715   /* We don't care that this was compiled with a different compiler
5716      snapshot; if the output is the same, that's what matters.  */
5717   if (at->dw_attr == DW_AT_producer)
5718     return;
5719
5720   switch (AT_class (at))
5721     {
5722     case dw_val_class_const:
5723       CHECKSUM (at->dw_attr_val.v.val_int);
5724       break;
5725     case dw_val_class_unsigned_const:
5726       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5727       break;
5728     case dw_val_class_const_double:
5729       CHECKSUM (at->dw_attr_val.v.val_double);
5730       break;
5731     case dw_val_class_vec:
5732       CHECKSUM (at->dw_attr_val.v.val_vec);
5733       break;
5734     case dw_val_class_flag:
5735       CHECKSUM (at->dw_attr_val.v.val_flag);
5736       break;
5737     case dw_val_class_str:
5738       CHECKSUM_STRING (AT_string (at));
5739       break;
5740
5741     case dw_val_class_addr:
5742       r = AT_addr (at);
5743       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5744       CHECKSUM_STRING (XSTR (r, 0));
5745       break;
5746
5747     case dw_val_class_offset:
5748       CHECKSUM (at->dw_attr_val.v.val_offset);
5749       break;
5750
5751     case dw_val_class_loc:
5752       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5753         loc_checksum (loc, ctx);
5754       break;
5755
5756     case dw_val_class_die_ref:
5757       die_checksum (AT_ref (at), ctx, mark);
5758       break;
5759
5760     case dw_val_class_fde_ref:
5761     case dw_val_class_vms_delta:
5762     case dw_val_class_lbl_id:
5763     case dw_val_class_lineptr:
5764     case dw_val_class_macptr:
5765       break;
5766
5767     case dw_val_class_file:
5768       CHECKSUM_STRING (AT_file (at)->filename);
5769       break;
5770
5771     case dw_val_class_data8:
5772       CHECKSUM (at->dw_attr_val.v.val_data8);
5773       break;
5774
5775     default:
5776       break;
5777     }
5778 }
5779
5780 /* Calculate the checksum of a DIE.  */
5781
5782 static void
5783 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5784 {
5785   dw_die_ref c;
5786   dw_attr_ref a;
5787   unsigned ix;
5788
5789   /* To avoid infinite recursion.  */
5790   if (die->die_mark)
5791     {
5792       CHECKSUM (die->die_mark);
5793       return;
5794     }
5795   die->die_mark = ++(*mark);
5796
5797   CHECKSUM (die->die_tag);
5798
5799   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5800     attr_checksum (a, ctx, mark);
5801
5802   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5803 }
5804
5805 #undef CHECKSUM
5806 #undef CHECKSUM_STRING
5807
5808 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5809 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5810 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5811 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5812 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5813 #define CHECKSUM_ATTR(FOO) \
5814   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5815
5816 /* Calculate the checksum of a number in signed LEB128 format.  */
5817
5818 static void
5819 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5820 {
5821   unsigned char byte;
5822   bool more;
5823
5824   while (1)
5825     {
5826       byte = (value & 0x7f);
5827       value >>= 7;
5828       more = !((value == 0 && (byte & 0x40) == 0)
5829                 || (value == -1 && (byte & 0x40) != 0));
5830       if (more)
5831         byte |= 0x80;
5832       CHECKSUM (byte);
5833       if (!more)
5834         break;
5835     }
5836 }
5837
5838 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5839
5840 static void
5841 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5842 {
5843   while (1)
5844     {
5845       unsigned char byte = (value & 0x7f);
5846       value >>= 7;
5847       if (value != 0)
5848         /* More bytes to follow.  */
5849         byte |= 0x80;
5850       CHECKSUM (byte);
5851       if (value == 0)
5852         break;
5853     }
5854 }
5855
5856 /* Checksum the context of the DIE.  This adds the names of any
5857    surrounding namespaces or structures to the checksum.  */
5858
5859 static void
5860 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5861 {
5862   const char *name;
5863   dw_die_ref spec;
5864   int tag = die->die_tag;
5865
5866   if (tag != DW_TAG_namespace
5867       && tag != DW_TAG_structure_type
5868       && tag != DW_TAG_class_type)
5869     return;
5870
5871   name = get_AT_string (die, DW_AT_name);
5872
5873   spec = get_AT_ref (die, DW_AT_specification);
5874   if (spec != NULL)
5875     die = spec;
5876
5877   if (die->die_parent != NULL)
5878     checksum_die_context (die->die_parent, ctx);
5879
5880   CHECKSUM_ULEB128 ('C');
5881   CHECKSUM_ULEB128 (tag);
5882   if (name != NULL)
5883     CHECKSUM_STRING (name);
5884 }
5885
5886 /* Calculate the checksum of a location expression.  */
5887
5888 static inline void
5889 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5890 {
5891   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5892      were emitted as a DW_FORM_sdata instead of a location expression.  */
5893   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5894     {
5895       CHECKSUM_ULEB128 (DW_FORM_sdata);
5896       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5897       return;
5898     }
5899
5900   /* Otherwise, just checksum the raw location expression.  */
5901   while (loc != NULL)
5902     {
5903       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5904       CHECKSUM (loc->dw_loc_oprnd1);
5905       CHECKSUM (loc->dw_loc_oprnd2);
5906       loc = loc->dw_loc_next;
5907     }
5908 }
5909
5910 /* Calculate the checksum of an attribute.  */
5911
5912 static void
5913 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5914                        struct md5_ctx *ctx, int *mark)
5915 {
5916   dw_loc_descr_ref loc;
5917   rtx r;
5918
5919   if (AT_class (at) == dw_val_class_die_ref)
5920     {
5921       dw_die_ref target_die = AT_ref (at);
5922
5923       /* For pointer and reference types, we checksum only the (qualified)
5924          name of the target type (if there is a name).  For friend entries,
5925          we checksum only the (qualified) name of the target type or function.
5926          This allows the checksum to remain the same whether the target type
5927          is complete or not.  */
5928       if ((at->dw_attr == DW_AT_type
5929            && (tag == DW_TAG_pointer_type
5930                || tag == DW_TAG_reference_type
5931                || tag == DW_TAG_rvalue_reference_type
5932                || tag == DW_TAG_ptr_to_member_type))
5933           || (at->dw_attr == DW_AT_friend
5934               && tag == DW_TAG_friend))
5935         {
5936           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5937
5938           if (name_attr != NULL)
5939             {
5940               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5941
5942               if (decl == NULL)
5943                 decl = target_die;
5944               CHECKSUM_ULEB128 ('N');
5945               CHECKSUM_ULEB128 (at->dw_attr);
5946               if (decl->die_parent != NULL)
5947                 checksum_die_context (decl->die_parent, ctx);
5948               CHECKSUM_ULEB128 ('E');
5949               CHECKSUM_STRING (AT_string (name_attr));
5950               return;
5951             }
5952         }
5953
5954       /* For all other references to another DIE, we check to see if the
5955          target DIE has already been visited.  If it has, we emit a
5956          backward reference; if not, we descend recursively.  */
5957       if (target_die->die_mark > 0)
5958         {
5959           CHECKSUM_ULEB128 ('R');
5960           CHECKSUM_ULEB128 (at->dw_attr);
5961           CHECKSUM_ULEB128 (target_die->die_mark);
5962         }
5963       else
5964         {
5965           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5966
5967           if (decl == NULL)
5968             decl = target_die;
5969           target_die->die_mark = ++(*mark);
5970           CHECKSUM_ULEB128 ('T');
5971           CHECKSUM_ULEB128 (at->dw_attr);
5972           if (decl->die_parent != NULL)
5973             checksum_die_context (decl->die_parent, ctx);
5974           die_checksum_ordered (target_die, ctx, mark);
5975         }
5976       return;
5977     }
5978
5979   CHECKSUM_ULEB128 ('A');
5980   CHECKSUM_ULEB128 (at->dw_attr);
5981
5982   switch (AT_class (at))
5983     {
5984     case dw_val_class_const:
5985       CHECKSUM_ULEB128 (DW_FORM_sdata);
5986       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5987       break;
5988
5989     case dw_val_class_unsigned_const:
5990       CHECKSUM_ULEB128 (DW_FORM_sdata);
5991       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5992       break;
5993
5994     case dw_val_class_const_double:
5995       CHECKSUM_ULEB128 (DW_FORM_block);
5996       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5997       CHECKSUM (at->dw_attr_val.v.val_double);
5998       break;
5999
6000     case dw_val_class_vec:
6001       CHECKSUM_ULEB128 (DW_FORM_block);
6002       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6003       CHECKSUM (at->dw_attr_val.v.val_vec);
6004       break;
6005
6006     case dw_val_class_flag:
6007       CHECKSUM_ULEB128 (DW_FORM_flag);
6008       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6009       break;
6010
6011     case dw_val_class_str:
6012       CHECKSUM_ULEB128 (DW_FORM_string);
6013       CHECKSUM_STRING (AT_string (at));
6014       break;
6015
6016     case dw_val_class_addr:
6017       r = AT_addr (at);
6018       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6019       CHECKSUM_ULEB128 (DW_FORM_string);
6020       CHECKSUM_STRING (XSTR (r, 0));
6021       break;
6022
6023     case dw_val_class_offset:
6024       CHECKSUM_ULEB128 (DW_FORM_sdata);
6025       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6026       break;
6027
6028     case dw_val_class_loc:
6029       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6030         loc_checksum_ordered (loc, ctx);
6031       break;
6032
6033     case dw_val_class_fde_ref:
6034     case dw_val_class_lbl_id:
6035     case dw_val_class_lineptr:
6036     case dw_val_class_macptr:
6037       break;
6038
6039     case dw_val_class_file:
6040       CHECKSUM_ULEB128 (DW_FORM_string);
6041       CHECKSUM_STRING (AT_file (at)->filename);
6042       break;
6043
6044     case dw_val_class_data8:
6045       CHECKSUM (at->dw_attr_val.v.val_data8);
6046       break;
6047
6048     default:
6049       break;
6050     }
6051 }
6052
6053 struct checksum_attributes
6054 {
6055   dw_attr_ref at_name;
6056   dw_attr_ref at_type;
6057   dw_attr_ref at_friend;
6058   dw_attr_ref at_accessibility;
6059   dw_attr_ref at_address_class;
6060   dw_attr_ref at_allocated;
6061   dw_attr_ref at_artificial;
6062   dw_attr_ref at_associated;
6063   dw_attr_ref at_binary_scale;
6064   dw_attr_ref at_bit_offset;
6065   dw_attr_ref at_bit_size;
6066   dw_attr_ref at_bit_stride;
6067   dw_attr_ref at_byte_size;
6068   dw_attr_ref at_byte_stride;
6069   dw_attr_ref at_const_value;
6070   dw_attr_ref at_containing_type;
6071   dw_attr_ref at_count;
6072   dw_attr_ref at_data_location;
6073   dw_attr_ref at_data_member_location;
6074   dw_attr_ref at_decimal_scale;
6075   dw_attr_ref at_decimal_sign;
6076   dw_attr_ref at_default_value;
6077   dw_attr_ref at_digit_count;
6078   dw_attr_ref at_discr;
6079   dw_attr_ref at_discr_list;
6080   dw_attr_ref at_discr_value;
6081   dw_attr_ref at_encoding;
6082   dw_attr_ref at_endianity;
6083   dw_attr_ref at_explicit;
6084   dw_attr_ref at_is_optional;
6085   dw_attr_ref at_location;
6086   dw_attr_ref at_lower_bound;
6087   dw_attr_ref at_mutable;
6088   dw_attr_ref at_ordering;
6089   dw_attr_ref at_picture_string;
6090   dw_attr_ref at_prototyped;
6091   dw_attr_ref at_small;
6092   dw_attr_ref at_segment;
6093   dw_attr_ref at_string_length;
6094   dw_attr_ref at_threads_scaled;
6095   dw_attr_ref at_upper_bound;
6096   dw_attr_ref at_use_location;
6097   dw_attr_ref at_use_UTF8;
6098   dw_attr_ref at_variable_parameter;
6099   dw_attr_ref at_virtuality;
6100   dw_attr_ref at_visibility;
6101   dw_attr_ref at_vtable_elem_location;
6102 };
6103
6104 /* Collect the attributes that we will want to use for the checksum.  */
6105
6106 static void
6107 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6108 {
6109   dw_attr_ref a;
6110   unsigned ix;
6111
6112   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6113     {
6114       switch (a->dw_attr)
6115         {
6116         case DW_AT_name:
6117           attrs->at_name = a;
6118           break;
6119         case DW_AT_type:
6120           attrs->at_type = a;
6121           break;
6122         case DW_AT_friend:
6123           attrs->at_friend = a;
6124           break;
6125         case DW_AT_accessibility:
6126           attrs->at_accessibility = a;
6127           break;
6128         case DW_AT_address_class:
6129           attrs->at_address_class = a;
6130           break;
6131         case DW_AT_allocated:
6132           attrs->at_allocated = a;
6133           break;
6134         case DW_AT_artificial:
6135           attrs->at_artificial = a;
6136           break;
6137         case DW_AT_associated:
6138           attrs->at_associated = a;
6139           break;
6140         case DW_AT_binary_scale:
6141           attrs->at_binary_scale = a;
6142           break;
6143         case DW_AT_bit_offset:
6144           attrs->at_bit_offset = a;
6145           break;
6146         case DW_AT_bit_size:
6147           attrs->at_bit_size = a;
6148           break;
6149         case DW_AT_bit_stride:
6150           attrs->at_bit_stride = a;
6151           break;
6152         case DW_AT_byte_size:
6153           attrs->at_byte_size = a;
6154           break;
6155         case DW_AT_byte_stride:
6156           attrs->at_byte_stride = a;
6157           break;
6158         case DW_AT_const_value:
6159           attrs->at_const_value = a;
6160           break;
6161         case DW_AT_containing_type:
6162           attrs->at_containing_type = a;
6163           break;
6164         case DW_AT_count:
6165           attrs->at_count = a;
6166           break;
6167         case DW_AT_data_location:
6168           attrs->at_data_location = a;
6169           break;
6170         case DW_AT_data_member_location:
6171           attrs->at_data_member_location = a;
6172           break;
6173         case DW_AT_decimal_scale:
6174           attrs->at_decimal_scale = a;
6175           break;
6176         case DW_AT_decimal_sign:
6177           attrs->at_decimal_sign = a;
6178           break;
6179         case DW_AT_default_value:
6180           attrs->at_default_value = a;
6181           break;
6182         case DW_AT_digit_count:
6183           attrs->at_digit_count = a;
6184           break;
6185         case DW_AT_discr:
6186           attrs->at_discr = a;
6187           break;
6188         case DW_AT_discr_list:
6189           attrs->at_discr_list = a;
6190           break;
6191         case DW_AT_discr_value:
6192           attrs->at_discr_value = a;
6193           break;
6194         case DW_AT_encoding:
6195           attrs->at_encoding = a;
6196           break;
6197         case DW_AT_endianity:
6198           attrs->at_endianity = a;
6199           break;
6200         case DW_AT_explicit:
6201           attrs->at_explicit = a;
6202           break;
6203         case DW_AT_is_optional:
6204           attrs->at_is_optional = a;
6205           break;
6206         case DW_AT_location:
6207           attrs->at_location = a;
6208           break;
6209         case DW_AT_lower_bound:
6210           attrs->at_lower_bound = a;
6211           break;
6212         case DW_AT_mutable:
6213           attrs->at_mutable = a;
6214           break;
6215         case DW_AT_ordering:
6216           attrs->at_ordering = a;
6217           break;
6218         case DW_AT_picture_string:
6219           attrs->at_picture_string = a;
6220           break;
6221         case DW_AT_prototyped:
6222           attrs->at_prototyped = a;
6223           break;
6224         case DW_AT_small:
6225           attrs->at_small = a;
6226           break;
6227         case DW_AT_segment:
6228           attrs->at_segment = a;
6229           break;
6230         case DW_AT_string_length:
6231           attrs->at_string_length = a;
6232           break;
6233         case DW_AT_threads_scaled:
6234           attrs->at_threads_scaled = a;
6235           break;
6236         case DW_AT_upper_bound:
6237           attrs->at_upper_bound = a;
6238           break;
6239         case DW_AT_use_location:
6240           attrs->at_use_location = a;
6241           break;
6242         case DW_AT_use_UTF8:
6243           attrs->at_use_UTF8 = a;
6244           break;
6245         case DW_AT_variable_parameter:
6246           attrs->at_variable_parameter = a;
6247           break;
6248         case DW_AT_virtuality:
6249           attrs->at_virtuality = a;
6250           break;
6251         case DW_AT_visibility:
6252           attrs->at_visibility = a;
6253           break;
6254         case DW_AT_vtable_elem_location:
6255           attrs->at_vtable_elem_location = a;
6256           break;
6257         default:
6258           break;
6259         }
6260     }
6261 }
6262
6263 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6264
6265 static void
6266 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6267 {
6268   dw_die_ref c;
6269   dw_die_ref decl;
6270   struct checksum_attributes attrs;
6271
6272   CHECKSUM_ULEB128 ('D');
6273   CHECKSUM_ULEB128 (die->die_tag);
6274
6275   memset (&attrs, 0, sizeof (attrs));
6276
6277   decl = get_AT_ref (die, DW_AT_specification);
6278   if (decl != NULL)
6279     collect_checksum_attributes (&attrs, decl);
6280   collect_checksum_attributes (&attrs, die);
6281
6282   CHECKSUM_ATTR (attrs.at_name);
6283   CHECKSUM_ATTR (attrs.at_accessibility);
6284   CHECKSUM_ATTR (attrs.at_address_class);
6285   CHECKSUM_ATTR (attrs.at_allocated);
6286   CHECKSUM_ATTR (attrs.at_artificial);
6287   CHECKSUM_ATTR (attrs.at_associated);
6288   CHECKSUM_ATTR (attrs.at_binary_scale);
6289   CHECKSUM_ATTR (attrs.at_bit_offset);
6290   CHECKSUM_ATTR (attrs.at_bit_size);
6291   CHECKSUM_ATTR (attrs.at_bit_stride);
6292   CHECKSUM_ATTR (attrs.at_byte_size);
6293   CHECKSUM_ATTR (attrs.at_byte_stride);
6294   CHECKSUM_ATTR (attrs.at_const_value);
6295   CHECKSUM_ATTR (attrs.at_containing_type);
6296   CHECKSUM_ATTR (attrs.at_count);
6297   CHECKSUM_ATTR (attrs.at_data_location);
6298   CHECKSUM_ATTR (attrs.at_data_member_location);
6299   CHECKSUM_ATTR (attrs.at_decimal_scale);
6300   CHECKSUM_ATTR (attrs.at_decimal_sign);
6301   CHECKSUM_ATTR (attrs.at_default_value);
6302   CHECKSUM_ATTR (attrs.at_digit_count);
6303   CHECKSUM_ATTR (attrs.at_discr);
6304   CHECKSUM_ATTR (attrs.at_discr_list);
6305   CHECKSUM_ATTR (attrs.at_discr_value);
6306   CHECKSUM_ATTR (attrs.at_encoding);
6307   CHECKSUM_ATTR (attrs.at_endianity);
6308   CHECKSUM_ATTR (attrs.at_explicit);
6309   CHECKSUM_ATTR (attrs.at_is_optional);
6310   CHECKSUM_ATTR (attrs.at_location);
6311   CHECKSUM_ATTR (attrs.at_lower_bound);
6312   CHECKSUM_ATTR (attrs.at_mutable);
6313   CHECKSUM_ATTR (attrs.at_ordering);
6314   CHECKSUM_ATTR (attrs.at_picture_string);
6315   CHECKSUM_ATTR (attrs.at_prototyped);
6316   CHECKSUM_ATTR (attrs.at_small);
6317   CHECKSUM_ATTR (attrs.at_segment);
6318   CHECKSUM_ATTR (attrs.at_string_length);
6319   CHECKSUM_ATTR (attrs.at_threads_scaled);
6320   CHECKSUM_ATTR (attrs.at_upper_bound);
6321   CHECKSUM_ATTR (attrs.at_use_location);
6322   CHECKSUM_ATTR (attrs.at_use_UTF8);
6323   CHECKSUM_ATTR (attrs.at_variable_parameter);
6324   CHECKSUM_ATTR (attrs.at_virtuality);
6325   CHECKSUM_ATTR (attrs.at_visibility);
6326   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6327   CHECKSUM_ATTR (attrs.at_type);
6328   CHECKSUM_ATTR (attrs.at_friend);
6329
6330   /* Checksum the child DIEs, except for nested types and member functions.  */
6331   c = die->die_child;
6332   if (c) do {
6333     dw_attr_ref name_attr;
6334
6335     c = c->die_sib;
6336     name_attr = get_AT (c, DW_AT_name);
6337     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6338         && name_attr != NULL)
6339       {
6340         CHECKSUM_ULEB128 ('S');
6341         CHECKSUM_ULEB128 (c->die_tag);
6342         CHECKSUM_STRING (AT_string (name_attr));
6343       }
6344     else
6345       {
6346         /* Mark this DIE so it gets processed when unmarking.  */
6347         if (c->die_mark == 0)
6348           c->die_mark = -1;
6349         die_checksum_ordered (c, ctx, mark);
6350       }
6351   } while (c != die->die_child);
6352
6353   CHECKSUM_ULEB128 (0);
6354 }
6355
6356 #undef CHECKSUM
6357 #undef CHECKSUM_STRING
6358 #undef CHECKSUM_ATTR
6359 #undef CHECKSUM_LEB128
6360 #undef CHECKSUM_ULEB128
6361
6362 /* Generate the type signature for DIE.  This is computed by generating an
6363    MD5 checksum over the DIE's tag, its relevant attributes, and its
6364    children.  Attributes that are references to other DIEs are processed
6365    by recursion, using the MARK field to prevent infinite recursion.
6366    If the DIE is nested inside a namespace or another type, we also
6367    need to include that context in the signature.  The lower 64 bits
6368    of the resulting MD5 checksum comprise the signature.  */
6369
6370 static void
6371 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6372 {
6373   int mark;
6374   const char *name;
6375   unsigned char checksum[16];
6376   struct md5_ctx ctx;
6377   dw_die_ref decl;
6378
6379   name = get_AT_string (die, DW_AT_name);
6380   decl = get_AT_ref (die, DW_AT_specification);
6381
6382   /* First, compute a signature for just the type name (and its surrounding
6383      context, if any.  This is stored in the type unit DIE for link-time
6384      ODR (one-definition rule) checking.  */
6385
6386   if (is_cxx() && name != NULL)
6387     {
6388       md5_init_ctx (&ctx);
6389
6390       /* Checksum the names of surrounding namespaces and structures.  */
6391       if (decl != NULL && decl->die_parent != NULL)
6392         checksum_die_context (decl->die_parent, &ctx);
6393
6394       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6395       md5_process_bytes (name, strlen (name) + 1, &ctx);
6396       md5_finish_ctx (&ctx, checksum);
6397
6398       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6399     }
6400
6401   /* Next, compute the complete type signature.  */
6402
6403   md5_init_ctx (&ctx);
6404   mark = 1;
6405   die->die_mark = mark;
6406
6407   /* Checksum the names of surrounding namespaces and structures.  */
6408   if (decl != NULL && decl->die_parent != NULL)
6409     checksum_die_context (decl->die_parent, &ctx);
6410
6411   /* Checksum the DIE and its children.  */
6412   die_checksum_ordered (die, &ctx, &mark);
6413   unmark_all_dies (die);
6414   md5_finish_ctx (&ctx, checksum);
6415
6416   /* Store the signature in the type node and link the type DIE and the
6417      type node together.  */
6418   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6419           DWARF_TYPE_SIGNATURE_SIZE);
6420   die->die_id.die_type_node = type_node;
6421   type_node->type_die = die;
6422
6423   /* If the DIE is a specification, link its declaration to the type node
6424      as well.  */
6425   if (decl != NULL)
6426     decl->die_id.die_type_node = type_node;
6427 }
6428
6429 /* Do the location expressions look same?  */
6430 static inline int
6431 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6432 {
6433   return loc1->dw_loc_opc == loc2->dw_loc_opc
6434          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6435          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6436 }
6437
6438 /* Do the values look the same?  */
6439 static int
6440 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6441 {
6442   dw_loc_descr_ref loc1, loc2;
6443   rtx r1, r2;
6444
6445   if (v1->val_class != v2->val_class)
6446     return 0;
6447
6448   switch (v1->val_class)
6449     {
6450     case dw_val_class_const:
6451       return v1->v.val_int == v2->v.val_int;
6452     case dw_val_class_unsigned_const:
6453       return v1->v.val_unsigned == v2->v.val_unsigned;
6454     case dw_val_class_const_double:
6455       return v1->v.val_double.high == v2->v.val_double.high
6456              && v1->v.val_double.low == v2->v.val_double.low;
6457     case dw_val_class_vec:
6458       if (v1->v.val_vec.length != v2->v.val_vec.length
6459           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6460         return 0;
6461       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6462                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6463         return 0;
6464       return 1;
6465     case dw_val_class_flag:
6466       return v1->v.val_flag == v2->v.val_flag;
6467     case dw_val_class_str:
6468       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6469
6470     case dw_val_class_addr:
6471       r1 = v1->v.val_addr;
6472       r2 = v2->v.val_addr;
6473       if (GET_CODE (r1) != GET_CODE (r2))
6474         return 0;
6475       return !rtx_equal_p (r1, r2);
6476
6477     case dw_val_class_offset:
6478       return v1->v.val_offset == v2->v.val_offset;
6479
6480     case dw_val_class_loc:
6481       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6482            loc1 && loc2;
6483            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6484         if (!same_loc_p (loc1, loc2, mark))
6485           return 0;
6486       return !loc1 && !loc2;
6487
6488     case dw_val_class_die_ref:
6489       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6490
6491     case dw_val_class_fde_ref:
6492     case dw_val_class_vms_delta:
6493     case dw_val_class_lbl_id:
6494     case dw_val_class_lineptr:
6495     case dw_val_class_macptr:
6496       return 1;
6497
6498     case dw_val_class_file:
6499       return v1->v.val_file == v2->v.val_file;
6500
6501     case dw_val_class_data8:
6502       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6503
6504     default:
6505       return 1;
6506     }
6507 }
6508
6509 /* Do the attributes look the same?  */
6510
6511 static int
6512 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6513 {
6514   if (at1->dw_attr != at2->dw_attr)
6515     return 0;
6516
6517   /* We don't care that this was compiled with a different compiler
6518      snapshot; if the output is the same, that's what matters. */
6519   if (at1->dw_attr == DW_AT_producer)
6520     return 1;
6521
6522   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6523 }
6524
6525 /* Do the dies look the same?  */
6526
6527 static int
6528 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6529 {
6530   dw_die_ref c1, c2;
6531   dw_attr_ref a1;
6532   unsigned ix;
6533
6534   /* To avoid infinite recursion.  */
6535   if (die1->die_mark)
6536     return die1->die_mark == die2->die_mark;
6537   die1->die_mark = die2->die_mark = ++(*mark);
6538
6539   if (die1->die_tag != die2->die_tag)
6540     return 0;
6541
6542   if (VEC_length (dw_attr_node, die1->die_attr)
6543       != VEC_length (dw_attr_node, die2->die_attr))
6544     return 0;
6545
6546   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6547     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6548       return 0;
6549
6550   c1 = die1->die_child;
6551   c2 = die2->die_child;
6552   if (! c1)
6553     {
6554       if (c2)
6555         return 0;
6556     }
6557   else
6558     for (;;)
6559       {
6560         if (!same_die_p (c1, c2, mark))
6561           return 0;
6562         c1 = c1->die_sib;
6563         c2 = c2->die_sib;
6564         if (c1 == die1->die_child)
6565           {
6566             if (c2 == die2->die_child)
6567               break;
6568             else
6569               return 0;
6570           }
6571     }
6572
6573   return 1;
6574 }
6575
6576 /* Do the dies look the same?  Wrapper around same_die_p.  */
6577
6578 static int
6579 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6580 {
6581   int mark = 0;
6582   int ret = same_die_p (die1, die2, &mark);
6583
6584   unmark_all_dies (die1);
6585   unmark_all_dies (die2);
6586
6587   return ret;
6588 }
6589
6590 /* The prefix to attach to symbols on DIEs in the current comdat debug
6591    info section.  */
6592 static char *comdat_symbol_id;
6593
6594 /* The index of the current symbol within the current comdat CU.  */
6595 static unsigned int comdat_symbol_number;
6596
6597 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6598    children, and set comdat_symbol_id accordingly.  */
6599
6600 static void
6601 compute_section_prefix (dw_die_ref unit_die)
6602 {
6603   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6604   const char *base = die_name ? lbasename (die_name) : "anonymous";
6605   char *name = XALLOCAVEC (char, strlen (base) + 64);
6606   char *p;
6607   int i, mark;
6608   unsigned char checksum[16];
6609   struct md5_ctx ctx;
6610
6611   /* Compute the checksum of the DIE, then append part of it as hex digits to
6612      the name filename of the unit.  */
6613
6614   md5_init_ctx (&ctx);
6615   mark = 0;
6616   die_checksum (unit_die, &ctx, &mark);
6617   unmark_all_dies (unit_die);
6618   md5_finish_ctx (&ctx, checksum);
6619
6620   sprintf (name, "%s.", base);
6621   clean_symbol_name (name);
6622
6623   p = name + strlen (name);
6624   for (i = 0; i < 4; i++)
6625     {
6626       sprintf (p, "%.2x", checksum[i]);
6627       p += 2;
6628     }
6629
6630   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6631   comdat_symbol_number = 0;
6632 }
6633
6634 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6635
6636 static int
6637 is_type_die (dw_die_ref die)
6638 {
6639   switch (die->die_tag)
6640     {
6641     case DW_TAG_array_type:
6642     case DW_TAG_class_type:
6643     case DW_TAG_interface_type:
6644     case DW_TAG_enumeration_type:
6645     case DW_TAG_pointer_type:
6646     case DW_TAG_reference_type:
6647     case DW_TAG_rvalue_reference_type:
6648     case DW_TAG_string_type:
6649     case DW_TAG_structure_type:
6650     case DW_TAG_subroutine_type:
6651     case DW_TAG_union_type:
6652     case DW_TAG_ptr_to_member_type:
6653     case DW_TAG_set_type:
6654     case DW_TAG_subrange_type:
6655     case DW_TAG_base_type:
6656     case DW_TAG_const_type:
6657     case DW_TAG_file_type:
6658     case DW_TAG_packed_type:
6659     case DW_TAG_volatile_type:
6660     case DW_TAG_typedef:
6661       return 1;
6662     default:
6663       return 0;
6664     }
6665 }
6666
6667 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6668    Basically, we want to choose the bits that are likely to be shared between
6669    compilations (types) and leave out the bits that are specific to individual
6670    compilations (functions).  */
6671
6672 static int
6673 is_comdat_die (dw_die_ref c)
6674 {
6675   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6676      we do for stabs.  The advantage is a greater likelihood of sharing between
6677      objects that don't include headers in the same order (and therefore would
6678      put the base types in a different comdat).  jason 8/28/00 */
6679
6680   if (c->die_tag == DW_TAG_base_type)
6681     return 0;
6682
6683   if (c->die_tag == DW_TAG_pointer_type
6684       || c->die_tag == DW_TAG_reference_type
6685       || c->die_tag == DW_TAG_rvalue_reference_type
6686       || c->die_tag == DW_TAG_const_type
6687       || c->die_tag == DW_TAG_volatile_type)
6688     {
6689       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6690
6691       return t ? is_comdat_die (t) : 0;
6692     }
6693
6694   return is_type_die (c);
6695 }
6696
6697 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6698    compilation unit.  */
6699
6700 static int
6701 is_symbol_die (dw_die_ref c)
6702 {
6703   return (is_type_die (c)
6704           || is_declaration_die (c)
6705           || c->die_tag == DW_TAG_namespace
6706           || c->die_tag == DW_TAG_module);
6707 }
6708
6709 /* Returns true iff C is a compile-unit DIE.  */
6710
6711 static inline bool
6712 is_cu_die (dw_die_ref c)
6713 {
6714   return c && c->die_tag == DW_TAG_compile_unit;
6715 }
6716
6717 static char *
6718 gen_internal_sym (const char *prefix)
6719 {
6720   char buf[256];
6721
6722   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6723   return xstrdup (buf);
6724 }
6725
6726 /* Assign symbols to all worthy DIEs under DIE.  */
6727
6728 static void
6729 assign_symbol_names (dw_die_ref die)
6730 {
6731   dw_die_ref c;
6732
6733   if (is_symbol_die (die))
6734     {
6735       if (comdat_symbol_id)
6736         {
6737           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6738
6739           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6740                    comdat_symbol_id, comdat_symbol_number++);
6741           die->die_id.die_symbol = xstrdup (p);
6742         }
6743       else
6744         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6745     }
6746
6747   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6748 }
6749
6750 struct cu_hash_table_entry
6751 {
6752   dw_die_ref cu;
6753   unsigned min_comdat_num, max_comdat_num;
6754   struct cu_hash_table_entry *next;
6755 };
6756
6757 /* Routines to manipulate hash table of CUs.  */
6758 static hashval_t
6759 htab_cu_hash (const void *of)
6760 {
6761   const struct cu_hash_table_entry *const entry =
6762     (const struct cu_hash_table_entry *) of;
6763
6764   return htab_hash_string (entry->cu->die_id.die_symbol);
6765 }
6766
6767 static int
6768 htab_cu_eq (const void *of1, const void *of2)
6769 {
6770   const struct cu_hash_table_entry *const entry1 =
6771     (const struct cu_hash_table_entry *) of1;
6772   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6773
6774   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6775 }
6776
6777 static void
6778 htab_cu_del (void *what)
6779 {
6780   struct cu_hash_table_entry *next,
6781     *entry = (struct cu_hash_table_entry *) what;
6782
6783   while (entry)
6784     {
6785       next = entry->next;
6786       free (entry);
6787       entry = next;
6788     }
6789 }
6790
6791 /* Check whether we have already seen this CU and set up SYM_NUM
6792    accordingly.  */
6793 static int
6794 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6795 {
6796   struct cu_hash_table_entry dummy;
6797   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6798
6799   dummy.max_comdat_num = 0;
6800
6801   slot = (struct cu_hash_table_entry **)
6802     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6803         INSERT);
6804   entry = *slot;
6805
6806   for (; entry; last = entry, entry = entry->next)
6807     {
6808       if (same_die_p_wrap (cu, entry->cu))
6809         break;
6810     }
6811
6812   if (entry)
6813     {
6814       *sym_num = entry->min_comdat_num;
6815       return 1;
6816     }
6817
6818   entry = XCNEW (struct cu_hash_table_entry);
6819   entry->cu = cu;
6820   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6821   entry->next = *slot;
6822   *slot = entry;
6823
6824   return 0;
6825 }
6826
6827 /* Record SYM_NUM to record of CU in HTABLE.  */
6828 static void
6829 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6830 {
6831   struct cu_hash_table_entry **slot, *entry;
6832
6833   slot = (struct cu_hash_table_entry **)
6834     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6835         NO_INSERT);
6836   entry = *slot;
6837
6838   entry->max_comdat_num = sym_num;
6839 }
6840
6841 /* Traverse the DIE (which is always comp_unit_die), and set up
6842    additional compilation units for each of the include files we see
6843    bracketed by BINCL/EINCL.  */
6844
6845 static void
6846 break_out_includes (dw_die_ref die)
6847 {
6848   dw_die_ref c;
6849   dw_die_ref unit = NULL;
6850   limbo_die_node *node, **pnode;
6851   htab_t cu_hash_table;
6852
6853   c = die->die_child;
6854   if (c) do {
6855     dw_die_ref prev = c;
6856     c = c->die_sib;
6857     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6858            || (unit && is_comdat_die (c)))
6859       {
6860         dw_die_ref next = c->die_sib;
6861
6862         /* This DIE is for a secondary CU; remove it from the main one.  */
6863         remove_child_with_prev (c, prev);
6864
6865         if (c->die_tag == DW_TAG_GNU_BINCL)
6866           unit = push_new_compile_unit (unit, c);
6867         else if (c->die_tag == DW_TAG_GNU_EINCL)
6868           unit = pop_compile_unit (unit);
6869         else
6870           add_child_die (unit, c);
6871         c = next;
6872         if (c == die->die_child)
6873           break;
6874       }
6875   } while (c != die->die_child);
6876
6877 #if 0
6878   /* We can only use this in debugging, since the frontend doesn't check
6879      to make sure that we leave every include file we enter.  */
6880   gcc_assert (!unit);
6881 #endif
6882
6883   assign_symbol_names (die);
6884   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6885   for (node = limbo_die_list, pnode = &limbo_die_list;
6886        node;
6887        node = node->next)
6888     {
6889       int is_dupl;
6890
6891       compute_section_prefix (node->die);
6892       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6893                         &comdat_symbol_number);
6894       assign_symbol_names (node->die);
6895       if (is_dupl)
6896         *pnode = node->next;
6897       else
6898         {
6899           pnode = &node->next;
6900           record_comdat_symbol_number (node->die, cu_hash_table,
6901                 comdat_symbol_number);
6902         }
6903     }
6904   htab_delete (cu_hash_table);
6905 }
6906
6907 /* Return non-zero if this DIE is a declaration.  */
6908
6909 static int
6910 is_declaration_die (dw_die_ref die)
6911 {
6912   dw_attr_ref a;
6913   unsigned ix;
6914
6915   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6916     if (a->dw_attr == DW_AT_declaration)
6917       return 1;
6918
6919   return 0;
6920 }
6921
6922 /* Return non-zero if this DIE is nested inside a subprogram.  */
6923
6924 static int
6925 is_nested_in_subprogram (dw_die_ref die)
6926 {
6927   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6928
6929   if (decl == NULL)
6930     decl = die;
6931   return local_scope_p (decl);
6932 }
6933
6934 /* Return non-zero if this DIE contains a defining declaration of a
6935    subprogram.  */
6936
6937 static int
6938 contains_subprogram_definition (dw_die_ref die)
6939 {
6940   dw_die_ref c;
6941
6942   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6943     return 1;
6944   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6945   return 0;
6946 }
6947
6948 /* Return non-zero if this is a type DIE that should be moved to a
6949    COMDAT .debug_types section.  */
6950
6951 static int
6952 should_move_die_to_comdat (dw_die_ref die)
6953 {
6954   switch (die->die_tag)
6955     {
6956     case DW_TAG_class_type:
6957     case DW_TAG_structure_type:
6958     case DW_TAG_enumeration_type:
6959     case DW_TAG_union_type:
6960       /* Don't move declarations, inlined instances, or types nested in a
6961          subprogram.  */
6962       if (is_declaration_die (die)
6963           || get_AT (die, DW_AT_abstract_origin)
6964           || is_nested_in_subprogram (die))
6965         return 0;
6966       /* A type definition should never contain a subprogram definition.  */
6967       gcc_assert (!contains_subprogram_definition (die));
6968       return 1;
6969     case DW_TAG_array_type:
6970     case DW_TAG_interface_type:
6971     case DW_TAG_pointer_type:
6972     case DW_TAG_reference_type:
6973     case DW_TAG_rvalue_reference_type:
6974     case DW_TAG_string_type:
6975     case DW_TAG_subroutine_type:
6976     case DW_TAG_ptr_to_member_type:
6977     case DW_TAG_set_type:
6978     case DW_TAG_subrange_type:
6979     case DW_TAG_base_type:
6980     case DW_TAG_const_type:
6981     case DW_TAG_file_type:
6982     case DW_TAG_packed_type:
6983     case DW_TAG_volatile_type:
6984     case DW_TAG_typedef:
6985     default:
6986       return 0;
6987     }
6988 }
6989
6990 /* Make a clone of DIE.  */
6991
6992 static dw_die_ref
6993 clone_die (dw_die_ref die)
6994 {
6995   dw_die_ref clone;
6996   dw_attr_ref a;
6997   unsigned ix;
6998
6999   clone = ggc_alloc_cleared_die_node ();
7000   clone->die_tag = die->die_tag;
7001
7002   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7003     add_dwarf_attr (clone, a);
7004
7005   return clone;
7006 }
7007
7008 /* Make a clone of the tree rooted at DIE.  */
7009
7010 static dw_die_ref
7011 clone_tree (dw_die_ref die)
7012 {
7013   dw_die_ref c;
7014   dw_die_ref clone = clone_die (die);
7015
7016   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7017
7018   return clone;
7019 }
7020
7021 /* Make a clone of DIE as a declaration.  */
7022
7023 static dw_die_ref
7024 clone_as_declaration (dw_die_ref die)
7025 {
7026   dw_die_ref clone;
7027   dw_die_ref decl;
7028   dw_attr_ref a;
7029   unsigned ix;
7030
7031   /* If the DIE is already a declaration, just clone it.  */
7032   if (is_declaration_die (die))
7033     return clone_die (die);
7034
7035   /* If the DIE is a specification, just clone its declaration DIE.  */
7036   decl = get_AT_ref (die, DW_AT_specification);
7037   if (decl != NULL)
7038     return clone_die (decl);
7039
7040   clone = ggc_alloc_cleared_die_node ();
7041   clone->die_tag = die->die_tag;
7042
7043   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7044     {
7045       /* We don't want to copy over all attributes.
7046          For example we don't want DW_AT_byte_size because otherwise we will no
7047          longer have a declaration and GDB will treat it as a definition.  */
7048
7049       switch (a->dw_attr)
7050         {
7051         case DW_AT_artificial:
7052         case DW_AT_containing_type:
7053         case DW_AT_external:
7054         case DW_AT_name:
7055         case DW_AT_type:
7056         case DW_AT_virtuality:
7057         case DW_AT_linkage_name:
7058         case DW_AT_MIPS_linkage_name:
7059           add_dwarf_attr (clone, a);
7060           break;
7061         case DW_AT_byte_size:
7062         default:
7063           break;
7064         }
7065     }
7066
7067   if (die->die_id.die_type_node)
7068     add_AT_die_ref (clone, DW_AT_signature, die);
7069
7070   add_AT_flag (clone, DW_AT_declaration, 1);
7071   return clone;
7072 }
7073
7074 /* Copy the declaration context to the new type unit DIE.  This includes
7075    any surrounding namespace or type declarations.  If the DIE has an
7076    AT_specification attribute, it also includes attributes and children
7077    attached to the specification, and returns a pointer to the original
7078    parent of the declaration DIE.  Returns NULL otherwise.  */
7079
7080 static dw_die_ref
7081 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7082 {
7083   dw_die_ref decl;
7084   dw_die_ref new_decl;
7085   dw_die_ref orig_parent = NULL;
7086
7087   decl = get_AT_ref (die, DW_AT_specification);
7088   if (decl == NULL)
7089     decl = die;
7090   else
7091     {
7092       unsigned ix;
7093       dw_die_ref c;
7094       dw_attr_ref a;
7095
7096       /* The original DIE will be changed to a declaration, and must
7097          be moved to be a child of the original declaration DIE.  */
7098       orig_parent = decl->die_parent;
7099
7100       /* Copy the type node pointer from the new DIE to the original
7101          declaration DIE so we can forward references later.  */
7102       decl->die_id.die_type_node = die->die_id.die_type_node;
7103
7104       remove_AT (die, DW_AT_specification);
7105
7106       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7107         {
7108           if (a->dw_attr != DW_AT_name
7109               && a->dw_attr != DW_AT_declaration
7110               && a->dw_attr != DW_AT_external)
7111             add_dwarf_attr (die, a);
7112         }
7113
7114       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7115     }
7116
7117   if (decl->die_parent != NULL
7118       && decl->die_parent->die_tag != DW_TAG_compile_unit
7119       && decl->die_parent->die_tag != DW_TAG_type_unit)
7120     {
7121       new_decl = copy_ancestor_tree (unit, decl, NULL);
7122       if (new_decl != NULL)
7123         {
7124           remove_AT (new_decl, DW_AT_signature);
7125           add_AT_specification (die, new_decl);
7126         }
7127     }
7128
7129   return orig_parent;
7130 }
7131
7132 /* Generate the skeleton ancestor tree for the given NODE, then clone
7133    the DIE and add the clone into the tree.  */
7134
7135 static void
7136 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7137 {
7138   if (node->new_die != NULL)
7139     return;
7140
7141   node->new_die = clone_as_declaration (node->old_die);
7142
7143   if (node->parent != NULL)
7144     {
7145       generate_skeleton_ancestor_tree (node->parent);
7146       add_child_die (node->parent->new_die, node->new_die);
7147     }
7148 }
7149
7150 /* Generate a skeleton tree of DIEs containing any declarations that are
7151    found in the original tree.  We traverse the tree looking for declaration
7152    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7153
7154 static void
7155 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7156 {
7157   skeleton_chain_node node;
7158   dw_die_ref c;
7159   dw_die_ref first;
7160   dw_die_ref prev = NULL;
7161   dw_die_ref next = NULL;
7162
7163   node.parent = parent;
7164
7165   first = c = parent->old_die->die_child;
7166   if (c)
7167     next = c->die_sib;
7168   if (c) do {
7169     if (prev == NULL || prev->die_sib == c)
7170       prev = c;
7171     c = next;
7172     next = (c == first ? NULL : c->die_sib);
7173     node.old_die = c;
7174     node.new_die = NULL;
7175     if (is_declaration_die (c))
7176       {
7177         /* Clone the existing DIE, move the original to the skeleton
7178            tree (which is in the main CU), and put the clone, with
7179            all the original's children, where the original came from.  */
7180         dw_die_ref clone = clone_die (c);
7181         move_all_children (c, clone);
7182
7183         replace_child (c, clone, prev);
7184         generate_skeleton_ancestor_tree (parent);
7185         add_child_die (parent->new_die, c);
7186         node.new_die = c;
7187         c = clone;
7188       }
7189     generate_skeleton_bottom_up (&node);
7190   } while (next != NULL);
7191 }
7192
7193 /* Wrapper function for generate_skeleton_bottom_up.  */
7194
7195 static dw_die_ref
7196 generate_skeleton (dw_die_ref die)
7197 {
7198   skeleton_chain_node node;
7199
7200   node.old_die = die;
7201   node.new_die = NULL;
7202   node.parent = NULL;
7203
7204   /* If this type definition is nested inside another type,
7205      always leave at least a declaration in its place.  */
7206   if (die->die_parent != NULL && is_type_die (die->die_parent))
7207     node.new_die = clone_as_declaration (die);
7208
7209   generate_skeleton_bottom_up (&node);
7210   return node.new_die;
7211 }
7212
7213 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7214    declaration.  The original DIE is moved to a new compile unit so that
7215    existing references to it follow it to the new location.  If any of the
7216    original DIE's descendants is a declaration, we need to replace the
7217    original DIE with a skeleton tree and move the declarations back into the
7218    skeleton tree.  */
7219
7220 static dw_die_ref
7221 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7222                                        dw_die_ref prev)
7223 {
7224   dw_die_ref skeleton, orig_parent;
7225
7226   /* Copy the declaration context to the type unit DIE.  If the returned
7227      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7228      that DIE.  */
7229   orig_parent = copy_declaration_context (unit, child);
7230
7231   skeleton = generate_skeleton (child);
7232   if (skeleton == NULL)
7233     remove_child_with_prev (child, prev);
7234   else
7235     {
7236       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7237
7238       /* If the original DIE was a specification, we need to put
7239          the skeleton under the parent DIE of the declaration.
7240          This leaves the original declaration in the tree, but
7241          it will be pruned later since there are no longer any
7242          references to it.  */
7243       if (orig_parent != NULL)
7244         {
7245           remove_child_with_prev (child, prev);
7246           add_child_die (orig_parent, skeleton);
7247         }
7248       else
7249         replace_child (child, skeleton, prev);
7250     }
7251
7252   return skeleton;
7253 }
7254
7255 /* Traverse the DIE and set up additional .debug_types sections for each
7256    type worthy of being placed in a COMDAT section.  */
7257
7258 static void
7259 break_out_comdat_types (dw_die_ref die)
7260 {
7261   dw_die_ref c;
7262   dw_die_ref first;
7263   dw_die_ref prev = NULL;
7264   dw_die_ref next = NULL;
7265   dw_die_ref unit = NULL;
7266
7267   first = c = die->die_child;
7268   if (c)
7269     next = c->die_sib;
7270   if (c) do {
7271     if (prev == NULL || prev->die_sib == c)
7272       prev = c;
7273     c = next;
7274     next = (c == first ? NULL : c->die_sib);
7275     if (should_move_die_to_comdat (c))
7276       {
7277         dw_die_ref replacement;
7278         comdat_type_node_ref type_node;
7279
7280         /* Create a new type unit DIE as the root for the new tree, and
7281            add it to the list of comdat types.  */
7282         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7283         add_AT_unsigned (unit, DW_AT_language,
7284                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7285         type_node = ggc_alloc_cleared_comdat_type_node ();
7286         type_node->root_die = unit;
7287         type_node->next = comdat_type_list;
7288         comdat_type_list = type_node;
7289
7290         /* Generate the type signature.  */
7291         generate_type_signature (c, type_node);
7292
7293         /* Copy the declaration context, attributes, and children of the
7294            declaration into the new type unit DIE, then remove this DIE
7295            from the main CU (or replace it with a skeleton if necessary).  */
7296         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7297
7298         /* Break out nested types into their own type units.  */
7299         break_out_comdat_types (c);
7300
7301         /* Add the DIE to the new compunit.  */
7302         add_child_die (unit, c);
7303
7304         if (replacement != NULL)
7305           c = replacement;
7306       }
7307     else if (c->die_tag == DW_TAG_namespace
7308              || c->die_tag == DW_TAG_class_type
7309              || c->die_tag == DW_TAG_structure_type
7310              || c->die_tag == DW_TAG_union_type)
7311       {
7312         /* Look for nested types that can be broken out.  */
7313         break_out_comdat_types (c);
7314       }
7315   } while (next != NULL);
7316 }
7317
7318 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7319
7320 struct decl_table_entry
7321 {
7322   dw_die_ref orig;
7323   dw_die_ref copy;
7324 };
7325
7326 /* Routines to manipulate hash table of copied declarations.  */
7327
7328 static hashval_t
7329 htab_decl_hash (const void *of)
7330 {
7331   const struct decl_table_entry *const entry =
7332     (const struct decl_table_entry *) of;
7333
7334   return htab_hash_pointer (entry->orig);
7335 }
7336
7337 static int
7338 htab_decl_eq (const void *of1, const void *of2)
7339 {
7340   const struct decl_table_entry *const entry1 =
7341     (const struct decl_table_entry *) of1;
7342   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7343
7344   return entry1->orig == entry2;
7345 }
7346
7347 static void
7348 htab_decl_del (void *what)
7349 {
7350   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7351
7352   free (entry);
7353 }
7354
7355 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7356    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7357    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7358    to check if the ancestor has already been copied into UNIT.  */
7359
7360 static dw_die_ref
7361 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7362 {
7363   dw_die_ref parent = die->die_parent;
7364   dw_die_ref new_parent = unit;
7365   dw_die_ref copy;
7366   void **slot = NULL;
7367   struct decl_table_entry *entry = NULL;
7368
7369   if (decl_table)
7370     {
7371       /* Check if the entry has already been copied to UNIT.  */
7372       slot = htab_find_slot_with_hash (decl_table, die,
7373                                        htab_hash_pointer (die), INSERT);
7374       if (*slot != HTAB_EMPTY_ENTRY)
7375         {
7376           entry = (struct decl_table_entry *) *slot;
7377           return entry->copy;
7378         }
7379
7380       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7381       entry = XCNEW (struct decl_table_entry);
7382       entry->orig = die;
7383       entry->copy = NULL;
7384       *slot = entry;
7385     }
7386
7387   if (parent != NULL)
7388     {
7389       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7390       if (spec != NULL)
7391         parent = spec;
7392       if (parent->die_tag != DW_TAG_compile_unit
7393           && parent->die_tag != DW_TAG_type_unit)
7394         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7395     }
7396
7397   copy = clone_as_declaration (die);
7398   add_child_die (new_parent, copy);
7399
7400   if (decl_table != NULL)
7401     {
7402       /* Record the pointer to the copy.  */
7403       entry->copy = copy;
7404     }
7405
7406   return copy;
7407 }
7408
7409 /* Walk the DIE and its children, looking for references to incomplete
7410    or trivial types that are unmarked (i.e., that are not in the current
7411    type_unit).  */
7412
7413 static void
7414 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7415 {
7416   dw_die_ref c;
7417   dw_attr_ref a;
7418   unsigned ix;
7419
7420   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7421     {
7422       if (AT_class (a) == dw_val_class_die_ref)
7423         {
7424           dw_die_ref targ = AT_ref (a);
7425           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7426           void **slot;
7427           struct decl_table_entry *entry;
7428
7429           if (targ->die_mark != 0 || type_node != NULL)
7430             continue;
7431
7432           slot = htab_find_slot_with_hash (decl_table, targ,
7433                                            htab_hash_pointer (targ), INSERT);
7434
7435           if (*slot != HTAB_EMPTY_ENTRY)
7436             {
7437               /* TARG has already been copied, so we just need to
7438                  modify the reference to point to the copy.  */
7439               entry = (struct decl_table_entry *) *slot;
7440               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7441             }
7442           else
7443             {
7444               dw_die_ref parent = unit;
7445               dw_die_ref copy = clone_tree (targ);
7446
7447               /* Make sure the cloned tree is marked as part of the
7448                  type unit.  */
7449               mark_dies (copy);
7450
7451               /* Record in DECL_TABLE that TARG has been copied.
7452                  Need to do this now, before the recursive call,
7453                  because DECL_TABLE may be expanded and SLOT
7454                  would no longer be a valid pointer.  */
7455               entry = XCNEW (struct decl_table_entry);
7456               entry->orig = targ;
7457               entry->copy = copy;
7458               *slot = entry;
7459
7460               /* If TARG has surrounding context, copy its ancestor tree
7461                  into the new type unit.  */
7462               if (targ->die_parent != NULL
7463                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7464                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7465                 parent = copy_ancestor_tree (unit, targ->die_parent,
7466                                              decl_table);
7467
7468               add_child_die (parent, copy);
7469               a->dw_attr_val.v.val_die_ref.die = copy;
7470
7471               /* Make sure the newly-copied DIE is walked.  If it was
7472                  installed in a previously-added context, it won't
7473                  get visited otherwise.  */
7474               if (parent != unit)
7475                 {
7476                   /* Find the highest point of the newly-added tree,
7477                      mark each node along the way, and walk from there.  */
7478                   parent->die_mark = 1;
7479                   while (parent->die_parent
7480                          && parent->die_parent->die_mark == 0)
7481                     {
7482                       parent = parent->die_parent;
7483                       parent->die_mark = 1;
7484                     }
7485                   copy_decls_walk (unit, parent, decl_table);
7486                 }
7487             }
7488         }
7489     }
7490
7491   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7492 }
7493
7494 /* Copy declarations for "unworthy" types into the new comdat section.
7495    Incomplete types, modified types, and certain other types aren't broken
7496    out into comdat sections of their own, so they don't have a signature,
7497    and we need to copy the declaration into the same section so that we
7498    don't have an external reference.  */
7499
7500 static void
7501 copy_decls_for_unworthy_types (dw_die_ref unit)
7502 {
7503   htab_t decl_table;
7504
7505   mark_dies (unit);
7506   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7507   copy_decls_walk (unit, unit, decl_table);
7508   htab_delete (decl_table);
7509   unmark_dies (unit);
7510 }
7511
7512 /* Traverse the DIE and add a sibling attribute if it may have the
7513    effect of speeding up access to siblings.  To save some space,
7514    avoid generating sibling attributes for DIE's without children.  */
7515
7516 static void
7517 add_sibling_attributes (dw_die_ref die)
7518 {
7519   dw_die_ref c;
7520
7521   if (! die->die_child)
7522     return;
7523
7524   if (die->die_parent && die != die->die_parent->die_child)
7525     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7526
7527   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7528 }
7529
7530 /* Output all location lists for the DIE and its children.  */
7531
7532 static void
7533 output_location_lists (dw_die_ref die)
7534 {
7535   dw_die_ref c;
7536   dw_attr_ref a;
7537   unsigned ix;
7538
7539   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7540     if (AT_class (a) == dw_val_class_loc_list)
7541       output_loc_list (AT_loc_list (a));
7542
7543   FOR_EACH_CHILD (die, c, output_location_lists (c));
7544 }
7545
7546 /* The format of each DIE (and its attribute value pairs) is encoded in an
7547    abbreviation table.  This routine builds the abbreviation table and assigns
7548    a unique abbreviation id for each abbreviation entry.  The children of each
7549    die are visited recursively.  */
7550
7551 static void
7552 build_abbrev_table (dw_die_ref die)
7553 {
7554   unsigned long abbrev_id;
7555   unsigned int n_alloc;
7556   dw_die_ref c;
7557   dw_attr_ref a;
7558   unsigned ix;
7559
7560   /* Scan the DIE references, and mark as external any that refer to
7561      DIEs from other CUs (i.e. those which are not marked).  */
7562   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7563     if (AT_class (a) == dw_val_class_die_ref
7564         && AT_ref (a)->die_mark == 0)
7565       {
7566         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7567         set_AT_ref_external (a, 1);
7568       }
7569
7570   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7571     {
7572       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7573       dw_attr_ref die_a, abbrev_a;
7574       unsigned ix;
7575       bool ok = true;
7576
7577       if (abbrev->die_tag != die->die_tag)
7578         continue;
7579       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7580         continue;
7581
7582       if (VEC_length (dw_attr_node, abbrev->die_attr)
7583           != VEC_length (dw_attr_node, die->die_attr))
7584         continue;
7585
7586       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7587         {
7588           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7589           if ((abbrev_a->dw_attr != die_a->dw_attr)
7590               || (value_format (abbrev_a) != value_format (die_a)))
7591             {
7592               ok = false;
7593               break;
7594             }
7595         }
7596       if (ok)
7597         break;
7598     }
7599
7600   if (abbrev_id >= abbrev_die_table_in_use)
7601     {
7602       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7603         {
7604           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7605           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7606                                             n_alloc);
7607
7608           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7609                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7610           abbrev_die_table_allocated = n_alloc;
7611         }
7612
7613       ++abbrev_die_table_in_use;
7614       abbrev_die_table[abbrev_id] = die;
7615     }
7616
7617   die->die_abbrev = abbrev_id;
7618   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7619 }
7620 \f
7621 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7622
7623 static int
7624 constant_size (unsigned HOST_WIDE_INT value)
7625 {
7626   int log;
7627
7628   if (value == 0)
7629     log = 0;
7630   else
7631     log = floor_log2 (value);
7632
7633   log = log / 8;
7634   log = 1 << (floor_log2 (log) + 1);
7635
7636   return log;
7637 }
7638
7639 /* Return the size of a DIE as it is represented in the
7640    .debug_info section.  */
7641
7642 static unsigned long
7643 size_of_die (dw_die_ref die)
7644 {
7645   unsigned long size = 0;
7646   dw_attr_ref a;
7647   unsigned ix;
7648
7649   size += size_of_uleb128 (die->die_abbrev);
7650   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7651     {
7652       switch (AT_class (a))
7653         {
7654         case dw_val_class_addr:
7655           size += DWARF2_ADDR_SIZE;
7656           break;
7657         case dw_val_class_offset:
7658           size += DWARF_OFFSET_SIZE;
7659           break;
7660         case dw_val_class_loc:
7661           {
7662             unsigned long lsize = size_of_locs (AT_loc (a));
7663
7664             /* Block length.  */
7665             if (dwarf_version >= 4)
7666               size += size_of_uleb128 (lsize);
7667             else
7668               size += constant_size (lsize);
7669             size += lsize;
7670           }
7671           break;
7672         case dw_val_class_loc_list:
7673           size += DWARF_OFFSET_SIZE;
7674           break;
7675         case dw_val_class_range_list:
7676           size += DWARF_OFFSET_SIZE;
7677           break;
7678         case dw_val_class_const:
7679           size += size_of_sleb128 (AT_int (a));
7680           break;
7681         case dw_val_class_unsigned_const:
7682           {
7683             int csize = constant_size (AT_unsigned (a));
7684             if (dwarf_version == 3
7685                 && a->dw_attr == DW_AT_data_member_location
7686                 && csize >= 4)
7687               size += size_of_uleb128 (AT_unsigned (a));
7688             else
7689               size += csize;
7690           }
7691           break;
7692         case dw_val_class_const_double:
7693           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7694           if (HOST_BITS_PER_WIDE_INT >= 64)
7695             size++; /* block */
7696           break;
7697         case dw_val_class_vec:
7698           size += constant_size (a->dw_attr_val.v.val_vec.length
7699                                  * a->dw_attr_val.v.val_vec.elt_size)
7700                   + a->dw_attr_val.v.val_vec.length
7701                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7702           break;
7703         case dw_val_class_flag:
7704           if (dwarf_version >= 4)
7705             /* Currently all add_AT_flag calls pass in 1 as last argument,
7706                so DW_FORM_flag_present can be used.  If that ever changes,
7707                we'll need to use DW_FORM_flag and have some optimization
7708                in build_abbrev_table that will change those to
7709                DW_FORM_flag_present if it is set to 1 in all DIEs using
7710                the same abbrev entry.  */
7711             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7712           else
7713             size += 1;
7714           break;
7715         case dw_val_class_die_ref:
7716           if (AT_ref_external (a))
7717             {
7718               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7719                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7720                  is sized by target address length, whereas in DWARF3
7721                  it's always sized as an offset.  */
7722               if (use_debug_types)
7723                 size += DWARF_TYPE_SIGNATURE_SIZE;
7724               else if (dwarf_version == 2)
7725                 size += DWARF2_ADDR_SIZE;
7726               else
7727                 size += DWARF_OFFSET_SIZE;
7728             }
7729           else
7730             size += DWARF_OFFSET_SIZE;
7731           break;
7732         case dw_val_class_fde_ref:
7733           size += DWARF_OFFSET_SIZE;
7734           break;
7735         case dw_val_class_lbl_id:
7736           size += DWARF2_ADDR_SIZE;
7737           break;
7738         case dw_val_class_lineptr:
7739         case dw_val_class_macptr:
7740           size += DWARF_OFFSET_SIZE;
7741           break;
7742         case dw_val_class_str:
7743           if (AT_string_form (a) == DW_FORM_strp)
7744             size += DWARF_OFFSET_SIZE;
7745           else
7746             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7747           break;
7748         case dw_val_class_file:
7749           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7750           break;
7751         case dw_val_class_data8:
7752           size += 8;
7753           break;
7754         case dw_val_class_vms_delta:
7755           size += DWARF_OFFSET_SIZE;
7756           break;
7757         default:
7758           gcc_unreachable ();
7759         }
7760     }
7761
7762   return size;
7763 }
7764
7765 /* Size the debugging information associated with a given DIE.  Visits the
7766    DIE's children recursively.  Updates the global variable next_die_offset, on
7767    each time through.  Uses the current value of next_die_offset to update the
7768    die_offset field in each DIE.  */
7769
7770 static void
7771 calc_die_sizes (dw_die_ref die)
7772 {
7773   dw_die_ref c;
7774
7775   gcc_assert (die->die_offset == 0
7776               || (unsigned long int) die->die_offset == next_die_offset);
7777   die->die_offset = next_die_offset;
7778   next_die_offset += size_of_die (die);
7779
7780   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7781
7782   if (die->die_child != NULL)
7783     /* Count the null byte used to terminate sibling lists.  */
7784     next_die_offset += 1;
7785 }
7786
7787 /* Size just the base type children at the start of the CU.
7788    This is needed because build_abbrev needs to size locs
7789    and sizing of type based stack ops needs to know die_offset
7790    values for the base types.  */
7791
7792 static void
7793 calc_base_type_die_sizes (void)
7794 {
7795   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7796   unsigned int i;
7797   dw_die_ref base_type;
7798 #if ENABLE_ASSERT_CHECKING
7799   dw_die_ref prev = comp_unit_die ()->die_child;
7800 #endif
7801
7802   die_offset += size_of_die (comp_unit_die ());
7803   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7804     {
7805 #if ENABLE_ASSERT_CHECKING
7806       gcc_assert (base_type->die_offset == 0
7807                   && prev->die_sib == base_type
7808                   && base_type->die_child == NULL
7809                   && base_type->die_abbrev);
7810       prev = base_type;
7811 #endif
7812       base_type->die_offset = die_offset;
7813       die_offset += size_of_die (base_type);
7814     }
7815 }
7816
7817 /* Set the marks for a die and its children.  We do this so
7818    that we know whether or not a reference needs to use FORM_ref_addr; only
7819    DIEs in the same CU will be marked.  We used to clear out the offset
7820    and use that as the flag, but ran into ordering problems.  */
7821
7822 static void
7823 mark_dies (dw_die_ref die)
7824 {
7825   dw_die_ref c;
7826
7827   gcc_assert (!die->die_mark);
7828
7829   die->die_mark = 1;
7830   FOR_EACH_CHILD (die, c, mark_dies (c));
7831 }
7832
7833 /* Clear the marks for a die and its children.  */
7834
7835 static void
7836 unmark_dies (dw_die_ref die)
7837 {
7838   dw_die_ref c;
7839
7840   if (! use_debug_types)
7841     gcc_assert (die->die_mark);
7842
7843   die->die_mark = 0;
7844   FOR_EACH_CHILD (die, c, unmark_dies (c));
7845 }
7846
7847 /* Clear the marks for a die, its children and referred dies.  */
7848
7849 static void
7850 unmark_all_dies (dw_die_ref die)
7851 {
7852   dw_die_ref c;
7853   dw_attr_ref a;
7854   unsigned ix;
7855
7856   if (!die->die_mark)
7857     return;
7858   die->die_mark = 0;
7859
7860   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7861
7862   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7863     if (AT_class (a) == dw_val_class_die_ref)
7864       unmark_all_dies (AT_ref (a));
7865 }
7866
7867 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7868    generated for the compilation unit.  */
7869
7870 static unsigned long
7871 size_of_pubnames (VEC (pubname_entry, gc) * names)
7872 {
7873   unsigned long size;
7874   unsigned i;
7875   pubname_ref p;
7876
7877   size = DWARF_PUBNAMES_HEADER_SIZE;
7878   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7879     if (names != pubtype_table
7880         || p->die->die_offset != 0
7881         || !flag_eliminate_unused_debug_types)
7882       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7883
7884   size += DWARF_OFFSET_SIZE;
7885   return size;
7886 }
7887
7888 /* Return the size of the information in the .debug_aranges section.  */
7889
7890 static unsigned long
7891 size_of_aranges (void)
7892 {
7893   unsigned long size;
7894
7895   size = DWARF_ARANGES_HEADER_SIZE;
7896
7897   /* Count the address/length pair for this compilation unit.  */
7898   if (text_section_used)
7899     size += 2 * DWARF2_ADDR_SIZE;
7900   if (cold_text_section_used)
7901     size += 2 * DWARF2_ADDR_SIZE;
7902   if (have_multiple_function_sections)
7903     {
7904       unsigned fde_idx;
7905       dw_fde_ref fde;
7906
7907       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7908         {
7909           if (!fde->in_std_section)
7910             size += 2 * DWARF2_ADDR_SIZE;
7911           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7912             size += 2 * DWARF2_ADDR_SIZE;
7913         }
7914     }
7915
7916   /* Count the two zero words used to terminated the address range table.  */
7917   size += 2 * DWARF2_ADDR_SIZE;
7918   return size;
7919 }
7920 \f
7921 /* Select the encoding of an attribute value.  */
7922
7923 static enum dwarf_form
7924 value_format (dw_attr_ref a)
7925 {
7926   switch (a->dw_attr_val.val_class)
7927     {
7928     case dw_val_class_addr:
7929       /* Only very few attributes allow DW_FORM_addr.  */
7930       switch (a->dw_attr)
7931         {
7932         case DW_AT_low_pc:
7933         case DW_AT_high_pc:
7934         case DW_AT_entry_pc:
7935         case DW_AT_trampoline:
7936           return DW_FORM_addr;
7937         default:
7938           break;
7939         }
7940       switch (DWARF2_ADDR_SIZE)
7941         {
7942         case 1:
7943           return DW_FORM_data1;
7944         case 2:
7945           return DW_FORM_data2;
7946         case 4:
7947           return DW_FORM_data4;
7948         case 8:
7949           return DW_FORM_data8;
7950         default:
7951           gcc_unreachable ();
7952         }
7953     case dw_val_class_range_list:
7954     case dw_val_class_loc_list:
7955       if (dwarf_version >= 4)
7956         return DW_FORM_sec_offset;
7957       /* FALLTHRU */
7958     case dw_val_class_vms_delta:
7959     case dw_val_class_offset:
7960       switch (DWARF_OFFSET_SIZE)
7961         {
7962         case 4:
7963           return DW_FORM_data4;
7964         case 8:
7965           return DW_FORM_data8;
7966         default:
7967           gcc_unreachable ();
7968         }
7969     case dw_val_class_loc:
7970       if (dwarf_version >= 4)
7971         return DW_FORM_exprloc;
7972       switch (constant_size (size_of_locs (AT_loc (a))))
7973         {
7974         case 1:
7975           return DW_FORM_block1;
7976         case 2:
7977           return DW_FORM_block2;
7978         default:
7979           gcc_unreachable ();
7980         }
7981     case dw_val_class_const:
7982       return DW_FORM_sdata;
7983     case dw_val_class_unsigned_const:
7984       switch (constant_size (AT_unsigned (a)))
7985         {
7986         case 1:
7987           return DW_FORM_data1;
7988         case 2:
7989           return DW_FORM_data2;
7990         case 4:
7991           /* In DWARF3 DW_AT_data_member_location with
7992              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7993              constant, so we need to use DW_FORM_udata if we need
7994              a large constant.  */
7995           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7996             return DW_FORM_udata;
7997           return DW_FORM_data4;
7998         case 8:
7999           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8000             return DW_FORM_udata;
8001           return DW_FORM_data8;
8002         default:
8003           gcc_unreachable ();
8004         }
8005     case dw_val_class_const_double:
8006       switch (HOST_BITS_PER_WIDE_INT)
8007         {
8008         case 8:
8009           return DW_FORM_data2;
8010         case 16:
8011           return DW_FORM_data4;
8012         case 32:
8013           return DW_FORM_data8;
8014         case 64:
8015         default:
8016           return DW_FORM_block1;
8017         }
8018     case dw_val_class_vec:
8019       switch (constant_size (a->dw_attr_val.v.val_vec.length
8020                              * a->dw_attr_val.v.val_vec.elt_size))
8021         {
8022         case 1:
8023           return DW_FORM_block1;
8024         case 2:
8025           return DW_FORM_block2;
8026         case 4:
8027           return DW_FORM_block4;
8028         default:
8029           gcc_unreachable ();
8030         }
8031     case dw_val_class_flag:
8032       if (dwarf_version >= 4)
8033         {
8034           /* Currently all add_AT_flag calls pass in 1 as last argument,
8035              so DW_FORM_flag_present can be used.  If that ever changes,
8036              we'll need to use DW_FORM_flag and have some optimization
8037              in build_abbrev_table that will change those to
8038              DW_FORM_flag_present if it is set to 1 in all DIEs using
8039              the same abbrev entry.  */
8040           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8041           return DW_FORM_flag_present;
8042         }
8043       return DW_FORM_flag;
8044     case dw_val_class_die_ref:
8045       if (AT_ref_external (a))
8046         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8047       else
8048         return DW_FORM_ref;
8049     case dw_val_class_fde_ref:
8050       return DW_FORM_data;
8051     case dw_val_class_lbl_id:
8052       return DW_FORM_addr;
8053     case dw_val_class_lineptr:
8054     case dw_val_class_macptr:
8055       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8056     case dw_val_class_str:
8057       return AT_string_form (a);
8058     case dw_val_class_file:
8059       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8060         {
8061         case 1:
8062           return DW_FORM_data1;
8063         case 2:
8064           return DW_FORM_data2;
8065         case 4:
8066           return DW_FORM_data4;
8067         default:
8068           gcc_unreachable ();
8069         }
8070
8071     case dw_val_class_data8:
8072       return DW_FORM_data8;
8073
8074     default:
8075       gcc_unreachable ();
8076     }
8077 }
8078
8079 /* Output the encoding of an attribute value.  */
8080
8081 static void
8082 output_value_format (dw_attr_ref a)
8083 {
8084   enum dwarf_form form = value_format (a);
8085
8086   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8087 }
8088
8089 /* Output the .debug_abbrev section which defines the DIE abbreviation
8090    table.  */
8091
8092 static void
8093 output_abbrev_section (void)
8094 {
8095   unsigned long abbrev_id;
8096
8097   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8098     {
8099       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8100       unsigned ix;
8101       dw_attr_ref a_attr;
8102
8103       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8104       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8105                                    dwarf_tag_name (abbrev->die_tag));
8106
8107       if (abbrev->die_child != NULL)
8108         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8109       else
8110         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8111
8112       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8113            ix++)
8114         {
8115           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8116                                        dwarf_attr_name (a_attr->dw_attr));
8117           output_value_format (a_attr);
8118         }
8119
8120       dw2_asm_output_data (1, 0, NULL);
8121       dw2_asm_output_data (1, 0, NULL);
8122     }
8123
8124   /* Terminate the table.  */
8125   dw2_asm_output_data (1, 0, NULL);
8126 }
8127
8128 /* Output a symbol we can use to refer to this DIE from another CU.  */
8129
8130 static inline void
8131 output_die_symbol (dw_die_ref die)
8132 {
8133   char *sym = die->die_id.die_symbol;
8134
8135   if (sym == 0)
8136     return;
8137
8138   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8139     /* We make these global, not weak; if the target doesn't support
8140        .linkonce, it doesn't support combining the sections, so debugging
8141        will break.  */
8142     targetm.asm_out.globalize_label (asm_out_file, sym);
8143
8144   ASM_OUTPUT_LABEL (asm_out_file, sym);
8145 }
8146
8147 /* Return a new location list, given the begin and end range, and the
8148    expression.  */
8149
8150 static inline dw_loc_list_ref
8151 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8152               const char *section)
8153 {
8154   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8155
8156   retlist->begin = begin;
8157   retlist->end = end;
8158   retlist->expr = expr;
8159   retlist->section = section;
8160
8161   return retlist;
8162 }
8163
8164 /* Generate a new internal symbol for this location list node, if it
8165    hasn't got one yet.  */
8166
8167 static inline void
8168 gen_llsym (dw_loc_list_ref list)
8169 {
8170   gcc_assert (!list->ll_symbol);
8171   list->ll_symbol = gen_internal_sym ("LLST");
8172 }
8173
8174 /* Output the location list given to us.  */
8175
8176 static void
8177 output_loc_list (dw_loc_list_ref list_head)
8178 {
8179   dw_loc_list_ref curr = list_head;
8180
8181   if (list_head->emitted)
8182     return;
8183   list_head->emitted = true;
8184
8185   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8186
8187   /* Walk the location list, and output each range + expression.  */
8188   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8189     {
8190       unsigned long size;
8191       /* Don't output an entry that starts and ends at the same address.  */
8192       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8193         continue;
8194       size = size_of_locs (curr->expr);
8195       /* If the expression is too large, drop it on the floor.  We could
8196          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8197          in the expression, but >= 64KB expressions for a single value
8198          in a single range are unlikely very useful.  */
8199       if (size > 0xffff)
8200         continue;
8201       if (!have_multiple_function_sections)
8202         {
8203           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8204                                 "Location list begin address (%s)",
8205                                 list_head->ll_symbol);
8206           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8207                                 "Location list end address (%s)",
8208                                 list_head->ll_symbol);
8209         }
8210       else
8211         {
8212           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8213                                "Location list begin address (%s)",
8214                                list_head->ll_symbol);
8215           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8216                                "Location list end address (%s)",
8217                                list_head->ll_symbol);
8218         }
8219
8220       /* Output the block length for this list of location operations.  */
8221       gcc_assert (size <= 0xffff);
8222       dw2_asm_output_data (2, size, "%s", "Location expression size");
8223
8224       output_loc_sequence (curr->expr, -1);
8225     }
8226
8227   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8228                        "Location list terminator begin (%s)",
8229                        list_head->ll_symbol);
8230   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8231                        "Location list terminator end (%s)",
8232                        list_head->ll_symbol);
8233 }
8234
8235 /* Output a type signature.  */
8236
8237 static inline void
8238 output_signature (const char *sig, const char *name)
8239 {
8240   int i;
8241
8242   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8243     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8244 }
8245
8246 /* Output the DIE and its attributes.  Called recursively to generate
8247    the definitions of each child DIE.  */
8248
8249 static void
8250 output_die (dw_die_ref die)
8251 {
8252   dw_attr_ref a;
8253   dw_die_ref c;
8254   unsigned long size;
8255   unsigned ix;
8256
8257   /* If someone in another CU might refer to us, set up a symbol for
8258      them to point to.  */
8259   if (! use_debug_types && die->die_id.die_symbol)
8260     output_die_symbol (die);
8261
8262   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8263                                (unsigned long)die->die_offset,
8264                                dwarf_tag_name (die->die_tag));
8265
8266   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8267     {
8268       const char *name = dwarf_attr_name (a->dw_attr);
8269
8270       switch (AT_class (a))
8271         {
8272         case dw_val_class_addr:
8273           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8274           break;
8275
8276         case dw_val_class_offset:
8277           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8278                                "%s", name);
8279           break;
8280
8281         case dw_val_class_range_list:
8282           {
8283             char *p = strchr (ranges_section_label, '\0');
8284
8285             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8286                      a->dw_attr_val.v.val_offset);
8287             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8288                                    debug_ranges_section, "%s", name);
8289             *p = '\0';
8290           }
8291           break;
8292
8293         case dw_val_class_loc:
8294           size = size_of_locs (AT_loc (a));
8295
8296           /* Output the block length for this list of location operations.  */
8297           if (dwarf_version >= 4)
8298             dw2_asm_output_data_uleb128 (size, "%s", name);
8299           else
8300             dw2_asm_output_data (constant_size (size), size, "%s", name);
8301
8302           output_loc_sequence (AT_loc (a), -1);
8303           break;
8304
8305         case dw_val_class_const:
8306           /* ??? It would be slightly more efficient to use a scheme like is
8307              used for unsigned constants below, but gdb 4.x does not sign
8308              extend.  Gdb 5.x does sign extend.  */
8309           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8310           break;
8311
8312         case dw_val_class_unsigned_const:
8313           {
8314             int csize = constant_size (AT_unsigned (a));
8315             if (dwarf_version == 3
8316                 && a->dw_attr == DW_AT_data_member_location
8317                 && csize >= 4)
8318               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8319             else
8320               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8321           }
8322           break;
8323
8324         case dw_val_class_const_double:
8325           {
8326             unsigned HOST_WIDE_INT first, second;
8327
8328             if (HOST_BITS_PER_WIDE_INT >= 64)
8329               dw2_asm_output_data (1,
8330                                    2 * HOST_BITS_PER_WIDE_INT
8331                                    / HOST_BITS_PER_CHAR,
8332                                    NULL);
8333
8334             if (WORDS_BIG_ENDIAN)
8335               {
8336                 first = a->dw_attr_val.v.val_double.high;
8337                 second = a->dw_attr_val.v.val_double.low;
8338               }
8339             else
8340               {
8341                 first = a->dw_attr_val.v.val_double.low;
8342                 second = a->dw_attr_val.v.val_double.high;
8343               }
8344
8345             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8346                                  first, name);
8347             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8348                                  second, NULL);
8349           }
8350           break;
8351
8352         case dw_val_class_vec:
8353           {
8354             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8355             unsigned int len = a->dw_attr_val.v.val_vec.length;
8356             unsigned int i;
8357             unsigned char *p;
8358
8359             dw2_asm_output_data (constant_size (len * elt_size),
8360                                  len * elt_size, "%s", name);
8361             if (elt_size > sizeof (HOST_WIDE_INT))
8362               {
8363                 elt_size /= 2;
8364                 len *= 2;
8365               }
8366             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8367                  i < len;
8368                  i++, p += elt_size)
8369               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8370                                    "fp or vector constant word %u", i);
8371             break;
8372           }
8373
8374         case dw_val_class_flag:
8375           if (dwarf_version >= 4)
8376             {
8377               /* Currently all add_AT_flag calls pass in 1 as last argument,
8378                  so DW_FORM_flag_present can be used.  If that ever changes,
8379                  we'll need to use DW_FORM_flag and have some optimization
8380                  in build_abbrev_table that will change those to
8381                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8382                  the same abbrev entry.  */
8383               gcc_assert (AT_flag (a) == 1);
8384               if (flag_debug_asm)
8385                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8386                          ASM_COMMENT_START, name);
8387               break;
8388             }
8389           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8390           break;
8391
8392         case dw_val_class_loc_list:
8393           {
8394             char *sym = AT_loc_list (a)->ll_symbol;
8395
8396             gcc_assert (sym);
8397             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8398                                    "%s", name);
8399           }
8400           break;
8401
8402         case dw_val_class_die_ref:
8403           if (AT_ref_external (a))
8404             {
8405               if (use_debug_types)
8406                 {
8407                   comdat_type_node_ref type_node =
8408                     AT_ref (a)->die_id.die_type_node;
8409
8410                   gcc_assert (type_node);
8411                   output_signature (type_node->signature, name);
8412                 }
8413               else
8414                 {
8415                   char *sym = AT_ref (a)->die_id.die_symbol;
8416                   int size;
8417
8418                   gcc_assert (sym);
8419                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8420                      length, whereas in DWARF3 it's always sized as an
8421                      offset.  */
8422                   if (dwarf_version == 2)
8423                     size = DWARF2_ADDR_SIZE;
8424                   else
8425                     size = DWARF_OFFSET_SIZE;
8426                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8427                                          name);
8428                 }
8429             }
8430           else
8431             {
8432               gcc_assert (AT_ref (a)->die_offset);
8433               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8434                                    "%s", name);
8435             }
8436           break;
8437
8438         case dw_val_class_fde_ref:
8439           {
8440             char l1[20];
8441
8442             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8443                                          a->dw_attr_val.v.val_fde_index * 2);
8444             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8445                                    "%s", name);
8446           }
8447           break;
8448
8449         case dw_val_class_vms_delta:
8450           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8451                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8452                                     "%s", name);
8453           break;
8454
8455         case dw_val_class_lbl_id:
8456           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8457           break;
8458
8459         case dw_val_class_lineptr:
8460           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8461                                  debug_line_section, "%s", name);
8462           break;
8463
8464         case dw_val_class_macptr:
8465           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8466                                  debug_macinfo_section, "%s", name);
8467           break;
8468
8469         case dw_val_class_str:
8470           if (AT_string_form (a) == DW_FORM_strp)
8471             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8472                                    a->dw_attr_val.v.val_str->label,
8473                                    debug_str_section,
8474                                    "%s: \"%s\"", name, AT_string (a));
8475           else
8476             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8477           break;
8478
8479         case dw_val_class_file:
8480           {
8481             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8482
8483             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8484                                  a->dw_attr_val.v.val_file->filename);
8485             break;
8486           }
8487
8488         case dw_val_class_data8:
8489           {
8490             int i;
8491
8492             for (i = 0; i < 8; i++)
8493               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8494                                    i == 0 ? "%s" : NULL, name);
8495             break;
8496           }
8497
8498         default:
8499           gcc_unreachable ();
8500         }
8501     }
8502
8503   FOR_EACH_CHILD (die, c, output_die (c));
8504
8505   /* Add null byte to terminate sibling list.  */
8506   if (die->die_child != NULL)
8507     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8508                          (unsigned long) die->die_offset);
8509 }
8510
8511 /* Output the compilation unit that appears at the beginning of the
8512    .debug_info section, and precedes the DIE descriptions.  */
8513
8514 static void
8515 output_compilation_unit_header (void)
8516 {
8517   int ver = dwarf_version;
8518
8519   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8520     dw2_asm_output_data (4, 0xffffffff,
8521       "Initial length escape value indicating 64-bit DWARF extension");
8522   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8523                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8524                        "Length of Compilation Unit Info");
8525   dw2_asm_output_data (2, ver, "DWARF version number");
8526   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8527                          debug_abbrev_section,
8528                          "Offset Into Abbrev. Section");
8529   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8530 }
8531
8532 /* Output the compilation unit DIE and its children.  */
8533
8534 static void
8535 output_comp_unit (dw_die_ref die, int output_if_empty)
8536 {
8537   const char *secname;
8538   char *oldsym, *tmp;
8539
8540   /* Unless we are outputting main CU, we may throw away empty ones.  */
8541   if (!output_if_empty && die->die_child == NULL)
8542     return;
8543
8544   /* Even if there are no children of this DIE, we must output the information
8545      about the compilation unit.  Otherwise, on an empty translation unit, we
8546      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8547      will then complain when examining the file.  First mark all the DIEs in
8548      this CU so we know which get local refs.  */
8549   mark_dies (die);
8550
8551   build_abbrev_table (die);
8552
8553   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8554   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8555   calc_die_sizes (die);
8556
8557   oldsym = die->die_id.die_symbol;
8558   if (oldsym)
8559     {
8560       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8561
8562       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8563       secname = tmp;
8564       die->die_id.die_symbol = NULL;
8565       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8566     }
8567   else
8568     {
8569       switch_to_section (debug_info_section);
8570       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8571       info_section_emitted = true;
8572     }
8573
8574   /* Output debugging information.  */
8575   output_compilation_unit_header ();
8576   output_die (die);
8577
8578   /* Leave the marks on the main CU, so we can check them in
8579      output_pubnames.  */
8580   if (oldsym)
8581     {
8582       unmark_dies (die);
8583       die->die_id.die_symbol = oldsym;
8584     }
8585 }
8586
8587 /* Output a comdat type unit DIE and its children.  */
8588
8589 static void
8590 output_comdat_type_unit (comdat_type_node *node)
8591 {
8592   const char *secname;
8593   char *tmp;
8594   int i;
8595 #if defined (OBJECT_FORMAT_ELF)
8596   tree comdat_key;
8597 #endif
8598
8599   /* First mark all the DIEs in this CU so we know which get local refs.  */
8600   mark_dies (node->root_die);
8601
8602   build_abbrev_table (node->root_die);
8603
8604   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8605   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8606   calc_die_sizes (node->root_die);
8607
8608 #if defined (OBJECT_FORMAT_ELF)
8609   secname = ".debug_types";
8610   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8611   sprintf (tmp, "wt.");
8612   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8613     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8614   comdat_key = get_identifier (tmp);
8615   targetm.asm_out.named_section (secname,
8616                                  SECTION_DEBUG | SECTION_LINKONCE,
8617                                  comdat_key);
8618 #else
8619   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8620   sprintf (tmp, ".gnu.linkonce.wt.");
8621   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8622     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8623   secname = tmp;
8624   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8625 #endif
8626
8627   /* Output debugging information.  */
8628   output_compilation_unit_header ();
8629   output_signature (node->signature, "Type Signature");
8630   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8631                        "Offset to Type DIE");
8632   output_die (node->root_die);
8633
8634   unmark_dies (node->root_die);
8635 }
8636
8637 /* Return the DWARF2/3 pubname associated with a decl.  */
8638
8639 static const char *
8640 dwarf2_name (tree decl, int scope)
8641 {
8642   if (DECL_NAMELESS (decl))
8643     return NULL;
8644   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8645 }
8646
8647 /* Add a new entry to .debug_pubnames if appropriate.  */
8648
8649 static void
8650 add_pubname_string (const char *str, dw_die_ref die)
8651 {
8652   if (targetm.want_debug_pub_sections)
8653     {
8654       pubname_entry e;
8655
8656       e.die = die;
8657       e.name = xstrdup (str);
8658       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8659     }
8660 }
8661
8662 static void
8663 add_pubname (tree decl, dw_die_ref die)
8664 {
8665   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8666     {
8667       const char *name = dwarf2_name (decl, 1);
8668       if (name)
8669         add_pubname_string (name, die);
8670     }
8671 }
8672
8673 /* Add a new entry to .debug_pubtypes if appropriate.  */
8674
8675 static void
8676 add_pubtype (tree decl, dw_die_ref die)
8677 {
8678   pubname_entry e;
8679
8680   if (!targetm.want_debug_pub_sections)
8681     return;
8682
8683   e.name = NULL;
8684   if ((TREE_PUBLIC (decl)
8685        || is_cu_die (die->die_parent))
8686       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8687     {
8688       e.die = die;
8689       if (TYPE_P (decl))
8690         {
8691           if (TYPE_NAME (decl))
8692             {
8693               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8694                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8695               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8696                        && DECL_NAME (TYPE_NAME (decl)))
8697                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8698               else
8699                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8700             }
8701         }
8702       else
8703         {
8704           e.name = dwarf2_name (decl, 1);
8705           if (e.name)
8706             e.name = xstrdup (e.name);
8707         }
8708
8709       /* If we don't have a name for the type, there's no point in adding
8710          it to the table.  */
8711       if (e.name && e.name[0] != '\0')
8712         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8713     }
8714 }
8715
8716 /* Output the public names table used to speed up access to externally
8717    visible names; or the public types table used to find type definitions.  */
8718
8719 static void
8720 output_pubnames (VEC (pubname_entry, gc) * names)
8721 {
8722   unsigned i;
8723   unsigned long pubnames_length = size_of_pubnames (names);
8724   pubname_ref pub;
8725
8726   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8727     dw2_asm_output_data (4, 0xffffffff,
8728       "Initial length escape value indicating 64-bit DWARF extension");
8729   if (names == pubname_table)
8730     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8731                          "Length of Public Names Info");
8732   else
8733     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8734                          "Length of Public Type Names Info");
8735   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8736   dw2_asm_output_data (2, 2, "DWARF Version");
8737   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8738                          debug_info_section,
8739                          "Offset of Compilation Unit Info");
8740   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8741                        "Compilation Unit Length");
8742
8743   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8744     {
8745       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8746       if (names == pubname_table)
8747         gcc_assert (pub->die->die_mark);
8748
8749       if (names != pubtype_table
8750           || pub->die->die_offset != 0
8751           || !flag_eliminate_unused_debug_types)
8752         {
8753           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8754                                "DIE offset");
8755
8756           dw2_asm_output_nstring (pub->name, -1, "external name");
8757         }
8758     }
8759
8760   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8761 }
8762
8763 /* Output the information that goes into the .debug_aranges table.
8764    Namely, define the beginning and ending address range of the
8765    text section generated for this compilation unit.  */
8766
8767 static void
8768 output_aranges (unsigned long aranges_length)
8769 {
8770   unsigned i;
8771
8772   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8773     dw2_asm_output_data (4, 0xffffffff,
8774       "Initial length escape value indicating 64-bit DWARF extension");
8775   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8776                        "Length of Address Ranges Info");
8777   /* Version number for aranges is still 2, even in DWARF3.  */
8778   dw2_asm_output_data (2, 2, "DWARF Version");
8779   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8780                          debug_info_section,
8781                          "Offset of Compilation Unit Info");
8782   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8783   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8784
8785   /* We need to align to twice the pointer size here.  */
8786   if (DWARF_ARANGES_PAD_SIZE)
8787     {
8788       /* Pad using a 2 byte words so that padding is correct for any
8789          pointer size.  */
8790       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8791                            2 * DWARF2_ADDR_SIZE);
8792       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8793         dw2_asm_output_data (2, 0, NULL);
8794     }
8795
8796   /* It is necessary not to output these entries if the sections were
8797      not used; if the sections were not used, the length will be 0 and
8798      the address may end up as 0 if the section is discarded by ld
8799      --gc-sections, leaving an invalid (0, 0) entry that can be
8800      confused with the terminator.  */
8801   if (text_section_used)
8802     {
8803       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8804       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8805                             text_section_label, "Length");
8806     }
8807   if (cold_text_section_used)
8808     {
8809       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8810                            "Address");
8811       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8812                             cold_text_section_label, "Length");
8813     }
8814
8815   if (have_multiple_function_sections)
8816     {
8817       unsigned fde_idx;
8818       dw_fde_ref fde;
8819
8820       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8821         {
8822           if (!fde->in_std_section)
8823             {
8824               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8825                                    "Address");
8826               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8827                                     fde->dw_fde_begin, "Length");
8828             }
8829           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8830             {
8831               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8832                                    "Address");
8833               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8834                                     fde->dw_fde_second_begin, "Length");
8835             }
8836         }
8837     }
8838
8839   /* Output the terminator words.  */
8840   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8841   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8842 }
8843
8844 /* Add a new entry to .debug_ranges.  Return the offset at which it
8845    was placed.  */
8846
8847 static unsigned int
8848 add_ranges_num (int num)
8849 {
8850   unsigned int in_use = ranges_table_in_use;
8851
8852   if (in_use == ranges_table_allocated)
8853     {
8854       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8855       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8856                                     ranges_table_allocated);
8857       memset (ranges_table + ranges_table_in_use, 0,
8858               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8859     }
8860
8861   ranges_table[in_use].num = num;
8862   ranges_table_in_use = in_use + 1;
8863
8864   return in_use * 2 * DWARF2_ADDR_SIZE;
8865 }
8866
8867 /* Add a new entry to .debug_ranges corresponding to a block, or a
8868    range terminator if BLOCK is NULL.  */
8869
8870 static unsigned int
8871 add_ranges (const_tree block)
8872 {
8873   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8874 }
8875
8876 /* Add a new entry to .debug_ranges corresponding to a pair of
8877    labels.  */
8878
8879 static void
8880 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8881                       bool *added)
8882 {
8883   unsigned int in_use = ranges_by_label_in_use;
8884   unsigned int offset;
8885
8886   if (in_use == ranges_by_label_allocated)
8887     {
8888       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8889       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8890                                        ranges_by_label,
8891                                        ranges_by_label_allocated);
8892       memset (ranges_by_label + ranges_by_label_in_use, 0,
8893               RANGES_TABLE_INCREMENT
8894               * sizeof (struct dw_ranges_by_label_struct));
8895     }
8896
8897   ranges_by_label[in_use].begin = begin;
8898   ranges_by_label[in_use].end = end;
8899   ranges_by_label_in_use = in_use + 1;
8900
8901   offset = add_ranges_num (-(int)in_use - 1);
8902   if (!*added)
8903     {
8904       add_AT_range_list (die, DW_AT_ranges, offset);
8905       *added = true;
8906     }
8907 }
8908
8909 static void
8910 output_ranges (void)
8911 {
8912   unsigned i;
8913   static const char *const start_fmt = "Offset %#x";
8914   const char *fmt = start_fmt;
8915
8916   for (i = 0; i < ranges_table_in_use; i++)
8917     {
8918       int block_num = ranges_table[i].num;
8919
8920       if (block_num > 0)
8921         {
8922           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8923           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8924
8925           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8926           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8927
8928           /* If all code is in the text section, then the compilation
8929              unit base address defaults to DW_AT_low_pc, which is the
8930              base of the text section.  */
8931           if (!have_multiple_function_sections)
8932             {
8933               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8934                                     text_section_label,
8935                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8936               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8937                                     text_section_label, NULL);
8938             }
8939
8940           /* Otherwise, the compilation unit base address is zero,
8941              which allows us to use absolute addresses, and not worry
8942              about whether the target supports cross-section
8943              arithmetic.  */
8944           else
8945             {
8946               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8947                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8948               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8949             }
8950
8951           fmt = NULL;
8952         }
8953
8954       /* Negative block_num stands for an index into ranges_by_label.  */
8955       else if (block_num < 0)
8956         {
8957           int lab_idx = - block_num - 1;
8958
8959           if (!have_multiple_function_sections)
8960             {
8961               gcc_unreachable ();
8962 #if 0
8963               /* If we ever use add_ranges_by_labels () for a single
8964                  function section, all we have to do is to take out
8965                  the #if 0 above.  */
8966               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8967                                     ranges_by_label[lab_idx].begin,
8968                                     text_section_label,
8969                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8970               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8971                                     ranges_by_label[lab_idx].end,
8972                                     text_section_label, NULL);
8973 #endif
8974             }
8975           else
8976             {
8977               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8978                                    ranges_by_label[lab_idx].begin,
8979                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8980               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8981                                    ranges_by_label[lab_idx].end,
8982                                    NULL);
8983             }
8984         }
8985       else
8986         {
8987           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8988           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8989           fmt = start_fmt;
8990         }
8991     }
8992 }
8993
8994 /* Data structure containing information about input files.  */
8995 struct file_info
8996 {
8997   const char *path;     /* Complete file name.  */
8998   const char *fname;    /* File name part.  */
8999   int length;           /* Length of entire string.  */
9000   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9001   int dir_idx;          /* Index in directory table.  */
9002 };
9003
9004 /* Data structure containing information about directories with source
9005    files.  */
9006 struct dir_info
9007 {
9008   const char *path;     /* Path including directory name.  */
9009   int length;           /* Path length.  */
9010   int prefix;           /* Index of directory entry which is a prefix.  */
9011   int count;            /* Number of files in this directory.  */
9012   int dir_idx;          /* Index of directory used as base.  */
9013 };
9014
9015 /* Callback function for file_info comparison.  We sort by looking at
9016    the directories in the path.  */
9017
9018 static int
9019 file_info_cmp (const void *p1, const void *p2)
9020 {
9021   const struct file_info *const s1 = (const struct file_info *) p1;
9022   const struct file_info *const s2 = (const struct file_info *) p2;
9023   const unsigned char *cp1;
9024   const unsigned char *cp2;
9025
9026   /* Take care of file names without directories.  We need to make sure that
9027      we return consistent values to qsort since some will get confused if
9028      we return the same value when identical operands are passed in opposite
9029      orders.  So if neither has a directory, return 0 and otherwise return
9030      1 or -1 depending on which one has the directory.  */
9031   if ((s1->path == s1->fname || s2->path == s2->fname))
9032     return (s2->path == s2->fname) - (s1->path == s1->fname);
9033
9034   cp1 = (const unsigned char *) s1->path;
9035   cp2 = (const unsigned char *) s2->path;
9036
9037   while (1)
9038     {
9039       ++cp1;
9040       ++cp2;
9041       /* Reached the end of the first path?  If so, handle like above.  */
9042       if ((cp1 == (const unsigned char *) s1->fname)
9043           || (cp2 == (const unsigned char *) s2->fname))
9044         return ((cp2 == (const unsigned char *) s2->fname)
9045                 - (cp1 == (const unsigned char *) s1->fname));
9046
9047       /* Character of current path component the same?  */
9048       else if (*cp1 != *cp2)
9049         return *cp1 - *cp2;
9050     }
9051 }
9052
9053 struct file_name_acquire_data
9054 {
9055   struct file_info *files;
9056   int used_files;
9057   int max_files;
9058 };
9059
9060 /* Traversal function for the hash table.  */
9061
9062 static int
9063 file_name_acquire (void ** slot, void *data)
9064 {
9065   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9066   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9067   struct file_info *fi;
9068   const char *f;
9069
9070   gcc_assert (fnad->max_files >= d->emitted_number);
9071
9072   if (! d->emitted_number)
9073     return 1;
9074
9075   gcc_assert (fnad->max_files != fnad->used_files);
9076
9077   fi = fnad->files + fnad->used_files++;
9078
9079   /* Skip all leading "./".  */
9080   f = d->filename;
9081   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9082     f += 2;
9083
9084   /* Create a new array entry.  */
9085   fi->path = f;
9086   fi->length = strlen (f);
9087   fi->file_idx = d;
9088
9089   /* Search for the file name part.  */
9090   f = strrchr (f, DIR_SEPARATOR);
9091 #if defined (DIR_SEPARATOR_2)
9092   {
9093     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9094
9095     if (g != NULL)
9096       {
9097         if (f == NULL || f < g)
9098           f = g;
9099       }
9100   }
9101 #endif
9102
9103   fi->fname = f == NULL ? fi->path : f + 1;
9104   return 1;
9105 }
9106
9107 /* Output the directory table and the file name table.  We try to minimize
9108    the total amount of memory needed.  A heuristic is used to avoid large
9109    slowdowns with many input files.  */
9110
9111 static void
9112 output_file_names (void)
9113 {
9114   struct file_name_acquire_data fnad;
9115   int numfiles;
9116   struct file_info *files;
9117   struct dir_info *dirs;
9118   int *saved;
9119   int *savehere;
9120   int *backmap;
9121   int ndirs;
9122   int idx_offset;
9123   int i;
9124
9125   if (!last_emitted_file)
9126     {
9127       dw2_asm_output_data (1, 0, "End directory table");
9128       dw2_asm_output_data (1, 0, "End file name table");
9129       return;
9130     }
9131
9132   numfiles = last_emitted_file->emitted_number;
9133
9134   /* Allocate the various arrays we need.  */
9135   files = XALLOCAVEC (struct file_info, numfiles);
9136   dirs = XALLOCAVEC (struct dir_info, numfiles);
9137
9138   fnad.files = files;
9139   fnad.used_files = 0;
9140   fnad.max_files = numfiles;
9141   htab_traverse (file_table, file_name_acquire, &fnad);
9142   gcc_assert (fnad.used_files == fnad.max_files);
9143
9144   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9145
9146   /* Find all the different directories used.  */
9147   dirs[0].path = files[0].path;
9148   dirs[0].length = files[0].fname - files[0].path;
9149   dirs[0].prefix = -1;
9150   dirs[0].count = 1;
9151   dirs[0].dir_idx = 0;
9152   files[0].dir_idx = 0;
9153   ndirs = 1;
9154
9155   for (i = 1; i < numfiles; i++)
9156     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9157         && memcmp (dirs[ndirs - 1].path, files[i].path,
9158                    dirs[ndirs - 1].length) == 0)
9159       {
9160         /* Same directory as last entry.  */
9161         files[i].dir_idx = ndirs - 1;
9162         ++dirs[ndirs - 1].count;
9163       }
9164     else
9165       {
9166         int j;
9167
9168         /* This is a new directory.  */
9169         dirs[ndirs].path = files[i].path;
9170         dirs[ndirs].length = files[i].fname - files[i].path;
9171         dirs[ndirs].count = 1;
9172         dirs[ndirs].dir_idx = ndirs;
9173         files[i].dir_idx = ndirs;
9174
9175         /* Search for a prefix.  */
9176         dirs[ndirs].prefix = -1;
9177         for (j = 0; j < ndirs; j++)
9178           if (dirs[j].length < dirs[ndirs].length
9179               && dirs[j].length > 1
9180               && (dirs[ndirs].prefix == -1
9181                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9182               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9183             dirs[ndirs].prefix = j;
9184
9185         ++ndirs;
9186       }
9187
9188   /* Now to the actual work.  We have to find a subset of the directories which
9189      allow expressing the file name using references to the directory table
9190      with the least amount of characters.  We do not do an exhaustive search
9191      where we would have to check out every combination of every single
9192      possible prefix.  Instead we use a heuristic which provides nearly optimal
9193      results in most cases and never is much off.  */
9194   saved = XALLOCAVEC (int, ndirs);
9195   savehere = XALLOCAVEC (int, ndirs);
9196
9197   memset (saved, '\0', ndirs * sizeof (saved[0]));
9198   for (i = 0; i < ndirs; i++)
9199     {
9200       int j;
9201       int total;
9202
9203       /* We can always save some space for the current directory.  But this
9204          does not mean it will be enough to justify adding the directory.  */
9205       savehere[i] = dirs[i].length;
9206       total = (savehere[i] - saved[i]) * dirs[i].count;
9207
9208       for (j = i + 1; j < ndirs; j++)
9209         {
9210           savehere[j] = 0;
9211           if (saved[j] < dirs[i].length)
9212             {
9213               /* Determine whether the dirs[i] path is a prefix of the
9214                  dirs[j] path.  */
9215               int k;
9216
9217               k = dirs[j].prefix;
9218               while (k != -1 && k != (int) i)
9219                 k = dirs[k].prefix;
9220
9221               if (k == (int) i)
9222                 {
9223                   /* Yes it is.  We can possibly save some memory by
9224                      writing the filenames in dirs[j] relative to
9225                      dirs[i].  */
9226                   savehere[j] = dirs[i].length;
9227                   total += (savehere[j] - saved[j]) * dirs[j].count;
9228                 }
9229             }
9230         }
9231
9232       /* Check whether we can save enough to justify adding the dirs[i]
9233          directory.  */
9234       if (total > dirs[i].length + 1)
9235         {
9236           /* It's worthwhile adding.  */
9237           for (j = i; j < ndirs; j++)
9238             if (savehere[j] > 0)
9239               {
9240                 /* Remember how much we saved for this directory so far.  */
9241                 saved[j] = savehere[j];
9242
9243                 /* Remember the prefix directory.  */
9244                 dirs[j].dir_idx = i;
9245               }
9246         }
9247     }
9248
9249   /* Emit the directory name table.  */
9250   idx_offset = dirs[0].length > 0 ? 1 : 0;
9251   for (i = 1 - idx_offset; i < ndirs; i++)
9252     dw2_asm_output_nstring (dirs[i].path,
9253                             dirs[i].length
9254                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9255                             "Directory Entry: %#x", i + idx_offset);
9256
9257   dw2_asm_output_data (1, 0, "End directory table");
9258
9259   /* We have to emit them in the order of emitted_number since that's
9260      used in the debug info generation.  To do this efficiently we
9261      generate a back-mapping of the indices first.  */
9262   backmap = XALLOCAVEC (int, numfiles);
9263   for (i = 0; i < numfiles; i++)
9264     backmap[files[i].file_idx->emitted_number - 1] = i;
9265
9266   /* Now write all the file names.  */
9267   for (i = 0; i < numfiles; i++)
9268     {
9269       int file_idx = backmap[i];
9270       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9271
9272 #ifdef VMS_DEBUGGING_INFO
9273 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9274
9275       /* Setting these fields can lead to debugger miscomparisons,
9276          but VMS Debug requires them to be set correctly.  */
9277
9278       int ver;
9279       long long cdt;
9280       long siz;
9281       int maxfilelen = strlen (files[file_idx].path)
9282                                + dirs[dir_idx].length
9283                                + MAX_VMS_VERSION_LEN + 1;
9284       char *filebuf = XALLOCAVEC (char, maxfilelen);
9285
9286       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9287       snprintf (filebuf, maxfilelen, "%s;%d",
9288                 files[file_idx].path + dirs[dir_idx].length, ver);
9289
9290       dw2_asm_output_nstring
9291         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9292
9293       /* Include directory index.  */
9294       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9295
9296       /* Modification time.  */
9297       dw2_asm_output_data_uleb128
9298         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9299           ? cdt : 0,
9300          NULL);
9301
9302       /* File length in bytes.  */
9303       dw2_asm_output_data_uleb128
9304         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9305           ? siz : 0,
9306          NULL);
9307 #else
9308       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9309                               "File Entry: %#x", (unsigned) i + 1);
9310
9311       /* Include directory index.  */
9312       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9313
9314       /* Modification time.  */
9315       dw2_asm_output_data_uleb128 (0, NULL);
9316
9317       /* File length in bytes.  */
9318       dw2_asm_output_data_uleb128 (0, NULL);
9319 #endif /* VMS_DEBUGGING_INFO */
9320     }
9321
9322   dw2_asm_output_data (1, 0, "End file name table");
9323 }
9324
9325
9326 /* Output one line number table into the .debug_line section.  */
9327
9328 static void
9329 output_one_line_info_table (dw_line_info_table *table)
9330 {
9331   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9332   unsigned int current_line = 1;
9333   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9334   dw_line_info_entry *ent;
9335   size_t i;
9336
9337   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9338     {
9339       switch (ent->opcode)
9340         {
9341         case LI_set_address:
9342           /* ??? Unfortunately, we have little choice here currently, and
9343              must always use the most general form.  GCC does not know the
9344              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9345              ports do have length attributes which will give an upper bound
9346              on the address range.  We could perhaps use length attributes
9347              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9348           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9349
9350           /* This can handle any delta.  This takes
9351              4+DWARF2_ADDR_SIZE bytes.  */
9352           dw2_asm_output_data (1, 0, "set address %s", line_label);
9353           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9354           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9355           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9356           break;
9357
9358         case LI_set_line:
9359           if (ent->val == current_line)
9360             {
9361               /* We still need to start a new row, so output a copy insn.  */
9362               dw2_asm_output_data (1, DW_LNS_copy,
9363                                    "copy line %u", current_line);
9364             }
9365           else
9366             {
9367               int line_offset = ent->val - current_line;
9368               int line_delta = line_offset - DWARF_LINE_BASE;
9369
9370               current_line = ent->val;
9371               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9372                 {
9373                   /* This can handle deltas from -10 to 234, using the current
9374                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9375                      This takes 1 byte.  */
9376                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9377                                        "line %u", current_line);
9378                 }
9379               else
9380                 {
9381                   /* This can handle any delta.  This takes at least 4 bytes,
9382                      depending on the value being encoded.  */
9383                   dw2_asm_output_data (1, DW_LNS_advance_line,
9384                                        "advance to line %u", current_line);
9385                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9386                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9387                 }
9388             }
9389           break;
9390
9391         case LI_set_file:
9392           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9393           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9394           break;
9395
9396         case LI_set_column:
9397           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9398           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9399           break;
9400
9401         case LI_negate_stmt:
9402           current_is_stmt = !current_is_stmt;
9403           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9404                                "is_stmt %d", current_is_stmt);
9405           break;
9406
9407         case LI_set_prologue_end:
9408           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9409                                "set prologue end");
9410           break;
9411           
9412         case LI_set_epilogue_begin:
9413           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9414                                "set epilogue begin");
9415           break;
9416
9417         case LI_set_discriminator:
9418           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9419           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9420           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9421           dw2_asm_output_data_uleb128 (ent->val, NULL);
9422           break;
9423         }
9424     }
9425
9426   /* Emit debug info for the address of the end of the table.  */
9427   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9428   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9429   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9430   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9431
9432   dw2_asm_output_data (1, 0, "end sequence");
9433   dw2_asm_output_data_uleb128 (1, NULL);
9434   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9435 }
9436
9437 /* Output the source line number correspondence information.  This
9438    information goes into the .debug_line section.  */
9439
9440 static void
9441 output_line_info (void)
9442 {
9443   char l1[20], l2[20], p1[20], p2[20];
9444   int ver = dwarf_version;
9445   bool saw_one = false;
9446   int opc;
9447
9448   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9449   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9450   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9451   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9452
9453   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9454     dw2_asm_output_data (4, 0xffffffff,
9455       "Initial length escape value indicating 64-bit DWARF extension");
9456   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9457                         "Length of Source Line Info");
9458   ASM_OUTPUT_LABEL (asm_out_file, l1);
9459
9460   dw2_asm_output_data (2, ver, "DWARF Version");
9461   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9462   ASM_OUTPUT_LABEL (asm_out_file, p1);
9463
9464   /* Define the architecture-dependent minimum instruction length (in bytes).
9465      In this implementation of DWARF, this field is used for information
9466      purposes only.  Since GCC generates assembly language, we have no
9467      a priori knowledge of how many instruction bytes are generated for each
9468      source line, and therefore can use only the DW_LNE_set_address and
9469      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9470      this as '1', which is "correct enough" for all architectures,
9471      and don't let the target override.  */
9472   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9473
9474   if (ver >= 4)
9475     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9476                          "Maximum Operations Per Instruction");
9477   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9478                        "Default is_stmt_start flag");
9479   dw2_asm_output_data (1, DWARF_LINE_BASE,
9480                        "Line Base Value (Special Opcodes)");
9481   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9482                        "Line Range Value (Special Opcodes)");
9483   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9484                        "Special Opcode Base");
9485
9486   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9487     {
9488       int n_op_args;
9489       switch (opc)
9490         {
9491         case DW_LNS_advance_pc:
9492         case DW_LNS_advance_line:
9493         case DW_LNS_set_file:
9494         case DW_LNS_set_column:
9495         case DW_LNS_fixed_advance_pc:
9496         case DW_LNS_set_isa:
9497           n_op_args = 1;
9498           break;
9499         default:
9500           n_op_args = 0;
9501           break;
9502         }
9503
9504       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9505                            opc, n_op_args);
9506     }
9507
9508   /* Write out the information about the files we use.  */
9509   output_file_names ();
9510   ASM_OUTPUT_LABEL (asm_out_file, p2);
9511
9512   if (separate_line_info)
9513     {
9514       dw_line_info_table *table;
9515       size_t i;
9516
9517       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9518         if (table->in_use)
9519           {
9520             output_one_line_info_table (table);
9521             saw_one = true;
9522           }
9523     }
9524   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9525     {
9526       output_one_line_info_table (cold_text_section_line_info);
9527       saw_one = true;
9528     }
9529
9530   /* ??? Some Darwin linkers crash on a .debug_line section with no
9531      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9532      sufficient -- the address column must also be initialized.
9533      Make sure to output at least one set_address/end_sequence pair,
9534      choosing .text since that section is always present.  */
9535   if (text_section_line_info->in_use || !saw_one)
9536     output_one_line_info_table (text_section_line_info);
9537
9538   /* Output the marker for the end of the line number info.  */
9539   ASM_OUTPUT_LABEL (asm_out_file, l2);
9540 }
9541 \f
9542 /* Given a pointer to a tree node for some base type, return a pointer to
9543    a DIE that describes the given type.
9544
9545    This routine must only be called for GCC type nodes that correspond to
9546    Dwarf base (fundamental) types.  */
9547
9548 static dw_die_ref
9549 base_type_die (tree type)
9550 {
9551   dw_die_ref base_type_result;
9552   enum dwarf_type encoding;
9553
9554   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9555     return 0;
9556
9557   /* If this is a subtype that should not be emitted as a subrange type,
9558      use the base type.  See subrange_type_for_debug_p.  */
9559   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9560     type = TREE_TYPE (type);
9561
9562   switch (TREE_CODE (type))
9563     {
9564     case INTEGER_TYPE:
9565       if ((dwarf_version >= 4 || !dwarf_strict)
9566           && TYPE_NAME (type)
9567           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9568           && DECL_IS_BUILTIN (TYPE_NAME (type))
9569           && DECL_NAME (TYPE_NAME (type)))
9570         {
9571           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9572           if (strcmp (name, "char16_t") == 0
9573               || strcmp (name, "char32_t") == 0)
9574             {
9575               encoding = DW_ATE_UTF;
9576               break;
9577             }
9578         }
9579       if (TYPE_STRING_FLAG (type))
9580         {
9581           if (TYPE_UNSIGNED (type))
9582             encoding = DW_ATE_unsigned_char;
9583           else
9584             encoding = DW_ATE_signed_char;
9585         }
9586       else if (TYPE_UNSIGNED (type))
9587         encoding = DW_ATE_unsigned;
9588       else
9589         encoding = DW_ATE_signed;
9590       break;
9591
9592     case REAL_TYPE:
9593       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9594         {
9595           if (dwarf_version >= 3 || !dwarf_strict)
9596             encoding = DW_ATE_decimal_float;
9597           else
9598             encoding = DW_ATE_lo_user;
9599         }
9600       else
9601         encoding = DW_ATE_float;
9602       break;
9603
9604     case FIXED_POINT_TYPE:
9605       if (!(dwarf_version >= 3 || !dwarf_strict))
9606         encoding = DW_ATE_lo_user;
9607       else if (TYPE_UNSIGNED (type))
9608         encoding = DW_ATE_unsigned_fixed;
9609       else
9610         encoding = DW_ATE_signed_fixed;
9611       break;
9612
9613       /* Dwarf2 doesn't know anything about complex ints, so use
9614          a user defined type for it.  */
9615     case COMPLEX_TYPE:
9616       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9617         encoding = DW_ATE_complex_float;
9618       else
9619         encoding = DW_ATE_lo_user;
9620       break;
9621
9622     case BOOLEAN_TYPE:
9623       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9624       encoding = DW_ATE_boolean;
9625       break;
9626
9627     default:
9628       /* No other TREE_CODEs are Dwarf fundamental types.  */
9629       gcc_unreachable ();
9630     }
9631
9632   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9633
9634   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9635                    int_size_in_bytes (type));
9636   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9637
9638   return base_type_result;
9639 }
9640
9641 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9642    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9643
9644 static inline int
9645 is_base_type (tree type)
9646 {
9647   switch (TREE_CODE (type))
9648     {
9649     case ERROR_MARK:
9650     case VOID_TYPE:
9651     case INTEGER_TYPE:
9652     case REAL_TYPE:
9653     case FIXED_POINT_TYPE:
9654     case COMPLEX_TYPE:
9655     case BOOLEAN_TYPE:
9656       return 1;
9657
9658     case ARRAY_TYPE:
9659     case RECORD_TYPE:
9660     case UNION_TYPE:
9661     case QUAL_UNION_TYPE:
9662     case ENUMERAL_TYPE:
9663     case FUNCTION_TYPE:
9664     case METHOD_TYPE:
9665     case POINTER_TYPE:
9666     case REFERENCE_TYPE:
9667     case NULLPTR_TYPE:
9668     case OFFSET_TYPE:
9669     case LANG_TYPE:
9670     case VECTOR_TYPE:
9671       return 0;
9672
9673     default:
9674       gcc_unreachable ();
9675     }
9676
9677   return 0;
9678 }
9679
9680 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9681    node, return the size in bits for the type if it is a constant, or else
9682    return the alignment for the type if the type's size is not constant, or
9683    else return BITS_PER_WORD if the type actually turns out to be an
9684    ERROR_MARK node.  */
9685
9686 static inline unsigned HOST_WIDE_INT
9687 simple_type_size_in_bits (const_tree type)
9688 {
9689   if (TREE_CODE (type) == ERROR_MARK)
9690     return BITS_PER_WORD;
9691   else if (TYPE_SIZE (type) == NULL_TREE)
9692     return 0;
9693   else if (host_integerp (TYPE_SIZE (type), 1))
9694     return tree_low_cst (TYPE_SIZE (type), 1);
9695   else
9696     return TYPE_ALIGN (type);
9697 }
9698
9699 /* Similarly, but return a double_int instead of UHWI.  */
9700
9701 static inline double_int
9702 double_int_type_size_in_bits (const_tree type)
9703 {
9704   if (TREE_CODE (type) == ERROR_MARK)
9705     return uhwi_to_double_int (BITS_PER_WORD);
9706   else if (TYPE_SIZE (type) == NULL_TREE)
9707     return double_int_zero;
9708   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9709     return tree_to_double_int (TYPE_SIZE (type));
9710   else
9711     return uhwi_to_double_int (TYPE_ALIGN (type));
9712 }
9713
9714 /*  Given a pointer to a tree node for a subrange type, return a pointer
9715     to a DIE that describes the given type.  */
9716
9717 static dw_die_ref
9718 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9719 {
9720   dw_die_ref subrange_die;
9721   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9722
9723   if (context_die == NULL)
9724     context_die = comp_unit_die ();
9725
9726   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9727
9728   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9729     {
9730       /* The size of the subrange type and its base type do not match,
9731          so we need to generate a size attribute for the subrange type.  */
9732       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9733     }
9734
9735   if (low)
9736     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9737   if (high)
9738     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9739
9740   return subrange_die;
9741 }
9742
9743 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9744    entry that chains various modifiers in front of the given type.  */
9745
9746 static dw_die_ref
9747 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9748                    dw_die_ref context_die)
9749 {
9750   enum tree_code code = TREE_CODE (type);
9751   dw_die_ref mod_type_die;
9752   dw_die_ref sub_die = NULL;
9753   tree item_type = NULL;
9754   tree qualified_type;
9755   tree name, low, high;
9756
9757   if (code == ERROR_MARK)
9758     return NULL;
9759
9760   /* See if we already have the appropriately qualified variant of
9761      this type.  */
9762   qualified_type
9763     = get_qualified_type (type,
9764                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9765                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9766
9767   if (qualified_type == sizetype
9768       && TYPE_NAME (qualified_type)
9769       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9770     {
9771       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9772
9773       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9774                            && TYPE_PRECISION (t)
9775                            == TYPE_PRECISION (qualified_type)
9776                            && TYPE_UNSIGNED (t)
9777                            == TYPE_UNSIGNED (qualified_type));
9778       qualified_type = t;
9779     }
9780
9781   /* If we do, then we can just use its DIE, if it exists.  */
9782   if (qualified_type)
9783     {
9784       mod_type_die = lookup_type_die (qualified_type);
9785       if (mod_type_die)
9786         return mod_type_die;
9787     }
9788
9789   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9790
9791   /* Handle C typedef types.  */
9792   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9793       && !DECL_ARTIFICIAL (name))
9794     {
9795       tree dtype = TREE_TYPE (name);
9796
9797       if (qualified_type == dtype)
9798         {
9799           /* For a named type, use the typedef.  */
9800           gen_type_die (qualified_type, context_die);
9801           return lookup_type_die (qualified_type);
9802         }
9803       else if (is_const_type < TYPE_READONLY (dtype)
9804                || is_volatile_type < TYPE_VOLATILE (dtype)
9805                || (is_const_type <= TYPE_READONLY (dtype)
9806                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9807                    && DECL_ORIGINAL_TYPE (name) != type))
9808         /* cv-unqualified version of named type.  Just use the unnamed
9809            type to which it refers.  */
9810         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9811                                   is_const_type, is_volatile_type,
9812                                   context_die);
9813       /* Else cv-qualified version of named type; fall through.  */
9814     }
9815
9816   if (is_const_type
9817       /* If both is_const_type and is_volatile_type, prefer the path
9818          which leads to a qualified type.  */
9819       && (!is_volatile_type
9820           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9821           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9822     {
9823       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9824       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9825     }
9826   else if (is_volatile_type)
9827     {
9828       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9829       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9830     }
9831   else if (code == POINTER_TYPE)
9832     {
9833       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9834       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9835                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9836       item_type = TREE_TYPE (type);
9837       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9838         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9839                          TYPE_ADDR_SPACE (item_type));
9840     }
9841   else if (code == REFERENCE_TYPE)
9842     {
9843       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9844         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9845                                 type);
9846       else
9847         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9848       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9849                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9850       item_type = TREE_TYPE (type);
9851       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9852         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9853                          TYPE_ADDR_SPACE (item_type));
9854     }
9855   else if (code == INTEGER_TYPE
9856            && TREE_TYPE (type) != NULL_TREE
9857            && subrange_type_for_debug_p (type, &low, &high))
9858     {
9859       mod_type_die = subrange_type_die (type, low, high, context_die);
9860       item_type = TREE_TYPE (type);
9861     }
9862   else if (is_base_type (type))
9863     mod_type_die = base_type_die (type);
9864   else
9865     {
9866       gen_type_die (type, context_die);
9867
9868       /* We have to get the type_main_variant here (and pass that to the
9869          `lookup_type_die' routine) because the ..._TYPE node we have
9870          might simply be a *copy* of some original type node (where the
9871          copy was created to help us keep track of typedef names) and
9872          that copy might have a different TYPE_UID from the original
9873          ..._TYPE node.  */
9874       if (TREE_CODE (type) != VECTOR_TYPE)
9875         return lookup_type_die (type_main_variant (type));
9876       else
9877         /* Vectors have the debugging information in the type,
9878            not the main variant.  */
9879         return lookup_type_die (type);
9880     }
9881
9882   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9883      don't output a DW_TAG_typedef, since there isn't one in the
9884      user's program; just attach a DW_AT_name to the type.
9885      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9886      if the base type already has the same name.  */
9887   if (name
9888       && ((TREE_CODE (name) != TYPE_DECL
9889            && (qualified_type == TYPE_MAIN_VARIANT (type)
9890                || (!is_const_type && !is_volatile_type)))
9891           || (TREE_CODE (name) == TYPE_DECL
9892               && TREE_TYPE (name) == qualified_type
9893               && DECL_NAME (name))))
9894     {
9895       if (TREE_CODE (name) == TYPE_DECL)
9896         /* Could just call add_name_and_src_coords_attributes here,
9897            but since this is a builtin type it doesn't have any
9898            useful source coordinates anyway.  */
9899         name = DECL_NAME (name);
9900       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9901       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9902       if (TYPE_ARTIFICIAL (type))
9903         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9904     }
9905   /* This probably indicates a bug.  */
9906   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9907     {
9908       name = TYPE_NAME (type);
9909       if (name
9910           && TREE_CODE (name) == TYPE_DECL)
9911         name = DECL_NAME (name);
9912       add_name_attribute (mod_type_die,
9913                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9914     }
9915
9916   if (qualified_type)
9917     equate_type_number_to_die (qualified_type, mod_type_die);
9918
9919   if (item_type)
9920     /* We must do this after the equate_type_number_to_die call, in case
9921        this is a recursive type.  This ensures that the modified_type_die
9922        recursion will terminate even if the type is recursive.  Recursive
9923        types are possible in Ada.  */
9924     sub_die = modified_type_die (item_type,
9925                                  TYPE_READONLY (item_type),
9926                                  TYPE_VOLATILE (item_type),
9927                                  context_die);
9928
9929   if (sub_die != NULL)
9930     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9931
9932   return mod_type_die;
9933 }
9934
9935 /* Generate DIEs for the generic parameters of T.
9936    T must be either a generic type or a generic function.
9937    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9938
9939 static void
9940 gen_generic_params_dies (tree t)
9941 {
9942   tree parms, args;
9943   int parms_num, i;
9944   dw_die_ref die = NULL;
9945
9946   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9947     return;
9948
9949   if (TYPE_P (t))
9950     die = lookup_type_die (t);
9951   else if (DECL_P (t))
9952     die = lookup_decl_die (t);
9953
9954   gcc_assert (die);
9955
9956   parms = lang_hooks.get_innermost_generic_parms (t);
9957   if (!parms)
9958     /* T has no generic parameter. It means T is neither a generic type
9959        or function. End of story.  */
9960     return;
9961
9962   parms_num = TREE_VEC_LENGTH (parms);
9963   args = lang_hooks.get_innermost_generic_args (t);
9964   for (i = 0; i < parms_num; i++)
9965     {
9966       tree parm, arg, arg_pack_elems;
9967
9968       parm = TREE_VEC_ELT (parms, i);
9969       arg = TREE_VEC_ELT (args, i);
9970       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9971       gcc_assert (parm && TREE_VALUE (parm) && arg);
9972
9973       if (parm && TREE_VALUE (parm) && arg)
9974         {
9975           /* If PARM represents a template parameter pack,
9976              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9977              by DW_TAG_template_*_parameter DIEs for the argument
9978              pack elements of ARG. Note that ARG would then be
9979              an argument pack.  */
9980           if (arg_pack_elems)
9981             template_parameter_pack_die (TREE_VALUE (parm),
9982                                          arg_pack_elems,
9983                                          die);
9984           else
9985             generic_parameter_die (TREE_VALUE (parm), arg,
9986                                    true /* Emit DW_AT_name */, die);
9987         }
9988     }
9989 }
9990
9991 /* Create and return a DIE for PARM which should be
9992    the representation of a generic type parameter.
9993    For instance, in the C++ front end, PARM would be a template parameter.
9994    ARG is the argument to PARM.
9995    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9996    name of the PARM.
9997    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9998    as a child node.  */
9999
10000 static dw_die_ref
10001 generic_parameter_die (tree parm, tree arg,
10002                        bool emit_name_p,
10003                        dw_die_ref parent_die)
10004 {
10005   dw_die_ref tmpl_die = NULL;
10006   const char *name = NULL;
10007
10008   if (!parm || !DECL_NAME (parm) || !arg)
10009     return NULL;
10010
10011   /* We support non-type generic parameters and arguments,
10012      type generic parameters and arguments, as well as
10013      generic generic parameters (a.k.a. template template parameters in C++)
10014      and arguments.  */
10015   if (TREE_CODE (parm) == PARM_DECL)
10016     /* PARM is a nontype generic parameter  */
10017     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10018   else if (TREE_CODE (parm) == TYPE_DECL)
10019     /* PARM is a type generic parameter.  */
10020     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10021   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10022     /* PARM is a generic generic parameter.
10023        Its DIE is a GNU extension. It shall have a
10024        DW_AT_name attribute to represent the name of the template template
10025        parameter, and a DW_AT_GNU_template_name attribute to represent the
10026        name of the template template argument.  */
10027     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10028                         parent_die, parm);
10029   else
10030     gcc_unreachable ();
10031
10032   if (tmpl_die)
10033     {
10034       tree tmpl_type;
10035
10036       /* If PARM is a generic parameter pack, it means we are
10037          emitting debug info for a template argument pack element.
10038          In other terms, ARG is a template argument pack element.
10039          In that case, we don't emit any DW_AT_name attribute for
10040          the die.  */
10041       if (emit_name_p)
10042         {
10043           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10044           gcc_assert (name);
10045           add_AT_string (tmpl_die, DW_AT_name, name);
10046         }
10047
10048       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10049         {
10050           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10051              TMPL_DIE should have a child DW_AT_type attribute that is set
10052              to the type of the argument to PARM, which is ARG.
10053              If PARM is a type generic parameter, TMPL_DIE should have a
10054              child DW_AT_type that is set to ARG.  */
10055           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10056           add_type_attribute (tmpl_die, tmpl_type, 0,
10057                               TREE_THIS_VOLATILE (tmpl_type),
10058                               parent_die);
10059         }
10060       else
10061         {
10062           /* So TMPL_DIE is a DIE representing a
10063              a generic generic template parameter, a.k.a template template
10064              parameter in C++ and arg is a template.  */
10065
10066           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10067              to the name of the argument.  */
10068           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10069           if (name)
10070             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10071         }
10072
10073       if (TREE_CODE (parm) == PARM_DECL)
10074         /* So PARM is a non-type generic parameter.
10075            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10076            attribute of TMPL_DIE which value represents the value
10077            of ARG.
10078            We must be careful here:
10079            The value of ARG might reference some function decls.
10080            We might currently be emitting debug info for a generic
10081            type and types are emitted before function decls, we don't
10082            know if the function decls referenced by ARG will actually be
10083            emitted after cgraph computations.
10084            So must defer the generation of the DW_AT_const_value to
10085            after cgraph is ready.  */
10086         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10087     }
10088
10089   return tmpl_die;
10090 }
10091
10092 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10093    PARM_PACK must be a template parameter pack. The returned DIE
10094    will be child DIE of PARENT_DIE.  */
10095
10096 static dw_die_ref
10097 template_parameter_pack_die (tree parm_pack,
10098                              tree parm_pack_args,
10099                              dw_die_ref parent_die)
10100 {
10101   dw_die_ref die;
10102   int j;
10103
10104   gcc_assert (parent_die && parm_pack);
10105
10106   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10107   add_name_and_src_coords_attributes (die, parm_pack);
10108   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10109     generic_parameter_die (parm_pack,
10110                            TREE_VEC_ELT (parm_pack_args, j),
10111                            false /* Don't emit DW_AT_name */,
10112                            die);
10113   return die;
10114 }
10115
10116 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10117    an enumerated type.  */
10118
10119 static inline int
10120 type_is_enum (const_tree type)
10121 {
10122   return TREE_CODE (type) == ENUMERAL_TYPE;
10123 }
10124
10125 /* Return the DBX register number described by a given RTL node.  */
10126
10127 static unsigned int
10128 dbx_reg_number (const_rtx rtl)
10129 {
10130   unsigned regno = REGNO (rtl);
10131
10132   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10133
10134 #ifdef LEAF_REG_REMAP
10135   if (current_function_uses_only_leaf_regs)
10136     {
10137       int leaf_reg = LEAF_REG_REMAP (regno);
10138       if (leaf_reg != -1)
10139         regno = (unsigned) leaf_reg;
10140     }
10141 #endif
10142
10143   return DBX_REGISTER_NUMBER (regno);
10144 }
10145
10146 /* Optionally add a DW_OP_piece term to a location description expression.
10147    DW_OP_piece is only added if the location description expression already
10148    doesn't end with DW_OP_piece.  */
10149
10150 static void
10151 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10152 {
10153   dw_loc_descr_ref loc;
10154
10155   if (*list_head != NULL)
10156     {
10157       /* Find the end of the chain.  */
10158       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10159         ;
10160
10161       if (loc->dw_loc_opc != DW_OP_piece)
10162         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10163     }
10164 }
10165
10166 /* Return a location descriptor that designates a machine register or
10167    zero if there is none.  */
10168
10169 static dw_loc_descr_ref
10170 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10171 {
10172   rtx regs;
10173
10174   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10175     return 0;
10176
10177   /* We only use "frame base" when we're sure we're talking about the
10178      post-prologue local stack frame.  We do this by *not* running
10179      register elimination until this point, and recognizing the special
10180      argument pointer and soft frame pointer rtx's.
10181      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10182   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10183       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10184     {
10185       dw_loc_descr_ref result = NULL;
10186
10187       if (dwarf_version >= 4 || !dwarf_strict)
10188         {
10189           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10190                                        initialized);
10191           if (result)
10192             add_loc_descr (&result,
10193                            new_loc_descr (DW_OP_stack_value, 0, 0));
10194         }
10195       return result;
10196     }
10197
10198   regs = targetm.dwarf_register_span (rtl);
10199
10200   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10201     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10202   else
10203     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10204 }
10205
10206 /* Return a location descriptor that designates a machine register for
10207    a given hard register number.  */
10208
10209 static dw_loc_descr_ref
10210 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10211 {
10212   dw_loc_descr_ref reg_loc_descr;
10213
10214   if (regno <= 31)
10215     reg_loc_descr
10216       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10217   else
10218     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10219
10220   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10221     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10222
10223   return reg_loc_descr;
10224 }
10225
10226 /* Given an RTL of a register, return a location descriptor that
10227    designates a value that spans more than one register.  */
10228
10229 static dw_loc_descr_ref
10230 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10231                              enum var_init_status initialized)
10232 {
10233   int nregs, size, i;
10234   unsigned reg;
10235   dw_loc_descr_ref loc_result = NULL;
10236
10237   reg = REGNO (rtl);
10238 #ifdef LEAF_REG_REMAP
10239   if (current_function_uses_only_leaf_regs)
10240     {
10241       int leaf_reg = LEAF_REG_REMAP (reg);
10242       if (leaf_reg != -1)
10243         reg = (unsigned) leaf_reg;
10244     }
10245 #endif
10246   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10247   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10248
10249   /* Simple, contiguous registers.  */
10250   if (regs == NULL_RTX)
10251     {
10252       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10253
10254       loc_result = NULL;
10255       while (nregs--)
10256         {
10257           dw_loc_descr_ref t;
10258
10259           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10260                                       VAR_INIT_STATUS_INITIALIZED);
10261           add_loc_descr (&loc_result, t);
10262           add_loc_descr_op_piece (&loc_result, size);
10263           ++reg;
10264         }
10265       return loc_result;
10266     }
10267
10268   /* Now onto stupid register sets in non contiguous locations.  */
10269
10270   gcc_assert (GET_CODE (regs) == PARALLEL);
10271
10272   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10273   loc_result = NULL;
10274
10275   for (i = 0; i < XVECLEN (regs, 0); ++i)
10276     {
10277       dw_loc_descr_ref t;
10278
10279       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10280                                   VAR_INIT_STATUS_INITIALIZED);
10281       add_loc_descr (&loc_result, t);
10282       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10283       add_loc_descr_op_piece (&loc_result, size);
10284     }
10285
10286   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10287     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10288   return loc_result;
10289 }
10290
10291 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10292
10293 /* Return a location descriptor that designates a constant i,
10294    as a compound operation from constant (i >> shift), constant shift
10295    and DW_OP_shl.  */
10296
10297 static dw_loc_descr_ref
10298 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10299 {
10300   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10301   add_loc_descr (&ret, int_loc_descriptor (shift));
10302   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10303   return ret;
10304 }
10305
10306 /* Return a location descriptor that designates a constant.  */
10307
10308 static dw_loc_descr_ref
10309 int_loc_descriptor (HOST_WIDE_INT i)
10310 {
10311   enum dwarf_location_atom op;
10312
10313   /* Pick the smallest representation of a constant, rather than just
10314      defaulting to the LEB encoding.  */
10315   if (i >= 0)
10316     {
10317       int clz = clz_hwi (i);
10318       int ctz = ctz_hwi (i);
10319       if (i <= 31)
10320         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10321       else if (i <= 0xff)
10322         op = DW_OP_const1u;
10323       else if (i <= 0xffff)
10324         op = DW_OP_const2u;
10325       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10326                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10327         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10328            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10329            while DW_OP_const4u is 5 bytes.  */
10330         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10331       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10332                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10333         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10334            while DW_OP_const4u is 5 bytes.  */
10335         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10336       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10337         op = DW_OP_const4u;
10338       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10339                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10340         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10341            while DW_OP_constu of constant >= 0x100000000 takes at least
10342            6 bytes.  */
10343         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10344       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10345                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10346                   >= HOST_BITS_PER_WIDE_INT)
10347         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10348            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10349            while DW_OP_constu takes in this case at least 6 bytes.  */
10350         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10351       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10352                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10353                && size_of_uleb128 (i) > 6)
10354         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10355         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10356       else
10357         op = DW_OP_constu;
10358     }
10359   else
10360     {
10361       if (i >= -0x80)
10362         op = DW_OP_const1s;
10363       else if (i >= -0x8000)
10364         op = DW_OP_const2s;
10365       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10366         {
10367           if (size_of_int_loc_descriptor (i) < 5)
10368             {
10369               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10370               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10371               return ret;
10372             }
10373           op = DW_OP_const4s;
10374         }
10375       else
10376         {
10377           if (size_of_int_loc_descriptor (i)
10378               < (unsigned long) 1 + size_of_sleb128 (i))
10379             {
10380               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10381               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10382               return ret;
10383             }
10384           op = DW_OP_consts;
10385         }
10386     }
10387
10388   return new_loc_descr (op, i, 0);
10389 }
10390
10391 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10392    without actually allocating it.  */
10393
10394 static unsigned long
10395 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10396 {
10397   return size_of_int_loc_descriptor (i >> shift)
10398          + size_of_int_loc_descriptor (shift)
10399          + 1;
10400 }
10401
10402 /* Return size_of_locs (int_loc_descriptor (i)) without
10403    actually allocating it.  */
10404
10405 static unsigned long
10406 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10407 {
10408   unsigned long s;
10409
10410   if (i >= 0)
10411     {
10412       int clz, ctz;
10413       if (i <= 31)
10414         return 1;
10415       else if (i <= 0xff)
10416         return 2;
10417       else if (i <= 0xffff)
10418         return 3;
10419       clz = clz_hwi (i);
10420       ctz = ctz_hwi (i);
10421       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10422           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10423         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10424                                                     - clz - 5);
10425       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10426                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10427         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10428                                                     - clz - 8);
10429       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10430         return 5;
10431       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10432       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10433           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10434         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10435                                                     - clz - 8);
10436       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10437                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10438         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10439                                                     - clz - 16);
10440       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10441                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10442                && s > 6)
10443         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10444                                                     - clz - 32);
10445       else
10446         return 1 + s;
10447     }
10448   else
10449     {
10450       if (i >= -0x80)
10451         return 2;
10452       else if (i >= -0x8000)
10453         return 3;
10454       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10455         {
10456           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10457             {
10458               s = size_of_int_loc_descriptor (-i) + 1;
10459               if (s < 5)
10460                 return s;
10461             }
10462           return 5;
10463         }
10464       else
10465         {
10466           unsigned long r = 1 + size_of_sleb128 (i);
10467           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10468             {
10469               s = size_of_int_loc_descriptor (-i) + 1;
10470               if (s < r)
10471                 return s;
10472             }
10473           return r;
10474         }
10475     }
10476 }
10477
10478 /* Return loc description representing "address" of integer value.
10479    This can appear only as toplevel expression.  */
10480
10481 static dw_loc_descr_ref
10482 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10483 {
10484   int litsize;
10485   dw_loc_descr_ref loc_result = NULL;
10486
10487   if (!(dwarf_version >= 4 || !dwarf_strict))
10488     return NULL;
10489
10490   litsize = size_of_int_loc_descriptor (i);
10491   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10492      is more compact.  For DW_OP_stack_value we need:
10493      litsize + 1 (DW_OP_stack_value)
10494      and for DW_OP_implicit_value:
10495      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10496   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10497     {
10498       loc_result = int_loc_descriptor (i);
10499       add_loc_descr (&loc_result,
10500                      new_loc_descr (DW_OP_stack_value, 0, 0));
10501       return loc_result;
10502     }
10503
10504   loc_result = new_loc_descr (DW_OP_implicit_value,
10505                               size, 0);
10506   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10507   loc_result->dw_loc_oprnd2.v.val_int = i;
10508   return loc_result;
10509 }
10510
10511 /* Return a location descriptor that designates a base+offset location.  */
10512
10513 static dw_loc_descr_ref
10514 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10515                  enum var_init_status initialized)
10516 {
10517   unsigned int regno;
10518   dw_loc_descr_ref result;
10519   dw_fde_ref fde = cfun->fde;
10520
10521   /* We only use "frame base" when we're sure we're talking about the
10522      post-prologue local stack frame.  We do this by *not* running
10523      register elimination until this point, and recognizing the special
10524      argument pointer and soft frame pointer rtx's.  */
10525   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10526     {
10527       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10528
10529       if (elim != reg)
10530         {
10531           if (GET_CODE (elim) == PLUS)
10532             {
10533               offset += INTVAL (XEXP (elim, 1));
10534               elim = XEXP (elim, 0);
10535             }
10536           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10537                        && (elim == hard_frame_pointer_rtx
10538                            || elim == stack_pointer_rtx))
10539                       || elim == (frame_pointer_needed
10540                                   ? hard_frame_pointer_rtx
10541                                   : stack_pointer_rtx));
10542
10543           /* If drap register is used to align stack, use frame
10544              pointer + offset to access stack variables.  If stack
10545              is aligned without drap, use stack pointer + offset to
10546              access stack variables.  */
10547           if (crtl->stack_realign_tried
10548               && reg == frame_pointer_rtx)
10549             {
10550               int base_reg
10551                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10552                                       ? HARD_FRAME_POINTER_REGNUM
10553                                       : REGNO (elim));
10554               return new_reg_loc_descr (base_reg, offset);
10555             }
10556
10557           gcc_assert (frame_pointer_fb_offset_valid);
10558           offset += frame_pointer_fb_offset;
10559           return new_loc_descr (DW_OP_fbreg, offset, 0);
10560         }
10561     }
10562
10563   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10564
10565   if (!optimize && fde
10566       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10567     {
10568       /* Use cfa+offset to represent the location of arguments passed
10569          on the stack when drap is used to align stack.
10570          Only do this when not optimizing, for optimized code var-tracking
10571          is supposed to track where the arguments live and the register
10572          used as vdrap or drap in some spot might be used for something
10573          else in other part of the routine.  */
10574       return new_loc_descr (DW_OP_fbreg, offset, 0);
10575     }
10576
10577   if (regno <= 31)
10578     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10579                             offset, 0);
10580   else
10581     result = new_loc_descr (DW_OP_bregx, regno, offset);
10582
10583   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10584     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10585
10586   return result;
10587 }
10588
10589 /* Return true if this RTL expression describes a base+offset calculation.  */
10590
10591 static inline int
10592 is_based_loc (const_rtx rtl)
10593 {
10594   return (GET_CODE (rtl) == PLUS
10595           && ((REG_P (XEXP (rtl, 0))
10596                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10597                && CONST_INT_P (XEXP (rtl, 1)))));
10598 }
10599
10600 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10601    failed.  */
10602
10603 static dw_loc_descr_ref
10604 tls_mem_loc_descriptor (rtx mem)
10605 {
10606   tree base;
10607   dw_loc_descr_ref loc_result;
10608
10609   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10610     return NULL;
10611
10612   base = get_base_address (MEM_EXPR (mem));
10613   if (base == NULL
10614       || TREE_CODE (base) != VAR_DECL
10615       || !DECL_THREAD_LOCAL_P (base))
10616     return NULL;
10617
10618   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10619   if (loc_result == NULL)
10620     return NULL;
10621
10622   if (MEM_OFFSET (mem))
10623     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10624
10625   return loc_result;
10626 }
10627
10628 /* Output debug info about reason why we failed to expand expression as dwarf
10629    expression.  */
10630
10631 static void
10632 expansion_failed (tree expr, rtx rtl, char const *reason)
10633 {
10634   if (dump_file && (dump_flags & TDF_DETAILS))
10635     {
10636       fprintf (dump_file, "Failed to expand as dwarf: ");
10637       if (expr)
10638         print_generic_expr (dump_file, expr, dump_flags);
10639       if (rtl)
10640         {
10641           fprintf (dump_file, "\n");
10642           print_rtl (dump_file, rtl);
10643         }
10644       fprintf (dump_file, "\nReason: %s\n", reason);
10645     }
10646 }
10647
10648 /* Helper function for const_ok_for_output, called either directly
10649    or via for_each_rtx.  */
10650
10651 static int
10652 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10653 {
10654   rtx rtl = *rtlp;
10655
10656   if (GET_CODE (rtl) == UNSPEC)
10657     {
10658       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10659          we can't express it in the debug info.  */
10660 #ifdef ENABLE_CHECKING
10661       /* Don't complain about TLS UNSPECs, those are just too hard to
10662          delegitimize.  */
10663       if (XVECLEN (rtl, 0) != 1
10664           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10665           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10666           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10667           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10668         inform (current_function_decl
10669                 ? DECL_SOURCE_LOCATION (current_function_decl)
10670                 : UNKNOWN_LOCATION,
10671 #if NUM_UNSPEC_VALUES > 0
10672                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10673                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10674                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10675                 XINT (rtl, 1));
10676 #else
10677                 "non-delegitimized UNSPEC %d found in variable location",
10678                 XINT (rtl, 1));
10679 #endif
10680 #endif
10681       expansion_failed (NULL_TREE, rtl,
10682                         "UNSPEC hasn't been delegitimized.\n");
10683       return 1;
10684     }
10685
10686   if (targetm.const_not_ok_for_debug_p (rtl))
10687     {
10688       expansion_failed (NULL_TREE, rtl,
10689                         "Expression rejected for debug by the backend.\n");
10690       return 1;
10691     }
10692
10693   if (GET_CODE (rtl) != SYMBOL_REF)
10694     return 0;
10695
10696   if (CONSTANT_POOL_ADDRESS_P (rtl))
10697     {
10698       bool marked;
10699       get_pool_constant_mark (rtl, &marked);
10700       /* If all references to this pool constant were optimized away,
10701          it was not output and thus we can't represent it.  */
10702       if (!marked)
10703         {
10704           expansion_failed (NULL_TREE, rtl,
10705                             "Constant was removed from constant pool.\n");
10706           return 1;
10707         }
10708     }
10709
10710   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10711     return 1;
10712
10713   /* Avoid references to external symbols in debug info, on several targets
10714      the linker might even refuse to link when linking a shared library,
10715      and in many other cases the relocations for .debug_info/.debug_loc are
10716      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10717      to be defined within the same shared library or executable are fine.  */
10718   if (SYMBOL_REF_EXTERNAL_P (rtl))
10719     {
10720       tree decl = SYMBOL_REF_DECL (rtl);
10721
10722       if (decl == NULL || !targetm.binds_local_p (decl))
10723         {
10724           expansion_failed (NULL_TREE, rtl,
10725                             "Symbol not defined in current TU.\n");
10726           return 1;
10727         }
10728     }
10729
10730   return 0;
10731 }
10732
10733 /* Return true if constant RTL can be emitted in DW_OP_addr or
10734    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10735    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10736
10737 static bool
10738 const_ok_for_output (rtx rtl)
10739 {
10740   if (GET_CODE (rtl) == SYMBOL_REF)
10741     return const_ok_for_output_1 (&rtl, NULL) == 0;
10742
10743   if (GET_CODE (rtl) == CONST)
10744     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10745
10746   return true;
10747 }
10748
10749 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10750    if possible, NULL otherwise.  */
10751
10752 static dw_die_ref
10753 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10754 {
10755   dw_die_ref type_die;
10756   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10757
10758   if (type == NULL)
10759     return NULL;
10760   switch (TREE_CODE (type))
10761     {
10762     case INTEGER_TYPE:
10763     case REAL_TYPE:
10764       break;
10765     default:
10766       return NULL;
10767     }
10768   type_die = lookup_type_die (type);
10769   if (!type_die)
10770     type_die = modified_type_die (type, false, false, comp_unit_die ());
10771   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10772     return NULL;
10773   return type_die;
10774 }
10775
10776 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10777    type matching MODE, or, if MODE is narrower than or as wide as
10778    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10779    possible.  */
10780
10781 static dw_loc_descr_ref
10782 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10783 {
10784   enum machine_mode outer_mode = mode;
10785   dw_die_ref type_die;
10786   dw_loc_descr_ref cvt;
10787
10788   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10789     {
10790       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10791       return op;
10792     }
10793   type_die = base_type_for_mode (outer_mode, 1);
10794   if (type_die == NULL)
10795     return NULL;
10796   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10797   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10798   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10799   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10800   add_loc_descr (&op, cvt);
10801   return op;
10802 }
10803
10804 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10805
10806 static dw_loc_descr_ref
10807 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10808                         dw_loc_descr_ref op1)
10809 {
10810   dw_loc_descr_ref ret = op0;
10811   add_loc_descr (&ret, op1);
10812   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10813   if (STORE_FLAG_VALUE != 1)
10814     {
10815       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10816       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10817     }
10818   return ret;
10819 }
10820
10821 /* Return location descriptor for signed comparison OP RTL.  */
10822
10823 static dw_loc_descr_ref
10824 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10825                          enum machine_mode mem_mode)
10826 {
10827   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10828   dw_loc_descr_ref op0, op1;
10829   int shift;
10830
10831   if (op_mode == VOIDmode)
10832     op_mode = GET_MODE (XEXP (rtl, 1));
10833   if (op_mode == VOIDmode)
10834     return NULL;
10835
10836   if (dwarf_strict
10837       && (GET_MODE_CLASS (op_mode) != MODE_INT
10838           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10839     return NULL;
10840
10841   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10842                             VAR_INIT_STATUS_INITIALIZED);
10843   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10844                             VAR_INIT_STATUS_INITIALIZED);
10845
10846   if (op0 == NULL || op1 == NULL)
10847     return NULL;
10848
10849   if (GET_MODE_CLASS (op_mode) != MODE_INT
10850       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10851     return compare_loc_descriptor (op, op0, op1);
10852
10853   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10854     {
10855       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10856       dw_loc_descr_ref cvt;
10857
10858       if (type_die == NULL)
10859         return NULL;
10860       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10861       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10862       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10863       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10864       add_loc_descr (&op0, cvt);
10865       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10866       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10867       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10868       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10869       add_loc_descr (&op1, cvt);
10870       return compare_loc_descriptor (op, op0, op1);
10871     }
10872
10873   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10874   /* For eq/ne, if the operands are known to be zero-extended,
10875      there is no need to do the fancy shifting up.  */
10876   if (op == DW_OP_eq || op == DW_OP_ne)
10877     {
10878       dw_loc_descr_ref last0, last1;
10879       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10880         ;
10881       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10882         ;
10883       /* deref_size zero extends, and for constants we can check
10884          whether they are zero extended or not.  */
10885       if (((last0->dw_loc_opc == DW_OP_deref_size
10886             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10887            || (CONST_INT_P (XEXP (rtl, 0))
10888                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10889                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10890           && ((last1->dw_loc_opc == DW_OP_deref_size
10891                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10892               || (CONST_INT_P (XEXP (rtl, 1))
10893                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10894                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10895         return compare_loc_descriptor (op, op0, op1);
10896
10897       /* EQ/NE comparison against constant in narrower type than
10898          DWARF2_ADDR_SIZE can be performed either as
10899          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10900          DW_OP_{eq,ne}
10901          or
10902          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10903          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10904       if (CONST_INT_P (XEXP (rtl, 1))
10905           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10906           && (size_of_int_loc_descriptor (shift) + 1
10907               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10908               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10909                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10910                                                & GET_MODE_MASK (op_mode))))
10911         {
10912           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10913           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10914           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10915                                     & GET_MODE_MASK (op_mode));
10916           return compare_loc_descriptor (op, op0, op1);
10917         }
10918     }
10919   add_loc_descr (&op0, int_loc_descriptor (shift));
10920   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10921   if (CONST_INT_P (XEXP (rtl, 1)))
10922     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10923   else
10924     {
10925       add_loc_descr (&op1, int_loc_descriptor (shift));
10926       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10927     }
10928   return compare_loc_descriptor (op, op0, op1);
10929 }
10930
10931 /* Return location descriptor for unsigned comparison OP RTL.  */
10932
10933 static dw_loc_descr_ref
10934 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10935                          enum machine_mode mem_mode)
10936 {
10937   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10938   dw_loc_descr_ref op0, op1;
10939
10940   if (op_mode == VOIDmode)
10941     op_mode = GET_MODE (XEXP (rtl, 1));
10942   if (op_mode == VOIDmode)
10943     return NULL;
10944   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10945     return NULL;
10946
10947   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10948     return NULL;
10949
10950   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10951                             VAR_INIT_STATUS_INITIALIZED);
10952   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10953                             VAR_INIT_STATUS_INITIALIZED);
10954
10955   if (op0 == NULL || op1 == NULL)
10956     return NULL;
10957
10958   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10959     {
10960       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10961       dw_loc_descr_ref last0, last1;
10962       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10963         ;
10964       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10965         ;
10966       if (CONST_INT_P (XEXP (rtl, 0)))
10967         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10968       /* deref_size zero extends, so no need to mask it again.  */
10969       else if (last0->dw_loc_opc != DW_OP_deref_size
10970                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10971         {
10972           add_loc_descr (&op0, int_loc_descriptor (mask));
10973           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10974         }
10975       if (CONST_INT_P (XEXP (rtl, 1)))
10976         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10977       /* deref_size zero extends, so no need to mask it again.  */
10978       else if (last1->dw_loc_opc != DW_OP_deref_size
10979                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10980         {
10981           add_loc_descr (&op1, int_loc_descriptor (mask));
10982           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10983         }
10984     }
10985   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10986     {
10987       HOST_WIDE_INT bias = 1;
10988       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10989       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10990       if (CONST_INT_P (XEXP (rtl, 1)))
10991         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10992                                   + INTVAL (XEXP (rtl, 1)));
10993       else
10994         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10995                                             bias, 0));
10996     }
10997   return compare_loc_descriptor (op, op0, op1);
10998 }
10999
11000 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11001
11002 static dw_loc_descr_ref
11003 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11004                        enum machine_mode mem_mode)
11005 {
11006   enum dwarf_location_atom op;
11007   dw_loc_descr_ref op0, op1, ret;
11008   dw_loc_descr_ref bra_node, drop_node;
11009
11010   if (dwarf_strict
11011       && (GET_MODE_CLASS (mode) != MODE_INT
11012           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11013     return NULL;
11014
11015   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11016                             VAR_INIT_STATUS_INITIALIZED);
11017   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11018                             VAR_INIT_STATUS_INITIALIZED);
11019
11020   if (op0 == NULL || op1 == NULL)
11021     return NULL;
11022
11023   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11024   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11025   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11026   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11027     {
11028       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11029         {
11030           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11031           add_loc_descr (&op0, int_loc_descriptor (mask));
11032           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11033           add_loc_descr (&op1, int_loc_descriptor (mask));
11034           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11035         }
11036       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11037         {
11038           HOST_WIDE_INT bias = 1;
11039           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11040           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11041           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11042         }
11043     }
11044   else if (GET_MODE_CLASS (mode) == MODE_INT
11045            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11046     {
11047       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11048       add_loc_descr (&op0, int_loc_descriptor (shift));
11049       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11050       add_loc_descr (&op1, int_loc_descriptor (shift));
11051       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11052     }
11053   else if (GET_MODE_CLASS (mode) == MODE_INT
11054            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11055     {
11056       dw_die_ref type_die = base_type_for_mode (mode, 0);
11057       dw_loc_descr_ref cvt;
11058       if (type_die == NULL)
11059         return NULL;
11060       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11061       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11062       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11063       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11064       add_loc_descr (&op0, cvt);
11065       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11066       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11067       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11068       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11069       add_loc_descr (&op1, cvt);
11070     }
11071
11072   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11073     op = DW_OP_lt;
11074   else
11075     op = DW_OP_gt;
11076   ret = op0;
11077   add_loc_descr (&ret, op1);
11078   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11079   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11080   add_loc_descr (&ret, bra_node);
11081   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11082   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11083   add_loc_descr (&ret, drop_node);
11084   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11085   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11086   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11087       && GET_MODE_CLASS (mode) == MODE_INT
11088       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11089     ret = convert_descriptor_to_mode (mode, ret);
11090   return ret;
11091 }
11092
11093 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11094    but after converting arguments to type_die, afterwards
11095    convert back to unsigned.  */
11096
11097 static dw_loc_descr_ref
11098 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11099              enum machine_mode mode, enum machine_mode mem_mode)
11100 {
11101   dw_loc_descr_ref cvt, op0, op1;
11102
11103   if (type_die == NULL)
11104     return NULL;
11105   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11106                             VAR_INIT_STATUS_INITIALIZED);
11107   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11108                             VAR_INIT_STATUS_INITIALIZED);
11109   if (op0 == NULL || op1 == NULL)
11110     return NULL;
11111   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11112   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11113   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11114   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11115   add_loc_descr (&op0, cvt);
11116   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11117   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11118   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11119   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11120   add_loc_descr (&op1, cvt);
11121   add_loc_descr (&op0, op1);
11122   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11123   return convert_descriptor_to_mode (mode, op0);
11124 }
11125
11126 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11127    const0 is DW_OP_lit0 or corresponding typed constant,
11128    const1 is DW_OP_lit1 or corresponding typed constant
11129    and constMSB is constant with just the MSB bit set
11130    for the mode):
11131        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11132    L1: const0 DW_OP_swap
11133    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11134        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11135    L3: DW_OP_drop
11136    L4: DW_OP_nop
11137
11138    CTZ is similar:
11139        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11140    L1: const0 DW_OP_swap
11141    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11142        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11143    L3: DW_OP_drop
11144    L4: DW_OP_nop
11145
11146    FFS is similar:
11147        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11148    L1: const1 DW_OP_swap
11149    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11150        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11151    L3: DW_OP_drop
11152    L4: DW_OP_nop  */
11153
11154 static dw_loc_descr_ref
11155 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11156                     enum machine_mode mem_mode)
11157 {
11158   dw_loc_descr_ref op0, ret, tmp;
11159   HOST_WIDE_INT valv;
11160   dw_loc_descr_ref l1jump, l1label;
11161   dw_loc_descr_ref l2jump, l2label;
11162   dw_loc_descr_ref l3jump, l3label;
11163   dw_loc_descr_ref l4jump, l4label;
11164   rtx msb;
11165
11166   if (GET_MODE_CLASS (mode) != MODE_INT
11167       || GET_MODE (XEXP (rtl, 0)) != mode
11168       || (GET_CODE (rtl) == CLZ
11169           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11170     return NULL;
11171
11172   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11173                             VAR_INIT_STATUS_INITIALIZED);
11174   if (op0 == NULL)
11175     return NULL;
11176   ret = op0;
11177   if (GET_CODE (rtl) == CLZ)
11178     {
11179       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11180         valv = GET_MODE_BITSIZE (mode);
11181     }
11182   else if (GET_CODE (rtl) == FFS)
11183     valv = 0;
11184   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11185     valv = GET_MODE_BITSIZE (mode);
11186   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11187   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11188   add_loc_descr (&ret, l1jump);
11189   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11190   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11191                             VAR_INIT_STATUS_INITIALIZED);
11192   if (tmp == NULL)
11193     return NULL;
11194   add_loc_descr (&ret, tmp);
11195   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11196   add_loc_descr (&ret, l4jump);
11197   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11198                                 ? const1_rtx : const0_rtx,
11199                                 mode, mem_mode,
11200                                 VAR_INIT_STATUS_INITIALIZED);
11201   if (l1label == NULL)
11202     return NULL;
11203   add_loc_descr (&ret, l1label);
11204   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11205   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11206   add_loc_descr (&ret, l2label);
11207   if (GET_CODE (rtl) != CLZ)
11208     msb = const1_rtx;
11209   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11210     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11211                    << (GET_MODE_BITSIZE (mode) - 1));
11212   else
11213     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11214                                   << (GET_MODE_BITSIZE (mode)
11215                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11216   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11217     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11218                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11219                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11220   else
11221     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11222                               VAR_INIT_STATUS_INITIALIZED);
11223   if (tmp == NULL)
11224     return NULL;
11225   add_loc_descr (&ret, tmp);
11226   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11227   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11228   add_loc_descr (&ret, l3jump);
11229   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11230                             VAR_INIT_STATUS_INITIALIZED);
11231   if (tmp == NULL)
11232     return NULL;
11233   add_loc_descr (&ret, tmp);
11234   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11235                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11236   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11237   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11238   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11239   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11240   add_loc_descr (&ret, l2jump);
11241   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11242   add_loc_descr (&ret, l3label);
11243   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11244   add_loc_descr (&ret, l4label);
11245   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11246   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11247   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11248   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11249   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11250   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11251   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11252   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11253   return ret;
11254 }
11255
11256 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11257    const1 is DW_OP_lit1 or corresponding typed constant):
11258        const0 DW_OP_swap
11259    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11260        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11261    L2: DW_OP_drop
11262
11263    PARITY is similar:
11264    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11265        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11266    L2: DW_OP_drop  */
11267
11268 static dw_loc_descr_ref
11269 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11270                          enum machine_mode mem_mode)
11271 {
11272   dw_loc_descr_ref op0, ret, tmp;
11273   dw_loc_descr_ref l1jump, l1label;
11274   dw_loc_descr_ref l2jump, l2label;
11275
11276   if (GET_MODE_CLASS (mode) != MODE_INT
11277       || GET_MODE (XEXP (rtl, 0)) != mode)
11278     return NULL;
11279
11280   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11281                             VAR_INIT_STATUS_INITIALIZED);
11282   if (op0 == NULL)
11283     return NULL;
11284   ret = op0;
11285   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11286                             VAR_INIT_STATUS_INITIALIZED);
11287   if (tmp == NULL)
11288     return NULL;
11289   add_loc_descr (&ret, tmp);
11290   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11291   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11292   add_loc_descr (&ret, l1label);
11293   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11294   add_loc_descr (&ret, l2jump);
11295   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11296   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11297   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11298                             VAR_INIT_STATUS_INITIALIZED);
11299   if (tmp == NULL)
11300     return NULL;
11301   add_loc_descr (&ret, tmp);
11302   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11303   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11304                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11305   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11306   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11307                             VAR_INIT_STATUS_INITIALIZED);
11308   add_loc_descr (&ret, tmp);
11309   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11310   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11311   add_loc_descr (&ret, l1jump);
11312   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11313   add_loc_descr (&ret, l2label);
11314   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11315   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11316   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11317   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11318   return ret;
11319 }
11320
11321 /* BSWAP (constS is initial shift count, either 56 or 24):
11322        constS const0
11323    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11324        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11325        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11326        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11327    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11328
11329 static dw_loc_descr_ref
11330 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11331                       enum machine_mode mem_mode)
11332 {
11333   dw_loc_descr_ref op0, ret, tmp;
11334   dw_loc_descr_ref l1jump, l1label;
11335   dw_loc_descr_ref l2jump, l2label;
11336
11337   if (GET_MODE_CLASS (mode) != MODE_INT
11338       || BITS_PER_UNIT != 8
11339       || (GET_MODE_BITSIZE (mode) != 32
11340           &&  GET_MODE_BITSIZE (mode) != 64))
11341     return NULL;
11342
11343   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11344                             VAR_INIT_STATUS_INITIALIZED);
11345   if (op0 == NULL)
11346     return NULL;
11347
11348   ret = op0;
11349   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11350                             mode, mem_mode,
11351                             VAR_INIT_STATUS_INITIALIZED);
11352   if (tmp == NULL)
11353     return NULL;
11354   add_loc_descr (&ret, tmp);
11355   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11356                             VAR_INIT_STATUS_INITIALIZED);
11357   if (tmp == NULL)
11358     return NULL;
11359   add_loc_descr (&ret, tmp);
11360   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11361   add_loc_descr (&ret, l1label);
11362   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11363                             mode, mem_mode,
11364                             VAR_INIT_STATUS_INITIALIZED);
11365   add_loc_descr (&ret, tmp);
11366   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11367   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11368   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11369   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11370                             VAR_INIT_STATUS_INITIALIZED);
11371   if (tmp == NULL)
11372     return NULL;
11373   add_loc_descr (&ret, tmp);
11374   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11375   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11376   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11377   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11378   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11379   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11380   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11381                             VAR_INIT_STATUS_INITIALIZED);
11382   add_loc_descr (&ret, tmp);
11383   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11384   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11385   add_loc_descr (&ret, l2jump);
11386   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11387                             VAR_INIT_STATUS_INITIALIZED);
11388   add_loc_descr (&ret, tmp);
11389   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11390   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11391   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11392   add_loc_descr (&ret, l1jump);
11393   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11394   add_loc_descr (&ret, l2label);
11395   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11396   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11397   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11398   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11399   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11400   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11401   return ret;
11402 }
11403
11404 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11405    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11406    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11407    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11408
11409    ROTATERT is similar:
11410    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11411    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11412    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11413
11414 static dw_loc_descr_ref
11415 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11416                        enum machine_mode mem_mode)
11417 {
11418   rtx rtlop1 = XEXP (rtl, 1);
11419   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11420   int i;
11421
11422   if (GET_MODE_CLASS (mode) != MODE_INT)
11423     return NULL;
11424
11425   if (GET_MODE (rtlop1) != VOIDmode
11426       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11427     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11428   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11429                             VAR_INIT_STATUS_INITIALIZED);
11430   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11431                             VAR_INIT_STATUS_INITIALIZED);
11432   if (op0 == NULL || op1 == NULL)
11433     return NULL;
11434   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11435     for (i = 0; i < 2; i++)
11436       {
11437         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11438           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11439                                         mode, mem_mode,
11440                                         VAR_INIT_STATUS_INITIALIZED);
11441         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11442           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11443                                    ? DW_OP_const4u
11444                                    : HOST_BITS_PER_WIDE_INT == 64
11445                                    ? DW_OP_const8u : DW_OP_constu,
11446                                    GET_MODE_MASK (mode), 0);
11447         else
11448           mask[i] = NULL;
11449         if (mask[i] == NULL)
11450           return NULL;
11451         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11452       }
11453   ret = op0;
11454   add_loc_descr (&ret, op1);
11455   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11456   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11457   if (GET_CODE (rtl) == ROTATERT)
11458     {
11459       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11460       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11461                                           GET_MODE_BITSIZE (mode), 0));
11462     }
11463   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11464   if (mask[0] != NULL)
11465     add_loc_descr (&ret, mask[0]);
11466   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11467   if (mask[1] != NULL)
11468     {
11469       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11470       add_loc_descr (&ret, mask[1]);
11471       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11472     }
11473   if (GET_CODE (rtl) == ROTATE)
11474     {
11475       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11476       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11477                                           GET_MODE_BITSIZE (mode), 0));
11478     }
11479   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11480   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11481   return ret;
11482 }
11483
11484 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11485    for DEBUG_PARAMETER_REF RTL.  */
11486
11487 static dw_loc_descr_ref
11488 parameter_ref_descriptor (rtx rtl)
11489 {
11490   dw_loc_descr_ref ret;
11491   dw_die_ref ref;
11492
11493   if (dwarf_strict)
11494     return NULL;
11495   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11496   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11497   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11498   if (ref)
11499     {
11500       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11501       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11502       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11503     }
11504   else
11505     {
11506       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11507       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11508     }
11509   return ret;
11510 }
11511
11512 /* Helper function to get mode of MEM's address.  */
11513
11514 enum machine_mode
11515 get_address_mode (rtx mem)
11516 {
11517   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11518   if (mode != VOIDmode)
11519     return mode;
11520   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11521 }
11522
11523 /* The following routine converts the RTL for a variable or parameter
11524    (resident in memory) into an equivalent Dwarf representation of a
11525    mechanism for getting the address of that same variable onto the top of a
11526    hypothetical "address evaluation" stack.
11527
11528    When creating memory location descriptors, we are effectively transforming
11529    the RTL for a memory-resident object into its Dwarf postfix expression
11530    equivalent.  This routine recursively descends an RTL tree, turning
11531    it into Dwarf postfix code as it goes.
11532
11533    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11534
11535    MEM_MODE is the mode of the memory reference, needed to handle some
11536    autoincrement addressing modes.
11537
11538    Return 0 if we can't represent the location.  */
11539
11540 dw_loc_descr_ref
11541 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11542                     enum machine_mode mem_mode,
11543                     enum var_init_status initialized)
11544 {
11545   dw_loc_descr_ref mem_loc_result = NULL;
11546   enum dwarf_location_atom op;
11547   dw_loc_descr_ref op0, op1;
11548
11549   if (mode == VOIDmode)
11550     mode = GET_MODE (rtl);
11551
11552   /* Note that for a dynamically sized array, the location we will generate a
11553      description of here will be the lowest numbered location which is
11554      actually within the array.  That's *not* necessarily the same as the
11555      zeroth element of the array.  */
11556
11557   rtl = targetm.delegitimize_address (rtl);
11558
11559   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11560     return NULL;
11561
11562   switch (GET_CODE (rtl))
11563     {
11564     case POST_INC:
11565     case POST_DEC:
11566     case POST_MODIFY:
11567       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11568
11569     case SUBREG:
11570       /* The case of a subreg may arise when we have a local (register)
11571          variable or a formal (register) parameter which doesn't quite fill
11572          up an entire register.  For now, just assume that it is
11573          legitimate to make the Dwarf info refer to the whole register which
11574          contains the given subreg.  */
11575       if (!subreg_lowpart_p (rtl))
11576         break;
11577       if (GET_MODE_CLASS (mode) == MODE_INT
11578           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11579           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11580 #ifdef POINTERS_EXTEND_UNSIGNED
11581               || (mode == Pmode && mem_mode != VOIDmode)
11582 #endif
11583              )
11584           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11585         {
11586           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11587                                                GET_MODE (SUBREG_REG (rtl)),
11588                                                mem_mode, initialized);
11589           break;
11590         }
11591       if (dwarf_strict)
11592         break;
11593       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11594         break;
11595       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11596           && (GET_MODE_CLASS (mode) != MODE_INT
11597               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11598         break;
11599       else
11600         {
11601           dw_die_ref type_die;
11602           dw_loc_descr_ref cvt;
11603
11604           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11605                                                GET_MODE (SUBREG_REG (rtl)),
11606                                                mem_mode, initialized);
11607           if (mem_loc_result == NULL)
11608             break;
11609           type_die = base_type_for_mode (mode,
11610                                          GET_MODE_CLASS (mode) == MODE_INT);
11611           if (type_die == NULL)
11612             {
11613               mem_loc_result = NULL;
11614               break;
11615             }
11616           if (GET_MODE_SIZE (mode)
11617               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11618             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11619           else
11620             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11621           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11622           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11623           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11624           add_loc_descr (&mem_loc_result, cvt);
11625         }
11626       break;
11627
11628     case REG:
11629       if (GET_MODE_CLASS (mode) != MODE_INT
11630           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11631 #ifdef POINTERS_EXTEND_UNSIGNED
11632               && (mode != Pmode || mem_mode == VOIDmode)
11633 #endif
11634               ))
11635         {
11636           dw_die_ref type_die;
11637
11638           if (dwarf_strict)
11639             break;
11640           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11641             break;
11642           type_die = base_type_for_mode (mode,
11643                                          GET_MODE_CLASS (mode) == MODE_INT);
11644           if (type_die == NULL)
11645             break;
11646           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11647                                           dbx_reg_number (rtl), 0);
11648           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11649           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11650           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11651           break;
11652         }
11653       /* Whenever a register number forms a part of the description of the
11654          method for calculating the (dynamic) address of a memory resident
11655          object, DWARF rules require the register number be referred to as
11656          a "base register".  This distinction is not based in any way upon
11657          what category of register the hardware believes the given register
11658          belongs to.  This is strictly DWARF terminology we're dealing with
11659          here. Note that in cases where the location of a memory-resident
11660          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11661          OP_CONST (0)) the actual DWARF location descriptor that we generate
11662          may just be OP_BASEREG (basereg).  This may look deceptively like
11663          the object in question was allocated to a register (rather than in
11664          memory) so DWARF consumers need to be aware of the subtle
11665          distinction between OP_REG and OP_BASEREG.  */
11666       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11667         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11668       else if (stack_realign_drap
11669                && crtl->drap_reg
11670                && crtl->args.internal_arg_pointer == rtl
11671                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11672         {
11673           /* If RTL is internal_arg_pointer, which has been optimized
11674              out, use DRAP instead.  */
11675           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11676                                             VAR_INIT_STATUS_INITIALIZED);
11677         }
11678       break;
11679
11680     case SIGN_EXTEND:
11681     case ZERO_EXTEND:
11682       if (GET_MODE_CLASS (mode) != MODE_INT)
11683         break;
11684       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11685                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11686       if (op0 == 0)
11687         break;
11688       else if (GET_CODE (rtl) == ZERO_EXTEND
11689                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11690                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11691                   < HOST_BITS_PER_WIDE_INT
11692                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11693                   to expand zero extend as two shifts instead of
11694                   masking.  */
11695                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11696         {
11697           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11698           mem_loc_result = op0;
11699           add_loc_descr (&mem_loc_result,
11700                          int_loc_descriptor (GET_MODE_MASK (imode)));
11701           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11702         }
11703       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11704         {
11705           int shift = DWARF2_ADDR_SIZE
11706                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11707           shift *= BITS_PER_UNIT;
11708           if (GET_CODE (rtl) == SIGN_EXTEND)
11709             op = DW_OP_shra;
11710           else
11711             op = DW_OP_shr;
11712           mem_loc_result = op0;
11713           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11714           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11715           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11716           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11717         }
11718       else if (!dwarf_strict)
11719         {
11720           dw_die_ref type_die1, type_die2;
11721           dw_loc_descr_ref cvt;
11722
11723           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11724                                           GET_CODE (rtl) == ZERO_EXTEND);
11725           if (type_die1 == NULL)
11726             break;
11727           type_die2 = base_type_for_mode (mode, 1);
11728           if (type_die2 == NULL)
11729             break;
11730           mem_loc_result = op0;
11731           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11732           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11733           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11734           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11735           add_loc_descr (&mem_loc_result, cvt);
11736           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11737           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11738           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11739           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11740           add_loc_descr (&mem_loc_result, cvt);
11741         }
11742       break;
11743
11744     case MEM:
11745       {
11746         rtx new_rtl = avoid_constant_pool_reference (rtl);
11747         if (new_rtl != rtl)
11748           {
11749             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11750                                                  initialized);
11751             if (mem_loc_result != NULL)
11752               return mem_loc_result;
11753           }
11754       }
11755       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11756                                            get_address_mode (rtl), mode,
11757                                            VAR_INIT_STATUS_INITIALIZED);
11758       if (mem_loc_result == NULL)
11759         mem_loc_result = tls_mem_loc_descriptor (rtl);
11760       if (mem_loc_result != NULL)
11761         {
11762           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11763               || GET_MODE_CLASS (mode) != MODE_INT)
11764             {
11765               dw_die_ref type_die;
11766               dw_loc_descr_ref deref;
11767
11768               if (dwarf_strict)
11769                 return NULL;
11770               type_die
11771                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11772               if (type_die == NULL)
11773                 return NULL;
11774               deref = new_loc_descr (DW_OP_GNU_deref_type,
11775                                      GET_MODE_SIZE (mode), 0);
11776               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11777               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11778               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11779               add_loc_descr (&mem_loc_result, deref);
11780             }
11781           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11782             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11783           else
11784             add_loc_descr (&mem_loc_result,
11785                            new_loc_descr (DW_OP_deref_size,
11786                                           GET_MODE_SIZE (mode), 0));
11787         }
11788       break;
11789
11790     case LO_SUM:
11791       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11792
11793     case LABEL_REF:
11794       /* Some ports can transform a symbol ref into a label ref, because
11795          the symbol ref is too far away and has to be dumped into a constant
11796          pool.  */
11797     case CONST:
11798     case SYMBOL_REF:
11799       if (GET_MODE_CLASS (mode) != MODE_INT
11800           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11801 #ifdef POINTERS_EXTEND_UNSIGNED
11802               && (mode != Pmode || mem_mode == VOIDmode)
11803 #endif
11804               ))
11805         break;
11806       if (GET_CODE (rtl) == SYMBOL_REF
11807           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11808         {
11809           dw_loc_descr_ref temp;
11810
11811           /* If this is not defined, we have no way to emit the data.  */
11812           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11813             break;
11814
11815           /* We used to emit DW_OP_addr here, but that's wrong, since
11816              DW_OP_addr should be relocated by the debug info consumer,
11817              while DW_OP_GNU_push_tls_address operand should not.  */
11818           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11819                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11820           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11821           temp->dw_loc_oprnd1.v.val_addr = rtl;
11822           temp->dtprel = true;
11823
11824           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11825           add_loc_descr (&mem_loc_result, temp);
11826
11827           break;
11828         }
11829
11830       if (!const_ok_for_output (rtl))
11831         break;
11832
11833     symref:
11834       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11835       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11836       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11837       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11838       break;
11839
11840     case CONCAT:
11841     case CONCATN:
11842     case VAR_LOCATION:
11843     case DEBUG_IMPLICIT_PTR:
11844       expansion_failed (NULL_TREE, rtl,
11845                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11846       return 0;
11847
11848     case ENTRY_VALUE:
11849       if (dwarf_strict)
11850         return NULL;
11851       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11852         {
11853           if (GET_MODE_CLASS (mode) != MODE_INT
11854               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11855             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11856                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11857           else
11858             op0
11859               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11860                                         VAR_INIT_STATUS_INITIALIZED);
11861         }
11862       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11863                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11864         {
11865           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11866                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11867           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11868             return NULL;
11869         }
11870       else
11871         gcc_unreachable ();
11872       if (op0 == NULL)
11873         return NULL;
11874       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11875       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11876       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11877       break;
11878
11879     case DEBUG_PARAMETER_REF:
11880       mem_loc_result = parameter_ref_descriptor (rtl);
11881       break;
11882
11883     case PRE_MODIFY:
11884       /* Extract the PLUS expression nested inside and fall into
11885          PLUS code below.  */
11886       rtl = XEXP (rtl, 1);
11887       goto plus;
11888
11889     case PRE_INC:
11890     case PRE_DEC:
11891       /* Turn these into a PLUS expression and fall into the PLUS code
11892          below.  */
11893       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11894                           GEN_INT (GET_CODE (rtl) == PRE_INC
11895                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11896                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11897
11898       /* ... fall through ...  */
11899
11900     case PLUS:
11901     plus:
11902       if (is_based_loc (rtl)
11903           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11904           && GET_MODE_CLASS (mode) == MODE_INT)
11905         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11906                                           INTVAL (XEXP (rtl, 1)),
11907                                           VAR_INIT_STATUS_INITIALIZED);
11908       else
11909         {
11910           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11911                                                VAR_INIT_STATUS_INITIALIZED);
11912           if (mem_loc_result == 0)
11913             break;
11914
11915           if (CONST_INT_P (XEXP (rtl, 1))
11916               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11917             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11918           else
11919             {
11920               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11921                                         VAR_INIT_STATUS_INITIALIZED);
11922               if (op1 == 0)
11923                 break;
11924               add_loc_descr (&mem_loc_result, op1);
11925               add_loc_descr (&mem_loc_result,
11926                              new_loc_descr (DW_OP_plus, 0, 0));
11927             }
11928         }
11929       break;
11930
11931     /* If a pseudo-reg is optimized away, it is possible for it to
11932        be replaced with a MEM containing a multiply or shift.  */
11933     case MINUS:
11934       op = DW_OP_minus;
11935       goto do_binop;
11936
11937     case MULT:
11938       op = DW_OP_mul;
11939       goto do_binop;
11940
11941     case DIV:
11942       if (!dwarf_strict
11943           && GET_MODE_CLASS (mode) == MODE_INT
11944           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11945         {
11946           mem_loc_result = typed_binop (DW_OP_div, rtl,
11947                                         base_type_for_mode (mode, 0),
11948                                         mode, mem_mode);
11949           break;
11950         }
11951       op = DW_OP_div;
11952       goto do_binop;
11953
11954     case UMOD:
11955       op = DW_OP_mod;
11956       goto do_binop;
11957
11958     case ASHIFT:
11959       op = DW_OP_shl;
11960       goto do_shift;
11961
11962     case ASHIFTRT:
11963       op = DW_OP_shra;
11964       goto do_shift;
11965
11966     case LSHIFTRT:
11967       op = DW_OP_shr;
11968       goto do_shift;
11969
11970     do_shift:
11971       if (GET_MODE_CLASS (mode) != MODE_INT)
11972         break;
11973       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11974                                 VAR_INIT_STATUS_INITIALIZED);
11975       {
11976         rtx rtlop1 = XEXP (rtl, 1);
11977         if (GET_MODE (rtlop1) != VOIDmode
11978             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11979                < GET_MODE_BITSIZE (mode))
11980           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11981         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11982                                   VAR_INIT_STATUS_INITIALIZED);
11983       }
11984
11985       if (op0 == 0 || op1 == 0)
11986         break;
11987
11988       mem_loc_result = op0;
11989       add_loc_descr (&mem_loc_result, op1);
11990       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11991       break;
11992
11993     case AND:
11994       op = DW_OP_and;
11995       goto do_binop;
11996
11997     case IOR:
11998       op = DW_OP_or;
11999       goto do_binop;
12000
12001     case XOR:
12002       op = DW_OP_xor;
12003       goto do_binop;
12004
12005     do_binop:
12006       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12007                                 VAR_INIT_STATUS_INITIALIZED);
12008       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12009                                 VAR_INIT_STATUS_INITIALIZED);
12010
12011       if (op0 == 0 || op1 == 0)
12012         break;
12013
12014       mem_loc_result = op0;
12015       add_loc_descr (&mem_loc_result, op1);
12016       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12017       break;
12018
12019     case MOD:
12020       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12021         {
12022           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12023                                         base_type_for_mode (mode, 0),
12024                                         mode, mem_mode);
12025           break;
12026         }
12027
12028       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12029                                 VAR_INIT_STATUS_INITIALIZED);
12030       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12031                                 VAR_INIT_STATUS_INITIALIZED);
12032
12033       if (op0 == 0 || op1 == 0)
12034         break;
12035
12036       mem_loc_result = op0;
12037       add_loc_descr (&mem_loc_result, op1);
12038       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12039       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12040       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12041       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12042       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12043       break;
12044
12045     case UDIV:
12046       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12047         {
12048           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12049             {
12050               op = DW_OP_div;
12051               goto do_binop;
12052             }
12053           mem_loc_result = typed_binop (DW_OP_div, rtl,
12054                                         base_type_for_mode (mode, 1),
12055                                         mode, mem_mode);
12056         }
12057       break;
12058
12059     case NOT:
12060       op = DW_OP_not;
12061       goto do_unop;
12062
12063     case ABS:
12064       op = DW_OP_abs;
12065       goto do_unop;
12066
12067     case NEG:
12068       op = DW_OP_neg;
12069       goto do_unop;
12070
12071     do_unop:
12072       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12073                                 VAR_INIT_STATUS_INITIALIZED);
12074
12075       if (op0 == 0)
12076         break;
12077
12078       mem_loc_result = op0;
12079       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12080       break;
12081
12082     case CONST_INT:
12083       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12084 #ifdef POINTERS_EXTEND_UNSIGNED
12085           || (mode == Pmode
12086               && mem_mode != VOIDmode
12087               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12088 #endif
12089           )
12090         {
12091           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12092           break;
12093         }
12094       if (!dwarf_strict
12095           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12096               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12097         {
12098           dw_die_ref type_die = base_type_for_mode (mode, 1);
12099           enum machine_mode amode;
12100           if (type_die == NULL)
12101             return NULL;
12102           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12103                                  MODE_INT, 0);
12104           if (INTVAL (rtl) >= 0
12105               && amode != BLKmode
12106               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12107               /* const DW_OP_GNU_convert <XXX> vs.
12108                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12109               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12110                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12111             {
12112               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12113               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12114               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12115               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12116               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12117               add_loc_descr (&mem_loc_result, op0);
12118               return mem_loc_result;
12119             }
12120           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12121                                           INTVAL (rtl));
12122           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12123           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12124           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12125           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12126             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12127           else
12128             {
12129               mem_loc_result->dw_loc_oprnd2.val_class
12130                 = dw_val_class_const_double;
12131               mem_loc_result->dw_loc_oprnd2.v.val_double
12132                 = shwi_to_double_int (INTVAL (rtl));
12133             }
12134         }
12135       break;
12136
12137     case CONST_DOUBLE:
12138       if (!dwarf_strict)
12139         {
12140           dw_die_ref type_die;
12141
12142           /* Note that a CONST_DOUBLE rtx could represent either an integer
12143              or a floating-point constant.  A CONST_DOUBLE is used whenever
12144              the constant requires more than one word in order to be
12145              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12146           if (mode == VOIDmode
12147               || (GET_MODE (rtl) == VOIDmode
12148                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12149             break;
12150           type_die = base_type_for_mode (mode,
12151                                          GET_MODE_CLASS (mode) == MODE_INT);
12152           if (type_die == NULL)
12153             return NULL;
12154           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12155           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12156           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12157           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12158           if (SCALAR_FLOAT_MODE_P (mode))
12159             {
12160               unsigned int length = GET_MODE_SIZE (mode);
12161               unsigned char *array
12162                   = (unsigned char*) ggc_alloc_atomic (length);
12163
12164               insert_float (rtl, array);
12165               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12166               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12167               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12168               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12169             }
12170           else
12171             {
12172               mem_loc_result->dw_loc_oprnd2.val_class
12173                 = dw_val_class_const_double;
12174               mem_loc_result->dw_loc_oprnd2.v.val_double
12175                 = rtx_to_double_int (rtl);
12176             }
12177         }
12178       break;
12179
12180     case EQ:
12181       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12182       break;
12183
12184     case GE:
12185       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12186       break;
12187
12188     case GT:
12189       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12190       break;
12191
12192     case LE:
12193       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12194       break;
12195
12196     case LT:
12197       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12198       break;
12199
12200     case NE:
12201       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12202       break;
12203
12204     case GEU:
12205       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12206       break;
12207
12208     case GTU:
12209       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12210       break;
12211
12212     case LEU:
12213       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12214       break;
12215
12216     case LTU:
12217       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12218       break;
12219
12220     case UMIN:
12221     case UMAX:
12222       if (GET_MODE_CLASS (mode) != MODE_INT)
12223         break;
12224       /* FALLTHRU */
12225     case SMIN:
12226     case SMAX:
12227       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12228       break;
12229
12230     case ZERO_EXTRACT:
12231     case SIGN_EXTRACT:
12232       if (CONST_INT_P (XEXP (rtl, 1))
12233           && CONST_INT_P (XEXP (rtl, 2))
12234           && ((unsigned) INTVAL (XEXP (rtl, 1))
12235               + (unsigned) INTVAL (XEXP (rtl, 2))
12236               <= GET_MODE_BITSIZE (mode))
12237           && GET_MODE_CLASS (mode) == MODE_INT
12238           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12239           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12240         {
12241           int shift, size;
12242           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12243                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12244           if (op0 == 0)
12245             break;
12246           if (GET_CODE (rtl) == SIGN_EXTRACT)
12247             op = DW_OP_shra;
12248           else
12249             op = DW_OP_shr;
12250           mem_loc_result = op0;
12251           size = INTVAL (XEXP (rtl, 1));
12252           shift = INTVAL (XEXP (rtl, 2));
12253           if (BITS_BIG_ENDIAN)
12254             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12255                     - shift - size;
12256           if (shift + size != (int) DWARF2_ADDR_SIZE)
12257             {
12258               add_loc_descr (&mem_loc_result,
12259                              int_loc_descriptor (DWARF2_ADDR_SIZE
12260                                                  - shift - size));
12261               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12262             }
12263           if (size != (int) DWARF2_ADDR_SIZE)
12264             {
12265               add_loc_descr (&mem_loc_result,
12266                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12267               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12268             }
12269         }
12270       break;
12271
12272     case IF_THEN_ELSE:
12273       {
12274         dw_loc_descr_ref op2, bra_node, drop_node;
12275         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12276                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12277                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12278                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12279         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12280                                   VAR_INIT_STATUS_INITIALIZED);
12281         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12282                                   VAR_INIT_STATUS_INITIALIZED);
12283         if (op0 == NULL || op1 == NULL || op2 == NULL)
12284           break;
12285
12286         mem_loc_result = op1;
12287         add_loc_descr (&mem_loc_result, op2);
12288         add_loc_descr (&mem_loc_result, op0);
12289         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12290         add_loc_descr (&mem_loc_result, bra_node);
12291         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12292         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12293         add_loc_descr (&mem_loc_result, drop_node);
12294         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12295         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12296       }
12297       break;
12298
12299     case FLOAT_EXTEND:
12300     case FLOAT_TRUNCATE:
12301     case FLOAT:
12302     case UNSIGNED_FLOAT:
12303     case FIX:
12304     case UNSIGNED_FIX:
12305       if (!dwarf_strict)
12306         {
12307           dw_die_ref type_die;
12308           dw_loc_descr_ref cvt;
12309
12310           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12311                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12312           if (op0 == NULL)
12313             break;
12314           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12315               && (GET_CODE (rtl) == FLOAT
12316                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12317                      <= DWARF2_ADDR_SIZE))
12318             {
12319               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12320                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12321               if (type_die == NULL)
12322                 break;
12323               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12324               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12325               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12326               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12327               add_loc_descr (&op0, cvt);
12328             }
12329           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12330           if (type_die == NULL)
12331             break;
12332           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12333           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12334           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12335           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12336           add_loc_descr (&op0, cvt);
12337           if (GET_MODE_CLASS (mode) == MODE_INT
12338               && (GET_CODE (rtl) == FIX
12339                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12340             {
12341               op0 = convert_descriptor_to_mode (mode, op0);
12342               if (op0 == NULL)
12343                 break;
12344             }
12345           mem_loc_result = op0;
12346         }
12347       break;
12348
12349     case CLZ:
12350     case CTZ:
12351     case FFS:
12352       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12353       break;
12354
12355     case POPCOUNT:
12356     case PARITY:
12357       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12358       break;
12359
12360     case BSWAP:
12361       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12362       break;
12363
12364     case ROTATE:
12365     case ROTATERT:
12366       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12367       break;
12368
12369     case COMPARE:
12370     case TRUNCATE:
12371       /* In theory, we could implement the above.  */
12372       /* DWARF cannot represent the unsigned compare operations
12373          natively.  */
12374     case SS_MULT:
12375     case US_MULT:
12376     case SS_DIV:
12377     case US_DIV:
12378     case SS_PLUS:
12379     case US_PLUS:
12380     case SS_MINUS:
12381     case US_MINUS:
12382     case SS_NEG:
12383     case US_NEG:
12384     case SS_ABS:
12385     case SS_ASHIFT:
12386     case US_ASHIFT:
12387     case SS_TRUNCATE:
12388     case US_TRUNCATE:
12389     case UNORDERED:
12390     case ORDERED:
12391     case UNEQ:
12392     case UNGE:
12393     case UNGT:
12394     case UNLE:
12395     case UNLT:
12396     case LTGT:
12397     case FRACT_CONVERT:
12398     case UNSIGNED_FRACT_CONVERT:
12399     case SAT_FRACT:
12400     case UNSIGNED_SAT_FRACT:
12401     case SQRT:
12402     case ASM_OPERANDS:
12403     case VEC_MERGE:
12404     case VEC_SELECT:
12405     case VEC_CONCAT:
12406     case VEC_DUPLICATE:
12407     case UNSPEC:
12408     case HIGH:
12409     case FMA:
12410     case STRICT_LOW_PART:
12411     case CONST_VECTOR:
12412     case CONST_FIXED:
12413     case CLRSB:
12414       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12415          can't express it in the debug info.  This can happen e.g. with some
12416          TLS UNSPECs.  */
12417       break;
12418
12419     case CONST_STRING:
12420       resolve_one_addr (&rtl, NULL);
12421       goto symref;
12422
12423     default:
12424 #ifdef ENABLE_CHECKING
12425       print_rtl (stderr, rtl);
12426       gcc_unreachable ();
12427 #else
12428       break;
12429 #endif
12430     }
12431
12432   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12433     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12434
12435   return mem_loc_result;
12436 }
12437
12438 /* Return a descriptor that describes the concatenation of two locations.
12439    This is typically a complex variable.  */
12440
12441 static dw_loc_descr_ref
12442 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12443 {
12444   dw_loc_descr_ref cc_loc_result = NULL;
12445   dw_loc_descr_ref x0_ref
12446     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12447   dw_loc_descr_ref x1_ref
12448     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12449
12450   if (x0_ref == 0 || x1_ref == 0)
12451     return 0;
12452
12453   cc_loc_result = x0_ref;
12454   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12455
12456   add_loc_descr (&cc_loc_result, x1_ref);
12457   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12458
12459   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12460     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12461
12462   return cc_loc_result;
12463 }
12464
12465 /* Return a descriptor that describes the concatenation of N
12466    locations.  */
12467
12468 static dw_loc_descr_ref
12469 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12470 {
12471   unsigned int i;
12472   dw_loc_descr_ref cc_loc_result = NULL;
12473   unsigned int n = XVECLEN (concatn, 0);
12474
12475   for (i = 0; i < n; ++i)
12476     {
12477       dw_loc_descr_ref ref;
12478       rtx x = XVECEXP (concatn, 0, i);
12479
12480       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12481       if (ref == NULL)
12482         return NULL;
12483
12484       add_loc_descr (&cc_loc_result, ref);
12485       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12486     }
12487
12488   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12489     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12490
12491   return cc_loc_result;
12492 }
12493
12494 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12495    for DEBUG_IMPLICIT_PTR RTL.  */
12496
12497 static dw_loc_descr_ref
12498 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12499 {
12500   dw_loc_descr_ref ret;
12501   dw_die_ref ref;
12502
12503   if (dwarf_strict)
12504     return NULL;
12505   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12506               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12507               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12508   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12509   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12510   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12511   if (ref)
12512     {
12513       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12514       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12515       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12516     }
12517   else
12518     {
12519       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12520       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12521     }
12522   return ret;
12523 }
12524
12525 /* Output a proper Dwarf location descriptor for a variable or parameter
12526    which is either allocated in a register or in a memory location.  For a
12527    register, we just generate an OP_REG and the register number.  For a
12528    memory location we provide a Dwarf postfix expression describing how to
12529    generate the (dynamic) address of the object onto the address stack.
12530
12531    MODE is mode of the decl if this loc_descriptor is going to be used in
12532    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12533    allowed, VOIDmode otherwise.
12534
12535    If we don't know how to describe it, return 0.  */
12536
12537 static dw_loc_descr_ref
12538 loc_descriptor (rtx rtl, enum machine_mode mode,
12539                 enum var_init_status initialized)
12540 {
12541   dw_loc_descr_ref loc_result = NULL;
12542
12543   switch (GET_CODE (rtl))
12544     {
12545     case SUBREG:
12546       /* The case of a subreg may arise when we have a local (register)
12547          variable or a formal (register) parameter which doesn't quite fill
12548          up an entire register.  For now, just assume that it is
12549          legitimate to make the Dwarf info refer to the whole register which
12550          contains the given subreg.  */
12551       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12552         loc_result = loc_descriptor (SUBREG_REG (rtl),
12553                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12554       else
12555         goto do_default;
12556       break;
12557
12558     case REG:
12559       loc_result = reg_loc_descriptor (rtl, initialized);
12560       break;
12561
12562     case MEM:
12563       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12564                                        GET_MODE (rtl), initialized);
12565       if (loc_result == NULL)
12566         loc_result = tls_mem_loc_descriptor (rtl);
12567       if (loc_result == NULL)
12568         {
12569           rtx new_rtl = avoid_constant_pool_reference (rtl);
12570           if (new_rtl != rtl)
12571             loc_result = loc_descriptor (new_rtl, mode, initialized);
12572         }
12573       break;
12574
12575     case CONCAT:
12576       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12577                                           initialized);
12578       break;
12579
12580     case CONCATN:
12581       loc_result = concatn_loc_descriptor (rtl, initialized);
12582       break;
12583
12584     case VAR_LOCATION:
12585       /* Single part.  */
12586       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12587         {
12588           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12589           if (GET_CODE (loc) == EXPR_LIST)
12590             loc = XEXP (loc, 0);
12591           loc_result = loc_descriptor (loc, mode, initialized);
12592           break;
12593         }
12594
12595       rtl = XEXP (rtl, 1);
12596       /* FALLTHRU */
12597
12598     case PARALLEL:
12599       {
12600         rtvec par_elems = XVEC (rtl, 0);
12601         int num_elem = GET_NUM_ELEM (par_elems);
12602         enum machine_mode mode;
12603         int i;
12604
12605         /* Create the first one, so we have something to add to.  */
12606         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12607                                      VOIDmode, initialized);
12608         if (loc_result == NULL)
12609           return NULL;
12610         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12611         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12612         for (i = 1; i < num_elem; i++)
12613           {
12614             dw_loc_descr_ref temp;
12615
12616             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12617                                    VOIDmode, initialized);
12618             if (temp == NULL)
12619               return NULL;
12620             add_loc_descr (&loc_result, temp);
12621             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12622             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12623           }
12624       }
12625       break;
12626
12627     case CONST_INT:
12628       if (mode != VOIDmode && mode != BLKmode)
12629         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12630                                                     INTVAL (rtl));
12631       break;
12632
12633     case CONST_DOUBLE:
12634       if (mode == VOIDmode)
12635         mode = GET_MODE (rtl);
12636
12637       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12638         {
12639           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12640
12641           /* Note that a CONST_DOUBLE rtx could represent either an integer
12642              or a floating-point constant.  A CONST_DOUBLE is used whenever
12643              the constant requires more than one word in order to be
12644              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12645           loc_result = new_loc_descr (DW_OP_implicit_value,
12646                                       GET_MODE_SIZE (mode), 0);
12647           if (SCALAR_FLOAT_MODE_P (mode))
12648             {
12649               unsigned int length = GET_MODE_SIZE (mode);
12650               unsigned char *array
12651                   = (unsigned char*) ggc_alloc_atomic (length);
12652
12653               insert_float (rtl, array);
12654               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12655               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12656               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12657               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12658             }
12659           else
12660             {
12661               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12662               loc_result->dw_loc_oprnd2.v.val_double
12663                 = rtx_to_double_int (rtl);
12664             }
12665         }
12666       break;
12667
12668     case CONST_VECTOR:
12669       if (mode == VOIDmode)
12670         mode = GET_MODE (rtl);
12671
12672       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12673         {
12674           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12675           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12676           unsigned char *array = (unsigned char *)
12677             ggc_alloc_atomic (length * elt_size);
12678           unsigned int i;
12679           unsigned char *p;
12680
12681           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12682           switch (GET_MODE_CLASS (mode))
12683             {
12684             case MODE_VECTOR_INT:
12685               for (i = 0, p = array; i < length; i++, p += elt_size)
12686                 {
12687                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12688                   double_int val = rtx_to_double_int (elt);
12689
12690                   if (elt_size <= sizeof (HOST_WIDE_INT))
12691                     insert_int (double_int_to_shwi (val), elt_size, p);
12692                   else
12693                     {
12694                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12695                       insert_double (val, p);
12696                     }
12697                 }
12698               break;
12699
12700             case MODE_VECTOR_FLOAT:
12701               for (i = 0, p = array; i < length; i++, p += elt_size)
12702                 {
12703                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12704                   insert_float (elt, p);
12705                 }
12706               break;
12707
12708             default:
12709               gcc_unreachable ();
12710             }
12711
12712           loc_result = new_loc_descr (DW_OP_implicit_value,
12713                                       length * elt_size, 0);
12714           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12715           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12716           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12717           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12718         }
12719       break;
12720
12721     case CONST:
12722       if (mode == VOIDmode
12723           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12724           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12725           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12726         {
12727           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12728           break;
12729         }
12730       /* FALLTHROUGH */
12731     case SYMBOL_REF:
12732       if (!const_ok_for_output (rtl))
12733         break;
12734     case LABEL_REF:
12735       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12736           && (dwarf_version >= 4 || !dwarf_strict))
12737         {
12738           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12739           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12740           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12741           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12742           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12743         }
12744       break;
12745
12746     case DEBUG_IMPLICIT_PTR:
12747       loc_result = implicit_ptr_descriptor (rtl, 0);
12748       break;
12749
12750     case PLUS:
12751       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12752           && CONST_INT_P (XEXP (rtl, 1)))
12753         {
12754           loc_result
12755             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12756           break;
12757         }
12758       /* FALLTHRU */
12759     do_default:
12760     default:
12761       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12762            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12763            && dwarf_version >= 4)
12764           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12765         {
12766           /* Value expression.  */
12767           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12768           if (loc_result)
12769             add_loc_descr (&loc_result,
12770                            new_loc_descr (DW_OP_stack_value, 0, 0));
12771         }
12772       break;
12773     }
12774
12775   return loc_result;
12776 }
12777
12778 /* We need to figure out what section we should use as the base for the
12779    address ranges where a given location is valid.
12780    1. If this particular DECL has a section associated with it, use that.
12781    2. If this function has a section associated with it, use that.
12782    3. Otherwise, use the text section.
12783    XXX: If you split a variable across multiple sections, we won't notice.  */
12784
12785 static const char *
12786 secname_for_decl (const_tree decl)
12787 {
12788   const char *secname;
12789
12790   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12791     {
12792       tree sectree = DECL_SECTION_NAME (decl);
12793       secname = TREE_STRING_POINTER (sectree);
12794     }
12795   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12796     {
12797       tree sectree = DECL_SECTION_NAME (current_function_decl);
12798       secname = TREE_STRING_POINTER (sectree);
12799     }
12800   else if (cfun && in_cold_section_p)
12801     secname = crtl->subsections.cold_section_label;
12802   else
12803     secname = text_section_label;
12804
12805   return secname;
12806 }
12807
12808 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12809
12810 static bool
12811 decl_by_reference_p (tree decl)
12812 {
12813   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12814            || TREE_CODE (decl) == VAR_DECL)
12815           && DECL_BY_REFERENCE (decl));
12816 }
12817
12818 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12819    for VARLOC.  */
12820
12821 static dw_loc_descr_ref
12822 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12823                enum var_init_status initialized)
12824 {
12825   int have_address = 0;
12826   dw_loc_descr_ref descr;
12827   enum machine_mode mode;
12828
12829   if (want_address != 2)
12830     {
12831       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12832       /* Single part.  */
12833       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12834         {
12835           varloc = PAT_VAR_LOCATION_LOC (varloc);
12836           if (GET_CODE (varloc) == EXPR_LIST)
12837             varloc = XEXP (varloc, 0);
12838           mode = GET_MODE (varloc);
12839           if (MEM_P (varloc))
12840             {
12841               rtx addr = XEXP (varloc, 0);
12842               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12843                                           mode, initialized);
12844               if (descr)
12845                 have_address = 1;
12846               else
12847                 {
12848                   rtx x = avoid_constant_pool_reference (varloc);
12849                   if (x != varloc)
12850                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12851                                                 initialized);
12852                 }
12853             }
12854           else
12855             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12856         }
12857       else
12858         return 0;
12859     }
12860   else
12861     {
12862       if (GET_CODE (varloc) == VAR_LOCATION)
12863         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12864       else
12865         mode = DECL_MODE (loc);
12866       descr = loc_descriptor (varloc, mode, initialized);
12867       have_address = 1;
12868     }
12869
12870   if (!descr)
12871     return 0;
12872
12873   if (want_address == 2 && !have_address
12874       && (dwarf_version >= 4 || !dwarf_strict))
12875     {
12876       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12877         {
12878           expansion_failed (loc, NULL_RTX,
12879                             "DWARF address size mismatch");
12880           return 0;
12881         }
12882       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12883       have_address = 1;
12884     }
12885   /* Show if we can't fill the request for an address.  */
12886   if (want_address && !have_address)
12887     {
12888       expansion_failed (loc, NULL_RTX,
12889                         "Want address and only have value");
12890       return 0;
12891     }
12892
12893   /* If we've got an address and don't want one, dereference.  */
12894   if (!want_address && have_address)
12895     {
12896       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12897       enum dwarf_location_atom op;
12898
12899       if (size > DWARF2_ADDR_SIZE || size == -1)
12900         {
12901           expansion_failed (loc, NULL_RTX,
12902                             "DWARF address size mismatch");
12903           return 0;
12904         }
12905       else if (size == DWARF2_ADDR_SIZE)
12906         op = DW_OP_deref;
12907       else
12908         op = DW_OP_deref_size;
12909
12910       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12911     }
12912
12913   return descr;
12914 }
12915
12916 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12917    if it is not possible.  */
12918
12919 static dw_loc_descr_ref
12920 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12921 {
12922   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12923     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12924   else if (dwarf_version >= 3 || !dwarf_strict)
12925     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12926   else
12927     return NULL;
12928 }
12929
12930 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12931    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12932
12933 static dw_loc_descr_ref
12934 dw_sra_loc_expr (tree decl, rtx loc)
12935 {
12936   rtx p;
12937   unsigned int padsize = 0;
12938   dw_loc_descr_ref descr, *descr_tail;
12939   unsigned HOST_WIDE_INT decl_size;
12940   rtx varloc;
12941   enum var_init_status initialized;
12942
12943   if (DECL_SIZE (decl) == NULL
12944       || !host_integerp (DECL_SIZE (decl), 1))
12945     return NULL;
12946
12947   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12948   descr = NULL;
12949   descr_tail = &descr;
12950
12951   for (p = loc; p; p = XEXP (p, 1))
12952     {
12953       unsigned int bitsize = decl_piece_bitsize (p);
12954       rtx loc_note = *decl_piece_varloc_ptr (p);
12955       dw_loc_descr_ref cur_descr;
12956       dw_loc_descr_ref *tail, last = NULL;
12957       unsigned int opsize = 0;
12958
12959       if (loc_note == NULL_RTX
12960           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12961         {
12962           padsize += bitsize;
12963           continue;
12964         }
12965       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12966       varloc = NOTE_VAR_LOCATION (loc_note);
12967       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12968       if (cur_descr == NULL)
12969         {
12970           padsize += bitsize;
12971           continue;
12972         }
12973
12974       /* Check that cur_descr either doesn't use
12975          DW_OP_*piece operations, or their sum is equal
12976          to bitsize.  Otherwise we can't embed it.  */
12977       for (tail = &cur_descr; *tail != NULL;
12978            tail = &(*tail)->dw_loc_next)
12979         if ((*tail)->dw_loc_opc == DW_OP_piece)
12980           {
12981             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12982                       * BITS_PER_UNIT;
12983             last = *tail;
12984           }
12985         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12986           {
12987             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12988             last = *tail;
12989           }
12990
12991       if (last != NULL && opsize != bitsize)
12992         {
12993           padsize += bitsize;
12994           continue;
12995         }
12996
12997       /* If there is a hole, add DW_OP_*piece after empty DWARF
12998          expression, which means that those bits are optimized out.  */
12999       if (padsize)
13000         {
13001           if (padsize > decl_size)
13002             return NULL;
13003           decl_size -= padsize;
13004           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13005           if (*descr_tail == NULL)
13006             return NULL;
13007           descr_tail = &(*descr_tail)->dw_loc_next;
13008           padsize = 0;
13009         }
13010       *descr_tail = cur_descr;
13011       descr_tail = tail;
13012       if (bitsize > decl_size)
13013         return NULL;
13014       decl_size -= bitsize;
13015       if (last == NULL)
13016         {
13017           HOST_WIDE_INT offset = 0;
13018           if (GET_CODE (varloc) == VAR_LOCATION
13019               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13020             {
13021               varloc = PAT_VAR_LOCATION_LOC (varloc);
13022               if (GET_CODE (varloc) == EXPR_LIST)
13023                 varloc = XEXP (varloc, 0);
13024             }
13025           do 
13026             {
13027               if (GET_CODE (varloc) == CONST
13028                   || GET_CODE (varloc) == SIGN_EXTEND
13029                   || GET_CODE (varloc) == ZERO_EXTEND)
13030                 varloc = XEXP (varloc, 0);
13031               else if (GET_CODE (varloc) == SUBREG)
13032                 varloc = SUBREG_REG (varloc);
13033               else
13034                 break;
13035             }
13036           while (1);
13037           /* DW_OP_bit_size offset should be zero for register
13038              or implicit location descriptions and empty location
13039              descriptions, but for memory addresses needs big endian
13040              adjustment.  */
13041           if (MEM_P (varloc))
13042             {
13043               unsigned HOST_WIDE_INT memsize
13044                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13045               if (memsize != bitsize)
13046                 {
13047                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13048                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13049                     return NULL;
13050                   if (memsize < bitsize)
13051                     return NULL;
13052                   if (BITS_BIG_ENDIAN)
13053                     offset = memsize - bitsize;
13054                 }
13055             }
13056
13057           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13058           if (*descr_tail == NULL)
13059             return NULL;
13060           descr_tail = &(*descr_tail)->dw_loc_next;
13061         }
13062     }
13063
13064   /* If there were any non-empty expressions, add padding till the end of
13065      the decl.  */
13066   if (descr != NULL && decl_size != 0)
13067     {
13068       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13069       if (*descr_tail == NULL)
13070         return NULL;
13071     }
13072   return descr;
13073 }
13074
13075 /* Return the dwarf representation of the location list LOC_LIST of
13076    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13077    function.  */
13078
13079 static dw_loc_list_ref
13080 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13081 {
13082   const char *endname, *secname;
13083   rtx varloc;
13084   enum var_init_status initialized;
13085   struct var_loc_node *node;
13086   dw_loc_descr_ref descr;
13087   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13088   dw_loc_list_ref list = NULL;
13089   dw_loc_list_ref *listp = &list;
13090
13091   /* Now that we know what section we are using for a base,
13092      actually construct the list of locations.
13093      The first location information is what is passed to the
13094      function that creates the location list, and the remaining
13095      locations just get added on to that list.
13096      Note that we only know the start address for a location
13097      (IE location changes), so to build the range, we use
13098      the range [current location start, next location start].
13099      This means we have to special case the last node, and generate
13100      a range of [last location start, end of function label].  */
13101
13102   secname = secname_for_decl (decl);
13103
13104   for (node = loc_list->first; node; node = node->next)
13105     if (GET_CODE (node->loc) == EXPR_LIST
13106         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13107       {
13108         if (GET_CODE (node->loc) == EXPR_LIST)
13109           {
13110             /* This requires DW_OP_{,bit_}piece, which is not usable
13111                inside DWARF expressions.  */
13112             if (want_address != 2)
13113               continue;
13114             descr = dw_sra_loc_expr (decl, node->loc);
13115             if (descr == NULL)
13116               continue;
13117           }
13118         else
13119           {
13120             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13121             varloc = NOTE_VAR_LOCATION (node->loc);
13122             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13123           }
13124         if (descr)
13125           {
13126             bool range_across_switch = false;
13127             /* If section switch happens in between node->label
13128                and node->next->label (or end of function) and
13129                we can't emit it as a single entry list,
13130                emit two ranges, first one ending at the end
13131                of first partition and second one starting at the
13132                beginning of second partition.  */
13133             if (node == loc_list->last_before_switch
13134                 && (node != loc_list->first || loc_list->first->next)
13135                 && current_function_decl)
13136               {
13137                 endname = cfun->fde->dw_fde_end;
13138                 range_across_switch = true;
13139               }
13140             /* The variable has a location between NODE->LABEL and
13141                NODE->NEXT->LABEL.  */
13142             else if (node->next)
13143               endname = node->next->label;
13144             /* If the variable has a location at the last label
13145                it keeps its location until the end of function.  */
13146             else if (!current_function_decl)
13147               endname = text_end_label;
13148             else
13149               {
13150                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13151                                              current_function_funcdef_no);
13152                 endname = ggc_strdup (label_id);
13153               }
13154
13155             *listp = new_loc_list (descr, node->label, endname, secname);
13156             if (TREE_CODE (decl) == PARM_DECL
13157                 && node == loc_list->first
13158                 && GET_CODE (node->loc) == NOTE
13159                 && strcmp (node->label, endname) == 0)
13160               (*listp)->force = true;
13161             listp = &(*listp)->dw_loc_next;
13162
13163             if (range_across_switch)
13164               {
13165                 if (GET_CODE (node->loc) == EXPR_LIST)
13166                   descr = dw_sra_loc_expr (decl, node->loc);
13167                 else
13168                   {
13169                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13170                     varloc = NOTE_VAR_LOCATION (node->loc);
13171                     descr = dw_loc_list_1 (decl, varloc, want_address,
13172                                            initialized);
13173                   }
13174                 gcc_assert (descr);
13175                 /* The variable has a location between NODE->LABEL and
13176                    NODE->NEXT->LABEL.  */
13177                 if (node->next)
13178                   endname = node->next->label;
13179                 else
13180                   endname = cfun->fde->dw_fde_second_end;
13181                 *listp = new_loc_list (descr,
13182                                        cfun->fde->dw_fde_second_begin,
13183                                        endname, secname);
13184                 listp = &(*listp)->dw_loc_next;
13185               }
13186           }
13187       }
13188
13189   /* Try to avoid the overhead of a location list emitting a location
13190      expression instead, but only if we didn't have more than one
13191      location entry in the first place.  If some entries were not
13192      representable, we don't want to pretend a single entry that was
13193      applies to the entire scope in which the variable is
13194      available.  */
13195   if (list && loc_list->first->next)
13196     gen_llsym (list);
13197
13198   return list;
13199 }
13200
13201 /* Return if the loc_list has only single element and thus can be represented
13202    as location description.   */
13203
13204 static bool
13205 single_element_loc_list_p (dw_loc_list_ref list)
13206 {
13207   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13208   return !list->ll_symbol;
13209 }
13210
13211 /* To each location in list LIST add loc descr REF.  */
13212
13213 static void
13214 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13215 {
13216   dw_loc_descr_ref copy;
13217   add_loc_descr (&list->expr, ref);
13218   list = list->dw_loc_next;
13219   while (list)
13220     {
13221       copy = ggc_alloc_dw_loc_descr_node ();
13222       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13223       add_loc_descr (&list->expr, copy);
13224       while (copy->dw_loc_next)
13225         {
13226           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13227           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13228           copy->dw_loc_next = new_copy;
13229           copy = new_copy;
13230         }
13231       list = list->dw_loc_next;
13232     }
13233 }
13234
13235 /* Given two lists RET and LIST
13236    produce location list that is result of adding expression in LIST
13237    to expression in RET on each possition in program.
13238    Might be destructive on both RET and LIST.
13239
13240    TODO: We handle only simple cases of RET or LIST having at most one
13241    element. General case would inolve sorting the lists in program order
13242    and merging them that will need some additional work.
13243    Adding that will improve quality of debug info especially for SRA-ed
13244    structures.  */
13245
13246 static void
13247 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13248 {
13249   if (!list)
13250     return;
13251   if (!*ret)
13252     {
13253       *ret = list;
13254       return;
13255     }
13256   if (!list->dw_loc_next)
13257     {
13258       add_loc_descr_to_each (*ret, list->expr);
13259       return;
13260     }
13261   if (!(*ret)->dw_loc_next)
13262     {
13263       add_loc_descr_to_each (list, (*ret)->expr);
13264       *ret = list;
13265       return;
13266     }
13267   expansion_failed (NULL_TREE, NULL_RTX,
13268                     "Don't know how to merge two non-trivial"
13269                     " location lists.\n");
13270   *ret = NULL;
13271   return;
13272 }
13273
13274 /* LOC is constant expression.  Try a luck, look it up in constant
13275    pool and return its loc_descr of its address.  */
13276
13277 static dw_loc_descr_ref
13278 cst_pool_loc_descr (tree loc)
13279 {
13280   /* Get an RTL for this, if something has been emitted.  */
13281   rtx rtl = lookup_constant_def (loc);
13282
13283   if (!rtl || !MEM_P (rtl))
13284     {
13285       gcc_assert (!rtl);
13286       return 0;
13287     }
13288   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13289
13290   /* TODO: We might get more coverage if we was actually delaying expansion
13291      of all expressions till end of compilation when constant pools are fully
13292      populated.  */
13293   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13294     {
13295       expansion_failed (loc, NULL_RTX,
13296                         "CST value in contant pool but not marked.");
13297       return 0;
13298     }
13299   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13300                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13301 }
13302
13303 /* Return dw_loc_list representing address of addr_expr LOC
13304    by looking for innder INDIRECT_REF expression and turing it
13305    into simple arithmetics.  */
13306
13307 static dw_loc_list_ref
13308 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13309 {
13310   tree obj, offset;
13311   HOST_WIDE_INT bitsize, bitpos, bytepos;
13312   enum machine_mode mode;
13313   int volatilep;
13314   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13315   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13316
13317   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13318                              &bitsize, &bitpos, &offset, &mode,
13319                              &unsignedp, &volatilep, false);
13320   STRIP_NOPS (obj);
13321   if (bitpos % BITS_PER_UNIT)
13322     {
13323       expansion_failed (loc, NULL_RTX, "bitfield access");
13324       return 0;
13325     }
13326   if (!INDIRECT_REF_P (obj))
13327     {
13328       expansion_failed (obj,
13329                         NULL_RTX, "no indirect ref in inner refrence");
13330       return 0;
13331     }
13332   if (!offset && !bitpos)
13333     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13334   else if (toplev
13335            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13336            && (dwarf_version >= 4 || !dwarf_strict))
13337     {
13338       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13339       if (!list_ret)
13340         return 0;
13341       if (offset)
13342         {
13343           /* Variable offset.  */
13344           list_ret1 = loc_list_from_tree (offset, 0);
13345           if (list_ret1 == 0)
13346             return 0;
13347           add_loc_list (&list_ret, list_ret1);
13348           if (!list_ret)
13349             return 0;
13350           add_loc_descr_to_each (list_ret,
13351                                  new_loc_descr (DW_OP_plus, 0, 0));
13352         }
13353       bytepos = bitpos / BITS_PER_UNIT;
13354       if (bytepos > 0)
13355         add_loc_descr_to_each (list_ret,
13356                                new_loc_descr (DW_OP_plus_uconst,
13357                                               bytepos, 0));
13358       else if (bytepos < 0)
13359         loc_list_plus_const (list_ret, bytepos);
13360       add_loc_descr_to_each (list_ret,
13361                              new_loc_descr (DW_OP_stack_value, 0, 0));
13362     }
13363   return list_ret;
13364 }
13365
13366
13367 /* Generate Dwarf location list representing LOC.
13368    If WANT_ADDRESS is false, expression computing LOC will be computed
13369    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13370    if WANT_ADDRESS is 2, expression computing address useable in location
13371      will be returned (i.e. DW_OP_reg can be used
13372      to refer to register values).  */
13373
13374 static dw_loc_list_ref
13375 loc_list_from_tree (tree loc, int want_address)
13376 {
13377   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13378   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13379   int have_address = 0;
13380   enum dwarf_location_atom op;
13381
13382   /* ??? Most of the time we do not take proper care for sign/zero
13383      extending the values properly.  Hopefully this won't be a real
13384      problem...  */
13385
13386   switch (TREE_CODE (loc))
13387     {
13388     case ERROR_MARK:
13389       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13390       return 0;
13391
13392     case PLACEHOLDER_EXPR:
13393       /* This case involves extracting fields from an object to determine the
13394          position of other fields.  We don't try to encode this here.  The
13395          only user of this is Ada, which encodes the needed information using
13396          the names of types.  */
13397       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13398       return 0;
13399
13400     case CALL_EXPR:
13401       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13402       /* There are no opcodes for these operations.  */
13403       return 0;
13404
13405     case PREINCREMENT_EXPR:
13406     case PREDECREMENT_EXPR:
13407     case POSTINCREMENT_EXPR:
13408     case POSTDECREMENT_EXPR:
13409       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13410       /* There are no opcodes for these operations.  */
13411       return 0;
13412
13413     case ADDR_EXPR:
13414       /* If we already want an address, see if there is INDIRECT_REF inside
13415          e.g. for &this->field.  */
13416       if (want_address)
13417         {
13418           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13419                        (loc, want_address == 2);
13420           if (list_ret)
13421             have_address = 1;
13422           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13423                    && (ret = cst_pool_loc_descr (loc)))
13424             have_address = 1;
13425         }
13426         /* Otherwise, process the argument and look for the address.  */
13427       if (!list_ret && !ret)
13428         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13429       else
13430         {
13431           if (want_address)
13432             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13433           return NULL;
13434         }
13435       break;
13436
13437     case VAR_DECL:
13438       if (DECL_THREAD_LOCAL_P (loc))
13439         {
13440           rtx rtl;
13441           enum dwarf_location_atom first_op;
13442           enum dwarf_location_atom second_op;
13443           bool dtprel = false;
13444
13445           if (targetm.have_tls)
13446             {
13447               /* If this is not defined, we have no way to emit the
13448                  data.  */
13449               if (!targetm.asm_out.output_dwarf_dtprel)
13450                 return 0;
13451
13452                /* The way DW_OP_GNU_push_tls_address is specified, we
13453                   can only look up addresses of objects in the current
13454                   module.  We used DW_OP_addr as first op, but that's
13455                   wrong, because DW_OP_addr is relocated by the debug
13456                   info consumer, while DW_OP_GNU_push_tls_address
13457                   operand shouldn't be.  */
13458               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13459                 return 0;
13460               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13461               dtprel = true;
13462               second_op = DW_OP_GNU_push_tls_address;
13463             }
13464           else
13465             {
13466               if (!targetm.emutls.debug_form_tls_address
13467                   || !(dwarf_version >= 3 || !dwarf_strict))
13468                 return 0;
13469               /* We stuffed the control variable into the DECL_VALUE_EXPR
13470                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13471                  no longer appear in gimple code.  We used the control
13472                  variable in specific so that we could pick it up here.  */
13473               loc = DECL_VALUE_EXPR (loc);
13474               first_op = DW_OP_addr;
13475               second_op = DW_OP_form_tls_address;
13476             }
13477
13478           rtl = rtl_for_decl_location (loc);
13479           if (rtl == NULL_RTX)
13480             return 0;
13481
13482           if (!MEM_P (rtl))
13483             return 0;
13484           rtl = XEXP (rtl, 0);
13485           if (! CONSTANT_P (rtl))
13486             return 0;
13487
13488           ret = new_loc_descr (first_op, 0, 0);
13489           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13490           ret->dw_loc_oprnd1.v.val_addr = rtl;
13491           ret->dtprel = dtprel;
13492
13493           ret1 = new_loc_descr (second_op, 0, 0);
13494           add_loc_descr (&ret, ret1);
13495
13496           have_address = 1;
13497           break;
13498         }
13499       /* FALLTHRU */
13500
13501     case PARM_DECL:
13502     case RESULT_DECL:
13503       if (DECL_HAS_VALUE_EXPR_P (loc))
13504         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13505                                    want_address);
13506       /* FALLTHRU */
13507
13508     case FUNCTION_DECL:
13509       {
13510         rtx rtl;
13511         var_loc_list *loc_list = lookup_decl_loc (loc);
13512
13513         if (loc_list && loc_list->first)
13514           {
13515             list_ret = dw_loc_list (loc_list, loc, want_address);
13516             have_address = want_address != 0;
13517             break;
13518           }
13519         rtl = rtl_for_decl_location (loc);
13520         if (rtl == NULL_RTX)
13521           {
13522             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13523             return 0;
13524           }
13525         else if (CONST_INT_P (rtl))
13526           {
13527             HOST_WIDE_INT val = INTVAL (rtl);
13528             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13529               val &= GET_MODE_MASK (DECL_MODE (loc));
13530             ret = int_loc_descriptor (val);
13531           }
13532         else if (GET_CODE (rtl) == CONST_STRING)
13533           {
13534             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13535             return 0;
13536           }
13537         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13538           {
13539             ret = new_loc_descr (DW_OP_addr, 0, 0);
13540             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13541             ret->dw_loc_oprnd1.v.val_addr = rtl;
13542           }
13543         else
13544           {
13545             enum machine_mode mode, mem_mode;
13546
13547             /* Certain constructs can only be represented at top-level.  */
13548             if (want_address == 2)
13549               {
13550                 ret = loc_descriptor (rtl, VOIDmode,
13551                                       VAR_INIT_STATUS_INITIALIZED);
13552                 have_address = 1;
13553               }
13554             else
13555               {
13556                 mode = GET_MODE (rtl);
13557                 mem_mode = VOIDmode;
13558                 if (MEM_P (rtl))
13559                   {
13560                     mem_mode = mode;
13561                     mode = get_address_mode (rtl);
13562                     rtl = XEXP (rtl, 0);
13563                     have_address = 1;
13564                   }
13565                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13566                                           VAR_INIT_STATUS_INITIALIZED);
13567               }
13568             if (!ret)
13569               expansion_failed (loc, rtl,
13570                                 "failed to produce loc descriptor for rtl");
13571           }
13572       }
13573       break;
13574
13575     case MEM_REF:
13576       /* ??? FIXME.  */
13577       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13578         return 0;
13579       /* Fallthru.  */
13580     case INDIRECT_REF:
13581       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13582       have_address = 1;
13583       break;
13584
13585     case COMPOUND_EXPR:
13586       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13587
13588     CASE_CONVERT:
13589     case VIEW_CONVERT_EXPR:
13590     case SAVE_EXPR:
13591     case MODIFY_EXPR:
13592       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13593
13594     case COMPONENT_REF:
13595     case BIT_FIELD_REF:
13596     case ARRAY_REF:
13597     case ARRAY_RANGE_REF:
13598     case REALPART_EXPR:
13599     case IMAGPART_EXPR:
13600       {
13601         tree obj, offset;
13602         HOST_WIDE_INT bitsize, bitpos, bytepos;
13603         enum machine_mode mode;
13604         int volatilep;
13605         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13606
13607         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13608                                    &unsignedp, &volatilep, false);
13609
13610         gcc_assert (obj != loc);
13611
13612         list_ret = loc_list_from_tree (obj,
13613                                        want_address == 2
13614                                        && !bitpos && !offset ? 2 : 1);
13615         /* TODO: We can extract value of the small expression via shifting even
13616            for nonzero bitpos.  */
13617         if (list_ret == 0)
13618           return 0;
13619         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13620           {
13621             expansion_failed (loc, NULL_RTX,
13622                               "bitfield access");
13623             return 0;
13624           }
13625
13626         if (offset != NULL_TREE)
13627           {
13628             /* Variable offset.  */
13629             list_ret1 = loc_list_from_tree (offset, 0);
13630             if (list_ret1 == 0)
13631               return 0;
13632             add_loc_list (&list_ret, list_ret1);
13633             if (!list_ret)
13634               return 0;
13635             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13636           }
13637
13638         bytepos = bitpos / BITS_PER_UNIT;
13639         if (bytepos > 0)
13640           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13641         else if (bytepos < 0)
13642           loc_list_plus_const (list_ret, bytepos);
13643
13644         have_address = 1;
13645         break;
13646       }
13647
13648     case INTEGER_CST:
13649       if ((want_address || !host_integerp (loc, 0))
13650           && (ret = cst_pool_loc_descr (loc)))
13651         have_address = 1;
13652       else if (want_address == 2
13653                && host_integerp (loc, 0)
13654                && (ret = address_of_int_loc_descriptor
13655                            (int_size_in_bytes (TREE_TYPE (loc)),
13656                             tree_low_cst (loc, 0))))
13657         have_address = 1;
13658       else if (host_integerp (loc, 0))
13659         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13660       else
13661         {
13662           expansion_failed (loc, NULL_RTX,
13663                             "Integer operand is not host integer");
13664           return 0;
13665         }
13666       break;
13667
13668     case CONSTRUCTOR:
13669     case REAL_CST:
13670     case STRING_CST:
13671     case COMPLEX_CST:
13672       if ((ret = cst_pool_loc_descr (loc)))
13673         have_address = 1;
13674       else
13675       /* We can construct small constants here using int_loc_descriptor.  */
13676         expansion_failed (loc, NULL_RTX,
13677                           "constructor or constant not in constant pool");
13678       break;
13679
13680     case TRUTH_AND_EXPR:
13681     case TRUTH_ANDIF_EXPR:
13682     case BIT_AND_EXPR:
13683       op = DW_OP_and;
13684       goto do_binop;
13685
13686     case TRUTH_XOR_EXPR:
13687     case BIT_XOR_EXPR:
13688       op = DW_OP_xor;
13689       goto do_binop;
13690
13691     case TRUTH_OR_EXPR:
13692     case TRUTH_ORIF_EXPR:
13693     case BIT_IOR_EXPR:
13694       op = DW_OP_or;
13695       goto do_binop;
13696
13697     case FLOOR_DIV_EXPR:
13698     case CEIL_DIV_EXPR:
13699     case ROUND_DIV_EXPR:
13700     case TRUNC_DIV_EXPR:
13701       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13702         return 0;
13703       op = DW_OP_div;
13704       goto do_binop;
13705
13706     case MINUS_EXPR:
13707       op = DW_OP_minus;
13708       goto do_binop;
13709
13710     case FLOOR_MOD_EXPR:
13711     case CEIL_MOD_EXPR:
13712     case ROUND_MOD_EXPR:
13713     case TRUNC_MOD_EXPR:
13714       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13715         {
13716           op = DW_OP_mod;
13717           goto do_binop;
13718         }
13719       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13720       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13721       if (list_ret == 0 || list_ret1 == 0)
13722         return 0;
13723
13724       add_loc_list (&list_ret, list_ret1);
13725       if (list_ret == 0)
13726         return 0;
13727       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13728       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13729       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13730       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13731       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13732       break;
13733
13734     case MULT_EXPR:
13735       op = DW_OP_mul;
13736       goto do_binop;
13737
13738     case LSHIFT_EXPR:
13739       op = DW_OP_shl;
13740       goto do_binop;
13741
13742     case RSHIFT_EXPR:
13743       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13744       goto do_binop;
13745
13746     case POINTER_PLUS_EXPR:
13747     case PLUS_EXPR:
13748       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13749         {
13750           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13751           if (list_ret == 0)
13752             return 0;
13753
13754           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13755           break;
13756         }
13757
13758       op = DW_OP_plus;
13759       goto do_binop;
13760
13761     case LE_EXPR:
13762       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13763         return 0;
13764
13765       op = DW_OP_le;
13766       goto do_binop;
13767
13768     case GE_EXPR:
13769       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13770         return 0;
13771
13772       op = DW_OP_ge;
13773       goto do_binop;
13774
13775     case LT_EXPR:
13776       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13777         return 0;
13778
13779       op = DW_OP_lt;
13780       goto do_binop;
13781
13782     case GT_EXPR:
13783       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13784         return 0;
13785
13786       op = DW_OP_gt;
13787       goto do_binop;
13788
13789     case EQ_EXPR:
13790       op = DW_OP_eq;
13791       goto do_binop;
13792
13793     case NE_EXPR:
13794       op = DW_OP_ne;
13795       goto do_binop;
13796
13797     do_binop:
13798       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13799       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13800       if (list_ret == 0 || list_ret1 == 0)
13801         return 0;
13802
13803       add_loc_list (&list_ret, list_ret1);
13804       if (list_ret == 0)
13805         return 0;
13806       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13807       break;
13808
13809     case TRUTH_NOT_EXPR:
13810     case BIT_NOT_EXPR:
13811       op = DW_OP_not;
13812       goto do_unop;
13813
13814     case ABS_EXPR:
13815       op = DW_OP_abs;
13816       goto do_unop;
13817
13818     case NEGATE_EXPR:
13819       op = DW_OP_neg;
13820       goto do_unop;
13821
13822     do_unop:
13823       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13824       if (list_ret == 0)
13825         return 0;
13826
13827       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13828       break;
13829
13830     case MIN_EXPR:
13831     case MAX_EXPR:
13832       {
13833         const enum tree_code code =
13834           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13835
13836         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13837                       build2 (code, integer_type_node,
13838                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13839                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13840       }
13841
13842       /* ... fall through ...  */
13843
13844     case COND_EXPR:
13845       {
13846         dw_loc_descr_ref lhs
13847           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13848         dw_loc_list_ref rhs
13849           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13850         dw_loc_descr_ref bra_node, jump_node, tmp;
13851
13852         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13853         if (list_ret == 0 || lhs == 0 || rhs == 0)
13854           return 0;
13855
13856         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13857         add_loc_descr_to_each (list_ret, bra_node);
13858
13859         add_loc_list (&list_ret, rhs);
13860         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13861         add_loc_descr_to_each (list_ret, jump_node);
13862
13863         add_loc_descr_to_each (list_ret, lhs);
13864         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13865         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13866
13867         /* ??? Need a node to point the skip at.  Use a nop.  */
13868         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13869         add_loc_descr_to_each (list_ret, tmp);
13870         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13871         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13872       }
13873       break;
13874
13875     case FIX_TRUNC_EXPR:
13876       return 0;
13877
13878     default:
13879       /* Leave front-end specific codes as simply unknown.  This comes
13880          up, for instance, with the C STMT_EXPR.  */
13881       if ((unsigned int) TREE_CODE (loc)
13882           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13883         {
13884           expansion_failed (loc, NULL_RTX,
13885                             "language specific tree node");
13886           return 0;
13887         }
13888
13889 #ifdef ENABLE_CHECKING
13890       /* Otherwise this is a generic code; we should just lists all of
13891          these explicitly.  We forgot one.  */
13892       gcc_unreachable ();
13893 #else
13894       /* In a release build, we want to degrade gracefully: better to
13895          generate incomplete debugging information than to crash.  */
13896       return NULL;
13897 #endif
13898     }
13899
13900   if (!ret && !list_ret)
13901     return 0;
13902
13903   if (want_address == 2 && !have_address
13904       && (dwarf_version >= 4 || !dwarf_strict))
13905     {
13906       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13907         {
13908           expansion_failed (loc, NULL_RTX,
13909                             "DWARF address size mismatch");
13910           return 0;
13911         }
13912       if (ret)
13913         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13914       else
13915         add_loc_descr_to_each (list_ret,
13916                                new_loc_descr (DW_OP_stack_value, 0, 0));
13917       have_address = 1;
13918     }
13919   /* Show if we can't fill the request for an address.  */
13920   if (want_address && !have_address)
13921     {
13922       expansion_failed (loc, NULL_RTX,
13923                         "Want address and only have value");
13924       return 0;
13925     }
13926
13927   gcc_assert (!ret || !list_ret);
13928
13929   /* If we've got an address and don't want one, dereference.  */
13930   if (!want_address && have_address)
13931     {
13932       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13933
13934       if (size > DWARF2_ADDR_SIZE || size == -1)
13935         {
13936           expansion_failed (loc, NULL_RTX,
13937                             "DWARF address size mismatch");
13938           return 0;
13939         }
13940       else if (size == DWARF2_ADDR_SIZE)
13941         op = DW_OP_deref;
13942       else
13943         op = DW_OP_deref_size;
13944
13945       if (ret)
13946         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13947       else
13948         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13949     }
13950   if (ret)
13951     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13952
13953   return list_ret;
13954 }
13955
13956 /* Same as above but return only single location expression.  */
13957 static dw_loc_descr_ref
13958 loc_descriptor_from_tree (tree loc, int want_address)
13959 {
13960   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13961   if (!ret)
13962     return NULL;
13963   if (ret->dw_loc_next)
13964     {
13965       expansion_failed (loc, NULL_RTX,
13966                         "Location list where only loc descriptor needed");
13967       return NULL;
13968     }
13969   return ret->expr;
13970 }
13971
13972 /* Given a value, round it up to the lowest multiple of `boundary'
13973    which is not less than the value itself.  */
13974
13975 static inline HOST_WIDE_INT
13976 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13977 {
13978   return (((value + boundary - 1) / boundary) * boundary);
13979 }
13980
13981 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13982    pointer to the declared type for the relevant field variable, or return
13983    `integer_type_node' if the given node turns out to be an
13984    ERROR_MARK node.  */
13985
13986 static inline tree
13987 field_type (const_tree decl)
13988 {
13989   tree type;
13990
13991   if (TREE_CODE (decl) == ERROR_MARK)
13992     return integer_type_node;
13993
13994   type = DECL_BIT_FIELD_TYPE (decl);
13995   if (type == NULL_TREE)
13996     type = TREE_TYPE (decl);
13997
13998   return type;
13999 }
14000
14001 /* Given a pointer to a tree node, return the alignment in bits for
14002    it, or else return BITS_PER_WORD if the node actually turns out to
14003    be an ERROR_MARK node.  */
14004
14005 static inline unsigned
14006 simple_type_align_in_bits (const_tree type)
14007 {
14008   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14009 }
14010
14011 static inline unsigned
14012 simple_decl_align_in_bits (const_tree decl)
14013 {
14014   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14015 }
14016
14017 /* Return the result of rounding T up to ALIGN.  */
14018
14019 static inline double_int
14020 round_up_to_align (double_int t, unsigned int align)
14021 {
14022   double_int alignd = uhwi_to_double_int (align);
14023   t = double_int_add (t, alignd);
14024   t = double_int_add (t, double_int_minus_one);
14025   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14026   t = double_int_mul (t, alignd);
14027   return t;
14028 }
14029
14030 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14031    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14032    or return 0 if we are unable to determine what that offset is, either
14033    because the argument turns out to be a pointer to an ERROR_MARK node, or
14034    because the offset is actually variable.  (We can't handle the latter case
14035    just yet).  */
14036
14037 static HOST_WIDE_INT
14038 field_byte_offset (const_tree decl)
14039 {
14040   double_int object_offset_in_bits;
14041   double_int object_offset_in_bytes;
14042   double_int bitpos_int;
14043
14044   if (TREE_CODE (decl) == ERROR_MARK)
14045     return 0;
14046
14047   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14048
14049   /* We cannot yet cope with fields whose positions are variable, so
14050      for now, when we see such things, we simply return 0.  Someday, we may
14051      be able to handle such cases, but it will be damn difficult.  */
14052   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14053     return 0;
14054
14055   bitpos_int = tree_to_double_int (bit_position (decl));
14056
14057 #ifdef PCC_BITFIELD_TYPE_MATTERS
14058   if (PCC_BITFIELD_TYPE_MATTERS)
14059     {
14060       tree type;
14061       tree field_size_tree;
14062       double_int deepest_bitpos;
14063       double_int field_size_in_bits;
14064       unsigned int type_align_in_bits;
14065       unsigned int decl_align_in_bits;
14066       double_int type_size_in_bits;
14067
14068       type = field_type (decl);
14069       type_size_in_bits = double_int_type_size_in_bits (type);
14070       type_align_in_bits = simple_type_align_in_bits (type);
14071
14072       field_size_tree = DECL_SIZE (decl);
14073
14074       /* The size could be unspecified if there was an error, or for
14075          a flexible array member.  */
14076       if (!field_size_tree)
14077         field_size_tree = bitsize_zero_node;
14078
14079       /* If the size of the field is not constant, use the type size.  */
14080       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14081         field_size_in_bits = tree_to_double_int (field_size_tree);
14082       else
14083         field_size_in_bits = type_size_in_bits;
14084
14085       decl_align_in_bits = simple_decl_align_in_bits (decl);
14086
14087       /* The GCC front-end doesn't make any attempt to keep track of the
14088          starting bit offset (relative to the start of the containing
14089          structure type) of the hypothetical "containing object" for a
14090          bit-field.  Thus, when computing the byte offset value for the
14091          start of the "containing object" of a bit-field, we must deduce
14092          this information on our own. This can be rather tricky to do in
14093          some cases.  For example, handling the following structure type
14094          definition when compiling for an i386/i486 target (which only
14095          aligns long long's to 32-bit boundaries) can be very tricky:
14096
14097          struct S { int field1; long long field2:31; };
14098
14099          Fortunately, there is a simple rule-of-thumb which can be used
14100          in such cases.  When compiling for an i386/i486, GCC will
14101          allocate 8 bytes for the structure shown above.  It decides to
14102          do this based upon one simple rule for bit-field allocation.
14103          GCC allocates each "containing object" for each bit-field at
14104          the first (i.e. lowest addressed) legitimate alignment boundary
14105          (based upon the required minimum alignment for the declared
14106          type of the field) which it can possibly use, subject to the
14107          condition that there is still enough available space remaining
14108          in the containing object (when allocated at the selected point)
14109          to fully accommodate all of the bits of the bit-field itself.
14110
14111          This simple rule makes it obvious why GCC allocates 8 bytes for
14112          each object of the structure type shown above.  When looking
14113          for a place to allocate the "containing object" for `field2',
14114          the compiler simply tries to allocate a 64-bit "containing
14115          object" at each successive 32-bit boundary (starting at zero)
14116          until it finds a place to allocate that 64- bit field such that
14117          at least 31 contiguous (and previously unallocated) bits remain
14118          within that selected 64 bit field.  (As it turns out, for the
14119          example above, the compiler finds it is OK to allocate the
14120          "containing object" 64-bit field at bit-offset zero within the
14121          structure type.)
14122
14123          Here we attempt to work backwards from the limited set of facts
14124          we're given, and we try to deduce from those facts, where GCC
14125          must have believed that the containing object started (within
14126          the structure type). The value we deduce is then used (by the
14127          callers of this routine) to generate DW_AT_location and
14128          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14129          the case of DW_AT_location, regular fields as well).  */
14130
14131       /* Figure out the bit-distance from the start of the structure to
14132          the "deepest" bit of the bit-field.  */
14133       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14134
14135       /* This is the tricky part.  Use some fancy footwork to deduce
14136          where the lowest addressed bit of the containing object must
14137          be.  */
14138       object_offset_in_bits
14139         = double_int_sub (deepest_bitpos, type_size_in_bits);
14140
14141       /* Round up to type_align by default.  This works best for
14142          bitfields.  */
14143       object_offset_in_bits
14144         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14145
14146       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14147         {
14148           object_offset_in_bits
14149             = double_int_sub (deepest_bitpos, type_size_in_bits);
14150
14151           /* Round up to decl_align instead.  */
14152           object_offset_in_bits
14153             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14154         }
14155     }
14156   else
14157 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14158     object_offset_in_bits = bitpos_int;
14159
14160   object_offset_in_bytes
14161     = double_int_div (object_offset_in_bits,
14162                       uhwi_to_double_int (BITS_PER_UNIT), true,
14163                       TRUNC_DIV_EXPR);
14164   return double_int_to_shwi (object_offset_in_bytes);
14165 }
14166 \f
14167 /* The following routines define various Dwarf attributes and any data
14168    associated with them.  */
14169
14170 /* Add a location description attribute value to a DIE.
14171
14172    This emits location attributes suitable for whole variables and
14173    whole parameters.  Note that the location attributes for struct fields are
14174    generated by the routine `data_member_location_attribute' below.  */
14175
14176 static inline void
14177 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14178                              dw_loc_list_ref descr)
14179 {
14180   if (descr == 0)
14181     return;
14182   if (single_element_loc_list_p (descr))
14183     add_AT_loc (die, attr_kind, descr->expr);
14184   else
14185     add_AT_loc_list (die, attr_kind, descr);
14186 }
14187
14188 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14189
14190 static void
14191 add_accessibility_attribute (dw_die_ref die, tree decl)
14192 {
14193   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14194      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14195      the default has always been DW_ACCESS_public.  */
14196   if (TREE_PROTECTED (decl))
14197     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14198   else if (TREE_PRIVATE (decl))
14199     {
14200       if (dwarf_version == 2
14201           || die->die_parent == NULL
14202           || die->die_parent->die_tag != DW_TAG_class_type)
14203         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14204     }
14205   else if (dwarf_version > 2
14206            && die->die_parent
14207            && die->die_parent->die_tag == DW_TAG_class_type)
14208     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14209 }
14210
14211 /* Attach the specialized form of location attribute used for data members of
14212    struct and union types.  In the special case of a FIELD_DECL node which
14213    represents a bit-field, the "offset" part of this special location
14214    descriptor must indicate the distance in bytes from the lowest-addressed
14215    byte of the containing struct or union type to the lowest-addressed byte of
14216    the "containing object" for the bit-field.  (See the `field_byte_offset'
14217    function above).
14218
14219    For any given bit-field, the "containing object" is a hypothetical object
14220    (of some integral or enum type) within which the given bit-field lives.  The
14221    type of this hypothetical "containing object" is always the same as the
14222    declared type of the individual bit-field itself (for GCC anyway... the
14223    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14224    bytes) of the hypothetical "containing object" which will be given in the
14225    DW_AT_byte_size attribute for this bit-field.  (See the
14226    `byte_size_attribute' function below.)  It is also used when calculating the
14227    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14228    function below.)  */
14229
14230 static void
14231 add_data_member_location_attribute (dw_die_ref die, tree decl)
14232 {
14233   HOST_WIDE_INT offset;
14234   dw_loc_descr_ref loc_descr = 0;
14235
14236   if (TREE_CODE (decl) == TREE_BINFO)
14237     {
14238       /* We're working on the TAG_inheritance for a base class.  */
14239       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14240         {
14241           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14242              aren't at a fixed offset from all (sub)objects of the same
14243              type.  We need to extract the appropriate offset from our
14244              vtable.  The following dwarf expression means
14245
14246                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14247
14248              This is specific to the V3 ABI, of course.  */
14249
14250           dw_loc_descr_ref tmp;
14251
14252           /* Make a copy of the object address.  */
14253           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14254           add_loc_descr (&loc_descr, tmp);
14255
14256           /* Extract the vtable address.  */
14257           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14258           add_loc_descr (&loc_descr, tmp);
14259
14260           /* Calculate the address of the offset.  */
14261           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14262           gcc_assert (offset < 0);
14263
14264           tmp = int_loc_descriptor (-offset);
14265           add_loc_descr (&loc_descr, tmp);
14266           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14267           add_loc_descr (&loc_descr, tmp);
14268
14269           /* Extract the offset.  */
14270           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14271           add_loc_descr (&loc_descr, tmp);
14272
14273           /* Add it to the object address.  */
14274           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14275           add_loc_descr (&loc_descr, tmp);
14276         }
14277       else
14278         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14279     }
14280   else
14281     offset = field_byte_offset (decl);
14282
14283   if (! loc_descr)
14284     {
14285       if (dwarf_version > 2)
14286         {
14287           /* Don't need to output a location expression, just the constant. */
14288           if (offset < 0)
14289             add_AT_int (die, DW_AT_data_member_location, offset);
14290           else
14291             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14292           return;
14293         }
14294       else
14295         {
14296           enum dwarf_location_atom op;
14297
14298           /* The DWARF2 standard says that we should assume that the structure
14299              address is already on the stack, so we can specify a structure
14300              field address by using DW_OP_plus_uconst.  */
14301
14302 #ifdef MIPS_DEBUGGING_INFO
14303           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14304              operator correctly.  It works only if we leave the offset on the
14305              stack.  */
14306           op = DW_OP_constu;
14307 #else
14308           op = DW_OP_plus_uconst;
14309 #endif
14310
14311           loc_descr = new_loc_descr (op, offset, 0);
14312         }
14313     }
14314
14315   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14316 }
14317
14318 /* Writes integer values to dw_vec_const array.  */
14319
14320 static void
14321 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14322 {
14323   while (size != 0)
14324     {
14325       *dest++ = val & 0xff;
14326       val >>= 8;
14327       --size;
14328     }
14329 }
14330
14331 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14332
14333 static HOST_WIDE_INT
14334 extract_int (const unsigned char *src, unsigned int size)
14335 {
14336   HOST_WIDE_INT val = 0;
14337
14338   src += size;
14339   while (size != 0)
14340     {
14341       val <<= 8;
14342       val |= *--src & 0xff;
14343       --size;
14344     }
14345   return val;
14346 }
14347
14348 /* Writes double_int values to dw_vec_const array.  */
14349
14350 static void
14351 insert_double (double_int val, unsigned char *dest)
14352 {
14353   unsigned char *p0 = dest;
14354   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14355
14356   if (WORDS_BIG_ENDIAN)
14357     {
14358       p0 = p1;
14359       p1 = dest;
14360     }
14361
14362   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14363   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14364 }
14365
14366 /* Writes floating point values to dw_vec_const array.  */
14367
14368 static void
14369 insert_float (const_rtx rtl, unsigned char *array)
14370 {
14371   REAL_VALUE_TYPE rv;
14372   long val[4];
14373   int i;
14374
14375   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14376   real_to_target (val, &rv, GET_MODE (rtl));
14377
14378   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14379   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14380     {
14381       insert_int (val[i], 4, array);
14382       array += 4;
14383     }
14384 }
14385
14386 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14387    does not have a "location" either in memory or in a register.  These
14388    things can arise in GNU C when a constant is passed as an actual parameter
14389    to an inlined function.  They can also arise in C++ where declared
14390    constants do not necessarily get memory "homes".  */
14391
14392 static bool
14393 add_const_value_attribute (dw_die_ref die, rtx rtl)
14394 {
14395   switch (GET_CODE (rtl))
14396     {
14397     case CONST_INT:
14398       {
14399         HOST_WIDE_INT val = INTVAL (rtl);
14400
14401         if (val < 0)
14402           add_AT_int (die, DW_AT_const_value, val);
14403         else
14404           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14405       }
14406       return true;
14407
14408     case CONST_DOUBLE:
14409       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14410          floating-point constant.  A CONST_DOUBLE is used whenever the
14411          constant requires more than one word in order to be adequately
14412          represented.  */
14413       {
14414         enum machine_mode mode = GET_MODE (rtl);
14415
14416         if (SCALAR_FLOAT_MODE_P (mode))
14417           {
14418             unsigned int length = GET_MODE_SIZE (mode);
14419             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14420
14421             insert_float (rtl, array);
14422             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14423           }
14424         else
14425           add_AT_double (die, DW_AT_const_value,
14426                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14427       }
14428       return true;
14429
14430     case CONST_VECTOR:
14431       {
14432         enum machine_mode mode = GET_MODE (rtl);
14433         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14434         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14435         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14436           (length * elt_size);
14437         unsigned int i;
14438         unsigned char *p;
14439
14440         switch (GET_MODE_CLASS (mode))
14441           {
14442           case MODE_VECTOR_INT:
14443             for (i = 0, p = array; i < length; i++, p += elt_size)
14444               {
14445                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14446                 double_int val = rtx_to_double_int (elt);
14447
14448                 if (elt_size <= sizeof (HOST_WIDE_INT))
14449                   insert_int (double_int_to_shwi (val), elt_size, p);
14450                 else
14451                   {
14452                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14453                     insert_double (val, p);
14454                   }
14455               }
14456             break;
14457
14458           case MODE_VECTOR_FLOAT:
14459             for (i = 0, p = array; i < length; i++, p += elt_size)
14460               {
14461                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14462                 insert_float (elt, p);
14463               }
14464             break;
14465
14466           default:
14467             gcc_unreachable ();
14468           }
14469
14470         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14471       }
14472       return true;
14473
14474     case CONST_STRING:
14475       if (dwarf_version >= 4 || !dwarf_strict)
14476         {
14477           dw_loc_descr_ref loc_result;
14478           resolve_one_addr (&rtl, NULL);
14479         rtl_addr:
14480           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14481           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14482           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14483           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14484           add_AT_loc (die, DW_AT_location, loc_result);
14485           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14486           return true;
14487         }
14488       return false;
14489
14490     case CONST:
14491       if (CONSTANT_P (XEXP (rtl, 0)))
14492         return add_const_value_attribute (die, XEXP (rtl, 0));
14493       /* FALLTHROUGH */
14494     case SYMBOL_REF:
14495       if (!const_ok_for_output (rtl))
14496         return false;
14497     case LABEL_REF:
14498       if (dwarf_version >= 4 || !dwarf_strict)
14499         goto rtl_addr;
14500       return false;
14501
14502     case PLUS:
14503       /* In cases where an inlined instance of an inline function is passed
14504          the address of an `auto' variable (which is local to the caller) we
14505          can get a situation where the DECL_RTL of the artificial local
14506          variable (for the inlining) which acts as a stand-in for the
14507          corresponding formal parameter (of the inline function) will look
14508          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14509          exactly a compile-time constant expression, but it isn't the address
14510          of the (artificial) local variable either.  Rather, it represents the
14511          *value* which the artificial local variable always has during its
14512          lifetime.  We currently have no way to represent such quasi-constant
14513          values in Dwarf, so for now we just punt and generate nothing.  */
14514       return false;
14515
14516     case HIGH:
14517     case CONST_FIXED:
14518       return false;
14519
14520     case MEM:
14521       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14522           && MEM_READONLY_P (rtl)
14523           && GET_MODE (rtl) == BLKmode)
14524         {
14525           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14526           return true;
14527         }
14528       return false;
14529
14530     default:
14531       /* No other kinds of rtx should be possible here.  */
14532       gcc_unreachable ();
14533     }
14534   return false;
14535 }
14536
14537 /* Determine whether the evaluation of EXPR references any variables
14538    or functions which aren't otherwise used (and therefore may not be
14539    output).  */
14540 static tree
14541 reference_to_unused (tree * tp, int * walk_subtrees,
14542                      void * data ATTRIBUTE_UNUSED)
14543 {
14544   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14545     *walk_subtrees = 0;
14546
14547   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14548       && ! TREE_ASM_WRITTEN (*tp))
14549     return *tp;
14550   /* ???  The C++ FE emits debug information for using decls, so
14551      putting gcc_unreachable here falls over.  See PR31899.  For now
14552      be conservative.  */
14553   else if (!cgraph_global_info_ready
14554            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14555     return *tp;
14556   else if (TREE_CODE (*tp) == VAR_DECL)
14557     {
14558       struct varpool_node *node = varpool_get_node (*tp);
14559       if (!node || !node->needed)
14560         return *tp;
14561     }
14562   else if (TREE_CODE (*tp) == FUNCTION_DECL
14563            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14564     {
14565       /* The call graph machinery must have finished analyzing,
14566          optimizing and gimplifying the CU by now.
14567          So if *TP has no call graph node associated
14568          to it, it means *TP will not be emitted.  */
14569       if (!cgraph_get_node (*tp))
14570         return *tp;
14571     }
14572   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14573     return *tp;
14574
14575   return NULL_TREE;
14576 }
14577
14578 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14579    for use in a later add_const_value_attribute call.  */
14580
14581 static rtx
14582 rtl_for_decl_init (tree init, tree type)
14583 {
14584   rtx rtl = NULL_RTX;
14585
14586   STRIP_NOPS (init);
14587
14588   /* If a variable is initialized with a string constant without embedded
14589      zeros, build CONST_STRING.  */
14590   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14591     {
14592       tree enttype = TREE_TYPE (type);
14593       tree domain = TYPE_DOMAIN (type);
14594       enum machine_mode mode = TYPE_MODE (enttype);
14595
14596       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14597           && domain
14598           && integer_zerop (TYPE_MIN_VALUE (domain))
14599           && compare_tree_int (TYPE_MAX_VALUE (domain),
14600                                TREE_STRING_LENGTH (init) - 1) == 0
14601           && ((size_t) TREE_STRING_LENGTH (init)
14602               == strlen (TREE_STRING_POINTER (init)) + 1))
14603         {
14604           rtl = gen_rtx_CONST_STRING (VOIDmode,
14605                                       ggc_strdup (TREE_STRING_POINTER (init)));
14606           rtl = gen_rtx_MEM (BLKmode, rtl);
14607           MEM_READONLY_P (rtl) = 1;
14608         }
14609     }
14610   /* Other aggregates, and complex values, could be represented using
14611      CONCAT: FIXME!  */
14612   else if (AGGREGATE_TYPE_P (type)
14613            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14614                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14615            || TREE_CODE (type) == COMPLEX_TYPE)
14616     ;
14617   /* Vectors only work if their mode is supported by the target.
14618      FIXME: generic vectors ought to work too.  */
14619   else if (TREE_CODE (type) == VECTOR_TYPE
14620            && !VECTOR_MODE_P (TYPE_MODE (type)))
14621     ;
14622   /* If the initializer is something that we know will expand into an
14623      immediate RTL constant, expand it now.  We must be careful not to
14624      reference variables which won't be output.  */
14625   else if (initializer_constant_valid_p (init, type)
14626            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14627     {
14628       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14629          possible.  */
14630       if (TREE_CODE (type) == VECTOR_TYPE)
14631         switch (TREE_CODE (init))
14632           {
14633           case VECTOR_CST:
14634             break;
14635           case CONSTRUCTOR:
14636             if (TREE_CONSTANT (init))
14637               {
14638                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14639                 bool constant_p = true;
14640                 tree value;
14641                 unsigned HOST_WIDE_INT ix;
14642
14643                 /* Even when ctor is constant, it might contain non-*_CST
14644                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14645                    belong into VECTOR_CST nodes.  */
14646                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14647                   if (!CONSTANT_CLASS_P (value))
14648                     {
14649                       constant_p = false;
14650                       break;
14651                     }
14652
14653                 if (constant_p)
14654                   {
14655                     init = build_vector_from_ctor (type, elts);
14656                     break;
14657                   }
14658               }
14659             /* FALLTHRU */
14660
14661           default:
14662             return NULL;
14663           }
14664
14665       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14666
14667       /* If expand_expr returns a MEM, it wasn't immediate.  */
14668       gcc_assert (!rtl || !MEM_P (rtl));
14669     }
14670
14671   return rtl;
14672 }
14673
14674 /* Generate RTL for the variable DECL to represent its location.  */
14675
14676 static rtx
14677 rtl_for_decl_location (tree decl)
14678 {
14679   rtx rtl;
14680
14681   /* Here we have to decide where we are going to say the parameter "lives"
14682      (as far as the debugger is concerned).  We only have a couple of
14683      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14684
14685      DECL_RTL normally indicates where the parameter lives during most of the
14686      activation of the function.  If optimization is enabled however, this
14687      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14688      that the parameter doesn't really live anywhere (as far as the code
14689      generation parts of GCC are concerned) during most of the function's
14690      activation.  That will happen (for example) if the parameter is never
14691      referenced within the function.
14692
14693      We could just generate a location descriptor here for all non-NULL
14694      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14695      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14696      where DECL_RTL is NULL or is a pseudo-reg.
14697
14698      Note however that we can only get away with using DECL_INCOMING_RTL as
14699      a backup substitute for DECL_RTL in certain limited cases.  In cases
14700      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14701      we can be sure that the parameter was passed using the same type as it is
14702      declared to have within the function, and that its DECL_INCOMING_RTL
14703      points us to a place where a value of that type is passed.
14704
14705      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14706      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14707      because in these cases DECL_INCOMING_RTL points us to a value of some
14708      type which is *different* from the type of the parameter itself.  Thus,
14709      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14710      such cases, the debugger would end up (for example) trying to fetch a
14711      `float' from a place which actually contains the first part of a
14712      `double'.  That would lead to really incorrect and confusing
14713      output at debug-time.
14714
14715      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14716      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14717      are a couple of exceptions however.  On little-endian machines we can
14718      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14719      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14720      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14721      when (on a little-endian machine) a non-prototyped function has a
14722      parameter declared to be of type `short' or `char'.  In such cases,
14723      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14724      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14725      passed `int' value.  If the debugger then uses that address to fetch
14726      a `short' or a `char' (on a little-endian machine) the result will be
14727      the correct data, so we allow for such exceptional cases below.
14728
14729      Note that our goal here is to describe the place where the given formal
14730      parameter lives during most of the function's activation (i.e. between the
14731      end of the prologue and the start of the epilogue).  We'll do that as best
14732      as we can. Note however that if the given formal parameter is modified
14733      sometime during the execution of the function, then a stack backtrace (at
14734      debug-time) will show the function as having been called with the *new*
14735      value rather than the value which was originally passed in.  This happens
14736      rarely enough that it is not a major problem, but it *is* a problem, and
14737      I'd like to fix it.
14738
14739      A future version of dwarf2out.c may generate two additional attributes for
14740      any given DW_TAG_formal_parameter DIE which will describe the "passed
14741      type" and the "passed location" for the given formal parameter in addition
14742      to the attributes we now generate to indicate the "declared type" and the
14743      "active location" for each parameter.  This additional set of attributes
14744      could be used by debuggers for stack backtraces. Separately, note that
14745      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14746      This happens (for example) for inlined-instances of inline function formal
14747      parameters which are never referenced.  This really shouldn't be
14748      happening.  All PARM_DECL nodes should get valid non-NULL
14749      DECL_INCOMING_RTL values.  FIXME.  */
14750
14751   /* Use DECL_RTL as the "location" unless we find something better.  */
14752   rtl = DECL_RTL_IF_SET (decl);
14753
14754   /* When generating abstract instances, ignore everything except
14755      constants, symbols living in memory, and symbols living in
14756      fixed registers.  */
14757   if (! reload_completed)
14758     {
14759       if (rtl
14760           && (CONSTANT_P (rtl)
14761               || (MEM_P (rtl)
14762                   && CONSTANT_P (XEXP (rtl, 0)))
14763               || (REG_P (rtl)
14764                   && TREE_CODE (decl) == VAR_DECL
14765                   && TREE_STATIC (decl))))
14766         {
14767           rtl = targetm.delegitimize_address (rtl);
14768           return rtl;
14769         }
14770       rtl = NULL_RTX;
14771     }
14772   else if (TREE_CODE (decl) == PARM_DECL)
14773     {
14774       if (rtl == NULL_RTX
14775           || is_pseudo_reg (rtl)
14776           || (MEM_P (rtl)
14777               && is_pseudo_reg (XEXP (rtl, 0))
14778               && DECL_INCOMING_RTL (decl)
14779               && MEM_P (DECL_INCOMING_RTL (decl))
14780               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14781         {
14782           tree declared_type = TREE_TYPE (decl);
14783           tree passed_type = DECL_ARG_TYPE (decl);
14784           enum machine_mode dmode = TYPE_MODE (declared_type);
14785           enum machine_mode pmode = TYPE_MODE (passed_type);
14786
14787           /* This decl represents a formal parameter which was optimized out.
14788              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14789              all cases where (rtl == NULL_RTX) just below.  */
14790           if (dmode == pmode)
14791             rtl = DECL_INCOMING_RTL (decl);
14792           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14793                    && SCALAR_INT_MODE_P (dmode)
14794                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14795                    && DECL_INCOMING_RTL (decl))
14796             {
14797               rtx inc = DECL_INCOMING_RTL (decl);
14798               if (REG_P (inc))
14799                 rtl = inc;
14800               else if (MEM_P (inc))
14801                 {
14802                   if (BYTES_BIG_ENDIAN)
14803                     rtl = adjust_address_nv (inc, dmode,
14804                                              GET_MODE_SIZE (pmode)
14805                                              - GET_MODE_SIZE (dmode));
14806                   else
14807                     rtl = inc;
14808                 }
14809             }
14810         }
14811
14812       /* If the parm was passed in registers, but lives on the stack, then
14813          make a big endian correction if the mode of the type of the
14814          parameter is not the same as the mode of the rtl.  */
14815       /* ??? This is the same series of checks that are made in dbxout.c before
14816          we reach the big endian correction code there.  It isn't clear if all
14817          of these checks are necessary here, but keeping them all is the safe
14818          thing to do.  */
14819       else if (MEM_P (rtl)
14820                && XEXP (rtl, 0) != const0_rtx
14821                && ! CONSTANT_P (XEXP (rtl, 0))
14822                /* Not passed in memory.  */
14823                && !MEM_P (DECL_INCOMING_RTL (decl))
14824                /* Not passed by invisible reference.  */
14825                && (!REG_P (XEXP (rtl, 0))
14826                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14827                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14828 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14829                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14830 #endif
14831                      )
14832                /* Big endian correction check.  */
14833                && BYTES_BIG_ENDIAN
14834                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14835                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14836                    < UNITS_PER_WORD))
14837         {
14838           int offset = (UNITS_PER_WORD
14839                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14840
14841           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14842                              plus_constant (XEXP (rtl, 0), offset));
14843         }
14844     }
14845   else if (TREE_CODE (decl) == VAR_DECL
14846            && rtl
14847            && MEM_P (rtl)
14848            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14849            && BYTES_BIG_ENDIAN)
14850     {
14851       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14852       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14853
14854       /* If a variable is declared "register" yet is smaller than
14855          a register, then if we store the variable to memory, it
14856          looks like we're storing a register-sized value, when in
14857          fact we are not.  We need to adjust the offset of the
14858          storage location to reflect the actual value's bytes,
14859          else gdb will not be able to display it.  */
14860       if (rsize > dsize)
14861         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14862                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14863     }
14864
14865   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14866      and will have been substituted directly into all expressions that use it.
14867      C does not have such a concept, but C++ and other languages do.  */
14868   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14869     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14870
14871   if (rtl)
14872     rtl = targetm.delegitimize_address (rtl);
14873
14874   /* If we don't look past the constant pool, we risk emitting a
14875      reference to a constant pool entry that isn't referenced from
14876      code, and thus is not emitted.  */
14877   if (rtl)
14878     rtl = avoid_constant_pool_reference (rtl);
14879
14880   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14881      in the current CU, resolve_addr will remove the expression referencing
14882      it.  */
14883   if (rtl == NULL_RTX
14884       && TREE_CODE (decl) == VAR_DECL
14885       && !DECL_EXTERNAL (decl)
14886       && TREE_STATIC (decl)
14887       && DECL_NAME (decl)
14888       && !DECL_HARD_REGISTER (decl)
14889       && DECL_MODE (decl) != VOIDmode)
14890     {
14891       rtl = make_decl_rtl_for_debug (decl);
14892       if (!MEM_P (rtl)
14893           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14894           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14895         rtl = NULL_RTX;
14896     }
14897
14898   return rtl;
14899 }
14900
14901 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14902    returned.  If so, the decl for the COMMON block is returned, and the
14903    value is the offset into the common block for the symbol.  */
14904
14905 static tree
14906 fortran_common (tree decl, HOST_WIDE_INT *value)
14907 {
14908   tree val_expr, cvar;
14909   enum machine_mode mode;
14910   HOST_WIDE_INT bitsize, bitpos;
14911   tree offset;
14912   int volatilep = 0, unsignedp = 0;
14913
14914   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14915      it does not have a value (the offset into the common area), or if it
14916      is thread local (as opposed to global) then it isn't common, and shouldn't
14917      be handled as such.  */
14918   if (TREE_CODE (decl) != VAR_DECL
14919       || !TREE_STATIC (decl)
14920       || !DECL_HAS_VALUE_EXPR_P (decl)
14921       || !is_fortran ())
14922     return NULL_TREE;
14923
14924   val_expr = DECL_VALUE_EXPR (decl);
14925   if (TREE_CODE (val_expr) != COMPONENT_REF)
14926     return NULL_TREE;
14927
14928   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14929                               &mode, &unsignedp, &volatilep, true);
14930
14931   if (cvar == NULL_TREE
14932       || TREE_CODE (cvar) != VAR_DECL
14933       || DECL_ARTIFICIAL (cvar)
14934       || !TREE_PUBLIC (cvar))
14935     return NULL_TREE;
14936
14937   *value = 0;
14938   if (offset != NULL)
14939     {
14940       if (!host_integerp (offset, 0))
14941         return NULL_TREE;
14942       *value = tree_low_cst (offset, 0);
14943     }
14944   if (bitpos != 0)
14945     *value += bitpos / BITS_PER_UNIT;
14946
14947   return cvar;
14948 }
14949
14950 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14951    data attribute for a variable or a parameter.  We generate the
14952    DW_AT_const_value attribute only in those cases where the given variable
14953    or parameter does not have a true "location" either in memory or in a
14954    register.  This can happen (for example) when a constant is passed as an
14955    actual argument in a call to an inline function.  (It's possible that
14956    these things can crop up in other ways also.)  Note that one type of
14957    constant value which can be passed into an inlined function is a constant
14958    pointer.  This can happen for example if an actual argument in an inlined
14959    function call evaluates to a compile-time constant address.
14960
14961    CACHE_P is true if it is worth caching the location list for DECL,
14962    so that future calls can reuse it rather than regenerate it from scratch.
14963    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14964    since we will need to refer to them each time the function is inlined.  */
14965
14966 static bool
14967 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14968                                        enum dwarf_attribute attr)
14969 {
14970   rtx rtl;
14971   dw_loc_list_ref list;
14972   var_loc_list *loc_list;
14973   cached_dw_loc_list *cache;
14974   void **slot;
14975
14976   if (TREE_CODE (decl) == ERROR_MARK)
14977     return false;
14978
14979   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14980               || TREE_CODE (decl) == RESULT_DECL);
14981
14982   /* Try to get some constant RTL for this decl, and use that as the value of
14983      the location.  */
14984
14985   rtl = rtl_for_decl_location (decl);
14986   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14987       && add_const_value_attribute (die, rtl))
14988     return true;
14989
14990   /* See if we have single element location list that is equivalent to
14991      a constant value.  That way we are better to use add_const_value_attribute
14992      rather than expanding constant value equivalent.  */
14993   loc_list = lookup_decl_loc (decl);
14994   if (loc_list
14995       && loc_list->first
14996       && loc_list->first->next == NULL
14997       && NOTE_P (loc_list->first->loc)
14998       && NOTE_VAR_LOCATION (loc_list->first->loc)
14999       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15000     {
15001       struct var_loc_node *node;
15002
15003       node = loc_list->first;
15004       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15005       if (GET_CODE (rtl) == EXPR_LIST)
15006         rtl = XEXP (rtl, 0);
15007       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15008           && add_const_value_attribute (die, rtl))
15009          return true;
15010     }
15011   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15012      list several times.  See if we've already cached the contents.  */
15013   list = NULL;
15014   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15015     cache_p = false;
15016   if (cache_p)
15017     {
15018       cache = (cached_dw_loc_list *)
15019         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15020       if (cache)
15021         list = cache->loc_list;
15022     }
15023   if (list == NULL)
15024     {
15025       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15026       /* It is usually worth caching this result if the decl is from
15027          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15028       if (cache_p && list && list->dw_loc_next)
15029         {
15030           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15031                                            DECL_UID (decl), INSERT);
15032           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15033           cache->decl_id = DECL_UID (decl);
15034           cache->loc_list = list;
15035           *slot = cache;
15036         }
15037     }
15038   if (list)
15039     {
15040       add_AT_location_description (die, attr, list);
15041       return true;
15042     }
15043   /* None of that worked, so it must not really have a location;
15044      try adding a constant value attribute from the DECL_INITIAL.  */
15045   return tree_add_const_value_attribute_for_decl (die, decl);
15046 }
15047
15048 /* Add VARIABLE and DIE into deferred locations list.  */
15049
15050 static void
15051 defer_location (tree variable, dw_die_ref die)
15052 {
15053   deferred_locations entry;
15054   entry.variable = variable;
15055   entry.die = die;
15056   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15057 }
15058
15059 /* Helper function for tree_add_const_value_attribute.  Natively encode
15060    initializer INIT into an array.  Return true if successful.  */
15061
15062 static bool
15063 native_encode_initializer (tree init, unsigned char *array, int size)
15064 {
15065   tree type;
15066
15067   if (init == NULL_TREE)
15068     return false;
15069
15070   STRIP_NOPS (init);
15071   switch (TREE_CODE (init))
15072     {
15073     case STRING_CST:
15074       type = TREE_TYPE (init);
15075       if (TREE_CODE (type) == ARRAY_TYPE)
15076         {
15077           tree enttype = TREE_TYPE (type);
15078           enum machine_mode mode = TYPE_MODE (enttype);
15079
15080           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15081             return false;
15082           if (int_size_in_bytes (type) != size)
15083             return false;
15084           if (size > TREE_STRING_LENGTH (init))
15085             {
15086               memcpy (array, TREE_STRING_POINTER (init),
15087                       TREE_STRING_LENGTH (init));
15088               memset (array + TREE_STRING_LENGTH (init),
15089                       '\0', size - TREE_STRING_LENGTH (init));
15090             }
15091           else
15092             memcpy (array, TREE_STRING_POINTER (init), size);
15093           return true;
15094         }
15095       return false;
15096     case CONSTRUCTOR:
15097       type = TREE_TYPE (init);
15098       if (int_size_in_bytes (type) != size)
15099         return false;
15100       if (TREE_CODE (type) == ARRAY_TYPE)
15101         {
15102           HOST_WIDE_INT min_index;
15103           unsigned HOST_WIDE_INT cnt;
15104           int curpos = 0, fieldsize;
15105           constructor_elt *ce;
15106
15107           if (TYPE_DOMAIN (type) == NULL_TREE
15108               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15109             return false;
15110
15111           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15112           if (fieldsize <= 0)
15113             return false;
15114
15115           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15116           memset (array, '\0', size);
15117           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15118             {
15119               tree val = ce->value;
15120               tree index = ce->index;
15121               int pos = curpos;
15122               if (index && TREE_CODE (index) == RANGE_EXPR)
15123                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15124                       * fieldsize;
15125               else if (index)
15126                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15127
15128               if (val)
15129                 {
15130                   STRIP_NOPS (val);
15131                   if (!native_encode_initializer (val, array + pos, fieldsize))
15132                     return false;
15133                 }
15134               curpos = pos + fieldsize;
15135               if (index && TREE_CODE (index) == RANGE_EXPR)
15136                 {
15137                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15138                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15139                   while (count-- > 0)
15140                     {
15141                       if (val)
15142                         memcpy (array + curpos, array + pos, fieldsize);
15143                       curpos += fieldsize;
15144                     }
15145                 }
15146               gcc_assert (curpos <= size);
15147             }
15148           return true;
15149         }
15150       else if (TREE_CODE (type) == RECORD_TYPE
15151                || TREE_CODE (type) == UNION_TYPE)
15152         {
15153           tree field = NULL_TREE;
15154           unsigned HOST_WIDE_INT cnt;
15155           constructor_elt *ce;
15156
15157           if (int_size_in_bytes (type) != size)
15158             return false;
15159
15160           if (TREE_CODE (type) == RECORD_TYPE)
15161             field = TYPE_FIELDS (type);
15162
15163           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15164             {
15165               tree val = ce->value;
15166               int pos, fieldsize;
15167
15168               if (ce->index != 0)
15169                 field = ce->index;
15170
15171               if (val)
15172                 STRIP_NOPS (val);
15173
15174               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15175                 return false;
15176
15177               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15178                   && TYPE_DOMAIN (TREE_TYPE (field))
15179                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15180                 return false;
15181               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15182                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15183                 return false;
15184               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15185               pos = int_byte_position (field);
15186               gcc_assert (pos + fieldsize <= size);
15187               if (val
15188                   && !native_encode_initializer (val, array + pos, fieldsize))
15189                 return false;
15190             }
15191           return true;
15192         }
15193       return false;
15194     case VIEW_CONVERT_EXPR:
15195     case NON_LVALUE_EXPR:
15196       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15197     default:
15198       return native_encode_expr (init, array, size) == size;
15199     }
15200 }
15201
15202 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15203    attribute is the const value T.  */
15204
15205 static bool
15206 tree_add_const_value_attribute (dw_die_ref die, tree t)
15207 {
15208   tree init;
15209   tree type = TREE_TYPE (t);
15210   rtx rtl;
15211
15212   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15213     return false;
15214
15215   init = t;
15216   gcc_assert (!DECL_P (init));
15217
15218   rtl = rtl_for_decl_init (init, type);
15219   if (rtl)
15220     return add_const_value_attribute (die, rtl);
15221   /* If the host and target are sane, try harder.  */
15222   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15223            && initializer_constant_valid_p (init, type))
15224     {
15225       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15226       if (size > 0 && (int) size == size)
15227         {
15228           unsigned char *array = (unsigned char *)
15229             ggc_alloc_cleared_atomic (size);
15230
15231           if (native_encode_initializer (init, array, size))
15232             {
15233               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15234               return true;
15235             }
15236         }
15237     }
15238   return false;
15239 }
15240
15241 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15242    attribute is the const value of T, where T is an integral constant
15243    variable with static storage duration
15244    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15245
15246 static bool
15247 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15248 {
15249
15250   if (!decl
15251       || (TREE_CODE (decl) != VAR_DECL
15252           && TREE_CODE (decl) != CONST_DECL)
15253       || (TREE_CODE (decl) == VAR_DECL
15254           && !TREE_STATIC (decl)))
15255     return false;
15256
15257     if (TREE_READONLY (decl)
15258         && ! TREE_THIS_VOLATILE (decl)
15259         && DECL_INITIAL (decl))
15260       /* OK */;
15261     else
15262       return false;
15263
15264   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15265   if (get_AT (var_die, DW_AT_const_value))
15266     return false;
15267
15268   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15269 }
15270
15271 /* Convert the CFI instructions for the current function into a
15272    location list.  This is used for DW_AT_frame_base when we targeting
15273    a dwarf2 consumer that does not support the dwarf3
15274    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15275    expressions.  */
15276
15277 static dw_loc_list_ref
15278 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15279 {
15280   int ix;
15281   dw_fde_ref fde;
15282   dw_loc_list_ref list, *list_tail;
15283   dw_cfi_ref cfi;
15284   dw_cfa_location last_cfa, next_cfa;
15285   const char *start_label, *last_label, *section;
15286   dw_cfa_location remember;
15287
15288   fde = cfun->fde;
15289   gcc_assert (fde != NULL);
15290
15291   section = secname_for_decl (current_function_decl);
15292   list_tail = &list;
15293   list = NULL;
15294
15295   memset (&next_cfa, 0, sizeof (next_cfa));
15296   next_cfa.reg = INVALID_REGNUM;
15297   remember = next_cfa;
15298
15299   start_label = fde->dw_fde_begin;
15300
15301   /* ??? Bald assumption that the CIE opcode list does not contain
15302      advance opcodes.  */
15303   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15304     lookup_cfa_1 (cfi, &next_cfa, &remember);
15305
15306   last_cfa = next_cfa;
15307   last_label = start_label;
15308
15309   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15310     {
15311       /* If the first partition contained no CFI adjustments, the
15312          CIE opcodes apply to the whole first partition.  */
15313       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15314                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15315       list_tail =&(*list_tail)->dw_loc_next;
15316       start_label = last_label = fde->dw_fde_second_begin;
15317     }
15318
15319   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15320     {
15321       switch (cfi->dw_cfi_opc)
15322         {
15323         case DW_CFA_set_loc:
15324         case DW_CFA_advance_loc1:
15325         case DW_CFA_advance_loc2:
15326         case DW_CFA_advance_loc4:
15327           if (!cfa_equal_p (&last_cfa, &next_cfa))
15328             {
15329               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15330                                          start_label, last_label, section);
15331
15332               list_tail = &(*list_tail)->dw_loc_next;
15333               last_cfa = next_cfa;
15334               start_label = last_label;
15335             }
15336           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15337           break;
15338
15339         case DW_CFA_advance_loc:
15340           /* The encoding is complex enough that we should never emit this.  */
15341           gcc_unreachable ();
15342
15343         default:
15344           lookup_cfa_1 (cfi, &next_cfa, &remember);
15345           break;
15346         }
15347       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15348         {
15349           if (!cfa_equal_p (&last_cfa, &next_cfa))
15350             {
15351               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15352                                          start_label, last_label, section);
15353
15354               list_tail = &(*list_tail)->dw_loc_next;
15355               last_cfa = next_cfa;
15356               start_label = last_label;
15357             }
15358           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15359                                      start_label, fde->dw_fde_end, section);
15360           list_tail = &(*list_tail)->dw_loc_next;
15361           start_label = last_label = fde->dw_fde_second_begin;
15362         }
15363     }
15364
15365   if (!cfa_equal_p (&last_cfa, &next_cfa))
15366     {
15367       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15368                                  start_label, last_label, section);
15369       list_tail = &(*list_tail)->dw_loc_next;
15370       start_label = last_label;
15371     }
15372
15373   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15374                              start_label,
15375                              fde->dw_fde_second_begin
15376                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15377                              section);
15378
15379   if (list && list->dw_loc_next)
15380     gen_llsym (list);
15381
15382   return list;
15383 }
15384
15385 /* Compute a displacement from the "steady-state frame pointer" to the
15386    frame base (often the same as the CFA), and store it in
15387    frame_pointer_fb_offset.  OFFSET is added to the displacement
15388    before the latter is negated.  */
15389
15390 static void
15391 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15392 {
15393   rtx reg, elim;
15394
15395 #ifdef FRAME_POINTER_CFA_OFFSET
15396   reg = frame_pointer_rtx;
15397   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15398 #else
15399   reg = arg_pointer_rtx;
15400   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15401 #endif
15402
15403   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15404   if (GET_CODE (elim) == PLUS)
15405     {
15406       offset += INTVAL (XEXP (elim, 1));
15407       elim = XEXP (elim, 0);
15408     }
15409
15410   frame_pointer_fb_offset = -offset;
15411
15412   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15413      in which to eliminate.  This is because it's stack pointer isn't 
15414      directly accessible as a register within the ISA.  To work around
15415      this, assume that while we cannot provide a proper value for
15416      frame_pointer_fb_offset, we won't need one either.  */
15417   frame_pointer_fb_offset_valid
15418     = ((SUPPORTS_STACK_ALIGNMENT
15419         && (elim == hard_frame_pointer_rtx
15420             || elim == stack_pointer_rtx))
15421        || elim == (frame_pointer_needed
15422                    ? hard_frame_pointer_rtx
15423                    : stack_pointer_rtx));
15424 }
15425
15426 /* Generate a DW_AT_name attribute given some string value to be included as
15427    the value of the attribute.  */
15428
15429 static void
15430 add_name_attribute (dw_die_ref die, const char *name_string)
15431 {
15432   if (name_string != NULL && *name_string != 0)
15433     {
15434       if (demangle_name_func)
15435         name_string = (*demangle_name_func) (name_string);
15436
15437       add_AT_string (die, DW_AT_name, name_string);
15438     }
15439 }
15440
15441 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15442    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15443    of TYPE accordingly.
15444
15445    ??? This is a temporary measure until after we're able to generate
15446    regular DWARF for the complex Ada type system.  */
15447
15448 static void 
15449 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15450                                      dw_die_ref context_die)
15451 {
15452   tree dtype;
15453   dw_die_ref dtype_die;
15454
15455   if (!lang_hooks.types.descriptive_type)
15456     return;
15457
15458   dtype = lang_hooks.types.descriptive_type (type);
15459   if (!dtype)
15460     return;
15461
15462   dtype_die = lookup_type_die (dtype);
15463   if (!dtype_die)
15464     {
15465       /* The descriptive type indirectly references TYPE if this is also the
15466          case for TYPE itself.  Do not deal with the circularity here.  */
15467       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 1;
15468       gen_type_die (dtype, context_die);
15469       TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type)) = 0;
15470       dtype_die = lookup_type_die (dtype);
15471       gcc_assert (dtype_die);
15472     }
15473
15474   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15475 }
15476
15477 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15478
15479 static void
15480 add_comp_dir_attribute (dw_die_ref die)
15481 {
15482   const char *wd = get_src_pwd ();
15483   char *wd1;
15484
15485   if (wd == NULL)
15486     return;
15487
15488   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15489     {
15490       int wdlen;
15491
15492       wdlen = strlen (wd);
15493       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15494       strcpy (wd1, wd);
15495       wd1 [wdlen] = DIR_SEPARATOR;
15496       wd1 [wdlen + 1] = 0;
15497       wd = wd1;
15498     }
15499
15500     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15501 }
15502
15503 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15504    default.  */
15505
15506 static int
15507 lower_bound_default (void)
15508 {
15509   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15510     {
15511     case DW_LANG_C:
15512     case DW_LANG_C89:
15513     case DW_LANG_C99:
15514     case DW_LANG_C_plus_plus:
15515     case DW_LANG_ObjC:
15516     case DW_LANG_ObjC_plus_plus:
15517     case DW_LANG_Java:
15518       return 0;
15519     case DW_LANG_Fortran77:
15520     case DW_LANG_Fortran90:
15521     case DW_LANG_Fortran95:
15522       return 1;
15523     case DW_LANG_UPC:
15524     case DW_LANG_D:
15525     case DW_LANG_Python:
15526       return dwarf_version >= 4 ? 0 : -1;
15527     case DW_LANG_Ada95:
15528     case DW_LANG_Ada83:
15529     case DW_LANG_Cobol74:
15530     case DW_LANG_Cobol85:
15531     case DW_LANG_Pascal83:
15532     case DW_LANG_Modula2:
15533     case DW_LANG_PLI:
15534       return dwarf_version >= 4 ? 1 : -1;
15535     default:
15536       return -1;
15537     }
15538 }
15539
15540 /* Given a tree node describing an array bound (either lower or upper) output
15541    a representation for that bound.  */
15542
15543 static void
15544 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15545 {
15546   switch (TREE_CODE (bound))
15547     {
15548     case ERROR_MARK:
15549       return;
15550
15551     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15552     case INTEGER_CST:
15553       {
15554         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15555         int dflt;
15556
15557         /* Use the default if possible.  */
15558         if (bound_attr == DW_AT_lower_bound
15559             && host_integerp (bound, 0)
15560             && (dflt = lower_bound_default ()) != -1
15561             && tree_low_cst (bound, 0) == dflt)
15562           ;
15563
15564         /* Otherwise represent the bound as an unsigned value with the
15565            precision of its type.  The precision and signedness of the
15566            type will be necessary to re-interpret it unambiguously.  */
15567         else if (prec < HOST_BITS_PER_WIDE_INT)
15568           {
15569             unsigned HOST_WIDE_INT mask
15570               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15571             add_AT_unsigned (subrange_die, bound_attr,
15572                              TREE_INT_CST_LOW (bound) & mask);
15573           }
15574         else if (prec == HOST_BITS_PER_WIDE_INT
15575                  || TREE_INT_CST_HIGH (bound) == 0)
15576           add_AT_unsigned (subrange_die, bound_attr,
15577                            TREE_INT_CST_LOW (bound));
15578         else
15579           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15580                          TREE_INT_CST_LOW (bound));
15581       }
15582       break;
15583
15584     CASE_CONVERT:
15585     case VIEW_CONVERT_EXPR:
15586       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15587       break;
15588
15589     case SAVE_EXPR:
15590       break;
15591
15592     case VAR_DECL:
15593     case PARM_DECL:
15594     case RESULT_DECL:
15595       {
15596         dw_die_ref decl_die = lookup_decl_die (bound);
15597
15598         /* ??? Can this happen, or should the variable have been bound
15599            first?  Probably it can, since I imagine that we try to create
15600            the types of parameters in the order in which they exist in
15601            the list, and won't have created a forward reference to a
15602            later parameter.  */
15603         if (decl_die != NULL)
15604           {
15605             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15606             break;
15607           }
15608       }
15609       /* FALLTHRU */
15610
15611     default:
15612       {
15613         /* Otherwise try to create a stack operation procedure to
15614            evaluate the value of the array bound.  */
15615
15616         dw_die_ref ctx, decl_die;
15617         dw_loc_list_ref list;
15618
15619         list = loc_list_from_tree (bound, 2);
15620         if (list == NULL || single_element_loc_list_p (list))
15621           {
15622             /* If DW_AT_*bound is not a reference nor constant, it is
15623                a DWARF expression rather than location description.
15624                For that loc_list_from_tree (bound, 0) is needed.
15625                If that fails to give a single element list,
15626                fall back to outputting this as a reference anyway.  */
15627             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15628             if (list2 && single_element_loc_list_p (list2))
15629               {
15630                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15631                 break;
15632               }
15633           }
15634         if (list == NULL)
15635           break;
15636
15637         if (current_function_decl == 0)
15638           ctx = comp_unit_die ();
15639         else
15640           ctx = lookup_decl_die (current_function_decl);
15641
15642         decl_die = new_die (DW_TAG_variable, ctx, bound);
15643         add_AT_flag (decl_die, DW_AT_artificial, 1);
15644         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15645         add_AT_location_description (decl_die, DW_AT_location, list);
15646         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15647         break;
15648       }
15649     }
15650 }
15651
15652 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15653    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15654    Note that the block of subscript information for an array type also
15655    includes information about the element type of the given array type.  */
15656
15657 static void
15658 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15659 {
15660   unsigned dimension_number;
15661   tree lower, upper;
15662   dw_die_ref subrange_die;
15663
15664   for (dimension_number = 0;
15665        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15666        type = TREE_TYPE (type), dimension_number++)
15667     {
15668       tree domain = TYPE_DOMAIN (type);
15669
15670       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15671         break;
15672
15673       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15674          and (in GNU C only) variable bounds.  Handle all three forms
15675          here.  */
15676       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15677       if (domain)
15678         {
15679           /* We have an array type with specified bounds.  */
15680           lower = TYPE_MIN_VALUE (domain);
15681           upper = TYPE_MAX_VALUE (domain);
15682
15683           /* Define the index type.  */
15684           if (TREE_TYPE (domain))
15685             {
15686               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15687                  TREE_TYPE field.  We can't emit debug info for this
15688                  because it is an unnamed integral type.  */
15689               if (TREE_CODE (domain) == INTEGER_TYPE
15690                   && TYPE_NAME (domain) == NULL_TREE
15691                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15692                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15693                 ;
15694               else
15695                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15696                                     type_die);
15697             }
15698
15699           /* ??? If upper is NULL, the array has unspecified length,
15700              but it does have a lower bound.  This happens with Fortran
15701                dimension arr(N:*)
15702              Since the debugger is definitely going to need to know N
15703              to produce useful results, go ahead and output the lower
15704              bound solo, and hope the debugger can cope.  */
15705
15706           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15707           if (upper)
15708             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15709         }
15710
15711       /* Otherwise we have an array type with an unspecified length.  The
15712          DWARF-2 spec does not say how to handle this; let's just leave out the
15713          bounds.  */
15714     }
15715 }
15716
15717 static void
15718 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15719 {
15720   unsigned size;
15721
15722   switch (TREE_CODE (tree_node))
15723     {
15724     case ERROR_MARK:
15725       size = 0;
15726       break;
15727     case ENUMERAL_TYPE:
15728     case RECORD_TYPE:
15729     case UNION_TYPE:
15730     case QUAL_UNION_TYPE:
15731       size = int_size_in_bytes (tree_node);
15732       break;
15733     case FIELD_DECL:
15734       /* For a data member of a struct or union, the DW_AT_byte_size is
15735          generally given as the number of bytes normally allocated for an
15736          object of the *declared* type of the member itself.  This is true
15737          even for bit-fields.  */
15738       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15739       break;
15740     default:
15741       gcc_unreachable ();
15742     }
15743
15744   /* Note that `size' might be -1 when we get to this point.  If it is, that
15745      indicates that the byte size of the entity in question is variable.  We
15746      have no good way of expressing this fact in Dwarf at the present time,
15747      so just let the -1 pass on through.  */
15748   add_AT_unsigned (die, DW_AT_byte_size, size);
15749 }
15750
15751 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15752    which specifies the distance in bits from the highest order bit of the
15753    "containing object" for the bit-field to the highest order bit of the
15754    bit-field itself.
15755
15756    For any given bit-field, the "containing object" is a hypothetical object
15757    (of some integral or enum type) within which the given bit-field lives.  The
15758    type of this hypothetical "containing object" is always the same as the
15759    declared type of the individual bit-field itself.  The determination of the
15760    exact location of the "containing object" for a bit-field is rather
15761    complicated.  It's handled by the `field_byte_offset' function (above).
15762
15763    Note that it is the size (in bytes) of the hypothetical "containing object"
15764    which will be given in the DW_AT_byte_size attribute for this bit-field.
15765    (See `byte_size_attribute' above).  */
15766
15767 static inline void
15768 add_bit_offset_attribute (dw_die_ref die, tree decl)
15769 {
15770   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15771   tree type = DECL_BIT_FIELD_TYPE (decl);
15772   HOST_WIDE_INT bitpos_int;
15773   HOST_WIDE_INT highest_order_object_bit_offset;
15774   HOST_WIDE_INT highest_order_field_bit_offset;
15775   HOST_WIDE_INT bit_offset;
15776
15777   /* Must be a field and a bit field.  */
15778   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15779
15780   /* We can't yet handle bit-fields whose offsets are variable, so if we
15781      encounter such things, just return without generating any attribute
15782      whatsoever.  Likewise for variable or too large size.  */
15783   if (! host_integerp (bit_position (decl), 0)
15784       || ! host_integerp (DECL_SIZE (decl), 1))
15785     return;
15786
15787   bitpos_int = int_bit_position (decl);
15788
15789   /* Note that the bit offset is always the distance (in bits) from the
15790      highest-order bit of the "containing object" to the highest-order bit of
15791      the bit-field itself.  Since the "high-order end" of any object or field
15792      is different on big-endian and little-endian machines, the computation
15793      below must take account of these differences.  */
15794   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15795   highest_order_field_bit_offset = bitpos_int;
15796
15797   if (! BYTES_BIG_ENDIAN)
15798     {
15799       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15800       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15801     }
15802
15803   bit_offset
15804     = (! BYTES_BIG_ENDIAN
15805        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15806        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15807
15808   if (bit_offset < 0)
15809     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15810   else
15811     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15812 }
15813
15814 /* For a FIELD_DECL node which represents a bit field, output an attribute
15815    which specifies the length in bits of the given field.  */
15816
15817 static inline void
15818 add_bit_size_attribute (dw_die_ref die, tree decl)
15819 {
15820   /* Must be a field and a bit field.  */
15821   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15822               && DECL_BIT_FIELD_TYPE (decl));
15823
15824   if (host_integerp (DECL_SIZE (decl), 1))
15825     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15826 }
15827
15828 /* If the compiled language is ANSI C, then add a 'prototyped'
15829    attribute, if arg types are given for the parameters of a function.  */
15830
15831 static inline void
15832 add_prototyped_attribute (dw_die_ref die, tree func_type)
15833 {
15834   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15835       && prototype_p (func_type))
15836     add_AT_flag (die, DW_AT_prototyped, 1);
15837 }
15838
15839 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15840    by looking in either the type declaration or object declaration
15841    equate table.  */
15842
15843 static inline dw_die_ref
15844 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15845 {
15846   dw_die_ref origin_die = NULL;
15847
15848   if (TREE_CODE (origin) != FUNCTION_DECL)
15849     {
15850       /* We may have gotten separated from the block for the inlined
15851          function, if we're in an exception handler or some such; make
15852          sure that the abstract function has been written out.
15853
15854          Doing this for nested functions is wrong, however; functions are
15855          distinct units, and our context might not even be inline.  */
15856       tree fn = origin;
15857
15858       if (TYPE_P (fn))
15859         fn = TYPE_STUB_DECL (fn);
15860
15861       fn = decl_function_context (fn);
15862       if (fn)
15863         dwarf2out_abstract_function (fn);
15864     }
15865
15866   if (DECL_P (origin))
15867     origin_die = lookup_decl_die (origin);
15868   else if (TYPE_P (origin))
15869     origin_die = lookup_type_die (origin);
15870
15871   /* XXX: Functions that are never lowered don't always have correct block
15872      trees (in the case of java, they simply have no block tree, in some other
15873      languages).  For these functions, there is nothing we can really do to
15874      output correct debug info for inlined functions in all cases.  Rather
15875      than die, we'll just produce deficient debug info now, in that we will
15876      have variables without a proper abstract origin.  In the future, when all
15877      functions are lowered, we should re-add a gcc_assert (origin_die)
15878      here.  */
15879
15880   if (origin_die)
15881     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15882   return origin_die;
15883 }
15884
15885 /* We do not currently support the pure_virtual attribute.  */
15886
15887 static inline void
15888 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15889 {
15890   if (DECL_VINDEX (func_decl))
15891     {
15892       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15893
15894       if (host_integerp (DECL_VINDEX (func_decl), 0))
15895         add_AT_loc (die, DW_AT_vtable_elem_location,
15896                     new_loc_descr (DW_OP_constu,
15897                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15898                                    0));
15899
15900       /* GNU extension: Record what type this method came from originally.  */
15901       if (debug_info_level > DINFO_LEVEL_TERSE
15902           && DECL_CONTEXT (func_decl))
15903         add_AT_die_ref (die, DW_AT_containing_type,
15904                         lookup_type_die (DECL_CONTEXT (func_decl)));
15905     }
15906 }
15907 \f
15908 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15909    given decl.  This used to be a vendor extension until after DWARF 4
15910    standardized it.  */
15911
15912 static void
15913 add_linkage_attr (dw_die_ref die, tree decl)
15914 {
15915   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15916
15917   /* Mimic what assemble_name_raw does with a leading '*'.  */
15918   if (name[0] == '*')
15919     name = &name[1];
15920
15921   if (dwarf_version >= 4)
15922     add_AT_string (die, DW_AT_linkage_name, name);
15923   else
15924     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15925 }
15926
15927 /* Add source coordinate attributes for the given decl.  */
15928
15929 static void
15930 add_src_coords_attributes (dw_die_ref die, tree decl)
15931 {
15932   expanded_location s;
15933
15934   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15935     return;
15936   s = expand_location (DECL_SOURCE_LOCATION (decl));
15937   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15938   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15939 }
15940
15941 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15942
15943 static void
15944 add_linkage_name (dw_die_ref die, tree decl)
15945 {
15946   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15947        && TREE_PUBLIC (decl)
15948        && !DECL_ABSTRACT (decl)
15949        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15950        && die->die_tag != DW_TAG_member)
15951     {
15952       /* Defer until we have an assembler name set.  */
15953       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15954         {
15955           limbo_die_node *asm_name;
15956
15957           asm_name = ggc_alloc_cleared_limbo_die_node ();
15958           asm_name->die = die;
15959           asm_name->created_for = decl;
15960           asm_name->next = deferred_asm_name;
15961           deferred_asm_name = asm_name;
15962         }
15963       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15964         add_linkage_attr (die, decl);
15965     }
15966 }
15967
15968 /* Add a DW_AT_name attribute and source coordinate attribute for the
15969    given decl, but only if it actually has a name.  */
15970
15971 static void
15972 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15973 {
15974   tree decl_name;
15975
15976   decl_name = DECL_NAME (decl);
15977   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15978     {
15979       const char *name = dwarf2_name (decl, 0);
15980       if (name)
15981         add_name_attribute (die, name);
15982       if (! DECL_ARTIFICIAL (decl))
15983         add_src_coords_attributes (die, decl);
15984
15985       add_linkage_name (die, decl);
15986     }
15987
15988 #ifdef VMS_DEBUGGING_INFO
15989   /* Get the function's name, as described by its RTL.  This may be different
15990      from the DECL_NAME name used in the source file.  */
15991   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15992     {
15993       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15994                    XEXP (DECL_RTL (decl), 0));
15995       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15996     }
15997 #endif /* VMS_DEBUGGING_INFO */
15998 }
15999
16000 #ifdef VMS_DEBUGGING_INFO
16001 /* Output the debug main pointer die for VMS */
16002
16003 void
16004 dwarf2out_vms_debug_main_pointer (void)
16005 {
16006   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16007   dw_die_ref die;
16008
16009   /* Allocate the VMS debug main subprogram die.  */
16010   die = ggc_alloc_cleared_die_node ();
16011   die->die_tag = DW_TAG_subprogram;
16012   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16013   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16014                                current_function_funcdef_no);
16015   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16016
16017   /* Make it the first child of comp_unit_die ().  */
16018   die->die_parent = comp_unit_die ();
16019   if (comp_unit_die ()->die_child)
16020     {
16021       die->die_sib = comp_unit_die ()->die_child->die_sib;
16022       comp_unit_die ()->die_child->die_sib = die;
16023     }
16024   else
16025     {
16026       die->die_sib = die;
16027       comp_unit_die ()->die_child = die;
16028     }
16029 }
16030 #endif /* VMS_DEBUGGING_INFO */
16031
16032 /* Push a new declaration scope.  */
16033
16034 static void
16035 push_decl_scope (tree scope)
16036 {
16037   VEC_safe_push (tree, gc, decl_scope_table, scope);
16038 }
16039
16040 /* Pop a declaration scope.  */
16041
16042 static inline void
16043 pop_decl_scope (void)
16044 {
16045   VEC_pop (tree, decl_scope_table);
16046 }
16047
16048 /* Return the DIE for the scope that immediately contains this type.
16049    Non-named types get global scope.  Named types nested in other
16050    types get their containing scope if it's open, or global scope
16051    otherwise.  All other types (i.e. function-local named types) get
16052    the current active scope.  */
16053
16054 static dw_die_ref
16055 scope_die_for (tree t, dw_die_ref context_die)
16056 {
16057   dw_die_ref scope_die = NULL;
16058   tree containing_scope;
16059   int i;
16060
16061   /* Non-types always go in the current scope.  */
16062   gcc_assert (TYPE_P (t));
16063
16064   containing_scope = TYPE_CONTEXT (t);
16065
16066   /* Use the containing namespace if it was passed in (for a declaration).  */
16067   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16068     {
16069       if (context_die == lookup_decl_die (containing_scope))
16070         /* OK */;
16071       else
16072         containing_scope = NULL_TREE;
16073     }
16074
16075   /* Ignore function type "scopes" from the C frontend.  They mean that
16076      a tagged type is local to a parmlist of a function declarator, but
16077      that isn't useful to DWARF.  */
16078   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16079     containing_scope = NULL_TREE;
16080
16081   if (SCOPE_FILE_SCOPE_P (containing_scope))
16082     scope_die = comp_unit_die ();
16083   else if (TYPE_P (containing_scope))
16084     {
16085       /* For types, we can just look up the appropriate DIE.  But
16086          first we check to see if we're in the middle of emitting it
16087          so we know where the new DIE should go.  */
16088       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16089         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16090           break;
16091
16092       if (i < 0)
16093         {
16094           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16095                       || TREE_ASM_WRITTEN (containing_scope));
16096           /*We are not in the middle of emitting the type
16097             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16098           scope_die = lookup_type_die (containing_scope);
16099
16100           /* If none of the current dies are suitable, we get file scope.  */
16101           if (scope_die == NULL)
16102             scope_die = comp_unit_die ();
16103         }
16104       else
16105         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16106     }
16107   else
16108     scope_die = context_die;
16109
16110   return scope_die;
16111 }
16112
16113 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16114
16115 static inline int
16116 local_scope_p (dw_die_ref context_die)
16117 {
16118   for (; context_die; context_die = context_die->die_parent)
16119     if (context_die->die_tag == DW_TAG_inlined_subroutine
16120         || context_die->die_tag == DW_TAG_subprogram)
16121       return 1;
16122
16123   return 0;
16124 }
16125
16126 /* Returns nonzero if CONTEXT_DIE is a class.  */
16127
16128 static inline int
16129 class_scope_p (dw_die_ref context_die)
16130 {
16131   return (context_die
16132           && (context_die->die_tag == DW_TAG_structure_type
16133               || context_die->die_tag == DW_TAG_class_type
16134               || context_die->die_tag == DW_TAG_interface_type
16135               || context_die->die_tag == DW_TAG_union_type));
16136 }
16137
16138 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16139    whether or not to treat a DIE in this context as a declaration.  */
16140
16141 static inline int
16142 class_or_namespace_scope_p (dw_die_ref context_die)
16143 {
16144   return (class_scope_p (context_die)
16145           || (context_die && context_die->die_tag == DW_TAG_namespace));
16146 }
16147
16148 /* Many forms of DIEs require a "type description" attribute.  This
16149    routine locates the proper "type descriptor" die for the type given
16150    by 'type', and adds a DW_AT_type attribute below the given die.  */
16151
16152 static void
16153 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16154                     int decl_volatile, dw_die_ref context_die)
16155 {
16156   enum tree_code code  = TREE_CODE (type);
16157   dw_die_ref type_die  = NULL;
16158
16159   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16160      or fixed-point type, use the inner type.  This is because we have no
16161      support for unnamed types in base_type_die.  This can happen if this is
16162      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16163   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16164       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16165     type = TREE_TYPE (type), code = TREE_CODE (type);
16166
16167   if (code == ERROR_MARK
16168       /* Handle a special case.  For functions whose return type is void, we
16169          generate *no* type attribute.  (Note that no object may have type
16170          `void', so this only applies to function return types).  */
16171       || code == VOID_TYPE)
16172     return;
16173
16174   type_die = modified_type_die (type,
16175                                 decl_const || TYPE_READONLY (type),
16176                                 decl_volatile || TYPE_VOLATILE (type),
16177                                 context_die);
16178
16179   if (type_die != NULL)
16180     add_AT_die_ref (object_die, DW_AT_type, type_die);
16181 }
16182
16183 /* Given an object die, add the calling convention attribute for the
16184    function call type.  */
16185 static void
16186 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16187 {
16188   enum dwarf_calling_convention value = DW_CC_normal;
16189
16190   value = ((enum dwarf_calling_convention)
16191            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16192
16193   if (is_fortran ()
16194       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16195     {
16196       /* DWARF 2 doesn't provide a way to identify a program's source-level
16197         entry point.  DW_AT_calling_convention attributes are only meant
16198         to describe functions' calling conventions.  However, lacking a
16199         better way to signal the Fortran main program, we used this for 
16200         a long time, following existing custom.  Now, DWARF 4 has 
16201         DW_AT_main_subprogram, which we add below, but some tools still
16202         rely on the old way, which we thus keep.  */
16203       value = DW_CC_program;
16204
16205       if (dwarf_version >= 4 || !dwarf_strict)
16206         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16207     }
16208
16209   /* Only add the attribute if the backend requests it, and
16210      is not DW_CC_normal.  */
16211   if (value && (value != DW_CC_normal))
16212     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16213 }
16214
16215 /* Given a tree pointer to a struct, class, union, or enum type node, return
16216    a pointer to the (string) tag name for the given type, or zero if the type
16217    was declared without a tag.  */
16218
16219 static const char *
16220 type_tag (const_tree type)
16221 {
16222   const char *name = 0;
16223
16224   if (TYPE_NAME (type) != 0)
16225     {
16226       tree t = 0;
16227
16228       /* Find the IDENTIFIER_NODE for the type name.  */
16229       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16230           && !TYPE_NAMELESS (type))
16231         t = TYPE_NAME (type);
16232
16233       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16234          a TYPE_DECL node, regardless of whether or not a `typedef' was
16235          involved.  */
16236       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16237                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16238         {
16239           /* We want to be extra verbose.  Don't call dwarf_name if
16240              DECL_NAME isn't set.  The default hook for decl_printable_name
16241              doesn't like that, and in this context it's correct to return
16242              0, instead of "<anonymous>" or the like.  */
16243           if (DECL_NAME (TYPE_NAME (type))
16244               && !DECL_NAMELESS (TYPE_NAME (type)))
16245             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16246         }
16247
16248       /* Now get the name as a string, or invent one.  */
16249       if (!name && t != 0)
16250         name = IDENTIFIER_POINTER (t);
16251     }
16252
16253   return (name == 0 || *name == '\0') ? 0 : name;
16254 }
16255
16256 /* Return the type associated with a data member, make a special check
16257    for bit field types.  */
16258
16259 static inline tree
16260 member_declared_type (const_tree member)
16261 {
16262   return (DECL_BIT_FIELD_TYPE (member)
16263           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16264 }
16265
16266 /* Get the decl's label, as described by its RTL. This may be different
16267    from the DECL_NAME name used in the source file.  */
16268
16269 #if 0
16270 static const char *
16271 decl_start_label (tree decl)
16272 {
16273   rtx x;
16274   const char *fnname;
16275
16276   x = DECL_RTL (decl);
16277   gcc_assert (MEM_P (x));
16278
16279   x = XEXP (x, 0);
16280   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16281
16282   fnname = XSTR (x, 0);
16283   return fnname;
16284 }
16285 #endif
16286 \f
16287 /* These routines generate the internal representation of the DIE's for
16288    the compilation unit.  Debugging information is collected by walking
16289    the declaration trees passed in from dwarf2out_decl().  */
16290
16291 static void
16292 gen_array_type_die (tree type, dw_die_ref context_die)
16293 {
16294   dw_die_ref scope_die = scope_die_for (type, context_die);
16295   dw_die_ref array_die;
16296
16297   /* GNU compilers represent multidimensional array types as sequences of one
16298      dimensional array types whose element types are themselves array types.
16299      We sometimes squish that down to a single array_type DIE with multiple
16300      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16301      say that we are allowed to do this kind of compression in C, because
16302      there is no difference between an array of arrays and a multidimensional
16303      array.  We don't do this for Ada to remain as close as possible to the
16304      actual representation, which is especially important against the language
16305      flexibilty wrt arrays of variable size.  */
16306
16307   bool collapse_nested_arrays = !is_ada ();
16308   tree element_type;
16309
16310   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16311      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16312   if (TYPE_STRING_FLAG (type)
16313       && TREE_CODE (type) == ARRAY_TYPE
16314       && is_fortran ()
16315       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16316     {
16317       HOST_WIDE_INT size;
16318
16319       array_die = new_die (DW_TAG_string_type, scope_die, type);
16320       add_name_attribute (array_die, type_tag (type));
16321       equate_type_number_to_die (type, array_die);
16322       size = int_size_in_bytes (type);
16323       if (size >= 0)
16324         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16325       else if (TYPE_DOMAIN (type) != NULL_TREE
16326                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16327                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16328         {
16329           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16330           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16331
16332           size = int_size_in_bytes (TREE_TYPE (szdecl));
16333           if (loc && size > 0)
16334             {
16335               add_AT_location_description (array_die, DW_AT_string_length, loc);
16336               if (size != DWARF2_ADDR_SIZE)
16337                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16338             }
16339         }
16340       return;
16341     }
16342
16343   /* ??? The SGI dwarf reader fails for array of array of enum types
16344      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16345      array type comes before the outer array type.  We thus call gen_type_die
16346      before we new_die and must prevent nested array types collapsing for this
16347      target.  */
16348
16349 #ifdef MIPS_DEBUGGING_INFO
16350   gen_type_die (TREE_TYPE (type), context_die);
16351   collapse_nested_arrays = false;
16352 #endif
16353
16354   array_die = new_die (DW_TAG_array_type, scope_die, type);
16355   add_name_attribute (array_die, type_tag (type));
16356   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16357   if (TYPE_ARTIFICIAL (type))
16358     add_AT_flag (array_die, DW_AT_artificial, 1);
16359   equate_type_number_to_die (type, array_die);
16360
16361   if (TREE_CODE (type) == VECTOR_TYPE)
16362     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16363
16364   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16365   if (is_fortran ()
16366       && TREE_CODE (type) == ARRAY_TYPE
16367       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16368       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16369     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16370
16371 #if 0
16372   /* We default the array ordering.  SDB will probably do
16373      the right things even if DW_AT_ordering is not present.  It's not even
16374      an issue until we start to get into multidimensional arrays anyway.  If
16375      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16376      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16377      and when we find out that we need to put these in, we will only do so
16378      for multidimensional arrays.  */
16379   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16380 #endif
16381
16382 #ifdef MIPS_DEBUGGING_INFO
16383   /* The SGI compilers handle arrays of unknown bound by setting
16384      AT_declaration and not emitting any subrange DIEs.  */
16385   if (TREE_CODE (type) == ARRAY_TYPE
16386       && ! TYPE_DOMAIN (type))
16387     add_AT_flag (array_die, DW_AT_declaration, 1);
16388   else
16389 #endif
16390   if (TREE_CODE (type) == VECTOR_TYPE)
16391     {
16392       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16393       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16394       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16395       add_bound_info (subrange_die, DW_AT_upper_bound,
16396                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16397     }
16398   else
16399     add_subscript_info (array_die, type, collapse_nested_arrays);
16400
16401   /* Add representation of the type of the elements of this array type and
16402      emit the corresponding DIE if we haven't done it already.  */
16403   element_type = TREE_TYPE (type);
16404   if (collapse_nested_arrays)
16405     while (TREE_CODE (element_type) == ARRAY_TYPE)
16406       {
16407         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16408           break;
16409         element_type = TREE_TYPE (element_type);
16410       }
16411
16412 #ifndef MIPS_DEBUGGING_INFO
16413   gen_type_die (element_type, context_die);
16414 #endif
16415
16416   add_type_attribute (array_die, element_type, 0, 0, context_die);
16417
16418   if (get_AT (array_die, DW_AT_name))
16419     add_pubtype (type, array_die);
16420 }
16421
16422 static dw_loc_descr_ref
16423 descr_info_loc (tree val, tree base_decl)
16424 {
16425   HOST_WIDE_INT size;
16426   dw_loc_descr_ref loc, loc2;
16427   enum dwarf_location_atom op;
16428
16429   if (val == base_decl)
16430     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16431
16432   switch (TREE_CODE (val))
16433     {
16434     CASE_CONVERT:
16435       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16436     case VAR_DECL:
16437       return loc_descriptor_from_tree (val, 0);
16438     case INTEGER_CST:
16439       if (host_integerp (val, 0))
16440         return int_loc_descriptor (tree_low_cst (val, 0));
16441       break;
16442     case INDIRECT_REF:
16443       size = int_size_in_bytes (TREE_TYPE (val));
16444       if (size < 0)
16445         break;
16446       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16447       if (!loc)
16448         break;
16449       if (size == DWARF2_ADDR_SIZE)
16450         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16451       else
16452         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16453       return loc;
16454     case POINTER_PLUS_EXPR:
16455     case PLUS_EXPR:
16456       if (host_integerp (TREE_OPERAND (val, 1), 1)
16457           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16458              < 16384)
16459         {
16460           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16461           if (!loc)
16462             break;
16463           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16464         }
16465       else
16466         {
16467           op = DW_OP_plus;
16468         do_binop:
16469           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16470           if (!loc)
16471             break;
16472           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16473           if (!loc2)
16474             break;
16475           add_loc_descr (&loc, loc2);
16476           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16477         }
16478       return loc;
16479     case MINUS_EXPR:
16480       op = DW_OP_minus;
16481       goto do_binop;
16482     case MULT_EXPR:
16483       op = DW_OP_mul;
16484       goto do_binop;
16485     case EQ_EXPR:
16486       op = DW_OP_eq;
16487       goto do_binop;
16488     case NE_EXPR:
16489       op = DW_OP_ne;
16490       goto do_binop;
16491     default:
16492       break;
16493     }
16494   return NULL;
16495 }
16496
16497 static void
16498 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16499                       tree val, tree base_decl)
16500 {
16501   dw_loc_descr_ref loc;
16502
16503   if (host_integerp (val, 0))
16504     {
16505       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16506       return;
16507     }
16508
16509   loc = descr_info_loc (val, base_decl);
16510   if (!loc)
16511     return;
16512
16513   add_AT_loc (die, attr, loc);
16514 }
16515
16516 /* This routine generates DIE for array with hidden descriptor, details
16517    are filled into *info by a langhook.  */
16518
16519 static void
16520 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16521                           dw_die_ref context_die)
16522 {
16523   dw_die_ref scope_die = scope_die_for (type, context_die);
16524   dw_die_ref array_die;
16525   int dim;
16526
16527   array_die = new_die (DW_TAG_array_type, scope_die, type);
16528   add_name_attribute (array_die, type_tag (type));
16529   equate_type_number_to_die (type, array_die);
16530
16531   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16532   if (is_fortran ()
16533       && info->ndimensions >= 2)
16534     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16535
16536   if (info->data_location)
16537     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16538                           info->base_decl);
16539   if (info->associated)
16540     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16541                           info->base_decl);
16542   if (info->allocated)
16543     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16544                           info->base_decl);
16545
16546   for (dim = 0; dim < info->ndimensions; dim++)
16547     {
16548       dw_die_ref subrange_die
16549         = new_die (DW_TAG_subrange_type, array_die, NULL);
16550
16551       if (info->dimen[dim].lower_bound)
16552         {
16553           /* If it is the default value, omit it.  */
16554           int dflt;
16555
16556           if (host_integerp (info->dimen[dim].lower_bound, 0)
16557               && (dflt = lower_bound_default ()) != -1
16558               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16559             ;
16560           else
16561             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16562                                   info->dimen[dim].lower_bound,
16563                                   info->base_decl);
16564         }
16565       if (info->dimen[dim].upper_bound)
16566         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16567                               info->dimen[dim].upper_bound,
16568                               info->base_decl);
16569       if (info->dimen[dim].stride)
16570         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16571                               info->dimen[dim].stride,
16572                               info->base_decl);
16573     }
16574
16575   gen_type_die (info->element_type, context_die);
16576   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16577
16578   if (get_AT (array_die, DW_AT_name))
16579     add_pubtype (type, array_die);
16580 }
16581
16582 #if 0
16583 static void
16584 gen_entry_point_die (tree decl, dw_die_ref context_die)
16585 {
16586   tree origin = decl_ultimate_origin (decl);
16587   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16588
16589   if (origin != NULL)
16590     add_abstract_origin_attribute (decl_die, origin);
16591   else
16592     {
16593       add_name_and_src_coords_attributes (decl_die, decl);
16594       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16595                           0, 0, context_die);
16596     }
16597
16598   if (DECL_ABSTRACT (decl))
16599     equate_decl_number_to_die (decl, decl_die);
16600   else
16601     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16602 }
16603 #endif
16604
16605 /* Walk through the list of incomplete types again, trying once more to
16606    emit full debugging info for them.  */
16607
16608 static void
16609 retry_incomplete_types (void)
16610 {
16611   int i;
16612
16613   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16614     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16615                                   DINFO_USAGE_DIR_USE))
16616       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16617 }
16618
16619 /* Determine what tag to use for a record type.  */
16620
16621 static enum dwarf_tag
16622 record_type_tag (tree type)
16623 {
16624   if (! lang_hooks.types.classify_record)
16625     return DW_TAG_structure_type;
16626
16627   switch (lang_hooks.types.classify_record (type))
16628     {
16629     case RECORD_IS_STRUCT:
16630       return DW_TAG_structure_type;
16631
16632     case RECORD_IS_CLASS:
16633       return DW_TAG_class_type;
16634
16635     case RECORD_IS_INTERFACE:
16636       if (dwarf_version >= 3 || !dwarf_strict)
16637         return DW_TAG_interface_type;
16638       return DW_TAG_structure_type;
16639
16640     default:
16641       gcc_unreachable ();
16642     }
16643 }
16644
16645 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16646    include all of the information about the enumeration values also. Each
16647    enumerated type name/value is listed as a child of the enumerated type
16648    DIE.  */
16649
16650 static dw_die_ref
16651 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16652 {
16653   dw_die_ref type_die = lookup_type_die (type);
16654
16655   if (type_die == NULL)
16656     {
16657       type_die = new_die (DW_TAG_enumeration_type,
16658                           scope_die_for (type, context_die), type);
16659       equate_type_number_to_die (type, type_die);
16660       add_name_attribute (type_die, type_tag (type));
16661       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16662       if (TYPE_ARTIFICIAL (type))
16663         add_AT_flag (type_die, DW_AT_artificial, 1);
16664       if (dwarf_version >= 4 || !dwarf_strict)
16665         {
16666           if (ENUM_IS_SCOPED (type))
16667             add_AT_flag (type_die, DW_AT_enum_class, 1);
16668           if (ENUM_IS_OPAQUE (type))
16669             add_AT_flag (type_die, DW_AT_declaration, 1);
16670         }
16671     }
16672   else if (! TYPE_SIZE (type))
16673     return type_die;
16674   else
16675     remove_AT (type_die, DW_AT_declaration);
16676
16677   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16678      given enum type is incomplete, do not generate the DW_AT_byte_size
16679      attribute or the DW_AT_element_list attribute.  */
16680   if (TYPE_SIZE (type))
16681     {
16682       tree link;
16683
16684       TREE_ASM_WRITTEN (type) = 1;
16685       add_byte_size_attribute (type_die, type);
16686       if (TYPE_STUB_DECL (type) != NULL_TREE)
16687         {
16688           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16689           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16690         }
16691
16692       /* If the first reference to this type was as the return type of an
16693          inline function, then it may not have a parent.  Fix this now.  */
16694       if (type_die->die_parent == NULL)
16695         add_child_die (scope_die_for (type, context_die), type_die);
16696
16697       for (link = TYPE_VALUES (type);
16698            link != NULL; link = TREE_CHAIN (link))
16699         {
16700           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16701           tree value = TREE_VALUE (link);
16702
16703           add_name_attribute (enum_die,
16704                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16705
16706           if (TREE_CODE (value) == CONST_DECL)
16707             value = DECL_INITIAL (value);
16708
16709           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16710             /* DWARF2 does not provide a way of indicating whether or
16711                not enumeration constants are signed or unsigned.  GDB
16712                always assumes the values are signed, so we output all
16713                values as if they were signed.  That means that
16714                enumeration constants with very large unsigned values
16715                will appear to have negative values in the debugger.  */
16716             add_AT_int (enum_die, DW_AT_const_value,
16717                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16718         }
16719     }
16720   else
16721     add_AT_flag (type_die, DW_AT_declaration, 1);
16722
16723   if (get_AT (type_die, DW_AT_name))
16724     add_pubtype (type, type_die);
16725
16726   return type_die;
16727 }
16728
16729 /* Generate a DIE to represent either a real live formal parameter decl or to
16730    represent just the type of some formal parameter position in some function
16731    type.
16732
16733    Note that this routine is a bit unusual because its argument may be a
16734    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16735    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16736    node.  If it's the former then this function is being called to output a
16737    DIE to represent a formal parameter object (or some inlining thereof).  If
16738    it's the latter, then this function is only being called to output a
16739    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16740    argument type of some subprogram type.
16741    If EMIT_NAME_P is true, name and source coordinate attributes
16742    are emitted.  */
16743
16744 static dw_die_ref
16745 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16746                           dw_die_ref context_die)
16747 {
16748   tree node_or_origin = node ? node : origin;
16749   tree ultimate_origin;
16750   dw_die_ref parm_die
16751     = new_die (DW_TAG_formal_parameter, context_die, node);
16752
16753   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16754     {
16755     case tcc_declaration:
16756       ultimate_origin = decl_ultimate_origin (node_or_origin);
16757       if (node || ultimate_origin)
16758         origin = ultimate_origin;
16759       if (origin != NULL)
16760         add_abstract_origin_attribute (parm_die, origin);
16761       else if (emit_name_p)
16762         add_name_and_src_coords_attributes (parm_die, node);
16763       if (origin == NULL
16764           || (! DECL_ABSTRACT (node_or_origin)
16765               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16766                                            decl_function_context
16767                                                             (node_or_origin))))
16768         {
16769           tree type = TREE_TYPE (node_or_origin);
16770           if (decl_by_reference_p (node_or_origin))
16771             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16772                                 context_die);
16773           else
16774             add_type_attribute (parm_die, type,
16775                                 TREE_READONLY (node_or_origin),
16776                                 TREE_THIS_VOLATILE (node_or_origin),
16777                                 context_die);
16778         }
16779       if (origin == NULL && DECL_ARTIFICIAL (node))
16780         add_AT_flag (parm_die, DW_AT_artificial, 1);
16781
16782       if (node && node != origin)
16783         equate_decl_number_to_die (node, parm_die);
16784       if (! DECL_ABSTRACT (node_or_origin))
16785         add_location_or_const_value_attribute (parm_die, node_or_origin,
16786                                                node == NULL, DW_AT_location);
16787
16788       break;
16789
16790     case tcc_type:
16791       /* We were called with some kind of a ..._TYPE node.  */
16792       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16793       break;
16794
16795     default:
16796       gcc_unreachable ();
16797     }
16798
16799   return parm_die;
16800 }
16801
16802 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16803    children DW_TAG_formal_parameter DIEs representing the arguments of the
16804    parameter pack.
16805
16806    PARM_PACK must be a function parameter pack.
16807    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16808    must point to the subsequent arguments of the function PACK_ARG belongs to.
16809    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16810    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16811    following the last one for which a DIE was generated.  */
16812
16813 static dw_die_ref
16814 gen_formal_parameter_pack_die  (tree parm_pack,
16815                                 tree pack_arg,
16816                                 dw_die_ref subr_die,
16817                                 tree *next_arg)
16818 {
16819   tree arg;
16820   dw_die_ref parm_pack_die;
16821
16822   gcc_assert (parm_pack
16823               && lang_hooks.function_parameter_pack_p (parm_pack)
16824               && subr_die);
16825
16826   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16827   add_src_coords_attributes (parm_pack_die, parm_pack);
16828
16829   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16830     {
16831       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16832                                                                  parm_pack))
16833         break;
16834       gen_formal_parameter_die (arg, NULL,
16835                                 false /* Don't emit name attribute.  */,
16836                                 parm_pack_die);
16837     }
16838   if (next_arg)
16839     *next_arg = arg;
16840   return parm_pack_die;
16841 }
16842
16843 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16844    at the end of an (ANSI prototyped) formal parameters list.  */
16845
16846 static void
16847 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16848 {
16849   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16850 }
16851
16852 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16853    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16854    parameters as specified in some function type specification (except for
16855    those which appear as part of a function *definition*).  */
16856
16857 static void
16858 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16859 {
16860   tree link;
16861   tree formal_type = NULL;
16862   tree first_parm_type;
16863   tree arg;
16864
16865   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16866     {
16867       arg = DECL_ARGUMENTS (function_or_method_type);
16868       function_or_method_type = TREE_TYPE (function_or_method_type);
16869     }
16870   else
16871     arg = NULL_TREE;
16872
16873   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16874
16875   /* Make our first pass over the list of formal parameter types and output a
16876      DW_TAG_formal_parameter DIE for each one.  */
16877   for (link = first_parm_type; link; )
16878     {
16879       dw_die_ref parm_die;
16880
16881       formal_type = TREE_VALUE (link);
16882       if (formal_type == void_type_node)
16883         break;
16884
16885       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16886       parm_die = gen_formal_parameter_die (formal_type, NULL,
16887                                            true /* Emit name attribute.  */,
16888                                            context_die);
16889       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16890           && link == first_parm_type)
16891         {
16892           add_AT_flag (parm_die, DW_AT_artificial, 1);
16893           if (dwarf_version >= 3 || !dwarf_strict)
16894             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16895         }
16896       else if (arg && DECL_ARTIFICIAL (arg))
16897         add_AT_flag (parm_die, DW_AT_artificial, 1);
16898
16899       link = TREE_CHAIN (link);
16900       if (arg)
16901         arg = DECL_CHAIN (arg);
16902     }
16903
16904   /* If this function type has an ellipsis, add a
16905      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16906   if (formal_type != void_type_node)
16907     gen_unspecified_parameters_die (function_or_method_type, context_die);
16908
16909   /* Make our second (and final) pass over the list of formal parameter types
16910      and output DIEs to represent those types (as necessary).  */
16911   for (link = TYPE_ARG_TYPES (function_or_method_type);
16912        link && TREE_VALUE (link);
16913        link = TREE_CHAIN (link))
16914     gen_type_die (TREE_VALUE (link), context_die);
16915 }
16916
16917 /* We want to generate the DIE for TYPE so that we can generate the
16918    die for MEMBER, which has been defined; we will need to refer back
16919    to the member declaration nested within TYPE.  If we're trying to
16920    generate minimal debug info for TYPE, processing TYPE won't do the
16921    trick; we need to attach the member declaration by hand.  */
16922
16923 static void
16924 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16925 {
16926   gen_type_die (type, context_die);
16927
16928   /* If we're trying to avoid duplicate debug info, we may not have
16929      emitted the member decl for this function.  Emit it now.  */
16930   if (TYPE_STUB_DECL (type)
16931       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16932       && ! lookup_decl_die (member))
16933     {
16934       dw_die_ref type_die;
16935       gcc_assert (!decl_ultimate_origin (member));
16936
16937       push_decl_scope (type);
16938       type_die = lookup_type_die_strip_naming_typedef (type);
16939       if (TREE_CODE (member) == FUNCTION_DECL)
16940         gen_subprogram_die (member, type_die);
16941       else if (TREE_CODE (member) == FIELD_DECL)
16942         {
16943           /* Ignore the nameless fields that are used to skip bits but handle
16944              C++ anonymous unions and structs.  */
16945           if (DECL_NAME (member) != NULL_TREE
16946               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16947               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16948             {
16949               gen_type_die (member_declared_type (member), type_die);
16950               gen_field_die (member, type_die);
16951             }
16952         }
16953       else
16954         gen_variable_die (member, NULL_TREE, type_die);
16955
16956       pop_decl_scope ();
16957     }
16958 }
16959
16960 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16961    may later generate inlined and/or out-of-line instances of.  */
16962
16963 static void
16964 dwarf2out_abstract_function (tree decl)
16965 {
16966   dw_die_ref old_die;
16967   tree save_fn;
16968   tree context;
16969   int was_abstract;
16970   htab_t old_decl_loc_table;
16971   htab_t old_cached_dw_loc_list_table;
16972   int old_call_site_count, old_tail_call_site_count;
16973   struct call_arg_loc_node *old_call_arg_locations;
16974
16975   /* Make sure we have the actual abstract inline, not a clone.  */
16976   decl = DECL_ORIGIN (decl);
16977
16978   old_die = lookup_decl_die (decl);
16979   if (old_die && get_AT (old_die, DW_AT_inline))
16980     /* We've already generated the abstract instance.  */
16981     return;
16982
16983   /* We can be called while recursively when seeing block defining inlined subroutine
16984      DIE.  Be sure to not clobber the outer location table nor use it or we would
16985      get locations in abstract instantces.  */
16986   old_decl_loc_table = decl_loc_table;
16987   decl_loc_table = NULL;
16988   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16989   cached_dw_loc_list_table = NULL;
16990   old_call_arg_locations = call_arg_locations;
16991   call_arg_locations = NULL;
16992   old_call_site_count = call_site_count;
16993   call_site_count = -1;
16994   old_tail_call_site_count = tail_call_site_count;
16995   tail_call_site_count = -1;
16996
16997   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16998      we don't get confused by DECL_ABSTRACT.  */
16999   if (debug_info_level > DINFO_LEVEL_TERSE)
17000     {
17001       context = decl_class_context (decl);
17002       if (context)
17003         gen_type_die_for_member
17004           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17005     }
17006
17007   /* Pretend we've just finished compiling this function.  */
17008   save_fn = current_function_decl;
17009   current_function_decl = decl;
17010   push_cfun (DECL_STRUCT_FUNCTION (decl));
17011
17012   was_abstract = DECL_ABSTRACT (decl);
17013   set_decl_abstract_flags (decl, 1);
17014   dwarf2out_decl (decl);
17015   if (! was_abstract)
17016     set_decl_abstract_flags (decl, 0);
17017
17018   current_function_decl = save_fn;
17019   decl_loc_table = old_decl_loc_table;
17020   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17021   call_arg_locations = old_call_arg_locations;
17022   call_site_count = old_call_site_count;
17023   tail_call_site_count = old_tail_call_site_count;
17024   pop_cfun ();
17025 }
17026
17027 /* Helper function of premark_used_types() which gets called through
17028    htab_traverse.
17029
17030    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17031    marked as unused by prune_unused_types.  */
17032
17033 static int
17034 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17035 {
17036   tree type;
17037   dw_die_ref die;
17038
17039   type = (tree) *slot;
17040   die = lookup_type_die (type);
17041   if (die != NULL)
17042     die->die_perennial_p = 1;
17043   return 1;
17044 }
17045
17046 /* Helper function of premark_types_used_by_global_vars which gets called
17047    through htab_traverse.
17048
17049    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17050    marked as unused by prune_unused_types. The DIE of the type is marked
17051    only if the global variable using the type will actually be emitted.  */
17052
17053 static int
17054 premark_types_used_by_global_vars_helper (void **slot,
17055                                           void *data ATTRIBUTE_UNUSED)
17056 {
17057   struct types_used_by_vars_entry *entry;
17058   dw_die_ref die;
17059
17060   entry = (struct types_used_by_vars_entry *) *slot;
17061   gcc_assert (entry->type != NULL
17062               && entry->var_decl != NULL);
17063   die = lookup_type_die (entry->type);
17064   if (die)
17065     {
17066       /* Ask cgraph if the global variable really is to be emitted.
17067          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17068       struct varpool_node *node = varpool_get_node (entry->var_decl);
17069       if (node && node->needed)
17070         {
17071           die->die_perennial_p = 1;
17072           /* Keep the parent DIEs as well.  */
17073           while ((die = die->die_parent) && die->die_perennial_p == 0)
17074             die->die_perennial_p = 1;
17075         }
17076     }
17077   return 1;
17078 }
17079
17080 /* Mark all members of used_types_hash as perennial.  */
17081
17082 static void
17083 premark_used_types (void)
17084 {
17085   if (cfun && cfun->used_types_hash)
17086     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17087 }
17088
17089 /* Mark all members of types_used_by_vars_entry as perennial.  */
17090
17091 static void
17092 premark_types_used_by_global_vars (void)
17093 {
17094   if (types_used_by_vars_hash)
17095     htab_traverse (types_used_by_vars_hash,
17096                    premark_types_used_by_global_vars_helper, NULL);
17097 }
17098
17099 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17100    for CA_LOC call arg loc node.  */
17101
17102 static dw_die_ref
17103 gen_call_site_die (tree decl, dw_die_ref subr_die,
17104                    struct call_arg_loc_node *ca_loc)
17105 {
17106   dw_die_ref stmt_die = NULL, die;
17107   tree block = ca_loc->block;
17108
17109   while (block
17110          && block != DECL_INITIAL (decl)
17111          && TREE_CODE (block) == BLOCK)
17112     {
17113       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17114         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17115       if (stmt_die)
17116         break;
17117       block = BLOCK_SUPERCONTEXT (block);
17118     }
17119   if (stmt_die == NULL)
17120     stmt_die = subr_die;
17121   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17122   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17123   if (ca_loc->tail_call_p)
17124     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17125   if (ca_loc->symbol_ref)
17126     {
17127       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17128       if (tdie)
17129         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17130       else
17131         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17132     }
17133   return die;
17134 }
17135
17136 /* Generate a DIE to represent a declared function (either file-scope or
17137    block-local).  */
17138
17139 static void
17140 gen_subprogram_die (tree decl, dw_die_ref context_die)
17141 {
17142   tree origin = decl_ultimate_origin (decl);
17143   dw_die_ref subr_die;
17144   tree outer_scope;
17145   dw_die_ref old_die = lookup_decl_die (decl);
17146   int declaration = (current_function_decl != decl
17147                      || class_or_namespace_scope_p (context_die));
17148
17149   premark_used_types ();
17150
17151   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17152      started to generate the abstract instance of an inline, decided to output
17153      its containing class, and proceeded to emit the declaration of the inline
17154      from the member list for the class.  If so, DECLARATION takes priority;
17155      we'll get back to the abstract instance when done with the class.  */
17156
17157   /* The class-scope declaration DIE must be the primary DIE.  */
17158   if (origin && declaration && class_or_namespace_scope_p (context_die))
17159     {
17160       origin = NULL;
17161       gcc_assert (!old_die);
17162     }
17163
17164   /* Now that the C++ front end lazily declares artificial member fns, we
17165      might need to retrofit the declaration into its class.  */
17166   if (!declaration && !origin && !old_die
17167       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17168       && !class_or_namespace_scope_p (context_die)
17169       && debug_info_level > DINFO_LEVEL_TERSE)
17170     old_die = force_decl_die (decl);
17171
17172   if (origin != NULL)
17173     {
17174       gcc_assert (!declaration || local_scope_p (context_die));
17175
17176       /* Fixup die_parent for the abstract instance of a nested
17177          inline function.  */
17178       if (old_die && old_die->die_parent == NULL)
17179         add_child_die (context_die, old_die);
17180
17181       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17182       add_abstract_origin_attribute (subr_die, origin);
17183       /*  This is where the actual code for a cloned function is.
17184           Let's emit linkage name attribute for it.  This helps
17185           debuggers to e.g, set breakpoints into
17186           constructors/destructors when the user asks "break
17187           K::K".  */
17188       add_linkage_name (subr_die, decl);
17189     }
17190   else if (old_die)
17191     {
17192       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17193       struct dwarf_file_data * file_index = lookup_filename (s.file);
17194
17195       if (!get_AT_flag (old_die, DW_AT_declaration)
17196           /* We can have a normal definition following an inline one in the
17197              case of redefinition of GNU C extern inlines.
17198              It seems reasonable to use AT_specification in this case.  */
17199           && !get_AT (old_die, DW_AT_inline))
17200         {
17201           /* Detect and ignore this case, where we are trying to output
17202              something we have already output.  */
17203           return;
17204         }
17205
17206       /* If the definition comes from the same place as the declaration,
17207          maybe use the old DIE.  We always want the DIE for this function
17208          that has the *_pc attributes to be under comp_unit_die so the
17209          debugger can find it.  We also need to do this for abstract
17210          instances of inlines, since the spec requires the out-of-line copy
17211          to have the same parent.  For local class methods, this doesn't
17212          apply; we just use the old DIE.  */
17213       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17214           && (DECL_ARTIFICIAL (decl)
17215               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17216                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17217                       == (unsigned) s.line))))
17218         {
17219           subr_die = old_die;
17220
17221           /* Clear out the declaration attribute and the formal parameters.
17222              Do not remove all children, because it is possible that this
17223              declaration die was forced using force_decl_die(). In such
17224              cases die that forced declaration die (e.g. TAG_imported_module)
17225              is one of the children that we do not want to remove.  */
17226           remove_AT (subr_die, DW_AT_declaration);
17227           remove_AT (subr_die, DW_AT_object_pointer);
17228           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17229         }
17230       else
17231         {
17232           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17233           add_AT_specification (subr_die, old_die);
17234           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17235             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17236           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17237             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17238         }
17239     }
17240   else
17241     {
17242       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17243
17244       if (TREE_PUBLIC (decl))
17245         add_AT_flag (subr_die, DW_AT_external, 1);
17246
17247       add_name_and_src_coords_attributes (subr_die, decl);
17248       if (debug_info_level > DINFO_LEVEL_TERSE)
17249         {
17250           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17251           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17252                               0, 0, context_die);
17253         }
17254
17255       add_pure_or_virtual_attribute (subr_die, decl);
17256       if (DECL_ARTIFICIAL (decl))
17257         add_AT_flag (subr_die, DW_AT_artificial, 1);
17258
17259       add_accessibility_attribute (subr_die, decl);
17260     }
17261
17262   if (declaration)
17263     {
17264       if (!old_die || !get_AT (old_die, DW_AT_inline))
17265         {
17266           add_AT_flag (subr_die, DW_AT_declaration, 1);
17267
17268           /* If this is an explicit function declaration then generate
17269              a DW_AT_explicit attribute.  */
17270           if (lang_hooks.decls.function_decl_explicit_p (decl)
17271               && (dwarf_version >= 3 || !dwarf_strict))
17272             add_AT_flag (subr_die, DW_AT_explicit, 1);
17273
17274           /* The first time we see a member function, it is in the context of
17275              the class to which it belongs.  We make sure of this by emitting
17276              the class first.  The next time is the definition, which is
17277              handled above.  The two may come from the same source text.
17278
17279              Note that force_decl_die() forces function declaration die. It is
17280              later reused to represent definition.  */
17281           equate_decl_number_to_die (decl, subr_die);
17282         }
17283     }
17284   else if (DECL_ABSTRACT (decl))
17285     {
17286       if (DECL_DECLARED_INLINE_P (decl))
17287         {
17288           if (cgraph_function_possibly_inlined_p (decl))
17289             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17290           else
17291             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17292         }
17293       else
17294         {
17295           if (cgraph_function_possibly_inlined_p (decl))
17296             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17297           else
17298             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17299         }
17300
17301       if (DECL_DECLARED_INLINE_P (decl)
17302           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17303         add_AT_flag (subr_die, DW_AT_artificial, 1);
17304
17305       equate_decl_number_to_die (decl, subr_die);
17306     }
17307   else if (!DECL_EXTERNAL (decl))
17308     {
17309       HOST_WIDE_INT cfa_fb_offset;
17310
17311       if (!old_die || !get_AT (old_die, DW_AT_inline))
17312         equate_decl_number_to_die (decl, subr_die);
17313
17314       if (!flag_reorder_blocks_and_partition)
17315         {
17316           dw_fde_ref fde = cfun->fde;
17317           if (fde->dw_fde_begin)
17318             {
17319               /* We have already generated the labels.  */
17320               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17321               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17322             }
17323           else
17324             {
17325               /* Create start/end labels and add the range.  */
17326               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17327               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17328                                            current_function_funcdef_no);
17329               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17330               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17331                                            current_function_funcdef_no);
17332               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17333             }
17334
17335 #if VMS_DEBUGGING_INFO
17336       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17337          Section 2.3 Prologue and Epilogue Attributes:
17338          When a breakpoint is set on entry to a function, it is generally
17339          desirable for execution to be suspended, not on the very first
17340          instruction of the function, but rather at a point after the
17341          function's frame has been set up, after any language defined local
17342          declaration processing has been completed, and before execution of
17343          the first statement of the function begins. Debuggers generally
17344          cannot properly determine where this point is.  Similarly for a
17345          breakpoint set on exit from a function. The prologue and epilogue
17346          attributes allow a compiler to communicate the location(s) to use.  */
17347
17348       {
17349         if (fde->dw_fde_vms_end_prologue)
17350           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17351             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17352
17353         if (fde->dw_fde_vms_begin_epilogue)
17354           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17355             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17356       }
17357 #endif
17358
17359           add_pubname (decl, subr_die);
17360         }
17361       else
17362         {
17363           /* Generate pubnames entries for the split function code ranges.  */
17364           dw_fde_ref fde = cfun->fde;
17365
17366           if (fde->dw_fde_second_begin)
17367             {
17368               if (dwarf_version >= 3 || !dwarf_strict)
17369                 {
17370                   /* We should use ranges for non-contiguous code section 
17371                      addresses.  Use the actual code range for the initial
17372                      section, since the HOT/COLD labels might precede an 
17373                      alignment offset.  */
17374                   bool range_list_added = false;
17375                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17376                                         fde->dw_fde_end, &range_list_added);
17377                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17378                                         fde->dw_fde_second_end,
17379                                         &range_list_added);
17380                   add_pubname (decl, subr_die);
17381                   if (range_list_added)
17382                     add_ranges (NULL);
17383                 }
17384               else
17385                 {
17386                   /* There is no real support in DW2 for this .. so we make
17387                      a work-around.  First, emit the pub name for the segment
17388                      containing the function label.  Then make and emit a
17389                      simplified subprogram DIE for the second segment with the
17390                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17391                      linkage name for the second die so that gdb will find both
17392                      sections when given "b foo".  */
17393                   const char *name = NULL;
17394                   tree decl_name = DECL_NAME (decl);
17395                   dw_die_ref seg_die;
17396
17397                   /* Do the 'primary' section.   */
17398                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17399                                  fde->dw_fde_begin);
17400                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17401                                  fde->dw_fde_end);
17402                   /* Add it.   */
17403                   add_pubname (decl, subr_die);
17404
17405                   /* Build a minimal DIE for the secondary section.  */
17406                   seg_die = new_die (DW_TAG_subprogram,
17407                                      subr_die->die_parent, decl);
17408
17409                   if (TREE_PUBLIC (decl))
17410                     add_AT_flag (seg_die, DW_AT_external, 1);
17411
17412                   if (decl_name != NULL 
17413                       && IDENTIFIER_POINTER (decl_name) != NULL)
17414                     {
17415                       name = dwarf2_name (decl, 1);
17416                       if (! DECL_ARTIFICIAL (decl))
17417                         add_src_coords_attributes (seg_die, decl);
17418
17419                       add_linkage_name (seg_die, decl);
17420                     }
17421                   gcc_assert (name != NULL);
17422                   add_pure_or_virtual_attribute (seg_die, decl);
17423                   if (DECL_ARTIFICIAL (decl))
17424                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17425
17426                   name = concat ("__second_sect_of_", name, NULL); 
17427                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17428                                  fde->dw_fde_second_begin);
17429                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17430                                  fde->dw_fde_second_end);
17431                   add_name_attribute (seg_die, name);
17432                   add_pubname_string (name, seg_die);
17433                 }
17434             }
17435           else
17436             {
17437               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17438               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17439               add_pubname (decl, subr_die);
17440             }
17441         }
17442
17443 #ifdef MIPS_DEBUGGING_INFO
17444       /* Add a reference to the FDE for this routine.  */
17445       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17446 #endif
17447
17448       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17449
17450       /* We define the "frame base" as the function's CFA.  This is more
17451          convenient for several reasons: (1) It's stable across the prologue
17452          and epilogue, which makes it better than just a frame pointer,
17453          (2) With dwarf3, there exists a one-byte encoding that allows us
17454          to reference the .debug_frame data by proxy, but failing that,
17455          (3) We can at least reuse the code inspection and interpretation
17456          code that determines the CFA position at various points in the
17457          function.  */
17458       if (dwarf_version >= 3)
17459         {
17460           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17461           add_AT_loc (subr_die, DW_AT_frame_base, op);
17462         }
17463       else
17464         {
17465           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17466           if (list->dw_loc_next)
17467             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17468           else
17469             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17470         }
17471
17472       /* Compute a displacement from the "steady-state frame pointer" to
17473          the CFA.  The former is what all stack slots and argument slots
17474          will reference in the rtl; the later is what we've told the
17475          debugger about.  We'll need to adjust all frame_base references
17476          by this displacement.  */
17477       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17478
17479       if (cfun->static_chain_decl)
17480         add_AT_location_description (subr_die, DW_AT_static_link,
17481                  loc_list_from_tree (cfun->static_chain_decl, 2));
17482     }
17483
17484   /* Generate child dies for template paramaters.  */
17485   if (debug_info_level > DINFO_LEVEL_TERSE)
17486     gen_generic_params_dies (decl);
17487
17488   /* Now output descriptions of the arguments for this function. This gets
17489      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17490      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17491      `...' at the end of the formal parameter list.  In order to find out if
17492      there was a trailing ellipsis or not, we must instead look at the type
17493      associated with the FUNCTION_DECL.  This will be a node of type
17494      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17495      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17496      an ellipsis at the end.  */
17497
17498   /* In the case where we are describing a mere function declaration, all we
17499      need to do here (and all we *can* do here) is to describe the *types* of
17500      its formal parameters.  */
17501   if (debug_info_level <= DINFO_LEVEL_TERSE)
17502     ;
17503   else if (declaration)
17504     gen_formal_types_die (decl, subr_die);
17505   else
17506     {
17507       /* Generate DIEs to represent all known formal parameters.  */
17508       tree parm = DECL_ARGUMENTS (decl);
17509       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17510       tree generic_decl_parm = generic_decl
17511                                 ? DECL_ARGUMENTS (generic_decl)
17512                                 : NULL;
17513
17514       /* Now we want to walk the list of parameters of the function and
17515          emit their relevant DIEs.
17516
17517          We consider the case of DECL being an instance of a generic function
17518          as well as it being a normal function.
17519
17520          If DECL is an instance of a generic function we walk the
17521          parameters of the generic function declaration _and_ the parameters of
17522          DECL itself. This is useful because we want to emit specific DIEs for
17523          function parameter packs and those are declared as part of the
17524          generic function declaration. In that particular case,
17525          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17526          That DIE has children DIEs representing the set of arguments
17527          of the pack. Note that the set of pack arguments can be empty.
17528          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17529          children DIE.
17530
17531          Otherwise, we just consider the parameters of DECL.  */
17532       while (generic_decl_parm || parm)
17533         {
17534           if (generic_decl_parm
17535               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17536             gen_formal_parameter_pack_die (generic_decl_parm,
17537                                            parm, subr_die,
17538                                            &parm);
17539           else if (parm)
17540             {
17541               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17542
17543               if (parm == DECL_ARGUMENTS (decl)
17544                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17545                   && parm_die
17546                   && (dwarf_version >= 3 || !dwarf_strict))
17547                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17548
17549               parm = DECL_CHAIN (parm);
17550             }
17551
17552           if (generic_decl_parm)
17553             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17554         }
17555
17556       /* Decide whether we need an unspecified_parameters DIE at the end.
17557          There are 2 more cases to do this for: 1) the ansi ... declaration -
17558          this is detectable when the end of the arg list is not a
17559          void_type_node 2) an unprototyped function declaration (not a
17560          definition).  This just means that we have no info about the
17561          parameters at all.  */
17562       if (prototype_p (TREE_TYPE (decl)))
17563         {
17564           /* This is the prototyped case, check for....  */
17565           if (stdarg_p (TREE_TYPE (decl)))
17566             gen_unspecified_parameters_die (decl, subr_die);
17567         }
17568       else if (DECL_INITIAL (decl) == NULL_TREE)
17569         gen_unspecified_parameters_die (decl, subr_die);
17570     }
17571
17572   /* Output Dwarf info for all of the stuff within the body of the function
17573      (if it has one - it may be just a declaration).  */
17574   outer_scope = DECL_INITIAL (decl);
17575
17576   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17577      a function.  This BLOCK actually represents the outermost binding contour
17578      for the function, i.e. the contour in which the function's formal
17579      parameters and labels get declared. Curiously, it appears that the front
17580      end doesn't actually put the PARM_DECL nodes for the current function onto
17581      the BLOCK_VARS list for this outer scope, but are strung off of the
17582      DECL_ARGUMENTS list for the function instead.
17583
17584      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17585      the LABEL_DECL nodes for the function however, and we output DWARF info
17586      for those in decls_for_scope.  Just within the `outer_scope' there will be
17587      a BLOCK node representing the function's outermost pair of curly braces,
17588      and any blocks used for the base and member initializers of a C++
17589      constructor function.  */
17590   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17591     {
17592       int call_site_note_count = 0;
17593       int tail_call_site_note_count = 0;
17594
17595       /* Emit a DW_TAG_variable DIE for a named return value.  */
17596       if (DECL_NAME (DECL_RESULT (decl)))
17597         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17598
17599       current_function_has_inlines = 0;
17600       decls_for_scope (outer_scope, subr_die, 0);
17601
17602       if (call_arg_locations && !dwarf_strict)
17603         {
17604           struct call_arg_loc_node *ca_loc;
17605           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17606             {
17607               dw_die_ref die = NULL;
17608               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17609               rtx arg, next_arg;
17610
17611               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17612                    arg; arg = next_arg)
17613                 {
17614                   dw_loc_descr_ref reg, val;
17615                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17616                   dw_die_ref cdie, tdie = NULL;
17617
17618                   next_arg = XEXP (arg, 1);
17619                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17620                       && next_arg
17621                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17622                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17623                       && REGNO (XEXP (XEXP (arg, 0), 0))
17624                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17625                     next_arg = XEXP (next_arg, 1);
17626                   if (mode == VOIDmode)
17627                     {
17628                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17629                       if (mode == VOIDmode)
17630                         mode = GET_MODE (XEXP (arg, 0));
17631                     }
17632                   if (mode == VOIDmode || mode == BLKmode)
17633                     continue;
17634                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17635                     {
17636                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17637                       tloc = XEXP (XEXP (arg, 0), 1);
17638                       continue;
17639                     }
17640                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17641                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17642                     {
17643                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17644                       tlocc = XEXP (XEXP (arg, 0), 1);
17645                       continue;
17646                     }
17647                   reg = NULL;
17648                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17649                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17650                                               VAR_INIT_STATUS_INITIALIZED);
17651                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17652                     {
17653                       rtx mem = XEXP (XEXP (arg, 0), 0);
17654                       reg = mem_loc_descriptor (XEXP (mem, 0),
17655                                                 get_address_mode (mem),
17656                                                 GET_MODE (mem),
17657                                                 VAR_INIT_STATUS_INITIALIZED);
17658                     }
17659                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17660                            == DEBUG_PARAMETER_REF)
17661                     {
17662                       tree tdecl
17663                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17664                       tdie = lookup_decl_die (tdecl);
17665                       if (tdie == NULL)
17666                         continue;
17667                     }
17668                   else
17669                     continue;
17670                   if (reg == NULL
17671                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17672                          != DEBUG_PARAMETER_REF)
17673                     continue;
17674                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17675                                             VOIDmode,
17676                                             VAR_INIT_STATUS_INITIALIZED);
17677                   if (val == NULL)
17678                     continue;
17679                   if (die == NULL)
17680                     die = gen_call_site_die (decl, subr_die, ca_loc);
17681                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17682                                   NULL_TREE);
17683                   if (reg != NULL)
17684                     add_AT_loc (cdie, DW_AT_location, reg);
17685                   else if (tdie != NULL)
17686                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17687                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17688                   if (next_arg != XEXP (arg, 1))
17689                     {
17690                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17691                       if (mode == VOIDmode)
17692                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17693                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17694                                                             0), 1),
17695                                                 mode, VOIDmode,
17696                                                 VAR_INIT_STATUS_INITIALIZED);
17697                       if (val != NULL)
17698                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17699                     }
17700                 }
17701               if (die == NULL
17702                   && (ca_loc->symbol_ref || tloc))
17703                 die = gen_call_site_die (decl, subr_die, ca_loc);
17704               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17705                 {
17706                   dw_loc_descr_ref tval = NULL;
17707
17708                   if (tloc != NULL_RTX)
17709                     tval = mem_loc_descriptor (tloc,
17710                                                GET_MODE (tloc) == VOIDmode
17711                                                ? Pmode : GET_MODE (tloc),
17712                                                VOIDmode,
17713                                                VAR_INIT_STATUS_INITIALIZED);
17714                   if (tval)
17715                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17716                   else if (tlocc != NULL_RTX)
17717                     {
17718                       tval = mem_loc_descriptor (tlocc,
17719                                                  GET_MODE (tlocc) == VOIDmode
17720                                                  ? Pmode : GET_MODE (tlocc),
17721                                                  VOIDmode,
17722                                                  VAR_INIT_STATUS_INITIALIZED);
17723                       if (tval)
17724                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17725                                     tval);
17726                     }
17727                 }
17728               if (die != NULL)
17729                 {
17730                   call_site_note_count++;
17731                   if (ca_loc->tail_call_p)
17732                     tail_call_site_note_count++;
17733                 }
17734             }
17735         }
17736       call_arg_locations = NULL;
17737       call_arg_loc_last = NULL;
17738       if (tail_call_site_count >= 0
17739           && tail_call_site_count == tail_call_site_note_count
17740           && !dwarf_strict)
17741         {
17742           if (call_site_count >= 0
17743               && call_site_count == call_site_note_count)
17744             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17745           else
17746             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17747         }
17748       call_site_count = -1;
17749       tail_call_site_count = -1;
17750     }
17751   /* Add the calling convention attribute if requested.  */
17752   add_calling_convention_attribute (subr_die, decl);
17753
17754 }
17755
17756 /* Returns a hash value for X (which really is a die_struct).  */
17757
17758 static hashval_t
17759 common_block_die_table_hash (const void *x)
17760 {
17761   const_dw_die_ref d = (const_dw_die_ref) x;
17762   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17763 }
17764
17765 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17766    as decl_id and die_parent of die_struct Y.  */
17767
17768 static int
17769 common_block_die_table_eq (const void *x, const void *y)
17770 {
17771   const_dw_die_ref d = (const_dw_die_ref) x;
17772   const_dw_die_ref e = (const_dw_die_ref) y;
17773   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17774 }
17775
17776 /* Generate a DIE to represent a declared data object.
17777    Either DECL or ORIGIN must be non-null.  */
17778
17779 static void
17780 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17781 {
17782   HOST_WIDE_INT off;
17783   tree com_decl;
17784   tree decl_or_origin = decl ? decl : origin;
17785   tree ultimate_origin;
17786   dw_die_ref var_die;
17787   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17788   dw_die_ref origin_die;
17789   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17790                       || class_or_namespace_scope_p (context_die));
17791   bool specialization_p = false;
17792
17793   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17794   if (decl || ultimate_origin)
17795     origin = ultimate_origin;
17796   com_decl = fortran_common (decl_or_origin, &off);
17797
17798   /* Symbol in common gets emitted as a child of the common block, in the form
17799      of a data member.  */
17800   if (com_decl)
17801     {
17802       dw_die_ref com_die;
17803       dw_loc_list_ref loc;
17804       die_node com_die_arg;
17805
17806       var_die = lookup_decl_die (decl_or_origin);
17807       if (var_die)
17808         {
17809           if (get_AT (var_die, DW_AT_location) == NULL)
17810             {
17811               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17812               if (loc)
17813                 {
17814                   if (off)
17815                     {
17816                       /* Optimize the common case.  */
17817                       if (single_element_loc_list_p (loc)
17818                           && loc->expr->dw_loc_opc == DW_OP_addr
17819                           && loc->expr->dw_loc_next == NULL
17820                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17821                              == SYMBOL_REF)
17822                         loc->expr->dw_loc_oprnd1.v.val_addr
17823                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17824                         else
17825                           loc_list_plus_const (loc, off);
17826                     }
17827                   add_AT_location_description (var_die, DW_AT_location, loc);
17828                   remove_AT (var_die, DW_AT_declaration);
17829                 }
17830             }
17831           return;
17832         }
17833
17834       if (common_block_die_table == NULL)
17835         common_block_die_table
17836           = htab_create_ggc (10, common_block_die_table_hash,
17837                              common_block_die_table_eq, NULL);
17838
17839       com_die_arg.decl_id = DECL_UID (com_decl);
17840       com_die_arg.die_parent = context_die;
17841       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17842       loc = loc_list_from_tree (com_decl, 2);
17843       if (com_die == NULL)
17844         {
17845           const char *cnam
17846             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17847           void **slot;
17848
17849           com_die = new_die (DW_TAG_common_block, context_die, decl);
17850           add_name_and_src_coords_attributes (com_die, com_decl);
17851           if (loc)
17852             {
17853               add_AT_location_description (com_die, DW_AT_location, loc);
17854               /* Avoid sharing the same loc descriptor between
17855                  DW_TAG_common_block and DW_TAG_variable.  */
17856               loc = loc_list_from_tree (com_decl, 2);
17857             }
17858           else if (DECL_EXTERNAL (decl))
17859             add_AT_flag (com_die, DW_AT_declaration, 1);
17860           add_pubname_string (cnam, com_die); /* ??? needed? */
17861           com_die->decl_id = DECL_UID (com_decl);
17862           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17863           *slot = (void *) com_die;
17864         }
17865       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17866         {
17867           add_AT_location_description (com_die, DW_AT_location, loc);
17868           loc = loc_list_from_tree (com_decl, 2);
17869           remove_AT (com_die, DW_AT_declaration);
17870         }
17871       var_die = new_die (DW_TAG_variable, com_die, decl);
17872       add_name_and_src_coords_attributes (var_die, decl);
17873       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17874                           TREE_THIS_VOLATILE (decl), context_die);
17875       add_AT_flag (var_die, DW_AT_external, 1);
17876       if (loc)
17877         {
17878           if (off)
17879             {
17880               /* Optimize the common case.  */
17881               if (single_element_loc_list_p (loc)
17882                   && loc->expr->dw_loc_opc == DW_OP_addr
17883                   && loc->expr->dw_loc_next == NULL
17884                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17885                 loc->expr->dw_loc_oprnd1.v.val_addr
17886                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17887               else
17888                 loc_list_plus_const (loc, off);
17889             }
17890           add_AT_location_description (var_die, DW_AT_location, loc);
17891         }
17892       else if (DECL_EXTERNAL (decl))
17893         add_AT_flag (var_die, DW_AT_declaration, 1);
17894       equate_decl_number_to_die (decl, var_die);
17895       return;
17896     }
17897
17898   /* If the compiler emitted a definition for the DECL declaration
17899      and if we already emitted a DIE for it, don't emit a second
17900      DIE for it again. Allow re-declarations of DECLs that are
17901      inside functions, though.  */
17902   if (old_die && declaration && !local_scope_p (context_die))
17903     return;
17904
17905   /* For static data members, the declaration in the class is supposed
17906      to have DW_TAG_member tag; the specification should still be
17907      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17908   if (declaration && class_scope_p (context_die))
17909     var_die = new_die (DW_TAG_member, context_die, decl);
17910   else
17911     var_die = new_die (DW_TAG_variable, context_die, decl);
17912
17913   origin_die = NULL;
17914   if (origin != NULL)
17915     origin_die = add_abstract_origin_attribute (var_die, origin);
17916
17917   /* Loop unrolling can create multiple blocks that refer to the same
17918      static variable, so we must test for the DW_AT_declaration flag.
17919
17920      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17921      copy decls and set the DECL_ABSTRACT flag on them instead of
17922      sharing them.
17923
17924      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17925
17926      ??? The declare_in_namespace support causes us to get two DIEs for one
17927      variable, both of which are declarations.  We want to avoid considering
17928      one to be a specification, so we must test that this DIE is not a
17929      declaration.  */
17930   else if (old_die && TREE_STATIC (decl) && ! declaration
17931            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17932     {
17933       /* This is a definition of a C++ class level static.  */
17934       add_AT_specification (var_die, old_die);
17935       specialization_p = true;
17936       if (DECL_NAME (decl))
17937         {
17938           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17939           struct dwarf_file_data * file_index = lookup_filename (s.file);
17940
17941           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17942             add_AT_file (var_die, DW_AT_decl_file, file_index);
17943
17944           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17945             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17946
17947           if (old_die->die_tag == DW_TAG_member)
17948             add_linkage_name (var_die, decl);
17949         }
17950     }
17951   else
17952     add_name_and_src_coords_attributes (var_die, decl);
17953
17954   if ((origin == NULL && !specialization_p)
17955       || (origin != NULL
17956           && !DECL_ABSTRACT (decl_or_origin)
17957           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17958                                        decl_function_context
17959                                                         (decl_or_origin))))
17960     {
17961       tree type = TREE_TYPE (decl_or_origin);
17962
17963       if (decl_by_reference_p (decl_or_origin))
17964         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17965       else
17966         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17967                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17968     }
17969
17970   if (origin == NULL && !specialization_p)
17971     {
17972       if (TREE_PUBLIC (decl))
17973         add_AT_flag (var_die, DW_AT_external, 1);
17974
17975       if (DECL_ARTIFICIAL (decl))
17976         add_AT_flag (var_die, DW_AT_artificial, 1);
17977
17978       add_accessibility_attribute (var_die, decl);
17979     }
17980
17981   if (declaration)
17982     add_AT_flag (var_die, DW_AT_declaration, 1);
17983
17984   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17985     equate_decl_number_to_die (decl, var_die);
17986
17987   if (! declaration
17988       && (! DECL_ABSTRACT (decl_or_origin)
17989           /* Local static vars are shared between all clones/inlines,
17990              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17991              already set.  */
17992           || (TREE_CODE (decl_or_origin) == VAR_DECL
17993               && TREE_STATIC (decl_or_origin)
17994               && DECL_RTL_SET_P (decl_or_origin)))
17995       /* When abstract origin already has DW_AT_location attribute, no need
17996          to add it again.  */
17997       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17998     {
17999       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18000           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18001         defer_location (decl_or_origin, var_die);
18002       else
18003         add_location_or_const_value_attribute (var_die, decl_or_origin,
18004                                                decl == NULL, DW_AT_location);
18005       add_pubname (decl_or_origin, var_die);
18006     }
18007   else
18008     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18009 }
18010
18011 /* Generate a DIE to represent a named constant.  */
18012
18013 static void
18014 gen_const_die (tree decl, dw_die_ref context_die)
18015 {
18016   dw_die_ref const_die;
18017   tree type = TREE_TYPE (decl);
18018
18019   const_die = new_die (DW_TAG_constant, context_die, decl);
18020   add_name_and_src_coords_attributes (const_die, decl);
18021   add_type_attribute (const_die, type, 1, 0, context_die);
18022   if (TREE_PUBLIC (decl))
18023     add_AT_flag (const_die, DW_AT_external, 1);
18024   if (DECL_ARTIFICIAL (decl))
18025     add_AT_flag (const_die, DW_AT_artificial, 1);
18026   tree_add_const_value_attribute_for_decl (const_die, decl);
18027 }
18028
18029 /* Generate a DIE to represent a label identifier.  */
18030
18031 static void
18032 gen_label_die (tree decl, dw_die_ref context_die)
18033 {
18034   tree origin = decl_ultimate_origin (decl);
18035   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18036   rtx insn;
18037   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18038
18039   if (origin != NULL)
18040     add_abstract_origin_attribute (lbl_die, origin);
18041   else
18042     add_name_and_src_coords_attributes (lbl_die, decl);
18043
18044   if (DECL_ABSTRACT (decl))
18045     equate_decl_number_to_die (decl, lbl_die);
18046   else
18047     {
18048       insn = DECL_RTL_IF_SET (decl);
18049
18050       /* Deleted labels are programmer specified labels which have been
18051          eliminated because of various optimizations.  We still emit them
18052          here so that it is possible to put breakpoints on them.  */
18053       if (insn
18054           && (LABEL_P (insn)
18055               || ((NOTE_P (insn)
18056                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18057         {
18058           /* When optimization is enabled (via -O) some parts of the compiler
18059              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18060              represent source-level labels which were explicitly declared by
18061              the user.  This really shouldn't be happening though, so catch
18062              it if it ever does happen.  */
18063           gcc_assert (!INSN_DELETED_P (insn));
18064
18065           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18066           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18067         }
18068       else if (insn
18069                && NOTE_P (insn)
18070                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18071                && CODE_LABEL_NUMBER (insn) != -1)
18072         {
18073           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18074           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18075         }
18076     }
18077 }
18078
18079 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18080    attributes to the DIE for a block STMT, to describe where the inlined
18081    function was called from.  This is similar to add_src_coords_attributes.  */
18082
18083 static inline void
18084 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18085 {
18086   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18087
18088   if (dwarf_version >= 3 || !dwarf_strict)
18089     {
18090       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18091       add_AT_unsigned (die, DW_AT_call_line, s.line);
18092     }
18093 }
18094
18095
18096 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18097    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18098
18099 static inline void
18100 add_high_low_attributes (tree stmt, dw_die_ref die)
18101 {
18102   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18103
18104   if (BLOCK_FRAGMENT_CHAIN (stmt)
18105       && (dwarf_version >= 3 || !dwarf_strict))
18106     {
18107       tree chain;
18108
18109       if (inlined_function_outer_scope_p (stmt))
18110         {
18111           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18112                                        BLOCK_NUMBER (stmt));
18113           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18114         }
18115
18116       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18117
18118       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18119       do
18120         {
18121           add_ranges (chain);
18122           chain = BLOCK_FRAGMENT_CHAIN (chain);
18123         }
18124       while (chain);
18125       add_ranges (NULL);
18126     }
18127   else
18128     {
18129       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18130                                    BLOCK_NUMBER (stmt));
18131       add_AT_lbl_id (die, DW_AT_low_pc, label);
18132       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18133                                    BLOCK_NUMBER (stmt));
18134       add_AT_lbl_id (die, DW_AT_high_pc, label);
18135     }
18136 }
18137
18138 /* Generate a DIE for a lexical block.  */
18139
18140 static void
18141 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18142 {
18143   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18144
18145   if (call_arg_locations)
18146     {
18147       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18148         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18149                                BLOCK_NUMBER (stmt) + 1);
18150       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18151     }
18152
18153   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18154     add_high_low_attributes (stmt, stmt_die);
18155
18156   decls_for_scope (stmt, stmt_die, depth);
18157 }
18158
18159 /* Generate a DIE for an inlined subprogram.  */
18160
18161 static void
18162 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18163 {
18164   tree decl;
18165
18166   /* The instance of function that is effectively being inlined shall not
18167      be abstract.  */
18168   gcc_assert (! BLOCK_ABSTRACT (stmt));
18169
18170   decl = block_ultimate_origin (stmt);
18171
18172   /* Emit info for the abstract instance first, if we haven't yet.  We
18173      must emit this even if the block is abstract, otherwise when we
18174      emit the block below (or elsewhere), we may end up trying to emit
18175      a die whose origin die hasn't been emitted, and crashing.  */
18176   dwarf2out_abstract_function (decl);
18177
18178   if (! BLOCK_ABSTRACT (stmt))
18179     {
18180       dw_die_ref subr_die
18181         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18182
18183       if (call_arg_locations)
18184         {
18185           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18186             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18187                                    BLOCK_NUMBER (stmt) + 1);
18188           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18189         }
18190       add_abstract_origin_attribute (subr_die, decl);
18191       if (TREE_ASM_WRITTEN (stmt))
18192         add_high_low_attributes (stmt, subr_die);
18193       add_call_src_coords_attributes (stmt, subr_die);
18194
18195       decls_for_scope (stmt, subr_die, depth);
18196       current_function_has_inlines = 1;
18197     }
18198 }
18199
18200 /* Generate a DIE for a field in a record, or structure.  */
18201
18202 static void
18203 gen_field_die (tree decl, dw_die_ref context_die)
18204 {
18205   dw_die_ref decl_die;
18206
18207   if (TREE_TYPE (decl) == error_mark_node)
18208     return;
18209
18210   decl_die = new_die (DW_TAG_member, context_die, decl);
18211   add_name_and_src_coords_attributes (decl_die, decl);
18212   add_type_attribute (decl_die, member_declared_type (decl),
18213                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18214                       context_die);
18215
18216   if (DECL_BIT_FIELD_TYPE (decl))
18217     {
18218       add_byte_size_attribute (decl_die, decl);
18219       add_bit_size_attribute (decl_die, decl);
18220       add_bit_offset_attribute (decl_die, decl);
18221     }
18222
18223   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18224     add_data_member_location_attribute (decl_die, decl);
18225
18226   if (DECL_ARTIFICIAL (decl))
18227     add_AT_flag (decl_die, DW_AT_artificial, 1);
18228
18229   add_accessibility_attribute (decl_die, decl);
18230
18231   /* Equate decl number to die, so that we can look up this decl later on.  */
18232   equate_decl_number_to_die (decl, decl_die);
18233 }
18234
18235 #if 0
18236 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18237    Use modified_type_die instead.
18238    We keep this code here just in case these types of DIEs may be needed to
18239    represent certain things in other languages (e.g. Pascal) someday.  */
18240
18241 static void
18242 gen_pointer_type_die (tree type, dw_die_ref context_die)
18243 {
18244   dw_die_ref ptr_die
18245     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18246
18247   equate_type_number_to_die (type, ptr_die);
18248   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18249   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18250 }
18251
18252 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18253    Use modified_type_die instead.
18254    We keep this code here just in case these types of DIEs may be needed to
18255    represent certain things in other languages (e.g. Pascal) someday.  */
18256
18257 static void
18258 gen_reference_type_die (tree type, dw_die_ref context_die)
18259 {
18260   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18261
18262   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18263     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18264   else
18265     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18266
18267   equate_type_number_to_die (type, ref_die);
18268   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18269   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18270 }
18271 #endif
18272
18273 /* Generate a DIE for a pointer to a member type.  */
18274
18275 static void
18276 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18277 {
18278   dw_die_ref ptr_die
18279     = new_die (DW_TAG_ptr_to_member_type,
18280                scope_die_for (type, context_die), type);
18281
18282   equate_type_number_to_die (type, ptr_die);
18283   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18284                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18285   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18286 }
18287
18288 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18289 DEF_VEC_P(dchar_p);
18290 DEF_VEC_ALLOC_P(dchar_p,heap);
18291
18292 static char *producer_string;
18293
18294 /* Return a heap allocated producer string including command line options
18295    if -grecord-gcc-switches.  */
18296
18297 static char *
18298 gen_producer_string (void)
18299 {
18300   size_t j;
18301   VEC(dchar_p, heap) *switches = NULL;
18302   const char *language_string = lang_hooks.name;
18303   char *producer, *tail;
18304   const char *p;
18305   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18306   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18307
18308   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18309     switch (save_decoded_options[j].opt_index)
18310       {
18311       case OPT_o:
18312       case OPT_d:
18313       case OPT_dumpbase:
18314       case OPT_dumpdir:
18315       case OPT_auxbase:
18316       case OPT_auxbase_strip:
18317       case OPT_quiet:
18318       case OPT_version:
18319       case OPT_v:
18320       case OPT_w:
18321       case OPT_L:
18322       case OPT_D:
18323       case OPT_I:
18324       case OPT_U:
18325       case OPT_SPECIAL_unknown:
18326       case OPT_SPECIAL_ignore:
18327       case OPT_SPECIAL_program_name:
18328       case OPT_SPECIAL_input_file:
18329       case OPT_grecord_gcc_switches:
18330       case OPT_gno_record_gcc_switches:
18331       case OPT__output_pch_:
18332       case OPT_fdiagnostics_show_location_:
18333       case OPT_fdiagnostics_show_option:
18334       case OPT_fverbose_asm:
18335       case OPT____:
18336       case OPT__sysroot_:
18337       case OPT_nostdinc:
18338       case OPT_nostdinc__:
18339         /* Ignore these.  */
18340         continue;
18341       default:
18342         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18343                              == '-');
18344         switch (save_decoded_options[j].canonical_option[0][1])
18345           {
18346           case 'M':
18347           case 'i':
18348           case 'W':
18349             continue;
18350           case 'f':
18351             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18352                          "dump", 4) == 0)
18353               continue;
18354             break;
18355           default:
18356             break;
18357           }
18358         VEC_safe_push (dchar_p, heap, switches,
18359                        save_decoded_options[j].orig_option_with_args_text);
18360         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18361         break;
18362       }
18363
18364   producer = XNEWVEC (char, plen + 1 + len + 1);
18365   tail = producer;
18366   sprintf (tail, "%s %s", language_string, version_string);
18367   tail += plen;
18368
18369   if (!dwarf_record_gcc_switches)
18370     {
18371 #ifdef MIPS_DEBUGGING_INFO
18372       /* The MIPS/SGI compilers place the 'cc' command line options in the
18373          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18374          if they do not appear in the producer string, the debugger reaches
18375          the conclusion that the object file is stripped and has no debugging
18376          information.  To get the MIPS/SGI debugger to believe that there is
18377          debugging information in the object file, we add a -g to the producer
18378          string.  */
18379       if (debug_info_level > DINFO_LEVEL_TERSE)
18380         {
18381           memcpy (tail, " -g", 3);
18382           tail += 3;
18383         }
18384 #endif
18385     }
18386
18387   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18388     {
18389       len = strlen (p);
18390       *tail = ' ';
18391       memcpy (tail + 1, p, len);
18392       tail += len + 1;
18393     }
18394
18395   *tail = '\0';
18396   VEC_free (dchar_p, heap, switches);
18397   return producer;
18398 }
18399
18400 /* Generate the DIE for the compilation unit.  */
18401
18402 static dw_die_ref
18403 gen_compile_unit_die (const char *filename)
18404 {
18405   dw_die_ref die;
18406   const char *language_string = lang_hooks.name;
18407   int language;
18408
18409   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18410
18411   if (filename)
18412     {
18413       add_name_attribute (die, filename);
18414       /* Don't add cwd for <built-in>.  */
18415       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18416         add_comp_dir_attribute (die);
18417     }
18418
18419   if (producer_string == NULL)
18420     producer_string = gen_producer_string ();
18421   add_AT_string (die, DW_AT_producer, producer_string);
18422
18423   /* If our producer is LTO try to figure out a common language to use
18424      from the global list of translation units.  */
18425   if (strcmp (language_string, "GNU GIMPLE") == 0)
18426     {
18427       unsigned i;
18428       tree t;
18429       const char *common_lang = NULL;
18430
18431       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18432         {
18433           if (!TRANSLATION_UNIT_LANGUAGE (t))
18434             continue;
18435           if (!common_lang)
18436             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18437           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18438             ;
18439           else if (strncmp (common_lang, "GNU C", 5) == 0
18440                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18441             /* Mixing C and C++ is ok, use C++ in that case.  */
18442             common_lang = "GNU C++";
18443           else
18444             {
18445               /* Fall back to C.  */
18446               common_lang = NULL;
18447               break;
18448             }
18449         }
18450
18451       if (common_lang)
18452         language_string = common_lang;
18453     }
18454
18455   language = DW_LANG_C89;
18456   if (strcmp (language_string, "GNU C++") == 0)
18457     language = DW_LANG_C_plus_plus;
18458   else if (strcmp (language_string, "GNU F77") == 0)
18459     language = DW_LANG_Fortran77;
18460   else if (strcmp (language_string, "GNU Pascal") == 0)
18461     language = DW_LANG_Pascal83;
18462   else if (dwarf_version >= 3 || !dwarf_strict)
18463     {
18464       if (strcmp (language_string, "GNU Ada") == 0)
18465         language = DW_LANG_Ada95;
18466       else if (strcmp (language_string, "GNU Fortran") == 0)
18467         language = DW_LANG_Fortran95;
18468       else if (strcmp (language_string, "GNU Java") == 0)
18469         language = DW_LANG_Java;
18470       else if (strcmp (language_string, "GNU Objective-C") == 0)
18471         language = DW_LANG_ObjC;
18472       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18473         language = DW_LANG_ObjC_plus_plus;
18474       else if (dwarf_version >= 5 || !dwarf_strict)
18475         {
18476           if (strcmp (language_string, "GNU Go") == 0)
18477             language = DW_LANG_Go;
18478         }
18479     }
18480
18481   add_AT_unsigned (die, DW_AT_language, language);
18482
18483   switch (language)
18484     {
18485     case DW_LANG_Fortran77:
18486     case DW_LANG_Fortran90:
18487     case DW_LANG_Fortran95:
18488       /* Fortran has case insensitive identifiers and the front-end
18489          lowercases everything.  */
18490       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18491       break;
18492     default:
18493       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18494       break;
18495     }
18496   return die;
18497 }
18498
18499 /* Generate the DIE for a base class.  */
18500
18501 static void
18502 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18503 {
18504   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18505
18506   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18507   add_data_member_location_attribute (die, binfo);
18508
18509   if (BINFO_VIRTUAL_P (binfo))
18510     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18511
18512   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18513      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18514      the default has always been DW_ACCESS_private.  */
18515   if (access == access_public_node)
18516     {
18517       if (dwarf_version == 2
18518           || context_die->die_tag == DW_TAG_class_type)
18519       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18520     }
18521   else if (access == access_protected_node)
18522     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18523   else if (dwarf_version > 2
18524            && context_die->die_tag != DW_TAG_class_type)
18525     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18526 }
18527
18528 /* Generate a DIE for a class member.  */
18529
18530 static void
18531 gen_member_die (tree type, dw_die_ref context_die)
18532 {
18533   tree member;
18534   tree binfo = TYPE_BINFO (type);
18535   dw_die_ref child;
18536
18537   /* If this is not an incomplete type, output descriptions of each of its
18538      members. Note that as we output the DIEs necessary to represent the
18539      members of this record or union type, we will also be trying to output
18540      DIEs to represent the *types* of those members. However the `type'
18541      function (above) will specifically avoid generating type DIEs for member
18542      types *within* the list of member DIEs for this (containing) type except
18543      for those types (of members) which are explicitly marked as also being
18544      members of this (containing) type themselves.  The g++ front- end can
18545      force any given type to be treated as a member of some other (containing)
18546      type by setting the TYPE_CONTEXT of the given (member) type to point to
18547      the TREE node representing the appropriate (containing) type.  */
18548
18549   /* First output info about the base classes.  */
18550   if (binfo)
18551     {
18552       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18553       int i;
18554       tree base;
18555
18556       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18557         gen_inheritance_die (base,
18558                              (accesses ? VEC_index (tree, accesses, i)
18559                               : access_public_node), context_die);
18560     }
18561
18562   /* Now output info about the data members and type members.  */
18563   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18564     {
18565       /* If we thought we were generating minimal debug info for TYPE
18566          and then changed our minds, some of the member declarations
18567          may have already been defined.  Don't define them again, but
18568          do put them in the right order.  */
18569
18570       child = lookup_decl_die (member);
18571       if (child)
18572         splice_child_die (context_die, child);
18573       else
18574         gen_decl_die (member, NULL, context_die);
18575     }
18576
18577   /* Now output info about the function members (if any).  */
18578   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18579     {
18580       /* Don't include clones in the member list.  */
18581       if (DECL_ABSTRACT_ORIGIN (member))
18582         continue;
18583
18584       child = lookup_decl_die (member);
18585       if (child)
18586         splice_child_die (context_die, child);
18587       else
18588         gen_decl_die (member, NULL, context_die);
18589     }
18590 }
18591
18592 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18593    is set, we pretend that the type was never defined, so we only get the
18594    member DIEs needed by later specification DIEs.  */
18595
18596 static void
18597 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18598                                 enum debug_info_usage usage)
18599 {
18600   dw_die_ref type_die = lookup_type_die (type);
18601   dw_die_ref scope_die = 0;
18602   int nested = 0;
18603   int complete = (TYPE_SIZE (type)
18604                   && (! TYPE_STUB_DECL (type)
18605                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18606   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18607   complete = complete && should_emit_struct_debug (type, usage);
18608
18609   if (type_die && ! complete)
18610     return;
18611
18612   if (TYPE_CONTEXT (type) != NULL_TREE
18613       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18614           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18615     nested = 1;
18616
18617   scope_die = scope_die_for (type, context_die);
18618
18619   if (! type_die || (nested && is_cu_die (scope_die)))
18620     /* First occurrence of type or toplevel definition of nested class.  */
18621     {
18622       dw_die_ref old_die = type_die;
18623
18624       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18625                           ? record_type_tag (type) : DW_TAG_union_type,
18626                           scope_die, type);
18627       equate_type_number_to_die (type, type_die);
18628       if (old_die)
18629         add_AT_specification (type_die, old_die);
18630       else
18631         {
18632           add_name_attribute (type_die, type_tag (type));
18633           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18634           if (TYPE_ARTIFICIAL (type))
18635             add_AT_flag (type_die, DW_AT_artificial, 1);
18636         }
18637     }
18638   else
18639     remove_AT (type_die, DW_AT_declaration);
18640
18641   /* Generate child dies for template paramaters.  */
18642   if (debug_info_level > DINFO_LEVEL_TERSE
18643       && COMPLETE_TYPE_P (type))
18644     schedule_generic_params_dies_gen (type);
18645
18646   /* If this type has been completed, then give it a byte_size attribute and
18647      then give a list of members.  */
18648   if (complete && !ns_decl)
18649     {
18650       /* Prevent infinite recursion in cases where the type of some member of
18651          this type is expressed in terms of this type itself.  */
18652       TREE_ASM_WRITTEN (type) = 1;
18653       add_byte_size_attribute (type_die, type);
18654       if (TYPE_STUB_DECL (type) != NULL_TREE)
18655         {
18656           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18657           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18658         }
18659
18660       /* If the first reference to this type was as the return type of an
18661          inline function, then it may not have a parent.  Fix this now.  */
18662       if (type_die->die_parent == NULL)
18663         add_child_die (scope_die, type_die);
18664
18665       push_decl_scope (type);
18666       gen_member_die (type, type_die);
18667       pop_decl_scope ();
18668
18669       /* GNU extension: Record what type our vtable lives in.  */
18670       if (TYPE_VFIELD (type))
18671         {
18672           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18673
18674           gen_type_die (vtype, context_die);
18675           add_AT_die_ref (type_die, DW_AT_containing_type,
18676                           lookup_type_die (vtype));
18677         }
18678     }
18679   else
18680     {
18681       add_AT_flag (type_die, DW_AT_declaration, 1);
18682
18683       /* We don't need to do this for function-local types.  */
18684       if (TYPE_STUB_DECL (type)
18685           && ! decl_function_context (TYPE_STUB_DECL (type)))
18686         VEC_safe_push (tree, gc, incomplete_types, type);
18687     }
18688
18689   if (get_AT (type_die, DW_AT_name))
18690     add_pubtype (type, type_die);
18691 }
18692
18693 /* Generate a DIE for a subroutine _type_.  */
18694
18695 static void
18696 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18697 {
18698   tree return_type = TREE_TYPE (type);
18699   dw_die_ref subr_die
18700     = new_die (DW_TAG_subroutine_type,
18701                scope_die_for (type, context_die), type);
18702
18703   equate_type_number_to_die (type, subr_die);
18704   add_prototyped_attribute (subr_die, type);
18705   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18706   gen_formal_types_die (type, subr_die);
18707
18708   if (get_AT (subr_die, DW_AT_name))
18709     add_pubtype (type, subr_die);
18710 }
18711
18712 /* Generate a DIE for a type definition.  */
18713
18714 static void
18715 gen_typedef_die (tree decl, dw_die_ref context_die)
18716 {
18717   dw_die_ref type_die;
18718   tree origin;
18719
18720   if (TREE_ASM_WRITTEN (decl))
18721     return;
18722
18723   TREE_ASM_WRITTEN (decl) = 1;
18724   type_die = new_die (DW_TAG_typedef, context_die, decl);
18725   origin = decl_ultimate_origin (decl);
18726   if (origin != NULL)
18727     add_abstract_origin_attribute (type_die, origin);
18728   else
18729     {
18730       tree type;
18731
18732       add_name_and_src_coords_attributes (type_die, decl);
18733       if (DECL_ORIGINAL_TYPE (decl))
18734         {
18735           type = DECL_ORIGINAL_TYPE (decl);
18736
18737           gcc_assert (type != TREE_TYPE (decl));
18738           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18739         }
18740       else
18741         {
18742           type = TREE_TYPE (decl);
18743
18744           if (is_naming_typedef_decl (TYPE_NAME (type)))
18745             {
18746               /* Here, we are in the case of decl being a typedef naming
18747                  an anonymous type, e.g:
18748                      typedef struct {...} foo;
18749                  In that case TREE_TYPE (decl) is not a typedef variant
18750                  type and TYPE_NAME of the anonymous type is set to the
18751                  TYPE_DECL of the typedef. This construct is emitted by
18752                  the C++ FE.
18753
18754                  TYPE is the anonymous struct named by the typedef
18755                  DECL. As we need the DW_AT_type attribute of the
18756                  DW_TAG_typedef to point to the DIE of TYPE, let's
18757                  generate that DIE right away. add_type_attribute
18758                  called below will then pick (via lookup_type_die) that
18759                  anonymous struct DIE.  */
18760               if (!TREE_ASM_WRITTEN (type))
18761                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18762
18763               /* This is a GNU Extension.  We are adding a
18764                  DW_AT_linkage_name attribute to the DIE of the
18765                  anonymous struct TYPE.  The value of that attribute
18766                  is the name of the typedef decl naming the anonymous
18767                  struct.  This greatly eases the work of consumers of
18768                  this debug info.  */
18769               add_linkage_attr (lookup_type_die (type), decl);
18770             }
18771         }
18772
18773       add_type_attribute (type_die, type, TREE_READONLY (decl),
18774                           TREE_THIS_VOLATILE (decl), context_die);
18775
18776       if (is_naming_typedef_decl (decl))
18777         /* We want that all subsequent calls to lookup_type_die with
18778            TYPE in argument yield the DW_TAG_typedef we have just
18779            created.  */
18780         equate_type_number_to_die (type, type_die);
18781
18782       add_accessibility_attribute (type_die, decl);
18783     }
18784
18785   if (DECL_ABSTRACT (decl))
18786     equate_decl_number_to_die (decl, type_die);
18787
18788   if (get_AT (type_die, DW_AT_name))
18789     add_pubtype (decl, type_die);
18790 }
18791
18792 /* Generate a DIE for a struct, class, enum or union type.  */
18793
18794 static void
18795 gen_tagged_type_die (tree type,
18796                      dw_die_ref context_die,
18797                      enum debug_info_usage usage)
18798 {
18799   int need_pop;
18800
18801   if (type == NULL_TREE
18802       || !is_tagged_type (type))
18803     return;
18804
18805   /* If this is a nested type whose containing class hasn't been written
18806      out yet, writing it out will cover this one, too.  This does not apply
18807      to instantiations of member class templates; they need to be added to
18808      the containing class as they are generated.  FIXME: This hurts the
18809      idea of combining type decls from multiple TUs, since we can't predict
18810      what set of template instantiations we'll get.  */
18811   if (TYPE_CONTEXT (type)
18812       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18813       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18814     {
18815       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18816
18817       if (TREE_ASM_WRITTEN (type))
18818         return;
18819
18820       /* If that failed, attach ourselves to the stub.  */
18821       push_decl_scope (TYPE_CONTEXT (type));
18822       context_die = lookup_type_die (TYPE_CONTEXT (type));
18823       need_pop = 1;
18824     }
18825   else if (TYPE_CONTEXT (type) != NULL_TREE
18826            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18827     {
18828       /* If this type is local to a function that hasn't been written
18829          out yet, use a NULL context for now; it will be fixed up in
18830          decls_for_scope.  */
18831       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18832       /* A declaration DIE doesn't count; nested types need to go in the
18833          specification.  */
18834       if (context_die && is_declaration_die (context_die))
18835         context_die = NULL;
18836       need_pop = 0;
18837     }
18838   else
18839     {
18840       context_die = declare_in_namespace (type, context_die);
18841       need_pop = 0;
18842     }
18843
18844   if (TREE_CODE (type) == ENUMERAL_TYPE)
18845     {
18846       /* This might have been written out by the call to
18847          declare_in_namespace.  */
18848       if (!TREE_ASM_WRITTEN (type))
18849         gen_enumeration_type_die (type, context_die);
18850     }
18851   else
18852     gen_struct_or_union_type_die (type, context_die, usage);
18853
18854   if (need_pop)
18855     pop_decl_scope ();
18856
18857   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18858      it up if it is ever completed.  gen_*_type_die will set it for us
18859      when appropriate.  */
18860 }
18861
18862 /* Generate a type description DIE.  */
18863
18864 static void
18865 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18866                          enum debug_info_usage usage)
18867 {
18868   struct array_descr_info info;
18869
18870   if (type == NULL_TREE || type == error_mark_node)
18871     return;
18872
18873   if (TYPE_NAME (type) != NULL_TREE
18874       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18875       && is_redundant_typedef (TYPE_NAME (type))
18876       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18877     /* The DECL of this type is a typedef we don't want to emit debug
18878        info for but we want debug info for its underlying typedef.
18879        This can happen for e.g, the injected-class-name of a C++
18880        type.  */
18881     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18882
18883   /* If TYPE is a typedef type variant, let's generate debug info
18884      for the parent typedef which TYPE is a type of.  */
18885   if (typedef_variant_p (type))
18886     {
18887       if (TREE_ASM_WRITTEN (type))
18888         return;
18889
18890       /* Prevent broken recursion; we can't hand off to the same type.  */
18891       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18892
18893       /* Use the DIE of the containing namespace as the parent DIE of
18894          the type description DIE we want to generate.  */
18895       if (DECL_FILE_SCOPE_P (TYPE_NAME (type))
18896           || (DECL_CONTEXT (TYPE_NAME (type))
18897               && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL))
18898         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18899
18900       TREE_ASM_WRITTEN (type) = 1;
18901
18902       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18903       return;
18904     }
18905
18906   /* If type is an anonymous tagged type named by a typedef, let's
18907      generate debug info for the typedef.  */
18908   if (is_naming_typedef_decl (TYPE_NAME (type)))
18909     {
18910       /* Use the DIE of the containing namespace as the parent DIE of
18911          the type description DIE we want to generate.  */
18912       if (DECL_CONTEXT (TYPE_NAME (type))
18913           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18914         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18915       
18916       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18917       return;
18918     }
18919
18920   /* If this is an array type with hidden descriptor, handle it first.  */
18921   if (!TREE_ASM_WRITTEN (type)
18922       && lang_hooks.types.get_array_descr_info
18923       && lang_hooks.types.get_array_descr_info (type, &info)
18924       && (dwarf_version >= 3 || !dwarf_strict))
18925     {
18926       gen_descr_array_type_die (type, &info, context_die);
18927       TREE_ASM_WRITTEN (type) = 1;
18928       return;
18929     }
18930
18931   /* We are going to output a DIE to represent the unqualified version
18932      of this type (i.e. without any const or volatile qualifiers) so
18933      get the main variant (i.e. the unqualified version) of this type
18934      now.  (Vectors are special because the debugging info is in the
18935      cloned type itself).  */
18936   if (TREE_CODE (type) != VECTOR_TYPE)
18937     type = type_main_variant (type);
18938
18939   if (TREE_ASM_WRITTEN (type))
18940     return;
18941
18942   switch (TREE_CODE (type))
18943     {
18944     case ERROR_MARK:
18945       break;
18946
18947     case POINTER_TYPE:
18948     case REFERENCE_TYPE:
18949       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18950          ensures that the gen_type_die recursion will terminate even if the
18951          type is recursive.  Recursive types are possible in Ada.  */
18952       /* ??? We could perhaps do this for all types before the switch
18953          statement.  */
18954       TREE_ASM_WRITTEN (type) = 1;
18955
18956       /* For these types, all that is required is that we output a DIE (or a
18957          set of DIEs) to represent the "basis" type.  */
18958       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18959                                 DINFO_USAGE_IND_USE);
18960       break;
18961
18962     case OFFSET_TYPE:
18963       /* This code is used for C++ pointer-to-data-member types.
18964          Output a description of the relevant class type.  */
18965       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18966                                         DINFO_USAGE_IND_USE);
18967
18968       /* Output a description of the type of the object pointed to.  */
18969       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18970                                         DINFO_USAGE_IND_USE);
18971
18972       /* Now output a DIE to represent this pointer-to-data-member type
18973          itself.  */
18974       gen_ptr_to_mbr_type_die (type, context_die);
18975       break;
18976
18977     case FUNCTION_TYPE:
18978       /* Force out return type (in case it wasn't forced out already).  */
18979       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18980                                         DINFO_USAGE_DIR_USE);
18981       gen_subroutine_type_die (type, context_die);
18982       break;
18983
18984     case METHOD_TYPE:
18985       /* Force out return type (in case it wasn't forced out already).  */
18986       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18987                                         DINFO_USAGE_DIR_USE);
18988       gen_subroutine_type_die (type, context_die);
18989       break;
18990
18991     case ARRAY_TYPE:
18992       gen_array_type_die (type, context_die);
18993       break;
18994
18995     case VECTOR_TYPE:
18996       gen_array_type_die (type, context_die);
18997       break;
18998
18999     case ENUMERAL_TYPE:
19000     case RECORD_TYPE:
19001     case UNION_TYPE:
19002     case QUAL_UNION_TYPE:
19003       gen_tagged_type_die (type, context_die, usage);
19004       return;
19005
19006     case VOID_TYPE:
19007     case INTEGER_TYPE:
19008     case REAL_TYPE:
19009     case FIXED_POINT_TYPE:
19010     case COMPLEX_TYPE:
19011     case BOOLEAN_TYPE:
19012       /* No DIEs needed for fundamental types.  */
19013       break;
19014
19015     case NULLPTR_TYPE:
19016     case LANG_TYPE:
19017       /* Just use DW_TAG_unspecified_type.  */
19018       {
19019         dw_die_ref type_die = lookup_type_die (type);
19020         if (type_die == NULL)
19021           {
19022             tree name = TYPE_NAME (type);
19023             if (TREE_CODE (name) == TYPE_DECL)
19024               name = DECL_NAME (name);
19025             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19026             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19027             equate_type_number_to_die (type, type_die);
19028           }
19029       }
19030       break;
19031
19032     default:
19033       gcc_unreachable ();
19034     }
19035
19036   TREE_ASM_WRITTEN (type) = 1;
19037 }
19038
19039 static void
19040 gen_type_die (tree type, dw_die_ref context_die)
19041 {
19042   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19043 }
19044
19045 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19046    things which are local to the given block.  */
19047
19048 static void
19049 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19050 {
19051   int must_output_die = 0;
19052   bool inlined_func;
19053
19054   /* Ignore blocks that are NULL.  */
19055   if (stmt == NULL_TREE)
19056     return;
19057
19058   inlined_func = inlined_function_outer_scope_p (stmt);
19059
19060   /* If the block is one fragment of a non-contiguous block, do not
19061      process the variables, since they will have been done by the
19062      origin block.  Do process subblocks.  */
19063   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19064     {
19065       tree sub;
19066
19067       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19068         gen_block_die (sub, context_die, depth + 1);
19069
19070       return;
19071     }
19072
19073   /* Determine if we need to output any Dwarf DIEs at all to represent this
19074      block.  */
19075   if (inlined_func)
19076     /* The outer scopes for inlinings *must* always be represented.  We
19077        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19078     must_output_die = 1;
19079   else
19080     {
19081       /* Determine if this block directly contains any "significant"
19082          local declarations which we will need to output DIEs for.  */
19083       if (debug_info_level > DINFO_LEVEL_TERSE)
19084         /* We are not in terse mode so *any* local declaration counts
19085            as being a "significant" one.  */
19086         must_output_die = ((BLOCK_VARS (stmt) != NULL
19087                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19088                            && (TREE_USED (stmt)
19089                                || TREE_ASM_WRITTEN (stmt)
19090                                || BLOCK_ABSTRACT (stmt)));
19091       else if ((TREE_USED (stmt)
19092                 || TREE_ASM_WRITTEN (stmt)
19093                 || BLOCK_ABSTRACT (stmt))
19094                && !dwarf2out_ignore_block (stmt))
19095         must_output_die = 1;
19096     }
19097
19098   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19099      DIE for any block which contains no significant local declarations at
19100      all.  Rather, in such cases we just call `decls_for_scope' so that any
19101      needed Dwarf info for any sub-blocks will get properly generated. Note
19102      that in terse mode, our definition of what constitutes a "significant"
19103      local declaration gets restricted to include only inlined function
19104      instances and local (nested) function definitions.  */
19105   if (must_output_die)
19106     {
19107       if (inlined_func)
19108         {
19109           /* If STMT block is abstract, that means we have been called
19110              indirectly from dwarf2out_abstract_function.
19111              That function rightfully marks the descendent blocks (of
19112              the abstract function it is dealing with) as being abstract,
19113              precisely to prevent us from emitting any
19114              DW_TAG_inlined_subroutine DIE as a descendent
19115              of an abstract function instance. So in that case, we should
19116              not call gen_inlined_subroutine_die.
19117
19118              Later though, when cgraph asks dwarf2out to emit info
19119              for the concrete instance of the function decl into which
19120              the concrete instance of STMT got inlined, the later will lead
19121              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19122           if (! BLOCK_ABSTRACT (stmt))
19123             gen_inlined_subroutine_die (stmt, context_die, depth);
19124         }
19125       else
19126         gen_lexical_block_die (stmt, context_die, depth);
19127     }
19128   else
19129     decls_for_scope (stmt, context_die, depth);
19130 }
19131
19132 /* Process variable DECL (or variable with origin ORIGIN) within
19133    block STMT and add it to CONTEXT_DIE.  */
19134 static void
19135 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19136 {
19137   dw_die_ref die;
19138   tree decl_or_origin = decl ? decl : origin;
19139
19140   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19141     die = lookup_decl_die (decl_or_origin);
19142   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19143            && TYPE_DECL_IS_STUB (decl_or_origin))
19144     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19145   else
19146     die = NULL;
19147
19148   if (die != NULL && die->die_parent == NULL)
19149     add_child_die (context_die, die);
19150   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19151     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19152                                          stmt, context_die);
19153   else
19154     gen_decl_die (decl, origin, context_die);
19155 }
19156
19157 /* Generate all of the decls declared within a given scope and (recursively)
19158    all of its sub-blocks.  */
19159
19160 static void
19161 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19162 {
19163   tree decl;
19164   unsigned int i;
19165   tree subblocks;
19166
19167   /* Ignore NULL blocks.  */
19168   if (stmt == NULL_TREE)
19169     return;
19170
19171   /* Output the DIEs to represent all of the data objects and typedefs
19172      declared directly within this block but not within any nested
19173      sub-blocks.  Also, nested function and tag DIEs have been
19174      generated with a parent of NULL; fix that up now.  */
19175   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19176     process_scope_var (stmt, decl, NULL_TREE, context_die);
19177   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19178     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19179                        context_die);
19180
19181   /* If we're at -g1, we're not interested in subblocks.  */
19182   if (debug_info_level <= DINFO_LEVEL_TERSE)
19183     return;
19184
19185   /* Output the DIEs to represent all sub-blocks (and the items declared
19186      therein) of this block.  */
19187   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19188        subblocks != NULL;
19189        subblocks = BLOCK_CHAIN (subblocks))
19190     gen_block_die (subblocks, context_die, depth + 1);
19191 }
19192
19193 /* Is this a typedef we can avoid emitting?  */
19194
19195 static inline int
19196 is_redundant_typedef (const_tree decl)
19197 {
19198   if (TYPE_DECL_IS_STUB (decl))
19199     return 1;
19200
19201   if (DECL_ARTIFICIAL (decl)
19202       && DECL_CONTEXT (decl)
19203       && is_tagged_type (DECL_CONTEXT (decl))
19204       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19205       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19206     /* Also ignore the artificial member typedef for the class name.  */
19207     return 1;
19208
19209   return 0;
19210 }
19211
19212 /* Return TRUE if TYPE is a typedef that names a type for linkage
19213    purposes. This kind of typedefs is produced by the C++ FE for
19214    constructs like:
19215
19216    typedef struct {...} foo;
19217
19218    In that case, there is no typedef variant type produced for foo.
19219    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19220    struct type.  */
19221
19222 static bool
19223 is_naming_typedef_decl (const_tree decl)
19224 {
19225   if (decl == NULL_TREE
19226       || TREE_CODE (decl) != TYPE_DECL
19227       || !is_tagged_type (TREE_TYPE (decl))
19228       || DECL_IS_BUILTIN (decl)
19229       || is_redundant_typedef (decl)
19230       /* It looks like Ada produces TYPE_DECLs that are very similar
19231          to C++ naming typedefs but that have different
19232          semantics. Let's be specific to c++ for now.  */
19233       || !is_cxx ())
19234     return FALSE;
19235
19236   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19237           && TYPE_NAME (TREE_TYPE (decl)) == decl
19238           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19239               != TYPE_NAME (TREE_TYPE (decl))));
19240 }
19241
19242 /* Returns the DIE for a context.  */
19243
19244 static inline dw_die_ref
19245 get_context_die (tree context)
19246 {
19247   if (context)
19248     {
19249       /* Find die that represents this context.  */
19250       if (TYPE_P (context))
19251         {
19252           context = TYPE_MAIN_VARIANT (context);
19253           return strip_naming_typedef (context, force_type_die (context));
19254         }
19255       else
19256         return force_decl_die (context);
19257     }
19258   return comp_unit_die ();
19259 }
19260
19261 /* Returns the DIE for decl.  A DIE will always be returned.  */
19262
19263 static dw_die_ref
19264 force_decl_die (tree decl)
19265 {
19266   dw_die_ref decl_die;
19267   unsigned saved_external_flag;
19268   tree save_fn = NULL_TREE;
19269   decl_die = lookup_decl_die (decl);
19270   if (!decl_die)
19271     {
19272       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19273
19274       decl_die = lookup_decl_die (decl);
19275       if (decl_die)
19276         return decl_die;
19277
19278       switch (TREE_CODE (decl))
19279         {
19280         case FUNCTION_DECL:
19281           /* Clear current_function_decl, so that gen_subprogram_die thinks
19282              that this is a declaration. At this point, we just want to force
19283              declaration die.  */
19284           save_fn = current_function_decl;
19285           current_function_decl = NULL_TREE;
19286           gen_subprogram_die (decl, context_die);
19287           current_function_decl = save_fn;
19288           break;
19289
19290         case VAR_DECL:
19291           /* Set external flag to force declaration die. Restore it after
19292            gen_decl_die() call.  */
19293           saved_external_flag = DECL_EXTERNAL (decl);
19294           DECL_EXTERNAL (decl) = 1;
19295           gen_decl_die (decl, NULL, context_die);
19296           DECL_EXTERNAL (decl) = saved_external_flag;
19297           break;
19298
19299         case NAMESPACE_DECL:
19300           if (dwarf_version >= 3 || !dwarf_strict)
19301             dwarf2out_decl (decl);
19302           else
19303             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19304             decl_die = comp_unit_die ();
19305           break;
19306
19307         case TRANSLATION_UNIT_DECL:
19308           decl_die = comp_unit_die ();
19309           break;
19310
19311         default:
19312           gcc_unreachable ();
19313         }
19314
19315       /* We should be able to find the DIE now.  */
19316       if (!decl_die)
19317         decl_die = lookup_decl_die (decl);
19318       gcc_assert (decl_die);
19319     }
19320
19321   return decl_die;
19322 }
19323
19324 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19325    always returned.  */
19326
19327 static dw_die_ref
19328 force_type_die (tree type)
19329 {
19330   dw_die_ref type_die;
19331
19332   type_die = lookup_type_die (type);
19333   if (!type_die)
19334     {
19335       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19336
19337       type_die = modified_type_die (type, TYPE_READONLY (type),
19338                                     TYPE_VOLATILE (type), context_die);
19339       gcc_assert (type_die);
19340     }
19341   return type_die;
19342 }
19343
19344 /* Force out any required namespaces to be able to output DECL,
19345    and return the new context_die for it, if it's changed.  */
19346
19347 static dw_die_ref
19348 setup_namespace_context (tree thing, dw_die_ref context_die)
19349 {
19350   tree context = (DECL_P (thing)
19351                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19352   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19353     /* Force out the namespace.  */
19354     context_die = force_decl_die (context);
19355
19356   return context_die;
19357 }
19358
19359 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19360    type) within its namespace, if appropriate.
19361
19362    For compatibility with older debuggers, namespace DIEs only contain
19363    declarations; all definitions are emitted at CU scope.  */
19364
19365 static dw_die_ref
19366 declare_in_namespace (tree thing, dw_die_ref context_die)
19367 {
19368   dw_die_ref ns_context;
19369
19370   if (debug_info_level <= DINFO_LEVEL_TERSE)
19371     return context_die;
19372
19373   /* If this decl is from an inlined function, then don't try to emit it in its
19374      namespace, as we will get confused.  It would have already been emitted
19375      when the abstract instance of the inline function was emitted anyways.  */
19376   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19377     return context_die;
19378
19379   ns_context = setup_namespace_context (thing, context_die);
19380
19381   if (ns_context != context_die)
19382     {
19383       if (is_fortran ())
19384         return ns_context;
19385       if (DECL_P (thing))
19386         gen_decl_die (thing, NULL, ns_context);
19387       else
19388         gen_type_die (thing, ns_context);
19389     }
19390   return context_die;
19391 }
19392
19393 /* Generate a DIE for a namespace or namespace alias.  */
19394
19395 static void
19396 gen_namespace_die (tree decl, dw_die_ref context_die)
19397 {
19398   dw_die_ref namespace_die;
19399
19400   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19401      they are an alias of.  */
19402   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19403     {
19404       /* Output a real namespace or module.  */
19405       context_die = setup_namespace_context (decl, comp_unit_die ());
19406       namespace_die = new_die (is_fortran ()
19407                                ? DW_TAG_module : DW_TAG_namespace,
19408                                context_die, decl);
19409       /* For Fortran modules defined in different CU don't add src coords.  */
19410       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19411         {
19412           const char *name = dwarf2_name (decl, 0);
19413           if (name)
19414             add_name_attribute (namespace_die, name);
19415         }
19416       else
19417         add_name_and_src_coords_attributes (namespace_die, decl);
19418       if (DECL_EXTERNAL (decl))
19419         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19420       equate_decl_number_to_die (decl, namespace_die);
19421     }
19422   else
19423     {
19424       /* Output a namespace alias.  */
19425
19426       /* Force out the namespace we are an alias of, if necessary.  */
19427       dw_die_ref origin_die
19428         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19429
19430       if (DECL_FILE_SCOPE_P (decl)
19431           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19432         context_die = setup_namespace_context (decl, comp_unit_die ());
19433       /* Now create the namespace alias DIE.  */
19434       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19435       add_name_and_src_coords_attributes (namespace_die, decl);
19436       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19437       equate_decl_number_to_die (decl, namespace_die);
19438     }
19439 }
19440
19441 /* Generate Dwarf debug information for a decl described by DECL.
19442    The return value is currently only meaningful for PARM_DECLs,
19443    for all other decls it returns NULL.  */
19444
19445 static dw_die_ref
19446 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19447 {
19448   tree decl_or_origin = decl ? decl : origin;
19449   tree class_origin = NULL, ultimate_origin;
19450
19451   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19452     return NULL;
19453
19454   switch (TREE_CODE (decl_or_origin))
19455     {
19456     case ERROR_MARK:
19457       break;
19458
19459     case CONST_DECL:
19460       if (!is_fortran () && !is_ada ())
19461         {
19462           /* The individual enumerators of an enum type get output when we output
19463              the Dwarf representation of the relevant enum type itself.  */
19464           break;
19465         }
19466
19467       /* Emit its type.  */
19468       gen_type_die (TREE_TYPE (decl), context_die);
19469
19470       /* And its containing namespace.  */
19471       context_die = declare_in_namespace (decl, context_die);
19472
19473       gen_const_die (decl, context_die);
19474       break;
19475
19476     case FUNCTION_DECL:
19477       /* Don't output any DIEs to represent mere function declarations,
19478          unless they are class members or explicit block externs.  */
19479       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19480           && DECL_FILE_SCOPE_P (decl_or_origin)
19481           && (current_function_decl == NULL_TREE
19482               || DECL_ARTIFICIAL (decl_or_origin)))
19483         break;
19484
19485 #if 0
19486       /* FIXME */
19487       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19488          on local redeclarations of global functions.  That seems broken.  */
19489       if (current_function_decl != decl)
19490         /* This is only a declaration.  */;
19491 #endif
19492
19493       /* If we're emitting a clone, emit info for the abstract instance.  */
19494       if (origin || DECL_ORIGIN (decl) != decl)
19495         dwarf2out_abstract_function (origin
19496                                      ? DECL_ORIGIN (origin)
19497                                      : DECL_ABSTRACT_ORIGIN (decl));
19498
19499       /* If we're emitting an out-of-line copy of an inline function,
19500          emit info for the abstract instance and set up to refer to it.  */
19501       else if (cgraph_function_possibly_inlined_p (decl)
19502                && ! DECL_ABSTRACT (decl)
19503                && ! class_or_namespace_scope_p (context_die)
19504                /* dwarf2out_abstract_function won't emit a die if this is just
19505                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19506                   that case, because that works only if we have a die.  */
19507                && DECL_INITIAL (decl) != NULL_TREE)
19508         {
19509           dwarf2out_abstract_function (decl);
19510           set_decl_origin_self (decl);
19511         }
19512
19513       /* Otherwise we're emitting the primary DIE for this decl.  */
19514       else if (debug_info_level > DINFO_LEVEL_TERSE)
19515         {
19516           /* Before we describe the FUNCTION_DECL itself, make sure that we
19517              have its containing type.  */
19518           if (!origin)
19519             origin = decl_class_context (decl);
19520           if (origin != NULL_TREE)
19521             gen_type_die (origin, context_die);
19522
19523           /* And its return type.  */
19524           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19525
19526           /* And its virtual context.  */
19527           if (DECL_VINDEX (decl) != NULL_TREE)
19528             gen_type_die (DECL_CONTEXT (decl), context_die);
19529
19530           /* Make sure we have a member DIE for decl.  */
19531           if (origin != NULL_TREE)
19532             gen_type_die_for_member (origin, decl, context_die);
19533
19534           /* And its containing namespace.  */
19535           context_die = declare_in_namespace (decl, context_die);
19536         }
19537
19538       /* Now output a DIE to represent the function itself.  */
19539       if (decl)
19540         gen_subprogram_die (decl, context_die);
19541       break;
19542
19543     case TYPE_DECL:
19544       /* If we are in terse mode, don't generate any DIEs to represent any
19545          actual typedefs.  */
19546       if (debug_info_level <= DINFO_LEVEL_TERSE)
19547         break;
19548
19549       /* In the special case of a TYPE_DECL node representing the declaration
19550          of some type tag, if the given TYPE_DECL is marked as having been
19551          instantiated from some other (original) TYPE_DECL node (e.g. one which
19552          was generated within the original definition of an inline function) we
19553          used to generate a special (abbreviated) DW_TAG_structure_type,
19554          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19555          should be actually referencing those DIEs, as variable DIEs with that
19556          type would be emitted already in the abstract origin, so it was always
19557          removed during unused type prunning.  Don't add anything in this
19558          case.  */
19559       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19560         break;
19561
19562       if (is_redundant_typedef (decl))
19563         gen_type_die (TREE_TYPE (decl), context_die);
19564       else
19565         /* Output a DIE to represent the typedef itself.  */
19566         gen_typedef_die (decl, context_die);
19567       break;
19568
19569     case LABEL_DECL:
19570       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19571         gen_label_die (decl, context_die);
19572       break;
19573
19574     case VAR_DECL:
19575     case RESULT_DECL:
19576       /* If we are in terse mode, don't generate any DIEs to represent any
19577          variable declarations or definitions.  */
19578       if (debug_info_level <= DINFO_LEVEL_TERSE)
19579         break;
19580
19581       /* Output any DIEs that are needed to specify the type of this data
19582          object.  */
19583       if (decl_by_reference_p (decl_or_origin))
19584         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19585       else
19586         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19587
19588       /* And its containing type.  */
19589       class_origin = decl_class_context (decl_or_origin);
19590       if (class_origin != NULL_TREE)
19591         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19592
19593       /* And its containing namespace.  */
19594       context_die = declare_in_namespace (decl_or_origin, context_die);
19595
19596       /* Now output the DIE to represent the data object itself.  This gets
19597          complicated because of the possibility that the VAR_DECL really
19598          represents an inlined instance of a formal parameter for an inline
19599          function.  */
19600       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19601       if (ultimate_origin != NULL_TREE
19602           && TREE_CODE (ultimate_origin) == PARM_DECL)
19603         gen_formal_parameter_die (decl, origin,
19604                                   true /* Emit name attribute.  */,
19605                                   context_die);
19606       else
19607         gen_variable_die (decl, origin, context_die);
19608       break;
19609
19610     case FIELD_DECL:
19611       /* Ignore the nameless fields that are used to skip bits but handle C++
19612          anonymous unions and structs.  */
19613       if (DECL_NAME (decl) != NULL_TREE
19614           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19615           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19616         {
19617           gen_type_die (member_declared_type (decl), context_die);
19618           gen_field_die (decl, context_die);
19619         }
19620       break;
19621
19622     case PARM_DECL:
19623       if (DECL_BY_REFERENCE (decl_or_origin))
19624         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19625       else
19626         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19627       return gen_formal_parameter_die (decl, origin,
19628                                        true /* Emit name attribute.  */,
19629                                        context_die);
19630
19631     case NAMESPACE_DECL:
19632     case IMPORTED_DECL:
19633       if (dwarf_version >= 3 || !dwarf_strict)
19634         gen_namespace_die (decl, context_die);
19635       break;
19636
19637     default:
19638       /* Probably some frontend-internal decl.  Assume we don't care.  */
19639       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19640       break;
19641     }
19642
19643   return NULL;
19644 }
19645 \f
19646 /* Output debug information for global decl DECL.  Called from toplev.c after
19647    compilation proper has finished.  */
19648
19649 static void
19650 dwarf2out_global_decl (tree decl)
19651 {
19652   /* Output DWARF2 information for file-scope tentative data object
19653      declarations, file-scope (extern) function declarations (which
19654      had no corresponding body) and file-scope tagged type declarations
19655      and definitions which have not yet been forced out.  */
19656   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19657     dwarf2out_decl (decl);
19658 }
19659
19660 /* Output debug information for type decl DECL.  Called from toplev.c
19661    and from language front ends (to record built-in types).  */
19662 static void
19663 dwarf2out_type_decl (tree decl, int local)
19664 {
19665   if (!local)
19666     dwarf2out_decl (decl);
19667 }
19668
19669 /* Output debug information for imported module or decl DECL.
19670    NAME is non-NULL name in the lexical block if the decl has been renamed.
19671    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19672    that DECL belongs to.
19673    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19674 static void
19675 dwarf2out_imported_module_or_decl_1 (tree decl,
19676                                      tree name,
19677                                      tree lexical_block,
19678                                      dw_die_ref lexical_block_die)
19679 {
19680   expanded_location xloc;
19681   dw_die_ref imported_die = NULL;
19682   dw_die_ref at_import_die;
19683
19684   if (TREE_CODE (decl) == IMPORTED_DECL)
19685     {
19686       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19687       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19688       gcc_assert (decl);
19689     }
19690   else
19691     xloc = expand_location (input_location);
19692
19693   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19694     {
19695       at_import_die = force_type_die (TREE_TYPE (decl));
19696       /* For namespace N { typedef void T; } using N::T; base_type_die
19697          returns NULL, but DW_TAG_imported_declaration requires
19698          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19699       if (!at_import_die)
19700         {
19701           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19702           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19703           at_import_die = lookup_type_die (TREE_TYPE (decl));
19704           gcc_assert (at_import_die);
19705         }
19706     }
19707   else
19708     {
19709       at_import_die = lookup_decl_die (decl);
19710       if (!at_import_die)
19711         {
19712           /* If we're trying to avoid duplicate debug info, we may not have
19713              emitted the member decl for this field.  Emit it now.  */
19714           if (TREE_CODE (decl) == FIELD_DECL)
19715             {
19716               tree type = DECL_CONTEXT (decl);
19717
19718               if (TYPE_CONTEXT (type)
19719                   && TYPE_P (TYPE_CONTEXT (type))
19720                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19721                                                 DINFO_USAGE_DIR_USE))
19722                 return;
19723               gen_type_die_for_member (type, decl,
19724                                        get_context_die (TYPE_CONTEXT (type)));
19725             }
19726           at_import_die = force_decl_die (decl);
19727         }
19728     }
19729
19730   if (TREE_CODE (decl) == NAMESPACE_DECL)
19731     {
19732       if (dwarf_version >= 3 || !dwarf_strict)
19733         imported_die = new_die (DW_TAG_imported_module,
19734                                 lexical_block_die,
19735                                 lexical_block);
19736       else
19737         return;
19738     }
19739   else
19740     imported_die = new_die (DW_TAG_imported_declaration,
19741                             lexical_block_die,
19742                             lexical_block);
19743
19744   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19745   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19746   if (name)
19747     add_AT_string (imported_die, DW_AT_name,
19748                    IDENTIFIER_POINTER (name));
19749   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19750 }
19751
19752 /* Output debug information for imported module or decl DECL.
19753    NAME is non-NULL name in context if the decl has been renamed.
19754    CHILD is true if decl is one of the renamed decls as part of
19755    importing whole module.  */
19756
19757 static void
19758 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19759                                    bool child)
19760 {
19761   /* dw_die_ref at_import_die;  */
19762   dw_die_ref scope_die;
19763
19764   if (debug_info_level <= DINFO_LEVEL_TERSE)
19765     return;
19766
19767   gcc_assert (decl);
19768
19769   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19770      We need decl DIE for reference and scope die. First, get DIE for the decl
19771      itself.  */
19772
19773   /* Get the scope die for decl context. Use comp_unit_die for global module
19774      or decl. If die is not found for non globals, force new die.  */
19775   if (context
19776       && TYPE_P (context)
19777       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19778     return;
19779
19780   if (!(dwarf_version >= 3 || !dwarf_strict))
19781     return;
19782
19783   scope_die = get_context_die (context);
19784
19785   if (child)
19786     {
19787       gcc_assert (scope_die->die_child);
19788       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19789       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19790       scope_die = scope_die->die_child;
19791     }
19792
19793   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19794   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19795
19796 }
19797
19798 /* Write the debugging output for DECL.  */
19799
19800 void
19801 dwarf2out_decl (tree decl)
19802 {
19803   dw_die_ref context_die = comp_unit_die ();
19804
19805   switch (TREE_CODE (decl))
19806     {
19807     case ERROR_MARK:
19808       return;
19809
19810     case FUNCTION_DECL:
19811       /* What we would really like to do here is to filter out all mere
19812          file-scope declarations of file-scope functions which are never
19813          referenced later within this translation unit (and keep all of ones
19814          that *are* referenced later on) but we aren't clairvoyant, so we have
19815          no idea which functions will be referenced in the future (i.e. later
19816          on within the current translation unit). So here we just ignore all
19817          file-scope function declarations which are not also definitions.  If
19818          and when the debugger needs to know something about these functions,
19819          it will have to hunt around and find the DWARF information associated
19820          with the definition of the function.
19821
19822          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19823          nodes represent definitions and which ones represent mere
19824          declarations.  We have to check DECL_INITIAL instead. That's because
19825          the C front-end supports some weird semantics for "extern inline"
19826          function definitions.  These can get inlined within the current
19827          translation unit (and thus, we need to generate Dwarf info for their
19828          abstract instances so that the Dwarf info for the concrete inlined
19829          instances can have something to refer to) but the compiler never
19830          generates any out-of-lines instances of such things (despite the fact
19831          that they *are* definitions).
19832
19833          The important point is that the C front-end marks these "extern
19834          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19835          them anyway. Note that the C++ front-end also plays some similar games
19836          for inline function definitions appearing within include files which
19837          also contain `#pragma interface' pragmas.  */
19838       if (DECL_INITIAL (decl) == NULL_TREE)
19839         return;
19840
19841       /* If we're a nested function, initially use a parent of NULL; if we're
19842          a plain function, this will be fixed up in decls_for_scope.  If
19843          we're a method, it will be ignored, since we already have a DIE.  */
19844       if (decl_function_context (decl)
19845           /* But if we're in terse mode, we don't care about scope.  */
19846           && debug_info_level > DINFO_LEVEL_TERSE)
19847         context_die = NULL;
19848       break;
19849
19850     case VAR_DECL:
19851       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19852          declaration and if the declaration was never even referenced from
19853          within this entire compilation unit.  We suppress these DIEs in
19854          order to save space in the .debug section (by eliminating entries
19855          which are probably useless).  Note that we must not suppress
19856          block-local extern declarations (whether used or not) because that
19857          would screw-up the debugger's name lookup mechanism and cause it to
19858          miss things which really ought to be in scope at a given point.  */
19859       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19860         return;
19861
19862       /* For local statics lookup proper context die.  */
19863       if (TREE_STATIC (decl) && decl_function_context (decl))
19864         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19865
19866       /* If we are in terse mode, don't generate any DIEs to represent any
19867          variable declarations or definitions.  */
19868       if (debug_info_level <= DINFO_LEVEL_TERSE)
19869         return;
19870       break;
19871
19872     case CONST_DECL:
19873       if (debug_info_level <= DINFO_LEVEL_TERSE)
19874         return;
19875       if (!is_fortran () && !is_ada ())
19876         return;
19877       if (TREE_STATIC (decl) && decl_function_context (decl))
19878         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19879       break;
19880
19881     case NAMESPACE_DECL:
19882     case IMPORTED_DECL:
19883       if (debug_info_level <= DINFO_LEVEL_TERSE)
19884         return;
19885       if (lookup_decl_die (decl) != NULL)
19886         return;
19887       break;
19888
19889     case TYPE_DECL:
19890       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19891       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19892         return;
19893
19894       /* Don't bother trying to generate any DIEs to represent any of the
19895          normal built-in types for the language we are compiling.  */
19896       if (DECL_IS_BUILTIN (decl))
19897         return;
19898
19899       /* If we are in terse mode, don't generate any DIEs for types.  */
19900       if (debug_info_level <= DINFO_LEVEL_TERSE)
19901         return;
19902
19903       /* If we're a function-scope tag, initially use a parent of NULL;
19904          this will be fixed up in decls_for_scope.  */
19905       if (decl_function_context (decl))
19906         context_die = NULL;
19907
19908       break;
19909
19910     default:
19911       return;
19912     }
19913
19914   gen_decl_die (decl, NULL, context_die);
19915 }
19916
19917 /* Write the debugging output for DECL.  */
19918
19919 static void
19920 dwarf2out_function_decl (tree decl)
19921 {
19922   dwarf2out_decl (decl);
19923   call_arg_locations = NULL;
19924   call_arg_loc_last = NULL;
19925   call_site_count = -1;
19926   tail_call_site_count = -1;
19927   VEC_free (dw_die_ref, heap, block_map);
19928   htab_empty (decl_loc_table);
19929   htab_empty (cached_dw_loc_list_table);
19930 }
19931
19932 /* Output a marker (i.e. a label) for the beginning of the generated code for
19933    a lexical block.  */
19934
19935 static void
19936 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19937                        unsigned int blocknum)
19938 {
19939   switch_to_section (current_function_section ());
19940   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19941 }
19942
19943 /* Output a marker (i.e. a label) for the end of the generated code for a
19944    lexical block.  */
19945
19946 static void
19947 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19948 {
19949   switch_to_section (current_function_section ());
19950   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19951 }
19952
19953 /* Returns nonzero if it is appropriate not to emit any debugging
19954    information for BLOCK, because it doesn't contain any instructions.
19955
19956    Don't allow this for blocks with nested functions or local classes
19957    as we would end up with orphans, and in the presence of scheduling
19958    we may end up calling them anyway.  */
19959
19960 static bool
19961 dwarf2out_ignore_block (const_tree block)
19962 {
19963   tree decl;
19964   unsigned int i;
19965
19966   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19967     if (TREE_CODE (decl) == FUNCTION_DECL
19968         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19969       return 0;
19970   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19971     {
19972       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19973       if (TREE_CODE (decl) == FUNCTION_DECL
19974           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19975       return 0;
19976     }
19977
19978   return 1;
19979 }
19980
19981 /* Hash table routines for file_hash.  */
19982
19983 static int
19984 file_table_eq (const void *p1_p, const void *p2_p)
19985 {
19986   const struct dwarf_file_data *const p1 =
19987     (const struct dwarf_file_data *) p1_p;
19988   const char *const p2 = (const char *) p2_p;
19989   return filename_cmp (p1->filename, p2) == 0;
19990 }
19991
19992 static hashval_t
19993 file_table_hash (const void *p_p)
19994 {
19995   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19996   return htab_hash_string (p->filename);
19997 }
19998
19999 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20000    dwarf2out.c) and return its "index".  The index of each (known) filename is
20001    just a unique number which is associated with only that one filename.  We
20002    need such numbers for the sake of generating labels (in the .debug_sfnames
20003    section) and references to those files numbers (in the .debug_srcinfo
20004    and.debug_macinfo sections).  If the filename given as an argument is not
20005    found in our current list, add it to the list and assign it the next
20006    available unique index number.  In order to speed up searches, we remember
20007    the index of the filename was looked up last.  This handles the majority of
20008    all searches.  */
20009
20010 static struct dwarf_file_data *
20011 lookup_filename (const char *file_name)
20012 {
20013   void ** slot;
20014   struct dwarf_file_data * created;
20015
20016   /* Check to see if the file name that was searched on the previous
20017      call matches this file name.  If so, return the index.  */
20018   if (file_table_last_lookup
20019       && (file_name == file_table_last_lookup->filename
20020           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20021     return file_table_last_lookup;
20022
20023   /* Didn't match the previous lookup, search the table.  */
20024   slot = htab_find_slot_with_hash (file_table, file_name,
20025                                    htab_hash_string (file_name), INSERT);
20026   if (*slot)
20027     return (struct dwarf_file_data *) *slot;
20028
20029   created = ggc_alloc_dwarf_file_data ();
20030   created->filename = file_name;
20031   created->emitted_number = 0;
20032   *slot = created;
20033   return created;
20034 }
20035
20036 /* If the assembler will construct the file table, then translate the compiler
20037    internal file table number into the assembler file table number, and emit
20038    a .file directive if we haven't already emitted one yet.  The file table
20039    numbers are different because we prune debug info for unused variables and
20040    types, which may include filenames.  */
20041
20042 static int
20043 maybe_emit_file (struct dwarf_file_data * fd)
20044 {
20045   if (! fd->emitted_number)
20046     {
20047       if (last_emitted_file)
20048         fd->emitted_number = last_emitted_file->emitted_number + 1;
20049       else
20050         fd->emitted_number = 1;
20051       last_emitted_file = fd;
20052
20053       if (DWARF2_ASM_LINE_DEBUG_INFO)
20054         {
20055           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20056           output_quoted_string (asm_out_file,
20057                                 remap_debug_filename (fd->filename));
20058           fputc ('\n', asm_out_file);
20059         }
20060     }
20061
20062   return fd->emitted_number;
20063 }
20064
20065 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20066    That generation should happen after function debug info has been
20067    generated. The value of the attribute is the constant value of ARG.  */
20068
20069 static void
20070 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20071 {
20072   die_arg_entry entry;
20073
20074   if (!die || !arg)
20075     return;
20076
20077   if (!tmpl_value_parm_die_table)
20078     tmpl_value_parm_die_table
20079       = VEC_alloc (die_arg_entry, gc, 32);
20080
20081   entry.die = die;
20082   entry.arg = arg;
20083   VEC_safe_push (die_arg_entry, gc,
20084                  tmpl_value_parm_die_table,
20085                  &entry);
20086 }
20087
20088 /* Return TRUE if T is an instance of generic type, FALSE
20089    otherwise.  */
20090
20091 static bool
20092 generic_type_p (tree t)
20093 {
20094   if (t == NULL_TREE || !TYPE_P (t))
20095     return false;
20096   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20097 }
20098
20099 /* Schedule the generation of the generic parameter dies for the
20100   instance of generic type T. The proper generation itself is later
20101   done by gen_scheduled_generic_parms_dies. */
20102
20103 static void
20104 schedule_generic_params_dies_gen (tree t)
20105 {
20106   if (!generic_type_p (t))
20107     return;
20108
20109   if (generic_type_instances == NULL)
20110     generic_type_instances = VEC_alloc (tree, gc, 256);
20111
20112   VEC_safe_push (tree, gc, generic_type_instances, t);
20113 }
20114
20115 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20116    by append_entry_to_tmpl_value_parm_die_table. This function must
20117    be called after function DIEs have been generated.  */
20118
20119 static void
20120 gen_remaining_tmpl_value_param_die_attribute (void)
20121 {
20122   if (tmpl_value_parm_die_table)
20123     {
20124       unsigned i;
20125       die_arg_entry *e;
20126
20127       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20128         tree_add_const_value_attribute (e->die, e->arg);
20129     }
20130 }
20131
20132 /* Generate generic parameters DIEs for instances of generic types
20133    that have been previously scheduled by
20134    schedule_generic_params_dies_gen. This function must be called
20135    after all the types of the CU have been laid out.  */
20136
20137 static void
20138 gen_scheduled_generic_parms_dies (void)
20139 {
20140   unsigned i;
20141   tree t;
20142
20143   if (generic_type_instances == NULL)
20144     return;
20145   
20146   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20147     gen_generic_params_dies (t);
20148 }
20149
20150
20151 /* Replace DW_AT_name for the decl with name.  */
20152
20153 static void
20154 dwarf2out_set_name (tree decl, tree name)
20155 {
20156   dw_die_ref die;
20157   dw_attr_ref attr;
20158   const char *dname;
20159
20160   die = TYPE_SYMTAB_DIE (decl);
20161   if (!die)
20162     return;
20163
20164   dname = dwarf2_name (name, 0);
20165   if (!dname)
20166     return;
20167
20168   attr = get_AT (die, DW_AT_name);
20169   if (attr)
20170     {
20171       struct indirect_string_node *node;
20172
20173       node = find_AT_string (dname);
20174       /* replace the string.  */
20175       attr->dw_attr_val.v.val_str = node;
20176     }
20177
20178   else
20179     add_name_attribute (die, dname);
20180 }
20181
20182 /* Called by the final INSN scan whenever we see a var location.  We
20183    use it to drop labels in the right places, and throw the location in
20184    our lookup table.  */
20185
20186 static void
20187 dwarf2out_var_location (rtx loc_note)
20188 {
20189   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20190   struct var_loc_node *newloc;
20191   rtx next_real, next_note;
20192   static const char *last_label;
20193   static const char *last_postcall_label;
20194   static bool last_in_cold_section_p;
20195   static rtx expected_next_loc_note;
20196   tree decl;
20197   bool var_loc_p;
20198
20199   if (!NOTE_P (loc_note))
20200     {
20201       if (CALL_P (loc_note))
20202         {
20203           call_site_count++;
20204           if (SIBLING_CALL_P (loc_note))
20205             tail_call_site_count++;
20206         }
20207       return;
20208     }
20209
20210   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20211   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20212     return;
20213
20214   /* Optimize processing a large consecutive sequence of location
20215      notes so we don't spend too much time in next_real_insn.  If the
20216      next insn is another location note, remember the next_real_insn
20217      calculation for next time.  */
20218   next_real = cached_next_real_insn;
20219   if (next_real)
20220     {
20221       if (expected_next_loc_note != loc_note)
20222         next_real = NULL_RTX;
20223     }
20224
20225   next_note = NEXT_INSN (loc_note);
20226   if (! next_note
20227       || INSN_DELETED_P (next_note)
20228       || GET_CODE (next_note) != NOTE
20229       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20230           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20231     next_note = NULL_RTX;
20232
20233   if (! next_real)
20234     next_real = next_real_insn (loc_note);
20235
20236   if (next_note)
20237     {
20238       expected_next_loc_note = next_note;
20239       cached_next_real_insn = next_real;
20240     }
20241   else
20242     cached_next_real_insn = NULL_RTX;
20243
20244   /* If there are no instructions which would be affected by this note,
20245      don't do anything.  */
20246   if (var_loc_p
20247       && next_real == NULL_RTX
20248       && !NOTE_DURING_CALL_P (loc_note))
20249     return;
20250
20251   if (next_real == NULL_RTX)
20252     next_real = get_last_insn ();
20253
20254   /* If there were any real insns between note we processed last time
20255      and this note (or if it is the first note), clear
20256      last_{,postcall_}label so that they are not reused this time.  */
20257   if (last_var_location_insn == NULL_RTX
20258       || last_var_location_insn != next_real
20259       || last_in_cold_section_p != in_cold_section_p)
20260     {
20261       last_label = NULL;
20262       last_postcall_label = NULL;
20263     }
20264
20265   if (var_loc_p)
20266     {
20267       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20268       newloc = add_var_loc_to_decl (decl, loc_note,
20269                                     NOTE_DURING_CALL_P (loc_note)
20270                                     ? last_postcall_label : last_label);
20271       if (newloc == NULL)
20272         return;
20273     }
20274   else
20275     {
20276       decl = NULL_TREE;
20277       newloc = NULL;
20278     }
20279
20280   /* If there were no real insns between note we processed last time
20281      and this note, use the label we emitted last time.  Otherwise
20282      create a new label and emit it.  */
20283   if (last_label == NULL)
20284     {
20285       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20286       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20287       loclabel_num++;
20288       last_label = ggc_strdup (loclabel);
20289     }
20290
20291   if (!var_loc_p)
20292     {
20293       struct call_arg_loc_node *ca_loc
20294         = ggc_alloc_cleared_call_arg_loc_node ();
20295       rtx prev = prev_real_insn (loc_note), x;
20296       ca_loc->call_arg_loc_note = loc_note;
20297       ca_loc->next = NULL;
20298       ca_loc->label = last_label;
20299       gcc_assert (prev
20300                   && (CALL_P (prev)
20301                       || (NONJUMP_INSN_P (prev)
20302                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20303                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20304       if (!CALL_P (prev))
20305         prev = XVECEXP (PATTERN (prev), 0, 0);
20306       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20307       x = PATTERN (prev);
20308       if (GET_CODE (x) == PARALLEL)
20309         x = XVECEXP (x, 0, 0);
20310       if (GET_CODE (x) == SET)
20311         x = SET_SRC (x);
20312       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20313         {
20314           x = XEXP (XEXP (x, 0), 0);
20315           if (GET_CODE (x) == SYMBOL_REF
20316               && SYMBOL_REF_DECL (x)
20317               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20318             ca_loc->symbol_ref = x;
20319         }
20320       ca_loc->block = insn_scope (prev);
20321       if (call_arg_locations)
20322         call_arg_loc_last->next = ca_loc;
20323       else
20324         call_arg_locations = ca_loc;
20325       call_arg_loc_last = ca_loc;
20326     }
20327   else if (!NOTE_DURING_CALL_P (loc_note))
20328     newloc->label = last_label;
20329   else
20330     {
20331       if (!last_postcall_label)
20332         {
20333           sprintf (loclabel, "%s-1", last_label);
20334           last_postcall_label = ggc_strdup (loclabel);
20335         }
20336       newloc->label = last_postcall_label;
20337     }
20338
20339   last_var_location_insn = next_real;
20340   last_in_cold_section_p = in_cold_section_p;
20341 }
20342
20343 /* Note in one location list that text section has changed.  */
20344
20345 static int
20346 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20347 {
20348   var_loc_list *list = (var_loc_list *) *slot;
20349   if (list->first)
20350     list->last_before_switch
20351       = list->last->next ? list->last->next : list->last;
20352   return 1;
20353 }
20354
20355 /* Note in all location lists that text section has changed.  */
20356
20357 static void
20358 var_location_switch_text_section (void)
20359 {
20360   if (decl_loc_table == NULL)
20361     return;
20362
20363   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20364 }
20365
20366 /* Create a new line number table.  */
20367
20368 static dw_line_info_table *
20369 new_line_info_table (void)
20370 {
20371   dw_line_info_table *table;
20372
20373   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20374   table->file_num = 1;
20375   table->line_num = 1;
20376   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20377
20378   return table;
20379 }
20380
20381 /* Lookup the "current" table into which we emit line info, so
20382    that we don't have to do it for every source line.  */
20383
20384 static void
20385 set_cur_line_info_table (section *sec)
20386 {
20387   dw_line_info_table *table;
20388
20389   if (sec == text_section)
20390     table = text_section_line_info;
20391   else if (sec == cold_text_section)
20392     {
20393       table = cold_text_section_line_info;
20394       if (!table)
20395         {
20396           cold_text_section_line_info = table = new_line_info_table ();
20397           table->end_label = cold_end_label;
20398         }
20399     }
20400   else
20401     {
20402       const char *end_label;
20403
20404       if (flag_reorder_blocks_and_partition)
20405         {
20406           if (in_cold_section_p)
20407             end_label = crtl->subsections.cold_section_end_label;
20408           else
20409             end_label = crtl->subsections.hot_section_end_label;
20410         }
20411       else
20412         {
20413           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20414           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20415                                        current_function_funcdef_no);
20416           end_label = ggc_strdup (label);
20417         }
20418
20419       table = new_line_info_table ();
20420       table->end_label = end_label;
20421
20422       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20423     }
20424
20425   if (DWARF2_ASM_LINE_DEBUG_INFO)
20426     table->is_stmt = (cur_line_info_table
20427                       ? cur_line_info_table->is_stmt
20428                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20429   cur_line_info_table = table;
20430 }
20431
20432
20433 /* We need to reset the locations at the beginning of each
20434    function. We can't do this in the end_function hook, because the
20435    declarations that use the locations won't have been output when
20436    that hook is called.  Also compute have_multiple_function_sections here.  */
20437
20438 static void
20439 dwarf2out_begin_function (tree fun)
20440 {
20441   section *sec = function_section (fun);
20442
20443   if (sec != text_section)
20444     have_multiple_function_sections = true;
20445
20446   if (flag_reorder_blocks_and_partition && !cold_text_section)
20447     {
20448       gcc_assert (current_function_decl == fun);
20449       cold_text_section = unlikely_text_section ();
20450       switch_to_section (cold_text_section);
20451       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20452       switch_to_section (sec);
20453     }
20454
20455   dwarf2out_note_section_used ();
20456   call_site_count = 0;
20457   tail_call_site_count = 0;
20458
20459   set_cur_line_info_table (sec);
20460 }
20461
20462 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20463
20464 static void
20465 push_dw_line_info_entry (dw_line_info_table *table,
20466                          enum dw_line_info_opcode opcode, unsigned int val)
20467 {
20468   dw_line_info_entry e;
20469   e.opcode = opcode;
20470   e.val = val;
20471   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20472 }
20473
20474 /* Output a label to mark the beginning of a source code line entry
20475    and record information relating to this source line, in
20476    'line_info_table' for later output of the .debug_line section.  */
20477 /* ??? The discriminator parameter ought to be unsigned.  */
20478
20479 static void
20480 dwarf2out_source_line (unsigned int line, const char *filename,
20481                        int discriminator, bool is_stmt)
20482 {
20483   unsigned int file_num;
20484   dw_line_info_table *table;
20485
20486   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20487     return;
20488
20489   /* The discriminator column was added in dwarf4.  Simplify the below
20490      by simply removing it if we're not supposed to output it.  */
20491   if (dwarf_version < 4 && dwarf_strict)
20492     discriminator = 0;
20493
20494   table = cur_line_info_table;
20495   file_num = maybe_emit_file (lookup_filename (filename));
20496
20497   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20498      the debugger has used the second (possibly duplicate) line number
20499      at the beginning of the function to mark the end of the prologue.
20500      We could eliminate any other duplicates within the function.  For
20501      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20502      that second line number entry.  */
20503   /* Recall that this end-of-prologue indication is *not* the same thing
20504      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20505      to which the hook corresponds, follows the last insn that was 
20506      emitted by gen_prologue.  What we need is to preceed the first insn
20507      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20508      insn that corresponds to something the user wrote.  These may be
20509      very different locations once scheduling is enabled.  */
20510
20511   if (0 && file_num == table->file_num
20512       && line == table->line_num
20513       && discriminator == table->discrim_num
20514       && is_stmt == table->is_stmt)
20515     return;
20516
20517   switch_to_section (current_function_section ());
20518
20519   /* If requested, emit something human-readable.  */
20520   if (flag_debug_asm)
20521     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20522
20523   if (DWARF2_ASM_LINE_DEBUG_INFO)
20524     {
20525       /* Emit the .loc directive understood by GNU as.  */
20526       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20527          file_num, line, is_stmt, discriminator */
20528       fputs ("\t.loc ", asm_out_file);
20529       fprint_ul (asm_out_file, file_num);
20530       putc (' ', asm_out_file);
20531       fprint_ul (asm_out_file, line);
20532       putc (' ', asm_out_file);
20533       putc ('0', asm_out_file);
20534
20535       if (is_stmt != table->is_stmt)
20536         {
20537           fputs (" is_stmt ", asm_out_file);
20538           putc (is_stmt ? '1' : '0', asm_out_file);
20539         }
20540       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20541         {
20542           gcc_assert (discriminator > 0);
20543           fputs (" discriminator ", asm_out_file);
20544           fprint_ul (asm_out_file, (unsigned long) discriminator);
20545         }
20546       putc ('\n', asm_out_file);
20547     }
20548   else
20549     {
20550       unsigned int label_num = ++line_info_label_num;
20551
20552       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20553
20554       push_dw_line_info_entry (table, LI_set_address, label_num);
20555       if (file_num != table->file_num)
20556         push_dw_line_info_entry (table, LI_set_file, file_num);
20557       if (discriminator != table->discrim_num)
20558         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20559       if (is_stmt != table->is_stmt)
20560         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20561       push_dw_line_info_entry (table, LI_set_line, line);
20562     }
20563
20564   table->file_num = file_num;
20565   table->line_num = line;
20566   table->discrim_num = discriminator;
20567   table->is_stmt = is_stmt;
20568   table->in_use = true;
20569 }
20570
20571 /* Record the beginning of a new source file.  */
20572
20573 static void
20574 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20575 {
20576   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20577     {
20578       /* Record the beginning of the file for break_out_includes.  */
20579       dw_die_ref bincl_die;
20580
20581       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20582       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20583     }
20584
20585   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20586     {
20587       macinfo_entry e;
20588       e.code = DW_MACINFO_start_file;
20589       e.lineno = lineno;
20590       e.info = ggc_strdup (filename);
20591       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20592     }
20593 }
20594
20595 /* Record the end of a source file.  */
20596
20597 static void
20598 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20599 {
20600   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20601     /* Record the end of the file for break_out_includes.  */
20602     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20603
20604   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20605     {
20606       macinfo_entry e;
20607       e.code = DW_MACINFO_end_file;
20608       e.lineno = lineno;
20609       e.info = NULL;
20610       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20611     }
20612 }
20613
20614 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20615    the tail part of the directive line, i.e. the part which is past the
20616    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20617
20618 static void
20619 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20620                   const char *buffer ATTRIBUTE_UNUSED)
20621 {
20622   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20623     {
20624       macinfo_entry e;
20625       /* Insert a dummy first entry to be able to optimize the whole
20626          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20627       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20628         {
20629           e.code = 0;
20630           e.lineno = 0;
20631           e.info = NULL;
20632           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20633         }
20634       e.code = DW_MACINFO_define;
20635       e.lineno = lineno;
20636       e.info = ggc_strdup (buffer);
20637       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20638     }
20639 }
20640
20641 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20642    the tail part of the directive line, i.e. the part which is past the
20643    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20644
20645 static void
20646 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20647                  const char *buffer ATTRIBUTE_UNUSED)
20648 {
20649   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20650     {
20651       macinfo_entry e;
20652       /* Insert a dummy first entry to be able to optimize the whole
20653          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20654       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20655         {
20656           e.code = 0;
20657           e.lineno = 0;
20658           e.info = NULL;
20659           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20660         }
20661       e.code = DW_MACINFO_undef;
20662       e.lineno = lineno;
20663       e.info = ggc_strdup (buffer);
20664       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20665     }
20666 }
20667
20668 /* Routines to manipulate hash table of CUs.  */
20669
20670 static hashval_t
20671 htab_macinfo_hash (const void *of)
20672 {
20673   const macinfo_entry *const entry =
20674     (const macinfo_entry *) of;
20675
20676   return htab_hash_string (entry->info);
20677 }
20678
20679 static int
20680 htab_macinfo_eq (const void *of1, const void *of2)
20681 {
20682   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20683   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20684
20685   return !strcmp (entry1->info, entry2->info);
20686 }
20687
20688 /* Output a single .debug_macinfo entry.  */
20689
20690 static void
20691 output_macinfo_op (macinfo_entry *ref)
20692 {
20693   int file_num;
20694   size_t len;
20695   struct indirect_string_node *node;
20696   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20697   struct dwarf_file_data *fd;
20698
20699   switch (ref->code)
20700     {
20701     case DW_MACINFO_start_file:
20702       fd = lookup_filename (ref->info);
20703       file_num = maybe_emit_file (fd);
20704       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20705       dw2_asm_output_data_uleb128 (ref->lineno,
20706                                    "Included from line number %lu", 
20707                                    (unsigned long) ref->lineno);
20708       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20709       break;
20710     case DW_MACINFO_end_file:
20711       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20712       break;
20713     case DW_MACINFO_define:
20714     case DW_MACINFO_undef:
20715       len = strlen (ref->info) + 1;
20716       if (!dwarf_strict
20717           && len > DWARF_OFFSET_SIZE
20718           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20719           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20720         {
20721           ref->code = ref->code == DW_MACINFO_define
20722                       ? DW_MACRO_GNU_define_indirect
20723                       : DW_MACRO_GNU_undef_indirect;
20724           output_macinfo_op (ref);
20725           return;
20726         }
20727       dw2_asm_output_data (1, ref->code,
20728                            ref->code == DW_MACINFO_define
20729                            ? "Define macro" : "Undefine macro");
20730       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20731                                    (unsigned long) ref->lineno);
20732       dw2_asm_output_nstring (ref->info, -1, "The macro");
20733       break;
20734     case DW_MACRO_GNU_define_indirect:
20735     case DW_MACRO_GNU_undef_indirect:
20736       node = find_AT_string (ref->info);
20737       if (node->form != DW_FORM_strp)
20738         {
20739           char label[32];
20740           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20741           ++dw2_string_counter;
20742           node->label = xstrdup (label);
20743           node->form = DW_FORM_strp;
20744         }
20745       dw2_asm_output_data (1, ref->code,
20746                            ref->code == DW_MACRO_GNU_define_indirect
20747                            ? "Define macro indirect"
20748                            : "Undefine macro indirect");
20749       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20750                                    (unsigned long) ref->lineno);
20751       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20752                              debug_str_section, "The macro: \"%s\"",
20753                              ref->info);
20754       break;
20755     case DW_MACRO_GNU_transparent_include:
20756       dw2_asm_output_data (1, ref->code, "Transparent include");
20757       ASM_GENERATE_INTERNAL_LABEL (label,
20758                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20759       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20760       break;
20761     default:
20762       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20763                ASM_COMMENT_START, (unsigned long) ref->code);
20764       break;
20765     }
20766 }
20767
20768 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20769    other compilation unit .debug_macinfo sections.  IDX is the first
20770    index of a define/undef, return the number of ops that should be
20771    emitted in a comdat .debug_macinfo section and emit
20772    a DW_MACRO_GNU_transparent_include entry referencing it.
20773    If the define/undef entry should be emitted normally, return 0.  */
20774
20775 static unsigned
20776 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20777                         htab_t *macinfo_htab)
20778 {
20779   macinfo_entry *first, *second, *cur, *inc;
20780   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20781   unsigned char checksum[16];
20782   struct md5_ctx ctx;
20783   char *grp_name, *tail;
20784   const char *base;
20785   unsigned int i, count, encoded_filename_len, linebuf_len;
20786   void **slot;
20787
20788   first = VEC_index (macinfo_entry, macinfo_table, idx);
20789   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20790
20791   /* Optimize only if there are at least two consecutive define/undef ops,
20792      and either all of them are before first DW_MACINFO_start_file
20793      with lineno 0 (i.e. predefined macro block), or all of them are
20794      in some included header file.  */
20795   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20796     return 0;
20797   if (VEC_empty (macinfo_entry, files))
20798     {
20799       if (first->lineno != 0 || second->lineno != 0)
20800         return 0;
20801     }
20802   else if (first->lineno == 0)
20803     return 0;
20804
20805   /* Find the last define/undef entry that can be grouped together
20806      with first and at the same time compute md5 checksum of their
20807      codes, linenumbers and strings.  */
20808   md5_init_ctx (&ctx);
20809   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20810     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20811       break;
20812     else if (first->lineno == 0 && cur->lineno != 0)
20813       break;
20814     else
20815       {
20816         unsigned char code = cur->code;
20817         md5_process_bytes (&code, 1, &ctx);
20818         checksum_uleb128 (cur->lineno, &ctx);
20819         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20820       }
20821   md5_finish_ctx (&ctx, checksum);
20822   count = i - idx;
20823
20824   /* From the containing include filename (if any) pick up just
20825      usable characters from its basename.  */
20826   if (first->lineno == 0)
20827     base = "";
20828   else
20829     base = lbasename (VEC_last (macinfo_entry, files)->info);
20830   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20831     if (ISIDNUM (base[i]) || base[i] == '.')
20832       encoded_filename_len++;
20833   /* Count . at the end.  */
20834   if (encoded_filename_len)
20835     encoded_filename_len++;
20836
20837   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20838   linebuf_len = strlen (linebuf);
20839
20840   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20841   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20842                          + 16 * 2 + 1);
20843   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20844   tail = grp_name + 4;
20845   if (encoded_filename_len)
20846     {
20847       for (i = 0; base[i]; i++)
20848         if (ISIDNUM (base[i]) || base[i] == '.')
20849           *tail++ = base[i];
20850       *tail++ = '.';
20851     }
20852   memcpy (tail, linebuf, linebuf_len);
20853   tail += linebuf_len;
20854   *tail++ = '.';
20855   for (i = 0; i < 16; i++)
20856     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20857
20858   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20859      in the empty vector entry before the first define/undef.  */
20860   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20861   inc->code = DW_MACRO_GNU_transparent_include;
20862   inc->lineno = 0;
20863   inc->info = ggc_strdup (grp_name);
20864   if (*macinfo_htab == NULL)
20865     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20866   /* Avoid emitting duplicates.  */
20867   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20868   if (*slot != NULL)
20869     {
20870       inc->code = 0;
20871       inc->info = NULL;
20872       /* If such an entry has been used before, just emit
20873          a DW_MACRO_GNU_transparent_include op.  */
20874       inc = (macinfo_entry *) *slot;
20875       output_macinfo_op (inc);
20876       /* And clear all macinfo_entry in the range to avoid emitting them
20877          in the second pass.  */
20878       for (i = idx;
20879            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20880            && i < idx + count;
20881            i++)
20882         {
20883           cur->code = 0;
20884           cur->info = NULL;
20885         }
20886     }
20887   else
20888     {
20889       *slot = inc;
20890       inc->lineno = htab_elements (*macinfo_htab);
20891       output_macinfo_op (inc);
20892     }
20893   return count;
20894 }
20895
20896 /* Output macinfo section(s).  */
20897
20898 static void
20899 output_macinfo (void)
20900 {
20901   unsigned i;
20902   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20903   macinfo_entry *ref;
20904   VEC (macinfo_entry, gc) *files = NULL;
20905   htab_t macinfo_htab = NULL;
20906
20907   if (! length)
20908     return;
20909
20910   /* output_macinfo* uses these interchangeably.  */
20911   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20912               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20913               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20914               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20915
20916   /* For .debug_macro emit the section header.  */
20917   if (!dwarf_strict)
20918     {
20919       dw2_asm_output_data (2, 4, "DWARF macro version number");
20920       if (DWARF_OFFSET_SIZE == 8)
20921         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20922       else
20923         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20924       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20925                              debug_line_section, NULL);
20926     }
20927
20928   /* In the first loop, it emits the primary .debug_macinfo section
20929      and after each emitted op the macinfo_entry is cleared.
20930      If a longer range of define/undef ops can be optimized using
20931      DW_MACRO_GNU_transparent_include, the
20932      DW_MACRO_GNU_transparent_include op is emitted and kept in
20933      the vector before the first define/undef in the range and the
20934      whole range of define/undef ops is not emitted and kept.  */
20935   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20936     {
20937       switch (ref->code)
20938         {
20939         case DW_MACINFO_start_file:
20940           VEC_safe_push (macinfo_entry, gc, files, ref);
20941           break;
20942         case DW_MACINFO_end_file:
20943           if (!VEC_empty (macinfo_entry, files))
20944             VEC_pop (macinfo_entry, files);
20945           break;
20946         case DW_MACINFO_define:
20947         case DW_MACINFO_undef:
20948           if (!dwarf_strict
20949               && HAVE_COMDAT_GROUP
20950               && VEC_length (macinfo_entry, files) != 1
20951               && i > 0
20952               && i + 1 < length
20953               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20954             {
20955               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20956               if (count)
20957                 {
20958                   i += count - 1;
20959                   continue;
20960                 }
20961             }
20962           break;
20963         case 0:
20964           /* A dummy entry may be inserted at the beginning to be able
20965              to optimize the whole block of predefined macros.  */
20966           if (i == 0)
20967             continue;
20968         default:
20969           break;
20970         }
20971       output_macinfo_op (ref);
20972       ref->info = NULL;
20973       ref->code = 0;
20974     }
20975
20976   if (macinfo_htab == NULL)
20977     return;
20978
20979   htab_delete (macinfo_htab);
20980
20981   /* If any DW_MACRO_GNU_transparent_include were used, on those
20982      DW_MACRO_GNU_transparent_include entries terminate the
20983      current chain and switch to a new comdat .debug_macinfo
20984      section and emit the define/undef entries within it.  */
20985   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20986     switch (ref->code)
20987       {
20988       case 0:
20989         continue;
20990       case DW_MACRO_GNU_transparent_include:
20991         {
20992           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20993           tree comdat_key = get_identifier (ref->info);
20994           /* Terminate the previous .debug_macinfo section.  */
20995           dw2_asm_output_data (1, 0, "End compilation unit");
20996           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20997                                          SECTION_DEBUG
20998                                          | SECTION_LINKONCE,
20999                                          comdat_key);
21000           ASM_GENERATE_INTERNAL_LABEL (label,
21001                                        DEBUG_MACRO_SECTION_LABEL,
21002                                        ref->lineno);
21003           ASM_OUTPUT_LABEL (asm_out_file, label);
21004           ref->code = 0;
21005           ref->info = NULL;
21006           dw2_asm_output_data (2, 4, "DWARF macro version number");
21007           if (DWARF_OFFSET_SIZE == 8)
21008             dw2_asm_output_data (1, 1, "Flags: 64-bit");
21009           else
21010             dw2_asm_output_data (1, 0, "Flags: 32-bit");
21011         }
21012         break;
21013       case DW_MACINFO_define:
21014       case DW_MACINFO_undef:
21015         output_macinfo_op (ref);
21016         ref->code = 0;
21017         ref->info = NULL;
21018         break;
21019       default:
21020         gcc_unreachable ();
21021       }
21022 }
21023
21024 /* Set up for Dwarf output at the start of compilation.  */
21025
21026 static void
21027 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21028 {
21029   /* Allocate the file_table.  */
21030   file_table = htab_create_ggc (50, file_table_hash,
21031                                 file_table_eq, NULL);
21032
21033   /* Allocate the decl_die_table.  */
21034   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21035                                     decl_die_table_eq, NULL);
21036
21037   /* Allocate the decl_loc_table.  */
21038   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21039                                     decl_loc_table_eq, NULL);
21040
21041   /* Allocate the cached_dw_loc_list_table.  */
21042   cached_dw_loc_list_table
21043     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21044                        cached_dw_loc_list_table_eq, NULL);
21045
21046   /* Allocate the initial hunk of the decl_scope_table.  */
21047   decl_scope_table = VEC_alloc (tree, gc, 256);
21048
21049   /* Allocate the initial hunk of the abbrev_die_table.  */
21050   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21051     (ABBREV_DIE_TABLE_INCREMENT);
21052   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21053   /* Zero-th entry is allocated, but unused.  */
21054   abbrev_die_table_in_use = 1;
21055
21056   /* Allocate the pubtypes and pubnames vectors.  */
21057   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21058   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21059
21060   incomplete_types = VEC_alloc (tree, gc, 64);
21061
21062   used_rtx_array = VEC_alloc (rtx, gc, 32);
21063
21064   debug_info_section = get_section (DEBUG_INFO_SECTION,
21065                                     SECTION_DEBUG, NULL);
21066   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21067                                       SECTION_DEBUG, NULL);
21068   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21069                                        SECTION_DEBUG, NULL);
21070   debug_macinfo_section = get_section (dwarf_strict
21071                                        ? DEBUG_MACINFO_SECTION
21072                                        : DEBUG_MACRO_SECTION,
21073                                        SECTION_DEBUG, NULL);
21074   debug_line_section = get_section (DEBUG_LINE_SECTION,
21075                                     SECTION_DEBUG, NULL);
21076   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21077                                    SECTION_DEBUG, NULL);
21078   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21079                                         SECTION_DEBUG, NULL);
21080   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21081                                         SECTION_DEBUG, NULL);
21082   debug_str_section = get_section (DEBUG_STR_SECTION,
21083                                    DEBUG_STR_SECTION_FLAGS, NULL);
21084   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21085                                       SECTION_DEBUG, NULL);
21086   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21087                                      SECTION_DEBUG, NULL);
21088
21089   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21090   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21091                                DEBUG_ABBREV_SECTION_LABEL, 0);
21092   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21093   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21094                                COLD_TEXT_SECTION_LABEL, 0);
21095   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21096
21097   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21098                                DEBUG_INFO_SECTION_LABEL, 0);
21099   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21100                                DEBUG_LINE_SECTION_LABEL, 0);
21101   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21102                                DEBUG_RANGES_SECTION_LABEL, 0);
21103   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21104                                dwarf_strict
21105                                ? DEBUG_MACINFO_SECTION_LABEL
21106                                : DEBUG_MACRO_SECTION_LABEL, 0);
21107
21108   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21109     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21110
21111   switch_to_section (text_section);
21112   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21113
21114   /* Make sure the line number table for .text always exists.  */
21115   text_section_line_info = new_line_info_table ();
21116   text_section_line_info->end_label = text_end_label;
21117 }
21118
21119 /* Called before cgraph_optimize starts outputtting functions, variables
21120    and toplevel asms into assembly.  */
21121
21122 static void
21123 dwarf2out_assembly_start (void)
21124 {
21125   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21126       && dwarf2out_do_cfi_asm ()
21127       && (!(flag_unwind_tables || flag_exceptions)
21128           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21129     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21130 }
21131
21132 /* A helper function for dwarf2out_finish called through
21133    htab_traverse.  Emit one queued .debug_str string.  */
21134
21135 static int
21136 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21137 {
21138   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21139
21140   if (node->form == DW_FORM_strp)
21141     {
21142       switch_to_section (debug_str_section);
21143       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21144       assemble_string (node->str, strlen (node->str) + 1);
21145     }
21146
21147   return 1;
21148 }
21149
21150 #if ENABLE_ASSERT_CHECKING
21151 /* Verify that all marks are clear.  */
21152
21153 static void
21154 verify_marks_clear (dw_die_ref die)
21155 {
21156   dw_die_ref c;
21157
21158   gcc_assert (! die->die_mark);
21159   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21160 }
21161 #endif /* ENABLE_ASSERT_CHECKING */
21162
21163 /* Clear the marks for a die and its children.
21164    Be cool if the mark isn't set.  */
21165
21166 static void
21167 prune_unmark_dies (dw_die_ref die)
21168 {
21169   dw_die_ref c;
21170
21171   if (die->die_mark)
21172     die->die_mark = 0;
21173   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21174 }
21175
21176 /* Given DIE that we're marking as used, find any other dies
21177    it references as attributes and mark them as used.  */
21178
21179 static void
21180 prune_unused_types_walk_attribs (dw_die_ref die)
21181 {
21182   dw_attr_ref a;
21183   unsigned ix;
21184
21185   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21186     {
21187       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21188         {
21189           /* A reference to another DIE.
21190              Make sure that it will get emitted.
21191              If it was broken out into a comdat group, don't follow it.  */
21192           if (! use_debug_types
21193               || a->dw_attr == DW_AT_specification
21194               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21195             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21196         }
21197       /* Set the string's refcount to 0 so that prune_unused_types_mark
21198          accounts properly for it.  */
21199       if (AT_class (a) == dw_val_class_str)
21200         a->dw_attr_val.v.val_str->refcount = 0;
21201     }
21202 }
21203
21204 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21205
21206 static void
21207 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21208 {
21209   dw_die_ref c;
21210
21211   if (die == NULL || die->die_child == NULL)
21212     return;
21213   c = die->die_child;
21214   do
21215     {
21216       switch (c->die_tag)
21217         {
21218         case DW_TAG_template_type_param:
21219         case DW_TAG_template_value_param:
21220         case DW_TAG_GNU_template_template_param:
21221         case DW_TAG_GNU_template_parameter_pack:
21222           prune_unused_types_mark (c, 1);
21223           break;
21224         default:
21225           break;
21226         }
21227       c = c->die_sib;
21228     } while (c && c != die->die_child);
21229 }
21230
21231 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21232    to DIE's children.  */
21233
21234 static void
21235 prune_unused_types_mark (dw_die_ref die, int dokids)
21236 {
21237   dw_die_ref c;
21238
21239   if (die->die_mark == 0)
21240     {
21241       /* We haven't done this node yet.  Mark it as used.  */
21242       die->die_mark = 1;
21243       /* If this is the DIE of a generic type instantiation,
21244          mark the children DIEs that describe its generic parms and
21245          args.  */
21246       prune_unused_types_mark_generic_parms_dies (die);
21247
21248       /* We also have to mark its parents as used.
21249          (But we don't want to mark our parents' kids due to this.)  */
21250       if (die->die_parent)
21251         prune_unused_types_mark (die->die_parent, 0);
21252
21253       /* Mark any referenced nodes.  */
21254       prune_unused_types_walk_attribs (die);
21255
21256       /* If this node is a specification,
21257          also mark the definition, if it exists.  */
21258       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21259         prune_unused_types_mark (die->die_definition, 1);
21260     }
21261
21262   if (dokids && die->die_mark != 2)
21263     {
21264       /* We need to walk the children, but haven't done so yet.
21265          Remember that we've walked the kids.  */
21266       die->die_mark = 2;
21267
21268       /* If this is an array type, we need to make sure our
21269          kids get marked, even if they're types.  If we're
21270          breaking out types into comdat sections, do this
21271          for all type definitions.  */
21272       if (die->die_tag == DW_TAG_array_type
21273           || (use_debug_types
21274               && is_type_die (die) && ! is_declaration_die (die)))
21275         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21276       else
21277         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21278     }
21279 }
21280
21281 /* For local classes, look if any static member functions were emitted
21282    and if so, mark them.  */
21283
21284 static void
21285 prune_unused_types_walk_local_classes (dw_die_ref die)
21286 {
21287   dw_die_ref c;
21288
21289   if (die->die_mark == 2)
21290     return;
21291
21292   switch (die->die_tag)
21293     {
21294     case DW_TAG_structure_type:
21295     case DW_TAG_union_type:
21296     case DW_TAG_class_type:
21297       break;
21298
21299     case DW_TAG_subprogram:
21300       if (!get_AT_flag (die, DW_AT_declaration)
21301           || die->die_definition != NULL)
21302         prune_unused_types_mark (die, 1);
21303       return;
21304
21305     default:
21306       return;
21307     }
21308
21309   /* Mark children.  */
21310   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21311 }
21312
21313 /* Walk the tree DIE and mark types that we actually use.  */
21314
21315 static void
21316 prune_unused_types_walk (dw_die_ref die)
21317 {
21318   dw_die_ref c;
21319
21320   /* Don't do anything if this node is already marked and
21321      children have been marked as well.  */
21322   if (die->die_mark == 2)
21323     return;
21324
21325   switch (die->die_tag)
21326     {
21327     case DW_TAG_structure_type:
21328     case DW_TAG_union_type:
21329     case DW_TAG_class_type:
21330       if (die->die_perennial_p)
21331         break;
21332
21333       for (c = die->die_parent; c; c = c->die_parent)
21334         if (c->die_tag == DW_TAG_subprogram)
21335           break;
21336
21337       /* Finding used static member functions inside of classes
21338          is needed just for local classes, because for other classes
21339          static member function DIEs with DW_AT_specification
21340          are emitted outside of the DW_TAG_*_type.  If we ever change
21341          it, we'd need to call this even for non-local classes.  */
21342       if (c)
21343         prune_unused_types_walk_local_classes (die);
21344
21345       /* It's a type node --- don't mark it.  */
21346       return;
21347
21348     case DW_TAG_const_type:
21349     case DW_TAG_packed_type:
21350     case DW_TAG_pointer_type:
21351     case DW_TAG_reference_type:
21352     case DW_TAG_rvalue_reference_type:
21353     case DW_TAG_volatile_type:
21354     case DW_TAG_typedef:
21355     case DW_TAG_array_type:
21356     case DW_TAG_interface_type:
21357     case DW_TAG_friend:
21358     case DW_TAG_variant_part:
21359     case DW_TAG_enumeration_type:
21360     case DW_TAG_subroutine_type:
21361     case DW_TAG_string_type:
21362     case DW_TAG_set_type:
21363     case DW_TAG_subrange_type:
21364     case DW_TAG_ptr_to_member_type:
21365     case DW_TAG_file_type:
21366       if (die->die_perennial_p)
21367         break;
21368
21369       /* It's a type node --- don't mark it.  */
21370       return;
21371
21372     default:
21373       /* Mark everything else.  */
21374       break;
21375   }
21376
21377   if (die->die_mark == 0)
21378     {
21379       die->die_mark = 1;
21380
21381       /* Now, mark any dies referenced from here.  */
21382       prune_unused_types_walk_attribs (die);
21383     }
21384
21385   die->die_mark = 2;
21386
21387   /* Mark children.  */
21388   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21389 }
21390
21391 /* Increment the string counts on strings referred to from DIE's
21392    attributes.  */
21393
21394 static void
21395 prune_unused_types_update_strings (dw_die_ref die)
21396 {
21397   dw_attr_ref a;
21398   unsigned ix;
21399
21400   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21401     if (AT_class (a) == dw_val_class_str)
21402       {
21403         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21404         s->refcount++;
21405         /* Avoid unnecessarily putting strings that are used less than
21406            twice in the hash table.  */
21407         if (s->refcount
21408             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21409           {
21410             void ** slot;
21411             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21412                                              htab_hash_string (s->str),
21413                                              INSERT);
21414             gcc_assert (*slot == NULL);
21415             *slot = s;
21416           }
21417       }
21418 }
21419
21420 /* Remove from the tree DIE any dies that aren't marked.  */
21421
21422 static void
21423 prune_unused_types_prune (dw_die_ref die)
21424 {
21425   dw_die_ref c;
21426
21427   gcc_assert (die->die_mark);
21428   prune_unused_types_update_strings (die);
21429
21430   if (! die->die_child)
21431     return;
21432
21433   c = die->die_child;
21434   do {
21435     dw_die_ref prev = c;
21436     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21437       if (c == die->die_child)
21438         {
21439           /* No marked children between 'prev' and the end of the list.  */
21440           if (prev == c)
21441             /* No marked children at all.  */
21442             die->die_child = NULL;
21443           else
21444             {
21445               prev->die_sib = c->die_sib;
21446               die->die_child = prev;
21447             }
21448           return;
21449         }
21450
21451     if (c != prev->die_sib)
21452       prev->die_sib = c;
21453     prune_unused_types_prune (c);
21454   } while (c != die->die_child);
21455 }
21456
21457 /* Remove dies representing declarations that we never use.  */
21458
21459 static void
21460 prune_unused_types (void)
21461 {
21462   unsigned int i;
21463   limbo_die_node *node;
21464   comdat_type_node *ctnode;
21465   pubname_ref pub;
21466   dw_die_ref base_type;
21467
21468 #if ENABLE_ASSERT_CHECKING
21469   /* All the marks should already be clear.  */
21470   verify_marks_clear (comp_unit_die ());
21471   for (node = limbo_die_list; node; node = node->next)
21472     verify_marks_clear (node->die);
21473   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21474     verify_marks_clear (ctnode->root_die);
21475 #endif /* ENABLE_ASSERT_CHECKING */
21476
21477   /* Mark types that are used in global variables.  */
21478   premark_types_used_by_global_vars ();
21479
21480   /* Set the mark on nodes that are actually used.  */
21481   prune_unused_types_walk (comp_unit_die ());
21482   for (node = limbo_die_list; node; node = node->next)
21483     prune_unused_types_walk (node->die);
21484   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21485     {
21486       prune_unused_types_walk (ctnode->root_die);
21487       prune_unused_types_mark (ctnode->type_die, 1);
21488     }
21489
21490   /* Also set the mark on nodes referenced from the
21491      pubname_table.  */
21492   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21493     prune_unused_types_mark (pub->die, 1);
21494   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21495     prune_unused_types_mark (base_type, 1);
21496
21497   if (debug_str_hash)
21498     htab_empty (debug_str_hash);
21499   prune_unused_types_prune (comp_unit_die ());
21500   for (node = limbo_die_list; node; node = node->next)
21501     prune_unused_types_prune (node->die);
21502   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21503     prune_unused_types_prune (ctnode->root_die);
21504
21505   /* Leave the marks clear.  */
21506   prune_unmark_dies (comp_unit_die ());
21507   for (node = limbo_die_list; node; node = node->next)
21508     prune_unmark_dies (node->die);
21509   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21510     prune_unmark_dies (ctnode->root_die);
21511 }
21512
21513 /* Set the parameter to true if there are any relative pathnames in
21514    the file table.  */
21515 static int
21516 file_table_relative_p (void ** slot, void *param)
21517 {
21518   bool *p = (bool *) param;
21519   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21520   if (!IS_ABSOLUTE_PATH (d->filename))
21521     {
21522       *p = true;
21523       return 0;
21524     }
21525   return 1;
21526 }
21527
21528 /* Routines to manipulate hash table of comdat type units.  */
21529
21530 static hashval_t
21531 htab_ct_hash (const void *of)
21532 {
21533   hashval_t h;
21534   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21535
21536   memcpy (&h, type_node->signature, sizeof (h));
21537   return h;
21538 }
21539
21540 static int
21541 htab_ct_eq (const void *of1, const void *of2)
21542 {
21543   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21544   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21545
21546   return (! memcmp (type_node_1->signature, type_node_2->signature,
21547                     DWARF_TYPE_SIGNATURE_SIZE));
21548 }
21549
21550 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21551    to the location it would have been added, should we know its
21552    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21553    probably improve compactness of debug info, removing equivalent
21554    abbrevs, and hide any differences caused by deferring the
21555    computation of the assembler name, triggered by e.g. PCH.  */
21556
21557 static inline void
21558 move_linkage_attr (dw_die_ref die)
21559 {
21560   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21561   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21562
21563   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21564               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21565
21566   while (--ix > 0)
21567     {
21568       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21569
21570       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21571         break;
21572     }
21573
21574   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21575     {
21576       VEC_pop (dw_attr_node, die->die_attr);
21577       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21578     }
21579 }
21580
21581 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21582    referenced from typed stack ops and count how often they are used.  */
21583
21584 static void
21585 mark_base_types (dw_loc_descr_ref loc)
21586 {
21587   dw_die_ref base_type = NULL;
21588
21589   for (; loc; loc = loc->dw_loc_next)
21590     {
21591       switch (loc->dw_loc_opc)
21592         {
21593         case DW_OP_GNU_regval_type:
21594         case DW_OP_GNU_deref_type:
21595           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21596           break;
21597         case DW_OP_GNU_convert:
21598         case DW_OP_GNU_reinterpret:
21599           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21600             continue;
21601           /* FALLTHRU */
21602         case DW_OP_GNU_const_type:
21603           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21604           break;
21605         case DW_OP_GNU_entry_value:
21606           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21607           continue;
21608         default:
21609           continue;
21610         }
21611       gcc_assert (base_type->die_parent == comp_unit_die ());
21612       if (base_type->die_mark)
21613         base_type->die_mark++;
21614       else
21615         {
21616           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21617           base_type->die_mark = 1;
21618         }
21619     }
21620 }
21621
21622 /* Comparison function for sorting marked base types.  */
21623
21624 static int
21625 base_type_cmp (const void *x, const void *y)
21626 {
21627   dw_die_ref dx = *(const dw_die_ref *) x;
21628   dw_die_ref dy = *(const dw_die_ref *) y;
21629   unsigned int byte_size1, byte_size2;
21630   unsigned int encoding1, encoding2;
21631   if (dx->die_mark > dy->die_mark)
21632     return -1;
21633   if (dx->die_mark < dy->die_mark)
21634     return 1;
21635   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21636   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21637   if (byte_size1 < byte_size2)
21638     return 1;
21639   if (byte_size1 > byte_size2)
21640     return -1;
21641   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21642   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21643   if (encoding1 < encoding2)
21644     return 1;
21645   if (encoding1 > encoding2)
21646     return -1;
21647   return 0;
21648 }
21649
21650 /* Move base types marked by mark_base_types as early as possible
21651    in the CU, sorted by decreasing usage count both to make the
21652    uleb128 references as small as possible and to make sure they
21653    will have die_offset already computed by calc_die_sizes when
21654    sizes of typed stack loc ops is computed.  */
21655
21656 static void
21657 move_marked_base_types (void)
21658 {
21659   unsigned int i;
21660   dw_die_ref base_type, die, c;
21661
21662   if (VEC_empty (dw_die_ref, base_types))
21663     return;
21664
21665   /* Sort by decreasing usage count, they will be added again in that
21666      order later on.  */
21667   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21668   die = comp_unit_die ();
21669   c = die->die_child;
21670   do
21671     {
21672       dw_die_ref prev = c;
21673       c = c->die_sib;
21674       while (c->die_mark)
21675         {
21676           remove_child_with_prev (c, prev);
21677           /* As base types got marked, there must be at least
21678              one node other than DW_TAG_base_type.  */
21679           gcc_assert (c != c->die_sib);
21680           c = c->die_sib;
21681         }
21682     }
21683   while (c != die->die_child);
21684   gcc_assert (die->die_child);
21685   c = die->die_child;
21686   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21687     {
21688       base_type->die_mark = 0;
21689       base_type->die_sib = c->die_sib;
21690       c->die_sib = base_type;
21691       c = base_type;
21692     }
21693 }
21694
21695 /* Helper function for resolve_addr, attempt to resolve
21696    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21697    SYMBOL_REFs refer to variables emitted in the current CU.  */
21698
21699 static int
21700 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21701 {
21702   rtx rtl = *addr;
21703
21704   if (GET_CODE (rtl) == CONST_STRING)
21705     {
21706       size_t len = strlen (XSTR (rtl, 0)) + 1;
21707       tree t = build_string (len, XSTR (rtl, 0));
21708       tree tlen = size_int (len - 1);
21709       TREE_TYPE (t)
21710         = build_array_type (char_type_node, build_index_type (tlen));
21711       rtl = lookup_constant_def (t);
21712       if (!rtl || !MEM_P (rtl))
21713         return 1;
21714       rtl = XEXP (rtl, 0);
21715       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21716       *addr = rtl;
21717       return 0;
21718     }
21719
21720   if (GET_CODE (rtl) == SYMBOL_REF
21721       && SYMBOL_REF_DECL (rtl))
21722     {
21723       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21724         {
21725           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21726             return 1;
21727         }
21728       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21729         return 1;
21730     }
21731
21732   if (GET_CODE (rtl) == CONST
21733       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21734     return 1;
21735
21736   return 0;
21737 }
21738
21739 /* Helper function for resolve_addr, handle one location
21740    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21741    the location list couldn't be resolved.  */
21742
21743 static bool
21744 resolve_addr_in_expr (dw_loc_descr_ref loc)
21745 {
21746   dw_loc_descr_ref keep = NULL;
21747   for (; loc; loc = loc->dw_loc_next)
21748     switch (loc->dw_loc_opc)
21749       {
21750       case DW_OP_addr:
21751         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21752           return false;
21753         break;
21754       case DW_OP_const4u:
21755       case DW_OP_const8u:
21756         if (loc->dtprel
21757             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21758           return false;
21759         break;
21760       case DW_OP_plus_uconst:
21761         if (size_of_loc_descr (loc)
21762             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21763               + 1
21764             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21765           {
21766             dw_loc_descr_ref repl
21767               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21768             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21769             add_loc_descr (&repl, loc->dw_loc_next);
21770             *loc = *repl;
21771           }
21772         break;
21773       case DW_OP_implicit_value:
21774         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21775             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21776           return false;
21777         break;
21778       case DW_OP_GNU_implicit_pointer:
21779       case DW_OP_GNU_parameter_ref:
21780         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21781           {
21782             dw_die_ref ref
21783               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21784             if (ref == NULL)
21785               return false;
21786             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21787             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21788             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21789           }
21790         break;
21791       case DW_OP_GNU_const_type:
21792       case DW_OP_GNU_regval_type:
21793       case DW_OP_GNU_deref_type:
21794       case DW_OP_GNU_convert:
21795       case DW_OP_GNU_reinterpret:
21796         while (loc->dw_loc_next
21797                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21798           {
21799             dw_die_ref base1, base2;
21800             unsigned enc1, enc2, size1, size2;
21801             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21802                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21803               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21804             else if (loc->dw_loc_oprnd1.val_class
21805                      == dw_val_class_unsigned_const)
21806               break;
21807             else
21808               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21809             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21810                 == dw_val_class_unsigned_const)
21811               break;
21812             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21813             gcc_assert (base1->die_tag == DW_TAG_base_type
21814                         && base2->die_tag == DW_TAG_base_type);
21815             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21816             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21817             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21818             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21819             if (size1 == size2
21820                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21821                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21822                      && loc != keep)
21823                     || enc1 == enc2))
21824               {
21825                 /* Optimize away next DW_OP_GNU_convert after
21826                    adjusting LOC's base type die reference.  */
21827                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21828                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21829                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21830                 else
21831                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21832                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21833                 continue;
21834               }
21835             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21836                point typed stack entry.  */
21837             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21838               keep = loc->dw_loc_next;
21839             break;
21840           }
21841         break;
21842       default:
21843         break;
21844       }
21845   return true;
21846 }
21847
21848 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21849    an address in .rodata section if the string literal is emitted there,
21850    or remove the containing location list or replace DW_AT_const_value
21851    with DW_AT_location and empty location expression, if it isn't found
21852    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21853    to something that has been emitted in the current CU.  */
21854
21855 static void
21856 resolve_addr (dw_die_ref die)
21857 {
21858   dw_die_ref c;
21859   dw_attr_ref a;
21860   dw_loc_list_ref *curr, *start, loc;
21861   unsigned ix;
21862
21863   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21864     switch (AT_class (a))
21865       {
21866       case dw_val_class_loc_list:
21867         start = curr = AT_loc_list_ptr (a);
21868         loc = *curr;
21869         gcc_assert (loc);
21870         /* The same list can be referenced more than once.  See if we have
21871            already recorded the result from a previous pass.  */
21872         if (loc->replaced)
21873           *curr = loc->dw_loc_next;
21874         else if (!loc->resolved_addr)
21875           {
21876             /* As things stand, we do not expect or allow one die to
21877                reference a suffix of another die's location list chain.
21878                References must be identical or completely separate.
21879                There is therefore no need to cache the result of this
21880                pass on any list other than the first; doing so
21881                would lead to unnecessary writes.  */
21882             while (*curr)
21883               {
21884                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21885                 if (!resolve_addr_in_expr ((*curr)->expr))
21886                   {
21887                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21888                     if (next && (*curr)->ll_symbol)
21889                       {
21890                         gcc_assert (!next->ll_symbol);
21891                         next->ll_symbol = (*curr)->ll_symbol;
21892                       }
21893                     *curr = next;
21894                   }
21895                 else
21896                   {
21897                     mark_base_types ((*curr)->expr);
21898                     curr = &(*curr)->dw_loc_next;
21899                   }
21900               }
21901             if (loc == *start)
21902               loc->resolved_addr = 1;
21903             else
21904               {
21905                 loc->replaced = 1;
21906                 loc->dw_loc_next = *start;
21907               }
21908           }
21909         if (!*start)
21910           {
21911             remove_AT (die, a->dw_attr);
21912             ix--;
21913           }
21914         break;
21915       case dw_val_class_loc:
21916         {
21917           dw_loc_descr_ref l = AT_loc (a);
21918           /* For -gdwarf-2 don't attempt to optimize
21919              DW_AT_data_member_location containing
21920              DW_OP_plus_uconst - older consumers might
21921              rely on it being that op instead of a more complex,
21922              but shorter, location description.  */
21923           if ((dwarf_version > 2
21924                || a->dw_attr != DW_AT_data_member_location
21925                || l == NULL
21926                || l->dw_loc_opc != DW_OP_plus_uconst
21927                || l->dw_loc_next != NULL)
21928               && !resolve_addr_in_expr (l))
21929             {
21930               remove_AT (die, a->dw_attr);
21931               ix--;
21932             }
21933           else
21934             mark_base_types (l);
21935         }
21936         break;
21937       case dw_val_class_addr:
21938         if (a->dw_attr == DW_AT_const_value
21939             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21940           {
21941             remove_AT (die, a->dw_attr);
21942             ix--;
21943           }
21944         if (die->die_tag == DW_TAG_GNU_call_site
21945             && a->dw_attr == DW_AT_abstract_origin)
21946           {
21947             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21948             dw_die_ref tdie = lookup_decl_die (tdecl);
21949             if (tdie == NULL
21950                 && DECL_EXTERNAL (tdecl)
21951                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21952               {
21953                 force_decl_die (tdecl);
21954                 tdie = lookup_decl_die (tdecl);
21955               }
21956             if (tdie)
21957               {
21958                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21959                 a->dw_attr_val.v.val_die_ref.die = tdie;
21960                 a->dw_attr_val.v.val_die_ref.external = 0;
21961               }
21962             else
21963               {
21964                 remove_AT (die, a->dw_attr);
21965                 ix--;
21966               }
21967           }
21968         break;
21969       default:
21970         break;
21971       }
21972
21973   FOR_EACH_CHILD (die, c, resolve_addr (c));
21974 }
21975 \f
21976 /* Helper routines for optimize_location_lists.
21977    This pass tries to share identical local lists in .debug_loc
21978    section.  */
21979
21980 /* Iteratively hash operands of LOC opcode.  */
21981
21982 static inline hashval_t
21983 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21984 {
21985   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21986   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21987
21988   switch (loc->dw_loc_opc)
21989     {
21990     case DW_OP_const4u:
21991     case DW_OP_const8u:
21992       if (loc->dtprel)
21993         goto hash_addr;
21994       /* FALLTHRU */
21995     case DW_OP_const1u:
21996     case DW_OP_const1s:
21997     case DW_OP_const2u:
21998     case DW_OP_const2s:
21999     case DW_OP_const4s:
22000     case DW_OP_const8s:
22001     case DW_OP_constu:
22002     case DW_OP_consts:
22003     case DW_OP_pick:
22004     case DW_OP_plus_uconst:
22005     case DW_OP_breg0:
22006     case DW_OP_breg1:
22007     case DW_OP_breg2:
22008     case DW_OP_breg3:
22009     case DW_OP_breg4:
22010     case DW_OP_breg5:
22011     case DW_OP_breg6:
22012     case DW_OP_breg7:
22013     case DW_OP_breg8:
22014     case DW_OP_breg9:
22015     case DW_OP_breg10:
22016     case DW_OP_breg11:
22017     case DW_OP_breg12:
22018     case DW_OP_breg13:
22019     case DW_OP_breg14:
22020     case DW_OP_breg15:
22021     case DW_OP_breg16:
22022     case DW_OP_breg17:
22023     case DW_OP_breg18:
22024     case DW_OP_breg19:
22025     case DW_OP_breg20:
22026     case DW_OP_breg21:
22027     case DW_OP_breg22:
22028     case DW_OP_breg23:
22029     case DW_OP_breg24:
22030     case DW_OP_breg25:
22031     case DW_OP_breg26:
22032     case DW_OP_breg27:
22033     case DW_OP_breg28:
22034     case DW_OP_breg29:
22035     case DW_OP_breg30:
22036     case DW_OP_breg31:
22037     case DW_OP_regx:
22038     case DW_OP_fbreg:
22039     case DW_OP_piece:
22040     case DW_OP_deref_size:
22041     case DW_OP_xderef_size:
22042       hash = iterative_hash_object (val1->v.val_int, hash);
22043       break;
22044     case DW_OP_skip:
22045     case DW_OP_bra:
22046       {
22047         int offset;
22048
22049         gcc_assert (val1->val_class == dw_val_class_loc);
22050         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22051         hash = iterative_hash_object (offset, hash);
22052       }
22053       break;
22054     case DW_OP_implicit_value:
22055       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22056       switch (val2->val_class)
22057         {
22058         case dw_val_class_const:
22059           hash = iterative_hash_object (val2->v.val_int, hash);
22060           break;
22061         case dw_val_class_vec:
22062           {
22063             unsigned int elt_size = val2->v.val_vec.elt_size;
22064             unsigned int len = val2->v.val_vec.length;
22065
22066             hash = iterative_hash_object (elt_size, hash);
22067             hash = iterative_hash_object (len, hash);
22068             hash = iterative_hash (val2->v.val_vec.array,
22069                                    len * elt_size, hash);
22070           }
22071           break;
22072         case dw_val_class_const_double:
22073           hash = iterative_hash_object (val2->v.val_double.low, hash);
22074           hash = iterative_hash_object (val2->v.val_double.high, hash);
22075           break;
22076         case dw_val_class_addr:
22077           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22078           break;
22079         default:
22080           gcc_unreachable ();
22081         }
22082       break;
22083     case DW_OP_bregx:
22084     case DW_OP_bit_piece:
22085       hash = iterative_hash_object (val1->v.val_int, hash);
22086       hash = iterative_hash_object (val2->v.val_int, hash);
22087       break;
22088     case DW_OP_addr:
22089     hash_addr:
22090       if (loc->dtprel)
22091         {
22092           unsigned char dtprel = 0xd1;
22093           hash = iterative_hash_object (dtprel, hash);
22094         }
22095       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22096       break;
22097     case DW_OP_GNU_implicit_pointer:
22098       hash = iterative_hash_object (val2->v.val_int, hash);
22099       break;
22100     case DW_OP_GNU_entry_value:
22101       hash = hash_loc_operands (val1->v.val_loc, hash);
22102       break;
22103     case DW_OP_GNU_regval_type:
22104     case DW_OP_GNU_deref_type:
22105       {
22106         unsigned int byte_size
22107           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22108         unsigned int encoding
22109           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22110         hash = iterative_hash_object (val1->v.val_int, hash);
22111         hash = iterative_hash_object (byte_size, hash);
22112         hash = iterative_hash_object (encoding, hash);
22113       }
22114       break;
22115     case DW_OP_GNU_convert:
22116     case DW_OP_GNU_reinterpret:
22117       if (val1->val_class == dw_val_class_unsigned_const)
22118         {
22119           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22120           break;
22121         }
22122       /* FALLTHRU */
22123     case DW_OP_GNU_const_type:
22124       {
22125         unsigned int byte_size
22126           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22127         unsigned int encoding
22128           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22129         hash = iterative_hash_object (byte_size, hash);
22130         hash = iterative_hash_object (encoding, hash);
22131         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22132           break;
22133         hash = iterative_hash_object (val2->val_class, hash);
22134         switch (val2->val_class)
22135           {
22136           case dw_val_class_const:
22137             hash = iterative_hash_object (val2->v.val_int, hash);
22138             break;
22139           case dw_val_class_vec:
22140             {
22141               unsigned int elt_size = val2->v.val_vec.elt_size;
22142               unsigned int len = val2->v.val_vec.length;
22143
22144               hash = iterative_hash_object (elt_size, hash);
22145               hash = iterative_hash_object (len, hash);
22146               hash = iterative_hash (val2->v.val_vec.array,
22147                                      len * elt_size, hash);
22148             }
22149             break;
22150           case dw_val_class_const_double:
22151             hash = iterative_hash_object (val2->v.val_double.low, hash);
22152             hash = iterative_hash_object (val2->v.val_double.high, hash);
22153             break;
22154           default:
22155             gcc_unreachable ();
22156           }
22157       }
22158       break;
22159
22160     default:
22161       /* Other codes have no operands.  */
22162       break;
22163     }
22164   return hash;
22165 }
22166
22167 /* Iteratively hash the whole DWARF location expression LOC.  */
22168
22169 static inline hashval_t
22170 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22171 {
22172   dw_loc_descr_ref l;
22173   bool sizes_computed = false;
22174   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22175   size_of_locs (loc);
22176
22177   for (l = loc; l != NULL; l = l->dw_loc_next)
22178     {
22179       enum dwarf_location_atom opc = l->dw_loc_opc;
22180       hash = iterative_hash_object (opc, hash);
22181       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22182         {
22183           size_of_locs (loc);
22184           sizes_computed = true;
22185         }
22186       hash = hash_loc_operands (l, hash);
22187     }
22188   return hash;
22189 }
22190
22191 /* Compute hash of the whole location list LIST_HEAD.  */
22192
22193 static inline void
22194 hash_loc_list (dw_loc_list_ref list_head)
22195 {
22196   dw_loc_list_ref curr = list_head;
22197   hashval_t hash = 0;
22198
22199   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22200     {
22201       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22202       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22203       if (curr->section)
22204         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22205                                hash);
22206       hash = hash_locs (curr->expr, hash);
22207     }
22208   list_head->hash = hash;
22209 }
22210
22211 /* Return true if X and Y opcodes have the same operands.  */
22212
22213 static inline bool
22214 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22215 {
22216   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22217   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22218   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22219   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22220
22221   switch (x->dw_loc_opc)
22222     {
22223     case DW_OP_const4u:
22224     case DW_OP_const8u:
22225       if (x->dtprel)
22226         goto hash_addr;
22227       /* FALLTHRU */
22228     case DW_OP_const1u:
22229     case DW_OP_const1s:
22230     case DW_OP_const2u:
22231     case DW_OP_const2s:
22232     case DW_OP_const4s:
22233     case DW_OP_const8s:
22234     case DW_OP_constu:
22235     case DW_OP_consts:
22236     case DW_OP_pick:
22237     case DW_OP_plus_uconst:
22238     case DW_OP_breg0:
22239     case DW_OP_breg1:
22240     case DW_OP_breg2:
22241     case DW_OP_breg3:
22242     case DW_OP_breg4:
22243     case DW_OP_breg5:
22244     case DW_OP_breg6:
22245     case DW_OP_breg7:
22246     case DW_OP_breg8:
22247     case DW_OP_breg9:
22248     case DW_OP_breg10:
22249     case DW_OP_breg11:
22250     case DW_OP_breg12:
22251     case DW_OP_breg13:
22252     case DW_OP_breg14:
22253     case DW_OP_breg15:
22254     case DW_OP_breg16:
22255     case DW_OP_breg17:
22256     case DW_OP_breg18:
22257     case DW_OP_breg19:
22258     case DW_OP_breg20:
22259     case DW_OP_breg21:
22260     case DW_OP_breg22:
22261     case DW_OP_breg23:
22262     case DW_OP_breg24:
22263     case DW_OP_breg25:
22264     case DW_OP_breg26:
22265     case DW_OP_breg27:
22266     case DW_OP_breg28:
22267     case DW_OP_breg29:
22268     case DW_OP_breg30:
22269     case DW_OP_breg31:
22270     case DW_OP_regx:
22271     case DW_OP_fbreg:
22272     case DW_OP_piece:
22273     case DW_OP_deref_size:
22274     case DW_OP_xderef_size:
22275       return valx1->v.val_int == valy1->v.val_int;
22276     case DW_OP_skip:
22277     case DW_OP_bra:
22278       gcc_assert (valx1->val_class == dw_val_class_loc
22279                   && valy1->val_class == dw_val_class_loc
22280                   && x->dw_loc_addr == y->dw_loc_addr);
22281       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22282     case DW_OP_implicit_value:
22283       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22284           || valx2->val_class != valy2->val_class)
22285         return false;
22286       switch (valx2->val_class)
22287         {
22288         case dw_val_class_const:
22289           return valx2->v.val_int == valy2->v.val_int;
22290         case dw_val_class_vec:
22291           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22292                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22293                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22294                             valx2->v.val_vec.elt_size
22295                             * valx2->v.val_vec.length) == 0;
22296         case dw_val_class_const_double:
22297           return valx2->v.val_double.low == valy2->v.val_double.low
22298                  && valx2->v.val_double.high == valy2->v.val_double.high;
22299         case dw_val_class_addr:
22300           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22301         default:
22302           gcc_unreachable ();
22303         }
22304     case DW_OP_bregx:
22305     case DW_OP_bit_piece:
22306       return valx1->v.val_int == valy1->v.val_int
22307              && valx2->v.val_int == valy2->v.val_int;
22308     case DW_OP_addr:
22309     hash_addr:
22310       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22311     case DW_OP_GNU_implicit_pointer:
22312       return valx1->val_class == dw_val_class_die_ref
22313              && valx1->val_class == valy1->val_class
22314              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22315              && valx2->v.val_int == valy2->v.val_int;
22316     case DW_OP_GNU_entry_value:
22317       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22318     case DW_OP_GNU_const_type:
22319       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22320           || valx2->val_class != valy2->val_class)
22321         return false;
22322       switch (valx2->val_class)
22323         {
22324         case dw_val_class_const:
22325           return valx2->v.val_int == valy2->v.val_int;
22326         case dw_val_class_vec:
22327           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22328                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22329                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22330                             valx2->v.val_vec.elt_size
22331                             * valx2->v.val_vec.length) == 0;
22332         case dw_val_class_const_double:
22333           return valx2->v.val_double.low == valy2->v.val_double.low
22334                  && valx2->v.val_double.high == valy2->v.val_double.high;
22335         default:
22336           gcc_unreachable ();
22337         }
22338     case DW_OP_GNU_regval_type:
22339     case DW_OP_GNU_deref_type:
22340       return valx1->v.val_int == valy1->v.val_int
22341              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22342     case DW_OP_GNU_convert:
22343     case DW_OP_GNU_reinterpret:
22344       if (valx1->val_class != valy1->val_class)
22345         return false;
22346       if (valx1->val_class == dw_val_class_unsigned_const)
22347         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22348       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22349     case DW_OP_GNU_parameter_ref:
22350       return valx1->val_class == dw_val_class_die_ref
22351              && valx1->val_class == valy1->val_class
22352              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22353     default:
22354       /* Other codes have no operands.  */
22355       return true;
22356     }
22357 }
22358
22359 /* Return true if DWARF location expressions X and Y are the same.  */
22360
22361 static inline bool
22362 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22363 {
22364   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22365     if (x->dw_loc_opc != y->dw_loc_opc
22366         || x->dtprel != y->dtprel
22367         || !compare_loc_operands (x, y))
22368       break;
22369   return x == NULL && y == NULL;
22370 }
22371
22372 /* Return precomputed hash of location list X.  */
22373
22374 static hashval_t
22375 loc_list_hash (const void *x)
22376 {
22377   return ((const struct dw_loc_list_struct *) x)->hash;
22378 }
22379
22380 /* Return 1 if location lists X and Y are the same.  */
22381
22382 static int
22383 loc_list_eq (const void *x, const void *y)
22384 {
22385   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22386   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22387   if (a == b)
22388     return 1;
22389   if (a->hash != b->hash)
22390     return 0;
22391   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22392     if (strcmp (a->begin, b->begin) != 0
22393         || strcmp (a->end, b->end) != 0
22394         || (a->section == NULL) != (b->section == NULL)
22395         || (a->section && strcmp (a->section, b->section) != 0)
22396         || !compare_locs (a->expr, b->expr))
22397       break;
22398   return a == NULL && b == NULL;
22399 }
22400
22401 /* Recursively optimize location lists referenced from DIE
22402    children and share them whenever possible.  */
22403
22404 static void
22405 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22406 {
22407   dw_die_ref c;
22408   dw_attr_ref a;
22409   unsigned ix;
22410   void **slot;
22411
22412   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22413     if (AT_class (a) == dw_val_class_loc_list)
22414       {
22415         dw_loc_list_ref list = AT_loc_list (a);
22416         /* TODO: perform some optimizations here, before hashing
22417            it and storing into the hash table.  */
22418         hash_loc_list (list);
22419         slot = htab_find_slot_with_hash (htab, list, list->hash,
22420                                          INSERT);
22421         if (*slot == NULL)
22422           *slot = (void *) list;
22423         else
22424           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22425       }
22426
22427   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22428 }
22429
22430 /* Optimize location lists referenced from DIE
22431    children and share them whenever possible.  */
22432
22433 static void
22434 optimize_location_lists (dw_die_ref die)
22435 {
22436   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22437   optimize_location_lists_1 (die, htab);
22438   htab_delete (htab);
22439 }
22440 \f
22441 /* Output stuff that dwarf requires at the end of every file,
22442    and generate the DWARF-2 debugging info.  */
22443
22444 static void
22445 dwarf2out_finish (const char *filename)
22446 {
22447   limbo_die_node *node, *next_node;
22448   comdat_type_node *ctnode;
22449   htab_t comdat_type_table;
22450   unsigned int i;
22451
22452   /* PCH might result in DW_AT_producer string being restored from the
22453      header compilation, fix it up if needed.  */
22454   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22455   if (strcmp (AT_string (producer), producer_string) != 0)
22456     {
22457       struct indirect_string_node *node = find_AT_string (producer_string);
22458       producer->dw_attr_val.v.val_str = node;
22459     }
22460
22461   gen_scheduled_generic_parms_dies ();
22462   gen_remaining_tmpl_value_param_die_attribute ();
22463
22464   /* Add the name for the main input file now.  We delayed this from
22465      dwarf2out_init to avoid complications with PCH.  */
22466   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22467   if (!IS_ABSOLUTE_PATH (filename))
22468     add_comp_dir_attribute (comp_unit_die ());
22469   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22470     {
22471       bool p = false;
22472       htab_traverse (file_table, file_table_relative_p, &p);
22473       if (p)
22474         add_comp_dir_attribute (comp_unit_die ());
22475     }
22476
22477   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22478     {
22479       add_location_or_const_value_attribute (
22480         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22481         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22482         false,
22483         DW_AT_location);
22484     }
22485
22486   /* Traverse the limbo die list, and add parent/child links.  The only
22487      dies without parents that should be here are concrete instances of
22488      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22489      For concrete instances, we can get the parent die from the abstract
22490      instance.  */
22491   for (node = limbo_die_list; node; node = next_node)
22492     {
22493       dw_die_ref die = node->die;
22494       next_node = node->next;
22495
22496       if (die->die_parent == NULL)
22497         {
22498           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22499
22500           if (origin && origin->die_parent)
22501             add_child_die (origin->die_parent, die);
22502           else if (is_cu_die (die))
22503             ;
22504           else if (seen_error ())
22505             /* It's OK to be confused by errors in the input.  */
22506             add_child_die (comp_unit_die (), die);
22507           else
22508             {
22509               /* In certain situations, the lexical block containing a
22510                  nested function can be optimized away, which results
22511                  in the nested function die being orphaned.  Likewise
22512                  with the return type of that nested function.  Force
22513                  this to be a child of the containing function.
22514
22515                  It may happen that even the containing function got fully
22516                  inlined and optimized out.  In that case we are lost and
22517                  assign the empty child.  This should not be big issue as
22518                  the function is likely unreachable too.  */
22519               tree context = NULL_TREE;
22520
22521               gcc_assert (node->created_for);
22522
22523               if (DECL_P (node->created_for))
22524                 context = DECL_CONTEXT (node->created_for);
22525               else if (TYPE_P (node->created_for))
22526                 context = TYPE_CONTEXT (node->created_for);
22527
22528               origin = get_context_die (context);
22529               add_child_die (origin, die);
22530             }
22531         }
22532     }
22533
22534   limbo_die_list = NULL;
22535
22536 #if ENABLE_ASSERT_CHECKING
22537   {
22538     dw_die_ref die = comp_unit_die (), c;
22539     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22540   }
22541 #endif
22542   resolve_addr (comp_unit_die ());
22543   move_marked_base_types ();
22544
22545   for (node = deferred_asm_name; node; node = node->next)
22546     {
22547       tree decl = node->created_for;
22548       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22549         {
22550           add_linkage_attr (node->die, decl);
22551           move_linkage_attr (node->die);
22552         }
22553     }
22554
22555   deferred_asm_name = NULL;
22556
22557   /* Walk through the list of incomplete types again, trying once more to
22558      emit full debugging info for them.  */
22559   retry_incomplete_types ();
22560
22561   if (flag_eliminate_unused_debug_types)
22562     prune_unused_types ();
22563
22564   /* Generate separate CUs for each of the include files we've seen.
22565      They will go into limbo_die_list.  */
22566   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22567     break_out_includes (comp_unit_die ());
22568
22569   /* Generate separate COMDAT sections for type DIEs. */
22570   if (use_debug_types)
22571     {
22572       break_out_comdat_types (comp_unit_die ());
22573
22574       /* Each new type_unit DIE was added to the limbo die list when created.
22575          Since these have all been added to comdat_type_list, clear the
22576          limbo die list.  */
22577       limbo_die_list = NULL;
22578
22579       /* For each new comdat type unit, copy declarations for incomplete
22580          types to make the new unit self-contained (i.e., no direct
22581          references to the main compile unit).  */
22582       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22583         copy_decls_for_unworthy_types (ctnode->root_die);
22584       copy_decls_for_unworthy_types (comp_unit_die ());
22585
22586       /* In the process of copying declarations from one unit to another,
22587          we may have left some declarations behind that are no longer
22588          referenced.  Prune them.  */
22589       prune_unused_types ();
22590     }
22591
22592   /* Traverse the DIE's and add add sibling attributes to those DIE's
22593      that have children.  */
22594   add_sibling_attributes (comp_unit_die ());
22595   for (node = limbo_die_list; node; node = node->next)
22596     add_sibling_attributes (node->die);
22597   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22598     add_sibling_attributes (ctnode->root_die);
22599
22600   /* Output a terminator label for the .text section.  */
22601   switch_to_section (text_section);
22602   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22603   if (cold_text_section)
22604     {
22605       switch_to_section (cold_text_section);
22606       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22607     }
22608
22609   /* We can only use the low/high_pc attributes if all of the code was
22610      in .text.  */
22611   if (!have_multiple_function_sections 
22612       || (dwarf_version < 3 && dwarf_strict))
22613     {
22614       /* Don't add if the CU has no associated code.  */
22615       if (text_section_used)
22616         {
22617           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22618           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22619         }
22620     }
22621   else
22622     {
22623       unsigned fde_idx;
22624       dw_fde_ref fde;
22625       bool range_list_added = false;
22626
22627       if (text_section_used)
22628         add_ranges_by_labels (comp_unit_die (), text_section_label,
22629                               text_end_label, &range_list_added);
22630       if (cold_text_section_used)
22631         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22632                               cold_end_label, &range_list_added);
22633
22634       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22635         {
22636           if (!fde->in_std_section)
22637             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22638                                   fde->dw_fde_end, &range_list_added);
22639           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22640             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22641                                   fde->dw_fde_second_end, &range_list_added);
22642         }
22643
22644       if (range_list_added)
22645         {
22646           /* We need to give .debug_loc and .debug_ranges an appropriate
22647              "base address".  Use zero so that these addresses become
22648              absolute.  Historically, we've emitted the unexpected
22649              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22650              Emit both to give time for other tools to adapt.  */
22651           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22652           if (! dwarf_strict && dwarf_version < 4)
22653             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22654
22655           add_ranges (NULL);
22656         }
22657     }
22658
22659   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22660     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22661                     debug_line_section_label);
22662
22663   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22664     add_AT_macptr (comp_unit_die (),
22665                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22666                    macinfo_section_label);
22667
22668   if (have_location_lists)
22669     optimize_location_lists (comp_unit_die ());
22670
22671   /* Output all of the compilation units.  We put the main one last so that
22672      the offsets are available to output_pubnames.  */
22673   for (node = limbo_die_list; node; node = node->next)
22674     output_comp_unit (node->die, 0);
22675
22676   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22677   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22678     {
22679       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22680
22681       /* Don't output duplicate types.  */
22682       if (*slot != HTAB_EMPTY_ENTRY)
22683         continue;
22684
22685       /* Add a pointer to the line table for the main compilation unit
22686          so that the debugger can make sense of DW_AT_decl_file
22687          attributes.  */
22688       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22689         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22690                         debug_line_section_label);
22691
22692       output_comdat_type_unit (ctnode);
22693       *slot = ctnode;
22694     }
22695   htab_delete (comdat_type_table);
22696
22697   /* Output the main compilation unit if non-empty or if .debug_macinfo
22698      will be emitted.  */
22699   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22700
22701   /* Output the abbreviation table.  */
22702   if (abbrev_die_table_in_use != 1)
22703     {
22704       switch_to_section (debug_abbrev_section);
22705       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22706       output_abbrev_section ();
22707     }
22708
22709   /* Output location list section if necessary.  */
22710   if (have_location_lists)
22711     {
22712       /* Output the location lists info.  */
22713       switch_to_section (debug_loc_section);
22714       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22715                                    DEBUG_LOC_SECTION_LABEL, 0);
22716       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22717       output_location_lists (comp_unit_die ());
22718     }
22719
22720   /* Output public names table if necessary.  */
22721   if (!VEC_empty (pubname_entry, pubname_table))
22722     {
22723       gcc_assert (info_section_emitted);
22724       switch_to_section (debug_pubnames_section);
22725       output_pubnames (pubname_table);
22726     }
22727
22728   /* Output public types table if necessary.  */
22729   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22730      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22731      simply won't look for the section.  */
22732   if (!VEC_empty (pubname_entry, pubtype_table))
22733     {
22734       bool empty = false;
22735       
22736       if (flag_eliminate_unused_debug_types)
22737         {
22738           /* The pubtypes table might be emptied by pruning unused items.  */
22739           unsigned i;
22740           pubname_ref p;
22741           empty = true;
22742           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22743             if (p->die->die_offset != 0)
22744               {
22745                 empty = false;
22746                 break;
22747               }
22748         }
22749       if (!empty)
22750         {
22751           gcc_assert (info_section_emitted);
22752           switch_to_section (debug_pubtypes_section);
22753           output_pubnames (pubtype_table);
22754         }
22755     }
22756
22757   /* Output the address range information if a CU (.debug_info section)
22758      was emitted.  We output an empty table even if we had no functions
22759      to put in it.  This because the consumer has no way to tell the
22760      difference between an empty table that we omitted and failure to
22761      generate a table that would have contained data.  */
22762   if (info_section_emitted)
22763     {
22764       unsigned long aranges_length = size_of_aranges ();
22765
22766       switch_to_section (debug_aranges_section);
22767       output_aranges (aranges_length);
22768     }
22769
22770   /* Output ranges section if necessary.  */
22771   if (ranges_table_in_use)
22772     {
22773       switch_to_section (debug_ranges_section);
22774       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22775       output_ranges ();
22776     }
22777
22778   /* Have to end the macro section.  */
22779   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22780     {
22781       switch_to_section (debug_macinfo_section);
22782       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22783       if (!VEC_empty (macinfo_entry, macinfo_table))
22784         output_macinfo ();
22785       dw2_asm_output_data (1, 0, "End compilation unit");
22786     }
22787
22788   /* Output the source line correspondence table.  We must do this
22789      even if there is no line information.  Otherwise, on an empty
22790      translation unit, we will generate a present, but empty,
22791      .debug_info section.  IRIX 6.5 `nm' will then complain when
22792      examining the file.  This is done late so that any filenames
22793      used by the debug_info section are marked as 'used'.  */
22794   switch_to_section (debug_line_section);
22795   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22796   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22797     output_line_info ();
22798
22799   /* If we emitted any DW_FORM_strp form attribute, output the string
22800      table too.  */
22801   if (debug_str_hash)
22802     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22803 }
22804
22805 #include "gt-dwarf2out.h"