OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97 #include "opts.h"
98
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_insn;
101 static rtx cached_next_real_insn;
102
103 #ifdef VMS_DEBUGGING_INFO
104 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
105
106 /* Define this macro to be a nonzero value if the directory specifications
107     which are output in the debug info should end with a separator.  */
108 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
109 /* Define this macro to evaluate to a nonzero value if GCC should refrain
110    from generating indirect strings in DWARF2 debug information, for instance
111    if your target is stuck with an old version of GDB that is unable to
112    process them properly or uses VMS Debug.  */
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
114 #else
115 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
116 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
117 #endif
118
119 /* ??? Poison these here until it can be done generically.  They've been
120    totally replaced in this file; make sure it stays that way.  */
121 #undef DWARF2_UNWIND_INFO
122 #undef DWARF2_FRAME_INFO
123 #if (GCC_VERSION >= 3000)
124  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
125 #endif
126
127 /* The size of the target's pointer type.  */
128 #ifndef PTR_SIZE
129 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
130 #endif
131
132 /* Array of RTXes referenced by the debugging information, which therefore
133    must be kept around forever.  */
134 static GTY(()) VEC(rtx,gc) *used_rtx_array;
135
136 /* A pointer to the base of a list of incomplete types which might be
137    completed at some later time.  incomplete_types_list needs to be a
138    VEC(tree,gc) because we want to tell the garbage collector about
139    it.  */
140 static GTY(()) VEC(tree,gc) *incomplete_types;
141
142 /* A pointer to the base of a table of references to declaration
143    scopes.  This table is a display which tracks the nesting
144    of declaration scopes at the current scope and containing
145    scopes.  This table is used to find the proper place to
146    define type declaration DIE's.  */
147 static GTY(()) VEC(tree,gc) *decl_scope_table;
148
149 /* Pointers to various DWARF2 sections.  */
150 static GTY(()) section *debug_info_section;
151 static GTY(()) section *debug_abbrev_section;
152 static GTY(()) section *debug_aranges_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_loc_section;
156 static GTY(()) section *debug_pubnames_section;
157 static GTY(()) section *debug_pubtypes_section;
158 static GTY(()) section *debug_str_section;
159 static GTY(()) section *debug_ranges_section;
160 static GTY(()) section *debug_frame_section;
161
162 /* Maximum size (in bytes) of an artificially generated label.  */
163 #define MAX_ARTIFICIAL_LABEL_BYTES      30
164
165 /* According to the (draft) DWARF 3 specification, the initial length
166    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
167    bytes are 0xffffffff, followed by the length stored in the next 8
168    bytes.
169
170    However, the SGI/MIPS ABI uses an initial length which is equal to
171    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
172
173 #ifndef DWARF_INITIAL_LENGTH_SIZE
174 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
175 #endif
176
177 /* Round SIZE up to the nearest BOUNDARY.  */
178 #define DWARF_ROUND(SIZE,BOUNDARY) \
179   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
180
181 /* CIE identifier.  */
182 #if HOST_BITS_PER_WIDE_INT >= 64
183 #define DWARF_CIE_ID \
184   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
185 #else
186 #define DWARF_CIE_ID DW_CIE_ID
187 #endif
188
189 DEF_VEC_P (dw_fde_ref);
190 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
191
192 /* A vector for a table that contains frame description
193    information for each routine.  */
194 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
195
196 struct GTY(()) indirect_string_node {
197   const char *str;
198   unsigned int refcount;
199   enum dwarf_form form;
200   char *label;
201 };
202
203 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
204
205 static GTY(()) int dw2_string_counter;
206
207 /* True if the compilation unit places functions in more than one section.  */
208 static GTY(()) bool have_multiple_function_sections = false;
209
210 /* Whether the default text and cold text sections have been used at all.  */
211
212 static GTY(()) bool text_section_used = false;
213 static GTY(()) bool cold_text_section_used = false;
214
215 /* The default cold text section.  */
216 static GTY(()) section *cold_text_section;
217
218 /* Forward declarations for functions defined in this file.  */
219
220 static char *stripattributes (const char *);
221 static void output_call_frame_info (int);
222 static void dwarf2out_note_section_used (void);
223
224 /* Personality decl of current unit.  Used only when assembler does not support
225    personality CFI.  */
226 static GTY(()) rtx current_unit_personality;
227
228 /* Data and reference forms for relocatable data.  */
229 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
230 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
231
232 #ifndef DEBUG_FRAME_SECTION
233 #define DEBUG_FRAME_SECTION     ".debug_frame"
234 #endif
235
236 #ifndef FUNC_BEGIN_LABEL
237 #define FUNC_BEGIN_LABEL        "LFB"
238 #endif
239
240 #ifndef FUNC_END_LABEL
241 #define FUNC_END_LABEL          "LFE"
242 #endif
243
244 #ifndef PROLOGUE_END_LABEL
245 #define PROLOGUE_END_LABEL      "LPE"
246 #endif
247
248 #ifndef EPILOGUE_BEGIN_LABEL
249 #define EPILOGUE_BEGIN_LABEL    "LEB"
250 #endif
251
252 #ifndef FRAME_BEGIN_LABEL
253 #define FRAME_BEGIN_LABEL       "Lframe"
254 #endif
255 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
256 #define CIE_END_LABEL           "LECIE"
257 #define FDE_LABEL               "LSFDE"
258 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
259 #define FDE_END_LABEL           "LEFDE"
260 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
261 #define LINE_NUMBER_END_LABEL   "LELT"
262 #define LN_PROLOG_AS_LABEL      "LASLTP"
263 #define LN_PROLOG_END_LABEL     "LELTP"
264 #define DIE_LABEL_PREFIX        "DW"
265 \f
266 /* Match the base name of a file to the base name of a compilation unit. */
267
268 static int
269 matches_main_base (const char *path)
270 {
271   /* Cache the last query. */
272   static const char *last_path = NULL;
273   static int last_match = 0;
274   if (path != last_path)
275     {
276       const char *base;
277       int length = base_of_path (path, &base);
278       last_path = path;
279       last_match = (length == main_input_baselength
280                     && memcmp (base, main_input_basename, length) == 0);
281     }
282   return last_match;
283 }
284
285 #ifdef DEBUG_DEBUG_STRUCT
286
287 static int
288 dump_struct_debug (tree type, enum debug_info_usage usage,
289                    enum debug_struct_file criterion, int generic,
290                    int matches, int result)
291 {
292   /* Find the type name. */
293   tree type_decl = TYPE_STUB_DECL (type);
294   tree t = type_decl;
295   const char *name = 0;
296   if (TREE_CODE (t) == TYPE_DECL)
297     t = DECL_NAME (t);
298   if (t)
299     name = IDENTIFIER_POINTER (t);
300
301   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
302            criterion,
303            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
304            matches ? "bas" : "hdr",
305            generic ? "gen" : "ord",
306            usage == DINFO_USAGE_DFN ? ";" :
307              usage == DINFO_USAGE_DIR_USE ? "." : "*",
308            result,
309            (void*) type_decl, name);
310   return result;
311 }
312 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
313   dump_struct_debug (type, usage, criterion, generic, matches, result)
314
315 #else
316
317 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
318   (result)
319
320 #endif
321
322 static bool
323 should_emit_struct_debug (tree type, enum debug_info_usage usage)
324 {
325   enum debug_struct_file criterion;
326   tree type_decl;
327   bool generic = lang_hooks.types.generic_p (type);
328
329   if (generic)
330     criterion = debug_struct_generic[usage];
331   else
332     criterion = debug_struct_ordinary[usage];
333
334   if (criterion == DINFO_STRUCT_FILE_NONE)
335     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
336   if (criterion == DINFO_STRUCT_FILE_ANY)
337     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
338
339   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
340
341   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
342     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
343
344   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
345     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
346   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
347 }
348 \f
349 /* Return a pointer to a copy of the section string name S with all
350    attributes stripped off, and an asterisk prepended (for assemble_name).  */
351
352 static inline char *
353 stripattributes (const char *s)
354 {
355   char *stripped = XNEWVEC (char, strlen (s) + 2);
356   char *p = stripped;
357
358   *p++ = '*';
359
360   while (*s && *s != ',')
361     *p++ = *s++;
362
363   *p = '\0';
364   return stripped;
365 }
366
367 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
368    switch to the data section instead, and write out a synthetic start label
369    for collect2 the first time around.  */
370
371 static void
372 switch_to_eh_frame_section (bool back)
373 {
374   tree label;
375
376 #ifdef EH_FRAME_SECTION_NAME
377   if (eh_frame_section == 0)
378     {
379       int flags;
380
381       if (EH_TABLES_CAN_BE_READ_ONLY)
382         {
383           int fde_encoding;
384           int per_encoding;
385           int lsda_encoding;
386
387           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
388                                                        /*global=*/0);
389           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
390                                                        /*global=*/1);
391           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
392                                                         /*global=*/0);
393           flags = ((! flag_pic
394                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
395                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
396                         && (per_encoding & 0x70) != DW_EH_PE_absptr
397                         && (per_encoding & 0x70) != DW_EH_PE_aligned
398                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
399                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
400                    ? 0 : SECTION_WRITE);
401         }
402       else
403         flags = SECTION_WRITE;
404       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
405     }
406 #endif /* EH_FRAME_SECTION_NAME */
407
408   if (eh_frame_section)
409     switch_to_section (eh_frame_section);
410   else
411     {
412       /* We have no special eh_frame section.  Put the information in
413          the data section and emit special labels to guide collect2.  */
414       switch_to_section (data_section);
415
416       if (!back)
417         {
418           label = get_file_function_name ("F");
419           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
420           targetm.asm_out.globalize_label (asm_out_file,
421                                            IDENTIFIER_POINTER (label));
422           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
423         }
424     }
425 }
426
427 /* Switch [BACK] to the eh or debug frame table section, depending on
428    FOR_EH.  */
429
430 static void
431 switch_to_frame_table_section (int for_eh, bool back)
432 {
433   if (for_eh)
434     switch_to_eh_frame_section (back);
435   else
436     {
437       if (!debug_frame_section)
438         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
439                                            SECTION_DEBUG, NULL);
440       switch_to_section (debug_frame_section);
441     }
442 }
443
444 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
445
446 enum dw_cfi_oprnd_type
447 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
448 {
449   switch (cfi)
450     {
451     case DW_CFA_nop:
452     case DW_CFA_GNU_window_save:
453     case DW_CFA_remember_state:
454     case DW_CFA_restore_state:
455       return dw_cfi_oprnd_unused;
456
457     case DW_CFA_set_loc:
458     case DW_CFA_advance_loc1:
459     case DW_CFA_advance_loc2:
460     case DW_CFA_advance_loc4:
461     case DW_CFA_MIPS_advance_loc8:
462       return dw_cfi_oprnd_addr;
463
464     case DW_CFA_offset:
465     case DW_CFA_offset_extended:
466     case DW_CFA_def_cfa:
467     case DW_CFA_offset_extended_sf:
468     case DW_CFA_def_cfa_sf:
469     case DW_CFA_restore:
470     case DW_CFA_restore_extended:
471     case DW_CFA_undefined:
472     case DW_CFA_same_value:
473     case DW_CFA_def_cfa_register:
474     case DW_CFA_register:
475     case DW_CFA_expression:
476       return dw_cfi_oprnd_reg_num;
477
478     case DW_CFA_def_cfa_offset:
479     case DW_CFA_GNU_args_size:
480     case DW_CFA_def_cfa_offset_sf:
481       return dw_cfi_oprnd_offset;
482
483     case DW_CFA_def_cfa_expression:
484       return dw_cfi_oprnd_loc;
485
486     default:
487       gcc_unreachable ();
488     }
489 }
490
491 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
492
493 enum dw_cfi_oprnd_type
494 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
495 {
496   switch (cfi)
497     {
498     case DW_CFA_def_cfa:
499     case DW_CFA_def_cfa_sf:
500     case DW_CFA_offset:
501     case DW_CFA_offset_extended_sf:
502     case DW_CFA_offset_extended:
503       return dw_cfi_oprnd_offset;
504
505     case DW_CFA_register:
506       return dw_cfi_oprnd_reg_num;
507
508     case DW_CFA_expression:
509       return dw_cfi_oprnd_loc;
510
511     default:
512       return dw_cfi_oprnd_unused;
513     }
514 }
515
516 /* Output one FDE.  */
517
518 static void
519 output_fde (dw_fde_ref fde, bool for_eh, bool second,
520             char *section_start_label, int fde_encoding, char *augmentation,
521             bool any_lsda_needed, int lsda_encoding)
522 {
523   const char *begin, *end;
524   static unsigned int j;
525   char l1[20], l2[20];
526
527   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
528                                      /* empty */ 0);
529   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
530                                   for_eh + j);
531   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
532   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
533   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
534     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
535                          " indicating 64-bit DWARF extension");
536   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
537                         "FDE Length");
538   ASM_OUTPUT_LABEL (asm_out_file, l1);
539
540   if (for_eh)
541     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
542   else
543     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
544                            debug_frame_section, "FDE CIE offset");
545
546   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
547   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
548
549   if (for_eh)
550     {
551       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
552       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
553       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
554                                        "FDE initial location");
555       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
556                             end, begin, "FDE address range");
557     }
558   else
559     {
560       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
561       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
562     }
563
564   if (augmentation[0])
565     {
566       if (any_lsda_needed)
567         {
568           int size = size_of_encoded_value (lsda_encoding);
569
570           if (lsda_encoding == DW_EH_PE_aligned)
571             {
572               int offset = (  4         /* Length */
573                             + 4         /* CIE offset */
574                             + 2 * size_of_encoded_value (fde_encoding)
575                             + 1         /* Augmentation size */ );
576               int pad = -offset & (PTR_SIZE - 1);
577
578               size += pad;
579               gcc_assert (size_of_uleb128 (size) == 1);
580             }
581
582           dw2_asm_output_data_uleb128 (size, "Augmentation size");
583
584           if (fde->uses_eh_lsda)
585             {
586               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
587                                            fde->funcdef_number);
588               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
589                                                gen_rtx_SYMBOL_REF (Pmode, l1),
590                                                false,
591                                                "Language Specific Data Area");
592             }
593           else
594             {
595               if (lsda_encoding == DW_EH_PE_aligned)
596                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
597               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
598                                    "Language Specific Data Area (none)");
599             }
600         }
601       else
602         dw2_asm_output_data_uleb128 (0, "Augmentation size");
603     }
604
605   /* Loop through the Call Frame Instructions associated with this FDE.  */
606   fde->dw_fde_current_label = begin;
607   {
608     size_t from, until, i;
609
610     from = 0;
611     until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
612
613     if (fde->dw_fde_second_begin == NULL)
614       ;
615     else if (!second)
616       until = fde->dw_fde_switch_cfi_index;
617     else
618       from = fde->dw_fde_switch_cfi_index;
619
620     for (i = from; i < until; i++)
621       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
622   }
623
624   /* If we are to emit a ref/link from function bodies to their frame tables,
625      do it now.  This is typically performed to make sure that tables
626      associated with functions are dragged with them and not discarded in
627      garbage collecting links. We need to do this on a per function basis to
628      cope with -ffunction-sections.  */
629
630 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
631   /* Switch to the function section, emit the ref to the tables, and
632      switch *back* into the table section.  */
633   switch_to_section (function_section (fde->decl));
634   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
635   switch_to_frame_table_section (for_eh, true);
636 #endif
637
638   /* Pad the FDE out to an address sized boundary.  */
639   ASM_OUTPUT_ALIGN (asm_out_file,
640                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
641   ASM_OUTPUT_LABEL (asm_out_file, l2);
642
643   j += 2;
644 }
645
646 /* Return true if frame description entry FDE is needed for EH.  */
647
648 static bool
649 fde_needed_for_eh_p (dw_fde_ref fde)
650 {
651   if (flag_asynchronous_unwind_tables)
652     return true;
653
654   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
655     return true;
656
657   if (fde->uses_eh_lsda)
658     return true;
659
660   /* If exceptions are enabled, we have collected nothrow info.  */
661   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
662     return false;
663
664   return true;
665 }
666
667 /* Output the call frame information used to record information
668    that relates to calculating the frame pointer, and records the
669    location of saved registers.  */
670
671 static void
672 output_call_frame_info (int for_eh)
673 {
674   unsigned int i;
675   dw_fde_ref fde;
676   dw_cfi_ref cfi;
677   char l1[20], l2[20], section_start_label[20];
678   bool any_lsda_needed = false;
679   char augmentation[6];
680   int augmentation_size;
681   int fde_encoding = DW_EH_PE_absptr;
682   int per_encoding = DW_EH_PE_absptr;
683   int lsda_encoding = DW_EH_PE_absptr;
684   int return_reg;
685   rtx personality = NULL;
686   int dw_cie_version;
687
688   /* Don't emit a CIE if there won't be any FDEs.  */
689   if (fde_vec == NULL)
690     return;
691
692   /* Nothing to do if the assembler's doing it all.  */
693   if (dwarf2out_do_cfi_asm ())
694     return;
695
696   /* If we don't have any functions we'll want to unwind out of, don't emit
697      any EH unwind information.  If we make FDEs linkonce, we may have to
698      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
699      want to avoid having an FDE kept around when the function it refers to
700      is discarded.  Example where this matters: a primary function template
701      in C++ requires EH information, an explicit specialization doesn't.  */
702   if (for_eh)
703     {
704       bool any_eh_needed = false;
705
706       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
707         {
708           if (fde->uses_eh_lsda)
709             any_eh_needed = any_lsda_needed = true;
710           else if (fde_needed_for_eh_p (fde))
711             any_eh_needed = true;
712           else if (TARGET_USES_WEAK_UNWIND_INFO)
713             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
714         }
715
716       if (!any_eh_needed)
717         return;
718     }
719
720   /* We're going to be generating comments, so turn on app.  */
721   if (flag_debug_asm)
722     app_enable ();
723
724   /* Switch to the proper frame section, first time.  */
725   switch_to_frame_table_section (for_eh, false);
726
727   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
728   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
729
730   /* Output the CIE.  */
731   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
732   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
733   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
734     dw2_asm_output_data (4, 0xffffffff,
735       "Initial length escape value indicating 64-bit DWARF extension");
736   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
737                         "Length of Common Information Entry");
738   ASM_OUTPUT_LABEL (asm_out_file, l1);
739
740   /* Now that the CIE pointer is PC-relative for EH,
741      use 0 to identify the CIE.  */
742   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
743                        (for_eh ? 0 : DWARF_CIE_ID),
744                        "CIE Identifier Tag");
745
746   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
747      use CIE version 1, unless that would produce incorrect results
748      due to overflowing the return register column.  */
749   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
750   dw_cie_version = 1;
751   if (return_reg >= 256 || dwarf_version > 2)
752     dw_cie_version = 3;
753   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
754
755   augmentation[0] = 0;
756   augmentation_size = 0;
757
758   personality = current_unit_personality;
759   if (for_eh)
760     {
761       char *p;
762
763       /* Augmentation:
764          z      Indicates that a uleb128 is present to size the
765                 augmentation section.
766          L      Indicates the encoding (and thus presence) of
767                 an LSDA pointer in the FDE augmentation.
768          R      Indicates a non-default pointer encoding for
769                 FDE code pointers.
770          P      Indicates the presence of an encoding + language
771                 personality routine in the CIE augmentation.  */
772
773       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
774       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
775       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
776
777       p = augmentation + 1;
778       if (personality)
779         {
780           *p++ = 'P';
781           augmentation_size += 1 + size_of_encoded_value (per_encoding);
782           assemble_external_libcall (personality);
783         }
784       if (any_lsda_needed)
785         {
786           *p++ = 'L';
787           augmentation_size += 1;
788         }
789       if (fde_encoding != DW_EH_PE_absptr)
790         {
791           *p++ = 'R';
792           augmentation_size += 1;
793         }
794       if (p > augmentation + 1)
795         {
796           augmentation[0] = 'z';
797           *p = '\0';
798         }
799
800       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
801       if (personality && per_encoding == DW_EH_PE_aligned)
802         {
803           int offset = (  4             /* Length */
804                         + 4             /* CIE Id */
805                         + 1             /* CIE version */
806                         + strlen (augmentation) + 1     /* Augmentation */
807                         + size_of_uleb128 (1)           /* Code alignment */
808                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
809                         + 1             /* RA column */
810                         + 1             /* Augmentation size */
811                         + 1             /* Personality encoding */ );
812           int pad = -offset & (PTR_SIZE - 1);
813
814           augmentation_size += pad;
815
816           /* Augmentations should be small, so there's scarce need to
817              iterate for a solution.  Die if we exceed one uleb128 byte.  */
818           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
819         }
820     }
821
822   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
823   if (dw_cie_version >= 4)
824     {
825       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
826       dw2_asm_output_data (1, 0, "CIE Segment Size");
827     }
828   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
829   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
830                                "CIE Data Alignment Factor");
831
832   if (dw_cie_version == 1)
833     dw2_asm_output_data (1, return_reg, "CIE RA Column");
834   else
835     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
836
837   if (augmentation[0])
838     {
839       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
840       if (personality)
841         {
842           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
843                                eh_data_format_name (per_encoding));
844           dw2_asm_output_encoded_addr_rtx (per_encoding,
845                                            personality,
846                                            true, NULL);
847         }
848
849       if (any_lsda_needed)
850         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
851                              eh_data_format_name (lsda_encoding));
852
853       if (fde_encoding != DW_EH_PE_absptr)
854         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
855                              eh_data_format_name (fde_encoding));
856     }
857
858   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
859     output_cfi (cfi, NULL, for_eh);
860
861   /* Pad the CIE out to an address sized boundary.  */
862   ASM_OUTPUT_ALIGN (asm_out_file,
863                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
864   ASM_OUTPUT_LABEL (asm_out_file, l2);
865
866   /* Loop through all of the FDE's.  */
867   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
868     {
869       unsigned int k;
870
871       /* Don't emit EH unwind info for leaf functions that don't need it.  */
872       if (for_eh && !fde_needed_for_eh_p (fde))
873         continue;
874
875       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
876         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
877                     augmentation, any_lsda_needed, lsda_encoding);
878     }
879
880   if (for_eh && targetm.terminate_dw2_eh_frame_info)
881     dw2_asm_output_data (4, 0, "End of Table");
882 #ifdef MIPS_DEBUGGING_INFO
883   /* Work around Irix 6 assembler bug whereby labels at the end of a section
884      get a value of 0.  Putting .align 0 after the label fixes it.  */
885   ASM_OUTPUT_ALIGN (asm_out_file, 0);
886 #endif
887
888   /* Turn off app to make assembly quicker.  */
889   if (flag_debug_asm)
890     app_disable ();
891 }
892
893 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
894
895 static void
896 dwarf2out_do_cfi_startproc (bool second)
897 {
898   int enc;
899   rtx ref;
900   rtx personality = get_personality_function (current_function_decl);
901
902   fprintf (asm_out_file, "\t.cfi_startproc\n");
903
904   if (personality)
905     {
906       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
907       ref = personality;
908
909       /* ??? The GAS support isn't entirely consistent.  We have to
910          handle indirect support ourselves, but PC-relative is done
911          in the assembler.  Further, the assembler can't handle any
912          of the weirder relocation types.  */
913       if (enc & DW_EH_PE_indirect)
914         ref = dw2_force_const_mem (ref, true);
915
916       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
917       output_addr_const (asm_out_file, ref);
918       fputc ('\n', asm_out_file);
919     }
920
921   if (crtl->uses_eh_lsda)
922     {
923       char lab[20];
924
925       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
926       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
927                                    current_function_funcdef_no);
928       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
929       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
930
931       if (enc & DW_EH_PE_indirect)
932         ref = dw2_force_const_mem (ref, true);
933
934       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
935       output_addr_const (asm_out_file, ref);
936       fputc ('\n', asm_out_file);
937     }
938 }
939
940 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
941    this allocation may be done before pass_final.  */
942
943 dw_fde_ref
944 dwarf2out_alloc_current_fde (void)
945 {
946   dw_fde_ref fde;
947
948   fde = ggc_alloc_cleared_dw_fde_node ();
949   fde->decl = current_function_decl;
950   fde->funcdef_number = current_function_funcdef_no;
951   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
952   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
953   fde->uses_eh_lsda = crtl->uses_eh_lsda;
954   fde->nothrow = crtl->nothrow;
955   fde->drap_reg = INVALID_REGNUM;
956   fde->vdrap_reg = INVALID_REGNUM;
957
958   /* Record the FDE associated with this function.  */
959   cfun->fde = fde;
960   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
961
962   return fde;
963 }
964
965 /* Output a marker (i.e. a label) for the beginning of a function, before
966    the prologue.  */
967
968 void
969 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
970                           const char *file ATTRIBUTE_UNUSED)
971 {
972   char label[MAX_ARTIFICIAL_LABEL_BYTES];
973   char * dup_label;
974   dw_fde_ref fde;
975   section *fnsec;
976   bool do_frame;
977
978   current_function_func_begin_label = NULL;
979
980   do_frame = dwarf2out_do_frame ();
981
982   /* ??? current_function_func_begin_label is also used by except.c for
983      call-site information.  We must emit this label if it might be used.  */
984   if (!do_frame
985       && (!flag_exceptions
986           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
987     return;
988
989   fnsec = function_section (current_function_decl);
990   switch_to_section (fnsec);
991   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
992                                current_function_funcdef_no);
993   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
994                           current_function_funcdef_no);
995   dup_label = xstrdup (label);
996   current_function_func_begin_label = dup_label;
997
998   /* We can elide the fde allocation if we're not emitting debug info.  */
999   if (!do_frame)
1000     return;
1001
1002   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1003      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1004      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1005      do so now.  */
1006   fde = cfun->fde;
1007   if (fde == NULL)
1008     fde = dwarf2out_alloc_current_fde ();
1009
1010   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1011   fde->dw_fde_begin = dup_label;
1012   fde->dw_fde_current_label = dup_label;
1013   fde->in_std_section = (fnsec == text_section
1014                          || (cold_text_section && fnsec == cold_text_section));
1015
1016   /* We only want to output line number information for the genuine dwarf2
1017      prologue case, not the eh frame case.  */
1018 #ifdef DWARF2_DEBUGGING_INFO
1019   if (file)
1020     dwarf2out_source_line (line, file, 0, true);
1021 #endif
1022
1023   if (dwarf2out_do_cfi_asm ())
1024     dwarf2out_do_cfi_startproc (false);
1025   else
1026     {
1027       rtx personality = get_personality_function (current_function_decl);
1028       if (!current_unit_personality)
1029         current_unit_personality = personality;
1030
1031       /* We cannot keep a current personality per function as without CFI
1032          asm, at the point where we emit the CFI data, there is no current
1033          function anymore.  */
1034       if (personality && current_unit_personality != personality)
1035         sorry ("multiple EH personalities are supported only with assemblers "
1036                "supporting .cfi_personality directive");
1037     }
1038 }
1039
1040 /* Output a marker (i.e. a label) for the end of the generated code
1041    for a function prologue.  This gets called *after* the prologue code has
1042    been generated.  */
1043
1044 void
1045 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1046                         const char *file ATTRIBUTE_UNUSED)
1047 {
1048   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1049
1050   /* Output a label to mark the endpoint of the code generated for this
1051      function.  */
1052   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1053                                current_function_funcdef_no);
1054   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1055                           current_function_funcdef_no);
1056   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1057 }
1058
1059 /* Output a marker (i.e. a label) for the beginning of the generated code
1060    for a function epilogue.  This gets called *before* the prologue code has
1061    been generated.  */
1062
1063 void
1064 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1065                           const char *file ATTRIBUTE_UNUSED)
1066 {
1067   dw_fde_ref fde = cfun->fde;
1068   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1069
1070   if (fde->dw_fde_vms_begin_epilogue)
1071     return;
1072
1073   /* Output a label to mark the endpoint of the code generated for this
1074      function.  */
1075   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1076                                current_function_funcdef_no);
1077   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1078                           current_function_funcdef_no);
1079   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1080 }
1081
1082 /* Output a marker (i.e. a label) for the absolute end of the generated code
1083    for a function definition.  This gets called *after* the epilogue code has
1084    been generated.  */
1085
1086 void
1087 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1088                         const char *file ATTRIBUTE_UNUSED)
1089 {
1090   dw_fde_ref fde;
1091   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1092
1093   last_var_location_insn = NULL_RTX;
1094   cached_next_real_insn = NULL_RTX;
1095
1096   if (dwarf2out_do_cfi_asm ())
1097     fprintf (asm_out_file, "\t.cfi_endproc\n");
1098
1099   /* Output a label to mark the endpoint of the code generated for this
1100      function.  */
1101   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1102                                current_function_funcdef_no);
1103   ASM_OUTPUT_LABEL (asm_out_file, label);
1104   fde = cfun->fde;
1105   gcc_assert (fde != NULL);
1106   if (fde->dw_fde_second_begin == NULL)
1107     fde->dw_fde_end = xstrdup (label);
1108 }
1109
1110 void
1111 dwarf2out_frame_finish (void)
1112 {
1113   /* Output call frame information.  */
1114   if (targetm.debug_unwind_info () == UI_DWARF2)
1115     output_call_frame_info (0);
1116
1117   /* Output another copy for the unwinder.  */
1118   if ((flag_unwind_tables || flag_exceptions)
1119       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1120     output_call_frame_info (1);
1121 }
1122
1123 /* Note that the current function section is being used for code.  */
1124
1125 static void
1126 dwarf2out_note_section_used (void)
1127 {
1128   section *sec = current_function_section ();
1129   if (sec == text_section)
1130     text_section_used = true;
1131   else if (sec == cold_text_section)
1132     cold_text_section_used = true;
1133 }
1134
1135 static void var_location_switch_text_section (void);
1136 static void set_cur_line_info_table (section *);
1137
1138 void
1139 dwarf2out_switch_text_section (void)
1140 {
1141   section *sect;
1142   dw_fde_ref fde = cfun->fde;
1143
1144   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1145
1146   if (!in_cold_section_p)
1147     {
1148       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1149       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1150       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1151     }
1152   else
1153     {
1154       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1155       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1156       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1157     }
1158   have_multiple_function_sections = true;
1159
1160   /* There is no need to mark used sections when not debugging.  */
1161   if (cold_text_section != NULL)
1162     dwarf2out_note_section_used ();
1163
1164   if (dwarf2out_do_cfi_asm ())
1165     fprintf (asm_out_file, "\t.cfi_endproc\n");
1166
1167   /* Now do the real section switch.  */
1168   sect = current_function_section ();
1169   switch_to_section (sect);
1170
1171   fde->second_in_std_section
1172     = (sect == text_section
1173        || (cold_text_section && sect == cold_text_section));
1174
1175   if (dwarf2out_do_cfi_asm ())
1176     dwarf2out_do_cfi_startproc (true);
1177
1178   var_location_switch_text_section ();
1179
1180   if (cold_text_section != NULL)
1181     set_cur_line_info_table (sect);
1182 }
1183 \f
1184 /* And now, the subset of the debugging information support code necessary
1185    for emitting location expressions.  */
1186
1187 /* Data about a single source file.  */
1188 struct GTY(()) dwarf_file_data {
1189   const char * filename;
1190   int emitted_number;
1191 };
1192
1193 typedef struct GTY(()) deferred_locations_struct
1194 {
1195   tree variable;
1196   dw_die_ref die;
1197 } deferred_locations;
1198
1199 DEF_VEC_O(deferred_locations);
1200 DEF_VEC_ALLOC_O(deferred_locations,gc);
1201
1202 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1203
1204 DEF_VEC_P(dw_die_ref);
1205 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1206
1207 /* Location lists are ranges + location descriptions for that range,
1208    so you can track variables that are in different places over
1209    their entire life.  */
1210 typedef struct GTY(()) dw_loc_list_struct {
1211   dw_loc_list_ref dw_loc_next;
1212   const char *begin; /* Label for begin address of range */
1213   const char *end;  /* Label for end address of range */
1214   char *ll_symbol; /* Label for beginning of location list.
1215                       Only on head of list */
1216   const char *section; /* Section this loclist is relative to */
1217   dw_loc_descr_ref expr;
1218   hashval_t hash;
1219   /* True if all addresses in this and subsequent lists are known to be
1220      resolved.  */
1221   bool resolved_addr;
1222   /* True if this list has been replaced by dw_loc_next.  */
1223   bool replaced;
1224   bool emitted;
1225   /* True if the range should be emitted even if begin and end
1226      are the same.  */
1227   bool force;
1228 } dw_loc_list_node;
1229
1230 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1231
1232 /* Convert a DWARF stack opcode into its string name.  */
1233
1234 static const char *
1235 dwarf_stack_op_name (unsigned int op)
1236 {
1237   switch (op)
1238     {
1239     case DW_OP_addr:
1240       return "DW_OP_addr";
1241     case DW_OP_deref:
1242       return "DW_OP_deref";
1243     case DW_OP_const1u:
1244       return "DW_OP_const1u";
1245     case DW_OP_const1s:
1246       return "DW_OP_const1s";
1247     case DW_OP_const2u:
1248       return "DW_OP_const2u";
1249     case DW_OP_const2s:
1250       return "DW_OP_const2s";
1251     case DW_OP_const4u:
1252       return "DW_OP_const4u";
1253     case DW_OP_const4s:
1254       return "DW_OP_const4s";
1255     case DW_OP_const8u:
1256       return "DW_OP_const8u";
1257     case DW_OP_const8s:
1258       return "DW_OP_const8s";
1259     case DW_OP_constu:
1260       return "DW_OP_constu";
1261     case DW_OP_consts:
1262       return "DW_OP_consts";
1263     case DW_OP_dup:
1264       return "DW_OP_dup";
1265     case DW_OP_drop:
1266       return "DW_OP_drop";
1267     case DW_OP_over:
1268       return "DW_OP_over";
1269     case DW_OP_pick:
1270       return "DW_OP_pick";
1271     case DW_OP_swap:
1272       return "DW_OP_swap";
1273     case DW_OP_rot:
1274       return "DW_OP_rot";
1275     case DW_OP_xderef:
1276       return "DW_OP_xderef";
1277     case DW_OP_abs:
1278       return "DW_OP_abs";
1279     case DW_OP_and:
1280       return "DW_OP_and";
1281     case DW_OP_div:
1282       return "DW_OP_div";
1283     case DW_OP_minus:
1284       return "DW_OP_minus";
1285     case DW_OP_mod:
1286       return "DW_OP_mod";
1287     case DW_OP_mul:
1288       return "DW_OP_mul";
1289     case DW_OP_neg:
1290       return "DW_OP_neg";
1291     case DW_OP_not:
1292       return "DW_OP_not";
1293     case DW_OP_or:
1294       return "DW_OP_or";
1295     case DW_OP_plus:
1296       return "DW_OP_plus";
1297     case DW_OP_plus_uconst:
1298       return "DW_OP_plus_uconst";
1299     case DW_OP_shl:
1300       return "DW_OP_shl";
1301     case DW_OP_shr:
1302       return "DW_OP_shr";
1303     case DW_OP_shra:
1304       return "DW_OP_shra";
1305     case DW_OP_xor:
1306       return "DW_OP_xor";
1307     case DW_OP_bra:
1308       return "DW_OP_bra";
1309     case DW_OP_eq:
1310       return "DW_OP_eq";
1311     case DW_OP_ge:
1312       return "DW_OP_ge";
1313     case DW_OP_gt:
1314       return "DW_OP_gt";
1315     case DW_OP_le:
1316       return "DW_OP_le";
1317     case DW_OP_lt:
1318       return "DW_OP_lt";
1319     case DW_OP_ne:
1320       return "DW_OP_ne";
1321     case DW_OP_skip:
1322       return "DW_OP_skip";
1323     case DW_OP_lit0:
1324       return "DW_OP_lit0";
1325     case DW_OP_lit1:
1326       return "DW_OP_lit1";
1327     case DW_OP_lit2:
1328       return "DW_OP_lit2";
1329     case DW_OP_lit3:
1330       return "DW_OP_lit3";
1331     case DW_OP_lit4:
1332       return "DW_OP_lit4";
1333     case DW_OP_lit5:
1334       return "DW_OP_lit5";
1335     case DW_OP_lit6:
1336       return "DW_OP_lit6";
1337     case DW_OP_lit7:
1338       return "DW_OP_lit7";
1339     case DW_OP_lit8:
1340       return "DW_OP_lit8";
1341     case DW_OP_lit9:
1342       return "DW_OP_lit9";
1343     case DW_OP_lit10:
1344       return "DW_OP_lit10";
1345     case DW_OP_lit11:
1346       return "DW_OP_lit11";
1347     case DW_OP_lit12:
1348       return "DW_OP_lit12";
1349     case DW_OP_lit13:
1350       return "DW_OP_lit13";
1351     case DW_OP_lit14:
1352       return "DW_OP_lit14";
1353     case DW_OP_lit15:
1354       return "DW_OP_lit15";
1355     case DW_OP_lit16:
1356       return "DW_OP_lit16";
1357     case DW_OP_lit17:
1358       return "DW_OP_lit17";
1359     case DW_OP_lit18:
1360       return "DW_OP_lit18";
1361     case DW_OP_lit19:
1362       return "DW_OP_lit19";
1363     case DW_OP_lit20:
1364       return "DW_OP_lit20";
1365     case DW_OP_lit21:
1366       return "DW_OP_lit21";
1367     case DW_OP_lit22:
1368       return "DW_OP_lit22";
1369     case DW_OP_lit23:
1370       return "DW_OP_lit23";
1371     case DW_OP_lit24:
1372       return "DW_OP_lit24";
1373     case DW_OP_lit25:
1374       return "DW_OP_lit25";
1375     case DW_OP_lit26:
1376       return "DW_OP_lit26";
1377     case DW_OP_lit27:
1378       return "DW_OP_lit27";
1379     case DW_OP_lit28:
1380       return "DW_OP_lit28";
1381     case DW_OP_lit29:
1382       return "DW_OP_lit29";
1383     case DW_OP_lit30:
1384       return "DW_OP_lit30";
1385     case DW_OP_lit31:
1386       return "DW_OP_lit31";
1387     case DW_OP_reg0:
1388       return "DW_OP_reg0";
1389     case DW_OP_reg1:
1390       return "DW_OP_reg1";
1391     case DW_OP_reg2:
1392       return "DW_OP_reg2";
1393     case DW_OP_reg3:
1394       return "DW_OP_reg3";
1395     case DW_OP_reg4:
1396       return "DW_OP_reg4";
1397     case DW_OP_reg5:
1398       return "DW_OP_reg5";
1399     case DW_OP_reg6:
1400       return "DW_OP_reg6";
1401     case DW_OP_reg7:
1402       return "DW_OP_reg7";
1403     case DW_OP_reg8:
1404       return "DW_OP_reg8";
1405     case DW_OP_reg9:
1406       return "DW_OP_reg9";
1407     case DW_OP_reg10:
1408       return "DW_OP_reg10";
1409     case DW_OP_reg11:
1410       return "DW_OP_reg11";
1411     case DW_OP_reg12:
1412       return "DW_OP_reg12";
1413     case DW_OP_reg13:
1414       return "DW_OP_reg13";
1415     case DW_OP_reg14:
1416       return "DW_OP_reg14";
1417     case DW_OP_reg15:
1418       return "DW_OP_reg15";
1419     case DW_OP_reg16:
1420       return "DW_OP_reg16";
1421     case DW_OP_reg17:
1422       return "DW_OP_reg17";
1423     case DW_OP_reg18:
1424       return "DW_OP_reg18";
1425     case DW_OP_reg19:
1426       return "DW_OP_reg19";
1427     case DW_OP_reg20:
1428       return "DW_OP_reg20";
1429     case DW_OP_reg21:
1430       return "DW_OP_reg21";
1431     case DW_OP_reg22:
1432       return "DW_OP_reg22";
1433     case DW_OP_reg23:
1434       return "DW_OP_reg23";
1435     case DW_OP_reg24:
1436       return "DW_OP_reg24";
1437     case DW_OP_reg25:
1438       return "DW_OP_reg25";
1439     case DW_OP_reg26:
1440       return "DW_OP_reg26";
1441     case DW_OP_reg27:
1442       return "DW_OP_reg27";
1443     case DW_OP_reg28:
1444       return "DW_OP_reg28";
1445     case DW_OP_reg29:
1446       return "DW_OP_reg29";
1447     case DW_OP_reg30:
1448       return "DW_OP_reg30";
1449     case DW_OP_reg31:
1450       return "DW_OP_reg31";
1451     case DW_OP_breg0:
1452       return "DW_OP_breg0";
1453     case DW_OP_breg1:
1454       return "DW_OP_breg1";
1455     case DW_OP_breg2:
1456       return "DW_OP_breg2";
1457     case DW_OP_breg3:
1458       return "DW_OP_breg3";
1459     case DW_OP_breg4:
1460       return "DW_OP_breg4";
1461     case DW_OP_breg5:
1462       return "DW_OP_breg5";
1463     case DW_OP_breg6:
1464       return "DW_OP_breg6";
1465     case DW_OP_breg7:
1466       return "DW_OP_breg7";
1467     case DW_OP_breg8:
1468       return "DW_OP_breg8";
1469     case DW_OP_breg9:
1470       return "DW_OP_breg9";
1471     case DW_OP_breg10:
1472       return "DW_OP_breg10";
1473     case DW_OP_breg11:
1474       return "DW_OP_breg11";
1475     case DW_OP_breg12:
1476       return "DW_OP_breg12";
1477     case DW_OP_breg13:
1478       return "DW_OP_breg13";
1479     case DW_OP_breg14:
1480       return "DW_OP_breg14";
1481     case DW_OP_breg15:
1482       return "DW_OP_breg15";
1483     case DW_OP_breg16:
1484       return "DW_OP_breg16";
1485     case DW_OP_breg17:
1486       return "DW_OP_breg17";
1487     case DW_OP_breg18:
1488       return "DW_OP_breg18";
1489     case DW_OP_breg19:
1490       return "DW_OP_breg19";
1491     case DW_OP_breg20:
1492       return "DW_OP_breg20";
1493     case DW_OP_breg21:
1494       return "DW_OP_breg21";
1495     case DW_OP_breg22:
1496       return "DW_OP_breg22";
1497     case DW_OP_breg23:
1498       return "DW_OP_breg23";
1499     case DW_OP_breg24:
1500       return "DW_OP_breg24";
1501     case DW_OP_breg25:
1502       return "DW_OP_breg25";
1503     case DW_OP_breg26:
1504       return "DW_OP_breg26";
1505     case DW_OP_breg27:
1506       return "DW_OP_breg27";
1507     case DW_OP_breg28:
1508       return "DW_OP_breg28";
1509     case DW_OP_breg29:
1510       return "DW_OP_breg29";
1511     case DW_OP_breg30:
1512       return "DW_OP_breg30";
1513     case DW_OP_breg31:
1514       return "DW_OP_breg31";
1515     case DW_OP_regx:
1516       return "DW_OP_regx";
1517     case DW_OP_fbreg:
1518       return "DW_OP_fbreg";
1519     case DW_OP_bregx:
1520       return "DW_OP_bregx";
1521     case DW_OP_piece:
1522       return "DW_OP_piece";
1523     case DW_OP_deref_size:
1524       return "DW_OP_deref_size";
1525     case DW_OP_xderef_size:
1526       return "DW_OP_xderef_size";
1527     case DW_OP_nop:
1528       return "DW_OP_nop";
1529
1530     case DW_OP_push_object_address:
1531       return "DW_OP_push_object_address";
1532     case DW_OP_call2:
1533       return "DW_OP_call2";
1534     case DW_OP_call4:
1535       return "DW_OP_call4";
1536     case DW_OP_call_ref:
1537       return "DW_OP_call_ref";
1538     case DW_OP_implicit_value:
1539       return "DW_OP_implicit_value";
1540     case DW_OP_stack_value:
1541       return "DW_OP_stack_value";
1542     case DW_OP_form_tls_address:
1543       return "DW_OP_form_tls_address";
1544     case DW_OP_call_frame_cfa:
1545       return "DW_OP_call_frame_cfa";
1546     case DW_OP_bit_piece:
1547       return "DW_OP_bit_piece";
1548
1549     case DW_OP_GNU_push_tls_address:
1550       return "DW_OP_GNU_push_tls_address";
1551     case DW_OP_GNU_uninit:
1552       return "DW_OP_GNU_uninit";
1553     case DW_OP_GNU_encoded_addr:
1554       return "DW_OP_GNU_encoded_addr";
1555     case DW_OP_GNU_implicit_pointer:
1556       return "DW_OP_GNU_implicit_pointer";
1557     case DW_OP_GNU_entry_value:
1558       return "DW_OP_GNU_entry_value";
1559     case DW_OP_GNU_const_type:
1560       return "DW_OP_GNU_const_type";
1561     case DW_OP_GNU_regval_type:
1562       return "DW_OP_GNU_regval_type";
1563     case DW_OP_GNU_deref_type:
1564       return "DW_OP_GNU_deref_type";
1565     case DW_OP_GNU_convert:
1566       return "DW_OP_GNU_convert";
1567     case DW_OP_GNU_reinterpret:
1568       return "DW_OP_GNU_reinterpret";
1569     case DW_OP_GNU_parameter_ref:
1570       return "DW_OP_GNU_parameter_ref";
1571
1572     default:
1573       return "OP_<unknown>";
1574     }
1575 }
1576
1577 /* Return a pointer to a newly allocated location description.  Location
1578    descriptions are simple expression terms that can be strung
1579    together to form more complicated location (address) descriptions.  */
1580
1581 static inline dw_loc_descr_ref
1582 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1583                unsigned HOST_WIDE_INT oprnd2)
1584 {
1585   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1586
1587   descr->dw_loc_opc = op;
1588   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1589   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1590   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1591   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1592
1593   return descr;
1594 }
1595
1596 /* Return a pointer to a newly allocated location description for
1597    REG and OFFSET.  */
1598
1599 static inline dw_loc_descr_ref
1600 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1601 {
1602   if (reg <= 31)
1603     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1604                           offset, 0);
1605   else
1606     return new_loc_descr (DW_OP_bregx, reg, offset);
1607 }
1608
1609 /* Add a location description term to a location description expression.  */
1610
1611 static inline void
1612 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1613 {
1614   dw_loc_descr_ref *d;
1615
1616   /* Find the end of the chain.  */
1617   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1618     ;
1619
1620   *d = descr;
1621 }
1622
1623 /* Compare two location operands for exact equality.  */
1624
1625 static bool
1626 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1627 {
1628   if (a->val_class != b->val_class)
1629     return false;
1630   switch (a->val_class)
1631     {
1632     case dw_val_class_none:
1633       return true;
1634     case dw_val_class_addr:
1635       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1636
1637     case dw_val_class_offset:
1638     case dw_val_class_unsigned_const:
1639     case dw_val_class_const:
1640     case dw_val_class_range_list:
1641     case dw_val_class_lineptr:
1642     case dw_val_class_macptr:
1643       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1644       return a->v.val_unsigned == b->v.val_unsigned;
1645
1646     case dw_val_class_loc:
1647       return a->v.val_loc == b->v.val_loc;
1648     case dw_val_class_loc_list:
1649       return a->v.val_loc_list == b->v.val_loc_list;
1650     case dw_val_class_die_ref:
1651       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1652     case dw_val_class_fde_ref:
1653       return a->v.val_fde_index == b->v.val_fde_index;
1654     case dw_val_class_lbl_id:
1655       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1656     case dw_val_class_str:
1657       return a->v.val_str == b->v.val_str;
1658     case dw_val_class_flag:
1659       return a->v.val_flag == b->v.val_flag;
1660     case dw_val_class_file:
1661       return a->v.val_file == b->v.val_file;
1662     case dw_val_class_decl_ref:
1663       return a->v.val_decl_ref == b->v.val_decl_ref;
1664     
1665     case dw_val_class_const_double:
1666       return (a->v.val_double.high == b->v.val_double.high
1667               && a->v.val_double.low == b->v.val_double.low);
1668
1669     case dw_val_class_vec:
1670       {
1671         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1672         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1673
1674         return (a_len == b_len
1675                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1676       }
1677
1678     case dw_val_class_data8:
1679       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1680
1681     case dw_val_class_vms_delta:
1682       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1683               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1684     }
1685   gcc_unreachable ();
1686 }
1687
1688 /* Compare two location atoms for exact equality.  */
1689
1690 static bool
1691 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1692 {
1693   if (a->dw_loc_opc != b->dw_loc_opc)
1694     return false;
1695
1696   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1697      address size, but since we always allocate cleared storage it
1698      should be zero for other types of locations.  */
1699   if (a->dtprel != b->dtprel)
1700     return false;
1701
1702   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1703           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1704 }
1705
1706 /* Compare two complete location expressions for exact equality.  */
1707
1708 bool
1709 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1710 {
1711   while (1)
1712     {
1713       if (a == b)
1714         return true;
1715       if (a == NULL || b == NULL)
1716         return false;
1717       if (!loc_descr_equal_p_1 (a, b))
1718         return false;
1719
1720       a = a->dw_loc_next;
1721       b = b->dw_loc_next;
1722     }
1723 }
1724
1725
1726 /* Add a constant OFFSET to a location expression.  */
1727
1728 static void
1729 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1730 {
1731   dw_loc_descr_ref loc;
1732   HOST_WIDE_INT *p;
1733
1734   gcc_assert (*list_head != NULL);
1735
1736   if (!offset)
1737     return;
1738
1739   /* Find the end of the chain.  */
1740   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1741     ;
1742
1743   p = NULL;
1744   if (loc->dw_loc_opc == DW_OP_fbreg
1745       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1746     p = &loc->dw_loc_oprnd1.v.val_int;
1747   else if (loc->dw_loc_opc == DW_OP_bregx)
1748     p = &loc->dw_loc_oprnd2.v.val_int;
1749
1750   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1751      offset.  Don't optimize if an signed integer overflow would happen.  */
1752   if (p != NULL
1753       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1754           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1755     *p += offset;
1756
1757   else if (offset > 0)
1758     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1759
1760   else
1761     {
1762       loc->dw_loc_next = int_loc_descriptor (-offset);
1763       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1764     }
1765 }
1766
1767 /* Add a constant OFFSET to a location list.  */
1768
1769 static void
1770 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1771 {
1772   dw_loc_list_ref d;
1773   for (d = list_head; d != NULL; d = d->dw_loc_next)
1774     loc_descr_plus_const (&d->expr, offset);
1775 }
1776
1777 #define DWARF_REF_SIZE  \
1778   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1779
1780 static unsigned long int get_base_type_offset (dw_die_ref);
1781
1782 /* Return the size of a location descriptor.  */
1783
1784 static unsigned long
1785 size_of_loc_descr (dw_loc_descr_ref loc)
1786 {
1787   unsigned long size = 1;
1788
1789   switch (loc->dw_loc_opc)
1790     {
1791     case DW_OP_addr:
1792       size += DWARF2_ADDR_SIZE;
1793       break;
1794     case DW_OP_const1u:
1795     case DW_OP_const1s:
1796       size += 1;
1797       break;
1798     case DW_OP_const2u:
1799     case DW_OP_const2s:
1800       size += 2;
1801       break;
1802     case DW_OP_const4u:
1803     case DW_OP_const4s:
1804       size += 4;
1805       break;
1806     case DW_OP_const8u:
1807     case DW_OP_const8s:
1808       size += 8;
1809       break;
1810     case DW_OP_constu:
1811       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1812       break;
1813     case DW_OP_consts:
1814       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1815       break;
1816     case DW_OP_pick:
1817       size += 1;
1818       break;
1819     case DW_OP_plus_uconst:
1820       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1821       break;
1822     case DW_OP_skip:
1823     case DW_OP_bra:
1824       size += 2;
1825       break;
1826     case DW_OP_breg0:
1827     case DW_OP_breg1:
1828     case DW_OP_breg2:
1829     case DW_OP_breg3:
1830     case DW_OP_breg4:
1831     case DW_OP_breg5:
1832     case DW_OP_breg6:
1833     case DW_OP_breg7:
1834     case DW_OP_breg8:
1835     case DW_OP_breg9:
1836     case DW_OP_breg10:
1837     case DW_OP_breg11:
1838     case DW_OP_breg12:
1839     case DW_OP_breg13:
1840     case DW_OP_breg14:
1841     case DW_OP_breg15:
1842     case DW_OP_breg16:
1843     case DW_OP_breg17:
1844     case DW_OP_breg18:
1845     case DW_OP_breg19:
1846     case DW_OP_breg20:
1847     case DW_OP_breg21:
1848     case DW_OP_breg22:
1849     case DW_OP_breg23:
1850     case DW_OP_breg24:
1851     case DW_OP_breg25:
1852     case DW_OP_breg26:
1853     case DW_OP_breg27:
1854     case DW_OP_breg28:
1855     case DW_OP_breg29:
1856     case DW_OP_breg30:
1857     case DW_OP_breg31:
1858       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1859       break;
1860     case DW_OP_regx:
1861       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1862       break;
1863     case DW_OP_fbreg:
1864       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1865       break;
1866     case DW_OP_bregx:
1867       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1868       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1869       break;
1870     case DW_OP_piece:
1871       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1872       break;
1873     case DW_OP_bit_piece:
1874       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1875       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1876       break;
1877     case DW_OP_deref_size:
1878     case DW_OP_xderef_size:
1879       size += 1;
1880       break;
1881     case DW_OP_call2:
1882       size += 2;
1883       break;
1884     case DW_OP_call4:
1885       size += 4;
1886       break;
1887     case DW_OP_call_ref:
1888       size += DWARF_REF_SIZE;
1889       break;
1890     case DW_OP_implicit_value:
1891       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1892               + loc->dw_loc_oprnd1.v.val_unsigned;
1893       break;
1894     case DW_OP_GNU_implicit_pointer:
1895       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1896       break;
1897     case DW_OP_GNU_entry_value:
1898       {
1899         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1900         size += size_of_uleb128 (op_size) + op_size;
1901         break;
1902       }
1903     case DW_OP_GNU_const_type:
1904       {
1905         unsigned long o
1906           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1907         size += size_of_uleb128 (o) + 1;
1908         switch (loc->dw_loc_oprnd2.val_class)
1909           {
1910           case dw_val_class_vec:
1911             size += loc->dw_loc_oprnd2.v.val_vec.length
1912                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1913             break;
1914           case dw_val_class_const:
1915             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1916             break;
1917           case dw_val_class_const_double:
1918             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1919             break;
1920           default:
1921             gcc_unreachable ();
1922           }
1923         break;
1924       }
1925     case DW_OP_GNU_regval_type:
1926       {
1927         unsigned long o
1928           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1929         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1930                 + size_of_uleb128 (o);
1931       }
1932       break;
1933     case DW_OP_GNU_deref_type:
1934       {
1935         unsigned long o
1936           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1937         size += 1 + size_of_uleb128 (o);
1938       }
1939       break;
1940     case DW_OP_GNU_convert:
1941     case DW_OP_GNU_reinterpret:
1942       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1943         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944       else
1945         {
1946           unsigned long o
1947             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1948           size += size_of_uleb128 (o);
1949         }
1950       break;
1951     case DW_OP_GNU_parameter_ref:
1952       size += 4;
1953       break;
1954     default:
1955       break;
1956     }
1957
1958   return size;
1959 }
1960
1961 /* Return the size of a series of location descriptors.  */
1962
1963 unsigned long
1964 size_of_locs (dw_loc_descr_ref loc)
1965 {
1966   dw_loc_descr_ref l;
1967   unsigned long size;
1968
1969   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1970      field, to avoid writing to a PCH file.  */
1971   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1972     {
1973       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1974         break;
1975       size += size_of_loc_descr (l);
1976     }
1977   if (! l)
1978     return size;
1979
1980   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1981     {
1982       l->dw_loc_addr = size;
1983       size += size_of_loc_descr (l);
1984     }
1985
1986   return size;
1987 }
1988
1989 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1990 static void get_ref_die_offset_label (char *, dw_die_ref);
1991 static unsigned long int get_ref_die_offset (dw_die_ref);
1992
1993 /* Output location description stack opcode's operands (if any).
1994    The for_eh_or_skip parameter controls whether register numbers are
1995    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1996    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1997    info).  This should be suppressed for the cases that have not been converted
1998    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1999
2000 static void
2001 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2002 {
2003   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2004   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2005
2006   switch (loc->dw_loc_opc)
2007     {
2008 #ifdef DWARF2_DEBUGGING_INFO
2009     case DW_OP_const2u:
2010     case DW_OP_const2s:
2011       dw2_asm_output_data (2, val1->v.val_int, NULL);
2012       break;
2013     case DW_OP_const4u:
2014       if (loc->dtprel)
2015         {
2016           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2017           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2018                                                val1->v.val_addr);
2019           fputc ('\n', asm_out_file);
2020           break;
2021         }
2022       /* FALLTHRU */
2023     case DW_OP_const4s:
2024       dw2_asm_output_data (4, val1->v.val_int, NULL);
2025       break;
2026     case DW_OP_const8u:
2027       if (loc->dtprel)
2028         {
2029           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2030           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2031                                                val1->v.val_addr);
2032           fputc ('\n', asm_out_file);
2033           break;
2034         }
2035       /* FALLTHRU */
2036     case DW_OP_const8s:
2037       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2038       dw2_asm_output_data (8, val1->v.val_int, NULL);
2039       break;
2040     case DW_OP_skip:
2041     case DW_OP_bra:
2042       {
2043         int offset;
2044
2045         gcc_assert (val1->val_class == dw_val_class_loc);
2046         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2047
2048         dw2_asm_output_data (2, offset, NULL);
2049       }
2050       break;
2051     case DW_OP_implicit_value:
2052       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2053       switch (val2->val_class)
2054         {
2055         case dw_val_class_const:
2056           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2057           break;
2058         case dw_val_class_vec:
2059           {
2060             unsigned int elt_size = val2->v.val_vec.elt_size;
2061             unsigned int len = val2->v.val_vec.length;
2062             unsigned int i;
2063             unsigned char *p;
2064
2065             if (elt_size > sizeof (HOST_WIDE_INT))
2066               {
2067                 elt_size /= 2;
2068                 len *= 2;
2069               }
2070             for (i = 0, p = val2->v.val_vec.array;
2071                  i < len;
2072                  i++, p += elt_size)
2073               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2074                                    "fp or vector constant word %u", i);
2075           }
2076           break;
2077         case dw_val_class_const_double:
2078           {
2079             unsigned HOST_WIDE_INT first, second;
2080
2081             if (WORDS_BIG_ENDIAN)
2082               {
2083                 first = val2->v.val_double.high;
2084                 second = val2->v.val_double.low;
2085               }
2086             else
2087               {
2088                 first = val2->v.val_double.low;
2089                 second = val2->v.val_double.high;
2090               }
2091             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2092                                  first, NULL);
2093             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2094                                  second, NULL);
2095           }
2096           break;
2097         case dw_val_class_addr:
2098           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2099           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2100           break;
2101         default:
2102           gcc_unreachable ();
2103         }
2104       break;
2105 #else
2106     case DW_OP_const2u:
2107     case DW_OP_const2s:
2108     case DW_OP_const4u:
2109     case DW_OP_const4s:
2110     case DW_OP_const8u:
2111     case DW_OP_const8s:
2112     case DW_OP_skip:
2113     case DW_OP_bra:
2114     case DW_OP_implicit_value:
2115       /* We currently don't make any attempt to make sure these are
2116          aligned properly like we do for the main unwind info, so
2117          don't support emitting things larger than a byte if we're
2118          only doing unwinding.  */
2119       gcc_unreachable ();
2120 #endif
2121     case DW_OP_const1u:
2122     case DW_OP_const1s:
2123       dw2_asm_output_data (1, val1->v.val_int, NULL);
2124       break;
2125     case DW_OP_constu:
2126       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2127       break;
2128     case DW_OP_consts:
2129       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2130       break;
2131     case DW_OP_pick:
2132       dw2_asm_output_data (1, val1->v.val_int, NULL);
2133       break;
2134     case DW_OP_plus_uconst:
2135       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2136       break;
2137     case DW_OP_breg0:
2138     case DW_OP_breg1:
2139     case DW_OP_breg2:
2140     case DW_OP_breg3:
2141     case DW_OP_breg4:
2142     case DW_OP_breg5:
2143     case DW_OP_breg6:
2144     case DW_OP_breg7:
2145     case DW_OP_breg8:
2146     case DW_OP_breg9:
2147     case DW_OP_breg10:
2148     case DW_OP_breg11:
2149     case DW_OP_breg12:
2150     case DW_OP_breg13:
2151     case DW_OP_breg14:
2152     case DW_OP_breg15:
2153     case DW_OP_breg16:
2154     case DW_OP_breg17:
2155     case DW_OP_breg18:
2156     case DW_OP_breg19:
2157     case DW_OP_breg20:
2158     case DW_OP_breg21:
2159     case DW_OP_breg22:
2160     case DW_OP_breg23:
2161     case DW_OP_breg24:
2162     case DW_OP_breg25:
2163     case DW_OP_breg26:
2164     case DW_OP_breg27:
2165     case DW_OP_breg28:
2166     case DW_OP_breg29:
2167     case DW_OP_breg30:
2168     case DW_OP_breg31:
2169       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2170       break;
2171     case DW_OP_regx:
2172       {
2173         unsigned r = val1->v.val_unsigned;
2174         if (for_eh_or_skip >= 0)
2175           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2176         gcc_assert (size_of_uleb128 (r) 
2177                     == size_of_uleb128 (val1->v.val_unsigned));
2178         dw2_asm_output_data_uleb128 (r, NULL);  
2179       }
2180       break;
2181     case DW_OP_fbreg:
2182       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2183       break;
2184     case DW_OP_bregx:
2185       {
2186         unsigned r = val1->v.val_unsigned;
2187         if (for_eh_or_skip >= 0)
2188           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2189         gcc_assert (size_of_uleb128 (r) 
2190                     == size_of_uleb128 (val1->v.val_unsigned));
2191         dw2_asm_output_data_uleb128 (r, NULL);  
2192         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2193       }
2194       break;
2195     case DW_OP_piece:
2196       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2197       break;
2198     case DW_OP_bit_piece:
2199       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2200       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2201       break;
2202     case DW_OP_deref_size:
2203     case DW_OP_xderef_size:
2204       dw2_asm_output_data (1, val1->v.val_int, NULL);
2205       break;
2206
2207     case DW_OP_addr:
2208       if (loc->dtprel)
2209         {
2210           if (targetm.asm_out.output_dwarf_dtprel)
2211             {
2212               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2213                                                    DWARF2_ADDR_SIZE,
2214                                                    val1->v.val_addr);
2215               fputc ('\n', asm_out_file);
2216             }
2217           else
2218             gcc_unreachable ();
2219         }
2220       else
2221         {
2222 #ifdef DWARF2_DEBUGGING_INFO
2223           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2224 #else
2225           gcc_unreachable ();
2226 #endif
2227         }
2228       break;
2229
2230     case DW_OP_GNU_implicit_pointer:
2231       {
2232         char label[MAX_ARTIFICIAL_LABEL_BYTES
2233                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2234         gcc_assert (val1->val_class == dw_val_class_die_ref);
2235         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2236         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2237         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2238       }
2239       break;
2240
2241     case DW_OP_GNU_entry_value:
2242       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2243       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2244       break;
2245
2246     case DW_OP_GNU_const_type:
2247       {
2248         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2249         gcc_assert (o);
2250         dw2_asm_output_data_uleb128 (o, NULL);
2251         switch (val2->val_class)
2252           {
2253           case dw_val_class_const:
2254             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2255             dw2_asm_output_data (1, l, NULL);
2256             dw2_asm_output_data (l, val2->v.val_int, NULL);
2257             break;
2258           case dw_val_class_vec:
2259             {
2260               unsigned int elt_size = val2->v.val_vec.elt_size;
2261               unsigned int len = val2->v.val_vec.length;
2262               unsigned int i;
2263               unsigned char *p;
2264
2265               l = len * elt_size;
2266               dw2_asm_output_data (1, l, NULL);
2267               if (elt_size > sizeof (HOST_WIDE_INT))
2268                 {
2269                   elt_size /= 2;
2270                   len *= 2;
2271                 }
2272               for (i = 0, p = val2->v.val_vec.array;
2273                    i < len;
2274                    i++, p += elt_size)
2275                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2276                                      "fp or vector constant word %u", i);
2277             }
2278             break;
2279           case dw_val_class_const_double:
2280             {
2281               unsigned HOST_WIDE_INT first, second;
2282               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2283
2284               dw2_asm_output_data (1, 2 * l, NULL);
2285               if (WORDS_BIG_ENDIAN)
2286                 {
2287                   first = val2->v.val_double.high;
2288                   second = val2->v.val_double.low;
2289                 }
2290               else
2291                 {
2292                   first = val2->v.val_double.low;
2293                   second = val2->v.val_double.high;
2294                 }
2295               dw2_asm_output_data (l, first, NULL);
2296               dw2_asm_output_data (l, second, NULL);
2297             }
2298             break;
2299           default:
2300             gcc_unreachable ();
2301           }
2302       }
2303       break;
2304     case DW_OP_GNU_regval_type:
2305       {
2306         unsigned r = val1->v.val_unsigned;
2307         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2308         gcc_assert (o);
2309         if (for_eh_or_skip >= 0)
2310           {
2311             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2312             gcc_assert (size_of_uleb128 (r)
2313                         == size_of_uleb128 (val1->v.val_unsigned));
2314           }
2315         dw2_asm_output_data_uleb128 (r, NULL);
2316         dw2_asm_output_data_uleb128 (o, NULL);
2317       }
2318       break;
2319     case DW_OP_GNU_deref_type:
2320       {
2321         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2322         gcc_assert (o);
2323         dw2_asm_output_data (1, val1->v.val_int, NULL);
2324         dw2_asm_output_data_uleb128 (o, NULL);
2325       }
2326       break;
2327     case DW_OP_GNU_convert:
2328     case DW_OP_GNU_reinterpret:
2329       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2330         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2331       else
2332         {
2333           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2334           gcc_assert (o);
2335           dw2_asm_output_data_uleb128 (o, NULL);
2336         }
2337       break;
2338
2339     case DW_OP_GNU_parameter_ref:
2340       {
2341         unsigned long o;
2342         gcc_assert (val1->val_class == dw_val_class_die_ref);
2343         o = get_ref_die_offset (val1->v.val_die_ref.die);
2344         dw2_asm_output_data (4, o, NULL);
2345       }
2346       break;
2347
2348     default:
2349       /* Other codes have no operands.  */
2350       break;
2351     }
2352 }
2353
2354 /* Output a sequence of location operations.  
2355    The for_eh_or_skip parameter controls whether register numbers are
2356    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2357    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2358    info).  This should be suppressed for the cases that have not been converted
2359    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2360
2361 void
2362 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2363 {
2364   for (; loc != NULL; loc = loc->dw_loc_next)
2365     {
2366       enum dwarf_location_atom opc = loc->dw_loc_opc;
2367       /* Output the opcode.  */
2368       if (for_eh_or_skip >= 0 
2369           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2370         {
2371           unsigned r = (opc - DW_OP_breg0);
2372           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2373           gcc_assert (r <= 31);
2374           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2375         }
2376       else if (for_eh_or_skip >= 0 
2377                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2378         {
2379           unsigned r = (opc - DW_OP_reg0);
2380           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2381           gcc_assert (r <= 31);
2382           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2383         }
2384
2385       dw2_asm_output_data (1, opc,
2386                              "%s", dwarf_stack_op_name (opc));
2387
2388       /* Output the operand(s) (if any).  */
2389       output_loc_operands (loc, for_eh_or_skip);
2390     }
2391 }
2392
2393 /* Output location description stack opcode's operands (if any).
2394    The output is single bytes on a line, suitable for .cfi_escape.  */
2395
2396 static void
2397 output_loc_operands_raw (dw_loc_descr_ref loc)
2398 {
2399   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2400   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2401
2402   switch (loc->dw_loc_opc)
2403     {
2404     case DW_OP_addr:
2405     case DW_OP_implicit_value:
2406       /* We cannot output addresses in .cfi_escape, only bytes.  */
2407       gcc_unreachable ();
2408
2409     case DW_OP_const1u:
2410     case DW_OP_const1s:
2411     case DW_OP_pick:
2412     case DW_OP_deref_size:
2413     case DW_OP_xderef_size:
2414       fputc (',', asm_out_file);
2415       dw2_asm_output_data_raw (1, val1->v.val_int);
2416       break;
2417
2418     case DW_OP_const2u:
2419     case DW_OP_const2s:
2420       fputc (',', asm_out_file);
2421       dw2_asm_output_data_raw (2, val1->v.val_int);
2422       break;
2423
2424     case DW_OP_const4u:
2425     case DW_OP_const4s:
2426       fputc (',', asm_out_file);
2427       dw2_asm_output_data_raw (4, val1->v.val_int);
2428       break;
2429
2430     case DW_OP_const8u:
2431     case DW_OP_const8s:
2432       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2433       fputc (',', asm_out_file);
2434       dw2_asm_output_data_raw (8, val1->v.val_int);
2435       break;
2436
2437     case DW_OP_skip:
2438     case DW_OP_bra:
2439       {
2440         int offset;
2441
2442         gcc_assert (val1->val_class == dw_val_class_loc);
2443         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2444
2445         fputc (',', asm_out_file);
2446         dw2_asm_output_data_raw (2, offset);
2447       }
2448       break;
2449
2450     case DW_OP_regx:
2451       {
2452         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2453         gcc_assert (size_of_uleb128 (r) 
2454                     == size_of_uleb128 (val1->v.val_unsigned));
2455         fputc (',', asm_out_file);
2456         dw2_asm_output_data_uleb128_raw (r);
2457       }
2458       break;
2459       
2460     case DW_OP_constu:
2461     case DW_OP_plus_uconst:
2462     case DW_OP_piece:
2463       fputc (',', asm_out_file);
2464       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2465       break;
2466
2467     case DW_OP_bit_piece:
2468       fputc (',', asm_out_file);
2469       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2470       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2471       break;
2472
2473     case DW_OP_consts:
2474     case DW_OP_breg0:
2475     case DW_OP_breg1:
2476     case DW_OP_breg2:
2477     case DW_OP_breg3:
2478     case DW_OP_breg4:
2479     case DW_OP_breg5:
2480     case DW_OP_breg6:
2481     case DW_OP_breg7:
2482     case DW_OP_breg8:
2483     case DW_OP_breg9:
2484     case DW_OP_breg10:
2485     case DW_OP_breg11:
2486     case DW_OP_breg12:
2487     case DW_OP_breg13:
2488     case DW_OP_breg14:
2489     case DW_OP_breg15:
2490     case DW_OP_breg16:
2491     case DW_OP_breg17:
2492     case DW_OP_breg18:
2493     case DW_OP_breg19:
2494     case DW_OP_breg20:
2495     case DW_OP_breg21:
2496     case DW_OP_breg22:
2497     case DW_OP_breg23:
2498     case DW_OP_breg24:
2499     case DW_OP_breg25:
2500     case DW_OP_breg26:
2501     case DW_OP_breg27:
2502     case DW_OP_breg28:
2503     case DW_OP_breg29:
2504     case DW_OP_breg30:
2505     case DW_OP_breg31:
2506     case DW_OP_fbreg:
2507       fputc (',', asm_out_file);
2508       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2509       break;
2510
2511     case DW_OP_bregx:
2512       {
2513         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2514         gcc_assert (size_of_uleb128 (r) 
2515                     == size_of_uleb128 (val1->v.val_unsigned));
2516         fputc (',', asm_out_file);
2517         dw2_asm_output_data_uleb128_raw (r);
2518         fputc (',', asm_out_file);
2519         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2520       }
2521       break;
2522
2523     case DW_OP_GNU_implicit_pointer:
2524     case DW_OP_GNU_entry_value:
2525     case DW_OP_GNU_const_type:
2526     case DW_OP_GNU_regval_type:
2527     case DW_OP_GNU_deref_type:
2528     case DW_OP_GNU_convert:
2529     case DW_OP_GNU_reinterpret:
2530     case DW_OP_GNU_parameter_ref:
2531       gcc_unreachable ();
2532       break;
2533
2534     default:
2535       /* Other codes have no operands.  */
2536       break;
2537     }
2538 }
2539
2540 void
2541 output_loc_sequence_raw (dw_loc_descr_ref loc)
2542 {
2543   while (1)
2544     {
2545       enum dwarf_location_atom opc = loc->dw_loc_opc;
2546       /* Output the opcode.  */
2547       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2548         {
2549           unsigned r = (opc - DW_OP_breg0);
2550           r = DWARF2_FRAME_REG_OUT (r, 1);
2551           gcc_assert (r <= 31);
2552           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2553         }
2554       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2555         {
2556           unsigned r = (opc - DW_OP_reg0);
2557           r = DWARF2_FRAME_REG_OUT (r, 1);
2558           gcc_assert (r <= 31);
2559           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2560         }
2561       /* Output the opcode.  */
2562       fprintf (asm_out_file, "%#x", opc);
2563       output_loc_operands_raw (loc);
2564
2565       if (!loc->dw_loc_next)
2566         break;
2567       loc = loc->dw_loc_next;
2568
2569       fputc (',', asm_out_file);
2570     }
2571 }
2572
2573 /* This function builds a dwarf location descriptor sequence from a
2574    dw_cfa_location, adding the given OFFSET to the result of the
2575    expression.  */
2576
2577 struct dw_loc_descr_struct *
2578 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2579 {
2580   struct dw_loc_descr_struct *head, *tmp;
2581
2582   offset += cfa->offset;
2583
2584   if (cfa->indirect)
2585     {
2586       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2587       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2588       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2589       add_loc_descr (&head, tmp);
2590       if (offset != 0)
2591         {
2592           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2593           add_loc_descr (&head, tmp);
2594         }
2595     }
2596   else
2597     head = new_reg_loc_descr (cfa->reg, offset);
2598
2599   return head;
2600 }
2601
2602 /* This function builds a dwarf location descriptor sequence for
2603    the address at OFFSET from the CFA when stack is aligned to
2604    ALIGNMENT byte.  */
2605
2606 struct dw_loc_descr_struct *
2607 build_cfa_aligned_loc (dw_cfa_location *cfa,
2608                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2609 {
2610   struct dw_loc_descr_struct *head;
2611   unsigned int dwarf_fp
2612     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2613
2614   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2615   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2616     {
2617       head = new_reg_loc_descr (dwarf_fp, 0);
2618       add_loc_descr (&head, int_loc_descriptor (alignment));
2619       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2620       loc_descr_plus_const (&head, offset);
2621     }
2622   else
2623     head = new_reg_loc_descr (dwarf_fp, offset);
2624   return head;
2625 }
2626 \f
2627 /* And now, the support for symbolic debugging information.  */
2628
2629 /* .debug_str support.  */
2630 static int output_indirect_string (void **, void *);
2631
2632 static void dwarf2out_init (const char *);
2633 static void dwarf2out_finish (const char *);
2634 static void dwarf2out_assembly_start (void);
2635 static void dwarf2out_define (unsigned int, const char *);
2636 static void dwarf2out_undef (unsigned int, const char *);
2637 static void dwarf2out_start_source_file (unsigned, const char *);
2638 static void dwarf2out_end_source_file (unsigned);
2639 static void dwarf2out_function_decl (tree);
2640 static void dwarf2out_begin_block (unsigned, unsigned);
2641 static void dwarf2out_end_block (unsigned, unsigned);
2642 static bool dwarf2out_ignore_block (const_tree);
2643 static void dwarf2out_global_decl (tree);
2644 static void dwarf2out_type_decl (tree, int);
2645 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2646 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2647                                                  dw_die_ref);
2648 static void dwarf2out_abstract_function (tree);
2649 static void dwarf2out_var_location (rtx);
2650 static void dwarf2out_begin_function (tree);
2651 static void dwarf2out_set_name (tree, tree);
2652
2653 /* The debug hooks structure.  */
2654
2655 const struct gcc_debug_hooks dwarf2_debug_hooks =
2656 {
2657   dwarf2out_init,
2658   dwarf2out_finish,
2659   dwarf2out_assembly_start,
2660   dwarf2out_define,
2661   dwarf2out_undef,
2662   dwarf2out_start_source_file,
2663   dwarf2out_end_source_file,
2664   dwarf2out_begin_block,
2665   dwarf2out_end_block,
2666   dwarf2out_ignore_block,
2667   dwarf2out_source_line,
2668   dwarf2out_begin_prologue,
2669 #if VMS_DEBUGGING_INFO
2670   dwarf2out_vms_end_prologue,
2671   dwarf2out_vms_begin_epilogue,
2672 #else
2673   debug_nothing_int_charstar,
2674   debug_nothing_int_charstar,
2675 #endif
2676   dwarf2out_end_epilogue,
2677   dwarf2out_begin_function,
2678   debug_nothing_int,            /* end_function */
2679   dwarf2out_function_decl,      /* function_decl */
2680   dwarf2out_global_decl,
2681   dwarf2out_type_decl,          /* type_decl */
2682   dwarf2out_imported_module_or_decl,
2683   debug_nothing_tree,           /* deferred_inline_function */
2684   /* The DWARF 2 backend tries to reduce debugging bloat by not
2685      emitting the abstract description of inline functions until
2686      something tries to reference them.  */
2687   dwarf2out_abstract_function,  /* outlining_inline_function */
2688   debug_nothing_rtx,            /* label */
2689   debug_nothing_int,            /* handle_pch */
2690   dwarf2out_var_location,
2691   dwarf2out_switch_text_section,
2692   dwarf2out_set_name,
2693   1,                            /* start_end_main_source_file */
2694   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2695 };
2696 \f
2697 /* NOTE: In the comments in this file, many references are made to
2698    "Debugging Information Entries".  This term is abbreviated as `DIE'
2699    throughout the remainder of this file.  */
2700
2701 /* An internal representation of the DWARF output is built, and then
2702    walked to generate the DWARF debugging info.  The walk of the internal
2703    representation is done after the entire program has been compiled.
2704    The types below are used to describe the internal representation.  */
2705
2706 /* Whether to put type DIEs into their own section .debug_types instead
2707    of making them part of the .debug_info section.  Only supported for
2708    Dwarf V4 or higher and the user didn't disable them through
2709    -fno-debug-types-section.  It is more efficient to put them in a
2710    separate comdat sections since the linker will then be able to
2711    remove duplicates.  But not all tools support .debug_types sections
2712    yet.  */
2713
2714 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2715
2716 /* Various DIE's use offsets relative to the beginning of the
2717    .debug_info section to refer to each other.  */
2718
2719 typedef long int dw_offset;
2720
2721 /* Define typedefs here to avoid circular dependencies.  */
2722
2723 typedef struct dw_attr_struct *dw_attr_ref;
2724 typedef struct dw_line_info_struct *dw_line_info_ref;
2725 typedef struct pubname_struct *pubname_ref;
2726 typedef struct dw_ranges_struct *dw_ranges_ref;
2727 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2728 typedef struct comdat_type_struct *comdat_type_node_ref;
2729
2730 /* The entries in the line_info table more-or-less mirror the opcodes
2731    that are used in the real dwarf line table.  Arrays of these entries
2732    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2733    supported.  */
2734
2735 enum dw_line_info_opcode {
2736   /* Emit DW_LNE_set_address; the operand is the label index.  */
2737   LI_set_address,
2738
2739   /* Emit a row to the matrix with the given line.  This may be done
2740      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2741      special opcodes.  */
2742   LI_set_line,
2743
2744   /* Emit a DW_LNS_set_file.  */
2745   LI_set_file,
2746
2747   /* Emit a DW_LNS_set_column.  */
2748   LI_set_column,
2749
2750   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2751   LI_negate_stmt,
2752
2753   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2754   LI_set_prologue_end,
2755   LI_set_epilogue_begin,
2756
2757   /* Emit a DW_LNE_set_discriminator.  */
2758   LI_set_discriminator
2759 };
2760
2761 typedef struct GTY(()) dw_line_info_struct {
2762   enum dw_line_info_opcode opcode;
2763   unsigned int val;
2764 } dw_line_info_entry;
2765
2766 DEF_VEC_O(dw_line_info_entry);
2767 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2768
2769 typedef struct GTY(()) dw_line_info_table_struct {
2770   /* The label that marks the end of this section.  */
2771   const char *end_label;
2772
2773   /* The values for the last row of the matrix, as collected in the table.
2774      These are used to minimize the changes to the next row.  */
2775   unsigned int file_num;
2776   unsigned int line_num;
2777   unsigned int column_num;
2778   int discrim_num;
2779   bool is_stmt;
2780   bool in_use;
2781
2782   VEC(dw_line_info_entry, gc) *entries;
2783 } dw_line_info_table;
2784
2785 typedef dw_line_info_table *dw_line_info_table_p;
2786
2787 DEF_VEC_P(dw_line_info_table_p);
2788 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2789
2790 /* Each DIE attribute has a field specifying the attribute kind,
2791    a link to the next attribute in the chain, and an attribute value.
2792    Attributes are typically linked below the DIE they modify.  */
2793
2794 typedef struct GTY(()) dw_attr_struct {
2795   enum dwarf_attribute dw_attr;
2796   dw_val_node dw_attr_val;
2797 }
2798 dw_attr_node;
2799
2800 DEF_VEC_O(dw_attr_node);
2801 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2802
2803 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2804    The children of each node form a circular list linked by
2805    die_sib.  die_child points to the node *before* the "first" child node.  */
2806
2807 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2808   union die_symbol_or_type_node
2809     {
2810       char * GTY ((tag ("0"))) die_symbol;
2811       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2812     }
2813   GTY ((desc ("use_debug_types"))) die_id;
2814   VEC(dw_attr_node,gc) * die_attr;
2815   dw_die_ref die_parent;
2816   dw_die_ref die_child;
2817   dw_die_ref die_sib;
2818   dw_die_ref die_definition; /* ref from a specification to its definition */
2819   dw_offset die_offset;
2820   unsigned long die_abbrev;
2821   int die_mark;
2822   /* Die is used and must not be pruned as unused.  */
2823   int die_perennial_p;
2824   unsigned int decl_id;
2825   enum dwarf_tag die_tag;
2826 }
2827 die_node;
2828
2829 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2830 #define FOR_EACH_CHILD(die, c, expr) do {       \
2831   c = die->die_child;                           \
2832   if (c) do {                                   \
2833     c = c->die_sib;                             \
2834     expr;                                       \
2835   } while (c != die->die_child);                \
2836 } while (0)
2837
2838 /* The pubname structure */
2839
2840 typedef struct GTY(()) pubname_struct {
2841   dw_die_ref die;
2842   const char *name;
2843 }
2844 pubname_entry;
2845
2846 DEF_VEC_O(pubname_entry);
2847 DEF_VEC_ALLOC_O(pubname_entry, gc);
2848
2849 struct GTY(()) dw_ranges_struct {
2850   /* If this is positive, it's a block number, otherwise it's a
2851      bitwise-negated index into dw_ranges_by_label.  */
2852   int num;
2853 };
2854
2855 /* A structure to hold a macinfo entry.  */
2856
2857 typedef struct GTY(()) macinfo_struct {
2858   unsigned char code;
2859   unsigned HOST_WIDE_INT lineno;
2860   const char *info;
2861 }
2862 macinfo_entry;
2863
2864 DEF_VEC_O(macinfo_entry);
2865 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2866
2867 struct GTY(()) dw_ranges_by_label_struct {
2868   const char *begin;
2869   const char *end;
2870 };
2871
2872 /* The comdat type node structure.  */
2873 typedef struct GTY(()) comdat_type_struct
2874 {
2875   dw_die_ref root_die;
2876   dw_die_ref type_die;
2877   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2878   struct comdat_type_struct *next;
2879 }
2880 comdat_type_node;
2881
2882 /* The limbo die list structure.  */
2883 typedef struct GTY(()) limbo_die_struct {
2884   dw_die_ref die;
2885   tree created_for;
2886   struct limbo_die_struct *next;
2887 }
2888 limbo_die_node;
2889
2890 typedef struct skeleton_chain_struct
2891 {
2892   dw_die_ref old_die;
2893   dw_die_ref new_die;
2894   struct skeleton_chain_struct *parent;
2895 }
2896 skeleton_chain_node;
2897
2898 /* Define a macro which returns nonzero for a TYPE_DECL which was
2899    implicitly generated for a type.
2900
2901    Note that, unlike the C front-end (which generates a NULL named
2902    TYPE_DECL node for each complete tagged type, each array type,
2903    and each function type node created) the C++ front-end generates
2904    a _named_ TYPE_DECL node for each tagged type node created.
2905    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2906    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2907    front-end, but for each type, tagged or not.  */
2908
2909 #define TYPE_DECL_IS_STUB(decl)                         \
2910   (DECL_NAME (decl) == NULL_TREE                        \
2911    || (DECL_ARTIFICIAL (decl)                           \
2912        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2913            /* This is necessary for stub decls that     \
2914               appear in nested inline functions.  */    \
2915            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2916                && (decl_ultimate_origin (decl)          \
2917                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2918
2919 /* Information concerning the compilation unit's programming
2920    language, and compiler version.  */
2921
2922 /* Fixed size portion of the DWARF compilation unit header.  */
2923 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2924   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2925
2926 /* Fixed size portion of the DWARF comdat type unit header.  */
2927 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2928   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2929    + DWARF_OFFSET_SIZE)
2930
2931 /* Fixed size portion of public names info.  */
2932 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2933
2934 /* Fixed size portion of the address range info.  */
2935 #define DWARF_ARANGES_HEADER_SIZE                                       \
2936   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2937                 DWARF2_ADDR_SIZE * 2)                                   \
2938    - DWARF_INITIAL_LENGTH_SIZE)
2939
2940 /* Size of padding portion in the address range info.  It must be
2941    aligned to twice the pointer size.  */
2942 #define DWARF_ARANGES_PAD_SIZE \
2943   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2944                 DWARF2_ADDR_SIZE * 2)                              \
2945    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2946
2947 /* Use assembler line directives if available.  */
2948 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2949 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2950 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2951 #else
2952 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2953 #endif
2954 #endif
2955
2956 /* Minimum line offset in a special line info. opcode.
2957    This value was chosen to give a reasonable range of values.  */
2958 #define DWARF_LINE_BASE  -10
2959
2960 /* First special line opcode - leave room for the standard opcodes.  */
2961 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2962
2963 /* Range of line offsets in a special line info. opcode.  */
2964 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2965
2966 /* Flag that indicates the initial value of the is_stmt_start flag.
2967    In the present implementation, we do not mark any lines as
2968    the beginning of a source statement, because that information
2969    is not made available by the GCC front-end.  */
2970 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2971
2972 /* Maximum number of operations per instruction bundle.  */
2973 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2974 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2975 #endif
2976
2977 /* This location is used by calc_die_sizes() to keep track
2978    the offset of each DIE within the .debug_info section.  */
2979 static unsigned long next_die_offset;
2980
2981 /* Record the root of the DIE's built for the current compilation unit.  */
2982 static GTY(()) dw_die_ref single_comp_unit_die;
2983
2984 /* A list of type DIEs that have been separated into comdat sections.  */
2985 static GTY(()) comdat_type_node *comdat_type_list;
2986
2987 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2988 static GTY(()) limbo_die_node *limbo_die_list;
2989
2990 /* A list of DIEs for which we may have to generate
2991    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2992 static GTY(()) limbo_die_node *deferred_asm_name;
2993
2994 /* Filenames referenced by this compilation unit.  */
2995 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2996
2997 /* A hash table of references to DIE's that describe declarations.
2998    The key is a DECL_UID() which is a unique number identifying each decl.  */
2999 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3000
3001 /* A hash table of references to DIE's that describe COMMON blocks.
3002    The key is DECL_UID() ^ die_parent.  */
3003 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3004
3005 typedef struct GTY(()) die_arg_entry_struct {
3006     dw_die_ref die;
3007     tree arg;
3008 } die_arg_entry;
3009
3010 DEF_VEC_O(die_arg_entry);
3011 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3012
3013 /* Node of the variable location list.  */
3014 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3015   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3016      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3017      in mode of the EXPR_LIST node and first EXPR_LIST operand
3018      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3019      location or NULL for padding.  For larger bitsizes,
3020      mode is 0 and first operand is a CONCAT with bitsize
3021      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3022      NULL as second operand.  */
3023   rtx GTY (()) loc;
3024   const char * GTY (()) label;
3025   struct var_loc_node * GTY (()) next;
3026 };
3027
3028 /* Variable location list.  */
3029 struct GTY (()) var_loc_list_def {
3030   struct var_loc_node * GTY (()) first;
3031
3032   /* Pointer to the last but one or last element of the
3033      chained list.  If the list is empty, both first and
3034      last are NULL, if the list contains just one node
3035      or the last node certainly is not redundant, it points
3036      to the last node, otherwise points to the last but one.
3037      Do not mark it for GC because it is marked through the chain.  */
3038   struct var_loc_node * GTY ((skip ("%h"))) last;
3039
3040   /* Pointer to the last element before section switch,
3041      if NULL, either sections weren't switched or first
3042      is after section switch.  */
3043   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3044
3045   /* DECL_UID of the variable decl.  */
3046   unsigned int decl_id;
3047 };
3048 typedef struct var_loc_list_def var_loc_list;
3049
3050 /* Call argument location list.  */
3051 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3052   rtx GTY (()) call_arg_loc_note;
3053   const char * GTY (()) label;
3054   tree GTY (()) block;
3055   bool tail_call_p;
3056   rtx GTY (()) symbol_ref;
3057   struct call_arg_loc_node * GTY (()) next;
3058 };
3059
3060
3061 /* Table of decl location linked lists.  */
3062 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3063
3064 /* Head and tail of call_arg_loc chain.  */
3065 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3066 static struct call_arg_loc_node *call_arg_loc_last;
3067
3068 /* Number of call sites in the current function.  */
3069 static int call_site_count = -1;
3070 /* Number of tail call sites in the current function.  */
3071 static int tail_call_site_count = -1;
3072
3073 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3074    DIEs.  */
3075 static VEC (dw_die_ref, heap) *block_map;
3076
3077 /* A cached location list.  */
3078 struct GTY (()) cached_dw_loc_list_def {
3079   /* The DECL_UID of the decl that this entry describes.  */
3080   unsigned int decl_id;
3081
3082   /* The cached location list.  */
3083   dw_loc_list_ref loc_list;
3084 };
3085 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3086
3087 /* Table of cached location lists.  */
3088 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3089
3090 /* A pointer to the base of a list of references to DIE's that
3091    are uniquely identified by their tag, presence/absence of
3092    children DIE's, and list of attribute/value pairs.  */
3093 static GTY((length ("abbrev_die_table_allocated")))
3094   dw_die_ref *abbrev_die_table;
3095
3096 /* Number of elements currently allocated for abbrev_die_table.  */
3097 static GTY(()) unsigned abbrev_die_table_allocated;
3098
3099 /* Number of elements in type_die_table currently in use.  */
3100 static GTY(()) unsigned abbrev_die_table_in_use;
3101
3102 /* Size (in elements) of increments by which we may expand the
3103    abbrev_die_table.  */
3104 #define ABBREV_DIE_TABLE_INCREMENT 256
3105
3106 /* A global counter for generating labels for line number data.  */
3107 static unsigned int line_info_label_num;
3108
3109 /* The current table to which we should emit line number information
3110    for the current function.  This will be set up at the beginning of
3111    assembly for the function.  */
3112 static dw_line_info_table *cur_line_info_table;
3113
3114 /* The two default tables of line number info.  */
3115 static GTY(()) dw_line_info_table *text_section_line_info;
3116 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3117
3118 /* The set of all non-default tables of line number info.  */
3119 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3120
3121 /* A flag to tell pubnames/types export if there is an info section to
3122    refer to.  */
3123 static bool info_section_emitted;
3124
3125 /* A pointer to the base of a table that contains a list of publicly
3126    accessible names.  */
3127 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3128
3129 /* A pointer to the base of a table that contains a list of publicly
3130    accessible types.  */
3131 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3132
3133 /* A pointer to the base of a table that contains a list of macro
3134    defines/undefines (and file start/end markers).  */
3135 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3136
3137 /* True if .debug_macinfo or .debug_macros section is going to be
3138    emitted.  */
3139 #define have_macinfo \
3140   (debug_info_level >= DINFO_LEVEL_VERBOSE \
3141    && !VEC_empty (macinfo_entry, macinfo_table))
3142
3143 /* Array of dies for which we should generate .debug_ranges info.  */
3144 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3145
3146 /* Number of elements currently allocated for ranges_table.  */
3147 static GTY(()) unsigned ranges_table_allocated;
3148
3149 /* Number of elements in ranges_table currently in use.  */
3150 static GTY(()) unsigned ranges_table_in_use;
3151
3152 /* Array of pairs of labels referenced in ranges_table.  */
3153 static GTY ((length ("ranges_by_label_allocated")))
3154      dw_ranges_by_label_ref ranges_by_label;
3155
3156 /* Number of elements currently allocated for ranges_by_label.  */
3157 static GTY(()) unsigned ranges_by_label_allocated;
3158
3159 /* Number of elements in ranges_by_label currently in use.  */
3160 static GTY(()) unsigned ranges_by_label_in_use;
3161
3162 /* Size (in elements) of increments by which we may expand the
3163    ranges_table.  */
3164 #define RANGES_TABLE_INCREMENT 64
3165
3166 /* Whether we have location lists that need outputting */
3167 static GTY(()) bool have_location_lists;
3168
3169 /* Unique label counter.  */
3170 static GTY(()) unsigned int loclabel_num;
3171
3172 /* Unique label counter for point-of-call tables.  */
3173 static GTY(()) unsigned int poc_label_num;
3174
3175 /* Record whether the function being analyzed contains inlined functions.  */
3176 static int current_function_has_inlines;
3177
3178 /* The last file entry emitted by maybe_emit_file().  */
3179 static GTY(()) struct dwarf_file_data * last_emitted_file;
3180
3181 /* Number of internal labels generated by gen_internal_sym().  */
3182 static GTY(()) int label_num;
3183
3184 /* Cached result of previous call to lookup_filename.  */
3185 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3186
3187 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3188
3189 /* Instances of generic types for which we need to generate debug
3190    info that describe their generic parameters and arguments. That
3191    generation needs to happen once all types are properly laid out so
3192    we do it at the end of compilation.  */
3193 static GTY(()) VEC(tree,gc) *generic_type_instances;
3194
3195 /* Offset from the "steady-state frame pointer" to the frame base,
3196    within the current function.  */
3197 static HOST_WIDE_INT frame_pointer_fb_offset;
3198 static bool frame_pointer_fb_offset_valid;
3199
3200 static VEC (dw_die_ref, heap) *base_types;
3201
3202 /* Forward declarations for functions defined in this file.  */
3203
3204 static int is_pseudo_reg (const_rtx);
3205 static tree type_main_variant (tree);
3206 static int is_tagged_type (const_tree);
3207 static const char *dwarf_tag_name (unsigned);
3208 static const char *dwarf_attr_name (unsigned);
3209 static const char *dwarf_form_name (unsigned);
3210 static tree decl_ultimate_origin (const_tree);
3211 static tree decl_class_context (tree);
3212 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3213 static inline enum dw_val_class AT_class (dw_attr_ref);
3214 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3215 static inline unsigned AT_flag (dw_attr_ref);
3216 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3217 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3218 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3219 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3220 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3221                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3222 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3223                                unsigned int, unsigned char *);
3224 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3225 static hashval_t debug_str_do_hash (const void *);
3226 static int debug_str_eq (const void *, const void *);
3227 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3228 static inline const char *AT_string (dw_attr_ref);
3229 static enum dwarf_form AT_string_form (dw_attr_ref);
3230 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3231 static void add_AT_specification (dw_die_ref, dw_die_ref);
3232 static inline dw_die_ref AT_ref (dw_attr_ref);
3233 static inline int AT_ref_external (dw_attr_ref);
3234 static inline void set_AT_ref_external (dw_attr_ref, int);
3235 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3236 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3237 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3238 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3239                              dw_loc_list_ref);
3240 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3241 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3242 static inline rtx AT_addr (dw_attr_ref);
3243 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3244 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3245 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3246 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3247                            unsigned HOST_WIDE_INT);
3248 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3249                                unsigned long);
3250 static inline const char *AT_lbl (dw_attr_ref);
3251 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3252 static const char *get_AT_low_pc (dw_die_ref);
3253 static const char *get_AT_hi_pc (dw_die_ref);
3254 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3255 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3256 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3257 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3258 static bool is_cxx (void);
3259 static bool is_fortran (void);
3260 static bool is_ada (void);
3261 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3262 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3263 static void add_child_die (dw_die_ref, dw_die_ref);
3264 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3265 static dw_die_ref lookup_type_die (tree);
3266 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3267 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3268 static void equate_type_number_to_die (tree, dw_die_ref);
3269 static hashval_t decl_die_table_hash (const void *);
3270 static int decl_die_table_eq (const void *, const void *);
3271 static dw_die_ref lookup_decl_die (tree);
3272 static hashval_t common_block_die_table_hash (const void *);
3273 static int common_block_die_table_eq (const void *, const void *);
3274 static hashval_t decl_loc_table_hash (const void *);
3275 static int decl_loc_table_eq (const void *, const void *);
3276 static var_loc_list *lookup_decl_loc (const_tree);
3277 static void equate_decl_number_to_die (tree, dw_die_ref);
3278 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3279 static void print_spaces (FILE *);
3280 static void print_die (dw_die_ref, FILE *);
3281 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3282 static dw_die_ref pop_compile_unit (dw_die_ref);
3283 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3284 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3285 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3286 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3287 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3288 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3289 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3290                                    struct md5_ctx *, int *);
3291 struct checksum_attributes;
3292 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3293 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3294 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3295 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3296 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3297 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3298 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3299 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3300 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3301 static void compute_section_prefix (dw_die_ref);
3302 static int is_type_die (dw_die_ref);
3303 static int is_comdat_die (dw_die_ref);
3304 static int is_symbol_die (dw_die_ref);
3305 static void assign_symbol_names (dw_die_ref);
3306 static void break_out_includes (dw_die_ref);
3307 static int is_declaration_die (dw_die_ref);
3308 static int should_move_die_to_comdat (dw_die_ref);
3309 static dw_die_ref clone_as_declaration (dw_die_ref);
3310 static dw_die_ref clone_die (dw_die_ref);
3311 static dw_die_ref clone_tree (dw_die_ref);
3312 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3313 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3314 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3315 static dw_die_ref generate_skeleton (dw_die_ref);
3316 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3317                                                          dw_die_ref,
3318                                                          dw_die_ref);
3319 static void break_out_comdat_types (dw_die_ref);
3320 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3321 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3322 static void copy_decls_for_unworthy_types (dw_die_ref);
3323
3324 static hashval_t htab_cu_hash (const void *);
3325 static int htab_cu_eq (const void *, const void *);
3326 static void htab_cu_del (void *);
3327 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3328 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3329 static void add_sibling_attributes (dw_die_ref);
3330 static void build_abbrev_table (dw_die_ref);
3331 static void output_location_lists (dw_die_ref);
3332 static int constant_size (unsigned HOST_WIDE_INT);
3333 static unsigned long size_of_die (dw_die_ref);
3334 static void calc_die_sizes (dw_die_ref);
3335 static void calc_base_type_die_sizes (void);
3336 static void mark_dies (dw_die_ref);
3337 static void unmark_dies (dw_die_ref);
3338 static void unmark_all_dies (dw_die_ref);
3339 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3340 static unsigned long size_of_aranges (void);
3341 static enum dwarf_form value_format (dw_attr_ref);
3342 static void output_value_format (dw_attr_ref);
3343 static void output_abbrev_section (void);
3344 static void output_die_symbol (dw_die_ref);
3345 static void output_die (dw_die_ref);
3346 static void output_compilation_unit_header (void);
3347 static void output_comp_unit (dw_die_ref, int);
3348 static void output_comdat_type_unit (comdat_type_node *);
3349 static const char *dwarf2_name (tree, int);
3350 static void add_pubname (tree, dw_die_ref);
3351 static void add_pubname_string (const char *, dw_die_ref);
3352 static void add_pubtype (tree, dw_die_ref);
3353 static void output_pubnames (VEC (pubname_entry,gc) *);
3354 static void output_aranges (unsigned long);
3355 static unsigned int add_ranges_num (int);
3356 static unsigned int add_ranges (const_tree);
3357 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3358                                   bool *);
3359 static void output_ranges (void);
3360 static dw_line_info_table *new_line_info_table (void);
3361 static void output_line_info (void);
3362 static void output_file_names (void);
3363 static dw_die_ref base_type_die (tree);
3364 static int is_base_type (tree);
3365 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3366 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3367 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3368 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3369 static int type_is_enum (const_tree);
3370 static unsigned int dbx_reg_number (const_rtx);
3371 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3372 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3373 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3374                                                 enum var_init_status);
3375 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3376                                                      enum var_init_status);
3377 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3378                                          enum var_init_status);
3379 static int is_based_loc (const_rtx);
3380 static int resolve_one_addr (rtx *, void *);
3381 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3382                                                enum var_init_status);
3383 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3384                                         enum var_init_status);
3385 static dw_loc_list_ref loc_list_from_tree (tree, int);
3386 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3387 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3388 static tree field_type (const_tree);
3389 static unsigned int simple_type_align_in_bits (const_tree);
3390 static unsigned int simple_decl_align_in_bits (const_tree);
3391 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3392 static HOST_WIDE_INT field_byte_offset (const_tree);
3393 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3394                                          dw_loc_list_ref);
3395 static void add_data_member_location_attribute (dw_die_ref, tree);
3396 static bool add_const_value_attribute (dw_die_ref, rtx);
3397 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3398 static void insert_double (double_int, unsigned char *);
3399 static void insert_float (const_rtx, unsigned char *);
3400 static rtx rtl_for_decl_location (tree);
3401 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3402                                                    enum dwarf_attribute);
3403 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3404 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3405 static void add_name_attribute (dw_die_ref, const char *);
3406 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3407 static void add_comp_dir_attribute (dw_die_ref);
3408 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3409 static void add_subscript_info (dw_die_ref, tree, bool);
3410 static void add_byte_size_attribute (dw_die_ref, tree);
3411 static void add_bit_offset_attribute (dw_die_ref, tree);
3412 static void add_bit_size_attribute (dw_die_ref, tree);
3413 static void add_prototyped_attribute (dw_die_ref, tree);
3414 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3415 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3416 static void add_src_coords_attributes (dw_die_ref, tree);
3417 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3418 static void push_decl_scope (tree);
3419 static void pop_decl_scope (void);
3420 static dw_die_ref scope_die_for (tree, dw_die_ref);
3421 static inline int local_scope_p (dw_die_ref);
3422 static inline int class_scope_p (dw_die_ref);
3423 static inline int class_or_namespace_scope_p (dw_die_ref);
3424 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3425 static void add_calling_convention_attribute (dw_die_ref, tree);
3426 static const char *type_tag (const_tree);
3427 static tree member_declared_type (const_tree);
3428 #if 0
3429 static const char *decl_start_label (tree);
3430 #endif
3431 static void gen_array_type_die (tree, dw_die_ref);
3432 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3433 #if 0
3434 static void gen_entry_point_die (tree, dw_die_ref);
3435 #endif
3436 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3437 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3438 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3439 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3440 static void gen_formal_types_die (tree, dw_die_ref);
3441 static void gen_subprogram_die (tree, dw_die_ref);
3442 static void gen_variable_die (tree, tree, dw_die_ref);
3443 static void gen_const_die (tree, dw_die_ref);
3444 static void gen_label_die (tree, dw_die_ref);
3445 static void gen_lexical_block_die (tree, dw_die_ref, int);
3446 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3447 static void gen_field_die (tree, dw_die_ref);
3448 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3449 static dw_die_ref gen_compile_unit_die (const char *);
3450 static void gen_inheritance_die (tree, tree, dw_die_ref);
3451 static void gen_member_die (tree, dw_die_ref);
3452 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3453                                                 enum debug_info_usage);
3454 static void gen_subroutine_type_die (tree, dw_die_ref);
3455 static void gen_typedef_die (tree, dw_die_ref);
3456 static void gen_type_die (tree, dw_die_ref);
3457 static void gen_block_die (tree, dw_die_ref, int);
3458 static void decls_for_scope (tree, dw_die_ref, int);
3459 static inline int is_redundant_typedef (const_tree);
3460 static bool is_naming_typedef_decl (const_tree);
3461 static inline dw_die_ref get_context_die (tree);
3462 static void gen_namespace_die (tree, dw_die_ref);
3463 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3464 static dw_die_ref force_decl_die (tree);
3465 static dw_die_ref force_type_die (tree);
3466 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3467 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3468 static struct dwarf_file_data * lookup_filename (const char *);
3469 static void retry_incomplete_types (void);
3470 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3471 static void gen_generic_params_dies (tree);
3472 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3473 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3474 static void splice_child_die (dw_die_ref, dw_die_ref);
3475 static int file_info_cmp (const void *, const void *);
3476 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3477                                      const char *, const char *);
3478 static void output_loc_list (dw_loc_list_ref);
3479 static char *gen_internal_sym (const char *);
3480
3481 static void prune_unmark_dies (dw_die_ref);
3482 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3483 static void prune_unused_types_mark (dw_die_ref, int);
3484 static void prune_unused_types_walk (dw_die_ref);
3485 static void prune_unused_types_walk_attribs (dw_die_ref);
3486 static void prune_unused_types_prune (dw_die_ref);
3487 static void prune_unused_types (void);
3488 static int maybe_emit_file (struct dwarf_file_data *fd);
3489 static inline const char *AT_vms_delta1 (dw_attr_ref);
3490 static inline const char *AT_vms_delta2 (dw_attr_ref);
3491 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3492                                      const char *, const char *);
3493 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3494 static void gen_remaining_tmpl_value_param_die_attribute (void);
3495 static bool generic_type_p (tree);
3496 static void schedule_generic_params_dies_gen (tree t);
3497 static void gen_scheduled_generic_parms_dies (void);
3498
3499 /* Section names used to hold DWARF debugging information.  */
3500 #ifndef DEBUG_INFO_SECTION
3501 #define DEBUG_INFO_SECTION      ".debug_info"
3502 #endif
3503 #ifndef DEBUG_ABBREV_SECTION
3504 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3505 #endif
3506 #ifndef DEBUG_ARANGES_SECTION
3507 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3508 #endif
3509 #ifndef DEBUG_MACINFO_SECTION
3510 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3511 #endif
3512 #ifndef DEBUG_MACRO_SECTION
3513 #define DEBUG_MACRO_SECTION     ".debug_macro"
3514 #endif
3515 #ifndef DEBUG_LINE_SECTION
3516 #define DEBUG_LINE_SECTION      ".debug_line"
3517 #endif
3518 #ifndef DEBUG_LOC_SECTION
3519 #define DEBUG_LOC_SECTION       ".debug_loc"
3520 #endif
3521 #ifndef DEBUG_PUBNAMES_SECTION
3522 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3523 #endif
3524 #ifndef DEBUG_PUBTYPES_SECTION
3525 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3526 #endif
3527 #ifndef DEBUG_STR_SECTION
3528 #define DEBUG_STR_SECTION       ".debug_str"
3529 #endif
3530 #ifndef DEBUG_RANGES_SECTION
3531 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3532 #endif
3533
3534 /* Standard ELF section names for compiled code and data.  */
3535 #ifndef TEXT_SECTION_NAME
3536 #define TEXT_SECTION_NAME       ".text"
3537 #endif
3538
3539 /* Section flags for .debug_str section.  */
3540 #define DEBUG_STR_SECTION_FLAGS \
3541   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3542    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3543    : SECTION_DEBUG)
3544
3545 /* Labels we insert at beginning sections we can reference instead of
3546    the section names themselves.  */
3547
3548 #ifndef TEXT_SECTION_LABEL
3549 #define TEXT_SECTION_LABEL              "Ltext"
3550 #endif
3551 #ifndef COLD_TEXT_SECTION_LABEL
3552 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3553 #endif
3554 #ifndef DEBUG_LINE_SECTION_LABEL
3555 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3556 #endif
3557 #ifndef DEBUG_INFO_SECTION_LABEL
3558 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3559 #endif
3560 #ifndef DEBUG_ABBREV_SECTION_LABEL
3561 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3562 #endif
3563 #ifndef DEBUG_LOC_SECTION_LABEL
3564 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3565 #endif
3566 #ifndef DEBUG_RANGES_SECTION_LABEL
3567 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3568 #endif
3569 #ifndef DEBUG_MACINFO_SECTION_LABEL
3570 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3571 #endif
3572 #ifndef DEBUG_MACRO_SECTION_LABEL
3573 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3574 #endif
3575
3576
3577 /* Definitions of defaults for formats and names of various special
3578    (artificial) labels which may be generated within this file (when the -g
3579    options is used and DWARF2_DEBUGGING_INFO is in effect.
3580    If necessary, these may be overridden from within the tm.h file, but
3581    typically, overriding these defaults is unnecessary.  */
3582
3583 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3584 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3585 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3586 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3587 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3588 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3589 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3590 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3591 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3592 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3593
3594 #ifndef TEXT_END_LABEL
3595 #define TEXT_END_LABEL          "Letext"
3596 #endif
3597 #ifndef COLD_END_LABEL
3598 #define COLD_END_LABEL          "Letext_cold"
3599 #endif
3600 #ifndef BLOCK_BEGIN_LABEL
3601 #define BLOCK_BEGIN_LABEL       "LBB"
3602 #endif
3603 #ifndef BLOCK_END_LABEL
3604 #define BLOCK_END_LABEL         "LBE"
3605 #endif
3606 #ifndef LINE_CODE_LABEL
3607 #define LINE_CODE_LABEL         "LM"
3608 #endif
3609
3610 \f
3611 /* Return the root of the DIE's built for the current compilation unit.  */
3612 static dw_die_ref
3613 comp_unit_die (void)
3614 {
3615   if (!single_comp_unit_die)
3616     single_comp_unit_die = gen_compile_unit_die (NULL);
3617   return single_comp_unit_die;
3618 }
3619
3620 /* We allow a language front-end to designate a function that is to be
3621    called to "demangle" any name before it is put into a DIE.  */
3622
3623 static const char *(*demangle_name_func) (const char *);
3624
3625 void
3626 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3627 {
3628   demangle_name_func = func;
3629 }
3630
3631 /* Test if rtl node points to a pseudo register.  */
3632
3633 static inline int
3634 is_pseudo_reg (const_rtx rtl)
3635 {
3636   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3637           || (GET_CODE (rtl) == SUBREG
3638               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3639 }
3640
3641 /* Return a reference to a type, with its const and volatile qualifiers
3642    removed.  */
3643
3644 static inline tree
3645 type_main_variant (tree type)
3646 {
3647   type = TYPE_MAIN_VARIANT (type);
3648
3649   /* ??? There really should be only one main variant among any group of
3650      variants of a given type (and all of the MAIN_VARIANT values for all
3651      members of the group should point to that one type) but sometimes the C
3652      front-end messes this up for array types, so we work around that bug
3653      here.  */
3654   if (TREE_CODE (type) == ARRAY_TYPE)
3655     while (type != TYPE_MAIN_VARIANT (type))
3656       type = TYPE_MAIN_VARIANT (type);
3657
3658   return type;
3659 }
3660
3661 /* Return nonzero if the given type node represents a tagged type.  */
3662
3663 static inline int
3664 is_tagged_type (const_tree type)
3665 {
3666   enum tree_code code = TREE_CODE (type);
3667
3668   return (code == RECORD_TYPE || code == UNION_TYPE
3669           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3670 }
3671
3672 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3673
3674 static void
3675 get_ref_die_offset_label (char *label, dw_die_ref ref)
3676 {
3677   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3678 }
3679
3680 /* Return die_offset of a DIE reference to a base type.  */
3681
3682 static unsigned long int
3683 get_base_type_offset (dw_die_ref ref)
3684 {
3685   if (ref->die_offset)
3686     return ref->die_offset;
3687   if (comp_unit_die ()->die_abbrev)
3688     {
3689       calc_base_type_die_sizes ();
3690       gcc_assert (ref->die_offset);
3691     }
3692   return ref->die_offset;
3693 }
3694
3695 /* Return die_offset of a DIE reference other than base type.  */
3696
3697 static unsigned long int
3698 get_ref_die_offset (dw_die_ref ref)
3699 {
3700   gcc_assert (ref->die_offset);
3701   return ref->die_offset;
3702 }
3703
3704 /* Convert a DIE tag into its string name.  */
3705
3706 static const char *
3707 dwarf_tag_name (unsigned int tag)
3708 {
3709   switch (tag)
3710     {
3711     case DW_TAG_padding:
3712       return "DW_TAG_padding";
3713     case DW_TAG_array_type:
3714       return "DW_TAG_array_type";
3715     case DW_TAG_class_type:
3716       return "DW_TAG_class_type";
3717     case DW_TAG_entry_point:
3718       return "DW_TAG_entry_point";
3719     case DW_TAG_enumeration_type:
3720       return "DW_TAG_enumeration_type";
3721     case DW_TAG_formal_parameter:
3722       return "DW_TAG_formal_parameter";
3723     case DW_TAG_imported_declaration:
3724       return "DW_TAG_imported_declaration";
3725     case DW_TAG_label:
3726       return "DW_TAG_label";
3727     case DW_TAG_lexical_block:
3728       return "DW_TAG_lexical_block";
3729     case DW_TAG_member:
3730       return "DW_TAG_member";
3731     case DW_TAG_pointer_type:
3732       return "DW_TAG_pointer_type";
3733     case DW_TAG_reference_type:
3734       return "DW_TAG_reference_type";
3735     case DW_TAG_compile_unit:
3736       return "DW_TAG_compile_unit";
3737     case DW_TAG_string_type:
3738       return "DW_TAG_string_type";
3739     case DW_TAG_structure_type:
3740       return "DW_TAG_structure_type";
3741     case DW_TAG_subroutine_type:
3742       return "DW_TAG_subroutine_type";
3743     case DW_TAG_typedef:
3744       return "DW_TAG_typedef";
3745     case DW_TAG_union_type:
3746       return "DW_TAG_union_type";
3747     case DW_TAG_unspecified_parameters:
3748       return "DW_TAG_unspecified_parameters";
3749     case DW_TAG_variant:
3750       return "DW_TAG_variant";
3751     case DW_TAG_common_block:
3752       return "DW_TAG_common_block";
3753     case DW_TAG_common_inclusion:
3754       return "DW_TAG_common_inclusion";
3755     case DW_TAG_inheritance:
3756       return "DW_TAG_inheritance";
3757     case DW_TAG_inlined_subroutine:
3758       return "DW_TAG_inlined_subroutine";
3759     case DW_TAG_module:
3760       return "DW_TAG_module";
3761     case DW_TAG_ptr_to_member_type:
3762       return "DW_TAG_ptr_to_member_type";
3763     case DW_TAG_set_type:
3764       return "DW_TAG_set_type";
3765     case DW_TAG_subrange_type:
3766       return "DW_TAG_subrange_type";
3767     case DW_TAG_with_stmt:
3768       return "DW_TAG_with_stmt";
3769     case DW_TAG_access_declaration:
3770       return "DW_TAG_access_declaration";
3771     case DW_TAG_base_type:
3772       return "DW_TAG_base_type";
3773     case DW_TAG_catch_block:
3774       return "DW_TAG_catch_block";
3775     case DW_TAG_const_type:
3776       return "DW_TAG_const_type";
3777     case DW_TAG_constant:
3778       return "DW_TAG_constant";
3779     case DW_TAG_enumerator:
3780       return "DW_TAG_enumerator";
3781     case DW_TAG_file_type:
3782       return "DW_TAG_file_type";
3783     case DW_TAG_friend:
3784       return "DW_TAG_friend";
3785     case DW_TAG_namelist:
3786       return "DW_TAG_namelist";
3787     case DW_TAG_namelist_item:
3788       return "DW_TAG_namelist_item";
3789     case DW_TAG_packed_type:
3790       return "DW_TAG_packed_type";
3791     case DW_TAG_subprogram:
3792       return "DW_TAG_subprogram";
3793     case DW_TAG_template_type_param:
3794       return "DW_TAG_template_type_param";
3795     case DW_TAG_template_value_param:
3796       return "DW_TAG_template_value_param";
3797     case DW_TAG_thrown_type:
3798       return "DW_TAG_thrown_type";
3799     case DW_TAG_try_block:
3800       return "DW_TAG_try_block";
3801     case DW_TAG_variant_part:
3802       return "DW_TAG_variant_part";
3803     case DW_TAG_variable:
3804       return "DW_TAG_variable";
3805     case DW_TAG_volatile_type:
3806       return "DW_TAG_volatile_type";
3807     case DW_TAG_dwarf_procedure:
3808       return "DW_TAG_dwarf_procedure";
3809     case DW_TAG_restrict_type:
3810       return "DW_TAG_restrict_type";
3811     case DW_TAG_interface_type:
3812       return "DW_TAG_interface_type";
3813     case DW_TAG_namespace:
3814       return "DW_TAG_namespace";
3815     case DW_TAG_imported_module:
3816       return "DW_TAG_imported_module";
3817     case DW_TAG_unspecified_type:
3818       return "DW_TAG_unspecified_type";
3819     case DW_TAG_partial_unit:
3820       return "DW_TAG_partial_unit";
3821     case DW_TAG_imported_unit:
3822       return "DW_TAG_imported_unit";
3823     case DW_TAG_condition:
3824       return "DW_TAG_condition";
3825     case DW_TAG_shared_type:
3826       return "DW_TAG_shared_type";
3827     case DW_TAG_type_unit:
3828       return "DW_TAG_type_unit";
3829     case DW_TAG_rvalue_reference_type:
3830       return "DW_TAG_rvalue_reference_type";
3831     case DW_TAG_template_alias:
3832       return "DW_TAG_template_alias";
3833     case DW_TAG_GNU_template_parameter_pack:
3834       return "DW_TAG_GNU_template_parameter_pack";
3835     case DW_TAG_GNU_formal_parameter_pack:
3836       return "DW_TAG_GNU_formal_parameter_pack";
3837     case DW_TAG_MIPS_loop:
3838       return "DW_TAG_MIPS_loop";
3839     case DW_TAG_format_label:
3840       return "DW_TAG_format_label";
3841     case DW_TAG_function_template:
3842       return "DW_TAG_function_template";
3843     case DW_TAG_class_template:
3844       return "DW_TAG_class_template";
3845     case DW_TAG_GNU_BINCL:
3846       return "DW_TAG_GNU_BINCL";
3847     case DW_TAG_GNU_EINCL:
3848       return "DW_TAG_GNU_EINCL";
3849     case DW_TAG_GNU_template_template_param:
3850       return "DW_TAG_GNU_template_template_param";
3851     case DW_TAG_GNU_call_site:
3852       return "DW_TAG_GNU_call_site";
3853     case DW_TAG_GNU_call_site_parameter:
3854       return "DW_TAG_GNU_call_site_parameter";
3855     default:
3856       return "DW_TAG_<unknown>";
3857     }
3858 }
3859
3860 /* Convert a DWARF attribute code into its string name.  */
3861
3862 static const char *
3863 dwarf_attr_name (unsigned int attr)
3864 {
3865   switch (attr)
3866     {
3867     case DW_AT_sibling:
3868       return "DW_AT_sibling";
3869     case DW_AT_location:
3870       return "DW_AT_location";
3871     case DW_AT_name:
3872       return "DW_AT_name";
3873     case DW_AT_ordering:
3874       return "DW_AT_ordering";
3875     case DW_AT_subscr_data:
3876       return "DW_AT_subscr_data";
3877     case DW_AT_byte_size:
3878       return "DW_AT_byte_size";
3879     case DW_AT_bit_offset:
3880       return "DW_AT_bit_offset";
3881     case DW_AT_bit_size:
3882       return "DW_AT_bit_size";
3883     case DW_AT_element_list:
3884       return "DW_AT_element_list";
3885     case DW_AT_stmt_list:
3886       return "DW_AT_stmt_list";
3887     case DW_AT_low_pc:
3888       return "DW_AT_low_pc";
3889     case DW_AT_high_pc:
3890       return "DW_AT_high_pc";
3891     case DW_AT_language:
3892       return "DW_AT_language";
3893     case DW_AT_member:
3894       return "DW_AT_member";
3895     case DW_AT_discr:
3896       return "DW_AT_discr";
3897     case DW_AT_discr_value:
3898       return "DW_AT_discr_value";
3899     case DW_AT_visibility:
3900       return "DW_AT_visibility";
3901     case DW_AT_import:
3902       return "DW_AT_import";
3903     case DW_AT_string_length:
3904       return "DW_AT_string_length";
3905     case DW_AT_common_reference:
3906       return "DW_AT_common_reference";
3907     case DW_AT_comp_dir:
3908       return "DW_AT_comp_dir";
3909     case DW_AT_const_value:
3910       return "DW_AT_const_value";
3911     case DW_AT_containing_type:
3912       return "DW_AT_containing_type";
3913     case DW_AT_default_value:
3914       return "DW_AT_default_value";
3915     case DW_AT_inline:
3916       return "DW_AT_inline";
3917     case DW_AT_is_optional:
3918       return "DW_AT_is_optional";
3919     case DW_AT_lower_bound:
3920       return "DW_AT_lower_bound";
3921     case DW_AT_producer:
3922       return "DW_AT_producer";
3923     case DW_AT_prototyped:
3924       return "DW_AT_prototyped";
3925     case DW_AT_return_addr:
3926       return "DW_AT_return_addr";
3927     case DW_AT_start_scope:
3928       return "DW_AT_start_scope";
3929     case DW_AT_bit_stride:
3930       return "DW_AT_bit_stride";
3931     case DW_AT_upper_bound:
3932       return "DW_AT_upper_bound";
3933     case DW_AT_abstract_origin:
3934       return "DW_AT_abstract_origin";
3935     case DW_AT_accessibility:
3936       return "DW_AT_accessibility";
3937     case DW_AT_address_class:
3938       return "DW_AT_address_class";
3939     case DW_AT_artificial:
3940       return "DW_AT_artificial";
3941     case DW_AT_base_types:
3942       return "DW_AT_base_types";
3943     case DW_AT_calling_convention:
3944       return "DW_AT_calling_convention";
3945     case DW_AT_count:
3946       return "DW_AT_count";
3947     case DW_AT_data_member_location:
3948       return "DW_AT_data_member_location";
3949     case DW_AT_decl_column:
3950       return "DW_AT_decl_column";
3951     case DW_AT_decl_file:
3952       return "DW_AT_decl_file";
3953     case DW_AT_decl_line:
3954       return "DW_AT_decl_line";
3955     case DW_AT_declaration:
3956       return "DW_AT_declaration";
3957     case DW_AT_discr_list:
3958       return "DW_AT_discr_list";
3959     case DW_AT_encoding:
3960       return "DW_AT_encoding";
3961     case DW_AT_external:
3962       return "DW_AT_external";
3963     case DW_AT_explicit:
3964       return "DW_AT_explicit";
3965     case DW_AT_frame_base:
3966       return "DW_AT_frame_base";
3967     case DW_AT_friend:
3968       return "DW_AT_friend";
3969     case DW_AT_identifier_case:
3970       return "DW_AT_identifier_case";
3971     case DW_AT_macro_info:
3972       return "DW_AT_macro_info";
3973     case DW_AT_namelist_items:
3974       return "DW_AT_namelist_items";
3975     case DW_AT_priority:
3976       return "DW_AT_priority";
3977     case DW_AT_segment:
3978       return "DW_AT_segment";
3979     case DW_AT_specification:
3980       return "DW_AT_specification";
3981     case DW_AT_static_link:
3982       return "DW_AT_static_link";
3983     case DW_AT_type:
3984       return "DW_AT_type";
3985     case DW_AT_use_location:
3986       return "DW_AT_use_location";
3987     case DW_AT_variable_parameter:
3988       return "DW_AT_variable_parameter";
3989     case DW_AT_virtuality:
3990       return "DW_AT_virtuality";
3991     case DW_AT_vtable_elem_location:
3992       return "DW_AT_vtable_elem_location";
3993
3994     case DW_AT_allocated:
3995       return "DW_AT_allocated";
3996     case DW_AT_associated:
3997       return "DW_AT_associated";
3998     case DW_AT_data_location:
3999       return "DW_AT_data_location";
4000     case DW_AT_byte_stride:
4001       return "DW_AT_byte_stride";
4002     case DW_AT_entry_pc:
4003       return "DW_AT_entry_pc";
4004     case DW_AT_use_UTF8:
4005       return "DW_AT_use_UTF8";
4006     case DW_AT_extension:
4007       return "DW_AT_extension";
4008     case DW_AT_ranges:
4009       return "DW_AT_ranges";
4010     case DW_AT_trampoline:
4011       return "DW_AT_trampoline";
4012     case DW_AT_call_column:
4013       return "DW_AT_call_column";
4014     case DW_AT_call_file:
4015       return "DW_AT_call_file";
4016     case DW_AT_call_line:
4017       return "DW_AT_call_line";
4018     case DW_AT_object_pointer:
4019       return "DW_AT_object_pointer";
4020
4021     case DW_AT_signature:
4022       return "DW_AT_signature";
4023     case DW_AT_main_subprogram:
4024       return "DW_AT_main_subprogram";
4025     case DW_AT_data_bit_offset:
4026       return "DW_AT_data_bit_offset";
4027     case DW_AT_const_expr:
4028       return "DW_AT_const_expr";
4029     case DW_AT_enum_class:
4030       return "DW_AT_enum_class";
4031     case DW_AT_linkage_name:
4032       return "DW_AT_linkage_name";
4033
4034     case DW_AT_MIPS_fde:
4035       return "DW_AT_MIPS_fde";
4036     case DW_AT_MIPS_loop_begin:
4037       return "DW_AT_MIPS_loop_begin";
4038     case DW_AT_MIPS_tail_loop_begin:
4039       return "DW_AT_MIPS_tail_loop_begin";
4040     case DW_AT_MIPS_epilog_begin:
4041       return "DW_AT_MIPS_epilog_begin";
4042 #if VMS_DEBUGGING_INFO
4043     case DW_AT_HP_prologue:
4044       return "DW_AT_HP_prologue";
4045 #else
4046     case DW_AT_MIPS_loop_unroll_factor:
4047       return "DW_AT_MIPS_loop_unroll_factor";
4048 #endif
4049     case DW_AT_MIPS_software_pipeline_depth:
4050       return "DW_AT_MIPS_software_pipeline_depth";
4051     case DW_AT_MIPS_linkage_name:
4052       return "DW_AT_MIPS_linkage_name";
4053 #if VMS_DEBUGGING_INFO
4054     case DW_AT_HP_epilogue:
4055       return "DW_AT_HP_epilogue";
4056 #else
4057     case DW_AT_MIPS_stride:
4058       return "DW_AT_MIPS_stride";
4059 #endif
4060     case DW_AT_MIPS_abstract_name:
4061       return "DW_AT_MIPS_abstract_name";
4062     case DW_AT_MIPS_clone_origin:
4063       return "DW_AT_MIPS_clone_origin";
4064     case DW_AT_MIPS_has_inlines:
4065       return "DW_AT_MIPS_has_inlines";
4066
4067     case DW_AT_sf_names:
4068       return "DW_AT_sf_names";
4069     case DW_AT_src_info:
4070       return "DW_AT_src_info";
4071     case DW_AT_mac_info:
4072       return "DW_AT_mac_info";
4073     case DW_AT_src_coords:
4074       return "DW_AT_src_coords";
4075     case DW_AT_body_begin:
4076       return "DW_AT_body_begin";
4077     case DW_AT_body_end:
4078       return "DW_AT_body_end";
4079
4080     case DW_AT_GNU_vector:
4081       return "DW_AT_GNU_vector";
4082     case DW_AT_GNU_guarded_by:
4083       return "DW_AT_GNU_guarded_by";
4084     case DW_AT_GNU_pt_guarded_by:
4085       return "DW_AT_GNU_pt_guarded_by";
4086     case DW_AT_GNU_guarded:
4087       return "DW_AT_GNU_guarded";
4088     case DW_AT_GNU_pt_guarded:
4089       return "DW_AT_GNU_pt_guarded";
4090     case DW_AT_GNU_locks_excluded:
4091       return "DW_AT_GNU_locks_excluded";
4092     case DW_AT_GNU_exclusive_locks_required:
4093       return "DW_AT_GNU_exclusive_locks_required";
4094     case DW_AT_GNU_shared_locks_required:
4095       return "DW_AT_GNU_shared_locks_required";
4096     case DW_AT_GNU_odr_signature:
4097       return "DW_AT_GNU_odr_signature";
4098     case DW_AT_GNU_template_name:
4099       return "DW_AT_GNU_template_name";
4100     case DW_AT_GNU_call_site_value:
4101       return "DW_AT_GNU_call_site_value";
4102     case DW_AT_GNU_call_site_data_value:
4103       return "DW_AT_GNU_call_site_data_value";
4104     case DW_AT_GNU_call_site_target:
4105       return "DW_AT_GNU_call_site_target";
4106     case DW_AT_GNU_call_site_target_clobbered:
4107       return "DW_AT_GNU_call_site_target_clobbered";
4108     case DW_AT_GNU_tail_call:
4109       return "DW_AT_GNU_tail_call";
4110     case DW_AT_GNU_all_tail_call_sites:
4111       return "DW_AT_GNU_all_tail_call_sites";
4112     case DW_AT_GNU_all_call_sites:
4113       return "DW_AT_GNU_all_call_sites";
4114     case DW_AT_GNU_all_source_call_sites:
4115       return "DW_AT_GNU_all_source_call_sites";
4116     case DW_AT_GNU_macros:
4117       return "DW_AT_GNU_macros";
4118
4119     case DW_AT_GNAT_descriptive_type:
4120       return "DW_AT_GNAT_descriptive_type";
4121
4122     case DW_AT_VMS_rtnbeg_pd_address:
4123       return "DW_AT_VMS_rtnbeg_pd_address";
4124
4125     default:
4126       return "DW_AT_<unknown>";
4127     }
4128 }
4129
4130 /* Convert a DWARF value form code into its string name.  */
4131
4132 static const char *
4133 dwarf_form_name (unsigned int form)
4134 {
4135   switch (form)
4136     {
4137     case DW_FORM_addr:
4138       return "DW_FORM_addr";
4139     case DW_FORM_block2:
4140       return "DW_FORM_block2";
4141     case DW_FORM_block4:
4142       return "DW_FORM_block4";
4143     case DW_FORM_data2:
4144       return "DW_FORM_data2";
4145     case DW_FORM_data4:
4146       return "DW_FORM_data4";
4147     case DW_FORM_data8:
4148       return "DW_FORM_data8";
4149     case DW_FORM_string:
4150       return "DW_FORM_string";
4151     case DW_FORM_block:
4152       return "DW_FORM_block";
4153     case DW_FORM_block1:
4154       return "DW_FORM_block1";
4155     case DW_FORM_data1:
4156       return "DW_FORM_data1";
4157     case DW_FORM_flag:
4158       return "DW_FORM_flag";
4159     case DW_FORM_sdata:
4160       return "DW_FORM_sdata";
4161     case DW_FORM_strp:
4162       return "DW_FORM_strp";
4163     case DW_FORM_udata:
4164       return "DW_FORM_udata";
4165     case DW_FORM_ref_addr:
4166       return "DW_FORM_ref_addr";
4167     case DW_FORM_ref1:
4168       return "DW_FORM_ref1";
4169     case DW_FORM_ref2:
4170       return "DW_FORM_ref2";
4171     case DW_FORM_ref4:
4172       return "DW_FORM_ref4";
4173     case DW_FORM_ref8:
4174       return "DW_FORM_ref8";
4175     case DW_FORM_ref_udata:
4176       return "DW_FORM_ref_udata";
4177     case DW_FORM_indirect:
4178       return "DW_FORM_indirect";
4179     case DW_FORM_sec_offset:
4180       return "DW_FORM_sec_offset";
4181     case DW_FORM_exprloc:
4182       return "DW_FORM_exprloc";
4183     case DW_FORM_flag_present:
4184       return "DW_FORM_flag_present";
4185     case DW_FORM_ref_sig8:
4186       return "DW_FORM_ref_sig8";
4187     default:
4188       return "DW_FORM_<unknown>";
4189     }
4190 }
4191 \f
4192 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4193    instance of an inlined instance of a decl which is local to an inline
4194    function, so we have to trace all of the way back through the origin chain
4195    to find out what sort of node actually served as the original seed for the
4196    given block.  */
4197
4198 static tree
4199 decl_ultimate_origin (const_tree decl)
4200 {
4201   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4202     return NULL_TREE;
4203
4204   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4205      nodes in the function to point to themselves; ignore that if
4206      we're trying to output the abstract instance of this function.  */
4207   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4208     return NULL_TREE;
4209
4210   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4211      most distant ancestor, this should never happen.  */
4212   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4213
4214   return DECL_ABSTRACT_ORIGIN (decl);
4215 }
4216
4217 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4218    of a virtual function may refer to a base class, so we check the 'this'
4219    parameter.  */
4220
4221 static tree
4222 decl_class_context (tree decl)
4223 {
4224   tree context = NULL_TREE;
4225
4226   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4227     context = DECL_CONTEXT (decl);
4228   else
4229     context = TYPE_MAIN_VARIANT
4230       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4231
4232   if (context && !TYPE_P (context))
4233     context = NULL_TREE;
4234
4235   return context;
4236 }
4237 \f
4238 /* Add an attribute/value pair to a DIE.  */
4239
4240 static inline void
4241 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4242 {
4243   /* Maybe this should be an assert?  */
4244   if (die == NULL)
4245     return;
4246
4247   if (die->die_attr == NULL)
4248     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4249   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4250 }
4251
4252 static inline enum dw_val_class
4253 AT_class (dw_attr_ref a)
4254 {
4255   return a->dw_attr_val.val_class;
4256 }
4257
4258 /* Add a flag value attribute to a DIE.  */
4259
4260 static inline void
4261 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4262 {
4263   dw_attr_node attr;
4264
4265   attr.dw_attr = attr_kind;
4266   attr.dw_attr_val.val_class = dw_val_class_flag;
4267   attr.dw_attr_val.v.val_flag = flag;
4268   add_dwarf_attr (die, &attr);
4269 }
4270
4271 static inline unsigned
4272 AT_flag (dw_attr_ref a)
4273 {
4274   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4275   return a->dw_attr_val.v.val_flag;
4276 }
4277
4278 /* Add a signed integer attribute value to a DIE.  */
4279
4280 static inline void
4281 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4282 {
4283   dw_attr_node attr;
4284
4285   attr.dw_attr = attr_kind;
4286   attr.dw_attr_val.val_class = dw_val_class_const;
4287   attr.dw_attr_val.v.val_int = int_val;
4288   add_dwarf_attr (die, &attr);
4289 }
4290
4291 static inline HOST_WIDE_INT
4292 AT_int (dw_attr_ref a)
4293 {
4294   gcc_assert (a && AT_class (a) == dw_val_class_const);
4295   return a->dw_attr_val.v.val_int;
4296 }
4297
4298 /* Add an unsigned integer attribute value to a DIE.  */
4299
4300 static inline void
4301 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4302                  unsigned HOST_WIDE_INT unsigned_val)
4303 {
4304   dw_attr_node attr;
4305
4306   attr.dw_attr = attr_kind;
4307   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4308   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4309   add_dwarf_attr (die, &attr);
4310 }
4311
4312 static inline unsigned HOST_WIDE_INT
4313 AT_unsigned (dw_attr_ref a)
4314 {
4315   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4316   return a->dw_attr_val.v.val_unsigned;
4317 }
4318
4319 /* Add an unsigned double integer attribute value to a DIE.  */
4320
4321 static inline void
4322 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4323                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4324 {
4325   dw_attr_node attr;
4326
4327   attr.dw_attr = attr_kind;
4328   attr.dw_attr_val.val_class = dw_val_class_const_double;
4329   attr.dw_attr_val.v.val_double.high = high;
4330   attr.dw_attr_val.v.val_double.low = low;
4331   add_dwarf_attr (die, &attr);
4332 }
4333
4334 /* Add a floating point attribute value to a DIE and return it.  */
4335
4336 static inline void
4337 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4338             unsigned int length, unsigned int elt_size, unsigned char *array)
4339 {
4340   dw_attr_node attr;
4341
4342   attr.dw_attr = attr_kind;
4343   attr.dw_attr_val.val_class = dw_val_class_vec;
4344   attr.dw_attr_val.v.val_vec.length = length;
4345   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4346   attr.dw_attr_val.v.val_vec.array = array;
4347   add_dwarf_attr (die, &attr);
4348 }
4349
4350 /* Add an 8-byte data attribute value to a DIE.  */
4351
4352 static inline void
4353 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4354               unsigned char data8[8])
4355 {
4356   dw_attr_node attr;
4357
4358   attr.dw_attr = attr_kind;
4359   attr.dw_attr_val.val_class = dw_val_class_data8;
4360   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4361   add_dwarf_attr (die, &attr);
4362 }
4363
4364 /* Hash and equality functions for debug_str_hash.  */
4365
4366 static hashval_t
4367 debug_str_do_hash (const void *x)
4368 {
4369   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4370 }
4371
4372 static int
4373 debug_str_eq (const void *x1, const void *x2)
4374 {
4375   return strcmp ((((const struct indirect_string_node *)x1)->str),
4376                  (const char *)x2) == 0;
4377 }
4378
4379 /* Add STR to the indirect string hash table.  */
4380
4381 static struct indirect_string_node *
4382 find_AT_string (const char *str)
4383 {
4384   struct indirect_string_node *node;
4385   void **slot;
4386
4387   if (! debug_str_hash)
4388     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4389                                       debug_str_eq, NULL);
4390
4391   slot = htab_find_slot_with_hash (debug_str_hash, str,
4392                                    htab_hash_string (str), INSERT);
4393   if (*slot == NULL)
4394     {
4395       node = ggc_alloc_cleared_indirect_string_node ();
4396       node->str = ggc_strdup (str);
4397       *slot = node;
4398     }
4399   else
4400     node = (struct indirect_string_node *) *slot;
4401
4402   node->refcount++;
4403   return node;
4404 }
4405
4406 /* Add a string attribute value to a DIE.  */
4407
4408 static inline void
4409 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4410 {
4411   dw_attr_node attr;
4412   struct indirect_string_node *node;
4413
4414   node = find_AT_string (str);
4415
4416   attr.dw_attr = attr_kind;
4417   attr.dw_attr_val.val_class = dw_val_class_str;
4418   attr.dw_attr_val.v.val_str = node;
4419   add_dwarf_attr (die, &attr);
4420 }
4421
4422 static inline const char *
4423 AT_string (dw_attr_ref a)
4424 {
4425   gcc_assert (a && AT_class (a) == dw_val_class_str);
4426   return a->dw_attr_val.v.val_str->str;
4427 }
4428
4429 /* Find out whether a string should be output inline in DIE
4430    or out-of-line in .debug_str section.  */
4431
4432 static enum dwarf_form
4433 AT_string_form (dw_attr_ref a)
4434 {
4435   struct indirect_string_node *node;
4436   unsigned int len;
4437   char label[32];
4438
4439   gcc_assert (a && AT_class (a) == dw_val_class_str);
4440
4441   node = a->dw_attr_val.v.val_str;
4442   if (node->form)
4443     return node->form;
4444
4445   len = strlen (node->str) + 1;
4446
4447   /* If the string is shorter or equal to the size of the reference, it is
4448      always better to put it inline.  */
4449   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4450     return node->form = DW_FORM_string;
4451
4452   /* If we cannot expect the linker to merge strings in .debug_str
4453      section, only put it into .debug_str if it is worth even in this
4454      single module.  */
4455   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4456       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4457       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4458     return node->form = DW_FORM_string;
4459
4460   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4461   ++dw2_string_counter;
4462   node->label = xstrdup (label);
4463
4464   return node->form = DW_FORM_strp;
4465 }
4466
4467 /* Add a DIE reference attribute value to a DIE.  */
4468
4469 static inline void
4470 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4471 {
4472   dw_attr_node attr;
4473
4474 #ifdef ENABLE_CHECKING
4475   gcc_assert (targ_die != NULL);
4476 #else
4477   /* With LTO we can end up trying to reference something we didn't create
4478      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4479   if (targ_die == NULL)
4480     return;
4481 #endif
4482
4483   attr.dw_attr = attr_kind;
4484   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4485   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4486   attr.dw_attr_val.v.val_die_ref.external = 0;
4487   add_dwarf_attr (die, &attr);
4488 }
4489
4490 /* Add an AT_specification attribute to a DIE, and also make the back
4491    pointer from the specification to the definition.  */
4492
4493 static inline void
4494 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4495 {
4496   add_AT_die_ref (die, DW_AT_specification, targ_die);
4497   gcc_assert (!targ_die->die_definition);
4498   targ_die->die_definition = die;
4499 }
4500
4501 static inline dw_die_ref
4502 AT_ref (dw_attr_ref a)
4503 {
4504   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4505   return a->dw_attr_val.v.val_die_ref.die;
4506 }
4507
4508 static inline int
4509 AT_ref_external (dw_attr_ref a)
4510 {
4511   if (a && AT_class (a) == dw_val_class_die_ref)
4512     return a->dw_attr_val.v.val_die_ref.external;
4513
4514   return 0;
4515 }
4516
4517 static inline void
4518 set_AT_ref_external (dw_attr_ref a, int i)
4519 {
4520   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4521   a->dw_attr_val.v.val_die_ref.external = i;
4522 }
4523
4524 /* Add an FDE reference attribute value to a DIE.  */
4525
4526 static inline void
4527 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4528 {
4529   dw_attr_node attr;
4530
4531   attr.dw_attr = attr_kind;
4532   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4533   attr.dw_attr_val.v.val_fde_index = targ_fde;
4534   add_dwarf_attr (die, &attr);
4535 }
4536
4537 /* Add a location description attribute value to a DIE.  */
4538
4539 static inline void
4540 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4541 {
4542   dw_attr_node attr;
4543
4544   attr.dw_attr = attr_kind;
4545   attr.dw_attr_val.val_class = dw_val_class_loc;
4546   attr.dw_attr_val.v.val_loc = loc;
4547   add_dwarf_attr (die, &attr);
4548 }
4549
4550 static inline dw_loc_descr_ref
4551 AT_loc (dw_attr_ref a)
4552 {
4553   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4554   return a->dw_attr_val.v.val_loc;
4555 }
4556
4557 static inline void
4558 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4559 {
4560   dw_attr_node attr;
4561
4562   attr.dw_attr = attr_kind;
4563   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4564   attr.dw_attr_val.v.val_loc_list = loc_list;
4565   add_dwarf_attr (die, &attr);
4566   have_location_lists = true;
4567 }
4568
4569 static inline dw_loc_list_ref
4570 AT_loc_list (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 static inline dw_loc_list_ref *
4577 AT_loc_list_ptr (dw_attr_ref a)
4578 {
4579   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4580   return &a->dw_attr_val.v.val_loc_list;
4581 }
4582
4583 /* Add an address constant attribute value to a DIE.  */
4584
4585 static inline void
4586 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4587 {
4588   dw_attr_node attr;
4589
4590   attr.dw_attr = attr_kind;
4591   attr.dw_attr_val.val_class = dw_val_class_addr;
4592   attr.dw_attr_val.v.val_addr = addr;
4593   add_dwarf_attr (die, &attr);
4594 }
4595
4596 /* Get the RTX from to an address DIE attribute.  */
4597
4598 static inline rtx
4599 AT_addr (dw_attr_ref a)
4600 {
4601   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4602   return a->dw_attr_val.v.val_addr;
4603 }
4604
4605 /* Add a file attribute value to a DIE.  */
4606
4607 static inline void
4608 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4609              struct dwarf_file_data *fd)
4610 {
4611   dw_attr_node attr;
4612
4613   attr.dw_attr = attr_kind;
4614   attr.dw_attr_val.val_class = dw_val_class_file;
4615   attr.dw_attr_val.v.val_file = fd;
4616   add_dwarf_attr (die, &attr);
4617 }
4618
4619 /* Get the dwarf_file_data from a file DIE attribute.  */
4620
4621 static inline struct dwarf_file_data *
4622 AT_file (dw_attr_ref a)
4623 {
4624   gcc_assert (a && AT_class (a) == dw_val_class_file);
4625   return a->dw_attr_val.v.val_file;
4626 }
4627
4628 /* Add a vms delta attribute value to a DIE.  */
4629
4630 static inline void
4631 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4632                   const char *lbl1, const char *lbl2)
4633 {
4634   dw_attr_node attr;
4635
4636   attr.dw_attr = attr_kind;
4637   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4638   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4639   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4640   add_dwarf_attr (die, &attr);
4641 }
4642
4643 /* Add a label identifier attribute value to a DIE.  */
4644
4645 static inline void
4646 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4647 {
4648   dw_attr_node attr;
4649
4650   attr.dw_attr = attr_kind;
4651   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4652   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4653   add_dwarf_attr (die, &attr);
4654 }
4655
4656 /* Add a section offset attribute value to a DIE, an offset into the
4657    debug_line section.  */
4658
4659 static inline void
4660 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4661                 const char *label)
4662 {
4663   dw_attr_node attr;
4664
4665   attr.dw_attr = attr_kind;
4666   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4667   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4668   add_dwarf_attr (die, &attr);
4669 }
4670
4671 /* Add a section offset attribute value to a DIE, an offset into the
4672    debug_macinfo section.  */
4673
4674 static inline void
4675 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4676                const char *label)
4677 {
4678   dw_attr_node attr;
4679
4680   attr.dw_attr = attr_kind;
4681   attr.dw_attr_val.val_class = dw_val_class_macptr;
4682   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4683   add_dwarf_attr (die, &attr);
4684 }
4685
4686 /* Add an offset attribute value to a DIE.  */
4687
4688 static inline void
4689 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4690                unsigned HOST_WIDE_INT offset)
4691 {
4692   dw_attr_node attr;
4693
4694   attr.dw_attr = attr_kind;
4695   attr.dw_attr_val.val_class = dw_val_class_offset;
4696   attr.dw_attr_val.v.val_offset = offset;
4697   add_dwarf_attr (die, &attr);
4698 }
4699
4700 /* Add an range_list attribute value to a DIE.  */
4701
4702 static void
4703 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4704                    long unsigned int offset)
4705 {
4706   dw_attr_node attr;
4707
4708   attr.dw_attr = attr_kind;
4709   attr.dw_attr_val.val_class = dw_val_class_range_list;
4710   attr.dw_attr_val.v.val_offset = offset;
4711   add_dwarf_attr (die, &attr);
4712 }
4713
4714 /* Return the start label of a delta attribute.  */
4715
4716 static inline const char *
4717 AT_vms_delta1 (dw_attr_ref a)
4718 {
4719   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4720   return a->dw_attr_val.v.val_vms_delta.lbl1;
4721 }
4722
4723 /* Return the end label of a delta attribute.  */
4724
4725 static inline const char *
4726 AT_vms_delta2 (dw_attr_ref a)
4727 {
4728   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4729   return a->dw_attr_val.v.val_vms_delta.lbl2;
4730 }
4731
4732 static inline const char *
4733 AT_lbl (dw_attr_ref a)
4734 {
4735   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4736                     || AT_class (a) == dw_val_class_lineptr
4737                     || AT_class (a) == dw_val_class_macptr));
4738   return a->dw_attr_val.v.val_lbl_id;
4739 }
4740
4741 /* Get the attribute of type attr_kind.  */
4742
4743 static dw_attr_ref
4744 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4745 {
4746   dw_attr_ref a;
4747   unsigned ix;
4748   dw_die_ref spec = NULL;
4749
4750   if (! die)
4751     return NULL;
4752
4753   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4754     if (a->dw_attr == attr_kind)
4755       return a;
4756     else if (a->dw_attr == DW_AT_specification
4757              || a->dw_attr == DW_AT_abstract_origin)
4758       spec = AT_ref (a);
4759
4760   if (spec)
4761     return get_AT (spec, attr_kind);
4762
4763   return NULL;
4764 }
4765
4766 /* Return the "low pc" attribute value, typically associated with a subprogram
4767    DIE.  Return null if the "low pc" attribute is either not present, or if it
4768    cannot be represented as an assembler label identifier.  */
4769
4770 static inline const char *
4771 get_AT_low_pc (dw_die_ref die)
4772 {
4773   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4774
4775   return a ? AT_lbl (a) : NULL;
4776 }
4777
4778 /* Return the "high pc" attribute value, typically associated with a subprogram
4779    DIE.  Return null if the "high pc" attribute is either not present, or if it
4780    cannot be represented as an assembler label identifier.  */
4781
4782 static inline const char *
4783 get_AT_hi_pc (dw_die_ref die)
4784 {
4785   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4786
4787   return a ? AT_lbl (a) : NULL;
4788 }
4789
4790 /* Return the value of the string attribute designated by ATTR_KIND, or
4791    NULL if it is not present.  */
4792
4793 static inline const char *
4794 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4795 {
4796   dw_attr_ref a = get_AT (die, attr_kind);
4797
4798   return a ? AT_string (a) : NULL;
4799 }
4800
4801 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4802    if it is not present.  */
4803
4804 static inline int
4805 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4806 {
4807   dw_attr_ref a = get_AT (die, attr_kind);
4808
4809   return a ? AT_flag (a) : 0;
4810 }
4811
4812 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4813    if it is not present.  */
4814
4815 static inline unsigned
4816 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4817 {
4818   dw_attr_ref a = get_AT (die, attr_kind);
4819
4820   return a ? AT_unsigned (a) : 0;
4821 }
4822
4823 static inline dw_die_ref
4824 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4825 {
4826   dw_attr_ref a = get_AT (die, attr_kind);
4827
4828   return a ? AT_ref (a) : NULL;
4829 }
4830
4831 static inline struct dwarf_file_data *
4832 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4833 {
4834   dw_attr_ref a = get_AT (die, attr_kind);
4835
4836   return a ? AT_file (a) : NULL;
4837 }
4838
4839 /* Return TRUE if the language is C++.  */
4840
4841 static inline bool
4842 is_cxx (void)
4843 {
4844   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4845
4846   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4847 }
4848
4849 /* Return TRUE if the language is Fortran.  */
4850
4851 static inline bool
4852 is_fortran (void)
4853 {
4854   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4855
4856   return (lang == DW_LANG_Fortran77
4857           || lang == DW_LANG_Fortran90
4858           || lang == DW_LANG_Fortran95);
4859 }
4860
4861 /* Return TRUE if the language is Ada.  */
4862
4863 static inline bool
4864 is_ada (void)
4865 {
4866   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4867
4868   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4869 }
4870
4871 /* Remove the specified attribute if present.  */
4872
4873 static void
4874 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4875 {
4876   dw_attr_ref a;
4877   unsigned ix;
4878
4879   if (! die)
4880     return;
4881
4882   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4883     if (a->dw_attr == attr_kind)
4884       {
4885         if (AT_class (a) == dw_val_class_str)
4886           if (a->dw_attr_val.v.val_str->refcount)
4887             a->dw_attr_val.v.val_str->refcount--;
4888
4889         /* VEC_ordered_remove should help reduce the number of abbrevs
4890            that are needed.  */
4891         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4892         return;
4893       }
4894 }
4895
4896 /* Remove CHILD from its parent.  PREV must have the property that
4897    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4898
4899 static void
4900 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4901 {
4902   gcc_assert (child->die_parent == prev->die_parent);
4903   gcc_assert (prev->die_sib == child);
4904   if (prev == child)
4905     {
4906       gcc_assert (child->die_parent->die_child == child);
4907       prev = NULL;
4908     }
4909   else
4910     prev->die_sib = child->die_sib;
4911   if (child->die_parent->die_child == child)
4912     child->die_parent->die_child = prev;
4913 }
4914
4915 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4916    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4917
4918 static void
4919 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4920 {
4921   dw_die_ref parent = old_child->die_parent;
4922
4923   gcc_assert (parent == prev->die_parent);
4924   gcc_assert (prev->die_sib == old_child);
4925
4926   new_child->die_parent = parent;
4927   if (prev == old_child)
4928     {
4929       gcc_assert (parent->die_child == old_child);
4930       new_child->die_sib = new_child;
4931     }
4932   else
4933     {
4934       prev->die_sib = new_child;
4935       new_child->die_sib = old_child->die_sib;
4936     }
4937   if (old_child->die_parent->die_child == old_child)
4938     old_child->die_parent->die_child = new_child;
4939 }
4940
4941 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4942
4943 static void
4944 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4945 {
4946   dw_die_ref c;
4947   new_parent->die_child = old_parent->die_child;
4948   old_parent->die_child = NULL;
4949   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4950 }
4951
4952 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4953    matches TAG.  */
4954
4955 static void
4956 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4957 {
4958   dw_die_ref c;
4959
4960   c = die->die_child;
4961   if (c) do {
4962     dw_die_ref prev = c;
4963     c = c->die_sib;
4964     while (c->die_tag == tag)
4965       {
4966         remove_child_with_prev (c, prev);
4967         /* Might have removed every child.  */
4968         if (c == c->die_sib)
4969           return;
4970         c = c->die_sib;
4971       }
4972   } while (c != die->die_child);
4973 }
4974
4975 /* Add a CHILD_DIE as the last child of DIE.  */
4976
4977 static void
4978 add_child_die (dw_die_ref die, dw_die_ref child_die)
4979 {
4980   /* FIXME this should probably be an assert.  */
4981   if (! die || ! child_die)
4982     return;
4983   gcc_assert (die != child_die);
4984
4985   child_die->die_parent = die;
4986   if (die->die_child)
4987     {
4988       child_die->die_sib = die->die_child->die_sib;
4989       die->die_child->die_sib = child_die;
4990     }
4991   else
4992     child_die->die_sib = child_die;
4993   die->die_child = child_die;
4994 }
4995
4996 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4997    is the specification, to the end of PARENT's list of children.
4998    This is done by removing and re-adding it.  */
4999
5000 static void
5001 splice_child_die (dw_die_ref parent, dw_die_ref child)
5002 {
5003   dw_die_ref p;
5004
5005   /* We want the declaration DIE from inside the class, not the
5006      specification DIE at toplevel.  */
5007   if (child->die_parent != parent)
5008     {
5009       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5010
5011       if (tmp)
5012         child = tmp;
5013     }
5014
5015   gcc_assert (child->die_parent == parent
5016               || (child->die_parent
5017                   == get_AT_ref (parent, DW_AT_specification)));
5018
5019   for (p = child->die_parent->die_child; ; p = p->die_sib)
5020     if (p->die_sib == child)
5021       {
5022         remove_child_with_prev (child, p);
5023         break;
5024       }
5025
5026   add_child_die (parent, child);
5027 }
5028
5029 /* Return a pointer to a newly created DIE node.  */
5030
5031 static inline dw_die_ref
5032 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5033 {
5034   dw_die_ref die = ggc_alloc_cleared_die_node ();
5035
5036   die->die_tag = tag_value;
5037
5038   if (parent_die != NULL)
5039     add_child_die (parent_die, die);
5040   else
5041     {
5042       limbo_die_node *limbo_node;
5043
5044       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5045       limbo_node->die = die;
5046       limbo_node->created_for = t;
5047       limbo_node->next = limbo_die_list;
5048       limbo_die_list = limbo_node;
5049     }
5050
5051   return die;
5052 }
5053
5054 /* Return the DIE associated with the given type specifier.  */
5055
5056 static inline dw_die_ref
5057 lookup_type_die (tree type)
5058 {
5059   return TYPE_SYMTAB_DIE (type);
5060 }
5061
5062 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5063    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5064    anonymous type instead the one of the naming typedef.  */
5065
5066 static inline dw_die_ref
5067 strip_naming_typedef (tree type, dw_die_ref type_die)
5068 {
5069   if (type
5070       && TREE_CODE (type) == RECORD_TYPE
5071       && type_die
5072       && type_die->die_tag == DW_TAG_typedef
5073       && is_naming_typedef_decl (TYPE_NAME (type)))
5074     type_die = get_AT_ref (type_die, DW_AT_type);
5075   return type_die;
5076 }
5077
5078 /* Like lookup_type_die, but if type is an anonymous type named by a
5079    typedef[1], return the DIE of the anonymous type instead the one of
5080    the naming typedef.  This is because in gen_typedef_die, we did
5081    equate the anonymous struct named by the typedef with the DIE of
5082    the naming typedef. So by default, lookup_type_die on an anonymous
5083    struct yields the DIE of the naming typedef.
5084
5085    [1]: Read the comment of is_naming_typedef_decl to learn about what
5086    a naming typedef is.  */
5087
5088 static inline dw_die_ref
5089 lookup_type_die_strip_naming_typedef (tree type)
5090 {
5091   dw_die_ref die = lookup_type_die (type);
5092   return strip_naming_typedef (type, die);
5093 }
5094
5095 /* Equate a DIE to a given type specifier.  */
5096
5097 static inline void
5098 equate_type_number_to_die (tree type, dw_die_ref type_die)
5099 {
5100   TYPE_SYMTAB_DIE (type) = type_die;
5101 }
5102
5103 /* Returns a hash value for X (which really is a die_struct).  */
5104
5105 static hashval_t
5106 decl_die_table_hash (const void *x)
5107 {
5108   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5109 }
5110
5111 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5112
5113 static int
5114 decl_die_table_eq (const void *x, const void *y)
5115 {
5116   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5117 }
5118
5119 /* Return the DIE associated with a given declaration.  */
5120
5121 static inline dw_die_ref
5122 lookup_decl_die (tree decl)
5123 {
5124   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5125 }
5126
5127 /* Returns a hash value for X (which really is a var_loc_list).  */
5128
5129 static hashval_t
5130 decl_loc_table_hash (const void *x)
5131 {
5132   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5133 }
5134
5135 /* Return nonzero if decl_id of var_loc_list X is the same as
5136    UID of decl *Y.  */
5137
5138 static int
5139 decl_loc_table_eq (const void *x, const void *y)
5140 {
5141   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5142 }
5143
5144 /* Return the var_loc list associated with a given declaration.  */
5145
5146 static inline var_loc_list *
5147 lookup_decl_loc (const_tree decl)
5148 {
5149   if (!decl_loc_table)
5150     return NULL;
5151   return (var_loc_list *)
5152     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5153 }
5154
5155 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5156
5157 static hashval_t
5158 cached_dw_loc_list_table_hash (const void *x)
5159 {
5160   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5161 }
5162
5163 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5164    UID of decl *Y.  */
5165
5166 static int
5167 cached_dw_loc_list_table_eq (const void *x, const void *y)
5168 {
5169   return (((const cached_dw_loc_list *) x)->decl_id
5170           == DECL_UID ((const_tree) y));
5171 }
5172
5173 /* Equate a DIE to a particular declaration.  */
5174
5175 static void
5176 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5177 {
5178   unsigned int decl_id = DECL_UID (decl);
5179   void **slot;
5180
5181   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5182   *slot = decl_die;
5183   decl_die->decl_id = decl_id;
5184 }
5185
5186 /* Return how many bits covers PIECE EXPR_LIST.  */
5187
5188 static int
5189 decl_piece_bitsize (rtx piece)
5190 {
5191   int ret = (int) GET_MODE (piece);
5192   if (ret)
5193     return ret;
5194   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5195               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5196   return INTVAL (XEXP (XEXP (piece, 0), 0));
5197 }
5198
5199 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5200
5201 static rtx *
5202 decl_piece_varloc_ptr (rtx piece)
5203 {
5204   if ((int) GET_MODE (piece))
5205     return &XEXP (piece, 0);
5206   else
5207     return &XEXP (XEXP (piece, 0), 1);
5208 }
5209
5210 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5211    Next is the chain of following piece nodes.  */
5212
5213 static rtx
5214 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5215 {
5216   if (bitsize <= (int) MAX_MACHINE_MODE)
5217     return alloc_EXPR_LIST (bitsize, loc_note, next);
5218   else
5219     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5220                                                GEN_INT (bitsize),
5221                                                loc_note), next);
5222 }
5223
5224 /* Return rtx that should be stored into loc field for
5225    LOC_NOTE and BITPOS/BITSIZE.  */
5226
5227 static rtx
5228 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5229                       HOST_WIDE_INT bitsize)
5230 {
5231   if (bitsize != -1)
5232     {
5233       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5234       if (bitpos != 0)
5235         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5236     }
5237   return loc_note;
5238 }
5239
5240 /* This function either modifies location piece list *DEST in
5241    place (if SRC and INNER is NULL), or copies location piece list
5242    *SRC to *DEST while modifying it.  Location BITPOS is modified
5243    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5244    not copied and if needed some padding around it is added.
5245    When modifying in place, DEST should point to EXPR_LIST where
5246    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5247    to the start of the whole list and INNER points to the EXPR_LIST
5248    where earlier pieces cover PIECE_BITPOS bits.  */
5249
5250 static void
5251 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5252                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5253                    HOST_WIDE_INT bitsize, rtx loc_note)
5254 {
5255   int diff;
5256   bool copy = inner != NULL;
5257
5258   if (copy)
5259     {
5260       /* First copy all nodes preceeding the current bitpos.  */
5261       while (src != inner)
5262         {
5263           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5264                                    decl_piece_bitsize (*src), NULL_RTX);
5265           dest = &XEXP (*dest, 1);
5266           src = &XEXP (*src, 1);
5267         }
5268     }
5269   /* Add padding if needed.  */
5270   if (bitpos != piece_bitpos)
5271     {
5272       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5273                                copy ? NULL_RTX : *dest);
5274       dest = &XEXP (*dest, 1);
5275     }
5276   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5277     {
5278       gcc_assert (!copy);
5279       /* A piece with correct bitpos and bitsize already exist,
5280          just update the location for it and return.  */
5281       *decl_piece_varloc_ptr (*dest) = loc_note;
5282       return;
5283     }
5284   /* Add the piece that changed.  */
5285   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5286   dest = &XEXP (*dest, 1);
5287   /* Skip over pieces that overlap it.  */
5288   diff = bitpos - piece_bitpos + bitsize;
5289   if (!copy)
5290     src = dest;
5291   while (diff > 0 && *src)
5292     {
5293       rtx piece = *src;
5294       diff -= decl_piece_bitsize (piece);
5295       if (copy)
5296         src = &XEXP (piece, 1);
5297       else
5298         {
5299           *src = XEXP (piece, 1);
5300           free_EXPR_LIST_node (piece);
5301         }
5302     }
5303   /* Add padding if needed.  */
5304   if (diff < 0 && *src)
5305     {
5306       if (!copy)
5307         dest = src;
5308       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5309       dest = &XEXP (*dest, 1);
5310     }
5311   if (!copy)
5312     return;
5313   /* Finally copy all nodes following it.  */
5314   while (*src)
5315     {
5316       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5317                                decl_piece_bitsize (*src), NULL_RTX);
5318       dest = &XEXP (*dest, 1);
5319       src = &XEXP (*src, 1);
5320     }
5321 }
5322
5323 /* Add a variable location node to the linked list for DECL.  */
5324
5325 static struct var_loc_node *
5326 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5327 {
5328   unsigned int decl_id;
5329   var_loc_list *temp;
5330   void **slot;
5331   struct var_loc_node *loc = NULL;
5332   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5333
5334   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5335     {
5336       tree realdecl = DECL_DEBUG_EXPR (decl);
5337       if (realdecl && handled_component_p (realdecl))
5338         {
5339           HOST_WIDE_INT maxsize;
5340           tree innerdecl;
5341           innerdecl
5342             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5343           if (!DECL_P (innerdecl)
5344               || DECL_IGNORED_P (innerdecl)
5345               || TREE_STATIC (innerdecl)
5346               || bitsize <= 0
5347               || bitpos + bitsize > 256
5348               || bitsize != maxsize)
5349             return NULL;
5350           decl = innerdecl;
5351         }
5352     }
5353
5354   decl_id = DECL_UID (decl);
5355   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5356   if (*slot == NULL)
5357     {
5358       temp = ggc_alloc_cleared_var_loc_list ();
5359       temp->decl_id = decl_id;
5360       *slot = temp;
5361     }
5362   else
5363     temp = (var_loc_list *) *slot;
5364
5365   /* For PARM_DECLs try to keep around the original incoming value,
5366      even if that means we'll emit a zero-range .debug_loc entry.  */
5367   if (temp->last
5368       && temp->first == temp->last
5369       && TREE_CODE (decl) == PARM_DECL
5370       && GET_CODE (temp->first->loc) == NOTE
5371       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5372       && DECL_INCOMING_RTL (decl)
5373       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5374       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5375          == GET_CODE (DECL_INCOMING_RTL (decl))
5376       && prev_real_insn (temp->first->loc) == NULL_RTX
5377       && (bitsize != -1
5378           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5379                            NOTE_VAR_LOCATION_LOC (loc_note))
5380           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5381               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5382     {
5383       loc = ggc_alloc_cleared_var_loc_node ();
5384       temp->first->next = loc;
5385       temp->last = loc;
5386       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5387     }
5388   else if (temp->last)
5389     {
5390       struct var_loc_node *last = temp->last, *unused = NULL;
5391       rtx *piece_loc = NULL, last_loc_note;
5392       int piece_bitpos = 0;
5393       if (last->next)
5394         {
5395           last = last->next;
5396           gcc_assert (last->next == NULL);
5397         }
5398       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5399         {
5400           piece_loc = &last->loc;
5401           do
5402             {
5403               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5404               if (piece_bitpos + cur_bitsize > bitpos)
5405                 break;
5406               piece_bitpos += cur_bitsize;
5407               piece_loc = &XEXP (*piece_loc, 1);
5408             }
5409           while (*piece_loc);
5410         }
5411       /* TEMP->LAST here is either pointer to the last but one or
5412          last element in the chained list, LAST is pointer to the
5413          last element.  */
5414       if (label && strcmp (last->label, label) == 0)
5415         {
5416           /* For SRA optimized variables if there weren't any real
5417              insns since last note, just modify the last node.  */
5418           if (piece_loc != NULL)
5419             {
5420               adjust_piece_list (piece_loc, NULL, NULL,
5421                                  bitpos, piece_bitpos, bitsize, loc_note);
5422               return NULL;
5423             }
5424           /* If the last note doesn't cover any instructions, remove it.  */
5425           if (temp->last != last)
5426             {
5427               temp->last->next = NULL;
5428               unused = last;
5429               last = temp->last;
5430               gcc_assert (strcmp (last->label, label) != 0);
5431             }
5432           else
5433             {
5434               gcc_assert (temp->first == temp->last
5435                           || (temp->first->next == temp->last
5436                               && TREE_CODE (decl) == PARM_DECL));
5437               memset (temp->last, '\0', sizeof (*temp->last));
5438               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5439               return temp->last;
5440             }
5441         }
5442       if (bitsize == -1 && NOTE_P (last->loc))
5443         last_loc_note = last->loc;
5444       else if (piece_loc != NULL
5445                && *piece_loc != NULL_RTX
5446                && piece_bitpos == bitpos
5447                && decl_piece_bitsize (*piece_loc) == bitsize)
5448         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5449       else
5450         last_loc_note = NULL_RTX;
5451       /* If the current location is the same as the end of the list,
5452          and either both or neither of the locations is uninitialized,
5453          we have nothing to do.  */
5454       if (last_loc_note == NULL_RTX
5455           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5456                             NOTE_VAR_LOCATION_LOC (loc_note)))
5457           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5458                != NOTE_VAR_LOCATION_STATUS (loc_note))
5459               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5460                    == VAR_INIT_STATUS_UNINITIALIZED)
5461                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5462                       == VAR_INIT_STATUS_UNINITIALIZED))))
5463         {
5464           /* Add LOC to the end of list and update LAST.  If the last
5465              element of the list has been removed above, reuse its
5466              memory for the new node, otherwise allocate a new one.  */
5467           if (unused)
5468             {
5469               loc = unused;
5470               memset (loc, '\0', sizeof (*loc));
5471             }
5472           else
5473             loc = ggc_alloc_cleared_var_loc_node ();
5474           if (bitsize == -1 || piece_loc == NULL)
5475             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5476           else
5477             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5478                                bitpos, piece_bitpos, bitsize, loc_note);
5479           last->next = loc;
5480           /* Ensure TEMP->LAST will point either to the new last but one
5481              element of the chain, or to the last element in it.  */
5482           if (last != temp->last)
5483             temp->last = last;
5484         }
5485       else if (unused)
5486         ggc_free (unused);
5487     }
5488   else
5489     {
5490       loc = ggc_alloc_cleared_var_loc_node ();
5491       temp->first = loc;
5492       temp->last = loc;
5493       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5494     }
5495   return loc;
5496 }
5497 \f
5498 /* Keep track of the number of spaces used to indent the
5499    output of the debugging routines that print the structure of
5500    the DIE internal representation.  */
5501 static int print_indent;
5502
5503 /* Indent the line the number of spaces given by print_indent.  */
5504
5505 static inline void
5506 print_spaces (FILE *outfile)
5507 {
5508   fprintf (outfile, "%*s", print_indent, "");
5509 }
5510
5511 /* Print a type signature in hex.  */
5512
5513 static inline void
5514 print_signature (FILE *outfile, char *sig)
5515 {
5516   int i;
5517
5518   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5519     fprintf (outfile, "%02x", sig[i] & 0xff);
5520 }
5521
5522 /* Print the information associated with a given DIE, and its children.
5523    This routine is a debugging aid only.  */
5524
5525 static void
5526 print_die (dw_die_ref die, FILE *outfile)
5527 {
5528   dw_attr_ref a;
5529   dw_die_ref c;
5530   unsigned ix;
5531
5532   print_spaces (outfile);
5533   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5534            die->die_offset, dwarf_tag_name (die->die_tag),
5535            (void*) die);
5536   print_spaces (outfile);
5537   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5538   fprintf (outfile, " offset: %ld", die->die_offset);
5539   fprintf (outfile, " mark: %d\n", die->die_mark);
5540
5541   if (use_debug_types && die->die_id.die_type_node)
5542     {
5543       print_spaces (outfile);
5544       fprintf (outfile, "  signature: ");
5545       print_signature (outfile, die->die_id.die_type_node->signature);
5546       fprintf (outfile, "\n");
5547     }
5548
5549   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5550     {
5551       print_spaces (outfile);
5552       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5553
5554       switch (AT_class (a))
5555         {
5556         case dw_val_class_addr:
5557           fprintf (outfile, "address");
5558           break;
5559         case dw_val_class_offset:
5560           fprintf (outfile, "offset");
5561           break;
5562         case dw_val_class_loc:
5563           fprintf (outfile, "location descriptor");
5564           break;
5565         case dw_val_class_loc_list:
5566           fprintf (outfile, "location list -> label:%s",
5567                    AT_loc_list (a)->ll_symbol);
5568           break;
5569         case dw_val_class_range_list:
5570           fprintf (outfile, "range list");
5571           break;
5572         case dw_val_class_const:
5573           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5574           break;
5575         case dw_val_class_unsigned_const:
5576           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5577           break;
5578         case dw_val_class_const_double:
5579           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5580                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5581                    a->dw_attr_val.v.val_double.high,
5582                    a->dw_attr_val.v.val_double.low);
5583           break;
5584         case dw_val_class_vec:
5585           fprintf (outfile, "floating-point or vector constant");
5586           break;
5587         case dw_val_class_flag:
5588           fprintf (outfile, "%u", AT_flag (a));
5589           break;
5590         case dw_val_class_die_ref:
5591           if (AT_ref (a) != NULL)
5592             {
5593               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5594                 {
5595                   fprintf (outfile, "die -> signature: ");
5596                   print_signature (outfile,
5597                                    AT_ref (a)->die_id.die_type_node->signature);
5598                 }
5599               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5600                 fprintf (outfile, "die -> label: %s",
5601                          AT_ref (a)->die_id.die_symbol);
5602               else
5603                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5604               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5605             }
5606           else
5607             fprintf (outfile, "die -> <null>");
5608           break;
5609         case dw_val_class_vms_delta:
5610           fprintf (outfile, "delta: @slotcount(%s-%s)",
5611                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5612           break;
5613         case dw_val_class_lbl_id:
5614         case dw_val_class_lineptr:
5615         case dw_val_class_macptr:
5616           fprintf (outfile, "label: %s", AT_lbl (a));
5617           break;
5618         case dw_val_class_str:
5619           if (AT_string (a) != NULL)
5620             fprintf (outfile, "\"%s\"", AT_string (a));
5621           else
5622             fprintf (outfile, "<null>");
5623           break;
5624         case dw_val_class_file:
5625           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5626                    AT_file (a)->emitted_number);
5627           break;
5628         case dw_val_class_data8:
5629           {
5630             int i;
5631
5632             for (i = 0; i < 8; i++)
5633               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5634             break;
5635           }
5636         default:
5637           break;
5638         }
5639
5640       fprintf (outfile, "\n");
5641     }
5642
5643   if (die->die_child != NULL)
5644     {
5645       print_indent += 4;
5646       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5647       print_indent -= 4;
5648     }
5649   if (print_indent == 0)
5650     fprintf (outfile, "\n");
5651 }
5652
5653 /* Print the information collected for a given DIE.  */
5654
5655 DEBUG_FUNCTION void
5656 debug_dwarf_die (dw_die_ref die)
5657 {
5658   print_die (die, stderr);
5659 }
5660
5661 /* Print all DWARF information collected for the compilation unit.
5662    This routine is a debugging aid only.  */
5663
5664 DEBUG_FUNCTION void
5665 debug_dwarf (void)
5666 {
5667   print_indent = 0;
5668   print_die (comp_unit_die (), stderr);
5669 }
5670 \f
5671 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5672    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5673    DIE that marks the start of the DIEs for this include file.  */
5674
5675 static dw_die_ref
5676 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5677 {
5678   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5679   dw_die_ref new_unit = gen_compile_unit_die (filename);
5680
5681   new_unit->die_sib = old_unit;
5682   return new_unit;
5683 }
5684
5685 /* Close an include-file CU and reopen the enclosing one.  */
5686
5687 static dw_die_ref
5688 pop_compile_unit (dw_die_ref old_unit)
5689 {
5690   dw_die_ref new_unit = old_unit->die_sib;
5691
5692   old_unit->die_sib = NULL;
5693   return new_unit;
5694 }
5695
5696 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5697 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5698
5699 /* Calculate the checksum of a location expression.  */
5700
5701 static inline void
5702 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5703 {
5704   int tem;
5705
5706   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5707   CHECKSUM (tem);
5708   CHECKSUM (loc->dw_loc_oprnd1);
5709   CHECKSUM (loc->dw_loc_oprnd2);
5710 }
5711
5712 /* Calculate the checksum of an attribute.  */
5713
5714 static void
5715 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5716 {
5717   dw_loc_descr_ref loc;
5718   rtx r;
5719
5720   CHECKSUM (at->dw_attr);
5721
5722   /* We don't care that this was compiled with a different compiler
5723      snapshot; if the output is the same, that's what matters.  */
5724   if (at->dw_attr == DW_AT_producer)
5725     return;
5726
5727   switch (AT_class (at))
5728     {
5729     case dw_val_class_const:
5730       CHECKSUM (at->dw_attr_val.v.val_int);
5731       break;
5732     case dw_val_class_unsigned_const:
5733       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5734       break;
5735     case dw_val_class_const_double:
5736       CHECKSUM (at->dw_attr_val.v.val_double);
5737       break;
5738     case dw_val_class_vec:
5739       CHECKSUM (at->dw_attr_val.v.val_vec);
5740       break;
5741     case dw_val_class_flag:
5742       CHECKSUM (at->dw_attr_val.v.val_flag);
5743       break;
5744     case dw_val_class_str:
5745       CHECKSUM_STRING (AT_string (at));
5746       break;
5747
5748     case dw_val_class_addr:
5749       r = AT_addr (at);
5750       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5751       CHECKSUM_STRING (XSTR (r, 0));
5752       break;
5753
5754     case dw_val_class_offset:
5755       CHECKSUM (at->dw_attr_val.v.val_offset);
5756       break;
5757
5758     case dw_val_class_loc:
5759       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5760         loc_checksum (loc, ctx);
5761       break;
5762
5763     case dw_val_class_die_ref:
5764       die_checksum (AT_ref (at), ctx, mark);
5765       break;
5766
5767     case dw_val_class_fde_ref:
5768     case dw_val_class_vms_delta:
5769     case dw_val_class_lbl_id:
5770     case dw_val_class_lineptr:
5771     case dw_val_class_macptr:
5772       break;
5773
5774     case dw_val_class_file:
5775       CHECKSUM_STRING (AT_file (at)->filename);
5776       break;
5777
5778     case dw_val_class_data8:
5779       CHECKSUM (at->dw_attr_val.v.val_data8);
5780       break;
5781
5782     default:
5783       break;
5784     }
5785 }
5786
5787 /* Calculate the checksum of a DIE.  */
5788
5789 static void
5790 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5791 {
5792   dw_die_ref c;
5793   dw_attr_ref a;
5794   unsigned ix;
5795
5796   /* To avoid infinite recursion.  */
5797   if (die->die_mark)
5798     {
5799       CHECKSUM (die->die_mark);
5800       return;
5801     }
5802   die->die_mark = ++(*mark);
5803
5804   CHECKSUM (die->die_tag);
5805
5806   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5807     attr_checksum (a, ctx, mark);
5808
5809   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5810 }
5811
5812 #undef CHECKSUM
5813 #undef CHECKSUM_STRING
5814
5815 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5816 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5817 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5818 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5819 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5820 #define CHECKSUM_ATTR(FOO) \
5821   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5822
5823 /* Calculate the checksum of a number in signed LEB128 format.  */
5824
5825 static void
5826 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5827 {
5828   unsigned char byte;
5829   bool more;
5830
5831   while (1)
5832     {
5833       byte = (value & 0x7f);
5834       value >>= 7;
5835       more = !((value == 0 && (byte & 0x40) == 0)
5836                 || (value == -1 && (byte & 0x40) != 0));
5837       if (more)
5838         byte |= 0x80;
5839       CHECKSUM (byte);
5840       if (!more)
5841         break;
5842     }
5843 }
5844
5845 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5846
5847 static void
5848 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5849 {
5850   while (1)
5851     {
5852       unsigned char byte = (value & 0x7f);
5853       value >>= 7;
5854       if (value != 0)
5855         /* More bytes to follow.  */
5856         byte |= 0x80;
5857       CHECKSUM (byte);
5858       if (value == 0)
5859         break;
5860     }
5861 }
5862
5863 /* Checksum the context of the DIE.  This adds the names of any
5864    surrounding namespaces or structures to the checksum.  */
5865
5866 static void
5867 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5868 {
5869   const char *name;
5870   dw_die_ref spec;
5871   int tag = die->die_tag;
5872
5873   if (tag != DW_TAG_namespace
5874       && tag != DW_TAG_structure_type
5875       && tag != DW_TAG_class_type)
5876     return;
5877
5878   name = get_AT_string (die, DW_AT_name);
5879
5880   spec = get_AT_ref (die, DW_AT_specification);
5881   if (spec != NULL)
5882     die = spec;
5883
5884   if (die->die_parent != NULL)
5885     checksum_die_context (die->die_parent, ctx);
5886
5887   CHECKSUM_ULEB128 ('C');
5888   CHECKSUM_ULEB128 (tag);
5889   if (name != NULL)
5890     CHECKSUM_STRING (name);
5891 }
5892
5893 /* Calculate the checksum of a location expression.  */
5894
5895 static inline void
5896 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5897 {
5898   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5899      were emitted as a DW_FORM_sdata instead of a location expression.  */
5900   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5901     {
5902       CHECKSUM_ULEB128 (DW_FORM_sdata);
5903       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5904       return;
5905     }
5906
5907   /* Otherwise, just checksum the raw location expression.  */
5908   while (loc != NULL)
5909     {
5910       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5911       CHECKSUM (loc->dw_loc_oprnd1);
5912       CHECKSUM (loc->dw_loc_oprnd2);
5913       loc = loc->dw_loc_next;
5914     }
5915 }
5916
5917 /* Calculate the checksum of an attribute.  */
5918
5919 static void
5920 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5921                        struct md5_ctx *ctx, int *mark)
5922 {
5923   dw_loc_descr_ref loc;
5924   rtx r;
5925
5926   if (AT_class (at) == dw_val_class_die_ref)
5927     {
5928       dw_die_ref target_die = AT_ref (at);
5929
5930       /* For pointer and reference types, we checksum only the (qualified)
5931          name of the target type (if there is a name).  For friend entries,
5932          we checksum only the (qualified) name of the target type or function.
5933          This allows the checksum to remain the same whether the target type
5934          is complete or not.  */
5935       if ((at->dw_attr == DW_AT_type
5936            && (tag == DW_TAG_pointer_type
5937                || tag == DW_TAG_reference_type
5938                || tag == DW_TAG_rvalue_reference_type
5939                || tag == DW_TAG_ptr_to_member_type))
5940           || (at->dw_attr == DW_AT_friend
5941               && tag == DW_TAG_friend))
5942         {
5943           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5944
5945           if (name_attr != NULL)
5946             {
5947               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5948
5949               if (decl == NULL)
5950                 decl = target_die;
5951               CHECKSUM_ULEB128 ('N');
5952               CHECKSUM_ULEB128 (at->dw_attr);
5953               if (decl->die_parent != NULL)
5954                 checksum_die_context (decl->die_parent, ctx);
5955               CHECKSUM_ULEB128 ('E');
5956               CHECKSUM_STRING (AT_string (name_attr));
5957               return;
5958             }
5959         }
5960
5961       /* For all other references to another DIE, we check to see if the
5962          target DIE has already been visited.  If it has, we emit a
5963          backward reference; if not, we descend recursively.  */
5964       if (target_die->die_mark > 0)
5965         {
5966           CHECKSUM_ULEB128 ('R');
5967           CHECKSUM_ULEB128 (at->dw_attr);
5968           CHECKSUM_ULEB128 (target_die->die_mark);
5969         }
5970       else
5971         {
5972           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5973
5974           if (decl == NULL)
5975             decl = target_die;
5976           target_die->die_mark = ++(*mark);
5977           CHECKSUM_ULEB128 ('T');
5978           CHECKSUM_ULEB128 (at->dw_attr);
5979           if (decl->die_parent != NULL)
5980             checksum_die_context (decl->die_parent, ctx);
5981           die_checksum_ordered (target_die, ctx, mark);
5982         }
5983       return;
5984     }
5985
5986   CHECKSUM_ULEB128 ('A');
5987   CHECKSUM_ULEB128 (at->dw_attr);
5988
5989   switch (AT_class (at))
5990     {
5991     case dw_val_class_const:
5992       CHECKSUM_ULEB128 (DW_FORM_sdata);
5993       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5994       break;
5995
5996     case dw_val_class_unsigned_const:
5997       CHECKSUM_ULEB128 (DW_FORM_sdata);
5998       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5999       break;
6000
6001     case dw_val_class_const_double:
6002       CHECKSUM_ULEB128 (DW_FORM_block);
6003       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6004       CHECKSUM (at->dw_attr_val.v.val_double);
6005       break;
6006
6007     case dw_val_class_vec:
6008       CHECKSUM_ULEB128 (DW_FORM_block);
6009       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6010       CHECKSUM (at->dw_attr_val.v.val_vec);
6011       break;
6012
6013     case dw_val_class_flag:
6014       CHECKSUM_ULEB128 (DW_FORM_flag);
6015       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6016       break;
6017
6018     case dw_val_class_str:
6019       CHECKSUM_ULEB128 (DW_FORM_string);
6020       CHECKSUM_STRING (AT_string (at));
6021       break;
6022
6023     case dw_val_class_addr:
6024       r = AT_addr (at);
6025       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6026       CHECKSUM_ULEB128 (DW_FORM_string);
6027       CHECKSUM_STRING (XSTR (r, 0));
6028       break;
6029
6030     case dw_val_class_offset:
6031       CHECKSUM_ULEB128 (DW_FORM_sdata);
6032       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6033       break;
6034
6035     case dw_val_class_loc:
6036       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6037         loc_checksum_ordered (loc, ctx);
6038       break;
6039
6040     case dw_val_class_fde_ref:
6041     case dw_val_class_lbl_id:
6042     case dw_val_class_lineptr:
6043     case dw_val_class_macptr:
6044       break;
6045
6046     case dw_val_class_file:
6047       CHECKSUM_ULEB128 (DW_FORM_string);
6048       CHECKSUM_STRING (AT_file (at)->filename);
6049       break;
6050
6051     case dw_val_class_data8:
6052       CHECKSUM (at->dw_attr_val.v.val_data8);
6053       break;
6054
6055     default:
6056       break;
6057     }
6058 }
6059
6060 struct checksum_attributes
6061 {
6062   dw_attr_ref at_name;
6063   dw_attr_ref at_type;
6064   dw_attr_ref at_friend;
6065   dw_attr_ref at_accessibility;
6066   dw_attr_ref at_address_class;
6067   dw_attr_ref at_allocated;
6068   dw_attr_ref at_artificial;
6069   dw_attr_ref at_associated;
6070   dw_attr_ref at_binary_scale;
6071   dw_attr_ref at_bit_offset;
6072   dw_attr_ref at_bit_size;
6073   dw_attr_ref at_bit_stride;
6074   dw_attr_ref at_byte_size;
6075   dw_attr_ref at_byte_stride;
6076   dw_attr_ref at_const_value;
6077   dw_attr_ref at_containing_type;
6078   dw_attr_ref at_count;
6079   dw_attr_ref at_data_location;
6080   dw_attr_ref at_data_member_location;
6081   dw_attr_ref at_decimal_scale;
6082   dw_attr_ref at_decimal_sign;
6083   dw_attr_ref at_default_value;
6084   dw_attr_ref at_digit_count;
6085   dw_attr_ref at_discr;
6086   dw_attr_ref at_discr_list;
6087   dw_attr_ref at_discr_value;
6088   dw_attr_ref at_encoding;
6089   dw_attr_ref at_endianity;
6090   dw_attr_ref at_explicit;
6091   dw_attr_ref at_is_optional;
6092   dw_attr_ref at_location;
6093   dw_attr_ref at_lower_bound;
6094   dw_attr_ref at_mutable;
6095   dw_attr_ref at_ordering;
6096   dw_attr_ref at_picture_string;
6097   dw_attr_ref at_prototyped;
6098   dw_attr_ref at_small;
6099   dw_attr_ref at_segment;
6100   dw_attr_ref at_string_length;
6101   dw_attr_ref at_threads_scaled;
6102   dw_attr_ref at_upper_bound;
6103   dw_attr_ref at_use_location;
6104   dw_attr_ref at_use_UTF8;
6105   dw_attr_ref at_variable_parameter;
6106   dw_attr_ref at_virtuality;
6107   dw_attr_ref at_visibility;
6108   dw_attr_ref at_vtable_elem_location;
6109 };
6110
6111 /* Collect the attributes that we will want to use for the checksum.  */
6112
6113 static void
6114 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6115 {
6116   dw_attr_ref a;
6117   unsigned ix;
6118
6119   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6120     {
6121       switch (a->dw_attr)
6122         {
6123         case DW_AT_name:
6124           attrs->at_name = a;
6125           break;
6126         case DW_AT_type:
6127           attrs->at_type = a;
6128           break;
6129         case DW_AT_friend:
6130           attrs->at_friend = a;
6131           break;
6132         case DW_AT_accessibility:
6133           attrs->at_accessibility = a;
6134           break;
6135         case DW_AT_address_class:
6136           attrs->at_address_class = a;
6137           break;
6138         case DW_AT_allocated:
6139           attrs->at_allocated = a;
6140           break;
6141         case DW_AT_artificial:
6142           attrs->at_artificial = a;
6143           break;
6144         case DW_AT_associated:
6145           attrs->at_associated = a;
6146           break;
6147         case DW_AT_binary_scale:
6148           attrs->at_binary_scale = a;
6149           break;
6150         case DW_AT_bit_offset:
6151           attrs->at_bit_offset = a;
6152           break;
6153         case DW_AT_bit_size:
6154           attrs->at_bit_size = a;
6155           break;
6156         case DW_AT_bit_stride:
6157           attrs->at_bit_stride = a;
6158           break;
6159         case DW_AT_byte_size:
6160           attrs->at_byte_size = a;
6161           break;
6162         case DW_AT_byte_stride:
6163           attrs->at_byte_stride = a;
6164           break;
6165         case DW_AT_const_value:
6166           attrs->at_const_value = a;
6167           break;
6168         case DW_AT_containing_type:
6169           attrs->at_containing_type = a;
6170           break;
6171         case DW_AT_count:
6172           attrs->at_count = a;
6173           break;
6174         case DW_AT_data_location:
6175           attrs->at_data_location = a;
6176           break;
6177         case DW_AT_data_member_location:
6178           attrs->at_data_member_location = a;
6179           break;
6180         case DW_AT_decimal_scale:
6181           attrs->at_decimal_scale = a;
6182           break;
6183         case DW_AT_decimal_sign:
6184           attrs->at_decimal_sign = a;
6185           break;
6186         case DW_AT_default_value:
6187           attrs->at_default_value = a;
6188           break;
6189         case DW_AT_digit_count:
6190           attrs->at_digit_count = a;
6191           break;
6192         case DW_AT_discr:
6193           attrs->at_discr = a;
6194           break;
6195         case DW_AT_discr_list:
6196           attrs->at_discr_list = a;
6197           break;
6198         case DW_AT_discr_value:
6199           attrs->at_discr_value = a;
6200           break;
6201         case DW_AT_encoding:
6202           attrs->at_encoding = a;
6203           break;
6204         case DW_AT_endianity:
6205           attrs->at_endianity = a;
6206           break;
6207         case DW_AT_explicit:
6208           attrs->at_explicit = a;
6209           break;
6210         case DW_AT_is_optional:
6211           attrs->at_is_optional = a;
6212           break;
6213         case DW_AT_location:
6214           attrs->at_location = a;
6215           break;
6216         case DW_AT_lower_bound:
6217           attrs->at_lower_bound = a;
6218           break;
6219         case DW_AT_mutable:
6220           attrs->at_mutable = a;
6221           break;
6222         case DW_AT_ordering:
6223           attrs->at_ordering = a;
6224           break;
6225         case DW_AT_picture_string:
6226           attrs->at_picture_string = a;
6227           break;
6228         case DW_AT_prototyped:
6229           attrs->at_prototyped = a;
6230           break;
6231         case DW_AT_small:
6232           attrs->at_small = a;
6233           break;
6234         case DW_AT_segment:
6235           attrs->at_segment = a;
6236           break;
6237         case DW_AT_string_length:
6238           attrs->at_string_length = a;
6239           break;
6240         case DW_AT_threads_scaled:
6241           attrs->at_threads_scaled = a;
6242           break;
6243         case DW_AT_upper_bound:
6244           attrs->at_upper_bound = a;
6245           break;
6246         case DW_AT_use_location:
6247           attrs->at_use_location = a;
6248           break;
6249         case DW_AT_use_UTF8:
6250           attrs->at_use_UTF8 = a;
6251           break;
6252         case DW_AT_variable_parameter:
6253           attrs->at_variable_parameter = a;
6254           break;
6255         case DW_AT_virtuality:
6256           attrs->at_virtuality = a;
6257           break;
6258         case DW_AT_visibility:
6259           attrs->at_visibility = a;
6260           break;
6261         case DW_AT_vtable_elem_location:
6262           attrs->at_vtable_elem_location = a;
6263           break;
6264         default:
6265           break;
6266         }
6267     }
6268 }
6269
6270 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6271
6272 static void
6273 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6274 {
6275   dw_die_ref c;
6276   dw_die_ref decl;
6277   struct checksum_attributes attrs;
6278
6279   CHECKSUM_ULEB128 ('D');
6280   CHECKSUM_ULEB128 (die->die_tag);
6281
6282   memset (&attrs, 0, sizeof (attrs));
6283
6284   decl = get_AT_ref (die, DW_AT_specification);
6285   if (decl != NULL)
6286     collect_checksum_attributes (&attrs, decl);
6287   collect_checksum_attributes (&attrs, die);
6288
6289   CHECKSUM_ATTR (attrs.at_name);
6290   CHECKSUM_ATTR (attrs.at_accessibility);
6291   CHECKSUM_ATTR (attrs.at_address_class);
6292   CHECKSUM_ATTR (attrs.at_allocated);
6293   CHECKSUM_ATTR (attrs.at_artificial);
6294   CHECKSUM_ATTR (attrs.at_associated);
6295   CHECKSUM_ATTR (attrs.at_binary_scale);
6296   CHECKSUM_ATTR (attrs.at_bit_offset);
6297   CHECKSUM_ATTR (attrs.at_bit_size);
6298   CHECKSUM_ATTR (attrs.at_bit_stride);
6299   CHECKSUM_ATTR (attrs.at_byte_size);
6300   CHECKSUM_ATTR (attrs.at_byte_stride);
6301   CHECKSUM_ATTR (attrs.at_const_value);
6302   CHECKSUM_ATTR (attrs.at_containing_type);
6303   CHECKSUM_ATTR (attrs.at_count);
6304   CHECKSUM_ATTR (attrs.at_data_location);
6305   CHECKSUM_ATTR (attrs.at_data_member_location);
6306   CHECKSUM_ATTR (attrs.at_decimal_scale);
6307   CHECKSUM_ATTR (attrs.at_decimal_sign);
6308   CHECKSUM_ATTR (attrs.at_default_value);
6309   CHECKSUM_ATTR (attrs.at_digit_count);
6310   CHECKSUM_ATTR (attrs.at_discr);
6311   CHECKSUM_ATTR (attrs.at_discr_list);
6312   CHECKSUM_ATTR (attrs.at_discr_value);
6313   CHECKSUM_ATTR (attrs.at_encoding);
6314   CHECKSUM_ATTR (attrs.at_endianity);
6315   CHECKSUM_ATTR (attrs.at_explicit);
6316   CHECKSUM_ATTR (attrs.at_is_optional);
6317   CHECKSUM_ATTR (attrs.at_location);
6318   CHECKSUM_ATTR (attrs.at_lower_bound);
6319   CHECKSUM_ATTR (attrs.at_mutable);
6320   CHECKSUM_ATTR (attrs.at_ordering);
6321   CHECKSUM_ATTR (attrs.at_picture_string);
6322   CHECKSUM_ATTR (attrs.at_prototyped);
6323   CHECKSUM_ATTR (attrs.at_small);
6324   CHECKSUM_ATTR (attrs.at_segment);
6325   CHECKSUM_ATTR (attrs.at_string_length);
6326   CHECKSUM_ATTR (attrs.at_threads_scaled);
6327   CHECKSUM_ATTR (attrs.at_upper_bound);
6328   CHECKSUM_ATTR (attrs.at_use_location);
6329   CHECKSUM_ATTR (attrs.at_use_UTF8);
6330   CHECKSUM_ATTR (attrs.at_variable_parameter);
6331   CHECKSUM_ATTR (attrs.at_virtuality);
6332   CHECKSUM_ATTR (attrs.at_visibility);
6333   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6334   CHECKSUM_ATTR (attrs.at_type);
6335   CHECKSUM_ATTR (attrs.at_friend);
6336
6337   /* Checksum the child DIEs, except for nested types and member functions.  */
6338   c = die->die_child;
6339   if (c) do {
6340     dw_attr_ref name_attr;
6341
6342     c = c->die_sib;
6343     name_attr = get_AT (c, DW_AT_name);
6344     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6345         && name_attr != NULL)
6346       {
6347         CHECKSUM_ULEB128 ('S');
6348         CHECKSUM_ULEB128 (c->die_tag);
6349         CHECKSUM_STRING (AT_string (name_attr));
6350       }
6351     else
6352       {
6353         /* Mark this DIE so it gets processed when unmarking.  */
6354         if (c->die_mark == 0)
6355           c->die_mark = -1;
6356         die_checksum_ordered (c, ctx, mark);
6357       }
6358   } while (c != die->die_child);
6359
6360   CHECKSUM_ULEB128 (0);
6361 }
6362
6363 #undef CHECKSUM
6364 #undef CHECKSUM_STRING
6365 #undef CHECKSUM_ATTR
6366 #undef CHECKSUM_LEB128
6367 #undef CHECKSUM_ULEB128
6368
6369 /* Generate the type signature for DIE.  This is computed by generating an
6370    MD5 checksum over the DIE's tag, its relevant attributes, and its
6371    children.  Attributes that are references to other DIEs are processed
6372    by recursion, using the MARK field to prevent infinite recursion.
6373    If the DIE is nested inside a namespace or another type, we also
6374    need to include that context in the signature.  The lower 64 bits
6375    of the resulting MD5 checksum comprise the signature.  */
6376
6377 static void
6378 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6379 {
6380   int mark;
6381   const char *name;
6382   unsigned char checksum[16];
6383   struct md5_ctx ctx;
6384   dw_die_ref decl;
6385
6386   name = get_AT_string (die, DW_AT_name);
6387   decl = get_AT_ref (die, DW_AT_specification);
6388
6389   /* First, compute a signature for just the type name (and its surrounding
6390      context, if any.  This is stored in the type unit DIE for link-time
6391      ODR (one-definition rule) checking.  */
6392
6393   if (is_cxx() && name != NULL)
6394     {
6395       md5_init_ctx (&ctx);
6396
6397       /* Checksum the names of surrounding namespaces and structures.  */
6398       if (decl != NULL && decl->die_parent != NULL)
6399         checksum_die_context (decl->die_parent, &ctx);
6400
6401       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6402       md5_process_bytes (name, strlen (name) + 1, &ctx);
6403       md5_finish_ctx (&ctx, checksum);
6404
6405       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6406     }
6407
6408   /* Next, compute the complete type signature.  */
6409
6410   md5_init_ctx (&ctx);
6411   mark = 1;
6412   die->die_mark = mark;
6413
6414   /* Checksum the names of surrounding namespaces and structures.  */
6415   if (decl != NULL && decl->die_parent != NULL)
6416     checksum_die_context (decl->die_parent, &ctx);
6417
6418   /* Checksum the DIE and its children.  */
6419   die_checksum_ordered (die, &ctx, &mark);
6420   unmark_all_dies (die);
6421   md5_finish_ctx (&ctx, checksum);
6422
6423   /* Store the signature in the type node and link the type DIE and the
6424      type node together.  */
6425   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6426           DWARF_TYPE_SIGNATURE_SIZE);
6427   die->die_id.die_type_node = type_node;
6428   type_node->type_die = die;
6429
6430   /* If the DIE is a specification, link its declaration to the type node
6431      as well.  */
6432   if (decl != NULL)
6433     decl->die_id.die_type_node = type_node;
6434 }
6435
6436 /* Do the location expressions look same?  */
6437 static inline int
6438 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6439 {
6440   return loc1->dw_loc_opc == loc2->dw_loc_opc
6441          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6442          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6443 }
6444
6445 /* Do the values look the same?  */
6446 static int
6447 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6448 {
6449   dw_loc_descr_ref loc1, loc2;
6450   rtx r1, r2;
6451
6452   if (v1->val_class != v2->val_class)
6453     return 0;
6454
6455   switch (v1->val_class)
6456     {
6457     case dw_val_class_const:
6458       return v1->v.val_int == v2->v.val_int;
6459     case dw_val_class_unsigned_const:
6460       return v1->v.val_unsigned == v2->v.val_unsigned;
6461     case dw_val_class_const_double:
6462       return v1->v.val_double.high == v2->v.val_double.high
6463              && v1->v.val_double.low == v2->v.val_double.low;
6464     case dw_val_class_vec:
6465       if (v1->v.val_vec.length != v2->v.val_vec.length
6466           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6467         return 0;
6468       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6469                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6470         return 0;
6471       return 1;
6472     case dw_val_class_flag:
6473       return v1->v.val_flag == v2->v.val_flag;
6474     case dw_val_class_str:
6475       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6476
6477     case dw_val_class_addr:
6478       r1 = v1->v.val_addr;
6479       r2 = v2->v.val_addr;
6480       if (GET_CODE (r1) != GET_CODE (r2))
6481         return 0;
6482       return !rtx_equal_p (r1, r2);
6483
6484     case dw_val_class_offset:
6485       return v1->v.val_offset == v2->v.val_offset;
6486
6487     case dw_val_class_loc:
6488       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6489            loc1 && loc2;
6490            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6491         if (!same_loc_p (loc1, loc2, mark))
6492           return 0;
6493       return !loc1 && !loc2;
6494
6495     case dw_val_class_die_ref:
6496       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6497
6498     case dw_val_class_fde_ref:
6499     case dw_val_class_vms_delta:
6500     case dw_val_class_lbl_id:
6501     case dw_val_class_lineptr:
6502     case dw_val_class_macptr:
6503       return 1;
6504
6505     case dw_val_class_file:
6506       return v1->v.val_file == v2->v.val_file;
6507
6508     case dw_val_class_data8:
6509       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6510
6511     default:
6512       return 1;
6513     }
6514 }
6515
6516 /* Do the attributes look the same?  */
6517
6518 static int
6519 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6520 {
6521   if (at1->dw_attr != at2->dw_attr)
6522     return 0;
6523
6524   /* We don't care that this was compiled with a different compiler
6525      snapshot; if the output is the same, that's what matters. */
6526   if (at1->dw_attr == DW_AT_producer)
6527     return 1;
6528
6529   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6530 }
6531
6532 /* Do the dies look the same?  */
6533
6534 static int
6535 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6536 {
6537   dw_die_ref c1, c2;
6538   dw_attr_ref a1;
6539   unsigned ix;
6540
6541   /* To avoid infinite recursion.  */
6542   if (die1->die_mark)
6543     return die1->die_mark == die2->die_mark;
6544   die1->die_mark = die2->die_mark = ++(*mark);
6545
6546   if (die1->die_tag != die2->die_tag)
6547     return 0;
6548
6549   if (VEC_length (dw_attr_node, die1->die_attr)
6550       != VEC_length (dw_attr_node, die2->die_attr))
6551     return 0;
6552
6553   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6554     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6555       return 0;
6556
6557   c1 = die1->die_child;
6558   c2 = die2->die_child;
6559   if (! c1)
6560     {
6561       if (c2)
6562         return 0;
6563     }
6564   else
6565     for (;;)
6566       {
6567         if (!same_die_p (c1, c2, mark))
6568           return 0;
6569         c1 = c1->die_sib;
6570         c2 = c2->die_sib;
6571         if (c1 == die1->die_child)
6572           {
6573             if (c2 == die2->die_child)
6574               break;
6575             else
6576               return 0;
6577           }
6578     }
6579
6580   return 1;
6581 }
6582
6583 /* Do the dies look the same?  Wrapper around same_die_p.  */
6584
6585 static int
6586 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6587 {
6588   int mark = 0;
6589   int ret = same_die_p (die1, die2, &mark);
6590
6591   unmark_all_dies (die1);
6592   unmark_all_dies (die2);
6593
6594   return ret;
6595 }
6596
6597 /* The prefix to attach to symbols on DIEs in the current comdat debug
6598    info section.  */
6599 static char *comdat_symbol_id;
6600
6601 /* The index of the current symbol within the current comdat CU.  */
6602 static unsigned int comdat_symbol_number;
6603
6604 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6605    children, and set comdat_symbol_id accordingly.  */
6606
6607 static void
6608 compute_section_prefix (dw_die_ref unit_die)
6609 {
6610   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6611   const char *base = die_name ? lbasename (die_name) : "anonymous";
6612   char *name = XALLOCAVEC (char, strlen (base) + 64);
6613   char *p;
6614   int i, mark;
6615   unsigned char checksum[16];
6616   struct md5_ctx ctx;
6617
6618   /* Compute the checksum of the DIE, then append part of it as hex digits to
6619      the name filename of the unit.  */
6620
6621   md5_init_ctx (&ctx);
6622   mark = 0;
6623   die_checksum (unit_die, &ctx, &mark);
6624   unmark_all_dies (unit_die);
6625   md5_finish_ctx (&ctx, checksum);
6626
6627   sprintf (name, "%s.", base);
6628   clean_symbol_name (name);
6629
6630   p = name + strlen (name);
6631   for (i = 0; i < 4; i++)
6632     {
6633       sprintf (p, "%.2x", checksum[i]);
6634       p += 2;
6635     }
6636
6637   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6638   comdat_symbol_number = 0;
6639 }
6640
6641 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6642
6643 static int
6644 is_type_die (dw_die_ref die)
6645 {
6646   switch (die->die_tag)
6647     {
6648     case DW_TAG_array_type:
6649     case DW_TAG_class_type:
6650     case DW_TAG_interface_type:
6651     case DW_TAG_enumeration_type:
6652     case DW_TAG_pointer_type:
6653     case DW_TAG_reference_type:
6654     case DW_TAG_rvalue_reference_type:
6655     case DW_TAG_string_type:
6656     case DW_TAG_structure_type:
6657     case DW_TAG_subroutine_type:
6658     case DW_TAG_union_type:
6659     case DW_TAG_ptr_to_member_type:
6660     case DW_TAG_set_type:
6661     case DW_TAG_subrange_type:
6662     case DW_TAG_base_type:
6663     case DW_TAG_const_type:
6664     case DW_TAG_file_type:
6665     case DW_TAG_packed_type:
6666     case DW_TAG_volatile_type:
6667     case DW_TAG_typedef:
6668       return 1;
6669     default:
6670       return 0;
6671     }
6672 }
6673
6674 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6675    Basically, we want to choose the bits that are likely to be shared between
6676    compilations (types) and leave out the bits that are specific to individual
6677    compilations (functions).  */
6678
6679 static int
6680 is_comdat_die (dw_die_ref c)
6681 {
6682   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6683      we do for stabs.  The advantage is a greater likelihood of sharing between
6684      objects that don't include headers in the same order (and therefore would
6685      put the base types in a different comdat).  jason 8/28/00 */
6686
6687   if (c->die_tag == DW_TAG_base_type)
6688     return 0;
6689
6690   if (c->die_tag == DW_TAG_pointer_type
6691       || c->die_tag == DW_TAG_reference_type
6692       || c->die_tag == DW_TAG_rvalue_reference_type
6693       || c->die_tag == DW_TAG_const_type
6694       || c->die_tag == DW_TAG_volatile_type)
6695     {
6696       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6697
6698       return t ? is_comdat_die (t) : 0;
6699     }
6700
6701   return is_type_die (c);
6702 }
6703
6704 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6705    compilation unit.  */
6706
6707 static int
6708 is_symbol_die (dw_die_ref c)
6709 {
6710   return (is_type_die (c)
6711           || is_declaration_die (c)
6712           || c->die_tag == DW_TAG_namespace
6713           || c->die_tag == DW_TAG_module);
6714 }
6715
6716 /* Returns true iff C is a compile-unit DIE.  */
6717
6718 static inline bool
6719 is_cu_die (dw_die_ref c)
6720 {
6721   return c && c->die_tag == DW_TAG_compile_unit;
6722 }
6723
6724 static char *
6725 gen_internal_sym (const char *prefix)
6726 {
6727   char buf[256];
6728
6729   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6730   return xstrdup (buf);
6731 }
6732
6733 /* Assign symbols to all worthy DIEs under DIE.  */
6734
6735 static void
6736 assign_symbol_names (dw_die_ref die)
6737 {
6738   dw_die_ref c;
6739
6740   if (is_symbol_die (die))
6741     {
6742       if (comdat_symbol_id)
6743         {
6744           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6745
6746           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6747                    comdat_symbol_id, comdat_symbol_number++);
6748           die->die_id.die_symbol = xstrdup (p);
6749         }
6750       else
6751         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6752     }
6753
6754   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6755 }
6756
6757 struct cu_hash_table_entry
6758 {
6759   dw_die_ref cu;
6760   unsigned min_comdat_num, max_comdat_num;
6761   struct cu_hash_table_entry *next;
6762 };
6763
6764 /* Routines to manipulate hash table of CUs.  */
6765 static hashval_t
6766 htab_cu_hash (const void *of)
6767 {
6768   const struct cu_hash_table_entry *const entry =
6769     (const struct cu_hash_table_entry *) of;
6770
6771   return htab_hash_string (entry->cu->die_id.die_symbol);
6772 }
6773
6774 static int
6775 htab_cu_eq (const void *of1, const void *of2)
6776 {
6777   const struct cu_hash_table_entry *const entry1 =
6778     (const struct cu_hash_table_entry *) of1;
6779   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6780
6781   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6782 }
6783
6784 static void
6785 htab_cu_del (void *what)
6786 {
6787   struct cu_hash_table_entry *next,
6788     *entry = (struct cu_hash_table_entry *) what;
6789
6790   while (entry)
6791     {
6792       next = entry->next;
6793       free (entry);
6794       entry = next;
6795     }
6796 }
6797
6798 /* Check whether we have already seen this CU and set up SYM_NUM
6799    accordingly.  */
6800 static int
6801 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6802 {
6803   struct cu_hash_table_entry dummy;
6804   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6805
6806   dummy.max_comdat_num = 0;
6807
6808   slot = (struct cu_hash_table_entry **)
6809     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6810         INSERT);
6811   entry = *slot;
6812
6813   for (; entry; last = entry, entry = entry->next)
6814     {
6815       if (same_die_p_wrap (cu, entry->cu))
6816         break;
6817     }
6818
6819   if (entry)
6820     {
6821       *sym_num = entry->min_comdat_num;
6822       return 1;
6823     }
6824
6825   entry = XCNEW (struct cu_hash_table_entry);
6826   entry->cu = cu;
6827   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6828   entry->next = *slot;
6829   *slot = entry;
6830
6831   return 0;
6832 }
6833
6834 /* Record SYM_NUM to record of CU in HTABLE.  */
6835 static void
6836 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6837 {
6838   struct cu_hash_table_entry **slot, *entry;
6839
6840   slot = (struct cu_hash_table_entry **)
6841     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6842         NO_INSERT);
6843   entry = *slot;
6844
6845   entry->max_comdat_num = sym_num;
6846 }
6847
6848 /* Traverse the DIE (which is always comp_unit_die), and set up
6849    additional compilation units for each of the include files we see
6850    bracketed by BINCL/EINCL.  */
6851
6852 static void
6853 break_out_includes (dw_die_ref die)
6854 {
6855   dw_die_ref c;
6856   dw_die_ref unit = NULL;
6857   limbo_die_node *node, **pnode;
6858   htab_t cu_hash_table;
6859
6860   c = die->die_child;
6861   if (c) do {
6862     dw_die_ref prev = c;
6863     c = c->die_sib;
6864     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6865            || (unit && is_comdat_die (c)))
6866       {
6867         dw_die_ref next = c->die_sib;
6868
6869         /* This DIE is for a secondary CU; remove it from the main one.  */
6870         remove_child_with_prev (c, prev);
6871
6872         if (c->die_tag == DW_TAG_GNU_BINCL)
6873           unit = push_new_compile_unit (unit, c);
6874         else if (c->die_tag == DW_TAG_GNU_EINCL)
6875           unit = pop_compile_unit (unit);
6876         else
6877           add_child_die (unit, c);
6878         c = next;
6879         if (c == die->die_child)
6880           break;
6881       }
6882   } while (c != die->die_child);
6883
6884 #if 0
6885   /* We can only use this in debugging, since the frontend doesn't check
6886      to make sure that we leave every include file we enter.  */
6887   gcc_assert (!unit);
6888 #endif
6889
6890   assign_symbol_names (die);
6891   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6892   for (node = limbo_die_list, pnode = &limbo_die_list;
6893        node;
6894        node = node->next)
6895     {
6896       int is_dupl;
6897
6898       compute_section_prefix (node->die);
6899       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6900                         &comdat_symbol_number);
6901       assign_symbol_names (node->die);
6902       if (is_dupl)
6903         *pnode = node->next;
6904       else
6905         {
6906           pnode = &node->next;
6907           record_comdat_symbol_number (node->die, cu_hash_table,
6908                 comdat_symbol_number);
6909         }
6910     }
6911   htab_delete (cu_hash_table);
6912 }
6913
6914 /* Return non-zero if this DIE is a declaration.  */
6915
6916 static int
6917 is_declaration_die (dw_die_ref die)
6918 {
6919   dw_attr_ref a;
6920   unsigned ix;
6921
6922   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6923     if (a->dw_attr == DW_AT_declaration)
6924       return 1;
6925
6926   return 0;
6927 }
6928
6929 /* Return non-zero if this DIE is nested inside a subprogram.  */
6930
6931 static int
6932 is_nested_in_subprogram (dw_die_ref die)
6933 {
6934   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6935
6936   if (decl == NULL)
6937     decl = die;
6938   return local_scope_p (decl);
6939 }
6940
6941 /* Return non-zero if this DIE contains a defining declaration of a
6942    subprogram.  */
6943
6944 static int
6945 contains_subprogram_definition (dw_die_ref die)
6946 {
6947   dw_die_ref c;
6948
6949   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6950     return 1;
6951   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6952   return 0;
6953 }
6954
6955 /* Return non-zero if this is a type DIE that should be moved to a
6956    COMDAT .debug_types section.  */
6957
6958 static int
6959 should_move_die_to_comdat (dw_die_ref die)
6960 {
6961   switch (die->die_tag)
6962     {
6963     case DW_TAG_class_type:
6964     case DW_TAG_structure_type:
6965     case DW_TAG_enumeration_type:
6966     case DW_TAG_union_type:
6967       /* Don't move declarations, inlined instances, or types nested in a
6968          subprogram.  */
6969       if (is_declaration_die (die)
6970           || get_AT (die, DW_AT_abstract_origin)
6971           || is_nested_in_subprogram (die))
6972         return 0;
6973       /* A type definition should never contain a subprogram definition.  */
6974       gcc_assert (!contains_subprogram_definition (die));
6975       return 1;
6976     case DW_TAG_array_type:
6977     case DW_TAG_interface_type:
6978     case DW_TAG_pointer_type:
6979     case DW_TAG_reference_type:
6980     case DW_TAG_rvalue_reference_type:
6981     case DW_TAG_string_type:
6982     case DW_TAG_subroutine_type:
6983     case DW_TAG_ptr_to_member_type:
6984     case DW_TAG_set_type:
6985     case DW_TAG_subrange_type:
6986     case DW_TAG_base_type:
6987     case DW_TAG_const_type:
6988     case DW_TAG_file_type:
6989     case DW_TAG_packed_type:
6990     case DW_TAG_volatile_type:
6991     case DW_TAG_typedef:
6992     default:
6993       return 0;
6994     }
6995 }
6996
6997 /* Make a clone of DIE.  */
6998
6999 static dw_die_ref
7000 clone_die (dw_die_ref die)
7001 {
7002   dw_die_ref clone;
7003   dw_attr_ref a;
7004   unsigned ix;
7005
7006   clone = ggc_alloc_cleared_die_node ();
7007   clone->die_tag = die->die_tag;
7008
7009   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7010     add_dwarf_attr (clone, a);
7011
7012   return clone;
7013 }
7014
7015 /* Make a clone of the tree rooted at DIE.  */
7016
7017 static dw_die_ref
7018 clone_tree (dw_die_ref die)
7019 {
7020   dw_die_ref c;
7021   dw_die_ref clone = clone_die (die);
7022
7023   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7024
7025   return clone;
7026 }
7027
7028 /* Make a clone of DIE as a declaration.  */
7029
7030 static dw_die_ref
7031 clone_as_declaration (dw_die_ref die)
7032 {
7033   dw_die_ref clone;
7034   dw_die_ref decl;
7035   dw_attr_ref a;
7036   unsigned ix;
7037
7038   /* If the DIE is already a declaration, just clone it.  */
7039   if (is_declaration_die (die))
7040     return clone_die (die);
7041
7042   /* If the DIE is a specification, just clone its declaration DIE.  */
7043   decl = get_AT_ref (die, DW_AT_specification);
7044   if (decl != NULL)
7045     return clone_die (decl);
7046
7047   clone = ggc_alloc_cleared_die_node ();
7048   clone->die_tag = die->die_tag;
7049
7050   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7051     {
7052       /* We don't want to copy over all attributes.
7053          For example we don't want DW_AT_byte_size because otherwise we will no
7054          longer have a declaration and GDB will treat it as a definition.  */
7055
7056       switch (a->dw_attr)
7057         {
7058         case DW_AT_artificial:
7059         case DW_AT_containing_type:
7060         case DW_AT_external:
7061         case DW_AT_name:
7062         case DW_AT_type:
7063         case DW_AT_virtuality:
7064         case DW_AT_linkage_name:
7065         case DW_AT_MIPS_linkage_name:
7066           add_dwarf_attr (clone, a);
7067           break;
7068         case DW_AT_byte_size:
7069         default:
7070           break;
7071         }
7072     }
7073
7074   if (die->die_id.die_type_node)
7075     add_AT_die_ref (clone, DW_AT_signature, die);
7076
7077   add_AT_flag (clone, DW_AT_declaration, 1);
7078   return clone;
7079 }
7080
7081 /* Copy the declaration context to the new type unit DIE.  This includes
7082    any surrounding namespace or type declarations.  If the DIE has an
7083    AT_specification attribute, it also includes attributes and children
7084    attached to the specification, and returns a pointer to the original
7085    parent of the declaration DIE.  Returns NULL otherwise.  */
7086
7087 static dw_die_ref
7088 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7089 {
7090   dw_die_ref decl;
7091   dw_die_ref new_decl;
7092   dw_die_ref orig_parent = NULL;
7093
7094   decl = get_AT_ref (die, DW_AT_specification);
7095   if (decl == NULL)
7096     decl = die;
7097   else
7098     {
7099       unsigned ix;
7100       dw_die_ref c;
7101       dw_attr_ref a;
7102
7103       /* The original DIE will be changed to a declaration, and must
7104          be moved to be a child of the original declaration DIE.  */
7105       orig_parent = decl->die_parent;
7106
7107       /* Copy the type node pointer from the new DIE to the original
7108          declaration DIE so we can forward references later.  */
7109       decl->die_id.die_type_node = die->die_id.die_type_node;
7110
7111       remove_AT (die, DW_AT_specification);
7112
7113       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7114         {
7115           if (a->dw_attr != DW_AT_name
7116               && a->dw_attr != DW_AT_declaration
7117               && a->dw_attr != DW_AT_external)
7118             add_dwarf_attr (die, a);
7119         }
7120
7121       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7122     }
7123
7124   if (decl->die_parent != NULL
7125       && decl->die_parent->die_tag != DW_TAG_compile_unit
7126       && decl->die_parent->die_tag != DW_TAG_type_unit)
7127     {
7128       new_decl = copy_ancestor_tree (unit, decl, NULL);
7129       if (new_decl != NULL)
7130         {
7131           remove_AT (new_decl, DW_AT_signature);
7132           add_AT_specification (die, new_decl);
7133         }
7134     }
7135
7136   return orig_parent;
7137 }
7138
7139 /* Generate the skeleton ancestor tree for the given NODE, then clone
7140    the DIE and add the clone into the tree.  */
7141
7142 static void
7143 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7144 {
7145   if (node->new_die != NULL)
7146     return;
7147
7148   node->new_die = clone_as_declaration (node->old_die);
7149
7150   if (node->parent != NULL)
7151     {
7152       generate_skeleton_ancestor_tree (node->parent);
7153       add_child_die (node->parent->new_die, node->new_die);
7154     }
7155 }
7156
7157 /* Generate a skeleton tree of DIEs containing any declarations that are
7158    found in the original tree.  We traverse the tree looking for declaration
7159    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7160
7161 static void
7162 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7163 {
7164   skeleton_chain_node node;
7165   dw_die_ref c;
7166   dw_die_ref first;
7167   dw_die_ref prev = NULL;
7168   dw_die_ref next = NULL;
7169
7170   node.parent = parent;
7171
7172   first = c = parent->old_die->die_child;
7173   if (c)
7174     next = c->die_sib;
7175   if (c) do {
7176     if (prev == NULL || prev->die_sib == c)
7177       prev = c;
7178     c = next;
7179     next = (c == first ? NULL : c->die_sib);
7180     node.old_die = c;
7181     node.new_die = NULL;
7182     if (is_declaration_die (c))
7183       {
7184         /* Clone the existing DIE, move the original to the skeleton
7185            tree (which is in the main CU), and put the clone, with
7186            all the original's children, where the original came from.  */
7187         dw_die_ref clone = clone_die (c);
7188         move_all_children (c, clone);
7189
7190         replace_child (c, clone, prev);
7191         generate_skeleton_ancestor_tree (parent);
7192         add_child_die (parent->new_die, c);
7193         node.new_die = c;
7194         c = clone;
7195       }
7196     generate_skeleton_bottom_up (&node);
7197   } while (next != NULL);
7198 }
7199
7200 /* Wrapper function for generate_skeleton_bottom_up.  */
7201
7202 static dw_die_ref
7203 generate_skeleton (dw_die_ref die)
7204 {
7205   skeleton_chain_node node;
7206
7207   node.old_die = die;
7208   node.new_die = NULL;
7209   node.parent = NULL;
7210
7211   /* If this type definition is nested inside another type,
7212      always leave at least a declaration in its place.  */
7213   if (die->die_parent != NULL && is_type_die (die->die_parent))
7214     node.new_die = clone_as_declaration (die);
7215
7216   generate_skeleton_bottom_up (&node);
7217   return node.new_die;
7218 }
7219
7220 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7221    declaration.  The original DIE is moved to a new compile unit so that
7222    existing references to it follow it to the new location.  If any of the
7223    original DIE's descendants is a declaration, we need to replace the
7224    original DIE with a skeleton tree and move the declarations back into the
7225    skeleton tree.  */
7226
7227 static dw_die_ref
7228 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7229                                        dw_die_ref prev)
7230 {
7231   dw_die_ref skeleton, orig_parent;
7232
7233   /* Copy the declaration context to the type unit DIE.  If the returned
7234      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7235      that DIE.  */
7236   orig_parent = copy_declaration_context (unit, child);
7237
7238   skeleton = generate_skeleton (child);
7239   if (skeleton == NULL)
7240     remove_child_with_prev (child, prev);
7241   else
7242     {
7243       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7244
7245       /* If the original DIE was a specification, we need to put
7246          the skeleton under the parent DIE of the declaration.
7247          This leaves the original declaration in the tree, but
7248          it will be pruned later since there are no longer any
7249          references to it.  */
7250       if (orig_parent != NULL)
7251         {
7252           remove_child_with_prev (child, prev);
7253           add_child_die (orig_parent, skeleton);
7254         }
7255       else
7256         replace_child (child, skeleton, prev);
7257     }
7258
7259   return skeleton;
7260 }
7261
7262 /* Traverse the DIE and set up additional .debug_types sections for each
7263    type worthy of being placed in a COMDAT section.  */
7264
7265 static void
7266 break_out_comdat_types (dw_die_ref die)
7267 {
7268   dw_die_ref c;
7269   dw_die_ref first;
7270   dw_die_ref prev = NULL;
7271   dw_die_ref next = NULL;
7272   dw_die_ref unit = NULL;
7273
7274   first = c = die->die_child;
7275   if (c)
7276     next = c->die_sib;
7277   if (c) do {
7278     if (prev == NULL || prev->die_sib == c)
7279       prev = c;
7280     c = next;
7281     next = (c == first ? NULL : c->die_sib);
7282     if (should_move_die_to_comdat (c))
7283       {
7284         dw_die_ref replacement;
7285         comdat_type_node_ref type_node;
7286
7287         /* Create a new type unit DIE as the root for the new tree, and
7288            add it to the list of comdat types.  */
7289         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7290         add_AT_unsigned (unit, DW_AT_language,
7291                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7292         type_node = ggc_alloc_cleared_comdat_type_node ();
7293         type_node->root_die = unit;
7294         type_node->next = comdat_type_list;
7295         comdat_type_list = type_node;
7296
7297         /* Generate the type signature.  */
7298         generate_type_signature (c, type_node);
7299
7300         /* Copy the declaration context, attributes, and children of the
7301            declaration into the new type unit DIE, then remove this DIE
7302            from the main CU (or replace it with a skeleton if necessary).  */
7303         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7304
7305         /* Break out nested types into their own type units.  */
7306         break_out_comdat_types (c);
7307
7308         /* Add the DIE to the new compunit.  */
7309         add_child_die (unit, c);
7310
7311         if (replacement != NULL)
7312           c = replacement;
7313       }
7314     else if (c->die_tag == DW_TAG_namespace
7315              || c->die_tag == DW_TAG_class_type
7316              || c->die_tag == DW_TAG_structure_type
7317              || c->die_tag == DW_TAG_union_type)
7318       {
7319         /* Look for nested types that can be broken out.  */
7320         break_out_comdat_types (c);
7321       }
7322   } while (next != NULL);
7323 }
7324
7325 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7326
7327 struct decl_table_entry
7328 {
7329   dw_die_ref orig;
7330   dw_die_ref copy;
7331 };
7332
7333 /* Routines to manipulate hash table of copied declarations.  */
7334
7335 static hashval_t
7336 htab_decl_hash (const void *of)
7337 {
7338   const struct decl_table_entry *const entry =
7339     (const struct decl_table_entry *) of;
7340
7341   return htab_hash_pointer (entry->orig);
7342 }
7343
7344 static int
7345 htab_decl_eq (const void *of1, const void *of2)
7346 {
7347   const struct decl_table_entry *const entry1 =
7348     (const struct decl_table_entry *) of1;
7349   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7350
7351   return entry1->orig == entry2;
7352 }
7353
7354 static void
7355 htab_decl_del (void *what)
7356 {
7357   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7358
7359   free (entry);
7360 }
7361
7362 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7363    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7364    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7365    to check if the ancestor has already been copied into UNIT.  */
7366
7367 static dw_die_ref
7368 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7369 {
7370   dw_die_ref parent = die->die_parent;
7371   dw_die_ref new_parent = unit;
7372   dw_die_ref copy;
7373   void **slot = NULL;
7374   struct decl_table_entry *entry = NULL;
7375
7376   if (decl_table)
7377     {
7378       /* Check if the entry has already been copied to UNIT.  */
7379       slot = htab_find_slot_with_hash (decl_table, die,
7380                                        htab_hash_pointer (die), INSERT);
7381       if (*slot != HTAB_EMPTY_ENTRY)
7382         {
7383           entry = (struct decl_table_entry *) *slot;
7384           return entry->copy;
7385         }
7386
7387       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7388       entry = XCNEW (struct decl_table_entry);
7389       entry->orig = die;
7390       entry->copy = NULL;
7391       *slot = entry;
7392     }
7393
7394   if (parent != NULL)
7395     {
7396       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7397       if (spec != NULL)
7398         parent = spec;
7399       if (parent->die_tag != DW_TAG_compile_unit
7400           && parent->die_tag != DW_TAG_type_unit)
7401         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7402     }
7403
7404   copy = clone_as_declaration (die);
7405   add_child_die (new_parent, copy);
7406
7407   if (decl_table != NULL)
7408     {
7409       /* Record the pointer to the copy.  */
7410       entry->copy = copy;
7411     }
7412
7413   return copy;
7414 }
7415
7416 /* Like clone_tree, but additionally enter all the children into
7417    the hash table decl_table.  */
7418
7419 static dw_die_ref
7420 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7421 {
7422   dw_die_ref c;
7423   dw_die_ref clone = clone_die (die);
7424   struct decl_table_entry *entry;
7425   void **slot = htab_find_slot_with_hash (decl_table, die,
7426                                           htab_hash_pointer (die), INSERT);
7427   /* Assert that DIE isn't in the hash table yet.  If it would be there
7428      before, the ancestors would be necessarily there as well, therefore
7429      clone_tree_hash wouldn't be called.  */
7430   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7431   entry = XCNEW (struct decl_table_entry);
7432   entry->orig = die;
7433   entry->copy = clone;
7434   *slot = entry;
7435
7436   FOR_EACH_CHILD (die, c,
7437                   add_child_die (clone, clone_tree_hash (c, decl_table)));
7438
7439   return clone;
7440 }
7441
7442 /* Walk the DIE and its children, looking for references to incomplete
7443    or trivial types that are unmarked (i.e., that are not in the current
7444    type_unit).  */
7445
7446 static void
7447 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7448 {
7449   dw_die_ref c;
7450   dw_attr_ref a;
7451   unsigned ix;
7452
7453   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7454     {
7455       if (AT_class (a) == dw_val_class_die_ref)
7456         {
7457           dw_die_ref targ = AT_ref (a);
7458           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7459           void **slot;
7460           struct decl_table_entry *entry;
7461
7462           if (targ->die_mark != 0 || type_node != NULL)
7463             continue;
7464
7465           slot = htab_find_slot_with_hash (decl_table, targ,
7466                                            htab_hash_pointer (targ), INSERT);
7467
7468           if (*slot != HTAB_EMPTY_ENTRY)
7469             {
7470               /* TARG has already been copied, so we just need to
7471                  modify the reference to point to the copy.  */
7472               entry = (struct decl_table_entry *) *slot;
7473               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7474             }
7475           else
7476             {
7477               dw_die_ref parent = unit;
7478               dw_die_ref copy = clone_die (targ);
7479
7480               /* Record in DECL_TABLE that TARG has been copied.
7481                  Need to do this now, before the recursive call,
7482                  because DECL_TABLE may be expanded and SLOT
7483                  would no longer be a valid pointer.  */
7484               entry = XCNEW (struct decl_table_entry);
7485               entry->orig = targ;
7486               entry->copy = copy;
7487               *slot = entry;
7488
7489               FOR_EACH_CHILD (targ, c,
7490                               add_child_die (copy,
7491                                              clone_tree_hash (c, decl_table)));
7492
7493               /* Make sure the cloned tree is marked as part of the
7494                  type unit.  */
7495               mark_dies (copy);
7496
7497               /* If TARG has surrounding context, copy its ancestor tree
7498                  into the new type unit.  */
7499               if (targ->die_parent != NULL
7500                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7501                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7502                 parent = copy_ancestor_tree (unit, targ->die_parent,
7503                                              decl_table);
7504
7505               add_child_die (parent, copy);
7506               a->dw_attr_val.v.val_die_ref.die = copy;
7507
7508               /* Make sure the newly-copied DIE is walked.  If it was
7509                  installed in a previously-added context, it won't
7510                  get visited otherwise.  */
7511               if (parent != unit)
7512                 {
7513                   /* Find the highest point of the newly-added tree,
7514                      mark each node along the way, and walk from there.  */
7515                   parent->die_mark = 1;
7516                   while (parent->die_parent
7517                          && parent->die_parent->die_mark == 0)
7518                     {
7519                       parent = parent->die_parent;
7520                       parent->die_mark = 1;
7521                     }
7522                   copy_decls_walk (unit, parent, decl_table);
7523                 }
7524             }
7525         }
7526     }
7527
7528   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7529 }
7530
7531 /* Copy declarations for "unworthy" types into the new comdat section.
7532    Incomplete types, modified types, and certain other types aren't broken
7533    out into comdat sections of their own, so they don't have a signature,
7534    and we need to copy the declaration into the same section so that we
7535    don't have an external reference.  */
7536
7537 static void
7538 copy_decls_for_unworthy_types (dw_die_ref unit)
7539 {
7540   htab_t decl_table;
7541
7542   mark_dies (unit);
7543   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7544   copy_decls_walk (unit, unit, decl_table);
7545   htab_delete (decl_table);
7546   unmark_dies (unit);
7547 }
7548
7549 /* Traverse the DIE and add a sibling attribute if it may have the
7550    effect of speeding up access to siblings.  To save some space,
7551    avoid generating sibling attributes for DIE's without children.  */
7552
7553 static void
7554 add_sibling_attributes (dw_die_ref die)
7555 {
7556   dw_die_ref c;
7557
7558   if (! die->die_child)
7559     return;
7560
7561   if (die->die_parent && die != die->die_parent->die_child)
7562     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7563
7564   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7565 }
7566
7567 /* Output all location lists for the DIE and its children.  */
7568
7569 static void
7570 output_location_lists (dw_die_ref die)
7571 {
7572   dw_die_ref c;
7573   dw_attr_ref a;
7574   unsigned ix;
7575
7576   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7577     if (AT_class (a) == dw_val_class_loc_list)
7578       output_loc_list (AT_loc_list (a));
7579
7580   FOR_EACH_CHILD (die, c, output_location_lists (c));
7581 }
7582
7583 /* The format of each DIE (and its attribute value pairs) is encoded in an
7584    abbreviation table.  This routine builds the abbreviation table and assigns
7585    a unique abbreviation id for each abbreviation entry.  The children of each
7586    die are visited recursively.  */
7587
7588 static void
7589 build_abbrev_table (dw_die_ref die)
7590 {
7591   unsigned long abbrev_id;
7592   unsigned int n_alloc;
7593   dw_die_ref c;
7594   dw_attr_ref a;
7595   unsigned ix;
7596
7597   /* Scan the DIE references, and mark as external any that refer to
7598      DIEs from other CUs (i.e. those which are not marked).  */
7599   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7600     if (AT_class (a) == dw_val_class_die_ref
7601         && AT_ref (a)->die_mark == 0)
7602       {
7603         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7604         set_AT_ref_external (a, 1);
7605       }
7606
7607   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7608     {
7609       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7610       dw_attr_ref die_a, abbrev_a;
7611       unsigned ix;
7612       bool ok = true;
7613
7614       if (abbrev->die_tag != die->die_tag)
7615         continue;
7616       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7617         continue;
7618
7619       if (VEC_length (dw_attr_node, abbrev->die_attr)
7620           != VEC_length (dw_attr_node, die->die_attr))
7621         continue;
7622
7623       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7624         {
7625           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7626           if ((abbrev_a->dw_attr != die_a->dw_attr)
7627               || (value_format (abbrev_a) != value_format (die_a)))
7628             {
7629               ok = false;
7630               break;
7631             }
7632         }
7633       if (ok)
7634         break;
7635     }
7636
7637   if (abbrev_id >= abbrev_die_table_in_use)
7638     {
7639       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7640         {
7641           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7642           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7643                                             n_alloc);
7644
7645           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7646                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7647           abbrev_die_table_allocated = n_alloc;
7648         }
7649
7650       ++abbrev_die_table_in_use;
7651       abbrev_die_table[abbrev_id] = die;
7652     }
7653
7654   die->die_abbrev = abbrev_id;
7655   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7656 }
7657 \f
7658 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7659
7660 static int
7661 constant_size (unsigned HOST_WIDE_INT value)
7662 {
7663   int log;
7664
7665   if (value == 0)
7666     log = 0;
7667   else
7668     log = floor_log2 (value);
7669
7670   log = log / 8;
7671   log = 1 << (floor_log2 (log) + 1);
7672
7673   return log;
7674 }
7675
7676 /* Return the size of a DIE as it is represented in the
7677    .debug_info section.  */
7678
7679 static unsigned long
7680 size_of_die (dw_die_ref die)
7681 {
7682   unsigned long size = 0;
7683   dw_attr_ref a;
7684   unsigned ix;
7685
7686   size += size_of_uleb128 (die->die_abbrev);
7687   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7688     {
7689       switch (AT_class (a))
7690         {
7691         case dw_val_class_addr:
7692           size += DWARF2_ADDR_SIZE;
7693           break;
7694         case dw_val_class_offset:
7695           size += DWARF_OFFSET_SIZE;
7696           break;
7697         case dw_val_class_loc:
7698           {
7699             unsigned long lsize = size_of_locs (AT_loc (a));
7700
7701             /* Block length.  */
7702             if (dwarf_version >= 4)
7703               size += size_of_uleb128 (lsize);
7704             else
7705               size += constant_size (lsize);
7706             size += lsize;
7707           }
7708           break;
7709         case dw_val_class_loc_list:
7710           size += DWARF_OFFSET_SIZE;
7711           break;
7712         case dw_val_class_range_list:
7713           size += DWARF_OFFSET_SIZE;
7714           break;
7715         case dw_val_class_const:
7716           size += size_of_sleb128 (AT_int (a));
7717           break;
7718         case dw_val_class_unsigned_const:
7719           {
7720             int csize = constant_size (AT_unsigned (a));
7721             if (dwarf_version == 3
7722                 && a->dw_attr == DW_AT_data_member_location
7723                 && csize >= 4)
7724               size += size_of_uleb128 (AT_unsigned (a));
7725             else
7726               size += csize;
7727           }
7728           break;
7729         case dw_val_class_const_double:
7730           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7731           if (HOST_BITS_PER_WIDE_INT >= 64)
7732             size++; /* block */
7733           break;
7734         case dw_val_class_vec:
7735           size += constant_size (a->dw_attr_val.v.val_vec.length
7736                                  * a->dw_attr_val.v.val_vec.elt_size)
7737                   + a->dw_attr_val.v.val_vec.length
7738                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7739           break;
7740         case dw_val_class_flag:
7741           if (dwarf_version >= 4)
7742             /* Currently all add_AT_flag calls pass in 1 as last argument,
7743                so DW_FORM_flag_present can be used.  If that ever changes,
7744                we'll need to use DW_FORM_flag and have some optimization
7745                in build_abbrev_table that will change those to
7746                DW_FORM_flag_present if it is set to 1 in all DIEs using
7747                the same abbrev entry.  */
7748             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7749           else
7750             size += 1;
7751           break;
7752         case dw_val_class_die_ref:
7753           if (AT_ref_external (a))
7754             {
7755               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7756                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7757                  is sized by target address length, whereas in DWARF3
7758                  it's always sized as an offset.  */
7759               if (use_debug_types)
7760                 size += DWARF_TYPE_SIGNATURE_SIZE;
7761               else if (dwarf_version == 2)
7762                 size += DWARF2_ADDR_SIZE;
7763               else
7764                 size += DWARF_OFFSET_SIZE;
7765             }
7766           else
7767             size += DWARF_OFFSET_SIZE;
7768           break;
7769         case dw_val_class_fde_ref:
7770           size += DWARF_OFFSET_SIZE;
7771           break;
7772         case dw_val_class_lbl_id:
7773           size += DWARF2_ADDR_SIZE;
7774           break;
7775         case dw_val_class_lineptr:
7776         case dw_val_class_macptr:
7777           size += DWARF_OFFSET_SIZE;
7778           break;
7779         case dw_val_class_str:
7780           if (AT_string_form (a) == DW_FORM_strp)
7781             size += DWARF_OFFSET_SIZE;
7782           else
7783             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7784           break;
7785         case dw_val_class_file:
7786           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7787           break;
7788         case dw_val_class_data8:
7789           size += 8;
7790           break;
7791         case dw_val_class_vms_delta:
7792           size += DWARF_OFFSET_SIZE;
7793           break;
7794         default:
7795           gcc_unreachable ();
7796         }
7797     }
7798
7799   return size;
7800 }
7801
7802 /* Size the debugging information associated with a given DIE.  Visits the
7803    DIE's children recursively.  Updates the global variable next_die_offset, on
7804    each time through.  Uses the current value of next_die_offset to update the
7805    die_offset field in each DIE.  */
7806
7807 static void
7808 calc_die_sizes (dw_die_ref die)
7809 {
7810   dw_die_ref c;
7811
7812   gcc_assert (die->die_offset == 0
7813               || (unsigned long int) die->die_offset == next_die_offset);
7814   die->die_offset = next_die_offset;
7815   next_die_offset += size_of_die (die);
7816
7817   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7818
7819   if (die->die_child != NULL)
7820     /* Count the null byte used to terminate sibling lists.  */
7821     next_die_offset += 1;
7822 }
7823
7824 /* Size just the base type children at the start of the CU.
7825    This is needed because build_abbrev needs to size locs
7826    and sizing of type based stack ops needs to know die_offset
7827    values for the base types.  */
7828
7829 static void
7830 calc_base_type_die_sizes (void)
7831 {
7832   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7833   unsigned int i;
7834   dw_die_ref base_type;
7835 #if ENABLE_ASSERT_CHECKING
7836   dw_die_ref prev = comp_unit_die ()->die_child;
7837 #endif
7838
7839   die_offset += size_of_die (comp_unit_die ());
7840   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7841     {
7842 #if ENABLE_ASSERT_CHECKING
7843       gcc_assert (base_type->die_offset == 0
7844                   && prev->die_sib == base_type
7845                   && base_type->die_child == NULL
7846                   && base_type->die_abbrev);
7847       prev = base_type;
7848 #endif
7849       base_type->die_offset = die_offset;
7850       die_offset += size_of_die (base_type);
7851     }
7852 }
7853
7854 /* Set the marks for a die and its children.  We do this so
7855    that we know whether or not a reference needs to use FORM_ref_addr; only
7856    DIEs in the same CU will be marked.  We used to clear out the offset
7857    and use that as the flag, but ran into ordering problems.  */
7858
7859 static void
7860 mark_dies (dw_die_ref die)
7861 {
7862   dw_die_ref c;
7863
7864   gcc_assert (!die->die_mark);
7865
7866   die->die_mark = 1;
7867   FOR_EACH_CHILD (die, c, mark_dies (c));
7868 }
7869
7870 /* Clear the marks for a die and its children.  */
7871
7872 static void
7873 unmark_dies (dw_die_ref die)
7874 {
7875   dw_die_ref c;
7876
7877   if (! use_debug_types)
7878     gcc_assert (die->die_mark);
7879
7880   die->die_mark = 0;
7881   FOR_EACH_CHILD (die, c, unmark_dies (c));
7882 }
7883
7884 /* Clear the marks for a die, its children and referred dies.  */
7885
7886 static void
7887 unmark_all_dies (dw_die_ref die)
7888 {
7889   dw_die_ref c;
7890   dw_attr_ref a;
7891   unsigned ix;
7892
7893   if (!die->die_mark)
7894     return;
7895   die->die_mark = 0;
7896
7897   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7898
7899   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7900     if (AT_class (a) == dw_val_class_die_ref)
7901       unmark_all_dies (AT_ref (a));
7902 }
7903
7904 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7905    generated for the compilation unit.  */
7906
7907 static unsigned long
7908 size_of_pubnames (VEC (pubname_entry, gc) * names)
7909 {
7910   unsigned long size;
7911   unsigned i;
7912   pubname_ref p;
7913
7914   size = DWARF_PUBNAMES_HEADER_SIZE;
7915   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7916     if (names != pubtype_table
7917         || p->die->die_offset != 0
7918         || !flag_eliminate_unused_debug_types)
7919       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7920
7921   size += DWARF_OFFSET_SIZE;
7922   return size;
7923 }
7924
7925 /* Return the size of the information in the .debug_aranges section.  */
7926
7927 static unsigned long
7928 size_of_aranges (void)
7929 {
7930   unsigned long size;
7931
7932   size = DWARF_ARANGES_HEADER_SIZE;
7933
7934   /* Count the address/length pair for this compilation unit.  */
7935   if (text_section_used)
7936     size += 2 * DWARF2_ADDR_SIZE;
7937   if (cold_text_section_used)
7938     size += 2 * DWARF2_ADDR_SIZE;
7939   if (have_multiple_function_sections)
7940     {
7941       unsigned fde_idx;
7942       dw_fde_ref fde;
7943
7944       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7945         {
7946           if (!fde->in_std_section)
7947             size += 2 * DWARF2_ADDR_SIZE;
7948           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7949             size += 2 * DWARF2_ADDR_SIZE;
7950         }
7951     }
7952
7953   /* Count the two zero words used to terminated the address range table.  */
7954   size += 2 * DWARF2_ADDR_SIZE;
7955   return size;
7956 }
7957 \f
7958 /* Select the encoding of an attribute value.  */
7959
7960 static enum dwarf_form
7961 value_format (dw_attr_ref a)
7962 {
7963   switch (a->dw_attr_val.val_class)
7964     {
7965     case dw_val_class_addr:
7966       /* Only very few attributes allow DW_FORM_addr.  */
7967       switch (a->dw_attr)
7968         {
7969         case DW_AT_low_pc:
7970         case DW_AT_high_pc:
7971         case DW_AT_entry_pc:
7972         case DW_AT_trampoline:
7973           return DW_FORM_addr;
7974         default:
7975           break;
7976         }
7977       switch (DWARF2_ADDR_SIZE)
7978         {
7979         case 1:
7980           return DW_FORM_data1;
7981         case 2:
7982           return DW_FORM_data2;
7983         case 4:
7984           return DW_FORM_data4;
7985         case 8:
7986           return DW_FORM_data8;
7987         default:
7988           gcc_unreachable ();
7989         }
7990     case dw_val_class_range_list:
7991     case dw_val_class_loc_list:
7992       if (dwarf_version >= 4)
7993         return DW_FORM_sec_offset;
7994       /* FALLTHRU */
7995     case dw_val_class_vms_delta:
7996     case dw_val_class_offset:
7997       switch (DWARF_OFFSET_SIZE)
7998         {
7999         case 4:
8000           return DW_FORM_data4;
8001         case 8:
8002           return DW_FORM_data8;
8003         default:
8004           gcc_unreachable ();
8005         }
8006     case dw_val_class_loc:
8007       if (dwarf_version >= 4)
8008         return DW_FORM_exprloc;
8009       switch (constant_size (size_of_locs (AT_loc (a))))
8010         {
8011         case 1:
8012           return DW_FORM_block1;
8013         case 2:
8014           return DW_FORM_block2;
8015         case 4:
8016           return DW_FORM_block4;
8017         default:
8018           gcc_unreachable ();
8019         }
8020     case dw_val_class_const:
8021       return DW_FORM_sdata;
8022     case dw_val_class_unsigned_const:
8023       switch (constant_size (AT_unsigned (a)))
8024         {
8025         case 1:
8026           return DW_FORM_data1;
8027         case 2:
8028           return DW_FORM_data2;
8029         case 4:
8030           /* In DWARF3 DW_AT_data_member_location with
8031              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8032              constant, so we need to use DW_FORM_udata if we need
8033              a large constant.  */
8034           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8035             return DW_FORM_udata;
8036           return DW_FORM_data4;
8037         case 8:
8038           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8039             return DW_FORM_udata;
8040           return DW_FORM_data8;
8041         default:
8042           gcc_unreachable ();
8043         }
8044     case dw_val_class_const_double:
8045       switch (HOST_BITS_PER_WIDE_INT)
8046         {
8047         case 8:
8048           return DW_FORM_data2;
8049         case 16:
8050           return DW_FORM_data4;
8051         case 32:
8052           return DW_FORM_data8;
8053         case 64:
8054         default:
8055           return DW_FORM_block1;
8056         }
8057     case dw_val_class_vec:
8058       switch (constant_size (a->dw_attr_val.v.val_vec.length
8059                              * a->dw_attr_val.v.val_vec.elt_size))
8060         {
8061         case 1:
8062           return DW_FORM_block1;
8063         case 2:
8064           return DW_FORM_block2;
8065         case 4:
8066           return DW_FORM_block4;
8067         default:
8068           gcc_unreachable ();
8069         }
8070     case dw_val_class_flag:
8071       if (dwarf_version >= 4)
8072         {
8073           /* Currently all add_AT_flag calls pass in 1 as last argument,
8074              so DW_FORM_flag_present can be used.  If that ever changes,
8075              we'll need to use DW_FORM_flag and have some optimization
8076              in build_abbrev_table that will change those to
8077              DW_FORM_flag_present if it is set to 1 in all DIEs using
8078              the same abbrev entry.  */
8079           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8080           return DW_FORM_flag_present;
8081         }
8082       return DW_FORM_flag;
8083     case dw_val_class_die_ref:
8084       if (AT_ref_external (a))
8085         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8086       else
8087         return DW_FORM_ref;
8088     case dw_val_class_fde_ref:
8089       return DW_FORM_data;
8090     case dw_val_class_lbl_id:
8091       return DW_FORM_addr;
8092     case dw_val_class_lineptr:
8093     case dw_val_class_macptr:
8094       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8095     case dw_val_class_str:
8096       return AT_string_form (a);
8097     case dw_val_class_file:
8098       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8099         {
8100         case 1:
8101           return DW_FORM_data1;
8102         case 2:
8103           return DW_FORM_data2;
8104         case 4:
8105           return DW_FORM_data4;
8106         default:
8107           gcc_unreachable ();
8108         }
8109
8110     case dw_val_class_data8:
8111       return DW_FORM_data8;
8112
8113     default:
8114       gcc_unreachable ();
8115     }
8116 }
8117
8118 /* Output the encoding of an attribute value.  */
8119
8120 static void
8121 output_value_format (dw_attr_ref a)
8122 {
8123   enum dwarf_form form = value_format (a);
8124
8125   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8126 }
8127
8128 /* Output the .debug_abbrev section which defines the DIE abbreviation
8129    table.  */
8130
8131 static void
8132 output_abbrev_section (void)
8133 {
8134   unsigned long abbrev_id;
8135
8136   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8137     {
8138       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8139       unsigned ix;
8140       dw_attr_ref a_attr;
8141
8142       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8143       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8144                                    dwarf_tag_name (abbrev->die_tag));
8145
8146       if (abbrev->die_child != NULL)
8147         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8148       else
8149         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8150
8151       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8152            ix++)
8153         {
8154           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8155                                        dwarf_attr_name (a_attr->dw_attr));
8156           output_value_format (a_attr);
8157         }
8158
8159       dw2_asm_output_data (1, 0, NULL);
8160       dw2_asm_output_data (1, 0, NULL);
8161     }
8162
8163   /* Terminate the table.  */
8164   dw2_asm_output_data (1, 0, NULL);
8165 }
8166
8167 /* Output a symbol we can use to refer to this DIE from another CU.  */
8168
8169 static inline void
8170 output_die_symbol (dw_die_ref die)
8171 {
8172   char *sym = die->die_id.die_symbol;
8173
8174   if (sym == 0)
8175     return;
8176
8177   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8178     /* We make these global, not weak; if the target doesn't support
8179        .linkonce, it doesn't support combining the sections, so debugging
8180        will break.  */
8181     targetm.asm_out.globalize_label (asm_out_file, sym);
8182
8183   ASM_OUTPUT_LABEL (asm_out_file, sym);
8184 }
8185
8186 /* Return a new location list, given the begin and end range, and the
8187    expression.  */
8188
8189 static inline dw_loc_list_ref
8190 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8191               const char *section)
8192 {
8193   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8194
8195   retlist->begin = begin;
8196   retlist->end = end;
8197   retlist->expr = expr;
8198   retlist->section = section;
8199
8200   return retlist;
8201 }
8202
8203 /* Generate a new internal symbol for this location list node, if it
8204    hasn't got one yet.  */
8205
8206 static inline void
8207 gen_llsym (dw_loc_list_ref list)
8208 {
8209   gcc_assert (!list->ll_symbol);
8210   list->ll_symbol = gen_internal_sym ("LLST");
8211 }
8212
8213 /* Output the location list given to us.  */
8214
8215 static void
8216 output_loc_list (dw_loc_list_ref list_head)
8217 {
8218   dw_loc_list_ref curr = list_head;
8219
8220   if (list_head->emitted)
8221     return;
8222   list_head->emitted = true;
8223
8224   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8225
8226   /* Walk the location list, and output each range + expression.  */
8227   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8228     {
8229       unsigned long size;
8230       /* Don't output an entry that starts and ends at the same address.  */
8231       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8232         continue;
8233       size = size_of_locs (curr->expr);
8234       /* If the expression is too large, drop it on the floor.  We could
8235          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8236          in the expression, but >= 64KB expressions for a single value
8237          in a single range are unlikely very useful.  */
8238       if (size > 0xffff)
8239         continue;
8240       if (!have_multiple_function_sections)
8241         {
8242           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8243                                 "Location list begin address (%s)",
8244                                 list_head->ll_symbol);
8245           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8246                                 "Location list end address (%s)",
8247                                 list_head->ll_symbol);
8248         }
8249       else
8250         {
8251           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8252                                "Location list begin address (%s)",
8253                                list_head->ll_symbol);
8254           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8255                                "Location list end address (%s)",
8256                                list_head->ll_symbol);
8257         }
8258
8259       /* Output the block length for this list of location operations.  */
8260       gcc_assert (size <= 0xffff);
8261       dw2_asm_output_data (2, size, "%s", "Location expression size");
8262
8263       output_loc_sequence (curr->expr, -1);
8264     }
8265
8266   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8267                        "Location list terminator begin (%s)",
8268                        list_head->ll_symbol);
8269   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8270                        "Location list terminator end (%s)",
8271                        list_head->ll_symbol);
8272 }
8273
8274 /* Output a type signature.  */
8275
8276 static inline void
8277 output_signature (const char *sig, const char *name)
8278 {
8279   int i;
8280
8281   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8282     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8283 }
8284
8285 /* Output the DIE and its attributes.  Called recursively to generate
8286    the definitions of each child DIE.  */
8287
8288 static void
8289 output_die (dw_die_ref die)
8290 {
8291   dw_attr_ref a;
8292   dw_die_ref c;
8293   unsigned long size;
8294   unsigned ix;
8295
8296   /* If someone in another CU might refer to us, set up a symbol for
8297      them to point to.  */
8298   if (! use_debug_types && die->die_id.die_symbol)
8299     output_die_symbol (die);
8300
8301   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8302                                (unsigned long)die->die_offset,
8303                                dwarf_tag_name (die->die_tag));
8304
8305   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8306     {
8307       const char *name = dwarf_attr_name (a->dw_attr);
8308
8309       switch (AT_class (a))
8310         {
8311         case dw_val_class_addr:
8312           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8313           break;
8314
8315         case dw_val_class_offset:
8316           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8317                                "%s", name);
8318           break;
8319
8320         case dw_val_class_range_list:
8321           {
8322             char *p = strchr (ranges_section_label, '\0');
8323
8324             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8325                      a->dw_attr_val.v.val_offset);
8326             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8327                                    debug_ranges_section, "%s", name);
8328             *p = '\0';
8329           }
8330           break;
8331
8332         case dw_val_class_loc:
8333           size = size_of_locs (AT_loc (a));
8334
8335           /* Output the block length for this list of location operations.  */
8336           if (dwarf_version >= 4)
8337             dw2_asm_output_data_uleb128 (size, "%s", name);
8338           else
8339             dw2_asm_output_data (constant_size (size), size, "%s", name);
8340
8341           output_loc_sequence (AT_loc (a), -1);
8342           break;
8343
8344         case dw_val_class_const:
8345           /* ??? It would be slightly more efficient to use a scheme like is
8346              used for unsigned constants below, but gdb 4.x does not sign
8347              extend.  Gdb 5.x does sign extend.  */
8348           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8349           break;
8350
8351         case dw_val_class_unsigned_const:
8352           {
8353             int csize = constant_size (AT_unsigned (a));
8354             if (dwarf_version == 3
8355                 && a->dw_attr == DW_AT_data_member_location
8356                 && csize >= 4)
8357               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8358             else
8359               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8360           }
8361           break;
8362
8363         case dw_val_class_const_double:
8364           {
8365             unsigned HOST_WIDE_INT first, second;
8366
8367             if (HOST_BITS_PER_WIDE_INT >= 64)
8368               dw2_asm_output_data (1,
8369                                    2 * HOST_BITS_PER_WIDE_INT
8370                                    / HOST_BITS_PER_CHAR,
8371                                    NULL);
8372
8373             if (WORDS_BIG_ENDIAN)
8374               {
8375                 first = a->dw_attr_val.v.val_double.high;
8376                 second = a->dw_attr_val.v.val_double.low;
8377               }
8378             else
8379               {
8380                 first = a->dw_attr_val.v.val_double.low;
8381                 second = a->dw_attr_val.v.val_double.high;
8382               }
8383
8384             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8385                                  first, name);
8386             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8387                                  second, NULL);
8388           }
8389           break;
8390
8391         case dw_val_class_vec:
8392           {
8393             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8394             unsigned int len = a->dw_attr_val.v.val_vec.length;
8395             unsigned int i;
8396             unsigned char *p;
8397
8398             dw2_asm_output_data (constant_size (len * elt_size),
8399                                  len * elt_size, "%s", name);
8400             if (elt_size > sizeof (HOST_WIDE_INT))
8401               {
8402                 elt_size /= 2;
8403                 len *= 2;
8404               }
8405             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8406                  i < len;
8407                  i++, p += elt_size)
8408               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8409                                    "fp or vector constant word %u", i);
8410             break;
8411           }
8412
8413         case dw_val_class_flag:
8414           if (dwarf_version >= 4)
8415             {
8416               /* Currently all add_AT_flag calls pass in 1 as last argument,
8417                  so DW_FORM_flag_present can be used.  If that ever changes,
8418                  we'll need to use DW_FORM_flag and have some optimization
8419                  in build_abbrev_table that will change those to
8420                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8421                  the same abbrev entry.  */
8422               gcc_assert (AT_flag (a) == 1);
8423               if (flag_debug_asm)
8424                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8425                          ASM_COMMENT_START, name);
8426               break;
8427             }
8428           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8429           break;
8430
8431         case dw_val_class_loc_list:
8432           {
8433             char *sym = AT_loc_list (a)->ll_symbol;
8434
8435             gcc_assert (sym);
8436             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8437                                    "%s", name);
8438           }
8439           break;
8440
8441         case dw_val_class_die_ref:
8442           if (AT_ref_external (a))
8443             {
8444               if (use_debug_types)
8445                 {
8446                   comdat_type_node_ref type_node =
8447                     AT_ref (a)->die_id.die_type_node;
8448
8449                   gcc_assert (type_node);
8450                   output_signature (type_node->signature, name);
8451                 }
8452               else
8453                 {
8454                   char *sym = AT_ref (a)->die_id.die_symbol;
8455                   int size;
8456
8457                   gcc_assert (sym);
8458                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8459                      length, whereas in DWARF3 it's always sized as an
8460                      offset.  */
8461                   if (dwarf_version == 2)
8462                     size = DWARF2_ADDR_SIZE;
8463                   else
8464                     size = DWARF_OFFSET_SIZE;
8465                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8466                                          name);
8467                 }
8468             }
8469           else
8470             {
8471               gcc_assert (AT_ref (a)->die_offset);
8472               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8473                                    "%s", name);
8474             }
8475           break;
8476
8477         case dw_val_class_fde_ref:
8478           {
8479             char l1[20];
8480
8481             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8482                                          a->dw_attr_val.v.val_fde_index * 2);
8483             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8484                                    "%s", name);
8485           }
8486           break;
8487
8488         case dw_val_class_vms_delta:
8489           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8490                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8491                                     "%s", name);
8492           break;
8493
8494         case dw_val_class_lbl_id:
8495           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8496           break;
8497
8498         case dw_val_class_lineptr:
8499           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8500                                  debug_line_section, "%s", name);
8501           break;
8502
8503         case dw_val_class_macptr:
8504           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8505                                  debug_macinfo_section, "%s", name);
8506           break;
8507
8508         case dw_val_class_str:
8509           if (AT_string_form (a) == DW_FORM_strp)
8510             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8511                                    a->dw_attr_val.v.val_str->label,
8512                                    debug_str_section,
8513                                    "%s: \"%s\"", name, AT_string (a));
8514           else
8515             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8516           break;
8517
8518         case dw_val_class_file:
8519           {
8520             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8521
8522             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8523                                  a->dw_attr_val.v.val_file->filename);
8524             break;
8525           }
8526
8527         case dw_val_class_data8:
8528           {
8529             int i;
8530
8531             for (i = 0; i < 8; i++)
8532               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8533                                    i == 0 ? "%s" : NULL, name);
8534             break;
8535           }
8536
8537         default:
8538           gcc_unreachable ();
8539         }
8540     }
8541
8542   FOR_EACH_CHILD (die, c, output_die (c));
8543
8544   /* Add null byte to terminate sibling list.  */
8545   if (die->die_child != NULL)
8546     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8547                          (unsigned long) die->die_offset);
8548 }
8549
8550 /* Output the compilation unit that appears at the beginning of the
8551    .debug_info section, and precedes the DIE descriptions.  */
8552
8553 static void
8554 output_compilation_unit_header (void)
8555 {
8556   int ver = dwarf_version;
8557
8558   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8559     dw2_asm_output_data (4, 0xffffffff,
8560       "Initial length escape value indicating 64-bit DWARF extension");
8561   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8562                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8563                        "Length of Compilation Unit Info");
8564   dw2_asm_output_data (2, ver, "DWARF version number");
8565   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8566                          debug_abbrev_section,
8567                          "Offset Into Abbrev. Section");
8568   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8569 }
8570
8571 /* Output the compilation unit DIE and its children.  */
8572
8573 static void
8574 output_comp_unit (dw_die_ref die, int output_if_empty)
8575 {
8576   const char *secname;
8577   char *oldsym, *tmp;
8578
8579   /* Unless we are outputting main CU, we may throw away empty ones.  */
8580   if (!output_if_empty && die->die_child == NULL)
8581     return;
8582
8583   /* Even if there are no children of this DIE, we must output the information
8584      about the compilation unit.  Otherwise, on an empty translation unit, we
8585      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8586      will then complain when examining the file.  First mark all the DIEs in
8587      this CU so we know which get local refs.  */
8588   mark_dies (die);
8589
8590   build_abbrev_table (die);
8591
8592   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8593   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8594   calc_die_sizes (die);
8595
8596   oldsym = die->die_id.die_symbol;
8597   if (oldsym)
8598     {
8599       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8600
8601       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8602       secname = tmp;
8603       die->die_id.die_symbol = NULL;
8604       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8605     }
8606   else
8607     {
8608       switch_to_section (debug_info_section);
8609       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8610       info_section_emitted = true;
8611     }
8612
8613   /* Output debugging information.  */
8614   output_compilation_unit_header ();
8615   output_die (die);
8616
8617   /* Leave the marks on the main CU, so we can check them in
8618      output_pubnames.  */
8619   if (oldsym)
8620     {
8621       unmark_dies (die);
8622       die->die_id.die_symbol = oldsym;
8623     }
8624 }
8625
8626 /* Output a comdat type unit DIE and its children.  */
8627
8628 static void
8629 output_comdat_type_unit (comdat_type_node *node)
8630 {
8631   const char *secname;
8632   char *tmp;
8633   int i;
8634 #if defined (OBJECT_FORMAT_ELF)
8635   tree comdat_key;
8636 #endif
8637
8638   /* First mark all the DIEs in this CU so we know which get local refs.  */
8639   mark_dies (node->root_die);
8640
8641   build_abbrev_table (node->root_die);
8642
8643   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8644   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8645   calc_die_sizes (node->root_die);
8646
8647 #if defined (OBJECT_FORMAT_ELF)
8648   secname = ".debug_types";
8649   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8650   sprintf (tmp, "wt.");
8651   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8652     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8653   comdat_key = get_identifier (tmp);
8654   targetm.asm_out.named_section (secname,
8655                                  SECTION_DEBUG | SECTION_LINKONCE,
8656                                  comdat_key);
8657 #else
8658   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8659   sprintf (tmp, ".gnu.linkonce.wt.");
8660   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8661     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8662   secname = tmp;
8663   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8664 #endif
8665
8666   /* Output debugging information.  */
8667   output_compilation_unit_header ();
8668   output_signature (node->signature, "Type Signature");
8669   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8670                        "Offset to Type DIE");
8671   output_die (node->root_die);
8672
8673   unmark_dies (node->root_die);
8674 }
8675
8676 /* Return the DWARF2/3 pubname associated with a decl.  */
8677
8678 static const char *
8679 dwarf2_name (tree decl, int scope)
8680 {
8681   if (DECL_NAMELESS (decl))
8682     return NULL;
8683   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8684 }
8685
8686 /* Add a new entry to .debug_pubnames if appropriate.  */
8687
8688 static void
8689 add_pubname_string (const char *str, dw_die_ref die)
8690 {
8691   if (targetm.want_debug_pub_sections)
8692     {
8693       pubname_entry e;
8694
8695       e.die = die;
8696       e.name = xstrdup (str);
8697       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8698     }
8699 }
8700
8701 static void
8702 add_pubname (tree decl, dw_die_ref die)
8703 {
8704   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8705     {
8706       const char *name = dwarf2_name (decl, 1);
8707       if (name)
8708         add_pubname_string (name, die);
8709     }
8710 }
8711
8712 /* Add a new entry to .debug_pubtypes if appropriate.  */
8713
8714 static void
8715 add_pubtype (tree decl, dw_die_ref die)
8716 {
8717   pubname_entry e;
8718
8719   if (!targetm.want_debug_pub_sections)
8720     return;
8721
8722   e.name = NULL;
8723   if ((TREE_PUBLIC (decl)
8724        || is_cu_die (die->die_parent))
8725       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8726     {
8727       e.die = die;
8728       if (TYPE_P (decl))
8729         {
8730           if (TYPE_NAME (decl))
8731             {
8732               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8733                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8734               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8735                        && DECL_NAME (TYPE_NAME (decl)))
8736                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8737               else
8738                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8739             }
8740         }
8741       else
8742         {
8743           e.name = dwarf2_name (decl, 1);
8744           if (e.name)
8745             e.name = xstrdup (e.name);
8746         }
8747
8748       /* If we don't have a name for the type, there's no point in adding
8749          it to the table.  */
8750       if (e.name && e.name[0] != '\0')
8751         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8752     }
8753 }
8754
8755 /* Output the public names table used to speed up access to externally
8756    visible names; or the public types table used to find type definitions.  */
8757
8758 static void
8759 output_pubnames (VEC (pubname_entry, gc) * names)
8760 {
8761   unsigned i;
8762   unsigned long pubnames_length = size_of_pubnames (names);
8763   pubname_ref pub;
8764
8765   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8766     dw2_asm_output_data (4, 0xffffffff,
8767       "Initial length escape value indicating 64-bit DWARF extension");
8768   if (names == pubname_table)
8769     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8770                          "Length of Public Names Info");
8771   else
8772     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8773                          "Length of Public Type Names Info");
8774   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8775   dw2_asm_output_data (2, 2, "DWARF Version");
8776   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8777                          debug_info_section,
8778                          "Offset of Compilation Unit Info");
8779   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8780                        "Compilation Unit Length");
8781
8782   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8783     {
8784       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8785       if (names == pubname_table)
8786         gcc_assert (pub->die->die_mark);
8787
8788       if (names != pubtype_table
8789           || pub->die->die_offset != 0
8790           || !flag_eliminate_unused_debug_types)
8791         {
8792           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8793                                "DIE offset");
8794
8795           dw2_asm_output_nstring (pub->name, -1, "external name");
8796         }
8797     }
8798
8799   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8800 }
8801
8802 /* Output the information that goes into the .debug_aranges table.
8803    Namely, define the beginning and ending address range of the
8804    text section generated for this compilation unit.  */
8805
8806 static void
8807 output_aranges (unsigned long aranges_length)
8808 {
8809   unsigned i;
8810
8811   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8812     dw2_asm_output_data (4, 0xffffffff,
8813       "Initial length escape value indicating 64-bit DWARF extension");
8814   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8815                        "Length of Address Ranges Info");
8816   /* Version number for aranges is still 2, even in DWARF3.  */
8817   dw2_asm_output_data (2, 2, "DWARF Version");
8818   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8819                          debug_info_section,
8820                          "Offset of Compilation Unit Info");
8821   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8822   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8823
8824   /* We need to align to twice the pointer size here.  */
8825   if (DWARF_ARANGES_PAD_SIZE)
8826     {
8827       /* Pad using a 2 byte words so that padding is correct for any
8828          pointer size.  */
8829       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8830                            2 * DWARF2_ADDR_SIZE);
8831       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8832         dw2_asm_output_data (2, 0, NULL);
8833     }
8834
8835   /* It is necessary not to output these entries if the sections were
8836      not used; if the sections were not used, the length will be 0 and
8837      the address may end up as 0 if the section is discarded by ld
8838      --gc-sections, leaving an invalid (0, 0) entry that can be
8839      confused with the terminator.  */
8840   if (text_section_used)
8841     {
8842       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8843       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8844                             text_section_label, "Length");
8845     }
8846   if (cold_text_section_used)
8847     {
8848       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8849                            "Address");
8850       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8851                             cold_text_section_label, "Length");
8852     }
8853
8854   if (have_multiple_function_sections)
8855     {
8856       unsigned fde_idx;
8857       dw_fde_ref fde;
8858
8859       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8860         {
8861           if (!fde->in_std_section)
8862             {
8863               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8864                                    "Address");
8865               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8866                                     fde->dw_fde_begin, "Length");
8867             }
8868           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8869             {
8870               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8871                                    "Address");
8872               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8873                                     fde->dw_fde_second_begin, "Length");
8874             }
8875         }
8876     }
8877
8878   /* Output the terminator words.  */
8879   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8880   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8881 }
8882
8883 /* Add a new entry to .debug_ranges.  Return the offset at which it
8884    was placed.  */
8885
8886 static unsigned int
8887 add_ranges_num (int num)
8888 {
8889   unsigned int in_use = ranges_table_in_use;
8890
8891   if (in_use == ranges_table_allocated)
8892     {
8893       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8894       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8895                                     ranges_table_allocated);
8896       memset (ranges_table + ranges_table_in_use, 0,
8897               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8898     }
8899
8900   ranges_table[in_use].num = num;
8901   ranges_table_in_use = in_use + 1;
8902
8903   return in_use * 2 * DWARF2_ADDR_SIZE;
8904 }
8905
8906 /* Add a new entry to .debug_ranges corresponding to a block, or a
8907    range terminator if BLOCK is NULL.  */
8908
8909 static unsigned int
8910 add_ranges (const_tree block)
8911 {
8912   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8913 }
8914
8915 /* Add a new entry to .debug_ranges corresponding to a pair of
8916    labels.  */
8917
8918 static void
8919 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8920                       bool *added)
8921 {
8922   unsigned int in_use = ranges_by_label_in_use;
8923   unsigned int offset;
8924
8925   if (in_use == ranges_by_label_allocated)
8926     {
8927       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8928       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8929                                        ranges_by_label,
8930                                        ranges_by_label_allocated);
8931       memset (ranges_by_label + ranges_by_label_in_use, 0,
8932               RANGES_TABLE_INCREMENT
8933               * sizeof (struct dw_ranges_by_label_struct));
8934     }
8935
8936   ranges_by_label[in_use].begin = begin;
8937   ranges_by_label[in_use].end = end;
8938   ranges_by_label_in_use = in_use + 1;
8939
8940   offset = add_ranges_num (-(int)in_use - 1);
8941   if (!*added)
8942     {
8943       add_AT_range_list (die, DW_AT_ranges, offset);
8944       *added = true;
8945     }
8946 }
8947
8948 static void
8949 output_ranges (void)
8950 {
8951   unsigned i;
8952   static const char *const start_fmt = "Offset %#x";
8953   const char *fmt = start_fmt;
8954
8955   for (i = 0; i < ranges_table_in_use; i++)
8956     {
8957       int block_num = ranges_table[i].num;
8958
8959       if (block_num > 0)
8960         {
8961           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8962           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8963
8964           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8965           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8966
8967           /* If all code is in the text section, then the compilation
8968              unit base address defaults to DW_AT_low_pc, which is the
8969              base of the text section.  */
8970           if (!have_multiple_function_sections)
8971             {
8972               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8973                                     text_section_label,
8974                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8975               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8976                                     text_section_label, NULL);
8977             }
8978
8979           /* Otherwise, the compilation unit base address is zero,
8980              which allows us to use absolute addresses, and not worry
8981              about whether the target supports cross-section
8982              arithmetic.  */
8983           else
8984             {
8985               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8986                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8987               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8988             }
8989
8990           fmt = NULL;
8991         }
8992
8993       /* Negative block_num stands for an index into ranges_by_label.  */
8994       else if (block_num < 0)
8995         {
8996           int lab_idx = - block_num - 1;
8997
8998           if (!have_multiple_function_sections)
8999             {
9000               gcc_unreachable ();
9001 #if 0
9002               /* If we ever use add_ranges_by_labels () for a single
9003                  function section, all we have to do is to take out
9004                  the #if 0 above.  */
9005               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9006                                     ranges_by_label[lab_idx].begin,
9007                                     text_section_label,
9008                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9009               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9010                                     ranges_by_label[lab_idx].end,
9011                                     text_section_label, NULL);
9012 #endif
9013             }
9014           else
9015             {
9016               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9017                                    ranges_by_label[lab_idx].begin,
9018                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9019               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9020                                    ranges_by_label[lab_idx].end,
9021                                    NULL);
9022             }
9023         }
9024       else
9025         {
9026           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9027           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9028           fmt = start_fmt;
9029         }
9030     }
9031 }
9032
9033 /* Data structure containing information about input files.  */
9034 struct file_info
9035 {
9036   const char *path;     /* Complete file name.  */
9037   const char *fname;    /* File name part.  */
9038   int length;           /* Length of entire string.  */
9039   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9040   int dir_idx;          /* Index in directory table.  */
9041 };
9042
9043 /* Data structure containing information about directories with source
9044    files.  */
9045 struct dir_info
9046 {
9047   const char *path;     /* Path including directory name.  */
9048   int length;           /* Path length.  */
9049   int prefix;           /* Index of directory entry which is a prefix.  */
9050   int count;            /* Number of files in this directory.  */
9051   int dir_idx;          /* Index of directory used as base.  */
9052 };
9053
9054 /* Callback function for file_info comparison.  We sort by looking at
9055    the directories in the path.  */
9056
9057 static int
9058 file_info_cmp (const void *p1, const void *p2)
9059 {
9060   const struct file_info *const s1 = (const struct file_info *) p1;
9061   const struct file_info *const s2 = (const struct file_info *) p2;
9062   const unsigned char *cp1;
9063   const unsigned char *cp2;
9064
9065   /* Take care of file names without directories.  We need to make sure that
9066      we return consistent values to qsort since some will get confused if
9067      we return the same value when identical operands are passed in opposite
9068      orders.  So if neither has a directory, return 0 and otherwise return
9069      1 or -1 depending on which one has the directory.  */
9070   if ((s1->path == s1->fname || s2->path == s2->fname))
9071     return (s2->path == s2->fname) - (s1->path == s1->fname);
9072
9073   cp1 = (const unsigned char *) s1->path;
9074   cp2 = (const unsigned char *) s2->path;
9075
9076   while (1)
9077     {
9078       ++cp1;
9079       ++cp2;
9080       /* Reached the end of the first path?  If so, handle like above.  */
9081       if ((cp1 == (const unsigned char *) s1->fname)
9082           || (cp2 == (const unsigned char *) s2->fname))
9083         return ((cp2 == (const unsigned char *) s2->fname)
9084                 - (cp1 == (const unsigned char *) s1->fname));
9085
9086       /* Character of current path component the same?  */
9087       else if (*cp1 != *cp2)
9088         return *cp1 - *cp2;
9089     }
9090 }
9091
9092 struct file_name_acquire_data
9093 {
9094   struct file_info *files;
9095   int used_files;
9096   int max_files;
9097 };
9098
9099 /* Traversal function for the hash table.  */
9100
9101 static int
9102 file_name_acquire (void ** slot, void *data)
9103 {
9104   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9105   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9106   struct file_info *fi;
9107   const char *f;
9108
9109   gcc_assert (fnad->max_files >= d->emitted_number);
9110
9111   if (! d->emitted_number)
9112     return 1;
9113
9114   gcc_assert (fnad->max_files != fnad->used_files);
9115
9116   fi = fnad->files + fnad->used_files++;
9117
9118   /* Skip all leading "./".  */
9119   f = d->filename;
9120   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9121     f += 2;
9122
9123   /* Create a new array entry.  */
9124   fi->path = f;
9125   fi->length = strlen (f);
9126   fi->file_idx = d;
9127
9128   /* Search for the file name part.  */
9129   f = strrchr (f, DIR_SEPARATOR);
9130 #if defined (DIR_SEPARATOR_2)
9131   {
9132     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9133
9134     if (g != NULL)
9135       {
9136         if (f == NULL || f < g)
9137           f = g;
9138       }
9139   }
9140 #endif
9141
9142   fi->fname = f == NULL ? fi->path : f + 1;
9143   return 1;
9144 }
9145
9146 /* Output the directory table and the file name table.  We try to minimize
9147    the total amount of memory needed.  A heuristic is used to avoid large
9148    slowdowns with many input files.  */
9149
9150 static void
9151 output_file_names (void)
9152 {
9153   struct file_name_acquire_data fnad;
9154   int numfiles;
9155   struct file_info *files;
9156   struct dir_info *dirs;
9157   int *saved;
9158   int *savehere;
9159   int *backmap;
9160   int ndirs;
9161   int idx_offset;
9162   int i;
9163
9164   if (!last_emitted_file)
9165     {
9166       dw2_asm_output_data (1, 0, "End directory table");
9167       dw2_asm_output_data (1, 0, "End file name table");
9168       return;
9169     }
9170
9171   numfiles = last_emitted_file->emitted_number;
9172
9173   /* Allocate the various arrays we need.  */
9174   files = XALLOCAVEC (struct file_info, numfiles);
9175   dirs = XALLOCAVEC (struct dir_info, numfiles);
9176
9177   fnad.files = files;
9178   fnad.used_files = 0;
9179   fnad.max_files = numfiles;
9180   htab_traverse (file_table, file_name_acquire, &fnad);
9181   gcc_assert (fnad.used_files == fnad.max_files);
9182
9183   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9184
9185   /* Find all the different directories used.  */
9186   dirs[0].path = files[0].path;
9187   dirs[0].length = files[0].fname - files[0].path;
9188   dirs[0].prefix = -1;
9189   dirs[0].count = 1;
9190   dirs[0].dir_idx = 0;
9191   files[0].dir_idx = 0;
9192   ndirs = 1;
9193
9194   for (i = 1; i < numfiles; i++)
9195     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9196         && memcmp (dirs[ndirs - 1].path, files[i].path,
9197                    dirs[ndirs - 1].length) == 0)
9198       {
9199         /* Same directory as last entry.  */
9200         files[i].dir_idx = ndirs - 1;
9201         ++dirs[ndirs - 1].count;
9202       }
9203     else
9204       {
9205         int j;
9206
9207         /* This is a new directory.  */
9208         dirs[ndirs].path = files[i].path;
9209         dirs[ndirs].length = files[i].fname - files[i].path;
9210         dirs[ndirs].count = 1;
9211         dirs[ndirs].dir_idx = ndirs;
9212         files[i].dir_idx = ndirs;
9213
9214         /* Search for a prefix.  */
9215         dirs[ndirs].prefix = -1;
9216         for (j = 0; j < ndirs; j++)
9217           if (dirs[j].length < dirs[ndirs].length
9218               && dirs[j].length > 1
9219               && (dirs[ndirs].prefix == -1
9220                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9221               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9222             dirs[ndirs].prefix = j;
9223
9224         ++ndirs;
9225       }
9226
9227   /* Now to the actual work.  We have to find a subset of the directories which
9228      allow expressing the file name using references to the directory table
9229      with the least amount of characters.  We do not do an exhaustive search
9230      where we would have to check out every combination of every single
9231      possible prefix.  Instead we use a heuristic which provides nearly optimal
9232      results in most cases and never is much off.  */
9233   saved = XALLOCAVEC (int, ndirs);
9234   savehere = XALLOCAVEC (int, ndirs);
9235
9236   memset (saved, '\0', ndirs * sizeof (saved[0]));
9237   for (i = 0; i < ndirs; i++)
9238     {
9239       int j;
9240       int total;
9241
9242       /* We can always save some space for the current directory.  But this
9243          does not mean it will be enough to justify adding the directory.  */
9244       savehere[i] = dirs[i].length;
9245       total = (savehere[i] - saved[i]) * dirs[i].count;
9246
9247       for (j = i + 1; j < ndirs; j++)
9248         {
9249           savehere[j] = 0;
9250           if (saved[j] < dirs[i].length)
9251             {
9252               /* Determine whether the dirs[i] path is a prefix of the
9253                  dirs[j] path.  */
9254               int k;
9255
9256               k = dirs[j].prefix;
9257               while (k != -1 && k != (int) i)
9258                 k = dirs[k].prefix;
9259
9260               if (k == (int) i)
9261                 {
9262                   /* Yes it is.  We can possibly save some memory by
9263                      writing the filenames in dirs[j] relative to
9264                      dirs[i].  */
9265                   savehere[j] = dirs[i].length;
9266                   total += (savehere[j] - saved[j]) * dirs[j].count;
9267                 }
9268             }
9269         }
9270
9271       /* Check whether we can save enough to justify adding the dirs[i]
9272          directory.  */
9273       if (total > dirs[i].length + 1)
9274         {
9275           /* It's worthwhile adding.  */
9276           for (j = i; j < ndirs; j++)
9277             if (savehere[j] > 0)
9278               {
9279                 /* Remember how much we saved for this directory so far.  */
9280                 saved[j] = savehere[j];
9281
9282                 /* Remember the prefix directory.  */
9283                 dirs[j].dir_idx = i;
9284               }
9285         }
9286     }
9287
9288   /* Emit the directory name table.  */
9289   idx_offset = dirs[0].length > 0 ? 1 : 0;
9290   for (i = 1 - idx_offset; i < ndirs; i++)
9291     dw2_asm_output_nstring (dirs[i].path,
9292                             dirs[i].length
9293                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9294                             "Directory Entry: %#x", i + idx_offset);
9295
9296   dw2_asm_output_data (1, 0, "End directory table");
9297
9298   /* We have to emit them in the order of emitted_number since that's
9299      used in the debug info generation.  To do this efficiently we
9300      generate a back-mapping of the indices first.  */
9301   backmap = XALLOCAVEC (int, numfiles);
9302   for (i = 0; i < numfiles; i++)
9303     backmap[files[i].file_idx->emitted_number - 1] = i;
9304
9305   /* Now write all the file names.  */
9306   for (i = 0; i < numfiles; i++)
9307     {
9308       int file_idx = backmap[i];
9309       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9310
9311 #ifdef VMS_DEBUGGING_INFO
9312 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9313
9314       /* Setting these fields can lead to debugger miscomparisons,
9315          but VMS Debug requires them to be set correctly.  */
9316
9317       int ver;
9318       long long cdt;
9319       long siz;
9320       int maxfilelen = strlen (files[file_idx].path)
9321                                + dirs[dir_idx].length
9322                                + MAX_VMS_VERSION_LEN + 1;
9323       char *filebuf = XALLOCAVEC (char, maxfilelen);
9324
9325       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9326       snprintf (filebuf, maxfilelen, "%s;%d",
9327                 files[file_idx].path + dirs[dir_idx].length, ver);
9328
9329       dw2_asm_output_nstring
9330         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9331
9332       /* Include directory index.  */
9333       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9334
9335       /* Modification time.  */
9336       dw2_asm_output_data_uleb128
9337         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9338           ? cdt : 0,
9339          NULL);
9340
9341       /* File length in bytes.  */
9342       dw2_asm_output_data_uleb128
9343         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9344           ? siz : 0,
9345          NULL);
9346 #else
9347       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9348                               "File Entry: %#x", (unsigned) i + 1);
9349
9350       /* Include directory index.  */
9351       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9352
9353       /* Modification time.  */
9354       dw2_asm_output_data_uleb128 (0, NULL);
9355
9356       /* File length in bytes.  */
9357       dw2_asm_output_data_uleb128 (0, NULL);
9358 #endif /* VMS_DEBUGGING_INFO */
9359     }
9360
9361   dw2_asm_output_data (1, 0, "End file name table");
9362 }
9363
9364
9365 /* Output one line number table into the .debug_line section.  */
9366
9367 static void
9368 output_one_line_info_table (dw_line_info_table *table)
9369 {
9370   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9371   unsigned int current_line = 1;
9372   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9373   dw_line_info_entry *ent;
9374   size_t i;
9375
9376   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9377     {
9378       switch (ent->opcode)
9379         {
9380         case LI_set_address:
9381           /* ??? Unfortunately, we have little choice here currently, and
9382              must always use the most general form.  GCC does not know the
9383              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9384              ports do have length attributes which will give an upper bound
9385              on the address range.  We could perhaps use length attributes
9386              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9387           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9388
9389           /* This can handle any delta.  This takes
9390              4+DWARF2_ADDR_SIZE bytes.  */
9391           dw2_asm_output_data (1, 0, "set address %s", line_label);
9392           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9393           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9394           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9395           break;
9396
9397         case LI_set_line:
9398           if (ent->val == current_line)
9399             {
9400               /* We still need to start a new row, so output a copy insn.  */
9401               dw2_asm_output_data (1, DW_LNS_copy,
9402                                    "copy line %u", current_line);
9403             }
9404           else
9405             {
9406               int line_offset = ent->val - current_line;
9407               int line_delta = line_offset - DWARF_LINE_BASE;
9408
9409               current_line = ent->val;
9410               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9411                 {
9412                   /* This can handle deltas from -10 to 234, using the current
9413                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9414                      This takes 1 byte.  */
9415                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9416                                        "line %u", current_line);
9417                 }
9418               else
9419                 {
9420                   /* This can handle any delta.  This takes at least 4 bytes,
9421                      depending on the value being encoded.  */
9422                   dw2_asm_output_data (1, DW_LNS_advance_line,
9423                                        "advance to line %u", current_line);
9424                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9425                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9426                 }
9427             }
9428           break;
9429
9430         case LI_set_file:
9431           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9432           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9433           break;
9434
9435         case LI_set_column:
9436           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9437           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9438           break;
9439
9440         case LI_negate_stmt:
9441           current_is_stmt = !current_is_stmt;
9442           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9443                                "is_stmt %d", current_is_stmt);
9444           break;
9445
9446         case LI_set_prologue_end:
9447           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9448                                "set prologue end");
9449           break;
9450           
9451         case LI_set_epilogue_begin:
9452           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9453                                "set epilogue begin");
9454           break;
9455
9456         case LI_set_discriminator:
9457           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9458           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9459           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9460           dw2_asm_output_data_uleb128 (ent->val, NULL);
9461           break;
9462         }
9463     }
9464
9465   /* Emit debug info for the address of the end of the table.  */
9466   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9467   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9468   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9469   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9470
9471   dw2_asm_output_data (1, 0, "end sequence");
9472   dw2_asm_output_data_uleb128 (1, NULL);
9473   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9474 }
9475
9476 /* Output the source line number correspondence information.  This
9477    information goes into the .debug_line section.  */
9478
9479 static void
9480 output_line_info (void)
9481 {
9482   char l1[20], l2[20], p1[20], p2[20];
9483   int ver = dwarf_version;
9484   bool saw_one = false;
9485   int opc;
9486
9487   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9488   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9489   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9490   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9491
9492   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9493     dw2_asm_output_data (4, 0xffffffff,
9494       "Initial length escape value indicating 64-bit DWARF extension");
9495   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9496                         "Length of Source Line Info");
9497   ASM_OUTPUT_LABEL (asm_out_file, l1);
9498
9499   dw2_asm_output_data (2, ver, "DWARF Version");
9500   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9501   ASM_OUTPUT_LABEL (asm_out_file, p1);
9502
9503   /* Define the architecture-dependent minimum instruction length (in bytes).
9504      In this implementation of DWARF, this field is used for information
9505      purposes only.  Since GCC generates assembly language, we have no
9506      a priori knowledge of how many instruction bytes are generated for each
9507      source line, and therefore can use only the DW_LNE_set_address and
9508      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9509      this as '1', which is "correct enough" for all architectures,
9510      and don't let the target override.  */
9511   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9512
9513   if (ver >= 4)
9514     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9515                          "Maximum Operations Per Instruction");
9516   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9517                        "Default is_stmt_start flag");
9518   dw2_asm_output_data (1, DWARF_LINE_BASE,
9519                        "Line Base Value (Special Opcodes)");
9520   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9521                        "Line Range Value (Special Opcodes)");
9522   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9523                        "Special Opcode Base");
9524
9525   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9526     {
9527       int n_op_args;
9528       switch (opc)
9529         {
9530         case DW_LNS_advance_pc:
9531         case DW_LNS_advance_line:
9532         case DW_LNS_set_file:
9533         case DW_LNS_set_column:
9534         case DW_LNS_fixed_advance_pc:
9535         case DW_LNS_set_isa:
9536           n_op_args = 1;
9537           break;
9538         default:
9539           n_op_args = 0;
9540           break;
9541         }
9542
9543       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9544                            opc, n_op_args);
9545     }
9546
9547   /* Write out the information about the files we use.  */
9548   output_file_names ();
9549   ASM_OUTPUT_LABEL (asm_out_file, p2);
9550
9551   if (separate_line_info)
9552     {
9553       dw_line_info_table *table;
9554       size_t i;
9555
9556       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9557         if (table->in_use)
9558           {
9559             output_one_line_info_table (table);
9560             saw_one = true;
9561           }
9562     }
9563   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9564     {
9565       output_one_line_info_table (cold_text_section_line_info);
9566       saw_one = true;
9567     }
9568
9569   /* ??? Some Darwin linkers crash on a .debug_line section with no
9570      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9571      sufficient -- the address column must also be initialized.
9572      Make sure to output at least one set_address/end_sequence pair,
9573      choosing .text since that section is always present.  */
9574   if (text_section_line_info->in_use || !saw_one)
9575     output_one_line_info_table (text_section_line_info);
9576
9577   /* Output the marker for the end of the line number info.  */
9578   ASM_OUTPUT_LABEL (asm_out_file, l2);
9579 }
9580 \f
9581 /* Given a pointer to a tree node for some base type, return a pointer to
9582    a DIE that describes the given type.
9583
9584    This routine must only be called for GCC type nodes that correspond to
9585    Dwarf base (fundamental) types.  */
9586
9587 static dw_die_ref
9588 base_type_die (tree type)
9589 {
9590   dw_die_ref base_type_result;
9591   enum dwarf_type encoding;
9592
9593   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9594     return 0;
9595
9596   /* If this is a subtype that should not be emitted as a subrange type,
9597      use the base type.  See subrange_type_for_debug_p.  */
9598   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9599     type = TREE_TYPE (type);
9600
9601   switch (TREE_CODE (type))
9602     {
9603     case INTEGER_TYPE:
9604       if ((dwarf_version >= 4 || !dwarf_strict)
9605           && TYPE_NAME (type)
9606           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9607           && DECL_IS_BUILTIN (TYPE_NAME (type))
9608           && DECL_NAME (TYPE_NAME (type)))
9609         {
9610           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9611           if (strcmp (name, "char16_t") == 0
9612               || strcmp (name, "char32_t") == 0)
9613             {
9614               encoding = DW_ATE_UTF;
9615               break;
9616             }
9617         }
9618       if (TYPE_STRING_FLAG (type))
9619         {
9620           if (TYPE_UNSIGNED (type))
9621             encoding = DW_ATE_unsigned_char;
9622           else
9623             encoding = DW_ATE_signed_char;
9624         }
9625       else if (TYPE_UNSIGNED (type))
9626         encoding = DW_ATE_unsigned;
9627       else
9628         encoding = DW_ATE_signed;
9629       break;
9630
9631     case REAL_TYPE:
9632       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9633         {
9634           if (dwarf_version >= 3 || !dwarf_strict)
9635             encoding = DW_ATE_decimal_float;
9636           else
9637             encoding = DW_ATE_lo_user;
9638         }
9639       else
9640         encoding = DW_ATE_float;
9641       break;
9642
9643     case FIXED_POINT_TYPE:
9644       if (!(dwarf_version >= 3 || !dwarf_strict))
9645         encoding = DW_ATE_lo_user;
9646       else if (TYPE_UNSIGNED (type))
9647         encoding = DW_ATE_unsigned_fixed;
9648       else
9649         encoding = DW_ATE_signed_fixed;
9650       break;
9651
9652       /* Dwarf2 doesn't know anything about complex ints, so use
9653          a user defined type for it.  */
9654     case COMPLEX_TYPE:
9655       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9656         encoding = DW_ATE_complex_float;
9657       else
9658         encoding = DW_ATE_lo_user;
9659       break;
9660
9661     case BOOLEAN_TYPE:
9662       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9663       encoding = DW_ATE_boolean;
9664       break;
9665
9666     default:
9667       /* No other TREE_CODEs are Dwarf fundamental types.  */
9668       gcc_unreachable ();
9669     }
9670
9671   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9672
9673   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9674                    int_size_in_bytes (type));
9675   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9676
9677   return base_type_result;
9678 }
9679
9680 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9681    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9682
9683 static inline int
9684 is_base_type (tree type)
9685 {
9686   switch (TREE_CODE (type))
9687     {
9688     case ERROR_MARK:
9689     case VOID_TYPE:
9690     case INTEGER_TYPE:
9691     case REAL_TYPE:
9692     case FIXED_POINT_TYPE:
9693     case COMPLEX_TYPE:
9694     case BOOLEAN_TYPE:
9695       return 1;
9696
9697     case ARRAY_TYPE:
9698     case RECORD_TYPE:
9699     case UNION_TYPE:
9700     case QUAL_UNION_TYPE:
9701     case ENUMERAL_TYPE:
9702     case FUNCTION_TYPE:
9703     case METHOD_TYPE:
9704     case POINTER_TYPE:
9705     case REFERENCE_TYPE:
9706     case NULLPTR_TYPE:
9707     case OFFSET_TYPE:
9708     case LANG_TYPE:
9709     case VECTOR_TYPE:
9710       return 0;
9711
9712     default:
9713       gcc_unreachable ();
9714     }
9715
9716   return 0;
9717 }
9718
9719 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9720    node, return the size in bits for the type if it is a constant, or else
9721    return the alignment for the type if the type's size is not constant, or
9722    else return BITS_PER_WORD if the type actually turns out to be an
9723    ERROR_MARK node.  */
9724
9725 static inline unsigned HOST_WIDE_INT
9726 simple_type_size_in_bits (const_tree type)
9727 {
9728   if (TREE_CODE (type) == ERROR_MARK)
9729     return BITS_PER_WORD;
9730   else if (TYPE_SIZE (type) == NULL_TREE)
9731     return 0;
9732   else if (host_integerp (TYPE_SIZE (type), 1))
9733     return tree_low_cst (TYPE_SIZE (type), 1);
9734   else
9735     return TYPE_ALIGN (type);
9736 }
9737
9738 /* Similarly, but return a double_int instead of UHWI.  */
9739
9740 static inline double_int
9741 double_int_type_size_in_bits (const_tree type)
9742 {
9743   if (TREE_CODE (type) == ERROR_MARK)
9744     return uhwi_to_double_int (BITS_PER_WORD);
9745   else if (TYPE_SIZE (type) == NULL_TREE)
9746     return double_int_zero;
9747   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9748     return tree_to_double_int (TYPE_SIZE (type));
9749   else
9750     return uhwi_to_double_int (TYPE_ALIGN (type));
9751 }
9752
9753 /*  Given a pointer to a tree node for a subrange type, return a pointer
9754     to a DIE that describes the given type.  */
9755
9756 static dw_die_ref
9757 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9758 {
9759   dw_die_ref subrange_die;
9760   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9761
9762   if (context_die == NULL)
9763     context_die = comp_unit_die ();
9764
9765   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9766
9767   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9768     {
9769       /* The size of the subrange type and its base type do not match,
9770          so we need to generate a size attribute for the subrange type.  */
9771       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9772     }
9773
9774   if (low)
9775     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9776   if (high)
9777     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9778
9779   return subrange_die;
9780 }
9781
9782 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9783    entry that chains various modifiers in front of the given type.  */
9784
9785 static dw_die_ref
9786 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9787                    dw_die_ref context_die)
9788 {
9789   enum tree_code code = TREE_CODE (type);
9790   dw_die_ref mod_type_die;
9791   dw_die_ref sub_die = NULL;
9792   tree item_type = NULL;
9793   tree qualified_type;
9794   tree name, low, high;
9795   dw_die_ref mod_scope;
9796
9797   if (code == ERROR_MARK)
9798     return NULL;
9799
9800   /* See if we already have the appropriately qualified variant of
9801      this type.  */
9802   qualified_type
9803     = get_qualified_type (type,
9804                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9805                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9806
9807   if (qualified_type == sizetype
9808       && TYPE_NAME (qualified_type)
9809       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9810     {
9811       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9812
9813       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9814                            && TYPE_PRECISION (t)
9815                            == TYPE_PRECISION (qualified_type)
9816                            && TYPE_UNSIGNED (t)
9817                            == TYPE_UNSIGNED (qualified_type));
9818       qualified_type = t;
9819     }
9820
9821   /* If we do, then we can just use its DIE, if it exists.  */
9822   if (qualified_type)
9823     {
9824       mod_type_die = lookup_type_die (qualified_type);
9825       if (mod_type_die)
9826         return mod_type_die;
9827     }
9828
9829   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9830
9831   /* Handle C typedef types.  */
9832   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9833       && !DECL_ARTIFICIAL (name))
9834     {
9835       tree dtype = TREE_TYPE (name);
9836
9837       if (qualified_type == dtype)
9838         {
9839           /* For a named type, use the typedef.  */
9840           gen_type_die (qualified_type, context_die);
9841           return lookup_type_die (qualified_type);
9842         }
9843       else if (is_const_type < TYPE_READONLY (dtype)
9844                || is_volatile_type < TYPE_VOLATILE (dtype)
9845                || (is_const_type <= TYPE_READONLY (dtype)
9846                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9847                    && DECL_ORIGINAL_TYPE (name) != type))
9848         /* cv-unqualified version of named type.  Just use the unnamed
9849            type to which it refers.  */
9850         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9851                                   is_const_type, is_volatile_type,
9852                                   context_die);
9853       /* Else cv-qualified version of named type; fall through.  */
9854     }
9855
9856   mod_scope = scope_die_for (type, context_die);
9857
9858   if (is_const_type
9859       /* If both is_const_type and is_volatile_type, prefer the path
9860          which leads to a qualified type.  */
9861       && (!is_volatile_type
9862           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9863           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9864     {
9865       mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9866       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9867     }
9868   else if (is_volatile_type)
9869     {
9870       mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9871       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9872     }
9873   else if (code == POINTER_TYPE)
9874     {
9875       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9876       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9877                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9878       item_type = TREE_TYPE (type);
9879       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9880         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9881                          TYPE_ADDR_SPACE (item_type));
9882     }
9883   else if (code == REFERENCE_TYPE)
9884     {
9885       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9886         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9887                                 type);
9888       else
9889         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9890       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9891                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9892       item_type = TREE_TYPE (type);
9893       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9894         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9895                          TYPE_ADDR_SPACE (item_type));
9896     }
9897   else if (code == INTEGER_TYPE
9898            && TREE_TYPE (type) != NULL_TREE
9899            && subrange_type_for_debug_p (type, &low, &high))
9900     {
9901       mod_type_die = subrange_type_die (type, low, high, context_die);
9902       item_type = TREE_TYPE (type);
9903     }
9904   else if (is_base_type (type))
9905     mod_type_die = base_type_die (type);
9906   else
9907     {
9908       gen_type_die (type, context_die);
9909
9910       /* We have to get the type_main_variant here (and pass that to the
9911          `lookup_type_die' routine) because the ..._TYPE node we have
9912          might simply be a *copy* of some original type node (where the
9913          copy was created to help us keep track of typedef names) and
9914          that copy might have a different TYPE_UID from the original
9915          ..._TYPE node.  */
9916       if (TREE_CODE (type) != VECTOR_TYPE)
9917         return lookup_type_die (type_main_variant (type));
9918       else
9919         /* Vectors have the debugging information in the type,
9920            not the main variant.  */
9921         return lookup_type_die (type);
9922     }
9923
9924   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9925      don't output a DW_TAG_typedef, since there isn't one in the
9926      user's program; just attach a DW_AT_name to the type.
9927      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9928      if the base type already has the same name.  */
9929   if (name
9930       && ((TREE_CODE (name) != TYPE_DECL
9931            && (qualified_type == TYPE_MAIN_VARIANT (type)
9932                || (!is_const_type && !is_volatile_type)))
9933           || (TREE_CODE (name) == TYPE_DECL
9934               && TREE_TYPE (name) == qualified_type
9935               && DECL_NAME (name))))
9936     {
9937       if (TREE_CODE (name) == TYPE_DECL)
9938         /* Could just call add_name_and_src_coords_attributes here,
9939            but since this is a builtin type it doesn't have any
9940            useful source coordinates anyway.  */
9941         name = DECL_NAME (name);
9942       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9943     }
9944   /* This probably indicates a bug.  */
9945   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9946     {
9947       name = TYPE_NAME (type);
9948       if (name
9949           && TREE_CODE (name) == TYPE_DECL)
9950         name = DECL_NAME (name);
9951       add_name_attribute (mod_type_die,
9952                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9953     }
9954
9955   if (qualified_type)
9956     equate_type_number_to_die (qualified_type, mod_type_die);
9957
9958   if (item_type)
9959     /* We must do this after the equate_type_number_to_die call, in case
9960        this is a recursive type.  This ensures that the modified_type_die
9961        recursion will terminate even if the type is recursive.  Recursive
9962        types are possible in Ada.  */
9963     sub_die = modified_type_die (item_type,
9964                                  TYPE_READONLY (item_type),
9965                                  TYPE_VOLATILE (item_type),
9966                                  context_die);
9967
9968   if (sub_die != NULL)
9969     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9970
9971   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9972   if (TYPE_ARTIFICIAL (type))
9973     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9974
9975   return mod_type_die;
9976 }
9977
9978 /* Generate DIEs for the generic parameters of T.
9979    T must be either a generic type or a generic function.
9980    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9981
9982 static void
9983 gen_generic_params_dies (tree t)
9984 {
9985   tree parms, args;
9986   int parms_num, i;
9987   dw_die_ref die = NULL;
9988
9989   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9990     return;
9991
9992   if (TYPE_P (t))
9993     die = lookup_type_die (t);
9994   else if (DECL_P (t))
9995     die = lookup_decl_die (t);
9996
9997   gcc_assert (die);
9998
9999   parms = lang_hooks.get_innermost_generic_parms (t);
10000   if (!parms)
10001     /* T has no generic parameter. It means T is neither a generic type
10002        or function. End of story.  */
10003     return;
10004
10005   parms_num = TREE_VEC_LENGTH (parms);
10006   args = lang_hooks.get_innermost_generic_args (t);
10007   for (i = 0; i < parms_num; i++)
10008     {
10009       tree parm, arg, arg_pack_elems;
10010
10011       parm = TREE_VEC_ELT (parms, i);
10012       arg = TREE_VEC_ELT (args, i);
10013       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10014       gcc_assert (parm && TREE_VALUE (parm) && arg);
10015
10016       if (parm && TREE_VALUE (parm) && arg)
10017         {
10018           /* If PARM represents a template parameter pack,
10019              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10020              by DW_TAG_template_*_parameter DIEs for the argument
10021              pack elements of ARG. Note that ARG would then be
10022              an argument pack.  */
10023           if (arg_pack_elems)
10024             template_parameter_pack_die (TREE_VALUE (parm),
10025                                          arg_pack_elems,
10026                                          die);
10027           else
10028             generic_parameter_die (TREE_VALUE (parm), arg,
10029                                    true /* Emit DW_AT_name */, die);
10030         }
10031     }
10032 }
10033
10034 /* Create and return a DIE for PARM which should be
10035    the representation of a generic type parameter.
10036    For instance, in the C++ front end, PARM would be a template parameter.
10037    ARG is the argument to PARM.
10038    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10039    name of the PARM.
10040    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10041    as a child node.  */
10042
10043 static dw_die_ref
10044 generic_parameter_die (tree parm, tree arg,
10045                        bool emit_name_p,
10046                        dw_die_ref parent_die)
10047 {
10048   dw_die_ref tmpl_die = NULL;
10049   const char *name = NULL;
10050
10051   if (!parm || !DECL_NAME (parm) || !arg)
10052     return NULL;
10053
10054   /* We support non-type generic parameters and arguments,
10055      type generic parameters and arguments, as well as
10056      generic generic parameters (a.k.a. template template parameters in C++)
10057      and arguments.  */
10058   if (TREE_CODE (parm) == PARM_DECL)
10059     /* PARM is a nontype generic parameter  */
10060     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10061   else if (TREE_CODE (parm) == TYPE_DECL)
10062     /* PARM is a type generic parameter.  */
10063     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10064   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10065     /* PARM is a generic generic parameter.
10066        Its DIE is a GNU extension. It shall have a
10067        DW_AT_name attribute to represent the name of the template template
10068        parameter, and a DW_AT_GNU_template_name attribute to represent the
10069        name of the template template argument.  */
10070     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10071                         parent_die, parm);
10072   else
10073     gcc_unreachable ();
10074
10075   if (tmpl_die)
10076     {
10077       tree tmpl_type;
10078
10079       /* If PARM is a generic parameter pack, it means we are
10080          emitting debug info for a template argument pack element.
10081          In other terms, ARG is a template argument pack element.
10082          In that case, we don't emit any DW_AT_name attribute for
10083          the die.  */
10084       if (emit_name_p)
10085         {
10086           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10087           gcc_assert (name);
10088           add_AT_string (tmpl_die, DW_AT_name, name);
10089         }
10090
10091       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10092         {
10093           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10094              TMPL_DIE should have a child DW_AT_type attribute that is set
10095              to the type of the argument to PARM, which is ARG.
10096              If PARM is a type generic parameter, TMPL_DIE should have a
10097              child DW_AT_type that is set to ARG.  */
10098           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10099           add_type_attribute (tmpl_die, tmpl_type, 0,
10100                               TREE_THIS_VOLATILE (tmpl_type),
10101                               parent_die);
10102         }
10103       else
10104         {
10105           /* So TMPL_DIE is a DIE representing a
10106              a generic generic template parameter, a.k.a template template
10107              parameter in C++ and arg is a template.  */
10108
10109           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10110              to the name of the argument.  */
10111           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10112           if (name)
10113             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10114         }
10115
10116       if (TREE_CODE (parm) == PARM_DECL)
10117         /* So PARM is a non-type generic parameter.
10118            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10119            attribute of TMPL_DIE which value represents the value
10120            of ARG.
10121            We must be careful here:
10122            The value of ARG might reference some function decls.
10123            We might currently be emitting debug info for a generic
10124            type and types are emitted before function decls, we don't
10125            know if the function decls referenced by ARG will actually be
10126            emitted after cgraph computations.
10127            So must defer the generation of the DW_AT_const_value to
10128            after cgraph is ready.  */
10129         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10130     }
10131
10132   return tmpl_die;
10133 }
10134
10135 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10136    PARM_PACK must be a template parameter pack. The returned DIE
10137    will be child DIE of PARENT_DIE.  */
10138
10139 static dw_die_ref
10140 template_parameter_pack_die (tree parm_pack,
10141                              tree parm_pack_args,
10142                              dw_die_ref parent_die)
10143 {
10144   dw_die_ref die;
10145   int j;
10146
10147   gcc_assert (parent_die && parm_pack);
10148
10149   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10150   add_name_and_src_coords_attributes (die, parm_pack);
10151   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10152     generic_parameter_die (parm_pack,
10153                            TREE_VEC_ELT (parm_pack_args, j),
10154                            false /* Don't emit DW_AT_name */,
10155                            die);
10156   return die;
10157 }
10158
10159 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10160    an enumerated type.  */
10161
10162 static inline int
10163 type_is_enum (const_tree type)
10164 {
10165   return TREE_CODE (type) == ENUMERAL_TYPE;
10166 }
10167
10168 /* Return the DBX register number described by a given RTL node.  */
10169
10170 static unsigned int
10171 dbx_reg_number (const_rtx rtl)
10172 {
10173   unsigned regno = REGNO (rtl);
10174
10175   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10176
10177 #ifdef LEAF_REG_REMAP
10178   if (current_function_uses_only_leaf_regs)
10179     {
10180       int leaf_reg = LEAF_REG_REMAP (regno);
10181       if (leaf_reg != -1)
10182         regno = (unsigned) leaf_reg;
10183     }
10184 #endif
10185
10186   return DBX_REGISTER_NUMBER (regno);
10187 }
10188
10189 /* Optionally add a DW_OP_piece term to a location description expression.
10190    DW_OP_piece is only added if the location description expression already
10191    doesn't end with DW_OP_piece.  */
10192
10193 static void
10194 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10195 {
10196   dw_loc_descr_ref loc;
10197
10198   if (*list_head != NULL)
10199     {
10200       /* Find the end of the chain.  */
10201       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10202         ;
10203
10204       if (loc->dw_loc_opc != DW_OP_piece)
10205         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10206     }
10207 }
10208
10209 /* Return a location descriptor that designates a machine register or
10210    zero if there is none.  */
10211
10212 static dw_loc_descr_ref
10213 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10214 {
10215   rtx regs;
10216
10217   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10218     return 0;
10219
10220   /* We only use "frame base" when we're sure we're talking about the
10221      post-prologue local stack frame.  We do this by *not* running
10222      register elimination until this point, and recognizing the special
10223      argument pointer and soft frame pointer rtx's.
10224      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10225   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10226       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10227     {
10228       dw_loc_descr_ref result = NULL;
10229
10230       if (dwarf_version >= 4 || !dwarf_strict)
10231         {
10232           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10233                                        initialized);
10234           if (result)
10235             add_loc_descr (&result,
10236                            new_loc_descr (DW_OP_stack_value, 0, 0));
10237         }
10238       return result;
10239     }
10240
10241   regs = targetm.dwarf_register_span (rtl);
10242
10243   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10244     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10245   else
10246     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10247 }
10248
10249 /* Return a location descriptor that designates a machine register for
10250    a given hard register number.  */
10251
10252 static dw_loc_descr_ref
10253 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10254 {
10255   dw_loc_descr_ref reg_loc_descr;
10256
10257   if (regno <= 31)
10258     reg_loc_descr
10259       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10260   else
10261     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10262
10263   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10264     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10265
10266   return reg_loc_descr;
10267 }
10268
10269 /* Given an RTL of a register, return a location descriptor that
10270    designates a value that spans more than one register.  */
10271
10272 static dw_loc_descr_ref
10273 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10274                              enum var_init_status initialized)
10275 {
10276   int nregs, size, i;
10277   unsigned reg;
10278   dw_loc_descr_ref loc_result = NULL;
10279
10280   reg = REGNO (rtl);
10281 #ifdef LEAF_REG_REMAP
10282   if (current_function_uses_only_leaf_regs)
10283     {
10284       int leaf_reg = LEAF_REG_REMAP (reg);
10285       if (leaf_reg != -1)
10286         reg = (unsigned) leaf_reg;
10287     }
10288 #endif
10289   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10290   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10291
10292   /* Simple, contiguous registers.  */
10293   if (regs == NULL_RTX)
10294     {
10295       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10296
10297       loc_result = NULL;
10298       while (nregs--)
10299         {
10300           dw_loc_descr_ref t;
10301
10302           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10303                                       VAR_INIT_STATUS_INITIALIZED);
10304           add_loc_descr (&loc_result, t);
10305           add_loc_descr_op_piece (&loc_result, size);
10306           ++reg;
10307         }
10308       return loc_result;
10309     }
10310
10311   /* Now onto stupid register sets in non contiguous locations.  */
10312
10313   gcc_assert (GET_CODE (regs) == PARALLEL);
10314
10315   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10316   loc_result = NULL;
10317
10318   for (i = 0; i < XVECLEN (regs, 0); ++i)
10319     {
10320       dw_loc_descr_ref t;
10321
10322       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10323                                   VAR_INIT_STATUS_INITIALIZED);
10324       add_loc_descr (&loc_result, t);
10325       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10326       add_loc_descr_op_piece (&loc_result, size);
10327     }
10328
10329   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10330     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10331   return loc_result;
10332 }
10333
10334 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10335
10336 /* Return a location descriptor that designates a constant i,
10337    as a compound operation from constant (i >> shift), constant shift
10338    and DW_OP_shl.  */
10339
10340 static dw_loc_descr_ref
10341 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10342 {
10343   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10344   add_loc_descr (&ret, int_loc_descriptor (shift));
10345   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10346   return ret;
10347 }
10348
10349 /* Return a location descriptor that designates a constant.  */
10350
10351 static dw_loc_descr_ref
10352 int_loc_descriptor (HOST_WIDE_INT i)
10353 {
10354   enum dwarf_location_atom op;
10355
10356   /* Pick the smallest representation of a constant, rather than just
10357      defaulting to the LEB encoding.  */
10358   if (i >= 0)
10359     {
10360       int clz = clz_hwi (i);
10361       int ctz = ctz_hwi (i);
10362       if (i <= 31)
10363         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10364       else if (i <= 0xff)
10365         op = DW_OP_const1u;
10366       else if (i <= 0xffff)
10367         op = DW_OP_const2u;
10368       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10369                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10370         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10371            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10372            while DW_OP_const4u is 5 bytes.  */
10373         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10374       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10375                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10376         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10377            while DW_OP_const4u is 5 bytes.  */
10378         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10379       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10380         op = DW_OP_const4u;
10381       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10382                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10383         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10384            while DW_OP_constu of constant >= 0x100000000 takes at least
10385            6 bytes.  */
10386         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10387       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10388                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10389                   >= HOST_BITS_PER_WIDE_INT)
10390         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10391            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10392            while DW_OP_constu takes in this case at least 6 bytes.  */
10393         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10394       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10395                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10396                && size_of_uleb128 (i) > 6)
10397         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10398         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10399       else
10400         op = DW_OP_constu;
10401     }
10402   else
10403     {
10404       if (i >= -0x80)
10405         op = DW_OP_const1s;
10406       else if (i >= -0x8000)
10407         op = DW_OP_const2s;
10408       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10409         {
10410           if (size_of_int_loc_descriptor (i) < 5)
10411             {
10412               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10413               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10414               return ret;
10415             }
10416           op = DW_OP_const4s;
10417         }
10418       else
10419         {
10420           if (size_of_int_loc_descriptor (i)
10421               < (unsigned long) 1 + size_of_sleb128 (i))
10422             {
10423               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10424               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10425               return ret;
10426             }
10427           op = DW_OP_consts;
10428         }
10429     }
10430
10431   return new_loc_descr (op, i, 0);
10432 }
10433
10434 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10435    without actually allocating it.  */
10436
10437 static unsigned long
10438 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10439 {
10440   return size_of_int_loc_descriptor (i >> shift)
10441          + size_of_int_loc_descriptor (shift)
10442          + 1;
10443 }
10444
10445 /* Return size_of_locs (int_loc_descriptor (i)) without
10446    actually allocating it.  */
10447
10448 static unsigned long
10449 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10450 {
10451   unsigned long s;
10452
10453   if (i >= 0)
10454     {
10455       int clz, ctz;
10456       if (i <= 31)
10457         return 1;
10458       else if (i <= 0xff)
10459         return 2;
10460       else if (i <= 0xffff)
10461         return 3;
10462       clz = clz_hwi (i);
10463       ctz = ctz_hwi (i);
10464       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10465           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10466         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10467                                                     - clz - 5);
10468       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10469                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10470         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10471                                                     - clz - 8);
10472       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10473         return 5;
10474       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10475       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10476           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10477         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10478                                                     - clz - 8);
10479       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10480                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10481         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10482                                                     - clz - 16);
10483       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10484                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10485                && s > 6)
10486         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10487                                                     - clz - 32);
10488       else
10489         return 1 + s;
10490     }
10491   else
10492     {
10493       if (i >= -0x80)
10494         return 2;
10495       else if (i >= -0x8000)
10496         return 3;
10497       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10498         {
10499           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10500             {
10501               s = size_of_int_loc_descriptor (-i) + 1;
10502               if (s < 5)
10503                 return s;
10504             }
10505           return 5;
10506         }
10507       else
10508         {
10509           unsigned long r = 1 + size_of_sleb128 (i);
10510           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10511             {
10512               s = size_of_int_loc_descriptor (-i) + 1;
10513               if (s < r)
10514                 return s;
10515             }
10516           return r;
10517         }
10518     }
10519 }
10520
10521 /* Return loc description representing "address" of integer value.
10522    This can appear only as toplevel expression.  */
10523
10524 static dw_loc_descr_ref
10525 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10526 {
10527   int litsize;
10528   dw_loc_descr_ref loc_result = NULL;
10529
10530   if (!(dwarf_version >= 4 || !dwarf_strict))
10531     return NULL;
10532
10533   litsize = size_of_int_loc_descriptor (i);
10534   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10535      is more compact.  For DW_OP_stack_value we need:
10536      litsize + 1 (DW_OP_stack_value)
10537      and for DW_OP_implicit_value:
10538      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10539   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10540     {
10541       loc_result = int_loc_descriptor (i);
10542       add_loc_descr (&loc_result,
10543                      new_loc_descr (DW_OP_stack_value, 0, 0));
10544       return loc_result;
10545     }
10546
10547   loc_result = new_loc_descr (DW_OP_implicit_value,
10548                               size, 0);
10549   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10550   loc_result->dw_loc_oprnd2.v.val_int = i;
10551   return loc_result;
10552 }
10553
10554 /* Return a location descriptor that designates a base+offset location.  */
10555
10556 static dw_loc_descr_ref
10557 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10558                  enum var_init_status initialized)
10559 {
10560   unsigned int regno;
10561   dw_loc_descr_ref result;
10562   dw_fde_ref fde = cfun->fde;
10563
10564   /* We only use "frame base" when we're sure we're talking about the
10565      post-prologue local stack frame.  We do this by *not* running
10566      register elimination until this point, and recognizing the special
10567      argument pointer and soft frame pointer rtx's.  */
10568   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10569     {
10570       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10571
10572       if (elim != reg)
10573         {
10574           if (GET_CODE (elim) == PLUS)
10575             {
10576               offset += INTVAL (XEXP (elim, 1));
10577               elim = XEXP (elim, 0);
10578             }
10579           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10580                        && (elim == hard_frame_pointer_rtx
10581                            || elim == stack_pointer_rtx))
10582                       || elim == (frame_pointer_needed
10583                                   ? hard_frame_pointer_rtx
10584                                   : stack_pointer_rtx));
10585
10586           /* If drap register is used to align stack, use frame
10587              pointer + offset to access stack variables.  If stack
10588              is aligned without drap, use stack pointer + offset to
10589              access stack variables.  */
10590           if (crtl->stack_realign_tried
10591               && reg == frame_pointer_rtx)
10592             {
10593               int base_reg
10594                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10595                                       ? HARD_FRAME_POINTER_REGNUM
10596                                       : REGNO (elim));
10597               return new_reg_loc_descr (base_reg, offset);
10598             }
10599
10600           gcc_assert (frame_pointer_fb_offset_valid);
10601           offset += frame_pointer_fb_offset;
10602           return new_loc_descr (DW_OP_fbreg, offset, 0);
10603         }
10604     }
10605
10606   regno = REGNO (reg);
10607 #ifdef LEAF_REG_REMAP
10608   if (current_function_uses_only_leaf_regs)
10609     {
10610       int leaf_reg = LEAF_REG_REMAP (regno);
10611       if (leaf_reg != -1)
10612         regno = (unsigned) leaf_reg;
10613     }
10614 #endif
10615   regno = DWARF_FRAME_REGNUM (regno);
10616
10617   if (!optimize && fde
10618       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10619     {
10620       /* Use cfa+offset to represent the location of arguments passed
10621          on the stack when drap is used to align stack.
10622          Only do this when not optimizing, for optimized code var-tracking
10623          is supposed to track where the arguments live and the register
10624          used as vdrap or drap in some spot might be used for something
10625          else in other part of the routine.  */
10626       return new_loc_descr (DW_OP_fbreg, offset, 0);
10627     }
10628
10629   if (regno <= 31)
10630     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10631                             offset, 0);
10632   else
10633     result = new_loc_descr (DW_OP_bregx, regno, offset);
10634
10635   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10636     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10637
10638   return result;
10639 }
10640
10641 /* Return true if this RTL expression describes a base+offset calculation.  */
10642
10643 static inline int
10644 is_based_loc (const_rtx rtl)
10645 {
10646   return (GET_CODE (rtl) == PLUS
10647           && ((REG_P (XEXP (rtl, 0))
10648                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10649                && CONST_INT_P (XEXP (rtl, 1)))));
10650 }
10651
10652 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10653    failed.  */
10654
10655 static dw_loc_descr_ref
10656 tls_mem_loc_descriptor (rtx mem)
10657 {
10658   tree base;
10659   dw_loc_descr_ref loc_result;
10660
10661   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10662     return NULL;
10663
10664   base = get_base_address (MEM_EXPR (mem));
10665   if (base == NULL
10666       || TREE_CODE (base) != VAR_DECL
10667       || !DECL_THREAD_LOCAL_P (base))
10668     return NULL;
10669
10670   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10671   if (loc_result == NULL)
10672     return NULL;
10673
10674   if (MEM_OFFSET (mem))
10675     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10676
10677   return loc_result;
10678 }
10679
10680 /* Output debug info about reason why we failed to expand expression as dwarf
10681    expression.  */
10682
10683 static void
10684 expansion_failed (tree expr, rtx rtl, char const *reason)
10685 {
10686   if (dump_file && (dump_flags & TDF_DETAILS))
10687     {
10688       fprintf (dump_file, "Failed to expand as dwarf: ");
10689       if (expr)
10690         print_generic_expr (dump_file, expr, dump_flags);
10691       if (rtl)
10692         {
10693           fprintf (dump_file, "\n");
10694           print_rtl (dump_file, rtl);
10695         }
10696       fprintf (dump_file, "\nReason: %s\n", reason);
10697     }
10698 }
10699
10700 /* Helper function for const_ok_for_output, called either directly
10701    or via for_each_rtx.  */
10702
10703 static int
10704 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10705 {
10706   rtx rtl = *rtlp;
10707
10708   if (GET_CODE (rtl) == UNSPEC)
10709     {
10710       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10711          we can't express it in the debug info.  */
10712 #ifdef ENABLE_CHECKING
10713       /* Don't complain about TLS UNSPECs, those are just too hard to
10714          delegitimize.  */
10715       if (XVECLEN (rtl, 0) != 1
10716           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10717           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10718           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10719           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10720         inform (current_function_decl
10721                 ? DECL_SOURCE_LOCATION (current_function_decl)
10722                 : UNKNOWN_LOCATION,
10723 #if NUM_UNSPEC_VALUES > 0
10724                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10725                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10726                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10727                 XINT (rtl, 1));
10728 #else
10729                 "non-delegitimized UNSPEC %d found in variable location",
10730                 XINT (rtl, 1));
10731 #endif
10732 #endif
10733       expansion_failed (NULL_TREE, rtl,
10734                         "UNSPEC hasn't been delegitimized.\n");
10735       return 1;
10736     }
10737
10738   if (targetm.const_not_ok_for_debug_p (rtl))
10739     {
10740       expansion_failed (NULL_TREE, rtl,
10741                         "Expression rejected for debug by the backend.\n");
10742       return 1;
10743     }
10744
10745   if (GET_CODE (rtl) != SYMBOL_REF)
10746     return 0;
10747
10748   if (CONSTANT_POOL_ADDRESS_P (rtl))
10749     {
10750       bool marked;
10751       get_pool_constant_mark (rtl, &marked);
10752       /* If all references to this pool constant were optimized away,
10753          it was not output and thus we can't represent it.  */
10754       if (!marked)
10755         {
10756           expansion_failed (NULL_TREE, rtl,
10757                             "Constant was removed from constant pool.\n");
10758           return 1;
10759         }
10760     }
10761
10762   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10763     return 1;
10764
10765   /* Avoid references to external symbols in debug info, on several targets
10766      the linker might even refuse to link when linking a shared library,
10767      and in many other cases the relocations for .debug_info/.debug_loc are
10768      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10769      to be defined within the same shared library or executable are fine.  */
10770   if (SYMBOL_REF_EXTERNAL_P (rtl))
10771     {
10772       tree decl = SYMBOL_REF_DECL (rtl);
10773
10774       if (decl == NULL || !targetm.binds_local_p (decl))
10775         {
10776           expansion_failed (NULL_TREE, rtl,
10777                             "Symbol not defined in current TU.\n");
10778           return 1;
10779         }
10780     }
10781
10782   return 0;
10783 }
10784
10785 /* Return true if constant RTL can be emitted in DW_OP_addr or
10786    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10787    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10788
10789 static bool
10790 const_ok_for_output (rtx rtl)
10791 {
10792   if (GET_CODE (rtl) == SYMBOL_REF)
10793     return const_ok_for_output_1 (&rtl, NULL) == 0;
10794
10795   if (GET_CODE (rtl) == CONST)
10796     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10797
10798   return true;
10799 }
10800
10801 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10802    if possible, NULL otherwise.  */
10803
10804 static dw_die_ref
10805 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10806 {
10807   dw_die_ref type_die;
10808   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10809
10810   if (type == NULL)
10811     return NULL;
10812   switch (TREE_CODE (type))
10813     {
10814     case INTEGER_TYPE:
10815     case REAL_TYPE:
10816       break;
10817     default:
10818       return NULL;
10819     }
10820   type_die = lookup_type_die (type);
10821   if (!type_die)
10822     type_die = modified_type_die (type, false, false, comp_unit_die ());
10823   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10824     return NULL;
10825   return type_die;
10826 }
10827
10828 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10829    type matching MODE, or, if MODE is narrower than or as wide as
10830    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10831    possible.  */
10832
10833 static dw_loc_descr_ref
10834 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10835 {
10836   enum machine_mode outer_mode = mode;
10837   dw_die_ref type_die;
10838   dw_loc_descr_ref cvt;
10839
10840   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10841     {
10842       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10843       return op;
10844     }
10845   type_die = base_type_for_mode (outer_mode, 1);
10846   if (type_die == NULL)
10847     return NULL;
10848   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10849   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10850   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10851   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10852   add_loc_descr (&op, cvt);
10853   return op;
10854 }
10855
10856 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10857
10858 static dw_loc_descr_ref
10859 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10860                         dw_loc_descr_ref op1)
10861 {
10862   dw_loc_descr_ref ret = op0;
10863   add_loc_descr (&ret, op1);
10864   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10865   if (STORE_FLAG_VALUE != 1)
10866     {
10867       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10868       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10869     }
10870   return ret;
10871 }
10872
10873 /* Return location descriptor for signed comparison OP RTL.  */
10874
10875 static dw_loc_descr_ref
10876 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10877                          enum machine_mode mem_mode)
10878 {
10879   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10880   dw_loc_descr_ref op0, op1;
10881   int shift;
10882
10883   if (op_mode == VOIDmode)
10884     op_mode = GET_MODE (XEXP (rtl, 1));
10885   if (op_mode == VOIDmode)
10886     return NULL;
10887
10888   if (dwarf_strict
10889       && (GET_MODE_CLASS (op_mode) != MODE_INT
10890           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10891     return NULL;
10892
10893   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10894                             VAR_INIT_STATUS_INITIALIZED);
10895   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10896                             VAR_INIT_STATUS_INITIALIZED);
10897
10898   if (op0 == NULL || op1 == NULL)
10899     return NULL;
10900
10901   if (GET_MODE_CLASS (op_mode) != MODE_INT
10902       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10903     return compare_loc_descriptor (op, op0, op1);
10904
10905   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10906     {
10907       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10908       dw_loc_descr_ref cvt;
10909
10910       if (type_die == NULL)
10911         return NULL;
10912       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10913       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10914       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10915       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10916       add_loc_descr (&op0, cvt);
10917       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10918       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10919       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10920       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10921       add_loc_descr (&op1, cvt);
10922       return compare_loc_descriptor (op, op0, op1);
10923     }
10924
10925   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10926   /* For eq/ne, if the operands are known to be zero-extended,
10927      there is no need to do the fancy shifting up.  */
10928   if (op == DW_OP_eq || op == DW_OP_ne)
10929     {
10930       dw_loc_descr_ref last0, last1;
10931       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10932         ;
10933       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10934         ;
10935       /* deref_size zero extends, and for constants we can check
10936          whether they are zero extended or not.  */
10937       if (((last0->dw_loc_opc == DW_OP_deref_size
10938             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10939            || (CONST_INT_P (XEXP (rtl, 0))
10940                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10941                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10942           && ((last1->dw_loc_opc == DW_OP_deref_size
10943                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10944               || (CONST_INT_P (XEXP (rtl, 1))
10945                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10946                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10947         return compare_loc_descriptor (op, op0, op1);
10948
10949       /* EQ/NE comparison against constant in narrower type than
10950          DWARF2_ADDR_SIZE can be performed either as
10951          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10952          DW_OP_{eq,ne}
10953          or
10954          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10955          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10956       if (CONST_INT_P (XEXP (rtl, 1))
10957           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10958           && (size_of_int_loc_descriptor (shift) + 1
10959               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10960               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10961                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10962                                                & GET_MODE_MASK (op_mode))))
10963         {
10964           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10965           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10966           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10967                                     & GET_MODE_MASK (op_mode));
10968           return compare_loc_descriptor (op, op0, op1);
10969         }
10970     }
10971   add_loc_descr (&op0, int_loc_descriptor (shift));
10972   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10973   if (CONST_INT_P (XEXP (rtl, 1)))
10974     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10975   else
10976     {
10977       add_loc_descr (&op1, int_loc_descriptor (shift));
10978       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10979     }
10980   return compare_loc_descriptor (op, op0, op1);
10981 }
10982
10983 /* Return location descriptor for unsigned comparison OP RTL.  */
10984
10985 static dw_loc_descr_ref
10986 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10987                          enum machine_mode mem_mode)
10988 {
10989   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10990   dw_loc_descr_ref op0, op1;
10991
10992   if (op_mode == VOIDmode)
10993     op_mode = GET_MODE (XEXP (rtl, 1));
10994   if (op_mode == VOIDmode)
10995     return NULL;
10996   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10997     return NULL;
10998
10999   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11000     return NULL;
11001
11002   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11003                             VAR_INIT_STATUS_INITIALIZED);
11004   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11005                             VAR_INIT_STATUS_INITIALIZED);
11006
11007   if (op0 == NULL || op1 == NULL)
11008     return NULL;
11009
11010   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11011     {
11012       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11013       dw_loc_descr_ref last0, last1;
11014       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11015         ;
11016       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11017         ;
11018       if (CONST_INT_P (XEXP (rtl, 0)))
11019         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11020       /* deref_size zero extends, so no need to mask it again.  */
11021       else if (last0->dw_loc_opc != DW_OP_deref_size
11022                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11023         {
11024           add_loc_descr (&op0, int_loc_descriptor (mask));
11025           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11026         }
11027       if (CONST_INT_P (XEXP (rtl, 1)))
11028         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11029       /* deref_size zero extends, so no need to mask it again.  */
11030       else if (last1->dw_loc_opc != DW_OP_deref_size
11031                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11032         {
11033           add_loc_descr (&op1, int_loc_descriptor (mask));
11034           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11035         }
11036     }
11037   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11038     {
11039       HOST_WIDE_INT bias = 1;
11040       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11041       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11042       if (CONST_INT_P (XEXP (rtl, 1)))
11043         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11044                                   + INTVAL (XEXP (rtl, 1)));
11045       else
11046         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11047                                             bias, 0));
11048     }
11049   return compare_loc_descriptor (op, op0, op1);
11050 }
11051
11052 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11053
11054 static dw_loc_descr_ref
11055 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11056                        enum machine_mode mem_mode)
11057 {
11058   enum dwarf_location_atom op;
11059   dw_loc_descr_ref op0, op1, ret;
11060   dw_loc_descr_ref bra_node, drop_node;
11061
11062   if (dwarf_strict
11063       && (GET_MODE_CLASS (mode) != MODE_INT
11064           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11065     return NULL;
11066
11067   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11068                             VAR_INIT_STATUS_INITIALIZED);
11069   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11070                             VAR_INIT_STATUS_INITIALIZED);
11071
11072   if (op0 == NULL || op1 == NULL)
11073     return NULL;
11074
11075   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11076   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11077   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11078   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11079     {
11080       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11081         {
11082           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11083           add_loc_descr (&op0, int_loc_descriptor (mask));
11084           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11085           add_loc_descr (&op1, int_loc_descriptor (mask));
11086           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11087         }
11088       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11089         {
11090           HOST_WIDE_INT bias = 1;
11091           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11092           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11093           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11094         }
11095     }
11096   else if (GET_MODE_CLASS (mode) == MODE_INT
11097            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11098     {
11099       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11100       add_loc_descr (&op0, int_loc_descriptor (shift));
11101       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11102       add_loc_descr (&op1, int_loc_descriptor (shift));
11103       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11104     }
11105   else if (GET_MODE_CLASS (mode) == MODE_INT
11106            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11107     {
11108       dw_die_ref type_die = base_type_for_mode (mode, 0);
11109       dw_loc_descr_ref cvt;
11110       if (type_die == NULL)
11111         return NULL;
11112       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11113       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11114       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11115       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11116       add_loc_descr (&op0, cvt);
11117       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11118       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11119       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11120       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11121       add_loc_descr (&op1, cvt);
11122     }
11123
11124   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11125     op = DW_OP_lt;
11126   else
11127     op = DW_OP_gt;
11128   ret = op0;
11129   add_loc_descr (&ret, op1);
11130   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11131   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11132   add_loc_descr (&ret, bra_node);
11133   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11134   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11135   add_loc_descr (&ret, drop_node);
11136   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11137   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11138   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11139       && GET_MODE_CLASS (mode) == MODE_INT
11140       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11141     ret = convert_descriptor_to_mode (mode, ret);
11142   return ret;
11143 }
11144
11145 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11146    but after converting arguments to type_die, afterwards
11147    convert back to unsigned.  */
11148
11149 static dw_loc_descr_ref
11150 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11151              enum machine_mode mode, enum machine_mode mem_mode)
11152 {
11153   dw_loc_descr_ref cvt, op0, op1;
11154
11155   if (type_die == NULL)
11156     return NULL;
11157   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11158                             VAR_INIT_STATUS_INITIALIZED);
11159   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11160                             VAR_INIT_STATUS_INITIALIZED);
11161   if (op0 == NULL || op1 == NULL)
11162     return NULL;
11163   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11164   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11165   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11166   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11167   add_loc_descr (&op0, cvt);
11168   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11169   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11170   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11171   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11172   add_loc_descr (&op1, cvt);
11173   add_loc_descr (&op0, op1);
11174   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11175   return convert_descriptor_to_mode (mode, op0);
11176 }
11177
11178 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11179    const0 is DW_OP_lit0 or corresponding typed constant,
11180    const1 is DW_OP_lit1 or corresponding typed constant
11181    and constMSB is constant with just the MSB bit set
11182    for the mode):
11183        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11184    L1: const0 DW_OP_swap
11185    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11186        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11187    L3: DW_OP_drop
11188    L4: DW_OP_nop
11189
11190    CTZ is similar:
11191        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11192    L1: const0 DW_OP_swap
11193    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11194        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11195    L3: DW_OP_drop
11196    L4: DW_OP_nop
11197
11198    FFS is similar:
11199        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11200    L1: const1 DW_OP_swap
11201    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11202        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11203    L3: DW_OP_drop
11204    L4: DW_OP_nop  */
11205
11206 static dw_loc_descr_ref
11207 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11208                     enum machine_mode mem_mode)
11209 {
11210   dw_loc_descr_ref op0, ret, tmp;
11211   HOST_WIDE_INT valv;
11212   dw_loc_descr_ref l1jump, l1label;
11213   dw_loc_descr_ref l2jump, l2label;
11214   dw_loc_descr_ref l3jump, l3label;
11215   dw_loc_descr_ref l4jump, l4label;
11216   rtx msb;
11217
11218   if (GET_MODE_CLASS (mode) != MODE_INT
11219       || GET_MODE (XEXP (rtl, 0)) != mode
11220       || (GET_CODE (rtl) == CLZ
11221           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11222     return NULL;
11223
11224   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11225                             VAR_INIT_STATUS_INITIALIZED);
11226   if (op0 == NULL)
11227     return NULL;
11228   ret = op0;
11229   if (GET_CODE (rtl) == CLZ)
11230     {
11231       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11232         valv = GET_MODE_BITSIZE (mode);
11233     }
11234   else if (GET_CODE (rtl) == FFS)
11235     valv = 0;
11236   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11237     valv = GET_MODE_BITSIZE (mode);
11238   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11239   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11240   add_loc_descr (&ret, l1jump);
11241   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11242   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11243                             VAR_INIT_STATUS_INITIALIZED);
11244   if (tmp == NULL)
11245     return NULL;
11246   add_loc_descr (&ret, tmp);
11247   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11248   add_loc_descr (&ret, l4jump);
11249   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11250                                 ? const1_rtx : const0_rtx,
11251                                 mode, mem_mode,
11252                                 VAR_INIT_STATUS_INITIALIZED);
11253   if (l1label == NULL)
11254     return NULL;
11255   add_loc_descr (&ret, l1label);
11256   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11257   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11258   add_loc_descr (&ret, l2label);
11259   if (GET_CODE (rtl) != CLZ)
11260     msb = const1_rtx;
11261   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11262     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11263                    << (GET_MODE_BITSIZE (mode) - 1));
11264   else
11265     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11266                                   << (GET_MODE_BITSIZE (mode)
11267                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11268   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11269     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11270                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11271                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11272   else
11273     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11274                               VAR_INIT_STATUS_INITIALIZED);
11275   if (tmp == NULL)
11276     return NULL;
11277   add_loc_descr (&ret, tmp);
11278   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11279   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11280   add_loc_descr (&ret, l3jump);
11281   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11282                             VAR_INIT_STATUS_INITIALIZED);
11283   if (tmp == NULL)
11284     return NULL;
11285   add_loc_descr (&ret, tmp);
11286   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11287                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11288   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11289   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11290   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11291   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11292   add_loc_descr (&ret, l2jump);
11293   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11294   add_loc_descr (&ret, l3label);
11295   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11296   add_loc_descr (&ret, l4label);
11297   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11298   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11299   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11300   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11301   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11302   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11303   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11304   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11305   return ret;
11306 }
11307
11308 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11309    const1 is DW_OP_lit1 or corresponding typed constant):
11310        const0 DW_OP_swap
11311    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11312        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11313    L2: DW_OP_drop
11314
11315    PARITY is similar:
11316    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11317        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11318    L2: DW_OP_drop  */
11319
11320 static dw_loc_descr_ref
11321 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11322                          enum machine_mode mem_mode)
11323 {
11324   dw_loc_descr_ref op0, ret, tmp;
11325   dw_loc_descr_ref l1jump, l1label;
11326   dw_loc_descr_ref l2jump, l2label;
11327
11328   if (GET_MODE_CLASS (mode) != MODE_INT
11329       || GET_MODE (XEXP (rtl, 0)) != mode)
11330     return NULL;
11331
11332   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11333                             VAR_INIT_STATUS_INITIALIZED);
11334   if (op0 == NULL)
11335     return NULL;
11336   ret = op0;
11337   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11338                             VAR_INIT_STATUS_INITIALIZED);
11339   if (tmp == NULL)
11340     return NULL;
11341   add_loc_descr (&ret, tmp);
11342   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11343   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11344   add_loc_descr (&ret, l1label);
11345   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11346   add_loc_descr (&ret, l2jump);
11347   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11348   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11349   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11350                             VAR_INIT_STATUS_INITIALIZED);
11351   if (tmp == NULL)
11352     return NULL;
11353   add_loc_descr (&ret, tmp);
11354   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11355   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11356                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11357   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11358   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11359                             VAR_INIT_STATUS_INITIALIZED);
11360   add_loc_descr (&ret, tmp);
11361   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11362   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11363   add_loc_descr (&ret, l1jump);
11364   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11365   add_loc_descr (&ret, l2label);
11366   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11367   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11368   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11369   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11370   return ret;
11371 }
11372
11373 /* BSWAP (constS is initial shift count, either 56 or 24):
11374        constS const0
11375    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11376        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11377        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11378        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11379    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11380
11381 static dw_loc_descr_ref
11382 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11383                       enum machine_mode mem_mode)
11384 {
11385   dw_loc_descr_ref op0, ret, tmp;
11386   dw_loc_descr_ref l1jump, l1label;
11387   dw_loc_descr_ref l2jump, l2label;
11388
11389   if (GET_MODE_CLASS (mode) != MODE_INT
11390       || BITS_PER_UNIT != 8
11391       || (GET_MODE_BITSIZE (mode) != 32
11392           &&  GET_MODE_BITSIZE (mode) != 64))
11393     return NULL;
11394
11395   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11396                             VAR_INIT_STATUS_INITIALIZED);
11397   if (op0 == NULL)
11398     return NULL;
11399
11400   ret = op0;
11401   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11402                             mode, mem_mode,
11403                             VAR_INIT_STATUS_INITIALIZED);
11404   if (tmp == NULL)
11405     return NULL;
11406   add_loc_descr (&ret, tmp);
11407   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11408                             VAR_INIT_STATUS_INITIALIZED);
11409   if (tmp == NULL)
11410     return NULL;
11411   add_loc_descr (&ret, tmp);
11412   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11413   add_loc_descr (&ret, l1label);
11414   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11415                             mode, mem_mode,
11416                             VAR_INIT_STATUS_INITIALIZED);
11417   add_loc_descr (&ret, tmp);
11418   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11419   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11420   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11421   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11422                             VAR_INIT_STATUS_INITIALIZED);
11423   if (tmp == NULL)
11424     return NULL;
11425   add_loc_descr (&ret, tmp);
11426   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11427   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11428   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11429   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11430   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11431   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11432   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11433                             VAR_INIT_STATUS_INITIALIZED);
11434   add_loc_descr (&ret, tmp);
11435   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11436   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11437   add_loc_descr (&ret, l2jump);
11438   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11439                             VAR_INIT_STATUS_INITIALIZED);
11440   add_loc_descr (&ret, tmp);
11441   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11442   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11443   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11444   add_loc_descr (&ret, l1jump);
11445   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11446   add_loc_descr (&ret, l2label);
11447   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11448   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11449   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11450   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11451   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11452   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11453   return ret;
11454 }
11455
11456 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11457    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11458    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11459    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11460
11461    ROTATERT is similar:
11462    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11463    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11464    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11465
11466 static dw_loc_descr_ref
11467 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11468                        enum machine_mode mem_mode)
11469 {
11470   rtx rtlop1 = XEXP (rtl, 1);
11471   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11472   int i;
11473
11474   if (GET_MODE_CLASS (mode) != MODE_INT)
11475     return NULL;
11476
11477   if (GET_MODE (rtlop1) != VOIDmode
11478       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11479     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11480   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11481                             VAR_INIT_STATUS_INITIALIZED);
11482   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11483                             VAR_INIT_STATUS_INITIALIZED);
11484   if (op0 == NULL || op1 == NULL)
11485     return NULL;
11486   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11487     for (i = 0; i < 2; i++)
11488       {
11489         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11490           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11491                                         mode, mem_mode,
11492                                         VAR_INIT_STATUS_INITIALIZED);
11493         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11494           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11495                                    ? DW_OP_const4u
11496                                    : HOST_BITS_PER_WIDE_INT == 64
11497                                    ? DW_OP_const8u : DW_OP_constu,
11498                                    GET_MODE_MASK (mode), 0);
11499         else
11500           mask[i] = NULL;
11501         if (mask[i] == NULL)
11502           return NULL;
11503         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11504       }
11505   ret = op0;
11506   add_loc_descr (&ret, op1);
11507   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11508   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11509   if (GET_CODE (rtl) == ROTATERT)
11510     {
11511       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11512       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11513                                           GET_MODE_BITSIZE (mode), 0));
11514     }
11515   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11516   if (mask[0] != NULL)
11517     add_loc_descr (&ret, mask[0]);
11518   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11519   if (mask[1] != NULL)
11520     {
11521       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11522       add_loc_descr (&ret, mask[1]);
11523       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11524     }
11525   if (GET_CODE (rtl) == ROTATE)
11526     {
11527       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11528       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11529                                           GET_MODE_BITSIZE (mode), 0));
11530     }
11531   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11532   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11533   return ret;
11534 }
11535
11536 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11537    for DEBUG_PARAMETER_REF RTL.  */
11538
11539 static dw_loc_descr_ref
11540 parameter_ref_descriptor (rtx rtl)
11541 {
11542   dw_loc_descr_ref ret;
11543   dw_die_ref ref;
11544
11545   if (dwarf_strict)
11546     return NULL;
11547   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11548   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11549   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11550   if (ref)
11551     {
11552       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11553       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11554       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11555     }
11556   else
11557     {
11558       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11559       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11560     }
11561   return ret;
11562 }
11563
11564 /* Helper function to get mode of MEM's address.  */
11565
11566 enum machine_mode
11567 get_address_mode (rtx mem)
11568 {
11569   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11570   if (mode != VOIDmode)
11571     return mode;
11572   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11573 }
11574
11575 /* The following routine converts the RTL for a variable or parameter
11576    (resident in memory) into an equivalent Dwarf representation of a
11577    mechanism for getting the address of that same variable onto the top of a
11578    hypothetical "address evaluation" stack.
11579
11580    When creating memory location descriptors, we are effectively transforming
11581    the RTL for a memory-resident object into its Dwarf postfix expression
11582    equivalent.  This routine recursively descends an RTL tree, turning
11583    it into Dwarf postfix code as it goes.
11584
11585    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11586
11587    MEM_MODE is the mode of the memory reference, needed to handle some
11588    autoincrement addressing modes.
11589
11590    Return 0 if we can't represent the location.  */
11591
11592 dw_loc_descr_ref
11593 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11594                     enum machine_mode mem_mode,
11595                     enum var_init_status initialized)
11596 {
11597   dw_loc_descr_ref mem_loc_result = NULL;
11598   enum dwarf_location_atom op;
11599   dw_loc_descr_ref op0, op1;
11600
11601   if (mode == VOIDmode)
11602     mode = GET_MODE (rtl);
11603
11604   /* Note that for a dynamically sized array, the location we will generate a
11605      description of here will be the lowest numbered location which is
11606      actually within the array.  That's *not* necessarily the same as the
11607      zeroth element of the array.  */
11608
11609   rtl = targetm.delegitimize_address (rtl);
11610
11611   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11612     return NULL;
11613
11614   switch (GET_CODE (rtl))
11615     {
11616     case POST_INC:
11617     case POST_DEC:
11618     case POST_MODIFY:
11619       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11620
11621     case SUBREG:
11622       /* The case of a subreg may arise when we have a local (register)
11623          variable or a formal (register) parameter which doesn't quite fill
11624          up an entire register.  For now, just assume that it is
11625          legitimate to make the Dwarf info refer to the whole register which
11626          contains the given subreg.  */
11627       if (!subreg_lowpart_p (rtl))
11628         break;
11629       if (GET_MODE_CLASS (mode) == MODE_INT
11630           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11631           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11632 #ifdef POINTERS_EXTEND_UNSIGNED
11633               || (mode == Pmode && mem_mode != VOIDmode)
11634 #endif
11635              )
11636           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11637         {
11638           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11639                                                GET_MODE (SUBREG_REG (rtl)),
11640                                                mem_mode, initialized);
11641           break;
11642         }
11643       if (dwarf_strict)
11644         break;
11645       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11646         break;
11647       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11648           && (GET_MODE_CLASS (mode) != MODE_INT
11649               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11650         break;
11651       else
11652         {
11653           dw_die_ref type_die;
11654           dw_loc_descr_ref cvt;
11655
11656           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11657                                                GET_MODE (SUBREG_REG (rtl)),
11658                                                mem_mode, initialized);
11659           if (mem_loc_result == NULL)
11660             break;
11661           type_die = base_type_for_mode (mode,
11662                                          GET_MODE_CLASS (mode) == MODE_INT);
11663           if (type_die == NULL)
11664             {
11665               mem_loc_result = NULL;
11666               break;
11667             }
11668           if (GET_MODE_SIZE (mode)
11669               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11670             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11671           else
11672             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11673           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11674           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11675           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11676           add_loc_descr (&mem_loc_result, cvt);
11677         }
11678       break;
11679
11680     case REG:
11681       if (GET_MODE_CLASS (mode) != MODE_INT
11682           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11683 #ifdef POINTERS_EXTEND_UNSIGNED
11684               && (mode != Pmode || mem_mode == VOIDmode)
11685 #endif
11686               ))
11687         {
11688           dw_die_ref type_die;
11689
11690           if (dwarf_strict)
11691             break;
11692           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11693             break;
11694           type_die = base_type_for_mode (mode,
11695                                          GET_MODE_CLASS (mode) == MODE_INT);
11696           if (type_die == NULL)
11697             break;
11698           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11699                                           dbx_reg_number (rtl), 0);
11700           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11701           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11702           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11703           break;
11704         }
11705       /* Whenever a register number forms a part of the description of the
11706          method for calculating the (dynamic) address of a memory resident
11707          object, DWARF rules require the register number be referred to as
11708          a "base register".  This distinction is not based in any way upon
11709          what category of register the hardware believes the given register
11710          belongs to.  This is strictly DWARF terminology we're dealing with
11711          here. Note that in cases where the location of a memory-resident
11712          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11713          OP_CONST (0)) the actual DWARF location descriptor that we generate
11714          may just be OP_BASEREG (basereg).  This may look deceptively like
11715          the object in question was allocated to a register (rather than in
11716          memory) so DWARF consumers need to be aware of the subtle
11717          distinction between OP_REG and OP_BASEREG.  */
11718       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11719         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11720       else if (stack_realign_drap
11721                && crtl->drap_reg
11722                && crtl->args.internal_arg_pointer == rtl
11723                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11724         {
11725           /* If RTL is internal_arg_pointer, which has been optimized
11726              out, use DRAP instead.  */
11727           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11728                                             VAR_INIT_STATUS_INITIALIZED);
11729         }
11730       break;
11731
11732     case SIGN_EXTEND:
11733     case ZERO_EXTEND:
11734       if (GET_MODE_CLASS (mode) != MODE_INT)
11735         break;
11736       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11737                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11738       if (op0 == 0)
11739         break;
11740       else if (GET_CODE (rtl) == ZERO_EXTEND
11741                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11742                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11743                   < HOST_BITS_PER_WIDE_INT
11744                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11745                   to expand zero extend as two shifts instead of
11746                   masking.  */
11747                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11748         {
11749           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11750           mem_loc_result = op0;
11751           add_loc_descr (&mem_loc_result,
11752                          int_loc_descriptor (GET_MODE_MASK (imode)));
11753           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11754         }
11755       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11756         {
11757           int shift = DWARF2_ADDR_SIZE
11758                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11759           shift *= BITS_PER_UNIT;
11760           if (GET_CODE (rtl) == SIGN_EXTEND)
11761             op = DW_OP_shra;
11762           else
11763             op = DW_OP_shr;
11764           mem_loc_result = op0;
11765           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11766           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11767           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11768           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11769         }
11770       else if (!dwarf_strict)
11771         {
11772           dw_die_ref type_die1, type_die2;
11773           dw_loc_descr_ref cvt;
11774
11775           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11776                                           GET_CODE (rtl) == ZERO_EXTEND);
11777           if (type_die1 == NULL)
11778             break;
11779           type_die2 = base_type_for_mode (mode, 1);
11780           if (type_die2 == NULL)
11781             break;
11782           mem_loc_result = op0;
11783           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11784           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11785           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11786           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11787           add_loc_descr (&mem_loc_result, cvt);
11788           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11789           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11790           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11791           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11792           add_loc_descr (&mem_loc_result, cvt);
11793         }
11794       break;
11795
11796     case MEM:
11797       {
11798         rtx new_rtl = avoid_constant_pool_reference (rtl);
11799         if (new_rtl != rtl)
11800           {
11801             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11802                                                  initialized);
11803             if (mem_loc_result != NULL)
11804               return mem_loc_result;
11805           }
11806       }
11807       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11808                                            get_address_mode (rtl), mode,
11809                                            VAR_INIT_STATUS_INITIALIZED);
11810       if (mem_loc_result == NULL)
11811         mem_loc_result = tls_mem_loc_descriptor (rtl);
11812       if (mem_loc_result != NULL)
11813         {
11814           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11815               || GET_MODE_CLASS (mode) != MODE_INT)
11816             {
11817               dw_die_ref type_die;
11818               dw_loc_descr_ref deref;
11819
11820               if (dwarf_strict)
11821                 return NULL;
11822               type_die
11823                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11824               if (type_die == NULL)
11825                 return NULL;
11826               deref = new_loc_descr (DW_OP_GNU_deref_type,
11827                                      GET_MODE_SIZE (mode), 0);
11828               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11829               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11830               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11831               add_loc_descr (&mem_loc_result, deref);
11832             }
11833           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11834             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11835           else
11836             add_loc_descr (&mem_loc_result,
11837                            new_loc_descr (DW_OP_deref_size,
11838                                           GET_MODE_SIZE (mode), 0));
11839         }
11840       break;
11841
11842     case LO_SUM:
11843       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11844
11845     case LABEL_REF:
11846       /* Some ports can transform a symbol ref into a label ref, because
11847          the symbol ref is too far away and has to be dumped into a constant
11848          pool.  */
11849     case CONST:
11850     case SYMBOL_REF:
11851       if (GET_MODE_CLASS (mode) != MODE_INT
11852           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11853 #ifdef POINTERS_EXTEND_UNSIGNED
11854               && (mode != Pmode || mem_mode == VOIDmode)
11855 #endif
11856               ))
11857         break;
11858       if (GET_CODE (rtl) == SYMBOL_REF
11859           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11860         {
11861           dw_loc_descr_ref temp;
11862
11863           /* If this is not defined, we have no way to emit the data.  */
11864           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11865             break;
11866
11867           /* We used to emit DW_OP_addr here, but that's wrong, since
11868              DW_OP_addr should be relocated by the debug info consumer,
11869              while DW_OP_GNU_push_tls_address operand should not.  */
11870           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11871                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11872           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11873           temp->dw_loc_oprnd1.v.val_addr = rtl;
11874           temp->dtprel = true;
11875
11876           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11877           add_loc_descr (&mem_loc_result, temp);
11878
11879           break;
11880         }
11881
11882       if (!const_ok_for_output (rtl))
11883         break;
11884
11885     symref:
11886       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11887       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11888       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11889       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11890       break;
11891
11892     case CONCAT:
11893     case CONCATN:
11894     case VAR_LOCATION:
11895     case DEBUG_IMPLICIT_PTR:
11896       expansion_failed (NULL_TREE, rtl,
11897                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11898       return 0;
11899
11900     case ENTRY_VALUE:
11901       if (dwarf_strict)
11902         return NULL;
11903       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11904         {
11905           if (GET_MODE_CLASS (mode) != MODE_INT
11906               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11907             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11908                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11909           else
11910             op0
11911               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11912                                         VAR_INIT_STATUS_INITIALIZED);
11913         }
11914       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11915                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11916         {
11917           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11918                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11919           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11920             return NULL;
11921         }
11922       else
11923         gcc_unreachable ();
11924       if (op0 == NULL)
11925         return NULL;
11926       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11927       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11928       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11929       break;
11930
11931     case DEBUG_PARAMETER_REF:
11932       mem_loc_result = parameter_ref_descriptor (rtl);
11933       break;
11934
11935     case PRE_MODIFY:
11936       /* Extract the PLUS expression nested inside and fall into
11937          PLUS code below.  */
11938       rtl = XEXP (rtl, 1);
11939       goto plus;
11940
11941     case PRE_INC:
11942     case PRE_DEC:
11943       /* Turn these into a PLUS expression and fall into the PLUS code
11944          below.  */
11945       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11946                           GEN_INT (GET_CODE (rtl) == PRE_INC
11947                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11948                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11949
11950       /* ... fall through ...  */
11951
11952     case PLUS:
11953     plus:
11954       if (is_based_loc (rtl)
11955           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11956           && GET_MODE_CLASS (mode) == MODE_INT)
11957         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11958                                           INTVAL (XEXP (rtl, 1)),
11959                                           VAR_INIT_STATUS_INITIALIZED);
11960       else
11961         {
11962           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11963                                                VAR_INIT_STATUS_INITIALIZED);
11964           if (mem_loc_result == 0)
11965             break;
11966
11967           if (CONST_INT_P (XEXP (rtl, 1))
11968               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11969             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11970           else
11971             {
11972               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11973                                         VAR_INIT_STATUS_INITIALIZED);
11974               if (op1 == 0)
11975                 break;
11976               add_loc_descr (&mem_loc_result, op1);
11977               add_loc_descr (&mem_loc_result,
11978                              new_loc_descr (DW_OP_plus, 0, 0));
11979             }
11980         }
11981       break;
11982
11983     /* If a pseudo-reg is optimized away, it is possible for it to
11984        be replaced with a MEM containing a multiply or shift.  */
11985     case MINUS:
11986       op = DW_OP_minus;
11987       goto do_binop;
11988
11989     case MULT:
11990       op = DW_OP_mul;
11991       goto do_binop;
11992
11993     case DIV:
11994       if (!dwarf_strict
11995           && GET_MODE_CLASS (mode) == MODE_INT
11996           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11997         {
11998           mem_loc_result = typed_binop (DW_OP_div, rtl,
11999                                         base_type_for_mode (mode, 0),
12000                                         mode, mem_mode);
12001           break;
12002         }
12003       op = DW_OP_div;
12004       goto do_binop;
12005
12006     case UMOD:
12007       op = DW_OP_mod;
12008       goto do_binop;
12009
12010     case ASHIFT:
12011       op = DW_OP_shl;
12012       goto do_shift;
12013
12014     case ASHIFTRT:
12015       op = DW_OP_shra;
12016       goto do_shift;
12017
12018     case LSHIFTRT:
12019       op = DW_OP_shr;
12020       goto do_shift;
12021
12022     do_shift:
12023       if (GET_MODE_CLASS (mode) != MODE_INT)
12024         break;
12025       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12026                                 VAR_INIT_STATUS_INITIALIZED);
12027       {
12028         rtx rtlop1 = XEXP (rtl, 1);
12029         if (GET_MODE (rtlop1) != VOIDmode
12030             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12031                < GET_MODE_BITSIZE (mode))
12032           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12033         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12034                                   VAR_INIT_STATUS_INITIALIZED);
12035       }
12036
12037       if (op0 == 0 || op1 == 0)
12038         break;
12039
12040       mem_loc_result = op0;
12041       add_loc_descr (&mem_loc_result, op1);
12042       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12043       break;
12044
12045     case AND:
12046       op = DW_OP_and;
12047       goto do_binop;
12048
12049     case IOR:
12050       op = DW_OP_or;
12051       goto do_binop;
12052
12053     case XOR:
12054       op = DW_OP_xor;
12055       goto do_binop;
12056
12057     do_binop:
12058       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12059                                 VAR_INIT_STATUS_INITIALIZED);
12060       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12061                                 VAR_INIT_STATUS_INITIALIZED);
12062
12063       if (op0 == 0 || op1 == 0)
12064         break;
12065
12066       mem_loc_result = op0;
12067       add_loc_descr (&mem_loc_result, op1);
12068       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12069       break;
12070
12071     case MOD:
12072       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12073         {
12074           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12075                                         base_type_for_mode (mode, 0),
12076                                         mode, mem_mode);
12077           break;
12078         }
12079
12080       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12081                                 VAR_INIT_STATUS_INITIALIZED);
12082       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12083                                 VAR_INIT_STATUS_INITIALIZED);
12084
12085       if (op0 == 0 || op1 == 0)
12086         break;
12087
12088       mem_loc_result = op0;
12089       add_loc_descr (&mem_loc_result, op1);
12090       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12091       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12092       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12093       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12094       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12095       break;
12096
12097     case UDIV:
12098       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12099         {
12100           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12101             {
12102               op = DW_OP_div;
12103               goto do_binop;
12104             }
12105           mem_loc_result = typed_binop (DW_OP_div, rtl,
12106                                         base_type_for_mode (mode, 1),
12107                                         mode, mem_mode);
12108         }
12109       break;
12110
12111     case NOT:
12112       op = DW_OP_not;
12113       goto do_unop;
12114
12115     case ABS:
12116       op = DW_OP_abs;
12117       goto do_unop;
12118
12119     case NEG:
12120       op = DW_OP_neg;
12121       goto do_unop;
12122
12123     do_unop:
12124       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12125                                 VAR_INIT_STATUS_INITIALIZED);
12126
12127       if (op0 == 0)
12128         break;
12129
12130       mem_loc_result = op0;
12131       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12132       break;
12133
12134     case CONST_INT:
12135       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12136 #ifdef POINTERS_EXTEND_UNSIGNED
12137           || (mode == Pmode
12138               && mem_mode != VOIDmode
12139               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12140 #endif
12141           )
12142         {
12143           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12144           break;
12145         }
12146       if (!dwarf_strict
12147           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12148               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12149         {
12150           dw_die_ref type_die = base_type_for_mode (mode, 1);
12151           enum machine_mode amode;
12152           if (type_die == NULL)
12153             return NULL;
12154           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12155                                  MODE_INT, 0);
12156           if (INTVAL (rtl) >= 0
12157               && amode != BLKmode
12158               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12159               /* const DW_OP_GNU_convert <XXX> vs.
12160                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12161               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12162                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12163             {
12164               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12165               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12166               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12167               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12168               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12169               add_loc_descr (&mem_loc_result, op0);
12170               return mem_loc_result;
12171             }
12172           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12173                                           INTVAL (rtl));
12174           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12175           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12176           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12177           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12178             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12179           else
12180             {
12181               mem_loc_result->dw_loc_oprnd2.val_class
12182                 = dw_val_class_const_double;
12183               mem_loc_result->dw_loc_oprnd2.v.val_double
12184                 = shwi_to_double_int (INTVAL (rtl));
12185             }
12186         }
12187       break;
12188
12189     case CONST_DOUBLE:
12190       if (!dwarf_strict)
12191         {
12192           dw_die_ref type_die;
12193
12194           /* Note that a CONST_DOUBLE rtx could represent either an integer
12195              or a floating-point constant.  A CONST_DOUBLE is used whenever
12196              the constant requires more than one word in order to be
12197              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12198           if (mode == VOIDmode
12199               || (GET_MODE (rtl) == VOIDmode
12200                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12201             break;
12202           type_die = base_type_for_mode (mode,
12203                                          GET_MODE_CLASS (mode) == MODE_INT);
12204           if (type_die == NULL)
12205             return NULL;
12206           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12207           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12208           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12209           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12210           if (SCALAR_FLOAT_MODE_P (mode))
12211             {
12212               unsigned int length = GET_MODE_SIZE (mode);
12213               unsigned char *array
12214                   = (unsigned char*) ggc_alloc_atomic (length);
12215
12216               insert_float (rtl, array);
12217               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12218               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12219               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12220               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12221             }
12222           else
12223             {
12224               mem_loc_result->dw_loc_oprnd2.val_class
12225                 = dw_val_class_const_double;
12226               mem_loc_result->dw_loc_oprnd2.v.val_double
12227                 = rtx_to_double_int (rtl);
12228             }
12229         }
12230       break;
12231
12232     case EQ:
12233       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12234       break;
12235
12236     case GE:
12237       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12238       break;
12239
12240     case GT:
12241       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12242       break;
12243
12244     case LE:
12245       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12246       break;
12247
12248     case LT:
12249       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12250       break;
12251
12252     case NE:
12253       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12254       break;
12255
12256     case GEU:
12257       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12258       break;
12259
12260     case GTU:
12261       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12262       break;
12263
12264     case LEU:
12265       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12266       break;
12267
12268     case LTU:
12269       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12270       break;
12271
12272     case UMIN:
12273     case UMAX:
12274       if (GET_MODE_CLASS (mode) != MODE_INT)
12275         break;
12276       /* FALLTHRU */
12277     case SMIN:
12278     case SMAX:
12279       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12280       break;
12281
12282     case ZERO_EXTRACT:
12283     case SIGN_EXTRACT:
12284       if (CONST_INT_P (XEXP (rtl, 1))
12285           && CONST_INT_P (XEXP (rtl, 2))
12286           && ((unsigned) INTVAL (XEXP (rtl, 1))
12287               + (unsigned) INTVAL (XEXP (rtl, 2))
12288               <= GET_MODE_BITSIZE (mode))
12289           && GET_MODE_CLASS (mode) == MODE_INT
12290           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12291           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12292         {
12293           int shift, size;
12294           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12295                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12296           if (op0 == 0)
12297             break;
12298           if (GET_CODE (rtl) == SIGN_EXTRACT)
12299             op = DW_OP_shra;
12300           else
12301             op = DW_OP_shr;
12302           mem_loc_result = op0;
12303           size = INTVAL (XEXP (rtl, 1));
12304           shift = INTVAL (XEXP (rtl, 2));
12305           if (BITS_BIG_ENDIAN)
12306             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12307                     - shift - size;
12308           if (shift + size != (int) DWARF2_ADDR_SIZE)
12309             {
12310               add_loc_descr (&mem_loc_result,
12311                              int_loc_descriptor (DWARF2_ADDR_SIZE
12312                                                  - shift - size));
12313               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12314             }
12315           if (size != (int) DWARF2_ADDR_SIZE)
12316             {
12317               add_loc_descr (&mem_loc_result,
12318                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12319               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12320             }
12321         }
12322       break;
12323
12324     case IF_THEN_ELSE:
12325       {
12326         dw_loc_descr_ref op2, bra_node, drop_node;
12327         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12328                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12329                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12330                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12331         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12332                                   VAR_INIT_STATUS_INITIALIZED);
12333         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12334                                   VAR_INIT_STATUS_INITIALIZED);
12335         if (op0 == NULL || op1 == NULL || op2 == NULL)
12336           break;
12337
12338         mem_loc_result = op1;
12339         add_loc_descr (&mem_loc_result, op2);
12340         add_loc_descr (&mem_loc_result, op0);
12341         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12342         add_loc_descr (&mem_loc_result, bra_node);
12343         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12344         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12345         add_loc_descr (&mem_loc_result, drop_node);
12346         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12347         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12348       }
12349       break;
12350
12351     case FLOAT_EXTEND:
12352     case FLOAT_TRUNCATE:
12353     case FLOAT:
12354     case UNSIGNED_FLOAT:
12355     case FIX:
12356     case UNSIGNED_FIX:
12357       if (!dwarf_strict)
12358         {
12359           dw_die_ref type_die;
12360           dw_loc_descr_ref cvt;
12361
12362           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12363                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12364           if (op0 == NULL)
12365             break;
12366           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12367               && (GET_CODE (rtl) == FLOAT
12368                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12369                      <= DWARF2_ADDR_SIZE))
12370             {
12371               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12372                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12373               if (type_die == NULL)
12374                 break;
12375               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12376               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12377               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12378               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12379               add_loc_descr (&op0, cvt);
12380             }
12381           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12382           if (type_die == NULL)
12383             break;
12384           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12385           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12386           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12387           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12388           add_loc_descr (&op0, cvt);
12389           if (GET_MODE_CLASS (mode) == MODE_INT
12390               && (GET_CODE (rtl) == FIX
12391                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12392             {
12393               op0 = convert_descriptor_to_mode (mode, op0);
12394               if (op0 == NULL)
12395                 break;
12396             }
12397           mem_loc_result = op0;
12398         }
12399       break;
12400
12401     case CLZ:
12402     case CTZ:
12403     case FFS:
12404       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12405       break;
12406
12407     case POPCOUNT:
12408     case PARITY:
12409       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12410       break;
12411
12412     case BSWAP:
12413       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12414       break;
12415
12416     case ROTATE:
12417     case ROTATERT:
12418       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12419       break;
12420
12421     case COMPARE:
12422     case TRUNCATE:
12423       /* In theory, we could implement the above.  */
12424       /* DWARF cannot represent the unsigned compare operations
12425          natively.  */
12426     case SS_MULT:
12427     case US_MULT:
12428     case SS_DIV:
12429     case US_DIV:
12430     case SS_PLUS:
12431     case US_PLUS:
12432     case SS_MINUS:
12433     case US_MINUS:
12434     case SS_NEG:
12435     case US_NEG:
12436     case SS_ABS:
12437     case SS_ASHIFT:
12438     case US_ASHIFT:
12439     case SS_TRUNCATE:
12440     case US_TRUNCATE:
12441     case UNORDERED:
12442     case ORDERED:
12443     case UNEQ:
12444     case UNGE:
12445     case UNGT:
12446     case UNLE:
12447     case UNLT:
12448     case LTGT:
12449     case FRACT_CONVERT:
12450     case UNSIGNED_FRACT_CONVERT:
12451     case SAT_FRACT:
12452     case UNSIGNED_SAT_FRACT:
12453     case SQRT:
12454     case ASM_OPERANDS:
12455     case VEC_MERGE:
12456     case VEC_SELECT:
12457     case VEC_CONCAT:
12458     case VEC_DUPLICATE:
12459     case UNSPEC:
12460     case HIGH:
12461     case FMA:
12462     case STRICT_LOW_PART:
12463     case CONST_VECTOR:
12464     case CONST_FIXED:
12465     case CLRSB:
12466       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12467          can't express it in the debug info.  This can happen e.g. with some
12468          TLS UNSPECs.  */
12469       break;
12470
12471     case CONST_STRING:
12472       resolve_one_addr (&rtl, NULL);
12473       goto symref;
12474
12475     default:
12476 #ifdef ENABLE_CHECKING
12477       print_rtl (stderr, rtl);
12478       gcc_unreachable ();
12479 #else
12480       break;
12481 #endif
12482     }
12483
12484   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12485     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12486
12487   return mem_loc_result;
12488 }
12489
12490 /* Return a descriptor that describes the concatenation of two locations.
12491    This is typically a complex variable.  */
12492
12493 static dw_loc_descr_ref
12494 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12495 {
12496   dw_loc_descr_ref cc_loc_result = NULL;
12497   dw_loc_descr_ref x0_ref
12498     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12499   dw_loc_descr_ref x1_ref
12500     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12501
12502   if (x0_ref == 0 || x1_ref == 0)
12503     return 0;
12504
12505   cc_loc_result = x0_ref;
12506   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12507
12508   add_loc_descr (&cc_loc_result, x1_ref);
12509   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12510
12511   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12512     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12513
12514   return cc_loc_result;
12515 }
12516
12517 /* Return a descriptor that describes the concatenation of N
12518    locations.  */
12519
12520 static dw_loc_descr_ref
12521 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12522 {
12523   unsigned int i;
12524   dw_loc_descr_ref cc_loc_result = NULL;
12525   unsigned int n = XVECLEN (concatn, 0);
12526
12527   for (i = 0; i < n; ++i)
12528     {
12529       dw_loc_descr_ref ref;
12530       rtx x = XVECEXP (concatn, 0, i);
12531
12532       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12533       if (ref == NULL)
12534         return NULL;
12535
12536       add_loc_descr (&cc_loc_result, ref);
12537       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12538     }
12539
12540   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12541     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12542
12543   return cc_loc_result;
12544 }
12545
12546 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12547    for DEBUG_IMPLICIT_PTR RTL.  */
12548
12549 static dw_loc_descr_ref
12550 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12551 {
12552   dw_loc_descr_ref ret;
12553   dw_die_ref ref;
12554
12555   if (dwarf_strict)
12556     return NULL;
12557   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12558               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12559               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12560   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12561   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12562   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12563   if (ref)
12564     {
12565       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12566       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12567       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12568     }
12569   else
12570     {
12571       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12572       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12573     }
12574   return ret;
12575 }
12576
12577 /* Output a proper Dwarf location descriptor for a variable or parameter
12578    which is either allocated in a register or in a memory location.  For a
12579    register, we just generate an OP_REG and the register number.  For a
12580    memory location we provide a Dwarf postfix expression describing how to
12581    generate the (dynamic) address of the object onto the address stack.
12582
12583    MODE is mode of the decl if this loc_descriptor is going to be used in
12584    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12585    allowed, VOIDmode otherwise.
12586
12587    If we don't know how to describe it, return 0.  */
12588
12589 static dw_loc_descr_ref
12590 loc_descriptor (rtx rtl, enum machine_mode mode,
12591                 enum var_init_status initialized)
12592 {
12593   dw_loc_descr_ref loc_result = NULL;
12594
12595   switch (GET_CODE (rtl))
12596     {
12597     case SUBREG:
12598       /* The case of a subreg may arise when we have a local (register)
12599          variable or a formal (register) parameter which doesn't quite fill
12600          up an entire register.  For now, just assume that it is
12601          legitimate to make the Dwarf info refer to the whole register which
12602          contains the given subreg.  */
12603       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12604         loc_result = loc_descriptor (SUBREG_REG (rtl),
12605                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12606       else
12607         goto do_default;
12608       break;
12609
12610     case REG:
12611       loc_result = reg_loc_descriptor (rtl, initialized);
12612       break;
12613
12614     case MEM:
12615       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12616                                        GET_MODE (rtl), initialized);
12617       if (loc_result == NULL)
12618         loc_result = tls_mem_loc_descriptor (rtl);
12619       if (loc_result == NULL)
12620         {
12621           rtx new_rtl = avoid_constant_pool_reference (rtl);
12622           if (new_rtl != rtl)
12623             loc_result = loc_descriptor (new_rtl, mode, initialized);
12624         }
12625       break;
12626
12627     case CONCAT:
12628       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12629                                           initialized);
12630       break;
12631
12632     case CONCATN:
12633       loc_result = concatn_loc_descriptor (rtl, initialized);
12634       break;
12635
12636     case VAR_LOCATION:
12637       /* Single part.  */
12638       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12639         {
12640           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12641           if (GET_CODE (loc) == EXPR_LIST)
12642             loc = XEXP (loc, 0);
12643           loc_result = loc_descriptor (loc, mode, initialized);
12644           break;
12645         }
12646
12647       rtl = XEXP (rtl, 1);
12648       /* FALLTHRU */
12649
12650     case PARALLEL:
12651       {
12652         rtvec par_elems = XVEC (rtl, 0);
12653         int num_elem = GET_NUM_ELEM (par_elems);
12654         enum machine_mode mode;
12655         int i;
12656
12657         /* Create the first one, so we have something to add to.  */
12658         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12659                                      VOIDmode, initialized);
12660         if (loc_result == NULL)
12661           return NULL;
12662         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12663         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12664         for (i = 1; i < num_elem; i++)
12665           {
12666             dw_loc_descr_ref temp;
12667
12668             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12669                                    VOIDmode, initialized);
12670             if (temp == NULL)
12671               return NULL;
12672             add_loc_descr (&loc_result, temp);
12673             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12674             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12675           }
12676       }
12677       break;
12678
12679     case CONST_INT:
12680       if (mode != VOIDmode && mode != BLKmode)
12681         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12682                                                     INTVAL (rtl));
12683       break;
12684
12685     case CONST_DOUBLE:
12686       if (mode == VOIDmode)
12687         mode = GET_MODE (rtl);
12688
12689       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12690         {
12691           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12692
12693           /* Note that a CONST_DOUBLE rtx could represent either an integer
12694              or a floating-point constant.  A CONST_DOUBLE is used whenever
12695              the constant requires more than one word in order to be
12696              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12697           loc_result = new_loc_descr (DW_OP_implicit_value,
12698                                       GET_MODE_SIZE (mode), 0);
12699           if (SCALAR_FLOAT_MODE_P (mode))
12700             {
12701               unsigned int length = GET_MODE_SIZE (mode);
12702               unsigned char *array
12703                   = (unsigned char*) ggc_alloc_atomic (length);
12704
12705               insert_float (rtl, array);
12706               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12707               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12708               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12709               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12710             }
12711           else
12712             {
12713               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12714               loc_result->dw_loc_oprnd2.v.val_double
12715                 = rtx_to_double_int (rtl);
12716             }
12717         }
12718       break;
12719
12720     case CONST_VECTOR:
12721       if (mode == VOIDmode)
12722         mode = GET_MODE (rtl);
12723
12724       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12725         {
12726           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12727           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12728           unsigned char *array = (unsigned char *)
12729             ggc_alloc_atomic (length * elt_size);
12730           unsigned int i;
12731           unsigned char *p;
12732
12733           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12734           switch (GET_MODE_CLASS (mode))
12735             {
12736             case MODE_VECTOR_INT:
12737               for (i = 0, p = array; i < length; i++, p += elt_size)
12738                 {
12739                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12740                   double_int val = rtx_to_double_int (elt);
12741
12742                   if (elt_size <= sizeof (HOST_WIDE_INT))
12743                     insert_int (double_int_to_shwi (val), elt_size, p);
12744                   else
12745                     {
12746                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12747                       insert_double (val, p);
12748                     }
12749                 }
12750               break;
12751
12752             case MODE_VECTOR_FLOAT:
12753               for (i = 0, p = array; i < length; i++, p += elt_size)
12754                 {
12755                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12756                   insert_float (elt, p);
12757                 }
12758               break;
12759
12760             default:
12761               gcc_unreachable ();
12762             }
12763
12764           loc_result = new_loc_descr (DW_OP_implicit_value,
12765                                       length * elt_size, 0);
12766           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12767           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12768           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12769           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12770         }
12771       break;
12772
12773     case CONST:
12774       if (mode == VOIDmode
12775           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12776           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12777           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12778         {
12779           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12780           break;
12781         }
12782       /* FALLTHROUGH */
12783     case SYMBOL_REF:
12784       if (!const_ok_for_output (rtl))
12785         break;
12786     case LABEL_REF:
12787       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12788           && (dwarf_version >= 4 || !dwarf_strict))
12789         {
12790           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12791           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12792           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12793           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12794           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12795         }
12796       break;
12797
12798     case DEBUG_IMPLICIT_PTR:
12799       loc_result = implicit_ptr_descriptor (rtl, 0);
12800       break;
12801
12802     case PLUS:
12803       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12804           && CONST_INT_P (XEXP (rtl, 1)))
12805         {
12806           loc_result
12807             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12808           break;
12809         }
12810       /* FALLTHRU */
12811     do_default:
12812     default:
12813       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12814            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12815            && dwarf_version >= 4)
12816           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12817         {
12818           /* Value expression.  */
12819           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12820           if (loc_result)
12821             add_loc_descr (&loc_result,
12822                            new_loc_descr (DW_OP_stack_value, 0, 0));
12823         }
12824       break;
12825     }
12826
12827   return loc_result;
12828 }
12829
12830 /* We need to figure out what section we should use as the base for the
12831    address ranges where a given location is valid.
12832    1. If this particular DECL has a section associated with it, use that.
12833    2. If this function has a section associated with it, use that.
12834    3. Otherwise, use the text section.
12835    XXX: If you split a variable across multiple sections, we won't notice.  */
12836
12837 static const char *
12838 secname_for_decl (const_tree decl)
12839 {
12840   const char *secname;
12841
12842   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12843     {
12844       tree sectree = DECL_SECTION_NAME (decl);
12845       secname = TREE_STRING_POINTER (sectree);
12846     }
12847   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12848     {
12849       tree sectree = DECL_SECTION_NAME (current_function_decl);
12850       secname = TREE_STRING_POINTER (sectree);
12851     }
12852   else if (cfun && in_cold_section_p)
12853     secname = crtl->subsections.cold_section_label;
12854   else
12855     secname = text_section_label;
12856
12857   return secname;
12858 }
12859
12860 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12861
12862 static bool
12863 decl_by_reference_p (tree decl)
12864 {
12865   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12866            || TREE_CODE (decl) == VAR_DECL)
12867           && DECL_BY_REFERENCE (decl));
12868 }
12869
12870 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12871    for VARLOC.  */
12872
12873 static dw_loc_descr_ref
12874 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12875                enum var_init_status initialized)
12876 {
12877   int have_address = 0;
12878   dw_loc_descr_ref descr;
12879   enum machine_mode mode;
12880
12881   if (want_address != 2)
12882     {
12883       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12884       /* Single part.  */
12885       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12886         {
12887           varloc = PAT_VAR_LOCATION_LOC (varloc);
12888           if (GET_CODE (varloc) == EXPR_LIST)
12889             varloc = XEXP (varloc, 0);
12890           mode = GET_MODE (varloc);
12891           if (MEM_P (varloc))
12892             {
12893               rtx addr = XEXP (varloc, 0);
12894               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12895                                           mode, initialized);
12896               if (descr)
12897                 have_address = 1;
12898               else
12899                 {
12900                   rtx x = avoid_constant_pool_reference (varloc);
12901                   if (x != varloc)
12902                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12903                                                 initialized);
12904                 }
12905             }
12906           else
12907             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12908         }
12909       else
12910         return 0;
12911     }
12912   else
12913     {
12914       if (GET_CODE (varloc) == VAR_LOCATION)
12915         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12916       else
12917         mode = DECL_MODE (loc);
12918       descr = loc_descriptor (varloc, mode, initialized);
12919       have_address = 1;
12920     }
12921
12922   if (!descr)
12923     return 0;
12924
12925   if (want_address == 2 && !have_address
12926       && (dwarf_version >= 4 || !dwarf_strict))
12927     {
12928       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12929         {
12930           expansion_failed (loc, NULL_RTX,
12931                             "DWARF address size mismatch");
12932           return 0;
12933         }
12934       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12935       have_address = 1;
12936     }
12937   /* Show if we can't fill the request for an address.  */
12938   if (want_address && !have_address)
12939     {
12940       expansion_failed (loc, NULL_RTX,
12941                         "Want address and only have value");
12942       return 0;
12943     }
12944
12945   /* If we've got an address and don't want one, dereference.  */
12946   if (!want_address && have_address)
12947     {
12948       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12949       enum dwarf_location_atom op;
12950
12951       if (size > DWARF2_ADDR_SIZE || size == -1)
12952         {
12953           expansion_failed (loc, NULL_RTX,
12954                             "DWARF address size mismatch");
12955           return 0;
12956         }
12957       else if (size == DWARF2_ADDR_SIZE)
12958         op = DW_OP_deref;
12959       else
12960         op = DW_OP_deref_size;
12961
12962       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12963     }
12964
12965   return descr;
12966 }
12967
12968 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12969    if it is not possible.  */
12970
12971 static dw_loc_descr_ref
12972 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12973 {
12974   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12975     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12976   else if (dwarf_version >= 3 || !dwarf_strict)
12977     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12978   else
12979     return NULL;
12980 }
12981
12982 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12983    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12984
12985 static dw_loc_descr_ref
12986 dw_sra_loc_expr (tree decl, rtx loc)
12987 {
12988   rtx p;
12989   unsigned int padsize = 0;
12990   dw_loc_descr_ref descr, *descr_tail;
12991   unsigned HOST_WIDE_INT decl_size;
12992   rtx varloc;
12993   enum var_init_status initialized;
12994
12995   if (DECL_SIZE (decl) == NULL
12996       || !host_integerp (DECL_SIZE (decl), 1))
12997     return NULL;
12998
12999   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13000   descr = NULL;
13001   descr_tail = &descr;
13002
13003   for (p = loc; p; p = XEXP (p, 1))
13004     {
13005       unsigned int bitsize = decl_piece_bitsize (p);
13006       rtx loc_note = *decl_piece_varloc_ptr (p);
13007       dw_loc_descr_ref cur_descr;
13008       dw_loc_descr_ref *tail, last = NULL;
13009       unsigned int opsize = 0;
13010
13011       if (loc_note == NULL_RTX
13012           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13013         {
13014           padsize += bitsize;
13015           continue;
13016         }
13017       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13018       varloc = NOTE_VAR_LOCATION (loc_note);
13019       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13020       if (cur_descr == NULL)
13021         {
13022           padsize += bitsize;
13023           continue;
13024         }
13025
13026       /* Check that cur_descr either doesn't use
13027          DW_OP_*piece operations, or their sum is equal
13028          to bitsize.  Otherwise we can't embed it.  */
13029       for (tail = &cur_descr; *tail != NULL;
13030            tail = &(*tail)->dw_loc_next)
13031         if ((*tail)->dw_loc_opc == DW_OP_piece)
13032           {
13033             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13034                       * BITS_PER_UNIT;
13035             last = *tail;
13036           }
13037         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13038           {
13039             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13040             last = *tail;
13041           }
13042
13043       if (last != NULL && opsize != bitsize)
13044         {
13045           padsize += bitsize;
13046           continue;
13047         }
13048
13049       /* If there is a hole, add DW_OP_*piece after empty DWARF
13050          expression, which means that those bits are optimized out.  */
13051       if (padsize)
13052         {
13053           if (padsize > decl_size)
13054             return NULL;
13055           decl_size -= padsize;
13056           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13057           if (*descr_tail == NULL)
13058             return NULL;
13059           descr_tail = &(*descr_tail)->dw_loc_next;
13060           padsize = 0;
13061         }
13062       *descr_tail = cur_descr;
13063       descr_tail = tail;
13064       if (bitsize > decl_size)
13065         return NULL;
13066       decl_size -= bitsize;
13067       if (last == NULL)
13068         {
13069           HOST_WIDE_INT offset = 0;
13070           if (GET_CODE (varloc) == VAR_LOCATION
13071               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13072             {
13073               varloc = PAT_VAR_LOCATION_LOC (varloc);
13074               if (GET_CODE (varloc) == EXPR_LIST)
13075                 varloc = XEXP (varloc, 0);
13076             }
13077           do 
13078             {
13079               if (GET_CODE (varloc) == CONST
13080                   || GET_CODE (varloc) == SIGN_EXTEND
13081                   || GET_CODE (varloc) == ZERO_EXTEND)
13082                 varloc = XEXP (varloc, 0);
13083               else if (GET_CODE (varloc) == SUBREG)
13084                 varloc = SUBREG_REG (varloc);
13085               else
13086                 break;
13087             }
13088           while (1);
13089           /* DW_OP_bit_size offset should be zero for register
13090              or implicit location descriptions and empty location
13091              descriptions, but for memory addresses needs big endian
13092              adjustment.  */
13093           if (MEM_P (varloc))
13094             {
13095               unsigned HOST_WIDE_INT memsize
13096                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13097               if (memsize != bitsize)
13098                 {
13099                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13100                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13101                     return NULL;
13102                   if (memsize < bitsize)
13103                     return NULL;
13104                   if (BITS_BIG_ENDIAN)
13105                     offset = memsize - bitsize;
13106                 }
13107             }
13108
13109           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13110           if (*descr_tail == NULL)
13111             return NULL;
13112           descr_tail = &(*descr_tail)->dw_loc_next;
13113         }
13114     }
13115
13116   /* If there were any non-empty expressions, add padding till the end of
13117      the decl.  */
13118   if (descr != NULL && decl_size != 0)
13119     {
13120       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13121       if (*descr_tail == NULL)
13122         return NULL;
13123     }
13124   return descr;
13125 }
13126
13127 /* Return the dwarf representation of the location list LOC_LIST of
13128    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13129    function.  */
13130
13131 static dw_loc_list_ref
13132 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13133 {
13134   const char *endname, *secname;
13135   rtx varloc;
13136   enum var_init_status initialized;
13137   struct var_loc_node *node;
13138   dw_loc_descr_ref descr;
13139   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13140   dw_loc_list_ref list = NULL;
13141   dw_loc_list_ref *listp = &list;
13142
13143   /* Now that we know what section we are using for a base,
13144      actually construct the list of locations.
13145      The first location information is what is passed to the
13146      function that creates the location list, and the remaining
13147      locations just get added on to that list.
13148      Note that we only know the start address for a location
13149      (IE location changes), so to build the range, we use
13150      the range [current location start, next location start].
13151      This means we have to special case the last node, and generate
13152      a range of [last location start, end of function label].  */
13153
13154   secname = secname_for_decl (decl);
13155
13156   for (node = loc_list->first; node; node = node->next)
13157     if (GET_CODE (node->loc) == EXPR_LIST
13158         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13159       {
13160         if (GET_CODE (node->loc) == EXPR_LIST)
13161           {
13162             /* This requires DW_OP_{,bit_}piece, which is not usable
13163                inside DWARF expressions.  */
13164             if (want_address != 2)
13165               continue;
13166             descr = dw_sra_loc_expr (decl, node->loc);
13167             if (descr == NULL)
13168               continue;
13169           }
13170         else
13171           {
13172             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13173             varloc = NOTE_VAR_LOCATION (node->loc);
13174             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13175           }
13176         if (descr)
13177           {
13178             bool range_across_switch = false;
13179             /* If section switch happens in between node->label
13180                and node->next->label (or end of function) and
13181                we can't emit it as a single entry list,
13182                emit two ranges, first one ending at the end
13183                of first partition and second one starting at the
13184                beginning of second partition.  */
13185             if (node == loc_list->last_before_switch
13186                 && (node != loc_list->first || loc_list->first->next)
13187                 && current_function_decl)
13188               {
13189                 endname = cfun->fde->dw_fde_end;
13190                 range_across_switch = true;
13191               }
13192             /* The variable has a location between NODE->LABEL and
13193                NODE->NEXT->LABEL.  */
13194             else if (node->next)
13195               endname = node->next->label;
13196             /* If the variable has a location at the last label
13197                it keeps its location until the end of function.  */
13198             else if (!current_function_decl)
13199               endname = text_end_label;
13200             else
13201               {
13202                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13203                                              current_function_funcdef_no);
13204                 endname = ggc_strdup (label_id);
13205               }
13206
13207             *listp = new_loc_list (descr, node->label, endname, secname);
13208             if (TREE_CODE (decl) == PARM_DECL
13209                 && node == loc_list->first
13210                 && GET_CODE (node->loc) == NOTE
13211                 && strcmp (node->label, endname) == 0)
13212               (*listp)->force = true;
13213             listp = &(*listp)->dw_loc_next;
13214
13215             if (range_across_switch)
13216               {
13217                 if (GET_CODE (node->loc) == EXPR_LIST)
13218                   descr = dw_sra_loc_expr (decl, node->loc);
13219                 else
13220                   {
13221                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13222                     varloc = NOTE_VAR_LOCATION (node->loc);
13223                     descr = dw_loc_list_1 (decl, varloc, want_address,
13224                                            initialized);
13225                   }
13226                 gcc_assert (descr);
13227                 /* The variable has a location between NODE->LABEL and
13228                    NODE->NEXT->LABEL.  */
13229                 if (node->next)
13230                   endname = node->next->label;
13231                 else
13232                   endname = cfun->fde->dw_fde_second_end;
13233                 *listp = new_loc_list (descr,
13234                                        cfun->fde->dw_fde_second_begin,
13235                                        endname, secname);
13236                 listp = &(*listp)->dw_loc_next;
13237               }
13238           }
13239       }
13240
13241   /* Try to avoid the overhead of a location list emitting a location
13242      expression instead, but only if we didn't have more than one
13243      location entry in the first place.  If some entries were not
13244      representable, we don't want to pretend a single entry that was
13245      applies to the entire scope in which the variable is
13246      available.  */
13247   if (list && loc_list->first->next)
13248     gen_llsym (list);
13249
13250   return list;
13251 }
13252
13253 /* Return if the loc_list has only single element and thus can be represented
13254    as location description.   */
13255
13256 static bool
13257 single_element_loc_list_p (dw_loc_list_ref list)
13258 {
13259   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13260   return !list->ll_symbol;
13261 }
13262
13263 /* To each location in list LIST add loc descr REF.  */
13264
13265 static void
13266 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13267 {
13268   dw_loc_descr_ref copy;
13269   add_loc_descr (&list->expr, ref);
13270   list = list->dw_loc_next;
13271   while (list)
13272     {
13273       copy = ggc_alloc_dw_loc_descr_node ();
13274       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13275       add_loc_descr (&list->expr, copy);
13276       while (copy->dw_loc_next)
13277         {
13278           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13279           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13280           copy->dw_loc_next = new_copy;
13281           copy = new_copy;
13282         }
13283       list = list->dw_loc_next;
13284     }
13285 }
13286
13287 /* Given two lists RET and LIST
13288    produce location list that is result of adding expression in LIST
13289    to expression in RET on each possition in program.
13290    Might be destructive on both RET and LIST.
13291
13292    TODO: We handle only simple cases of RET or LIST having at most one
13293    element. General case would inolve sorting the lists in program order
13294    and merging them that will need some additional work.
13295    Adding that will improve quality of debug info especially for SRA-ed
13296    structures.  */
13297
13298 static void
13299 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13300 {
13301   if (!list)
13302     return;
13303   if (!*ret)
13304     {
13305       *ret = list;
13306       return;
13307     }
13308   if (!list->dw_loc_next)
13309     {
13310       add_loc_descr_to_each (*ret, list->expr);
13311       return;
13312     }
13313   if (!(*ret)->dw_loc_next)
13314     {
13315       add_loc_descr_to_each (list, (*ret)->expr);
13316       *ret = list;
13317       return;
13318     }
13319   expansion_failed (NULL_TREE, NULL_RTX,
13320                     "Don't know how to merge two non-trivial"
13321                     " location lists.\n");
13322   *ret = NULL;
13323   return;
13324 }
13325
13326 /* LOC is constant expression.  Try a luck, look it up in constant
13327    pool and return its loc_descr of its address.  */
13328
13329 static dw_loc_descr_ref
13330 cst_pool_loc_descr (tree loc)
13331 {
13332   /* Get an RTL for this, if something has been emitted.  */
13333   rtx rtl = lookup_constant_def (loc);
13334
13335   if (!rtl || !MEM_P (rtl))
13336     {
13337       gcc_assert (!rtl);
13338       return 0;
13339     }
13340   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13341
13342   /* TODO: We might get more coverage if we was actually delaying expansion
13343      of all expressions till end of compilation when constant pools are fully
13344      populated.  */
13345   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13346     {
13347       expansion_failed (loc, NULL_RTX,
13348                         "CST value in contant pool but not marked.");
13349       return 0;
13350     }
13351   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13352                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13353 }
13354
13355 /* Return dw_loc_list representing address of addr_expr LOC
13356    by looking for innder INDIRECT_REF expression and turing it
13357    into simple arithmetics.  */
13358
13359 static dw_loc_list_ref
13360 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13361 {
13362   tree obj, offset;
13363   HOST_WIDE_INT bitsize, bitpos, bytepos;
13364   enum machine_mode mode;
13365   int volatilep;
13366   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13367   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13368
13369   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13370                              &bitsize, &bitpos, &offset, &mode,
13371                              &unsignedp, &volatilep, false);
13372   STRIP_NOPS (obj);
13373   if (bitpos % BITS_PER_UNIT)
13374     {
13375       expansion_failed (loc, NULL_RTX, "bitfield access");
13376       return 0;
13377     }
13378   if (!INDIRECT_REF_P (obj))
13379     {
13380       expansion_failed (obj,
13381                         NULL_RTX, "no indirect ref in inner refrence");
13382       return 0;
13383     }
13384   if (!offset && !bitpos)
13385     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13386   else if (toplev
13387            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13388            && (dwarf_version >= 4 || !dwarf_strict))
13389     {
13390       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13391       if (!list_ret)
13392         return 0;
13393       if (offset)
13394         {
13395           /* Variable offset.  */
13396           list_ret1 = loc_list_from_tree (offset, 0);
13397           if (list_ret1 == 0)
13398             return 0;
13399           add_loc_list (&list_ret, list_ret1);
13400           if (!list_ret)
13401             return 0;
13402           add_loc_descr_to_each (list_ret,
13403                                  new_loc_descr (DW_OP_plus, 0, 0));
13404         }
13405       bytepos = bitpos / BITS_PER_UNIT;
13406       if (bytepos > 0)
13407         add_loc_descr_to_each (list_ret,
13408                                new_loc_descr (DW_OP_plus_uconst,
13409                                               bytepos, 0));
13410       else if (bytepos < 0)
13411         loc_list_plus_const (list_ret, bytepos);
13412       add_loc_descr_to_each (list_ret,
13413                              new_loc_descr (DW_OP_stack_value, 0, 0));
13414     }
13415   return list_ret;
13416 }
13417
13418
13419 /* Generate Dwarf location list representing LOC.
13420    If WANT_ADDRESS is false, expression computing LOC will be computed
13421    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13422    if WANT_ADDRESS is 2, expression computing address useable in location
13423      will be returned (i.e. DW_OP_reg can be used
13424      to refer to register values).  */
13425
13426 static dw_loc_list_ref
13427 loc_list_from_tree (tree loc, int want_address)
13428 {
13429   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13430   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13431   int have_address = 0;
13432   enum dwarf_location_atom op;
13433
13434   /* ??? Most of the time we do not take proper care for sign/zero
13435      extending the values properly.  Hopefully this won't be a real
13436      problem...  */
13437
13438   switch (TREE_CODE (loc))
13439     {
13440     case ERROR_MARK:
13441       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13442       return 0;
13443
13444     case PLACEHOLDER_EXPR:
13445       /* This case involves extracting fields from an object to determine the
13446          position of other fields.  We don't try to encode this here.  The
13447          only user of this is Ada, which encodes the needed information using
13448          the names of types.  */
13449       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13450       return 0;
13451
13452     case CALL_EXPR:
13453       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13454       /* There are no opcodes for these operations.  */
13455       return 0;
13456
13457     case PREINCREMENT_EXPR:
13458     case PREDECREMENT_EXPR:
13459     case POSTINCREMENT_EXPR:
13460     case POSTDECREMENT_EXPR:
13461       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13462       /* There are no opcodes for these operations.  */
13463       return 0;
13464
13465     case ADDR_EXPR:
13466       /* If we already want an address, see if there is INDIRECT_REF inside
13467          e.g. for &this->field.  */
13468       if (want_address)
13469         {
13470           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13471                        (loc, want_address == 2);
13472           if (list_ret)
13473             have_address = 1;
13474           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13475                    && (ret = cst_pool_loc_descr (loc)))
13476             have_address = 1;
13477         }
13478         /* Otherwise, process the argument and look for the address.  */
13479       if (!list_ret && !ret)
13480         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13481       else
13482         {
13483           if (want_address)
13484             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13485           return NULL;
13486         }
13487       break;
13488
13489     case VAR_DECL:
13490       if (DECL_THREAD_LOCAL_P (loc))
13491         {
13492           rtx rtl;
13493           enum dwarf_location_atom first_op;
13494           enum dwarf_location_atom second_op;
13495           bool dtprel = false;
13496
13497           if (targetm.have_tls)
13498             {
13499               /* If this is not defined, we have no way to emit the
13500                  data.  */
13501               if (!targetm.asm_out.output_dwarf_dtprel)
13502                 return 0;
13503
13504                /* The way DW_OP_GNU_push_tls_address is specified, we
13505                   can only look up addresses of objects in the current
13506                   module.  We used DW_OP_addr as first op, but that's
13507                   wrong, because DW_OP_addr is relocated by the debug
13508                   info consumer, while DW_OP_GNU_push_tls_address
13509                   operand shouldn't be.  */
13510               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13511                 return 0;
13512               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13513               dtprel = true;
13514               second_op = DW_OP_GNU_push_tls_address;
13515             }
13516           else
13517             {
13518               if (!targetm.emutls.debug_form_tls_address
13519                   || !(dwarf_version >= 3 || !dwarf_strict))
13520                 return 0;
13521               /* We stuffed the control variable into the DECL_VALUE_EXPR
13522                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13523                  no longer appear in gimple code.  We used the control
13524                  variable in specific so that we could pick it up here.  */
13525               loc = DECL_VALUE_EXPR (loc);
13526               first_op = DW_OP_addr;
13527               second_op = DW_OP_form_tls_address;
13528             }
13529
13530           rtl = rtl_for_decl_location (loc);
13531           if (rtl == NULL_RTX)
13532             return 0;
13533
13534           if (!MEM_P (rtl))
13535             return 0;
13536           rtl = XEXP (rtl, 0);
13537           if (! CONSTANT_P (rtl))
13538             return 0;
13539
13540           ret = new_loc_descr (first_op, 0, 0);
13541           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13542           ret->dw_loc_oprnd1.v.val_addr = rtl;
13543           ret->dtprel = dtprel;
13544
13545           ret1 = new_loc_descr (second_op, 0, 0);
13546           add_loc_descr (&ret, ret1);
13547
13548           have_address = 1;
13549           break;
13550         }
13551       /* FALLTHRU */
13552
13553     case PARM_DECL:
13554     case RESULT_DECL:
13555       if (DECL_HAS_VALUE_EXPR_P (loc))
13556         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13557                                    want_address);
13558       /* FALLTHRU */
13559
13560     case FUNCTION_DECL:
13561       {
13562         rtx rtl;
13563         var_loc_list *loc_list = lookup_decl_loc (loc);
13564
13565         if (loc_list && loc_list->first)
13566           {
13567             list_ret = dw_loc_list (loc_list, loc, want_address);
13568             have_address = want_address != 0;
13569             break;
13570           }
13571         rtl = rtl_for_decl_location (loc);
13572         if (rtl == NULL_RTX)
13573           {
13574             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13575             return 0;
13576           }
13577         else if (CONST_INT_P (rtl))
13578           {
13579             HOST_WIDE_INT val = INTVAL (rtl);
13580             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13581               val &= GET_MODE_MASK (DECL_MODE (loc));
13582             ret = int_loc_descriptor (val);
13583           }
13584         else if (GET_CODE (rtl) == CONST_STRING)
13585           {
13586             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13587             return 0;
13588           }
13589         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13590           {
13591             ret = new_loc_descr (DW_OP_addr, 0, 0);
13592             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13593             ret->dw_loc_oprnd1.v.val_addr = rtl;
13594           }
13595         else
13596           {
13597             enum machine_mode mode, mem_mode;
13598
13599             /* Certain constructs can only be represented at top-level.  */
13600             if (want_address == 2)
13601               {
13602                 ret = loc_descriptor (rtl, VOIDmode,
13603                                       VAR_INIT_STATUS_INITIALIZED);
13604                 have_address = 1;
13605               }
13606             else
13607               {
13608                 mode = GET_MODE (rtl);
13609                 mem_mode = VOIDmode;
13610                 if (MEM_P (rtl))
13611                   {
13612                     mem_mode = mode;
13613                     mode = get_address_mode (rtl);
13614                     rtl = XEXP (rtl, 0);
13615                     have_address = 1;
13616                   }
13617                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13618                                           VAR_INIT_STATUS_INITIALIZED);
13619               }
13620             if (!ret)
13621               expansion_failed (loc, rtl,
13622                                 "failed to produce loc descriptor for rtl");
13623           }
13624       }
13625       break;
13626
13627     case MEM_REF:
13628       /* ??? FIXME.  */
13629       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13630         return 0;
13631       /* Fallthru.  */
13632     case INDIRECT_REF:
13633       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13634       have_address = 1;
13635       break;
13636
13637     case COMPOUND_EXPR:
13638       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13639
13640     CASE_CONVERT:
13641     case VIEW_CONVERT_EXPR:
13642     case SAVE_EXPR:
13643     case MODIFY_EXPR:
13644       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13645
13646     case COMPONENT_REF:
13647     case BIT_FIELD_REF:
13648     case ARRAY_REF:
13649     case ARRAY_RANGE_REF:
13650     case REALPART_EXPR:
13651     case IMAGPART_EXPR:
13652       {
13653         tree obj, offset;
13654         HOST_WIDE_INT bitsize, bitpos, bytepos;
13655         enum machine_mode mode;
13656         int volatilep;
13657         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13658
13659         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13660                                    &unsignedp, &volatilep, false);
13661
13662         gcc_assert (obj != loc);
13663
13664         list_ret = loc_list_from_tree (obj,
13665                                        want_address == 2
13666                                        && !bitpos && !offset ? 2 : 1);
13667         /* TODO: We can extract value of the small expression via shifting even
13668            for nonzero bitpos.  */
13669         if (list_ret == 0)
13670           return 0;
13671         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13672           {
13673             expansion_failed (loc, NULL_RTX,
13674                               "bitfield access");
13675             return 0;
13676           }
13677
13678         if (offset != NULL_TREE)
13679           {
13680             /* Variable offset.  */
13681             list_ret1 = loc_list_from_tree (offset, 0);
13682             if (list_ret1 == 0)
13683               return 0;
13684             add_loc_list (&list_ret, list_ret1);
13685             if (!list_ret)
13686               return 0;
13687             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13688           }
13689
13690         bytepos = bitpos / BITS_PER_UNIT;
13691         if (bytepos > 0)
13692           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13693         else if (bytepos < 0)
13694           loc_list_plus_const (list_ret, bytepos);
13695
13696         have_address = 1;
13697         break;
13698       }
13699
13700     case INTEGER_CST:
13701       if ((want_address || !host_integerp (loc, 0))
13702           && (ret = cst_pool_loc_descr (loc)))
13703         have_address = 1;
13704       else if (want_address == 2
13705                && host_integerp (loc, 0)
13706                && (ret = address_of_int_loc_descriptor
13707                            (int_size_in_bytes (TREE_TYPE (loc)),
13708                             tree_low_cst (loc, 0))))
13709         have_address = 1;
13710       else if (host_integerp (loc, 0))
13711         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13712       else
13713         {
13714           expansion_failed (loc, NULL_RTX,
13715                             "Integer operand is not host integer");
13716           return 0;
13717         }
13718       break;
13719
13720     case CONSTRUCTOR:
13721     case REAL_CST:
13722     case STRING_CST:
13723     case COMPLEX_CST:
13724       if ((ret = cst_pool_loc_descr (loc)))
13725         have_address = 1;
13726       else
13727       /* We can construct small constants here using int_loc_descriptor.  */
13728         expansion_failed (loc, NULL_RTX,
13729                           "constructor or constant not in constant pool");
13730       break;
13731
13732     case TRUTH_AND_EXPR:
13733     case TRUTH_ANDIF_EXPR:
13734     case BIT_AND_EXPR:
13735       op = DW_OP_and;
13736       goto do_binop;
13737
13738     case TRUTH_XOR_EXPR:
13739     case BIT_XOR_EXPR:
13740       op = DW_OP_xor;
13741       goto do_binop;
13742
13743     case TRUTH_OR_EXPR:
13744     case TRUTH_ORIF_EXPR:
13745     case BIT_IOR_EXPR:
13746       op = DW_OP_or;
13747       goto do_binop;
13748
13749     case FLOOR_DIV_EXPR:
13750     case CEIL_DIV_EXPR:
13751     case ROUND_DIV_EXPR:
13752     case TRUNC_DIV_EXPR:
13753       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13754         return 0;
13755       op = DW_OP_div;
13756       goto do_binop;
13757
13758     case MINUS_EXPR:
13759       op = DW_OP_minus;
13760       goto do_binop;
13761
13762     case FLOOR_MOD_EXPR:
13763     case CEIL_MOD_EXPR:
13764     case ROUND_MOD_EXPR:
13765     case TRUNC_MOD_EXPR:
13766       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13767         {
13768           op = DW_OP_mod;
13769           goto do_binop;
13770         }
13771       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13772       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13773       if (list_ret == 0 || list_ret1 == 0)
13774         return 0;
13775
13776       add_loc_list (&list_ret, list_ret1);
13777       if (list_ret == 0)
13778         return 0;
13779       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13780       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13781       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13782       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13783       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13784       break;
13785
13786     case MULT_EXPR:
13787       op = DW_OP_mul;
13788       goto do_binop;
13789
13790     case LSHIFT_EXPR:
13791       op = DW_OP_shl;
13792       goto do_binop;
13793
13794     case RSHIFT_EXPR:
13795       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13796       goto do_binop;
13797
13798     case POINTER_PLUS_EXPR:
13799     case PLUS_EXPR:
13800       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13801         {
13802           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13803           if (list_ret == 0)
13804             return 0;
13805
13806           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13807           break;
13808         }
13809
13810       op = DW_OP_plus;
13811       goto do_binop;
13812
13813     case LE_EXPR:
13814       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13815         return 0;
13816
13817       op = DW_OP_le;
13818       goto do_binop;
13819
13820     case GE_EXPR:
13821       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13822         return 0;
13823
13824       op = DW_OP_ge;
13825       goto do_binop;
13826
13827     case LT_EXPR:
13828       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13829         return 0;
13830
13831       op = DW_OP_lt;
13832       goto do_binop;
13833
13834     case GT_EXPR:
13835       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13836         return 0;
13837
13838       op = DW_OP_gt;
13839       goto do_binop;
13840
13841     case EQ_EXPR:
13842       op = DW_OP_eq;
13843       goto do_binop;
13844
13845     case NE_EXPR:
13846       op = DW_OP_ne;
13847       goto do_binop;
13848
13849     do_binop:
13850       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13851       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13852       if (list_ret == 0 || list_ret1 == 0)
13853         return 0;
13854
13855       add_loc_list (&list_ret, list_ret1);
13856       if (list_ret == 0)
13857         return 0;
13858       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13859       break;
13860
13861     case TRUTH_NOT_EXPR:
13862     case BIT_NOT_EXPR:
13863       op = DW_OP_not;
13864       goto do_unop;
13865
13866     case ABS_EXPR:
13867       op = DW_OP_abs;
13868       goto do_unop;
13869
13870     case NEGATE_EXPR:
13871       op = DW_OP_neg;
13872       goto do_unop;
13873
13874     do_unop:
13875       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13876       if (list_ret == 0)
13877         return 0;
13878
13879       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13880       break;
13881
13882     case MIN_EXPR:
13883     case MAX_EXPR:
13884       {
13885         const enum tree_code code =
13886           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13887
13888         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13889                       build2 (code, integer_type_node,
13890                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13891                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13892       }
13893
13894       /* ... fall through ...  */
13895
13896     case COND_EXPR:
13897       {
13898         dw_loc_descr_ref lhs
13899           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13900         dw_loc_list_ref rhs
13901           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13902         dw_loc_descr_ref bra_node, jump_node, tmp;
13903
13904         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13905         if (list_ret == 0 || lhs == 0 || rhs == 0)
13906           return 0;
13907
13908         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13909         add_loc_descr_to_each (list_ret, bra_node);
13910
13911         add_loc_list (&list_ret, rhs);
13912         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13913         add_loc_descr_to_each (list_ret, jump_node);
13914
13915         add_loc_descr_to_each (list_ret, lhs);
13916         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13917         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13918
13919         /* ??? Need a node to point the skip at.  Use a nop.  */
13920         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13921         add_loc_descr_to_each (list_ret, tmp);
13922         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13923         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13924       }
13925       break;
13926
13927     case FIX_TRUNC_EXPR:
13928       return 0;
13929
13930     default:
13931       /* Leave front-end specific codes as simply unknown.  This comes
13932          up, for instance, with the C STMT_EXPR.  */
13933       if ((unsigned int) TREE_CODE (loc)
13934           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13935         {
13936           expansion_failed (loc, NULL_RTX,
13937                             "language specific tree node");
13938           return 0;
13939         }
13940
13941 #ifdef ENABLE_CHECKING
13942       /* Otherwise this is a generic code; we should just lists all of
13943          these explicitly.  We forgot one.  */
13944       gcc_unreachable ();
13945 #else
13946       /* In a release build, we want to degrade gracefully: better to
13947          generate incomplete debugging information than to crash.  */
13948       return NULL;
13949 #endif
13950     }
13951
13952   if (!ret && !list_ret)
13953     return 0;
13954
13955   if (want_address == 2 && !have_address
13956       && (dwarf_version >= 4 || !dwarf_strict))
13957     {
13958       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13959         {
13960           expansion_failed (loc, NULL_RTX,
13961                             "DWARF address size mismatch");
13962           return 0;
13963         }
13964       if (ret)
13965         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13966       else
13967         add_loc_descr_to_each (list_ret,
13968                                new_loc_descr (DW_OP_stack_value, 0, 0));
13969       have_address = 1;
13970     }
13971   /* Show if we can't fill the request for an address.  */
13972   if (want_address && !have_address)
13973     {
13974       expansion_failed (loc, NULL_RTX,
13975                         "Want address and only have value");
13976       return 0;
13977     }
13978
13979   gcc_assert (!ret || !list_ret);
13980
13981   /* If we've got an address and don't want one, dereference.  */
13982   if (!want_address && have_address)
13983     {
13984       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13985
13986       if (size > DWARF2_ADDR_SIZE || size == -1)
13987         {
13988           expansion_failed (loc, NULL_RTX,
13989                             "DWARF address size mismatch");
13990           return 0;
13991         }
13992       else if (size == DWARF2_ADDR_SIZE)
13993         op = DW_OP_deref;
13994       else
13995         op = DW_OP_deref_size;
13996
13997       if (ret)
13998         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13999       else
14000         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14001     }
14002   if (ret)
14003     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14004
14005   return list_ret;
14006 }
14007
14008 /* Same as above but return only single location expression.  */
14009 static dw_loc_descr_ref
14010 loc_descriptor_from_tree (tree loc, int want_address)
14011 {
14012   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14013   if (!ret)
14014     return NULL;
14015   if (ret->dw_loc_next)
14016     {
14017       expansion_failed (loc, NULL_RTX,
14018                         "Location list where only loc descriptor needed");
14019       return NULL;
14020     }
14021   return ret->expr;
14022 }
14023
14024 /* Given a value, round it up to the lowest multiple of `boundary'
14025    which is not less than the value itself.  */
14026
14027 static inline HOST_WIDE_INT
14028 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14029 {
14030   return (((value + boundary - 1) / boundary) * boundary);
14031 }
14032
14033 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14034    pointer to the declared type for the relevant field variable, or return
14035    `integer_type_node' if the given node turns out to be an
14036    ERROR_MARK node.  */
14037
14038 static inline tree
14039 field_type (const_tree decl)
14040 {
14041   tree type;
14042
14043   if (TREE_CODE (decl) == ERROR_MARK)
14044     return integer_type_node;
14045
14046   type = DECL_BIT_FIELD_TYPE (decl);
14047   if (type == NULL_TREE)
14048     type = TREE_TYPE (decl);
14049
14050   return type;
14051 }
14052
14053 /* Given a pointer to a tree node, return the alignment in bits for
14054    it, or else return BITS_PER_WORD if the node actually turns out to
14055    be an ERROR_MARK node.  */
14056
14057 static inline unsigned
14058 simple_type_align_in_bits (const_tree type)
14059 {
14060   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14061 }
14062
14063 static inline unsigned
14064 simple_decl_align_in_bits (const_tree decl)
14065 {
14066   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14067 }
14068
14069 /* Return the result of rounding T up to ALIGN.  */
14070
14071 static inline double_int
14072 round_up_to_align (double_int t, unsigned int align)
14073 {
14074   double_int alignd = uhwi_to_double_int (align);
14075   t = double_int_add (t, alignd);
14076   t = double_int_add (t, double_int_minus_one);
14077   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14078   t = double_int_mul (t, alignd);
14079   return t;
14080 }
14081
14082 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14083    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14084    or return 0 if we are unable to determine what that offset is, either
14085    because the argument turns out to be a pointer to an ERROR_MARK node, or
14086    because the offset is actually variable.  (We can't handle the latter case
14087    just yet).  */
14088
14089 static HOST_WIDE_INT
14090 field_byte_offset (const_tree decl)
14091 {
14092   double_int object_offset_in_bits;
14093   double_int object_offset_in_bytes;
14094   double_int bitpos_int;
14095
14096   if (TREE_CODE (decl) == ERROR_MARK)
14097     return 0;
14098
14099   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14100
14101   /* We cannot yet cope with fields whose positions are variable, so
14102      for now, when we see such things, we simply return 0.  Someday, we may
14103      be able to handle such cases, but it will be damn difficult.  */
14104   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14105     return 0;
14106
14107   bitpos_int = tree_to_double_int (bit_position (decl));
14108
14109 #ifdef PCC_BITFIELD_TYPE_MATTERS
14110   if (PCC_BITFIELD_TYPE_MATTERS)
14111     {
14112       tree type;
14113       tree field_size_tree;
14114       double_int deepest_bitpos;
14115       double_int field_size_in_bits;
14116       unsigned int type_align_in_bits;
14117       unsigned int decl_align_in_bits;
14118       double_int type_size_in_bits;
14119
14120       type = field_type (decl);
14121       type_size_in_bits = double_int_type_size_in_bits (type);
14122       type_align_in_bits = simple_type_align_in_bits (type);
14123
14124       field_size_tree = DECL_SIZE (decl);
14125
14126       /* The size could be unspecified if there was an error, or for
14127          a flexible array member.  */
14128       if (!field_size_tree)
14129         field_size_tree = bitsize_zero_node;
14130
14131       /* If the size of the field is not constant, use the type size.  */
14132       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14133         field_size_in_bits = tree_to_double_int (field_size_tree);
14134       else
14135         field_size_in_bits = type_size_in_bits;
14136
14137       decl_align_in_bits = simple_decl_align_in_bits (decl);
14138
14139       /* The GCC front-end doesn't make any attempt to keep track of the
14140          starting bit offset (relative to the start of the containing
14141          structure type) of the hypothetical "containing object" for a
14142          bit-field.  Thus, when computing the byte offset value for the
14143          start of the "containing object" of a bit-field, we must deduce
14144          this information on our own. This can be rather tricky to do in
14145          some cases.  For example, handling the following structure type
14146          definition when compiling for an i386/i486 target (which only
14147          aligns long long's to 32-bit boundaries) can be very tricky:
14148
14149          struct S { int field1; long long field2:31; };
14150
14151          Fortunately, there is a simple rule-of-thumb which can be used
14152          in such cases.  When compiling for an i386/i486, GCC will
14153          allocate 8 bytes for the structure shown above.  It decides to
14154          do this based upon one simple rule for bit-field allocation.
14155          GCC allocates each "containing object" for each bit-field at
14156          the first (i.e. lowest addressed) legitimate alignment boundary
14157          (based upon the required minimum alignment for the declared
14158          type of the field) which it can possibly use, subject to the
14159          condition that there is still enough available space remaining
14160          in the containing object (when allocated at the selected point)
14161          to fully accommodate all of the bits of the bit-field itself.
14162
14163          This simple rule makes it obvious why GCC allocates 8 bytes for
14164          each object of the structure type shown above.  When looking
14165          for a place to allocate the "containing object" for `field2',
14166          the compiler simply tries to allocate a 64-bit "containing
14167          object" at each successive 32-bit boundary (starting at zero)
14168          until it finds a place to allocate that 64- bit field such that
14169          at least 31 contiguous (and previously unallocated) bits remain
14170          within that selected 64 bit field.  (As it turns out, for the
14171          example above, the compiler finds it is OK to allocate the
14172          "containing object" 64-bit field at bit-offset zero within the
14173          structure type.)
14174
14175          Here we attempt to work backwards from the limited set of facts
14176          we're given, and we try to deduce from those facts, where GCC
14177          must have believed that the containing object started (within
14178          the structure type). The value we deduce is then used (by the
14179          callers of this routine) to generate DW_AT_location and
14180          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14181          the case of DW_AT_location, regular fields as well).  */
14182
14183       /* Figure out the bit-distance from the start of the structure to
14184          the "deepest" bit of the bit-field.  */
14185       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14186
14187       /* This is the tricky part.  Use some fancy footwork to deduce
14188          where the lowest addressed bit of the containing object must
14189          be.  */
14190       object_offset_in_bits
14191         = double_int_sub (deepest_bitpos, type_size_in_bits);
14192
14193       /* Round up to type_align by default.  This works best for
14194          bitfields.  */
14195       object_offset_in_bits
14196         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14197
14198       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14199         {
14200           object_offset_in_bits
14201             = double_int_sub (deepest_bitpos, type_size_in_bits);
14202
14203           /* Round up to decl_align instead.  */
14204           object_offset_in_bits
14205             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14206         }
14207     }
14208   else
14209 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14210     object_offset_in_bits = bitpos_int;
14211
14212   object_offset_in_bytes
14213     = double_int_div (object_offset_in_bits,
14214                       uhwi_to_double_int (BITS_PER_UNIT), true,
14215                       TRUNC_DIV_EXPR);
14216   return double_int_to_shwi (object_offset_in_bytes);
14217 }
14218 \f
14219 /* The following routines define various Dwarf attributes and any data
14220    associated with them.  */
14221
14222 /* Add a location description attribute value to a DIE.
14223
14224    This emits location attributes suitable for whole variables and
14225    whole parameters.  Note that the location attributes for struct fields are
14226    generated by the routine `data_member_location_attribute' below.  */
14227
14228 static inline void
14229 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14230                              dw_loc_list_ref descr)
14231 {
14232   if (descr == 0)
14233     return;
14234   if (single_element_loc_list_p (descr))
14235     add_AT_loc (die, attr_kind, descr->expr);
14236   else
14237     add_AT_loc_list (die, attr_kind, descr);
14238 }
14239
14240 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14241
14242 static void
14243 add_accessibility_attribute (dw_die_ref die, tree decl)
14244 {
14245   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14246      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14247      the default has always been DW_ACCESS_public.  */
14248   if (TREE_PROTECTED (decl))
14249     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14250   else if (TREE_PRIVATE (decl))
14251     {
14252       if (dwarf_version == 2
14253           || die->die_parent == NULL
14254           || die->die_parent->die_tag != DW_TAG_class_type)
14255         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14256     }
14257   else if (dwarf_version > 2
14258            && die->die_parent
14259            && die->die_parent->die_tag == DW_TAG_class_type)
14260     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14261 }
14262
14263 /* Attach the specialized form of location attribute used for data members of
14264    struct and union types.  In the special case of a FIELD_DECL node which
14265    represents a bit-field, the "offset" part of this special location
14266    descriptor must indicate the distance in bytes from the lowest-addressed
14267    byte of the containing struct or union type to the lowest-addressed byte of
14268    the "containing object" for the bit-field.  (See the `field_byte_offset'
14269    function above).
14270
14271    For any given bit-field, the "containing object" is a hypothetical object
14272    (of some integral or enum type) within which the given bit-field lives.  The
14273    type of this hypothetical "containing object" is always the same as the
14274    declared type of the individual bit-field itself (for GCC anyway... the
14275    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14276    bytes) of the hypothetical "containing object" which will be given in the
14277    DW_AT_byte_size attribute for this bit-field.  (See the
14278    `byte_size_attribute' function below.)  It is also used when calculating the
14279    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14280    function below.)  */
14281
14282 static void
14283 add_data_member_location_attribute (dw_die_ref die, tree decl)
14284 {
14285   HOST_WIDE_INT offset;
14286   dw_loc_descr_ref loc_descr = 0;
14287
14288   if (TREE_CODE (decl) == TREE_BINFO)
14289     {
14290       /* We're working on the TAG_inheritance for a base class.  */
14291       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14292         {
14293           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14294              aren't at a fixed offset from all (sub)objects of the same
14295              type.  We need to extract the appropriate offset from our
14296              vtable.  The following dwarf expression means
14297
14298                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14299
14300              This is specific to the V3 ABI, of course.  */
14301
14302           dw_loc_descr_ref tmp;
14303
14304           /* Make a copy of the object address.  */
14305           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14306           add_loc_descr (&loc_descr, tmp);
14307
14308           /* Extract the vtable address.  */
14309           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14310           add_loc_descr (&loc_descr, tmp);
14311
14312           /* Calculate the address of the offset.  */
14313           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14314           gcc_assert (offset < 0);
14315
14316           tmp = int_loc_descriptor (-offset);
14317           add_loc_descr (&loc_descr, tmp);
14318           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14319           add_loc_descr (&loc_descr, tmp);
14320
14321           /* Extract the offset.  */
14322           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14323           add_loc_descr (&loc_descr, tmp);
14324
14325           /* Add it to the object address.  */
14326           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14327           add_loc_descr (&loc_descr, tmp);
14328         }
14329       else
14330         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14331     }
14332   else
14333     offset = field_byte_offset (decl);
14334
14335   if (! loc_descr)
14336     {
14337       if (dwarf_version > 2)
14338         {
14339           /* Don't need to output a location expression, just the constant. */
14340           if (offset < 0)
14341             add_AT_int (die, DW_AT_data_member_location, offset);
14342           else
14343             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14344           return;
14345         }
14346       else
14347         {
14348           enum dwarf_location_atom op;
14349
14350           /* The DWARF2 standard says that we should assume that the structure
14351              address is already on the stack, so we can specify a structure
14352              field address by using DW_OP_plus_uconst.  */
14353
14354 #ifdef MIPS_DEBUGGING_INFO
14355           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14356              operator correctly.  It works only if we leave the offset on the
14357              stack.  */
14358           op = DW_OP_constu;
14359 #else
14360           op = DW_OP_plus_uconst;
14361 #endif
14362
14363           loc_descr = new_loc_descr (op, offset, 0);
14364         }
14365     }
14366
14367   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14368 }
14369
14370 /* Writes integer values to dw_vec_const array.  */
14371
14372 static void
14373 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14374 {
14375   while (size != 0)
14376     {
14377       *dest++ = val & 0xff;
14378       val >>= 8;
14379       --size;
14380     }
14381 }
14382
14383 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14384
14385 static HOST_WIDE_INT
14386 extract_int (const unsigned char *src, unsigned int size)
14387 {
14388   HOST_WIDE_INT val = 0;
14389
14390   src += size;
14391   while (size != 0)
14392     {
14393       val <<= 8;
14394       val |= *--src & 0xff;
14395       --size;
14396     }
14397   return val;
14398 }
14399
14400 /* Writes double_int values to dw_vec_const array.  */
14401
14402 static void
14403 insert_double (double_int val, unsigned char *dest)
14404 {
14405   unsigned char *p0 = dest;
14406   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14407
14408   if (WORDS_BIG_ENDIAN)
14409     {
14410       p0 = p1;
14411       p1 = dest;
14412     }
14413
14414   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14415   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14416 }
14417
14418 /* Writes floating point values to dw_vec_const array.  */
14419
14420 static void
14421 insert_float (const_rtx rtl, unsigned char *array)
14422 {
14423   REAL_VALUE_TYPE rv;
14424   long val[4];
14425   int i;
14426
14427   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14428   real_to_target (val, &rv, GET_MODE (rtl));
14429
14430   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14431   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14432     {
14433       insert_int (val[i], 4, array);
14434       array += 4;
14435     }
14436 }
14437
14438 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14439    does not have a "location" either in memory or in a register.  These
14440    things can arise in GNU C when a constant is passed as an actual parameter
14441    to an inlined function.  They can also arise in C++ where declared
14442    constants do not necessarily get memory "homes".  */
14443
14444 static bool
14445 add_const_value_attribute (dw_die_ref die, rtx rtl)
14446 {
14447   switch (GET_CODE (rtl))
14448     {
14449     case CONST_INT:
14450       {
14451         HOST_WIDE_INT val = INTVAL (rtl);
14452
14453         if (val < 0)
14454           add_AT_int (die, DW_AT_const_value, val);
14455         else
14456           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14457       }
14458       return true;
14459
14460     case CONST_DOUBLE:
14461       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14462          floating-point constant.  A CONST_DOUBLE is used whenever the
14463          constant requires more than one word in order to be adequately
14464          represented.  */
14465       {
14466         enum machine_mode mode = GET_MODE (rtl);
14467
14468         if (SCALAR_FLOAT_MODE_P (mode))
14469           {
14470             unsigned int length = GET_MODE_SIZE (mode);
14471             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14472
14473             insert_float (rtl, array);
14474             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14475           }
14476         else
14477           add_AT_double (die, DW_AT_const_value,
14478                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14479       }
14480       return true;
14481
14482     case CONST_VECTOR:
14483       {
14484         enum machine_mode mode = GET_MODE (rtl);
14485         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14486         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14487         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14488           (length * elt_size);
14489         unsigned int i;
14490         unsigned char *p;
14491
14492         switch (GET_MODE_CLASS (mode))
14493           {
14494           case MODE_VECTOR_INT:
14495             for (i = 0, p = array; i < length; i++, p += elt_size)
14496               {
14497                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14498                 double_int val = rtx_to_double_int (elt);
14499
14500                 if (elt_size <= sizeof (HOST_WIDE_INT))
14501                   insert_int (double_int_to_shwi (val), elt_size, p);
14502                 else
14503                   {
14504                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14505                     insert_double (val, p);
14506                   }
14507               }
14508             break;
14509
14510           case MODE_VECTOR_FLOAT:
14511             for (i = 0, p = array; i < length; i++, p += elt_size)
14512               {
14513                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14514                 insert_float (elt, p);
14515               }
14516             break;
14517
14518           default:
14519             gcc_unreachable ();
14520           }
14521
14522         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14523       }
14524       return true;
14525
14526     case CONST_STRING:
14527       if (dwarf_version >= 4 || !dwarf_strict)
14528         {
14529           dw_loc_descr_ref loc_result;
14530           resolve_one_addr (&rtl, NULL);
14531         rtl_addr:
14532           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14533           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14534           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14535           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14536           add_AT_loc (die, DW_AT_location, loc_result);
14537           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14538           return true;
14539         }
14540       return false;
14541
14542     case CONST:
14543       if (CONSTANT_P (XEXP (rtl, 0)))
14544         return add_const_value_attribute (die, XEXP (rtl, 0));
14545       /* FALLTHROUGH */
14546     case SYMBOL_REF:
14547       if (!const_ok_for_output (rtl))
14548         return false;
14549     case LABEL_REF:
14550       if (dwarf_version >= 4 || !dwarf_strict)
14551         goto rtl_addr;
14552       return false;
14553
14554     case PLUS:
14555       /* In cases where an inlined instance of an inline function is passed
14556          the address of an `auto' variable (which is local to the caller) we
14557          can get a situation where the DECL_RTL of the artificial local
14558          variable (for the inlining) which acts as a stand-in for the
14559          corresponding formal parameter (of the inline function) will look
14560          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14561          exactly a compile-time constant expression, but it isn't the address
14562          of the (artificial) local variable either.  Rather, it represents the
14563          *value* which the artificial local variable always has during its
14564          lifetime.  We currently have no way to represent such quasi-constant
14565          values in Dwarf, so for now we just punt and generate nothing.  */
14566       return false;
14567
14568     case HIGH:
14569     case CONST_FIXED:
14570       return false;
14571
14572     case MEM:
14573       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14574           && MEM_READONLY_P (rtl)
14575           && GET_MODE (rtl) == BLKmode)
14576         {
14577           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14578           return true;
14579         }
14580       return false;
14581
14582     default:
14583       /* No other kinds of rtx should be possible here.  */
14584       gcc_unreachable ();
14585     }
14586   return false;
14587 }
14588
14589 /* Determine whether the evaluation of EXPR references any variables
14590    or functions which aren't otherwise used (and therefore may not be
14591    output).  */
14592 static tree
14593 reference_to_unused (tree * tp, int * walk_subtrees,
14594                      void * data ATTRIBUTE_UNUSED)
14595 {
14596   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14597     *walk_subtrees = 0;
14598
14599   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14600       && ! TREE_ASM_WRITTEN (*tp))
14601     return *tp;
14602   /* ???  The C++ FE emits debug information for using decls, so
14603      putting gcc_unreachable here falls over.  See PR31899.  For now
14604      be conservative.  */
14605   else if (!cgraph_global_info_ready
14606            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14607     return *tp;
14608   else if (TREE_CODE (*tp) == VAR_DECL)
14609     {
14610       struct varpool_node *node = varpool_get_node (*tp);
14611       if (!node || !node->needed)
14612         return *tp;
14613     }
14614   else if (TREE_CODE (*tp) == FUNCTION_DECL
14615            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14616     {
14617       /* The call graph machinery must have finished analyzing,
14618          optimizing and gimplifying the CU by now.
14619          So if *TP has no call graph node associated
14620          to it, it means *TP will not be emitted.  */
14621       if (!cgraph_get_node (*tp))
14622         return *tp;
14623     }
14624   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14625     return *tp;
14626
14627   return NULL_TREE;
14628 }
14629
14630 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14631    for use in a later add_const_value_attribute call.  */
14632
14633 static rtx
14634 rtl_for_decl_init (tree init, tree type)
14635 {
14636   rtx rtl = NULL_RTX;
14637
14638   STRIP_NOPS (init);
14639
14640   /* If a variable is initialized with a string constant without embedded
14641      zeros, build CONST_STRING.  */
14642   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14643     {
14644       tree enttype = TREE_TYPE (type);
14645       tree domain = TYPE_DOMAIN (type);
14646       enum machine_mode mode = TYPE_MODE (enttype);
14647
14648       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14649           && domain
14650           && integer_zerop (TYPE_MIN_VALUE (domain))
14651           && compare_tree_int (TYPE_MAX_VALUE (domain),
14652                                TREE_STRING_LENGTH (init) - 1) == 0
14653           && ((size_t) TREE_STRING_LENGTH (init)
14654               == strlen (TREE_STRING_POINTER (init)) + 1))
14655         {
14656           rtl = gen_rtx_CONST_STRING (VOIDmode,
14657                                       ggc_strdup (TREE_STRING_POINTER (init)));
14658           rtl = gen_rtx_MEM (BLKmode, rtl);
14659           MEM_READONLY_P (rtl) = 1;
14660         }
14661     }
14662   /* Other aggregates, and complex values, could be represented using
14663      CONCAT: FIXME!  */
14664   else if (AGGREGATE_TYPE_P (type)
14665            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14666                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14667            || TREE_CODE (type) == COMPLEX_TYPE)
14668     ;
14669   /* Vectors only work if their mode is supported by the target.
14670      FIXME: generic vectors ought to work too.  */
14671   else if (TREE_CODE (type) == VECTOR_TYPE
14672            && !VECTOR_MODE_P (TYPE_MODE (type)))
14673     ;
14674   /* If the initializer is something that we know will expand into an
14675      immediate RTL constant, expand it now.  We must be careful not to
14676      reference variables which won't be output.  */
14677   else if (initializer_constant_valid_p (init, type)
14678            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14679     {
14680       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14681          possible.  */
14682       if (TREE_CODE (type) == VECTOR_TYPE)
14683         switch (TREE_CODE (init))
14684           {
14685           case VECTOR_CST:
14686             break;
14687           case CONSTRUCTOR:
14688             if (TREE_CONSTANT (init))
14689               {
14690                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14691                 bool constant_p = true;
14692                 tree value;
14693                 unsigned HOST_WIDE_INT ix;
14694
14695                 /* Even when ctor is constant, it might contain non-*_CST
14696                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14697                    belong into VECTOR_CST nodes.  */
14698                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14699                   if (!CONSTANT_CLASS_P (value))
14700                     {
14701                       constant_p = false;
14702                       break;
14703                     }
14704
14705                 if (constant_p)
14706                   {
14707                     init = build_vector_from_ctor (type, elts);
14708                     break;
14709                   }
14710               }
14711             /* FALLTHRU */
14712
14713           default:
14714             return NULL;
14715           }
14716
14717       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14718
14719       /* If expand_expr returns a MEM, it wasn't immediate.  */
14720       gcc_assert (!rtl || !MEM_P (rtl));
14721     }
14722
14723   return rtl;
14724 }
14725
14726 /* Generate RTL for the variable DECL to represent its location.  */
14727
14728 static rtx
14729 rtl_for_decl_location (tree decl)
14730 {
14731   rtx rtl;
14732
14733   /* Here we have to decide where we are going to say the parameter "lives"
14734      (as far as the debugger is concerned).  We only have a couple of
14735      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14736
14737      DECL_RTL normally indicates where the parameter lives during most of the
14738      activation of the function.  If optimization is enabled however, this
14739      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14740      that the parameter doesn't really live anywhere (as far as the code
14741      generation parts of GCC are concerned) during most of the function's
14742      activation.  That will happen (for example) if the parameter is never
14743      referenced within the function.
14744
14745      We could just generate a location descriptor here for all non-NULL
14746      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14747      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14748      where DECL_RTL is NULL or is a pseudo-reg.
14749
14750      Note however that we can only get away with using DECL_INCOMING_RTL as
14751      a backup substitute for DECL_RTL in certain limited cases.  In cases
14752      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14753      we can be sure that the parameter was passed using the same type as it is
14754      declared to have within the function, and that its DECL_INCOMING_RTL
14755      points us to a place where a value of that type is passed.
14756
14757      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14758      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14759      because in these cases DECL_INCOMING_RTL points us to a value of some
14760      type which is *different* from the type of the parameter itself.  Thus,
14761      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14762      such cases, the debugger would end up (for example) trying to fetch a
14763      `float' from a place which actually contains the first part of a
14764      `double'.  That would lead to really incorrect and confusing
14765      output at debug-time.
14766
14767      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14768      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14769      are a couple of exceptions however.  On little-endian machines we can
14770      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14771      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14772      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14773      when (on a little-endian machine) a non-prototyped function has a
14774      parameter declared to be of type `short' or `char'.  In such cases,
14775      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14776      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14777      passed `int' value.  If the debugger then uses that address to fetch
14778      a `short' or a `char' (on a little-endian machine) the result will be
14779      the correct data, so we allow for such exceptional cases below.
14780
14781      Note that our goal here is to describe the place where the given formal
14782      parameter lives during most of the function's activation (i.e. between the
14783      end of the prologue and the start of the epilogue).  We'll do that as best
14784      as we can. Note however that if the given formal parameter is modified
14785      sometime during the execution of the function, then a stack backtrace (at
14786      debug-time) will show the function as having been called with the *new*
14787      value rather than the value which was originally passed in.  This happens
14788      rarely enough that it is not a major problem, but it *is* a problem, and
14789      I'd like to fix it.
14790
14791      A future version of dwarf2out.c may generate two additional attributes for
14792      any given DW_TAG_formal_parameter DIE which will describe the "passed
14793      type" and the "passed location" for the given formal parameter in addition
14794      to the attributes we now generate to indicate the "declared type" and the
14795      "active location" for each parameter.  This additional set of attributes
14796      could be used by debuggers for stack backtraces. Separately, note that
14797      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14798      This happens (for example) for inlined-instances of inline function formal
14799      parameters which are never referenced.  This really shouldn't be
14800      happening.  All PARM_DECL nodes should get valid non-NULL
14801      DECL_INCOMING_RTL values.  FIXME.  */
14802
14803   /* Use DECL_RTL as the "location" unless we find something better.  */
14804   rtl = DECL_RTL_IF_SET (decl);
14805
14806   /* When generating abstract instances, ignore everything except
14807      constants, symbols living in memory, and symbols living in
14808      fixed registers.  */
14809   if (! reload_completed)
14810     {
14811       if (rtl
14812           && (CONSTANT_P (rtl)
14813               || (MEM_P (rtl)
14814                   && CONSTANT_P (XEXP (rtl, 0)))
14815               || (REG_P (rtl)
14816                   && TREE_CODE (decl) == VAR_DECL
14817                   && TREE_STATIC (decl))))
14818         {
14819           rtl = targetm.delegitimize_address (rtl);
14820           return rtl;
14821         }
14822       rtl = NULL_RTX;
14823     }
14824   else if (TREE_CODE (decl) == PARM_DECL)
14825     {
14826       if (rtl == NULL_RTX
14827           || is_pseudo_reg (rtl)
14828           || (MEM_P (rtl)
14829               && is_pseudo_reg (XEXP (rtl, 0))
14830               && DECL_INCOMING_RTL (decl)
14831               && MEM_P (DECL_INCOMING_RTL (decl))
14832               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14833         {
14834           tree declared_type = TREE_TYPE (decl);
14835           tree passed_type = DECL_ARG_TYPE (decl);
14836           enum machine_mode dmode = TYPE_MODE (declared_type);
14837           enum machine_mode pmode = TYPE_MODE (passed_type);
14838
14839           /* This decl represents a formal parameter which was optimized out.
14840              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14841              all cases where (rtl == NULL_RTX) just below.  */
14842           if (dmode == pmode)
14843             rtl = DECL_INCOMING_RTL (decl);
14844           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14845                    && SCALAR_INT_MODE_P (dmode)
14846                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14847                    && DECL_INCOMING_RTL (decl))
14848             {
14849               rtx inc = DECL_INCOMING_RTL (decl);
14850               if (REG_P (inc))
14851                 rtl = inc;
14852               else if (MEM_P (inc))
14853                 {
14854                   if (BYTES_BIG_ENDIAN)
14855                     rtl = adjust_address_nv (inc, dmode,
14856                                              GET_MODE_SIZE (pmode)
14857                                              - GET_MODE_SIZE (dmode));
14858                   else
14859                     rtl = inc;
14860                 }
14861             }
14862         }
14863
14864       /* If the parm was passed in registers, but lives on the stack, then
14865          make a big endian correction if the mode of the type of the
14866          parameter is not the same as the mode of the rtl.  */
14867       /* ??? This is the same series of checks that are made in dbxout.c before
14868          we reach the big endian correction code there.  It isn't clear if all
14869          of these checks are necessary here, but keeping them all is the safe
14870          thing to do.  */
14871       else if (MEM_P (rtl)
14872                && XEXP (rtl, 0) != const0_rtx
14873                && ! CONSTANT_P (XEXP (rtl, 0))
14874                /* Not passed in memory.  */
14875                && !MEM_P (DECL_INCOMING_RTL (decl))
14876                /* Not passed by invisible reference.  */
14877                && (!REG_P (XEXP (rtl, 0))
14878                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14879                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14880 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14881                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14882 #endif
14883                      )
14884                /* Big endian correction check.  */
14885                && BYTES_BIG_ENDIAN
14886                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14887                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14888                    < UNITS_PER_WORD))
14889         {
14890           int offset = (UNITS_PER_WORD
14891                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14892
14893           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14894                              plus_constant (XEXP (rtl, 0), offset));
14895         }
14896     }
14897   else if (TREE_CODE (decl) == VAR_DECL
14898            && rtl
14899            && MEM_P (rtl)
14900            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14901            && BYTES_BIG_ENDIAN)
14902     {
14903       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14904       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14905
14906       /* If a variable is declared "register" yet is smaller than
14907          a register, then if we store the variable to memory, it
14908          looks like we're storing a register-sized value, when in
14909          fact we are not.  We need to adjust the offset of the
14910          storage location to reflect the actual value's bytes,
14911          else gdb will not be able to display it.  */
14912       if (rsize > dsize)
14913         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14914                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14915     }
14916
14917   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14918      and will have been substituted directly into all expressions that use it.
14919      C does not have such a concept, but C++ and other languages do.  */
14920   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14921     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14922
14923   if (rtl)
14924     rtl = targetm.delegitimize_address (rtl);
14925
14926   /* If we don't look past the constant pool, we risk emitting a
14927      reference to a constant pool entry that isn't referenced from
14928      code, and thus is not emitted.  */
14929   if (rtl)
14930     rtl = avoid_constant_pool_reference (rtl);
14931
14932   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14933      in the current CU, resolve_addr will remove the expression referencing
14934      it.  */
14935   if (rtl == NULL_RTX
14936       && TREE_CODE (decl) == VAR_DECL
14937       && !DECL_EXTERNAL (decl)
14938       && TREE_STATIC (decl)
14939       && DECL_NAME (decl)
14940       && !DECL_HARD_REGISTER (decl)
14941       && DECL_MODE (decl) != VOIDmode)
14942     {
14943       rtl = make_decl_rtl_for_debug (decl);
14944       if (!MEM_P (rtl)
14945           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14946           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14947         rtl = NULL_RTX;
14948     }
14949
14950   return rtl;
14951 }
14952
14953 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14954    returned.  If so, the decl for the COMMON block is returned, and the
14955    value is the offset into the common block for the symbol.  */
14956
14957 static tree
14958 fortran_common (tree decl, HOST_WIDE_INT *value)
14959 {
14960   tree val_expr, cvar;
14961   enum machine_mode mode;
14962   HOST_WIDE_INT bitsize, bitpos;
14963   tree offset;
14964   int volatilep = 0, unsignedp = 0;
14965
14966   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14967      it does not have a value (the offset into the common area), or if it
14968      is thread local (as opposed to global) then it isn't common, and shouldn't
14969      be handled as such.  */
14970   if (TREE_CODE (decl) != VAR_DECL
14971       || !TREE_STATIC (decl)
14972       || !DECL_HAS_VALUE_EXPR_P (decl)
14973       || !is_fortran ())
14974     return NULL_TREE;
14975
14976   val_expr = DECL_VALUE_EXPR (decl);
14977   if (TREE_CODE (val_expr) != COMPONENT_REF)
14978     return NULL_TREE;
14979
14980   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14981                               &mode, &unsignedp, &volatilep, true);
14982
14983   if (cvar == NULL_TREE
14984       || TREE_CODE (cvar) != VAR_DECL
14985       || DECL_ARTIFICIAL (cvar)
14986       || !TREE_PUBLIC (cvar))
14987     return NULL_TREE;
14988
14989   *value = 0;
14990   if (offset != NULL)
14991     {
14992       if (!host_integerp (offset, 0))
14993         return NULL_TREE;
14994       *value = tree_low_cst (offset, 0);
14995     }
14996   if (bitpos != 0)
14997     *value += bitpos / BITS_PER_UNIT;
14998
14999   return cvar;
15000 }
15001
15002 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15003    data attribute for a variable or a parameter.  We generate the
15004    DW_AT_const_value attribute only in those cases where the given variable
15005    or parameter does not have a true "location" either in memory or in a
15006    register.  This can happen (for example) when a constant is passed as an
15007    actual argument in a call to an inline function.  (It's possible that
15008    these things can crop up in other ways also.)  Note that one type of
15009    constant value which can be passed into an inlined function is a constant
15010    pointer.  This can happen for example if an actual argument in an inlined
15011    function call evaluates to a compile-time constant address.
15012
15013    CACHE_P is true if it is worth caching the location list for DECL,
15014    so that future calls can reuse it rather than regenerate it from scratch.
15015    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15016    since we will need to refer to them each time the function is inlined.  */
15017
15018 static bool
15019 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15020                                        enum dwarf_attribute attr)
15021 {
15022   rtx rtl;
15023   dw_loc_list_ref list;
15024   var_loc_list *loc_list;
15025   cached_dw_loc_list *cache;
15026   void **slot;
15027
15028   if (TREE_CODE (decl) == ERROR_MARK)
15029     return false;
15030
15031   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15032               || TREE_CODE (decl) == RESULT_DECL);
15033
15034   /* Try to get some constant RTL for this decl, and use that as the value of
15035      the location.  */
15036
15037   rtl = rtl_for_decl_location (decl);
15038   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15039       && add_const_value_attribute (die, rtl))
15040     return true;
15041
15042   /* See if we have single element location list that is equivalent to
15043      a constant value.  That way we are better to use add_const_value_attribute
15044      rather than expanding constant value equivalent.  */
15045   loc_list = lookup_decl_loc (decl);
15046   if (loc_list
15047       && loc_list->first
15048       && loc_list->first->next == NULL
15049       && NOTE_P (loc_list->first->loc)
15050       && NOTE_VAR_LOCATION (loc_list->first->loc)
15051       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15052     {
15053       struct var_loc_node *node;
15054
15055       node = loc_list->first;
15056       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15057       if (GET_CODE (rtl) == EXPR_LIST)
15058         rtl = XEXP (rtl, 0);
15059       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15060           && add_const_value_attribute (die, rtl))
15061          return true;
15062     }
15063   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15064      list several times.  See if we've already cached the contents.  */
15065   list = NULL;
15066   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15067     cache_p = false;
15068   if (cache_p)
15069     {
15070       cache = (cached_dw_loc_list *)
15071         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15072       if (cache)
15073         list = cache->loc_list;
15074     }
15075   if (list == NULL)
15076     {
15077       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15078       /* It is usually worth caching this result if the decl is from
15079          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15080       if (cache_p && list && list->dw_loc_next)
15081         {
15082           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15083                                            DECL_UID (decl), INSERT);
15084           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15085           cache->decl_id = DECL_UID (decl);
15086           cache->loc_list = list;
15087           *slot = cache;
15088         }
15089     }
15090   if (list)
15091     {
15092       add_AT_location_description (die, attr, list);
15093       return true;
15094     }
15095   /* None of that worked, so it must not really have a location;
15096      try adding a constant value attribute from the DECL_INITIAL.  */
15097   return tree_add_const_value_attribute_for_decl (die, decl);
15098 }
15099
15100 /* Add VARIABLE and DIE into deferred locations list.  */
15101
15102 static void
15103 defer_location (tree variable, dw_die_ref die)
15104 {
15105   deferred_locations entry;
15106   entry.variable = variable;
15107   entry.die = die;
15108   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15109 }
15110
15111 /* Helper function for tree_add_const_value_attribute.  Natively encode
15112    initializer INIT into an array.  Return true if successful.  */
15113
15114 static bool
15115 native_encode_initializer (tree init, unsigned char *array, int size)
15116 {
15117   tree type;
15118
15119   if (init == NULL_TREE)
15120     return false;
15121
15122   STRIP_NOPS (init);
15123   switch (TREE_CODE (init))
15124     {
15125     case STRING_CST:
15126       type = TREE_TYPE (init);
15127       if (TREE_CODE (type) == ARRAY_TYPE)
15128         {
15129           tree enttype = TREE_TYPE (type);
15130           enum machine_mode mode = TYPE_MODE (enttype);
15131
15132           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15133             return false;
15134           if (int_size_in_bytes (type) != size)
15135             return false;
15136           if (size > TREE_STRING_LENGTH (init))
15137             {
15138               memcpy (array, TREE_STRING_POINTER (init),
15139                       TREE_STRING_LENGTH (init));
15140               memset (array + TREE_STRING_LENGTH (init),
15141                       '\0', size - TREE_STRING_LENGTH (init));
15142             }
15143           else
15144             memcpy (array, TREE_STRING_POINTER (init), size);
15145           return true;
15146         }
15147       return false;
15148     case CONSTRUCTOR:
15149       type = TREE_TYPE (init);
15150       if (int_size_in_bytes (type) != size)
15151         return false;
15152       if (TREE_CODE (type) == ARRAY_TYPE)
15153         {
15154           HOST_WIDE_INT min_index;
15155           unsigned HOST_WIDE_INT cnt;
15156           int curpos = 0, fieldsize;
15157           constructor_elt *ce;
15158
15159           if (TYPE_DOMAIN (type) == NULL_TREE
15160               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15161             return false;
15162
15163           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15164           if (fieldsize <= 0)
15165             return false;
15166
15167           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15168           memset (array, '\0', size);
15169           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15170             {
15171               tree val = ce->value;
15172               tree index = ce->index;
15173               int pos = curpos;
15174               if (index && TREE_CODE (index) == RANGE_EXPR)
15175                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15176                       * fieldsize;
15177               else if (index)
15178                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15179
15180               if (val)
15181                 {
15182                   STRIP_NOPS (val);
15183                   if (!native_encode_initializer (val, array + pos, fieldsize))
15184                     return false;
15185                 }
15186               curpos = pos + fieldsize;
15187               if (index && TREE_CODE (index) == RANGE_EXPR)
15188                 {
15189                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15190                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15191                   while (count-- > 0)
15192                     {
15193                       if (val)
15194                         memcpy (array + curpos, array + pos, fieldsize);
15195                       curpos += fieldsize;
15196                     }
15197                 }
15198               gcc_assert (curpos <= size);
15199             }
15200           return true;
15201         }
15202       else if (TREE_CODE (type) == RECORD_TYPE
15203                || TREE_CODE (type) == UNION_TYPE)
15204         {
15205           tree field = NULL_TREE;
15206           unsigned HOST_WIDE_INT cnt;
15207           constructor_elt *ce;
15208
15209           if (int_size_in_bytes (type) != size)
15210             return false;
15211
15212           if (TREE_CODE (type) == RECORD_TYPE)
15213             field = TYPE_FIELDS (type);
15214
15215           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15216             {
15217               tree val = ce->value;
15218               int pos, fieldsize;
15219
15220               if (ce->index != 0)
15221                 field = ce->index;
15222
15223               if (val)
15224                 STRIP_NOPS (val);
15225
15226               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15227                 return false;
15228
15229               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15230                   && TYPE_DOMAIN (TREE_TYPE (field))
15231                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15232                 return false;
15233               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15234                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15235                 return false;
15236               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15237               pos = int_byte_position (field);
15238               gcc_assert (pos + fieldsize <= size);
15239               if (val
15240                   && !native_encode_initializer (val, array + pos, fieldsize))
15241                 return false;
15242             }
15243           return true;
15244         }
15245       return false;
15246     case VIEW_CONVERT_EXPR:
15247     case NON_LVALUE_EXPR:
15248       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15249     default:
15250       return native_encode_expr (init, array, size) == size;
15251     }
15252 }
15253
15254 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15255    attribute is the const value T.  */
15256
15257 static bool
15258 tree_add_const_value_attribute (dw_die_ref die, tree t)
15259 {
15260   tree init;
15261   tree type = TREE_TYPE (t);
15262   rtx rtl;
15263
15264   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15265     return false;
15266
15267   init = t;
15268   gcc_assert (!DECL_P (init));
15269
15270   rtl = rtl_for_decl_init (init, type);
15271   if (rtl)
15272     return add_const_value_attribute (die, rtl);
15273   /* If the host and target are sane, try harder.  */
15274   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15275            && initializer_constant_valid_p (init, type))
15276     {
15277       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15278       if (size > 0 && (int) size == size)
15279         {
15280           unsigned char *array = (unsigned char *)
15281             ggc_alloc_cleared_atomic (size);
15282
15283           if (native_encode_initializer (init, array, size))
15284             {
15285               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15286               return true;
15287             }
15288         }
15289     }
15290   return false;
15291 }
15292
15293 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15294    attribute is the const value of T, where T is an integral constant
15295    variable with static storage duration
15296    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15297
15298 static bool
15299 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15300 {
15301
15302   if (!decl
15303       || (TREE_CODE (decl) != VAR_DECL
15304           && TREE_CODE (decl) != CONST_DECL)
15305       || (TREE_CODE (decl) == VAR_DECL
15306           && !TREE_STATIC (decl)))
15307     return false;
15308
15309     if (TREE_READONLY (decl)
15310         && ! TREE_THIS_VOLATILE (decl)
15311         && DECL_INITIAL (decl))
15312       /* OK */;
15313     else
15314       return false;
15315
15316   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15317   if (get_AT (var_die, DW_AT_const_value))
15318     return false;
15319
15320   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15321 }
15322
15323 /* Convert the CFI instructions for the current function into a
15324    location list.  This is used for DW_AT_frame_base when we targeting
15325    a dwarf2 consumer that does not support the dwarf3
15326    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15327    expressions.  */
15328
15329 static dw_loc_list_ref
15330 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15331 {
15332   int ix;
15333   dw_fde_ref fde;
15334   dw_loc_list_ref list, *list_tail;
15335   dw_cfi_ref cfi;
15336   dw_cfa_location last_cfa, next_cfa;
15337   const char *start_label, *last_label, *section;
15338   dw_cfa_location remember;
15339
15340   fde = cfun->fde;
15341   gcc_assert (fde != NULL);
15342
15343   section = secname_for_decl (current_function_decl);
15344   list_tail = &list;
15345   list = NULL;
15346
15347   memset (&next_cfa, 0, sizeof (next_cfa));
15348   next_cfa.reg = INVALID_REGNUM;
15349   remember = next_cfa;
15350
15351   start_label = fde->dw_fde_begin;
15352
15353   /* ??? Bald assumption that the CIE opcode list does not contain
15354      advance opcodes.  */
15355   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15356     lookup_cfa_1 (cfi, &next_cfa, &remember);
15357
15358   last_cfa = next_cfa;
15359   last_label = start_label;
15360
15361   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15362     {
15363       /* If the first partition contained no CFI adjustments, the
15364          CIE opcodes apply to the whole first partition.  */
15365       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15366                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15367       list_tail =&(*list_tail)->dw_loc_next;
15368       start_label = last_label = fde->dw_fde_second_begin;
15369     }
15370
15371   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15372     {
15373       switch (cfi->dw_cfi_opc)
15374         {
15375         case DW_CFA_set_loc:
15376         case DW_CFA_advance_loc1:
15377         case DW_CFA_advance_loc2:
15378         case DW_CFA_advance_loc4:
15379           if (!cfa_equal_p (&last_cfa, &next_cfa))
15380             {
15381               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15382                                          start_label, last_label, section);
15383
15384               list_tail = &(*list_tail)->dw_loc_next;
15385               last_cfa = next_cfa;
15386               start_label = last_label;
15387             }
15388           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15389           break;
15390
15391         case DW_CFA_advance_loc:
15392           /* The encoding is complex enough that we should never emit this.  */
15393           gcc_unreachable ();
15394
15395         default:
15396           lookup_cfa_1 (cfi, &next_cfa, &remember);
15397           break;
15398         }
15399       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15400         {
15401           if (!cfa_equal_p (&last_cfa, &next_cfa))
15402             {
15403               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15404                                          start_label, last_label, section);
15405
15406               list_tail = &(*list_tail)->dw_loc_next;
15407               last_cfa = next_cfa;
15408               start_label = last_label;
15409             }
15410           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15411                                      start_label, fde->dw_fde_end, section);
15412           list_tail = &(*list_tail)->dw_loc_next;
15413           start_label = last_label = fde->dw_fde_second_begin;
15414         }
15415     }
15416
15417   if (!cfa_equal_p (&last_cfa, &next_cfa))
15418     {
15419       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15420                                  start_label, last_label, section);
15421       list_tail = &(*list_tail)->dw_loc_next;
15422       start_label = last_label;
15423     }
15424
15425   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15426                              start_label,
15427                              fde->dw_fde_second_begin
15428                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15429                              section);
15430
15431   if (list && list->dw_loc_next)
15432     gen_llsym (list);
15433
15434   return list;
15435 }
15436
15437 /* Compute a displacement from the "steady-state frame pointer" to the
15438    frame base (often the same as the CFA), and store it in
15439    frame_pointer_fb_offset.  OFFSET is added to the displacement
15440    before the latter is negated.  */
15441
15442 static void
15443 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15444 {
15445   rtx reg, elim;
15446
15447 #ifdef FRAME_POINTER_CFA_OFFSET
15448   reg = frame_pointer_rtx;
15449   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15450 #else
15451   reg = arg_pointer_rtx;
15452   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15453 #endif
15454
15455   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15456   if (GET_CODE (elim) == PLUS)
15457     {
15458       offset += INTVAL (XEXP (elim, 1));
15459       elim = XEXP (elim, 0);
15460     }
15461
15462   frame_pointer_fb_offset = -offset;
15463
15464   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15465      in which to eliminate.  This is because it's stack pointer isn't 
15466      directly accessible as a register within the ISA.  To work around
15467      this, assume that while we cannot provide a proper value for
15468      frame_pointer_fb_offset, we won't need one either.  */
15469   frame_pointer_fb_offset_valid
15470     = ((SUPPORTS_STACK_ALIGNMENT
15471         && (elim == hard_frame_pointer_rtx
15472             || elim == stack_pointer_rtx))
15473        || elim == (frame_pointer_needed
15474                    ? hard_frame_pointer_rtx
15475                    : stack_pointer_rtx));
15476 }
15477
15478 /* Generate a DW_AT_name attribute given some string value to be included as
15479    the value of the attribute.  */
15480
15481 static void
15482 add_name_attribute (dw_die_ref die, const char *name_string)
15483 {
15484   if (name_string != NULL && *name_string != 0)
15485     {
15486       if (demangle_name_func)
15487         name_string = (*demangle_name_func) (name_string);
15488
15489       add_AT_string (die, DW_AT_name, name_string);
15490     }
15491 }
15492
15493 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15494    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15495    of TYPE accordingly.
15496
15497    ??? This is a temporary measure until after we're able to generate
15498    regular DWARF for the complex Ada type system.  */
15499
15500 static void 
15501 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15502                                      dw_die_ref context_die)
15503 {
15504   tree dtype;
15505   dw_die_ref dtype_die;
15506
15507   if (!lang_hooks.types.descriptive_type)
15508     return;
15509
15510   dtype = lang_hooks.types.descriptive_type (type);
15511   if (!dtype)
15512     return;
15513
15514   dtype_die = lookup_type_die (dtype);
15515   if (!dtype_die)
15516     {
15517       gen_type_die (dtype, context_die);
15518       dtype_die = lookup_type_die (dtype);
15519       gcc_assert (dtype_die);
15520     }
15521
15522   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15523 }
15524
15525 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15526
15527 static void
15528 add_comp_dir_attribute (dw_die_ref die)
15529 {
15530   const char *wd = get_src_pwd ();
15531   char *wd1;
15532
15533   if (wd == NULL)
15534     return;
15535
15536   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15537     {
15538       int wdlen;
15539
15540       wdlen = strlen (wd);
15541       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15542       strcpy (wd1, wd);
15543       wd1 [wdlen] = DIR_SEPARATOR;
15544       wd1 [wdlen + 1] = 0;
15545       wd = wd1;
15546     }
15547
15548     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15549 }
15550
15551 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15552    default.  */
15553
15554 static int
15555 lower_bound_default (void)
15556 {
15557   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15558     {
15559     case DW_LANG_C:
15560     case DW_LANG_C89:
15561     case DW_LANG_C99:
15562     case DW_LANG_C_plus_plus:
15563     case DW_LANG_ObjC:
15564     case DW_LANG_ObjC_plus_plus:
15565     case DW_LANG_Java:
15566       return 0;
15567     case DW_LANG_Fortran77:
15568     case DW_LANG_Fortran90:
15569     case DW_LANG_Fortran95:
15570       return 1;
15571     case DW_LANG_UPC:
15572     case DW_LANG_D:
15573     case DW_LANG_Python:
15574       return dwarf_version >= 4 ? 0 : -1;
15575     case DW_LANG_Ada95:
15576     case DW_LANG_Ada83:
15577     case DW_LANG_Cobol74:
15578     case DW_LANG_Cobol85:
15579     case DW_LANG_Pascal83:
15580     case DW_LANG_Modula2:
15581     case DW_LANG_PLI:
15582       return dwarf_version >= 4 ? 1 : -1;
15583     default:
15584       return -1;
15585     }
15586 }
15587
15588 /* Given a tree node describing an array bound (either lower or upper) output
15589    a representation for that bound.  */
15590
15591 static void
15592 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15593 {
15594   switch (TREE_CODE (bound))
15595     {
15596     case ERROR_MARK:
15597       return;
15598
15599     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15600     case INTEGER_CST:
15601       {
15602         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15603         int dflt;
15604
15605         /* Use the default if possible.  */
15606         if (bound_attr == DW_AT_lower_bound
15607             && host_integerp (bound, 0)
15608             && (dflt = lower_bound_default ()) != -1
15609             && tree_low_cst (bound, 0) == dflt)
15610           ;
15611
15612         /* Otherwise represent the bound as an unsigned value with the
15613            precision of its type.  The precision and signedness of the
15614            type will be necessary to re-interpret it unambiguously.  */
15615         else if (prec < HOST_BITS_PER_WIDE_INT)
15616           {
15617             unsigned HOST_WIDE_INT mask
15618               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15619             add_AT_unsigned (subrange_die, bound_attr,
15620                              TREE_INT_CST_LOW (bound) & mask);
15621           }
15622         else if (prec == HOST_BITS_PER_WIDE_INT
15623                  || TREE_INT_CST_HIGH (bound) == 0)
15624           add_AT_unsigned (subrange_die, bound_attr,
15625                            TREE_INT_CST_LOW (bound));
15626         else
15627           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15628                          TREE_INT_CST_LOW (bound));
15629       }
15630       break;
15631
15632     CASE_CONVERT:
15633     case VIEW_CONVERT_EXPR:
15634       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15635       break;
15636
15637     case SAVE_EXPR:
15638       break;
15639
15640     case VAR_DECL:
15641     case PARM_DECL:
15642     case RESULT_DECL:
15643       {
15644         dw_die_ref decl_die = lookup_decl_die (bound);
15645
15646         /* ??? Can this happen, or should the variable have been bound
15647            first?  Probably it can, since I imagine that we try to create
15648            the types of parameters in the order in which they exist in
15649            the list, and won't have created a forward reference to a
15650            later parameter.  */
15651         if (decl_die != NULL)
15652           {
15653             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15654             break;
15655           }
15656       }
15657       /* FALLTHRU */
15658
15659     default:
15660       {
15661         /* Otherwise try to create a stack operation procedure to
15662            evaluate the value of the array bound.  */
15663
15664         dw_die_ref ctx, decl_die;
15665         dw_loc_list_ref list;
15666
15667         list = loc_list_from_tree (bound, 2);
15668         if (list == NULL || single_element_loc_list_p (list))
15669           {
15670             /* If DW_AT_*bound is not a reference nor constant, it is
15671                a DWARF expression rather than location description.
15672                For that loc_list_from_tree (bound, 0) is needed.
15673                If that fails to give a single element list,
15674                fall back to outputting this as a reference anyway.  */
15675             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15676             if (list2 && single_element_loc_list_p (list2))
15677               {
15678                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15679                 break;
15680               }
15681           }
15682         if (list == NULL)
15683           break;
15684
15685         if (current_function_decl == 0)
15686           ctx = comp_unit_die ();
15687         else
15688           ctx = lookup_decl_die (current_function_decl);
15689
15690         decl_die = new_die (DW_TAG_variable, ctx, bound);
15691         add_AT_flag (decl_die, DW_AT_artificial, 1);
15692         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15693         add_AT_location_description (decl_die, DW_AT_location, list);
15694         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15695         break;
15696       }
15697     }
15698 }
15699
15700 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15701    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15702    Note that the block of subscript information for an array type also
15703    includes information about the element type of the given array type.  */
15704
15705 static void
15706 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15707 {
15708   unsigned dimension_number;
15709   tree lower, upper;
15710   dw_die_ref subrange_die;
15711
15712   for (dimension_number = 0;
15713        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15714        type = TREE_TYPE (type), dimension_number++)
15715     {
15716       tree domain = TYPE_DOMAIN (type);
15717
15718       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15719         break;
15720
15721       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15722          and (in GNU C only) variable bounds.  Handle all three forms
15723          here.  */
15724       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15725       if (domain)
15726         {
15727           /* We have an array type with specified bounds.  */
15728           lower = TYPE_MIN_VALUE (domain);
15729           upper = TYPE_MAX_VALUE (domain);
15730
15731           /* Define the index type.  */
15732           if (TREE_TYPE (domain))
15733             {
15734               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15735                  TREE_TYPE field.  We can't emit debug info for this
15736                  because it is an unnamed integral type.  */
15737               if (TREE_CODE (domain) == INTEGER_TYPE
15738                   && TYPE_NAME (domain) == NULL_TREE
15739                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15740                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15741                 ;
15742               else
15743                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15744                                     type_die);
15745             }
15746
15747           /* ??? If upper is NULL, the array has unspecified length,
15748              but it does have a lower bound.  This happens with Fortran
15749                dimension arr(N:*)
15750              Since the debugger is definitely going to need to know N
15751              to produce useful results, go ahead and output the lower
15752              bound solo, and hope the debugger can cope.  */
15753
15754           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15755           if (upper)
15756             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15757         }
15758
15759       /* Otherwise we have an array type with an unspecified length.  The
15760          DWARF-2 spec does not say how to handle this; let's just leave out the
15761          bounds.  */
15762     }
15763 }
15764
15765 static void
15766 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15767 {
15768   unsigned size;
15769
15770   switch (TREE_CODE (tree_node))
15771     {
15772     case ERROR_MARK:
15773       size = 0;
15774       break;
15775     case ENUMERAL_TYPE:
15776     case RECORD_TYPE:
15777     case UNION_TYPE:
15778     case QUAL_UNION_TYPE:
15779       size = int_size_in_bytes (tree_node);
15780       break;
15781     case FIELD_DECL:
15782       /* For a data member of a struct or union, the DW_AT_byte_size is
15783          generally given as the number of bytes normally allocated for an
15784          object of the *declared* type of the member itself.  This is true
15785          even for bit-fields.  */
15786       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15787       break;
15788     default:
15789       gcc_unreachable ();
15790     }
15791
15792   /* Note that `size' might be -1 when we get to this point.  If it is, that
15793      indicates that the byte size of the entity in question is variable.  We
15794      have no good way of expressing this fact in Dwarf at the present time,
15795      so just let the -1 pass on through.  */
15796   add_AT_unsigned (die, DW_AT_byte_size, size);
15797 }
15798
15799 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15800    which specifies the distance in bits from the highest order bit of the
15801    "containing object" for the bit-field to the highest order bit of the
15802    bit-field itself.
15803
15804    For any given bit-field, the "containing object" is a hypothetical object
15805    (of some integral or enum type) within which the given bit-field lives.  The
15806    type of this hypothetical "containing object" is always the same as the
15807    declared type of the individual bit-field itself.  The determination of the
15808    exact location of the "containing object" for a bit-field is rather
15809    complicated.  It's handled by the `field_byte_offset' function (above).
15810
15811    Note that it is the size (in bytes) of the hypothetical "containing object"
15812    which will be given in the DW_AT_byte_size attribute for this bit-field.
15813    (See `byte_size_attribute' above).  */
15814
15815 static inline void
15816 add_bit_offset_attribute (dw_die_ref die, tree decl)
15817 {
15818   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15819   tree type = DECL_BIT_FIELD_TYPE (decl);
15820   HOST_WIDE_INT bitpos_int;
15821   HOST_WIDE_INT highest_order_object_bit_offset;
15822   HOST_WIDE_INT highest_order_field_bit_offset;
15823   HOST_WIDE_INT bit_offset;
15824
15825   /* Must be a field and a bit field.  */
15826   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15827
15828   /* We can't yet handle bit-fields whose offsets are variable, so if we
15829      encounter such things, just return without generating any attribute
15830      whatsoever.  Likewise for variable or too large size.  */
15831   if (! host_integerp (bit_position (decl), 0)
15832       || ! host_integerp (DECL_SIZE (decl), 1))
15833     return;
15834
15835   bitpos_int = int_bit_position (decl);
15836
15837   /* Note that the bit offset is always the distance (in bits) from the
15838      highest-order bit of the "containing object" to the highest-order bit of
15839      the bit-field itself.  Since the "high-order end" of any object or field
15840      is different on big-endian and little-endian machines, the computation
15841      below must take account of these differences.  */
15842   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15843   highest_order_field_bit_offset = bitpos_int;
15844
15845   if (! BYTES_BIG_ENDIAN)
15846     {
15847       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15848       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15849     }
15850
15851   bit_offset
15852     = (! BYTES_BIG_ENDIAN
15853        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15854        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15855
15856   if (bit_offset < 0)
15857     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15858   else
15859     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15860 }
15861
15862 /* For a FIELD_DECL node which represents a bit field, output an attribute
15863    which specifies the length in bits of the given field.  */
15864
15865 static inline void
15866 add_bit_size_attribute (dw_die_ref die, tree decl)
15867 {
15868   /* Must be a field and a bit field.  */
15869   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15870               && DECL_BIT_FIELD_TYPE (decl));
15871
15872   if (host_integerp (DECL_SIZE (decl), 1))
15873     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15874 }
15875
15876 /* If the compiled language is ANSI C, then add a 'prototyped'
15877    attribute, if arg types are given for the parameters of a function.  */
15878
15879 static inline void
15880 add_prototyped_attribute (dw_die_ref die, tree func_type)
15881 {
15882   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15883       && prototype_p (func_type))
15884     add_AT_flag (die, DW_AT_prototyped, 1);
15885 }
15886
15887 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15888    by looking in either the type declaration or object declaration
15889    equate table.  */
15890
15891 static inline dw_die_ref
15892 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15893 {
15894   dw_die_ref origin_die = NULL;
15895
15896   if (TREE_CODE (origin) != FUNCTION_DECL)
15897     {
15898       /* We may have gotten separated from the block for the inlined
15899          function, if we're in an exception handler or some such; make
15900          sure that the abstract function has been written out.
15901
15902          Doing this for nested functions is wrong, however; functions are
15903          distinct units, and our context might not even be inline.  */
15904       tree fn = origin;
15905
15906       if (TYPE_P (fn))
15907         fn = TYPE_STUB_DECL (fn);
15908
15909       fn = decl_function_context (fn);
15910       if (fn)
15911         dwarf2out_abstract_function (fn);
15912     }
15913
15914   if (DECL_P (origin))
15915     origin_die = lookup_decl_die (origin);
15916   else if (TYPE_P (origin))
15917     origin_die = lookup_type_die (origin);
15918
15919   /* XXX: Functions that are never lowered don't always have correct block
15920      trees (in the case of java, they simply have no block tree, in some other
15921      languages).  For these functions, there is nothing we can really do to
15922      output correct debug info for inlined functions in all cases.  Rather
15923      than die, we'll just produce deficient debug info now, in that we will
15924      have variables without a proper abstract origin.  In the future, when all
15925      functions are lowered, we should re-add a gcc_assert (origin_die)
15926      here.  */
15927
15928   if (origin_die)
15929     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15930   return origin_die;
15931 }
15932
15933 /* We do not currently support the pure_virtual attribute.  */
15934
15935 static inline void
15936 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15937 {
15938   if (DECL_VINDEX (func_decl))
15939     {
15940       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15941
15942       if (host_integerp (DECL_VINDEX (func_decl), 0))
15943         add_AT_loc (die, DW_AT_vtable_elem_location,
15944                     new_loc_descr (DW_OP_constu,
15945                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15946                                    0));
15947
15948       /* GNU extension: Record what type this method came from originally.  */
15949       if (debug_info_level > DINFO_LEVEL_TERSE
15950           && DECL_CONTEXT (func_decl))
15951         add_AT_die_ref (die, DW_AT_containing_type,
15952                         lookup_type_die (DECL_CONTEXT (func_decl)));
15953     }
15954 }
15955 \f
15956 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15957    given decl.  This used to be a vendor extension until after DWARF 4
15958    standardized it.  */
15959
15960 static void
15961 add_linkage_attr (dw_die_ref die, tree decl)
15962 {
15963   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15964
15965   /* Mimic what assemble_name_raw does with a leading '*'.  */
15966   if (name[0] == '*')
15967     name = &name[1];
15968
15969   if (dwarf_version >= 4)
15970     add_AT_string (die, DW_AT_linkage_name, name);
15971   else
15972     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15973 }
15974
15975 /* Add source coordinate attributes for the given decl.  */
15976
15977 static void
15978 add_src_coords_attributes (dw_die_ref die, tree decl)
15979 {
15980   expanded_location s;
15981
15982   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15983     return;
15984   s = expand_location (DECL_SOURCE_LOCATION (decl));
15985   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15986   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15987 }
15988
15989 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15990
15991 static void
15992 add_linkage_name (dw_die_ref die, tree decl)
15993 {
15994   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15995        && TREE_PUBLIC (decl)
15996        && !DECL_ABSTRACT (decl)
15997        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15998        && die->die_tag != DW_TAG_member)
15999     {
16000       /* Defer until we have an assembler name set.  */
16001       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16002         {
16003           limbo_die_node *asm_name;
16004
16005           asm_name = ggc_alloc_cleared_limbo_die_node ();
16006           asm_name->die = die;
16007           asm_name->created_for = decl;
16008           asm_name->next = deferred_asm_name;
16009           deferred_asm_name = asm_name;
16010         }
16011       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16012         add_linkage_attr (die, decl);
16013     }
16014 }
16015
16016 /* Add a DW_AT_name attribute and source coordinate attribute for the
16017    given decl, but only if it actually has a name.  */
16018
16019 static void
16020 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16021 {
16022   tree decl_name;
16023
16024   decl_name = DECL_NAME (decl);
16025   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16026     {
16027       const char *name = dwarf2_name (decl, 0);
16028       if (name)
16029         add_name_attribute (die, name);
16030       if (! DECL_ARTIFICIAL (decl))
16031         add_src_coords_attributes (die, decl);
16032
16033       add_linkage_name (die, decl);
16034     }
16035
16036 #ifdef VMS_DEBUGGING_INFO
16037   /* Get the function's name, as described by its RTL.  This may be different
16038      from the DECL_NAME name used in the source file.  */
16039   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16040     {
16041       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16042                    XEXP (DECL_RTL (decl), 0));
16043       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16044     }
16045 #endif /* VMS_DEBUGGING_INFO */
16046 }
16047
16048 #ifdef VMS_DEBUGGING_INFO
16049 /* Output the debug main pointer die for VMS */
16050
16051 void
16052 dwarf2out_vms_debug_main_pointer (void)
16053 {
16054   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16055   dw_die_ref die;
16056
16057   /* Allocate the VMS debug main subprogram die.  */
16058   die = ggc_alloc_cleared_die_node ();
16059   die->die_tag = DW_TAG_subprogram;
16060   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16061   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16062                                current_function_funcdef_no);
16063   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16064
16065   /* Make it the first child of comp_unit_die ().  */
16066   die->die_parent = comp_unit_die ();
16067   if (comp_unit_die ()->die_child)
16068     {
16069       die->die_sib = comp_unit_die ()->die_child->die_sib;
16070       comp_unit_die ()->die_child->die_sib = die;
16071     }
16072   else
16073     {
16074       die->die_sib = die;
16075       comp_unit_die ()->die_child = die;
16076     }
16077 }
16078 #endif /* VMS_DEBUGGING_INFO */
16079
16080 /* Push a new declaration scope.  */
16081
16082 static void
16083 push_decl_scope (tree scope)
16084 {
16085   VEC_safe_push (tree, gc, decl_scope_table, scope);
16086 }
16087
16088 /* Pop a declaration scope.  */
16089
16090 static inline void
16091 pop_decl_scope (void)
16092 {
16093   VEC_pop (tree, decl_scope_table);
16094 }
16095
16096 /* walk_tree helper function for uses_local_type, below.  */
16097
16098 static tree
16099 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16100 {
16101   if (!TYPE_P (*tp))
16102     *walk_subtrees = 0;
16103   else
16104     {
16105       tree name = TYPE_NAME (*tp);
16106       if (name && DECL_P (name) && decl_function_context (name))
16107         return *tp;
16108     }
16109   return NULL_TREE;
16110 }
16111
16112 /* If TYPE involves a function-local type (including a local typedef to a
16113    non-local type), returns that type; otherwise returns NULL_TREE.  */
16114
16115 static tree
16116 uses_local_type (tree type)
16117 {
16118   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16119   return used;
16120 }
16121
16122 /* Return the DIE for the scope that immediately contains this type.
16123    Non-named types that do not involve a function-local type get global
16124    scope.  Named types nested in namespaces or other types get their
16125    containing scope.  All other types (i.e. function-local named types) get
16126    the current active scope.  */
16127
16128 static dw_die_ref
16129 scope_die_for (tree t, dw_die_ref context_die)
16130 {
16131   dw_die_ref scope_die = NULL;
16132   tree containing_scope;
16133
16134   /* Non-types always go in the current scope.  */
16135   gcc_assert (TYPE_P (t));
16136
16137   /* Use the scope of the typedef, rather than the scope of the type
16138      it refers to.  */
16139   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16140     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16141   else
16142     containing_scope = TYPE_CONTEXT (t);
16143
16144   /* Use the containing namespace if there is one.  */
16145   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16146     {
16147       if (context_die == lookup_decl_die (containing_scope))
16148         /* OK */;
16149       else if (debug_info_level > DINFO_LEVEL_TERSE)
16150         context_die = get_context_die (containing_scope);
16151       else
16152         containing_scope = NULL_TREE;
16153     }
16154
16155   /* Ignore function type "scopes" from the C frontend.  They mean that
16156      a tagged type is local to a parmlist of a function declarator, but
16157      that isn't useful to DWARF.  */
16158   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16159     containing_scope = NULL_TREE;
16160
16161   if (SCOPE_FILE_SCOPE_P (containing_scope))
16162     {
16163       /* If T uses a local type keep it local as well, to avoid references
16164          to function-local DIEs from outside the function.  */
16165       if (current_function_decl && uses_local_type (t))
16166         scope_die = context_die;
16167       else
16168         scope_die = comp_unit_die ();
16169     }
16170   else if (TYPE_P (containing_scope))
16171     {
16172       /* For types, we can just look up the appropriate DIE.  */
16173       if (debug_info_level > DINFO_LEVEL_TERSE)
16174         scope_die = get_context_die (containing_scope);
16175       else
16176         {
16177           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16178           if (scope_die == NULL)
16179             scope_die = comp_unit_die ();
16180         }
16181     }
16182   else
16183     scope_die = context_die;
16184
16185   return scope_die;
16186 }
16187
16188 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16189
16190 static inline int
16191 local_scope_p (dw_die_ref context_die)
16192 {
16193   for (; context_die; context_die = context_die->die_parent)
16194     if (context_die->die_tag == DW_TAG_inlined_subroutine
16195         || context_die->die_tag == DW_TAG_subprogram)
16196       return 1;
16197
16198   return 0;
16199 }
16200
16201 /* Returns nonzero if CONTEXT_DIE is a class.  */
16202
16203 static inline int
16204 class_scope_p (dw_die_ref context_die)
16205 {
16206   return (context_die
16207           && (context_die->die_tag == DW_TAG_structure_type
16208               || context_die->die_tag == DW_TAG_class_type
16209               || context_die->die_tag == DW_TAG_interface_type
16210               || context_die->die_tag == DW_TAG_union_type));
16211 }
16212
16213 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16214    whether or not to treat a DIE in this context as a declaration.  */
16215
16216 static inline int
16217 class_or_namespace_scope_p (dw_die_ref context_die)
16218 {
16219   return (class_scope_p (context_die)
16220           || (context_die && context_die->die_tag == DW_TAG_namespace));
16221 }
16222
16223 /* Many forms of DIEs require a "type description" attribute.  This
16224    routine locates the proper "type descriptor" die for the type given
16225    by 'type', and adds a DW_AT_type attribute below the given die.  */
16226
16227 static void
16228 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16229                     int decl_volatile, dw_die_ref context_die)
16230 {
16231   enum tree_code code  = TREE_CODE (type);
16232   dw_die_ref type_die  = NULL;
16233
16234   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16235      or fixed-point type, use the inner type.  This is because we have no
16236      support for unnamed types in base_type_die.  This can happen if this is
16237      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16238   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16239       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16240     type = TREE_TYPE (type), code = TREE_CODE (type);
16241
16242   if (code == ERROR_MARK
16243       /* Handle a special case.  For functions whose return type is void, we
16244          generate *no* type attribute.  (Note that no object may have type
16245          `void', so this only applies to function return types).  */
16246       || code == VOID_TYPE)
16247     return;
16248
16249   type_die = modified_type_die (type,
16250                                 decl_const || TYPE_READONLY (type),
16251                                 decl_volatile || TYPE_VOLATILE (type),
16252                                 context_die);
16253
16254   if (type_die != NULL)
16255     add_AT_die_ref (object_die, DW_AT_type, type_die);
16256 }
16257
16258 /* Given an object die, add the calling convention attribute for the
16259    function call type.  */
16260 static void
16261 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16262 {
16263   enum dwarf_calling_convention value = DW_CC_normal;
16264
16265   value = ((enum dwarf_calling_convention)
16266            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16267
16268   if (is_fortran ()
16269       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16270     {
16271       /* DWARF 2 doesn't provide a way to identify a program's source-level
16272         entry point.  DW_AT_calling_convention attributes are only meant
16273         to describe functions' calling conventions.  However, lacking a
16274         better way to signal the Fortran main program, we used this for 
16275         a long time, following existing custom.  Now, DWARF 4 has 
16276         DW_AT_main_subprogram, which we add below, but some tools still
16277         rely on the old way, which we thus keep.  */
16278       value = DW_CC_program;
16279
16280       if (dwarf_version >= 4 || !dwarf_strict)
16281         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16282     }
16283
16284   /* Only add the attribute if the backend requests it, and
16285      is not DW_CC_normal.  */
16286   if (value && (value != DW_CC_normal))
16287     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16288 }
16289
16290 /* Given a tree pointer to a struct, class, union, or enum type node, return
16291    a pointer to the (string) tag name for the given type, or zero if the type
16292    was declared without a tag.  */
16293
16294 static const char *
16295 type_tag (const_tree type)
16296 {
16297   const char *name = 0;
16298
16299   if (TYPE_NAME (type) != 0)
16300     {
16301       tree t = 0;
16302
16303       /* Find the IDENTIFIER_NODE for the type name.  */
16304       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16305           && !TYPE_NAMELESS (type))
16306         t = TYPE_NAME (type);
16307
16308       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16309          a TYPE_DECL node, regardless of whether or not a `typedef' was
16310          involved.  */
16311       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16312                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16313         {
16314           /* We want to be extra verbose.  Don't call dwarf_name if
16315              DECL_NAME isn't set.  The default hook for decl_printable_name
16316              doesn't like that, and in this context it's correct to return
16317              0, instead of "<anonymous>" or the like.  */
16318           if (DECL_NAME (TYPE_NAME (type))
16319               && !DECL_NAMELESS (TYPE_NAME (type)))
16320             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16321         }
16322
16323       /* Now get the name as a string, or invent one.  */
16324       if (!name && t != 0)
16325         name = IDENTIFIER_POINTER (t);
16326     }
16327
16328   return (name == 0 || *name == '\0') ? 0 : name;
16329 }
16330
16331 /* Return the type associated with a data member, make a special check
16332    for bit field types.  */
16333
16334 static inline tree
16335 member_declared_type (const_tree member)
16336 {
16337   return (DECL_BIT_FIELD_TYPE (member)
16338           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16339 }
16340
16341 /* Get the decl's label, as described by its RTL. This may be different
16342    from the DECL_NAME name used in the source file.  */
16343
16344 #if 0
16345 static const char *
16346 decl_start_label (tree decl)
16347 {
16348   rtx x;
16349   const char *fnname;
16350
16351   x = DECL_RTL (decl);
16352   gcc_assert (MEM_P (x));
16353
16354   x = XEXP (x, 0);
16355   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16356
16357   fnname = XSTR (x, 0);
16358   return fnname;
16359 }
16360 #endif
16361 \f
16362 /* These routines generate the internal representation of the DIE's for
16363    the compilation unit.  Debugging information is collected by walking
16364    the declaration trees passed in from dwarf2out_decl().  */
16365
16366 static void
16367 gen_array_type_die (tree type, dw_die_ref context_die)
16368 {
16369   dw_die_ref scope_die = scope_die_for (type, context_die);
16370   dw_die_ref array_die;
16371
16372   /* GNU compilers represent multidimensional array types as sequences of one
16373      dimensional array types whose element types are themselves array types.
16374      We sometimes squish that down to a single array_type DIE with multiple
16375      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16376      say that we are allowed to do this kind of compression in C, because
16377      there is no difference between an array of arrays and a multidimensional
16378      array.  We don't do this for Ada to remain as close as possible to the
16379      actual representation, which is especially important against the language
16380      flexibilty wrt arrays of variable size.  */
16381
16382   bool collapse_nested_arrays = !is_ada ();
16383   tree element_type;
16384
16385   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16386      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16387   if (TYPE_STRING_FLAG (type)
16388       && TREE_CODE (type) == ARRAY_TYPE
16389       && is_fortran ()
16390       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16391     {
16392       HOST_WIDE_INT size;
16393
16394       array_die = new_die (DW_TAG_string_type, scope_die, type);
16395       add_name_attribute (array_die, type_tag (type));
16396       equate_type_number_to_die (type, array_die);
16397       size = int_size_in_bytes (type);
16398       if (size >= 0)
16399         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16400       else if (TYPE_DOMAIN (type) != NULL_TREE
16401                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16402                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16403         {
16404           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16405           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16406
16407           size = int_size_in_bytes (TREE_TYPE (szdecl));
16408           if (loc && size > 0)
16409             {
16410               add_AT_location_description (array_die, DW_AT_string_length, loc);
16411               if (size != DWARF2_ADDR_SIZE)
16412                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16413             }
16414         }
16415       return;
16416     }
16417
16418   /* ??? The SGI dwarf reader fails for array of array of enum types
16419      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16420      array type comes before the outer array type.  We thus call gen_type_die
16421      before we new_die and must prevent nested array types collapsing for this
16422      target.  */
16423
16424 #ifdef MIPS_DEBUGGING_INFO
16425   gen_type_die (TREE_TYPE (type), context_die);
16426   collapse_nested_arrays = false;
16427 #endif
16428
16429   array_die = new_die (DW_TAG_array_type, scope_die, type);
16430   add_name_attribute (array_die, type_tag (type));
16431   equate_type_number_to_die (type, array_die);
16432
16433   if (TREE_CODE (type) == VECTOR_TYPE)
16434     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16435
16436   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16437   if (is_fortran ()
16438       && TREE_CODE (type) == ARRAY_TYPE
16439       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16440       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16441     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16442
16443 #if 0
16444   /* We default the array ordering.  SDB will probably do
16445      the right things even if DW_AT_ordering is not present.  It's not even
16446      an issue until we start to get into multidimensional arrays anyway.  If
16447      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16448      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16449      and when we find out that we need to put these in, we will only do so
16450      for multidimensional arrays.  */
16451   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16452 #endif
16453
16454 #ifdef MIPS_DEBUGGING_INFO
16455   /* The SGI compilers handle arrays of unknown bound by setting
16456      AT_declaration and not emitting any subrange DIEs.  */
16457   if (TREE_CODE (type) == ARRAY_TYPE
16458       && ! TYPE_DOMAIN (type))
16459     add_AT_flag (array_die, DW_AT_declaration, 1);
16460   else
16461 #endif
16462   if (TREE_CODE (type) == VECTOR_TYPE)
16463     {
16464       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16465       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16466       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16467       add_bound_info (subrange_die, DW_AT_upper_bound,
16468                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16469     }
16470   else
16471     add_subscript_info (array_die, type, collapse_nested_arrays);
16472
16473   /* Add representation of the type of the elements of this array type and
16474      emit the corresponding DIE if we haven't done it already.  */
16475   element_type = TREE_TYPE (type);
16476   if (collapse_nested_arrays)
16477     while (TREE_CODE (element_type) == ARRAY_TYPE)
16478       {
16479         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16480           break;
16481         element_type = TREE_TYPE (element_type);
16482       }
16483
16484 #ifndef MIPS_DEBUGGING_INFO
16485   gen_type_die (element_type, context_die);
16486 #endif
16487
16488   add_type_attribute (array_die, element_type, 0, 0, context_die);
16489
16490   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16491   if (TYPE_ARTIFICIAL (type))
16492     add_AT_flag (array_die, DW_AT_artificial, 1);
16493
16494   if (get_AT (array_die, DW_AT_name))
16495     add_pubtype (type, array_die);
16496 }
16497
16498 static dw_loc_descr_ref
16499 descr_info_loc (tree val, tree base_decl)
16500 {
16501   HOST_WIDE_INT size;
16502   dw_loc_descr_ref loc, loc2;
16503   enum dwarf_location_atom op;
16504
16505   if (val == base_decl)
16506     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16507
16508   switch (TREE_CODE (val))
16509     {
16510     CASE_CONVERT:
16511       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16512     case VAR_DECL:
16513       return loc_descriptor_from_tree (val, 0);
16514     case INTEGER_CST:
16515       if (host_integerp (val, 0))
16516         return int_loc_descriptor (tree_low_cst (val, 0));
16517       break;
16518     case INDIRECT_REF:
16519       size = int_size_in_bytes (TREE_TYPE (val));
16520       if (size < 0)
16521         break;
16522       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16523       if (!loc)
16524         break;
16525       if (size == DWARF2_ADDR_SIZE)
16526         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16527       else
16528         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16529       return loc;
16530     case POINTER_PLUS_EXPR:
16531     case PLUS_EXPR:
16532       if (host_integerp (TREE_OPERAND (val, 1), 1)
16533           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16534              < 16384)
16535         {
16536           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16537           if (!loc)
16538             break;
16539           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16540         }
16541       else
16542         {
16543           op = DW_OP_plus;
16544         do_binop:
16545           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16546           if (!loc)
16547             break;
16548           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16549           if (!loc2)
16550             break;
16551           add_loc_descr (&loc, loc2);
16552           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16553         }
16554       return loc;
16555     case MINUS_EXPR:
16556       op = DW_OP_minus;
16557       goto do_binop;
16558     case MULT_EXPR:
16559       op = DW_OP_mul;
16560       goto do_binop;
16561     case EQ_EXPR:
16562       op = DW_OP_eq;
16563       goto do_binop;
16564     case NE_EXPR:
16565       op = DW_OP_ne;
16566       goto do_binop;
16567     default:
16568       break;
16569     }
16570   return NULL;
16571 }
16572
16573 static void
16574 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16575                       tree val, tree base_decl)
16576 {
16577   dw_loc_descr_ref loc;
16578
16579   if (host_integerp (val, 0))
16580     {
16581       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16582       return;
16583     }
16584
16585   loc = descr_info_loc (val, base_decl);
16586   if (!loc)
16587     return;
16588
16589   add_AT_loc (die, attr, loc);
16590 }
16591
16592 /* This routine generates DIE for array with hidden descriptor, details
16593    are filled into *info by a langhook.  */
16594
16595 static void
16596 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16597                           dw_die_ref context_die)
16598 {
16599   dw_die_ref scope_die = scope_die_for (type, context_die);
16600   dw_die_ref array_die;
16601   int dim;
16602
16603   array_die = new_die (DW_TAG_array_type, scope_die, type);
16604   add_name_attribute (array_die, type_tag (type));
16605   equate_type_number_to_die (type, array_die);
16606
16607   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16608   if (is_fortran ()
16609       && info->ndimensions >= 2)
16610     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16611
16612   if (info->data_location)
16613     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16614                           info->base_decl);
16615   if (info->associated)
16616     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16617                           info->base_decl);
16618   if (info->allocated)
16619     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16620                           info->base_decl);
16621
16622   for (dim = 0; dim < info->ndimensions; dim++)
16623     {
16624       dw_die_ref subrange_die
16625         = new_die (DW_TAG_subrange_type, array_die, NULL);
16626
16627       if (info->dimen[dim].lower_bound)
16628         {
16629           /* If it is the default value, omit it.  */
16630           int dflt;
16631
16632           if (host_integerp (info->dimen[dim].lower_bound, 0)
16633               && (dflt = lower_bound_default ()) != -1
16634               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16635             ;
16636           else
16637             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16638                                   info->dimen[dim].lower_bound,
16639                                   info->base_decl);
16640         }
16641       if (info->dimen[dim].upper_bound)
16642         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16643                               info->dimen[dim].upper_bound,
16644                               info->base_decl);
16645       if (info->dimen[dim].stride)
16646         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16647                               info->dimen[dim].stride,
16648                               info->base_decl);
16649     }
16650
16651   gen_type_die (info->element_type, context_die);
16652   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16653
16654   if (get_AT (array_die, DW_AT_name))
16655     add_pubtype (type, array_die);
16656 }
16657
16658 #if 0
16659 static void
16660 gen_entry_point_die (tree decl, dw_die_ref context_die)
16661 {
16662   tree origin = decl_ultimate_origin (decl);
16663   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16664
16665   if (origin != NULL)
16666     add_abstract_origin_attribute (decl_die, origin);
16667   else
16668     {
16669       add_name_and_src_coords_attributes (decl_die, decl);
16670       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16671                           0, 0, context_die);
16672     }
16673
16674   if (DECL_ABSTRACT (decl))
16675     equate_decl_number_to_die (decl, decl_die);
16676   else
16677     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16678 }
16679 #endif
16680
16681 /* Walk through the list of incomplete types again, trying once more to
16682    emit full debugging info for them.  */
16683
16684 static void
16685 retry_incomplete_types (void)
16686 {
16687   int i;
16688
16689   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16690     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16691                                   DINFO_USAGE_DIR_USE))
16692       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16693 }
16694
16695 /* Determine what tag to use for a record type.  */
16696
16697 static enum dwarf_tag
16698 record_type_tag (tree type)
16699 {
16700   if (! lang_hooks.types.classify_record)
16701     return DW_TAG_structure_type;
16702
16703   switch (lang_hooks.types.classify_record (type))
16704     {
16705     case RECORD_IS_STRUCT:
16706       return DW_TAG_structure_type;
16707
16708     case RECORD_IS_CLASS:
16709       return DW_TAG_class_type;
16710
16711     case RECORD_IS_INTERFACE:
16712       if (dwarf_version >= 3 || !dwarf_strict)
16713         return DW_TAG_interface_type;
16714       return DW_TAG_structure_type;
16715
16716     default:
16717       gcc_unreachable ();
16718     }
16719 }
16720
16721 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16722    include all of the information about the enumeration values also. Each
16723    enumerated type name/value is listed as a child of the enumerated type
16724    DIE.  */
16725
16726 static dw_die_ref
16727 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16728 {
16729   dw_die_ref type_die = lookup_type_die (type);
16730
16731   if (type_die == NULL)
16732     {
16733       type_die = new_die (DW_TAG_enumeration_type,
16734                           scope_die_for (type, context_die), type);
16735       equate_type_number_to_die (type, type_die);
16736       add_name_attribute (type_die, type_tag (type));
16737       if (dwarf_version >= 4 || !dwarf_strict)
16738         {
16739           if (ENUM_IS_SCOPED (type))
16740             add_AT_flag (type_die, DW_AT_enum_class, 1);
16741           if (ENUM_IS_OPAQUE (type))
16742             add_AT_flag (type_die, DW_AT_declaration, 1);
16743         }
16744     }
16745   else if (! TYPE_SIZE (type))
16746     return type_die;
16747   else
16748     remove_AT (type_die, DW_AT_declaration);
16749
16750   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16751      given enum type is incomplete, do not generate the DW_AT_byte_size
16752      attribute or the DW_AT_element_list attribute.  */
16753   if (TYPE_SIZE (type))
16754     {
16755       tree link;
16756
16757       TREE_ASM_WRITTEN (type) = 1;
16758       add_byte_size_attribute (type_die, type);
16759       if (TYPE_STUB_DECL (type) != NULL_TREE)
16760         {
16761           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16762           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16763         }
16764
16765       /* If the first reference to this type was as the return type of an
16766          inline function, then it may not have a parent.  Fix this now.  */
16767       if (type_die->die_parent == NULL)
16768         add_child_die (scope_die_for (type, context_die), type_die);
16769
16770       for (link = TYPE_VALUES (type);
16771            link != NULL; link = TREE_CHAIN (link))
16772         {
16773           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16774           tree value = TREE_VALUE (link);
16775
16776           add_name_attribute (enum_die,
16777                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16778
16779           if (TREE_CODE (value) == CONST_DECL)
16780             value = DECL_INITIAL (value);
16781
16782           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16783             /* DWARF2 does not provide a way of indicating whether or
16784                not enumeration constants are signed or unsigned.  GDB
16785                always assumes the values are signed, so we output all
16786                values as if they were signed.  That means that
16787                enumeration constants with very large unsigned values
16788                will appear to have negative values in the debugger.  */
16789             add_AT_int (enum_die, DW_AT_const_value,
16790                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16791         }
16792
16793       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16794       if (TYPE_ARTIFICIAL (type))
16795         add_AT_flag (type_die, DW_AT_artificial, 1);
16796     }
16797   else
16798     add_AT_flag (type_die, DW_AT_declaration, 1);
16799
16800   if (get_AT (type_die, DW_AT_name))
16801     add_pubtype (type, type_die);
16802
16803   return type_die;
16804 }
16805
16806 /* Generate a DIE to represent either a real live formal parameter decl or to
16807    represent just the type of some formal parameter position in some function
16808    type.
16809
16810    Note that this routine is a bit unusual because its argument may be a
16811    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16812    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16813    node.  If it's the former then this function is being called to output a
16814    DIE to represent a formal parameter object (or some inlining thereof).  If
16815    it's the latter, then this function is only being called to output a
16816    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16817    argument type of some subprogram type.
16818    If EMIT_NAME_P is true, name and source coordinate attributes
16819    are emitted.  */
16820
16821 static dw_die_ref
16822 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16823                           dw_die_ref context_die)
16824 {
16825   tree node_or_origin = node ? node : origin;
16826   tree ultimate_origin;
16827   dw_die_ref parm_die
16828     = new_die (DW_TAG_formal_parameter, context_die, node);
16829
16830   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16831     {
16832     case tcc_declaration:
16833       ultimate_origin = decl_ultimate_origin (node_or_origin);
16834       if (node || ultimate_origin)
16835         origin = ultimate_origin;
16836       if (origin != NULL)
16837         add_abstract_origin_attribute (parm_die, origin);
16838       else if (emit_name_p)
16839         add_name_and_src_coords_attributes (parm_die, node);
16840       if (origin == NULL
16841           || (! DECL_ABSTRACT (node_or_origin)
16842               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16843                                            decl_function_context
16844                                                             (node_or_origin))))
16845         {
16846           tree type = TREE_TYPE (node_or_origin);
16847           if (decl_by_reference_p (node_or_origin))
16848             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16849                                 context_die);
16850           else
16851             add_type_attribute (parm_die, type,
16852                                 TREE_READONLY (node_or_origin),
16853                                 TREE_THIS_VOLATILE (node_or_origin),
16854                                 context_die);
16855         }
16856       if (origin == NULL && DECL_ARTIFICIAL (node))
16857         add_AT_flag (parm_die, DW_AT_artificial, 1);
16858
16859       if (node && node != origin)
16860         equate_decl_number_to_die (node, parm_die);
16861       if (! DECL_ABSTRACT (node_or_origin))
16862         add_location_or_const_value_attribute (parm_die, node_or_origin,
16863                                                node == NULL, DW_AT_location);
16864
16865       break;
16866
16867     case tcc_type:
16868       /* We were called with some kind of a ..._TYPE node.  */
16869       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16870       break;
16871
16872     default:
16873       gcc_unreachable ();
16874     }
16875
16876   return parm_die;
16877 }
16878
16879 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16880    children DW_TAG_formal_parameter DIEs representing the arguments of the
16881    parameter pack.
16882
16883    PARM_PACK must be a function parameter pack.
16884    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16885    must point to the subsequent arguments of the function PACK_ARG belongs to.
16886    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16887    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16888    following the last one for which a DIE was generated.  */
16889
16890 static dw_die_ref
16891 gen_formal_parameter_pack_die  (tree parm_pack,
16892                                 tree pack_arg,
16893                                 dw_die_ref subr_die,
16894                                 tree *next_arg)
16895 {
16896   tree arg;
16897   dw_die_ref parm_pack_die;
16898
16899   gcc_assert (parm_pack
16900               && lang_hooks.function_parameter_pack_p (parm_pack)
16901               && subr_die);
16902
16903   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16904   add_src_coords_attributes (parm_pack_die, parm_pack);
16905
16906   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16907     {
16908       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16909                                                                  parm_pack))
16910         break;
16911       gen_formal_parameter_die (arg, NULL,
16912                                 false /* Don't emit name attribute.  */,
16913                                 parm_pack_die);
16914     }
16915   if (next_arg)
16916     *next_arg = arg;
16917   return parm_pack_die;
16918 }
16919
16920 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16921    at the end of an (ANSI prototyped) formal parameters list.  */
16922
16923 static void
16924 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16925 {
16926   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16927 }
16928
16929 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16930    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16931    parameters as specified in some function type specification (except for
16932    those which appear as part of a function *definition*).  */
16933
16934 static void
16935 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16936 {
16937   tree link;
16938   tree formal_type = NULL;
16939   tree first_parm_type;
16940   tree arg;
16941
16942   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16943     {
16944       arg = DECL_ARGUMENTS (function_or_method_type);
16945       function_or_method_type = TREE_TYPE (function_or_method_type);
16946     }
16947   else
16948     arg = NULL_TREE;
16949
16950   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16951
16952   /* Make our first pass over the list of formal parameter types and output a
16953      DW_TAG_formal_parameter DIE for each one.  */
16954   for (link = first_parm_type; link; )
16955     {
16956       dw_die_ref parm_die;
16957
16958       formal_type = TREE_VALUE (link);
16959       if (formal_type == void_type_node)
16960         break;
16961
16962       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16963       parm_die = gen_formal_parameter_die (formal_type, NULL,
16964                                            true /* Emit name attribute.  */,
16965                                            context_die);
16966       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16967           && link == first_parm_type)
16968         {
16969           add_AT_flag (parm_die, DW_AT_artificial, 1);
16970           if (dwarf_version >= 3 || !dwarf_strict)
16971             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16972         }
16973       else if (arg && DECL_ARTIFICIAL (arg))
16974         add_AT_flag (parm_die, DW_AT_artificial, 1);
16975
16976       link = TREE_CHAIN (link);
16977       if (arg)
16978         arg = DECL_CHAIN (arg);
16979     }
16980
16981   /* If this function type has an ellipsis, add a
16982      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16983   if (formal_type != void_type_node)
16984     gen_unspecified_parameters_die (function_or_method_type, context_die);
16985
16986   /* Make our second (and final) pass over the list of formal parameter types
16987      and output DIEs to represent those types (as necessary).  */
16988   for (link = TYPE_ARG_TYPES (function_or_method_type);
16989        link && TREE_VALUE (link);
16990        link = TREE_CHAIN (link))
16991     gen_type_die (TREE_VALUE (link), context_die);
16992 }
16993
16994 /* We want to generate the DIE for TYPE so that we can generate the
16995    die for MEMBER, which has been defined; we will need to refer back
16996    to the member declaration nested within TYPE.  If we're trying to
16997    generate minimal debug info for TYPE, processing TYPE won't do the
16998    trick; we need to attach the member declaration by hand.  */
16999
17000 static void
17001 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17002 {
17003   gen_type_die (type, context_die);
17004
17005   /* If we're trying to avoid duplicate debug info, we may not have
17006      emitted the member decl for this function.  Emit it now.  */
17007   if (TYPE_STUB_DECL (type)
17008       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17009       && ! lookup_decl_die (member))
17010     {
17011       dw_die_ref type_die;
17012       gcc_assert (!decl_ultimate_origin (member));
17013
17014       push_decl_scope (type);
17015       type_die = lookup_type_die_strip_naming_typedef (type);
17016       if (TREE_CODE (member) == FUNCTION_DECL)
17017         gen_subprogram_die (member, type_die);
17018       else if (TREE_CODE (member) == FIELD_DECL)
17019         {
17020           /* Ignore the nameless fields that are used to skip bits but handle
17021              C++ anonymous unions and structs.  */
17022           if (DECL_NAME (member) != NULL_TREE
17023               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17024               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17025             {
17026               gen_type_die (member_declared_type (member), type_die);
17027               gen_field_die (member, type_die);
17028             }
17029         }
17030       else
17031         gen_variable_die (member, NULL_TREE, type_die);
17032
17033       pop_decl_scope ();
17034     }
17035 }
17036
17037 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17038    may later generate inlined and/or out-of-line instances of.  */
17039
17040 static void
17041 dwarf2out_abstract_function (tree decl)
17042 {
17043   dw_die_ref old_die;
17044   tree save_fn;
17045   tree context;
17046   int was_abstract;
17047   htab_t old_decl_loc_table;
17048   htab_t old_cached_dw_loc_list_table;
17049   int old_call_site_count, old_tail_call_site_count;
17050   struct call_arg_loc_node *old_call_arg_locations;
17051
17052   /* Make sure we have the actual abstract inline, not a clone.  */
17053   decl = DECL_ORIGIN (decl);
17054
17055   old_die = lookup_decl_die (decl);
17056   if (old_die && get_AT (old_die, DW_AT_inline))
17057     /* We've already generated the abstract instance.  */
17058     return;
17059
17060   /* We can be called while recursively when seeing block defining inlined subroutine
17061      DIE.  Be sure to not clobber the outer location table nor use it or we would
17062      get locations in abstract instantces.  */
17063   old_decl_loc_table = decl_loc_table;
17064   decl_loc_table = NULL;
17065   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17066   cached_dw_loc_list_table = NULL;
17067   old_call_arg_locations = call_arg_locations;
17068   call_arg_locations = NULL;
17069   old_call_site_count = call_site_count;
17070   call_site_count = -1;
17071   old_tail_call_site_count = tail_call_site_count;
17072   tail_call_site_count = -1;
17073
17074   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17075      we don't get confused by DECL_ABSTRACT.  */
17076   if (debug_info_level > DINFO_LEVEL_TERSE)
17077     {
17078       context = decl_class_context (decl);
17079       if (context)
17080         gen_type_die_for_member
17081           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17082     }
17083
17084   /* Pretend we've just finished compiling this function.  */
17085   save_fn = current_function_decl;
17086   current_function_decl = decl;
17087   push_cfun (DECL_STRUCT_FUNCTION (decl));
17088
17089   was_abstract = DECL_ABSTRACT (decl);
17090   set_decl_abstract_flags (decl, 1);
17091   dwarf2out_decl (decl);
17092   if (! was_abstract)
17093     set_decl_abstract_flags (decl, 0);
17094
17095   current_function_decl = save_fn;
17096   decl_loc_table = old_decl_loc_table;
17097   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17098   call_arg_locations = old_call_arg_locations;
17099   call_site_count = old_call_site_count;
17100   tail_call_site_count = old_tail_call_site_count;
17101   pop_cfun ();
17102 }
17103
17104 /* Helper function of premark_used_types() which gets called through
17105    htab_traverse.
17106
17107    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17108    marked as unused by prune_unused_types.  */
17109
17110 static int
17111 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17112 {
17113   tree type;
17114   dw_die_ref die;
17115
17116   type = (tree) *slot;
17117   die = lookup_type_die (type);
17118   if (die != NULL)
17119     die->die_perennial_p = 1;
17120   return 1;
17121 }
17122
17123 /* Helper function of premark_types_used_by_global_vars which gets called
17124    through htab_traverse.
17125
17126    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17127    marked as unused by prune_unused_types. The DIE of the type is marked
17128    only if the global variable using the type will actually be emitted.  */
17129
17130 static int
17131 premark_types_used_by_global_vars_helper (void **slot,
17132                                           void *data ATTRIBUTE_UNUSED)
17133 {
17134   struct types_used_by_vars_entry *entry;
17135   dw_die_ref die;
17136
17137   entry = (struct types_used_by_vars_entry *) *slot;
17138   gcc_assert (entry->type != NULL
17139               && entry->var_decl != NULL);
17140   die = lookup_type_die (entry->type);
17141   if (die)
17142     {
17143       /* Ask cgraph if the global variable really is to be emitted.
17144          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17145       struct varpool_node *node = varpool_get_node (entry->var_decl);
17146       if (node && node->needed)
17147         {
17148           die->die_perennial_p = 1;
17149           /* Keep the parent DIEs as well.  */
17150           while ((die = die->die_parent) && die->die_perennial_p == 0)
17151             die->die_perennial_p = 1;
17152         }
17153     }
17154   return 1;
17155 }
17156
17157 /* Mark all members of used_types_hash as perennial.  */
17158
17159 static void
17160 premark_used_types (void)
17161 {
17162   if (cfun && cfun->used_types_hash)
17163     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17164 }
17165
17166 /* Mark all members of types_used_by_vars_entry as perennial.  */
17167
17168 static void
17169 premark_types_used_by_global_vars (void)
17170 {
17171   if (types_used_by_vars_hash)
17172     htab_traverse (types_used_by_vars_hash,
17173                    premark_types_used_by_global_vars_helper, NULL);
17174 }
17175
17176 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17177    for CA_LOC call arg loc node.  */
17178
17179 static dw_die_ref
17180 gen_call_site_die (tree decl, dw_die_ref subr_die,
17181                    struct call_arg_loc_node *ca_loc)
17182 {
17183   dw_die_ref stmt_die = NULL, die;
17184   tree block = ca_loc->block;
17185
17186   while (block
17187          && block != DECL_INITIAL (decl)
17188          && TREE_CODE (block) == BLOCK)
17189     {
17190       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17191         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17192       if (stmt_die)
17193         break;
17194       block = BLOCK_SUPERCONTEXT (block);
17195     }
17196   if (stmt_die == NULL)
17197     stmt_die = subr_die;
17198   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17199   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17200   if (ca_loc->tail_call_p)
17201     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17202   if (ca_loc->symbol_ref)
17203     {
17204       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17205       if (tdie)
17206         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17207       else
17208         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17209     }
17210   return die;
17211 }
17212
17213 /* Generate a DIE to represent a declared function (either file-scope or
17214    block-local).  */
17215
17216 static void
17217 gen_subprogram_die (tree decl, dw_die_ref context_die)
17218 {
17219   tree origin = decl_ultimate_origin (decl);
17220   dw_die_ref subr_die;
17221   tree outer_scope;
17222   dw_die_ref old_die = lookup_decl_die (decl);
17223   int declaration = (current_function_decl != decl
17224                      || class_or_namespace_scope_p (context_die));
17225
17226   premark_used_types ();
17227
17228   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17229      started to generate the abstract instance of an inline, decided to output
17230      its containing class, and proceeded to emit the declaration of the inline
17231      from the member list for the class.  If so, DECLARATION takes priority;
17232      we'll get back to the abstract instance when done with the class.  */
17233
17234   /* The class-scope declaration DIE must be the primary DIE.  */
17235   if (origin && declaration && class_or_namespace_scope_p (context_die))
17236     {
17237       origin = NULL;
17238       gcc_assert (!old_die);
17239     }
17240
17241   /* Now that the C++ front end lazily declares artificial member fns, we
17242      might need to retrofit the declaration into its class.  */
17243   if (!declaration && !origin && !old_die
17244       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17245       && !class_or_namespace_scope_p (context_die)
17246       && debug_info_level > DINFO_LEVEL_TERSE)
17247     old_die = force_decl_die (decl);
17248
17249   if (origin != NULL)
17250     {
17251       gcc_assert (!declaration || local_scope_p (context_die));
17252
17253       /* Fixup die_parent for the abstract instance of a nested
17254          inline function.  */
17255       if (old_die && old_die->die_parent == NULL)
17256         add_child_die (context_die, old_die);
17257
17258       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17259       add_abstract_origin_attribute (subr_die, origin);
17260       /*  This is where the actual code for a cloned function is.
17261           Let's emit linkage name attribute for it.  This helps
17262           debuggers to e.g, set breakpoints into
17263           constructors/destructors when the user asks "break
17264           K::K".  */
17265       add_linkage_name (subr_die, decl);
17266     }
17267   else if (old_die)
17268     {
17269       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17270       struct dwarf_file_data * file_index = lookup_filename (s.file);
17271
17272       if (!get_AT_flag (old_die, DW_AT_declaration)
17273           /* We can have a normal definition following an inline one in the
17274              case of redefinition of GNU C extern inlines.
17275              It seems reasonable to use AT_specification in this case.  */
17276           && !get_AT (old_die, DW_AT_inline))
17277         {
17278           /* Detect and ignore this case, where we are trying to output
17279              something we have already output.  */
17280           return;
17281         }
17282
17283       /* If the definition comes from the same place as the declaration,
17284          maybe use the old DIE.  We always want the DIE for this function
17285          that has the *_pc attributes to be under comp_unit_die so the
17286          debugger can find it.  We also need to do this for abstract
17287          instances of inlines, since the spec requires the out-of-line copy
17288          to have the same parent.  For local class methods, this doesn't
17289          apply; we just use the old DIE.  */
17290       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17291           && (DECL_ARTIFICIAL (decl)
17292               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17293                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17294                       == (unsigned) s.line))))
17295         {
17296           subr_die = old_die;
17297
17298           /* Clear out the declaration attribute and the formal parameters.
17299              Do not remove all children, because it is possible that this
17300              declaration die was forced using force_decl_die(). In such
17301              cases die that forced declaration die (e.g. TAG_imported_module)
17302              is one of the children that we do not want to remove.  */
17303           remove_AT (subr_die, DW_AT_declaration);
17304           remove_AT (subr_die, DW_AT_object_pointer);
17305           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17306         }
17307       else
17308         {
17309           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17310           add_AT_specification (subr_die, old_die);
17311           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17312             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17313           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17314             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17315         }
17316     }
17317   else
17318     {
17319       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17320
17321       if (TREE_PUBLIC (decl))
17322         add_AT_flag (subr_die, DW_AT_external, 1);
17323
17324       add_name_and_src_coords_attributes (subr_die, decl);
17325       if (debug_info_level > DINFO_LEVEL_TERSE)
17326         {
17327           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17328           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17329                               0, 0, context_die);
17330         }
17331
17332       add_pure_or_virtual_attribute (subr_die, decl);
17333       if (DECL_ARTIFICIAL (decl))
17334         add_AT_flag (subr_die, DW_AT_artificial, 1);
17335
17336       add_accessibility_attribute (subr_die, decl);
17337     }
17338
17339   if (declaration)
17340     {
17341       if (!old_die || !get_AT (old_die, DW_AT_inline))
17342         {
17343           add_AT_flag (subr_die, DW_AT_declaration, 1);
17344
17345           /* If this is an explicit function declaration then generate
17346              a DW_AT_explicit attribute.  */
17347           if (lang_hooks.decls.function_decl_explicit_p (decl)
17348               && (dwarf_version >= 3 || !dwarf_strict))
17349             add_AT_flag (subr_die, DW_AT_explicit, 1);
17350
17351           /* The first time we see a member function, it is in the context of
17352              the class to which it belongs.  We make sure of this by emitting
17353              the class first.  The next time is the definition, which is
17354              handled above.  The two may come from the same source text.
17355
17356              Note that force_decl_die() forces function declaration die. It is
17357              later reused to represent definition.  */
17358           equate_decl_number_to_die (decl, subr_die);
17359         }
17360     }
17361   else if (DECL_ABSTRACT (decl))
17362     {
17363       if (DECL_DECLARED_INLINE_P (decl))
17364         {
17365           if (cgraph_function_possibly_inlined_p (decl))
17366             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17367           else
17368             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17369         }
17370       else
17371         {
17372           if (cgraph_function_possibly_inlined_p (decl))
17373             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17374           else
17375             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17376         }
17377
17378       if (DECL_DECLARED_INLINE_P (decl)
17379           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17380         add_AT_flag (subr_die, DW_AT_artificial, 1);
17381
17382       equate_decl_number_to_die (decl, subr_die);
17383     }
17384   else if (!DECL_EXTERNAL (decl))
17385     {
17386       HOST_WIDE_INT cfa_fb_offset;
17387
17388       if (!old_die || !get_AT (old_die, DW_AT_inline))
17389         equate_decl_number_to_die (decl, subr_die);
17390
17391       if (!flag_reorder_blocks_and_partition)
17392         {
17393           dw_fde_ref fde = cfun->fde;
17394           if (fde->dw_fde_begin)
17395             {
17396               /* We have already generated the labels.  */
17397               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17398               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17399             }
17400           else
17401             {
17402               /* Create start/end labels and add the range.  */
17403               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17404               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17405                                            current_function_funcdef_no);
17406               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17407               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17408                                            current_function_funcdef_no);
17409               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17410             }
17411
17412 #if VMS_DEBUGGING_INFO
17413       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17414          Section 2.3 Prologue and Epilogue Attributes:
17415          When a breakpoint is set on entry to a function, it is generally
17416          desirable for execution to be suspended, not on the very first
17417          instruction of the function, but rather at a point after the
17418          function's frame has been set up, after any language defined local
17419          declaration processing has been completed, and before execution of
17420          the first statement of the function begins. Debuggers generally
17421          cannot properly determine where this point is.  Similarly for a
17422          breakpoint set on exit from a function. The prologue and epilogue
17423          attributes allow a compiler to communicate the location(s) to use.  */
17424
17425       {
17426         if (fde->dw_fde_vms_end_prologue)
17427           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17428             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17429
17430         if (fde->dw_fde_vms_begin_epilogue)
17431           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17432             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17433       }
17434 #endif
17435
17436           add_pubname (decl, subr_die);
17437         }
17438       else
17439         {
17440           /* Generate pubnames entries for the split function code ranges.  */
17441           dw_fde_ref fde = cfun->fde;
17442
17443           if (fde->dw_fde_second_begin)
17444             {
17445               if (dwarf_version >= 3 || !dwarf_strict)
17446                 {
17447                   /* We should use ranges for non-contiguous code section 
17448                      addresses.  Use the actual code range for the initial
17449                      section, since the HOT/COLD labels might precede an 
17450                      alignment offset.  */
17451                   bool range_list_added = false;
17452                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17453                                         fde->dw_fde_end, &range_list_added);
17454                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17455                                         fde->dw_fde_second_end,
17456                                         &range_list_added);
17457                   add_pubname (decl, subr_die);
17458                   if (range_list_added)
17459                     add_ranges (NULL);
17460                 }
17461               else
17462                 {
17463                   /* There is no real support in DW2 for this .. so we make
17464                      a work-around.  First, emit the pub name for the segment
17465                      containing the function label.  Then make and emit a
17466                      simplified subprogram DIE for the second segment with the
17467                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17468                      linkage name for the second die so that gdb will find both
17469                      sections when given "b foo".  */
17470                   const char *name = NULL;
17471                   tree decl_name = DECL_NAME (decl);
17472                   dw_die_ref seg_die;
17473
17474                   /* Do the 'primary' section.   */
17475                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17476                                  fde->dw_fde_begin);
17477                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17478                                  fde->dw_fde_end);
17479                   /* Add it.   */
17480                   add_pubname (decl, subr_die);
17481
17482                   /* Build a minimal DIE for the secondary section.  */
17483                   seg_die = new_die (DW_TAG_subprogram,
17484                                      subr_die->die_parent, decl);
17485
17486                   if (TREE_PUBLIC (decl))
17487                     add_AT_flag (seg_die, DW_AT_external, 1);
17488
17489                   if (decl_name != NULL 
17490                       && IDENTIFIER_POINTER (decl_name) != NULL)
17491                     {
17492                       name = dwarf2_name (decl, 1);
17493                       if (! DECL_ARTIFICIAL (decl))
17494                         add_src_coords_attributes (seg_die, decl);
17495
17496                       add_linkage_name (seg_die, decl);
17497                     }
17498                   gcc_assert (name != NULL);
17499                   add_pure_or_virtual_attribute (seg_die, decl);
17500                   if (DECL_ARTIFICIAL (decl))
17501                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17502
17503                   name = concat ("__second_sect_of_", name, NULL); 
17504                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17505                                  fde->dw_fde_second_begin);
17506                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17507                                  fde->dw_fde_second_end);
17508                   add_name_attribute (seg_die, name);
17509                   add_pubname_string (name, seg_die);
17510                 }
17511             }
17512           else
17513             {
17514               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17515               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17516               add_pubname (decl, subr_die);
17517             }
17518         }
17519
17520 #ifdef MIPS_DEBUGGING_INFO
17521       /* Add a reference to the FDE for this routine.  */
17522       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17523 #endif
17524
17525       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17526
17527       /* We define the "frame base" as the function's CFA.  This is more
17528          convenient for several reasons: (1) It's stable across the prologue
17529          and epilogue, which makes it better than just a frame pointer,
17530          (2) With dwarf3, there exists a one-byte encoding that allows us
17531          to reference the .debug_frame data by proxy, but failing that,
17532          (3) We can at least reuse the code inspection and interpretation
17533          code that determines the CFA position at various points in the
17534          function.  */
17535       if (dwarf_version >= 3)
17536         {
17537           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17538           add_AT_loc (subr_die, DW_AT_frame_base, op);
17539         }
17540       else
17541         {
17542           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17543           if (list->dw_loc_next)
17544             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17545           else
17546             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17547         }
17548
17549       /* Compute a displacement from the "steady-state frame pointer" to
17550          the CFA.  The former is what all stack slots and argument slots
17551          will reference in the rtl; the later is what we've told the
17552          debugger about.  We'll need to adjust all frame_base references
17553          by this displacement.  */
17554       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17555
17556       if (cfun->static_chain_decl)
17557         add_AT_location_description (subr_die, DW_AT_static_link,
17558                  loc_list_from_tree (cfun->static_chain_decl, 2));
17559     }
17560
17561   /* Generate child dies for template paramaters.  */
17562   if (debug_info_level > DINFO_LEVEL_TERSE)
17563     gen_generic_params_dies (decl);
17564
17565   /* Now output descriptions of the arguments for this function. This gets
17566      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17567      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17568      `...' at the end of the formal parameter list.  In order to find out if
17569      there was a trailing ellipsis or not, we must instead look at the type
17570      associated with the FUNCTION_DECL.  This will be a node of type
17571      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17572      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17573      an ellipsis at the end.  */
17574
17575   /* In the case where we are describing a mere function declaration, all we
17576      need to do here (and all we *can* do here) is to describe the *types* of
17577      its formal parameters.  */
17578   if (debug_info_level <= DINFO_LEVEL_TERSE)
17579     ;
17580   else if (declaration)
17581     gen_formal_types_die (decl, subr_die);
17582   else
17583     {
17584       /* Generate DIEs to represent all known formal parameters.  */
17585       tree parm = DECL_ARGUMENTS (decl);
17586       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17587       tree generic_decl_parm = generic_decl
17588                                 ? DECL_ARGUMENTS (generic_decl)
17589                                 : NULL;
17590
17591       /* Now we want to walk the list of parameters of the function and
17592          emit their relevant DIEs.
17593
17594          We consider the case of DECL being an instance of a generic function
17595          as well as it being a normal function.
17596
17597          If DECL is an instance of a generic function we walk the
17598          parameters of the generic function declaration _and_ the parameters of
17599          DECL itself. This is useful because we want to emit specific DIEs for
17600          function parameter packs and those are declared as part of the
17601          generic function declaration. In that particular case,
17602          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17603          That DIE has children DIEs representing the set of arguments
17604          of the pack. Note that the set of pack arguments can be empty.
17605          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17606          children DIE.
17607
17608          Otherwise, we just consider the parameters of DECL.  */
17609       while (generic_decl_parm || parm)
17610         {
17611           if (generic_decl_parm
17612               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17613             gen_formal_parameter_pack_die (generic_decl_parm,
17614                                            parm, subr_die,
17615                                            &parm);
17616           else if (parm)
17617             {
17618               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17619
17620               if (parm == DECL_ARGUMENTS (decl)
17621                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17622                   && parm_die
17623                   && (dwarf_version >= 3 || !dwarf_strict))
17624                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17625
17626               parm = DECL_CHAIN (parm);
17627             }
17628
17629           if (generic_decl_parm)
17630             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17631         }
17632
17633       /* Decide whether we need an unspecified_parameters DIE at the end.
17634          There are 2 more cases to do this for: 1) the ansi ... declaration -
17635          this is detectable when the end of the arg list is not a
17636          void_type_node 2) an unprototyped function declaration (not a
17637          definition).  This just means that we have no info about the
17638          parameters at all.  */
17639       if (prototype_p (TREE_TYPE (decl)))
17640         {
17641           /* This is the prototyped case, check for....  */
17642           if (stdarg_p (TREE_TYPE (decl)))
17643             gen_unspecified_parameters_die (decl, subr_die);
17644         }
17645       else if (DECL_INITIAL (decl) == NULL_TREE)
17646         gen_unspecified_parameters_die (decl, subr_die);
17647     }
17648
17649   /* Output Dwarf info for all of the stuff within the body of the function
17650      (if it has one - it may be just a declaration).  */
17651   outer_scope = DECL_INITIAL (decl);
17652
17653   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17654      a function.  This BLOCK actually represents the outermost binding contour
17655      for the function, i.e. the contour in which the function's formal
17656      parameters and labels get declared. Curiously, it appears that the front
17657      end doesn't actually put the PARM_DECL nodes for the current function onto
17658      the BLOCK_VARS list for this outer scope, but are strung off of the
17659      DECL_ARGUMENTS list for the function instead.
17660
17661      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17662      the LABEL_DECL nodes for the function however, and we output DWARF info
17663      for those in decls_for_scope.  Just within the `outer_scope' there will be
17664      a BLOCK node representing the function's outermost pair of curly braces,
17665      and any blocks used for the base and member initializers of a C++
17666      constructor function.  */
17667   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17668     {
17669       int call_site_note_count = 0;
17670       int tail_call_site_note_count = 0;
17671
17672       /* Emit a DW_TAG_variable DIE for a named return value.  */
17673       if (DECL_NAME (DECL_RESULT (decl)))
17674         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17675
17676       current_function_has_inlines = 0;
17677       decls_for_scope (outer_scope, subr_die, 0);
17678
17679       if (call_arg_locations && !dwarf_strict)
17680         {
17681           struct call_arg_loc_node *ca_loc;
17682           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17683             {
17684               dw_die_ref die = NULL;
17685               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17686               rtx arg, next_arg;
17687
17688               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17689                    arg; arg = next_arg)
17690                 {
17691                   dw_loc_descr_ref reg, val;
17692                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17693                   dw_die_ref cdie, tdie = NULL;
17694
17695                   next_arg = XEXP (arg, 1);
17696                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17697                       && next_arg
17698                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17699                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17700                       && REGNO (XEXP (XEXP (arg, 0), 0))
17701                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17702                     next_arg = XEXP (next_arg, 1);
17703                   if (mode == VOIDmode)
17704                     {
17705                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17706                       if (mode == VOIDmode)
17707                         mode = GET_MODE (XEXP (arg, 0));
17708                     }
17709                   if (mode == VOIDmode || mode == BLKmode)
17710                     continue;
17711                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17712                     {
17713                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17714                       tloc = XEXP (XEXP (arg, 0), 1);
17715                       continue;
17716                     }
17717                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17718                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17719                     {
17720                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17721                       tlocc = XEXP (XEXP (arg, 0), 1);
17722                       continue;
17723                     }
17724                   reg = NULL;
17725                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17726                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17727                                               VAR_INIT_STATUS_INITIALIZED);
17728                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17729                     {
17730                       rtx mem = XEXP (XEXP (arg, 0), 0);
17731                       reg = mem_loc_descriptor (XEXP (mem, 0),
17732                                                 get_address_mode (mem),
17733                                                 GET_MODE (mem),
17734                                                 VAR_INIT_STATUS_INITIALIZED);
17735                     }
17736                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17737                            == DEBUG_PARAMETER_REF)
17738                     {
17739                       tree tdecl
17740                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17741                       tdie = lookup_decl_die (tdecl);
17742                       if (tdie == NULL)
17743                         continue;
17744                     }
17745                   else
17746                     continue;
17747                   if (reg == NULL
17748                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17749                          != DEBUG_PARAMETER_REF)
17750                     continue;
17751                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17752                                             VOIDmode,
17753                                             VAR_INIT_STATUS_INITIALIZED);
17754                   if (val == NULL)
17755                     continue;
17756                   if (die == NULL)
17757                     die = gen_call_site_die (decl, subr_die, ca_loc);
17758                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17759                                   NULL_TREE);
17760                   if (reg != NULL)
17761                     add_AT_loc (cdie, DW_AT_location, reg);
17762                   else if (tdie != NULL)
17763                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17764                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17765                   if (next_arg != XEXP (arg, 1))
17766                     {
17767                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17768                       if (mode == VOIDmode)
17769                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17770                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17771                                                             0), 1),
17772                                                 mode, VOIDmode,
17773                                                 VAR_INIT_STATUS_INITIALIZED);
17774                       if (val != NULL)
17775                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17776                     }
17777                 }
17778               if (die == NULL
17779                   && (ca_loc->symbol_ref || tloc))
17780                 die = gen_call_site_die (decl, subr_die, ca_loc);
17781               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17782                 {
17783                   dw_loc_descr_ref tval = NULL;
17784
17785                   if (tloc != NULL_RTX)
17786                     tval = mem_loc_descriptor (tloc,
17787                                                GET_MODE (tloc) == VOIDmode
17788                                                ? Pmode : GET_MODE (tloc),
17789                                                VOIDmode,
17790                                                VAR_INIT_STATUS_INITIALIZED);
17791                   if (tval)
17792                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17793                   else if (tlocc != NULL_RTX)
17794                     {
17795                       tval = mem_loc_descriptor (tlocc,
17796                                                  GET_MODE (tlocc) == VOIDmode
17797                                                  ? Pmode : GET_MODE (tlocc),
17798                                                  VOIDmode,
17799                                                  VAR_INIT_STATUS_INITIALIZED);
17800                       if (tval)
17801                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17802                                     tval);
17803                     }
17804                 }
17805               if (die != NULL)
17806                 {
17807                   call_site_note_count++;
17808                   if (ca_loc->tail_call_p)
17809                     tail_call_site_note_count++;
17810                 }
17811             }
17812         }
17813       call_arg_locations = NULL;
17814       call_arg_loc_last = NULL;
17815       if (tail_call_site_count >= 0
17816           && tail_call_site_count == tail_call_site_note_count
17817           && !dwarf_strict)
17818         {
17819           if (call_site_count >= 0
17820               && call_site_count == call_site_note_count)
17821             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17822           else
17823             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17824         }
17825       call_site_count = -1;
17826       tail_call_site_count = -1;
17827     }
17828   /* Add the calling convention attribute if requested.  */
17829   add_calling_convention_attribute (subr_die, decl);
17830
17831 }
17832
17833 /* Returns a hash value for X (which really is a die_struct).  */
17834
17835 static hashval_t
17836 common_block_die_table_hash (const void *x)
17837 {
17838   const_dw_die_ref d = (const_dw_die_ref) x;
17839   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17840 }
17841
17842 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17843    as decl_id and die_parent of die_struct Y.  */
17844
17845 static int
17846 common_block_die_table_eq (const void *x, const void *y)
17847 {
17848   const_dw_die_ref d = (const_dw_die_ref) x;
17849   const_dw_die_ref e = (const_dw_die_ref) y;
17850   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17851 }
17852
17853 /* Generate a DIE to represent a declared data object.
17854    Either DECL or ORIGIN must be non-null.  */
17855
17856 static void
17857 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17858 {
17859   HOST_WIDE_INT off;
17860   tree com_decl;
17861   tree decl_or_origin = decl ? decl : origin;
17862   tree ultimate_origin;
17863   dw_die_ref var_die;
17864   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17865   dw_die_ref origin_die;
17866   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17867                       || class_or_namespace_scope_p (context_die));
17868   bool specialization_p = false;
17869
17870   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17871   if (decl || ultimate_origin)
17872     origin = ultimate_origin;
17873   com_decl = fortran_common (decl_or_origin, &off);
17874
17875   /* Symbol in common gets emitted as a child of the common block, in the form
17876      of a data member.  */
17877   if (com_decl)
17878     {
17879       dw_die_ref com_die;
17880       dw_loc_list_ref loc;
17881       die_node com_die_arg;
17882
17883       var_die = lookup_decl_die (decl_or_origin);
17884       if (var_die)
17885         {
17886           if (get_AT (var_die, DW_AT_location) == NULL)
17887             {
17888               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17889               if (loc)
17890                 {
17891                   if (off)
17892                     {
17893                       /* Optimize the common case.  */
17894                       if (single_element_loc_list_p (loc)
17895                           && loc->expr->dw_loc_opc == DW_OP_addr
17896                           && loc->expr->dw_loc_next == NULL
17897                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17898                              == SYMBOL_REF)
17899                         loc->expr->dw_loc_oprnd1.v.val_addr
17900                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17901                         else
17902                           loc_list_plus_const (loc, off);
17903                     }
17904                   add_AT_location_description (var_die, DW_AT_location, loc);
17905                   remove_AT (var_die, DW_AT_declaration);
17906                 }
17907             }
17908           return;
17909         }
17910
17911       if (common_block_die_table == NULL)
17912         common_block_die_table
17913           = htab_create_ggc (10, common_block_die_table_hash,
17914                              common_block_die_table_eq, NULL);
17915
17916       com_die_arg.decl_id = DECL_UID (com_decl);
17917       com_die_arg.die_parent = context_die;
17918       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17919       loc = loc_list_from_tree (com_decl, 2);
17920       if (com_die == NULL)
17921         {
17922           const char *cnam
17923             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17924           void **slot;
17925
17926           com_die = new_die (DW_TAG_common_block, context_die, decl);
17927           add_name_and_src_coords_attributes (com_die, com_decl);
17928           if (loc)
17929             {
17930               add_AT_location_description (com_die, DW_AT_location, loc);
17931               /* Avoid sharing the same loc descriptor between
17932                  DW_TAG_common_block and DW_TAG_variable.  */
17933               loc = loc_list_from_tree (com_decl, 2);
17934             }
17935           else if (DECL_EXTERNAL (decl))
17936             add_AT_flag (com_die, DW_AT_declaration, 1);
17937           add_pubname_string (cnam, com_die); /* ??? needed? */
17938           com_die->decl_id = DECL_UID (com_decl);
17939           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17940           *slot = (void *) com_die;
17941         }
17942       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17943         {
17944           add_AT_location_description (com_die, DW_AT_location, loc);
17945           loc = loc_list_from_tree (com_decl, 2);
17946           remove_AT (com_die, DW_AT_declaration);
17947         }
17948       var_die = new_die (DW_TAG_variable, com_die, decl);
17949       add_name_and_src_coords_attributes (var_die, decl);
17950       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17951                           TREE_THIS_VOLATILE (decl), context_die);
17952       add_AT_flag (var_die, DW_AT_external, 1);
17953       if (loc)
17954         {
17955           if (off)
17956             {
17957               /* Optimize the common case.  */
17958               if (single_element_loc_list_p (loc)
17959                   && loc->expr->dw_loc_opc == DW_OP_addr
17960                   && loc->expr->dw_loc_next == NULL
17961                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17962                 loc->expr->dw_loc_oprnd1.v.val_addr
17963                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17964               else
17965                 loc_list_plus_const (loc, off);
17966             }
17967           add_AT_location_description (var_die, DW_AT_location, loc);
17968         }
17969       else if (DECL_EXTERNAL (decl))
17970         add_AT_flag (var_die, DW_AT_declaration, 1);
17971       equate_decl_number_to_die (decl, var_die);
17972       return;
17973     }
17974
17975   /* If the compiler emitted a definition for the DECL declaration
17976      and if we already emitted a DIE for it, don't emit a second
17977      DIE for it again. Allow re-declarations of DECLs that are
17978      inside functions, though.  */
17979   if (old_die && declaration && !local_scope_p (context_die))
17980     return;
17981
17982   /* For static data members, the declaration in the class is supposed
17983      to have DW_TAG_member tag; the specification should still be
17984      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17985   if (declaration && class_scope_p (context_die))
17986     var_die = new_die (DW_TAG_member, context_die, decl);
17987   else
17988     var_die = new_die (DW_TAG_variable, context_die, decl);
17989
17990   origin_die = NULL;
17991   if (origin != NULL)
17992     origin_die = add_abstract_origin_attribute (var_die, origin);
17993
17994   /* Loop unrolling can create multiple blocks that refer to the same
17995      static variable, so we must test for the DW_AT_declaration flag.
17996
17997      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17998      copy decls and set the DECL_ABSTRACT flag on them instead of
17999      sharing them.
18000
18001      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18002
18003      ??? The declare_in_namespace support causes us to get two DIEs for one
18004      variable, both of which are declarations.  We want to avoid considering
18005      one to be a specification, so we must test that this DIE is not a
18006      declaration.  */
18007   else if (old_die && TREE_STATIC (decl) && ! declaration
18008            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18009     {
18010       /* This is a definition of a C++ class level static.  */
18011       add_AT_specification (var_die, old_die);
18012       specialization_p = true;
18013       if (DECL_NAME (decl))
18014         {
18015           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18016           struct dwarf_file_data * file_index = lookup_filename (s.file);
18017
18018           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18019             add_AT_file (var_die, DW_AT_decl_file, file_index);
18020
18021           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18022             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18023
18024           if (old_die->die_tag == DW_TAG_member)
18025             add_linkage_name (var_die, decl);
18026         }
18027     }
18028   else
18029     add_name_and_src_coords_attributes (var_die, decl);
18030
18031   if ((origin == NULL && !specialization_p)
18032       || (origin != NULL
18033           && !DECL_ABSTRACT (decl_or_origin)
18034           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18035                                        decl_function_context
18036                                                         (decl_or_origin))))
18037     {
18038       tree type = TREE_TYPE (decl_or_origin);
18039
18040       if (decl_by_reference_p (decl_or_origin))
18041         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18042       else
18043         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18044                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
18045     }
18046
18047   if (origin == NULL && !specialization_p)
18048     {
18049       if (TREE_PUBLIC (decl))
18050         add_AT_flag (var_die, DW_AT_external, 1);
18051
18052       if (DECL_ARTIFICIAL (decl))
18053         add_AT_flag (var_die, DW_AT_artificial, 1);
18054
18055       add_accessibility_attribute (var_die, decl);
18056     }
18057
18058   if (declaration)
18059     add_AT_flag (var_die, DW_AT_declaration, 1);
18060
18061   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18062     equate_decl_number_to_die (decl, var_die);
18063
18064   if (! declaration
18065       && (! DECL_ABSTRACT (decl_or_origin)
18066           /* Local static vars are shared between all clones/inlines,
18067              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18068              already set.  */
18069           || (TREE_CODE (decl_or_origin) == VAR_DECL
18070               && TREE_STATIC (decl_or_origin)
18071               && DECL_RTL_SET_P (decl_or_origin)))
18072       /* When abstract origin already has DW_AT_location attribute, no need
18073          to add it again.  */
18074       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18075     {
18076       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18077           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18078         defer_location (decl_or_origin, var_die);
18079       else
18080         add_location_or_const_value_attribute (var_die, decl_or_origin,
18081                                                decl == NULL, DW_AT_location);
18082       add_pubname (decl_or_origin, var_die);
18083     }
18084   else
18085     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18086 }
18087
18088 /* Generate a DIE to represent a named constant.  */
18089
18090 static void
18091 gen_const_die (tree decl, dw_die_ref context_die)
18092 {
18093   dw_die_ref const_die;
18094   tree type = TREE_TYPE (decl);
18095
18096   const_die = new_die (DW_TAG_constant, context_die, decl);
18097   add_name_and_src_coords_attributes (const_die, decl);
18098   add_type_attribute (const_die, type, 1, 0, context_die);
18099   if (TREE_PUBLIC (decl))
18100     add_AT_flag (const_die, DW_AT_external, 1);
18101   if (DECL_ARTIFICIAL (decl))
18102     add_AT_flag (const_die, DW_AT_artificial, 1);
18103   tree_add_const_value_attribute_for_decl (const_die, decl);
18104 }
18105
18106 /* Generate a DIE to represent a label identifier.  */
18107
18108 static void
18109 gen_label_die (tree decl, dw_die_ref context_die)
18110 {
18111   tree origin = decl_ultimate_origin (decl);
18112   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18113   rtx insn;
18114   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18115
18116   if (origin != NULL)
18117     add_abstract_origin_attribute (lbl_die, origin);
18118   else
18119     add_name_and_src_coords_attributes (lbl_die, decl);
18120
18121   if (DECL_ABSTRACT (decl))
18122     equate_decl_number_to_die (decl, lbl_die);
18123   else
18124     {
18125       insn = DECL_RTL_IF_SET (decl);
18126
18127       /* Deleted labels are programmer specified labels which have been
18128          eliminated because of various optimizations.  We still emit them
18129          here so that it is possible to put breakpoints on them.  */
18130       if (insn
18131           && (LABEL_P (insn)
18132               || ((NOTE_P (insn)
18133                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18134         {
18135           /* When optimization is enabled (via -O) some parts of the compiler
18136              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18137              represent source-level labels which were explicitly declared by
18138              the user.  This really shouldn't be happening though, so catch
18139              it if it ever does happen.  */
18140           gcc_assert (!INSN_DELETED_P (insn));
18141
18142           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18143           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18144         }
18145       else if (insn
18146                && NOTE_P (insn)
18147                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18148                && CODE_LABEL_NUMBER (insn) != -1)
18149         {
18150           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18151           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18152         }
18153     }
18154 }
18155
18156 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18157    attributes to the DIE for a block STMT, to describe where the inlined
18158    function was called from.  This is similar to add_src_coords_attributes.  */
18159
18160 static inline void
18161 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18162 {
18163   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18164
18165   if (dwarf_version >= 3 || !dwarf_strict)
18166     {
18167       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18168       add_AT_unsigned (die, DW_AT_call_line, s.line);
18169     }
18170 }
18171
18172
18173 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18174    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18175
18176 static inline void
18177 add_high_low_attributes (tree stmt, dw_die_ref die)
18178 {
18179   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18180
18181   if (BLOCK_FRAGMENT_CHAIN (stmt)
18182       && (dwarf_version >= 3 || !dwarf_strict))
18183     {
18184       tree chain;
18185
18186       if (inlined_function_outer_scope_p (stmt))
18187         {
18188           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18189                                        BLOCK_NUMBER (stmt));
18190           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18191         }
18192
18193       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18194
18195       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18196       do
18197         {
18198           add_ranges (chain);
18199           chain = BLOCK_FRAGMENT_CHAIN (chain);
18200         }
18201       while (chain);
18202       add_ranges (NULL);
18203     }
18204   else
18205     {
18206       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18207                                    BLOCK_NUMBER (stmt));
18208       add_AT_lbl_id (die, DW_AT_low_pc, label);
18209       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18210                                    BLOCK_NUMBER (stmt));
18211       add_AT_lbl_id (die, DW_AT_high_pc, label);
18212     }
18213 }
18214
18215 /* Generate a DIE for a lexical block.  */
18216
18217 static void
18218 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18219 {
18220   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18221
18222   if (call_arg_locations)
18223     {
18224       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18225         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18226                                BLOCK_NUMBER (stmt) + 1);
18227       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18228     }
18229
18230   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18231     add_high_low_attributes (stmt, stmt_die);
18232
18233   decls_for_scope (stmt, stmt_die, depth);
18234 }
18235
18236 /* Generate a DIE for an inlined subprogram.  */
18237
18238 static void
18239 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18240 {
18241   tree decl;
18242
18243   /* The instance of function that is effectively being inlined shall not
18244      be abstract.  */
18245   gcc_assert (! BLOCK_ABSTRACT (stmt));
18246
18247   decl = block_ultimate_origin (stmt);
18248
18249   /* Emit info for the abstract instance first, if we haven't yet.  We
18250      must emit this even if the block is abstract, otherwise when we
18251      emit the block below (or elsewhere), we may end up trying to emit
18252      a die whose origin die hasn't been emitted, and crashing.  */
18253   dwarf2out_abstract_function (decl);
18254
18255   if (! BLOCK_ABSTRACT (stmt))
18256     {
18257       dw_die_ref subr_die
18258         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18259
18260       if (call_arg_locations)
18261         {
18262           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18263             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18264                                    BLOCK_NUMBER (stmt) + 1);
18265           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18266         }
18267       add_abstract_origin_attribute (subr_die, decl);
18268       if (TREE_ASM_WRITTEN (stmt))
18269         add_high_low_attributes (stmt, subr_die);
18270       add_call_src_coords_attributes (stmt, subr_die);
18271
18272       decls_for_scope (stmt, subr_die, depth);
18273       current_function_has_inlines = 1;
18274     }
18275 }
18276
18277 /* Generate a DIE for a field in a record, or structure.  */
18278
18279 static void
18280 gen_field_die (tree decl, dw_die_ref context_die)
18281 {
18282   dw_die_ref decl_die;
18283
18284   if (TREE_TYPE (decl) == error_mark_node)
18285     return;
18286
18287   decl_die = new_die (DW_TAG_member, context_die, decl);
18288   add_name_and_src_coords_attributes (decl_die, decl);
18289   add_type_attribute (decl_die, member_declared_type (decl),
18290                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18291                       context_die);
18292
18293   if (DECL_BIT_FIELD_TYPE (decl))
18294     {
18295       add_byte_size_attribute (decl_die, decl);
18296       add_bit_size_attribute (decl_die, decl);
18297       add_bit_offset_attribute (decl_die, decl);
18298     }
18299
18300   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18301     add_data_member_location_attribute (decl_die, decl);
18302
18303   if (DECL_ARTIFICIAL (decl))
18304     add_AT_flag (decl_die, DW_AT_artificial, 1);
18305
18306   add_accessibility_attribute (decl_die, decl);
18307
18308   /* Equate decl number to die, so that we can look up this decl later on.  */
18309   equate_decl_number_to_die (decl, decl_die);
18310 }
18311
18312 #if 0
18313 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18314    Use modified_type_die instead.
18315    We keep this code here just in case these types of DIEs may be needed to
18316    represent certain things in other languages (e.g. Pascal) someday.  */
18317
18318 static void
18319 gen_pointer_type_die (tree type, dw_die_ref context_die)
18320 {
18321   dw_die_ref ptr_die
18322     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18323
18324   equate_type_number_to_die (type, ptr_die);
18325   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18326   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18327 }
18328
18329 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18330    Use modified_type_die instead.
18331    We keep this code here just in case these types of DIEs may be needed to
18332    represent certain things in other languages (e.g. Pascal) someday.  */
18333
18334 static void
18335 gen_reference_type_die (tree type, dw_die_ref context_die)
18336 {
18337   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18338
18339   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18340     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18341   else
18342     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18343
18344   equate_type_number_to_die (type, ref_die);
18345   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18346   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18347 }
18348 #endif
18349
18350 /* Generate a DIE for a pointer to a member type.  */
18351
18352 static void
18353 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18354 {
18355   dw_die_ref ptr_die
18356     = new_die (DW_TAG_ptr_to_member_type,
18357                scope_die_for (type, context_die), type);
18358
18359   equate_type_number_to_die (type, ptr_die);
18360   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18361                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18362   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18363 }
18364
18365 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18366 DEF_VEC_P(dchar_p);
18367 DEF_VEC_ALLOC_P(dchar_p,heap);
18368
18369 static char *producer_string;
18370
18371 /* Return a heap allocated producer string including command line options
18372    if -grecord-gcc-switches.  */
18373
18374 static char *
18375 gen_producer_string (void)
18376 {
18377   size_t j;
18378   VEC(dchar_p, heap) *switches = NULL;
18379   const char *language_string = lang_hooks.name;
18380   char *producer, *tail;
18381   const char *p;
18382   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18383   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18384
18385   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18386     switch (save_decoded_options[j].opt_index)
18387       {
18388       case OPT_o:
18389       case OPT_d:
18390       case OPT_dumpbase:
18391       case OPT_dumpdir:
18392       case OPT_auxbase:
18393       case OPT_auxbase_strip:
18394       case OPT_quiet:
18395       case OPT_version:
18396       case OPT_v:
18397       case OPT_w:
18398       case OPT_L:
18399       case OPT_D:
18400       case OPT_I:
18401       case OPT_U:
18402       case OPT_SPECIAL_unknown:
18403       case OPT_SPECIAL_ignore:
18404       case OPT_SPECIAL_program_name:
18405       case OPT_SPECIAL_input_file:
18406       case OPT_grecord_gcc_switches:
18407       case OPT_gno_record_gcc_switches:
18408       case OPT__output_pch_:
18409       case OPT_fdiagnostics_show_location_:
18410       case OPT_fdiagnostics_show_option:
18411       case OPT_fverbose_asm:
18412       case OPT____:
18413       case OPT__sysroot_:
18414       case OPT_nostdinc:
18415       case OPT_nostdinc__:
18416         /* Ignore these.  */
18417         continue;
18418       default:
18419         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18420                              == '-');
18421         switch (save_decoded_options[j].canonical_option[0][1])
18422           {
18423           case 'M':
18424           case 'i':
18425           case 'W':
18426             continue;
18427           case 'f':
18428             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18429                          "dump", 4) == 0)
18430               continue;
18431             break;
18432           default:
18433             break;
18434           }
18435         VEC_safe_push (dchar_p, heap, switches,
18436                        save_decoded_options[j].orig_option_with_args_text);
18437         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18438         break;
18439       }
18440
18441   producer = XNEWVEC (char, plen + 1 + len + 1);
18442   tail = producer;
18443   sprintf (tail, "%s %s", language_string, version_string);
18444   tail += plen;
18445
18446   if (!dwarf_record_gcc_switches)
18447     {
18448 #ifdef MIPS_DEBUGGING_INFO
18449       /* The MIPS/SGI compilers place the 'cc' command line options in the
18450          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18451          if they do not appear in the producer string, the debugger reaches
18452          the conclusion that the object file is stripped and has no debugging
18453          information.  To get the MIPS/SGI debugger to believe that there is
18454          debugging information in the object file, we add a -g to the producer
18455          string.  */
18456       if (debug_info_level > DINFO_LEVEL_TERSE)
18457         {
18458           memcpy (tail, " -g", 3);
18459           tail += 3;
18460         }
18461 #endif
18462     }
18463
18464   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18465     {
18466       len = strlen (p);
18467       *tail = ' ';
18468       memcpy (tail + 1, p, len);
18469       tail += len + 1;
18470     }
18471
18472   *tail = '\0';
18473   VEC_free (dchar_p, heap, switches);
18474   return producer;
18475 }
18476
18477 /* Generate the DIE for the compilation unit.  */
18478
18479 static dw_die_ref
18480 gen_compile_unit_die (const char *filename)
18481 {
18482   dw_die_ref die;
18483   const char *language_string = lang_hooks.name;
18484   int language;
18485
18486   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18487
18488   if (filename)
18489     {
18490       add_name_attribute (die, filename);
18491       /* Don't add cwd for <built-in>.  */
18492       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18493         add_comp_dir_attribute (die);
18494     }
18495
18496   if (producer_string == NULL)
18497     producer_string = gen_producer_string ();
18498   add_AT_string (die, DW_AT_producer, producer_string);
18499
18500   /* If our producer is LTO try to figure out a common language to use
18501      from the global list of translation units.  */
18502   if (strcmp (language_string, "GNU GIMPLE") == 0)
18503     {
18504       unsigned i;
18505       tree t;
18506       const char *common_lang = NULL;
18507
18508       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18509         {
18510           if (!TRANSLATION_UNIT_LANGUAGE (t))
18511             continue;
18512           if (!common_lang)
18513             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18514           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18515             ;
18516           else if (strncmp (common_lang, "GNU C", 5) == 0
18517                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18518             /* Mixing C and C++ is ok, use C++ in that case.  */
18519             common_lang = "GNU C++";
18520           else
18521             {
18522               /* Fall back to C.  */
18523               common_lang = NULL;
18524               break;
18525             }
18526         }
18527
18528       if (common_lang)
18529         language_string = common_lang;
18530     }
18531
18532   language = DW_LANG_C89;
18533   if (strcmp (language_string, "GNU C++") == 0)
18534     language = DW_LANG_C_plus_plus;
18535   else if (strcmp (language_string, "GNU F77") == 0)
18536     language = DW_LANG_Fortran77;
18537   else if (strcmp (language_string, "GNU Pascal") == 0)
18538     language = DW_LANG_Pascal83;
18539   else if (dwarf_version >= 3 || !dwarf_strict)
18540     {
18541       if (strcmp (language_string, "GNU Ada") == 0)
18542         language = DW_LANG_Ada95;
18543       else if (strcmp (language_string, "GNU Fortran") == 0)
18544         language = DW_LANG_Fortran95;
18545       else if (strcmp (language_string, "GNU Java") == 0)
18546         language = DW_LANG_Java;
18547       else if (strcmp (language_string, "GNU Objective-C") == 0)
18548         language = DW_LANG_ObjC;
18549       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18550         language = DW_LANG_ObjC_plus_plus;
18551       else if (dwarf_version >= 5 || !dwarf_strict)
18552         {
18553           if (strcmp (language_string, "GNU Go") == 0)
18554             language = DW_LANG_Go;
18555         }
18556     }
18557
18558   add_AT_unsigned (die, DW_AT_language, language);
18559
18560   switch (language)
18561     {
18562     case DW_LANG_Fortran77:
18563     case DW_LANG_Fortran90:
18564     case DW_LANG_Fortran95:
18565       /* Fortran has case insensitive identifiers and the front-end
18566          lowercases everything.  */
18567       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18568       break;
18569     default:
18570       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18571       break;
18572     }
18573   return die;
18574 }
18575
18576 /* Generate the DIE for a base class.  */
18577
18578 static void
18579 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18580 {
18581   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18582
18583   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18584   add_data_member_location_attribute (die, binfo);
18585
18586   if (BINFO_VIRTUAL_P (binfo))
18587     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18588
18589   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18590      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18591      the default has always been DW_ACCESS_private.  */
18592   if (access == access_public_node)
18593     {
18594       if (dwarf_version == 2
18595           || context_die->die_tag == DW_TAG_class_type)
18596       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18597     }
18598   else if (access == access_protected_node)
18599     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18600   else if (dwarf_version > 2
18601            && context_die->die_tag != DW_TAG_class_type)
18602     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18603 }
18604
18605 /* Generate a DIE for a class member.  */
18606
18607 static void
18608 gen_member_die (tree type, dw_die_ref context_die)
18609 {
18610   tree member;
18611   tree binfo = TYPE_BINFO (type);
18612   dw_die_ref child;
18613
18614   /* If this is not an incomplete type, output descriptions of each of its
18615      members. Note that as we output the DIEs necessary to represent the
18616      members of this record or union type, we will also be trying to output
18617      DIEs to represent the *types* of those members. However the `type'
18618      function (above) will specifically avoid generating type DIEs for member
18619      types *within* the list of member DIEs for this (containing) type except
18620      for those types (of members) which are explicitly marked as also being
18621      members of this (containing) type themselves.  The g++ front- end can
18622      force any given type to be treated as a member of some other (containing)
18623      type by setting the TYPE_CONTEXT of the given (member) type to point to
18624      the TREE node representing the appropriate (containing) type.  */
18625
18626   /* First output info about the base classes.  */
18627   if (binfo)
18628     {
18629       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18630       int i;
18631       tree base;
18632
18633       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18634         gen_inheritance_die (base,
18635                              (accesses ? VEC_index (tree, accesses, i)
18636                               : access_public_node), context_die);
18637     }
18638
18639   /* Now output info about the data members and type members.  */
18640   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18641     {
18642       /* If we thought we were generating minimal debug info for TYPE
18643          and then changed our minds, some of the member declarations
18644          may have already been defined.  Don't define them again, but
18645          do put them in the right order.  */
18646
18647       child = lookup_decl_die (member);
18648       if (child)
18649         splice_child_die (context_die, child);
18650       else
18651         gen_decl_die (member, NULL, context_die);
18652     }
18653
18654   /* Now output info about the function members (if any).  */
18655   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18656     {
18657       /* Don't include clones in the member list.  */
18658       if (DECL_ABSTRACT_ORIGIN (member))
18659         continue;
18660
18661       child = lookup_decl_die (member);
18662       if (child)
18663         splice_child_die (context_die, child);
18664       else
18665         gen_decl_die (member, NULL, context_die);
18666     }
18667 }
18668
18669 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18670    is set, we pretend that the type was never defined, so we only get the
18671    member DIEs needed by later specification DIEs.  */
18672
18673 static void
18674 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18675                                 enum debug_info_usage usage)
18676 {
18677   dw_die_ref type_die = lookup_type_die (type);
18678   dw_die_ref scope_die = 0;
18679   int nested = 0;
18680   int complete = (TYPE_SIZE (type)
18681                   && (! TYPE_STUB_DECL (type)
18682                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18683   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18684   complete = complete && should_emit_struct_debug (type, usage);
18685
18686   if (type_die && ! complete)
18687     return;
18688
18689   if (TYPE_CONTEXT (type) != NULL_TREE
18690       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18691           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18692     nested = 1;
18693
18694   scope_die = scope_die_for (type, context_die);
18695
18696   if (! type_die || (nested && is_cu_die (scope_die)))
18697     /* First occurrence of type or toplevel definition of nested class.  */
18698     {
18699       dw_die_ref old_die = type_die;
18700
18701       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18702                           ? record_type_tag (type) : DW_TAG_union_type,
18703                           scope_die, type);
18704       equate_type_number_to_die (type, type_die);
18705       if (old_die)
18706         add_AT_specification (type_die, old_die);
18707       else
18708         add_name_attribute (type_die, type_tag (type));
18709     }
18710   else
18711     remove_AT (type_die, DW_AT_declaration);
18712
18713   /* Generate child dies for template paramaters.  */
18714   if (debug_info_level > DINFO_LEVEL_TERSE
18715       && COMPLETE_TYPE_P (type))
18716     schedule_generic_params_dies_gen (type);
18717
18718   /* If this type has been completed, then give it a byte_size attribute and
18719      then give a list of members.  */
18720   if (complete && !ns_decl)
18721     {
18722       /* Prevent infinite recursion in cases where the type of some member of
18723          this type is expressed in terms of this type itself.  */
18724       TREE_ASM_WRITTEN (type) = 1;
18725       add_byte_size_attribute (type_die, type);
18726       if (TYPE_STUB_DECL (type) != NULL_TREE)
18727         {
18728           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18729           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18730         }
18731
18732       /* If the first reference to this type was as the return type of an
18733          inline function, then it may not have a parent.  Fix this now.  */
18734       if (type_die->die_parent == NULL)
18735         add_child_die (scope_die, type_die);
18736
18737       push_decl_scope (type);
18738       gen_member_die (type, type_die);
18739       pop_decl_scope ();
18740
18741       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18742       if (TYPE_ARTIFICIAL (type))
18743         add_AT_flag (type_die, DW_AT_artificial, 1);
18744
18745       /* GNU extension: Record what type our vtable lives in.  */
18746       if (TYPE_VFIELD (type))
18747         {
18748           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18749
18750           gen_type_die (vtype, context_die);
18751           add_AT_die_ref (type_die, DW_AT_containing_type,
18752                           lookup_type_die (vtype));
18753         }
18754     }
18755   else
18756     {
18757       add_AT_flag (type_die, DW_AT_declaration, 1);
18758
18759       /* We don't need to do this for function-local types.  */
18760       if (TYPE_STUB_DECL (type)
18761           && ! decl_function_context (TYPE_STUB_DECL (type)))
18762         VEC_safe_push (tree, gc, incomplete_types, type);
18763     }
18764
18765   if (get_AT (type_die, DW_AT_name))
18766     add_pubtype (type, type_die);
18767 }
18768
18769 /* Generate a DIE for a subroutine _type_.  */
18770
18771 static void
18772 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18773 {
18774   tree return_type = TREE_TYPE (type);
18775   dw_die_ref subr_die
18776     = new_die (DW_TAG_subroutine_type,
18777                scope_die_for (type, context_die), type);
18778
18779   equate_type_number_to_die (type, subr_die);
18780   add_prototyped_attribute (subr_die, type);
18781   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18782   gen_formal_types_die (type, subr_die);
18783
18784   if (get_AT (subr_die, DW_AT_name))
18785     add_pubtype (type, subr_die);
18786 }
18787
18788 /* Generate a DIE for a type definition.  */
18789
18790 static void
18791 gen_typedef_die (tree decl, dw_die_ref context_die)
18792 {
18793   dw_die_ref type_die;
18794   tree origin;
18795
18796   if (TREE_ASM_WRITTEN (decl))
18797     return;
18798
18799   TREE_ASM_WRITTEN (decl) = 1;
18800   type_die = new_die (DW_TAG_typedef, context_die, decl);
18801   origin = decl_ultimate_origin (decl);
18802   if (origin != NULL)
18803     add_abstract_origin_attribute (type_die, origin);
18804   else
18805     {
18806       tree type;
18807
18808       add_name_and_src_coords_attributes (type_die, decl);
18809       if (DECL_ORIGINAL_TYPE (decl))
18810         {
18811           type = DECL_ORIGINAL_TYPE (decl);
18812
18813           gcc_assert (type != TREE_TYPE (decl));
18814           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18815         }
18816       else
18817         {
18818           type = TREE_TYPE (decl);
18819
18820           if (is_naming_typedef_decl (TYPE_NAME (type)))
18821             {
18822               /* Here, we are in the case of decl being a typedef naming
18823                  an anonymous type, e.g:
18824                      typedef struct {...} foo;
18825                  In that case TREE_TYPE (decl) is not a typedef variant
18826                  type and TYPE_NAME of the anonymous type is set to the
18827                  TYPE_DECL of the typedef. This construct is emitted by
18828                  the C++ FE.
18829
18830                  TYPE is the anonymous struct named by the typedef
18831                  DECL. As we need the DW_AT_type attribute of the
18832                  DW_TAG_typedef to point to the DIE of TYPE, let's
18833                  generate that DIE right away. add_type_attribute
18834                  called below will then pick (via lookup_type_die) that
18835                  anonymous struct DIE.  */
18836               if (!TREE_ASM_WRITTEN (type))
18837                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18838
18839               /* This is a GNU Extension.  We are adding a
18840                  DW_AT_linkage_name attribute to the DIE of the
18841                  anonymous struct TYPE.  The value of that attribute
18842                  is the name of the typedef decl naming the anonymous
18843                  struct.  This greatly eases the work of consumers of
18844                  this debug info.  */
18845               add_linkage_attr (lookup_type_die (type), decl);
18846             }
18847         }
18848
18849       add_type_attribute (type_die, type, TREE_READONLY (decl),
18850                           TREE_THIS_VOLATILE (decl), context_die);
18851
18852       if (is_naming_typedef_decl (decl))
18853         /* We want that all subsequent calls to lookup_type_die with
18854            TYPE in argument yield the DW_TAG_typedef we have just
18855            created.  */
18856         equate_type_number_to_die (type, type_die);
18857
18858       add_accessibility_attribute (type_die, decl);
18859     }
18860
18861   if (DECL_ABSTRACT (decl))
18862     equate_decl_number_to_die (decl, type_die);
18863
18864   if (get_AT (type_die, DW_AT_name))
18865     add_pubtype (decl, type_die);
18866 }
18867
18868 /* Generate a DIE for a struct, class, enum or union type.  */
18869
18870 static void
18871 gen_tagged_type_die (tree type,
18872                      dw_die_ref context_die,
18873                      enum debug_info_usage usage)
18874 {
18875   int need_pop;
18876
18877   if (type == NULL_TREE
18878       || !is_tagged_type (type))
18879     return;
18880
18881   /* If this is a nested type whose containing class hasn't been written
18882      out yet, writing it out will cover this one, too.  This does not apply
18883      to instantiations of member class templates; they need to be added to
18884      the containing class as they are generated.  FIXME: This hurts the
18885      idea of combining type decls from multiple TUs, since we can't predict
18886      what set of template instantiations we'll get.  */
18887   if (TYPE_CONTEXT (type)
18888       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18889       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18890     {
18891       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18892
18893       if (TREE_ASM_WRITTEN (type))
18894         return;
18895
18896       /* If that failed, attach ourselves to the stub.  */
18897       push_decl_scope (TYPE_CONTEXT (type));
18898       context_die = lookup_type_die (TYPE_CONTEXT (type));
18899       need_pop = 1;
18900     }
18901   else if (TYPE_CONTEXT (type) != NULL_TREE
18902            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18903     {
18904       /* If this type is local to a function that hasn't been written
18905          out yet, use a NULL context for now; it will be fixed up in
18906          decls_for_scope.  */
18907       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18908       /* A declaration DIE doesn't count; nested types need to go in the
18909          specification.  */
18910       if (context_die && is_declaration_die (context_die))
18911         context_die = NULL;
18912       need_pop = 0;
18913     }
18914   else
18915     {
18916       context_die = declare_in_namespace (type, context_die);
18917       need_pop = 0;
18918     }
18919
18920   if (TREE_CODE (type) == ENUMERAL_TYPE)
18921     {
18922       /* This might have been written out by the call to
18923          declare_in_namespace.  */
18924       if (!TREE_ASM_WRITTEN (type))
18925         gen_enumeration_type_die (type, context_die);
18926     }
18927   else
18928     gen_struct_or_union_type_die (type, context_die, usage);
18929
18930   if (need_pop)
18931     pop_decl_scope ();
18932
18933   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18934      it up if it is ever completed.  gen_*_type_die will set it for us
18935      when appropriate.  */
18936 }
18937
18938 /* Generate a type description DIE.  */
18939
18940 static void
18941 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18942                          enum debug_info_usage usage)
18943 {
18944   struct array_descr_info info;
18945
18946   if (type == NULL_TREE || type == error_mark_node)
18947     return;
18948
18949   if (TYPE_NAME (type) != NULL_TREE
18950       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18951       && is_redundant_typedef (TYPE_NAME (type))
18952       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18953     /* The DECL of this type is a typedef we don't want to emit debug
18954        info for but we want debug info for its underlying typedef.
18955        This can happen for e.g, the injected-class-name of a C++
18956        type.  */
18957     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18958
18959   /* If TYPE is a typedef type variant, let's generate debug info
18960      for the parent typedef which TYPE is a type of.  */
18961   if (typedef_variant_p (type))
18962     {
18963       if (TREE_ASM_WRITTEN (type))
18964         return;
18965
18966       /* Prevent broken recursion; we can't hand off to the same type.  */
18967       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18968
18969       /* Give typedefs the right scope.  */
18970       context_die = scope_die_for (type, context_die);
18971
18972       TREE_ASM_WRITTEN (type) = 1;
18973
18974       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18975       return;
18976     }
18977
18978   /* If type is an anonymous tagged type named by a typedef, let's
18979      generate debug info for the typedef.  */
18980   if (is_naming_typedef_decl (TYPE_NAME (type)))
18981     {
18982       /* Use the DIE of the containing namespace as the parent DIE of
18983          the type description DIE we want to generate.  */
18984       if (DECL_CONTEXT (TYPE_NAME (type))
18985           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18986         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18987       
18988       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18989       return;
18990     }
18991
18992   /* If this is an array type with hidden descriptor, handle it first.  */
18993   if (!TREE_ASM_WRITTEN (type)
18994       && lang_hooks.types.get_array_descr_info
18995       && lang_hooks.types.get_array_descr_info (type, &info)
18996       && (dwarf_version >= 3 || !dwarf_strict))
18997     {
18998       gen_descr_array_type_die (type, &info, context_die);
18999       TREE_ASM_WRITTEN (type) = 1;
19000       return;
19001     }
19002
19003   /* We are going to output a DIE to represent the unqualified version
19004      of this type (i.e. without any const or volatile qualifiers) so
19005      get the main variant (i.e. the unqualified version) of this type
19006      now.  (Vectors are special because the debugging info is in the
19007      cloned type itself).  */
19008   if (TREE_CODE (type) != VECTOR_TYPE)
19009     type = type_main_variant (type);
19010
19011   if (TREE_ASM_WRITTEN (type))
19012     return;
19013
19014   switch (TREE_CODE (type))
19015     {
19016     case ERROR_MARK:
19017       break;
19018
19019     case POINTER_TYPE:
19020     case REFERENCE_TYPE:
19021       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19022          ensures that the gen_type_die recursion will terminate even if the
19023          type is recursive.  Recursive types are possible in Ada.  */
19024       /* ??? We could perhaps do this for all types before the switch
19025          statement.  */
19026       TREE_ASM_WRITTEN (type) = 1;
19027
19028       /* For these types, all that is required is that we output a DIE (or a
19029          set of DIEs) to represent the "basis" type.  */
19030       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19031                                 DINFO_USAGE_IND_USE);
19032       break;
19033
19034     case OFFSET_TYPE:
19035       /* This code is used for C++ pointer-to-data-member types.
19036          Output a description of the relevant class type.  */
19037       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19038                                         DINFO_USAGE_IND_USE);
19039
19040       /* Output a description of the type of the object pointed to.  */
19041       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19042                                         DINFO_USAGE_IND_USE);
19043
19044       /* Now output a DIE to represent this pointer-to-data-member type
19045          itself.  */
19046       gen_ptr_to_mbr_type_die (type, context_die);
19047       break;
19048
19049     case FUNCTION_TYPE:
19050       /* Force out return type (in case it wasn't forced out already).  */
19051       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19052                                         DINFO_USAGE_DIR_USE);
19053       gen_subroutine_type_die (type, context_die);
19054       break;
19055
19056     case METHOD_TYPE:
19057       /* Force out return type (in case it wasn't forced out already).  */
19058       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19059                                         DINFO_USAGE_DIR_USE);
19060       gen_subroutine_type_die (type, context_die);
19061       break;
19062
19063     case ARRAY_TYPE:
19064       gen_array_type_die (type, context_die);
19065       break;
19066
19067     case VECTOR_TYPE:
19068       gen_array_type_die (type, context_die);
19069       break;
19070
19071     case ENUMERAL_TYPE:
19072     case RECORD_TYPE:
19073     case UNION_TYPE:
19074     case QUAL_UNION_TYPE:
19075       gen_tagged_type_die (type, context_die, usage);
19076       return;
19077
19078     case VOID_TYPE:
19079     case INTEGER_TYPE:
19080     case REAL_TYPE:
19081     case FIXED_POINT_TYPE:
19082     case COMPLEX_TYPE:
19083     case BOOLEAN_TYPE:
19084       /* No DIEs needed for fundamental types.  */
19085       break;
19086
19087     case NULLPTR_TYPE:
19088     case LANG_TYPE:
19089       /* Just use DW_TAG_unspecified_type.  */
19090       {
19091         dw_die_ref type_die = lookup_type_die (type);
19092         if (type_die == NULL)
19093           {
19094             tree name = TYPE_NAME (type);
19095             if (TREE_CODE (name) == TYPE_DECL)
19096               name = DECL_NAME (name);
19097             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19098             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19099             equate_type_number_to_die (type, type_die);
19100           }
19101       }
19102       break;
19103
19104     default:
19105       gcc_unreachable ();
19106     }
19107
19108   TREE_ASM_WRITTEN (type) = 1;
19109 }
19110
19111 static void
19112 gen_type_die (tree type, dw_die_ref context_die)
19113 {
19114   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19115 }
19116
19117 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19118    things which are local to the given block.  */
19119
19120 static void
19121 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19122 {
19123   int must_output_die = 0;
19124   bool inlined_func;
19125
19126   /* Ignore blocks that are NULL.  */
19127   if (stmt == NULL_TREE)
19128     return;
19129
19130   inlined_func = inlined_function_outer_scope_p (stmt);
19131
19132   /* If the block is one fragment of a non-contiguous block, do not
19133      process the variables, since they will have been done by the
19134      origin block.  Do process subblocks.  */
19135   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19136     {
19137       tree sub;
19138
19139       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19140         gen_block_die (sub, context_die, depth + 1);
19141
19142       return;
19143     }
19144
19145   /* Determine if we need to output any Dwarf DIEs at all to represent this
19146      block.  */
19147   if (inlined_func)
19148     /* The outer scopes for inlinings *must* always be represented.  We
19149        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19150     must_output_die = 1;
19151   else
19152     {
19153       /* Determine if this block directly contains any "significant"
19154          local declarations which we will need to output DIEs for.  */
19155       if (debug_info_level > DINFO_LEVEL_TERSE)
19156         /* We are not in terse mode so *any* local declaration counts
19157            as being a "significant" one.  */
19158         must_output_die = ((BLOCK_VARS (stmt) != NULL
19159                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19160                            && (TREE_USED (stmt)
19161                                || TREE_ASM_WRITTEN (stmt)
19162                                || BLOCK_ABSTRACT (stmt)));
19163       else if ((TREE_USED (stmt)
19164                 || TREE_ASM_WRITTEN (stmt)
19165                 || BLOCK_ABSTRACT (stmt))
19166                && !dwarf2out_ignore_block (stmt))
19167         must_output_die = 1;
19168     }
19169
19170   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19171      DIE for any block which contains no significant local declarations at
19172      all.  Rather, in such cases we just call `decls_for_scope' so that any
19173      needed Dwarf info for any sub-blocks will get properly generated. Note
19174      that in terse mode, our definition of what constitutes a "significant"
19175      local declaration gets restricted to include only inlined function
19176      instances and local (nested) function definitions.  */
19177   if (must_output_die)
19178     {
19179       if (inlined_func)
19180         {
19181           /* If STMT block is abstract, that means we have been called
19182              indirectly from dwarf2out_abstract_function.
19183              That function rightfully marks the descendent blocks (of
19184              the abstract function it is dealing with) as being abstract,
19185              precisely to prevent us from emitting any
19186              DW_TAG_inlined_subroutine DIE as a descendent
19187              of an abstract function instance. So in that case, we should
19188              not call gen_inlined_subroutine_die.
19189
19190              Later though, when cgraph asks dwarf2out to emit info
19191              for the concrete instance of the function decl into which
19192              the concrete instance of STMT got inlined, the later will lead
19193              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19194           if (! BLOCK_ABSTRACT (stmt))
19195             gen_inlined_subroutine_die (stmt, context_die, depth);
19196         }
19197       else
19198         gen_lexical_block_die (stmt, context_die, depth);
19199     }
19200   else
19201     decls_for_scope (stmt, context_die, depth);
19202 }
19203
19204 /* Process variable DECL (or variable with origin ORIGIN) within
19205    block STMT and add it to CONTEXT_DIE.  */
19206 static void
19207 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19208 {
19209   dw_die_ref die;
19210   tree decl_or_origin = decl ? decl : origin;
19211
19212   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19213     die = lookup_decl_die (decl_or_origin);
19214   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19215            && TYPE_DECL_IS_STUB (decl_or_origin))
19216     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19217   else
19218     die = NULL;
19219
19220   if (die != NULL && die->die_parent == NULL)
19221     add_child_die (context_die, die);
19222   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19223     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19224                                          stmt, context_die);
19225   else
19226     gen_decl_die (decl, origin, context_die);
19227 }
19228
19229 /* Generate all of the decls declared within a given scope and (recursively)
19230    all of its sub-blocks.  */
19231
19232 static void
19233 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19234 {
19235   tree decl;
19236   unsigned int i;
19237   tree subblocks;
19238
19239   /* Ignore NULL blocks.  */
19240   if (stmt == NULL_TREE)
19241     return;
19242
19243   /* Output the DIEs to represent all of the data objects and typedefs
19244      declared directly within this block but not within any nested
19245      sub-blocks.  Also, nested function and tag DIEs have been
19246      generated with a parent of NULL; fix that up now.  */
19247   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19248     process_scope_var (stmt, decl, NULL_TREE, context_die);
19249   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19250     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19251                        context_die);
19252
19253   /* If we're at -g1, we're not interested in subblocks.  */
19254   if (debug_info_level <= DINFO_LEVEL_TERSE)
19255     return;
19256
19257   /* Output the DIEs to represent all sub-blocks (and the items declared
19258      therein) of this block.  */
19259   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19260        subblocks != NULL;
19261        subblocks = BLOCK_CHAIN (subblocks))
19262     gen_block_die (subblocks, context_die, depth + 1);
19263 }
19264
19265 /* Is this a typedef we can avoid emitting?  */
19266
19267 static inline int
19268 is_redundant_typedef (const_tree decl)
19269 {
19270   if (TYPE_DECL_IS_STUB (decl))
19271     return 1;
19272
19273   if (DECL_ARTIFICIAL (decl)
19274       && DECL_CONTEXT (decl)
19275       && is_tagged_type (DECL_CONTEXT (decl))
19276       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19277       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19278     /* Also ignore the artificial member typedef for the class name.  */
19279     return 1;
19280
19281   return 0;
19282 }
19283
19284 /* Return TRUE if TYPE is a typedef that names a type for linkage
19285    purposes. This kind of typedefs is produced by the C++ FE for
19286    constructs like:
19287
19288    typedef struct {...} foo;
19289
19290    In that case, there is no typedef variant type produced for foo.
19291    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19292    struct type.  */
19293
19294 static bool
19295 is_naming_typedef_decl (const_tree decl)
19296 {
19297   if (decl == NULL_TREE
19298       || TREE_CODE (decl) != TYPE_DECL
19299       || !is_tagged_type (TREE_TYPE (decl))
19300       || DECL_IS_BUILTIN (decl)
19301       || is_redundant_typedef (decl)
19302       /* It looks like Ada produces TYPE_DECLs that are very similar
19303          to C++ naming typedefs but that have different
19304          semantics. Let's be specific to c++ for now.  */
19305       || !is_cxx ())
19306     return FALSE;
19307
19308   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19309           && TYPE_NAME (TREE_TYPE (decl)) == decl
19310           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19311               != TYPE_NAME (TREE_TYPE (decl))));
19312 }
19313
19314 /* Returns the DIE for a context.  */
19315
19316 static inline dw_die_ref
19317 get_context_die (tree context)
19318 {
19319   if (context)
19320     {
19321       /* Find die that represents this context.  */
19322       if (TYPE_P (context))
19323         {
19324           context = TYPE_MAIN_VARIANT (context);
19325           return strip_naming_typedef (context, force_type_die (context));
19326         }
19327       else
19328         return force_decl_die (context);
19329     }
19330   return comp_unit_die ();
19331 }
19332
19333 /* Returns the DIE for decl.  A DIE will always be returned.  */
19334
19335 static dw_die_ref
19336 force_decl_die (tree decl)
19337 {
19338   dw_die_ref decl_die;
19339   unsigned saved_external_flag;
19340   tree save_fn = NULL_TREE;
19341   decl_die = lookup_decl_die (decl);
19342   if (!decl_die)
19343     {
19344       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19345
19346       decl_die = lookup_decl_die (decl);
19347       if (decl_die)
19348         return decl_die;
19349
19350       switch (TREE_CODE (decl))
19351         {
19352         case FUNCTION_DECL:
19353           /* Clear current_function_decl, so that gen_subprogram_die thinks
19354              that this is a declaration. At this point, we just want to force
19355              declaration die.  */
19356           save_fn = current_function_decl;
19357           current_function_decl = NULL_TREE;
19358           gen_subprogram_die (decl, context_die);
19359           current_function_decl = save_fn;
19360           break;
19361
19362         case VAR_DECL:
19363           /* Set external flag to force declaration die. Restore it after
19364            gen_decl_die() call.  */
19365           saved_external_flag = DECL_EXTERNAL (decl);
19366           DECL_EXTERNAL (decl) = 1;
19367           gen_decl_die (decl, NULL, context_die);
19368           DECL_EXTERNAL (decl) = saved_external_flag;
19369           break;
19370
19371         case NAMESPACE_DECL:
19372           if (dwarf_version >= 3 || !dwarf_strict)
19373             dwarf2out_decl (decl);
19374           else
19375             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19376             decl_die = comp_unit_die ();
19377           break;
19378
19379         case TRANSLATION_UNIT_DECL:
19380           decl_die = comp_unit_die ();
19381           break;
19382
19383         default:
19384           gcc_unreachable ();
19385         }
19386
19387       /* We should be able to find the DIE now.  */
19388       if (!decl_die)
19389         decl_die = lookup_decl_die (decl);
19390       gcc_assert (decl_die);
19391     }
19392
19393   return decl_die;
19394 }
19395
19396 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19397    always returned.  */
19398
19399 static dw_die_ref
19400 force_type_die (tree type)
19401 {
19402   dw_die_ref type_die;
19403
19404   type_die = lookup_type_die (type);
19405   if (!type_die)
19406     {
19407       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19408
19409       type_die = modified_type_die (type, TYPE_READONLY (type),
19410                                     TYPE_VOLATILE (type), context_die);
19411       gcc_assert (type_die);
19412     }
19413   return type_die;
19414 }
19415
19416 /* Force out any required namespaces to be able to output DECL,
19417    and return the new context_die for it, if it's changed.  */
19418
19419 static dw_die_ref
19420 setup_namespace_context (tree thing, dw_die_ref context_die)
19421 {
19422   tree context = (DECL_P (thing)
19423                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19424   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19425     /* Force out the namespace.  */
19426     context_die = force_decl_die (context);
19427
19428   return context_die;
19429 }
19430
19431 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19432    type) within its namespace, if appropriate.
19433
19434    For compatibility with older debuggers, namespace DIEs only contain
19435    declarations; all definitions are emitted at CU scope.  */
19436
19437 static dw_die_ref
19438 declare_in_namespace (tree thing, dw_die_ref context_die)
19439 {
19440   dw_die_ref ns_context;
19441
19442   if (debug_info_level <= DINFO_LEVEL_TERSE)
19443     return context_die;
19444
19445   /* If this decl is from an inlined function, then don't try to emit it in its
19446      namespace, as we will get confused.  It would have already been emitted
19447      when the abstract instance of the inline function was emitted anyways.  */
19448   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19449     return context_die;
19450
19451   ns_context = setup_namespace_context (thing, context_die);
19452
19453   if (ns_context != context_die)
19454     {
19455       if (is_fortran ())
19456         return ns_context;
19457       if (DECL_P (thing))
19458         gen_decl_die (thing, NULL, ns_context);
19459       else
19460         gen_type_die (thing, ns_context);
19461     }
19462   return context_die;
19463 }
19464
19465 /* Generate a DIE for a namespace or namespace alias.  */
19466
19467 static void
19468 gen_namespace_die (tree decl, dw_die_ref context_die)
19469 {
19470   dw_die_ref namespace_die;
19471
19472   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19473      they are an alias of.  */
19474   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19475     {
19476       /* Output a real namespace or module.  */
19477       context_die = setup_namespace_context (decl, comp_unit_die ());
19478       namespace_die = new_die (is_fortran ()
19479                                ? DW_TAG_module : DW_TAG_namespace,
19480                                context_die, decl);
19481       /* For Fortran modules defined in different CU don't add src coords.  */
19482       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19483         {
19484           const char *name = dwarf2_name (decl, 0);
19485           if (name)
19486             add_name_attribute (namespace_die, name);
19487         }
19488       else
19489         add_name_and_src_coords_attributes (namespace_die, decl);
19490       if (DECL_EXTERNAL (decl))
19491         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19492       equate_decl_number_to_die (decl, namespace_die);
19493     }
19494   else
19495     {
19496       /* Output a namespace alias.  */
19497
19498       /* Force out the namespace we are an alias of, if necessary.  */
19499       dw_die_ref origin_die
19500         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19501
19502       if (DECL_FILE_SCOPE_P (decl)
19503           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19504         context_die = setup_namespace_context (decl, comp_unit_die ());
19505       /* Now create the namespace alias DIE.  */
19506       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19507       add_name_and_src_coords_attributes (namespace_die, decl);
19508       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19509       equate_decl_number_to_die (decl, namespace_die);
19510     }
19511 }
19512
19513 /* Generate Dwarf debug information for a decl described by DECL.
19514    The return value is currently only meaningful for PARM_DECLs,
19515    for all other decls it returns NULL.  */
19516
19517 static dw_die_ref
19518 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19519 {
19520   tree decl_or_origin = decl ? decl : origin;
19521   tree class_origin = NULL, ultimate_origin;
19522
19523   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19524     return NULL;
19525
19526   switch (TREE_CODE (decl_or_origin))
19527     {
19528     case ERROR_MARK:
19529       break;
19530
19531     case CONST_DECL:
19532       if (!is_fortran () && !is_ada ())
19533         {
19534           /* The individual enumerators of an enum type get output when we output
19535              the Dwarf representation of the relevant enum type itself.  */
19536           break;
19537         }
19538
19539       /* Emit its type.  */
19540       gen_type_die (TREE_TYPE (decl), context_die);
19541
19542       /* And its containing namespace.  */
19543       context_die = declare_in_namespace (decl, context_die);
19544
19545       gen_const_die (decl, context_die);
19546       break;
19547
19548     case FUNCTION_DECL:
19549       /* Don't output any DIEs to represent mere function declarations,
19550          unless they are class members or explicit block externs.  */
19551       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19552           && DECL_FILE_SCOPE_P (decl_or_origin)
19553           && (current_function_decl == NULL_TREE
19554               || DECL_ARTIFICIAL (decl_or_origin)))
19555         break;
19556
19557 #if 0
19558       /* FIXME */
19559       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19560          on local redeclarations of global functions.  That seems broken.  */
19561       if (current_function_decl != decl)
19562         /* This is only a declaration.  */;
19563 #endif
19564
19565       /* If we're emitting a clone, emit info for the abstract instance.  */
19566       if (origin || DECL_ORIGIN (decl) != decl)
19567         dwarf2out_abstract_function (origin
19568                                      ? DECL_ORIGIN (origin)
19569                                      : DECL_ABSTRACT_ORIGIN (decl));
19570
19571       /* If we're emitting an out-of-line copy of an inline function,
19572          emit info for the abstract instance and set up to refer to it.  */
19573       else if (cgraph_function_possibly_inlined_p (decl)
19574                && ! DECL_ABSTRACT (decl)
19575                && ! class_or_namespace_scope_p (context_die)
19576                /* dwarf2out_abstract_function won't emit a die if this is just
19577                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19578                   that case, because that works only if we have a die.  */
19579                && DECL_INITIAL (decl) != NULL_TREE)
19580         {
19581           dwarf2out_abstract_function (decl);
19582           set_decl_origin_self (decl);
19583         }
19584
19585       /* Otherwise we're emitting the primary DIE for this decl.  */
19586       else if (debug_info_level > DINFO_LEVEL_TERSE)
19587         {
19588           /* Before we describe the FUNCTION_DECL itself, make sure that we
19589              have its containing type.  */
19590           if (!origin)
19591             origin = decl_class_context (decl);
19592           if (origin != NULL_TREE)
19593             gen_type_die (origin, context_die);
19594
19595           /* And its return type.  */
19596           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19597
19598           /* And its virtual context.  */
19599           if (DECL_VINDEX (decl) != NULL_TREE)
19600             gen_type_die (DECL_CONTEXT (decl), context_die);
19601
19602           /* Make sure we have a member DIE for decl.  */
19603           if (origin != NULL_TREE)
19604             gen_type_die_for_member (origin, decl, context_die);
19605
19606           /* And its containing namespace.  */
19607           context_die = declare_in_namespace (decl, context_die);
19608         }
19609
19610       /* Now output a DIE to represent the function itself.  */
19611       if (decl)
19612         gen_subprogram_die (decl, context_die);
19613       break;
19614
19615     case TYPE_DECL:
19616       /* If we are in terse mode, don't generate any DIEs to represent any
19617          actual typedefs.  */
19618       if (debug_info_level <= DINFO_LEVEL_TERSE)
19619         break;
19620
19621       /* In the special case of a TYPE_DECL node representing the declaration
19622          of some type tag, if the given TYPE_DECL is marked as having been
19623          instantiated from some other (original) TYPE_DECL node (e.g. one which
19624          was generated within the original definition of an inline function) we
19625          used to generate a special (abbreviated) DW_TAG_structure_type,
19626          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19627          should be actually referencing those DIEs, as variable DIEs with that
19628          type would be emitted already in the abstract origin, so it was always
19629          removed during unused type prunning.  Don't add anything in this
19630          case.  */
19631       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19632         break;
19633
19634       if (is_redundant_typedef (decl))
19635         gen_type_die (TREE_TYPE (decl), context_die);
19636       else
19637         /* Output a DIE to represent the typedef itself.  */
19638         gen_typedef_die (decl, context_die);
19639       break;
19640
19641     case LABEL_DECL:
19642       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19643         gen_label_die (decl, context_die);
19644       break;
19645
19646     case VAR_DECL:
19647     case RESULT_DECL:
19648       /* If we are in terse mode, don't generate any DIEs to represent any
19649          variable declarations or definitions.  */
19650       if (debug_info_level <= DINFO_LEVEL_TERSE)
19651         break;
19652
19653       /* Output any DIEs that are needed to specify the type of this data
19654          object.  */
19655       if (decl_by_reference_p (decl_or_origin))
19656         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19657       else
19658         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19659
19660       /* And its containing type.  */
19661       class_origin = decl_class_context (decl_or_origin);
19662       if (class_origin != NULL_TREE)
19663         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19664
19665       /* And its containing namespace.  */
19666       context_die = declare_in_namespace (decl_or_origin, context_die);
19667
19668       /* Now output the DIE to represent the data object itself.  This gets
19669          complicated because of the possibility that the VAR_DECL really
19670          represents an inlined instance of a formal parameter for an inline
19671          function.  */
19672       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19673       if (ultimate_origin != NULL_TREE
19674           && TREE_CODE (ultimate_origin) == PARM_DECL)
19675         gen_formal_parameter_die (decl, origin,
19676                                   true /* Emit name attribute.  */,
19677                                   context_die);
19678       else
19679         gen_variable_die (decl, origin, context_die);
19680       break;
19681
19682     case FIELD_DECL:
19683       /* Ignore the nameless fields that are used to skip bits but handle C++
19684          anonymous unions and structs.  */
19685       if (DECL_NAME (decl) != NULL_TREE
19686           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19687           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19688         {
19689           gen_type_die (member_declared_type (decl), context_die);
19690           gen_field_die (decl, context_die);
19691         }
19692       break;
19693
19694     case PARM_DECL:
19695       if (DECL_BY_REFERENCE (decl_or_origin))
19696         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19697       else
19698         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19699       return gen_formal_parameter_die (decl, origin,
19700                                        true /* Emit name attribute.  */,
19701                                        context_die);
19702
19703     case NAMESPACE_DECL:
19704     case IMPORTED_DECL:
19705       if (dwarf_version >= 3 || !dwarf_strict)
19706         gen_namespace_die (decl, context_die);
19707       break;
19708
19709     default:
19710       /* Probably some frontend-internal decl.  Assume we don't care.  */
19711       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19712       break;
19713     }
19714
19715   return NULL;
19716 }
19717 \f
19718 /* Output debug information for global decl DECL.  Called from toplev.c after
19719    compilation proper has finished.  */
19720
19721 static void
19722 dwarf2out_global_decl (tree decl)
19723 {
19724   /* Output DWARF2 information for file-scope tentative data object
19725      declarations, file-scope (extern) function declarations (which
19726      had no corresponding body) and file-scope tagged type declarations
19727      and definitions which have not yet been forced out.  */
19728   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19729     dwarf2out_decl (decl);
19730 }
19731
19732 /* Output debug information for type decl DECL.  Called from toplev.c
19733    and from language front ends (to record built-in types).  */
19734 static void
19735 dwarf2out_type_decl (tree decl, int local)
19736 {
19737   if (!local)
19738     dwarf2out_decl (decl);
19739 }
19740
19741 /* Output debug information for imported module or decl DECL.
19742    NAME is non-NULL name in the lexical block if the decl has been renamed.
19743    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19744    that DECL belongs to.
19745    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19746 static void
19747 dwarf2out_imported_module_or_decl_1 (tree decl,
19748                                      tree name,
19749                                      tree lexical_block,
19750                                      dw_die_ref lexical_block_die)
19751 {
19752   expanded_location xloc;
19753   dw_die_ref imported_die = NULL;
19754   dw_die_ref at_import_die;
19755
19756   if (TREE_CODE (decl) == IMPORTED_DECL)
19757     {
19758       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19759       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19760       gcc_assert (decl);
19761     }
19762   else
19763     xloc = expand_location (input_location);
19764
19765   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19766     {
19767       at_import_die = force_type_die (TREE_TYPE (decl));
19768       /* For namespace N { typedef void T; } using N::T; base_type_die
19769          returns NULL, but DW_TAG_imported_declaration requires
19770          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19771       if (!at_import_die)
19772         {
19773           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19774           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19775           at_import_die = lookup_type_die (TREE_TYPE (decl));
19776           gcc_assert (at_import_die);
19777         }
19778     }
19779   else
19780     {
19781       at_import_die = lookup_decl_die (decl);
19782       if (!at_import_die)
19783         {
19784           /* If we're trying to avoid duplicate debug info, we may not have
19785              emitted the member decl for this field.  Emit it now.  */
19786           if (TREE_CODE (decl) == FIELD_DECL)
19787             {
19788               tree type = DECL_CONTEXT (decl);
19789
19790               if (TYPE_CONTEXT (type)
19791                   && TYPE_P (TYPE_CONTEXT (type))
19792                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19793                                                 DINFO_USAGE_DIR_USE))
19794                 return;
19795               gen_type_die_for_member (type, decl,
19796                                        get_context_die (TYPE_CONTEXT (type)));
19797             }
19798           at_import_die = force_decl_die (decl);
19799         }
19800     }
19801
19802   if (TREE_CODE (decl) == NAMESPACE_DECL)
19803     {
19804       if (dwarf_version >= 3 || !dwarf_strict)
19805         imported_die = new_die (DW_TAG_imported_module,
19806                                 lexical_block_die,
19807                                 lexical_block);
19808       else
19809         return;
19810     }
19811   else
19812     imported_die = new_die (DW_TAG_imported_declaration,
19813                             lexical_block_die,
19814                             lexical_block);
19815
19816   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19817   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19818   if (name)
19819     add_AT_string (imported_die, DW_AT_name,
19820                    IDENTIFIER_POINTER (name));
19821   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19822 }
19823
19824 /* Output debug information for imported module or decl DECL.
19825    NAME is non-NULL name in context if the decl has been renamed.
19826    CHILD is true if decl is one of the renamed decls as part of
19827    importing whole module.  */
19828
19829 static void
19830 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19831                                    bool child)
19832 {
19833   /* dw_die_ref at_import_die;  */
19834   dw_die_ref scope_die;
19835
19836   if (debug_info_level <= DINFO_LEVEL_TERSE)
19837     return;
19838
19839   gcc_assert (decl);
19840
19841   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19842      We need decl DIE for reference and scope die. First, get DIE for the decl
19843      itself.  */
19844
19845   /* Get the scope die for decl context. Use comp_unit_die for global module
19846      or decl. If die is not found for non globals, force new die.  */
19847   if (context
19848       && TYPE_P (context)
19849       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19850     return;
19851
19852   if (!(dwarf_version >= 3 || !dwarf_strict))
19853     return;
19854
19855   scope_die = get_context_die (context);
19856
19857   if (child)
19858     {
19859       gcc_assert (scope_die->die_child);
19860       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19861       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19862       scope_die = scope_die->die_child;
19863     }
19864
19865   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19866   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19867
19868 }
19869
19870 /* Write the debugging output for DECL.  */
19871
19872 void
19873 dwarf2out_decl (tree decl)
19874 {
19875   dw_die_ref context_die = comp_unit_die ();
19876
19877   switch (TREE_CODE (decl))
19878     {
19879     case ERROR_MARK:
19880       return;
19881
19882     case FUNCTION_DECL:
19883       /* What we would really like to do here is to filter out all mere
19884          file-scope declarations of file-scope functions which are never
19885          referenced later within this translation unit (and keep all of ones
19886          that *are* referenced later on) but we aren't clairvoyant, so we have
19887          no idea which functions will be referenced in the future (i.e. later
19888          on within the current translation unit). So here we just ignore all
19889          file-scope function declarations which are not also definitions.  If
19890          and when the debugger needs to know something about these functions,
19891          it will have to hunt around and find the DWARF information associated
19892          with the definition of the function.
19893
19894          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19895          nodes represent definitions and which ones represent mere
19896          declarations.  We have to check DECL_INITIAL instead. That's because
19897          the C front-end supports some weird semantics for "extern inline"
19898          function definitions.  These can get inlined within the current
19899          translation unit (and thus, we need to generate Dwarf info for their
19900          abstract instances so that the Dwarf info for the concrete inlined
19901          instances can have something to refer to) but the compiler never
19902          generates any out-of-lines instances of such things (despite the fact
19903          that they *are* definitions).
19904
19905          The important point is that the C front-end marks these "extern
19906          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19907          them anyway. Note that the C++ front-end also plays some similar games
19908          for inline function definitions appearing within include files which
19909          also contain `#pragma interface' pragmas.  */
19910       if (DECL_INITIAL (decl) == NULL_TREE)
19911         return;
19912
19913       /* If we're a nested function, initially use a parent of NULL; if we're
19914          a plain function, this will be fixed up in decls_for_scope.  If
19915          we're a method, it will be ignored, since we already have a DIE.  */
19916       if (decl_function_context (decl)
19917           /* But if we're in terse mode, we don't care about scope.  */
19918           && debug_info_level > DINFO_LEVEL_TERSE)
19919         context_die = NULL;
19920       break;
19921
19922     case VAR_DECL:
19923       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19924          declaration and if the declaration was never even referenced from
19925          within this entire compilation unit.  We suppress these DIEs in
19926          order to save space in the .debug section (by eliminating entries
19927          which are probably useless).  Note that we must not suppress
19928          block-local extern declarations (whether used or not) because that
19929          would screw-up the debugger's name lookup mechanism and cause it to
19930          miss things which really ought to be in scope at a given point.  */
19931       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19932         return;
19933
19934       /* For local statics lookup proper context die.  */
19935       if (TREE_STATIC (decl)
19936           && DECL_CONTEXT (decl)
19937           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19938         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19939
19940       /* If we are in terse mode, don't generate any DIEs to represent any
19941          variable declarations or definitions.  */
19942       if (debug_info_level <= DINFO_LEVEL_TERSE)
19943         return;
19944       break;
19945
19946     case CONST_DECL:
19947       if (debug_info_level <= DINFO_LEVEL_TERSE)
19948         return;
19949       if (!is_fortran () && !is_ada ())
19950         return;
19951       if (TREE_STATIC (decl) && decl_function_context (decl))
19952         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19953       break;
19954
19955     case NAMESPACE_DECL:
19956     case IMPORTED_DECL:
19957       if (debug_info_level <= DINFO_LEVEL_TERSE)
19958         return;
19959       if (lookup_decl_die (decl) != NULL)
19960         return;
19961       break;
19962
19963     case TYPE_DECL:
19964       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19965       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19966         return;
19967
19968       /* Don't bother trying to generate any DIEs to represent any of the
19969          normal built-in types for the language we are compiling.  */
19970       if (DECL_IS_BUILTIN (decl))
19971         return;
19972
19973       /* If we are in terse mode, don't generate any DIEs for types.  */
19974       if (debug_info_level <= DINFO_LEVEL_TERSE)
19975         return;
19976
19977       /* If we're a function-scope tag, initially use a parent of NULL;
19978          this will be fixed up in decls_for_scope.  */
19979       if (decl_function_context (decl))
19980         context_die = NULL;
19981
19982       break;
19983
19984     default:
19985       return;
19986     }
19987
19988   gen_decl_die (decl, NULL, context_die);
19989 }
19990
19991 /* Write the debugging output for DECL.  */
19992
19993 static void
19994 dwarf2out_function_decl (tree decl)
19995 {
19996   dwarf2out_decl (decl);
19997   call_arg_locations = NULL;
19998   call_arg_loc_last = NULL;
19999   call_site_count = -1;
20000   tail_call_site_count = -1;
20001   VEC_free (dw_die_ref, heap, block_map);
20002   htab_empty (decl_loc_table);
20003   htab_empty (cached_dw_loc_list_table);
20004 }
20005
20006 /* Output a marker (i.e. a label) for the beginning of the generated code for
20007    a lexical block.  */
20008
20009 static void
20010 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20011                        unsigned int blocknum)
20012 {
20013   switch_to_section (current_function_section ());
20014   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20015 }
20016
20017 /* Output a marker (i.e. a label) for the end of the generated code for a
20018    lexical block.  */
20019
20020 static void
20021 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20022 {
20023   switch_to_section (current_function_section ());
20024   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20025 }
20026
20027 /* Returns nonzero if it is appropriate not to emit any debugging
20028    information for BLOCK, because it doesn't contain any instructions.
20029
20030    Don't allow this for blocks with nested functions or local classes
20031    as we would end up with orphans, and in the presence of scheduling
20032    we may end up calling them anyway.  */
20033
20034 static bool
20035 dwarf2out_ignore_block (const_tree block)
20036 {
20037   tree decl;
20038   unsigned int i;
20039
20040   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20041     if (TREE_CODE (decl) == FUNCTION_DECL
20042         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20043       return 0;
20044   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20045     {
20046       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20047       if (TREE_CODE (decl) == FUNCTION_DECL
20048           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20049       return 0;
20050     }
20051
20052   return 1;
20053 }
20054
20055 /* Hash table routines for file_hash.  */
20056
20057 static int
20058 file_table_eq (const void *p1_p, const void *p2_p)
20059 {
20060   const struct dwarf_file_data *const p1 =
20061     (const struct dwarf_file_data *) p1_p;
20062   const char *const p2 = (const char *) p2_p;
20063   return filename_cmp (p1->filename, p2) == 0;
20064 }
20065
20066 static hashval_t
20067 file_table_hash (const void *p_p)
20068 {
20069   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20070   return htab_hash_string (p->filename);
20071 }
20072
20073 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20074    dwarf2out.c) and return its "index".  The index of each (known) filename is
20075    just a unique number which is associated with only that one filename.  We
20076    need such numbers for the sake of generating labels (in the .debug_sfnames
20077    section) and references to those files numbers (in the .debug_srcinfo
20078    and.debug_macinfo sections).  If the filename given as an argument is not
20079    found in our current list, add it to the list and assign it the next
20080    available unique index number.  In order to speed up searches, we remember
20081    the index of the filename was looked up last.  This handles the majority of
20082    all searches.  */
20083
20084 static struct dwarf_file_data *
20085 lookup_filename (const char *file_name)
20086 {
20087   void ** slot;
20088   struct dwarf_file_data * created;
20089
20090   /* Check to see if the file name that was searched on the previous
20091      call matches this file name.  If so, return the index.  */
20092   if (file_table_last_lookup
20093       && (file_name == file_table_last_lookup->filename
20094           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20095     return file_table_last_lookup;
20096
20097   /* Didn't match the previous lookup, search the table.  */
20098   slot = htab_find_slot_with_hash (file_table, file_name,
20099                                    htab_hash_string (file_name), INSERT);
20100   if (*slot)
20101     return (struct dwarf_file_data *) *slot;
20102
20103   created = ggc_alloc_dwarf_file_data ();
20104   created->filename = file_name;
20105   created->emitted_number = 0;
20106   *slot = created;
20107   return created;
20108 }
20109
20110 /* If the assembler will construct the file table, then translate the compiler
20111    internal file table number into the assembler file table number, and emit
20112    a .file directive if we haven't already emitted one yet.  The file table
20113    numbers are different because we prune debug info for unused variables and
20114    types, which may include filenames.  */
20115
20116 static int
20117 maybe_emit_file (struct dwarf_file_data * fd)
20118 {
20119   if (! fd->emitted_number)
20120     {
20121       if (last_emitted_file)
20122         fd->emitted_number = last_emitted_file->emitted_number + 1;
20123       else
20124         fd->emitted_number = 1;
20125       last_emitted_file = fd;
20126
20127       if (DWARF2_ASM_LINE_DEBUG_INFO)
20128         {
20129           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20130           output_quoted_string (asm_out_file,
20131                                 remap_debug_filename (fd->filename));
20132           fputc ('\n', asm_out_file);
20133         }
20134     }
20135
20136   return fd->emitted_number;
20137 }
20138
20139 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20140    That generation should happen after function debug info has been
20141    generated. The value of the attribute is the constant value of ARG.  */
20142
20143 static void
20144 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20145 {
20146   die_arg_entry entry;
20147
20148   if (!die || !arg)
20149     return;
20150
20151   if (!tmpl_value_parm_die_table)
20152     tmpl_value_parm_die_table
20153       = VEC_alloc (die_arg_entry, gc, 32);
20154
20155   entry.die = die;
20156   entry.arg = arg;
20157   VEC_safe_push (die_arg_entry, gc,
20158                  tmpl_value_parm_die_table,
20159                  &entry);
20160 }
20161
20162 /* Return TRUE if T is an instance of generic type, FALSE
20163    otherwise.  */
20164
20165 static bool
20166 generic_type_p (tree t)
20167 {
20168   if (t == NULL_TREE || !TYPE_P (t))
20169     return false;
20170   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20171 }
20172
20173 /* Schedule the generation of the generic parameter dies for the
20174   instance of generic type T. The proper generation itself is later
20175   done by gen_scheduled_generic_parms_dies. */
20176
20177 static void
20178 schedule_generic_params_dies_gen (tree t)
20179 {
20180   if (!generic_type_p (t))
20181     return;
20182
20183   if (generic_type_instances == NULL)
20184     generic_type_instances = VEC_alloc (tree, gc, 256);
20185
20186   VEC_safe_push (tree, gc, generic_type_instances, t);
20187 }
20188
20189 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20190    by append_entry_to_tmpl_value_parm_die_table. This function must
20191    be called after function DIEs have been generated.  */
20192
20193 static void
20194 gen_remaining_tmpl_value_param_die_attribute (void)
20195 {
20196   if (tmpl_value_parm_die_table)
20197     {
20198       unsigned i;
20199       die_arg_entry *e;
20200
20201       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20202         tree_add_const_value_attribute (e->die, e->arg);
20203     }
20204 }
20205
20206 /* Generate generic parameters DIEs for instances of generic types
20207    that have been previously scheduled by
20208    schedule_generic_params_dies_gen. This function must be called
20209    after all the types of the CU have been laid out.  */
20210
20211 static void
20212 gen_scheduled_generic_parms_dies (void)
20213 {
20214   unsigned i;
20215   tree t;
20216
20217   if (generic_type_instances == NULL)
20218     return;
20219   
20220   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20221     gen_generic_params_dies (t);
20222 }
20223
20224
20225 /* Replace DW_AT_name for the decl with name.  */
20226
20227 static void
20228 dwarf2out_set_name (tree decl, tree name)
20229 {
20230   dw_die_ref die;
20231   dw_attr_ref attr;
20232   const char *dname;
20233
20234   die = TYPE_SYMTAB_DIE (decl);
20235   if (!die)
20236     return;
20237
20238   dname = dwarf2_name (name, 0);
20239   if (!dname)
20240     return;
20241
20242   attr = get_AT (die, DW_AT_name);
20243   if (attr)
20244     {
20245       struct indirect_string_node *node;
20246
20247       node = find_AT_string (dname);
20248       /* replace the string.  */
20249       attr->dw_attr_val.v.val_str = node;
20250     }
20251
20252   else
20253     add_name_attribute (die, dname);
20254 }
20255
20256 /* Called by the final INSN scan whenever we see a var location.  We
20257    use it to drop labels in the right places, and throw the location in
20258    our lookup table.  */
20259
20260 static void
20261 dwarf2out_var_location (rtx loc_note)
20262 {
20263   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20264   struct var_loc_node *newloc;
20265   rtx next_real, next_note;
20266   static const char *last_label;
20267   static const char *last_postcall_label;
20268   static bool last_in_cold_section_p;
20269   static rtx expected_next_loc_note;
20270   tree decl;
20271   bool var_loc_p;
20272
20273   if (!NOTE_P (loc_note))
20274     {
20275       if (CALL_P (loc_note))
20276         {
20277           call_site_count++;
20278           if (SIBLING_CALL_P (loc_note))
20279             tail_call_site_count++;
20280         }
20281       return;
20282     }
20283
20284   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20285   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20286     return;
20287
20288   /* Optimize processing a large consecutive sequence of location
20289      notes so we don't spend too much time in next_real_insn.  If the
20290      next insn is another location note, remember the next_real_insn
20291      calculation for next time.  */
20292   next_real = cached_next_real_insn;
20293   if (next_real)
20294     {
20295       if (expected_next_loc_note != loc_note)
20296         next_real = NULL_RTX;
20297     }
20298
20299   next_note = NEXT_INSN (loc_note);
20300   if (! next_note
20301       || INSN_DELETED_P (next_note)
20302       || GET_CODE (next_note) != NOTE
20303       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20304           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20305     next_note = NULL_RTX;
20306
20307   if (! next_real)
20308     next_real = next_real_insn (loc_note);
20309
20310   if (next_note)
20311     {
20312       expected_next_loc_note = next_note;
20313       cached_next_real_insn = next_real;
20314     }
20315   else
20316     cached_next_real_insn = NULL_RTX;
20317
20318   /* If there are no instructions which would be affected by this note,
20319      don't do anything.  */
20320   if (var_loc_p
20321       && next_real == NULL_RTX
20322       && !NOTE_DURING_CALL_P (loc_note))
20323     return;
20324
20325   if (next_real == NULL_RTX)
20326     next_real = get_last_insn ();
20327
20328   /* If there were any real insns between note we processed last time
20329      and this note (or if it is the first note), clear
20330      last_{,postcall_}label so that they are not reused this time.  */
20331   if (last_var_location_insn == NULL_RTX
20332       || last_var_location_insn != next_real
20333       || last_in_cold_section_p != in_cold_section_p)
20334     {
20335       last_label = NULL;
20336       last_postcall_label = NULL;
20337     }
20338
20339   if (var_loc_p)
20340     {
20341       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20342       newloc = add_var_loc_to_decl (decl, loc_note,
20343                                     NOTE_DURING_CALL_P (loc_note)
20344                                     ? last_postcall_label : last_label);
20345       if (newloc == NULL)
20346         return;
20347     }
20348   else
20349     {
20350       decl = NULL_TREE;
20351       newloc = NULL;
20352     }
20353
20354   /* If there were no real insns between note we processed last time
20355      and this note, use the label we emitted last time.  Otherwise
20356      create a new label and emit it.  */
20357   if (last_label == NULL)
20358     {
20359       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20360       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20361       loclabel_num++;
20362       last_label = ggc_strdup (loclabel);
20363     }
20364
20365   if (!var_loc_p)
20366     {
20367       struct call_arg_loc_node *ca_loc
20368         = ggc_alloc_cleared_call_arg_loc_node ();
20369       rtx prev = prev_real_insn (loc_note), x;
20370       ca_loc->call_arg_loc_note = loc_note;
20371       ca_loc->next = NULL;
20372       ca_loc->label = last_label;
20373       gcc_assert (prev
20374                   && (CALL_P (prev)
20375                       || (NONJUMP_INSN_P (prev)
20376                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20377                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20378       if (!CALL_P (prev))
20379         prev = XVECEXP (PATTERN (prev), 0, 0);
20380       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20381       x = PATTERN (prev);
20382       if (GET_CODE (x) == PARALLEL)
20383         x = XVECEXP (x, 0, 0);
20384       if (GET_CODE (x) == SET)
20385         x = SET_SRC (x);
20386       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20387         {
20388           x = XEXP (XEXP (x, 0), 0);
20389           if (GET_CODE (x) == SYMBOL_REF
20390               && SYMBOL_REF_DECL (x)
20391               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20392             ca_loc->symbol_ref = x;
20393         }
20394       ca_loc->block = insn_scope (prev);
20395       if (call_arg_locations)
20396         call_arg_loc_last->next = ca_loc;
20397       else
20398         call_arg_locations = ca_loc;
20399       call_arg_loc_last = ca_loc;
20400     }
20401   else if (!NOTE_DURING_CALL_P (loc_note))
20402     newloc->label = last_label;
20403   else
20404     {
20405       if (!last_postcall_label)
20406         {
20407           sprintf (loclabel, "%s-1", last_label);
20408           last_postcall_label = ggc_strdup (loclabel);
20409         }
20410       newloc->label = last_postcall_label;
20411     }
20412
20413   last_var_location_insn = next_real;
20414   last_in_cold_section_p = in_cold_section_p;
20415 }
20416
20417 /* Note in one location list that text section has changed.  */
20418
20419 static int
20420 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20421 {
20422   var_loc_list *list = (var_loc_list *) *slot;
20423   if (list->first)
20424     list->last_before_switch
20425       = list->last->next ? list->last->next : list->last;
20426   return 1;
20427 }
20428
20429 /* Note in all location lists that text section has changed.  */
20430
20431 static void
20432 var_location_switch_text_section (void)
20433 {
20434   if (decl_loc_table == NULL)
20435     return;
20436
20437   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20438 }
20439
20440 /* Create a new line number table.  */
20441
20442 static dw_line_info_table *
20443 new_line_info_table (void)
20444 {
20445   dw_line_info_table *table;
20446
20447   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20448   table->file_num = 1;
20449   table->line_num = 1;
20450   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20451
20452   return table;
20453 }
20454
20455 /* Lookup the "current" table into which we emit line info, so
20456    that we don't have to do it for every source line.  */
20457
20458 static void
20459 set_cur_line_info_table (section *sec)
20460 {
20461   dw_line_info_table *table;
20462
20463   if (sec == text_section)
20464     table = text_section_line_info;
20465   else if (sec == cold_text_section)
20466     {
20467       table = cold_text_section_line_info;
20468       if (!table)
20469         {
20470           cold_text_section_line_info = table = new_line_info_table ();
20471           table->end_label = cold_end_label;
20472         }
20473     }
20474   else
20475     {
20476       const char *end_label;
20477
20478       if (flag_reorder_blocks_and_partition)
20479         {
20480           if (in_cold_section_p)
20481             end_label = crtl->subsections.cold_section_end_label;
20482           else
20483             end_label = crtl->subsections.hot_section_end_label;
20484         }
20485       else
20486         {
20487           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20488           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20489                                        current_function_funcdef_no);
20490           end_label = ggc_strdup (label);
20491         }
20492
20493       table = new_line_info_table ();
20494       table->end_label = end_label;
20495
20496       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20497     }
20498
20499   if (DWARF2_ASM_LINE_DEBUG_INFO)
20500     table->is_stmt = (cur_line_info_table
20501                       ? cur_line_info_table->is_stmt
20502                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20503   cur_line_info_table = table;
20504 }
20505
20506
20507 /* We need to reset the locations at the beginning of each
20508    function. We can't do this in the end_function hook, because the
20509    declarations that use the locations won't have been output when
20510    that hook is called.  Also compute have_multiple_function_sections here.  */
20511
20512 static void
20513 dwarf2out_begin_function (tree fun)
20514 {
20515   section *sec = function_section (fun);
20516
20517   if (sec != text_section)
20518     have_multiple_function_sections = true;
20519
20520   if (flag_reorder_blocks_and_partition && !cold_text_section)
20521     {
20522       gcc_assert (current_function_decl == fun);
20523       cold_text_section = unlikely_text_section ();
20524       switch_to_section (cold_text_section);
20525       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20526       switch_to_section (sec);
20527     }
20528
20529   dwarf2out_note_section_used ();
20530   call_site_count = 0;
20531   tail_call_site_count = 0;
20532
20533   set_cur_line_info_table (sec);
20534 }
20535
20536 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20537
20538 static void
20539 push_dw_line_info_entry (dw_line_info_table *table,
20540                          enum dw_line_info_opcode opcode, unsigned int val)
20541 {
20542   dw_line_info_entry e;
20543   e.opcode = opcode;
20544   e.val = val;
20545   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20546 }
20547
20548 /* Output a label to mark the beginning of a source code line entry
20549    and record information relating to this source line, in
20550    'line_info_table' for later output of the .debug_line section.  */
20551 /* ??? The discriminator parameter ought to be unsigned.  */
20552
20553 static void
20554 dwarf2out_source_line (unsigned int line, const char *filename,
20555                        int discriminator, bool is_stmt)
20556 {
20557   unsigned int file_num;
20558   dw_line_info_table *table;
20559
20560   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20561     return;
20562
20563   /* The discriminator column was added in dwarf4.  Simplify the below
20564      by simply removing it if we're not supposed to output it.  */
20565   if (dwarf_version < 4 && dwarf_strict)
20566     discriminator = 0;
20567
20568   table = cur_line_info_table;
20569   file_num = maybe_emit_file (lookup_filename (filename));
20570
20571   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20572      the debugger has used the second (possibly duplicate) line number
20573      at the beginning of the function to mark the end of the prologue.
20574      We could eliminate any other duplicates within the function.  For
20575      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20576      that second line number entry.  */
20577   /* Recall that this end-of-prologue indication is *not* the same thing
20578      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20579      to which the hook corresponds, follows the last insn that was 
20580      emitted by gen_prologue.  What we need is to preceed the first insn
20581      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20582      insn that corresponds to something the user wrote.  These may be
20583      very different locations once scheduling is enabled.  */
20584
20585   if (0 && file_num == table->file_num
20586       && line == table->line_num
20587       && discriminator == table->discrim_num
20588       && is_stmt == table->is_stmt)
20589     return;
20590
20591   switch_to_section (current_function_section ());
20592
20593   /* If requested, emit something human-readable.  */
20594   if (flag_debug_asm)
20595     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20596
20597   if (DWARF2_ASM_LINE_DEBUG_INFO)
20598     {
20599       /* Emit the .loc directive understood by GNU as.  */
20600       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20601          file_num, line, is_stmt, discriminator */
20602       fputs ("\t.loc ", asm_out_file);
20603       fprint_ul (asm_out_file, file_num);
20604       putc (' ', asm_out_file);
20605       fprint_ul (asm_out_file, line);
20606       putc (' ', asm_out_file);
20607       putc ('0', asm_out_file);
20608
20609       if (is_stmt != table->is_stmt)
20610         {
20611           fputs (" is_stmt ", asm_out_file);
20612           putc (is_stmt ? '1' : '0', asm_out_file);
20613         }
20614       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20615         {
20616           gcc_assert (discriminator > 0);
20617           fputs (" discriminator ", asm_out_file);
20618           fprint_ul (asm_out_file, (unsigned long) discriminator);
20619         }
20620       putc ('\n', asm_out_file);
20621     }
20622   else
20623     {
20624       unsigned int label_num = ++line_info_label_num;
20625
20626       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20627
20628       push_dw_line_info_entry (table, LI_set_address, label_num);
20629       if (file_num != table->file_num)
20630         push_dw_line_info_entry (table, LI_set_file, file_num);
20631       if (discriminator != table->discrim_num)
20632         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20633       if (is_stmt != table->is_stmt)
20634         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20635       push_dw_line_info_entry (table, LI_set_line, line);
20636     }
20637
20638   table->file_num = file_num;
20639   table->line_num = line;
20640   table->discrim_num = discriminator;
20641   table->is_stmt = is_stmt;
20642   table->in_use = true;
20643 }
20644
20645 /* Record the beginning of a new source file.  */
20646
20647 static void
20648 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20649 {
20650   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20651     {
20652       /* Record the beginning of the file for break_out_includes.  */
20653       dw_die_ref bincl_die;
20654
20655       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20656       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20657     }
20658
20659   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20660     {
20661       macinfo_entry e;
20662       e.code = DW_MACINFO_start_file;
20663       e.lineno = lineno;
20664       e.info = ggc_strdup (filename);
20665       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20666     }
20667 }
20668
20669 /* Record the end of a source file.  */
20670
20671 static void
20672 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20673 {
20674   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20675     /* Record the end of the file for break_out_includes.  */
20676     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20677
20678   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20679     {
20680       macinfo_entry e;
20681       e.code = DW_MACINFO_end_file;
20682       e.lineno = lineno;
20683       e.info = NULL;
20684       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20685     }
20686 }
20687
20688 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20689    the tail part of the directive line, i.e. the part which is past the
20690    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20691
20692 static void
20693 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20694                   const char *buffer ATTRIBUTE_UNUSED)
20695 {
20696   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20697     {
20698       macinfo_entry e;
20699       /* Insert a dummy first entry to be able to optimize the whole
20700          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20701       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20702         {
20703           e.code = 0;
20704           e.lineno = 0;
20705           e.info = NULL;
20706           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20707         }
20708       e.code = DW_MACINFO_define;
20709       e.lineno = lineno;
20710       e.info = ggc_strdup (buffer);
20711       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20712     }
20713 }
20714
20715 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20716    the tail part of the directive line, i.e. the part which is past the
20717    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20718
20719 static void
20720 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20721                  const char *buffer ATTRIBUTE_UNUSED)
20722 {
20723   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20724     {
20725       macinfo_entry e;
20726       /* Insert a dummy first entry to be able to optimize the whole
20727          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20728       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20729         {
20730           e.code = 0;
20731           e.lineno = 0;
20732           e.info = NULL;
20733           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20734         }
20735       e.code = DW_MACINFO_undef;
20736       e.lineno = lineno;
20737       e.info = ggc_strdup (buffer);
20738       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20739     }
20740 }
20741
20742 /* Routines to manipulate hash table of CUs.  */
20743
20744 static hashval_t
20745 htab_macinfo_hash (const void *of)
20746 {
20747   const macinfo_entry *const entry =
20748     (const macinfo_entry *) of;
20749
20750   return htab_hash_string (entry->info);
20751 }
20752
20753 static int
20754 htab_macinfo_eq (const void *of1, const void *of2)
20755 {
20756   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20757   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20758
20759   return !strcmp (entry1->info, entry2->info);
20760 }
20761
20762 /* Output a single .debug_macinfo entry.  */
20763
20764 static void
20765 output_macinfo_op (macinfo_entry *ref)
20766 {
20767   int file_num;
20768   size_t len;
20769   struct indirect_string_node *node;
20770   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20771   struct dwarf_file_data *fd;
20772
20773   switch (ref->code)
20774     {
20775     case DW_MACINFO_start_file:
20776       fd = lookup_filename (ref->info);
20777       file_num = maybe_emit_file (fd);
20778       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20779       dw2_asm_output_data_uleb128 (ref->lineno,
20780                                    "Included from line number %lu", 
20781                                    (unsigned long) ref->lineno);
20782       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20783       break;
20784     case DW_MACINFO_end_file:
20785       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20786       break;
20787     case DW_MACINFO_define:
20788     case DW_MACINFO_undef:
20789       len = strlen (ref->info) + 1;
20790       if (!dwarf_strict
20791           && len > DWARF_OFFSET_SIZE
20792           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20793           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20794         {
20795           ref->code = ref->code == DW_MACINFO_define
20796                       ? DW_MACRO_GNU_define_indirect
20797                       : DW_MACRO_GNU_undef_indirect;
20798           output_macinfo_op (ref);
20799           return;
20800         }
20801       dw2_asm_output_data (1, ref->code,
20802                            ref->code == DW_MACINFO_define
20803                            ? "Define macro" : "Undefine macro");
20804       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20805                                    (unsigned long) ref->lineno);
20806       dw2_asm_output_nstring (ref->info, -1, "The macro");
20807       break;
20808     case DW_MACRO_GNU_define_indirect:
20809     case DW_MACRO_GNU_undef_indirect:
20810       node = find_AT_string (ref->info);
20811       if (node->form != DW_FORM_strp)
20812         {
20813           char label[32];
20814           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20815           ++dw2_string_counter;
20816           node->label = xstrdup (label);
20817           node->form = DW_FORM_strp;
20818         }
20819       dw2_asm_output_data (1, ref->code,
20820                            ref->code == DW_MACRO_GNU_define_indirect
20821                            ? "Define macro indirect"
20822                            : "Undefine macro indirect");
20823       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20824                                    (unsigned long) ref->lineno);
20825       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20826                              debug_str_section, "The macro: \"%s\"",
20827                              ref->info);
20828       break;
20829     case DW_MACRO_GNU_transparent_include:
20830       dw2_asm_output_data (1, ref->code, "Transparent include");
20831       ASM_GENERATE_INTERNAL_LABEL (label,
20832                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20833       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20834       break;
20835     default:
20836       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20837                ASM_COMMENT_START, (unsigned long) ref->code);
20838       break;
20839     }
20840 }
20841
20842 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20843    other compilation unit .debug_macinfo sections.  IDX is the first
20844    index of a define/undef, return the number of ops that should be
20845    emitted in a comdat .debug_macinfo section and emit
20846    a DW_MACRO_GNU_transparent_include entry referencing it.
20847    If the define/undef entry should be emitted normally, return 0.  */
20848
20849 static unsigned
20850 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20851                         htab_t *macinfo_htab)
20852 {
20853   macinfo_entry *first, *second, *cur, *inc;
20854   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20855   unsigned char checksum[16];
20856   struct md5_ctx ctx;
20857   char *grp_name, *tail;
20858   const char *base;
20859   unsigned int i, count, encoded_filename_len, linebuf_len;
20860   void **slot;
20861
20862   first = VEC_index (macinfo_entry, macinfo_table, idx);
20863   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20864
20865   /* Optimize only if there are at least two consecutive define/undef ops,
20866      and either all of them are before first DW_MACINFO_start_file
20867      with lineno {0,1} (i.e. predefined macro block), or all of them are
20868      in some included header file.  */
20869   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20870     return 0;
20871   if (VEC_empty (macinfo_entry, files))
20872     {
20873       if (first->lineno > 1 || second->lineno > 1)
20874         return 0;
20875     }
20876   else if (first->lineno == 0)
20877     return 0;
20878
20879   /* Find the last define/undef entry that can be grouped together
20880      with first and at the same time compute md5 checksum of their
20881      codes, linenumbers and strings.  */
20882   md5_init_ctx (&ctx);
20883   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20884     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20885       break;
20886     else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20887       break;
20888     else
20889       {
20890         unsigned char code = cur->code;
20891         md5_process_bytes (&code, 1, &ctx);
20892         checksum_uleb128 (cur->lineno, &ctx);
20893         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20894       }
20895   md5_finish_ctx (&ctx, checksum);
20896   count = i - idx;
20897
20898   /* From the containing include filename (if any) pick up just
20899      usable characters from its basename.  */
20900   if (VEC_empty (macinfo_entry, files))
20901     base = "";
20902   else
20903     base = lbasename (VEC_last (macinfo_entry, files)->info);
20904   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20905     if (ISIDNUM (base[i]) || base[i] == '.')
20906       encoded_filename_len++;
20907   /* Count . at the end.  */
20908   if (encoded_filename_len)
20909     encoded_filename_len++;
20910
20911   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20912   linebuf_len = strlen (linebuf);
20913
20914   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20915   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20916                          + 16 * 2 + 1);
20917   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20918   tail = grp_name + 4;
20919   if (encoded_filename_len)
20920     {
20921       for (i = 0; base[i]; i++)
20922         if (ISIDNUM (base[i]) || base[i] == '.')
20923           *tail++ = base[i];
20924       *tail++ = '.';
20925     }
20926   memcpy (tail, linebuf, linebuf_len);
20927   tail += linebuf_len;
20928   *tail++ = '.';
20929   for (i = 0; i < 16; i++)
20930     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20931
20932   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20933      in the empty vector entry before the first define/undef.  */
20934   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20935   inc->code = DW_MACRO_GNU_transparent_include;
20936   inc->lineno = 0;
20937   inc->info = ggc_strdup (grp_name);
20938   if (*macinfo_htab == NULL)
20939     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20940   /* Avoid emitting duplicates.  */
20941   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20942   if (*slot != NULL)
20943     {
20944       inc->code = 0;
20945       inc->info = NULL;
20946       /* If such an entry has been used before, just emit
20947          a DW_MACRO_GNU_transparent_include op.  */
20948       inc = (macinfo_entry *) *slot;
20949       output_macinfo_op (inc);
20950       /* And clear all macinfo_entry in the range to avoid emitting them
20951          in the second pass.  */
20952       for (i = idx;
20953            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20954            && i < idx + count;
20955            i++)
20956         {
20957           cur->code = 0;
20958           cur->info = NULL;
20959         }
20960     }
20961   else
20962     {
20963       *slot = inc;
20964       inc->lineno = htab_elements (*macinfo_htab);
20965       output_macinfo_op (inc);
20966     }
20967   return count;
20968 }
20969
20970 /* Output macinfo section(s).  */
20971
20972 static void
20973 output_macinfo (void)
20974 {
20975   unsigned i;
20976   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20977   macinfo_entry *ref;
20978   VEC (macinfo_entry, gc) *files = NULL;
20979   htab_t macinfo_htab = NULL;
20980
20981   if (! length)
20982     return;
20983
20984   /* output_macinfo* uses these interchangeably.  */
20985   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20986               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20987               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20988               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20989
20990   /* For .debug_macro emit the section header.  */
20991   if (!dwarf_strict)
20992     {
20993       dw2_asm_output_data (2, 4, "DWARF macro version number");
20994       if (DWARF_OFFSET_SIZE == 8)
20995         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20996       else
20997         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20998       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20999                              debug_line_section, NULL);
21000     }
21001
21002   /* In the first loop, it emits the primary .debug_macinfo section
21003      and after each emitted op the macinfo_entry is cleared.
21004      If a longer range of define/undef ops can be optimized using
21005      DW_MACRO_GNU_transparent_include, the
21006      DW_MACRO_GNU_transparent_include op is emitted and kept in
21007      the vector before the first define/undef in the range and the
21008      whole range of define/undef ops is not emitted and kept.  */
21009   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21010     {
21011       switch (ref->code)
21012         {
21013         case DW_MACINFO_start_file:
21014           VEC_safe_push (macinfo_entry, gc, files, ref);
21015           break;
21016         case DW_MACINFO_end_file:
21017           if (!VEC_empty (macinfo_entry, files))
21018             VEC_pop (macinfo_entry, files);
21019           break;
21020         case DW_MACINFO_define:
21021         case DW_MACINFO_undef:
21022           if (!dwarf_strict
21023               && HAVE_COMDAT_GROUP
21024               && VEC_length (macinfo_entry, files) != 1
21025               && i > 0
21026               && i + 1 < length
21027               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
21028             {
21029               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21030               if (count)
21031                 {
21032                   i += count - 1;
21033                   continue;
21034                 }
21035             }
21036           break;
21037         case 0:
21038           /* A dummy entry may be inserted at the beginning to be able
21039              to optimize the whole block of predefined macros.  */
21040           if (i == 0)
21041             continue;
21042         default:
21043           break;
21044         }
21045       output_macinfo_op (ref);
21046       ref->info = NULL;
21047       ref->code = 0;
21048     }
21049
21050   if (macinfo_htab == NULL)
21051     return;
21052
21053   htab_delete (macinfo_htab);
21054
21055   /* If any DW_MACRO_GNU_transparent_include were used, on those
21056      DW_MACRO_GNU_transparent_include entries terminate the
21057      current chain and switch to a new comdat .debug_macinfo
21058      section and emit the define/undef entries within it.  */
21059   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21060     switch (ref->code)
21061       {
21062       case 0:
21063         continue;
21064       case DW_MACRO_GNU_transparent_include:
21065         {
21066           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21067           tree comdat_key = get_identifier (ref->info);
21068           /* Terminate the previous .debug_macinfo section.  */
21069           dw2_asm_output_data (1, 0, "End compilation unit");
21070           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21071                                          SECTION_DEBUG
21072                                          | SECTION_LINKONCE,
21073                                          comdat_key);
21074           ASM_GENERATE_INTERNAL_LABEL (label,
21075                                        DEBUG_MACRO_SECTION_LABEL,
21076                                        ref->lineno);
21077           ASM_OUTPUT_LABEL (asm_out_file, label);
21078           ref->code = 0;
21079           ref->info = NULL;
21080           dw2_asm_output_data (2, 4, "DWARF macro version number");
21081           if (DWARF_OFFSET_SIZE == 8)
21082             dw2_asm_output_data (1, 1, "Flags: 64-bit");
21083           else
21084             dw2_asm_output_data (1, 0, "Flags: 32-bit");
21085         }
21086         break;
21087       case DW_MACINFO_define:
21088       case DW_MACINFO_undef:
21089         output_macinfo_op (ref);
21090         ref->code = 0;
21091         ref->info = NULL;
21092         break;
21093       default:
21094         gcc_unreachable ();
21095       }
21096 }
21097
21098 /* Set up for Dwarf output at the start of compilation.  */
21099
21100 static void
21101 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21102 {
21103   /* Allocate the file_table.  */
21104   file_table = htab_create_ggc (50, file_table_hash,
21105                                 file_table_eq, NULL);
21106
21107   /* Allocate the decl_die_table.  */
21108   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21109                                     decl_die_table_eq, NULL);
21110
21111   /* Allocate the decl_loc_table.  */
21112   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21113                                     decl_loc_table_eq, NULL);
21114
21115   /* Allocate the cached_dw_loc_list_table.  */
21116   cached_dw_loc_list_table
21117     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21118                        cached_dw_loc_list_table_eq, NULL);
21119
21120   /* Allocate the initial hunk of the decl_scope_table.  */
21121   decl_scope_table = VEC_alloc (tree, gc, 256);
21122
21123   /* Allocate the initial hunk of the abbrev_die_table.  */
21124   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21125     (ABBREV_DIE_TABLE_INCREMENT);
21126   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21127   /* Zero-th entry is allocated, but unused.  */
21128   abbrev_die_table_in_use = 1;
21129
21130   /* Allocate the pubtypes and pubnames vectors.  */
21131   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21132   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21133
21134   incomplete_types = VEC_alloc (tree, gc, 64);
21135
21136   used_rtx_array = VEC_alloc (rtx, gc, 32);
21137
21138   debug_info_section = get_section (DEBUG_INFO_SECTION,
21139                                     SECTION_DEBUG, NULL);
21140   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21141                                       SECTION_DEBUG, NULL);
21142   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21143                                        SECTION_DEBUG, NULL);
21144   debug_macinfo_section = get_section (dwarf_strict
21145                                        ? DEBUG_MACINFO_SECTION
21146                                        : DEBUG_MACRO_SECTION,
21147                                        SECTION_DEBUG, NULL);
21148   debug_line_section = get_section (DEBUG_LINE_SECTION,
21149                                     SECTION_DEBUG, NULL);
21150   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21151                                    SECTION_DEBUG, NULL);
21152   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21153                                         SECTION_DEBUG, NULL);
21154   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21155                                         SECTION_DEBUG, NULL);
21156   debug_str_section = get_section (DEBUG_STR_SECTION,
21157                                    DEBUG_STR_SECTION_FLAGS, NULL);
21158   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21159                                       SECTION_DEBUG, NULL);
21160   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21161                                      SECTION_DEBUG, NULL);
21162
21163   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21164   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21165                                DEBUG_ABBREV_SECTION_LABEL, 0);
21166   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21167   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21168                                COLD_TEXT_SECTION_LABEL, 0);
21169   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21170
21171   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21172                                DEBUG_INFO_SECTION_LABEL, 0);
21173   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21174                                DEBUG_LINE_SECTION_LABEL, 0);
21175   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21176                                DEBUG_RANGES_SECTION_LABEL, 0);
21177   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21178                                dwarf_strict
21179                                ? DEBUG_MACINFO_SECTION_LABEL
21180                                : DEBUG_MACRO_SECTION_LABEL, 0);
21181
21182   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21183     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
21184
21185   switch_to_section (text_section);
21186   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21187
21188   /* Make sure the line number table for .text always exists.  */
21189   text_section_line_info = new_line_info_table ();
21190   text_section_line_info->end_label = text_end_label;
21191 }
21192
21193 /* Called before cgraph_optimize starts outputtting functions, variables
21194    and toplevel asms into assembly.  */
21195
21196 static void
21197 dwarf2out_assembly_start (void)
21198 {
21199   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21200       && dwarf2out_do_cfi_asm ()
21201       && (!(flag_unwind_tables || flag_exceptions)
21202           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21203     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21204 }
21205
21206 /* A helper function for dwarf2out_finish called through
21207    htab_traverse.  Emit one queued .debug_str string.  */
21208
21209 static int
21210 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21211 {
21212   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21213
21214   if (node->form == DW_FORM_strp)
21215     {
21216       switch_to_section (debug_str_section);
21217       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21218       assemble_string (node->str, strlen (node->str) + 1);
21219     }
21220
21221   return 1;
21222 }
21223
21224 #if ENABLE_ASSERT_CHECKING
21225 /* Verify that all marks are clear.  */
21226
21227 static void
21228 verify_marks_clear (dw_die_ref die)
21229 {
21230   dw_die_ref c;
21231
21232   gcc_assert (! die->die_mark);
21233   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21234 }
21235 #endif /* ENABLE_ASSERT_CHECKING */
21236
21237 /* Clear the marks for a die and its children.
21238    Be cool if the mark isn't set.  */
21239
21240 static void
21241 prune_unmark_dies (dw_die_ref die)
21242 {
21243   dw_die_ref c;
21244
21245   if (die->die_mark)
21246     die->die_mark = 0;
21247   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21248 }
21249
21250 /* Given DIE that we're marking as used, find any other dies
21251    it references as attributes and mark them as used.  */
21252
21253 static void
21254 prune_unused_types_walk_attribs (dw_die_ref die)
21255 {
21256   dw_attr_ref a;
21257   unsigned ix;
21258
21259   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21260     {
21261       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21262         {
21263           /* A reference to another DIE.
21264              Make sure that it will get emitted.
21265              If it was broken out into a comdat group, don't follow it.  */
21266           if (! use_debug_types
21267               || a->dw_attr == DW_AT_specification
21268               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21269             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21270         }
21271       /* Set the string's refcount to 0 so that prune_unused_types_mark
21272          accounts properly for it.  */
21273       if (AT_class (a) == dw_val_class_str)
21274         a->dw_attr_val.v.val_str->refcount = 0;
21275     }
21276 }
21277
21278 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21279
21280 static void
21281 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21282 {
21283   dw_die_ref c;
21284
21285   if (die == NULL || die->die_child == NULL)
21286     return;
21287   c = die->die_child;
21288   do
21289     {
21290       switch (c->die_tag)
21291         {
21292         case DW_TAG_template_type_param:
21293         case DW_TAG_template_value_param:
21294         case DW_TAG_GNU_template_template_param:
21295         case DW_TAG_GNU_template_parameter_pack:
21296           prune_unused_types_mark (c, 1);
21297           break;
21298         default:
21299           break;
21300         }
21301       c = c->die_sib;
21302     } while (c && c != die->die_child);
21303 }
21304
21305 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21306    to DIE's children.  */
21307
21308 static void
21309 prune_unused_types_mark (dw_die_ref die, int dokids)
21310 {
21311   dw_die_ref c;
21312
21313   if (die->die_mark == 0)
21314     {
21315       /* We haven't done this node yet.  Mark it as used.  */
21316       die->die_mark = 1;
21317       /* If this is the DIE of a generic type instantiation,
21318          mark the children DIEs that describe its generic parms and
21319          args.  */
21320       prune_unused_types_mark_generic_parms_dies (die);
21321
21322       /* We also have to mark its parents as used.
21323          (But we don't want to mark our parents' kids due to this.)  */
21324       if (die->die_parent)
21325         prune_unused_types_mark (die->die_parent, 0);
21326
21327       /* Mark any referenced nodes.  */
21328       prune_unused_types_walk_attribs (die);
21329
21330       /* If this node is a specification,
21331          also mark the definition, if it exists.  */
21332       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21333         prune_unused_types_mark (die->die_definition, 1);
21334     }
21335
21336   if (dokids && die->die_mark != 2)
21337     {
21338       /* We need to walk the children, but haven't done so yet.
21339          Remember that we've walked the kids.  */
21340       die->die_mark = 2;
21341
21342       /* If this is an array type, we need to make sure our
21343          kids get marked, even if they're types.  If we're
21344          breaking out types into comdat sections, do this
21345          for all type definitions.  */
21346       if (die->die_tag == DW_TAG_array_type
21347           || (use_debug_types
21348               && is_type_die (die) && ! is_declaration_die (die)))
21349         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21350       else
21351         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21352     }
21353 }
21354
21355 /* For local classes, look if any static member functions were emitted
21356    and if so, mark them.  */
21357
21358 static void
21359 prune_unused_types_walk_local_classes (dw_die_ref die)
21360 {
21361   dw_die_ref c;
21362
21363   if (die->die_mark == 2)
21364     return;
21365
21366   switch (die->die_tag)
21367     {
21368     case DW_TAG_structure_type:
21369     case DW_TAG_union_type:
21370     case DW_TAG_class_type:
21371       break;
21372
21373     case DW_TAG_subprogram:
21374       if (!get_AT_flag (die, DW_AT_declaration)
21375           || die->die_definition != NULL)
21376         prune_unused_types_mark (die, 1);
21377       return;
21378
21379     default:
21380       return;
21381     }
21382
21383   /* Mark children.  */
21384   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21385 }
21386
21387 /* Walk the tree DIE and mark types that we actually use.  */
21388
21389 static void
21390 prune_unused_types_walk (dw_die_ref die)
21391 {
21392   dw_die_ref c;
21393
21394   /* Don't do anything if this node is already marked and
21395      children have been marked as well.  */
21396   if (die->die_mark == 2)
21397     return;
21398
21399   switch (die->die_tag)
21400     {
21401     case DW_TAG_structure_type:
21402     case DW_TAG_union_type:
21403     case DW_TAG_class_type:
21404       if (die->die_perennial_p)
21405         break;
21406
21407       for (c = die->die_parent; c; c = c->die_parent)
21408         if (c->die_tag == DW_TAG_subprogram)
21409           break;
21410
21411       /* Finding used static member functions inside of classes
21412          is needed just for local classes, because for other classes
21413          static member function DIEs with DW_AT_specification
21414          are emitted outside of the DW_TAG_*_type.  If we ever change
21415          it, we'd need to call this even for non-local classes.  */
21416       if (c)
21417         prune_unused_types_walk_local_classes (die);
21418
21419       /* It's a type node --- don't mark it.  */
21420       return;
21421
21422     case DW_TAG_const_type:
21423     case DW_TAG_packed_type:
21424     case DW_TAG_pointer_type:
21425     case DW_TAG_reference_type:
21426     case DW_TAG_rvalue_reference_type:
21427     case DW_TAG_volatile_type:
21428     case DW_TAG_typedef:
21429     case DW_TAG_array_type:
21430     case DW_TAG_interface_type:
21431     case DW_TAG_friend:
21432     case DW_TAG_variant_part:
21433     case DW_TAG_enumeration_type:
21434     case DW_TAG_subroutine_type:
21435     case DW_TAG_string_type:
21436     case DW_TAG_set_type:
21437     case DW_TAG_subrange_type:
21438     case DW_TAG_ptr_to_member_type:
21439     case DW_TAG_file_type:
21440       if (die->die_perennial_p)
21441         break;
21442
21443       /* It's a type node --- don't mark it.  */
21444       return;
21445
21446     default:
21447       /* Mark everything else.  */
21448       break;
21449   }
21450
21451   if (die->die_mark == 0)
21452     {
21453       die->die_mark = 1;
21454
21455       /* Now, mark any dies referenced from here.  */
21456       prune_unused_types_walk_attribs (die);
21457     }
21458
21459   die->die_mark = 2;
21460
21461   /* Mark children.  */
21462   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21463 }
21464
21465 /* Increment the string counts on strings referred to from DIE's
21466    attributes.  */
21467
21468 static void
21469 prune_unused_types_update_strings (dw_die_ref die)
21470 {
21471   dw_attr_ref a;
21472   unsigned ix;
21473
21474   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21475     if (AT_class (a) == dw_val_class_str)
21476       {
21477         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21478         s->refcount++;
21479         /* Avoid unnecessarily putting strings that are used less than
21480            twice in the hash table.  */
21481         if (s->refcount
21482             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21483           {
21484             void ** slot;
21485             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21486                                              htab_hash_string (s->str),
21487                                              INSERT);
21488             gcc_assert (*slot == NULL);
21489             *slot = s;
21490           }
21491       }
21492 }
21493
21494 /* Remove from the tree DIE any dies that aren't marked.  */
21495
21496 static void
21497 prune_unused_types_prune (dw_die_ref die)
21498 {
21499   dw_die_ref c;
21500
21501   gcc_assert (die->die_mark);
21502   prune_unused_types_update_strings (die);
21503
21504   if (! die->die_child)
21505     return;
21506
21507   c = die->die_child;
21508   do {
21509     dw_die_ref prev = c;
21510     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21511       if (c == die->die_child)
21512         {
21513           /* No marked children between 'prev' and the end of the list.  */
21514           if (prev == c)
21515             /* No marked children at all.  */
21516             die->die_child = NULL;
21517           else
21518             {
21519               prev->die_sib = c->die_sib;
21520               die->die_child = prev;
21521             }
21522           return;
21523         }
21524
21525     if (c != prev->die_sib)
21526       prev->die_sib = c;
21527     prune_unused_types_prune (c);
21528   } while (c != die->die_child);
21529 }
21530
21531 /* Remove dies representing declarations that we never use.  */
21532
21533 static void
21534 prune_unused_types (void)
21535 {
21536   unsigned int i;
21537   limbo_die_node *node;
21538   comdat_type_node *ctnode;
21539   pubname_ref pub;
21540   dw_die_ref base_type;
21541
21542 #if ENABLE_ASSERT_CHECKING
21543   /* All the marks should already be clear.  */
21544   verify_marks_clear (comp_unit_die ());
21545   for (node = limbo_die_list; node; node = node->next)
21546     verify_marks_clear (node->die);
21547   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21548     verify_marks_clear (ctnode->root_die);
21549 #endif /* ENABLE_ASSERT_CHECKING */
21550
21551   /* Mark types that are used in global variables.  */
21552   premark_types_used_by_global_vars ();
21553
21554   /* Set the mark on nodes that are actually used.  */
21555   prune_unused_types_walk (comp_unit_die ());
21556   for (node = limbo_die_list; node; node = node->next)
21557     prune_unused_types_walk (node->die);
21558   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21559     {
21560       prune_unused_types_walk (ctnode->root_die);
21561       prune_unused_types_mark (ctnode->type_die, 1);
21562     }
21563
21564   /* Also set the mark on nodes referenced from the
21565      pubname_table.  */
21566   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21567     prune_unused_types_mark (pub->die, 1);
21568   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21569     prune_unused_types_mark (base_type, 1);
21570
21571   if (debug_str_hash)
21572     htab_empty (debug_str_hash);
21573   prune_unused_types_prune (comp_unit_die ());
21574   for (node = limbo_die_list; node; node = node->next)
21575     prune_unused_types_prune (node->die);
21576   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21577     prune_unused_types_prune (ctnode->root_die);
21578
21579   /* Leave the marks clear.  */
21580   prune_unmark_dies (comp_unit_die ());
21581   for (node = limbo_die_list; node; node = node->next)
21582     prune_unmark_dies (node->die);
21583   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21584     prune_unmark_dies (ctnode->root_die);
21585 }
21586
21587 /* Set the parameter to true if there are any relative pathnames in
21588    the file table.  */
21589 static int
21590 file_table_relative_p (void ** slot, void *param)
21591 {
21592   bool *p = (bool *) param;
21593   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21594   if (!IS_ABSOLUTE_PATH (d->filename))
21595     {
21596       *p = true;
21597       return 0;
21598     }
21599   return 1;
21600 }
21601
21602 /* Routines to manipulate hash table of comdat type units.  */
21603
21604 static hashval_t
21605 htab_ct_hash (const void *of)
21606 {
21607   hashval_t h;
21608   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21609
21610   memcpy (&h, type_node->signature, sizeof (h));
21611   return h;
21612 }
21613
21614 static int
21615 htab_ct_eq (const void *of1, const void *of2)
21616 {
21617   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21618   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21619
21620   return (! memcmp (type_node_1->signature, type_node_2->signature,
21621                     DWARF_TYPE_SIGNATURE_SIZE));
21622 }
21623
21624 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21625    to the location it would have been added, should we know its
21626    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21627    probably improve compactness of debug info, removing equivalent
21628    abbrevs, and hide any differences caused by deferring the
21629    computation of the assembler name, triggered by e.g. PCH.  */
21630
21631 static inline void
21632 move_linkage_attr (dw_die_ref die)
21633 {
21634   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21635   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21636
21637   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21638               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21639
21640   while (--ix > 0)
21641     {
21642       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21643
21644       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21645         break;
21646     }
21647
21648   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21649     {
21650       VEC_pop (dw_attr_node, die->die_attr);
21651       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21652     }
21653 }
21654
21655 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21656    referenced from typed stack ops and count how often they are used.  */
21657
21658 static void
21659 mark_base_types (dw_loc_descr_ref loc)
21660 {
21661   dw_die_ref base_type = NULL;
21662
21663   for (; loc; loc = loc->dw_loc_next)
21664     {
21665       switch (loc->dw_loc_opc)
21666         {
21667         case DW_OP_GNU_regval_type:
21668         case DW_OP_GNU_deref_type:
21669           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21670           break;
21671         case DW_OP_GNU_convert:
21672         case DW_OP_GNU_reinterpret:
21673           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21674             continue;
21675           /* FALLTHRU */
21676         case DW_OP_GNU_const_type:
21677           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21678           break;
21679         case DW_OP_GNU_entry_value:
21680           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21681           continue;
21682         default:
21683           continue;
21684         }
21685       gcc_assert (base_type->die_parent == comp_unit_die ());
21686       if (base_type->die_mark)
21687         base_type->die_mark++;
21688       else
21689         {
21690           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21691           base_type->die_mark = 1;
21692         }
21693     }
21694 }
21695
21696 /* Comparison function for sorting marked base types.  */
21697
21698 static int
21699 base_type_cmp (const void *x, const void *y)
21700 {
21701   dw_die_ref dx = *(const dw_die_ref *) x;
21702   dw_die_ref dy = *(const dw_die_ref *) y;
21703   unsigned int byte_size1, byte_size2;
21704   unsigned int encoding1, encoding2;
21705   if (dx->die_mark > dy->die_mark)
21706     return -1;
21707   if (dx->die_mark < dy->die_mark)
21708     return 1;
21709   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21710   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21711   if (byte_size1 < byte_size2)
21712     return 1;
21713   if (byte_size1 > byte_size2)
21714     return -1;
21715   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21716   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21717   if (encoding1 < encoding2)
21718     return 1;
21719   if (encoding1 > encoding2)
21720     return -1;
21721   return 0;
21722 }
21723
21724 /* Move base types marked by mark_base_types as early as possible
21725    in the CU, sorted by decreasing usage count both to make the
21726    uleb128 references as small as possible and to make sure they
21727    will have die_offset already computed by calc_die_sizes when
21728    sizes of typed stack loc ops is computed.  */
21729
21730 static void
21731 move_marked_base_types (void)
21732 {
21733   unsigned int i;
21734   dw_die_ref base_type, die, c;
21735
21736   if (VEC_empty (dw_die_ref, base_types))
21737     return;
21738
21739   /* Sort by decreasing usage count, they will be added again in that
21740      order later on.  */
21741   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21742   die = comp_unit_die ();
21743   c = die->die_child;
21744   do
21745     {
21746       dw_die_ref prev = c;
21747       c = c->die_sib;
21748       while (c->die_mark)
21749         {
21750           remove_child_with_prev (c, prev);
21751           /* As base types got marked, there must be at least
21752              one node other than DW_TAG_base_type.  */
21753           gcc_assert (c != c->die_sib);
21754           c = c->die_sib;
21755         }
21756     }
21757   while (c != die->die_child);
21758   gcc_assert (die->die_child);
21759   c = die->die_child;
21760   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21761     {
21762       base_type->die_mark = 0;
21763       base_type->die_sib = c->die_sib;
21764       c->die_sib = base_type;
21765       c = base_type;
21766     }
21767 }
21768
21769 /* Helper function for resolve_addr, attempt to resolve
21770    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21771    SYMBOL_REFs refer to variables emitted in the current CU.  */
21772
21773 static int
21774 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21775 {
21776   rtx rtl = *addr;
21777
21778   if (GET_CODE (rtl) == CONST_STRING)
21779     {
21780       size_t len = strlen (XSTR (rtl, 0)) + 1;
21781       tree t = build_string (len, XSTR (rtl, 0));
21782       tree tlen = size_int (len - 1);
21783       TREE_TYPE (t)
21784         = build_array_type (char_type_node, build_index_type (tlen));
21785       rtl = lookup_constant_def (t);
21786       if (!rtl || !MEM_P (rtl))
21787         return 1;
21788       rtl = XEXP (rtl, 0);
21789       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21790       *addr = rtl;
21791       return 0;
21792     }
21793
21794   if (GET_CODE (rtl) == SYMBOL_REF
21795       && SYMBOL_REF_DECL (rtl))
21796     {
21797       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21798         {
21799           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21800             return 1;
21801         }
21802       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21803         return 1;
21804     }
21805
21806   if (GET_CODE (rtl) == CONST
21807       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21808     return 1;
21809
21810   return 0;
21811 }
21812
21813 /* Helper function for resolve_addr, handle one location
21814    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21815    the location list couldn't be resolved.  */
21816
21817 static bool
21818 resolve_addr_in_expr (dw_loc_descr_ref loc)
21819 {
21820   dw_loc_descr_ref keep = NULL;
21821   for (; loc; loc = loc->dw_loc_next)
21822     switch (loc->dw_loc_opc)
21823       {
21824       case DW_OP_addr:
21825         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21826           return false;
21827         break;
21828       case DW_OP_const4u:
21829       case DW_OP_const8u:
21830         if (loc->dtprel
21831             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21832           return false;
21833         break;
21834       case DW_OP_plus_uconst:
21835         if (size_of_loc_descr (loc)
21836             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21837               + 1
21838             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21839           {
21840             dw_loc_descr_ref repl
21841               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21842             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21843             add_loc_descr (&repl, loc->dw_loc_next);
21844             *loc = *repl;
21845           }
21846         break;
21847       case DW_OP_implicit_value:
21848         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21849             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21850           return false;
21851         break;
21852       case DW_OP_GNU_implicit_pointer:
21853       case DW_OP_GNU_parameter_ref:
21854         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21855           {
21856             dw_die_ref ref
21857               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21858             if (ref == NULL)
21859               return false;
21860             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21861             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21862             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21863           }
21864         break;
21865       case DW_OP_GNU_const_type:
21866       case DW_OP_GNU_regval_type:
21867       case DW_OP_GNU_deref_type:
21868       case DW_OP_GNU_convert:
21869       case DW_OP_GNU_reinterpret:
21870         while (loc->dw_loc_next
21871                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21872           {
21873             dw_die_ref base1, base2;
21874             unsigned enc1, enc2, size1, size2;
21875             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21876                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21877               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21878             else if (loc->dw_loc_oprnd1.val_class
21879                      == dw_val_class_unsigned_const)
21880               break;
21881             else
21882               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21883             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21884                 == dw_val_class_unsigned_const)
21885               break;
21886             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21887             gcc_assert (base1->die_tag == DW_TAG_base_type
21888                         && base2->die_tag == DW_TAG_base_type);
21889             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21890             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21891             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21892             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21893             if (size1 == size2
21894                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21895                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21896                      && loc != keep)
21897                     || enc1 == enc2))
21898               {
21899                 /* Optimize away next DW_OP_GNU_convert after
21900                    adjusting LOC's base type die reference.  */
21901                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21902                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21903                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21904                 else
21905                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21906                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21907                 continue;
21908               }
21909             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21910                point typed stack entry.  */
21911             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21912               keep = loc->dw_loc_next;
21913             break;
21914           }
21915         break;
21916       default:
21917         break;
21918       }
21919   return true;
21920 }
21921
21922 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21923    an address in .rodata section if the string literal is emitted there,
21924    or remove the containing location list or replace DW_AT_const_value
21925    with DW_AT_location and empty location expression, if it isn't found
21926    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21927    to something that has been emitted in the current CU.  */
21928
21929 static void
21930 resolve_addr (dw_die_ref die)
21931 {
21932   dw_die_ref c;
21933   dw_attr_ref a;
21934   dw_loc_list_ref *curr, *start, loc;
21935   unsigned ix;
21936
21937   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21938     switch (AT_class (a))
21939       {
21940       case dw_val_class_loc_list:
21941         start = curr = AT_loc_list_ptr (a);
21942         loc = *curr;
21943         gcc_assert (loc);
21944         /* The same list can be referenced more than once.  See if we have
21945            already recorded the result from a previous pass.  */
21946         if (loc->replaced)
21947           *curr = loc->dw_loc_next;
21948         else if (!loc->resolved_addr)
21949           {
21950             /* As things stand, we do not expect or allow one die to
21951                reference a suffix of another die's location list chain.
21952                References must be identical or completely separate.
21953                There is therefore no need to cache the result of this
21954                pass on any list other than the first; doing so
21955                would lead to unnecessary writes.  */
21956             while (*curr)
21957               {
21958                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21959                 if (!resolve_addr_in_expr ((*curr)->expr))
21960                   {
21961                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21962                     if (next && (*curr)->ll_symbol)
21963                       {
21964                         gcc_assert (!next->ll_symbol);
21965                         next->ll_symbol = (*curr)->ll_symbol;
21966                       }
21967                     *curr = next;
21968                   }
21969                 else
21970                   {
21971                     mark_base_types ((*curr)->expr);
21972                     curr = &(*curr)->dw_loc_next;
21973                   }
21974               }
21975             if (loc == *start)
21976               loc->resolved_addr = 1;
21977             else
21978               {
21979                 loc->replaced = 1;
21980                 loc->dw_loc_next = *start;
21981               }
21982           }
21983         if (!*start)
21984           {
21985             remove_AT (die, a->dw_attr);
21986             ix--;
21987           }
21988         break;
21989       case dw_val_class_loc:
21990         {
21991           dw_loc_descr_ref l = AT_loc (a);
21992           /* For -gdwarf-2 don't attempt to optimize
21993              DW_AT_data_member_location containing
21994              DW_OP_plus_uconst - older consumers might
21995              rely on it being that op instead of a more complex,
21996              but shorter, location description.  */
21997           if ((dwarf_version > 2
21998                || a->dw_attr != DW_AT_data_member_location
21999                || l == NULL
22000                || l->dw_loc_opc != DW_OP_plus_uconst
22001                || l->dw_loc_next != NULL)
22002               && !resolve_addr_in_expr (l))
22003             {
22004               remove_AT (die, a->dw_attr);
22005               ix--;
22006             }
22007           else
22008             mark_base_types (l);
22009         }
22010         break;
22011       case dw_val_class_addr:
22012         if (a->dw_attr == DW_AT_const_value
22013             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22014           {
22015             remove_AT (die, a->dw_attr);
22016             ix--;
22017           }
22018         if (die->die_tag == DW_TAG_GNU_call_site
22019             && a->dw_attr == DW_AT_abstract_origin)
22020           {
22021             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22022             dw_die_ref tdie = lookup_decl_die (tdecl);
22023             if (tdie == NULL
22024                 && DECL_EXTERNAL (tdecl)
22025                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22026               {
22027                 force_decl_die (tdecl);
22028                 tdie = lookup_decl_die (tdecl);
22029               }
22030             if (tdie)
22031               {
22032                 a->dw_attr_val.val_class = dw_val_class_die_ref;
22033                 a->dw_attr_val.v.val_die_ref.die = tdie;
22034                 a->dw_attr_val.v.val_die_ref.external = 0;
22035               }
22036             else
22037               {
22038                 remove_AT (die, a->dw_attr);
22039                 ix--;
22040               }
22041           }
22042         break;
22043       default:
22044         break;
22045       }
22046
22047   FOR_EACH_CHILD (die, c, resolve_addr (c));
22048 }
22049 \f
22050 /* Helper routines for optimize_location_lists.
22051    This pass tries to share identical local lists in .debug_loc
22052    section.  */
22053
22054 /* Iteratively hash operands of LOC opcode.  */
22055
22056 static inline hashval_t
22057 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22058 {
22059   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22060   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22061
22062   switch (loc->dw_loc_opc)
22063     {
22064     case DW_OP_const4u:
22065     case DW_OP_const8u:
22066       if (loc->dtprel)
22067         goto hash_addr;
22068       /* FALLTHRU */
22069     case DW_OP_const1u:
22070     case DW_OP_const1s:
22071     case DW_OP_const2u:
22072     case DW_OP_const2s:
22073     case DW_OP_const4s:
22074     case DW_OP_const8s:
22075     case DW_OP_constu:
22076     case DW_OP_consts:
22077     case DW_OP_pick:
22078     case DW_OP_plus_uconst:
22079     case DW_OP_breg0:
22080     case DW_OP_breg1:
22081     case DW_OP_breg2:
22082     case DW_OP_breg3:
22083     case DW_OP_breg4:
22084     case DW_OP_breg5:
22085     case DW_OP_breg6:
22086     case DW_OP_breg7:
22087     case DW_OP_breg8:
22088     case DW_OP_breg9:
22089     case DW_OP_breg10:
22090     case DW_OP_breg11:
22091     case DW_OP_breg12:
22092     case DW_OP_breg13:
22093     case DW_OP_breg14:
22094     case DW_OP_breg15:
22095     case DW_OP_breg16:
22096     case DW_OP_breg17:
22097     case DW_OP_breg18:
22098     case DW_OP_breg19:
22099     case DW_OP_breg20:
22100     case DW_OP_breg21:
22101     case DW_OP_breg22:
22102     case DW_OP_breg23:
22103     case DW_OP_breg24:
22104     case DW_OP_breg25:
22105     case DW_OP_breg26:
22106     case DW_OP_breg27:
22107     case DW_OP_breg28:
22108     case DW_OP_breg29:
22109     case DW_OP_breg30:
22110     case DW_OP_breg31:
22111     case DW_OP_regx:
22112     case DW_OP_fbreg:
22113     case DW_OP_piece:
22114     case DW_OP_deref_size:
22115     case DW_OP_xderef_size:
22116       hash = iterative_hash_object (val1->v.val_int, hash);
22117       break;
22118     case DW_OP_skip:
22119     case DW_OP_bra:
22120       {
22121         int offset;
22122
22123         gcc_assert (val1->val_class == dw_val_class_loc);
22124         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22125         hash = iterative_hash_object (offset, hash);
22126       }
22127       break;
22128     case DW_OP_implicit_value:
22129       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22130       switch (val2->val_class)
22131         {
22132         case dw_val_class_const:
22133           hash = iterative_hash_object (val2->v.val_int, hash);
22134           break;
22135         case dw_val_class_vec:
22136           {
22137             unsigned int elt_size = val2->v.val_vec.elt_size;
22138             unsigned int len = val2->v.val_vec.length;
22139
22140             hash = iterative_hash_object (elt_size, hash);
22141             hash = iterative_hash_object (len, hash);
22142             hash = iterative_hash (val2->v.val_vec.array,
22143                                    len * elt_size, hash);
22144           }
22145           break;
22146         case dw_val_class_const_double:
22147           hash = iterative_hash_object (val2->v.val_double.low, hash);
22148           hash = iterative_hash_object (val2->v.val_double.high, hash);
22149           break;
22150         case dw_val_class_addr:
22151           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22152           break;
22153         default:
22154           gcc_unreachable ();
22155         }
22156       break;
22157     case DW_OP_bregx:
22158     case DW_OP_bit_piece:
22159       hash = iterative_hash_object (val1->v.val_int, hash);
22160       hash = iterative_hash_object (val2->v.val_int, hash);
22161       break;
22162     case DW_OP_addr:
22163     hash_addr:
22164       if (loc->dtprel)
22165         {
22166           unsigned char dtprel = 0xd1;
22167           hash = iterative_hash_object (dtprel, hash);
22168         }
22169       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22170       break;
22171     case DW_OP_GNU_implicit_pointer:
22172       hash = iterative_hash_object (val2->v.val_int, hash);
22173       break;
22174     case DW_OP_GNU_entry_value:
22175       hash = hash_loc_operands (val1->v.val_loc, hash);
22176       break;
22177     case DW_OP_GNU_regval_type:
22178     case DW_OP_GNU_deref_type:
22179       {
22180         unsigned int byte_size
22181           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22182         unsigned int encoding
22183           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22184         hash = iterative_hash_object (val1->v.val_int, hash);
22185         hash = iterative_hash_object (byte_size, hash);
22186         hash = iterative_hash_object (encoding, hash);
22187       }
22188       break;
22189     case DW_OP_GNU_convert:
22190     case DW_OP_GNU_reinterpret:
22191       if (val1->val_class == dw_val_class_unsigned_const)
22192         {
22193           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22194           break;
22195         }
22196       /* FALLTHRU */
22197     case DW_OP_GNU_const_type:
22198       {
22199         unsigned int byte_size
22200           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22201         unsigned int encoding
22202           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22203         hash = iterative_hash_object (byte_size, hash);
22204         hash = iterative_hash_object (encoding, hash);
22205         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22206           break;
22207         hash = iterative_hash_object (val2->val_class, hash);
22208         switch (val2->val_class)
22209           {
22210           case dw_val_class_const:
22211             hash = iterative_hash_object (val2->v.val_int, hash);
22212             break;
22213           case dw_val_class_vec:
22214             {
22215               unsigned int elt_size = val2->v.val_vec.elt_size;
22216               unsigned int len = val2->v.val_vec.length;
22217
22218               hash = iterative_hash_object (elt_size, hash);
22219               hash = iterative_hash_object (len, hash);
22220               hash = iterative_hash (val2->v.val_vec.array,
22221                                      len * elt_size, hash);
22222             }
22223             break;
22224           case dw_val_class_const_double:
22225             hash = iterative_hash_object (val2->v.val_double.low, hash);
22226             hash = iterative_hash_object (val2->v.val_double.high, hash);
22227             break;
22228           default:
22229             gcc_unreachable ();
22230           }
22231       }
22232       break;
22233
22234     default:
22235       /* Other codes have no operands.  */
22236       break;
22237     }
22238   return hash;
22239 }
22240
22241 /* Iteratively hash the whole DWARF location expression LOC.  */
22242
22243 static inline hashval_t
22244 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22245 {
22246   dw_loc_descr_ref l;
22247   bool sizes_computed = false;
22248   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22249   size_of_locs (loc);
22250
22251   for (l = loc; l != NULL; l = l->dw_loc_next)
22252     {
22253       enum dwarf_location_atom opc = l->dw_loc_opc;
22254       hash = iterative_hash_object (opc, hash);
22255       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22256         {
22257           size_of_locs (loc);
22258           sizes_computed = true;
22259         }
22260       hash = hash_loc_operands (l, hash);
22261     }
22262   return hash;
22263 }
22264
22265 /* Compute hash of the whole location list LIST_HEAD.  */
22266
22267 static inline void
22268 hash_loc_list (dw_loc_list_ref list_head)
22269 {
22270   dw_loc_list_ref curr = list_head;
22271   hashval_t hash = 0;
22272
22273   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22274     {
22275       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22276       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22277       if (curr->section)
22278         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22279                                hash);
22280       hash = hash_locs (curr->expr, hash);
22281     }
22282   list_head->hash = hash;
22283 }
22284
22285 /* Return true if X and Y opcodes have the same operands.  */
22286
22287 static inline bool
22288 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22289 {
22290   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22291   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22292   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22293   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22294
22295   switch (x->dw_loc_opc)
22296     {
22297     case DW_OP_const4u:
22298     case DW_OP_const8u:
22299       if (x->dtprel)
22300         goto hash_addr;
22301       /* FALLTHRU */
22302     case DW_OP_const1u:
22303     case DW_OP_const1s:
22304     case DW_OP_const2u:
22305     case DW_OP_const2s:
22306     case DW_OP_const4s:
22307     case DW_OP_const8s:
22308     case DW_OP_constu:
22309     case DW_OP_consts:
22310     case DW_OP_pick:
22311     case DW_OP_plus_uconst:
22312     case DW_OP_breg0:
22313     case DW_OP_breg1:
22314     case DW_OP_breg2:
22315     case DW_OP_breg3:
22316     case DW_OP_breg4:
22317     case DW_OP_breg5:
22318     case DW_OP_breg6:
22319     case DW_OP_breg7:
22320     case DW_OP_breg8:
22321     case DW_OP_breg9:
22322     case DW_OP_breg10:
22323     case DW_OP_breg11:
22324     case DW_OP_breg12:
22325     case DW_OP_breg13:
22326     case DW_OP_breg14:
22327     case DW_OP_breg15:
22328     case DW_OP_breg16:
22329     case DW_OP_breg17:
22330     case DW_OP_breg18:
22331     case DW_OP_breg19:
22332     case DW_OP_breg20:
22333     case DW_OP_breg21:
22334     case DW_OP_breg22:
22335     case DW_OP_breg23:
22336     case DW_OP_breg24:
22337     case DW_OP_breg25:
22338     case DW_OP_breg26:
22339     case DW_OP_breg27:
22340     case DW_OP_breg28:
22341     case DW_OP_breg29:
22342     case DW_OP_breg30:
22343     case DW_OP_breg31:
22344     case DW_OP_regx:
22345     case DW_OP_fbreg:
22346     case DW_OP_piece:
22347     case DW_OP_deref_size:
22348     case DW_OP_xderef_size:
22349       return valx1->v.val_int == valy1->v.val_int;
22350     case DW_OP_skip:
22351     case DW_OP_bra:
22352       gcc_assert (valx1->val_class == dw_val_class_loc
22353                   && valy1->val_class == dw_val_class_loc
22354                   && x->dw_loc_addr == y->dw_loc_addr);
22355       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22356     case DW_OP_implicit_value:
22357       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22358           || valx2->val_class != valy2->val_class)
22359         return false;
22360       switch (valx2->val_class)
22361         {
22362         case dw_val_class_const:
22363           return valx2->v.val_int == valy2->v.val_int;
22364         case dw_val_class_vec:
22365           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22366                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22367                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22368                             valx2->v.val_vec.elt_size
22369                             * valx2->v.val_vec.length) == 0;
22370         case dw_val_class_const_double:
22371           return valx2->v.val_double.low == valy2->v.val_double.low
22372                  && valx2->v.val_double.high == valy2->v.val_double.high;
22373         case dw_val_class_addr:
22374           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22375         default:
22376           gcc_unreachable ();
22377         }
22378     case DW_OP_bregx:
22379     case DW_OP_bit_piece:
22380       return valx1->v.val_int == valy1->v.val_int
22381              && valx2->v.val_int == valy2->v.val_int;
22382     case DW_OP_addr:
22383     hash_addr:
22384       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22385     case DW_OP_GNU_implicit_pointer:
22386       return valx1->val_class == dw_val_class_die_ref
22387              && valx1->val_class == valy1->val_class
22388              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22389              && valx2->v.val_int == valy2->v.val_int;
22390     case DW_OP_GNU_entry_value:
22391       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22392     case DW_OP_GNU_const_type:
22393       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22394           || valx2->val_class != valy2->val_class)
22395         return false;
22396       switch (valx2->val_class)
22397         {
22398         case dw_val_class_const:
22399           return valx2->v.val_int == valy2->v.val_int;
22400         case dw_val_class_vec:
22401           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22402                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22403                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22404                             valx2->v.val_vec.elt_size
22405                             * valx2->v.val_vec.length) == 0;
22406         case dw_val_class_const_double:
22407           return valx2->v.val_double.low == valy2->v.val_double.low
22408                  && valx2->v.val_double.high == valy2->v.val_double.high;
22409         default:
22410           gcc_unreachable ();
22411         }
22412     case DW_OP_GNU_regval_type:
22413     case DW_OP_GNU_deref_type:
22414       return valx1->v.val_int == valy1->v.val_int
22415              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22416     case DW_OP_GNU_convert:
22417     case DW_OP_GNU_reinterpret:
22418       if (valx1->val_class != valy1->val_class)
22419         return false;
22420       if (valx1->val_class == dw_val_class_unsigned_const)
22421         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22422       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22423     case DW_OP_GNU_parameter_ref:
22424       return valx1->val_class == dw_val_class_die_ref
22425              && valx1->val_class == valy1->val_class
22426              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22427     default:
22428       /* Other codes have no operands.  */
22429       return true;
22430     }
22431 }
22432
22433 /* Return true if DWARF location expressions X and Y are the same.  */
22434
22435 static inline bool
22436 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22437 {
22438   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22439     if (x->dw_loc_opc != y->dw_loc_opc
22440         || x->dtprel != y->dtprel
22441         || !compare_loc_operands (x, y))
22442       break;
22443   return x == NULL && y == NULL;
22444 }
22445
22446 /* Return precomputed hash of location list X.  */
22447
22448 static hashval_t
22449 loc_list_hash (const void *x)
22450 {
22451   return ((const struct dw_loc_list_struct *) x)->hash;
22452 }
22453
22454 /* Return 1 if location lists X and Y are the same.  */
22455
22456 static int
22457 loc_list_eq (const void *x, const void *y)
22458 {
22459   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22460   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22461   if (a == b)
22462     return 1;
22463   if (a->hash != b->hash)
22464     return 0;
22465   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22466     if (strcmp (a->begin, b->begin) != 0
22467         || strcmp (a->end, b->end) != 0
22468         || (a->section == NULL) != (b->section == NULL)
22469         || (a->section && strcmp (a->section, b->section) != 0)
22470         || !compare_locs (a->expr, b->expr))
22471       break;
22472   return a == NULL && b == NULL;
22473 }
22474
22475 /* Recursively optimize location lists referenced from DIE
22476    children and share them whenever possible.  */
22477
22478 static void
22479 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22480 {
22481   dw_die_ref c;
22482   dw_attr_ref a;
22483   unsigned ix;
22484   void **slot;
22485
22486   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22487     if (AT_class (a) == dw_val_class_loc_list)
22488       {
22489         dw_loc_list_ref list = AT_loc_list (a);
22490         /* TODO: perform some optimizations here, before hashing
22491            it and storing into the hash table.  */
22492         hash_loc_list (list);
22493         slot = htab_find_slot_with_hash (htab, list, list->hash,
22494                                          INSERT);
22495         if (*slot == NULL)
22496           *slot = (void *) list;
22497         else
22498           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22499       }
22500
22501   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22502 }
22503
22504 /* Optimize location lists referenced from DIE
22505    children and share them whenever possible.  */
22506
22507 static void
22508 optimize_location_lists (dw_die_ref die)
22509 {
22510   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22511   optimize_location_lists_1 (die, htab);
22512   htab_delete (htab);
22513 }
22514 \f
22515 /* Output stuff that dwarf requires at the end of every file,
22516    and generate the DWARF-2 debugging info.  */
22517
22518 static void
22519 dwarf2out_finish (const char *filename)
22520 {
22521   limbo_die_node *node, *next_node;
22522   comdat_type_node *ctnode;
22523   htab_t comdat_type_table;
22524   unsigned int i;
22525
22526   /* PCH might result in DW_AT_producer string being restored from the
22527      header compilation, fix it up if needed.  */
22528   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22529   if (strcmp (AT_string (producer), producer_string) != 0)
22530     {
22531       struct indirect_string_node *node = find_AT_string (producer_string);
22532       producer->dw_attr_val.v.val_str = node;
22533     }
22534
22535   gen_scheduled_generic_parms_dies ();
22536   gen_remaining_tmpl_value_param_die_attribute ();
22537
22538   /* Add the name for the main input file now.  We delayed this from
22539      dwarf2out_init to avoid complications with PCH.  */
22540   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22541   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22542     add_comp_dir_attribute (comp_unit_die ());
22543   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22544     {
22545       bool p = false;
22546       htab_traverse (file_table, file_table_relative_p, &p);
22547       if (p)
22548         add_comp_dir_attribute (comp_unit_die ());
22549     }
22550
22551   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22552     {
22553       add_location_or_const_value_attribute (
22554         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22555         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22556         false,
22557         DW_AT_location);
22558     }
22559
22560   /* Traverse the limbo die list, and add parent/child links.  The only
22561      dies without parents that should be here are concrete instances of
22562      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22563      For concrete instances, we can get the parent die from the abstract
22564      instance.  */
22565   for (node = limbo_die_list; node; node = next_node)
22566     {
22567       dw_die_ref die = node->die;
22568       next_node = node->next;
22569
22570       if (die->die_parent == NULL)
22571         {
22572           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22573
22574           if (origin && origin->die_parent)
22575             add_child_die (origin->die_parent, die);
22576           else if (is_cu_die (die))
22577             ;
22578           else if (seen_error ())
22579             /* It's OK to be confused by errors in the input.  */
22580             add_child_die (comp_unit_die (), die);
22581           else
22582             {
22583               /* In certain situations, the lexical block containing a
22584                  nested function can be optimized away, which results
22585                  in the nested function die being orphaned.  Likewise
22586                  with the return type of that nested function.  Force
22587                  this to be a child of the containing function.
22588
22589                  It may happen that even the containing function got fully
22590                  inlined and optimized out.  In that case we are lost and
22591                  assign the empty child.  This should not be big issue as
22592                  the function is likely unreachable too.  */
22593               gcc_assert (node->created_for);
22594
22595               if (DECL_P (node->created_for))
22596                 origin = get_context_die (DECL_CONTEXT (node->created_for));
22597               else if (TYPE_P (node->created_for))
22598                 origin = scope_die_for (node->created_for, comp_unit_die ());
22599               else
22600                 origin = comp_unit_die ();
22601
22602               add_child_die (origin, die);
22603             }
22604         }
22605     }
22606
22607   limbo_die_list = NULL;
22608
22609 #if ENABLE_ASSERT_CHECKING
22610   {
22611     dw_die_ref die = comp_unit_die (), c;
22612     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22613   }
22614 #endif
22615   resolve_addr (comp_unit_die ());
22616   move_marked_base_types ();
22617
22618   for (node = deferred_asm_name; node; node = node->next)
22619     {
22620       tree decl = node->created_for;
22621       /* When generating LTO bytecode we can not generate new assembler
22622          names at this point and all important decls got theirs via
22623          free-lang-data.  */
22624       if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22625           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22626         {
22627           add_linkage_attr (node->die, decl);
22628           move_linkage_attr (node->die);
22629         }
22630     }
22631
22632   deferred_asm_name = NULL;
22633
22634   /* Walk through the list of incomplete types again, trying once more to
22635      emit full debugging info for them.  */
22636   retry_incomplete_types ();
22637
22638   if (flag_eliminate_unused_debug_types)
22639     prune_unused_types ();
22640
22641   /* Generate separate CUs for each of the include files we've seen.
22642      They will go into limbo_die_list.  */
22643   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22644     break_out_includes (comp_unit_die ());
22645
22646   /* Generate separate COMDAT sections for type DIEs. */
22647   if (use_debug_types)
22648     {
22649       break_out_comdat_types (comp_unit_die ());
22650
22651       /* Each new type_unit DIE was added to the limbo die list when created.
22652          Since these have all been added to comdat_type_list, clear the
22653          limbo die list.  */
22654       limbo_die_list = NULL;
22655
22656       /* For each new comdat type unit, copy declarations for incomplete
22657          types to make the new unit self-contained (i.e., no direct
22658          references to the main compile unit).  */
22659       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22660         copy_decls_for_unworthy_types (ctnode->root_die);
22661       copy_decls_for_unworthy_types (comp_unit_die ());
22662
22663       /* In the process of copying declarations from one unit to another,
22664          we may have left some declarations behind that are no longer
22665          referenced.  Prune them.  */
22666       prune_unused_types ();
22667     }
22668
22669   /* Traverse the DIE's and add add sibling attributes to those DIE's
22670      that have children.  */
22671   add_sibling_attributes (comp_unit_die ());
22672   for (node = limbo_die_list; node; node = node->next)
22673     add_sibling_attributes (node->die);
22674   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22675     add_sibling_attributes (ctnode->root_die);
22676
22677   /* Output a terminator label for the .text section.  */
22678   switch_to_section (text_section);
22679   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22680   if (cold_text_section)
22681     {
22682       switch_to_section (cold_text_section);
22683       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22684     }
22685
22686   /* We can only use the low/high_pc attributes if all of the code was
22687      in .text.  */
22688   if (!have_multiple_function_sections 
22689       || (dwarf_version < 3 && dwarf_strict))
22690     {
22691       /* Don't add if the CU has no associated code.  */
22692       if (text_section_used)
22693         {
22694           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22695           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22696         }
22697     }
22698   else
22699     {
22700       unsigned fde_idx;
22701       dw_fde_ref fde;
22702       bool range_list_added = false;
22703
22704       if (text_section_used)
22705         add_ranges_by_labels (comp_unit_die (), text_section_label,
22706                               text_end_label, &range_list_added);
22707       if (cold_text_section_used)
22708         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22709                               cold_end_label, &range_list_added);
22710
22711       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22712         {
22713           if (!fde->in_std_section)
22714             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22715                                   fde->dw_fde_end, &range_list_added);
22716           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22717             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22718                                   fde->dw_fde_second_end, &range_list_added);
22719         }
22720
22721       if (range_list_added)
22722         {
22723           /* We need to give .debug_loc and .debug_ranges an appropriate
22724              "base address".  Use zero so that these addresses become
22725              absolute.  Historically, we've emitted the unexpected
22726              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22727              Emit both to give time for other tools to adapt.  */
22728           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22729           if (! dwarf_strict && dwarf_version < 4)
22730             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22731
22732           add_ranges (NULL);
22733         }
22734     }
22735
22736   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22737     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22738                     debug_line_section_label);
22739
22740   if (have_macinfo)
22741     add_AT_macptr (comp_unit_die (),
22742                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22743                    macinfo_section_label);
22744
22745   if (have_location_lists)
22746     optimize_location_lists (comp_unit_die ());
22747
22748   /* Output all of the compilation units.  We put the main one last so that
22749      the offsets are available to output_pubnames.  */
22750   for (node = limbo_die_list; node; node = node->next)
22751     output_comp_unit (node->die, 0);
22752
22753   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22754   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22755     {
22756       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22757
22758       /* Don't output duplicate types.  */
22759       if (*slot != HTAB_EMPTY_ENTRY)
22760         continue;
22761
22762       /* Add a pointer to the line table for the main compilation unit
22763          so that the debugger can make sense of DW_AT_decl_file
22764          attributes.  */
22765       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22766         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22767                         debug_line_section_label);
22768
22769       output_comdat_type_unit (ctnode);
22770       *slot = ctnode;
22771     }
22772   htab_delete (comdat_type_table);
22773
22774   /* Output the main compilation unit if non-empty or if .debug_macinfo
22775      or .debug_macro will be emitted.  */
22776   output_comp_unit (comp_unit_die (), have_macinfo);
22777
22778   /* Output the abbreviation table.  */
22779   if (abbrev_die_table_in_use != 1)
22780     {
22781       switch_to_section (debug_abbrev_section);
22782       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22783       output_abbrev_section ();
22784     }
22785
22786   /* Output location list section if necessary.  */
22787   if (have_location_lists)
22788     {
22789       /* Output the location lists info.  */
22790       switch_to_section (debug_loc_section);
22791       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22792                                    DEBUG_LOC_SECTION_LABEL, 0);
22793       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22794       output_location_lists (comp_unit_die ());
22795     }
22796
22797   /* Output public names table if necessary.  */
22798   if (!VEC_empty (pubname_entry, pubname_table))
22799     {
22800       gcc_assert (info_section_emitted);
22801       switch_to_section (debug_pubnames_section);
22802       output_pubnames (pubname_table);
22803     }
22804
22805   /* Output public types table if necessary.  */
22806   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22807      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22808      simply won't look for the section.  */
22809   if (!VEC_empty (pubname_entry, pubtype_table))
22810     {
22811       bool empty = false;
22812       
22813       if (flag_eliminate_unused_debug_types)
22814         {
22815           /* The pubtypes table might be emptied by pruning unused items.  */
22816           unsigned i;
22817           pubname_ref p;
22818           empty = true;
22819           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22820             if (p->die->die_offset != 0)
22821               {
22822                 empty = false;
22823                 break;
22824               }
22825         }
22826       if (!empty)
22827         {
22828           gcc_assert (info_section_emitted);
22829           switch_to_section (debug_pubtypes_section);
22830           output_pubnames (pubtype_table);
22831         }
22832     }
22833
22834   /* Output the address range information if a CU (.debug_info section)
22835      was emitted.  We output an empty table even if we had no functions
22836      to put in it.  This because the consumer has no way to tell the
22837      difference between an empty table that we omitted and failure to
22838      generate a table that would have contained data.  */
22839   if (info_section_emitted)
22840     {
22841       unsigned long aranges_length = size_of_aranges ();
22842
22843       switch_to_section (debug_aranges_section);
22844       output_aranges (aranges_length);
22845     }
22846
22847   /* Output ranges section if necessary.  */
22848   if (ranges_table_in_use)
22849     {
22850       switch_to_section (debug_ranges_section);
22851       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22852       output_ranges ();
22853     }
22854
22855   /* Have to end the macro section.  */
22856   if (have_macinfo)
22857     {
22858       switch_to_section (debug_macinfo_section);
22859       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22860       output_macinfo ();
22861       dw2_asm_output_data (1, 0, "End compilation unit");
22862     }
22863
22864   /* Output the source line correspondence table.  We must do this
22865      even if there is no line information.  Otherwise, on an empty
22866      translation unit, we will generate a present, but empty,
22867      .debug_info section.  IRIX 6.5 `nm' will then complain when
22868      examining the file.  This is done late so that any filenames
22869      used by the debug_info section are marked as 'used'.  */
22870   switch_to_section (debug_line_section);
22871   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22872   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22873     output_line_info ();
22874
22875   /* If we emitted any DW_FORM_strp form attribute, output the string
22876      table too.  */
22877   if (debug_str_hash)
22878     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22879 }
22880
22881 #include "gt-dwarf2out.h"