OSDN Git Service

* dwarf2cfi.c (DW_STACK_POINTER_REGNUM): New.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically.  They've been
118    totally replaced in this file; make sure it stays that way.  */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 /* The size of the target's pointer type.  */
126 #ifndef PTR_SIZE
127 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
128 #endif
129
130 /* Array of RTXes referenced by the debugging information, which therefore
131    must be kept around forever.  */
132 static GTY(()) VEC(rtx,gc) *used_rtx_array;
133
134 /* A pointer to the base of a list of incomplete types which might be
135    completed at some later time.  incomplete_types_list needs to be a
136    VEC(tree,gc) because we want to tell the garbage collector about
137    it.  */
138 static GTY(()) VEC(tree,gc) *incomplete_types;
139
140 /* A pointer to the base of a table of references to declaration
141    scopes.  This table is a display which tracks the nesting
142    of declaration scopes at the current scope and containing
143    scopes.  This table is used to find the proper place to
144    define type declaration DIE's.  */
145 static GTY(()) VEC(tree,gc) *decl_scope_table;
146
147 /* Pointers to various DWARF2 sections.  */
148 static GTY(()) section *debug_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_aranges_section;
151 static GTY(()) section *debug_macinfo_section;
152 static GTY(()) section *debug_line_section;
153 static GTY(()) section *debug_loc_section;
154 static GTY(()) section *debug_pubnames_section;
155 static GTY(()) section *debug_pubtypes_section;
156 static GTY(()) section *debug_str_section;
157 static GTY(()) section *debug_ranges_section;
158 static GTY(()) section *debug_frame_section;
159
160 /* Maximum size (in bytes) of an artificially generated label.  */
161 #define MAX_ARTIFICIAL_LABEL_BYTES      30
162
163 /* According to the (draft) DWARF 3 specification, the initial length
164    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
165    bytes are 0xffffffff, followed by the length stored in the next 8
166    bytes.
167
168    However, the SGI/MIPS ABI uses an initial length which is equal to
169    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
170
171 #ifndef DWARF_INITIAL_LENGTH_SIZE
172 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
173 #endif
174
175 /* Round SIZE up to the nearest BOUNDARY.  */
176 #define DWARF_ROUND(SIZE,BOUNDARY) \
177   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
178
179 /* CIE identifier.  */
180 #if HOST_BITS_PER_WIDE_INT >= 64
181 #define DWARF_CIE_ID \
182   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
183 #else
184 #define DWARF_CIE_ID DW_CIE_ID
185 #endif
186
187 DEF_VEC_P (dw_fde_ref);
188 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
189
190 /* A vector for a table that contains frame description
191    information for each routine.  */
192 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
193
194 struct GTY(()) indirect_string_node {
195   const char *str;
196   unsigned int refcount;
197   enum dwarf_form form;
198   char *label;
199 };
200
201 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
202
203 static GTY(()) int dw2_string_counter;
204
205 /* True if the compilation unit places functions in more than one section.  */
206 static GTY(()) bool have_multiple_function_sections = false;
207
208 /* Whether the default text and cold text sections have been used at all.  */
209
210 static GTY(()) bool text_section_used = false;
211 static GTY(()) bool cold_text_section_used = false;
212
213 /* The default cold text section.  */
214 static GTY(()) section *cold_text_section;
215
216 /* Forward declarations for functions defined in this file.  */
217
218 static char *stripattributes (const char *);
219 static void output_call_frame_info (int);
220 static void dwarf2out_note_section_used (void);
221
222 /* Personality decl of current unit.  Used only when assembler does not support
223    personality CFI.  */
224 static GTY(()) rtx current_unit_personality;
225
226 /* Data and reference forms for relocatable data.  */
227 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
228 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
229
230 #ifndef DEBUG_FRAME_SECTION
231 #define DEBUG_FRAME_SECTION     ".debug_frame"
232 #endif
233
234 #ifndef FUNC_BEGIN_LABEL
235 #define FUNC_BEGIN_LABEL        "LFB"
236 #endif
237
238 #ifndef FUNC_END_LABEL
239 #define FUNC_END_LABEL          "LFE"
240 #endif
241
242 #ifndef PROLOGUE_END_LABEL
243 #define PROLOGUE_END_LABEL      "LPE"
244 #endif
245
246 #ifndef EPILOGUE_BEGIN_LABEL
247 #define EPILOGUE_BEGIN_LABEL    "LEB"
248 #endif
249
250 #ifndef FRAME_BEGIN_LABEL
251 #define FRAME_BEGIN_LABEL       "Lframe"
252 #endif
253 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
254 #define CIE_END_LABEL           "LECIE"
255 #define FDE_LABEL               "LSFDE"
256 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
257 #define FDE_END_LABEL           "LEFDE"
258 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
259 #define LINE_NUMBER_END_LABEL   "LELT"
260 #define LN_PROLOG_AS_LABEL      "LASLTP"
261 #define LN_PROLOG_END_LABEL     "LELTP"
262 #define DIE_LABEL_PREFIX        "DW"
263 \f
264 /* Match the base name of a file to the base name of a compilation unit. */
265
266 static int
267 matches_main_base (const char *path)
268 {
269   /* Cache the last query. */
270   static const char *last_path = NULL;
271   static int last_match = 0;
272   if (path != last_path)
273     {
274       const char *base;
275       int length = base_of_path (path, &base);
276       last_path = path;
277       last_match = (length == main_input_baselength
278                     && memcmp (base, main_input_basename, length) == 0);
279     }
280   return last_match;
281 }
282
283 #ifdef DEBUG_DEBUG_STRUCT
284
285 static int
286 dump_struct_debug (tree type, enum debug_info_usage usage,
287                    enum debug_struct_file criterion, int generic,
288                    int matches, int result)
289 {
290   /* Find the type name. */
291   tree type_decl = TYPE_STUB_DECL (type);
292   tree t = type_decl;
293   const char *name = 0;
294   if (TREE_CODE (t) == TYPE_DECL)
295     t = DECL_NAME (t);
296   if (t)
297     name = IDENTIFIER_POINTER (t);
298
299   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
300            criterion,
301            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
302            matches ? "bas" : "hdr",
303            generic ? "gen" : "ord",
304            usage == DINFO_USAGE_DFN ? ";" :
305              usage == DINFO_USAGE_DIR_USE ? "." : "*",
306            result,
307            (void*) type_decl, name);
308   return result;
309 }
310 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
311   dump_struct_debug (type, usage, criterion, generic, matches, result)
312
313 #else
314
315 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
316   (result)
317
318 #endif
319
320 static bool
321 should_emit_struct_debug (tree type, enum debug_info_usage usage)
322 {
323   enum debug_struct_file criterion;
324   tree type_decl;
325   bool generic = lang_hooks.types.generic_p (type);
326
327   if (generic)
328     criterion = debug_struct_generic[usage];
329   else
330     criterion = debug_struct_ordinary[usage];
331
332   if (criterion == DINFO_STRUCT_FILE_NONE)
333     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
334   if (criterion == DINFO_STRUCT_FILE_ANY)
335     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
336
337   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
338
339   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
340     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
341
342   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
343     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
344   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
345 }
346 \f
347 /* Return a pointer to a copy of the section string name S with all
348    attributes stripped off, and an asterisk prepended (for assemble_name).  */
349
350 static inline char *
351 stripattributes (const char *s)
352 {
353   char *stripped = XNEWVEC (char, strlen (s) + 2);
354   char *p = stripped;
355
356   *p++ = '*';
357
358   while (*s && *s != ',')
359     *p++ = *s++;
360
361   *p = '\0';
362   return stripped;
363 }
364
365 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
366    switch to the data section instead, and write out a synthetic start label
367    for collect2 the first time around.  */
368
369 static void
370 switch_to_eh_frame_section (bool back)
371 {
372   tree label;
373
374 #ifdef EH_FRAME_SECTION_NAME
375   if (eh_frame_section == 0)
376     {
377       int flags;
378
379       if (EH_TABLES_CAN_BE_READ_ONLY)
380         {
381           int fde_encoding;
382           int per_encoding;
383           int lsda_encoding;
384
385           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
386                                                        /*global=*/0);
387           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
388                                                        /*global=*/1);
389           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
390                                                         /*global=*/0);
391           flags = ((! flag_pic
392                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
393                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
394                         && (per_encoding & 0x70) != DW_EH_PE_absptr
395                         && (per_encoding & 0x70) != DW_EH_PE_aligned
396                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
397                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
398                    ? 0 : SECTION_WRITE);
399         }
400       else
401         flags = SECTION_WRITE;
402       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
403     }
404 #endif /* EH_FRAME_SECTION_NAME */
405
406   if (eh_frame_section)
407     switch_to_section (eh_frame_section);
408   else
409     {
410       /* We have no special eh_frame section.  Put the information in
411          the data section and emit special labels to guide collect2.  */
412       switch_to_section (data_section);
413
414       if (!back)
415         {
416           label = get_file_function_name ("F");
417           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
418           targetm.asm_out.globalize_label (asm_out_file,
419                                            IDENTIFIER_POINTER (label));
420           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
421         }
422     }
423 }
424
425 /* Switch [BACK] to the eh or debug frame table section, depending on
426    FOR_EH.  */
427
428 static void
429 switch_to_frame_table_section (int for_eh, bool back)
430 {
431   if (for_eh)
432     switch_to_eh_frame_section (back);
433   else
434     {
435       if (!debug_frame_section)
436         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
437                                            SECTION_DEBUG, NULL);
438       switch_to_section (debug_frame_section);
439     }
440 }
441
442 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
443
444 enum dw_cfi_oprnd_type
445 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
446 {
447   switch (cfi)
448     {
449     case DW_CFA_nop:
450     case DW_CFA_GNU_window_save:
451     case DW_CFA_remember_state:
452     case DW_CFA_restore_state:
453       return dw_cfi_oprnd_unused;
454
455     case DW_CFA_set_loc:
456     case DW_CFA_advance_loc1:
457     case DW_CFA_advance_loc2:
458     case DW_CFA_advance_loc4:
459     case DW_CFA_MIPS_advance_loc8:
460       return dw_cfi_oprnd_addr;
461
462     case DW_CFA_offset:
463     case DW_CFA_offset_extended:
464     case DW_CFA_def_cfa:
465     case DW_CFA_offset_extended_sf:
466     case DW_CFA_def_cfa_sf:
467     case DW_CFA_restore:
468     case DW_CFA_restore_extended:
469     case DW_CFA_undefined:
470     case DW_CFA_same_value:
471     case DW_CFA_def_cfa_register:
472     case DW_CFA_register:
473     case DW_CFA_expression:
474       return dw_cfi_oprnd_reg_num;
475
476     case DW_CFA_def_cfa_offset:
477     case DW_CFA_GNU_args_size:
478     case DW_CFA_def_cfa_offset_sf:
479       return dw_cfi_oprnd_offset;
480
481     case DW_CFA_def_cfa_expression:
482       return dw_cfi_oprnd_loc;
483
484     default:
485       gcc_unreachable ();
486     }
487 }
488
489 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
490
491 enum dw_cfi_oprnd_type
492 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
493 {
494   switch (cfi)
495     {
496     case DW_CFA_def_cfa:
497     case DW_CFA_def_cfa_sf:
498     case DW_CFA_offset:
499     case DW_CFA_offset_extended_sf:
500     case DW_CFA_offset_extended:
501       return dw_cfi_oprnd_offset;
502
503     case DW_CFA_register:
504       return dw_cfi_oprnd_reg_num;
505
506     case DW_CFA_expression:
507       return dw_cfi_oprnd_loc;
508
509     default:
510       return dw_cfi_oprnd_unused;
511     }
512 }
513
514 /* Output one FDE.  */
515
516 static void
517 output_fde (dw_fde_ref fde, bool for_eh, bool second,
518             char *section_start_label, int fde_encoding, char *augmentation,
519             bool any_lsda_needed, int lsda_encoding)
520 {
521   int ix;
522   const char *begin, *end;
523   static unsigned int j;
524   char l1[20], l2[20];
525   dw_cfi_ref cfi;
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
606      this FDE.  */
607   fde->dw_fde_current_label = begin;
608   if (fde->dw_fde_second_begin == NULL)
609     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
610       output_cfi (cfi, fde, for_eh);
611   else if (!second)
612     {
613       if (fde->dw_fde_switch_cfi_index > 0)
614         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
615           {
616             if (ix == fde->dw_fde_switch_cfi_index)
617               break;
618             output_cfi (cfi, fde, for_eh);
619           }
620     }
621   else
622     {
623       int i, from = 0;
624       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
625
626       if (fde->dw_fde_switch_cfi_index > 0)
627         {
628           from = fde->dw_fde_switch_cfi_index;
629           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
630         }
631       for (i = from; i < until; i++)
632         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
633                     fde, for_eh);
634     }
635
636   /* If we are to emit a ref/link from function bodies to their frame tables,
637      do it now.  This is typically performed to make sure that tables
638      associated with functions are dragged with them and not discarded in
639      garbage collecting links. We need to do this on a per function basis to
640      cope with -ffunction-sections.  */
641
642 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
643   /* Switch to the function section, emit the ref to the tables, and
644      switch *back* into the table section.  */
645   switch_to_section (function_section (fde->decl));
646   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
647   switch_to_frame_table_section (for_eh, true);
648 #endif
649
650   /* Pad the FDE out to an address sized boundary.  */
651   ASM_OUTPUT_ALIGN (asm_out_file,
652                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
653   ASM_OUTPUT_LABEL (asm_out_file, l2);
654
655   j += 2;
656 }
657
658 /* Return true if frame description entry FDE is needed for EH.  */
659
660 static bool
661 fde_needed_for_eh_p (dw_fde_ref fde)
662 {
663   if (flag_asynchronous_unwind_tables)
664     return true;
665
666   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
667     return true;
668
669   if (fde->uses_eh_lsda)
670     return true;
671
672   /* If exceptions are enabled, we have collected nothrow info.  */
673   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
674     return false;
675
676   return true;
677 }
678
679 /* Output the call frame information used to record information
680    that relates to calculating the frame pointer, and records the
681    location of saved registers.  */
682
683 static void
684 output_call_frame_info (int for_eh)
685 {
686   unsigned int i;
687   dw_fde_ref fde;
688   dw_cfi_ref cfi;
689   char l1[20], l2[20], section_start_label[20];
690   bool any_lsda_needed = false;
691   char augmentation[6];
692   int augmentation_size;
693   int fde_encoding = DW_EH_PE_absptr;
694   int per_encoding = DW_EH_PE_absptr;
695   int lsda_encoding = DW_EH_PE_absptr;
696   int return_reg;
697   rtx personality = NULL;
698   int dw_cie_version;
699
700   /* Don't emit a CIE if there won't be any FDEs.  */
701   if (fde_vec == NULL)
702     return;
703
704   /* Nothing to do if the assembler's doing it all.  */
705   if (dwarf2out_do_cfi_asm ())
706     return;
707
708   /* If we don't have any functions we'll want to unwind out of, don't emit
709      any EH unwind information.  If we make FDEs linkonce, we may have to
710      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
711      want to avoid having an FDE kept around when the function it refers to
712      is discarded.  Example where this matters: a primary function template
713      in C++ requires EH information, an explicit specialization doesn't.  */
714   if (for_eh)
715     {
716       bool any_eh_needed = false;
717
718       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
719         {
720           if (fde->uses_eh_lsda)
721             any_eh_needed = any_lsda_needed = true;
722           else if (fde_needed_for_eh_p (fde))
723             any_eh_needed = true;
724           else if (TARGET_USES_WEAK_UNWIND_INFO)
725             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
726         }
727
728       if (!any_eh_needed)
729         return;
730     }
731
732   /* We're going to be generating comments, so turn on app.  */
733   if (flag_debug_asm)
734     app_enable ();
735
736   /* Switch to the proper frame section, first time.  */
737   switch_to_frame_table_section (for_eh, false);
738
739   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
740   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
741
742   /* Output the CIE.  */
743   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
744   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
745   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
746     dw2_asm_output_data (4, 0xffffffff,
747       "Initial length escape value indicating 64-bit DWARF extension");
748   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
749                         "Length of Common Information Entry");
750   ASM_OUTPUT_LABEL (asm_out_file, l1);
751
752   /* Now that the CIE pointer is PC-relative for EH,
753      use 0 to identify the CIE.  */
754   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
755                        (for_eh ? 0 : DWARF_CIE_ID),
756                        "CIE Identifier Tag");
757
758   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
759      use CIE version 1, unless that would produce incorrect results
760      due to overflowing the return register column.  */
761   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
762   dw_cie_version = 1;
763   if (return_reg >= 256 || dwarf_version > 2)
764     dw_cie_version = 3;
765   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
766
767   augmentation[0] = 0;
768   augmentation_size = 0;
769
770   personality = current_unit_personality;
771   if (for_eh)
772     {
773       char *p;
774
775       /* Augmentation:
776          z      Indicates that a uleb128 is present to size the
777                 augmentation section.
778          L      Indicates the encoding (and thus presence) of
779                 an LSDA pointer in the FDE augmentation.
780          R      Indicates a non-default pointer encoding for
781                 FDE code pointers.
782          P      Indicates the presence of an encoding + language
783                 personality routine in the CIE augmentation.  */
784
785       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
786       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
787       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
788
789       p = augmentation + 1;
790       if (personality)
791         {
792           *p++ = 'P';
793           augmentation_size += 1 + size_of_encoded_value (per_encoding);
794           assemble_external_libcall (personality);
795         }
796       if (any_lsda_needed)
797         {
798           *p++ = 'L';
799           augmentation_size += 1;
800         }
801       if (fde_encoding != DW_EH_PE_absptr)
802         {
803           *p++ = 'R';
804           augmentation_size += 1;
805         }
806       if (p > augmentation + 1)
807         {
808           augmentation[0] = 'z';
809           *p = '\0';
810         }
811
812       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
813       if (personality && per_encoding == DW_EH_PE_aligned)
814         {
815           int offset = (  4             /* Length */
816                         + 4             /* CIE Id */
817                         + 1             /* CIE version */
818                         + strlen (augmentation) + 1     /* Augmentation */
819                         + size_of_uleb128 (1)           /* Code alignment */
820                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
821                         + 1             /* RA column */
822                         + 1             /* Augmentation size */
823                         + 1             /* Personality encoding */ );
824           int pad = -offset & (PTR_SIZE - 1);
825
826           augmentation_size += pad;
827
828           /* Augmentations should be small, so there's scarce need to
829              iterate for a solution.  Die if we exceed one uleb128 byte.  */
830           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
831         }
832     }
833
834   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
835   if (dw_cie_version >= 4)
836     {
837       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
838       dw2_asm_output_data (1, 0, "CIE Segment Size");
839     }
840   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
841   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
842                                "CIE Data Alignment Factor");
843
844   if (dw_cie_version == 1)
845     dw2_asm_output_data (1, return_reg, "CIE RA Column");
846   else
847     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
848
849   if (augmentation[0])
850     {
851       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
852       if (personality)
853         {
854           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
855                                eh_data_format_name (per_encoding));
856           dw2_asm_output_encoded_addr_rtx (per_encoding,
857                                            personality,
858                                            true, NULL);
859         }
860
861       if (any_lsda_needed)
862         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
863                              eh_data_format_name (lsda_encoding));
864
865       if (fde_encoding != DW_EH_PE_absptr)
866         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
867                              eh_data_format_name (fde_encoding));
868     }
869
870   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
871     output_cfi (cfi, NULL, for_eh);
872
873   /* Pad the CIE out to an address sized boundary.  */
874   ASM_OUTPUT_ALIGN (asm_out_file,
875                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
876   ASM_OUTPUT_LABEL (asm_out_file, l2);
877
878   /* Loop through all of the FDE's.  */
879   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
880     {
881       unsigned int k;
882
883       /* Don't emit EH unwind info for leaf functions that don't need it.  */
884       if (for_eh && !fde_needed_for_eh_p (fde))
885         continue;
886
887       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
888         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
889                     augmentation, any_lsda_needed, lsda_encoding);
890     }
891
892   if (for_eh && targetm.terminate_dw2_eh_frame_info)
893     dw2_asm_output_data (4, 0, "End of Table");
894 #ifdef MIPS_DEBUGGING_INFO
895   /* Work around Irix 6 assembler bug whereby labels at the end of a section
896      get a value of 0.  Putting .align 0 after the label fixes it.  */
897   ASM_OUTPUT_ALIGN (asm_out_file, 0);
898 #endif
899
900   /* Turn off app to make assembly quicker.  */
901   if (flag_debug_asm)
902     app_disable ();
903 }
904
905 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
906
907 static void
908 dwarf2out_do_cfi_startproc (bool second)
909 {
910   int enc;
911   rtx ref;
912   rtx personality = get_personality_function (current_function_decl);
913
914   fprintf (asm_out_file, "\t.cfi_startproc\n");
915
916   if (personality)
917     {
918       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
919       ref = personality;
920
921       /* ??? The GAS support isn't entirely consistent.  We have to
922          handle indirect support ourselves, but PC-relative is done
923          in the assembler.  Further, the assembler can't handle any
924          of the weirder relocation types.  */
925       if (enc & DW_EH_PE_indirect)
926         ref = dw2_force_const_mem (ref, true);
927
928       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
929       output_addr_const (asm_out_file, ref);
930       fputc ('\n', asm_out_file);
931     }
932
933   if (crtl->uses_eh_lsda)
934     {
935       char lab[20];
936
937       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
938       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
939                                    current_function_funcdef_no);
940       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
941       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
942
943       if (enc & DW_EH_PE_indirect)
944         ref = dw2_force_const_mem (ref, true);
945
946       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
947       output_addr_const (asm_out_file, ref);
948       fputc ('\n', asm_out_file);
949     }
950 }
951
952 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
953    this allocation may be done before pass_final.  */
954
955 dw_fde_ref
956 dwarf2out_alloc_current_fde (void)
957 {
958   dw_fde_ref fde;
959
960   fde = ggc_alloc_cleared_dw_fde_node ();
961   fde->decl = current_function_decl;
962   fde->funcdef_number = current_function_funcdef_no;
963   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
964   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
965   fde->uses_eh_lsda = crtl->uses_eh_lsda;
966   fde->nothrow = crtl->nothrow;
967   fde->drap_reg = INVALID_REGNUM;
968   fde->vdrap_reg = INVALID_REGNUM;
969
970   /* Record the FDE associated with this function.  */
971   cfun->fde = fde;
972   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
973
974   return fde;
975 }
976
977 /* Output a marker (i.e. a label) for the beginning of a function, before
978    the prologue.  */
979
980 void
981 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
982                           const char *file ATTRIBUTE_UNUSED)
983 {
984   char label[MAX_ARTIFICIAL_LABEL_BYTES];
985   char * dup_label;
986   dw_fde_ref fde;
987   section *fnsec;
988   bool do_frame;
989
990   current_function_func_begin_label = NULL;
991
992   do_frame = dwarf2out_do_frame ();
993
994   /* ??? current_function_func_begin_label is also used by except.c for
995      call-site information.  We must emit this label if it might be used.  */
996   if (!do_frame
997       && (!flag_exceptions
998           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
999     return;
1000
1001   fnsec = function_section (current_function_decl);
1002   switch_to_section (fnsec);
1003   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1004                                current_function_funcdef_no);
1005   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1006                           current_function_funcdef_no);
1007   dup_label = xstrdup (label);
1008   current_function_func_begin_label = dup_label;
1009
1010   /* We can elide the fde allocation if we're not emitting debug info.  */
1011   if (!do_frame)
1012     return;
1013
1014   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1015      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1016      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1017      do so now.  */
1018   fde = cfun->fde;
1019   if (fde == NULL)
1020     fde = dwarf2out_alloc_current_fde ();
1021
1022   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1023   fde->dw_fde_begin = dup_label;
1024   fde->dw_fde_current_label = dup_label;
1025   fde->in_std_section = (fnsec == text_section
1026                          || (cold_text_section && fnsec == cold_text_section));
1027
1028   /* We only want to output line number information for the genuine dwarf2
1029      prologue case, not the eh frame case.  */
1030 #ifdef DWARF2_DEBUGGING_INFO
1031   if (file)
1032     dwarf2out_source_line (line, file, 0, true);
1033 #endif
1034
1035   if (dwarf2out_do_cfi_asm ())
1036     dwarf2out_do_cfi_startproc (false);
1037   else
1038     {
1039       rtx personality = get_personality_function (current_function_decl);
1040       if (!current_unit_personality)
1041         current_unit_personality = personality;
1042
1043       /* We cannot keep a current personality per function as without CFI
1044          asm, at the point where we emit the CFI data, there is no current
1045          function anymore.  */
1046       if (personality && current_unit_personality != personality)
1047         sorry ("multiple EH personalities are supported only with assemblers "
1048                "supporting .cfi_personality directive");
1049     }
1050 }
1051
1052 /* Output a marker (i.e. a label) for the end of the generated code
1053    for a function prologue.  This gets called *after* the prologue code has
1054    been generated.  */
1055
1056 void
1057 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1058                         const char *file ATTRIBUTE_UNUSED)
1059 {
1060   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1061
1062   /* Output a label to mark the endpoint of the code generated for this
1063      function.  */
1064   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1065                                current_function_funcdef_no);
1066   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1067                           current_function_funcdef_no);
1068   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1069 }
1070
1071 /* Output a marker (i.e. a label) for the beginning of the generated code
1072    for a function epilogue.  This gets called *before* the prologue code has
1073    been generated.  */
1074
1075 void
1076 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1077                           const char *file ATTRIBUTE_UNUSED)
1078 {
1079   dw_fde_ref fde = cfun->fde;
1080   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1081
1082   if (fde->dw_fde_vms_begin_epilogue)
1083     return;
1084
1085   /* Output a label to mark the endpoint of the code generated for this
1086      function.  */
1087   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1088                                current_function_funcdef_no);
1089   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1090                           current_function_funcdef_no);
1091   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1092 }
1093
1094 /* Output a marker (i.e. a label) for the absolute end of the generated code
1095    for a function definition.  This gets called *after* the epilogue code has
1096    been generated.  */
1097
1098 void
1099 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1100                         const char *file ATTRIBUTE_UNUSED)
1101 {
1102   dw_fde_ref fde;
1103   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1104
1105   last_var_location_insn = NULL_RTX;
1106
1107   if (dwarf2out_do_cfi_asm ())
1108     fprintf (asm_out_file, "\t.cfi_endproc\n");
1109
1110   /* Output a label to mark the endpoint of the code generated for this
1111      function.  */
1112   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1113                                current_function_funcdef_no);
1114   ASM_OUTPUT_LABEL (asm_out_file, label);
1115   fde = cfun->fde;
1116   gcc_assert (fde != NULL);
1117   if (fde->dw_fde_second_begin == NULL)
1118     fde->dw_fde_end = xstrdup (label);
1119 }
1120
1121 void
1122 dwarf2out_frame_finish (void)
1123 {
1124   /* Output call frame information.  */
1125   if (targetm.debug_unwind_info () == UI_DWARF2)
1126     output_call_frame_info (0);
1127
1128   /* Output another copy for the unwinder.  */
1129   if ((flag_unwind_tables || flag_exceptions)
1130       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1131     output_call_frame_info (1);
1132 }
1133
1134 /* Note that the current function section is being used for code.  */
1135
1136 static void
1137 dwarf2out_note_section_used (void)
1138 {
1139   section *sec = current_function_section ();
1140   if (sec == text_section)
1141     text_section_used = true;
1142   else if (sec == cold_text_section)
1143     cold_text_section_used = true;
1144 }
1145
1146 static void var_location_switch_text_section (void);
1147 static void set_cur_line_info_table (section *);
1148
1149 void
1150 dwarf2out_switch_text_section (void)
1151 {
1152   section *sect;
1153   dw_fde_ref fde = cfun->fde;
1154
1155   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1156
1157   if (!in_cold_section_p)
1158     {
1159       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1160       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1161       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1162     }
1163   else
1164     {
1165       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1166       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1167       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1168     }
1169   have_multiple_function_sections = true;
1170
1171   /* There is no need to mark used sections when not debugging.  */
1172   if (cold_text_section != NULL)
1173     dwarf2out_note_section_used ();
1174
1175   if (dwarf2out_do_cfi_asm ())
1176     fprintf (asm_out_file, "\t.cfi_endproc\n");
1177
1178   /* Now do the real section switch.  */
1179   sect = current_function_section ();
1180   switch_to_section (sect);
1181
1182   fde->second_in_std_section
1183     = (sect == text_section
1184        || (cold_text_section && sect == cold_text_section));
1185
1186   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1187
1188   if (dwarf2out_do_cfi_asm ())
1189     {
1190       dwarf2out_do_cfi_startproc (true);
1191       /* As this is a different FDE, insert all current CFI instructions
1192          again.  */
1193       output_cfis (fde->dw_fde_cfi, fde->dw_fde_switch_cfi_index,
1194                    true, fde, true);
1195     }
1196
1197   var_location_switch_text_section ();
1198
1199   set_cur_line_info_table (sect);
1200 }
1201 \f
1202 /* And now, the subset of the debugging information support code necessary
1203    for emitting location expressions.  */
1204
1205 /* Data about a single source file.  */
1206 struct GTY(()) dwarf_file_data {
1207   const char * filename;
1208   int emitted_number;
1209 };
1210
1211 typedef struct GTY(()) deferred_locations_struct
1212 {
1213   tree variable;
1214   dw_die_ref die;
1215 } deferred_locations;
1216
1217 DEF_VEC_O(deferred_locations);
1218 DEF_VEC_ALLOC_O(deferred_locations,gc);
1219
1220 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1221
1222 DEF_VEC_P(dw_die_ref);
1223 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1224
1225 /* Location lists are ranges + location descriptions for that range,
1226    so you can track variables that are in different places over
1227    their entire life.  */
1228 typedef struct GTY(()) dw_loc_list_struct {
1229   dw_loc_list_ref dw_loc_next;
1230   const char *begin; /* Label for begin address of range */
1231   const char *end;  /* Label for end address of range */
1232   char *ll_symbol; /* Label for beginning of location list.
1233                       Only on head of list */
1234   const char *section; /* Section this loclist is relative to */
1235   dw_loc_descr_ref expr;
1236   hashval_t hash;
1237   /* True if all addresses in this and subsequent lists are known to be
1238      resolved.  */
1239   bool resolved_addr;
1240   /* True if this list has been replaced by dw_loc_next.  */
1241   bool replaced;
1242   bool emitted;
1243   /* True if the range should be emitted even if begin and end
1244      are the same.  */
1245   bool force;
1246 } dw_loc_list_node;
1247
1248 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1249
1250 /* Convert a DWARF stack opcode into its string name.  */
1251
1252 static const char *
1253 dwarf_stack_op_name (unsigned int op)
1254 {
1255   switch (op)
1256     {
1257     case DW_OP_addr:
1258       return "DW_OP_addr";
1259     case DW_OP_deref:
1260       return "DW_OP_deref";
1261     case DW_OP_const1u:
1262       return "DW_OP_const1u";
1263     case DW_OP_const1s:
1264       return "DW_OP_const1s";
1265     case DW_OP_const2u:
1266       return "DW_OP_const2u";
1267     case DW_OP_const2s:
1268       return "DW_OP_const2s";
1269     case DW_OP_const4u:
1270       return "DW_OP_const4u";
1271     case DW_OP_const4s:
1272       return "DW_OP_const4s";
1273     case DW_OP_const8u:
1274       return "DW_OP_const8u";
1275     case DW_OP_const8s:
1276       return "DW_OP_const8s";
1277     case DW_OP_constu:
1278       return "DW_OP_constu";
1279     case DW_OP_consts:
1280       return "DW_OP_consts";
1281     case DW_OP_dup:
1282       return "DW_OP_dup";
1283     case DW_OP_drop:
1284       return "DW_OP_drop";
1285     case DW_OP_over:
1286       return "DW_OP_over";
1287     case DW_OP_pick:
1288       return "DW_OP_pick";
1289     case DW_OP_swap:
1290       return "DW_OP_swap";
1291     case DW_OP_rot:
1292       return "DW_OP_rot";
1293     case DW_OP_xderef:
1294       return "DW_OP_xderef";
1295     case DW_OP_abs:
1296       return "DW_OP_abs";
1297     case DW_OP_and:
1298       return "DW_OP_and";
1299     case DW_OP_div:
1300       return "DW_OP_div";
1301     case DW_OP_minus:
1302       return "DW_OP_minus";
1303     case DW_OP_mod:
1304       return "DW_OP_mod";
1305     case DW_OP_mul:
1306       return "DW_OP_mul";
1307     case DW_OP_neg:
1308       return "DW_OP_neg";
1309     case DW_OP_not:
1310       return "DW_OP_not";
1311     case DW_OP_or:
1312       return "DW_OP_or";
1313     case DW_OP_plus:
1314       return "DW_OP_plus";
1315     case DW_OP_plus_uconst:
1316       return "DW_OP_plus_uconst";
1317     case DW_OP_shl:
1318       return "DW_OP_shl";
1319     case DW_OP_shr:
1320       return "DW_OP_shr";
1321     case DW_OP_shra:
1322       return "DW_OP_shra";
1323     case DW_OP_xor:
1324       return "DW_OP_xor";
1325     case DW_OP_bra:
1326       return "DW_OP_bra";
1327     case DW_OP_eq:
1328       return "DW_OP_eq";
1329     case DW_OP_ge:
1330       return "DW_OP_ge";
1331     case DW_OP_gt:
1332       return "DW_OP_gt";
1333     case DW_OP_le:
1334       return "DW_OP_le";
1335     case DW_OP_lt:
1336       return "DW_OP_lt";
1337     case DW_OP_ne:
1338       return "DW_OP_ne";
1339     case DW_OP_skip:
1340       return "DW_OP_skip";
1341     case DW_OP_lit0:
1342       return "DW_OP_lit0";
1343     case DW_OP_lit1:
1344       return "DW_OP_lit1";
1345     case DW_OP_lit2:
1346       return "DW_OP_lit2";
1347     case DW_OP_lit3:
1348       return "DW_OP_lit3";
1349     case DW_OP_lit4:
1350       return "DW_OP_lit4";
1351     case DW_OP_lit5:
1352       return "DW_OP_lit5";
1353     case DW_OP_lit6:
1354       return "DW_OP_lit6";
1355     case DW_OP_lit7:
1356       return "DW_OP_lit7";
1357     case DW_OP_lit8:
1358       return "DW_OP_lit8";
1359     case DW_OP_lit9:
1360       return "DW_OP_lit9";
1361     case DW_OP_lit10:
1362       return "DW_OP_lit10";
1363     case DW_OP_lit11:
1364       return "DW_OP_lit11";
1365     case DW_OP_lit12:
1366       return "DW_OP_lit12";
1367     case DW_OP_lit13:
1368       return "DW_OP_lit13";
1369     case DW_OP_lit14:
1370       return "DW_OP_lit14";
1371     case DW_OP_lit15:
1372       return "DW_OP_lit15";
1373     case DW_OP_lit16:
1374       return "DW_OP_lit16";
1375     case DW_OP_lit17:
1376       return "DW_OP_lit17";
1377     case DW_OP_lit18:
1378       return "DW_OP_lit18";
1379     case DW_OP_lit19:
1380       return "DW_OP_lit19";
1381     case DW_OP_lit20:
1382       return "DW_OP_lit20";
1383     case DW_OP_lit21:
1384       return "DW_OP_lit21";
1385     case DW_OP_lit22:
1386       return "DW_OP_lit22";
1387     case DW_OP_lit23:
1388       return "DW_OP_lit23";
1389     case DW_OP_lit24:
1390       return "DW_OP_lit24";
1391     case DW_OP_lit25:
1392       return "DW_OP_lit25";
1393     case DW_OP_lit26:
1394       return "DW_OP_lit26";
1395     case DW_OP_lit27:
1396       return "DW_OP_lit27";
1397     case DW_OP_lit28:
1398       return "DW_OP_lit28";
1399     case DW_OP_lit29:
1400       return "DW_OP_lit29";
1401     case DW_OP_lit30:
1402       return "DW_OP_lit30";
1403     case DW_OP_lit31:
1404       return "DW_OP_lit31";
1405     case DW_OP_reg0:
1406       return "DW_OP_reg0";
1407     case DW_OP_reg1:
1408       return "DW_OP_reg1";
1409     case DW_OP_reg2:
1410       return "DW_OP_reg2";
1411     case DW_OP_reg3:
1412       return "DW_OP_reg3";
1413     case DW_OP_reg4:
1414       return "DW_OP_reg4";
1415     case DW_OP_reg5:
1416       return "DW_OP_reg5";
1417     case DW_OP_reg6:
1418       return "DW_OP_reg6";
1419     case DW_OP_reg7:
1420       return "DW_OP_reg7";
1421     case DW_OP_reg8:
1422       return "DW_OP_reg8";
1423     case DW_OP_reg9:
1424       return "DW_OP_reg9";
1425     case DW_OP_reg10:
1426       return "DW_OP_reg10";
1427     case DW_OP_reg11:
1428       return "DW_OP_reg11";
1429     case DW_OP_reg12:
1430       return "DW_OP_reg12";
1431     case DW_OP_reg13:
1432       return "DW_OP_reg13";
1433     case DW_OP_reg14:
1434       return "DW_OP_reg14";
1435     case DW_OP_reg15:
1436       return "DW_OP_reg15";
1437     case DW_OP_reg16:
1438       return "DW_OP_reg16";
1439     case DW_OP_reg17:
1440       return "DW_OP_reg17";
1441     case DW_OP_reg18:
1442       return "DW_OP_reg18";
1443     case DW_OP_reg19:
1444       return "DW_OP_reg19";
1445     case DW_OP_reg20:
1446       return "DW_OP_reg20";
1447     case DW_OP_reg21:
1448       return "DW_OP_reg21";
1449     case DW_OP_reg22:
1450       return "DW_OP_reg22";
1451     case DW_OP_reg23:
1452       return "DW_OP_reg23";
1453     case DW_OP_reg24:
1454       return "DW_OP_reg24";
1455     case DW_OP_reg25:
1456       return "DW_OP_reg25";
1457     case DW_OP_reg26:
1458       return "DW_OP_reg26";
1459     case DW_OP_reg27:
1460       return "DW_OP_reg27";
1461     case DW_OP_reg28:
1462       return "DW_OP_reg28";
1463     case DW_OP_reg29:
1464       return "DW_OP_reg29";
1465     case DW_OP_reg30:
1466       return "DW_OP_reg30";
1467     case DW_OP_reg31:
1468       return "DW_OP_reg31";
1469     case DW_OP_breg0:
1470       return "DW_OP_breg0";
1471     case DW_OP_breg1:
1472       return "DW_OP_breg1";
1473     case DW_OP_breg2:
1474       return "DW_OP_breg2";
1475     case DW_OP_breg3:
1476       return "DW_OP_breg3";
1477     case DW_OP_breg4:
1478       return "DW_OP_breg4";
1479     case DW_OP_breg5:
1480       return "DW_OP_breg5";
1481     case DW_OP_breg6:
1482       return "DW_OP_breg6";
1483     case DW_OP_breg7:
1484       return "DW_OP_breg7";
1485     case DW_OP_breg8:
1486       return "DW_OP_breg8";
1487     case DW_OP_breg9:
1488       return "DW_OP_breg9";
1489     case DW_OP_breg10:
1490       return "DW_OP_breg10";
1491     case DW_OP_breg11:
1492       return "DW_OP_breg11";
1493     case DW_OP_breg12:
1494       return "DW_OP_breg12";
1495     case DW_OP_breg13:
1496       return "DW_OP_breg13";
1497     case DW_OP_breg14:
1498       return "DW_OP_breg14";
1499     case DW_OP_breg15:
1500       return "DW_OP_breg15";
1501     case DW_OP_breg16:
1502       return "DW_OP_breg16";
1503     case DW_OP_breg17:
1504       return "DW_OP_breg17";
1505     case DW_OP_breg18:
1506       return "DW_OP_breg18";
1507     case DW_OP_breg19:
1508       return "DW_OP_breg19";
1509     case DW_OP_breg20:
1510       return "DW_OP_breg20";
1511     case DW_OP_breg21:
1512       return "DW_OP_breg21";
1513     case DW_OP_breg22:
1514       return "DW_OP_breg22";
1515     case DW_OP_breg23:
1516       return "DW_OP_breg23";
1517     case DW_OP_breg24:
1518       return "DW_OP_breg24";
1519     case DW_OP_breg25:
1520       return "DW_OP_breg25";
1521     case DW_OP_breg26:
1522       return "DW_OP_breg26";
1523     case DW_OP_breg27:
1524       return "DW_OP_breg27";
1525     case DW_OP_breg28:
1526       return "DW_OP_breg28";
1527     case DW_OP_breg29:
1528       return "DW_OP_breg29";
1529     case DW_OP_breg30:
1530       return "DW_OP_breg30";
1531     case DW_OP_breg31:
1532       return "DW_OP_breg31";
1533     case DW_OP_regx:
1534       return "DW_OP_regx";
1535     case DW_OP_fbreg:
1536       return "DW_OP_fbreg";
1537     case DW_OP_bregx:
1538       return "DW_OP_bregx";
1539     case DW_OP_piece:
1540       return "DW_OP_piece";
1541     case DW_OP_deref_size:
1542       return "DW_OP_deref_size";
1543     case DW_OP_xderef_size:
1544       return "DW_OP_xderef_size";
1545     case DW_OP_nop:
1546       return "DW_OP_nop";
1547
1548     case DW_OP_push_object_address:
1549       return "DW_OP_push_object_address";
1550     case DW_OP_call2:
1551       return "DW_OP_call2";
1552     case DW_OP_call4:
1553       return "DW_OP_call4";
1554     case DW_OP_call_ref:
1555       return "DW_OP_call_ref";
1556     case DW_OP_implicit_value:
1557       return "DW_OP_implicit_value";
1558     case DW_OP_stack_value:
1559       return "DW_OP_stack_value";
1560     case DW_OP_form_tls_address:
1561       return "DW_OP_form_tls_address";
1562     case DW_OP_call_frame_cfa:
1563       return "DW_OP_call_frame_cfa";
1564     case DW_OP_bit_piece:
1565       return "DW_OP_bit_piece";
1566
1567     case DW_OP_GNU_push_tls_address:
1568       return "DW_OP_GNU_push_tls_address";
1569     case DW_OP_GNU_uninit:
1570       return "DW_OP_GNU_uninit";
1571     case DW_OP_GNU_encoded_addr:
1572       return "DW_OP_GNU_encoded_addr";
1573     case DW_OP_GNU_implicit_pointer:
1574       return "DW_OP_GNU_implicit_pointer";
1575     case DW_OP_GNU_entry_value:
1576       return "DW_OP_GNU_entry_value";
1577     case DW_OP_GNU_const_type:
1578       return "DW_OP_GNU_const_type";
1579     case DW_OP_GNU_regval_type:
1580       return "DW_OP_GNU_regval_type";
1581     case DW_OP_GNU_deref_type:
1582       return "DW_OP_GNU_deref_type";
1583     case DW_OP_GNU_convert:
1584       return "DW_OP_GNU_convert";
1585     case DW_OP_GNU_reinterpret:
1586       return "DW_OP_GNU_reinterpret";
1587     case DW_OP_GNU_parameter_ref:
1588       return "DW_OP_GNU_parameter_ref";
1589
1590     default:
1591       return "OP_<unknown>";
1592     }
1593 }
1594
1595 /* Return a pointer to a newly allocated location description.  Location
1596    descriptions are simple expression terms that can be strung
1597    together to form more complicated location (address) descriptions.  */
1598
1599 static inline dw_loc_descr_ref
1600 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1601                unsigned HOST_WIDE_INT oprnd2)
1602 {
1603   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1604
1605   descr->dw_loc_opc = op;
1606   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1607   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1608   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1609   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1610
1611   return descr;
1612 }
1613
1614 /* Return a pointer to a newly allocated location description for
1615    REG and OFFSET.  */
1616
1617 static inline dw_loc_descr_ref
1618 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1619 {
1620   if (reg <= 31)
1621     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1622                           offset, 0);
1623   else
1624     return new_loc_descr (DW_OP_bregx, reg, offset);
1625 }
1626
1627 /* Add a location description term to a location description expression.  */
1628
1629 static inline void
1630 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1631 {
1632   dw_loc_descr_ref *d;
1633
1634   /* Find the end of the chain.  */
1635   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1636     ;
1637
1638   *d = descr;
1639 }
1640
1641 /* Add a constant OFFSET to a location expression.  */
1642
1643 static void
1644 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1645 {
1646   dw_loc_descr_ref loc;
1647   HOST_WIDE_INT *p;
1648
1649   gcc_assert (*list_head != NULL);
1650
1651   if (!offset)
1652     return;
1653
1654   /* Find the end of the chain.  */
1655   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1656     ;
1657
1658   p = NULL;
1659   if (loc->dw_loc_opc == DW_OP_fbreg
1660       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1661     p = &loc->dw_loc_oprnd1.v.val_int;
1662   else if (loc->dw_loc_opc == DW_OP_bregx)
1663     p = &loc->dw_loc_oprnd2.v.val_int;
1664
1665   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1666      offset.  Don't optimize if an signed integer overflow would happen.  */
1667   if (p != NULL
1668       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1669           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1670     *p += offset;
1671
1672   else if (offset > 0)
1673     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1674
1675   else
1676     {
1677       loc->dw_loc_next = int_loc_descriptor (-offset);
1678       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1679     }
1680 }
1681
1682 /* Add a constant OFFSET to a location list.  */
1683
1684 static void
1685 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1686 {
1687   dw_loc_list_ref d;
1688   for (d = list_head; d != NULL; d = d->dw_loc_next)
1689     loc_descr_plus_const (&d->expr, offset);
1690 }
1691
1692 #define DWARF_REF_SIZE  \
1693   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1694
1695 static unsigned long int get_base_type_offset (dw_die_ref);
1696
1697 /* Return the size of a location descriptor.  */
1698
1699 static unsigned long
1700 size_of_loc_descr (dw_loc_descr_ref loc)
1701 {
1702   unsigned long size = 1;
1703
1704   switch (loc->dw_loc_opc)
1705     {
1706     case DW_OP_addr:
1707       size += DWARF2_ADDR_SIZE;
1708       break;
1709     case DW_OP_const1u:
1710     case DW_OP_const1s:
1711       size += 1;
1712       break;
1713     case DW_OP_const2u:
1714     case DW_OP_const2s:
1715       size += 2;
1716       break;
1717     case DW_OP_const4u:
1718     case DW_OP_const4s:
1719       size += 4;
1720       break;
1721     case DW_OP_const8u:
1722     case DW_OP_const8s:
1723       size += 8;
1724       break;
1725     case DW_OP_constu:
1726       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727       break;
1728     case DW_OP_consts:
1729       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730       break;
1731     case DW_OP_pick:
1732       size += 1;
1733       break;
1734     case DW_OP_plus_uconst:
1735       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736       break;
1737     case DW_OP_skip:
1738     case DW_OP_bra:
1739       size += 2;
1740       break;
1741     case DW_OP_breg0:
1742     case DW_OP_breg1:
1743     case DW_OP_breg2:
1744     case DW_OP_breg3:
1745     case DW_OP_breg4:
1746     case DW_OP_breg5:
1747     case DW_OP_breg6:
1748     case DW_OP_breg7:
1749     case DW_OP_breg8:
1750     case DW_OP_breg9:
1751     case DW_OP_breg10:
1752     case DW_OP_breg11:
1753     case DW_OP_breg12:
1754     case DW_OP_breg13:
1755     case DW_OP_breg14:
1756     case DW_OP_breg15:
1757     case DW_OP_breg16:
1758     case DW_OP_breg17:
1759     case DW_OP_breg18:
1760     case DW_OP_breg19:
1761     case DW_OP_breg20:
1762     case DW_OP_breg21:
1763     case DW_OP_breg22:
1764     case DW_OP_breg23:
1765     case DW_OP_breg24:
1766     case DW_OP_breg25:
1767     case DW_OP_breg26:
1768     case DW_OP_breg27:
1769     case DW_OP_breg28:
1770     case DW_OP_breg29:
1771     case DW_OP_breg30:
1772     case DW_OP_breg31:
1773       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774       break;
1775     case DW_OP_regx:
1776       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777       break;
1778     case DW_OP_fbreg:
1779       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780       break;
1781     case DW_OP_bregx:
1782       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784       break;
1785     case DW_OP_piece:
1786       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787       break;
1788     case DW_OP_bit_piece:
1789       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791       break;
1792     case DW_OP_deref_size:
1793     case DW_OP_xderef_size:
1794       size += 1;
1795       break;
1796     case DW_OP_call2:
1797       size += 2;
1798       break;
1799     case DW_OP_call4:
1800       size += 4;
1801       break;
1802     case DW_OP_call_ref:
1803       size += DWARF_REF_SIZE;
1804       break;
1805     case DW_OP_implicit_value:
1806       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1807               + loc->dw_loc_oprnd1.v.val_unsigned;
1808       break;
1809     case DW_OP_GNU_implicit_pointer:
1810       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1811       break;
1812     case DW_OP_GNU_entry_value:
1813       {
1814         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1815         size += size_of_uleb128 (op_size) + op_size;
1816         break;
1817       }
1818     case DW_OP_GNU_const_type:
1819       {
1820         unsigned long o
1821           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1822         size += size_of_uleb128 (o) + 1;
1823         switch (loc->dw_loc_oprnd2.val_class)
1824           {
1825           case dw_val_class_vec:
1826             size += loc->dw_loc_oprnd2.v.val_vec.length
1827                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1828             break;
1829           case dw_val_class_const:
1830             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1831             break;
1832           case dw_val_class_const_double:
1833             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1834             break;
1835           default:
1836             gcc_unreachable ();
1837           }
1838         break;
1839       }
1840     case DW_OP_GNU_regval_type:
1841       {
1842         unsigned long o
1843           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1844         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1845                 + size_of_uleb128 (o);
1846       }
1847       break;
1848     case DW_OP_GNU_deref_type:
1849       {
1850         unsigned long o
1851           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1852         size += 1 + size_of_uleb128 (o);
1853       }
1854       break;
1855     case DW_OP_GNU_convert:
1856     case DW_OP_GNU_reinterpret:
1857       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1858         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1859       else
1860         {
1861           unsigned long o
1862             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1863           size += size_of_uleb128 (o);
1864         }
1865       break;
1866     case DW_OP_GNU_parameter_ref:
1867       size += 4;
1868       break;
1869     default:
1870       break;
1871     }
1872
1873   return size;
1874 }
1875
1876 /* Return the size of a series of location descriptors.  */
1877
1878 unsigned long
1879 size_of_locs (dw_loc_descr_ref loc)
1880 {
1881   dw_loc_descr_ref l;
1882   unsigned long size;
1883
1884   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1885      field, to avoid writing to a PCH file.  */
1886   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1887     {
1888       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1889         break;
1890       size += size_of_loc_descr (l);
1891     }
1892   if (! l)
1893     return size;
1894
1895   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1896     {
1897       l->dw_loc_addr = size;
1898       size += size_of_loc_descr (l);
1899     }
1900
1901   return size;
1902 }
1903
1904 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1905 static void get_ref_die_offset_label (char *, dw_die_ref);
1906 static unsigned long int get_ref_die_offset (dw_die_ref);
1907
1908 /* Output location description stack opcode's operands (if any).
1909    The for_eh_or_skip parameter controls whether register numbers are
1910    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1911    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1912    info).  This should be suppressed for the cases that have not been converted
1913    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1914
1915 static void
1916 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1917 {
1918   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1919   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1920
1921   switch (loc->dw_loc_opc)
1922     {
1923 #ifdef DWARF2_DEBUGGING_INFO
1924     case DW_OP_const2u:
1925     case DW_OP_const2s:
1926       dw2_asm_output_data (2, val1->v.val_int, NULL);
1927       break;
1928     case DW_OP_const4u:
1929       if (loc->dtprel)
1930         {
1931           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1932           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1933                                                val1->v.val_addr);
1934           fputc ('\n', asm_out_file);
1935           break;
1936         }
1937       /* FALLTHRU */
1938     case DW_OP_const4s:
1939       dw2_asm_output_data (4, val1->v.val_int, NULL);
1940       break;
1941     case DW_OP_const8u:
1942       if (loc->dtprel)
1943         {
1944           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1945           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1946                                                val1->v.val_addr);
1947           fputc ('\n', asm_out_file);
1948           break;
1949         }
1950       /* FALLTHRU */
1951     case DW_OP_const8s:
1952       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1953       dw2_asm_output_data (8, val1->v.val_int, NULL);
1954       break;
1955     case DW_OP_skip:
1956     case DW_OP_bra:
1957       {
1958         int offset;
1959
1960         gcc_assert (val1->val_class == dw_val_class_loc);
1961         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1962
1963         dw2_asm_output_data (2, offset, NULL);
1964       }
1965       break;
1966     case DW_OP_implicit_value:
1967       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1968       switch (val2->val_class)
1969         {
1970         case dw_val_class_const:
1971           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1972           break;
1973         case dw_val_class_vec:
1974           {
1975             unsigned int elt_size = val2->v.val_vec.elt_size;
1976             unsigned int len = val2->v.val_vec.length;
1977             unsigned int i;
1978             unsigned char *p;
1979
1980             if (elt_size > sizeof (HOST_WIDE_INT))
1981               {
1982                 elt_size /= 2;
1983                 len *= 2;
1984               }
1985             for (i = 0, p = val2->v.val_vec.array;
1986                  i < len;
1987                  i++, p += elt_size)
1988               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1989                                    "fp or vector constant word %u", i);
1990           }
1991           break;
1992         case dw_val_class_const_double:
1993           {
1994             unsigned HOST_WIDE_INT first, second;
1995
1996             if (WORDS_BIG_ENDIAN)
1997               {
1998                 first = val2->v.val_double.high;
1999                 second = val2->v.val_double.low;
2000               }
2001             else
2002               {
2003                 first = val2->v.val_double.low;
2004                 second = val2->v.val_double.high;
2005               }
2006             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2007                                  first, NULL);
2008             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2009                                  second, NULL);
2010           }
2011           break;
2012         case dw_val_class_addr:
2013           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2014           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2015           break;
2016         default:
2017           gcc_unreachable ();
2018         }
2019       break;
2020 #else
2021     case DW_OP_const2u:
2022     case DW_OP_const2s:
2023     case DW_OP_const4u:
2024     case DW_OP_const4s:
2025     case DW_OP_const8u:
2026     case DW_OP_const8s:
2027     case DW_OP_skip:
2028     case DW_OP_bra:
2029     case DW_OP_implicit_value:
2030       /* We currently don't make any attempt to make sure these are
2031          aligned properly like we do for the main unwind info, so
2032          don't support emitting things larger than a byte if we're
2033          only doing unwinding.  */
2034       gcc_unreachable ();
2035 #endif
2036     case DW_OP_const1u:
2037     case DW_OP_const1s:
2038       dw2_asm_output_data (1, val1->v.val_int, NULL);
2039       break;
2040     case DW_OP_constu:
2041       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2042       break;
2043     case DW_OP_consts:
2044       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2045       break;
2046     case DW_OP_pick:
2047       dw2_asm_output_data (1, val1->v.val_int, NULL);
2048       break;
2049     case DW_OP_plus_uconst:
2050       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2051       break;
2052     case DW_OP_breg0:
2053     case DW_OP_breg1:
2054     case DW_OP_breg2:
2055     case DW_OP_breg3:
2056     case DW_OP_breg4:
2057     case DW_OP_breg5:
2058     case DW_OP_breg6:
2059     case DW_OP_breg7:
2060     case DW_OP_breg8:
2061     case DW_OP_breg9:
2062     case DW_OP_breg10:
2063     case DW_OP_breg11:
2064     case DW_OP_breg12:
2065     case DW_OP_breg13:
2066     case DW_OP_breg14:
2067     case DW_OP_breg15:
2068     case DW_OP_breg16:
2069     case DW_OP_breg17:
2070     case DW_OP_breg18:
2071     case DW_OP_breg19:
2072     case DW_OP_breg20:
2073     case DW_OP_breg21:
2074     case DW_OP_breg22:
2075     case DW_OP_breg23:
2076     case DW_OP_breg24:
2077     case DW_OP_breg25:
2078     case DW_OP_breg26:
2079     case DW_OP_breg27:
2080     case DW_OP_breg28:
2081     case DW_OP_breg29:
2082     case DW_OP_breg30:
2083     case DW_OP_breg31:
2084       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2085       break;
2086     case DW_OP_regx:
2087       {
2088         unsigned r = val1->v.val_unsigned;
2089         if (for_eh_or_skip >= 0)
2090           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2091         gcc_assert (size_of_uleb128 (r) 
2092                     == size_of_uleb128 (val1->v.val_unsigned));
2093         dw2_asm_output_data_uleb128 (r, NULL);  
2094       }
2095       break;
2096     case DW_OP_fbreg:
2097       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2098       break;
2099     case DW_OP_bregx:
2100       {
2101         unsigned r = val1->v.val_unsigned;
2102         if (for_eh_or_skip >= 0)
2103           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2104         gcc_assert (size_of_uleb128 (r) 
2105                     == size_of_uleb128 (val1->v.val_unsigned));
2106         dw2_asm_output_data_uleb128 (r, NULL);  
2107         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2108       }
2109       break;
2110     case DW_OP_piece:
2111       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2112       break;
2113     case DW_OP_bit_piece:
2114       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2116       break;
2117     case DW_OP_deref_size:
2118     case DW_OP_xderef_size:
2119       dw2_asm_output_data (1, val1->v.val_int, NULL);
2120       break;
2121
2122     case DW_OP_addr:
2123       if (loc->dtprel)
2124         {
2125           if (targetm.asm_out.output_dwarf_dtprel)
2126             {
2127               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2128                                                    DWARF2_ADDR_SIZE,
2129                                                    val1->v.val_addr);
2130               fputc ('\n', asm_out_file);
2131             }
2132           else
2133             gcc_unreachable ();
2134         }
2135       else
2136         {
2137 #ifdef DWARF2_DEBUGGING_INFO
2138           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2139 #else
2140           gcc_unreachable ();
2141 #endif
2142         }
2143       break;
2144
2145     case DW_OP_GNU_implicit_pointer:
2146       {
2147         char label[MAX_ARTIFICIAL_LABEL_BYTES
2148                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2149         gcc_assert (val1->val_class == dw_val_class_die_ref);
2150         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2151         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2152         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2153       }
2154       break;
2155
2156     case DW_OP_GNU_entry_value:
2157       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2158       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2159       break;
2160
2161     case DW_OP_GNU_const_type:
2162       {
2163         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2164         gcc_assert (o);
2165         dw2_asm_output_data_uleb128 (o, NULL);
2166         switch (val2->val_class)
2167           {
2168           case dw_val_class_const:
2169             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2170             dw2_asm_output_data (1, l, NULL);
2171             dw2_asm_output_data (l, val2->v.val_int, NULL);
2172             break;
2173           case dw_val_class_vec:
2174             {
2175               unsigned int elt_size = val2->v.val_vec.elt_size;
2176               unsigned int len = val2->v.val_vec.length;
2177               unsigned int i;
2178               unsigned char *p;
2179
2180               l = len * elt_size;
2181               dw2_asm_output_data (1, l, NULL);
2182               if (elt_size > sizeof (HOST_WIDE_INT))
2183                 {
2184                   elt_size /= 2;
2185                   len *= 2;
2186                 }
2187               for (i = 0, p = val2->v.val_vec.array;
2188                    i < len;
2189                    i++, p += elt_size)
2190                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2191                                      "fp or vector constant word %u", i);
2192             }
2193             break;
2194           case dw_val_class_const_double:
2195             {
2196               unsigned HOST_WIDE_INT first, second;
2197               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2198
2199               dw2_asm_output_data (1, 2 * l, NULL);
2200               if (WORDS_BIG_ENDIAN)
2201                 {
2202                   first = val2->v.val_double.high;
2203                   second = val2->v.val_double.low;
2204                 }
2205               else
2206                 {
2207                   first = val2->v.val_double.low;
2208                   second = val2->v.val_double.high;
2209                 }
2210               dw2_asm_output_data (l, first, NULL);
2211               dw2_asm_output_data (l, second, NULL);
2212             }
2213             break;
2214           default:
2215             gcc_unreachable ();
2216           }
2217       }
2218       break;
2219     case DW_OP_GNU_regval_type:
2220       {
2221         unsigned r = val1->v.val_unsigned;
2222         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2223         gcc_assert (o);
2224         if (for_eh_or_skip >= 0)
2225           {
2226             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2227             gcc_assert (size_of_uleb128 (r)
2228                         == size_of_uleb128 (val1->v.val_unsigned));
2229           }
2230         dw2_asm_output_data_uleb128 (r, NULL);
2231         dw2_asm_output_data_uleb128 (o, NULL);
2232       }
2233       break;
2234     case DW_OP_GNU_deref_type:
2235       {
2236         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2237         gcc_assert (o);
2238         dw2_asm_output_data (1, val1->v.val_int, NULL);
2239         dw2_asm_output_data_uleb128 (o, NULL);
2240       }
2241       break;
2242     case DW_OP_GNU_convert:
2243     case DW_OP_GNU_reinterpret:
2244       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2245         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2246       else
2247         {
2248           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2249           gcc_assert (o);
2250           dw2_asm_output_data_uleb128 (o, NULL);
2251         }
2252       break;
2253
2254     case DW_OP_GNU_parameter_ref:
2255       {
2256         unsigned long o;
2257         gcc_assert (val1->val_class == dw_val_class_die_ref);
2258         o = get_ref_die_offset (val1->v.val_die_ref.die);
2259         dw2_asm_output_data (4, o, NULL);
2260       }
2261       break;
2262
2263     default:
2264       /* Other codes have no operands.  */
2265       break;
2266     }
2267 }
2268
2269 /* Output a sequence of location operations.  
2270    The for_eh_or_skip parameter controls whether register numbers are
2271    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2272    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2273    info).  This should be suppressed for the cases that have not been converted
2274    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2275
2276 void
2277 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2278 {
2279   for (; loc != NULL; loc = loc->dw_loc_next)
2280     {
2281       enum dwarf_location_atom opc = loc->dw_loc_opc;
2282       /* Output the opcode.  */
2283       if (for_eh_or_skip >= 0 
2284           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2285         {
2286           unsigned r = (opc - DW_OP_breg0);
2287           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2288           gcc_assert (r <= 31);
2289           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2290         }
2291       else if (for_eh_or_skip >= 0 
2292                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2293         {
2294           unsigned r = (opc - DW_OP_reg0);
2295           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2296           gcc_assert (r <= 31);
2297           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2298         }
2299
2300       dw2_asm_output_data (1, opc,
2301                              "%s", dwarf_stack_op_name (opc));
2302
2303       /* Output the operand(s) (if any).  */
2304       output_loc_operands (loc, for_eh_or_skip);
2305     }
2306 }
2307
2308 /* Output location description stack opcode's operands (if any).
2309    The output is single bytes on a line, suitable for .cfi_escape.  */
2310
2311 static void
2312 output_loc_operands_raw (dw_loc_descr_ref loc)
2313 {
2314   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2315   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2316
2317   switch (loc->dw_loc_opc)
2318     {
2319     case DW_OP_addr:
2320     case DW_OP_implicit_value:
2321       /* We cannot output addresses in .cfi_escape, only bytes.  */
2322       gcc_unreachable ();
2323
2324     case DW_OP_const1u:
2325     case DW_OP_const1s:
2326     case DW_OP_pick:
2327     case DW_OP_deref_size:
2328     case DW_OP_xderef_size:
2329       fputc (',', asm_out_file);
2330       dw2_asm_output_data_raw (1, val1->v.val_int);
2331       break;
2332
2333     case DW_OP_const2u:
2334     case DW_OP_const2s:
2335       fputc (',', asm_out_file);
2336       dw2_asm_output_data_raw (2, val1->v.val_int);
2337       break;
2338
2339     case DW_OP_const4u:
2340     case DW_OP_const4s:
2341       fputc (',', asm_out_file);
2342       dw2_asm_output_data_raw (4, val1->v.val_int);
2343       break;
2344
2345     case DW_OP_const8u:
2346     case DW_OP_const8s:
2347       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2348       fputc (',', asm_out_file);
2349       dw2_asm_output_data_raw (8, val1->v.val_int);
2350       break;
2351
2352     case DW_OP_skip:
2353     case DW_OP_bra:
2354       {
2355         int offset;
2356
2357         gcc_assert (val1->val_class == dw_val_class_loc);
2358         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2359
2360         fputc (',', asm_out_file);
2361         dw2_asm_output_data_raw (2, offset);
2362       }
2363       break;
2364
2365     case DW_OP_regx:
2366       {
2367         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2368         gcc_assert (size_of_uleb128 (r) 
2369                     == size_of_uleb128 (val1->v.val_unsigned));
2370         fputc (',', asm_out_file);
2371         dw2_asm_output_data_uleb128_raw (r);
2372       }
2373       break;
2374       
2375     case DW_OP_constu:
2376     case DW_OP_plus_uconst:
2377     case DW_OP_piece:
2378       fputc (',', asm_out_file);
2379       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2380       break;
2381
2382     case DW_OP_bit_piece:
2383       fputc (',', asm_out_file);
2384       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2385       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2386       break;
2387
2388     case DW_OP_consts:
2389     case DW_OP_breg0:
2390     case DW_OP_breg1:
2391     case DW_OP_breg2:
2392     case DW_OP_breg3:
2393     case DW_OP_breg4:
2394     case DW_OP_breg5:
2395     case DW_OP_breg6:
2396     case DW_OP_breg7:
2397     case DW_OP_breg8:
2398     case DW_OP_breg9:
2399     case DW_OP_breg10:
2400     case DW_OP_breg11:
2401     case DW_OP_breg12:
2402     case DW_OP_breg13:
2403     case DW_OP_breg14:
2404     case DW_OP_breg15:
2405     case DW_OP_breg16:
2406     case DW_OP_breg17:
2407     case DW_OP_breg18:
2408     case DW_OP_breg19:
2409     case DW_OP_breg20:
2410     case DW_OP_breg21:
2411     case DW_OP_breg22:
2412     case DW_OP_breg23:
2413     case DW_OP_breg24:
2414     case DW_OP_breg25:
2415     case DW_OP_breg26:
2416     case DW_OP_breg27:
2417     case DW_OP_breg28:
2418     case DW_OP_breg29:
2419     case DW_OP_breg30:
2420     case DW_OP_breg31:
2421     case DW_OP_fbreg:
2422       fputc (',', asm_out_file);
2423       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2424       break;
2425
2426     case DW_OP_bregx:
2427       {
2428         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2429         gcc_assert (size_of_uleb128 (r) 
2430                     == size_of_uleb128 (val1->v.val_unsigned));
2431         fputc (',', asm_out_file);
2432         dw2_asm_output_data_uleb128_raw (r);
2433         fputc (',', asm_out_file);
2434         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2435       }
2436       break;
2437
2438     case DW_OP_GNU_implicit_pointer:
2439     case DW_OP_GNU_entry_value:
2440     case DW_OP_GNU_const_type:
2441     case DW_OP_GNU_regval_type:
2442     case DW_OP_GNU_deref_type:
2443     case DW_OP_GNU_convert:
2444     case DW_OP_GNU_reinterpret:
2445     case DW_OP_GNU_parameter_ref:
2446       gcc_unreachable ();
2447       break;
2448
2449     default:
2450       /* Other codes have no operands.  */
2451       break;
2452     }
2453 }
2454
2455 void
2456 output_loc_sequence_raw (dw_loc_descr_ref loc)
2457 {
2458   while (1)
2459     {
2460       enum dwarf_location_atom opc = loc->dw_loc_opc;
2461       /* Output the opcode.  */
2462       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2463         {
2464           unsigned r = (opc - DW_OP_breg0);
2465           r = DWARF2_FRAME_REG_OUT (r, 1);
2466           gcc_assert (r <= 31);
2467           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2468         }
2469       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2470         {
2471           unsigned r = (opc - DW_OP_reg0);
2472           r = DWARF2_FRAME_REG_OUT (r, 1);
2473           gcc_assert (r <= 31);
2474           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2475         }
2476       /* Output the opcode.  */
2477       fprintf (asm_out_file, "%#x", opc);
2478       output_loc_operands_raw (loc);
2479
2480       if (!loc->dw_loc_next)
2481         break;
2482       loc = loc->dw_loc_next;
2483
2484       fputc (',', asm_out_file);
2485     }
2486 }
2487
2488 /* This function builds a dwarf location descriptor sequence from a
2489    dw_cfa_location, adding the given OFFSET to the result of the
2490    expression.  */
2491
2492 struct dw_loc_descr_struct *
2493 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2494 {
2495   struct dw_loc_descr_struct *head, *tmp;
2496
2497   offset += cfa->offset;
2498
2499   if (cfa->indirect)
2500     {
2501       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2502       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2503       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2504       add_loc_descr (&head, tmp);
2505       if (offset != 0)
2506         {
2507           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2508           add_loc_descr (&head, tmp);
2509         }
2510     }
2511   else
2512     head = new_reg_loc_descr (cfa->reg, offset);
2513
2514   return head;
2515 }
2516
2517 /* This function builds a dwarf location descriptor sequence for
2518    the address at OFFSET from the CFA when stack is aligned to
2519    ALIGNMENT byte.  */
2520
2521 struct dw_loc_descr_struct *
2522 build_cfa_aligned_loc (dw_cfa_location *cfa,
2523                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2524 {
2525   struct dw_loc_descr_struct *head;
2526   unsigned int dwarf_fp
2527     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2528
2529   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2530   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2531     {
2532       head = new_reg_loc_descr (dwarf_fp, 0);
2533       add_loc_descr (&head, int_loc_descriptor (alignment));
2534       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2535       loc_descr_plus_const (&head, offset);
2536     }
2537   else
2538     head = new_reg_loc_descr (dwarf_fp, offset);
2539   return head;
2540 }
2541 \f
2542 /* And now, the support for symbolic debugging information.  */
2543
2544 /* .debug_str support.  */
2545 static int output_indirect_string (void **, void *);
2546
2547 static void dwarf2out_init (const char *);
2548 static void dwarf2out_finish (const char *);
2549 static void dwarf2out_assembly_start (void);
2550 static void dwarf2out_define (unsigned int, const char *);
2551 static void dwarf2out_undef (unsigned int, const char *);
2552 static void dwarf2out_start_source_file (unsigned, const char *);
2553 static void dwarf2out_end_source_file (unsigned);
2554 static void dwarf2out_function_decl (tree);
2555 static void dwarf2out_begin_block (unsigned, unsigned);
2556 static void dwarf2out_end_block (unsigned, unsigned);
2557 static bool dwarf2out_ignore_block (const_tree);
2558 static void dwarf2out_global_decl (tree);
2559 static void dwarf2out_type_decl (tree, int);
2560 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2561 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2562                                                  dw_die_ref);
2563 static void dwarf2out_abstract_function (tree);
2564 static void dwarf2out_var_location (rtx);
2565 static void dwarf2out_begin_function (tree);
2566 static void dwarf2out_set_name (tree, tree);
2567
2568 /* The debug hooks structure.  */
2569
2570 const struct gcc_debug_hooks dwarf2_debug_hooks =
2571 {
2572   dwarf2out_init,
2573   dwarf2out_finish,
2574   dwarf2out_assembly_start,
2575   dwarf2out_define,
2576   dwarf2out_undef,
2577   dwarf2out_start_source_file,
2578   dwarf2out_end_source_file,
2579   dwarf2out_begin_block,
2580   dwarf2out_end_block,
2581   dwarf2out_ignore_block,
2582   dwarf2out_source_line,
2583   dwarf2out_begin_prologue,
2584 #if VMS_DEBUGGING_INFO
2585   dwarf2out_vms_end_prologue,
2586   dwarf2out_vms_begin_epilogue,
2587 #else
2588   debug_nothing_int_charstar,
2589   debug_nothing_int_charstar,
2590 #endif
2591   dwarf2out_end_epilogue,
2592   dwarf2out_begin_function,
2593   debug_nothing_int,            /* end_function */
2594   dwarf2out_function_decl,      /* function_decl */
2595   dwarf2out_global_decl,
2596   dwarf2out_type_decl,          /* type_decl */
2597   dwarf2out_imported_module_or_decl,
2598   debug_nothing_tree,           /* deferred_inline_function */
2599   /* The DWARF 2 backend tries to reduce debugging bloat by not
2600      emitting the abstract description of inline functions until
2601      something tries to reference them.  */
2602   dwarf2out_abstract_function,  /* outlining_inline_function */
2603   debug_nothing_rtx,            /* label */
2604   debug_nothing_int,            /* handle_pch */
2605   dwarf2out_var_location,
2606   dwarf2out_switch_text_section,
2607   dwarf2out_set_name,
2608   1,                            /* start_end_main_source_file */
2609   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2610 };
2611 \f
2612 /* NOTE: In the comments in this file, many references are made to
2613    "Debugging Information Entries".  This term is abbreviated as `DIE'
2614    throughout the remainder of this file.  */
2615
2616 /* An internal representation of the DWARF output is built, and then
2617    walked to generate the DWARF debugging info.  The walk of the internal
2618    representation is done after the entire program has been compiled.
2619    The types below are used to describe the internal representation.  */
2620
2621 /* Whether to put type DIEs into their own section .debug_types instead
2622    of making them part of the .debug_info section.  Only supported for
2623    Dwarf V4 or higher and the user didn't disable them through
2624    -fno-debug-types-section.  It is more efficient to put them in a
2625    separate comdat sections since the linker will then be able to
2626    remove duplicates.  But not all tools support .debug_types sections
2627    yet.  */
2628
2629 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2630
2631 /* Various DIE's use offsets relative to the beginning of the
2632    .debug_info section to refer to each other.  */
2633
2634 typedef long int dw_offset;
2635
2636 /* Define typedefs here to avoid circular dependencies.  */
2637
2638 typedef struct dw_attr_struct *dw_attr_ref;
2639 typedef struct dw_line_info_struct *dw_line_info_ref;
2640 typedef struct pubname_struct *pubname_ref;
2641 typedef struct dw_ranges_struct *dw_ranges_ref;
2642 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2643 typedef struct comdat_type_struct *comdat_type_node_ref;
2644
2645 /* The entries in the line_info table more-or-less mirror the opcodes
2646    that are used in the real dwarf line table.  Arrays of these entries
2647    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2648    supported.  */
2649
2650 enum dw_line_info_opcode {
2651   /* Emit DW_LNE_set_address; the operand is the label index.  */
2652   LI_set_address,
2653
2654   /* Emit a row to the matrix with the given line.  This may be done
2655      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2656      special opcodes.  */
2657   LI_set_line,
2658
2659   /* Emit a DW_LNS_set_file.  */
2660   LI_set_file,
2661
2662   /* Emit a DW_LNS_set_column.  */
2663   LI_set_column,
2664
2665   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2666   LI_negate_stmt,
2667
2668   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2669   LI_set_prologue_end,
2670   LI_set_epilogue_begin,
2671
2672   /* Emit a DW_LNE_set_discriminator.  */
2673   LI_set_discriminator
2674 };
2675
2676 typedef struct GTY(()) dw_line_info_struct {
2677   enum dw_line_info_opcode opcode;
2678   unsigned int val;
2679 } dw_line_info_entry;
2680
2681 DEF_VEC_O(dw_line_info_entry);
2682 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2683
2684 typedef struct GTY(()) dw_line_info_table_struct {
2685   /* The label that marks the end of this section.  */
2686   const char *end_label;
2687
2688   /* The values for the last row of the matrix, as collected in the table.
2689      These are used to minimize the changes to the next row.  */
2690   unsigned int file_num;
2691   unsigned int line_num;
2692   unsigned int column_num;
2693   int discrim_num;
2694   bool is_stmt;
2695   bool in_use;
2696
2697   VEC(dw_line_info_entry, gc) *entries;
2698 } dw_line_info_table;
2699
2700 typedef dw_line_info_table *dw_line_info_table_p;
2701
2702 DEF_VEC_P(dw_line_info_table_p);
2703 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2704
2705 /* Each DIE attribute has a field specifying the attribute kind,
2706    a link to the next attribute in the chain, and an attribute value.
2707    Attributes are typically linked below the DIE they modify.  */
2708
2709 typedef struct GTY(()) dw_attr_struct {
2710   enum dwarf_attribute dw_attr;
2711   dw_val_node dw_attr_val;
2712 }
2713 dw_attr_node;
2714
2715 DEF_VEC_O(dw_attr_node);
2716 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2717
2718 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2719    The children of each node form a circular list linked by
2720    die_sib.  die_child points to the node *before* the "first" child node.  */
2721
2722 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2723   union die_symbol_or_type_node
2724     {
2725       char * GTY ((tag ("0"))) die_symbol;
2726       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2727     }
2728   GTY ((desc ("use_debug_types"))) die_id;
2729   VEC(dw_attr_node,gc) * die_attr;
2730   dw_die_ref die_parent;
2731   dw_die_ref die_child;
2732   dw_die_ref die_sib;
2733   dw_die_ref die_definition; /* ref from a specification to its definition */
2734   dw_offset die_offset;
2735   unsigned long die_abbrev;
2736   int die_mark;
2737   /* Die is used and must not be pruned as unused.  */
2738   int die_perennial_p;
2739   unsigned int decl_id;
2740   enum dwarf_tag die_tag;
2741 }
2742 die_node;
2743
2744 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2745 #define FOR_EACH_CHILD(die, c, expr) do {       \
2746   c = die->die_child;                           \
2747   if (c) do {                                   \
2748     c = c->die_sib;                             \
2749     expr;                                       \
2750   } while (c != die->die_child);                \
2751 } while (0)
2752
2753 /* The pubname structure */
2754
2755 typedef struct GTY(()) pubname_struct {
2756   dw_die_ref die;
2757   const char *name;
2758 }
2759 pubname_entry;
2760
2761 DEF_VEC_O(pubname_entry);
2762 DEF_VEC_ALLOC_O(pubname_entry, gc);
2763
2764 struct GTY(()) dw_ranges_struct {
2765   /* If this is positive, it's a block number, otherwise it's a
2766      bitwise-negated index into dw_ranges_by_label.  */
2767   int num;
2768 };
2769
2770 /* A structure to hold a macinfo entry.  */
2771
2772 typedef struct GTY(()) macinfo_struct {
2773   unsigned HOST_WIDE_INT code;
2774   unsigned HOST_WIDE_INT lineno;
2775   const char *info;
2776 }
2777 macinfo_entry;
2778
2779 DEF_VEC_O(macinfo_entry);
2780 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2781
2782 struct GTY(()) dw_ranges_by_label_struct {
2783   const char *begin;
2784   const char *end;
2785 };
2786
2787 /* The comdat type node structure.  */
2788 typedef struct GTY(()) comdat_type_struct
2789 {
2790   dw_die_ref root_die;
2791   dw_die_ref type_die;
2792   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2793   struct comdat_type_struct *next;
2794 }
2795 comdat_type_node;
2796
2797 /* The limbo die list structure.  */
2798 typedef struct GTY(()) limbo_die_struct {
2799   dw_die_ref die;
2800   tree created_for;
2801   struct limbo_die_struct *next;
2802 }
2803 limbo_die_node;
2804
2805 typedef struct skeleton_chain_struct
2806 {
2807   dw_die_ref old_die;
2808   dw_die_ref new_die;
2809   struct skeleton_chain_struct *parent;
2810 }
2811 skeleton_chain_node;
2812
2813 /* Define a macro which returns nonzero for a TYPE_DECL which was
2814    implicitly generated for a type.
2815
2816    Note that, unlike the C front-end (which generates a NULL named
2817    TYPE_DECL node for each complete tagged type, each array type,
2818    and each function type node created) the C++ front-end generates
2819    a _named_ TYPE_DECL node for each tagged type node created.
2820    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2821    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2822    front-end, but for each type, tagged or not.  */
2823
2824 #define TYPE_DECL_IS_STUB(decl)                         \
2825   (DECL_NAME (decl) == NULL_TREE                        \
2826    || (DECL_ARTIFICIAL (decl)                           \
2827        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2828            /* This is necessary for stub decls that     \
2829               appear in nested inline functions.  */    \
2830            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2831                && (decl_ultimate_origin (decl)          \
2832                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2833
2834 /* Information concerning the compilation unit's programming
2835    language, and compiler version.  */
2836
2837 /* Fixed size portion of the DWARF compilation unit header.  */
2838 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2839   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2840
2841 /* Fixed size portion of the DWARF comdat type unit header.  */
2842 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2843   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2844    + DWARF_OFFSET_SIZE)
2845
2846 /* Fixed size portion of public names info.  */
2847 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2848
2849 /* Fixed size portion of the address range info.  */
2850 #define DWARF_ARANGES_HEADER_SIZE                                       \
2851   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2852                 DWARF2_ADDR_SIZE * 2)                                   \
2853    - DWARF_INITIAL_LENGTH_SIZE)
2854
2855 /* Size of padding portion in the address range info.  It must be
2856    aligned to twice the pointer size.  */
2857 #define DWARF_ARANGES_PAD_SIZE \
2858   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2859                 DWARF2_ADDR_SIZE * 2)                              \
2860    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2861
2862 /* Use assembler line directives if available.  */
2863 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2864 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2865 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2866 #else
2867 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2868 #endif
2869 #endif
2870
2871 /* Minimum line offset in a special line info. opcode.
2872    This value was chosen to give a reasonable range of values.  */
2873 #define DWARF_LINE_BASE  -10
2874
2875 /* First special line opcode - leave room for the standard opcodes.  */
2876 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2877
2878 /* Range of line offsets in a special line info. opcode.  */
2879 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2880
2881 /* Flag that indicates the initial value of the is_stmt_start flag.
2882    In the present implementation, we do not mark any lines as
2883    the beginning of a source statement, because that information
2884    is not made available by the GCC front-end.  */
2885 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2886
2887 /* Maximum number of operations per instruction bundle.  */
2888 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2889 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2890 #endif
2891
2892 /* This location is used by calc_die_sizes() to keep track
2893    the offset of each DIE within the .debug_info section.  */
2894 static unsigned long next_die_offset;
2895
2896 /* Record the root of the DIE's built for the current compilation unit.  */
2897 static GTY(()) dw_die_ref single_comp_unit_die;
2898
2899 /* A list of type DIEs that have been separated into comdat sections.  */
2900 static GTY(()) comdat_type_node *comdat_type_list;
2901
2902 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2903 static GTY(()) limbo_die_node *limbo_die_list;
2904
2905 /* A list of DIEs for which we may have to generate
2906    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2907 static GTY(()) limbo_die_node *deferred_asm_name;
2908
2909 /* Filenames referenced by this compilation unit.  */
2910 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2911
2912 /* A hash table of references to DIE's that describe declarations.
2913    The key is a DECL_UID() which is a unique number identifying each decl.  */
2914 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2915
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917    The key is DECL_UID() ^ die_parent.  */
2918 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2919
2920 typedef struct GTY(()) die_arg_entry_struct {
2921     dw_die_ref die;
2922     tree arg;
2923 } die_arg_entry;
2924
2925 DEF_VEC_O(die_arg_entry);
2926 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2927
2928 /* Node of the variable location list.  */
2929 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2930   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2931      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2932      in mode of the EXPR_LIST node and first EXPR_LIST operand
2933      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2934      location or NULL for padding.  For larger bitsizes,
2935      mode is 0 and first operand is a CONCAT with bitsize
2936      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2937      NULL as second operand.  */
2938   rtx GTY (()) loc;
2939   const char * GTY (()) label;
2940   struct var_loc_node * GTY (()) next;
2941 };
2942
2943 /* Variable location list.  */
2944 struct GTY (()) var_loc_list_def {
2945   struct var_loc_node * GTY (()) first;
2946
2947   /* Pointer to the last but one or last element of the
2948      chained list.  If the list is empty, both first and
2949      last are NULL, if the list contains just one node
2950      or the last node certainly is not redundant, it points
2951      to the last node, otherwise points to the last but one.
2952      Do not mark it for GC because it is marked through the chain.  */
2953   struct var_loc_node * GTY ((skip ("%h"))) last;
2954
2955   /* Pointer to the last element before section switch,
2956      if NULL, either sections weren't switched or first
2957      is after section switch.  */
2958   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2959
2960   /* DECL_UID of the variable decl.  */
2961   unsigned int decl_id;
2962 };
2963 typedef struct var_loc_list_def var_loc_list;
2964
2965 /* Call argument location list.  */
2966 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2967   rtx GTY (()) call_arg_loc_note;
2968   const char * GTY (()) label;
2969   tree GTY (()) block;
2970   bool tail_call_p;
2971   rtx GTY (()) symbol_ref;
2972   struct call_arg_loc_node * GTY (()) next;
2973 };
2974
2975
2976 /* Table of decl location linked lists.  */
2977 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2978
2979 /* Head and tail of call_arg_loc chain.  */
2980 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2981 static struct call_arg_loc_node *call_arg_loc_last;
2982
2983 /* Number of call sites in the current function.  */
2984 static int call_site_count = -1;
2985 /* Number of tail call sites in the current function.  */
2986 static int tail_call_site_count = -1;
2987
2988 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2989    DIEs.  */
2990 static VEC (dw_die_ref, heap) *block_map;
2991
2992 /* A cached location list.  */
2993 struct GTY (()) cached_dw_loc_list_def {
2994   /* The DECL_UID of the decl that this entry describes.  */
2995   unsigned int decl_id;
2996
2997   /* The cached location list.  */
2998   dw_loc_list_ref loc_list;
2999 };
3000 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3001
3002 /* Table of cached location lists.  */
3003 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3004
3005 /* A pointer to the base of a list of references to DIE's that
3006    are uniquely identified by their tag, presence/absence of
3007    children DIE's, and list of attribute/value pairs.  */
3008 static GTY((length ("abbrev_die_table_allocated")))
3009   dw_die_ref *abbrev_die_table;
3010
3011 /* Number of elements currently allocated for abbrev_die_table.  */
3012 static GTY(()) unsigned abbrev_die_table_allocated;
3013
3014 /* Number of elements in type_die_table currently in use.  */
3015 static GTY(()) unsigned abbrev_die_table_in_use;
3016
3017 /* Size (in elements) of increments by which we may expand the
3018    abbrev_die_table.  */
3019 #define ABBREV_DIE_TABLE_INCREMENT 256
3020
3021 /* A global counter for generating labels for line number data.  */
3022 static unsigned int line_info_label_num;
3023
3024 /* The current table to which we should emit line number information
3025    for the current function.  This will be set up at the beginning of
3026    assembly for the function.  */
3027 static dw_line_info_table *cur_line_info_table;
3028
3029 /* The two default tables of line number info.  */
3030 static GTY(()) dw_line_info_table *text_section_line_info;
3031 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3032
3033 /* The set of all non-default tables of line number info.  */
3034 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3035
3036 /* A flag to tell pubnames/types export if there is an info section to
3037    refer to.  */
3038 static bool info_section_emitted;
3039
3040 /* A pointer to the base of a table that contains a list of publicly
3041    accessible names.  */
3042 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3043
3044 /* A pointer to the base of a table that contains a list of publicly
3045    accessible types.  */
3046 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3047
3048 /* A pointer to the base of a table that contains a list of macro
3049    defines/undefines (and file start/end markers).  */
3050 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3051
3052 /* Array of dies for which we should generate .debug_ranges info.  */
3053 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3054
3055 /* Number of elements currently allocated for ranges_table.  */
3056 static GTY(()) unsigned ranges_table_allocated;
3057
3058 /* Number of elements in ranges_table currently in use.  */
3059 static GTY(()) unsigned ranges_table_in_use;
3060
3061 /* Array of pairs of labels referenced in ranges_table.  */
3062 static GTY ((length ("ranges_by_label_allocated")))
3063      dw_ranges_by_label_ref ranges_by_label;
3064
3065 /* Number of elements currently allocated for ranges_by_label.  */
3066 static GTY(()) unsigned ranges_by_label_allocated;
3067
3068 /* Number of elements in ranges_by_label currently in use.  */
3069 static GTY(()) unsigned ranges_by_label_in_use;
3070
3071 /* Size (in elements) of increments by which we may expand the
3072    ranges_table.  */
3073 #define RANGES_TABLE_INCREMENT 64
3074
3075 /* Whether we have location lists that need outputting */
3076 static GTY(()) bool have_location_lists;
3077
3078 /* Unique label counter.  */
3079 static GTY(()) unsigned int loclabel_num;
3080
3081 /* Unique label counter for point-of-call tables.  */
3082 static GTY(()) unsigned int poc_label_num;
3083
3084 /* Record whether the function being analyzed contains inlined functions.  */
3085 static int current_function_has_inlines;
3086
3087 /* The last file entry emitted by maybe_emit_file().  */
3088 static GTY(()) struct dwarf_file_data * last_emitted_file;
3089
3090 /* Number of internal labels generated by gen_internal_sym().  */
3091 static GTY(()) int label_num;
3092
3093 /* Cached result of previous call to lookup_filename.  */
3094 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3095
3096 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3097
3098 /* Instances of generic types for which we need to generate debug
3099    info that describe their generic parameters and arguments. That
3100    generation needs to happen once all types are properly laid out so
3101    we do it at the end of compilation.  */
3102 static GTY(()) VEC(tree,gc) *generic_type_instances;
3103
3104 /* Offset from the "steady-state frame pointer" to the frame base,
3105    within the current function.  */
3106 static HOST_WIDE_INT frame_pointer_fb_offset;
3107 static bool frame_pointer_fb_offset_valid;
3108
3109 static VEC (dw_die_ref, heap) *base_types;
3110
3111 /* Forward declarations for functions defined in this file.  */
3112
3113 static int is_pseudo_reg (const_rtx);
3114 static tree type_main_variant (tree);
3115 static int is_tagged_type (const_tree);
3116 static const char *dwarf_tag_name (unsigned);
3117 static const char *dwarf_attr_name (unsigned);
3118 static const char *dwarf_form_name (unsigned);
3119 static tree decl_ultimate_origin (const_tree);
3120 static tree decl_class_context (tree);
3121 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3122 static inline enum dw_val_class AT_class (dw_attr_ref);
3123 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3124 static inline unsigned AT_flag (dw_attr_ref);
3125 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3126 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3127 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3128 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3129 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3130                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3131 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3132                                unsigned int, unsigned char *);
3133 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3134 static hashval_t debug_str_do_hash (const void *);
3135 static int debug_str_eq (const void *, const void *);
3136 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3137 static inline const char *AT_string (dw_attr_ref);
3138 static enum dwarf_form AT_string_form (dw_attr_ref);
3139 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3140 static void add_AT_specification (dw_die_ref, dw_die_ref);
3141 static inline dw_die_ref AT_ref (dw_attr_ref);
3142 static inline int AT_ref_external (dw_attr_ref);
3143 static inline void set_AT_ref_external (dw_attr_ref, int);
3144 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3145 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3146 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3147 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3148                              dw_loc_list_ref);
3149 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3150 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3151 static inline rtx AT_addr (dw_attr_ref);
3152 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3153 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3154 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3155 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3156                            unsigned HOST_WIDE_INT);
3157 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3158                                unsigned long);
3159 static inline const char *AT_lbl (dw_attr_ref);
3160 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3161 static const char *get_AT_low_pc (dw_die_ref);
3162 static const char *get_AT_hi_pc (dw_die_ref);
3163 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3164 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3165 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3166 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3167 static bool is_cxx (void);
3168 static bool is_fortran (void);
3169 static bool is_ada (void);
3170 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3171 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3172 static void add_child_die (dw_die_ref, dw_die_ref);
3173 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3174 static dw_die_ref lookup_type_die (tree);
3175 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3176 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3177 static void equate_type_number_to_die (tree, dw_die_ref);
3178 static hashval_t decl_die_table_hash (const void *);
3179 static int decl_die_table_eq (const void *, const void *);
3180 static dw_die_ref lookup_decl_die (tree);
3181 static hashval_t common_block_die_table_hash (const void *);
3182 static int common_block_die_table_eq (const void *, const void *);
3183 static hashval_t decl_loc_table_hash (const void *);
3184 static int decl_loc_table_eq (const void *, const void *);
3185 static var_loc_list *lookup_decl_loc (const_tree);
3186 static void equate_decl_number_to_die (tree, dw_die_ref);
3187 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3188 static void print_spaces (FILE *);
3189 static void print_die (dw_die_ref, FILE *);
3190 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3191 static dw_die_ref pop_compile_unit (dw_die_ref);
3192 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3193 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3194 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3195 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3196 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3197 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3198 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3199                                    struct md5_ctx *, int *);
3200 struct checksum_attributes;
3201 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3202 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3203 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3204 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3205 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3206 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3207 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3208 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3209 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3210 static void compute_section_prefix (dw_die_ref);
3211 static int is_type_die (dw_die_ref);
3212 static int is_comdat_die (dw_die_ref);
3213 static int is_symbol_die (dw_die_ref);
3214 static void assign_symbol_names (dw_die_ref);
3215 static void break_out_includes (dw_die_ref);
3216 static int is_declaration_die (dw_die_ref);
3217 static int should_move_die_to_comdat (dw_die_ref);
3218 static dw_die_ref clone_as_declaration (dw_die_ref);
3219 static dw_die_ref clone_die (dw_die_ref);
3220 static dw_die_ref clone_tree (dw_die_ref);
3221 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3222 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3223 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3224 static dw_die_ref generate_skeleton (dw_die_ref);
3225 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3226                                                          dw_die_ref);
3227 static void break_out_comdat_types (dw_die_ref);
3228 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3229 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3230 static void copy_decls_for_unworthy_types (dw_die_ref);
3231
3232 static hashval_t htab_cu_hash (const void *);
3233 static int htab_cu_eq (const void *, const void *);
3234 static void htab_cu_del (void *);
3235 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3236 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3237 static void add_sibling_attributes (dw_die_ref);
3238 static void build_abbrev_table (dw_die_ref);
3239 static void output_location_lists (dw_die_ref);
3240 static int constant_size (unsigned HOST_WIDE_INT);
3241 static unsigned long size_of_die (dw_die_ref);
3242 static void calc_die_sizes (dw_die_ref);
3243 static void calc_base_type_die_sizes (void);
3244 static void mark_dies (dw_die_ref);
3245 static void unmark_dies (dw_die_ref);
3246 static void unmark_all_dies (dw_die_ref);
3247 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3248 static unsigned long size_of_aranges (void);
3249 static enum dwarf_form value_format (dw_attr_ref);
3250 static void output_value_format (dw_attr_ref);
3251 static void output_abbrev_section (void);
3252 static void output_die_symbol (dw_die_ref);
3253 static void output_die (dw_die_ref);
3254 static void output_compilation_unit_header (void);
3255 static void output_comp_unit (dw_die_ref, int);
3256 static void output_comdat_type_unit (comdat_type_node *);
3257 static const char *dwarf2_name (tree, int);
3258 static void add_pubname (tree, dw_die_ref);
3259 static void add_pubname_string (const char *, dw_die_ref);
3260 static void add_pubtype (tree, dw_die_ref);
3261 static void output_pubnames (VEC (pubname_entry,gc) *);
3262 static void output_aranges (unsigned long);
3263 static unsigned int add_ranges_num (int);
3264 static unsigned int add_ranges (const_tree);
3265 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3266                                   bool *);
3267 static void output_ranges (void);
3268 static dw_line_info_table *new_line_info_table (void);
3269 static void output_line_info (void);
3270 static void output_file_names (void);
3271 static dw_die_ref base_type_die (tree);
3272 static int is_base_type (tree);
3273 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3274 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3275 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3276 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3277 static int type_is_enum (const_tree);
3278 static unsigned int dbx_reg_number (const_rtx);
3279 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3280 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3281 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3282                                                 enum var_init_status);
3283 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3284                                                      enum var_init_status);
3285 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3286                                          enum var_init_status);
3287 static int is_based_loc (const_rtx);
3288 static int resolve_one_addr (rtx *, void *);
3289 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3290                                                enum var_init_status);
3291 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3292                                         enum var_init_status);
3293 static dw_loc_list_ref loc_list_from_tree (tree, int);
3294 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3295 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3296 static tree field_type (const_tree);
3297 static unsigned int simple_type_align_in_bits (const_tree);
3298 static unsigned int simple_decl_align_in_bits (const_tree);
3299 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3300 static HOST_WIDE_INT field_byte_offset (const_tree);
3301 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3302                                          dw_loc_list_ref);
3303 static void add_data_member_location_attribute (dw_die_ref, tree);
3304 static bool add_const_value_attribute (dw_die_ref, rtx);
3305 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3306 static void insert_double (double_int, unsigned char *);
3307 static void insert_float (const_rtx, unsigned char *);
3308 static rtx rtl_for_decl_location (tree);
3309 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3310                                                    enum dwarf_attribute);
3311 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3312 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3313 static void add_name_attribute (dw_die_ref, const char *);
3314 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3315 static void add_comp_dir_attribute (dw_die_ref);
3316 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3317 static void add_subscript_info (dw_die_ref, tree, bool);
3318 static void add_byte_size_attribute (dw_die_ref, tree);
3319 static void add_bit_offset_attribute (dw_die_ref, tree);
3320 static void add_bit_size_attribute (dw_die_ref, tree);
3321 static void add_prototyped_attribute (dw_die_ref, tree);
3322 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3323 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3324 static void add_src_coords_attributes (dw_die_ref, tree);
3325 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3326 static void push_decl_scope (tree);
3327 static void pop_decl_scope (void);
3328 static dw_die_ref scope_die_for (tree, dw_die_ref);
3329 static inline int local_scope_p (dw_die_ref);
3330 static inline int class_scope_p (dw_die_ref);
3331 static inline int class_or_namespace_scope_p (dw_die_ref);
3332 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3333 static void add_calling_convention_attribute (dw_die_ref, tree);
3334 static const char *type_tag (const_tree);
3335 static tree member_declared_type (const_tree);
3336 #if 0
3337 static const char *decl_start_label (tree);
3338 #endif
3339 static void gen_array_type_die (tree, dw_die_ref);
3340 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3341 #if 0
3342 static void gen_entry_point_die (tree, dw_die_ref);
3343 #endif
3344 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3345 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3346 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3347 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3348 static void gen_formal_types_die (tree, dw_die_ref);
3349 static void gen_subprogram_die (tree, dw_die_ref);
3350 static void gen_variable_die (tree, tree, dw_die_ref);
3351 static void gen_const_die (tree, dw_die_ref);
3352 static void gen_label_die (tree, dw_die_ref);
3353 static void gen_lexical_block_die (tree, dw_die_ref, int);
3354 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3355 static void gen_field_die (tree, dw_die_ref);
3356 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3357 static dw_die_ref gen_compile_unit_die (const char *);
3358 static void gen_inheritance_die (tree, tree, dw_die_ref);
3359 static void gen_member_die (tree, dw_die_ref);
3360 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3361                                                 enum debug_info_usage);
3362 static void gen_subroutine_type_die (tree, dw_die_ref);
3363 static void gen_typedef_die (tree, dw_die_ref);
3364 static void gen_type_die (tree, dw_die_ref);
3365 static void gen_block_die (tree, dw_die_ref, int);
3366 static void decls_for_scope (tree, dw_die_ref, int);
3367 static inline int is_redundant_typedef (const_tree);
3368 static bool is_naming_typedef_decl (const_tree);
3369 static inline dw_die_ref get_context_die (tree);
3370 static void gen_namespace_die (tree, dw_die_ref);
3371 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3372 static dw_die_ref force_decl_die (tree);
3373 static dw_die_ref force_type_die (tree);
3374 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3375 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3376 static struct dwarf_file_data * lookup_filename (const char *);
3377 static void retry_incomplete_types (void);
3378 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3379 static void gen_generic_params_dies (tree);
3380 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3381 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3382 static void splice_child_die (dw_die_ref, dw_die_ref);
3383 static int file_info_cmp (const void *, const void *);
3384 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3385                                      const char *, const char *);
3386 static void output_loc_list (dw_loc_list_ref);
3387 static char *gen_internal_sym (const char *);
3388
3389 static void prune_unmark_dies (dw_die_ref);
3390 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3391 static void prune_unused_types_mark (dw_die_ref, int);
3392 static void prune_unused_types_walk (dw_die_ref);
3393 static void prune_unused_types_walk_attribs (dw_die_ref);
3394 static void prune_unused_types_prune (dw_die_ref);
3395 static void prune_unused_types (void);
3396 static int maybe_emit_file (struct dwarf_file_data *fd);
3397 static inline const char *AT_vms_delta1 (dw_attr_ref);
3398 static inline const char *AT_vms_delta2 (dw_attr_ref);
3399 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3400                                      const char *, const char *);
3401 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3402 static void gen_remaining_tmpl_value_param_die_attribute (void);
3403 static bool generic_type_p (tree);
3404 static void schedule_generic_params_dies_gen (tree t);
3405 static void gen_scheduled_generic_parms_dies (void);
3406
3407 /* Section names used to hold DWARF debugging information.  */
3408 #ifndef DEBUG_INFO_SECTION
3409 #define DEBUG_INFO_SECTION      ".debug_info"
3410 #endif
3411 #ifndef DEBUG_ABBREV_SECTION
3412 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3413 #endif
3414 #ifndef DEBUG_ARANGES_SECTION
3415 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3416 #endif
3417 #ifndef DEBUG_MACINFO_SECTION
3418 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3419 #endif
3420 #ifndef DEBUG_LINE_SECTION
3421 #define DEBUG_LINE_SECTION      ".debug_line"
3422 #endif
3423 #ifndef DEBUG_LOC_SECTION
3424 #define DEBUG_LOC_SECTION       ".debug_loc"
3425 #endif
3426 #ifndef DEBUG_PUBNAMES_SECTION
3427 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3428 #endif
3429 #ifndef DEBUG_PUBTYPES_SECTION
3430 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3431 #endif
3432 #ifndef DEBUG_STR_SECTION
3433 #define DEBUG_STR_SECTION       ".debug_str"
3434 #endif
3435 #ifndef DEBUG_RANGES_SECTION
3436 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3437 #endif
3438
3439 /* Standard ELF section names for compiled code and data.  */
3440 #ifndef TEXT_SECTION_NAME
3441 #define TEXT_SECTION_NAME       ".text"
3442 #endif
3443
3444 /* Section flags for .debug_str section.  */
3445 #define DEBUG_STR_SECTION_FLAGS \
3446   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3447    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3448    : SECTION_DEBUG)
3449
3450 /* Labels we insert at beginning sections we can reference instead of
3451    the section names themselves.  */
3452
3453 #ifndef TEXT_SECTION_LABEL
3454 #define TEXT_SECTION_LABEL              "Ltext"
3455 #endif
3456 #ifndef COLD_TEXT_SECTION_LABEL
3457 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3458 #endif
3459 #ifndef DEBUG_LINE_SECTION_LABEL
3460 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3461 #endif
3462 #ifndef DEBUG_INFO_SECTION_LABEL
3463 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3464 #endif
3465 #ifndef DEBUG_ABBREV_SECTION_LABEL
3466 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3467 #endif
3468 #ifndef DEBUG_LOC_SECTION_LABEL
3469 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3470 #endif
3471 #ifndef DEBUG_RANGES_SECTION_LABEL
3472 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3473 #endif
3474 #ifndef DEBUG_MACINFO_SECTION_LABEL
3475 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3476 #endif
3477
3478
3479 /* Definitions of defaults for formats and names of various special
3480    (artificial) labels which may be generated within this file (when the -g
3481    options is used and DWARF2_DEBUGGING_INFO is in effect.
3482    If necessary, these may be overridden from within the tm.h file, but
3483    typically, overriding these defaults is unnecessary.  */
3484
3485 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3495
3496 #ifndef TEXT_END_LABEL
3497 #define TEXT_END_LABEL          "Letext"
3498 #endif
3499 #ifndef COLD_END_LABEL
3500 #define COLD_END_LABEL          "Letext_cold"
3501 #endif
3502 #ifndef BLOCK_BEGIN_LABEL
3503 #define BLOCK_BEGIN_LABEL       "LBB"
3504 #endif
3505 #ifndef BLOCK_END_LABEL
3506 #define BLOCK_END_LABEL         "LBE"
3507 #endif
3508 #ifndef LINE_CODE_LABEL
3509 #define LINE_CODE_LABEL         "LM"
3510 #endif
3511
3512 \f
3513 /* Return the root of the DIE's built for the current compilation unit.  */
3514 static dw_die_ref
3515 comp_unit_die (void)
3516 {
3517   if (!single_comp_unit_die)
3518     single_comp_unit_die = gen_compile_unit_die (NULL);
3519   return single_comp_unit_die;
3520 }
3521
3522 /* We allow a language front-end to designate a function that is to be
3523    called to "demangle" any name before it is put into a DIE.  */
3524
3525 static const char *(*demangle_name_func) (const char *);
3526
3527 void
3528 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3529 {
3530   demangle_name_func = func;
3531 }
3532
3533 /* Test if rtl node points to a pseudo register.  */
3534
3535 static inline int
3536 is_pseudo_reg (const_rtx rtl)
3537 {
3538   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3539           || (GET_CODE (rtl) == SUBREG
3540               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3541 }
3542
3543 /* Return a reference to a type, with its const and volatile qualifiers
3544    removed.  */
3545
3546 static inline tree
3547 type_main_variant (tree type)
3548 {
3549   type = TYPE_MAIN_VARIANT (type);
3550
3551   /* ??? There really should be only one main variant among any group of
3552      variants of a given type (and all of the MAIN_VARIANT values for all
3553      members of the group should point to that one type) but sometimes the C
3554      front-end messes this up for array types, so we work around that bug
3555      here.  */
3556   if (TREE_CODE (type) == ARRAY_TYPE)
3557     while (type != TYPE_MAIN_VARIANT (type))
3558       type = TYPE_MAIN_VARIANT (type);
3559
3560   return type;
3561 }
3562
3563 /* Return nonzero if the given type node represents a tagged type.  */
3564
3565 static inline int
3566 is_tagged_type (const_tree type)
3567 {
3568   enum tree_code code = TREE_CODE (type);
3569
3570   return (code == RECORD_TYPE || code == UNION_TYPE
3571           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3572 }
3573
3574 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3575
3576 static void
3577 get_ref_die_offset_label (char *label, dw_die_ref ref)
3578 {
3579   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3580 }
3581
3582 /* Return die_offset of a DIE reference to a base type.  */
3583
3584 static unsigned long int
3585 get_base_type_offset (dw_die_ref ref)
3586 {
3587   if (ref->die_offset)
3588     return ref->die_offset;
3589   if (comp_unit_die ()->die_abbrev)
3590     {
3591       calc_base_type_die_sizes ();
3592       gcc_assert (ref->die_offset);
3593     }
3594   return ref->die_offset;
3595 }
3596
3597 /* Return die_offset of a DIE reference other than base type.  */
3598
3599 static unsigned long int
3600 get_ref_die_offset (dw_die_ref ref)
3601 {
3602   gcc_assert (ref->die_offset);
3603   return ref->die_offset;
3604 }
3605
3606 /* Convert a DIE tag into its string name.  */
3607
3608 static const char *
3609 dwarf_tag_name (unsigned int tag)
3610 {
3611   switch (tag)
3612     {
3613     case DW_TAG_padding:
3614       return "DW_TAG_padding";
3615     case DW_TAG_array_type:
3616       return "DW_TAG_array_type";
3617     case DW_TAG_class_type:
3618       return "DW_TAG_class_type";
3619     case DW_TAG_entry_point:
3620       return "DW_TAG_entry_point";
3621     case DW_TAG_enumeration_type:
3622       return "DW_TAG_enumeration_type";
3623     case DW_TAG_formal_parameter:
3624       return "DW_TAG_formal_parameter";
3625     case DW_TAG_imported_declaration:
3626       return "DW_TAG_imported_declaration";
3627     case DW_TAG_label:
3628       return "DW_TAG_label";
3629     case DW_TAG_lexical_block:
3630       return "DW_TAG_lexical_block";
3631     case DW_TAG_member:
3632       return "DW_TAG_member";
3633     case DW_TAG_pointer_type:
3634       return "DW_TAG_pointer_type";
3635     case DW_TAG_reference_type:
3636       return "DW_TAG_reference_type";
3637     case DW_TAG_compile_unit:
3638       return "DW_TAG_compile_unit";
3639     case DW_TAG_string_type:
3640       return "DW_TAG_string_type";
3641     case DW_TAG_structure_type:
3642       return "DW_TAG_structure_type";
3643     case DW_TAG_subroutine_type:
3644       return "DW_TAG_subroutine_type";
3645     case DW_TAG_typedef:
3646       return "DW_TAG_typedef";
3647     case DW_TAG_union_type:
3648       return "DW_TAG_union_type";
3649     case DW_TAG_unspecified_parameters:
3650       return "DW_TAG_unspecified_parameters";
3651     case DW_TAG_variant:
3652       return "DW_TAG_variant";
3653     case DW_TAG_common_block:
3654       return "DW_TAG_common_block";
3655     case DW_TAG_common_inclusion:
3656       return "DW_TAG_common_inclusion";
3657     case DW_TAG_inheritance:
3658       return "DW_TAG_inheritance";
3659     case DW_TAG_inlined_subroutine:
3660       return "DW_TAG_inlined_subroutine";
3661     case DW_TAG_module:
3662       return "DW_TAG_module";
3663     case DW_TAG_ptr_to_member_type:
3664       return "DW_TAG_ptr_to_member_type";
3665     case DW_TAG_set_type:
3666       return "DW_TAG_set_type";
3667     case DW_TAG_subrange_type:
3668       return "DW_TAG_subrange_type";
3669     case DW_TAG_with_stmt:
3670       return "DW_TAG_with_stmt";
3671     case DW_TAG_access_declaration:
3672       return "DW_TAG_access_declaration";
3673     case DW_TAG_base_type:
3674       return "DW_TAG_base_type";
3675     case DW_TAG_catch_block:
3676       return "DW_TAG_catch_block";
3677     case DW_TAG_const_type:
3678       return "DW_TAG_const_type";
3679     case DW_TAG_constant:
3680       return "DW_TAG_constant";
3681     case DW_TAG_enumerator:
3682       return "DW_TAG_enumerator";
3683     case DW_TAG_file_type:
3684       return "DW_TAG_file_type";
3685     case DW_TAG_friend:
3686       return "DW_TAG_friend";
3687     case DW_TAG_namelist:
3688       return "DW_TAG_namelist";
3689     case DW_TAG_namelist_item:
3690       return "DW_TAG_namelist_item";
3691     case DW_TAG_packed_type:
3692       return "DW_TAG_packed_type";
3693     case DW_TAG_subprogram:
3694       return "DW_TAG_subprogram";
3695     case DW_TAG_template_type_param:
3696       return "DW_TAG_template_type_param";
3697     case DW_TAG_template_value_param:
3698       return "DW_TAG_template_value_param";
3699     case DW_TAG_thrown_type:
3700       return "DW_TAG_thrown_type";
3701     case DW_TAG_try_block:
3702       return "DW_TAG_try_block";
3703     case DW_TAG_variant_part:
3704       return "DW_TAG_variant_part";
3705     case DW_TAG_variable:
3706       return "DW_TAG_variable";
3707     case DW_TAG_volatile_type:
3708       return "DW_TAG_volatile_type";
3709     case DW_TAG_dwarf_procedure:
3710       return "DW_TAG_dwarf_procedure";
3711     case DW_TAG_restrict_type:
3712       return "DW_TAG_restrict_type";
3713     case DW_TAG_interface_type:
3714       return "DW_TAG_interface_type";
3715     case DW_TAG_namespace:
3716       return "DW_TAG_namespace";
3717     case DW_TAG_imported_module:
3718       return "DW_TAG_imported_module";
3719     case DW_TAG_unspecified_type:
3720       return "DW_TAG_unspecified_type";
3721     case DW_TAG_partial_unit:
3722       return "DW_TAG_partial_unit";
3723     case DW_TAG_imported_unit:
3724       return "DW_TAG_imported_unit";
3725     case DW_TAG_condition:
3726       return "DW_TAG_condition";
3727     case DW_TAG_shared_type:
3728       return "DW_TAG_shared_type";
3729     case DW_TAG_type_unit:
3730       return "DW_TAG_type_unit";
3731     case DW_TAG_rvalue_reference_type:
3732       return "DW_TAG_rvalue_reference_type";
3733     case DW_TAG_template_alias:
3734       return "DW_TAG_template_alias";
3735     case DW_TAG_GNU_template_parameter_pack:
3736       return "DW_TAG_GNU_template_parameter_pack";
3737     case DW_TAG_GNU_formal_parameter_pack:
3738       return "DW_TAG_GNU_formal_parameter_pack";
3739     case DW_TAG_MIPS_loop:
3740       return "DW_TAG_MIPS_loop";
3741     case DW_TAG_format_label:
3742       return "DW_TAG_format_label";
3743     case DW_TAG_function_template:
3744       return "DW_TAG_function_template";
3745     case DW_TAG_class_template:
3746       return "DW_TAG_class_template";
3747     case DW_TAG_GNU_BINCL:
3748       return "DW_TAG_GNU_BINCL";
3749     case DW_TAG_GNU_EINCL:
3750       return "DW_TAG_GNU_EINCL";
3751     case DW_TAG_GNU_template_template_param:
3752       return "DW_TAG_GNU_template_template_param";
3753     case DW_TAG_GNU_call_site:
3754       return "DW_TAG_GNU_call_site";
3755     case DW_TAG_GNU_call_site_parameter:
3756       return "DW_TAG_GNU_call_site_parameter";
3757     default:
3758       return "DW_TAG_<unknown>";
3759     }
3760 }
3761
3762 /* Convert a DWARF attribute code into its string name.  */
3763
3764 static const char *
3765 dwarf_attr_name (unsigned int attr)
3766 {
3767   switch (attr)
3768     {
3769     case DW_AT_sibling:
3770       return "DW_AT_sibling";
3771     case DW_AT_location:
3772       return "DW_AT_location";
3773     case DW_AT_name:
3774       return "DW_AT_name";
3775     case DW_AT_ordering:
3776       return "DW_AT_ordering";
3777     case DW_AT_subscr_data:
3778       return "DW_AT_subscr_data";
3779     case DW_AT_byte_size:
3780       return "DW_AT_byte_size";
3781     case DW_AT_bit_offset:
3782       return "DW_AT_bit_offset";
3783     case DW_AT_bit_size:
3784       return "DW_AT_bit_size";
3785     case DW_AT_element_list:
3786       return "DW_AT_element_list";
3787     case DW_AT_stmt_list:
3788       return "DW_AT_stmt_list";
3789     case DW_AT_low_pc:
3790       return "DW_AT_low_pc";
3791     case DW_AT_high_pc:
3792       return "DW_AT_high_pc";
3793     case DW_AT_language:
3794       return "DW_AT_language";
3795     case DW_AT_member:
3796       return "DW_AT_member";
3797     case DW_AT_discr:
3798       return "DW_AT_discr";
3799     case DW_AT_discr_value:
3800       return "DW_AT_discr_value";
3801     case DW_AT_visibility:
3802       return "DW_AT_visibility";
3803     case DW_AT_import:
3804       return "DW_AT_import";
3805     case DW_AT_string_length:
3806       return "DW_AT_string_length";
3807     case DW_AT_common_reference:
3808       return "DW_AT_common_reference";
3809     case DW_AT_comp_dir:
3810       return "DW_AT_comp_dir";
3811     case DW_AT_const_value:
3812       return "DW_AT_const_value";
3813     case DW_AT_containing_type:
3814       return "DW_AT_containing_type";
3815     case DW_AT_default_value:
3816       return "DW_AT_default_value";
3817     case DW_AT_inline:
3818       return "DW_AT_inline";
3819     case DW_AT_is_optional:
3820       return "DW_AT_is_optional";
3821     case DW_AT_lower_bound:
3822       return "DW_AT_lower_bound";
3823     case DW_AT_producer:
3824       return "DW_AT_producer";
3825     case DW_AT_prototyped:
3826       return "DW_AT_prototyped";
3827     case DW_AT_return_addr:
3828       return "DW_AT_return_addr";
3829     case DW_AT_start_scope:
3830       return "DW_AT_start_scope";
3831     case DW_AT_bit_stride:
3832       return "DW_AT_bit_stride";
3833     case DW_AT_upper_bound:
3834       return "DW_AT_upper_bound";
3835     case DW_AT_abstract_origin:
3836       return "DW_AT_abstract_origin";
3837     case DW_AT_accessibility:
3838       return "DW_AT_accessibility";
3839     case DW_AT_address_class:
3840       return "DW_AT_address_class";
3841     case DW_AT_artificial:
3842       return "DW_AT_artificial";
3843     case DW_AT_base_types:
3844       return "DW_AT_base_types";
3845     case DW_AT_calling_convention:
3846       return "DW_AT_calling_convention";
3847     case DW_AT_count:
3848       return "DW_AT_count";
3849     case DW_AT_data_member_location:
3850       return "DW_AT_data_member_location";
3851     case DW_AT_decl_column:
3852       return "DW_AT_decl_column";
3853     case DW_AT_decl_file:
3854       return "DW_AT_decl_file";
3855     case DW_AT_decl_line:
3856       return "DW_AT_decl_line";
3857     case DW_AT_declaration:
3858       return "DW_AT_declaration";
3859     case DW_AT_discr_list:
3860       return "DW_AT_discr_list";
3861     case DW_AT_encoding:
3862       return "DW_AT_encoding";
3863     case DW_AT_external:
3864       return "DW_AT_external";
3865     case DW_AT_explicit:
3866       return "DW_AT_explicit";
3867     case DW_AT_frame_base:
3868       return "DW_AT_frame_base";
3869     case DW_AT_friend:
3870       return "DW_AT_friend";
3871     case DW_AT_identifier_case:
3872       return "DW_AT_identifier_case";
3873     case DW_AT_macro_info:
3874       return "DW_AT_macro_info";
3875     case DW_AT_namelist_items:
3876       return "DW_AT_namelist_items";
3877     case DW_AT_priority:
3878       return "DW_AT_priority";
3879     case DW_AT_segment:
3880       return "DW_AT_segment";
3881     case DW_AT_specification:
3882       return "DW_AT_specification";
3883     case DW_AT_static_link:
3884       return "DW_AT_static_link";
3885     case DW_AT_type:
3886       return "DW_AT_type";
3887     case DW_AT_use_location:
3888       return "DW_AT_use_location";
3889     case DW_AT_variable_parameter:
3890       return "DW_AT_variable_parameter";
3891     case DW_AT_virtuality:
3892       return "DW_AT_virtuality";
3893     case DW_AT_vtable_elem_location:
3894       return "DW_AT_vtable_elem_location";
3895
3896     case DW_AT_allocated:
3897       return "DW_AT_allocated";
3898     case DW_AT_associated:
3899       return "DW_AT_associated";
3900     case DW_AT_data_location:
3901       return "DW_AT_data_location";
3902     case DW_AT_byte_stride:
3903       return "DW_AT_byte_stride";
3904     case DW_AT_entry_pc:
3905       return "DW_AT_entry_pc";
3906     case DW_AT_use_UTF8:
3907       return "DW_AT_use_UTF8";
3908     case DW_AT_extension:
3909       return "DW_AT_extension";
3910     case DW_AT_ranges:
3911       return "DW_AT_ranges";
3912     case DW_AT_trampoline:
3913       return "DW_AT_trampoline";
3914     case DW_AT_call_column:
3915       return "DW_AT_call_column";
3916     case DW_AT_call_file:
3917       return "DW_AT_call_file";
3918     case DW_AT_call_line:
3919       return "DW_AT_call_line";
3920     case DW_AT_object_pointer:
3921       return "DW_AT_object_pointer";
3922
3923     case DW_AT_signature:
3924       return "DW_AT_signature";
3925     case DW_AT_main_subprogram:
3926       return "DW_AT_main_subprogram";
3927     case DW_AT_data_bit_offset:
3928       return "DW_AT_data_bit_offset";
3929     case DW_AT_const_expr:
3930       return "DW_AT_const_expr";
3931     case DW_AT_enum_class:
3932       return "DW_AT_enum_class";
3933     case DW_AT_linkage_name:
3934       return "DW_AT_linkage_name";
3935
3936     case DW_AT_MIPS_fde:
3937       return "DW_AT_MIPS_fde";
3938     case DW_AT_MIPS_loop_begin:
3939       return "DW_AT_MIPS_loop_begin";
3940     case DW_AT_MIPS_tail_loop_begin:
3941       return "DW_AT_MIPS_tail_loop_begin";
3942     case DW_AT_MIPS_epilog_begin:
3943       return "DW_AT_MIPS_epilog_begin";
3944 #if VMS_DEBUGGING_INFO
3945     case DW_AT_HP_prologue:
3946       return "DW_AT_HP_prologue";
3947 #else
3948     case DW_AT_MIPS_loop_unroll_factor:
3949       return "DW_AT_MIPS_loop_unroll_factor";
3950 #endif
3951     case DW_AT_MIPS_software_pipeline_depth:
3952       return "DW_AT_MIPS_software_pipeline_depth";
3953     case DW_AT_MIPS_linkage_name:
3954       return "DW_AT_MIPS_linkage_name";
3955 #if VMS_DEBUGGING_INFO
3956     case DW_AT_HP_epilogue:
3957       return "DW_AT_HP_epilogue";
3958 #else
3959     case DW_AT_MIPS_stride:
3960       return "DW_AT_MIPS_stride";
3961 #endif
3962     case DW_AT_MIPS_abstract_name:
3963       return "DW_AT_MIPS_abstract_name";
3964     case DW_AT_MIPS_clone_origin:
3965       return "DW_AT_MIPS_clone_origin";
3966     case DW_AT_MIPS_has_inlines:
3967       return "DW_AT_MIPS_has_inlines";
3968
3969     case DW_AT_sf_names:
3970       return "DW_AT_sf_names";
3971     case DW_AT_src_info:
3972       return "DW_AT_src_info";
3973     case DW_AT_mac_info:
3974       return "DW_AT_mac_info";
3975     case DW_AT_src_coords:
3976       return "DW_AT_src_coords";
3977     case DW_AT_body_begin:
3978       return "DW_AT_body_begin";
3979     case DW_AT_body_end:
3980       return "DW_AT_body_end";
3981
3982     case DW_AT_GNU_vector:
3983       return "DW_AT_GNU_vector";
3984     case DW_AT_GNU_guarded_by:
3985       return "DW_AT_GNU_guarded_by";
3986     case DW_AT_GNU_pt_guarded_by:
3987       return "DW_AT_GNU_pt_guarded_by";
3988     case DW_AT_GNU_guarded:
3989       return "DW_AT_GNU_guarded";
3990     case DW_AT_GNU_pt_guarded:
3991       return "DW_AT_GNU_pt_guarded";
3992     case DW_AT_GNU_locks_excluded:
3993       return "DW_AT_GNU_locks_excluded";
3994     case DW_AT_GNU_exclusive_locks_required:
3995       return "DW_AT_GNU_exclusive_locks_required";
3996     case DW_AT_GNU_shared_locks_required:
3997       return "DW_AT_GNU_shared_locks_required";
3998     case DW_AT_GNU_odr_signature:
3999       return "DW_AT_GNU_odr_signature";
4000     case DW_AT_GNU_template_name:
4001       return "DW_AT_GNU_template_name";
4002     case DW_AT_GNU_call_site_value:
4003       return "DW_AT_GNU_call_site_value";
4004     case DW_AT_GNU_call_site_data_value:
4005       return "DW_AT_GNU_call_site_data_value";
4006     case DW_AT_GNU_call_site_target:
4007       return "DW_AT_GNU_call_site_target";
4008     case DW_AT_GNU_call_site_target_clobbered:
4009       return "DW_AT_GNU_call_site_target_clobbered";
4010     case DW_AT_GNU_tail_call:
4011       return "DW_AT_GNU_tail_call";
4012     case DW_AT_GNU_all_tail_call_sites:
4013       return "DW_AT_GNU_all_tail_call_sites";
4014     case DW_AT_GNU_all_call_sites:
4015       return "DW_AT_GNU_all_call_sites";
4016     case DW_AT_GNU_all_source_call_sites:
4017       return "DW_AT_GNU_all_source_call_sites";
4018
4019     case DW_AT_GNAT_descriptive_type:
4020       return "DW_AT_GNAT_descriptive_type";
4021
4022     case DW_AT_VMS_rtnbeg_pd_address:
4023       return "DW_AT_VMS_rtnbeg_pd_address";
4024
4025     default:
4026       return "DW_AT_<unknown>";
4027     }
4028 }
4029
4030 /* Convert a DWARF value form code into its string name.  */
4031
4032 static const char *
4033 dwarf_form_name (unsigned int form)
4034 {
4035   switch (form)
4036     {
4037     case DW_FORM_addr:
4038       return "DW_FORM_addr";
4039     case DW_FORM_block2:
4040       return "DW_FORM_block2";
4041     case DW_FORM_block4:
4042       return "DW_FORM_block4";
4043     case DW_FORM_data2:
4044       return "DW_FORM_data2";
4045     case DW_FORM_data4:
4046       return "DW_FORM_data4";
4047     case DW_FORM_data8:
4048       return "DW_FORM_data8";
4049     case DW_FORM_string:
4050       return "DW_FORM_string";
4051     case DW_FORM_block:
4052       return "DW_FORM_block";
4053     case DW_FORM_block1:
4054       return "DW_FORM_block1";
4055     case DW_FORM_data1:
4056       return "DW_FORM_data1";
4057     case DW_FORM_flag:
4058       return "DW_FORM_flag";
4059     case DW_FORM_sdata:
4060       return "DW_FORM_sdata";
4061     case DW_FORM_strp:
4062       return "DW_FORM_strp";
4063     case DW_FORM_udata:
4064       return "DW_FORM_udata";
4065     case DW_FORM_ref_addr:
4066       return "DW_FORM_ref_addr";
4067     case DW_FORM_ref1:
4068       return "DW_FORM_ref1";
4069     case DW_FORM_ref2:
4070       return "DW_FORM_ref2";
4071     case DW_FORM_ref4:
4072       return "DW_FORM_ref4";
4073     case DW_FORM_ref8:
4074       return "DW_FORM_ref8";
4075     case DW_FORM_ref_udata:
4076       return "DW_FORM_ref_udata";
4077     case DW_FORM_indirect:
4078       return "DW_FORM_indirect";
4079     case DW_FORM_sec_offset:
4080       return "DW_FORM_sec_offset";
4081     case DW_FORM_exprloc:
4082       return "DW_FORM_exprloc";
4083     case DW_FORM_flag_present:
4084       return "DW_FORM_flag_present";
4085     case DW_FORM_ref_sig8:
4086       return "DW_FORM_ref_sig8";
4087     default:
4088       return "DW_FORM_<unknown>";
4089     }
4090 }
4091 \f
4092 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4093    instance of an inlined instance of a decl which is local to an inline
4094    function, so we have to trace all of the way back through the origin chain
4095    to find out what sort of node actually served as the original seed for the
4096    given block.  */
4097
4098 static tree
4099 decl_ultimate_origin (const_tree decl)
4100 {
4101   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4102     return NULL_TREE;
4103
4104   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4105      nodes in the function to point to themselves; ignore that if
4106      we're trying to output the abstract instance of this function.  */
4107   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4108     return NULL_TREE;
4109
4110   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4111      most distant ancestor, this should never happen.  */
4112   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4113
4114   return DECL_ABSTRACT_ORIGIN (decl);
4115 }
4116
4117 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4118    of a virtual function may refer to a base class, so we check the 'this'
4119    parameter.  */
4120
4121 static tree
4122 decl_class_context (tree decl)
4123 {
4124   tree context = NULL_TREE;
4125
4126   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4127     context = DECL_CONTEXT (decl);
4128   else
4129     context = TYPE_MAIN_VARIANT
4130       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4131
4132   if (context && !TYPE_P (context))
4133     context = NULL_TREE;
4134
4135   return context;
4136 }
4137 \f
4138 /* Add an attribute/value pair to a DIE.  */
4139
4140 static inline void
4141 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4142 {
4143   /* Maybe this should be an assert?  */
4144   if (die == NULL)
4145     return;
4146
4147   if (die->die_attr == NULL)
4148     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4149   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4150 }
4151
4152 static inline enum dw_val_class
4153 AT_class (dw_attr_ref a)
4154 {
4155   return a->dw_attr_val.val_class;
4156 }
4157
4158 /* Add a flag value attribute to a DIE.  */
4159
4160 static inline void
4161 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4162 {
4163   dw_attr_node attr;
4164
4165   attr.dw_attr = attr_kind;
4166   attr.dw_attr_val.val_class = dw_val_class_flag;
4167   attr.dw_attr_val.v.val_flag = flag;
4168   add_dwarf_attr (die, &attr);
4169 }
4170
4171 static inline unsigned
4172 AT_flag (dw_attr_ref a)
4173 {
4174   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4175   return a->dw_attr_val.v.val_flag;
4176 }
4177
4178 /* Add a signed integer attribute value to a DIE.  */
4179
4180 static inline void
4181 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4182 {
4183   dw_attr_node attr;
4184
4185   attr.dw_attr = attr_kind;
4186   attr.dw_attr_val.val_class = dw_val_class_const;
4187   attr.dw_attr_val.v.val_int = int_val;
4188   add_dwarf_attr (die, &attr);
4189 }
4190
4191 static inline HOST_WIDE_INT
4192 AT_int (dw_attr_ref a)
4193 {
4194   gcc_assert (a && AT_class (a) == dw_val_class_const);
4195   return a->dw_attr_val.v.val_int;
4196 }
4197
4198 /* Add an unsigned integer attribute value to a DIE.  */
4199
4200 static inline void
4201 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4202                  unsigned HOST_WIDE_INT unsigned_val)
4203 {
4204   dw_attr_node attr;
4205
4206   attr.dw_attr = attr_kind;
4207   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4208   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4209   add_dwarf_attr (die, &attr);
4210 }
4211
4212 static inline unsigned HOST_WIDE_INT
4213 AT_unsigned (dw_attr_ref a)
4214 {
4215   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4216   return a->dw_attr_val.v.val_unsigned;
4217 }
4218
4219 /* Add an unsigned double integer attribute value to a DIE.  */
4220
4221 static inline void
4222 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4223                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4224 {
4225   dw_attr_node attr;
4226
4227   attr.dw_attr = attr_kind;
4228   attr.dw_attr_val.val_class = dw_val_class_const_double;
4229   attr.dw_attr_val.v.val_double.high = high;
4230   attr.dw_attr_val.v.val_double.low = low;
4231   add_dwarf_attr (die, &attr);
4232 }
4233
4234 /* Add a floating point attribute value to a DIE and return it.  */
4235
4236 static inline void
4237 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4238             unsigned int length, unsigned int elt_size, unsigned char *array)
4239 {
4240   dw_attr_node attr;
4241
4242   attr.dw_attr = attr_kind;
4243   attr.dw_attr_val.val_class = dw_val_class_vec;
4244   attr.dw_attr_val.v.val_vec.length = length;
4245   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4246   attr.dw_attr_val.v.val_vec.array = array;
4247   add_dwarf_attr (die, &attr);
4248 }
4249
4250 /* Add an 8-byte data attribute value to a DIE.  */
4251
4252 static inline void
4253 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4254               unsigned char data8[8])
4255 {
4256   dw_attr_node attr;
4257
4258   attr.dw_attr = attr_kind;
4259   attr.dw_attr_val.val_class = dw_val_class_data8;
4260   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4261   add_dwarf_attr (die, &attr);
4262 }
4263
4264 /* Hash and equality functions for debug_str_hash.  */
4265
4266 static hashval_t
4267 debug_str_do_hash (const void *x)
4268 {
4269   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4270 }
4271
4272 static int
4273 debug_str_eq (const void *x1, const void *x2)
4274 {
4275   return strcmp ((((const struct indirect_string_node *)x1)->str),
4276                  (const char *)x2) == 0;
4277 }
4278
4279 /* Add STR to the indirect string hash table.  */
4280
4281 static struct indirect_string_node *
4282 find_AT_string (const char *str)
4283 {
4284   struct indirect_string_node *node;
4285   void **slot;
4286
4287   if (! debug_str_hash)
4288     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4289                                       debug_str_eq, NULL);
4290
4291   slot = htab_find_slot_with_hash (debug_str_hash, str,
4292                                    htab_hash_string (str), INSERT);
4293   if (*slot == NULL)
4294     {
4295       node = ggc_alloc_cleared_indirect_string_node ();
4296       node->str = ggc_strdup (str);
4297       *slot = node;
4298     }
4299   else
4300     node = (struct indirect_string_node *) *slot;
4301
4302   node->refcount++;
4303   return node;
4304 }
4305
4306 /* Add a string attribute value to a DIE.  */
4307
4308 static inline void
4309 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4310 {
4311   dw_attr_node attr;
4312   struct indirect_string_node *node;
4313
4314   node = find_AT_string (str);
4315
4316   attr.dw_attr = attr_kind;
4317   attr.dw_attr_val.val_class = dw_val_class_str;
4318   attr.dw_attr_val.v.val_str = node;
4319   add_dwarf_attr (die, &attr);
4320 }
4321
4322 static inline const char *
4323 AT_string (dw_attr_ref a)
4324 {
4325   gcc_assert (a && AT_class (a) == dw_val_class_str);
4326   return a->dw_attr_val.v.val_str->str;
4327 }
4328
4329 /* Find out whether a string should be output inline in DIE
4330    or out-of-line in .debug_str section.  */
4331
4332 static enum dwarf_form
4333 AT_string_form (dw_attr_ref a)
4334 {
4335   struct indirect_string_node *node;
4336   unsigned int len;
4337   char label[32];
4338
4339   gcc_assert (a && AT_class (a) == dw_val_class_str);
4340
4341   node = a->dw_attr_val.v.val_str;
4342   if (node->form)
4343     return node->form;
4344
4345   len = strlen (node->str) + 1;
4346
4347   /* If the string is shorter or equal to the size of the reference, it is
4348      always better to put it inline.  */
4349   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4350     return node->form = DW_FORM_string;
4351
4352   /* If we cannot expect the linker to merge strings in .debug_str
4353      section, only put it into .debug_str if it is worth even in this
4354      single module.  */
4355   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4356       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4357       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4358     return node->form = DW_FORM_string;
4359
4360   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4361   ++dw2_string_counter;
4362   node->label = xstrdup (label);
4363
4364   return node->form = DW_FORM_strp;
4365 }
4366
4367 /* Add a DIE reference attribute value to a DIE.  */
4368
4369 static inline void
4370 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4371 {
4372   dw_attr_node attr;
4373
4374 #ifdef ENABLE_CHECKING
4375   gcc_assert (targ_die != NULL);
4376 #else
4377   /* With LTO we can end up trying to reference something we didn't create
4378      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4379   if (targ_die == NULL)
4380     return;
4381 #endif
4382
4383   attr.dw_attr = attr_kind;
4384   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4385   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4386   attr.dw_attr_val.v.val_die_ref.external = 0;
4387   add_dwarf_attr (die, &attr);
4388 }
4389
4390 /* Add an AT_specification attribute to a DIE, and also make the back
4391    pointer from the specification to the definition.  */
4392
4393 static inline void
4394 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4395 {
4396   add_AT_die_ref (die, DW_AT_specification, targ_die);
4397   gcc_assert (!targ_die->die_definition);
4398   targ_die->die_definition = die;
4399 }
4400
4401 static inline dw_die_ref
4402 AT_ref (dw_attr_ref a)
4403 {
4404   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4405   return a->dw_attr_val.v.val_die_ref.die;
4406 }
4407
4408 static inline int
4409 AT_ref_external (dw_attr_ref a)
4410 {
4411   if (a && AT_class (a) == dw_val_class_die_ref)
4412     return a->dw_attr_val.v.val_die_ref.external;
4413
4414   return 0;
4415 }
4416
4417 static inline void
4418 set_AT_ref_external (dw_attr_ref a, int i)
4419 {
4420   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4421   a->dw_attr_val.v.val_die_ref.external = i;
4422 }
4423
4424 /* Add an FDE reference attribute value to a DIE.  */
4425
4426 static inline void
4427 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4428 {
4429   dw_attr_node attr;
4430
4431   attr.dw_attr = attr_kind;
4432   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4433   attr.dw_attr_val.v.val_fde_index = targ_fde;
4434   add_dwarf_attr (die, &attr);
4435 }
4436
4437 /* Add a location description attribute value to a DIE.  */
4438
4439 static inline void
4440 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4441 {
4442   dw_attr_node attr;
4443
4444   attr.dw_attr = attr_kind;
4445   attr.dw_attr_val.val_class = dw_val_class_loc;
4446   attr.dw_attr_val.v.val_loc = loc;
4447   add_dwarf_attr (die, &attr);
4448 }
4449
4450 static inline dw_loc_descr_ref
4451 AT_loc (dw_attr_ref a)
4452 {
4453   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4454   return a->dw_attr_val.v.val_loc;
4455 }
4456
4457 static inline void
4458 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4459 {
4460   dw_attr_node attr;
4461
4462   attr.dw_attr = attr_kind;
4463   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4464   attr.dw_attr_val.v.val_loc_list = loc_list;
4465   add_dwarf_attr (die, &attr);
4466   have_location_lists = true;
4467 }
4468
4469 static inline dw_loc_list_ref
4470 AT_loc_list (dw_attr_ref a)
4471 {
4472   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4473   return a->dw_attr_val.v.val_loc_list;
4474 }
4475
4476 static inline dw_loc_list_ref *
4477 AT_loc_list_ptr (dw_attr_ref a)
4478 {
4479   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4480   return &a->dw_attr_val.v.val_loc_list;
4481 }
4482
4483 /* Add an address constant attribute value to a DIE.  */
4484
4485 static inline void
4486 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4487 {
4488   dw_attr_node attr;
4489
4490   attr.dw_attr = attr_kind;
4491   attr.dw_attr_val.val_class = dw_val_class_addr;
4492   attr.dw_attr_val.v.val_addr = addr;
4493   add_dwarf_attr (die, &attr);
4494 }
4495
4496 /* Get the RTX from to an address DIE attribute.  */
4497
4498 static inline rtx
4499 AT_addr (dw_attr_ref a)
4500 {
4501   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4502   return a->dw_attr_val.v.val_addr;
4503 }
4504
4505 /* Add a file attribute value to a DIE.  */
4506
4507 static inline void
4508 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4509              struct dwarf_file_data *fd)
4510 {
4511   dw_attr_node attr;
4512
4513   attr.dw_attr = attr_kind;
4514   attr.dw_attr_val.val_class = dw_val_class_file;
4515   attr.dw_attr_val.v.val_file = fd;
4516   add_dwarf_attr (die, &attr);
4517 }
4518
4519 /* Get the dwarf_file_data from a file DIE attribute.  */
4520
4521 static inline struct dwarf_file_data *
4522 AT_file (dw_attr_ref a)
4523 {
4524   gcc_assert (a && AT_class (a) == dw_val_class_file);
4525   return a->dw_attr_val.v.val_file;
4526 }
4527
4528 /* Add a vms delta attribute value to a DIE.  */
4529
4530 static inline void
4531 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4532                   const char *lbl1, const char *lbl2)
4533 {
4534   dw_attr_node attr;
4535
4536   attr.dw_attr = attr_kind;
4537   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4538   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4539   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4540   add_dwarf_attr (die, &attr);
4541 }
4542
4543 /* Add a label identifier attribute value to a DIE.  */
4544
4545 static inline void
4546 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4547 {
4548   dw_attr_node attr;
4549
4550   attr.dw_attr = attr_kind;
4551   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4552   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4553   add_dwarf_attr (die, &attr);
4554 }
4555
4556 /* Add a section offset attribute value to a DIE, an offset into the
4557    debug_line section.  */
4558
4559 static inline void
4560 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4561                 const char *label)
4562 {
4563   dw_attr_node attr;
4564
4565   attr.dw_attr = attr_kind;
4566   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4567   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4568   add_dwarf_attr (die, &attr);
4569 }
4570
4571 /* Add a section offset attribute value to a DIE, an offset into the
4572    debug_macinfo section.  */
4573
4574 static inline void
4575 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4576                const char *label)
4577 {
4578   dw_attr_node attr;
4579
4580   attr.dw_attr = attr_kind;
4581   attr.dw_attr_val.val_class = dw_val_class_macptr;
4582   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4583   add_dwarf_attr (die, &attr);
4584 }
4585
4586 /* Add an offset attribute value to a DIE.  */
4587
4588 static inline void
4589 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4590                unsigned HOST_WIDE_INT offset)
4591 {
4592   dw_attr_node attr;
4593
4594   attr.dw_attr = attr_kind;
4595   attr.dw_attr_val.val_class = dw_val_class_offset;
4596   attr.dw_attr_val.v.val_offset = offset;
4597   add_dwarf_attr (die, &attr);
4598 }
4599
4600 /* Add an range_list attribute value to a DIE.  */
4601
4602 static void
4603 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4604                    long unsigned int offset)
4605 {
4606   dw_attr_node attr;
4607
4608   attr.dw_attr = attr_kind;
4609   attr.dw_attr_val.val_class = dw_val_class_range_list;
4610   attr.dw_attr_val.v.val_offset = offset;
4611   add_dwarf_attr (die, &attr);
4612 }
4613
4614 /* Return the start label of a delta attribute.  */
4615
4616 static inline const char *
4617 AT_vms_delta1 (dw_attr_ref a)
4618 {
4619   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4620   return a->dw_attr_val.v.val_vms_delta.lbl1;
4621 }
4622
4623 /* Return the end label of a delta attribute.  */
4624
4625 static inline const char *
4626 AT_vms_delta2 (dw_attr_ref a)
4627 {
4628   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4629   return a->dw_attr_val.v.val_vms_delta.lbl2;
4630 }
4631
4632 static inline const char *
4633 AT_lbl (dw_attr_ref a)
4634 {
4635   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4636                     || AT_class (a) == dw_val_class_lineptr
4637                     || AT_class (a) == dw_val_class_macptr));
4638   return a->dw_attr_val.v.val_lbl_id;
4639 }
4640
4641 /* Get the attribute of type attr_kind.  */
4642
4643 static dw_attr_ref
4644 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4645 {
4646   dw_attr_ref a;
4647   unsigned ix;
4648   dw_die_ref spec = NULL;
4649
4650   if (! die)
4651     return NULL;
4652
4653   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4654     if (a->dw_attr == attr_kind)
4655       return a;
4656     else if (a->dw_attr == DW_AT_specification
4657              || a->dw_attr == DW_AT_abstract_origin)
4658       spec = AT_ref (a);
4659
4660   if (spec)
4661     return get_AT (spec, attr_kind);
4662
4663   return NULL;
4664 }
4665
4666 /* Return the "low pc" attribute value, typically associated with a subprogram
4667    DIE.  Return null if the "low pc" attribute is either not present, or if it
4668    cannot be represented as an assembler label identifier.  */
4669
4670 static inline const char *
4671 get_AT_low_pc (dw_die_ref die)
4672 {
4673   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4674
4675   return a ? AT_lbl (a) : NULL;
4676 }
4677
4678 /* Return the "high pc" attribute value, typically associated with a subprogram
4679    DIE.  Return null if the "high pc" attribute is either not present, or if it
4680    cannot be represented as an assembler label identifier.  */
4681
4682 static inline const char *
4683 get_AT_hi_pc (dw_die_ref die)
4684 {
4685   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4686
4687   return a ? AT_lbl (a) : NULL;
4688 }
4689
4690 /* Return the value of the string attribute designated by ATTR_KIND, or
4691    NULL if it is not present.  */
4692
4693 static inline const char *
4694 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4695 {
4696   dw_attr_ref a = get_AT (die, attr_kind);
4697
4698   return a ? AT_string (a) : NULL;
4699 }
4700
4701 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4702    if it is not present.  */
4703
4704 static inline int
4705 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4706 {
4707   dw_attr_ref a = get_AT (die, attr_kind);
4708
4709   return a ? AT_flag (a) : 0;
4710 }
4711
4712 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4713    if it is not present.  */
4714
4715 static inline unsigned
4716 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4717 {
4718   dw_attr_ref a = get_AT (die, attr_kind);
4719
4720   return a ? AT_unsigned (a) : 0;
4721 }
4722
4723 static inline dw_die_ref
4724 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4725 {
4726   dw_attr_ref a = get_AT (die, attr_kind);
4727
4728   return a ? AT_ref (a) : NULL;
4729 }
4730
4731 static inline struct dwarf_file_data *
4732 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4733 {
4734   dw_attr_ref a = get_AT (die, attr_kind);
4735
4736   return a ? AT_file (a) : NULL;
4737 }
4738
4739 /* Return TRUE if the language is C++.  */
4740
4741 static inline bool
4742 is_cxx (void)
4743 {
4744   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4745
4746   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4747 }
4748
4749 /* Return TRUE if the language is Fortran.  */
4750
4751 static inline bool
4752 is_fortran (void)
4753 {
4754   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4755
4756   return (lang == DW_LANG_Fortran77
4757           || lang == DW_LANG_Fortran90
4758           || lang == DW_LANG_Fortran95);
4759 }
4760
4761 /* Return TRUE if the language is Ada.  */
4762
4763 static inline bool
4764 is_ada (void)
4765 {
4766   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4767
4768   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4769 }
4770
4771 /* Remove the specified attribute if present.  */
4772
4773 static void
4774 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4775 {
4776   dw_attr_ref a;
4777   unsigned ix;
4778
4779   if (! die)
4780     return;
4781
4782   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4783     if (a->dw_attr == attr_kind)
4784       {
4785         if (AT_class (a) == dw_val_class_str)
4786           if (a->dw_attr_val.v.val_str->refcount)
4787             a->dw_attr_val.v.val_str->refcount--;
4788
4789         /* VEC_ordered_remove should help reduce the number of abbrevs
4790            that are needed.  */
4791         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4792         return;
4793       }
4794 }
4795
4796 /* Remove CHILD from its parent.  PREV must have the property that
4797    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4798
4799 static void
4800 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4801 {
4802   gcc_assert (child->die_parent == prev->die_parent);
4803   gcc_assert (prev->die_sib == child);
4804   if (prev == child)
4805     {
4806       gcc_assert (child->die_parent->die_child == child);
4807       prev = NULL;
4808     }
4809   else
4810     prev->die_sib = child->die_sib;
4811   if (child->die_parent->die_child == child)
4812     child->die_parent->die_child = prev;
4813 }
4814
4815 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4816    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4817
4818 static void
4819 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4820 {
4821   dw_die_ref parent = old_child->die_parent;
4822
4823   gcc_assert (parent == prev->die_parent);
4824   gcc_assert (prev->die_sib == old_child);
4825
4826   new_child->die_parent = parent;
4827   if (prev == old_child)
4828     {
4829       gcc_assert (parent->die_child == old_child);
4830       new_child->die_sib = new_child;
4831     }
4832   else
4833     {
4834       prev->die_sib = new_child;
4835       new_child->die_sib = old_child->die_sib;
4836     }
4837   if (old_child->die_parent->die_child == old_child)
4838     old_child->die_parent->die_child = new_child;
4839 }
4840
4841 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4842
4843 static void
4844 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4845 {
4846   dw_die_ref c;
4847   new_parent->die_child = old_parent->die_child;
4848   old_parent->die_child = NULL;
4849   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4850 }
4851
4852 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4853    matches TAG.  */
4854
4855 static void
4856 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4857 {
4858   dw_die_ref c;
4859
4860   c = die->die_child;
4861   if (c) do {
4862     dw_die_ref prev = c;
4863     c = c->die_sib;
4864     while (c->die_tag == tag)
4865       {
4866         remove_child_with_prev (c, prev);
4867         /* Might have removed every child.  */
4868         if (c == c->die_sib)
4869           return;
4870         c = c->die_sib;
4871       }
4872   } while (c != die->die_child);
4873 }
4874
4875 /* Add a CHILD_DIE as the last child of DIE.  */
4876
4877 static void
4878 add_child_die (dw_die_ref die, dw_die_ref child_die)
4879 {
4880   /* FIXME this should probably be an assert.  */
4881   if (! die || ! child_die)
4882     return;
4883   gcc_assert (die != child_die);
4884
4885   child_die->die_parent = die;
4886   if (die->die_child)
4887     {
4888       child_die->die_sib = die->die_child->die_sib;
4889       die->die_child->die_sib = child_die;
4890     }
4891   else
4892     child_die->die_sib = child_die;
4893   die->die_child = child_die;
4894 }
4895
4896 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4897    is the specification, to the end of PARENT's list of children.
4898    This is done by removing and re-adding it.  */
4899
4900 static void
4901 splice_child_die (dw_die_ref parent, dw_die_ref child)
4902 {
4903   dw_die_ref p;
4904
4905   /* We want the declaration DIE from inside the class, not the
4906      specification DIE at toplevel.  */
4907   if (child->die_parent != parent)
4908     {
4909       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4910
4911       if (tmp)
4912         child = tmp;
4913     }
4914
4915   gcc_assert (child->die_parent == parent
4916               || (child->die_parent
4917                   == get_AT_ref (parent, DW_AT_specification)));
4918
4919   for (p = child->die_parent->die_child; ; p = p->die_sib)
4920     if (p->die_sib == child)
4921       {
4922         remove_child_with_prev (child, p);
4923         break;
4924       }
4925
4926   add_child_die (parent, child);
4927 }
4928
4929 /* Return a pointer to a newly created DIE node.  */
4930
4931 static inline dw_die_ref
4932 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4933 {
4934   dw_die_ref die = ggc_alloc_cleared_die_node ();
4935
4936   die->die_tag = tag_value;
4937
4938   if (parent_die != NULL)
4939     add_child_die (parent_die, die);
4940   else
4941     {
4942       limbo_die_node *limbo_node;
4943
4944       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4945       limbo_node->die = die;
4946       limbo_node->created_for = t;
4947       limbo_node->next = limbo_die_list;
4948       limbo_die_list = limbo_node;
4949     }
4950
4951   return die;
4952 }
4953
4954 /* Return the DIE associated with the given type specifier.  */
4955
4956 static inline dw_die_ref
4957 lookup_type_die (tree type)
4958 {
4959   return TYPE_SYMTAB_DIE (type);
4960 }
4961
4962 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4963    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4964    anonymous type instead the one of the naming typedef.  */
4965
4966 static inline dw_die_ref
4967 strip_naming_typedef (tree type, dw_die_ref type_die)
4968 {
4969   if (type
4970       && TREE_CODE (type) == RECORD_TYPE
4971       && type_die
4972       && type_die->die_tag == DW_TAG_typedef
4973       && is_naming_typedef_decl (TYPE_NAME (type)))
4974     type_die = get_AT_ref (type_die, DW_AT_type);
4975   return type_die;
4976 }
4977
4978 /* Like lookup_type_die, but if type is an anonymous type named by a
4979    typedef[1], return the DIE of the anonymous type instead the one of
4980    the naming typedef.  This is because in gen_typedef_die, we did
4981    equate the anonymous struct named by the typedef with the DIE of
4982    the naming typedef. So by default, lookup_type_die on an anonymous
4983    struct yields the DIE of the naming typedef.
4984
4985    [1]: Read the comment of is_naming_typedef_decl to learn about what
4986    a naming typedef is.  */
4987
4988 static inline dw_die_ref
4989 lookup_type_die_strip_naming_typedef (tree type)
4990 {
4991   dw_die_ref die = lookup_type_die (type);
4992   return strip_naming_typedef (type, die);
4993 }
4994
4995 /* Equate a DIE to a given type specifier.  */
4996
4997 static inline void
4998 equate_type_number_to_die (tree type, dw_die_ref type_die)
4999 {
5000   TYPE_SYMTAB_DIE (type) = type_die;
5001 }
5002
5003 /* Returns a hash value for X (which really is a die_struct).  */
5004
5005 static hashval_t
5006 decl_die_table_hash (const void *x)
5007 {
5008   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5009 }
5010
5011 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5012
5013 static int
5014 decl_die_table_eq (const void *x, const void *y)
5015 {
5016   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5017 }
5018
5019 /* Return the DIE associated with a given declaration.  */
5020
5021 static inline dw_die_ref
5022 lookup_decl_die (tree decl)
5023 {
5024   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5025 }
5026
5027 /* Returns a hash value for X (which really is a var_loc_list).  */
5028
5029 static hashval_t
5030 decl_loc_table_hash (const void *x)
5031 {
5032   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5033 }
5034
5035 /* Return nonzero if decl_id of var_loc_list X is the same as
5036    UID of decl *Y.  */
5037
5038 static int
5039 decl_loc_table_eq (const void *x, const void *y)
5040 {
5041   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5042 }
5043
5044 /* Return the var_loc list associated with a given declaration.  */
5045
5046 static inline var_loc_list *
5047 lookup_decl_loc (const_tree decl)
5048 {
5049   if (!decl_loc_table)
5050     return NULL;
5051   return (var_loc_list *)
5052     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5053 }
5054
5055 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5056
5057 static hashval_t
5058 cached_dw_loc_list_table_hash (const void *x)
5059 {
5060   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5061 }
5062
5063 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5064    UID of decl *Y.  */
5065
5066 static int
5067 cached_dw_loc_list_table_eq (const void *x, const void *y)
5068 {
5069   return (((const cached_dw_loc_list *) x)->decl_id
5070           == DECL_UID ((const_tree) y));
5071 }
5072
5073 /* Equate a DIE to a particular declaration.  */
5074
5075 static void
5076 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5077 {
5078   unsigned int decl_id = DECL_UID (decl);
5079   void **slot;
5080
5081   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5082   *slot = decl_die;
5083   decl_die->decl_id = decl_id;
5084 }
5085
5086 /* Return how many bits covers PIECE EXPR_LIST.  */
5087
5088 static int
5089 decl_piece_bitsize (rtx piece)
5090 {
5091   int ret = (int) GET_MODE (piece);
5092   if (ret)
5093     return ret;
5094   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5095               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5096   return INTVAL (XEXP (XEXP (piece, 0), 0));
5097 }
5098
5099 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5100
5101 static rtx *
5102 decl_piece_varloc_ptr (rtx piece)
5103 {
5104   if ((int) GET_MODE (piece))
5105     return &XEXP (piece, 0);
5106   else
5107     return &XEXP (XEXP (piece, 0), 1);
5108 }
5109
5110 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5111    Next is the chain of following piece nodes.  */
5112
5113 static rtx
5114 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5115 {
5116   if (bitsize <= (int) MAX_MACHINE_MODE)
5117     return alloc_EXPR_LIST (bitsize, loc_note, next);
5118   else
5119     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5120                                                GEN_INT (bitsize),
5121                                                loc_note), next);
5122 }
5123
5124 /* Return rtx that should be stored into loc field for
5125    LOC_NOTE and BITPOS/BITSIZE.  */
5126
5127 static rtx
5128 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5129                       HOST_WIDE_INT bitsize)
5130 {
5131   if (bitsize != -1)
5132     {
5133       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5134       if (bitpos != 0)
5135         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5136     }
5137   return loc_note;
5138 }
5139
5140 /* This function either modifies location piece list *DEST in
5141    place (if SRC and INNER is NULL), or copies location piece list
5142    *SRC to *DEST while modifying it.  Location BITPOS is modified
5143    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5144    not copied and if needed some padding around it is added.
5145    When modifying in place, DEST should point to EXPR_LIST where
5146    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5147    to the start of the whole list and INNER points to the EXPR_LIST
5148    where earlier pieces cover PIECE_BITPOS bits.  */
5149
5150 static void
5151 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5152                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5153                    HOST_WIDE_INT bitsize, rtx loc_note)
5154 {
5155   int diff;
5156   bool copy = inner != NULL;
5157
5158   if (copy)
5159     {
5160       /* First copy all nodes preceeding the current bitpos.  */
5161       while (src != inner)
5162         {
5163           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5164                                    decl_piece_bitsize (*src), NULL_RTX);
5165           dest = &XEXP (*dest, 1);
5166           src = &XEXP (*src, 1);
5167         }
5168     }
5169   /* Add padding if needed.  */
5170   if (bitpos != piece_bitpos)
5171     {
5172       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5173                                copy ? NULL_RTX : *dest);
5174       dest = &XEXP (*dest, 1);
5175     }
5176   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5177     {
5178       gcc_assert (!copy);
5179       /* A piece with correct bitpos and bitsize already exist,
5180          just update the location for it and return.  */
5181       *decl_piece_varloc_ptr (*dest) = loc_note;
5182       return;
5183     }
5184   /* Add the piece that changed.  */
5185   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5186   dest = &XEXP (*dest, 1);
5187   /* Skip over pieces that overlap it.  */
5188   diff = bitpos - piece_bitpos + bitsize;
5189   if (!copy)
5190     src = dest;
5191   while (diff > 0 && *src)
5192     {
5193       rtx piece = *src;
5194       diff -= decl_piece_bitsize (piece);
5195       if (copy)
5196         src = &XEXP (piece, 1);
5197       else
5198         {
5199           *src = XEXP (piece, 1);
5200           free_EXPR_LIST_node (piece);
5201         }
5202     }
5203   /* Add padding if needed.  */
5204   if (diff < 0 && *src)
5205     {
5206       if (!copy)
5207         dest = src;
5208       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5209       dest = &XEXP (*dest, 1);
5210     }
5211   if (!copy)
5212     return;
5213   /* Finally copy all nodes following it.  */
5214   while (*src)
5215     {
5216       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5217                                decl_piece_bitsize (*src), NULL_RTX);
5218       dest = &XEXP (*dest, 1);
5219       src = &XEXP (*src, 1);
5220     }
5221 }
5222
5223 /* Add a variable location node to the linked list for DECL.  */
5224
5225 static struct var_loc_node *
5226 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5227 {
5228   unsigned int decl_id;
5229   var_loc_list *temp;
5230   void **slot;
5231   struct var_loc_node *loc = NULL;
5232   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5233
5234   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5235     {
5236       tree realdecl = DECL_DEBUG_EXPR (decl);
5237       if (realdecl && handled_component_p (realdecl))
5238         {
5239           HOST_WIDE_INT maxsize;
5240           tree innerdecl;
5241           innerdecl
5242             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5243           if (!DECL_P (innerdecl)
5244               || DECL_IGNORED_P (innerdecl)
5245               || TREE_STATIC (innerdecl)
5246               || bitsize <= 0
5247               || bitpos + bitsize > 256
5248               || bitsize != maxsize)
5249             return NULL;
5250           decl = innerdecl;
5251         }
5252     }
5253
5254   decl_id = DECL_UID (decl);
5255   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5256   if (*slot == NULL)
5257     {
5258       temp = ggc_alloc_cleared_var_loc_list ();
5259       temp->decl_id = decl_id;
5260       *slot = temp;
5261     }
5262   else
5263     temp = (var_loc_list *) *slot;
5264
5265   /* For PARM_DECLs try to keep around the original incoming value,
5266      even if that means we'll emit a zero-range .debug_loc entry.  */
5267   if (temp->last
5268       && temp->first == temp->last
5269       && TREE_CODE (decl) == PARM_DECL
5270       && GET_CODE (temp->first->loc) == NOTE
5271       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5272       && DECL_INCOMING_RTL (decl)
5273       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5274       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5275          == GET_CODE (DECL_INCOMING_RTL (decl))
5276       && prev_real_insn (temp->first->loc) == NULL_RTX
5277       && (bitsize != -1
5278           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5279                            NOTE_VAR_LOCATION_LOC (loc_note))
5280           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5281               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5282     {
5283       loc = ggc_alloc_cleared_var_loc_node ();
5284       temp->first->next = loc;
5285       temp->last = loc;
5286       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5287     }
5288   else if (temp->last)
5289     {
5290       struct var_loc_node *last = temp->last, *unused = NULL;
5291       rtx *piece_loc = NULL, last_loc_note;
5292       int piece_bitpos = 0;
5293       if (last->next)
5294         {
5295           last = last->next;
5296           gcc_assert (last->next == NULL);
5297         }
5298       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5299         {
5300           piece_loc = &last->loc;
5301           do
5302             {
5303               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5304               if (piece_bitpos + cur_bitsize > bitpos)
5305                 break;
5306               piece_bitpos += cur_bitsize;
5307               piece_loc = &XEXP (*piece_loc, 1);
5308             }
5309           while (*piece_loc);
5310         }
5311       /* TEMP->LAST here is either pointer to the last but one or
5312          last element in the chained list, LAST is pointer to the
5313          last element.  */
5314       if (label && strcmp (last->label, label) == 0)
5315         {
5316           /* For SRA optimized variables if there weren't any real
5317              insns since last note, just modify the last node.  */
5318           if (piece_loc != NULL)
5319             {
5320               adjust_piece_list (piece_loc, NULL, NULL,
5321                                  bitpos, piece_bitpos, bitsize, loc_note);
5322               return NULL;
5323             }
5324           /* If the last note doesn't cover any instructions, remove it.  */
5325           if (temp->last != last)
5326             {
5327               temp->last->next = NULL;
5328               unused = last;
5329               last = temp->last;
5330               gcc_assert (strcmp (last->label, label) != 0);
5331             }
5332           else
5333             {
5334               gcc_assert (temp->first == temp->last
5335                           || (temp->first->next == temp->last
5336                               && TREE_CODE (decl) == PARM_DECL));
5337               memset (temp->last, '\0', sizeof (*temp->last));
5338               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5339               return temp->last;
5340             }
5341         }
5342       if (bitsize == -1 && NOTE_P (last->loc))
5343         last_loc_note = last->loc;
5344       else if (piece_loc != NULL
5345                && *piece_loc != NULL_RTX
5346                && piece_bitpos == bitpos
5347                && decl_piece_bitsize (*piece_loc) == bitsize)
5348         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5349       else
5350         last_loc_note = NULL_RTX;
5351       /* If the current location is the same as the end of the list,
5352          and either both or neither of the locations is uninitialized,
5353          we have nothing to do.  */
5354       if (last_loc_note == NULL_RTX
5355           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5356                             NOTE_VAR_LOCATION_LOC (loc_note)))
5357           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5358                != NOTE_VAR_LOCATION_STATUS (loc_note))
5359               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5360                    == VAR_INIT_STATUS_UNINITIALIZED)
5361                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5362                       == VAR_INIT_STATUS_UNINITIALIZED))))
5363         {
5364           /* Add LOC to the end of list and update LAST.  If the last
5365              element of the list has been removed above, reuse its
5366              memory for the new node, otherwise allocate a new one.  */
5367           if (unused)
5368             {
5369               loc = unused;
5370               memset (loc, '\0', sizeof (*loc));
5371             }
5372           else
5373             loc = ggc_alloc_cleared_var_loc_node ();
5374           if (bitsize == -1 || piece_loc == NULL)
5375             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5376           else
5377             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5378                                bitpos, piece_bitpos, bitsize, loc_note);
5379           last->next = loc;
5380           /* Ensure TEMP->LAST will point either to the new last but one
5381              element of the chain, or to the last element in it.  */
5382           if (last != temp->last)
5383             temp->last = last;
5384         }
5385       else if (unused)
5386         ggc_free (unused);
5387     }
5388   else
5389     {
5390       loc = ggc_alloc_cleared_var_loc_node ();
5391       temp->first = loc;
5392       temp->last = loc;
5393       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5394     }
5395   return loc;
5396 }
5397 \f
5398 /* Keep track of the number of spaces used to indent the
5399    output of the debugging routines that print the structure of
5400    the DIE internal representation.  */
5401 static int print_indent;
5402
5403 /* Indent the line the number of spaces given by print_indent.  */
5404
5405 static inline void
5406 print_spaces (FILE *outfile)
5407 {
5408   fprintf (outfile, "%*s", print_indent, "");
5409 }
5410
5411 /* Print a type signature in hex.  */
5412
5413 static inline void
5414 print_signature (FILE *outfile, char *sig)
5415 {
5416   int i;
5417
5418   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5419     fprintf (outfile, "%02x", sig[i] & 0xff);
5420 }
5421
5422 /* Print the information associated with a given DIE, and its children.
5423    This routine is a debugging aid only.  */
5424
5425 static void
5426 print_die (dw_die_ref die, FILE *outfile)
5427 {
5428   dw_attr_ref a;
5429   dw_die_ref c;
5430   unsigned ix;
5431
5432   print_spaces (outfile);
5433   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5434            die->die_offset, dwarf_tag_name (die->die_tag),
5435            (void*) die);
5436   print_spaces (outfile);
5437   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5438   fprintf (outfile, " offset: %ld", die->die_offset);
5439   fprintf (outfile, " mark: %d\n", die->die_mark);
5440
5441   if (use_debug_types && die->die_id.die_type_node)
5442     {
5443       print_spaces (outfile);
5444       fprintf (outfile, "  signature: ");
5445       print_signature (outfile, die->die_id.die_type_node->signature);
5446       fprintf (outfile, "\n");
5447     }
5448
5449   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5450     {
5451       print_spaces (outfile);
5452       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5453
5454       switch (AT_class (a))
5455         {
5456         case dw_val_class_addr:
5457           fprintf (outfile, "address");
5458           break;
5459         case dw_val_class_offset:
5460           fprintf (outfile, "offset");
5461           break;
5462         case dw_val_class_loc:
5463           fprintf (outfile, "location descriptor");
5464           break;
5465         case dw_val_class_loc_list:
5466           fprintf (outfile, "location list -> label:%s",
5467                    AT_loc_list (a)->ll_symbol);
5468           break;
5469         case dw_val_class_range_list:
5470           fprintf (outfile, "range list");
5471           break;
5472         case dw_val_class_const:
5473           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5474           break;
5475         case dw_val_class_unsigned_const:
5476           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5477           break;
5478         case dw_val_class_const_double:
5479           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5480                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5481                    a->dw_attr_val.v.val_double.high,
5482                    a->dw_attr_val.v.val_double.low);
5483           break;
5484         case dw_val_class_vec:
5485           fprintf (outfile, "floating-point or vector constant");
5486           break;
5487         case dw_val_class_flag:
5488           fprintf (outfile, "%u", AT_flag (a));
5489           break;
5490         case dw_val_class_die_ref:
5491           if (AT_ref (a) != NULL)
5492             {
5493               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5494                 {
5495                   fprintf (outfile, "die -> signature: ");
5496                   print_signature (outfile,
5497                                    AT_ref (a)->die_id.die_type_node->signature);
5498                 }
5499               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5500                 fprintf (outfile, "die -> label: %s",
5501                          AT_ref (a)->die_id.die_symbol);
5502               else
5503                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5504               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5505             }
5506           else
5507             fprintf (outfile, "die -> <null>");
5508           break;
5509         case dw_val_class_vms_delta:
5510           fprintf (outfile, "delta: @slotcount(%s-%s)",
5511                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5512           break;
5513         case dw_val_class_lbl_id:
5514         case dw_val_class_lineptr:
5515         case dw_val_class_macptr:
5516           fprintf (outfile, "label: %s", AT_lbl (a));
5517           break;
5518         case dw_val_class_str:
5519           if (AT_string (a) != NULL)
5520             fprintf (outfile, "\"%s\"", AT_string (a));
5521           else
5522             fprintf (outfile, "<null>");
5523           break;
5524         case dw_val_class_file:
5525           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5526                    AT_file (a)->emitted_number);
5527           break;
5528         case dw_val_class_data8:
5529           {
5530             int i;
5531
5532             for (i = 0; i < 8; i++)
5533               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5534             break;
5535           }
5536         default:
5537           break;
5538         }
5539
5540       fprintf (outfile, "\n");
5541     }
5542
5543   if (die->die_child != NULL)
5544     {
5545       print_indent += 4;
5546       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5547       print_indent -= 4;
5548     }
5549   if (print_indent == 0)
5550     fprintf (outfile, "\n");
5551 }
5552
5553 /* Print the information collected for a given DIE.  */
5554
5555 DEBUG_FUNCTION void
5556 debug_dwarf_die (dw_die_ref die)
5557 {
5558   print_die (die, stderr);
5559 }
5560
5561 /* Print all DWARF information collected for the compilation unit.
5562    This routine is a debugging aid only.  */
5563
5564 DEBUG_FUNCTION void
5565 debug_dwarf (void)
5566 {
5567   print_indent = 0;
5568   print_die (comp_unit_die (), stderr);
5569 }
5570 \f
5571 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5572    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5573    DIE that marks the start of the DIEs for this include file.  */
5574
5575 static dw_die_ref
5576 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5577 {
5578   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5579   dw_die_ref new_unit = gen_compile_unit_die (filename);
5580
5581   new_unit->die_sib = old_unit;
5582   return new_unit;
5583 }
5584
5585 /* Close an include-file CU and reopen the enclosing one.  */
5586
5587 static dw_die_ref
5588 pop_compile_unit (dw_die_ref old_unit)
5589 {
5590   dw_die_ref new_unit = old_unit->die_sib;
5591
5592   old_unit->die_sib = NULL;
5593   return new_unit;
5594 }
5595
5596 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5597 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5598
5599 /* Calculate the checksum of a location expression.  */
5600
5601 static inline void
5602 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5603 {
5604   int tem;
5605
5606   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5607   CHECKSUM (tem);
5608   CHECKSUM (loc->dw_loc_oprnd1);
5609   CHECKSUM (loc->dw_loc_oprnd2);
5610 }
5611
5612 /* Calculate the checksum of an attribute.  */
5613
5614 static void
5615 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5616 {
5617   dw_loc_descr_ref loc;
5618   rtx r;
5619
5620   CHECKSUM (at->dw_attr);
5621
5622   /* We don't care that this was compiled with a different compiler
5623      snapshot; if the output is the same, that's what matters.  */
5624   if (at->dw_attr == DW_AT_producer)
5625     return;
5626
5627   switch (AT_class (at))
5628     {
5629     case dw_val_class_const:
5630       CHECKSUM (at->dw_attr_val.v.val_int);
5631       break;
5632     case dw_val_class_unsigned_const:
5633       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5634       break;
5635     case dw_val_class_const_double:
5636       CHECKSUM (at->dw_attr_val.v.val_double);
5637       break;
5638     case dw_val_class_vec:
5639       CHECKSUM (at->dw_attr_val.v.val_vec);
5640       break;
5641     case dw_val_class_flag:
5642       CHECKSUM (at->dw_attr_val.v.val_flag);
5643       break;
5644     case dw_val_class_str:
5645       CHECKSUM_STRING (AT_string (at));
5646       break;
5647
5648     case dw_val_class_addr:
5649       r = AT_addr (at);
5650       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5651       CHECKSUM_STRING (XSTR (r, 0));
5652       break;
5653
5654     case dw_val_class_offset:
5655       CHECKSUM (at->dw_attr_val.v.val_offset);
5656       break;
5657
5658     case dw_val_class_loc:
5659       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5660         loc_checksum (loc, ctx);
5661       break;
5662
5663     case dw_val_class_die_ref:
5664       die_checksum (AT_ref (at), ctx, mark);
5665       break;
5666
5667     case dw_val_class_fde_ref:
5668     case dw_val_class_vms_delta:
5669     case dw_val_class_lbl_id:
5670     case dw_val_class_lineptr:
5671     case dw_val_class_macptr:
5672       break;
5673
5674     case dw_val_class_file:
5675       CHECKSUM_STRING (AT_file (at)->filename);
5676       break;
5677
5678     case dw_val_class_data8:
5679       CHECKSUM (at->dw_attr_val.v.val_data8);
5680       break;
5681
5682     default:
5683       break;
5684     }
5685 }
5686
5687 /* Calculate the checksum of a DIE.  */
5688
5689 static void
5690 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5691 {
5692   dw_die_ref c;
5693   dw_attr_ref a;
5694   unsigned ix;
5695
5696   /* To avoid infinite recursion.  */
5697   if (die->die_mark)
5698     {
5699       CHECKSUM (die->die_mark);
5700       return;
5701     }
5702   die->die_mark = ++(*mark);
5703
5704   CHECKSUM (die->die_tag);
5705
5706   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5707     attr_checksum (a, ctx, mark);
5708
5709   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5710 }
5711
5712 #undef CHECKSUM
5713 #undef CHECKSUM_STRING
5714
5715 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5716 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5717 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5718 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5719 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5720 #define CHECKSUM_ATTR(FOO) \
5721   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5722
5723 /* Calculate the checksum of a number in signed LEB128 format.  */
5724
5725 static void
5726 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5727 {
5728   unsigned char byte;
5729   bool more;
5730
5731   while (1)
5732     {
5733       byte = (value & 0x7f);
5734       value >>= 7;
5735       more = !((value == 0 && (byte & 0x40) == 0)
5736                 || (value == -1 && (byte & 0x40) != 0));
5737       if (more)
5738         byte |= 0x80;
5739       CHECKSUM (byte);
5740       if (!more)
5741         break;
5742     }
5743 }
5744
5745 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5746
5747 static void
5748 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5749 {
5750   while (1)
5751     {
5752       unsigned char byte = (value & 0x7f);
5753       value >>= 7;
5754       if (value != 0)
5755         /* More bytes to follow.  */
5756         byte |= 0x80;
5757       CHECKSUM (byte);
5758       if (value == 0)
5759         break;
5760     }
5761 }
5762
5763 /* Checksum the context of the DIE.  This adds the names of any
5764    surrounding namespaces or structures to the checksum.  */
5765
5766 static void
5767 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5768 {
5769   const char *name;
5770   dw_die_ref spec;
5771   int tag = die->die_tag;
5772
5773   if (tag != DW_TAG_namespace
5774       && tag != DW_TAG_structure_type
5775       && tag != DW_TAG_class_type)
5776     return;
5777
5778   name = get_AT_string (die, DW_AT_name);
5779
5780   spec = get_AT_ref (die, DW_AT_specification);
5781   if (spec != NULL)
5782     die = spec;
5783
5784   if (die->die_parent != NULL)
5785     checksum_die_context (die->die_parent, ctx);
5786
5787   CHECKSUM_ULEB128 ('C');
5788   CHECKSUM_ULEB128 (tag);
5789   if (name != NULL)
5790     CHECKSUM_STRING (name);
5791 }
5792
5793 /* Calculate the checksum of a location expression.  */
5794
5795 static inline void
5796 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5797 {
5798   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5799      were emitted as a DW_FORM_sdata instead of a location expression.  */
5800   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5801     {
5802       CHECKSUM_ULEB128 (DW_FORM_sdata);
5803       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5804       return;
5805     }
5806
5807   /* Otherwise, just checksum the raw location expression.  */
5808   while (loc != NULL)
5809     {
5810       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5811       CHECKSUM (loc->dw_loc_oprnd1);
5812       CHECKSUM (loc->dw_loc_oprnd2);
5813       loc = loc->dw_loc_next;
5814     }
5815 }
5816
5817 /* Calculate the checksum of an attribute.  */
5818
5819 static void
5820 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5821                        struct md5_ctx *ctx, int *mark)
5822 {
5823   dw_loc_descr_ref loc;
5824   rtx r;
5825
5826   if (AT_class (at) == dw_val_class_die_ref)
5827     {
5828       dw_die_ref target_die = AT_ref (at);
5829
5830       /* For pointer and reference types, we checksum only the (qualified)
5831          name of the target type (if there is a name).  For friend entries,
5832          we checksum only the (qualified) name of the target type or function.
5833          This allows the checksum to remain the same whether the target type
5834          is complete or not.  */
5835       if ((at->dw_attr == DW_AT_type
5836            && (tag == DW_TAG_pointer_type
5837                || tag == DW_TAG_reference_type
5838                || tag == DW_TAG_rvalue_reference_type
5839                || tag == DW_TAG_ptr_to_member_type))
5840           || (at->dw_attr == DW_AT_friend
5841               && tag == DW_TAG_friend))
5842         {
5843           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5844
5845           if (name_attr != NULL)
5846             {
5847               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5848
5849               if (decl == NULL)
5850                 decl = target_die;
5851               CHECKSUM_ULEB128 ('N');
5852               CHECKSUM_ULEB128 (at->dw_attr);
5853               if (decl->die_parent != NULL)
5854                 checksum_die_context (decl->die_parent, ctx);
5855               CHECKSUM_ULEB128 ('E');
5856               CHECKSUM_STRING (AT_string (name_attr));
5857               return;
5858             }
5859         }
5860
5861       /* For all other references to another DIE, we check to see if the
5862          target DIE has already been visited.  If it has, we emit a
5863          backward reference; if not, we descend recursively.  */
5864       if (target_die->die_mark > 0)
5865         {
5866           CHECKSUM_ULEB128 ('R');
5867           CHECKSUM_ULEB128 (at->dw_attr);
5868           CHECKSUM_ULEB128 (target_die->die_mark);
5869         }
5870       else
5871         {
5872           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5873
5874           if (decl == NULL)
5875             decl = target_die;
5876           target_die->die_mark = ++(*mark);
5877           CHECKSUM_ULEB128 ('T');
5878           CHECKSUM_ULEB128 (at->dw_attr);
5879           if (decl->die_parent != NULL)
5880             checksum_die_context (decl->die_parent, ctx);
5881           die_checksum_ordered (target_die, ctx, mark);
5882         }
5883       return;
5884     }
5885
5886   CHECKSUM_ULEB128 ('A');
5887   CHECKSUM_ULEB128 (at->dw_attr);
5888
5889   switch (AT_class (at))
5890     {
5891     case dw_val_class_const:
5892       CHECKSUM_ULEB128 (DW_FORM_sdata);
5893       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5894       break;
5895
5896     case dw_val_class_unsigned_const:
5897       CHECKSUM_ULEB128 (DW_FORM_sdata);
5898       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5899       break;
5900
5901     case dw_val_class_const_double:
5902       CHECKSUM_ULEB128 (DW_FORM_block);
5903       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5904       CHECKSUM (at->dw_attr_val.v.val_double);
5905       break;
5906
5907     case dw_val_class_vec:
5908       CHECKSUM_ULEB128 (DW_FORM_block);
5909       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5910       CHECKSUM (at->dw_attr_val.v.val_vec);
5911       break;
5912
5913     case dw_val_class_flag:
5914       CHECKSUM_ULEB128 (DW_FORM_flag);
5915       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5916       break;
5917
5918     case dw_val_class_str:
5919       CHECKSUM_ULEB128 (DW_FORM_string);
5920       CHECKSUM_STRING (AT_string (at));
5921       break;
5922
5923     case dw_val_class_addr:
5924       r = AT_addr (at);
5925       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5926       CHECKSUM_ULEB128 (DW_FORM_string);
5927       CHECKSUM_STRING (XSTR (r, 0));
5928       break;
5929
5930     case dw_val_class_offset:
5931       CHECKSUM_ULEB128 (DW_FORM_sdata);
5932       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5933       break;
5934
5935     case dw_val_class_loc:
5936       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5937         loc_checksum_ordered (loc, ctx);
5938       break;
5939
5940     case dw_val_class_fde_ref:
5941     case dw_val_class_lbl_id:
5942     case dw_val_class_lineptr:
5943     case dw_val_class_macptr:
5944       break;
5945
5946     case dw_val_class_file:
5947       CHECKSUM_ULEB128 (DW_FORM_string);
5948       CHECKSUM_STRING (AT_file (at)->filename);
5949       break;
5950
5951     case dw_val_class_data8:
5952       CHECKSUM (at->dw_attr_val.v.val_data8);
5953       break;
5954
5955     default:
5956       break;
5957     }
5958 }
5959
5960 struct checksum_attributes
5961 {
5962   dw_attr_ref at_name;
5963   dw_attr_ref at_type;
5964   dw_attr_ref at_friend;
5965   dw_attr_ref at_accessibility;
5966   dw_attr_ref at_address_class;
5967   dw_attr_ref at_allocated;
5968   dw_attr_ref at_artificial;
5969   dw_attr_ref at_associated;
5970   dw_attr_ref at_binary_scale;
5971   dw_attr_ref at_bit_offset;
5972   dw_attr_ref at_bit_size;
5973   dw_attr_ref at_bit_stride;
5974   dw_attr_ref at_byte_size;
5975   dw_attr_ref at_byte_stride;
5976   dw_attr_ref at_const_value;
5977   dw_attr_ref at_containing_type;
5978   dw_attr_ref at_count;
5979   dw_attr_ref at_data_location;
5980   dw_attr_ref at_data_member_location;
5981   dw_attr_ref at_decimal_scale;
5982   dw_attr_ref at_decimal_sign;
5983   dw_attr_ref at_default_value;
5984   dw_attr_ref at_digit_count;
5985   dw_attr_ref at_discr;
5986   dw_attr_ref at_discr_list;
5987   dw_attr_ref at_discr_value;
5988   dw_attr_ref at_encoding;
5989   dw_attr_ref at_endianity;
5990   dw_attr_ref at_explicit;
5991   dw_attr_ref at_is_optional;
5992   dw_attr_ref at_location;
5993   dw_attr_ref at_lower_bound;
5994   dw_attr_ref at_mutable;
5995   dw_attr_ref at_ordering;
5996   dw_attr_ref at_picture_string;
5997   dw_attr_ref at_prototyped;
5998   dw_attr_ref at_small;
5999   dw_attr_ref at_segment;
6000   dw_attr_ref at_string_length;
6001   dw_attr_ref at_threads_scaled;
6002   dw_attr_ref at_upper_bound;
6003   dw_attr_ref at_use_location;
6004   dw_attr_ref at_use_UTF8;
6005   dw_attr_ref at_variable_parameter;
6006   dw_attr_ref at_virtuality;
6007   dw_attr_ref at_visibility;
6008   dw_attr_ref at_vtable_elem_location;
6009 };
6010
6011 /* Collect the attributes that we will want to use for the checksum.  */
6012
6013 static void
6014 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6015 {
6016   dw_attr_ref a;
6017   unsigned ix;
6018
6019   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6020     {
6021       switch (a->dw_attr)
6022         {
6023         case DW_AT_name:
6024           attrs->at_name = a;
6025           break;
6026         case DW_AT_type:
6027           attrs->at_type = a;
6028           break;
6029         case DW_AT_friend:
6030           attrs->at_friend = a;
6031           break;
6032         case DW_AT_accessibility:
6033           attrs->at_accessibility = a;
6034           break;
6035         case DW_AT_address_class:
6036           attrs->at_address_class = a;
6037           break;
6038         case DW_AT_allocated:
6039           attrs->at_allocated = a;
6040           break;
6041         case DW_AT_artificial:
6042           attrs->at_artificial = a;
6043           break;
6044         case DW_AT_associated:
6045           attrs->at_associated = a;
6046           break;
6047         case DW_AT_binary_scale:
6048           attrs->at_binary_scale = a;
6049           break;
6050         case DW_AT_bit_offset:
6051           attrs->at_bit_offset = a;
6052           break;
6053         case DW_AT_bit_size:
6054           attrs->at_bit_size = a;
6055           break;
6056         case DW_AT_bit_stride:
6057           attrs->at_bit_stride = a;
6058           break;
6059         case DW_AT_byte_size:
6060           attrs->at_byte_size = a;
6061           break;
6062         case DW_AT_byte_stride:
6063           attrs->at_byte_stride = a;
6064           break;
6065         case DW_AT_const_value:
6066           attrs->at_const_value = a;
6067           break;
6068         case DW_AT_containing_type:
6069           attrs->at_containing_type = a;
6070           break;
6071         case DW_AT_count:
6072           attrs->at_count = a;
6073           break;
6074         case DW_AT_data_location:
6075           attrs->at_data_location = a;
6076           break;
6077         case DW_AT_data_member_location:
6078           attrs->at_data_member_location = a;
6079           break;
6080         case DW_AT_decimal_scale:
6081           attrs->at_decimal_scale = a;
6082           break;
6083         case DW_AT_decimal_sign:
6084           attrs->at_decimal_sign = a;
6085           break;
6086         case DW_AT_default_value:
6087           attrs->at_default_value = a;
6088           break;
6089         case DW_AT_digit_count:
6090           attrs->at_digit_count = a;
6091           break;
6092         case DW_AT_discr:
6093           attrs->at_discr = a;
6094           break;
6095         case DW_AT_discr_list:
6096           attrs->at_discr_list = a;
6097           break;
6098         case DW_AT_discr_value:
6099           attrs->at_discr_value = a;
6100           break;
6101         case DW_AT_encoding:
6102           attrs->at_encoding = a;
6103           break;
6104         case DW_AT_endianity:
6105           attrs->at_endianity = a;
6106           break;
6107         case DW_AT_explicit:
6108           attrs->at_explicit = a;
6109           break;
6110         case DW_AT_is_optional:
6111           attrs->at_is_optional = a;
6112           break;
6113         case DW_AT_location:
6114           attrs->at_location = a;
6115           break;
6116         case DW_AT_lower_bound:
6117           attrs->at_lower_bound = a;
6118           break;
6119         case DW_AT_mutable:
6120           attrs->at_mutable = a;
6121           break;
6122         case DW_AT_ordering:
6123           attrs->at_ordering = a;
6124           break;
6125         case DW_AT_picture_string:
6126           attrs->at_picture_string = a;
6127           break;
6128         case DW_AT_prototyped:
6129           attrs->at_prototyped = a;
6130           break;
6131         case DW_AT_small:
6132           attrs->at_small = a;
6133           break;
6134         case DW_AT_segment:
6135           attrs->at_segment = a;
6136           break;
6137         case DW_AT_string_length:
6138           attrs->at_string_length = a;
6139           break;
6140         case DW_AT_threads_scaled:
6141           attrs->at_threads_scaled = a;
6142           break;
6143         case DW_AT_upper_bound:
6144           attrs->at_upper_bound = a;
6145           break;
6146         case DW_AT_use_location:
6147           attrs->at_use_location = a;
6148           break;
6149         case DW_AT_use_UTF8:
6150           attrs->at_use_UTF8 = a;
6151           break;
6152         case DW_AT_variable_parameter:
6153           attrs->at_variable_parameter = a;
6154           break;
6155         case DW_AT_virtuality:
6156           attrs->at_virtuality = a;
6157           break;
6158         case DW_AT_visibility:
6159           attrs->at_visibility = a;
6160           break;
6161         case DW_AT_vtable_elem_location:
6162           attrs->at_vtable_elem_location = a;
6163           break;
6164         default:
6165           break;
6166         }
6167     }
6168 }
6169
6170 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6171
6172 static void
6173 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6174 {
6175   dw_die_ref c;
6176   dw_die_ref decl;
6177   struct checksum_attributes attrs;
6178
6179   CHECKSUM_ULEB128 ('D');
6180   CHECKSUM_ULEB128 (die->die_tag);
6181
6182   memset (&attrs, 0, sizeof (attrs));
6183
6184   decl = get_AT_ref (die, DW_AT_specification);
6185   if (decl != NULL)
6186     collect_checksum_attributes (&attrs, decl);
6187   collect_checksum_attributes (&attrs, die);
6188
6189   CHECKSUM_ATTR (attrs.at_name);
6190   CHECKSUM_ATTR (attrs.at_accessibility);
6191   CHECKSUM_ATTR (attrs.at_address_class);
6192   CHECKSUM_ATTR (attrs.at_allocated);
6193   CHECKSUM_ATTR (attrs.at_artificial);
6194   CHECKSUM_ATTR (attrs.at_associated);
6195   CHECKSUM_ATTR (attrs.at_binary_scale);
6196   CHECKSUM_ATTR (attrs.at_bit_offset);
6197   CHECKSUM_ATTR (attrs.at_bit_size);
6198   CHECKSUM_ATTR (attrs.at_bit_stride);
6199   CHECKSUM_ATTR (attrs.at_byte_size);
6200   CHECKSUM_ATTR (attrs.at_byte_stride);
6201   CHECKSUM_ATTR (attrs.at_const_value);
6202   CHECKSUM_ATTR (attrs.at_containing_type);
6203   CHECKSUM_ATTR (attrs.at_count);
6204   CHECKSUM_ATTR (attrs.at_data_location);
6205   CHECKSUM_ATTR (attrs.at_data_member_location);
6206   CHECKSUM_ATTR (attrs.at_decimal_scale);
6207   CHECKSUM_ATTR (attrs.at_decimal_sign);
6208   CHECKSUM_ATTR (attrs.at_default_value);
6209   CHECKSUM_ATTR (attrs.at_digit_count);
6210   CHECKSUM_ATTR (attrs.at_discr);
6211   CHECKSUM_ATTR (attrs.at_discr_list);
6212   CHECKSUM_ATTR (attrs.at_discr_value);
6213   CHECKSUM_ATTR (attrs.at_encoding);
6214   CHECKSUM_ATTR (attrs.at_endianity);
6215   CHECKSUM_ATTR (attrs.at_explicit);
6216   CHECKSUM_ATTR (attrs.at_is_optional);
6217   CHECKSUM_ATTR (attrs.at_location);
6218   CHECKSUM_ATTR (attrs.at_lower_bound);
6219   CHECKSUM_ATTR (attrs.at_mutable);
6220   CHECKSUM_ATTR (attrs.at_ordering);
6221   CHECKSUM_ATTR (attrs.at_picture_string);
6222   CHECKSUM_ATTR (attrs.at_prototyped);
6223   CHECKSUM_ATTR (attrs.at_small);
6224   CHECKSUM_ATTR (attrs.at_segment);
6225   CHECKSUM_ATTR (attrs.at_string_length);
6226   CHECKSUM_ATTR (attrs.at_threads_scaled);
6227   CHECKSUM_ATTR (attrs.at_upper_bound);
6228   CHECKSUM_ATTR (attrs.at_use_location);
6229   CHECKSUM_ATTR (attrs.at_use_UTF8);
6230   CHECKSUM_ATTR (attrs.at_variable_parameter);
6231   CHECKSUM_ATTR (attrs.at_virtuality);
6232   CHECKSUM_ATTR (attrs.at_visibility);
6233   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6234   CHECKSUM_ATTR (attrs.at_type);
6235   CHECKSUM_ATTR (attrs.at_friend);
6236
6237   /* Checksum the child DIEs, except for nested types and member functions.  */
6238   c = die->die_child;
6239   if (c) do {
6240     dw_attr_ref name_attr;
6241
6242     c = c->die_sib;
6243     name_attr = get_AT (c, DW_AT_name);
6244     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6245         && name_attr != NULL)
6246       {
6247         CHECKSUM_ULEB128 ('S');
6248         CHECKSUM_ULEB128 (c->die_tag);
6249         CHECKSUM_STRING (AT_string (name_attr));
6250       }
6251     else
6252       {
6253         /* Mark this DIE so it gets processed when unmarking.  */
6254         if (c->die_mark == 0)
6255           c->die_mark = -1;
6256         die_checksum_ordered (c, ctx, mark);
6257       }
6258   } while (c != die->die_child);
6259
6260   CHECKSUM_ULEB128 (0);
6261 }
6262
6263 #undef CHECKSUM
6264 #undef CHECKSUM_STRING
6265 #undef CHECKSUM_ATTR
6266 #undef CHECKSUM_LEB128
6267 #undef CHECKSUM_ULEB128
6268
6269 /* Generate the type signature for DIE.  This is computed by generating an
6270    MD5 checksum over the DIE's tag, its relevant attributes, and its
6271    children.  Attributes that are references to other DIEs are processed
6272    by recursion, using the MARK field to prevent infinite recursion.
6273    If the DIE is nested inside a namespace or another type, we also
6274    need to include that context in the signature.  The lower 64 bits
6275    of the resulting MD5 checksum comprise the signature.  */
6276
6277 static void
6278 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6279 {
6280   int mark;
6281   const char *name;
6282   unsigned char checksum[16];
6283   struct md5_ctx ctx;
6284   dw_die_ref decl;
6285
6286   name = get_AT_string (die, DW_AT_name);
6287   decl = get_AT_ref (die, DW_AT_specification);
6288
6289   /* First, compute a signature for just the type name (and its surrounding
6290      context, if any.  This is stored in the type unit DIE for link-time
6291      ODR (one-definition rule) checking.  */
6292
6293   if (is_cxx() && name != NULL)
6294     {
6295       md5_init_ctx (&ctx);
6296
6297       /* Checksum the names of surrounding namespaces and structures.  */
6298       if (decl != NULL && decl->die_parent != NULL)
6299         checksum_die_context (decl->die_parent, &ctx);
6300
6301       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6302       md5_process_bytes (name, strlen (name) + 1, &ctx);
6303       md5_finish_ctx (&ctx, checksum);
6304
6305       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6306     }
6307
6308   /* Next, compute the complete type signature.  */
6309
6310   md5_init_ctx (&ctx);
6311   mark = 1;
6312   die->die_mark = mark;
6313
6314   /* Checksum the names of surrounding namespaces and structures.  */
6315   if (decl != NULL && decl->die_parent != NULL)
6316     checksum_die_context (decl->die_parent, &ctx);
6317
6318   /* Checksum the DIE and its children.  */
6319   die_checksum_ordered (die, &ctx, &mark);
6320   unmark_all_dies (die);
6321   md5_finish_ctx (&ctx, checksum);
6322
6323   /* Store the signature in the type node and link the type DIE and the
6324      type node together.  */
6325   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6326           DWARF_TYPE_SIGNATURE_SIZE);
6327   die->die_id.die_type_node = type_node;
6328   type_node->type_die = die;
6329
6330   /* If the DIE is a specification, link its declaration to the type node
6331      as well.  */
6332   if (decl != NULL)
6333     decl->die_id.die_type_node = type_node;
6334 }
6335
6336 /* Do the location expressions look same?  */
6337 static inline int
6338 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6339 {
6340   return loc1->dw_loc_opc == loc2->dw_loc_opc
6341          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6342          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6343 }
6344
6345 /* Do the values look the same?  */
6346 static int
6347 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6348 {
6349   dw_loc_descr_ref loc1, loc2;
6350   rtx r1, r2;
6351
6352   if (v1->val_class != v2->val_class)
6353     return 0;
6354
6355   switch (v1->val_class)
6356     {
6357     case dw_val_class_const:
6358       return v1->v.val_int == v2->v.val_int;
6359     case dw_val_class_unsigned_const:
6360       return v1->v.val_unsigned == v2->v.val_unsigned;
6361     case dw_val_class_const_double:
6362       return v1->v.val_double.high == v2->v.val_double.high
6363              && v1->v.val_double.low == v2->v.val_double.low;
6364     case dw_val_class_vec:
6365       if (v1->v.val_vec.length != v2->v.val_vec.length
6366           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6367         return 0;
6368       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6369                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6370         return 0;
6371       return 1;
6372     case dw_val_class_flag:
6373       return v1->v.val_flag == v2->v.val_flag;
6374     case dw_val_class_str:
6375       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6376
6377     case dw_val_class_addr:
6378       r1 = v1->v.val_addr;
6379       r2 = v2->v.val_addr;
6380       if (GET_CODE (r1) != GET_CODE (r2))
6381         return 0;
6382       return !rtx_equal_p (r1, r2);
6383
6384     case dw_val_class_offset:
6385       return v1->v.val_offset == v2->v.val_offset;
6386
6387     case dw_val_class_loc:
6388       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6389            loc1 && loc2;
6390            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6391         if (!same_loc_p (loc1, loc2, mark))
6392           return 0;
6393       return !loc1 && !loc2;
6394
6395     case dw_val_class_die_ref:
6396       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6397
6398     case dw_val_class_fde_ref:
6399     case dw_val_class_vms_delta:
6400     case dw_val_class_lbl_id:
6401     case dw_val_class_lineptr:
6402     case dw_val_class_macptr:
6403       return 1;
6404
6405     case dw_val_class_file:
6406       return v1->v.val_file == v2->v.val_file;
6407
6408     case dw_val_class_data8:
6409       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6410
6411     default:
6412       return 1;
6413     }
6414 }
6415
6416 /* Do the attributes look the same?  */
6417
6418 static int
6419 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6420 {
6421   if (at1->dw_attr != at2->dw_attr)
6422     return 0;
6423
6424   /* We don't care that this was compiled with a different compiler
6425      snapshot; if the output is the same, that's what matters. */
6426   if (at1->dw_attr == DW_AT_producer)
6427     return 1;
6428
6429   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6430 }
6431
6432 /* Do the dies look the same?  */
6433
6434 static int
6435 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6436 {
6437   dw_die_ref c1, c2;
6438   dw_attr_ref a1;
6439   unsigned ix;
6440
6441   /* To avoid infinite recursion.  */
6442   if (die1->die_mark)
6443     return die1->die_mark == die2->die_mark;
6444   die1->die_mark = die2->die_mark = ++(*mark);
6445
6446   if (die1->die_tag != die2->die_tag)
6447     return 0;
6448
6449   if (VEC_length (dw_attr_node, die1->die_attr)
6450       != VEC_length (dw_attr_node, die2->die_attr))
6451     return 0;
6452
6453   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6454     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6455       return 0;
6456
6457   c1 = die1->die_child;
6458   c2 = die2->die_child;
6459   if (! c1)
6460     {
6461       if (c2)
6462         return 0;
6463     }
6464   else
6465     for (;;)
6466       {
6467         if (!same_die_p (c1, c2, mark))
6468           return 0;
6469         c1 = c1->die_sib;
6470         c2 = c2->die_sib;
6471         if (c1 == die1->die_child)
6472           {
6473             if (c2 == die2->die_child)
6474               break;
6475             else
6476               return 0;
6477           }
6478     }
6479
6480   return 1;
6481 }
6482
6483 /* Do the dies look the same?  Wrapper around same_die_p.  */
6484
6485 static int
6486 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6487 {
6488   int mark = 0;
6489   int ret = same_die_p (die1, die2, &mark);
6490
6491   unmark_all_dies (die1);
6492   unmark_all_dies (die2);
6493
6494   return ret;
6495 }
6496
6497 /* The prefix to attach to symbols on DIEs in the current comdat debug
6498    info section.  */
6499 static char *comdat_symbol_id;
6500
6501 /* The index of the current symbol within the current comdat CU.  */
6502 static unsigned int comdat_symbol_number;
6503
6504 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6505    children, and set comdat_symbol_id accordingly.  */
6506
6507 static void
6508 compute_section_prefix (dw_die_ref unit_die)
6509 {
6510   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6511   const char *base = die_name ? lbasename (die_name) : "anonymous";
6512   char *name = XALLOCAVEC (char, strlen (base) + 64);
6513   char *p;
6514   int i, mark;
6515   unsigned char checksum[16];
6516   struct md5_ctx ctx;
6517
6518   /* Compute the checksum of the DIE, then append part of it as hex digits to
6519      the name filename of the unit.  */
6520
6521   md5_init_ctx (&ctx);
6522   mark = 0;
6523   die_checksum (unit_die, &ctx, &mark);
6524   unmark_all_dies (unit_die);
6525   md5_finish_ctx (&ctx, checksum);
6526
6527   sprintf (name, "%s.", base);
6528   clean_symbol_name (name);
6529
6530   p = name + strlen (name);
6531   for (i = 0; i < 4; i++)
6532     {
6533       sprintf (p, "%.2x", checksum[i]);
6534       p += 2;
6535     }
6536
6537   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6538   comdat_symbol_number = 0;
6539 }
6540
6541 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6542
6543 static int
6544 is_type_die (dw_die_ref die)
6545 {
6546   switch (die->die_tag)
6547     {
6548     case DW_TAG_array_type:
6549     case DW_TAG_class_type:
6550     case DW_TAG_interface_type:
6551     case DW_TAG_enumeration_type:
6552     case DW_TAG_pointer_type:
6553     case DW_TAG_reference_type:
6554     case DW_TAG_rvalue_reference_type:
6555     case DW_TAG_string_type:
6556     case DW_TAG_structure_type:
6557     case DW_TAG_subroutine_type:
6558     case DW_TAG_union_type:
6559     case DW_TAG_ptr_to_member_type:
6560     case DW_TAG_set_type:
6561     case DW_TAG_subrange_type:
6562     case DW_TAG_base_type:
6563     case DW_TAG_const_type:
6564     case DW_TAG_file_type:
6565     case DW_TAG_packed_type:
6566     case DW_TAG_volatile_type:
6567     case DW_TAG_typedef:
6568       return 1;
6569     default:
6570       return 0;
6571     }
6572 }
6573
6574 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6575    Basically, we want to choose the bits that are likely to be shared between
6576    compilations (types) and leave out the bits that are specific to individual
6577    compilations (functions).  */
6578
6579 static int
6580 is_comdat_die (dw_die_ref c)
6581 {
6582   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6583      we do for stabs.  The advantage is a greater likelihood of sharing between
6584      objects that don't include headers in the same order (and therefore would
6585      put the base types in a different comdat).  jason 8/28/00 */
6586
6587   if (c->die_tag == DW_TAG_base_type)
6588     return 0;
6589
6590   if (c->die_tag == DW_TAG_pointer_type
6591       || c->die_tag == DW_TAG_reference_type
6592       || c->die_tag == DW_TAG_rvalue_reference_type
6593       || c->die_tag == DW_TAG_const_type
6594       || c->die_tag == DW_TAG_volatile_type)
6595     {
6596       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6597
6598       return t ? is_comdat_die (t) : 0;
6599     }
6600
6601   return is_type_die (c);
6602 }
6603
6604 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6605    compilation unit.  */
6606
6607 static int
6608 is_symbol_die (dw_die_ref c)
6609 {
6610   return (is_type_die (c)
6611           || is_declaration_die (c)
6612           || c->die_tag == DW_TAG_namespace
6613           || c->die_tag == DW_TAG_module);
6614 }
6615
6616 /* Returns true iff C is a compile-unit DIE.  */
6617
6618 static inline bool
6619 is_cu_die (dw_die_ref c)
6620 {
6621   return c && c->die_tag == DW_TAG_compile_unit;
6622 }
6623
6624 static char *
6625 gen_internal_sym (const char *prefix)
6626 {
6627   char buf[256];
6628
6629   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6630   return xstrdup (buf);
6631 }
6632
6633 /* Assign symbols to all worthy DIEs under DIE.  */
6634
6635 static void
6636 assign_symbol_names (dw_die_ref die)
6637 {
6638   dw_die_ref c;
6639
6640   if (is_symbol_die (die))
6641     {
6642       if (comdat_symbol_id)
6643         {
6644           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6645
6646           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6647                    comdat_symbol_id, comdat_symbol_number++);
6648           die->die_id.die_symbol = xstrdup (p);
6649         }
6650       else
6651         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6652     }
6653
6654   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6655 }
6656
6657 struct cu_hash_table_entry
6658 {
6659   dw_die_ref cu;
6660   unsigned min_comdat_num, max_comdat_num;
6661   struct cu_hash_table_entry *next;
6662 };
6663
6664 /* Routines to manipulate hash table of CUs.  */
6665 static hashval_t
6666 htab_cu_hash (const void *of)
6667 {
6668   const struct cu_hash_table_entry *const entry =
6669     (const struct cu_hash_table_entry *) of;
6670
6671   return htab_hash_string (entry->cu->die_id.die_symbol);
6672 }
6673
6674 static int
6675 htab_cu_eq (const void *of1, const void *of2)
6676 {
6677   const struct cu_hash_table_entry *const entry1 =
6678     (const struct cu_hash_table_entry *) of1;
6679   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6680
6681   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6682 }
6683
6684 static void
6685 htab_cu_del (void *what)
6686 {
6687   struct cu_hash_table_entry *next,
6688     *entry = (struct cu_hash_table_entry *) what;
6689
6690   while (entry)
6691     {
6692       next = entry->next;
6693       free (entry);
6694       entry = next;
6695     }
6696 }
6697
6698 /* Check whether we have already seen this CU and set up SYM_NUM
6699    accordingly.  */
6700 static int
6701 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6702 {
6703   struct cu_hash_table_entry dummy;
6704   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6705
6706   dummy.max_comdat_num = 0;
6707
6708   slot = (struct cu_hash_table_entry **)
6709     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6710         INSERT);
6711   entry = *slot;
6712
6713   for (; entry; last = entry, entry = entry->next)
6714     {
6715       if (same_die_p_wrap (cu, entry->cu))
6716         break;
6717     }
6718
6719   if (entry)
6720     {
6721       *sym_num = entry->min_comdat_num;
6722       return 1;
6723     }
6724
6725   entry = XCNEW (struct cu_hash_table_entry);
6726   entry->cu = cu;
6727   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6728   entry->next = *slot;
6729   *slot = entry;
6730
6731   return 0;
6732 }
6733
6734 /* Record SYM_NUM to record of CU in HTABLE.  */
6735 static void
6736 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6737 {
6738   struct cu_hash_table_entry **slot, *entry;
6739
6740   slot = (struct cu_hash_table_entry **)
6741     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6742         NO_INSERT);
6743   entry = *slot;
6744
6745   entry->max_comdat_num = sym_num;
6746 }
6747
6748 /* Traverse the DIE (which is always comp_unit_die), and set up
6749    additional compilation units for each of the include files we see
6750    bracketed by BINCL/EINCL.  */
6751
6752 static void
6753 break_out_includes (dw_die_ref die)
6754 {
6755   dw_die_ref c;
6756   dw_die_ref unit = NULL;
6757   limbo_die_node *node, **pnode;
6758   htab_t cu_hash_table;
6759
6760   c = die->die_child;
6761   if (c) do {
6762     dw_die_ref prev = c;
6763     c = c->die_sib;
6764     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6765            || (unit && is_comdat_die (c)))
6766       {
6767         dw_die_ref next = c->die_sib;
6768
6769         /* This DIE is for a secondary CU; remove it from the main one.  */
6770         remove_child_with_prev (c, prev);
6771
6772         if (c->die_tag == DW_TAG_GNU_BINCL)
6773           unit = push_new_compile_unit (unit, c);
6774         else if (c->die_tag == DW_TAG_GNU_EINCL)
6775           unit = pop_compile_unit (unit);
6776         else
6777           add_child_die (unit, c);
6778         c = next;
6779         if (c == die->die_child)
6780           break;
6781       }
6782   } while (c != die->die_child);
6783
6784 #if 0
6785   /* We can only use this in debugging, since the frontend doesn't check
6786      to make sure that we leave every include file we enter.  */
6787   gcc_assert (!unit);
6788 #endif
6789
6790   assign_symbol_names (die);
6791   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6792   for (node = limbo_die_list, pnode = &limbo_die_list;
6793        node;
6794        node = node->next)
6795     {
6796       int is_dupl;
6797
6798       compute_section_prefix (node->die);
6799       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6800                         &comdat_symbol_number);
6801       assign_symbol_names (node->die);
6802       if (is_dupl)
6803         *pnode = node->next;
6804       else
6805         {
6806           pnode = &node->next;
6807           record_comdat_symbol_number (node->die, cu_hash_table,
6808                 comdat_symbol_number);
6809         }
6810     }
6811   htab_delete (cu_hash_table);
6812 }
6813
6814 /* Return non-zero if this DIE is a declaration.  */
6815
6816 static int
6817 is_declaration_die (dw_die_ref die)
6818 {
6819   dw_attr_ref a;
6820   unsigned ix;
6821
6822   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6823     if (a->dw_attr == DW_AT_declaration)
6824       return 1;
6825
6826   return 0;
6827 }
6828
6829 /* Return non-zero if this DIE is nested inside a subprogram.  */
6830
6831 static int
6832 is_nested_in_subprogram (dw_die_ref die)
6833 {
6834   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6835
6836   if (decl == NULL)
6837     decl = die;
6838   return local_scope_p (decl);
6839 }
6840
6841 /* Return non-zero if this DIE contains a defining declaration of a
6842    subprogram.  */
6843
6844 static int
6845 contains_subprogram_definition (dw_die_ref die)
6846 {
6847   dw_die_ref c;
6848
6849   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6850     return 1;
6851   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6852   return 0;
6853 }
6854
6855 /* Return non-zero if this is a type DIE that should be moved to a
6856    COMDAT .debug_types section.  */
6857
6858 static int
6859 should_move_die_to_comdat (dw_die_ref die)
6860 {
6861   switch (die->die_tag)
6862     {
6863     case DW_TAG_class_type:
6864     case DW_TAG_structure_type:
6865     case DW_TAG_enumeration_type:
6866     case DW_TAG_union_type:
6867       /* Don't move declarations, inlined instances, or types nested in a
6868          subprogram.  */
6869       if (is_declaration_die (die)
6870           || get_AT (die, DW_AT_abstract_origin)
6871           || is_nested_in_subprogram (die))
6872         return 0;
6873       /* A type definition should never contain a subprogram definition.  */
6874       gcc_assert (!contains_subprogram_definition (die));
6875       return 1;
6876     case DW_TAG_array_type:
6877     case DW_TAG_interface_type:
6878     case DW_TAG_pointer_type:
6879     case DW_TAG_reference_type:
6880     case DW_TAG_rvalue_reference_type:
6881     case DW_TAG_string_type:
6882     case DW_TAG_subroutine_type:
6883     case DW_TAG_ptr_to_member_type:
6884     case DW_TAG_set_type:
6885     case DW_TAG_subrange_type:
6886     case DW_TAG_base_type:
6887     case DW_TAG_const_type:
6888     case DW_TAG_file_type:
6889     case DW_TAG_packed_type:
6890     case DW_TAG_volatile_type:
6891     case DW_TAG_typedef:
6892     default:
6893       return 0;
6894     }
6895 }
6896
6897 /* Make a clone of DIE.  */
6898
6899 static dw_die_ref
6900 clone_die (dw_die_ref die)
6901 {
6902   dw_die_ref clone;
6903   dw_attr_ref a;
6904   unsigned ix;
6905
6906   clone = ggc_alloc_cleared_die_node ();
6907   clone->die_tag = die->die_tag;
6908
6909   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6910     add_dwarf_attr (clone, a);
6911
6912   return clone;
6913 }
6914
6915 /* Make a clone of the tree rooted at DIE.  */
6916
6917 static dw_die_ref
6918 clone_tree (dw_die_ref die)
6919 {
6920   dw_die_ref c;
6921   dw_die_ref clone = clone_die (die);
6922
6923   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6924
6925   return clone;
6926 }
6927
6928 /* Make a clone of DIE as a declaration.  */
6929
6930 static dw_die_ref
6931 clone_as_declaration (dw_die_ref die)
6932 {
6933   dw_die_ref clone;
6934   dw_die_ref decl;
6935   dw_attr_ref a;
6936   unsigned ix;
6937
6938   /* If the DIE is already a declaration, just clone it.  */
6939   if (is_declaration_die (die))
6940     return clone_die (die);
6941
6942   /* If the DIE is a specification, just clone its declaration DIE.  */
6943   decl = get_AT_ref (die, DW_AT_specification);
6944   if (decl != NULL)
6945     return clone_die (decl);
6946
6947   clone = ggc_alloc_cleared_die_node ();
6948   clone->die_tag = die->die_tag;
6949
6950   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6951     {
6952       /* We don't want to copy over all attributes.
6953          For example we don't want DW_AT_byte_size because otherwise we will no
6954          longer have a declaration and GDB will treat it as a definition.  */
6955
6956       switch (a->dw_attr)
6957         {
6958         case DW_AT_artificial:
6959         case DW_AT_containing_type:
6960         case DW_AT_external:
6961         case DW_AT_name:
6962         case DW_AT_type:
6963         case DW_AT_virtuality:
6964         case DW_AT_linkage_name:
6965         case DW_AT_MIPS_linkage_name:
6966           add_dwarf_attr (clone, a);
6967           break;
6968         case DW_AT_byte_size:
6969         default:
6970           break;
6971         }
6972     }
6973
6974   if (die->die_id.die_type_node)
6975     add_AT_die_ref (clone, DW_AT_signature, die);
6976
6977   add_AT_flag (clone, DW_AT_declaration, 1);
6978   return clone;
6979 }
6980
6981 /* Copy the declaration context to the new compile unit DIE.  This includes
6982    any surrounding namespace or type declarations.  If the DIE has an
6983    AT_specification attribute, it also includes attributes and children
6984    attached to the specification.  */
6985
6986 static void
6987 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6988 {
6989   dw_die_ref decl;
6990   dw_die_ref new_decl;
6991
6992   decl = get_AT_ref (die, DW_AT_specification);
6993   if (decl == NULL)
6994     decl = die;
6995   else
6996     {
6997       unsigned ix;
6998       dw_die_ref c;
6999       dw_attr_ref a;
7000
7001       /* Copy the type node pointer from the new DIE to the original
7002          declaration DIE so we can forward references later.  */
7003       decl->die_id.die_type_node = die->die_id.die_type_node;
7004
7005       remove_AT (die, DW_AT_specification);
7006
7007       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7008         {
7009           if (a->dw_attr != DW_AT_name
7010               && a->dw_attr != DW_AT_declaration
7011               && a->dw_attr != DW_AT_external)
7012             add_dwarf_attr (die, a);
7013         }
7014
7015       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7016     }
7017
7018   if (decl->die_parent != NULL
7019       && decl->die_parent->die_tag != DW_TAG_compile_unit
7020       && decl->die_parent->die_tag != DW_TAG_type_unit)
7021     {
7022       new_decl = copy_ancestor_tree (unit, decl, NULL);
7023       if (new_decl != NULL)
7024         {
7025           remove_AT (new_decl, DW_AT_signature);
7026           add_AT_specification (die, new_decl);
7027         }
7028     }
7029 }
7030
7031 /* Generate the skeleton ancestor tree for the given NODE, then clone
7032    the DIE and add the clone into the tree.  */
7033
7034 static void
7035 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7036 {
7037   if (node->new_die != NULL)
7038     return;
7039
7040   node->new_die = clone_as_declaration (node->old_die);
7041
7042   if (node->parent != NULL)
7043     {
7044       generate_skeleton_ancestor_tree (node->parent);
7045       add_child_die (node->parent->new_die, node->new_die);
7046     }
7047 }
7048
7049 /* Generate a skeleton tree of DIEs containing any declarations that are
7050    found in the original tree.  We traverse the tree looking for declaration
7051    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7052
7053 static void
7054 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7055 {
7056   skeleton_chain_node node;
7057   dw_die_ref c;
7058   dw_die_ref first;
7059   dw_die_ref prev = NULL;
7060   dw_die_ref next = NULL;
7061
7062   node.parent = parent;
7063
7064   first = c = parent->old_die->die_child;
7065   if (c)
7066     next = c->die_sib;
7067   if (c) do {
7068     if (prev == NULL || prev->die_sib == c)
7069       prev = c;
7070     c = next;
7071     next = (c == first ? NULL : c->die_sib);
7072     node.old_die = c;
7073     node.new_die = NULL;
7074     if (is_declaration_die (c))
7075       {
7076         /* Clone the existing DIE, move the original to the skeleton
7077            tree (which is in the main CU), and put the clone, with
7078            all the original's children, where the original came from.  */
7079         dw_die_ref clone = clone_die (c);
7080         move_all_children (c, clone);
7081
7082         replace_child (c, clone, prev);
7083         generate_skeleton_ancestor_tree (parent);
7084         add_child_die (parent->new_die, c);
7085         node.new_die = c;
7086         c = clone;
7087       }
7088     generate_skeleton_bottom_up (&node);
7089   } while (next != NULL);
7090 }
7091
7092 /* Wrapper function for generate_skeleton_bottom_up.  */
7093
7094 static dw_die_ref
7095 generate_skeleton (dw_die_ref die)
7096 {
7097   skeleton_chain_node node;
7098
7099   node.old_die = die;
7100   node.new_die = NULL;
7101   node.parent = NULL;
7102
7103   /* If this type definition is nested inside another type,
7104      always leave at least a declaration in its place.  */
7105   if (die->die_parent != NULL && is_type_die (die->die_parent))
7106     node.new_die = clone_as_declaration (die);
7107
7108   generate_skeleton_bottom_up (&node);
7109   return node.new_die;
7110 }
7111
7112 /* Remove the DIE from its parent, possibly replacing it with a cloned
7113    declaration.  The original DIE will be moved to a new compile unit
7114    so that existing references to it follow it to the new location.  If
7115    any of the original DIE's descendants is a declaration, we need to
7116    replace the original DIE with a skeleton tree and move the
7117    declarations back into the skeleton tree.  */
7118
7119 static dw_die_ref
7120 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7121 {
7122   dw_die_ref skeleton;
7123
7124   skeleton = generate_skeleton (child);
7125   if (skeleton == NULL)
7126     remove_child_with_prev (child, prev);
7127   else
7128     {
7129       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7130       replace_child (child, skeleton, prev);
7131     }
7132
7133   return skeleton;
7134 }
7135
7136 /* Traverse the DIE and set up additional .debug_types sections for each
7137    type worthy of being placed in a COMDAT section.  */
7138
7139 static void
7140 break_out_comdat_types (dw_die_ref die)
7141 {
7142   dw_die_ref c;
7143   dw_die_ref first;
7144   dw_die_ref prev = NULL;
7145   dw_die_ref next = NULL;
7146   dw_die_ref unit = NULL;
7147
7148   first = c = die->die_child;
7149   if (c)
7150     next = c->die_sib;
7151   if (c) do {
7152     if (prev == NULL || prev->die_sib == c)
7153       prev = c;
7154     c = next;
7155     next = (c == first ? NULL : c->die_sib);
7156     if (should_move_die_to_comdat (c))
7157       {
7158         dw_die_ref replacement;
7159         comdat_type_node_ref type_node;
7160
7161         /* Create a new type unit DIE as the root for the new tree, and
7162            add it to the list of comdat types.  */
7163         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7164         add_AT_unsigned (unit, DW_AT_language,
7165                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7166         type_node = ggc_alloc_cleared_comdat_type_node ();
7167         type_node->root_die = unit;
7168         type_node->next = comdat_type_list;
7169         comdat_type_list = type_node;
7170
7171         /* Generate the type signature.  */
7172         generate_type_signature (c, type_node);
7173
7174         /* Copy the declaration context, attributes, and children of the
7175            declaration into the new compile unit DIE.  */
7176         copy_declaration_context (unit, c);
7177
7178         /* Remove this DIE from the main CU.  */
7179         replacement = remove_child_or_replace_with_skeleton (c, prev);
7180
7181         /* Break out nested types into their own type units.  */
7182         break_out_comdat_types (c);
7183
7184         /* Add the DIE to the new compunit.  */
7185         add_child_die (unit, c);
7186
7187         if (replacement != NULL)
7188           c = replacement;
7189       }
7190     else if (c->die_tag == DW_TAG_namespace
7191              || c->die_tag == DW_TAG_class_type
7192              || c->die_tag == DW_TAG_structure_type
7193              || c->die_tag == DW_TAG_union_type)
7194       {
7195         /* Look for nested types that can be broken out.  */
7196         break_out_comdat_types (c);
7197       }
7198   } while (next != NULL);
7199 }
7200
7201 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7202
7203 struct decl_table_entry
7204 {
7205   dw_die_ref orig;
7206   dw_die_ref copy;
7207 };
7208
7209 /* Routines to manipulate hash table of copied declarations.  */
7210
7211 static hashval_t
7212 htab_decl_hash (const void *of)
7213 {
7214   const struct decl_table_entry *const entry =
7215     (const struct decl_table_entry *) of;
7216
7217   return htab_hash_pointer (entry->orig);
7218 }
7219
7220 static int
7221 htab_decl_eq (const void *of1, const void *of2)
7222 {
7223   const struct decl_table_entry *const entry1 =
7224     (const struct decl_table_entry *) of1;
7225   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7226
7227   return entry1->orig == entry2;
7228 }
7229
7230 static void
7231 htab_decl_del (void *what)
7232 {
7233   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7234
7235   free (entry);
7236 }
7237
7238 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7239    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7240    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7241    to check if the ancestor has already been copied into UNIT.  */
7242
7243 static dw_die_ref
7244 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7245 {
7246   dw_die_ref parent = die->die_parent;
7247   dw_die_ref new_parent = unit;
7248   dw_die_ref copy;
7249   void **slot = NULL;
7250   struct decl_table_entry *entry = NULL;
7251
7252   if (decl_table)
7253     {
7254       /* Check if the entry has already been copied to UNIT.  */
7255       slot = htab_find_slot_with_hash (decl_table, die,
7256                                        htab_hash_pointer (die), INSERT);
7257       if (*slot != HTAB_EMPTY_ENTRY)
7258         {
7259           entry = (struct decl_table_entry *) *slot;
7260           return entry->copy;
7261         }
7262
7263       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7264       entry = XCNEW (struct decl_table_entry);
7265       entry->orig = die;
7266       entry->copy = NULL;
7267       *slot = entry;
7268     }
7269
7270   if (parent != NULL)
7271     {
7272       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7273       if (spec != NULL)
7274         parent = spec;
7275       if (parent->die_tag != DW_TAG_compile_unit
7276           && parent->die_tag != DW_TAG_type_unit)
7277         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7278     }
7279
7280   copy = clone_as_declaration (die);
7281   add_child_die (new_parent, copy);
7282
7283   if (decl_table != NULL)
7284     {
7285       /* Record the pointer to the copy.  */
7286       entry->copy = copy;
7287     }
7288
7289   return copy;
7290 }
7291
7292 /* Walk the DIE and its children, looking for references to incomplete
7293    or trivial types that are unmarked (i.e., that are not in the current
7294    type_unit).  */
7295
7296 static void
7297 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7298 {
7299   dw_die_ref c;
7300   dw_attr_ref a;
7301   unsigned ix;
7302
7303   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7304     {
7305       if (AT_class (a) == dw_val_class_die_ref)
7306         {
7307           dw_die_ref targ = AT_ref (a);
7308           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7309           void **slot;
7310           struct decl_table_entry *entry;
7311
7312           if (targ->die_mark != 0 || type_node != NULL)
7313             continue;
7314
7315           slot = htab_find_slot_with_hash (decl_table, targ,
7316                                            htab_hash_pointer (targ), INSERT);
7317
7318           if (*slot != HTAB_EMPTY_ENTRY)
7319             {
7320               /* TARG has already been copied, so we just need to
7321                  modify the reference to point to the copy.  */
7322               entry = (struct decl_table_entry *) *slot;
7323               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7324             }
7325           else
7326             {
7327               dw_die_ref parent = unit;
7328               dw_die_ref copy = clone_tree (targ);
7329
7330               /* Make sure the cloned tree is marked as part of the
7331                  type unit.  */
7332               mark_dies (copy);
7333
7334               /* Record in DECL_TABLE that TARG has been copied.
7335                  Need to do this now, before the recursive call,
7336                  because DECL_TABLE may be expanded and SLOT
7337                  would no longer be a valid pointer.  */
7338               entry = XCNEW (struct decl_table_entry);
7339               entry->orig = targ;
7340               entry->copy = copy;
7341               *slot = entry;
7342
7343               /* If TARG has surrounding context, copy its ancestor tree
7344                  into the new type unit.  */
7345               if (targ->die_parent != NULL
7346                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7347                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7348                 parent = copy_ancestor_tree (unit, targ->die_parent,
7349                                              decl_table);
7350
7351               add_child_die (parent, copy);
7352               a->dw_attr_val.v.val_die_ref.die = copy;
7353
7354               /* Make sure the newly-copied DIE is walked.  If it was
7355                  installed in a previously-added context, it won't
7356                  get visited otherwise.  */
7357               if (parent != unit)
7358                 {
7359                   /* Find the highest point of the newly-added tree,
7360                      mark each node along the way, and walk from there.  */
7361                   parent->die_mark = 1;
7362                   while (parent->die_parent
7363                          && parent->die_parent->die_mark == 0)
7364                     {
7365                       parent = parent->die_parent;
7366                       parent->die_mark = 1;
7367                     }
7368                   copy_decls_walk (unit, parent, decl_table);
7369                 }
7370             }
7371         }
7372     }
7373
7374   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7375 }
7376
7377 /* Copy declarations for "unworthy" types into the new comdat section.
7378    Incomplete types, modified types, and certain other types aren't broken
7379    out into comdat sections of their own, so they don't have a signature,
7380    and we need to copy the declaration into the same section so that we
7381    don't have an external reference.  */
7382
7383 static void
7384 copy_decls_for_unworthy_types (dw_die_ref unit)
7385 {
7386   htab_t decl_table;
7387
7388   mark_dies (unit);
7389   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7390   copy_decls_walk (unit, unit, decl_table);
7391   htab_delete (decl_table);
7392   unmark_dies (unit);
7393 }
7394
7395 /* Traverse the DIE and add a sibling attribute if it may have the
7396    effect of speeding up access to siblings.  To save some space,
7397    avoid generating sibling attributes for DIE's without children.  */
7398
7399 static void
7400 add_sibling_attributes (dw_die_ref die)
7401 {
7402   dw_die_ref c;
7403
7404   if (! die->die_child)
7405     return;
7406
7407   if (die->die_parent && die != die->die_parent->die_child)
7408     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7409
7410   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7411 }
7412
7413 /* Output all location lists for the DIE and its children.  */
7414
7415 static void
7416 output_location_lists (dw_die_ref die)
7417 {
7418   dw_die_ref c;
7419   dw_attr_ref a;
7420   unsigned ix;
7421
7422   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7423     if (AT_class (a) == dw_val_class_loc_list)
7424       output_loc_list (AT_loc_list (a));
7425
7426   FOR_EACH_CHILD (die, c, output_location_lists (c));
7427 }
7428
7429 /* The format of each DIE (and its attribute value pairs) is encoded in an
7430    abbreviation table.  This routine builds the abbreviation table and assigns
7431    a unique abbreviation id for each abbreviation entry.  The children of each
7432    die are visited recursively.  */
7433
7434 static void
7435 build_abbrev_table (dw_die_ref die)
7436 {
7437   unsigned long abbrev_id;
7438   unsigned int n_alloc;
7439   dw_die_ref c;
7440   dw_attr_ref a;
7441   unsigned ix;
7442
7443   /* Scan the DIE references, and mark as external any that refer to
7444      DIEs from other CUs (i.e. those which are not marked).  */
7445   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7446     if (AT_class (a) == dw_val_class_die_ref
7447         && AT_ref (a)->die_mark == 0)
7448       {
7449         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7450         set_AT_ref_external (a, 1);
7451       }
7452
7453   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7454     {
7455       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7456       dw_attr_ref die_a, abbrev_a;
7457       unsigned ix;
7458       bool ok = true;
7459
7460       if (abbrev->die_tag != die->die_tag)
7461         continue;
7462       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7463         continue;
7464
7465       if (VEC_length (dw_attr_node, abbrev->die_attr)
7466           != VEC_length (dw_attr_node, die->die_attr))
7467         continue;
7468
7469       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7470         {
7471           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7472           if ((abbrev_a->dw_attr != die_a->dw_attr)
7473               || (value_format (abbrev_a) != value_format (die_a)))
7474             {
7475               ok = false;
7476               break;
7477             }
7478         }
7479       if (ok)
7480         break;
7481     }
7482
7483   if (abbrev_id >= abbrev_die_table_in_use)
7484     {
7485       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7486         {
7487           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7488           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7489                                             n_alloc);
7490
7491           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7492                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7493           abbrev_die_table_allocated = n_alloc;
7494         }
7495
7496       ++abbrev_die_table_in_use;
7497       abbrev_die_table[abbrev_id] = die;
7498     }
7499
7500   die->die_abbrev = abbrev_id;
7501   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7502 }
7503 \f
7504 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7505
7506 static int
7507 constant_size (unsigned HOST_WIDE_INT value)
7508 {
7509   int log;
7510
7511   if (value == 0)
7512     log = 0;
7513   else
7514     log = floor_log2 (value);
7515
7516   log = log / 8;
7517   log = 1 << (floor_log2 (log) + 1);
7518
7519   return log;
7520 }
7521
7522 /* Return the size of a DIE as it is represented in the
7523    .debug_info section.  */
7524
7525 static unsigned long
7526 size_of_die (dw_die_ref die)
7527 {
7528   unsigned long size = 0;
7529   dw_attr_ref a;
7530   unsigned ix;
7531
7532   size += size_of_uleb128 (die->die_abbrev);
7533   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7534     {
7535       switch (AT_class (a))
7536         {
7537         case dw_val_class_addr:
7538           size += DWARF2_ADDR_SIZE;
7539           break;
7540         case dw_val_class_offset:
7541           size += DWARF_OFFSET_SIZE;
7542           break;
7543         case dw_val_class_loc:
7544           {
7545             unsigned long lsize = size_of_locs (AT_loc (a));
7546
7547             /* Block length.  */
7548             if (dwarf_version >= 4)
7549               size += size_of_uleb128 (lsize);
7550             else
7551               size += constant_size (lsize);
7552             size += lsize;
7553           }
7554           break;
7555         case dw_val_class_loc_list:
7556           size += DWARF_OFFSET_SIZE;
7557           break;
7558         case dw_val_class_range_list:
7559           size += DWARF_OFFSET_SIZE;
7560           break;
7561         case dw_val_class_const:
7562           size += size_of_sleb128 (AT_int (a));
7563           break;
7564         case dw_val_class_unsigned_const:
7565           size += constant_size (AT_unsigned (a));
7566           break;
7567         case dw_val_class_const_double:
7568           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7569           if (HOST_BITS_PER_WIDE_INT >= 64)
7570             size++; /* block */
7571           break;
7572         case dw_val_class_vec:
7573           size += constant_size (a->dw_attr_val.v.val_vec.length
7574                                  * a->dw_attr_val.v.val_vec.elt_size)
7575                   + a->dw_attr_val.v.val_vec.length
7576                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7577           break;
7578         case dw_val_class_flag:
7579           if (dwarf_version >= 4)
7580             /* Currently all add_AT_flag calls pass in 1 as last argument,
7581                so DW_FORM_flag_present can be used.  If that ever changes,
7582                we'll need to use DW_FORM_flag and have some optimization
7583                in build_abbrev_table that will change those to
7584                DW_FORM_flag_present if it is set to 1 in all DIEs using
7585                the same abbrev entry.  */
7586             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7587           else
7588             size += 1;
7589           break;
7590         case dw_val_class_die_ref:
7591           if (AT_ref_external (a))
7592             {
7593               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7594                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7595                  is sized by target address length, whereas in DWARF3
7596                  it's always sized as an offset.  */
7597               if (use_debug_types)
7598                 size += DWARF_TYPE_SIGNATURE_SIZE;
7599               else if (dwarf_version == 2)
7600                 size += DWARF2_ADDR_SIZE;
7601               else
7602                 size += DWARF_OFFSET_SIZE;
7603             }
7604           else
7605             size += DWARF_OFFSET_SIZE;
7606           break;
7607         case dw_val_class_fde_ref:
7608           size += DWARF_OFFSET_SIZE;
7609           break;
7610         case dw_val_class_lbl_id:
7611           size += DWARF2_ADDR_SIZE;
7612           break;
7613         case dw_val_class_lineptr:
7614         case dw_val_class_macptr:
7615           size += DWARF_OFFSET_SIZE;
7616           break;
7617         case dw_val_class_str:
7618           if (AT_string_form (a) == DW_FORM_strp)
7619             size += DWARF_OFFSET_SIZE;
7620           else
7621             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7622           break;
7623         case dw_val_class_file:
7624           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7625           break;
7626         case dw_val_class_data8:
7627           size += 8;
7628           break;
7629         case dw_val_class_vms_delta:
7630           size += DWARF_OFFSET_SIZE;
7631           break;
7632         default:
7633           gcc_unreachable ();
7634         }
7635     }
7636
7637   return size;
7638 }
7639
7640 /* Size the debugging information associated with a given DIE.  Visits the
7641    DIE's children recursively.  Updates the global variable next_die_offset, on
7642    each time through.  Uses the current value of next_die_offset to update the
7643    die_offset field in each DIE.  */
7644
7645 static void
7646 calc_die_sizes (dw_die_ref die)
7647 {
7648   dw_die_ref c;
7649
7650   gcc_assert (die->die_offset == 0
7651               || (unsigned long int) die->die_offset == next_die_offset);
7652   die->die_offset = next_die_offset;
7653   next_die_offset += size_of_die (die);
7654
7655   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7656
7657   if (die->die_child != NULL)
7658     /* Count the null byte used to terminate sibling lists.  */
7659     next_die_offset += 1;
7660 }
7661
7662 /* Size just the base type children at the start of the CU.
7663    This is needed because build_abbrev needs to size locs
7664    and sizing of type based stack ops needs to know die_offset
7665    values for the base types.  */
7666
7667 static void
7668 calc_base_type_die_sizes (void)
7669 {
7670   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7671   unsigned int i;
7672   dw_die_ref base_type;
7673 #if ENABLE_ASSERT_CHECKING
7674   dw_die_ref prev = comp_unit_die ()->die_child;
7675 #endif
7676
7677   die_offset += size_of_die (comp_unit_die ());
7678   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7679     {
7680 #if ENABLE_ASSERT_CHECKING
7681       gcc_assert (base_type->die_offset == 0
7682                   && prev->die_sib == base_type
7683                   && base_type->die_child == NULL
7684                   && base_type->die_abbrev);
7685       prev = base_type;
7686 #endif
7687       base_type->die_offset = die_offset;
7688       die_offset += size_of_die (base_type);
7689     }
7690 }
7691
7692 /* Set the marks for a die and its children.  We do this so
7693    that we know whether or not a reference needs to use FORM_ref_addr; only
7694    DIEs in the same CU will be marked.  We used to clear out the offset
7695    and use that as the flag, but ran into ordering problems.  */
7696
7697 static void
7698 mark_dies (dw_die_ref die)
7699 {
7700   dw_die_ref c;
7701
7702   gcc_assert (!die->die_mark);
7703
7704   die->die_mark = 1;
7705   FOR_EACH_CHILD (die, c, mark_dies (c));
7706 }
7707
7708 /* Clear the marks for a die and its children.  */
7709
7710 static void
7711 unmark_dies (dw_die_ref die)
7712 {
7713   dw_die_ref c;
7714
7715   if (! use_debug_types)
7716     gcc_assert (die->die_mark);
7717
7718   die->die_mark = 0;
7719   FOR_EACH_CHILD (die, c, unmark_dies (c));
7720 }
7721
7722 /* Clear the marks for a die, its children and referred dies.  */
7723
7724 static void
7725 unmark_all_dies (dw_die_ref die)
7726 {
7727   dw_die_ref c;
7728   dw_attr_ref a;
7729   unsigned ix;
7730
7731   if (!die->die_mark)
7732     return;
7733   die->die_mark = 0;
7734
7735   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7736
7737   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7738     if (AT_class (a) == dw_val_class_die_ref)
7739       unmark_all_dies (AT_ref (a));
7740 }
7741
7742 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7743    generated for the compilation unit.  */
7744
7745 static unsigned long
7746 size_of_pubnames (VEC (pubname_entry, gc) * names)
7747 {
7748   unsigned long size;
7749   unsigned i;
7750   pubname_ref p;
7751
7752   size = DWARF_PUBNAMES_HEADER_SIZE;
7753   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7754     if (names != pubtype_table
7755         || p->die->die_offset != 0
7756         || !flag_eliminate_unused_debug_types)
7757       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7758
7759   size += DWARF_OFFSET_SIZE;
7760   return size;
7761 }
7762
7763 /* Return the size of the information in the .debug_aranges section.  */
7764
7765 static unsigned long
7766 size_of_aranges (void)
7767 {
7768   unsigned long size;
7769
7770   size = DWARF_ARANGES_HEADER_SIZE;
7771
7772   /* Count the address/length pair for this compilation unit.  */
7773   if (text_section_used)
7774     size += 2 * DWARF2_ADDR_SIZE;
7775   if (cold_text_section_used)
7776     size += 2 * DWARF2_ADDR_SIZE;
7777   if (have_multiple_function_sections)
7778     {
7779       unsigned fde_idx;
7780       dw_fde_ref fde;
7781
7782       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7783         {
7784           if (!fde->in_std_section)
7785             size += 2 * DWARF2_ADDR_SIZE;
7786           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7787             size += 2 * DWARF2_ADDR_SIZE;
7788         }
7789     }
7790
7791   /* Count the two zero words used to terminated the address range table.  */
7792   size += 2 * DWARF2_ADDR_SIZE;
7793   return size;
7794 }
7795 \f
7796 /* Select the encoding of an attribute value.  */
7797
7798 static enum dwarf_form
7799 value_format (dw_attr_ref a)
7800 {
7801   switch (a->dw_attr_val.val_class)
7802     {
7803     case dw_val_class_addr:
7804       /* Only very few attributes allow DW_FORM_addr.  */
7805       switch (a->dw_attr)
7806         {
7807         case DW_AT_low_pc:
7808         case DW_AT_high_pc:
7809         case DW_AT_entry_pc:
7810         case DW_AT_trampoline:
7811           return DW_FORM_addr;
7812         default:
7813           break;
7814         }
7815       switch (DWARF2_ADDR_SIZE)
7816         {
7817         case 1:
7818           return DW_FORM_data1;
7819         case 2:
7820           return DW_FORM_data2;
7821         case 4:
7822           return DW_FORM_data4;
7823         case 8:
7824           return DW_FORM_data8;
7825         default:
7826           gcc_unreachable ();
7827         }
7828     case dw_val_class_range_list:
7829     case dw_val_class_loc_list:
7830       if (dwarf_version >= 4)
7831         return DW_FORM_sec_offset;
7832       /* FALLTHRU */
7833     case dw_val_class_vms_delta:
7834     case dw_val_class_offset:
7835       switch (DWARF_OFFSET_SIZE)
7836         {
7837         case 4:
7838           return DW_FORM_data4;
7839         case 8:
7840           return DW_FORM_data8;
7841         default:
7842           gcc_unreachable ();
7843         }
7844     case dw_val_class_loc:
7845       if (dwarf_version >= 4)
7846         return DW_FORM_exprloc;
7847       switch (constant_size (size_of_locs (AT_loc (a))))
7848         {
7849         case 1:
7850           return DW_FORM_block1;
7851         case 2:
7852           return DW_FORM_block2;
7853         default:
7854           gcc_unreachable ();
7855         }
7856     case dw_val_class_const:
7857       return DW_FORM_sdata;
7858     case dw_val_class_unsigned_const:
7859       switch (constant_size (AT_unsigned (a)))
7860         {
7861         case 1:
7862           return DW_FORM_data1;
7863         case 2:
7864           return DW_FORM_data2;
7865         case 4:
7866           return DW_FORM_data4;
7867         case 8:
7868           return DW_FORM_data8;
7869         default:
7870           gcc_unreachable ();
7871         }
7872     case dw_val_class_const_double:
7873       switch (HOST_BITS_PER_WIDE_INT)
7874         {
7875         case 8:
7876           return DW_FORM_data2;
7877         case 16:
7878           return DW_FORM_data4;
7879         case 32:
7880           return DW_FORM_data8;
7881         case 64:
7882         default:
7883           return DW_FORM_block1;
7884         }
7885     case dw_val_class_vec:
7886       switch (constant_size (a->dw_attr_val.v.val_vec.length
7887                              * a->dw_attr_val.v.val_vec.elt_size))
7888         {
7889         case 1:
7890           return DW_FORM_block1;
7891         case 2:
7892           return DW_FORM_block2;
7893         case 4:
7894           return DW_FORM_block4;
7895         default:
7896           gcc_unreachable ();
7897         }
7898     case dw_val_class_flag:
7899       if (dwarf_version >= 4)
7900         {
7901           /* Currently all add_AT_flag calls pass in 1 as last argument,
7902              so DW_FORM_flag_present can be used.  If that ever changes,
7903              we'll need to use DW_FORM_flag and have some optimization
7904              in build_abbrev_table that will change those to
7905              DW_FORM_flag_present if it is set to 1 in all DIEs using
7906              the same abbrev entry.  */
7907           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7908           return DW_FORM_flag_present;
7909         }
7910       return DW_FORM_flag;
7911     case dw_val_class_die_ref:
7912       if (AT_ref_external (a))
7913         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7914       else
7915         return DW_FORM_ref;
7916     case dw_val_class_fde_ref:
7917       return DW_FORM_data;
7918     case dw_val_class_lbl_id:
7919       return DW_FORM_addr;
7920     case dw_val_class_lineptr:
7921     case dw_val_class_macptr:
7922       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7923     case dw_val_class_str:
7924       return AT_string_form (a);
7925     case dw_val_class_file:
7926       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7927         {
7928         case 1:
7929           return DW_FORM_data1;
7930         case 2:
7931           return DW_FORM_data2;
7932         case 4:
7933           return DW_FORM_data4;
7934         default:
7935           gcc_unreachable ();
7936         }
7937
7938     case dw_val_class_data8:
7939       return DW_FORM_data8;
7940
7941     default:
7942       gcc_unreachable ();
7943     }
7944 }
7945
7946 /* Output the encoding of an attribute value.  */
7947
7948 static void
7949 output_value_format (dw_attr_ref a)
7950 {
7951   enum dwarf_form form = value_format (a);
7952
7953   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7954 }
7955
7956 /* Output the .debug_abbrev section which defines the DIE abbreviation
7957    table.  */
7958
7959 static void
7960 output_abbrev_section (void)
7961 {
7962   unsigned long abbrev_id;
7963
7964   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7965     {
7966       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7967       unsigned ix;
7968       dw_attr_ref a_attr;
7969
7970       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7971       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7972                                    dwarf_tag_name (abbrev->die_tag));
7973
7974       if (abbrev->die_child != NULL)
7975         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7976       else
7977         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7978
7979       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7980            ix++)
7981         {
7982           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7983                                        dwarf_attr_name (a_attr->dw_attr));
7984           output_value_format (a_attr);
7985         }
7986
7987       dw2_asm_output_data (1, 0, NULL);
7988       dw2_asm_output_data (1, 0, NULL);
7989     }
7990
7991   /* Terminate the table.  */
7992   dw2_asm_output_data (1, 0, NULL);
7993 }
7994
7995 /* Output a symbol we can use to refer to this DIE from another CU.  */
7996
7997 static inline void
7998 output_die_symbol (dw_die_ref die)
7999 {
8000   char *sym = die->die_id.die_symbol;
8001
8002   if (sym == 0)
8003     return;
8004
8005   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8006     /* We make these global, not weak; if the target doesn't support
8007        .linkonce, it doesn't support combining the sections, so debugging
8008        will break.  */
8009     targetm.asm_out.globalize_label (asm_out_file, sym);
8010
8011   ASM_OUTPUT_LABEL (asm_out_file, sym);
8012 }
8013
8014 /* Return a new location list, given the begin and end range, and the
8015    expression.  */
8016
8017 static inline dw_loc_list_ref
8018 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8019               const char *section)
8020 {
8021   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8022
8023   retlist->begin = begin;
8024   retlist->end = end;
8025   retlist->expr = expr;
8026   retlist->section = section;
8027
8028   return retlist;
8029 }
8030
8031 /* Generate a new internal symbol for this location list node, if it
8032    hasn't got one yet.  */
8033
8034 static inline void
8035 gen_llsym (dw_loc_list_ref list)
8036 {
8037   gcc_assert (!list->ll_symbol);
8038   list->ll_symbol = gen_internal_sym ("LLST");
8039 }
8040
8041 /* Output the location list given to us.  */
8042
8043 static void
8044 output_loc_list (dw_loc_list_ref list_head)
8045 {
8046   dw_loc_list_ref curr = list_head;
8047
8048   if (list_head->emitted)
8049     return;
8050   list_head->emitted = true;
8051
8052   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8053
8054   /* Walk the location list, and output each range + expression.  */
8055   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8056     {
8057       unsigned long size;
8058       /* Don't output an entry that starts and ends at the same address.  */
8059       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8060         continue;
8061       if (!have_multiple_function_sections)
8062         {
8063           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8064                                 "Location list begin address (%s)",
8065                                 list_head->ll_symbol);
8066           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8067                                 "Location list end address (%s)",
8068                                 list_head->ll_symbol);
8069         }
8070       else
8071         {
8072           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8073                                "Location list begin address (%s)",
8074                                list_head->ll_symbol);
8075           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8076                                "Location list end address (%s)",
8077                                list_head->ll_symbol);
8078         }
8079       size = size_of_locs (curr->expr);
8080
8081       /* Output the block length for this list of location operations.  */
8082       gcc_assert (size <= 0xffff);
8083       dw2_asm_output_data (2, size, "%s", "Location expression size");
8084
8085       output_loc_sequence (curr->expr, -1);
8086     }
8087
8088   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8089                        "Location list terminator begin (%s)",
8090                        list_head->ll_symbol);
8091   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8092                        "Location list terminator end (%s)",
8093                        list_head->ll_symbol);
8094 }
8095
8096 /* Output a type signature.  */
8097
8098 static inline void
8099 output_signature (const char *sig, const char *name)
8100 {
8101   int i;
8102
8103   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8104     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8105 }
8106
8107 /* Output the DIE and its attributes.  Called recursively to generate
8108    the definitions of each child DIE.  */
8109
8110 static void
8111 output_die (dw_die_ref die)
8112 {
8113   dw_attr_ref a;
8114   dw_die_ref c;
8115   unsigned long size;
8116   unsigned ix;
8117
8118   /* If someone in another CU might refer to us, set up a symbol for
8119      them to point to.  */
8120   if (! use_debug_types && die->die_id.die_symbol)
8121     output_die_symbol (die);
8122
8123   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8124                                (unsigned long)die->die_offset,
8125                                dwarf_tag_name (die->die_tag));
8126
8127   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8128     {
8129       const char *name = dwarf_attr_name (a->dw_attr);
8130
8131       switch (AT_class (a))
8132         {
8133         case dw_val_class_addr:
8134           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8135           break;
8136
8137         case dw_val_class_offset:
8138           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8139                                "%s", name);
8140           break;
8141
8142         case dw_val_class_range_list:
8143           {
8144             char *p = strchr (ranges_section_label, '\0');
8145
8146             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8147                      a->dw_attr_val.v.val_offset);
8148             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8149                                    debug_ranges_section, "%s", name);
8150             *p = '\0';
8151           }
8152           break;
8153
8154         case dw_val_class_loc:
8155           size = size_of_locs (AT_loc (a));
8156
8157           /* Output the block length for this list of location operations.  */
8158           if (dwarf_version >= 4)
8159             dw2_asm_output_data_uleb128 (size, "%s", name);
8160           else
8161             dw2_asm_output_data (constant_size (size), size, "%s", name);
8162
8163           output_loc_sequence (AT_loc (a), -1);
8164           break;
8165
8166         case dw_val_class_const:
8167           /* ??? It would be slightly more efficient to use a scheme like is
8168              used for unsigned constants below, but gdb 4.x does not sign
8169              extend.  Gdb 5.x does sign extend.  */
8170           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8171           break;
8172
8173         case dw_val_class_unsigned_const:
8174           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8175                                AT_unsigned (a), "%s", name);
8176           break;
8177
8178         case dw_val_class_const_double:
8179           {
8180             unsigned HOST_WIDE_INT first, second;
8181
8182             if (HOST_BITS_PER_WIDE_INT >= 64)
8183               dw2_asm_output_data (1,
8184                                    2 * HOST_BITS_PER_WIDE_INT
8185                                    / HOST_BITS_PER_CHAR,
8186                                    NULL);
8187
8188             if (WORDS_BIG_ENDIAN)
8189               {
8190                 first = a->dw_attr_val.v.val_double.high;
8191                 second = a->dw_attr_val.v.val_double.low;
8192               }
8193             else
8194               {
8195                 first = a->dw_attr_val.v.val_double.low;
8196                 second = a->dw_attr_val.v.val_double.high;
8197               }
8198
8199             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8200                                  first, name);
8201             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8202                                  second, NULL);
8203           }
8204           break;
8205
8206         case dw_val_class_vec:
8207           {
8208             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8209             unsigned int len = a->dw_attr_val.v.val_vec.length;
8210             unsigned int i;
8211             unsigned char *p;
8212
8213             dw2_asm_output_data (constant_size (len * elt_size),
8214                                  len * elt_size, "%s", name);
8215             if (elt_size > sizeof (HOST_WIDE_INT))
8216               {
8217                 elt_size /= 2;
8218                 len *= 2;
8219               }
8220             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8221                  i < len;
8222                  i++, p += elt_size)
8223               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8224                                    "fp or vector constant word %u", i);
8225             break;
8226           }
8227
8228         case dw_val_class_flag:
8229           if (dwarf_version >= 4)
8230             {
8231               /* Currently all add_AT_flag calls pass in 1 as last argument,
8232                  so DW_FORM_flag_present can be used.  If that ever changes,
8233                  we'll need to use DW_FORM_flag and have some optimization
8234                  in build_abbrev_table that will change those to
8235                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8236                  the same abbrev entry.  */
8237               gcc_assert (AT_flag (a) == 1);
8238               if (flag_debug_asm)
8239                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8240                          ASM_COMMENT_START, name);
8241               break;
8242             }
8243           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8244           break;
8245
8246         case dw_val_class_loc_list:
8247           {
8248             char *sym = AT_loc_list (a)->ll_symbol;
8249
8250             gcc_assert (sym);
8251             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8252                                    "%s", name);
8253           }
8254           break;
8255
8256         case dw_val_class_die_ref:
8257           if (AT_ref_external (a))
8258             {
8259               if (use_debug_types)
8260                 {
8261                   comdat_type_node_ref type_node =
8262                     AT_ref (a)->die_id.die_type_node;
8263
8264                   gcc_assert (type_node);
8265                   output_signature (type_node->signature, name);
8266                 }
8267               else
8268                 {
8269                   char *sym = AT_ref (a)->die_id.die_symbol;
8270                   int size;
8271
8272                   gcc_assert (sym);
8273                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8274                      length, whereas in DWARF3 it's always sized as an
8275                      offset.  */
8276                   if (dwarf_version == 2)
8277                     size = DWARF2_ADDR_SIZE;
8278                   else
8279                     size = DWARF_OFFSET_SIZE;
8280                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8281                                          name);
8282                 }
8283             }
8284           else
8285             {
8286               gcc_assert (AT_ref (a)->die_offset);
8287               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8288                                    "%s", name);
8289             }
8290           break;
8291
8292         case dw_val_class_fde_ref:
8293           {
8294             char l1[20];
8295
8296             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8297                                          a->dw_attr_val.v.val_fde_index * 2);
8298             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8299                                    "%s", name);
8300           }
8301           break;
8302
8303         case dw_val_class_vms_delta:
8304           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8305                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8306                                     "%s", name);
8307           break;
8308
8309         case dw_val_class_lbl_id:
8310           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8311           break;
8312
8313         case dw_val_class_lineptr:
8314           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8315                                  debug_line_section, "%s", name);
8316           break;
8317
8318         case dw_val_class_macptr:
8319           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8320                                  debug_macinfo_section, "%s", name);
8321           break;
8322
8323         case dw_val_class_str:
8324           if (AT_string_form (a) == DW_FORM_strp)
8325             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8326                                    a->dw_attr_val.v.val_str->label,
8327                                    debug_str_section,
8328                                    "%s: \"%s\"", name, AT_string (a));
8329           else
8330             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8331           break;
8332
8333         case dw_val_class_file:
8334           {
8335             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8336
8337             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8338                                  a->dw_attr_val.v.val_file->filename);
8339             break;
8340           }
8341
8342         case dw_val_class_data8:
8343           {
8344             int i;
8345
8346             for (i = 0; i < 8; i++)
8347               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8348                                    i == 0 ? "%s" : NULL, name);
8349             break;
8350           }
8351
8352         default:
8353           gcc_unreachable ();
8354         }
8355     }
8356
8357   FOR_EACH_CHILD (die, c, output_die (c));
8358
8359   /* Add null byte to terminate sibling list.  */
8360   if (die->die_child != NULL)
8361     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8362                          (unsigned long) die->die_offset);
8363 }
8364
8365 /* Output the compilation unit that appears at the beginning of the
8366    .debug_info section, and precedes the DIE descriptions.  */
8367
8368 static void
8369 output_compilation_unit_header (void)
8370 {
8371   int ver = dwarf_version;
8372
8373   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8374     dw2_asm_output_data (4, 0xffffffff,
8375       "Initial length escape value indicating 64-bit DWARF extension");
8376   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8377                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8378                        "Length of Compilation Unit Info");
8379   dw2_asm_output_data (2, ver, "DWARF version number");
8380   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8381                          debug_abbrev_section,
8382                          "Offset Into Abbrev. Section");
8383   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8384 }
8385
8386 /* Output the compilation unit DIE and its children.  */
8387
8388 static void
8389 output_comp_unit (dw_die_ref die, int output_if_empty)
8390 {
8391   const char *secname;
8392   char *oldsym, *tmp;
8393
8394   /* Unless we are outputting main CU, we may throw away empty ones.  */
8395   if (!output_if_empty && die->die_child == NULL)
8396     return;
8397
8398   /* Even if there are no children of this DIE, we must output the information
8399      about the compilation unit.  Otherwise, on an empty translation unit, we
8400      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8401      will then complain when examining the file.  First mark all the DIEs in
8402      this CU so we know which get local refs.  */
8403   mark_dies (die);
8404
8405   build_abbrev_table (die);
8406
8407   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8408   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8409   calc_die_sizes (die);
8410
8411   oldsym = die->die_id.die_symbol;
8412   if (oldsym)
8413     {
8414       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8415
8416       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8417       secname = tmp;
8418       die->die_id.die_symbol = NULL;
8419       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8420     }
8421   else
8422     {
8423       switch_to_section (debug_info_section);
8424       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8425       info_section_emitted = true;
8426     }
8427
8428   /* Output debugging information.  */
8429   output_compilation_unit_header ();
8430   output_die (die);
8431
8432   /* Leave the marks on the main CU, so we can check them in
8433      output_pubnames.  */
8434   if (oldsym)
8435     {
8436       unmark_dies (die);
8437       die->die_id.die_symbol = oldsym;
8438     }
8439 }
8440
8441 /* Output a comdat type unit DIE and its children.  */
8442
8443 static void
8444 output_comdat_type_unit (comdat_type_node *node)
8445 {
8446   const char *secname;
8447   char *tmp;
8448   int i;
8449 #if defined (OBJECT_FORMAT_ELF)
8450   tree comdat_key;
8451 #endif
8452
8453   /* First mark all the DIEs in this CU so we know which get local refs.  */
8454   mark_dies (node->root_die);
8455
8456   build_abbrev_table (node->root_die);
8457
8458   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8459   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8460   calc_die_sizes (node->root_die);
8461
8462 #if defined (OBJECT_FORMAT_ELF)
8463   secname = ".debug_types";
8464   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8465   sprintf (tmp, "wt.");
8466   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8467     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8468   comdat_key = get_identifier (tmp);
8469   targetm.asm_out.named_section (secname,
8470                                  SECTION_DEBUG | SECTION_LINKONCE,
8471                                  comdat_key);
8472 #else
8473   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8474   sprintf (tmp, ".gnu.linkonce.wt.");
8475   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8476     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8477   secname = tmp;
8478   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8479 #endif
8480
8481   /* Output debugging information.  */
8482   output_compilation_unit_header ();
8483   output_signature (node->signature, "Type Signature");
8484   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8485                        "Offset to Type DIE");
8486   output_die (node->root_die);
8487
8488   unmark_dies (node->root_die);
8489 }
8490
8491 /* Return the DWARF2/3 pubname associated with a decl.  */
8492
8493 static const char *
8494 dwarf2_name (tree decl, int scope)
8495 {
8496   if (DECL_NAMELESS (decl))
8497     return NULL;
8498   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8499 }
8500
8501 /* Add a new entry to .debug_pubnames if appropriate.  */
8502
8503 static void
8504 add_pubname_string (const char *str, dw_die_ref die)
8505 {
8506   if (targetm.want_debug_pub_sections)
8507     {
8508       pubname_entry e;
8509
8510       e.die = die;
8511       e.name = xstrdup (str);
8512       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8513     }
8514 }
8515
8516 static void
8517 add_pubname (tree decl, dw_die_ref die)
8518 {
8519   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8520     {
8521       const char *name = dwarf2_name (decl, 1);
8522       if (name)
8523         add_pubname_string (name, die);
8524     }
8525 }
8526
8527 /* Add a new entry to .debug_pubtypes if appropriate.  */
8528
8529 static void
8530 add_pubtype (tree decl, dw_die_ref die)
8531 {
8532   pubname_entry e;
8533
8534   if (!targetm.want_debug_pub_sections)
8535     return;
8536
8537   e.name = NULL;
8538   if ((TREE_PUBLIC (decl)
8539        || is_cu_die (die->die_parent))
8540       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8541     {
8542       e.die = die;
8543       if (TYPE_P (decl))
8544         {
8545           if (TYPE_NAME (decl))
8546             {
8547               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8548                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8549               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8550                        && DECL_NAME (TYPE_NAME (decl)))
8551                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8552               else
8553                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8554             }
8555         }
8556       else
8557         {
8558           e.name = dwarf2_name (decl, 1);
8559           if (e.name)
8560             e.name = xstrdup (e.name);
8561         }
8562
8563       /* If we don't have a name for the type, there's no point in adding
8564          it to the table.  */
8565       if (e.name && e.name[0] != '\0')
8566         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8567     }
8568 }
8569
8570 /* Output the public names table used to speed up access to externally
8571    visible names; or the public types table used to find type definitions.  */
8572
8573 static void
8574 output_pubnames (VEC (pubname_entry, gc) * names)
8575 {
8576   unsigned i;
8577   unsigned long pubnames_length = size_of_pubnames (names);
8578   pubname_ref pub;
8579
8580   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8581     dw2_asm_output_data (4, 0xffffffff,
8582       "Initial length escape value indicating 64-bit DWARF extension");
8583   if (names == pubname_table)
8584     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8585                          "Length of Public Names Info");
8586   else
8587     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8588                          "Length of Public Type Names Info");
8589   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8590   dw2_asm_output_data (2, 2, "DWARF Version");
8591   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8592                          debug_info_section,
8593                          "Offset of Compilation Unit Info");
8594   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8595                        "Compilation Unit Length");
8596
8597   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8598     {
8599       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8600       if (names == pubname_table)
8601         gcc_assert (pub->die->die_mark);
8602
8603       if (names != pubtype_table
8604           || pub->die->die_offset != 0
8605           || !flag_eliminate_unused_debug_types)
8606         {
8607           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8608                                "DIE offset");
8609
8610           dw2_asm_output_nstring (pub->name, -1, "external name");
8611         }
8612     }
8613
8614   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8615 }
8616
8617 /* Output the information that goes into the .debug_aranges table.
8618    Namely, define the beginning and ending address range of the
8619    text section generated for this compilation unit.  */
8620
8621 static void
8622 output_aranges (unsigned long aranges_length)
8623 {
8624   unsigned i;
8625
8626   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8627     dw2_asm_output_data (4, 0xffffffff,
8628       "Initial length escape value indicating 64-bit DWARF extension");
8629   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8630                        "Length of Address Ranges Info");
8631   /* Version number for aranges is still 2, even in DWARF3.  */
8632   dw2_asm_output_data (2, 2, "DWARF Version");
8633   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8634                          debug_info_section,
8635                          "Offset of Compilation Unit Info");
8636   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8637   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8638
8639   /* We need to align to twice the pointer size here.  */
8640   if (DWARF_ARANGES_PAD_SIZE)
8641     {
8642       /* Pad using a 2 byte words so that padding is correct for any
8643          pointer size.  */
8644       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8645                            2 * DWARF2_ADDR_SIZE);
8646       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8647         dw2_asm_output_data (2, 0, NULL);
8648     }
8649
8650   /* It is necessary not to output these entries if the sections were
8651      not used; if the sections were not used, the length will be 0 and
8652      the address may end up as 0 if the section is discarded by ld
8653      --gc-sections, leaving an invalid (0, 0) entry that can be
8654      confused with the terminator.  */
8655   if (text_section_used)
8656     {
8657       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8658       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8659                             text_section_label, "Length");
8660     }
8661   if (cold_text_section_used)
8662     {
8663       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8664                            "Address");
8665       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8666                             cold_text_section_label, "Length");
8667     }
8668
8669   if (have_multiple_function_sections)
8670     {
8671       unsigned fde_idx;
8672       dw_fde_ref fde;
8673
8674       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8675         {
8676           if (!fde->in_std_section)
8677             {
8678               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8679                                    "Address");
8680               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8681                                     fde->dw_fde_begin, "Length");
8682             }
8683           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8684             {
8685               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8686                                    "Address");
8687               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8688                                     fde->dw_fde_second_begin, "Length");
8689             }
8690         }
8691     }
8692
8693   /* Output the terminator words.  */
8694   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8695   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8696 }
8697
8698 /* Add a new entry to .debug_ranges.  Return the offset at which it
8699    was placed.  */
8700
8701 static unsigned int
8702 add_ranges_num (int num)
8703 {
8704   unsigned int in_use = ranges_table_in_use;
8705
8706   if (in_use == ranges_table_allocated)
8707     {
8708       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8709       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8710                                     ranges_table_allocated);
8711       memset (ranges_table + ranges_table_in_use, 0,
8712               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8713     }
8714
8715   ranges_table[in_use].num = num;
8716   ranges_table_in_use = in_use + 1;
8717
8718   return in_use * 2 * DWARF2_ADDR_SIZE;
8719 }
8720
8721 /* Add a new entry to .debug_ranges corresponding to a block, or a
8722    range terminator if BLOCK is NULL.  */
8723
8724 static unsigned int
8725 add_ranges (const_tree block)
8726 {
8727   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8728 }
8729
8730 /* Add a new entry to .debug_ranges corresponding to a pair of
8731    labels.  */
8732
8733 static void
8734 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8735                       bool *added)
8736 {
8737   unsigned int in_use = ranges_by_label_in_use;
8738   unsigned int offset;
8739
8740   if (in_use == ranges_by_label_allocated)
8741     {
8742       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8743       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8744                                        ranges_by_label,
8745                                        ranges_by_label_allocated);
8746       memset (ranges_by_label + ranges_by_label_in_use, 0,
8747               RANGES_TABLE_INCREMENT
8748               * sizeof (struct dw_ranges_by_label_struct));
8749     }
8750
8751   ranges_by_label[in_use].begin = begin;
8752   ranges_by_label[in_use].end = end;
8753   ranges_by_label_in_use = in_use + 1;
8754
8755   offset = add_ranges_num (-(int)in_use - 1);
8756   if (!*added)
8757     {
8758       add_AT_range_list (die, DW_AT_ranges, offset);
8759       *added = true;
8760     }
8761 }
8762
8763 static void
8764 output_ranges (void)
8765 {
8766   unsigned i;
8767   static const char *const start_fmt = "Offset %#x";
8768   const char *fmt = start_fmt;
8769
8770   for (i = 0; i < ranges_table_in_use; i++)
8771     {
8772       int block_num = ranges_table[i].num;
8773
8774       if (block_num > 0)
8775         {
8776           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8777           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8778
8779           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8780           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8781
8782           /* If all code is in the text section, then the compilation
8783              unit base address defaults to DW_AT_low_pc, which is the
8784              base of the text section.  */
8785           if (!have_multiple_function_sections)
8786             {
8787               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8788                                     text_section_label,
8789                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8790               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8791                                     text_section_label, NULL);
8792             }
8793
8794           /* Otherwise, the compilation unit base address is zero,
8795              which allows us to use absolute addresses, and not worry
8796              about whether the target supports cross-section
8797              arithmetic.  */
8798           else
8799             {
8800               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8801                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8802               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8803             }
8804
8805           fmt = NULL;
8806         }
8807
8808       /* Negative block_num stands for an index into ranges_by_label.  */
8809       else if (block_num < 0)
8810         {
8811           int lab_idx = - block_num - 1;
8812
8813           if (!have_multiple_function_sections)
8814             {
8815               gcc_unreachable ();
8816 #if 0
8817               /* If we ever use add_ranges_by_labels () for a single
8818                  function section, all we have to do is to take out
8819                  the #if 0 above.  */
8820               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8821                                     ranges_by_label[lab_idx].begin,
8822                                     text_section_label,
8823                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8824               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8825                                     ranges_by_label[lab_idx].end,
8826                                     text_section_label, NULL);
8827 #endif
8828             }
8829           else
8830             {
8831               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8832                                    ranges_by_label[lab_idx].begin,
8833                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8834               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8835                                    ranges_by_label[lab_idx].end,
8836                                    NULL);
8837             }
8838         }
8839       else
8840         {
8841           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8842           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8843           fmt = start_fmt;
8844         }
8845     }
8846 }
8847
8848 /* Data structure containing information about input files.  */
8849 struct file_info
8850 {
8851   const char *path;     /* Complete file name.  */
8852   const char *fname;    /* File name part.  */
8853   int length;           /* Length of entire string.  */
8854   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8855   int dir_idx;          /* Index in directory table.  */
8856 };
8857
8858 /* Data structure containing information about directories with source
8859    files.  */
8860 struct dir_info
8861 {
8862   const char *path;     /* Path including directory name.  */
8863   int length;           /* Path length.  */
8864   int prefix;           /* Index of directory entry which is a prefix.  */
8865   int count;            /* Number of files in this directory.  */
8866   int dir_idx;          /* Index of directory used as base.  */
8867 };
8868
8869 /* Callback function for file_info comparison.  We sort by looking at
8870    the directories in the path.  */
8871
8872 static int
8873 file_info_cmp (const void *p1, const void *p2)
8874 {
8875   const struct file_info *const s1 = (const struct file_info *) p1;
8876   const struct file_info *const s2 = (const struct file_info *) p2;
8877   const unsigned char *cp1;
8878   const unsigned char *cp2;
8879
8880   /* Take care of file names without directories.  We need to make sure that
8881      we return consistent values to qsort since some will get confused if
8882      we return the same value when identical operands are passed in opposite
8883      orders.  So if neither has a directory, return 0 and otherwise return
8884      1 or -1 depending on which one has the directory.  */
8885   if ((s1->path == s1->fname || s2->path == s2->fname))
8886     return (s2->path == s2->fname) - (s1->path == s1->fname);
8887
8888   cp1 = (const unsigned char *) s1->path;
8889   cp2 = (const unsigned char *) s2->path;
8890
8891   while (1)
8892     {
8893       ++cp1;
8894       ++cp2;
8895       /* Reached the end of the first path?  If so, handle like above.  */
8896       if ((cp1 == (const unsigned char *) s1->fname)
8897           || (cp2 == (const unsigned char *) s2->fname))
8898         return ((cp2 == (const unsigned char *) s2->fname)
8899                 - (cp1 == (const unsigned char *) s1->fname));
8900
8901       /* Character of current path component the same?  */
8902       else if (*cp1 != *cp2)
8903         return *cp1 - *cp2;
8904     }
8905 }
8906
8907 struct file_name_acquire_data
8908 {
8909   struct file_info *files;
8910   int used_files;
8911   int max_files;
8912 };
8913
8914 /* Traversal function for the hash table.  */
8915
8916 static int
8917 file_name_acquire (void ** slot, void *data)
8918 {
8919   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8920   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8921   struct file_info *fi;
8922   const char *f;
8923
8924   gcc_assert (fnad->max_files >= d->emitted_number);
8925
8926   if (! d->emitted_number)
8927     return 1;
8928
8929   gcc_assert (fnad->max_files != fnad->used_files);
8930
8931   fi = fnad->files + fnad->used_files++;
8932
8933   /* Skip all leading "./".  */
8934   f = d->filename;
8935   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8936     f += 2;
8937
8938   /* Create a new array entry.  */
8939   fi->path = f;
8940   fi->length = strlen (f);
8941   fi->file_idx = d;
8942
8943   /* Search for the file name part.  */
8944   f = strrchr (f, DIR_SEPARATOR);
8945 #if defined (DIR_SEPARATOR_2)
8946   {
8947     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8948
8949     if (g != NULL)
8950       {
8951         if (f == NULL || f < g)
8952           f = g;
8953       }
8954   }
8955 #endif
8956
8957   fi->fname = f == NULL ? fi->path : f + 1;
8958   return 1;
8959 }
8960
8961 /* Output the directory table and the file name table.  We try to minimize
8962    the total amount of memory needed.  A heuristic is used to avoid large
8963    slowdowns with many input files.  */
8964
8965 static void
8966 output_file_names (void)
8967 {
8968   struct file_name_acquire_data fnad;
8969   int numfiles;
8970   struct file_info *files;
8971   struct dir_info *dirs;
8972   int *saved;
8973   int *savehere;
8974   int *backmap;
8975   int ndirs;
8976   int idx_offset;
8977   int i;
8978
8979   if (!last_emitted_file)
8980     {
8981       dw2_asm_output_data (1, 0, "End directory table");
8982       dw2_asm_output_data (1, 0, "End file name table");
8983       return;
8984     }
8985
8986   numfiles = last_emitted_file->emitted_number;
8987
8988   /* Allocate the various arrays we need.  */
8989   files = XALLOCAVEC (struct file_info, numfiles);
8990   dirs = XALLOCAVEC (struct dir_info, numfiles);
8991
8992   fnad.files = files;
8993   fnad.used_files = 0;
8994   fnad.max_files = numfiles;
8995   htab_traverse (file_table, file_name_acquire, &fnad);
8996   gcc_assert (fnad.used_files == fnad.max_files);
8997
8998   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8999
9000   /* Find all the different directories used.  */
9001   dirs[0].path = files[0].path;
9002   dirs[0].length = files[0].fname - files[0].path;
9003   dirs[0].prefix = -1;
9004   dirs[0].count = 1;
9005   dirs[0].dir_idx = 0;
9006   files[0].dir_idx = 0;
9007   ndirs = 1;
9008
9009   for (i = 1; i < numfiles; i++)
9010     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9011         && memcmp (dirs[ndirs - 1].path, files[i].path,
9012                    dirs[ndirs - 1].length) == 0)
9013       {
9014         /* Same directory as last entry.  */
9015         files[i].dir_idx = ndirs - 1;
9016         ++dirs[ndirs - 1].count;
9017       }
9018     else
9019       {
9020         int j;
9021
9022         /* This is a new directory.  */
9023         dirs[ndirs].path = files[i].path;
9024         dirs[ndirs].length = files[i].fname - files[i].path;
9025         dirs[ndirs].count = 1;
9026         dirs[ndirs].dir_idx = ndirs;
9027         files[i].dir_idx = ndirs;
9028
9029         /* Search for a prefix.  */
9030         dirs[ndirs].prefix = -1;
9031         for (j = 0; j < ndirs; j++)
9032           if (dirs[j].length < dirs[ndirs].length
9033               && dirs[j].length > 1
9034               && (dirs[ndirs].prefix == -1
9035                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9036               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9037             dirs[ndirs].prefix = j;
9038
9039         ++ndirs;
9040       }
9041
9042   /* Now to the actual work.  We have to find a subset of the directories which
9043      allow expressing the file name using references to the directory table
9044      with the least amount of characters.  We do not do an exhaustive search
9045      where we would have to check out every combination of every single
9046      possible prefix.  Instead we use a heuristic which provides nearly optimal
9047      results in most cases and never is much off.  */
9048   saved = XALLOCAVEC (int, ndirs);
9049   savehere = XALLOCAVEC (int, ndirs);
9050
9051   memset (saved, '\0', ndirs * sizeof (saved[0]));
9052   for (i = 0; i < ndirs; i++)
9053     {
9054       int j;
9055       int total;
9056
9057       /* We can always save some space for the current directory.  But this
9058          does not mean it will be enough to justify adding the directory.  */
9059       savehere[i] = dirs[i].length;
9060       total = (savehere[i] - saved[i]) * dirs[i].count;
9061
9062       for (j = i + 1; j < ndirs; j++)
9063         {
9064           savehere[j] = 0;
9065           if (saved[j] < dirs[i].length)
9066             {
9067               /* Determine whether the dirs[i] path is a prefix of the
9068                  dirs[j] path.  */
9069               int k;
9070
9071               k = dirs[j].prefix;
9072               while (k != -1 && k != (int) i)
9073                 k = dirs[k].prefix;
9074
9075               if (k == (int) i)
9076                 {
9077                   /* Yes it is.  We can possibly save some memory by
9078                      writing the filenames in dirs[j] relative to
9079                      dirs[i].  */
9080                   savehere[j] = dirs[i].length;
9081                   total += (savehere[j] - saved[j]) * dirs[j].count;
9082                 }
9083             }
9084         }
9085
9086       /* Check whether we can save enough to justify adding the dirs[i]
9087          directory.  */
9088       if (total > dirs[i].length + 1)
9089         {
9090           /* It's worthwhile adding.  */
9091           for (j = i; j < ndirs; j++)
9092             if (savehere[j] > 0)
9093               {
9094                 /* Remember how much we saved for this directory so far.  */
9095                 saved[j] = savehere[j];
9096
9097                 /* Remember the prefix directory.  */
9098                 dirs[j].dir_idx = i;
9099               }
9100         }
9101     }
9102
9103   /* Emit the directory name table.  */
9104   idx_offset = dirs[0].length > 0 ? 1 : 0;
9105   for (i = 1 - idx_offset; i < ndirs; i++)
9106     dw2_asm_output_nstring (dirs[i].path,
9107                             dirs[i].length
9108                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9109                             "Directory Entry: %#x", i + idx_offset);
9110
9111   dw2_asm_output_data (1, 0, "End directory table");
9112
9113   /* We have to emit them in the order of emitted_number since that's
9114      used in the debug info generation.  To do this efficiently we
9115      generate a back-mapping of the indices first.  */
9116   backmap = XALLOCAVEC (int, numfiles);
9117   for (i = 0; i < numfiles; i++)
9118     backmap[files[i].file_idx->emitted_number - 1] = i;
9119
9120   /* Now write all the file names.  */
9121   for (i = 0; i < numfiles; i++)
9122     {
9123       int file_idx = backmap[i];
9124       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9125
9126 #ifdef VMS_DEBUGGING_INFO
9127 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9128
9129       /* Setting these fields can lead to debugger miscomparisons,
9130          but VMS Debug requires them to be set correctly.  */
9131
9132       int ver;
9133       long long cdt;
9134       long siz;
9135       int maxfilelen = strlen (files[file_idx].path)
9136                                + dirs[dir_idx].length
9137                                + MAX_VMS_VERSION_LEN + 1;
9138       char *filebuf = XALLOCAVEC (char, maxfilelen);
9139
9140       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9141       snprintf (filebuf, maxfilelen, "%s;%d",
9142                 files[file_idx].path + dirs[dir_idx].length, ver);
9143
9144       dw2_asm_output_nstring
9145         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9146
9147       /* Include directory index.  */
9148       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9149
9150       /* Modification time.  */
9151       dw2_asm_output_data_uleb128
9152         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9153           ? cdt : 0,
9154          NULL);
9155
9156       /* File length in bytes.  */
9157       dw2_asm_output_data_uleb128
9158         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9159           ? siz : 0,
9160          NULL);
9161 #else
9162       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9163                               "File Entry: %#x", (unsigned) i + 1);
9164
9165       /* Include directory index.  */
9166       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9167
9168       /* Modification time.  */
9169       dw2_asm_output_data_uleb128 (0, NULL);
9170
9171       /* File length in bytes.  */
9172       dw2_asm_output_data_uleb128 (0, NULL);
9173 #endif /* VMS_DEBUGGING_INFO */
9174     }
9175
9176   dw2_asm_output_data (1, 0, "End file name table");
9177 }
9178
9179
9180 /* Output one line number table into the .debug_line section.  */
9181
9182 static void
9183 output_one_line_info_table (dw_line_info_table *table)
9184 {
9185   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9186   unsigned int current_line = 1;
9187   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9188   dw_line_info_entry *ent;
9189   size_t i;
9190
9191   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9192     {
9193       switch (ent->opcode)
9194         {
9195         case LI_set_address:
9196           /* ??? Unfortunately, we have little choice here currently, and
9197              must always use the most general form.  GCC does not know the
9198              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9199              ports do have length attributes which will give an upper bound
9200              on the address range.  We could perhaps use length attributes
9201              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9202           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9203
9204           /* This can handle any delta.  This takes
9205              4+DWARF2_ADDR_SIZE bytes.  */
9206           dw2_asm_output_data (1, 0, "set address %s", line_label);
9207           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9208           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9209           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9210           break;
9211
9212         case LI_set_line:
9213           if (ent->val == current_line)
9214             {
9215               /* We still need to start a new row, so output a copy insn.  */
9216               dw2_asm_output_data (1, DW_LNS_copy,
9217                                    "copy line %u", current_line);
9218             }
9219           else
9220             {
9221               int line_offset = ent->val - current_line;
9222               int line_delta = line_offset - DWARF_LINE_BASE;
9223
9224               current_line = ent->val;
9225               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9226                 {
9227                   /* This can handle deltas from -10 to 234, using the current
9228                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9229                      This takes 1 byte.  */
9230                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9231                                        "line %u", current_line);
9232                 }
9233               else
9234                 {
9235                   /* This can handle any delta.  This takes at least 4 bytes,
9236                      depending on the value being encoded.  */
9237                   dw2_asm_output_data (1, DW_LNS_advance_line,
9238                                        "advance to line %u", current_line);
9239                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9240                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9241                 }
9242             }
9243           break;
9244
9245         case LI_set_file:
9246           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9247           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9248           break;
9249
9250         case LI_set_column:
9251           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9252           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9253           break;
9254
9255         case LI_negate_stmt:
9256           current_is_stmt = !current_is_stmt;
9257           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9258                                "is_stmt %d", current_is_stmt);
9259           break;
9260
9261         case LI_set_prologue_end:
9262           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9263                                "set prologue end");
9264           break;
9265           
9266         case LI_set_epilogue_begin:
9267           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9268                                "set epilogue begin");
9269           break;
9270
9271         case LI_set_discriminator:
9272           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9273           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9274           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9275           dw2_asm_output_data_uleb128 (ent->val, NULL);
9276           break;
9277         }
9278     }
9279
9280   /* Emit debug info for the address of the end of the table.  */
9281   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9282   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9283   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9284   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9285
9286   dw2_asm_output_data (1, 0, "end sequence");
9287   dw2_asm_output_data_uleb128 (1, NULL);
9288   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9289 }
9290
9291 /* Output the source line number correspondence information.  This
9292    information goes into the .debug_line section.  */
9293
9294 static void
9295 output_line_info (void)
9296 {
9297   char l1[20], l2[20], p1[20], p2[20];
9298   int ver = dwarf_version;
9299   bool saw_one = false;
9300   int opc;
9301
9302   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9303   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9304   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9305   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9306
9307   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9308     dw2_asm_output_data (4, 0xffffffff,
9309       "Initial length escape value indicating 64-bit DWARF extension");
9310   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9311                         "Length of Source Line Info");
9312   ASM_OUTPUT_LABEL (asm_out_file, l1);
9313
9314   dw2_asm_output_data (2, ver, "DWARF Version");
9315   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9316   ASM_OUTPUT_LABEL (asm_out_file, p1);
9317
9318   /* Define the architecture-dependent minimum instruction length (in bytes).
9319      In this implementation of DWARF, this field is used for information
9320      purposes only.  Since GCC generates assembly language, we have no
9321      a priori knowledge of how many instruction bytes are generated for each
9322      source line, and therefore can use only the DW_LNE_set_address and
9323      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9324      this as '1', which is "correct enough" for all architectures,
9325      and don't let the target override.  */
9326   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9327
9328   if (ver >= 4)
9329     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9330                          "Maximum Operations Per Instruction");
9331   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9332                        "Default is_stmt_start flag");
9333   dw2_asm_output_data (1, DWARF_LINE_BASE,
9334                        "Line Base Value (Special Opcodes)");
9335   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9336                        "Line Range Value (Special Opcodes)");
9337   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9338                        "Special Opcode Base");
9339
9340   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9341     {
9342       int n_op_args;
9343       switch (opc)
9344         {
9345         case DW_LNS_advance_pc:
9346         case DW_LNS_advance_line:
9347         case DW_LNS_set_file:
9348         case DW_LNS_set_column:
9349         case DW_LNS_fixed_advance_pc:
9350         case DW_LNS_set_isa:
9351           n_op_args = 1;
9352           break;
9353         default:
9354           n_op_args = 0;
9355           break;
9356         }
9357
9358       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9359                            opc, n_op_args);
9360     }
9361
9362   /* Write out the information about the files we use.  */
9363   output_file_names ();
9364   ASM_OUTPUT_LABEL (asm_out_file, p2);
9365
9366   if (separate_line_info)
9367     {
9368       dw_line_info_table *table;
9369       size_t i;
9370
9371       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9372         if (table->in_use)
9373           {
9374             output_one_line_info_table (table);
9375             saw_one = true;
9376           }
9377     }
9378   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9379     {
9380       output_one_line_info_table (cold_text_section_line_info);
9381       saw_one = true;
9382     }
9383
9384   /* ??? Some Darwin linkers crash on a .debug_line section with no
9385      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9386      sufficient -- the address column must also be initialized.
9387      Make sure to output at least one set_address/end_sequence pair,
9388      choosing .text since that section is always present.  */
9389   if (text_section_line_info->in_use || !saw_one)
9390     output_one_line_info_table (text_section_line_info);
9391
9392   /* Output the marker for the end of the line number info.  */
9393   ASM_OUTPUT_LABEL (asm_out_file, l2);
9394 }
9395 \f
9396 /* Given a pointer to a tree node for some base type, return a pointer to
9397    a DIE that describes the given type.
9398
9399    This routine must only be called for GCC type nodes that correspond to
9400    Dwarf base (fundamental) types.  */
9401
9402 static dw_die_ref
9403 base_type_die (tree type)
9404 {
9405   dw_die_ref base_type_result;
9406   enum dwarf_type encoding;
9407
9408   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9409     return 0;
9410
9411   /* If this is a subtype that should not be emitted as a subrange type,
9412      use the base type.  See subrange_type_for_debug_p.  */
9413   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9414     type = TREE_TYPE (type);
9415
9416   switch (TREE_CODE (type))
9417     {
9418     case INTEGER_TYPE:
9419       if ((dwarf_version >= 4 || !dwarf_strict)
9420           && TYPE_NAME (type)
9421           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9422           && DECL_IS_BUILTIN (TYPE_NAME (type))
9423           && DECL_NAME (TYPE_NAME (type)))
9424         {
9425           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9426           if (strcmp (name, "char16_t") == 0
9427               || strcmp (name, "char32_t") == 0)
9428             {
9429               encoding = DW_ATE_UTF;
9430               break;
9431             }
9432         }
9433       if (TYPE_STRING_FLAG (type))
9434         {
9435           if (TYPE_UNSIGNED (type))
9436             encoding = DW_ATE_unsigned_char;
9437           else
9438             encoding = DW_ATE_signed_char;
9439         }
9440       else if (TYPE_UNSIGNED (type))
9441         encoding = DW_ATE_unsigned;
9442       else
9443         encoding = DW_ATE_signed;
9444       break;
9445
9446     case REAL_TYPE:
9447       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9448         {
9449           if (dwarf_version >= 3 || !dwarf_strict)
9450             encoding = DW_ATE_decimal_float;
9451           else
9452             encoding = DW_ATE_lo_user;
9453         }
9454       else
9455         encoding = DW_ATE_float;
9456       break;
9457
9458     case FIXED_POINT_TYPE:
9459       if (!(dwarf_version >= 3 || !dwarf_strict))
9460         encoding = DW_ATE_lo_user;
9461       else if (TYPE_UNSIGNED (type))
9462         encoding = DW_ATE_unsigned_fixed;
9463       else
9464         encoding = DW_ATE_signed_fixed;
9465       break;
9466
9467       /* Dwarf2 doesn't know anything about complex ints, so use
9468          a user defined type for it.  */
9469     case COMPLEX_TYPE:
9470       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9471         encoding = DW_ATE_complex_float;
9472       else
9473         encoding = DW_ATE_lo_user;
9474       break;
9475
9476     case BOOLEAN_TYPE:
9477       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9478       encoding = DW_ATE_boolean;
9479       break;
9480
9481     default:
9482       /* No other TREE_CODEs are Dwarf fundamental types.  */
9483       gcc_unreachable ();
9484     }
9485
9486   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9487
9488   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9489                    int_size_in_bytes (type));
9490   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9491
9492   return base_type_result;
9493 }
9494
9495 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9496    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9497
9498 static inline int
9499 is_base_type (tree type)
9500 {
9501   switch (TREE_CODE (type))
9502     {
9503     case ERROR_MARK:
9504     case VOID_TYPE:
9505     case INTEGER_TYPE:
9506     case REAL_TYPE:
9507     case FIXED_POINT_TYPE:
9508     case COMPLEX_TYPE:
9509     case BOOLEAN_TYPE:
9510       return 1;
9511
9512     case ARRAY_TYPE:
9513     case RECORD_TYPE:
9514     case UNION_TYPE:
9515     case QUAL_UNION_TYPE:
9516     case ENUMERAL_TYPE:
9517     case FUNCTION_TYPE:
9518     case METHOD_TYPE:
9519     case POINTER_TYPE:
9520     case REFERENCE_TYPE:
9521     case NULLPTR_TYPE:
9522     case OFFSET_TYPE:
9523     case LANG_TYPE:
9524     case VECTOR_TYPE:
9525       return 0;
9526
9527     default:
9528       gcc_unreachable ();
9529     }
9530
9531   return 0;
9532 }
9533
9534 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9535    node, return the size in bits for the type if it is a constant, or else
9536    return the alignment for the type if the type's size is not constant, or
9537    else return BITS_PER_WORD if the type actually turns out to be an
9538    ERROR_MARK node.  */
9539
9540 static inline unsigned HOST_WIDE_INT
9541 simple_type_size_in_bits (const_tree type)
9542 {
9543   if (TREE_CODE (type) == ERROR_MARK)
9544     return BITS_PER_WORD;
9545   else if (TYPE_SIZE (type) == NULL_TREE)
9546     return 0;
9547   else if (host_integerp (TYPE_SIZE (type), 1))
9548     return tree_low_cst (TYPE_SIZE (type), 1);
9549   else
9550     return TYPE_ALIGN (type);
9551 }
9552
9553 /* Similarly, but return a double_int instead of UHWI.  */
9554
9555 static inline double_int
9556 double_int_type_size_in_bits (const_tree type)
9557 {
9558   if (TREE_CODE (type) == ERROR_MARK)
9559     return uhwi_to_double_int (BITS_PER_WORD);
9560   else if (TYPE_SIZE (type) == NULL_TREE)
9561     return double_int_zero;
9562   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9563     return tree_to_double_int (TYPE_SIZE (type));
9564   else
9565     return uhwi_to_double_int (TYPE_ALIGN (type));
9566 }
9567
9568 /*  Given a pointer to a tree node for a subrange type, return a pointer
9569     to a DIE that describes the given type.  */
9570
9571 static dw_die_ref
9572 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9573 {
9574   dw_die_ref subrange_die;
9575   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9576
9577   if (context_die == NULL)
9578     context_die = comp_unit_die ();
9579
9580   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9581
9582   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9583     {
9584       /* The size of the subrange type and its base type do not match,
9585          so we need to generate a size attribute for the subrange type.  */
9586       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9587     }
9588
9589   if (low)
9590     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9591   if (high)
9592     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9593
9594   return subrange_die;
9595 }
9596
9597 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9598    entry that chains various modifiers in front of the given type.  */
9599
9600 static dw_die_ref
9601 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9602                    dw_die_ref context_die)
9603 {
9604   enum tree_code code = TREE_CODE (type);
9605   dw_die_ref mod_type_die;
9606   dw_die_ref sub_die = NULL;
9607   tree item_type = NULL;
9608   tree qualified_type;
9609   tree name, low, high;
9610
9611   if (code == ERROR_MARK)
9612     return NULL;
9613
9614   /* See if we already have the appropriately qualified variant of
9615      this type.  */
9616   qualified_type
9617     = get_qualified_type (type,
9618                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9619                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9620
9621   if (qualified_type == sizetype
9622       && TYPE_NAME (qualified_type)
9623       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9624     {
9625       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9626
9627       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9628                            && TYPE_PRECISION (t)
9629                            == TYPE_PRECISION (qualified_type)
9630                            && TYPE_UNSIGNED (t)
9631                            == TYPE_UNSIGNED (qualified_type));
9632       qualified_type = t;
9633     }
9634
9635   /* If we do, then we can just use its DIE, if it exists.  */
9636   if (qualified_type)
9637     {
9638       mod_type_die = lookup_type_die (qualified_type);
9639       if (mod_type_die)
9640         return mod_type_die;
9641     }
9642
9643   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9644
9645   /* Handle C typedef types.  */
9646   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9647       && !DECL_ARTIFICIAL (name))
9648     {
9649       tree dtype = TREE_TYPE (name);
9650
9651       if (qualified_type == dtype)
9652         {
9653           /* For a named type, use the typedef.  */
9654           gen_type_die (qualified_type, context_die);
9655           return lookup_type_die (qualified_type);
9656         }
9657       else if (is_const_type < TYPE_READONLY (dtype)
9658                || is_volatile_type < TYPE_VOLATILE (dtype)
9659                || (is_const_type <= TYPE_READONLY (dtype)
9660                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9661                    && DECL_ORIGINAL_TYPE (name) != type))
9662         /* cv-unqualified version of named type.  Just use the unnamed
9663            type to which it refers.  */
9664         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9665                                   is_const_type, is_volatile_type,
9666                                   context_die);
9667       /* Else cv-qualified version of named type; fall through.  */
9668     }
9669
9670   if (is_const_type
9671       /* If both is_const_type and is_volatile_type, prefer the path
9672          which leads to a qualified type.  */
9673       && (!is_volatile_type
9674           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9675           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9676     {
9677       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9678       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9679     }
9680   else if (is_volatile_type)
9681     {
9682       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9683       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9684     }
9685   else if (code == POINTER_TYPE)
9686     {
9687       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9688       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9689                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9690       item_type = TREE_TYPE (type);
9691       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9692         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9693                          TYPE_ADDR_SPACE (item_type));
9694     }
9695   else if (code == REFERENCE_TYPE)
9696     {
9697       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9698         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9699                                 type);
9700       else
9701         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9702       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9703                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9704       item_type = TREE_TYPE (type);
9705       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9706         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9707                          TYPE_ADDR_SPACE (item_type));
9708     }
9709   else if (code == INTEGER_TYPE
9710            && TREE_TYPE (type) != NULL_TREE
9711            && subrange_type_for_debug_p (type, &low, &high))
9712     {
9713       mod_type_die = subrange_type_die (type, low, high, context_die);
9714       item_type = TREE_TYPE (type);
9715     }
9716   else if (is_base_type (type))
9717     mod_type_die = base_type_die (type);
9718   else
9719     {
9720       gen_type_die (type, context_die);
9721
9722       /* We have to get the type_main_variant here (and pass that to the
9723          `lookup_type_die' routine) because the ..._TYPE node we have
9724          might simply be a *copy* of some original type node (where the
9725          copy was created to help us keep track of typedef names) and
9726          that copy might have a different TYPE_UID from the original
9727          ..._TYPE node.  */
9728       if (TREE_CODE (type) != VECTOR_TYPE)
9729         return lookup_type_die (type_main_variant (type));
9730       else
9731         /* Vectors have the debugging information in the type,
9732            not the main variant.  */
9733         return lookup_type_die (type);
9734     }
9735
9736   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9737      don't output a DW_TAG_typedef, since there isn't one in the
9738      user's program; just attach a DW_AT_name to the type.
9739      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9740      if the base type already has the same name.  */
9741   if (name
9742       && ((TREE_CODE (name) != TYPE_DECL
9743            && (qualified_type == TYPE_MAIN_VARIANT (type)
9744                || (!is_const_type && !is_volatile_type)))
9745           || (TREE_CODE (name) == TYPE_DECL
9746               && TREE_TYPE (name) == qualified_type
9747               && DECL_NAME (name))))
9748     {
9749       if (TREE_CODE (name) == TYPE_DECL)
9750         /* Could just call add_name_and_src_coords_attributes here,
9751            but since this is a builtin type it doesn't have any
9752            useful source coordinates anyway.  */
9753         name = DECL_NAME (name);
9754       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9755       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9756       if (TYPE_ARTIFICIAL (type))
9757         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9758     }
9759   /* This probably indicates a bug.  */
9760   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9761     add_name_attribute (mod_type_die, "__unknown__");
9762
9763   if (qualified_type)
9764     equate_type_number_to_die (qualified_type, mod_type_die);
9765
9766   if (item_type)
9767     /* We must do this after the equate_type_number_to_die call, in case
9768        this is a recursive type.  This ensures that the modified_type_die
9769        recursion will terminate even if the type is recursive.  Recursive
9770        types are possible in Ada.  */
9771     sub_die = modified_type_die (item_type,
9772                                  TYPE_READONLY (item_type),
9773                                  TYPE_VOLATILE (item_type),
9774                                  context_die);
9775
9776   if (sub_die != NULL)
9777     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9778
9779   return mod_type_die;
9780 }
9781
9782 /* Generate DIEs for the generic parameters of T.
9783    T must be either a generic type or a generic function.
9784    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9785
9786 static void
9787 gen_generic_params_dies (tree t)
9788 {
9789   tree parms, args;
9790   int parms_num, i;
9791   dw_die_ref die = NULL;
9792
9793   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9794     return;
9795
9796   if (TYPE_P (t))
9797     die = lookup_type_die (t);
9798   else if (DECL_P (t))
9799     die = lookup_decl_die (t);
9800
9801   gcc_assert (die);
9802
9803   parms = lang_hooks.get_innermost_generic_parms (t);
9804   if (!parms)
9805     /* T has no generic parameter. It means T is neither a generic type
9806        or function. End of story.  */
9807     return;
9808
9809   parms_num = TREE_VEC_LENGTH (parms);
9810   args = lang_hooks.get_innermost_generic_args (t);
9811   for (i = 0; i < parms_num; i++)
9812     {
9813       tree parm, arg, arg_pack_elems;
9814
9815       parm = TREE_VEC_ELT (parms, i);
9816       arg = TREE_VEC_ELT (args, i);
9817       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9818       gcc_assert (parm && TREE_VALUE (parm) && arg);
9819
9820       if (parm && TREE_VALUE (parm) && arg)
9821         {
9822           /* If PARM represents a template parameter pack,
9823              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9824              by DW_TAG_template_*_parameter DIEs for the argument
9825              pack elements of ARG. Note that ARG would then be
9826              an argument pack.  */
9827           if (arg_pack_elems)
9828             template_parameter_pack_die (TREE_VALUE (parm),
9829                                          arg_pack_elems,
9830                                          die);
9831           else
9832             generic_parameter_die (TREE_VALUE (parm), arg,
9833                                    true /* Emit DW_AT_name */, die);
9834         }
9835     }
9836 }
9837
9838 /* Create and return a DIE for PARM which should be
9839    the representation of a generic type parameter.
9840    For instance, in the C++ front end, PARM would be a template parameter.
9841    ARG is the argument to PARM.
9842    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9843    name of the PARM.
9844    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9845    as a child node.  */
9846
9847 static dw_die_ref
9848 generic_parameter_die (tree parm, tree arg,
9849                        bool emit_name_p,
9850                        dw_die_ref parent_die)
9851 {
9852   dw_die_ref tmpl_die = NULL;
9853   const char *name = NULL;
9854
9855   if (!parm || !DECL_NAME (parm) || !arg)
9856     return NULL;
9857
9858   /* We support non-type generic parameters and arguments,
9859      type generic parameters and arguments, as well as
9860      generic generic parameters (a.k.a. template template parameters in C++)
9861      and arguments.  */
9862   if (TREE_CODE (parm) == PARM_DECL)
9863     /* PARM is a nontype generic parameter  */
9864     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9865   else if (TREE_CODE (parm) == TYPE_DECL)
9866     /* PARM is a type generic parameter.  */
9867     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9868   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9869     /* PARM is a generic generic parameter.
9870        Its DIE is a GNU extension. It shall have a
9871        DW_AT_name attribute to represent the name of the template template
9872        parameter, and a DW_AT_GNU_template_name attribute to represent the
9873        name of the template template argument.  */
9874     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9875                         parent_die, parm);
9876   else
9877     gcc_unreachable ();
9878
9879   if (tmpl_die)
9880     {
9881       tree tmpl_type;
9882
9883       /* If PARM is a generic parameter pack, it means we are
9884          emitting debug info for a template argument pack element.
9885          In other terms, ARG is a template argument pack element.
9886          In that case, we don't emit any DW_AT_name attribute for
9887          the die.  */
9888       if (emit_name_p)
9889         {
9890           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9891           gcc_assert (name);
9892           add_AT_string (tmpl_die, DW_AT_name, name);
9893         }
9894
9895       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9896         {
9897           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9898              TMPL_DIE should have a child DW_AT_type attribute that is set
9899              to the type of the argument to PARM, which is ARG.
9900              If PARM is a type generic parameter, TMPL_DIE should have a
9901              child DW_AT_type that is set to ARG.  */
9902           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9903           add_type_attribute (tmpl_die, tmpl_type, 0,
9904                               TREE_THIS_VOLATILE (tmpl_type),
9905                               parent_die);
9906         }
9907       else
9908         {
9909           /* So TMPL_DIE is a DIE representing a
9910              a generic generic template parameter, a.k.a template template
9911              parameter in C++ and arg is a template.  */
9912
9913           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9914              to the name of the argument.  */
9915           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9916           if (name)
9917             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9918         }
9919
9920       if (TREE_CODE (parm) == PARM_DECL)
9921         /* So PARM is a non-type generic parameter.
9922            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9923            attribute of TMPL_DIE which value represents the value
9924            of ARG.
9925            We must be careful here:
9926            The value of ARG might reference some function decls.
9927            We might currently be emitting debug info for a generic
9928            type and types are emitted before function decls, we don't
9929            know if the function decls referenced by ARG will actually be
9930            emitted after cgraph computations.
9931            So must defer the generation of the DW_AT_const_value to
9932            after cgraph is ready.  */
9933         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9934     }
9935
9936   return tmpl_die;
9937 }
9938
9939 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9940    PARM_PACK must be a template parameter pack. The returned DIE
9941    will be child DIE of PARENT_DIE.  */
9942
9943 static dw_die_ref
9944 template_parameter_pack_die (tree parm_pack,
9945                              tree parm_pack_args,
9946                              dw_die_ref parent_die)
9947 {
9948   dw_die_ref die;
9949   int j;
9950
9951   gcc_assert (parent_die && parm_pack);
9952
9953   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9954   add_name_and_src_coords_attributes (die, parm_pack);
9955   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9956     generic_parameter_die (parm_pack,
9957                            TREE_VEC_ELT (parm_pack_args, j),
9958                            false /* Don't emit DW_AT_name */,
9959                            die);
9960   return die;
9961 }
9962
9963 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9964    an enumerated type.  */
9965
9966 static inline int
9967 type_is_enum (const_tree type)
9968 {
9969   return TREE_CODE (type) == ENUMERAL_TYPE;
9970 }
9971
9972 /* Return the DBX register number described by a given RTL node.  */
9973
9974 static unsigned int
9975 dbx_reg_number (const_rtx rtl)
9976 {
9977   unsigned regno = REGNO (rtl);
9978
9979   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9980
9981 #ifdef LEAF_REG_REMAP
9982   if (current_function_uses_only_leaf_regs)
9983     {
9984       int leaf_reg = LEAF_REG_REMAP (regno);
9985       if (leaf_reg != -1)
9986         regno = (unsigned) leaf_reg;
9987     }
9988 #endif
9989
9990   return DBX_REGISTER_NUMBER (regno);
9991 }
9992
9993 /* Optionally add a DW_OP_piece term to a location description expression.
9994    DW_OP_piece is only added if the location description expression already
9995    doesn't end with DW_OP_piece.  */
9996
9997 static void
9998 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9999 {
10000   dw_loc_descr_ref loc;
10001
10002   if (*list_head != NULL)
10003     {
10004       /* Find the end of the chain.  */
10005       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10006         ;
10007
10008       if (loc->dw_loc_opc != DW_OP_piece)
10009         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10010     }
10011 }
10012
10013 /* Return a location descriptor that designates a machine register or
10014    zero if there is none.  */
10015
10016 static dw_loc_descr_ref
10017 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10018 {
10019   rtx regs;
10020
10021   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10022     return 0;
10023
10024   /* We only use "frame base" when we're sure we're talking about the
10025      post-prologue local stack frame.  We do this by *not* running
10026      register elimination until this point, and recognizing the special
10027      argument pointer and soft frame pointer rtx's.
10028      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10029   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10030       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10031     {
10032       dw_loc_descr_ref result = NULL;
10033
10034       if (dwarf_version >= 4 || !dwarf_strict)
10035         {
10036           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10037                                        initialized);
10038           if (result)
10039             add_loc_descr (&result,
10040                            new_loc_descr (DW_OP_stack_value, 0, 0));
10041         }
10042       return result;
10043     }
10044
10045   regs = targetm.dwarf_register_span (rtl);
10046
10047   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10048     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10049   else
10050     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10051 }
10052
10053 /* Return a location descriptor that designates a machine register for
10054    a given hard register number.  */
10055
10056 static dw_loc_descr_ref
10057 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10058 {
10059   dw_loc_descr_ref reg_loc_descr;
10060
10061   if (regno <= 31)
10062     reg_loc_descr
10063       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10064   else
10065     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10066
10067   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10068     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10069
10070   return reg_loc_descr;
10071 }
10072
10073 /* Given an RTL of a register, return a location descriptor that
10074    designates a value that spans more than one register.  */
10075
10076 static dw_loc_descr_ref
10077 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10078                              enum var_init_status initialized)
10079 {
10080   int nregs, size, i;
10081   unsigned reg;
10082   dw_loc_descr_ref loc_result = NULL;
10083
10084   reg = REGNO (rtl);
10085 #ifdef LEAF_REG_REMAP
10086   if (current_function_uses_only_leaf_regs)
10087     {
10088       int leaf_reg = LEAF_REG_REMAP (reg);
10089       if (leaf_reg != -1)
10090         reg = (unsigned) leaf_reg;
10091     }
10092 #endif
10093   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10094   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10095
10096   /* Simple, contiguous registers.  */
10097   if (regs == NULL_RTX)
10098     {
10099       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10100
10101       loc_result = NULL;
10102       while (nregs--)
10103         {
10104           dw_loc_descr_ref t;
10105
10106           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10107                                       VAR_INIT_STATUS_INITIALIZED);
10108           add_loc_descr (&loc_result, t);
10109           add_loc_descr_op_piece (&loc_result, size);
10110           ++reg;
10111         }
10112       return loc_result;
10113     }
10114
10115   /* Now onto stupid register sets in non contiguous locations.  */
10116
10117   gcc_assert (GET_CODE (regs) == PARALLEL);
10118
10119   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10120   loc_result = NULL;
10121
10122   for (i = 0; i < XVECLEN (regs, 0); ++i)
10123     {
10124       dw_loc_descr_ref t;
10125
10126       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10127                                   VAR_INIT_STATUS_INITIALIZED);
10128       add_loc_descr (&loc_result, t);
10129       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10130       add_loc_descr_op_piece (&loc_result, size);
10131     }
10132
10133   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10134     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10135   return loc_result;
10136 }
10137
10138 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10139
10140 /* Return a location descriptor that designates a constant i,
10141    as a compound operation from constant (i >> shift), constant shift
10142    and DW_OP_shl.  */
10143
10144 static dw_loc_descr_ref
10145 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10146 {
10147   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10148   add_loc_descr (&ret, int_loc_descriptor (shift));
10149   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10150   return ret;
10151 }
10152
10153 /* Return a location descriptor that designates a constant.  */
10154
10155 static dw_loc_descr_ref
10156 int_loc_descriptor (HOST_WIDE_INT i)
10157 {
10158   enum dwarf_location_atom op;
10159
10160   /* Pick the smallest representation of a constant, rather than just
10161      defaulting to the LEB encoding.  */
10162   if (i >= 0)
10163     {
10164       int clz = clz_hwi (i);
10165       int ctz = ctz_hwi (i);
10166       if (i <= 31)
10167         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10168       else if (i <= 0xff)
10169         op = DW_OP_const1u;
10170       else if (i <= 0xffff)
10171         op = DW_OP_const2u;
10172       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10173                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10174         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10175            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10176            while DW_OP_const4u is 5 bytes.  */
10177         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10178       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10179                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10180         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10181            while DW_OP_const4u is 5 bytes.  */
10182         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10183       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10184         op = DW_OP_const4u;
10185       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10186                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10187         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10188            while DW_OP_constu of constant >= 0x100000000 takes at least
10189            6 bytes.  */
10190         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10191       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10192                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10193                   >= HOST_BITS_PER_WIDE_INT)
10194         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10195            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10196            while DW_OP_constu takes in this case at least 6 bytes.  */
10197         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10198       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10199                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10200                && size_of_uleb128 (i) > 6)
10201         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10202         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10203       else
10204         op = DW_OP_constu;
10205     }
10206   else
10207     {
10208       if (i >= -0x80)
10209         op = DW_OP_const1s;
10210       else if (i >= -0x8000)
10211         op = DW_OP_const2s;
10212       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10213         {
10214           if (size_of_int_loc_descriptor (i) < 5)
10215             {
10216               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10217               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10218               return ret;
10219             }
10220           op = DW_OP_const4s;
10221         }
10222       else
10223         {
10224           if (size_of_int_loc_descriptor (i)
10225               < (unsigned long) 1 + size_of_sleb128 (i))
10226             {
10227               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10228               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10229               return ret;
10230             }
10231           op = DW_OP_consts;
10232         }
10233     }
10234
10235   return new_loc_descr (op, i, 0);
10236 }
10237
10238 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10239    without actually allocating it.  */
10240
10241 static unsigned long
10242 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10243 {
10244   return size_of_int_loc_descriptor (i >> shift)
10245          + size_of_int_loc_descriptor (shift)
10246          + 1;
10247 }
10248
10249 /* Return size_of_locs (int_loc_descriptor (i)) without
10250    actually allocating it.  */
10251
10252 static unsigned long
10253 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10254 {
10255   unsigned long s;
10256
10257   if (i >= 0)
10258     {
10259       int clz, ctz;
10260       if (i <= 31)
10261         return 1;
10262       else if (i <= 0xff)
10263         return 2;
10264       else if (i <= 0xffff)
10265         return 3;
10266       clz = clz_hwi (i);
10267       ctz = ctz_hwi (i);
10268       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10269           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10270         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10271                                                     - clz - 5);
10272       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10273                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10274         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10275                                                     - clz - 8);
10276       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10277         return 5;
10278       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10279       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10280           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10281         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10282                                                     - clz - 8);
10283       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10284                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10285         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10286                                                     - clz - 16);
10287       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10288                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10289                && s > 6)
10290         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10291                                                     - clz - 32);
10292       else
10293         return 1 + s;
10294     }
10295   else
10296     {
10297       if (i >= -0x80)
10298         return 2;
10299       else if (i >= -0x8000)
10300         return 3;
10301       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10302         {
10303           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10304             {
10305               s = size_of_int_loc_descriptor (-i) + 1;
10306               if (s < 5)
10307                 return s;
10308             }
10309           return 5;
10310         }
10311       else
10312         {
10313           unsigned long r = 1 + size_of_sleb128 (i);
10314           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10315             {
10316               s = size_of_int_loc_descriptor (-i) + 1;
10317               if (s < r)
10318                 return s;
10319             }
10320           return r;
10321         }
10322     }
10323 }
10324
10325 /* Return loc description representing "address" of integer value.
10326    This can appear only as toplevel expression.  */
10327
10328 static dw_loc_descr_ref
10329 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10330 {
10331   int litsize;
10332   dw_loc_descr_ref loc_result = NULL;
10333
10334   if (!(dwarf_version >= 4 || !dwarf_strict))
10335     return NULL;
10336
10337   litsize = size_of_int_loc_descriptor (i);
10338   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10339      is more compact.  For DW_OP_stack_value we need:
10340      litsize + 1 (DW_OP_stack_value)
10341      and for DW_OP_implicit_value:
10342      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10343   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10344     {
10345       loc_result = int_loc_descriptor (i);
10346       add_loc_descr (&loc_result,
10347                      new_loc_descr (DW_OP_stack_value, 0, 0));
10348       return loc_result;
10349     }
10350
10351   loc_result = new_loc_descr (DW_OP_implicit_value,
10352                               size, 0);
10353   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10354   loc_result->dw_loc_oprnd2.v.val_int = i;
10355   return loc_result;
10356 }
10357
10358 /* Return a location descriptor that designates a base+offset location.  */
10359
10360 static dw_loc_descr_ref
10361 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10362                  enum var_init_status initialized)
10363 {
10364   unsigned int regno;
10365   dw_loc_descr_ref result;
10366   dw_fde_ref fde = cfun->fde;
10367
10368   /* We only use "frame base" when we're sure we're talking about the
10369      post-prologue local stack frame.  We do this by *not* running
10370      register elimination until this point, and recognizing the special
10371      argument pointer and soft frame pointer rtx's.  */
10372   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10373     {
10374       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10375
10376       if (elim != reg)
10377         {
10378           if (GET_CODE (elim) == PLUS)
10379             {
10380               offset += INTVAL (XEXP (elim, 1));
10381               elim = XEXP (elim, 0);
10382             }
10383           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10384                        && (elim == hard_frame_pointer_rtx
10385                            || elim == stack_pointer_rtx))
10386                       || elim == (frame_pointer_needed
10387                                   ? hard_frame_pointer_rtx
10388                                   : stack_pointer_rtx));
10389
10390           /* If drap register is used to align stack, use frame
10391              pointer + offset to access stack variables.  If stack
10392              is aligned without drap, use stack pointer + offset to
10393              access stack variables.  */
10394           if (crtl->stack_realign_tried
10395               && reg == frame_pointer_rtx)
10396             {
10397               int base_reg
10398                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10399                                       ? HARD_FRAME_POINTER_REGNUM
10400                                       : REGNO (elim));
10401               return new_reg_loc_descr (base_reg, offset);
10402             }
10403
10404           gcc_assert (frame_pointer_fb_offset_valid);
10405           offset += frame_pointer_fb_offset;
10406           return new_loc_descr (DW_OP_fbreg, offset, 0);
10407         }
10408     }
10409   else if (!optimize
10410            && fde
10411            && (fde->drap_reg == dwarf_frame_regnum (REGNO (reg))
10412                || fde->vdrap_reg == dwarf_frame_regnum (REGNO (reg))))
10413     {
10414       /* Use cfa+offset to represent the location of arguments passed
10415          on the stack when drap is used to align stack.
10416          Only do this when not optimizing, for optimized code var-tracking
10417          is supposed to track where the arguments live and the register
10418          used as vdrap or drap in some spot might be used for something
10419          else in other part of the routine.  */
10420       return new_loc_descr (DW_OP_fbreg, offset, 0);
10421     }
10422
10423   regno = dbx_reg_number (reg);
10424   if (regno <= 31)
10425     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10426                             offset, 0);
10427   else
10428     result = new_loc_descr (DW_OP_bregx, regno, offset);
10429
10430   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10431     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10432
10433   return result;
10434 }
10435
10436 /* Return true if this RTL expression describes a base+offset calculation.  */
10437
10438 static inline int
10439 is_based_loc (const_rtx rtl)
10440 {
10441   return (GET_CODE (rtl) == PLUS
10442           && ((REG_P (XEXP (rtl, 0))
10443                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10444                && CONST_INT_P (XEXP (rtl, 1)))));
10445 }
10446
10447 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10448    failed.  */
10449
10450 static dw_loc_descr_ref
10451 tls_mem_loc_descriptor (rtx mem)
10452 {
10453   tree base;
10454   dw_loc_descr_ref loc_result;
10455
10456   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10457     return NULL;
10458
10459   base = get_base_address (MEM_EXPR (mem));
10460   if (base == NULL
10461       || TREE_CODE (base) != VAR_DECL
10462       || !DECL_THREAD_LOCAL_P (base))
10463     return NULL;
10464
10465   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10466   if (loc_result == NULL)
10467     return NULL;
10468
10469   if (INTVAL (MEM_OFFSET (mem)))
10470     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10471
10472   return loc_result;
10473 }
10474
10475 /* Output debug info about reason why we failed to expand expression as dwarf
10476    expression.  */
10477
10478 static void
10479 expansion_failed (tree expr, rtx rtl, char const *reason)
10480 {
10481   if (dump_file && (dump_flags & TDF_DETAILS))
10482     {
10483       fprintf (dump_file, "Failed to expand as dwarf: ");
10484       if (expr)
10485         print_generic_expr (dump_file, expr, dump_flags);
10486       if (rtl)
10487         {
10488           fprintf (dump_file, "\n");
10489           print_rtl (dump_file, rtl);
10490         }
10491       fprintf (dump_file, "\nReason: %s\n", reason);
10492     }
10493 }
10494
10495 /* Helper function for const_ok_for_output, called either directly
10496    or via for_each_rtx.  */
10497
10498 static int
10499 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10500 {
10501   rtx rtl = *rtlp;
10502
10503   if (GET_CODE (rtl) == UNSPEC)
10504     {
10505       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10506          we can't express it in the debug info.  */
10507 #ifdef ENABLE_CHECKING
10508       /* Don't complain about TLS UNSPECs, those are just too hard to
10509          delegitimize.  */
10510       if (XVECLEN (rtl, 0) != 1
10511           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10512           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10513           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10514           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10515         inform (current_function_decl
10516                 ? DECL_SOURCE_LOCATION (current_function_decl)
10517                 : UNKNOWN_LOCATION,
10518 #if NUM_UNSPEC_VALUES > 0
10519                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10520                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10521                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10522                 XINT (rtl, 1));
10523 #else
10524                 "non-delegitimized UNSPEC %d found in variable location",
10525                 XINT (rtl, 1));
10526 #endif
10527 #endif
10528       expansion_failed (NULL_TREE, rtl,
10529                         "UNSPEC hasn't been delegitimized.\n");
10530       return 1;
10531     }
10532
10533   if (GET_CODE (rtl) != SYMBOL_REF)
10534     return 0;
10535
10536   if (CONSTANT_POOL_ADDRESS_P (rtl))
10537     {
10538       bool marked;
10539       get_pool_constant_mark (rtl, &marked);
10540       /* If all references to this pool constant were optimized away,
10541          it was not output and thus we can't represent it.  */
10542       if (!marked)
10543         {
10544           expansion_failed (NULL_TREE, rtl,
10545                             "Constant was removed from constant pool.\n");
10546           return 1;
10547         }
10548     }
10549
10550   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10551     return 1;
10552
10553   /* Avoid references to external symbols in debug info, on several targets
10554      the linker might even refuse to link when linking a shared library,
10555      and in many other cases the relocations for .debug_info/.debug_loc are
10556      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10557      to be defined within the same shared library or executable are fine.  */
10558   if (SYMBOL_REF_EXTERNAL_P (rtl))
10559     {
10560       tree decl = SYMBOL_REF_DECL (rtl);
10561
10562       if (decl == NULL || !targetm.binds_local_p (decl))
10563         {
10564           expansion_failed (NULL_TREE, rtl,
10565                             "Symbol not defined in current TU.\n");
10566           return 1;
10567         }
10568     }
10569
10570   return 0;
10571 }
10572
10573 /* Return true if constant RTL can be emitted in DW_OP_addr or
10574    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10575    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10576
10577 static bool
10578 const_ok_for_output (rtx rtl)
10579 {
10580   if (GET_CODE (rtl) == SYMBOL_REF)
10581     return const_ok_for_output_1 (&rtl, NULL) == 0;
10582
10583   if (GET_CODE (rtl) == CONST)
10584     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10585
10586   return true;
10587 }
10588
10589 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10590    if possible, NULL otherwise.  */
10591
10592 static dw_die_ref
10593 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10594 {
10595   dw_die_ref type_die;
10596   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10597
10598   if (type == NULL)
10599     return NULL;
10600   switch (TREE_CODE (type))
10601     {
10602     case INTEGER_TYPE:
10603     case REAL_TYPE:
10604       break;
10605     default:
10606       return NULL;
10607     }
10608   type_die = lookup_type_die (type);
10609   if (!type_die)
10610     type_die = modified_type_die (type, false, false, comp_unit_die ());
10611   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10612     return NULL;
10613   return type_die;
10614 }
10615
10616 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10617    type matching MODE, or, if MODE is narrower than or as wide as
10618    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10619    possible.  */
10620
10621 static dw_loc_descr_ref
10622 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10623 {
10624   enum machine_mode outer_mode = mode;
10625   dw_die_ref type_die;
10626   dw_loc_descr_ref cvt;
10627
10628   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10629     {
10630       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10631       return op;
10632     }
10633   type_die = base_type_for_mode (outer_mode, 1);
10634   if (type_die == NULL)
10635     return NULL;
10636   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10637   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10638   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10639   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10640   add_loc_descr (&op, cvt);
10641   return op;
10642 }
10643
10644 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10645
10646 static dw_loc_descr_ref
10647 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10648                         dw_loc_descr_ref op1)
10649 {
10650   dw_loc_descr_ref ret = op0;
10651   add_loc_descr (&ret, op1);
10652   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10653   if (STORE_FLAG_VALUE != 1)
10654     {
10655       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10656       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10657     }
10658   return ret;
10659 }
10660
10661 /* Return location descriptor for signed comparison OP RTL.  */
10662
10663 static dw_loc_descr_ref
10664 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10665                          enum machine_mode mem_mode)
10666 {
10667   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10668   dw_loc_descr_ref op0, op1;
10669   int shift;
10670
10671   if (op_mode == VOIDmode)
10672     op_mode = GET_MODE (XEXP (rtl, 1));
10673   if (op_mode == VOIDmode)
10674     return NULL;
10675
10676   if (dwarf_strict
10677       && (GET_MODE_CLASS (op_mode) != MODE_INT
10678           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10679     return NULL;
10680
10681   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10682                             VAR_INIT_STATUS_INITIALIZED);
10683   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10684                             VAR_INIT_STATUS_INITIALIZED);
10685
10686   if (op0 == NULL || op1 == NULL)
10687     return NULL;
10688
10689   if (GET_MODE_CLASS (op_mode) != MODE_INT
10690       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10691     return compare_loc_descriptor (op, op0, op1);
10692
10693   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10694     {
10695       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10696       dw_loc_descr_ref cvt;
10697
10698       if (type_die == NULL)
10699         return NULL;
10700       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10701       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10702       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10703       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10704       add_loc_descr (&op0, cvt);
10705       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10706       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10707       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10708       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10709       add_loc_descr (&op1, cvt);
10710       return compare_loc_descriptor (op, op0, op1);
10711     }
10712
10713   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10714   /* For eq/ne, if the operands are known to be zero-extended,
10715      there is no need to do the fancy shifting up.  */
10716   if (op == DW_OP_eq || op == DW_OP_ne)
10717     {
10718       dw_loc_descr_ref last0, last1;
10719       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10720         ;
10721       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10722         ;
10723       /* deref_size zero extends, and for constants we can check
10724          whether they are zero extended or not.  */
10725       if (((last0->dw_loc_opc == DW_OP_deref_size
10726             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10727            || (CONST_INT_P (XEXP (rtl, 0))
10728                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10729                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10730           && ((last1->dw_loc_opc == DW_OP_deref_size
10731                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10732               || (CONST_INT_P (XEXP (rtl, 1))
10733                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10734                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10735         return compare_loc_descriptor (op, op0, op1);
10736
10737       /* EQ/NE comparison against constant in narrower type than
10738          DWARF2_ADDR_SIZE can be performed either as
10739          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10740          DW_OP_{eq,ne}
10741          or
10742          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10743          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10744       if (CONST_INT_P (XEXP (rtl, 1))
10745           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10746           && (size_of_int_loc_descriptor (shift) + 1
10747               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10748               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10749                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10750                                                & GET_MODE_MASK (op_mode))))
10751         {
10752           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10753           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10754           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10755                                     & GET_MODE_MASK (op_mode));
10756           return compare_loc_descriptor (op, op0, op1);
10757         }
10758     }
10759   add_loc_descr (&op0, int_loc_descriptor (shift));
10760   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10761   if (CONST_INT_P (XEXP (rtl, 1)))
10762     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10763   else
10764     {
10765       add_loc_descr (&op1, int_loc_descriptor (shift));
10766       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10767     }
10768   return compare_loc_descriptor (op, op0, op1);
10769 }
10770
10771 /* Return location descriptor for unsigned comparison OP RTL.  */
10772
10773 static dw_loc_descr_ref
10774 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10775                          enum machine_mode mem_mode)
10776 {
10777   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10778   dw_loc_descr_ref op0, op1;
10779
10780   if (op_mode == VOIDmode)
10781     op_mode = GET_MODE (XEXP (rtl, 1));
10782   if (op_mode == VOIDmode)
10783     return NULL;
10784   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10785     return NULL;
10786
10787   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10788     return NULL;
10789
10790   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10791                             VAR_INIT_STATUS_INITIALIZED);
10792   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10793                             VAR_INIT_STATUS_INITIALIZED);
10794
10795   if (op0 == NULL || op1 == NULL)
10796     return NULL;
10797
10798   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10799     {
10800       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10801       dw_loc_descr_ref last0, last1;
10802       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10803         ;
10804       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10805         ;
10806       if (CONST_INT_P (XEXP (rtl, 0)))
10807         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10808       /* deref_size zero extends, so no need to mask it again.  */
10809       else if (last0->dw_loc_opc != DW_OP_deref_size
10810                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10811         {
10812           add_loc_descr (&op0, int_loc_descriptor (mask));
10813           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10814         }
10815       if (CONST_INT_P (XEXP (rtl, 1)))
10816         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10817       /* deref_size zero extends, so no need to mask it again.  */
10818       else if (last1->dw_loc_opc != DW_OP_deref_size
10819                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10820         {
10821           add_loc_descr (&op1, int_loc_descriptor (mask));
10822           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10823         }
10824     }
10825   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10826     {
10827       HOST_WIDE_INT bias = 1;
10828       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10829       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10830       if (CONST_INT_P (XEXP (rtl, 1)))
10831         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10832                                   + INTVAL (XEXP (rtl, 1)));
10833       else
10834         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10835                                             bias, 0));
10836     }
10837   return compare_loc_descriptor (op, op0, op1);
10838 }
10839
10840 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10841
10842 static dw_loc_descr_ref
10843 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10844                        enum machine_mode mem_mode)
10845 {
10846   enum dwarf_location_atom op;
10847   dw_loc_descr_ref op0, op1, ret;
10848   dw_loc_descr_ref bra_node, drop_node;
10849
10850   if (dwarf_strict
10851       && (GET_MODE_CLASS (mode) != MODE_INT
10852           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10853     return NULL;
10854
10855   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10856                             VAR_INIT_STATUS_INITIALIZED);
10857   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10858                             VAR_INIT_STATUS_INITIALIZED);
10859
10860   if (op0 == NULL || op1 == NULL)
10861     return NULL;
10862
10863   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10864   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10865   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10866   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10867     {
10868       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10869         {
10870           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10871           add_loc_descr (&op0, int_loc_descriptor (mask));
10872           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10873           add_loc_descr (&op1, int_loc_descriptor (mask));
10874           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10875         }
10876       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10877         {
10878           HOST_WIDE_INT bias = 1;
10879           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10880           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10881           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10882         }
10883     }
10884   else if (GET_MODE_CLASS (mode) == MODE_INT
10885            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10886     {
10887       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10888       add_loc_descr (&op0, int_loc_descriptor (shift));
10889       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10890       add_loc_descr (&op1, int_loc_descriptor (shift));
10891       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10892     }
10893   else if (GET_MODE_CLASS (mode) == MODE_INT
10894            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10895     {
10896       dw_die_ref type_die = base_type_for_mode (mode, 0);
10897       dw_loc_descr_ref cvt;
10898       if (type_die == NULL)
10899         return NULL;
10900       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10901       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10902       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10903       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10904       add_loc_descr (&op0, cvt);
10905       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10906       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10907       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10908       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10909       add_loc_descr (&op1, cvt);
10910     }
10911
10912   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10913     op = DW_OP_lt;
10914   else
10915     op = DW_OP_gt;
10916   ret = op0;
10917   add_loc_descr (&ret, op1);
10918   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10919   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10920   add_loc_descr (&ret, bra_node);
10921   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10922   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10923   add_loc_descr (&ret, drop_node);
10924   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10925   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10926   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10927       && GET_MODE_CLASS (mode) == MODE_INT
10928       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10929     ret = convert_descriptor_to_mode (mode, ret);
10930   return ret;
10931 }
10932
10933 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10934    but after converting arguments to type_die, afterwards
10935    convert back to unsigned.  */
10936
10937 static dw_loc_descr_ref
10938 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10939              enum machine_mode mode, enum machine_mode mem_mode)
10940 {
10941   dw_loc_descr_ref cvt, op0, op1;
10942
10943   if (type_die == NULL)
10944     return NULL;
10945   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10946                             VAR_INIT_STATUS_INITIALIZED);
10947   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10948                             VAR_INIT_STATUS_INITIALIZED);
10949   if (op0 == NULL || op1 == NULL)
10950     return NULL;
10951   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10952   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10953   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10954   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10955   add_loc_descr (&op0, cvt);
10956   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10957   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10958   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10959   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10960   add_loc_descr (&op1, cvt);
10961   add_loc_descr (&op0, op1);
10962   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10963   return convert_descriptor_to_mode (mode, op0);
10964 }
10965
10966 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10967    const0 is DW_OP_lit0 or corresponding typed constant,
10968    const1 is DW_OP_lit1 or corresponding typed constant
10969    and constMSB is constant with just the MSB bit set
10970    for the mode):
10971        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10972    L1: const0 DW_OP_swap
10973    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10974        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10975    L3: DW_OP_drop
10976    L4: DW_OP_nop
10977
10978    CTZ is similar:
10979        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10980    L1: const0 DW_OP_swap
10981    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10982        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10983    L3: DW_OP_drop
10984    L4: DW_OP_nop
10985
10986    FFS is similar:
10987        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10988    L1: const1 DW_OP_swap
10989    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10990        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10991    L3: DW_OP_drop
10992    L4: DW_OP_nop  */
10993
10994 static dw_loc_descr_ref
10995 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10996                     enum machine_mode mem_mode)
10997 {
10998   dw_loc_descr_ref op0, ret, tmp;
10999   HOST_WIDE_INT valv;
11000   dw_loc_descr_ref l1jump, l1label;
11001   dw_loc_descr_ref l2jump, l2label;
11002   dw_loc_descr_ref l3jump, l3label;
11003   dw_loc_descr_ref l4jump, l4label;
11004   rtx msb;
11005
11006   if (GET_MODE_CLASS (mode) != MODE_INT
11007       || GET_MODE (XEXP (rtl, 0)) != mode
11008       || (GET_CODE (rtl) == CLZ
11009           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11010     return NULL;
11011
11012   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11013                             VAR_INIT_STATUS_INITIALIZED);
11014   if (op0 == NULL)
11015     return NULL;
11016   ret = op0;
11017   if (GET_CODE (rtl) == CLZ)
11018     {
11019       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11020         valv = GET_MODE_BITSIZE (mode);
11021     }
11022   else if (GET_CODE (rtl) == FFS)
11023     valv = 0;
11024   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11025     valv = GET_MODE_BITSIZE (mode);
11026   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11027   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11028   add_loc_descr (&ret, l1jump);
11029   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11030   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11031                             VAR_INIT_STATUS_INITIALIZED);
11032   if (tmp == NULL)
11033     return NULL;
11034   add_loc_descr (&ret, tmp);
11035   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11036   add_loc_descr (&ret, l4jump);
11037   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11038                                 ? const1_rtx : const0_rtx,
11039                                 mode, mem_mode,
11040                                 VAR_INIT_STATUS_INITIALIZED);
11041   if (l1label == NULL)
11042     return NULL;
11043   add_loc_descr (&ret, l1label);
11044   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11045   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11046   add_loc_descr (&ret, l2label);
11047   if (GET_CODE (rtl) != CLZ)
11048     msb = const1_rtx;
11049   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11050     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11051                    << (GET_MODE_BITSIZE (mode) - 1));
11052   else
11053     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11054                                   << (GET_MODE_BITSIZE (mode)
11055                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11056   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11057     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11058                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11059                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11060   else
11061     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11062                               VAR_INIT_STATUS_INITIALIZED);
11063   if (tmp == NULL)
11064     return NULL;
11065   add_loc_descr (&ret, tmp);
11066   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11067   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11068   add_loc_descr (&ret, l3jump);
11069   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11070                             VAR_INIT_STATUS_INITIALIZED);
11071   if (tmp == NULL)
11072     return NULL;
11073   add_loc_descr (&ret, tmp);
11074   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11075                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11076   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11077   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11078   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11079   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11080   add_loc_descr (&ret, l2jump);
11081   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11082   add_loc_descr (&ret, l3label);
11083   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11084   add_loc_descr (&ret, l4label);
11085   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11086   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11087   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11088   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11089   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11090   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11091   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11092   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11093   return ret;
11094 }
11095
11096 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11097    const1 is DW_OP_lit1 or corresponding typed constant):
11098        const0 DW_OP_swap
11099    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11100        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11101    L2: DW_OP_drop
11102
11103    PARITY is similar:
11104    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11105        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11106    L2: DW_OP_drop  */
11107
11108 static dw_loc_descr_ref
11109 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11110                          enum machine_mode mem_mode)
11111 {
11112   dw_loc_descr_ref op0, ret, tmp;
11113   dw_loc_descr_ref l1jump, l1label;
11114   dw_loc_descr_ref l2jump, l2label;
11115
11116   if (GET_MODE_CLASS (mode) != MODE_INT
11117       || GET_MODE (XEXP (rtl, 0)) != mode)
11118     return NULL;
11119
11120   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11121                             VAR_INIT_STATUS_INITIALIZED);
11122   if (op0 == NULL)
11123     return NULL;
11124   ret = op0;
11125   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11126                             VAR_INIT_STATUS_INITIALIZED);
11127   if (tmp == NULL)
11128     return NULL;
11129   add_loc_descr (&ret, tmp);
11130   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11131   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11132   add_loc_descr (&ret, l1label);
11133   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11134   add_loc_descr (&ret, l2jump);
11135   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11136   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11137   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11138                             VAR_INIT_STATUS_INITIALIZED);
11139   if (tmp == NULL)
11140     return NULL;
11141   add_loc_descr (&ret, tmp);
11142   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11143   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11144                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11145   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11146   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11147                             VAR_INIT_STATUS_INITIALIZED);
11148   add_loc_descr (&ret, tmp);
11149   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11150   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11151   add_loc_descr (&ret, l1jump);
11152   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11153   add_loc_descr (&ret, l2label);
11154   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11155   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11156   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11157   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11158   return ret;
11159 }
11160
11161 /* BSWAP (constS is initial shift count, either 56 or 24):
11162        constS const0
11163    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11164        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11165        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11166        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11167    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11168
11169 static dw_loc_descr_ref
11170 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11171                       enum machine_mode mem_mode)
11172 {
11173   dw_loc_descr_ref op0, ret, tmp;
11174   dw_loc_descr_ref l1jump, l1label;
11175   dw_loc_descr_ref l2jump, l2label;
11176
11177   if (GET_MODE_CLASS (mode) != MODE_INT
11178       || BITS_PER_UNIT != 8
11179       || (GET_MODE_BITSIZE (mode) != 32
11180           &&  GET_MODE_BITSIZE (mode) != 64))
11181     return NULL;
11182
11183   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11184                             VAR_INIT_STATUS_INITIALIZED);
11185   if (op0 == NULL)
11186     return NULL;
11187
11188   ret = op0;
11189   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11190                             mode, mem_mode,
11191                             VAR_INIT_STATUS_INITIALIZED);
11192   if (tmp == NULL)
11193     return NULL;
11194   add_loc_descr (&ret, tmp);
11195   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11196                             VAR_INIT_STATUS_INITIALIZED);
11197   if (tmp == NULL)
11198     return NULL;
11199   add_loc_descr (&ret, tmp);
11200   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11201   add_loc_descr (&ret, l1label);
11202   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11203                             mode, mem_mode,
11204                             VAR_INIT_STATUS_INITIALIZED);
11205   add_loc_descr (&ret, tmp);
11206   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11207   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11208   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11209   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11210                             VAR_INIT_STATUS_INITIALIZED);
11211   if (tmp == NULL)
11212     return NULL;
11213   add_loc_descr (&ret, tmp);
11214   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11215   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11216   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11217   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11218   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11219   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11220   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11221                             VAR_INIT_STATUS_INITIALIZED);
11222   add_loc_descr (&ret, tmp);
11223   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11224   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11225   add_loc_descr (&ret, l2jump);
11226   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11227                             VAR_INIT_STATUS_INITIALIZED);
11228   add_loc_descr (&ret, tmp);
11229   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11230   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11231   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11232   add_loc_descr (&ret, l1jump);
11233   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11234   add_loc_descr (&ret, l2label);
11235   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11236   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11237   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11238   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11239   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11240   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11241   return ret;
11242 }
11243
11244 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11245    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11246    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11247    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11248
11249    ROTATERT is similar:
11250    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11251    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11252    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11253
11254 static dw_loc_descr_ref
11255 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11256                        enum machine_mode mem_mode)
11257 {
11258   rtx rtlop1 = XEXP (rtl, 1);
11259   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11260   int i;
11261
11262   if (GET_MODE_CLASS (mode) != MODE_INT)
11263     return NULL;
11264
11265   if (GET_MODE (rtlop1) != VOIDmode
11266       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11267     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11268   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11269                             VAR_INIT_STATUS_INITIALIZED);
11270   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11271                             VAR_INIT_STATUS_INITIALIZED);
11272   if (op0 == NULL || op1 == NULL)
11273     return NULL;
11274   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11275     for (i = 0; i < 2; i++)
11276       {
11277         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11278           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11279                                         mode, mem_mode,
11280                                         VAR_INIT_STATUS_INITIALIZED);
11281         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11282           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11283                                    ? DW_OP_const4u
11284                                    : HOST_BITS_PER_WIDE_INT == 64
11285                                    ? DW_OP_const8u : DW_OP_constu,
11286                                    GET_MODE_MASK (mode), 0);
11287         else
11288           mask[i] = NULL;
11289         if (mask[i] == NULL)
11290           return NULL;
11291         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11292       }
11293   ret = op0;
11294   add_loc_descr (&ret, op1);
11295   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11296   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11297   if (GET_CODE (rtl) == ROTATERT)
11298     {
11299       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11300       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11301                                           GET_MODE_BITSIZE (mode), 0));
11302     }
11303   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11304   if (mask[0] != NULL)
11305     add_loc_descr (&ret, mask[0]);
11306   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11307   if (mask[1] != NULL)
11308     {
11309       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11310       add_loc_descr (&ret, mask[1]);
11311       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11312     }
11313   if (GET_CODE (rtl) == ROTATE)
11314     {
11315       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11316       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11317                                           GET_MODE_BITSIZE (mode), 0));
11318     }
11319   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11320   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11321   return ret;
11322 }
11323
11324 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11325    for DEBUG_PARAMETER_REF RTL.  */
11326
11327 static dw_loc_descr_ref
11328 parameter_ref_descriptor (rtx rtl)
11329 {
11330   dw_loc_descr_ref ret;
11331   dw_die_ref ref;
11332
11333   if (dwarf_strict)
11334     return NULL;
11335   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11336   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11337   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11338   if (ref)
11339     {
11340       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11341       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11342       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11343     }
11344   else
11345     {
11346       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11347       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11348     }
11349   return ret;
11350 }
11351
11352 /* Helper function to get mode of MEM's address.  */
11353
11354 enum machine_mode
11355 get_address_mode (rtx mem)
11356 {
11357   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11358   if (mode != VOIDmode)
11359     return mode;
11360   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11361 }
11362
11363 /* The following routine converts the RTL for a variable or parameter
11364    (resident in memory) into an equivalent Dwarf representation of a
11365    mechanism for getting the address of that same variable onto the top of a
11366    hypothetical "address evaluation" stack.
11367
11368    When creating memory location descriptors, we are effectively transforming
11369    the RTL for a memory-resident object into its Dwarf postfix expression
11370    equivalent.  This routine recursively descends an RTL tree, turning
11371    it into Dwarf postfix code as it goes.
11372
11373    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11374
11375    MEM_MODE is the mode of the memory reference, needed to handle some
11376    autoincrement addressing modes.
11377
11378    Return 0 if we can't represent the location.  */
11379
11380 dw_loc_descr_ref
11381 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11382                     enum machine_mode mem_mode,
11383                     enum var_init_status initialized)
11384 {
11385   dw_loc_descr_ref mem_loc_result = NULL;
11386   enum dwarf_location_atom op;
11387   dw_loc_descr_ref op0, op1;
11388
11389   if (mode == VOIDmode)
11390     mode = GET_MODE (rtl);
11391
11392   /* Note that for a dynamically sized array, the location we will generate a
11393      description of here will be the lowest numbered location which is
11394      actually within the array.  That's *not* necessarily the same as the
11395      zeroth element of the array.  */
11396
11397   rtl = targetm.delegitimize_address (rtl);
11398
11399   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11400     return NULL;
11401
11402   switch (GET_CODE (rtl))
11403     {
11404     case POST_INC:
11405     case POST_DEC:
11406     case POST_MODIFY:
11407       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11408
11409     case SUBREG:
11410       /* The case of a subreg may arise when we have a local (register)
11411          variable or a formal (register) parameter which doesn't quite fill
11412          up an entire register.  For now, just assume that it is
11413          legitimate to make the Dwarf info refer to the whole register which
11414          contains the given subreg.  */
11415       if (!subreg_lowpart_p (rtl))
11416         break;
11417       if (GET_MODE_CLASS (mode) == MODE_INT
11418           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11419           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11420 #ifdef POINTERS_EXTEND_UNSIGNED
11421               || (mode == Pmode && mem_mode != VOIDmode)
11422 #endif
11423              )
11424           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11425         {
11426           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11427                                                GET_MODE (SUBREG_REG (rtl)),
11428                                                mem_mode, initialized);
11429           break;
11430         }
11431       if (dwarf_strict)
11432         break;
11433       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11434         break;
11435       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11436           && (GET_MODE_CLASS (mode) != MODE_INT
11437               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11438         break;
11439       else
11440         {
11441           dw_die_ref type_die;
11442           dw_loc_descr_ref cvt;
11443
11444           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11445                                                GET_MODE (SUBREG_REG (rtl)),
11446                                                mem_mode, initialized);
11447           if (mem_loc_result == NULL)
11448             break;
11449           type_die = base_type_for_mode (mode,
11450                                          GET_MODE_CLASS (mode) == MODE_INT);
11451           if (type_die == NULL)
11452             {
11453               mem_loc_result = NULL;
11454               break;
11455             }
11456           if (GET_MODE_SIZE (mode)
11457               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11458             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11459           else
11460             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11461           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11462           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11463           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11464           add_loc_descr (&mem_loc_result, cvt);
11465         }
11466       break;
11467
11468     case REG:
11469       if (GET_MODE_CLASS (mode) != MODE_INT
11470           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11471 #ifdef POINTERS_EXTEND_UNSIGNED
11472               && (mode != Pmode || mem_mode == VOIDmode)
11473 #endif
11474               ))
11475         {
11476           dw_die_ref type_die;
11477
11478           if (dwarf_strict)
11479             break;
11480           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11481             break;
11482           type_die = base_type_for_mode (mode,
11483                                          GET_MODE_CLASS (mode) == MODE_INT);
11484           if (type_die == NULL)
11485             break;
11486           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11487                                           dbx_reg_number (rtl), 0);
11488           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11489           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11490           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11491           break;
11492         }
11493       /* Whenever a register number forms a part of the description of the
11494          method for calculating the (dynamic) address of a memory resident
11495          object, DWARF rules require the register number be referred to as
11496          a "base register".  This distinction is not based in any way upon
11497          what category of register the hardware believes the given register
11498          belongs to.  This is strictly DWARF terminology we're dealing with
11499          here. Note that in cases where the location of a memory-resident
11500          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11501          OP_CONST (0)) the actual DWARF location descriptor that we generate
11502          may just be OP_BASEREG (basereg).  This may look deceptively like
11503          the object in question was allocated to a register (rather than in
11504          memory) so DWARF consumers need to be aware of the subtle
11505          distinction between OP_REG and OP_BASEREG.  */
11506       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11507         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11508       else if (stack_realign_drap
11509                && crtl->drap_reg
11510                && crtl->args.internal_arg_pointer == rtl
11511                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11512         {
11513           /* If RTL is internal_arg_pointer, which has been optimized
11514              out, use DRAP instead.  */
11515           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11516                                             VAR_INIT_STATUS_INITIALIZED);
11517         }
11518       break;
11519
11520     case SIGN_EXTEND:
11521     case ZERO_EXTEND:
11522       if (GET_MODE_CLASS (mode) != MODE_INT)
11523         break;
11524       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11525                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11526       if (op0 == 0)
11527         break;
11528       else if (GET_CODE (rtl) == ZERO_EXTEND
11529                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11530                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11531                   < HOST_BITS_PER_WIDE_INT
11532                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11533                   to expand zero extend as two shifts instead of
11534                   masking.  */
11535                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11536         {
11537           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11538           mem_loc_result = op0;
11539           add_loc_descr (&mem_loc_result,
11540                          int_loc_descriptor (GET_MODE_MASK (imode)));
11541           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11542         }
11543       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11544         {
11545           int shift = DWARF2_ADDR_SIZE
11546                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11547           shift *= BITS_PER_UNIT;
11548           if (GET_CODE (rtl) == SIGN_EXTEND)
11549             op = DW_OP_shra;
11550           else
11551             op = DW_OP_shr;
11552           mem_loc_result = op0;
11553           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11554           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11555           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11556           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11557         }
11558       else if (!dwarf_strict)
11559         {
11560           dw_die_ref type_die1, type_die2;
11561           dw_loc_descr_ref cvt;
11562
11563           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11564                                           GET_CODE (rtl) == ZERO_EXTEND);
11565           if (type_die1 == NULL)
11566             break;
11567           type_die2 = base_type_for_mode (mode, 1);
11568           if (type_die2 == NULL)
11569             break;
11570           mem_loc_result = op0;
11571           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11572           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11573           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11574           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11575           add_loc_descr (&mem_loc_result, cvt);
11576           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11577           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11578           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11579           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11580           add_loc_descr (&mem_loc_result, cvt);
11581         }
11582       break;
11583
11584     case MEM:
11585       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11586                                            get_address_mode (rtl), mode,
11587                                            VAR_INIT_STATUS_INITIALIZED);
11588       if (mem_loc_result == NULL)
11589         mem_loc_result = tls_mem_loc_descriptor (rtl);
11590       if (mem_loc_result != 0)
11591         {
11592           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11593               || GET_MODE_CLASS (mode) != MODE_INT)
11594             {
11595               dw_die_ref type_die;
11596               dw_loc_descr_ref deref;
11597
11598               if (dwarf_strict)
11599                 return NULL;
11600               type_die
11601                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11602               if (type_die == NULL)
11603                 return NULL;
11604               deref = new_loc_descr (DW_OP_GNU_deref_type,
11605                                      GET_MODE_SIZE (mode), 0);
11606               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11607               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11608               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11609               add_loc_descr (&mem_loc_result, deref);
11610             }
11611           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11612             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11613           else
11614             add_loc_descr (&mem_loc_result,
11615                            new_loc_descr (DW_OP_deref_size,
11616                                           GET_MODE_SIZE (mode), 0));
11617         }
11618       else
11619         {
11620           rtx new_rtl = avoid_constant_pool_reference (rtl);
11621           if (new_rtl != rtl)
11622             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
11623         }
11624       break;
11625
11626     case LO_SUM:
11627       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11628
11629     case LABEL_REF:
11630       /* Some ports can transform a symbol ref into a label ref, because
11631          the symbol ref is too far away and has to be dumped into a constant
11632          pool.  */
11633     case CONST:
11634     case SYMBOL_REF:
11635       if (GET_MODE_CLASS (mode) != MODE_INT
11636           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11637 #ifdef POINTERS_EXTEND_UNSIGNED
11638               && (mode != Pmode || mem_mode == VOIDmode)
11639 #endif
11640               ))
11641         break;
11642       if (GET_CODE (rtl) == SYMBOL_REF
11643           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11644         {
11645           dw_loc_descr_ref temp;
11646
11647           /* If this is not defined, we have no way to emit the data.  */
11648           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11649             break;
11650
11651           /* We used to emit DW_OP_addr here, but that's wrong, since
11652              DW_OP_addr should be relocated by the debug info consumer,
11653              while DW_OP_GNU_push_tls_address operand should not.  */
11654           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11655                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11656           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11657           temp->dw_loc_oprnd1.v.val_addr = rtl;
11658           temp->dtprel = true;
11659
11660           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11661           add_loc_descr (&mem_loc_result, temp);
11662
11663           break;
11664         }
11665
11666       if (!const_ok_for_output (rtl))
11667         break;
11668
11669     symref:
11670       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11671       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11672       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11673       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11674       break;
11675
11676     case CONCAT:
11677     case CONCATN:
11678     case VAR_LOCATION:
11679     case DEBUG_IMPLICIT_PTR:
11680       expansion_failed (NULL_TREE, rtl,
11681                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11682       return 0;
11683
11684     case ENTRY_VALUE:
11685       if (dwarf_strict)
11686         return NULL;
11687       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11688         {
11689           if (GET_MODE_CLASS (mode) != MODE_INT
11690               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11691             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11692                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11693           else
11694             op0
11695               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11696                                         VAR_INIT_STATUS_INITIALIZED);
11697         }
11698       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11699                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11700         {
11701           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11702                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11703           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11704             return NULL;
11705         }
11706       else
11707         gcc_unreachable ();
11708       if (op0 == NULL)
11709         return NULL;
11710       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11711       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11712       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11713       break;
11714
11715     case DEBUG_PARAMETER_REF:
11716       mem_loc_result = parameter_ref_descriptor (rtl);
11717       break;
11718
11719     case PRE_MODIFY:
11720       /* Extract the PLUS expression nested inside and fall into
11721          PLUS code below.  */
11722       rtl = XEXP (rtl, 1);
11723       goto plus;
11724
11725     case PRE_INC:
11726     case PRE_DEC:
11727       /* Turn these into a PLUS expression and fall into the PLUS code
11728          below.  */
11729       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11730                           GEN_INT (GET_CODE (rtl) == PRE_INC
11731                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11732                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11733
11734       /* ... fall through ...  */
11735
11736     case PLUS:
11737     plus:
11738       if (is_based_loc (rtl)
11739           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11740           && GET_MODE_CLASS (mode) == MODE_INT)
11741         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11742                                           INTVAL (XEXP (rtl, 1)),
11743                                           VAR_INIT_STATUS_INITIALIZED);
11744       else
11745         {
11746           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11747                                                VAR_INIT_STATUS_INITIALIZED);
11748           if (mem_loc_result == 0)
11749             break;
11750
11751           if (CONST_INT_P (XEXP (rtl, 1))
11752               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11753             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11754           else
11755             {
11756               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11757                                         VAR_INIT_STATUS_INITIALIZED);
11758               if (op1 == 0)
11759                 break;
11760               add_loc_descr (&mem_loc_result, op1);
11761               add_loc_descr (&mem_loc_result,
11762                              new_loc_descr (DW_OP_plus, 0, 0));
11763             }
11764         }
11765       break;
11766
11767     /* If a pseudo-reg is optimized away, it is possible for it to
11768        be replaced with a MEM containing a multiply or shift.  */
11769     case MINUS:
11770       op = DW_OP_minus;
11771       goto do_binop;
11772
11773     case MULT:
11774       op = DW_OP_mul;
11775       goto do_binop;
11776
11777     case DIV:
11778       if (!dwarf_strict
11779           && GET_MODE_CLASS (mode) == MODE_INT
11780           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11781         {
11782           mem_loc_result = typed_binop (DW_OP_div, rtl,
11783                                         base_type_for_mode (mode, 0),
11784                                         mode, mem_mode);
11785           break;
11786         }
11787       op = DW_OP_div;
11788       goto do_binop;
11789
11790     case UMOD:
11791       op = DW_OP_mod;
11792       goto do_binop;
11793
11794     case ASHIFT:
11795       op = DW_OP_shl;
11796       goto do_shift;
11797
11798     case ASHIFTRT:
11799       op = DW_OP_shra;
11800       goto do_shift;
11801
11802     case LSHIFTRT:
11803       op = DW_OP_shr;
11804       goto do_shift;
11805
11806     do_shift:
11807       if (GET_MODE_CLASS (mode) != MODE_INT)
11808         break;
11809       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11810                                 VAR_INIT_STATUS_INITIALIZED);
11811       {
11812         rtx rtlop1 = XEXP (rtl, 1);
11813         if (GET_MODE (rtlop1) != VOIDmode
11814             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11815                < GET_MODE_BITSIZE (mode))
11816           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11817         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11818                                   VAR_INIT_STATUS_INITIALIZED);
11819       }
11820
11821       if (op0 == 0 || op1 == 0)
11822         break;
11823
11824       mem_loc_result = op0;
11825       add_loc_descr (&mem_loc_result, op1);
11826       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11827       break;
11828
11829     case AND:
11830       op = DW_OP_and;
11831       goto do_binop;
11832
11833     case IOR:
11834       op = DW_OP_or;
11835       goto do_binop;
11836
11837     case XOR:
11838       op = DW_OP_xor;
11839       goto do_binop;
11840
11841     do_binop:
11842       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11843                                 VAR_INIT_STATUS_INITIALIZED);
11844       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11845                                 VAR_INIT_STATUS_INITIALIZED);
11846
11847       if (op0 == 0 || op1 == 0)
11848         break;
11849
11850       mem_loc_result = op0;
11851       add_loc_descr (&mem_loc_result, op1);
11852       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11853       break;
11854
11855     case MOD:
11856       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11857         {
11858           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11859                                         base_type_for_mode (mode, 0),
11860                                         mode, mem_mode);
11861           break;
11862         }
11863
11864       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11865                                 VAR_INIT_STATUS_INITIALIZED);
11866       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11867                                 VAR_INIT_STATUS_INITIALIZED);
11868
11869       if (op0 == 0 || op1 == 0)
11870         break;
11871
11872       mem_loc_result = op0;
11873       add_loc_descr (&mem_loc_result, op1);
11874       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11875       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11876       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11877       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11878       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11879       break;
11880
11881     case UDIV:
11882       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11883         {
11884           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11885             {
11886               op = DW_OP_div;
11887               goto do_binop;
11888             }
11889           mem_loc_result = typed_binop (DW_OP_div, rtl,
11890                                         base_type_for_mode (mode, 1),
11891                                         mode, mem_mode);
11892         }
11893       break;
11894
11895     case NOT:
11896       op = DW_OP_not;
11897       goto do_unop;
11898
11899     case ABS:
11900       op = DW_OP_abs;
11901       goto do_unop;
11902
11903     case NEG:
11904       op = DW_OP_neg;
11905       goto do_unop;
11906
11907     do_unop:
11908       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11909                                 VAR_INIT_STATUS_INITIALIZED);
11910
11911       if (op0 == 0)
11912         break;
11913
11914       mem_loc_result = op0;
11915       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11916       break;
11917
11918     case CONST_INT:
11919       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11920 #ifdef POINTERS_EXTEND_UNSIGNED
11921           || (mode == Pmode
11922               && mem_mode != VOIDmode
11923               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11924 #endif
11925           )
11926         {
11927           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11928           break;
11929         }
11930       if (!dwarf_strict
11931           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11932               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
11933         {
11934           dw_die_ref type_die = base_type_for_mode (mode, 1);
11935           enum machine_mode amode;
11936           if (type_die == NULL)
11937             return NULL;
11938           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11939                                  MODE_INT, 0);
11940           if (INTVAL (rtl) >= 0
11941               && amode != BLKmode
11942               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11943               /* const DW_OP_GNU_convert <XXX> vs.
11944                  DW_OP_GNU_const_type <XXX, 1, const>.  */
11945               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11946                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11947             {
11948               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11949               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11950               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11951               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11952               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11953               add_loc_descr (&mem_loc_result, op0);
11954               return mem_loc_result;
11955             }
11956           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11957                                           INTVAL (rtl));
11958           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11959           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11960           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11961           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11962             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11963           else
11964             {
11965               mem_loc_result->dw_loc_oprnd2.val_class
11966                 = dw_val_class_const_double;
11967               mem_loc_result->dw_loc_oprnd2.v.val_double
11968                 = shwi_to_double_int (INTVAL (rtl));
11969             }
11970         }
11971       break;
11972
11973     case CONST_DOUBLE:
11974       if (!dwarf_strict)
11975         {
11976           dw_die_ref type_die;
11977
11978           /* Note that a CONST_DOUBLE rtx could represent either an integer
11979              or a floating-point constant.  A CONST_DOUBLE is used whenever
11980              the constant requires more than one word in order to be
11981              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11982           if (mode == VOIDmode
11983               || (GET_MODE (rtl) == VOIDmode
11984                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
11985             break;
11986           type_die = base_type_for_mode (mode,
11987                                          GET_MODE_CLASS (mode) == MODE_INT);
11988           if (type_die == NULL)
11989             return NULL;
11990           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11991           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11992           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11993           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11994           if (SCALAR_FLOAT_MODE_P (mode))
11995             {
11996               unsigned int length = GET_MODE_SIZE (mode);
11997               unsigned char *array
11998                   = (unsigned char*) ggc_alloc_atomic (length);
11999
12000               insert_float (rtl, array);
12001               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12002               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12003               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12004               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12005             }
12006           else
12007             {
12008               mem_loc_result->dw_loc_oprnd2.val_class
12009                 = dw_val_class_const_double;
12010               mem_loc_result->dw_loc_oprnd2.v.val_double
12011                 = rtx_to_double_int (rtl);
12012             }
12013         }
12014       break;
12015
12016     case EQ:
12017       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12018       break;
12019
12020     case GE:
12021       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12022       break;
12023
12024     case GT:
12025       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12026       break;
12027
12028     case LE:
12029       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12030       break;
12031
12032     case LT:
12033       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12034       break;
12035
12036     case NE:
12037       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12038       break;
12039
12040     case GEU:
12041       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12042       break;
12043
12044     case GTU:
12045       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12046       break;
12047
12048     case LEU:
12049       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12050       break;
12051
12052     case LTU:
12053       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12054       break;
12055
12056     case UMIN:
12057     case UMAX:
12058       if (GET_MODE_CLASS (mode) != MODE_INT)
12059         break;
12060       /* FALLTHRU */
12061     case SMIN:
12062     case SMAX:
12063       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12064       break;
12065
12066     case ZERO_EXTRACT:
12067     case SIGN_EXTRACT:
12068       if (CONST_INT_P (XEXP (rtl, 1))
12069           && CONST_INT_P (XEXP (rtl, 2))
12070           && ((unsigned) INTVAL (XEXP (rtl, 1))
12071               + (unsigned) INTVAL (XEXP (rtl, 2))
12072               <= GET_MODE_BITSIZE (mode))
12073           && GET_MODE_CLASS (mode) == MODE_INT
12074           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12075           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12076         {
12077           int shift, size;
12078           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12079                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12080           if (op0 == 0)
12081             break;
12082           if (GET_CODE (rtl) == SIGN_EXTRACT)
12083             op = DW_OP_shra;
12084           else
12085             op = DW_OP_shr;
12086           mem_loc_result = op0;
12087           size = INTVAL (XEXP (rtl, 1));
12088           shift = INTVAL (XEXP (rtl, 2));
12089           if (BITS_BIG_ENDIAN)
12090             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12091                     - shift - size;
12092           if (shift + size != (int) DWARF2_ADDR_SIZE)
12093             {
12094               add_loc_descr (&mem_loc_result,
12095                              int_loc_descriptor (DWARF2_ADDR_SIZE
12096                                                  - shift - size));
12097               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12098             }
12099           if (size != (int) DWARF2_ADDR_SIZE)
12100             {
12101               add_loc_descr (&mem_loc_result,
12102                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12103               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12104             }
12105         }
12106       break;
12107
12108     case IF_THEN_ELSE:
12109       {
12110         dw_loc_descr_ref op2, bra_node, drop_node;
12111         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12112                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12113                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12114                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12115         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12116                                   VAR_INIT_STATUS_INITIALIZED);
12117         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12118                                   VAR_INIT_STATUS_INITIALIZED);
12119         if (op0 == NULL || op1 == NULL || op2 == NULL)
12120           break;
12121
12122         mem_loc_result = op1;
12123         add_loc_descr (&mem_loc_result, op2);
12124         add_loc_descr (&mem_loc_result, op0);
12125         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12126         add_loc_descr (&mem_loc_result, bra_node);
12127         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12128         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12129         add_loc_descr (&mem_loc_result, drop_node);
12130         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12131         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12132       }
12133       break;
12134
12135     case FLOAT_EXTEND:
12136     case FLOAT_TRUNCATE:
12137     case FLOAT:
12138     case UNSIGNED_FLOAT:
12139     case FIX:
12140     case UNSIGNED_FIX:
12141       if (!dwarf_strict)
12142         {
12143           dw_die_ref type_die;
12144           dw_loc_descr_ref cvt;
12145
12146           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12147                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12148           if (op0 == NULL)
12149             break;
12150           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12151               && (GET_CODE (rtl) == FLOAT
12152                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12153                      <= DWARF2_ADDR_SIZE))
12154             {
12155               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12156                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12157               if (type_die == NULL)
12158                 break;
12159               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12160               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12161               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12162               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12163               add_loc_descr (&op0, cvt);
12164             }
12165           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12166           if (type_die == NULL)
12167             break;
12168           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12169           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12170           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12171           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12172           add_loc_descr (&op0, cvt);
12173           if (GET_MODE_CLASS (mode) == MODE_INT
12174               && (GET_CODE (rtl) == FIX
12175                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12176             {
12177               op0 = convert_descriptor_to_mode (mode, op0);
12178               if (op0 == NULL)
12179                 break;
12180             }
12181           mem_loc_result = op0;
12182         }
12183       break;
12184
12185     case CLZ:
12186     case CTZ:
12187     case FFS:
12188       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12189       break;
12190
12191     case POPCOUNT:
12192     case PARITY:
12193       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12194       break;
12195
12196     case BSWAP:
12197       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12198       break;
12199
12200     case ROTATE:
12201     case ROTATERT:
12202       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12203       break;
12204
12205     case COMPARE:
12206     case TRUNCATE:
12207       /* In theory, we could implement the above.  */
12208       /* DWARF cannot represent the unsigned compare operations
12209          natively.  */
12210     case SS_MULT:
12211     case US_MULT:
12212     case SS_DIV:
12213     case US_DIV:
12214     case SS_PLUS:
12215     case US_PLUS:
12216     case SS_MINUS:
12217     case US_MINUS:
12218     case SS_NEG:
12219     case US_NEG:
12220     case SS_ABS:
12221     case SS_ASHIFT:
12222     case US_ASHIFT:
12223     case SS_TRUNCATE:
12224     case US_TRUNCATE:
12225     case UNORDERED:
12226     case ORDERED:
12227     case UNEQ:
12228     case UNGE:
12229     case UNGT:
12230     case UNLE:
12231     case UNLT:
12232     case LTGT:
12233     case FRACT_CONVERT:
12234     case UNSIGNED_FRACT_CONVERT:
12235     case SAT_FRACT:
12236     case UNSIGNED_SAT_FRACT:
12237     case SQRT:
12238     case ASM_OPERANDS:
12239     case VEC_MERGE:
12240     case VEC_SELECT:
12241     case VEC_CONCAT:
12242     case VEC_DUPLICATE:
12243     case UNSPEC:
12244     case HIGH:
12245     case FMA:
12246     case STRICT_LOW_PART:
12247     case CONST_VECTOR:
12248     case CONST_FIXED:
12249     case CLRSB:
12250       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12251          can't express it in the debug info.  This can happen e.g. with some
12252          TLS UNSPECs.  */
12253       break;
12254
12255     case CONST_STRING:
12256       resolve_one_addr (&rtl, NULL);
12257       goto symref;
12258
12259     default:
12260 #ifdef ENABLE_CHECKING
12261       print_rtl (stderr, rtl);
12262       gcc_unreachable ();
12263 #else
12264       break;
12265 #endif
12266     }
12267
12268   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12269     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12270
12271   return mem_loc_result;
12272 }
12273
12274 /* Return a descriptor that describes the concatenation of two locations.
12275    This is typically a complex variable.  */
12276
12277 static dw_loc_descr_ref
12278 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12279 {
12280   dw_loc_descr_ref cc_loc_result = NULL;
12281   dw_loc_descr_ref x0_ref
12282     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12283   dw_loc_descr_ref x1_ref
12284     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12285
12286   if (x0_ref == 0 || x1_ref == 0)
12287     return 0;
12288
12289   cc_loc_result = x0_ref;
12290   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12291
12292   add_loc_descr (&cc_loc_result, x1_ref);
12293   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12294
12295   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12296     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12297
12298   return cc_loc_result;
12299 }
12300
12301 /* Return a descriptor that describes the concatenation of N
12302    locations.  */
12303
12304 static dw_loc_descr_ref
12305 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12306 {
12307   unsigned int i;
12308   dw_loc_descr_ref cc_loc_result = NULL;
12309   unsigned int n = XVECLEN (concatn, 0);
12310
12311   for (i = 0; i < n; ++i)
12312     {
12313       dw_loc_descr_ref ref;
12314       rtx x = XVECEXP (concatn, 0, i);
12315
12316       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12317       if (ref == NULL)
12318         return NULL;
12319
12320       add_loc_descr (&cc_loc_result, ref);
12321       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12322     }
12323
12324   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12325     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12326
12327   return cc_loc_result;
12328 }
12329
12330 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12331    for DEBUG_IMPLICIT_PTR RTL.  */
12332
12333 static dw_loc_descr_ref
12334 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12335 {
12336   dw_loc_descr_ref ret;
12337   dw_die_ref ref;
12338
12339   if (dwarf_strict)
12340     return NULL;
12341   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12342               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12343               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12344   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12345   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12346   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12347   if (ref)
12348     {
12349       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12350       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12351       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12352     }
12353   else
12354     {
12355       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12356       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12357     }
12358   return ret;
12359 }
12360
12361 /* Output a proper Dwarf location descriptor for a variable or parameter
12362    which is either allocated in a register or in a memory location.  For a
12363    register, we just generate an OP_REG and the register number.  For a
12364    memory location we provide a Dwarf postfix expression describing how to
12365    generate the (dynamic) address of the object onto the address stack.
12366
12367    MODE is mode of the decl if this loc_descriptor is going to be used in
12368    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12369    allowed, VOIDmode otherwise.
12370
12371    If we don't know how to describe it, return 0.  */
12372
12373 static dw_loc_descr_ref
12374 loc_descriptor (rtx rtl, enum machine_mode mode,
12375                 enum var_init_status initialized)
12376 {
12377   dw_loc_descr_ref loc_result = NULL;
12378
12379   switch (GET_CODE (rtl))
12380     {
12381     case SUBREG:
12382       /* The case of a subreg may arise when we have a local (register)
12383          variable or a formal (register) parameter which doesn't quite fill
12384          up an entire register.  For now, just assume that it is
12385          legitimate to make the Dwarf info refer to the whole register which
12386          contains the given subreg.  */
12387       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12388         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12389       else
12390         goto do_default;
12391       break;
12392
12393     case REG:
12394       loc_result = reg_loc_descriptor (rtl, initialized);
12395       break;
12396
12397     case MEM:
12398       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12399                                        GET_MODE (rtl), initialized);
12400       if (loc_result == NULL)
12401         loc_result = tls_mem_loc_descriptor (rtl);
12402       if (loc_result == NULL)
12403         {
12404           rtx new_rtl = avoid_constant_pool_reference (rtl);
12405           if (new_rtl != rtl)
12406             loc_result = loc_descriptor (new_rtl, mode, initialized);
12407         }
12408       break;
12409
12410     case CONCAT:
12411       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12412                                           initialized);
12413       break;
12414
12415     case CONCATN:
12416       loc_result = concatn_loc_descriptor (rtl, initialized);
12417       break;
12418
12419     case VAR_LOCATION:
12420       /* Single part.  */
12421       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12422         {
12423           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12424           if (GET_CODE (loc) == EXPR_LIST)
12425             loc = XEXP (loc, 0);
12426           loc_result = loc_descriptor (loc, mode, initialized);
12427           break;
12428         }
12429
12430       rtl = XEXP (rtl, 1);
12431       /* FALLTHRU */
12432
12433     case PARALLEL:
12434       {
12435         rtvec par_elems = XVEC (rtl, 0);
12436         int num_elem = GET_NUM_ELEM (par_elems);
12437         enum machine_mode mode;
12438         int i;
12439
12440         /* Create the first one, so we have something to add to.  */
12441         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12442                                      VOIDmode, initialized);
12443         if (loc_result == NULL)
12444           return NULL;
12445         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12446         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12447         for (i = 1; i < num_elem; i++)
12448           {
12449             dw_loc_descr_ref temp;
12450
12451             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12452                                    VOIDmode, initialized);
12453             if (temp == NULL)
12454               return NULL;
12455             add_loc_descr (&loc_result, temp);
12456             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12457             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12458           }
12459       }
12460       break;
12461
12462     case CONST_INT:
12463       if (mode != VOIDmode && mode != BLKmode)
12464         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12465                                                     INTVAL (rtl));
12466       break;
12467
12468     case CONST_DOUBLE:
12469       if (mode == VOIDmode)
12470         mode = GET_MODE (rtl);
12471
12472       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12473         {
12474           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12475
12476           /* Note that a CONST_DOUBLE rtx could represent either an integer
12477              or a floating-point constant.  A CONST_DOUBLE is used whenever
12478              the constant requires more than one word in order to be
12479              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12480           loc_result = new_loc_descr (DW_OP_implicit_value,
12481                                       GET_MODE_SIZE (mode), 0);
12482           if (SCALAR_FLOAT_MODE_P (mode))
12483             {
12484               unsigned int length = GET_MODE_SIZE (mode);
12485               unsigned char *array
12486                   = (unsigned char*) ggc_alloc_atomic (length);
12487
12488               insert_float (rtl, array);
12489               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12490               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12491               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12492               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12493             }
12494           else
12495             {
12496               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12497               loc_result->dw_loc_oprnd2.v.val_double
12498                 = rtx_to_double_int (rtl);
12499             }
12500         }
12501       break;
12502
12503     case CONST_VECTOR:
12504       if (mode == VOIDmode)
12505         mode = GET_MODE (rtl);
12506
12507       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12508         {
12509           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12510           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12511           unsigned char *array = (unsigned char *)
12512             ggc_alloc_atomic (length * elt_size);
12513           unsigned int i;
12514           unsigned char *p;
12515
12516           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12517           switch (GET_MODE_CLASS (mode))
12518             {
12519             case MODE_VECTOR_INT:
12520               for (i = 0, p = array; i < length; i++, p += elt_size)
12521                 {
12522                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12523                   double_int val = rtx_to_double_int (elt);
12524
12525                   if (elt_size <= sizeof (HOST_WIDE_INT))
12526                     insert_int (double_int_to_shwi (val), elt_size, p);
12527                   else
12528                     {
12529                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12530                       insert_double (val, p);
12531                     }
12532                 }
12533               break;
12534
12535             case MODE_VECTOR_FLOAT:
12536               for (i = 0, p = array; i < length; i++, p += elt_size)
12537                 {
12538                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12539                   insert_float (elt, p);
12540                 }
12541               break;
12542
12543             default:
12544               gcc_unreachable ();
12545             }
12546
12547           loc_result = new_loc_descr (DW_OP_implicit_value,
12548                                       length * elt_size, 0);
12549           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12550           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12551           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12552           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12553         }
12554       break;
12555
12556     case CONST:
12557       if (mode == VOIDmode
12558           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12559           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12560           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12561         {
12562           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12563           break;
12564         }
12565       /* FALLTHROUGH */
12566     case SYMBOL_REF:
12567       if (!const_ok_for_output (rtl))
12568         break;
12569     case LABEL_REF:
12570       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12571           && (dwarf_version >= 4 || !dwarf_strict))
12572         {
12573           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12574           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12575           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12576           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12577           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12578         }
12579       break;
12580
12581     case DEBUG_IMPLICIT_PTR:
12582       loc_result = implicit_ptr_descriptor (rtl, 0);
12583       break;
12584
12585     case PLUS:
12586       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12587           && CONST_INT_P (XEXP (rtl, 1)))
12588         {
12589           loc_result
12590             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12591           break;
12592         }
12593       /* FALLTHRU */
12594     do_default:
12595     default:
12596       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12597            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12598            && dwarf_version >= 4)
12599           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12600         {
12601           /* Value expression.  */
12602           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12603           if (loc_result)
12604             add_loc_descr (&loc_result,
12605                            new_loc_descr (DW_OP_stack_value, 0, 0));
12606         }
12607       break;
12608     }
12609
12610   return loc_result;
12611 }
12612
12613 /* We need to figure out what section we should use as the base for the
12614    address ranges where a given location is valid.
12615    1. If this particular DECL has a section associated with it, use that.
12616    2. If this function has a section associated with it, use that.
12617    3. Otherwise, use the text section.
12618    XXX: If you split a variable across multiple sections, we won't notice.  */
12619
12620 static const char *
12621 secname_for_decl (const_tree decl)
12622 {
12623   const char *secname;
12624
12625   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12626     {
12627       tree sectree = DECL_SECTION_NAME (decl);
12628       secname = TREE_STRING_POINTER (sectree);
12629     }
12630   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12631     {
12632       tree sectree = DECL_SECTION_NAME (current_function_decl);
12633       secname = TREE_STRING_POINTER (sectree);
12634     }
12635   else if (cfun && in_cold_section_p)
12636     secname = crtl->subsections.cold_section_label;
12637   else
12638     secname = text_section_label;
12639
12640   return secname;
12641 }
12642
12643 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12644
12645 static bool
12646 decl_by_reference_p (tree decl)
12647 {
12648   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12649            || TREE_CODE (decl) == VAR_DECL)
12650           && DECL_BY_REFERENCE (decl));
12651 }
12652
12653 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12654    for VARLOC.  */
12655
12656 static dw_loc_descr_ref
12657 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12658                enum var_init_status initialized)
12659 {
12660   int have_address = 0;
12661   dw_loc_descr_ref descr;
12662   enum machine_mode mode;
12663
12664   if (want_address != 2)
12665     {
12666       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12667       /* Single part.  */
12668       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12669         {
12670           varloc = PAT_VAR_LOCATION_LOC (varloc);
12671           if (GET_CODE (varloc) == EXPR_LIST)
12672             varloc = XEXP (varloc, 0);
12673           mode = GET_MODE (varloc);
12674           if (MEM_P (varloc))
12675             {
12676               rtx addr = XEXP (varloc, 0);
12677               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12678                                           mode, initialized);
12679               if (descr)
12680                 have_address = 1;
12681               else
12682                 {
12683                   rtx x = avoid_constant_pool_reference (varloc);
12684                   if (x != varloc)
12685                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12686                                                 initialized);
12687                 }
12688             }
12689           else
12690             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12691         }
12692       else
12693         return 0;
12694     }
12695   else
12696     {
12697       if (GET_CODE (varloc) == VAR_LOCATION)
12698         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12699       else
12700         mode = DECL_MODE (loc);
12701       descr = loc_descriptor (varloc, mode, initialized);
12702       have_address = 1;
12703     }
12704
12705   if (!descr)
12706     return 0;
12707
12708   if (want_address == 2 && !have_address
12709       && (dwarf_version >= 4 || !dwarf_strict))
12710     {
12711       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12712         {
12713           expansion_failed (loc, NULL_RTX,
12714                             "DWARF address size mismatch");
12715           return 0;
12716         }
12717       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12718       have_address = 1;
12719     }
12720   /* Show if we can't fill the request for an address.  */
12721   if (want_address && !have_address)
12722     {
12723       expansion_failed (loc, NULL_RTX,
12724                         "Want address and only have value");
12725       return 0;
12726     }
12727
12728   /* If we've got an address and don't want one, dereference.  */
12729   if (!want_address && have_address)
12730     {
12731       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12732       enum dwarf_location_atom op;
12733
12734       if (size > DWARF2_ADDR_SIZE || size == -1)
12735         {
12736           expansion_failed (loc, NULL_RTX,
12737                             "DWARF address size mismatch");
12738           return 0;
12739         }
12740       else if (size == DWARF2_ADDR_SIZE)
12741         op = DW_OP_deref;
12742       else
12743         op = DW_OP_deref_size;
12744
12745       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12746     }
12747
12748   return descr;
12749 }
12750
12751 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12752    if it is not possible.  */
12753
12754 static dw_loc_descr_ref
12755 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12756 {
12757   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12758     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12759   else if (dwarf_version >= 3 || !dwarf_strict)
12760     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12761   else
12762     return NULL;
12763 }
12764
12765 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12766    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12767
12768 static dw_loc_descr_ref
12769 dw_sra_loc_expr (tree decl, rtx loc)
12770 {
12771   rtx p;
12772   unsigned int padsize = 0;
12773   dw_loc_descr_ref descr, *descr_tail;
12774   unsigned HOST_WIDE_INT decl_size;
12775   rtx varloc;
12776   enum var_init_status initialized;
12777
12778   if (DECL_SIZE (decl) == NULL
12779       || !host_integerp (DECL_SIZE (decl), 1))
12780     return NULL;
12781
12782   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12783   descr = NULL;
12784   descr_tail = &descr;
12785
12786   for (p = loc; p; p = XEXP (p, 1))
12787     {
12788       unsigned int bitsize = decl_piece_bitsize (p);
12789       rtx loc_note = *decl_piece_varloc_ptr (p);
12790       dw_loc_descr_ref cur_descr;
12791       dw_loc_descr_ref *tail, last = NULL;
12792       unsigned int opsize = 0;
12793
12794       if (loc_note == NULL_RTX
12795           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12796         {
12797           padsize += bitsize;
12798           continue;
12799         }
12800       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12801       varloc = NOTE_VAR_LOCATION (loc_note);
12802       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12803       if (cur_descr == NULL)
12804         {
12805           padsize += bitsize;
12806           continue;
12807         }
12808
12809       /* Check that cur_descr either doesn't use
12810          DW_OP_*piece operations, or their sum is equal
12811          to bitsize.  Otherwise we can't embed it.  */
12812       for (tail = &cur_descr; *tail != NULL;
12813            tail = &(*tail)->dw_loc_next)
12814         if ((*tail)->dw_loc_opc == DW_OP_piece)
12815           {
12816             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12817                       * BITS_PER_UNIT;
12818             last = *tail;
12819           }
12820         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12821           {
12822             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12823             last = *tail;
12824           }
12825
12826       if (last != NULL && opsize != bitsize)
12827         {
12828           padsize += bitsize;
12829           continue;
12830         }
12831
12832       /* If there is a hole, add DW_OP_*piece after empty DWARF
12833          expression, which means that those bits are optimized out.  */
12834       if (padsize)
12835         {
12836           if (padsize > decl_size)
12837             return NULL;
12838           decl_size -= padsize;
12839           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12840           if (*descr_tail == NULL)
12841             return NULL;
12842           descr_tail = &(*descr_tail)->dw_loc_next;
12843           padsize = 0;
12844         }
12845       *descr_tail = cur_descr;
12846       descr_tail = tail;
12847       if (bitsize > decl_size)
12848         return NULL;
12849       decl_size -= bitsize;
12850       if (last == NULL)
12851         {
12852           HOST_WIDE_INT offset = 0;
12853           if (GET_CODE (varloc) == VAR_LOCATION
12854               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12855             {
12856               varloc = PAT_VAR_LOCATION_LOC (varloc);
12857               if (GET_CODE (varloc) == EXPR_LIST)
12858                 varloc = XEXP (varloc, 0);
12859             }
12860           do 
12861             {
12862               if (GET_CODE (varloc) == CONST
12863                   || GET_CODE (varloc) == SIGN_EXTEND
12864                   || GET_CODE (varloc) == ZERO_EXTEND)
12865                 varloc = XEXP (varloc, 0);
12866               else if (GET_CODE (varloc) == SUBREG)
12867                 varloc = SUBREG_REG (varloc);
12868               else
12869                 break;
12870             }
12871           while (1);
12872           /* DW_OP_bit_size offset should be zero for register
12873              or implicit location descriptions and empty location
12874              descriptions, but for memory addresses needs big endian
12875              adjustment.  */
12876           if (MEM_P (varloc))
12877             {
12878               unsigned HOST_WIDE_INT memsize
12879                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
12880               if (memsize != bitsize)
12881                 {
12882                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12883                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12884                     return NULL;
12885                   if (memsize < bitsize)
12886                     return NULL;
12887                   if (BITS_BIG_ENDIAN)
12888                     offset = memsize - bitsize;
12889                 }
12890             }
12891
12892           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12893           if (*descr_tail == NULL)
12894             return NULL;
12895           descr_tail = &(*descr_tail)->dw_loc_next;
12896         }
12897     }
12898
12899   /* If there were any non-empty expressions, add padding till the end of
12900      the decl.  */
12901   if (descr != NULL && decl_size != 0)
12902     {
12903       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12904       if (*descr_tail == NULL)
12905         return NULL;
12906     }
12907   return descr;
12908 }
12909
12910 /* Return the dwarf representation of the location list LOC_LIST of
12911    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12912    function.  */
12913
12914 static dw_loc_list_ref
12915 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12916 {
12917   const char *endname, *secname;
12918   rtx varloc;
12919   enum var_init_status initialized;
12920   struct var_loc_node *node;
12921   dw_loc_descr_ref descr;
12922   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12923   dw_loc_list_ref list = NULL;
12924   dw_loc_list_ref *listp = &list;
12925
12926   /* Now that we know what section we are using for a base,
12927      actually construct the list of locations.
12928      The first location information is what is passed to the
12929      function that creates the location list, and the remaining
12930      locations just get added on to that list.
12931      Note that we only know the start address for a location
12932      (IE location changes), so to build the range, we use
12933      the range [current location start, next location start].
12934      This means we have to special case the last node, and generate
12935      a range of [last location start, end of function label].  */
12936
12937   secname = secname_for_decl (decl);
12938
12939   for (node = loc_list->first; node; node = node->next)
12940     if (GET_CODE (node->loc) == EXPR_LIST
12941         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12942       {
12943         if (GET_CODE (node->loc) == EXPR_LIST)
12944           {
12945             /* This requires DW_OP_{,bit_}piece, which is not usable
12946                inside DWARF expressions.  */
12947             if (want_address != 2)
12948               continue;
12949             descr = dw_sra_loc_expr (decl, node->loc);
12950             if (descr == NULL)
12951               continue;
12952           }
12953         else
12954           {
12955             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12956             varloc = NOTE_VAR_LOCATION (node->loc);
12957             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12958           }
12959         if (descr)
12960           {
12961             bool range_across_switch = false;
12962             /* If section switch happens in between node->label
12963                and node->next->label (or end of function) and
12964                we can't emit it as a single entry list,
12965                emit two ranges, first one ending at the end
12966                of first partition and second one starting at the
12967                beginning of second partition.  */
12968             if (node == loc_list->last_before_switch
12969                 && (node != loc_list->first || loc_list->first->next)
12970                 && current_function_decl)
12971               {
12972                 endname = cfun->fde->dw_fde_end;
12973                 range_across_switch = true;
12974               }
12975             /* The variable has a location between NODE->LABEL and
12976                NODE->NEXT->LABEL.  */
12977             else if (node->next)
12978               endname = node->next->label;
12979             /* If the variable has a location at the last label
12980                it keeps its location until the end of function.  */
12981             else if (!current_function_decl)
12982               endname = text_end_label;
12983             else
12984               {
12985                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12986                                              current_function_funcdef_no);
12987                 endname = ggc_strdup (label_id);
12988               }
12989
12990             *listp = new_loc_list (descr, node->label, endname, secname);
12991             if (TREE_CODE (decl) == PARM_DECL
12992                 && node == loc_list->first
12993                 && GET_CODE (node->loc) == NOTE
12994                 && strcmp (node->label, endname) == 0)
12995               (*listp)->force = true;
12996             listp = &(*listp)->dw_loc_next;
12997
12998             if (range_across_switch)
12999               {
13000                 if (GET_CODE (node->loc) == EXPR_LIST)
13001                   descr = dw_sra_loc_expr (decl, node->loc);
13002                 else
13003                   {
13004                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13005                     varloc = NOTE_VAR_LOCATION (node->loc);
13006                     descr = dw_loc_list_1 (decl, varloc, want_address,
13007                                            initialized);
13008                   }
13009                 gcc_assert (descr);
13010                 /* The variable has a location between NODE->LABEL and
13011                    NODE->NEXT->LABEL.  */
13012                 if (node->next)
13013                   endname = node->next->label;
13014                 else
13015                   endname = cfun->fde->dw_fde_second_end;
13016                 *listp = new_loc_list (descr,
13017                                        cfun->fde->dw_fde_second_begin,
13018                                        endname, secname);
13019                 listp = &(*listp)->dw_loc_next;
13020               }
13021           }
13022       }
13023
13024   /* Try to avoid the overhead of a location list emitting a location
13025      expression instead, but only if we didn't have more than one
13026      location entry in the first place.  If some entries were not
13027      representable, we don't want to pretend a single entry that was
13028      applies to the entire scope in which the variable is
13029      available.  */
13030   if (list && loc_list->first->next)
13031     gen_llsym (list);
13032
13033   return list;
13034 }
13035
13036 /* Return if the loc_list has only single element and thus can be represented
13037    as location description.   */
13038
13039 static bool
13040 single_element_loc_list_p (dw_loc_list_ref list)
13041 {
13042   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13043   return !list->ll_symbol;
13044 }
13045
13046 /* To each location in list LIST add loc descr REF.  */
13047
13048 static void
13049 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13050 {
13051   dw_loc_descr_ref copy;
13052   add_loc_descr (&list->expr, ref);
13053   list = list->dw_loc_next;
13054   while (list)
13055     {
13056       copy = ggc_alloc_dw_loc_descr_node ();
13057       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13058       add_loc_descr (&list->expr, copy);
13059       while (copy->dw_loc_next)
13060         {
13061           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13062           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13063           copy->dw_loc_next = new_copy;
13064           copy = new_copy;
13065         }
13066       list = list->dw_loc_next;
13067     }
13068 }
13069
13070 /* Given two lists RET and LIST
13071    produce location list that is result of adding expression in LIST
13072    to expression in RET on each possition in program.
13073    Might be destructive on both RET and LIST.
13074
13075    TODO: We handle only simple cases of RET or LIST having at most one
13076    element. General case would inolve sorting the lists in program order
13077    and merging them that will need some additional work.
13078    Adding that will improve quality of debug info especially for SRA-ed
13079    structures.  */
13080
13081 static void
13082 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13083 {
13084   if (!list)
13085     return;
13086   if (!*ret)
13087     {
13088       *ret = list;
13089       return;
13090     }
13091   if (!list->dw_loc_next)
13092     {
13093       add_loc_descr_to_each (*ret, list->expr);
13094       return;
13095     }
13096   if (!(*ret)->dw_loc_next)
13097     {
13098       add_loc_descr_to_each (list, (*ret)->expr);
13099       *ret = list;
13100       return;
13101     }
13102   expansion_failed (NULL_TREE, NULL_RTX,
13103                     "Don't know how to merge two non-trivial"
13104                     " location lists.\n");
13105   *ret = NULL;
13106   return;
13107 }
13108
13109 /* LOC is constant expression.  Try a luck, look it up in constant
13110    pool and return its loc_descr of its address.  */
13111
13112 static dw_loc_descr_ref
13113 cst_pool_loc_descr (tree loc)
13114 {
13115   /* Get an RTL for this, if something has been emitted.  */
13116   rtx rtl = lookup_constant_def (loc);
13117
13118   if (!rtl || !MEM_P (rtl))
13119     {
13120       gcc_assert (!rtl);
13121       return 0;
13122     }
13123   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13124
13125   /* TODO: We might get more coverage if we was actually delaying expansion
13126      of all expressions till end of compilation when constant pools are fully
13127      populated.  */
13128   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13129     {
13130       expansion_failed (loc, NULL_RTX,
13131                         "CST value in contant pool but not marked.");
13132       return 0;
13133     }
13134   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13135                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13136 }
13137
13138 /* Return dw_loc_list representing address of addr_expr LOC
13139    by looking for innder INDIRECT_REF expression and turing it
13140    into simple arithmetics.  */
13141
13142 static dw_loc_list_ref
13143 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13144 {
13145   tree obj, offset;
13146   HOST_WIDE_INT bitsize, bitpos, bytepos;
13147   enum machine_mode mode;
13148   int volatilep;
13149   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13150   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13151
13152   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13153                              &bitsize, &bitpos, &offset, &mode,
13154                              &unsignedp, &volatilep, false);
13155   STRIP_NOPS (obj);
13156   if (bitpos % BITS_PER_UNIT)
13157     {
13158       expansion_failed (loc, NULL_RTX, "bitfield access");
13159       return 0;
13160     }
13161   if (!INDIRECT_REF_P (obj))
13162     {
13163       expansion_failed (obj,
13164                         NULL_RTX, "no indirect ref in inner refrence");
13165       return 0;
13166     }
13167   if (!offset && !bitpos)
13168     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13169   else if (toplev
13170            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13171            && (dwarf_version >= 4 || !dwarf_strict))
13172     {
13173       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13174       if (!list_ret)
13175         return 0;
13176       if (offset)
13177         {
13178           /* Variable offset.  */
13179           list_ret1 = loc_list_from_tree (offset, 0);
13180           if (list_ret1 == 0)
13181             return 0;
13182           add_loc_list (&list_ret, list_ret1);
13183           if (!list_ret)
13184             return 0;
13185           add_loc_descr_to_each (list_ret,
13186                                  new_loc_descr (DW_OP_plus, 0, 0));
13187         }
13188       bytepos = bitpos / BITS_PER_UNIT;
13189       if (bytepos > 0)
13190         add_loc_descr_to_each (list_ret,
13191                                new_loc_descr (DW_OP_plus_uconst,
13192                                               bytepos, 0));
13193       else if (bytepos < 0)
13194         loc_list_plus_const (list_ret, bytepos);
13195       add_loc_descr_to_each (list_ret,
13196                              new_loc_descr (DW_OP_stack_value, 0, 0));
13197     }
13198   return list_ret;
13199 }
13200
13201
13202 /* Generate Dwarf location list representing LOC.
13203    If WANT_ADDRESS is false, expression computing LOC will be computed
13204    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13205    if WANT_ADDRESS is 2, expression computing address useable in location
13206      will be returned (i.e. DW_OP_reg can be used
13207      to refer to register values).  */
13208
13209 static dw_loc_list_ref
13210 loc_list_from_tree (tree loc, int want_address)
13211 {
13212   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13213   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13214   int have_address = 0;
13215   enum dwarf_location_atom op;
13216
13217   /* ??? Most of the time we do not take proper care for sign/zero
13218      extending the values properly.  Hopefully this won't be a real
13219      problem...  */
13220
13221   switch (TREE_CODE (loc))
13222     {
13223     case ERROR_MARK:
13224       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13225       return 0;
13226
13227     case PLACEHOLDER_EXPR:
13228       /* This case involves extracting fields from an object to determine the
13229          position of other fields.  We don't try to encode this here.  The
13230          only user of this is Ada, which encodes the needed information using
13231          the names of types.  */
13232       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13233       return 0;
13234
13235     case CALL_EXPR:
13236       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13237       /* There are no opcodes for these operations.  */
13238       return 0;
13239
13240     case PREINCREMENT_EXPR:
13241     case PREDECREMENT_EXPR:
13242     case POSTINCREMENT_EXPR:
13243     case POSTDECREMENT_EXPR:
13244       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13245       /* There are no opcodes for these operations.  */
13246       return 0;
13247
13248     case ADDR_EXPR:
13249       /* If we already want an address, see if there is INDIRECT_REF inside
13250          e.g. for &this->field.  */
13251       if (want_address)
13252         {
13253           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13254                        (loc, want_address == 2);
13255           if (list_ret)
13256             have_address = 1;
13257           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13258                    && (ret = cst_pool_loc_descr (loc)))
13259             have_address = 1;
13260         }
13261         /* Otherwise, process the argument and look for the address.  */
13262       if (!list_ret && !ret)
13263         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13264       else
13265         {
13266           if (want_address)
13267             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13268           return NULL;
13269         }
13270       break;
13271
13272     case VAR_DECL:
13273       if (DECL_THREAD_LOCAL_P (loc))
13274         {
13275           rtx rtl;
13276           enum dwarf_location_atom first_op;
13277           enum dwarf_location_atom second_op;
13278           bool dtprel = false;
13279
13280           if (targetm.have_tls)
13281             {
13282               /* If this is not defined, we have no way to emit the
13283                  data.  */
13284               if (!targetm.asm_out.output_dwarf_dtprel)
13285                 return 0;
13286
13287                /* The way DW_OP_GNU_push_tls_address is specified, we
13288                   can only look up addresses of objects in the current
13289                   module.  We used DW_OP_addr as first op, but that's
13290                   wrong, because DW_OP_addr is relocated by the debug
13291                   info consumer, while DW_OP_GNU_push_tls_address
13292                   operand shouldn't be.  */
13293               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13294                 return 0;
13295               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13296               dtprel = true;
13297               second_op = DW_OP_GNU_push_tls_address;
13298             }
13299           else
13300             {
13301               if (!targetm.emutls.debug_form_tls_address
13302                   || !(dwarf_version >= 3 || !dwarf_strict))
13303                 return 0;
13304               /* We stuffed the control variable into the DECL_VALUE_EXPR
13305                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13306                  no longer appear in gimple code.  We used the control
13307                  variable in specific so that we could pick it up here.  */
13308               loc = DECL_VALUE_EXPR (loc);
13309               first_op = DW_OP_addr;
13310               second_op = DW_OP_form_tls_address;
13311             }
13312
13313           rtl = rtl_for_decl_location (loc);
13314           if (rtl == NULL_RTX)
13315             return 0;
13316
13317           if (!MEM_P (rtl))
13318             return 0;
13319           rtl = XEXP (rtl, 0);
13320           if (! CONSTANT_P (rtl))
13321             return 0;
13322
13323           ret = new_loc_descr (first_op, 0, 0);
13324           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13325           ret->dw_loc_oprnd1.v.val_addr = rtl;
13326           ret->dtprel = dtprel;
13327
13328           ret1 = new_loc_descr (second_op, 0, 0);
13329           add_loc_descr (&ret, ret1);
13330
13331           have_address = 1;
13332           break;
13333         }
13334       /* FALLTHRU */
13335
13336     case PARM_DECL:
13337     case RESULT_DECL:
13338       if (DECL_HAS_VALUE_EXPR_P (loc))
13339         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13340                                    want_address);
13341       /* FALLTHRU */
13342
13343     case FUNCTION_DECL:
13344       {
13345         rtx rtl;
13346         var_loc_list *loc_list = lookup_decl_loc (loc);
13347
13348         if (loc_list && loc_list->first)
13349           {
13350             list_ret = dw_loc_list (loc_list, loc, want_address);
13351             have_address = want_address != 0;
13352             break;
13353           }
13354         rtl = rtl_for_decl_location (loc);
13355         if (rtl == NULL_RTX)
13356           {
13357             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13358             return 0;
13359           }
13360         else if (CONST_INT_P (rtl))
13361           {
13362             HOST_WIDE_INT val = INTVAL (rtl);
13363             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13364               val &= GET_MODE_MASK (DECL_MODE (loc));
13365             ret = int_loc_descriptor (val);
13366           }
13367         else if (GET_CODE (rtl) == CONST_STRING)
13368           {
13369             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13370             return 0;
13371           }
13372         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13373           {
13374             ret = new_loc_descr (DW_OP_addr, 0, 0);
13375             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13376             ret->dw_loc_oprnd1.v.val_addr = rtl;
13377           }
13378         else
13379           {
13380             enum machine_mode mode, mem_mode;
13381
13382             /* Certain constructs can only be represented at top-level.  */
13383             if (want_address == 2)
13384               {
13385                 ret = loc_descriptor (rtl, VOIDmode,
13386                                       VAR_INIT_STATUS_INITIALIZED);
13387                 have_address = 1;
13388               }
13389             else
13390               {
13391                 mode = GET_MODE (rtl);
13392                 mem_mode = VOIDmode;
13393                 if (MEM_P (rtl))
13394                   {
13395                     mem_mode = mode;
13396                     mode = get_address_mode (rtl);
13397                     rtl = XEXP (rtl, 0);
13398                     have_address = 1;
13399                   }
13400                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13401                                           VAR_INIT_STATUS_INITIALIZED);
13402               }
13403             if (!ret)
13404               expansion_failed (loc, rtl,
13405                                 "failed to produce loc descriptor for rtl");
13406           }
13407       }
13408       break;
13409
13410     case MEM_REF:
13411       /* ??? FIXME.  */
13412       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13413         return 0;
13414       /* Fallthru.  */
13415     case INDIRECT_REF:
13416       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13417       have_address = 1;
13418       break;
13419
13420     case COMPOUND_EXPR:
13421       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13422
13423     CASE_CONVERT:
13424     case VIEW_CONVERT_EXPR:
13425     case SAVE_EXPR:
13426     case MODIFY_EXPR:
13427       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13428
13429     case COMPONENT_REF:
13430     case BIT_FIELD_REF:
13431     case ARRAY_REF:
13432     case ARRAY_RANGE_REF:
13433     case REALPART_EXPR:
13434     case IMAGPART_EXPR:
13435       {
13436         tree obj, offset;
13437         HOST_WIDE_INT bitsize, bitpos, bytepos;
13438         enum machine_mode mode;
13439         int volatilep;
13440         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13441
13442         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13443                                    &unsignedp, &volatilep, false);
13444
13445         gcc_assert (obj != loc);
13446
13447         list_ret = loc_list_from_tree (obj,
13448                                        want_address == 2
13449                                        && !bitpos && !offset ? 2 : 1);
13450         /* TODO: We can extract value of the small expression via shifting even
13451            for nonzero bitpos.  */
13452         if (list_ret == 0)
13453           return 0;
13454         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13455           {
13456             expansion_failed (loc, NULL_RTX,
13457                               "bitfield access");
13458             return 0;
13459           }
13460
13461         if (offset != NULL_TREE)
13462           {
13463             /* Variable offset.  */
13464             list_ret1 = loc_list_from_tree (offset, 0);
13465             if (list_ret1 == 0)
13466               return 0;
13467             add_loc_list (&list_ret, list_ret1);
13468             if (!list_ret)
13469               return 0;
13470             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13471           }
13472
13473         bytepos = bitpos / BITS_PER_UNIT;
13474         if (bytepos > 0)
13475           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13476         else if (bytepos < 0)
13477           loc_list_plus_const (list_ret, bytepos);
13478
13479         have_address = 1;
13480         break;
13481       }
13482
13483     case INTEGER_CST:
13484       if ((want_address || !host_integerp (loc, 0))
13485           && (ret = cst_pool_loc_descr (loc)))
13486         have_address = 1;
13487       else if (want_address == 2
13488                && host_integerp (loc, 0)
13489                && (ret = address_of_int_loc_descriptor
13490                            (int_size_in_bytes (TREE_TYPE (loc)),
13491                             tree_low_cst (loc, 0))))
13492         have_address = 1;
13493       else if (host_integerp (loc, 0))
13494         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13495       else
13496         {
13497           expansion_failed (loc, NULL_RTX,
13498                             "Integer operand is not host integer");
13499           return 0;
13500         }
13501       break;
13502
13503     case CONSTRUCTOR:
13504     case REAL_CST:
13505     case STRING_CST:
13506     case COMPLEX_CST:
13507       if ((ret = cst_pool_loc_descr (loc)))
13508         have_address = 1;
13509       else
13510       /* We can construct small constants here using int_loc_descriptor.  */
13511         expansion_failed (loc, NULL_RTX,
13512                           "constructor or constant not in constant pool");
13513       break;
13514
13515     case TRUTH_AND_EXPR:
13516     case TRUTH_ANDIF_EXPR:
13517     case BIT_AND_EXPR:
13518       op = DW_OP_and;
13519       goto do_binop;
13520
13521     case TRUTH_XOR_EXPR:
13522     case BIT_XOR_EXPR:
13523       op = DW_OP_xor;
13524       goto do_binop;
13525
13526     case TRUTH_OR_EXPR:
13527     case TRUTH_ORIF_EXPR:
13528     case BIT_IOR_EXPR:
13529       op = DW_OP_or;
13530       goto do_binop;
13531
13532     case FLOOR_DIV_EXPR:
13533     case CEIL_DIV_EXPR:
13534     case ROUND_DIV_EXPR:
13535     case TRUNC_DIV_EXPR:
13536       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13537         return 0;
13538       op = DW_OP_div;
13539       goto do_binop;
13540
13541     case MINUS_EXPR:
13542       op = DW_OP_minus;
13543       goto do_binop;
13544
13545     case FLOOR_MOD_EXPR:
13546     case CEIL_MOD_EXPR:
13547     case ROUND_MOD_EXPR:
13548     case TRUNC_MOD_EXPR:
13549       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13550         {
13551           op = DW_OP_mod;
13552           goto do_binop;
13553         }
13554       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13555       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13556       if (list_ret == 0 || list_ret1 == 0)
13557         return 0;
13558
13559       add_loc_list (&list_ret, list_ret1);
13560       if (list_ret == 0)
13561         return 0;
13562       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13563       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13564       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13565       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13566       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13567       break;
13568
13569     case MULT_EXPR:
13570       op = DW_OP_mul;
13571       goto do_binop;
13572
13573     case LSHIFT_EXPR:
13574       op = DW_OP_shl;
13575       goto do_binop;
13576
13577     case RSHIFT_EXPR:
13578       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13579       goto do_binop;
13580
13581     case POINTER_PLUS_EXPR:
13582     case PLUS_EXPR:
13583       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13584         {
13585           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13586           if (list_ret == 0)
13587             return 0;
13588
13589           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13590           break;
13591         }
13592
13593       op = DW_OP_plus;
13594       goto do_binop;
13595
13596     case LE_EXPR:
13597       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13598         return 0;
13599
13600       op = DW_OP_le;
13601       goto do_binop;
13602
13603     case GE_EXPR:
13604       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13605         return 0;
13606
13607       op = DW_OP_ge;
13608       goto do_binop;
13609
13610     case LT_EXPR:
13611       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13612         return 0;
13613
13614       op = DW_OP_lt;
13615       goto do_binop;
13616
13617     case GT_EXPR:
13618       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13619         return 0;
13620
13621       op = DW_OP_gt;
13622       goto do_binop;
13623
13624     case EQ_EXPR:
13625       op = DW_OP_eq;
13626       goto do_binop;
13627
13628     case NE_EXPR:
13629       op = DW_OP_ne;
13630       goto do_binop;
13631
13632     do_binop:
13633       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13634       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13635       if (list_ret == 0 || list_ret1 == 0)
13636         return 0;
13637
13638       add_loc_list (&list_ret, list_ret1);
13639       if (list_ret == 0)
13640         return 0;
13641       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13642       break;
13643
13644     case TRUTH_NOT_EXPR:
13645     case BIT_NOT_EXPR:
13646       op = DW_OP_not;
13647       goto do_unop;
13648
13649     case ABS_EXPR:
13650       op = DW_OP_abs;
13651       goto do_unop;
13652
13653     case NEGATE_EXPR:
13654       op = DW_OP_neg;
13655       goto do_unop;
13656
13657     do_unop:
13658       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13659       if (list_ret == 0)
13660         return 0;
13661
13662       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13663       break;
13664
13665     case MIN_EXPR:
13666     case MAX_EXPR:
13667       {
13668         const enum tree_code code =
13669           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13670
13671         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13672                       build2 (code, integer_type_node,
13673                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13674                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13675       }
13676
13677       /* ... fall through ...  */
13678
13679     case COND_EXPR:
13680       {
13681         dw_loc_descr_ref lhs
13682           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13683         dw_loc_list_ref rhs
13684           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13685         dw_loc_descr_ref bra_node, jump_node, tmp;
13686
13687         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13688         if (list_ret == 0 || lhs == 0 || rhs == 0)
13689           return 0;
13690
13691         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13692         add_loc_descr_to_each (list_ret, bra_node);
13693
13694         add_loc_list (&list_ret, rhs);
13695         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13696         add_loc_descr_to_each (list_ret, jump_node);
13697
13698         add_loc_descr_to_each (list_ret, lhs);
13699         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13700         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13701
13702         /* ??? Need a node to point the skip at.  Use a nop.  */
13703         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13704         add_loc_descr_to_each (list_ret, tmp);
13705         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13706         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13707       }
13708       break;
13709
13710     case FIX_TRUNC_EXPR:
13711       return 0;
13712
13713     default:
13714       /* Leave front-end specific codes as simply unknown.  This comes
13715          up, for instance, with the C STMT_EXPR.  */
13716       if ((unsigned int) TREE_CODE (loc)
13717           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13718         {
13719           expansion_failed (loc, NULL_RTX,
13720                             "language specific tree node");
13721           return 0;
13722         }
13723
13724 #ifdef ENABLE_CHECKING
13725       /* Otherwise this is a generic code; we should just lists all of
13726          these explicitly.  We forgot one.  */
13727       gcc_unreachable ();
13728 #else
13729       /* In a release build, we want to degrade gracefully: better to
13730          generate incomplete debugging information than to crash.  */
13731       return NULL;
13732 #endif
13733     }
13734
13735   if (!ret && !list_ret)
13736     return 0;
13737
13738   if (want_address == 2 && !have_address
13739       && (dwarf_version >= 4 || !dwarf_strict))
13740     {
13741       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13742         {
13743           expansion_failed (loc, NULL_RTX,
13744                             "DWARF address size mismatch");
13745           return 0;
13746         }
13747       if (ret)
13748         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13749       else
13750         add_loc_descr_to_each (list_ret,
13751                                new_loc_descr (DW_OP_stack_value, 0, 0));
13752       have_address = 1;
13753     }
13754   /* Show if we can't fill the request for an address.  */
13755   if (want_address && !have_address)
13756     {
13757       expansion_failed (loc, NULL_RTX,
13758                         "Want address and only have value");
13759       return 0;
13760     }
13761
13762   gcc_assert (!ret || !list_ret);
13763
13764   /* If we've got an address and don't want one, dereference.  */
13765   if (!want_address && have_address)
13766     {
13767       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13768
13769       if (size > DWARF2_ADDR_SIZE || size == -1)
13770         {
13771           expansion_failed (loc, NULL_RTX,
13772                             "DWARF address size mismatch");
13773           return 0;
13774         }
13775       else if (size == DWARF2_ADDR_SIZE)
13776         op = DW_OP_deref;
13777       else
13778         op = DW_OP_deref_size;
13779
13780       if (ret)
13781         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13782       else
13783         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13784     }
13785   if (ret)
13786     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13787
13788   return list_ret;
13789 }
13790
13791 /* Same as above but return only single location expression.  */
13792 static dw_loc_descr_ref
13793 loc_descriptor_from_tree (tree loc, int want_address)
13794 {
13795   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13796   if (!ret)
13797     return NULL;
13798   if (ret->dw_loc_next)
13799     {
13800       expansion_failed (loc, NULL_RTX,
13801                         "Location list where only loc descriptor needed");
13802       return NULL;
13803     }
13804   return ret->expr;
13805 }
13806
13807 /* Given a value, round it up to the lowest multiple of `boundary'
13808    which is not less than the value itself.  */
13809
13810 static inline HOST_WIDE_INT
13811 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13812 {
13813   return (((value + boundary - 1) / boundary) * boundary);
13814 }
13815
13816 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13817    pointer to the declared type for the relevant field variable, or return
13818    `integer_type_node' if the given node turns out to be an
13819    ERROR_MARK node.  */
13820
13821 static inline tree
13822 field_type (const_tree decl)
13823 {
13824   tree type;
13825
13826   if (TREE_CODE (decl) == ERROR_MARK)
13827     return integer_type_node;
13828
13829   type = DECL_BIT_FIELD_TYPE (decl);
13830   if (type == NULL_TREE)
13831     type = TREE_TYPE (decl);
13832
13833   return type;
13834 }
13835
13836 /* Given a pointer to a tree node, return the alignment in bits for
13837    it, or else return BITS_PER_WORD if the node actually turns out to
13838    be an ERROR_MARK node.  */
13839
13840 static inline unsigned
13841 simple_type_align_in_bits (const_tree type)
13842 {
13843   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13844 }
13845
13846 static inline unsigned
13847 simple_decl_align_in_bits (const_tree decl)
13848 {
13849   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13850 }
13851
13852 /* Return the result of rounding T up to ALIGN.  */
13853
13854 static inline double_int
13855 round_up_to_align (double_int t, unsigned int align)
13856 {
13857   double_int alignd = uhwi_to_double_int (align);
13858   t = double_int_add (t, alignd);
13859   t = double_int_add (t, double_int_minus_one);
13860   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13861   t = double_int_mul (t, alignd);
13862   return t;
13863 }
13864
13865 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13866    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13867    or return 0 if we are unable to determine what that offset is, either
13868    because the argument turns out to be a pointer to an ERROR_MARK node, or
13869    because the offset is actually variable.  (We can't handle the latter case
13870    just yet).  */
13871
13872 static HOST_WIDE_INT
13873 field_byte_offset (const_tree decl)
13874 {
13875   double_int object_offset_in_bits;
13876   double_int object_offset_in_bytes;
13877   double_int bitpos_int;
13878
13879   if (TREE_CODE (decl) == ERROR_MARK)
13880     return 0;
13881
13882   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13883
13884   /* We cannot yet cope with fields whose positions are variable, so
13885      for now, when we see such things, we simply return 0.  Someday, we may
13886      be able to handle such cases, but it will be damn difficult.  */
13887   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13888     return 0;
13889
13890   bitpos_int = tree_to_double_int (bit_position (decl));
13891
13892 #ifdef PCC_BITFIELD_TYPE_MATTERS
13893   if (PCC_BITFIELD_TYPE_MATTERS)
13894     {
13895       tree type;
13896       tree field_size_tree;
13897       double_int deepest_bitpos;
13898       double_int field_size_in_bits;
13899       unsigned int type_align_in_bits;
13900       unsigned int decl_align_in_bits;
13901       double_int type_size_in_bits;
13902
13903       type = field_type (decl);
13904       type_size_in_bits = double_int_type_size_in_bits (type);
13905       type_align_in_bits = simple_type_align_in_bits (type);
13906
13907       field_size_tree = DECL_SIZE (decl);
13908
13909       /* The size could be unspecified if there was an error, or for
13910          a flexible array member.  */
13911       if (!field_size_tree)
13912         field_size_tree = bitsize_zero_node;
13913
13914       /* If the size of the field is not constant, use the type size.  */
13915       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13916         field_size_in_bits = tree_to_double_int (field_size_tree);
13917       else
13918         field_size_in_bits = type_size_in_bits;
13919
13920       decl_align_in_bits = simple_decl_align_in_bits (decl);
13921
13922       /* The GCC front-end doesn't make any attempt to keep track of the
13923          starting bit offset (relative to the start of the containing
13924          structure type) of the hypothetical "containing object" for a
13925          bit-field.  Thus, when computing the byte offset value for the
13926          start of the "containing object" of a bit-field, we must deduce
13927          this information on our own. This can be rather tricky to do in
13928          some cases.  For example, handling the following structure type
13929          definition when compiling for an i386/i486 target (which only
13930          aligns long long's to 32-bit boundaries) can be very tricky:
13931
13932          struct S { int field1; long long field2:31; };
13933
13934          Fortunately, there is a simple rule-of-thumb which can be used
13935          in such cases.  When compiling for an i386/i486, GCC will
13936          allocate 8 bytes for the structure shown above.  It decides to
13937          do this based upon one simple rule for bit-field allocation.
13938          GCC allocates each "containing object" for each bit-field at
13939          the first (i.e. lowest addressed) legitimate alignment boundary
13940          (based upon the required minimum alignment for the declared
13941          type of the field) which it can possibly use, subject to the
13942          condition that there is still enough available space remaining
13943          in the containing object (when allocated at the selected point)
13944          to fully accommodate all of the bits of the bit-field itself.
13945
13946          This simple rule makes it obvious why GCC allocates 8 bytes for
13947          each object of the structure type shown above.  When looking
13948          for a place to allocate the "containing object" for `field2',
13949          the compiler simply tries to allocate a 64-bit "containing
13950          object" at each successive 32-bit boundary (starting at zero)
13951          until it finds a place to allocate that 64- bit field such that
13952          at least 31 contiguous (and previously unallocated) bits remain
13953          within that selected 64 bit field.  (As it turns out, for the
13954          example above, the compiler finds it is OK to allocate the
13955          "containing object" 64-bit field at bit-offset zero within the
13956          structure type.)
13957
13958          Here we attempt to work backwards from the limited set of facts
13959          we're given, and we try to deduce from those facts, where GCC
13960          must have believed that the containing object started (within
13961          the structure type). The value we deduce is then used (by the
13962          callers of this routine) to generate DW_AT_location and
13963          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13964          the case of DW_AT_location, regular fields as well).  */
13965
13966       /* Figure out the bit-distance from the start of the structure to
13967          the "deepest" bit of the bit-field.  */
13968       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
13969
13970       /* This is the tricky part.  Use some fancy footwork to deduce
13971          where the lowest addressed bit of the containing object must
13972          be.  */
13973       object_offset_in_bits
13974         = double_int_sub (deepest_bitpos, type_size_in_bits);
13975
13976       /* Round up to type_align by default.  This works best for
13977          bitfields.  */
13978       object_offset_in_bits
13979         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13980
13981       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13982         {
13983           object_offset_in_bits
13984             = double_int_sub (deepest_bitpos, type_size_in_bits);
13985
13986           /* Round up to decl_align instead.  */
13987           object_offset_in_bits
13988             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13989         }
13990     }
13991   else
13992 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13993     object_offset_in_bits = bitpos_int;
13994
13995   object_offset_in_bytes
13996     = double_int_div (object_offset_in_bits,
13997                       uhwi_to_double_int (BITS_PER_UNIT), true,
13998                       TRUNC_DIV_EXPR);
13999   return double_int_to_shwi (object_offset_in_bytes);
14000 }
14001 \f
14002 /* The following routines define various Dwarf attributes and any data
14003    associated with them.  */
14004
14005 /* Add a location description attribute value to a DIE.
14006
14007    This emits location attributes suitable for whole variables and
14008    whole parameters.  Note that the location attributes for struct fields are
14009    generated by the routine `data_member_location_attribute' below.  */
14010
14011 static inline void
14012 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14013                              dw_loc_list_ref descr)
14014 {
14015   if (descr == 0)
14016     return;
14017   if (single_element_loc_list_p (descr))
14018     add_AT_loc (die, attr_kind, descr->expr);
14019   else
14020     add_AT_loc_list (die, attr_kind, descr);
14021 }
14022
14023 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14024
14025 static void
14026 add_accessibility_attribute (dw_die_ref die, tree decl)
14027 {
14028   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14029      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14030      the default has always been DW_ACCESS_public.  */
14031   if (TREE_PROTECTED (decl))
14032     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14033   else if (TREE_PRIVATE (decl))
14034     {
14035       if (dwarf_version == 2
14036           || die->die_parent == NULL
14037           || die->die_parent->die_tag != DW_TAG_class_type)
14038         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14039     }
14040   else if (dwarf_version > 2
14041            && die->die_parent
14042            && die->die_parent->die_tag == DW_TAG_class_type)
14043     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14044 }
14045
14046 /* Attach the specialized form of location attribute used for data members of
14047    struct and union types.  In the special case of a FIELD_DECL node which
14048    represents a bit-field, the "offset" part of this special location
14049    descriptor must indicate the distance in bytes from the lowest-addressed
14050    byte of the containing struct or union type to the lowest-addressed byte of
14051    the "containing object" for the bit-field.  (See the `field_byte_offset'
14052    function above).
14053
14054    For any given bit-field, the "containing object" is a hypothetical object
14055    (of some integral or enum type) within which the given bit-field lives.  The
14056    type of this hypothetical "containing object" is always the same as the
14057    declared type of the individual bit-field itself (for GCC anyway... the
14058    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14059    bytes) of the hypothetical "containing object" which will be given in the
14060    DW_AT_byte_size attribute for this bit-field.  (See the
14061    `byte_size_attribute' function below.)  It is also used when calculating the
14062    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14063    function below.)  */
14064
14065 static void
14066 add_data_member_location_attribute (dw_die_ref die, tree decl)
14067 {
14068   HOST_WIDE_INT offset;
14069   dw_loc_descr_ref loc_descr = 0;
14070
14071   if (TREE_CODE (decl) == TREE_BINFO)
14072     {
14073       /* We're working on the TAG_inheritance for a base class.  */
14074       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14075         {
14076           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14077              aren't at a fixed offset from all (sub)objects of the same
14078              type.  We need to extract the appropriate offset from our
14079              vtable.  The following dwarf expression means
14080
14081                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14082
14083              This is specific to the V3 ABI, of course.  */
14084
14085           dw_loc_descr_ref tmp;
14086
14087           /* Make a copy of the object address.  */
14088           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14089           add_loc_descr (&loc_descr, tmp);
14090
14091           /* Extract the vtable address.  */
14092           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14093           add_loc_descr (&loc_descr, tmp);
14094
14095           /* Calculate the address of the offset.  */
14096           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14097           gcc_assert (offset < 0);
14098
14099           tmp = int_loc_descriptor (-offset);
14100           add_loc_descr (&loc_descr, tmp);
14101           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14102           add_loc_descr (&loc_descr, tmp);
14103
14104           /* Extract the offset.  */
14105           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14106           add_loc_descr (&loc_descr, tmp);
14107
14108           /* Add it to the object address.  */
14109           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14110           add_loc_descr (&loc_descr, tmp);
14111         }
14112       else
14113         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14114     }
14115   else
14116     offset = field_byte_offset (decl);
14117
14118   if (! loc_descr)
14119     {
14120       if (dwarf_version > 2)
14121         {
14122           /* Don't need to output a location expression, just the constant. */
14123           if (offset < 0)
14124             add_AT_int (die, DW_AT_data_member_location, offset);
14125           else
14126             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14127           return;
14128         }
14129       else
14130         {
14131           enum dwarf_location_atom op;
14132
14133           /* The DWARF2 standard says that we should assume that the structure
14134              address is already on the stack, so we can specify a structure
14135              field address by using DW_OP_plus_uconst.  */
14136
14137 #ifdef MIPS_DEBUGGING_INFO
14138           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14139              operator correctly.  It works only if we leave the offset on the
14140              stack.  */
14141           op = DW_OP_constu;
14142 #else
14143           op = DW_OP_plus_uconst;
14144 #endif
14145
14146           loc_descr = new_loc_descr (op, offset, 0);
14147         }
14148     }
14149
14150   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14151 }
14152
14153 /* Writes integer values to dw_vec_const array.  */
14154
14155 static void
14156 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14157 {
14158   while (size != 0)
14159     {
14160       *dest++ = val & 0xff;
14161       val >>= 8;
14162       --size;
14163     }
14164 }
14165
14166 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14167
14168 static HOST_WIDE_INT
14169 extract_int (const unsigned char *src, unsigned int size)
14170 {
14171   HOST_WIDE_INT val = 0;
14172
14173   src += size;
14174   while (size != 0)
14175     {
14176       val <<= 8;
14177       val |= *--src & 0xff;
14178       --size;
14179     }
14180   return val;
14181 }
14182
14183 /* Writes double_int values to dw_vec_const array.  */
14184
14185 static void
14186 insert_double (double_int val, unsigned char *dest)
14187 {
14188   unsigned char *p0 = dest;
14189   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14190
14191   if (WORDS_BIG_ENDIAN)
14192     {
14193       p0 = p1;
14194       p1 = dest;
14195     }
14196
14197   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14198   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14199 }
14200
14201 /* Writes floating point values to dw_vec_const array.  */
14202
14203 static void
14204 insert_float (const_rtx rtl, unsigned char *array)
14205 {
14206   REAL_VALUE_TYPE rv;
14207   long val[4];
14208   int i;
14209
14210   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14211   real_to_target (val, &rv, GET_MODE (rtl));
14212
14213   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14214   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14215     {
14216       insert_int (val[i], 4, array);
14217       array += 4;
14218     }
14219 }
14220
14221 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14222    does not have a "location" either in memory or in a register.  These
14223    things can arise in GNU C when a constant is passed as an actual parameter
14224    to an inlined function.  They can also arise in C++ where declared
14225    constants do not necessarily get memory "homes".  */
14226
14227 static bool
14228 add_const_value_attribute (dw_die_ref die, rtx rtl)
14229 {
14230   switch (GET_CODE (rtl))
14231     {
14232     case CONST_INT:
14233       {
14234         HOST_WIDE_INT val = INTVAL (rtl);
14235
14236         if (val < 0)
14237           add_AT_int (die, DW_AT_const_value, val);
14238         else
14239           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14240       }
14241       return true;
14242
14243     case CONST_DOUBLE:
14244       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14245          floating-point constant.  A CONST_DOUBLE is used whenever the
14246          constant requires more than one word in order to be adequately
14247          represented.  */
14248       {
14249         enum machine_mode mode = GET_MODE (rtl);
14250
14251         if (SCALAR_FLOAT_MODE_P (mode))
14252           {
14253             unsigned int length = GET_MODE_SIZE (mode);
14254             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14255
14256             insert_float (rtl, array);
14257             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14258           }
14259         else
14260           add_AT_double (die, DW_AT_const_value,
14261                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14262       }
14263       return true;
14264
14265     case CONST_VECTOR:
14266       {
14267         enum machine_mode mode = GET_MODE (rtl);
14268         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14269         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14270         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14271           (length * elt_size);
14272         unsigned int i;
14273         unsigned char *p;
14274
14275         switch (GET_MODE_CLASS (mode))
14276           {
14277           case MODE_VECTOR_INT:
14278             for (i = 0, p = array; i < length; i++, p += elt_size)
14279               {
14280                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14281                 double_int val = rtx_to_double_int (elt);
14282
14283                 if (elt_size <= sizeof (HOST_WIDE_INT))
14284                   insert_int (double_int_to_shwi (val), elt_size, p);
14285                 else
14286                   {
14287                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14288                     insert_double (val, p);
14289                   }
14290               }
14291             break;
14292
14293           case MODE_VECTOR_FLOAT:
14294             for (i = 0, p = array; i < length; i++, p += elt_size)
14295               {
14296                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14297                 insert_float (elt, p);
14298               }
14299             break;
14300
14301           default:
14302             gcc_unreachable ();
14303           }
14304
14305         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14306       }
14307       return true;
14308
14309     case CONST_STRING:
14310       if (dwarf_version >= 4 || !dwarf_strict)
14311         {
14312           dw_loc_descr_ref loc_result;
14313           resolve_one_addr (&rtl, NULL);
14314         rtl_addr:
14315           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14316           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14317           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14318           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14319           add_AT_loc (die, DW_AT_location, loc_result);
14320           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14321           return true;
14322         }
14323       return false;
14324
14325     case CONST:
14326       if (CONSTANT_P (XEXP (rtl, 0)))
14327         return add_const_value_attribute (die, XEXP (rtl, 0));
14328       /* FALLTHROUGH */
14329     case SYMBOL_REF:
14330       if (!const_ok_for_output (rtl))
14331         return false;
14332     case LABEL_REF:
14333       if (dwarf_version >= 4 || !dwarf_strict)
14334         goto rtl_addr;
14335       return false;
14336
14337     case PLUS:
14338       /* In cases where an inlined instance of an inline function is passed
14339          the address of an `auto' variable (which is local to the caller) we
14340          can get a situation where the DECL_RTL of the artificial local
14341          variable (for the inlining) which acts as a stand-in for the
14342          corresponding formal parameter (of the inline function) will look
14343          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14344          exactly a compile-time constant expression, but it isn't the address
14345          of the (artificial) local variable either.  Rather, it represents the
14346          *value* which the artificial local variable always has during its
14347          lifetime.  We currently have no way to represent such quasi-constant
14348          values in Dwarf, so for now we just punt and generate nothing.  */
14349       return false;
14350
14351     case HIGH:
14352     case CONST_FIXED:
14353       return false;
14354
14355     case MEM:
14356       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14357           && MEM_READONLY_P (rtl)
14358           && GET_MODE (rtl) == BLKmode)
14359         {
14360           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14361           return true;
14362         }
14363       return false;
14364
14365     default:
14366       /* No other kinds of rtx should be possible here.  */
14367       gcc_unreachable ();
14368     }
14369   return false;
14370 }
14371
14372 /* Determine whether the evaluation of EXPR references any variables
14373    or functions which aren't otherwise used (and therefore may not be
14374    output).  */
14375 static tree
14376 reference_to_unused (tree * tp, int * walk_subtrees,
14377                      void * data ATTRIBUTE_UNUSED)
14378 {
14379   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14380     *walk_subtrees = 0;
14381
14382   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14383       && ! TREE_ASM_WRITTEN (*tp))
14384     return *tp;
14385   /* ???  The C++ FE emits debug information for using decls, so
14386      putting gcc_unreachable here falls over.  See PR31899.  For now
14387      be conservative.  */
14388   else if (!cgraph_global_info_ready
14389            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14390     return *tp;
14391   else if (TREE_CODE (*tp) == VAR_DECL)
14392     {
14393       struct varpool_node *node = varpool_get_node (*tp);
14394       if (!node || !node->needed)
14395         return *tp;
14396     }
14397   else if (TREE_CODE (*tp) == FUNCTION_DECL
14398            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14399     {
14400       /* The call graph machinery must have finished analyzing,
14401          optimizing and gimplifying the CU by now.
14402          So if *TP has no call graph node associated
14403          to it, it means *TP will not be emitted.  */
14404       if (!cgraph_get_node (*tp))
14405         return *tp;
14406     }
14407   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14408     return *tp;
14409
14410   return NULL_TREE;
14411 }
14412
14413 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14414    for use in a later add_const_value_attribute call.  */
14415
14416 static rtx
14417 rtl_for_decl_init (tree init, tree type)
14418 {
14419   rtx rtl = NULL_RTX;
14420
14421   STRIP_NOPS (init);
14422
14423   /* If a variable is initialized with a string constant without embedded
14424      zeros, build CONST_STRING.  */
14425   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14426     {
14427       tree enttype = TREE_TYPE (type);
14428       tree domain = TYPE_DOMAIN (type);
14429       enum machine_mode mode = TYPE_MODE (enttype);
14430
14431       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14432           && domain
14433           && integer_zerop (TYPE_MIN_VALUE (domain))
14434           && compare_tree_int (TYPE_MAX_VALUE (domain),
14435                                TREE_STRING_LENGTH (init) - 1) == 0
14436           && ((size_t) TREE_STRING_LENGTH (init)
14437               == strlen (TREE_STRING_POINTER (init)) + 1))
14438         {
14439           rtl = gen_rtx_CONST_STRING (VOIDmode,
14440                                       ggc_strdup (TREE_STRING_POINTER (init)));
14441           rtl = gen_rtx_MEM (BLKmode, rtl);
14442           MEM_READONLY_P (rtl) = 1;
14443         }
14444     }
14445   /* Other aggregates, and complex values, could be represented using
14446      CONCAT: FIXME!  */
14447   else if (AGGREGATE_TYPE_P (type)
14448            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14449                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14450            || TREE_CODE (type) == COMPLEX_TYPE)
14451     ;
14452   /* Vectors only work if their mode is supported by the target.
14453      FIXME: generic vectors ought to work too.  */
14454   else if (TREE_CODE (type) == VECTOR_TYPE
14455            && !VECTOR_MODE_P (TYPE_MODE (type)))
14456     ;
14457   /* If the initializer is something that we know will expand into an
14458      immediate RTL constant, expand it now.  We must be careful not to
14459      reference variables which won't be output.  */
14460   else if (initializer_constant_valid_p (init, type)
14461            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14462     {
14463       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14464          possible.  */
14465       if (TREE_CODE (type) == VECTOR_TYPE)
14466         switch (TREE_CODE (init))
14467           {
14468           case VECTOR_CST:
14469             break;
14470           case CONSTRUCTOR:
14471             if (TREE_CONSTANT (init))
14472               {
14473                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14474                 bool constant_p = true;
14475                 tree value;
14476                 unsigned HOST_WIDE_INT ix;
14477
14478                 /* Even when ctor is constant, it might contain non-*_CST
14479                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14480                    belong into VECTOR_CST nodes.  */
14481                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14482                   if (!CONSTANT_CLASS_P (value))
14483                     {
14484                       constant_p = false;
14485                       break;
14486                     }
14487
14488                 if (constant_p)
14489                   {
14490                     init = build_vector_from_ctor (type, elts);
14491                     break;
14492                   }
14493               }
14494             /* FALLTHRU */
14495
14496           default:
14497             return NULL;
14498           }
14499
14500       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14501
14502       /* If expand_expr returns a MEM, it wasn't immediate.  */
14503       gcc_assert (!rtl || !MEM_P (rtl));
14504     }
14505
14506   return rtl;
14507 }
14508
14509 /* Generate RTL for the variable DECL to represent its location.  */
14510
14511 static rtx
14512 rtl_for_decl_location (tree decl)
14513 {
14514   rtx rtl;
14515
14516   /* Here we have to decide where we are going to say the parameter "lives"
14517      (as far as the debugger is concerned).  We only have a couple of
14518      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14519
14520      DECL_RTL normally indicates where the parameter lives during most of the
14521      activation of the function.  If optimization is enabled however, this
14522      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14523      that the parameter doesn't really live anywhere (as far as the code
14524      generation parts of GCC are concerned) during most of the function's
14525      activation.  That will happen (for example) if the parameter is never
14526      referenced within the function.
14527
14528      We could just generate a location descriptor here for all non-NULL
14529      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14530      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14531      where DECL_RTL is NULL or is a pseudo-reg.
14532
14533      Note however that we can only get away with using DECL_INCOMING_RTL as
14534      a backup substitute for DECL_RTL in certain limited cases.  In cases
14535      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14536      we can be sure that the parameter was passed using the same type as it is
14537      declared to have within the function, and that its DECL_INCOMING_RTL
14538      points us to a place where a value of that type is passed.
14539
14540      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14541      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14542      because in these cases DECL_INCOMING_RTL points us to a value of some
14543      type which is *different* from the type of the parameter itself.  Thus,
14544      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14545      such cases, the debugger would end up (for example) trying to fetch a
14546      `float' from a place which actually contains the first part of a
14547      `double'.  That would lead to really incorrect and confusing
14548      output at debug-time.
14549
14550      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14551      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14552      are a couple of exceptions however.  On little-endian machines we can
14553      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14554      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14555      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14556      when (on a little-endian machine) a non-prototyped function has a
14557      parameter declared to be of type `short' or `char'.  In such cases,
14558      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14559      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14560      passed `int' value.  If the debugger then uses that address to fetch
14561      a `short' or a `char' (on a little-endian machine) the result will be
14562      the correct data, so we allow for such exceptional cases below.
14563
14564      Note that our goal here is to describe the place where the given formal
14565      parameter lives during most of the function's activation (i.e. between the
14566      end of the prologue and the start of the epilogue).  We'll do that as best
14567      as we can. Note however that if the given formal parameter is modified
14568      sometime during the execution of the function, then a stack backtrace (at
14569      debug-time) will show the function as having been called with the *new*
14570      value rather than the value which was originally passed in.  This happens
14571      rarely enough that it is not a major problem, but it *is* a problem, and
14572      I'd like to fix it.
14573
14574      A future version of dwarf2out.c may generate two additional attributes for
14575      any given DW_TAG_formal_parameter DIE which will describe the "passed
14576      type" and the "passed location" for the given formal parameter in addition
14577      to the attributes we now generate to indicate the "declared type" and the
14578      "active location" for each parameter.  This additional set of attributes
14579      could be used by debuggers for stack backtraces. Separately, note that
14580      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14581      This happens (for example) for inlined-instances of inline function formal
14582      parameters which are never referenced.  This really shouldn't be
14583      happening.  All PARM_DECL nodes should get valid non-NULL
14584      DECL_INCOMING_RTL values.  FIXME.  */
14585
14586   /* Use DECL_RTL as the "location" unless we find something better.  */
14587   rtl = DECL_RTL_IF_SET (decl);
14588
14589   /* When generating abstract instances, ignore everything except
14590      constants, symbols living in memory, and symbols living in
14591      fixed registers.  */
14592   if (! reload_completed)
14593     {
14594       if (rtl
14595           && (CONSTANT_P (rtl)
14596               || (MEM_P (rtl)
14597                   && CONSTANT_P (XEXP (rtl, 0)))
14598               || (REG_P (rtl)
14599                   && TREE_CODE (decl) == VAR_DECL
14600                   && TREE_STATIC (decl))))
14601         {
14602           rtl = targetm.delegitimize_address (rtl);
14603           return rtl;
14604         }
14605       rtl = NULL_RTX;
14606     }
14607   else if (TREE_CODE (decl) == PARM_DECL)
14608     {
14609       if (rtl == NULL_RTX
14610           || is_pseudo_reg (rtl)
14611           || (MEM_P (rtl)
14612               && is_pseudo_reg (XEXP (rtl, 0))
14613               && DECL_INCOMING_RTL (decl)
14614               && MEM_P (DECL_INCOMING_RTL (decl))
14615               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14616         {
14617           tree declared_type = TREE_TYPE (decl);
14618           tree passed_type = DECL_ARG_TYPE (decl);
14619           enum machine_mode dmode = TYPE_MODE (declared_type);
14620           enum machine_mode pmode = TYPE_MODE (passed_type);
14621
14622           /* This decl represents a formal parameter which was optimized out.
14623              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14624              all cases where (rtl == NULL_RTX) just below.  */
14625           if (dmode == pmode)
14626             rtl = DECL_INCOMING_RTL (decl);
14627           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14628                    && SCALAR_INT_MODE_P (dmode)
14629                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14630                    && DECL_INCOMING_RTL (decl))
14631             {
14632               rtx inc = DECL_INCOMING_RTL (decl);
14633               if (REG_P (inc))
14634                 rtl = inc;
14635               else if (MEM_P (inc))
14636                 {
14637                   if (BYTES_BIG_ENDIAN)
14638                     rtl = adjust_address_nv (inc, dmode,
14639                                              GET_MODE_SIZE (pmode)
14640                                              - GET_MODE_SIZE (dmode));
14641                   else
14642                     rtl = inc;
14643                 }
14644             }
14645         }
14646
14647       /* If the parm was passed in registers, but lives on the stack, then
14648          make a big endian correction if the mode of the type of the
14649          parameter is not the same as the mode of the rtl.  */
14650       /* ??? This is the same series of checks that are made in dbxout.c before
14651          we reach the big endian correction code there.  It isn't clear if all
14652          of these checks are necessary here, but keeping them all is the safe
14653          thing to do.  */
14654       else if (MEM_P (rtl)
14655                && XEXP (rtl, 0) != const0_rtx
14656                && ! CONSTANT_P (XEXP (rtl, 0))
14657                /* Not passed in memory.  */
14658                && !MEM_P (DECL_INCOMING_RTL (decl))
14659                /* Not passed by invisible reference.  */
14660                && (!REG_P (XEXP (rtl, 0))
14661                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14662                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14663 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14664                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14665 #endif
14666                      )
14667                /* Big endian correction check.  */
14668                && BYTES_BIG_ENDIAN
14669                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14670                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14671                    < UNITS_PER_WORD))
14672         {
14673           int offset = (UNITS_PER_WORD
14674                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14675
14676           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14677                              plus_constant (XEXP (rtl, 0), offset));
14678         }
14679     }
14680   else if (TREE_CODE (decl) == VAR_DECL
14681            && rtl
14682            && MEM_P (rtl)
14683            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14684            && BYTES_BIG_ENDIAN)
14685     {
14686       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14687       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14688
14689       /* If a variable is declared "register" yet is smaller than
14690          a register, then if we store the variable to memory, it
14691          looks like we're storing a register-sized value, when in
14692          fact we are not.  We need to adjust the offset of the
14693          storage location to reflect the actual value's bytes,
14694          else gdb will not be able to display it.  */
14695       if (rsize > dsize)
14696         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14697                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14698     }
14699
14700   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14701      and will have been substituted directly into all expressions that use it.
14702      C does not have such a concept, but C++ and other languages do.  */
14703   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14704     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14705
14706   if (rtl)
14707     rtl = targetm.delegitimize_address (rtl);
14708
14709   /* If we don't look past the constant pool, we risk emitting a
14710      reference to a constant pool entry that isn't referenced from
14711      code, and thus is not emitted.  */
14712   if (rtl)
14713     rtl = avoid_constant_pool_reference (rtl);
14714
14715   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14716      in the current CU, resolve_addr will remove the expression referencing
14717      it.  */
14718   if (rtl == NULL_RTX
14719       && TREE_CODE (decl) == VAR_DECL
14720       && !DECL_EXTERNAL (decl)
14721       && TREE_STATIC (decl)
14722       && DECL_NAME (decl)
14723       && !DECL_HARD_REGISTER (decl)
14724       && DECL_MODE (decl) != VOIDmode)
14725     {
14726       rtl = make_decl_rtl_for_debug (decl);
14727       if (!MEM_P (rtl)
14728           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14729           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14730         rtl = NULL_RTX;
14731     }
14732
14733   return rtl;
14734 }
14735
14736 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14737    returned.  If so, the decl for the COMMON block is returned, and the
14738    value is the offset into the common block for the symbol.  */
14739
14740 static tree
14741 fortran_common (tree decl, HOST_WIDE_INT *value)
14742 {
14743   tree val_expr, cvar;
14744   enum machine_mode mode;
14745   HOST_WIDE_INT bitsize, bitpos;
14746   tree offset;
14747   int volatilep = 0, unsignedp = 0;
14748
14749   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14750      it does not have a value (the offset into the common area), or if it
14751      is thread local (as opposed to global) then it isn't common, and shouldn't
14752      be handled as such.  */
14753   if (TREE_CODE (decl) != VAR_DECL
14754       || !TREE_STATIC (decl)
14755       || !DECL_HAS_VALUE_EXPR_P (decl)
14756       || !is_fortran ())
14757     return NULL_TREE;
14758
14759   val_expr = DECL_VALUE_EXPR (decl);
14760   if (TREE_CODE (val_expr) != COMPONENT_REF)
14761     return NULL_TREE;
14762
14763   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14764                               &mode, &unsignedp, &volatilep, true);
14765
14766   if (cvar == NULL_TREE
14767       || TREE_CODE (cvar) != VAR_DECL
14768       || DECL_ARTIFICIAL (cvar)
14769       || !TREE_PUBLIC (cvar))
14770     return NULL_TREE;
14771
14772   *value = 0;
14773   if (offset != NULL)
14774     {
14775       if (!host_integerp (offset, 0))
14776         return NULL_TREE;
14777       *value = tree_low_cst (offset, 0);
14778     }
14779   if (bitpos != 0)
14780     *value += bitpos / BITS_PER_UNIT;
14781
14782   return cvar;
14783 }
14784
14785 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14786    data attribute for a variable or a parameter.  We generate the
14787    DW_AT_const_value attribute only in those cases where the given variable
14788    or parameter does not have a true "location" either in memory or in a
14789    register.  This can happen (for example) when a constant is passed as an
14790    actual argument in a call to an inline function.  (It's possible that
14791    these things can crop up in other ways also.)  Note that one type of
14792    constant value which can be passed into an inlined function is a constant
14793    pointer.  This can happen for example if an actual argument in an inlined
14794    function call evaluates to a compile-time constant address.
14795
14796    CACHE_P is true if it is worth caching the location list for DECL,
14797    so that future calls can reuse it rather than regenerate it from scratch.
14798    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14799    since we will need to refer to them each time the function is inlined.  */
14800
14801 static bool
14802 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14803                                        enum dwarf_attribute attr)
14804 {
14805   rtx rtl;
14806   dw_loc_list_ref list;
14807   var_loc_list *loc_list;
14808   cached_dw_loc_list *cache;
14809   void **slot;
14810
14811   if (TREE_CODE (decl) == ERROR_MARK)
14812     return false;
14813
14814   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14815               || TREE_CODE (decl) == RESULT_DECL);
14816
14817   /* Try to get some constant RTL for this decl, and use that as the value of
14818      the location.  */
14819
14820   rtl = rtl_for_decl_location (decl);
14821   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14822       && add_const_value_attribute (die, rtl))
14823     return true;
14824
14825   /* See if we have single element location list that is equivalent to
14826      a constant value.  That way we are better to use add_const_value_attribute
14827      rather than expanding constant value equivalent.  */
14828   loc_list = lookup_decl_loc (decl);
14829   if (loc_list
14830       && loc_list->first
14831       && loc_list->first->next == NULL
14832       && NOTE_P (loc_list->first->loc)
14833       && NOTE_VAR_LOCATION (loc_list->first->loc)
14834       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14835     {
14836       struct var_loc_node *node;
14837
14838       node = loc_list->first;
14839       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14840       if (GET_CODE (rtl) == EXPR_LIST)
14841         rtl = XEXP (rtl, 0);
14842       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14843           && add_const_value_attribute (die, rtl))
14844          return true;
14845     }
14846   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14847      list several times.  See if we've already cached the contents.  */
14848   list = NULL;
14849   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14850     cache_p = false;
14851   if (cache_p)
14852     {
14853       cache = (cached_dw_loc_list *)
14854         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14855       if (cache)
14856         list = cache->loc_list;
14857     }
14858   if (list == NULL)
14859     {
14860       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14861       /* It is usually worth caching this result if the decl is from
14862          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14863       if (cache_p && list && list->dw_loc_next)
14864         {
14865           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14866                                            DECL_UID (decl), INSERT);
14867           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14868           cache->decl_id = DECL_UID (decl);
14869           cache->loc_list = list;
14870           *slot = cache;
14871         }
14872     }
14873   if (list)
14874     {
14875       add_AT_location_description (die, attr, list);
14876       return true;
14877     }
14878   /* None of that worked, so it must not really have a location;
14879      try adding a constant value attribute from the DECL_INITIAL.  */
14880   return tree_add_const_value_attribute_for_decl (die, decl);
14881 }
14882
14883 /* Add VARIABLE and DIE into deferred locations list.  */
14884
14885 static void
14886 defer_location (tree variable, dw_die_ref die)
14887 {
14888   deferred_locations entry;
14889   entry.variable = variable;
14890   entry.die = die;
14891   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14892 }
14893
14894 /* Helper function for tree_add_const_value_attribute.  Natively encode
14895    initializer INIT into an array.  Return true if successful.  */
14896
14897 static bool
14898 native_encode_initializer (tree init, unsigned char *array, int size)
14899 {
14900   tree type;
14901
14902   if (init == NULL_TREE)
14903     return false;
14904
14905   STRIP_NOPS (init);
14906   switch (TREE_CODE (init))
14907     {
14908     case STRING_CST:
14909       type = TREE_TYPE (init);
14910       if (TREE_CODE (type) == ARRAY_TYPE)
14911         {
14912           tree enttype = TREE_TYPE (type);
14913           enum machine_mode mode = TYPE_MODE (enttype);
14914
14915           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14916             return false;
14917           if (int_size_in_bytes (type) != size)
14918             return false;
14919           if (size > TREE_STRING_LENGTH (init))
14920             {
14921               memcpy (array, TREE_STRING_POINTER (init),
14922                       TREE_STRING_LENGTH (init));
14923               memset (array + TREE_STRING_LENGTH (init),
14924                       '\0', size - TREE_STRING_LENGTH (init));
14925             }
14926           else
14927             memcpy (array, TREE_STRING_POINTER (init), size);
14928           return true;
14929         }
14930       return false;
14931     case CONSTRUCTOR:
14932       type = TREE_TYPE (init);
14933       if (int_size_in_bytes (type) != size)
14934         return false;
14935       if (TREE_CODE (type) == ARRAY_TYPE)
14936         {
14937           HOST_WIDE_INT min_index;
14938           unsigned HOST_WIDE_INT cnt;
14939           int curpos = 0, fieldsize;
14940           constructor_elt *ce;
14941
14942           if (TYPE_DOMAIN (type) == NULL_TREE
14943               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14944             return false;
14945
14946           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14947           if (fieldsize <= 0)
14948             return false;
14949
14950           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14951           memset (array, '\0', size);
14952           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14953             {
14954               tree val = ce->value;
14955               tree index = ce->index;
14956               int pos = curpos;
14957               if (index && TREE_CODE (index) == RANGE_EXPR)
14958                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14959                       * fieldsize;
14960               else if (index)
14961                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14962
14963               if (val)
14964                 {
14965                   STRIP_NOPS (val);
14966                   if (!native_encode_initializer (val, array + pos, fieldsize))
14967                     return false;
14968                 }
14969               curpos = pos + fieldsize;
14970               if (index && TREE_CODE (index) == RANGE_EXPR)
14971                 {
14972                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14973                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14974                   while (count-- > 0)
14975                     {
14976                       if (val)
14977                         memcpy (array + curpos, array + pos, fieldsize);
14978                       curpos += fieldsize;
14979                     }
14980                 }
14981               gcc_assert (curpos <= size);
14982             }
14983           return true;
14984         }
14985       else if (TREE_CODE (type) == RECORD_TYPE
14986                || TREE_CODE (type) == UNION_TYPE)
14987         {
14988           tree field = NULL_TREE;
14989           unsigned HOST_WIDE_INT cnt;
14990           constructor_elt *ce;
14991
14992           if (int_size_in_bytes (type) != size)
14993             return false;
14994
14995           if (TREE_CODE (type) == RECORD_TYPE)
14996             field = TYPE_FIELDS (type);
14997
14998           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14999             {
15000               tree val = ce->value;
15001               int pos, fieldsize;
15002
15003               if (ce->index != 0)
15004                 field = ce->index;
15005
15006               if (val)
15007                 STRIP_NOPS (val);
15008
15009               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15010                 return false;
15011
15012               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15013                   && TYPE_DOMAIN (TREE_TYPE (field))
15014                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15015                 return false;
15016               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15017                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15018                 return false;
15019               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15020               pos = int_byte_position (field);
15021               gcc_assert (pos + fieldsize <= size);
15022               if (val
15023                   && !native_encode_initializer (val, array + pos, fieldsize))
15024                 return false;
15025             }
15026           return true;
15027         }
15028       return false;
15029     case VIEW_CONVERT_EXPR:
15030     case NON_LVALUE_EXPR:
15031       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15032     default:
15033       return native_encode_expr (init, array, size) == size;
15034     }
15035 }
15036
15037 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15038    attribute is the const value T.  */
15039
15040 static bool
15041 tree_add_const_value_attribute (dw_die_ref die, tree t)
15042 {
15043   tree init;
15044   tree type = TREE_TYPE (t);
15045   rtx rtl;
15046
15047   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15048     return false;
15049
15050   init = t;
15051   gcc_assert (!DECL_P (init));
15052
15053   rtl = rtl_for_decl_init (init, type);
15054   if (rtl)
15055     return add_const_value_attribute (die, rtl);
15056   /* If the host and target are sane, try harder.  */
15057   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15058            && initializer_constant_valid_p (init, type))
15059     {
15060       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15061       if (size > 0 && (int) size == size)
15062         {
15063           unsigned char *array = (unsigned char *)
15064             ggc_alloc_cleared_atomic (size);
15065
15066           if (native_encode_initializer (init, array, size))
15067             {
15068               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15069               return true;
15070             }
15071         }
15072     }
15073   return false;
15074 }
15075
15076 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15077    attribute is the const value of T, where T is an integral constant
15078    variable with static storage duration
15079    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15080
15081 static bool
15082 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15083 {
15084
15085   if (!decl
15086       || (TREE_CODE (decl) != VAR_DECL
15087           && TREE_CODE (decl) != CONST_DECL)
15088       || (TREE_CODE (decl) == VAR_DECL
15089           && !TREE_STATIC (decl)))
15090     return false;
15091
15092     if (TREE_READONLY (decl)
15093         && ! TREE_THIS_VOLATILE (decl)
15094         && DECL_INITIAL (decl))
15095       /* OK */;
15096     else
15097       return false;
15098
15099   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15100   if (get_AT (var_die, DW_AT_const_value))
15101     return false;
15102
15103   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15104 }
15105
15106 /* Convert the CFI instructions for the current function into a
15107    location list.  This is used for DW_AT_frame_base when we targeting
15108    a dwarf2 consumer that does not support the dwarf3
15109    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15110    expressions.  */
15111
15112 static dw_loc_list_ref
15113 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15114 {
15115   int ix;
15116   dw_fde_ref fde;
15117   dw_loc_list_ref list, *list_tail;
15118   dw_cfi_ref cfi;
15119   dw_cfa_location last_cfa, next_cfa;
15120   const char *start_label, *last_label, *section;
15121   dw_cfa_location remember;
15122
15123   fde = cfun->fde;
15124   gcc_assert (fde != NULL);
15125
15126   section = secname_for_decl (current_function_decl);
15127   list_tail = &list;
15128   list = NULL;
15129
15130   memset (&next_cfa, 0, sizeof (next_cfa));
15131   next_cfa.reg = INVALID_REGNUM;
15132   remember = next_cfa;
15133
15134   start_label = fde->dw_fde_begin;
15135
15136   /* ??? Bald assumption that the CIE opcode list does not contain
15137      advance opcodes.  */
15138   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15139     lookup_cfa_1 (cfi, &next_cfa, &remember);
15140
15141   last_cfa = next_cfa;
15142   last_label = start_label;
15143
15144   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15145     {
15146       /* If the first partition contained no CFI adjustments, the
15147          CIE opcodes apply to the whole first partition.  */
15148       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15149                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15150       list_tail =&(*list_tail)->dw_loc_next;
15151       start_label = last_label = fde->dw_fde_second_begin;
15152     }
15153
15154   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15155     {
15156       switch (cfi->dw_cfi_opc)
15157         {
15158         case DW_CFA_set_loc:
15159         case DW_CFA_advance_loc1:
15160         case DW_CFA_advance_loc2:
15161         case DW_CFA_advance_loc4:
15162           if (!cfa_equal_p (&last_cfa, &next_cfa))
15163             {
15164               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15165                                          start_label, last_label, section);
15166
15167               list_tail = &(*list_tail)->dw_loc_next;
15168               last_cfa = next_cfa;
15169               start_label = last_label;
15170             }
15171           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15172           break;
15173
15174         case DW_CFA_advance_loc:
15175           /* The encoding is complex enough that we should never emit this.  */
15176           gcc_unreachable ();
15177
15178         default:
15179           lookup_cfa_1 (cfi, &next_cfa, &remember);
15180           break;
15181         }
15182       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15183         {
15184           if (!cfa_equal_p (&last_cfa, &next_cfa))
15185             {
15186               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15187                                          start_label, last_label, section);
15188
15189               list_tail = &(*list_tail)->dw_loc_next;
15190               last_cfa = next_cfa;
15191               start_label = last_label;
15192             }
15193           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15194                                      start_label, fde->dw_fde_end, section);
15195           list_tail = &(*list_tail)->dw_loc_next;
15196           start_label = last_label = fde->dw_fde_second_begin;
15197         }
15198     }
15199
15200   if (!cfa_equal_p (&last_cfa, &next_cfa))
15201     {
15202       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15203                                  start_label, last_label, section);
15204       list_tail = &(*list_tail)->dw_loc_next;
15205       start_label = last_label;
15206     }
15207
15208   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15209                              start_label,
15210                              fde->dw_fde_second_begin
15211                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15212                              section);
15213
15214   if (list && list->dw_loc_next)
15215     gen_llsym (list);
15216
15217   return list;
15218 }
15219
15220 /* Compute a displacement from the "steady-state frame pointer" to the
15221    frame base (often the same as the CFA), and store it in
15222    frame_pointer_fb_offset.  OFFSET is added to the displacement
15223    before the latter is negated.  */
15224
15225 static void
15226 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15227 {
15228   rtx reg, elim;
15229
15230 #ifdef FRAME_POINTER_CFA_OFFSET
15231   reg = frame_pointer_rtx;
15232   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15233 #else
15234   reg = arg_pointer_rtx;
15235   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15236 #endif
15237
15238   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15239   if (GET_CODE (elim) == PLUS)
15240     {
15241       offset += INTVAL (XEXP (elim, 1));
15242       elim = XEXP (elim, 0);
15243     }
15244
15245   frame_pointer_fb_offset = -offset;
15246
15247   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15248      in which to eliminate.  This is because it's stack pointer isn't 
15249      directly accessible as a register within the ISA.  To work around
15250      this, assume that while we cannot provide a proper value for
15251      frame_pointer_fb_offset, we won't need one either.  */
15252   frame_pointer_fb_offset_valid
15253     = ((SUPPORTS_STACK_ALIGNMENT
15254         && (elim == hard_frame_pointer_rtx
15255             || elim == stack_pointer_rtx))
15256        || elim == (frame_pointer_needed
15257                    ? hard_frame_pointer_rtx
15258                    : stack_pointer_rtx));
15259 }
15260
15261 /* Generate a DW_AT_name attribute given some string value to be included as
15262    the value of the attribute.  */
15263
15264 static void
15265 add_name_attribute (dw_die_ref die, const char *name_string)
15266 {
15267   if (name_string != NULL && *name_string != 0)
15268     {
15269       if (demangle_name_func)
15270         name_string = (*demangle_name_func) (name_string);
15271
15272       add_AT_string (die, DW_AT_name, name_string);
15273     }
15274 }
15275
15276 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15277    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15278    of TYPE accordingly.
15279
15280    ??? This is a temporary measure until after we're able to generate
15281    regular DWARF for the complex Ada type system.  */
15282
15283 static void 
15284 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15285                                      dw_die_ref context_die)
15286 {
15287   tree dtype;
15288   dw_die_ref dtype_die;
15289
15290   if (!lang_hooks.types.descriptive_type)
15291     return;
15292
15293   dtype = lang_hooks.types.descriptive_type (type);
15294   if (!dtype)
15295     return;
15296
15297   dtype_die = lookup_type_die (dtype);
15298   if (!dtype_die)
15299     {
15300       gen_type_die (dtype, context_die);
15301       dtype_die = lookup_type_die (dtype);
15302       gcc_assert (dtype_die);
15303     }
15304
15305   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15306 }
15307
15308 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15309
15310 static void
15311 add_comp_dir_attribute (dw_die_ref die)
15312 {
15313   const char *wd = get_src_pwd ();
15314   char *wd1;
15315
15316   if (wd == NULL)
15317     return;
15318
15319   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15320     {
15321       int wdlen;
15322
15323       wdlen = strlen (wd);
15324       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15325       strcpy (wd1, wd);
15326       wd1 [wdlen] = DIR_SEPARATOR;
15327       wd1 [wdlen + 1] = 0;
15328       wd = wd1;
15329     }
15330
15331     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15332 }
15333
15334 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15335    default.  */
15336
15337 static int
15338 lower_bound_default (void)
15339 {
15340   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15341     {
15342     case DW_LANG_C:
15343     case DW_LANG_C89:
15344     case DW_LANG_C99:
15345     case DW_LANG_C_plus_plus:
15346     case DW_LANG_ObjC:
15347     case DW_LANG_ObjC_plus_plus:
15348     case DW_LANG_Java:
15349       return 0;
15350     case DW_LANG_Fortran77:
15351     case DW_LANG_Fortran90:
15352     case DW_LANG_Fortran95:
15353       return 1;
15354     case DW_LANG_UPC:
15355     case DW_LANG_D:
15356     case DW_LANG_Python:
15357       return dwarf_version >= 4 ? 0 : -1;
15358     case DW_LANG_Ada95:
15359     case DW_LANG_Ada83:
15360     case DW_LANG_Cobol74:
15361     case DW_LANG_Cobol85:
15362     case DW_LANG_Pascal83:
15363     case DW_LANG_Modula2:
15364     case DW_LANG_PLI:
15365       return dwarf_version >= 4 ? 1 : -1;
15366     default:
15367       return -1;
15368     }
15369 }
15370
15371 /* Given a tree node describing an array bound (either lower or upper) output
15372    a representation for that bound.  */
15373
15374 static void
15375 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15376 {
15377   switch (TREE_CODE (bound))
15378     {
15379     case ERROR_MARK:
15380       return;
15381
15382     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15383     case INTEGER_CST:
15384       {
15385         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15386         int dflt;
15387
15388         /* Use the default if possible.  */
15389         if (bound_attr == DW_AT_lower_bound
15390             && host_integerp (bound, 0)
15391             && (dflt = lower_bound_default ()) != -1
15392             && tree_low_cst (bound, 0) == dflt)
15393           ;
15394
15395         /* Otherwise represent the bound as an unsigned value with the
15396            precision of its type.  The precision and signedness of the
15397            type will be necessary to re-interpret it unambiguously.  */
15398         else if (prec < HOST_BITS_PER_WIDE_INT)
15399           {
15400             unsigned HOST_WIDE_INT mask
15401               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15402             add_AT_unsigned (subrange_die, bound_attr,
15403                              TREE_INT_CST_LOW (bound) & mask);
15404           }
15405         else if (prec == HOST_BITS_PER_WIDE_INT
15406                  || TREE_INT_CST_HIGH (bound) == 0)
15407           add_AT_unsigned (subrange_die, bound_attr,
15408                            TREE_INT_CST_LOW (bound));
15409         else
15410           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15411                          TREE_INT_CST_LOW (bound));
15412       }
15413       break;
15414
15415     CASE_CONVERT:
15416     case VIEW_CONVERT_EXPR:
15417       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15418       break;
15419
15420     case SAVE_EXPR:
15421       break;
15422
15423     case VAR_DECL:
15424     case PARM_DECL:
15425     case RESULT_DECL:
15426       {
15427         dw_die_ref decl_die = lookup_decl_die (bound);
15428
15429         /* ??? Can this happen, or should the variable have been bound
15430            first?  Probably it can, since I imagine that we try to create
15431            the types of parameters in the order in which they exist in
15432            the list, and won't have created a forward reference to a
15433            later parameter.  */
15434         if (decl_die != NULL)
15435           {
15436             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15437             break;
15438           }
15439       }
15440       /* FALLTHRU */
15441
15442     default:
15443       {
15444         /* Otherwise try to create a stack operation procedure to
15445            evaluate the value of the array bound.  */
15446
15447         dw_die_ref ctx, decl_die;
15448         dw_loc_list_ref list;
15449
15450         list = loc_list_from_tree (bound, 2);
15451         if (list == NULL || single_element_loc_list_p (list))
15452           {
15453             /* If DW_AT_*bound is not a reference nor constant, it is
15454                a DWARF expression rather than location description.
15455                For that loc_list_from_tree (bound, 0) is needed.
15456                If that fails to give a single element list,
15457                fall back to outputting this as a reference anyway.  */
15458             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15459             if (list2 && single_element_loc_list_p (list2))
15460               {
15461                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15462                 break;
15463               }
15464           }
15465         if (list == NULL)
15466           break;
15467
15468         if (current_function_decl == 0)
15469           ctx = comp_unit_die ();
15470         else
15471           ctx = lookup_decl_die (current_function_decl);
15472
15473         decl_die = new_die (DW_TAG_variable, ctx, bound);
15474         add_AT_flag (decl_die, DW_AT_artificial, 1);
15475         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15476         add_AT_location_description (decl_die, DW_AT_location, list);
15477         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15478         break;
15479       }
15480     }
15481 }
15482
15483 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15484    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15485    Note that the block of subscript information for an array type also
15486    includes information about the element type of the given array type.  */
15487
15488 static void
15489 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15490 {
15491   unsigned dimension_number;
15492   tree lower, upper;
15493   dw_die_ref subrange_die;
15494
15495   for (dimension_number = 0;
15496        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15497        type = TREE_TYPE (type), dimension_number++)
15498     {
15499       tree domain = TYPE_DOMAIN (type);
15500
15501       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15502         break;
15503
15504       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15505          and (in GNU C only) variable bounds.  Handle all three forms
15506          here.  */
15507       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15508       if (domain)
15509         {
15510           /* We have an array type with specified bounds.  */
15511           lower = TYPE_MIN_VALUE (domain);
15512           upper = TYPE_MAX_VALUE (domain);
15513
15514           /* Define the index type.  */
15515           if (TREE_TYPE (domain))
15516             {
15517               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15518                  TREE_TYPE field.  We can't emit debug info for this
15519                  because it is an unnamed integral type.  */
15520               if (TREE_CODE (domain) == INTEGER_TYPE
15521                   && TYPE_NAME (domain) == NULL_TREE
15522                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15523                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15524                 ;
15525               else
15526                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15527                                     type_die);
15528             }
15529
15530           /* ??? If upper is NULL, the array has unspecified length,
15531              but it does have a lower bound.  This happens with Fortran
15532                dimension arr(N:*)
15533              Since the debugger is definitely going to need to know N
15534              to produce useful results, go ahead and output the lower
15535              bound solo, and hope the debugger can cope.  */
15536
15537           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15538           if (upper)
15539             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15540         }
15541
15542       /* Otherwise we have an array type with an unspecified length.  The
15543          DWARF-2 spec does not say how to handle this; let's just leave out the
15544          bounds.  */
15545     }
15546 }
15547
15548 static void
15549 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15550 {
15551   unsigned size;
15552
15553   switch (TREE_CODE (tree_node))
15554     {
15555     case ERROR_MARK:
15556       size = 0;
15557       break;
15558     case ENUMERAL_TYPE:
15559     case RECORD_TYPE:
15560     case UNION_TYPE:
15561     case QUAL_UNION_TYPE:
15562       size = int_size_in_bytes (tree_node);
15563       break;
15564     case FIELD_DECL:
15565       /* For a data member of a struct or union, the DW_AT_byte_size is
15566          generally given as the number of bytes normally allocated for an
15567          object of the *declared* type of the member itself.  This is true
15568          even for bit-fields.  */
15569       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15570       break;
15571     default:
15572       gcc_unreachable ();
15573     }
15574
15575   /* Note that `size' might be -1 when we get to this point.  If it is, that
15576      indicates that the byte size of the entity in question is variable.  We
15577      have no good way of expressing this fact in Dwarf at the present time,
15578      so just let the -1 pass on through.  */
15579   add_AT_unsigned (die, DW_AT_byte_size, size);
15580 }
15581
15582 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15583    which specifies the distance in bits from the highest order bit of the
15584    "containing object" for the bit-field to the highest order bit of the
15585    bit-field itself.
15586
15587    For any given bit-field, the "containing object" is a hypothetical object
15588    (of some integral or enum type) within which the given bit-field lives.  The
15589    type of this hypothetical "containing object" is always the same as the
15590    declared type of the individual bit-field itself.  The determination of the
15591    exact location of the "containing object" for a bit-field is rather
15592    complicated.  It's handled by the `field_byte_offset' function (above).
15593
15594    Note that it is the size (in bytes) of the hypothetical "containing object"
15595    which will be given in the DW_AT_byte_size attribute for this bit-field.
15596    (See `byte_size_attribute' above).  */
15597
15598 static inline void
15599 add_bit_offset_attribute (dw_die_ref die, tree decl)
15600 {
15601   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15602   tree type = DECL_BIT_FIELD_TYPE (decl);
15603   HOST_WIDE_INT bitpos_int;
15604   HOST_WIDE_INT highest_order_object_bit_offset;
15605   HOST_WIDE_INT highest_order_field_bit_offset;
15606   HOST_WIDE_INT bit_offset;
15607
15608   /* Must be a field and a bit field.  */
15609   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15610
15611   /* We can't yet handle bit-fields whose offsets are variable, so if we
15612      encounter such things, just return without generating any attribute
15613      whatsoever.  Likewise for variable or too large size.  */
15614   if (! host_integerp (bit_position (decl), 0)
15615       || ! host_integerp (DECL_SIZE (decl), 1))
15616     return;
15617
15618   bitpos_int = int_bit_position (decl);
15619
15620   /* Note that the bit offset is always the distance (in bits) from the
15621      highest-order bit of the "containing object" to the highest-order bit of
15622      the bit-field itself.  Since the "high-order end" of any object or field
15623      is different on big-endian and little-endian machines, the computation
15624      below must take account of these differences.  */
15625   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15626   highest_order_field_bit_offset = bitpos_int;
15627
15628   if (! BYTES_BIG_ENDIAN)
15629     {
15630       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15631       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15632     }
15633
15634   bit_offset
15635     = (! BYTES_BIG_ENDIAN
15636        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15637        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15638
15639   if (bit_offset < 0)
15640     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15641   else
15642     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15643 }
15644
15645 /* For a FIELD_DECL node which represents a bit field, output an attribute
15646    which specifies the length in bits of the given field.  */
15647
15648 static inline void
15649 add_bit_size_attribute (dw_die_ref die, tree decl)
15650 {
15651   /* Must be a field and a bit field.  */
15652   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15653               && DECL_BIT_FIELD_TYPE (decl));
15654
15655   if (host_integerp (DECL_SIZE (decl), 1))
15656     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15657 }
15658
15659 /* If the compiled language is ANSI C, then add a 'prototyped'
15660    attribute, if arg types are given for the parameters of a function.  */
15661
15662 static inline void
15663 add_prototyped_attribute (dw_die_ref die, tree func_type)
15664 {
15665   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15666       && prototype_p (func_type))
15667     add_AT_flag (die, DW_AT_prototyped, 1);
15668 }
15669
15670 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15671    by looking in either the type declaration or object declaration
15672    equate table.  */
15673
15674 static inline dw_die_ref
15675 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15676 {
15677   dw_die_ref origin_die = NULL;
15678
15679   if (TREE_CODE (origin) != FUNCTION_DECL)
15680     {
15681       /* We may have gotten separated from the block for the inlined
15682          function, if we're in an exception handler or some such; make
15683          sure that the abstract function has been written out.
15684
15685          Doing this for nested functions is wrong, however; functions are
15686          distinct units, and our context might not even be inline.  */
15687       tree fn = origin;
15688
15689       if (TYPE_P (fn))
15690         fn = TYPE_STUB_DECL (fn);
15691
15692       fn = decl_function_context (fn);
15693       if (fn)
15694         dwarf2out_abstract_function (fn);
15695     }
15696
15697   if (DECL_P (origin))
15698     origin_die = lookup_decl_die (origin);
15699   else if (TYPE_P (origin))
15700     origin_die = lookup_type_die (origin);
15701
15702   /* XXX: Functions that are never lowered don't always have correct block
15703      trees (in the case of java, they simply have no block tree, in some other
15704      languages).  For these functions, there is nothing we can really do to
15705      output correct debug info for inlined functions in all cases.  Rather
15706      than die, we'll just produce deficient debug info now, in that we will
15707      have variables without a proper abstract origin.  In the future, when all
15708      functions are lowered, we should re-add a gcc_assert (origin_die)
15709      here.  */
15710
15711   if (origin_die)
15712     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15713   return origin_die;
15714 }
15715
15716 /* We do not currently support the pure_virtual attribute.  */
15717
15718 static inline void
15719 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15720 {
15721   if (DECL_VINDEX (func_decl))
15722     {
15723       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15724
15725       if (host_integerp (DECL_VINDEX (func_decl), 0))
15726         add_AT_loc (die, DW_AT_vtable_elem_location,
15727                     new_loc_descr (DW_OP_constu,
15728                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15729                                    0));
15730
15731       /* GNU extension: Record what type this method came from originally.  */
15732       if (debug_info_level > DINFO_LEVEL_TERSE
15733           && DECL_CONTEXT (func_decl))
15734         add_AT_die_ref (die, DW_AT_containing_type,
15735                         lookup_type_die (DECL_CONTEXT (func_decl)));
15736     }
15737 }
15738 \f
15739 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15740    given decl.  This used to be a vendor extension until after DWARF 4
15741    standardized it.  */
15742
15743 static void
15744 add_linkage_attr (dw_die_ref die, tree decl)
15745 {
15746   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15747
15748   /* Mimic what assemble_name_raw does with a leading '*'.  */
15749   if (name[0] == '*')
15750     name = &name[1];
15751
15752   if (dwarf_version >= 4)
15753     add_AT_string (die, DW_AT_linkage_name, name);
15754   else
15755     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15756 }
15757
15758 /* Add source coordinate attributes for the given decl.  */
15759
15760 static void
15761 add_src_coords_attributes (dw_die_ref die, tree decl)
15762 {
15763   expanded_location s;
15764
15765   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15766     return;
15767   s = expand_location (DECL_SOURCE_LOCATION (decl));
15768   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15769   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15770 }
15771
15772 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15773
15774 static void
15775 add_linkage_name (dw_die_ref die, tree decl)
15776 {
15777   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15778        && TREE_PUBLIC (decl)
15779        && !DECL_ABSTRACT (decl)
15780        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15781        && die->die_tag != DW_TAG_member)
15782     {
15783       /* Defer until we have an assembler name set.  */
15784       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15785         {
15786           limbo_die_node *asm_name;
15787
15788           asm_name = ggc_alloc_cleared_limbo_die_node ();
15789           asm_name->die = die;
15790           asm_name->created_for = decl;
15791           asm_name->next = deferred_asm_name;
15792           deferred_asm_name = asm_name;
15793         }
15794       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15795         add_linkage_attr (die, decl);
15796     }
15797 }
15798
15799 /* Add a DW_AT_name attribute and source coordinate attribute for the
15800    given decl, but only if it actually has a name.  */
15801
15802 static void
15803 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15804 {
15805   tree decl_name;
15806
15807   decl_name = DECL_NAME (decl);
15808   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15809     {
15810       const char *name = dwarf2_name (decl, 0);
15811       if (name)
15812         add_name_attribute (die, name);
15813       if (! DECL_ARTIFICIAL (decl))
15814         add_src_coords_attributes (die, decl);
15815
15816       add_linkage_name (die, decl);
15817     }
15818
15819 #ifdef VMS_DEBUGGING_INFO
15820   /* Get the function's name, as described by its RTL.  This may be different
15821      from the DECL_NAME name used in the source file.  */
15822   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15823     {
15824       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15825                    XEXP (DECL_RTL (decl), 0));
15826       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15827     }
15828 #endif /* VMS_DEBUGGING_INFO */
15829 }
15830
15831 #ifdef VMS_DEBUGGING_INFO
15832 /* Output the debug main pointer die for VMS */
15833
15834 void
15835 dwarf2out_vms_debug_main_pointer (void)
15836 {
15837   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15838   dw_die_ref die;
15839
15840   /* Allocate the VMS debug main subprogram die.  */
15841   die = ggc_alloc_cleared_die_node ();
15842   die->die_tag = DW_TAG_subprogram;
15843   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15844   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15845                                current_function_funcdef_no);
15846   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15847
15848   /* Make it the first child of comp_unit_die ().  */
15849   die->die_parent = comp_unit_die ();
15850   if (comp_unit_die ()->die_child)
15851     {
15852       die->die_sib = comp_unit_die ()->die_child->die_sib;
15853       comp_unit_die ()->die_child->die_sib = die;
15854     }
15855   else
15856     {
15857       die->die_sib = die;
15858       comp_unit_die ()->die_child = die;
15859     }
15860 }
15861 #endif /* VMS_DEBUGGING_INFO */
15862
15863 /* Push a new declaration scope.  */
15864
15865 static void
15866 push_decl_scope (tree scope)
15867 {
15868   VEC_safe_push (tree, gc, decl_scope_table, scope);
15869 }
15870
15871 /* Pop a declaration scope.  */
15872
15873 static inline void
15874 pop_decl_scope (void)
15875 {
15876   VEC_pop (tree, decl_scope_table);
15877 }
15878
15879 /* Return the DIE for the scope that immediately contains this type.
15880    Non-named types get global scope.  Named types nested in other
15881    types get their containing scope if it's open, or global scope
15882    otherwise.  All other types (i.e. function-local named types) get
15883    the current active scope.  */
15884
15885 static dw_die_ref
15886 scope_die_for (tree t, dw_die_ref context_die)
15887 {
15888   dw_die_ref scope_die = NULL;
15889   tree containing_scope;
15890   int i;
15891
15892   /* Non-types always go in the current scope.  */
15893   gcc_assert (TYPE_P (t));
15894
15895   containing_scope = TYPE_CONTEXT (t);
15896
15897   /* Use the containing namespace if it was passed in (for a declaration).  */
15898   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15899     {
15900       if (context_die == lookup_decl_die (containing_scope))
15901         /* OK */;
15902       else
15903         containing_scope = NULL_TREE;
15904     }
15905
15906   /* Ignore function type "scopes" from the C frontend.  They mean that
15907      a tagged type is local to a parmlist of a function declarator, but
15908      that isn't useful to DWARF.  */
15909   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15910     containing_scope = NULL_TREE;
15911
15912   if (SCOPE_FILE_SCOPE_P (containing_scope))
15913     scope_die = comp_unit_die ();
15914   else if (TYPE_P (containing_scope))
15915     {
15916       /* For types, we can just look up the appropriate DIE.  But
15917          first we check to see if we're in the middle of emitting it
15918          so we know where the new DIE should go.  */
15919       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
15920         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
15921           break;
15922
15923       if (i < 0)
15924         {
15925           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
15926                       || TREE_ASM_WRITTEN (containing_scope));
15927           /*We are not in the middle of emitting the type
15928             CONTAINING_SCOPE. Let's see if it's emitted already.  */
15929           scope_die = lookup_type_die (containing_scope);
15930
15931           /* If none of the current dies are suitable, we get file scope.  */
15932           if (scope_die == NULL)
15933             scope_die = comp_unit_die ();
15934         }
15935       else
15936         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15937     }
15938   else
15939     scope_die = context_die;
15940
15941   return scope_die;
15942 }
15943
15944 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15945
15946 static inline int
15947 local_scope_p (dw_die_ref context_die)
15948 {
15949   for (; context_die; context_die = context_die->die_parent)
15950     if (context_die->die_tag == DW_TAG_inlined_subroutine
15951         || context_die->die_tag == DW_TAG_subprogram)
15952       return 1;
15953
15954   return 0;
15955 }
15956
15957 /* Returns nonzero if CONTEXT_DIE is a class.  */
15958
15959 static inline int
15960 class_scope_p (dw_die_ref context_die)
15961 {
15962   return (context_die
15963           && (context_die->die_tag == DW_TAG_structure_type
15964               || context_die->die_tag == DW_TAG_class_type
15965               || context_die->die_tag == DW_TAG_interface_type
15966               || context_die->die_tag == DW_TAG_union_type));
15967 }
15968
15969 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15970    whether or not to treat a DIE in this context as a declaration.  */
15971
15972 static inline int
15973 class_or_namespace_scope_p (dw_die_ref context_die)
15974 {
15975   return (class_scope_p (context_die)
15976           || (context_die && context_die->die_tag == DW_TAG_namespace));
15977 }
15978
15979 /* Many forms of DIEs require a "type description" attribute.  This
15980    routine locates the proper "type descriptor" die for the type given
15981    by 'type', and adds a DW_AT_type attribute below the given die.  */
15982
15983 static void
15984 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15985                     int decl_volatile, dw_die_ref context_die)
15986 {
15987   enum tree_code code  = TREE_CODE (type);
15988   dw_die_ref type_die  = NULL;
15989
15990   /* ??? If this type is an unnamed subrange type of an integral, floating-point
15991      or fixed-point type, use the inner type.  This is because we have no
15992      support for unnamed types in base_type_die.  This can happen if this is
15993      an Ada subrange type.  Correct solution is emit a subrange type die.  */
15994   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15995       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15996     type = TREE_TYPE (type), code = TREE_CODE (type);
15997
15998   if (code == ERROR_MARK
15999       /* Handle a special case.  For functions whose return type is void, we
16000          generate *no* type attribute.  (Note that no object may have type
16001          `void', so this only applies to function return types).  */
16002       || code == VOID_TYPE)
16003     return;
16004
16005   type_die = modified_type_die (type,
16006                                 decl_const || TYPE_READONLY (type),
16007                                 decl_volatile || TYPE_VOLATILE (type),
16008                                 context_die);
16009
16010   if (type_die != NULL)
16011     add_AT_die_ref (object_die, DW_AT_type, type_die);
16012 }
16013
16014 /* Given an object die, add the calling convention attribute for the
16015    function call type.  */
16016 static void
16017 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16018 {
16019   enum dwarf_calling_convention value = DW_CC_normal;
16020
16021   value = ((enum dwarf_calling_convention)
16022            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16023
16024   if (is_fortran ()
16025       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16026     {
16027       /* DWARF 2 doesn't provide a way to identify a program's source-level
16028         entry point.  DW_AT_calling_convention attributes are only meant
16029         to describe functions' calling conventions.  However, lacking a
16030         better way to signal the Fortran main program, we used this for 
16031         a long time, following existing custom.  Now, DWARF 4 has 
16032         DW_AT_main_subprogram, which we add below, but some tools still
16033         rely on the old way, which we thus keep.  */
16034       value = DW_CC_program;
16035
16036       if (dwarf_version >= 4 || !dwarf_strict)
16037         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16038     }
16039
16040   /* Only add the attribute if the backend requests it, and
16041      is not DW_CC_normal.  */
16042   if (value && (value != DW_CC_normal))
16043     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16044 }
16045
16046 /* Given a tree pointer to a struct, class, union, or enum type node, return
16047    a pointer to the (string) tag name for the given type, or zero if the type
16048    was declared without a tag.  */
16049
16050 static const char *
16051 type_tag (const_tree type)
16052 {
16053   const char *name = 0;
16054
16055   if (TYPE_NAME (type) != 0)
16056     {
16057       tree t = 0;
16058
16059       /* Find the IDENTIFIER_NODE for the type name.  */
16060       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16061           && !TYPE_NAMELESS (type))
16062         t = TYPE_NAME (type);
16063
16064       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16065          a TYPE_DECL node, regardless of whether or not a `typedef' was
16066          involved.  */
16067       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16068                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16069         {
16070           /* We want to be extra verbose.  Don't call dwarf_name if
16071              DECL_NAME isn't set.  The default hook for decl_printable_name
16072              doesn't like that, and in this context it's correct to return
16073              0, instead of "<anonymous>" or the like.  */
16074           if (DECL_NAME (TYPE_NAME (type))
16075               && !DECL_NAMELESS (TYPE_NAME (type)))
16076             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16077         }
16078
16079       /* Now get the name as a string, or invent one.  */
16080       if (!name && t != 0)
16081         name = IDENTIFIER_POINTER (t);
16082     }
16083
16084   return (name == 0 || *name == '\0') ? 0 : name;
16085 }
16086
16087 /* Return the type associated with a data member, make a special check
16088    for bit field types.  */
16089
16090 static inline tree
16091 member_declared_type (const_tree member)
16092 {
16093   return (DECL_BIT_FIELD_TYPE (member)
16094           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16095 }
16096
16097 /* Get the decl's label, as described by its RTL. This may be different
16098    from the DECL_NAME name used in the source file.  */
16099
16100 #if 0
16101 static const char *
16102 decl_start_label (tree decl)
16103 {
16104   rtx x;
16105   const char *fnname;
16106
16107   x = DECL_RTL (decl);
16108   gcc_assert (MEM_P (x));
16109
16110   x = XEXP (x, 0);
16111   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16112
16113   fnname = XSTR (x, 0);
16114   return fnname;
16115 }
16116 #endif
16117 \f
16118 /* These routines generate the internal representation of the DIE's for
16119    the compilation unit.  Debugging information is collected by walking
16120    the declaration trees passed in from dwarf2out_decl().  */
16121
16122 static void
16123 gen_array_type_die (tree type, dw_die_ref context_die)
16124 {
16125   dw_die_ref scope_die = scope_die_for (type, context_die);
16126   dw_die_ref array_die;
16127
16128   /* GNU compilers represent multidimensional array types as sequences of one
16129      dimensional array types whose element types are themselves array types.
16130      We sometimes squish that down to a single array_type DIE with multiple
16131      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16132      say that we are allowed to do this kind of compression in C, because
16133      there is no difference between an array of arrays and a multidimensional
16134      array.  We don't do this for Ada to remain as close as possible to the
16135      actual representation, which is especially important against the language
16136      flexibilty wrt arrays of variable size.  */
16137
16138   bool collapse_nested_arrays = !is_ada ();
16139   tree element_type;
16140
16141   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16142      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16143   if (TYPE_STRING_FLAG (type)
16144       && TREE_CODE (type) == ARRAY_TYPE
16145       && is_fortran ()
16146       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16147     {
16148       HOST_WIDE_INT size;
16149
16150       array_die = new_die (DW_TAG_string_type, scope_die, type);
16151       add_name_attribute (array_die, type_tag (type));
16152       equate_type_number_to_die (type, array_die);
16153       size = int_size_in_bytes (type);
16154       if (size >= 0)
16155         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16156       else if (TYPE_DOMAIN (type) != NULL_TREE
16157                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16158                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16159         {
16160           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16161           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16162
16163           size = int_size_in_bytes (TREE_TYPE (szdecl));
16164           if (loc && size > 0)
16165             {
16166               add_AT_location_description (array_die, DW_AT_string_length, loc);
16167               if (size != DWARF2_ADDR_SIZE)
16168                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16169             }
16170         }
16171       return;
16172     }
16173
16174   /* ??? The SGI dwarf reader fails for array of array of enum types
16175      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16176      array type comes before the outer array type.  We thus call gen_type_die
16177      before we new_die and must prevent nested array types collapsing for this
16178      target.  */
16179
16180 #ifdef MIPS_DEBUGGING_INFO
16181   gen_type_die (TREE_TYPE (type), context_die);
16182   collapse_nested_arrays = false;
16183 #endif
16184
16185   array_die = new_die (DW_TAG_array_type, scope_die, type);
16186   add_name_attribute (array_die, type_tag (type));
16187   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16188   if (TYPE_ARTIFICIAL (type))
16189     add_AT_flag (array_die, DW_AT_artificial, 1);
16190   equate_type_number_to_die (type, array_die);
16191
16192   if (TREE_CODE (type) == VECTOR_TYPE)
16193     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16194
16195   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16196   if (is_fortran ()
16197       && TREE_CODE (type) == ARRAY_TYPE
16198       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16199       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16200     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16201
16202 #if 0
16203   /* We default the array ordering.  SDB will probably do
16204      the right things even if DW_AT_ordering is not present.  It's not even
16205      an issue until we start to get into multidimensional arrays anyway.  If
16206      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16207      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16208      and when we find out that we need to put these in, we will only do so
16209      for multidimensional arrays.  */
16210   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16211 #endif
16212
16213 #ifdef MIPS_DEBUGGING_INFO
16214   /* The SGI compilers handle arrays of unknown bound by setting
16215      AT_declaration and not emitting any subrange DIEs.  */
16216   if (TREE_CODE (type) == ARRAY_TYPE
16217       && ! TYPE_DOMAIN (type))
16218     add_AT_flag (array_die, DW_AT_declaration, 1);
16219   else
16220 #endif
16221   if (TREE_CODE (type) == VECTOR_TYPE)
16222     {
16223       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16224       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16225       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16226       add_bound_info (subrange_die, DW_AT_upper_bound,
16227                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16228     }
16229   else
16230     add_subscript_info (array_die, type, collapse_nested_arrays);
16231
16232   /* Add representation of the type of the elements of this array type and
16233      emit the corresponding DIE if we haven't done it already.  */
16234   element_type = TREE_TYPE (type);
16235   if (collapse_nested_arrays)
16236     while (TREE_CODE (element_type) == ARRAY_TYPE)
16237       {
16238         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16239           break;
16240         element_type = TREE_TYPE (element_type);
16241       }
16242
16243 #ifndef MIPS_DEBUGGING_INFO
16244   gen_type_die (element_type, context_die);
16245 #endif
16246
16247   add_type_attribute (array_die, element_type, 0, 0, context_die);
16248
16249   if (get_AT (array_die, DW_AT_name))
16250     add_pubtype (type, array_die);
16251 }
16252
16253 static dw_loc_descr_ref
16254 descr_info_loc (tree val, tree base_decl)
16255 {
16256   HOST_WIDE_INT size;
16257   dw_loc_descr_ref loc, loc2;
16258   enum dwarf_location_atom op;
16259
16260   if (val == base_decl)
16261     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16262
16263   switch (TREE_CODE (val))
16264     {
16265     CASE_CONVERT:
16266       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16267     case VAR_DECL:
16268       return loc_descriptor_from_tree (val, 0);
16269     case INTEGER_CST:
16270       if (host_integerp (val, 0))
16271         return int_loc_descriptor (tree_low_cst (val, 0));
16272       break;
16273     case INDIRECT_REF:
16274       size = int_size_in_bytes (TREE_TYPE (val));
16275       if (size < 0)
16276         break;
16277       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16278       if (!loc)
16279         break;
16280       if (size == DWARF2_ADDR_SIZE)
16281         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16282       else
16283         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16284       return loc;
16285     case POINTER_PLUS_EXPR:
16286     case PLUS_EXPR:
16287       if (host_integerp (TREE_OPERAND (val, 1), 1)
16288           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16289              < 16384)
16290         {
16291           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16292           if (!loc)
16293             break;
16294           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16295         }
16296       else
16297         {
16298           op = DW_OP_plus;
16299         do_binop:
16300           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16301           if (!loc)
16302             break;
16303           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16304           if (!loc2)
16305             break;
16306           add_loc_descr (&loc, loc2);
16307           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16308         }
16309       return loc;
16310     case MINUS_EXPR:
16311       op = DW_OP_minus;
16312       goto do_binop;
16313     case MULT_EXPR:
16314       op = DW_OP_mul;
16315       goto do_binop;
16316     case EQ_EXPR:
16317       op = DW_OP_eq;
16318       goto do_binop;
16319     case NE_EXPR:
16320       op = DW_OP_ne;
16321       goto do_binop;
16322     default:
16323       break;
16324     }
16325   return NULL;
16326 }
16327
16328 static void
16329 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16330                       tree val, tree base_decl)
16331 {
16332   dw_loc_descr_ref loc;
16333
16334   if (host_integerp (val, 0))
16335     {
16336       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16337       return;
16338     }
16339
16340   loc = descr_info_loc (val, base_decl);
16341   if (!loc)
16342     return;
16343
16344   add_AT_loc (die, attr, loc);
16345 }
16346
16347 /* This routine generates DIE for array with hidden descriptor, details
16348    are filled into *info by a langhook.  */
16349
16350 static void
16351 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16352                           dw_die_ref context_die)
16353 {
16354   dw_die_ref scope_die = scope_die_for (type, context_die);
16355   dw_die_ref array_die;
16356   int dim;
16357
16358   array_die = new_die (DW_TAG_array_type, scope_die, type);
16359   add_name_attribute (array_die, type_tag (type));
16360   equate_type_number_to_die (type, array_die);
16361
16362   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16363   if (is_fortran ()
16364       && info->ndimensions >= 2)
16365     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16366
16367   if (info->data_location)
16368     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16369                           info->base_decl);
16370   if (info->associated)
16371     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16372                           info->base_decl);
16373   if (info->allocated)
16374     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16375                           info->base_decl);
16376
16377   for (dim = 0; dim < info->ndimensions; dim++)
16378     {
16379       dw_die_ref subrange_die
16380         = new_die (DW_TAG_subrange_type, array_die, NULL);
16381
16382       if (info->dimen[dim].lower_bound)
16383         {
16384           /* If it is the default value, omit it.  */
16385           int dflt;
16386
16387           if (host_integerp (info->dimen[dim].lower_bound, 0)
16388               && (dflt = lower_bound_default ()) != -1
16389               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16390             ;
16391           else
16392             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16393                                   info->dimen[dim].lower_bound,
16394                                   info->base_decl);
16395         }
16396       if (info->dimen[dim].upper_bound)
16397         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16398                               info->dimen[dim].upper_bound,
16399                               info->base_decl);
16400       if (info->dimen[dim].stride)
16401         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16402                               info->dimen[dim].stride,
16403                               info->base_decl);
16404     }
16405
16406   gen_type_die (info->element_type, context_die);
16407   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16408
16409   if (get_AT (array_die, DW_AT_name))
16410     add_pubtype (type, array_die);
16411 }
16412
16413 #if 0
16414 static void
16415 gen_entry_point_die (tree decl, dw_die_ref context_die)
16416 {
16417   tree origin = decl_ultimate_origin (decl);
16418   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16419
16420   if (origin != NULL)
16421     add_abstract_origin_attribute (decl_die, origin);
16422   else
16423     {
16424       add_name_and_src_coords_attributes (decl_die, decl);
16425       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16426                           0, 0, context_die);
16427     }
16428
16429   if (DECL_ABSTRACT (decl))
16430     equate_decl_number_to_die (decl, decl_die);
16431   else
16432     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16433 }
16434 #endif
16435
16436 /* Walk through the list of incomplete types again, trying once more to
16437    emit full debugging info for them.  */
16438
16439 static void
16440 retry_incomplete_types (void)
16441 {
16442   int i;
16443
16444   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16445     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16446                                   DINFO_USAGE_DIR_USE))
16447       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16448 }
16449
16450 /* Determine what tag to use for a record type.  */
16451
16452 static enum dwarf_tag
16453 record_type_tag (tree type)
16454 {
16455   if (! lang_hooks.types.classify_record)
16456     return DW_TAG_structure_type;
16457
16458   switch (lang_hooks.types.classify_record (type))
16459     {
16460     case RECORD_IS_STRUCT:
16461       return DW_TAG_structure_type;
16462
16463     case RECORD_IS_CLASS:
16464       return DW_TAG_class_type;
16465
16466     case RECORD_IS_INTERFACE:
16467       if (dwarf_version >= 3 || !dwarf_strict)
16468         return DW_TAG_interface_type;
16469       return DW_TAG_structure_type;
16470
16471     default:
16472       gcc_unreachable ();
16473     }
16474 }
16475
16476 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16477    include all of the information about the enumeration values also. Each
16478    enumerated type name/value is listed as a child of the enumerated type
16479    DIE.  */
16480
16481 static dw_die_ref
16482 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16483 {
16484   dw_die_ref type_die = lookup_type_die (type);
16485
16486   if (type_die == NULL)
16487     {
16488       type_die = new_die (DW_TAG_enumeration_type,
16489                           scope_die_for (type, context_die), type);
16490       equate_type_number_to_die (type, type_die);
16491       add_name_attribute (type_die, type_tag (type));
16492       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16493       if (TYPE_ARTIFICIAL (type))
16494         add_AT_flag (type_die, DW_AT_artificial, 1);
16495       if (dwarf_version >= 4 || !dwarf_strict)
16496         {
16497           if (ENUM_IS_SCOPED (type))
16498             add_AT_flag (type_die, DW_AT_enum_class, 1);
16499           if (ENUM_IS_OPAQUE (type))
16500             add_AT_flag (type_die, DW_AT_declaration, 1);
16501         }
16502     }
16503   else if (! TYPE_SIZE (type))
16504     return type_die;
16505   else
16506     remove_AT (type_die, DW_AT_declaration);
16507
16508   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16509      given enum type is incomplete, do not generate the DW_AT_byte_size
16510      attribute or the DW_AT_element_list attribute.  */
16511   if (TYPE_SIZE (type))
16512     {
16513       tree link;
16514
16515       TREE_ASM_WRITTEN (type) = 1;
16516       add_byte_size_attribute (type_die, type);
16517       if (TYPE_STUB_DECL (type) != NULL_TREE)
16518         {
16519           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16520           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16521         }
16522
16523       /* If the first reference to this type was as the return type of an
16524          inline function, then it may not have a parent.  Fix this now.  */
16525       if (type_die->die_parent == NULL)
16526         add_child_die (scope_die_for (type, context_die), type_die);
16527
16528       for (link = TYPE_VALUES (type);
16529            link != NULL; link = TREE_CHAIN (link))
16530         {
16531           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16532           tree value = TREE_VALUE (link);
16533
16534           add_name_attribute (enum_die,
16535                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16536
16537           if (TREE_CODE (value) == CONST_DECL)
16538             value = DECL_INITIAL (value);
16539
16540           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16541             /* DWARF2 does not provide a way of indicating whether or
16542                not enumeration constants are signed or unsigned.  GDB
16543                always assumes the values are signed, so we output all
16544                values as if they were signed.  That means that
16545                enumeration constants with very large unsigned values
16546                will appear to have negative values in the debugger.  */
16547             add_AT_int (enum_die, DW_AT_const_value,
16548                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16549         }
16550     }
16551   else
16552     add_AT_flag (type_die, DW_AT_declaration, 1);
16553
16554   if (get_AT (type_die, DW_AT_name))
16555     add_pubtype (type, type_die);
16556
16557   return type_die;
16558 }
16559
16560 /* Generate a DIE to represent either a real live formal parameter decl or to
16561    represent just the type of some formal parameter position in some function
16562    type.
16563
16564    Note that this routine is a bit unusual because its argument may be a
16565    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16566    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16567    node.  If it's the former then this function is being called to output a
16568    DIE to represent a formal parameter object (or some inlining thereof).  If
16569    it's the latter, then this function is only being called to output a
16570    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16571    argument type of some subprogram type.
16572    If EMIT_NAME_P is true, name and source coordinate attributes
16573    are emitted.  */
16574
16575 static dw_die_ref
16576 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16577                           dw_die_ref context_die)
16578 {
16579   tree node_or_origin = node ? node : origin;
16580   tree ultimate_origin;
16581   dw_die_ref parm_die
16582     = new_die (DW_TAG_formal_parameter, context_die, node);
16583
16584   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16585     {
16586     case tcc_declaration:
16587       ultimate_origin = decl_ultimate_origin (node_or_origin);
16588       if (node || ultimate_origin)
16589         origin = ultimate_origin;
16590       if (origin != NULL)
16591         add_abstract_origin_attribute (parm_die, origin);
16592       else if (emit_name_p)
16593         add_name_and_src_coords_attributes (parm_die, node);
16594       if (origin == NULL
16595           || (! DECL_ABSTRACT (node_or_origin)
16596               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16597                                            decl_function_context
16598                                                             (node_or_origin))))
16599         {
16600           tree type = TREE_TYPE (node_or_origin);
16601           if (decl_by_reference_p (node_or_origin))
16602             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16603                                 context_die);
16604           else
16605             add_type_attribute (parm_die, type,
16606                                 TREE_READONLY (node_or_origin),
16607                                 TREE_THIS_VOLATILE (node_or_origin),
16608                                 context_die);
16609         }
16610       if (origin == NULL && DECL_ARTIFICIAL (node))
16611         add_AT_flag (parm_die, DW_AT_artificial, 1);
16612
16613       if (node && node != origin)
16614         equate_decl_number_to_die (node, parm_die);
16615       if (! DECL_ABSTRACT (node_or_origin))
16616         add_location_or_const_value_attribute (parm_die, node_or_origin,
16617                                                node == NULL, DW_AT_location);
16618
16619       break;
16620
16621     case tcc_type:
16622       /* We were called with some kind of a ..._TYPE node.  */
16623       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16624       break;
16625
16626     default:
16627       gcc_unreachable ();
16628     }
16629
16630   return parm_die;
16631 }
16632
16633 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16634    children DW_TAG_formal_parameter DIEs representing the arguments of the
16635    parameter pack.
16636
16637    PARM_PACK must be a function parameter pack.
16638    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16639    must point to the subsequent arguments of the function PACK_ARG belongs to.
16640    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16641    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16642    following the last one for which a DIE was generated.  */
16643
16644 static dw_die_ref
16645 gen_formal_parameter_pack_die  (tree parm_pack,
16646                                 tree pack_arg,
16647                                 dw_die_ref subr_die,
16648                                 tree *next_arg)
16649 {
16650   tree arg;
16651   dw_die_ref parm_pack_die;
16652
16653   gcc_assert (parm_pack
16654               && lang_hooks.function_parameter_pack_p (parm_pack)
16655               && subr_die);
16656
16657   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16658   add_src_coords_attributes (parm_pack_die, parm_pack);
16659
16660   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16661     {
16662       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16663                                                                  parm_pack))
16664         break;
16665       gen_formal_parameter_die (arg, NULL,
16666                                 false /* Don't emit name attribute.  */,
16667                                 parm_pack_die);
16668     }
16669   if (next_arg)
16670     *next_arg = arg;
16671   return parm_pack_die;
16672 }
16673
16674 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16675    at the end of an (ANSI prototyped) formal parameters list.  */
16676
16677 static void
16678 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16679 {
16680   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16681 }
16682
16683 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16684    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16685    parameters as specified in some function type specification (except for
16686    those which appear as part of a function *definition*).  */
16687
16688 static void
16689 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16690 {
16691   tree link;
16692   tree formal_type = NULL;
16693   tree first_parm_type;
16694   tree arg;
16695
16696   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16697     {
16698       arg = DECL_ARGUMENTS (function_or_method_type);
16699       function_or_method_type = TREE_TYPE (function_or_method_type);
16700     }
16701   else
16702     arg = NULL_TREE;
16703
16704   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16705
16706   /* Make our first pass over the list of formal parameter types and output a
16707      DW_TAG_formal_parameter DIE for each one.  */
16708   for (link = first_parm_type; link; )
16709     {
16710       dw_die_ref parm_die;
16711
16712       formal_type = TREE_VALUE (link);
16713       if (formal_type == void_type_node)
16714         break;
16715
16716       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16717       parm_die = gen_formal_parameter_die (formal_type, NULL,
16718                                            true /* Emit name attribute.  */,
16719                                            context_die);
16720       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16721           && link == first_parm_type)
16722         {
16723           add_AT_flag (parm_die, DW_AT_artificial, 1);
16724           if (dwarf_version >= 3 || !dwarf_strict)
16725             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16726         }
16727       else if (arg && DECL_ARTIFICIAL (arg))
16728         add_AT_flag (parm_die, DW_AT_artificial, 1);
16729
16730       link = TREE_CHAIN (link);
16731       if (arg)
16732         arg = DECL_CHAIN (arg);
16733     }
16734
16735   /* If this function type has an ellipsis, add a
16736      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16737   if (formal_type != void_type_node)
16738     gen_unspecified_parameters_die (function_or_method_type, context_die);
16739
16740   /* Make our second (and final) pass over the list of formal parameter types
16741      and output DIEs to represent those types (as necessary).  */
16742   for (link = TYPE_ARG_TYPES (function_or_method_type);
16743        link && TREE_VALUE (link);
16744        link = TREE_CHAIN (link))
16745     gen_type_die (TREE_VALUE (link), context_die);
16746 }
16747
16748 /* We want to generate the DIE for TYPE so that we can generate the
16749    die for MEMBER, which has been defined; we will need to refer back
16750    to the member declaration nested within TYPE.  If we're trying to
16751    generate minimal debug info for TYPE, processing TYPE won't do the
16752    trick; we need to attach the member declaration by hand.  */
16753
16754 static void
16755 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16756 {
16757   gen_type_die (type, context_die);
16758
16759   /* If we're trying to avoid duplicate debug info, we may not have
16760      emitted the member decl for this function.  Emit it now.  */
16761   if (TYPE_STUB_DECL (type)
16762       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16763       && ! lookup_decl_die (member))
16764     {
16765       dw_die_ref type_die;
16766       gcc_assert (!decl_ultimate_origin (member));
16767
16768       push_decl_scope (type);
16769       type_die = lookup_type_die_strip_naming_typedef (type);
16770       if (TREE_CODE (member) == FUNCTION_DECL)
16771         gen_subprogram_die (member, type_die);
16772       else if (TREE_CODE (member) == FIELD_DECL)
16773         {
16774           /* Ignore the nameless fields that are used to skip bits but handle
16775              C++ anonymous unions and structs.  */
16776           if (DECL_NAME (member) != NULL_TREE
16777               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16778               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16779             {
16780               gen_type_die (member_declared_type (member), type_die);
16781               gen_field_die (member, type_die);
16782             }
16783         }
16784       else
16785         gen_variable_die (member, NULL_TREE, type_die);
16786
16787       pop_decl_scope ();
16788     }
16789 }
16790
16791 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16792    may later generate inlined and/or out-of-line instances of.  */
16793
16794 static void
16795 dwarf2out_abstract_function (tree decl)
16796 {
16797   dw_die_ref old_die;
16798   tree save_fn;
16799   tree context;
16800   int was_abstract;
16801   htab_t old_decl_loc_table;
16802   htab_t old_cached_dw_loc_list_table;
16803   int old_call_site_count, old_tail_call_site_count;
16804   struct call_arg_loc_node *old_call_arg_locations;
16805
16806   /* Make sure we have the actual abstract inline, not a clone.  */
16807   decl = DECL_ORIGIN (decl);
16808
16809   old_die = lookup_decl_die (decl);
16810   if (old_die && get_AT (old_die, DW_AT_inline))
16811     /* We've already generated the abstract instance.  */
16812     return;
16813
16814   /* We can be called while recursively when seeing block defining inlined subroutine
16815      DIE.  Be sure to not clobber the outer location table nor use it or we would
16816      get locations in abstract instantces.  */
16817   old_decl_loc_table = decl_loc_table;
16818   decl_loc_table = NULL;
16819   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16820   cached_dw_loc_list_table = NULL;
16821   old_call_arg_locations = call_arg_locations;
16822   call_arg_locations = NULL;
16823   old_call_site_count = call_site_count;
16824   call_site_count = -1;
16825   old_tail_call_site_count = tail_call_site_count;
16826   tail_call_site_count = -1;
16827
16828   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16829      we don't get confused by DECL_ABSTRACT.  */
16830   if (debug_info_level > DINFO_LEVEL_TERSE)
16831     {
16832       context = decl_class_context (decl);
16833       if (context)
16834         gen_type_die_for_member
16835           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16836     }
16837
16838   /* Pretend we've just finished compiling this function.  */
16839   save_fn = current_function_decl;
16840   current_function_decl = decl;
16841   push_cfun (DECL_STRUCT_FUNCTION (decl));
16842
16843   was_abstract = DECL_ABSTRACT (decl);
16844   set_decl_abstract_flags (decl, 1);
16845   dwarf2out_decl (decl);
16846   if (! was_abstract)
16847     set_decl_abstract_flags (decl, 0);
16848
16849   current_function_decl = save_fn;
16850   decl_loc_table = old_decl_loc_table;
16851   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16852   call_arg_locations = old_call_arg_locations;
16853   call_site_count = old_call_site_count;
16854   tail_call_site_count = old_tail_call_site_count;
16855   pop_cfun ();
16856 }
16857
16858 /* Helper function of premark_used_types() which gets called through
16859    htab_traverse.
16860
16861    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16862    marked as unused by prune_unused_types.  */
16863
16864 static int
16865 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16866 {
16867   tree type;
16868   dw_die_ref die;
16869
16870   type = (tree) *slot;
16871   die = lookup_type_die (type);
16872   if (die != NULL)
16873     die->die_perennial_p = 1;
16874   return 1;
16875 }
16876
16877 /* Helper function of premark_types_used_by_global_vars which gets called
16878    through htab_traverse.
16879
16880    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16881    marked as unused by prune_unused_types. The DIE of the type is marked
16882    only if the global variable using the type will actually be emitted.  */
16883
16884 static int
16885 premark_types_used_by_global_vars_helper (void **slot,
16886                                           void *data ATTRIBUTE_UNUSED)
16887 {
16888   struct types_used_by_vars_entry *entry;
16889   dw_die_ref die;
16890
16891   entry = (struct types_used_by_vars_entry *) *slot;
16892   gcc_assert (entry->type != NULL
16893               && entry->var_decl != NULL);
16894   die = lookup_type_die (entry->type);
16895   if (die)
16896     {
16897       /* Ask cgraph if the global variable really is to be emitted.
16898          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16899       struct varpool_node *node = varpool_get_node (entry->var_decl);
16900       if (node && node->needed)
16901         {
16902           die->die_perennial_p = 1;
16903           /* Keep the parent DIEs as well.  */
16904           while ((die = die->die_parent) && die->die_perennial_p == 0)
16905             die->die_perennial_p = 1;
16906         }
16907     }
16908   return 1;
16909 }
16910
16911 /* Mark all members of used_types_hash as perennial.  */
16912
16913 static void
16914 premark_used_types (void)
16915 {
16916   if (cfun && cfun->used_types_hash)
16917     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16918 }
16919
16920 /* Mark all members of types_used_by_vars_entry as perennial.  */
16921
16922 static void
16923 premark_types_used_by_global_vars (void)
16924 {
16925   if (types_used_by_vars_hash)
16926     htab_traverse (types_used_by_vars_hash,
16927                    premark_types_used_by_global_vars_helper, NULL);
16928 }
16929
16930 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16931    for CA_LOC call arg loc node.  */
16932
16933 static dw_die_ref
16934 gen_call_site_die (tree decl, dw_die_ref subr_die,
16935                    struct call_arg_loc_node *ca_loc)
16936 {
16937   dw_die_ref stmt_die = NULL, die;
16938   tree block = ca_loc->block;
16939
16940   while (block
16941          && block != DECL_INITIAL (decl)
16942          && TREE_CODE (block) == BLOCK)
16943     {
16944       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16945         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16946       if (stmt_die)
16947         break;
16948       block = BLOCK_SUPERCONTEXT (block);
16949     }
16950   if (stmt_die == NULL)
16951     stmt_die = subr_die;
16952   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16953   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16954   if (ca_loc->tail_call_p)
16955     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16956   if (ca_loc->symbol_ref)
16957     {
16958       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16959       if (tdie)
16960         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16961       else
16962         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16963     }
16964   return die;
16965 }
16966
16967 /* Generate a DIE to represent a declared function (either file-scope or
16968    block-local).  */
16969
16970 static void
16971 gen_subprogram_die (tree decl, dw_die_ref context_die)
16972 {
16973   tree origin = decl_ultimate_origin (decl);
16974   dw_die_ref subr_die;
16975   tree outer_scope;
16976   dw_die_ref old_die = lookup_decl_die (decl);
16977   int declaration = (current_function_decl != decl
16978                      || class_or_namespace_scope_p (context_die));
16979
16980   premark_used_types ();
16981
16982   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16983      started to generate the abstract instance of an inline, decided to output
16984      its containing class, and proceeded to emit the declaration of the inline
16985      from the member list for the class.  If so, DECLARATION takes priority;
16986      we'll get back to the abstract instance when done with the class.  */
16987
16988   /* The class-scope declaration DIE must be the primary DIE.  */
16989   if (origin && declaration && class_or_namespace_scope_p (context_die))
16990     {
16991       origin = NULL;
16992       gcc_assert (!old_die);
16993     }
16994
16995   /* Now that the C++ front end lazily declares artificial member fns, we
16996      might need to retrofit the declaration into its class.  */
16997   if (!declaration && !origin && !old_die
16998       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16999       && !class_or_namespace_scope_p (context_die)
17000       && debug_info_level > DINFO_LEVEL_TERSE)
17001     old_die = force_decl_die (decl);
17002
17003   if (origin != NULL)
17004     {
17005       gcc_assert (!declaration || local_scope_p (context_die));
17006
17007       /* Fixup die_parent for the abstract instance of a nested
17008          inline function.  */
17009       if (old_die && old_die->die_parent == NULL)
17010         add_child_die (context_die, old_die);
17011
17012       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17013       add_abstract_origin_attribute (subr_die, origin);
17014       /*  This is where the actual code for a cloned function is.
17015           Let's emit linkage name attribute for it.  This helps
17016           debuggers to e.g, set breakpoints into
17017           constructors/destructors when the user asks "break
17018           K::K".  */
17019       add_linkage_name (subr_die, decl);
17020     }
17021   else if (old_die)
17022     {
17023       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17024       struct dwarf_file_data * file_index = lookup_filename (s.file);
17025
17026       if (!get_AT_flag (old_die, DW_AT_declaration)
17027           /* We can have a normal definition following an inline one in the
17028              case of redefinition of GNU C extern inlines.
17029              It seems reasonable to use AT_specification in this case.  */
17030           && !get_AT (old_die, DW_AT_inline))
17031         {
17032           /* Detect and ignore this case, where we are trying to output
17033              something we have already output.  */
17034           return;
17035         }
17036
17037       /* If the definition comes from the same place as the declaration,
17038          maybe use the old DIE.  We always want the DIE for this function
17039          that has the *_pc attributes to be under comp_unit_die so the
17040          debugger can find it.  We also need to do this for abstract
17041          instances of inlines, since the spec requires the out-of-line copy
17042          to have the same parent.  For local class methods, this doesn't
17043          apply; we just use the old DIE.  */
17044       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17045           && (DECL_ARTIFICIAL (decl)
17046               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17047                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17048                       == (unsigned) s.line))))
17049         {
17050           subr_die = old_die;
17051
17052           /* Clear out the declaration attribute and the formal parameters.
17053              Do not remove all children, because it is possible that this
17054              declaration die was forced using force_decl_die(). In such
17055              cases die that forced declaration die (e.g. TAG_imported_module)
17056              is one of the children that we do not want to remove.  */
17057           remove_AT (subr_die, DW_AT_declaration);
17058           remove_AT (subr_die, DW_AT_object_pointer);
17059           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17060         }
17061       else
17062         {
17063           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17064           add_AT_specification (subr_die, old_die);
17065           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17066             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17067           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17068             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17069         }
17070     }
17071   else
17072     {
17073       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17074
17075       if (TREE_PUBLIC (decl))
17076         add_AT_flag (subr_die, DW_AT_external, 1);
17077
17078       add_name_and_src_coords_attributes (subr_die, decl);
17079       if (debug_info_level > DINFO_LEVEL_TERSE)
17080         {
17081           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17082           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17083                               0, 0, context_die);
17084         }
17085
17086       add_pure_or_virtual_attribute (subr_die, decl);
17087       if (DECL_ARTIFICIAL (decl))
17088         add_AT_flag (subr_die, DW_AT_artificial, 1);
17089
17090       add_accessibility_attribute (subr_die, decl);
17091     }
17092
17093   if (declaration)
17094     {
17095       if (!old_die || !get_AT (old_die, DW_AT_inline))
17096         {
17097           add_AT_flag (subr_die, DW_AT_declaration, 1);
17098
17099           /* If this is an explicit function declaration then generate
17100              a DW_AT_explicit attribute.  */
17101           if (lang_hooks.decls.function_decl_explicit_p (decl)
17102               && (dwarf_version >= 3 || !dwarf_strict))
17103             add_AT_flag (subr_die, DW_AT_explicit, 1);
17104
17105           /* The first time we see a member function, it is in the context of
17106              the class to which it belongs.  We make sure of this by emitting
17107              the class first.  The next time is the definition, which is
17108              handled above.  The two may come from the same source text.
17109
17110              Note that force_decl_die() forces function declaration die. It is
17111              later reused to represent definition.  */
17112           equate_decl_number_to_die (decl, subr_die);
17113         }
17114     }
17115   else if (DECL_ABSTRACT (decl))
17116     {
17117       if (DECL_DECLARED_INLINE_P (decl))
17118         {
17119           if (cgraph_function_possibly_inlined_p (decl))
17120             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17121           else
17122             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17123         }
17124       else
17125         {
17126           if (cgraph_function_possibly_inlined_p (decl))
17127             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17128           else
17129             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17130         }
17131
17132       if (DECL_DECLARED_INLINE_P (decl)
17133           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17134         add_AT_flag (subr_die, DW_AT_artificial, 1);
17135
17136       equate_decl_number_to_die (decl, subr_die);
17137     }
17138   else if (!DECL_EXTERNAL (decl))
17139     {
17140       HOST_WIDE_INT cfa_fb_offset;
17141
17142       if (!old_die || !get_AT (old_die, DW_AT_inline))
17143         equate_decl_number_to_die (decl, subr_die);
17144
17145       if (!flag_reorder_blocks_and_partition)
17146         {
17147           dw_fde_ref fde = cfun->fde;
17148           if (fde->dw_fde_begin)
17149             {
17150               /* We have already generated the labels.  */
17151               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17152               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17153             }
17154           else
17155             {
17156               /* Create start/end labels and add the range.  */
17157               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17158               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17159                                            current_function_funcdef_no);
17160               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17161               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17162                                            current_function_funcdef_no);
17163               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17164             }
17165
17166 #if VMS_DEBUGGING_INFO
17167       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17168          Section 2.3 Prologue and Epilogue Attributes:
17169          When a breakpoint is set on entry to a function, it is generally
17170          desirable for execution to be suspended, not on the very first
17171          instruction of the function, but rather at a point after the
17172          function's frame has been set up, after any language defined local
17173          declaration processing has been completed, and before execution of
17174          the first statement of the function begins. Debuggers generally
17175          cannot properly determine where this point is.  Similarly for a
17176          breakpoint set on exit from a function. The prologue and epilogue
17177          attributes allow a compiler to communicate the location(s) to use.  */
17178
17179       {
17180         if (fde->dw_fde_vms_end_prologue)
17181           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17182             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17183
17184         if (fde->dw_fde_vms_begin_epilogue)
17185           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17186             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17187       }
17188 #endif
17189
17190           add_pubname (decl, subr_die);
17191         }
17192       else
17193         {
17194           /* Generate pubnames entries for the split function code ranges.  */
17195           dw_fde_ref fde = cfun->fde;
17196
17197           if (fde->dw_fde_second_begin)
17198             {
17199               if (dwarf_version >= 3 || !dwarf_strict)
17200                 {
17201                   /* We should use ranges for non-contiguous code section 
17202                      addresses.  Use the actual code range for the initial
17203                      section, since the HOT/COLD labels might precede an 
17204                      alignment offset.  */
17205                   bool range_list_added = false;
17206                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17207                                         fde->dw_fde_end, &range_list_added);
17208                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17209                                         fde->dw_fde_second_end,
17210                                         &range_list_added);
17211                   add_pubname (decl, subr_die);
17212                   if (range_list_added)
17213                     add_ranges (NULL);
17214                 }
17215               else
17216                 {
17217                   /* There is no real support in DW2 for this .. so we make
17218                      a work-around.  First, emit the pub name for the segment
17219                      containing the function label.  Then make and emit a
17220                      simplified subprogram DIE for the second segment with the
17221                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17222                      linkage name for the second die so that gdb will find both
17223                      sections when given "b foo".  */
17224                   const char *name = NULL;
17225                   tree decl_name = DECL_NAME (decl);
17226                   dw_die_ref seg_die;
17227
17228                   /* Do the 'primary' section.   */
17229                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17230                                  fde->dw_fde_begin);
17231                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17232                                  fde->dw_fde_end);
17233                   /* Add it.   */
17234                   add_pubname (decl, subr_die);
17235
17236                   /* Build a minimal DIE for the secondary section.  */
17237                   seg_die = new_die (DW_TAG_subprogram,
17238                                      subr_die->die_parent, decl);
17239
17240                   if (TREE_PUBLIC (decl))
17241                     add_AT_flag (seg_die, DW_AT_external, 1);
17242
17243                   if (decl_name != NULL 
17244                       && IDENTIFIER_POINTER (decl_name) != NULL)
17245                     {
17246                       name = dwarf2_name (decl, 1);
17247                       if (! DECL_ARTIFICIAL (decl))
17248                         add_src_coords_attributes (seg_die, decl);
17249
17250                       add_linkage_name (seg_die, decl);
17251                     }
17252                   gcc_assert (name != NULL);
17253                   add_pure_or_virtual_attribute (seg_die, decl);
17254                   if (DECL_ARTIFICIAL (decl))
17255                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17256
17257                   name = concat ("__second_sect_of_", name, NULL); 
17258                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17259                                  fde->dw_fde_second_begin);
17260                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17261                                  fde->dw_fde_second_end);
17262                   add_name_attribute (seg_die, name);
17263                   add_pubname_string (name, seg_die);
17264                 }
17265             }
17266           else
17267             {
17268               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17269               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17270               add_pubname (decl, subr_die);
17271             }
17272         }
17273
17274 #ifdef MIPS_DEBUGGING_INFO
17275       /* Add a reference to the FDE for this routine.  */
17276       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17277 #endif
17278
17279       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17280
17281       /* We define the "frame base" as the function's CFA.  This is more
17282          convenient for several reasons: (1) It's stable across the prologue
17283          and epilogue, which makes it better than just a frame pointer,
17284          (2) With dwarf3, there exists a one-byte encoding that allows us
17285          to reference the .debug_frame data by proxy, but failing that,
17286          (3) We can at least reuse the code inspection and interpretation
17287          code that determines the CFA position at various points in the
17288          function.  */
17289       if (dwarf_version >= 3)
17290         {
17291           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17292           add_AT_loc (subr_die, DW_AT_frame_base, op);
17293         }
17294       else
17295         {
17296           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17297           if (list->dw_loc_next)
17298             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17299           else
17300             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17301         }
17302
17303       /* Compute a displacement from the "steady-state frame pointer" to
17304          the CFA.  The former is what all stack slots and argument slots
17305          will reference in the rtl; the later is what we've told the
17306          debugger about.  We'll need to adjust all frame_base references
17307          by this displacement.  */
17308       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17309
17310       if (cfun->static_chain_decl)
17311         add_AT_location_description (subr_die, DW_AT_static_link,
17312                  loc_list_from_tree (cfun->static_chain_decl, 2));
17313     }
17314
17315   /* Generate child dies for template paramaters.  */
17316   if (debug_info_level > DINFO_LEVEL_TERSE)
17317     gen_generic_params_dies (decl);
17318
17319   /* Now output descriptions of the arguments for this function. This gets
17320      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17321      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17322      `...' at the end of the formal parameter list.  In order to find out if
17323      there was a trailing ellipsis or not, we must instead look at the type
17324      associated with the FUNCTION_DECL.  This will be a node of type
17325      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17326      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17327      an ellipsis at the end.  */
17328
17329   /* In the case where we are describing a mere function declaration, all we
17330      need to do here (and all we *can* do here) is to describe the *types* of
17331      its formal parameters.  */
17332   if (debug_info_level <= DINFO_LEVEL_TERSE)
17333     ;
17334   else if (declaration)
17335     gen_formal_types_die (decl, subr_die);
17336   else
17337     {
17338       /* Generate DIEs to represent all known formal parameters.  */
17339       tree parm = DECL_ARGUMENTS (decl);
17340       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17341       tree generic_decl_parm = generic_decl
17342                                 ? DECL_ARGUMENTS (generic_decl)
17343                                 : NULL;
17344
17345       /* Now we want to walk the list of parameters of the function and
17346          emit their relevant DIEs.
17347
17348          We consider the case of DECL being an instance of a generic function
17349          as well as it being a normal function.
17350
17351          If DECL is an instance of a generic function we walk the
17352          parameters of the generic function declaration _and_ the parameters of
17353          DECL itself. This is useful because we want to emit specific DIEs for
17354          function parameter packs and those are declared as part of the
17355          generic function declaration. In that particular case,
17356          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17357          That DIE has children DIEs representing the set of arguments
17358          of the pack. Note that the set of pack arguments can be empty.
17359          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17360          children DIE.
17361
17362          Otherwise, we just consider the parameters of DECL.  */
17363       while (generic_decl_parm || parm)
17364         {
17365           if (generic_decl_parm
17366               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17367             gen_formal_parameter_pack_die (generic_decl_parm,
17368                                            parm, subr_die,
17369                                            &parm);
17370           else if (parm)
17371             {
17372               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17373
17374               if (parm == DECL_ARGUMENTS (decl)
17375                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17376                   && parm_die
17377                   && (dwarf_version >= 3 || !dwarf_strict))
17378                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17379
17380               parm = DECL_CHAIN (parm);
17381             }
17382
17383           if (generic_decl_parm)
17384             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17385         }
17386
17387       /* Decide whether we need an unspecified_parameters DIE at the end.
17388          There are 2 more cases to do this for: 1) the ansi ... declaration -
17389          this is detectable when the end of the arg list is not a
17390          void_type_node 2) an unprototyped function declaration (not a
17391          definition).  This just means that we have no info about the
17392          parameters at all.  */
17393       if (prototype_p (TREE_TYPE (decl)))
17394         {
17395           /* This is the prototyped case, check for....  */
17396           if (stdarg_p (TREE_TYPE (decl)))
17397             gen_unspecified_parameters_die (decl, subr_die);
17398         }
17399       else if (DECL_INITIAL (decl) == NULL_TREE)
17400         gen_unspecified_parameters_die (decl, subr_die);
17401     }
17402
17403   /* Output Dwarf info for all of the stuff within the body of the function
17404      (if it has one - it may be just a declaration).  */
17405   outer_scope = DECL_INITIAL (decl);
17406
17407   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17408      a function.  This BLOCK actually represents the outermost binding contour
17409      for the function, i.e. the contour in which the function's formal
17410      parameters and labels get declared. Curiously, it appears that the front
17411      end doesn't actually put the PARM_DECL nodes for the current function onto
17412      the BLOCK_VARS list for this outer scope, but are strung off of the
17413      DECL_ARGUMENTS list for the function instead.
17414
17415      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17416      the LABEL_DECL nodes for the function however, and we output DWARF info
17417      for those in decls_for_scope.  Just within the `outer_scope' there will be
17418      a BLOCK node representing the function's outermost pair of curly braces,
17419      and any blocks used for the base and member initializers of a C++
17420      constructor function.  */
17421   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17422     {
17423       int call_site_note_count = 0;
17424       int tail_call_site_note_count = 0;
17425
17426       /* Emit a DW_TAG_variable DIE for a named return value.  */
17427       if (DECL_NAME (DECL_RESULT (decl)))
17428         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17429
17430       current_function_has_inlines = 0;
17431       decls_for_scope (outer_scope, subr_die, 0);
17432
17433       if (call_arg_locations && !dwarf_strict)
17434         {
17435           struct call_arg_loc_node *ca_loc;
17436           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17437             {
17438               dw_die_ref die = NULL;
17439               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17440               rtx arg, next_arg;
17441
17442               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17443                    arg; arg = next_arg)
17444                 {
17445                   dw_loc_descr_ref reg, val;
17446                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17447                   dw_die_ref cdie, tdie = NULL;
17448
17449                   next_arg = XEXP (arg, 1);
17450                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17451                       && next_arg
17452                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17453                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17454                       && REGNO (XEXP (XEXP (arg, 0), 0))
17455                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17456                     next_arg = XEXP (next_arg, 1);
17457                   if (mode == VOIDmode)
17458                     {
17459                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17460                       if (mode == VOIDmode)
17461                         mode = GET_MODE (XEXP (arg, 0));
17462                     }
17463                   if (mode == VOIDmode || mode == BLKmode)
17464                     continue;
17465                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17466                     {
17467                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17468                       tloc = XEXP (XEXP (arg, 0), 1);
17469                       continue;
17470                     }
17471                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17472                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17473                     {
17474                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17475                       tlocc = XEXP (XEXP (arg, 0), 1);
17476                       continue;
17477                     }
17478                   reg = NULL;
17479                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17480                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17481                                               VAR_INIT_STATUS_INITIALIZED);
17482                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17483                     {
17484                       rtx mem = XEXP (XEXP (arg, 0), 0);
17485                       reg = mem_loc_descriptor (XEXP (mem, 0),
17486                                                 get_address_mode (mem),
17487                                                 GET_MODE (mem),
17488                                                 VAR_INIT_STATUS_INITIALIZED);
17489                     }
17490                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17491                            == DEBUG_PARAMETER_REF)
17492                     {
17493                       tree tdecl
17494                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17495                       tdie = lookup_decl_die (tdecl);
17496                       if (tdie == NULL)
17497                         continue;
17498                     }
17499                   else
17500                     continue;
17501                   if (reg == NULL
17502                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17503                          != DEBUG_PARAMETER_REF)
17504                     continue;
17505                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17506                                             VOIDmode,
17507                                             VAR_INIT_STATUS_INITIALIZED);
17508                   if (val == NULL)
17509                     continue;
17510                   if (die == NULL)
17511                     die = gen_call_site_die (decl, subr_die, ca_loc);
17512                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17513                                   NULL_TREE);
17514                   if (reg != NULL)
17515                     add_AT_loc (cdie, DW_AT_location, reg);
17516                   else if (tdie != NULL)
17517                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17518                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17519                   if (next_arg != XEXP (arg, 1))
17520                     {
17521                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17522                       if (mode == VOIDmode)
17523                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17524                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17525                                                             0), 1),
17526                                                 mode, VOIDmode,
17527                                                 VAR_INIT_STATUS_INITIALIZED);
17528                       if (val != NULL)
17529                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17530                     }
17531                 }
17532               if (die == NULL
17533                   && (ca_loc->symbol_ref || tloc))
17534                 die = gen_call_site_die (decl, subr_die, ca_loc);
17535               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17536                 {
17537                   dw_loc_descr_ref tval = NULL;
17538
17539                   if (tloc != NULL_RTX)
17540                     tval = mem_loc_descriptor (tloc,
17541                                                GET_MODE (tloc) == VOIDmode
17542                                                ? Pmode : GET_MODE (tloc),
17543                                                VOIDmode,
17544                                                VAR_INIT_STATUS_INITIALIZED);
17545                   if (tval)
17546                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17547                   else if (tlocc != NULL_RTX)
17548                     {
17549                       tval = mem_loc_descriptor (tlocc,
17550                                                  GET_MODE (tlocc) == VOIDmode
17551                                                  ? Pmode : GET_MODE (tlocc),
17552                                                  VOIDmode,
17553                                                  VAR_INIT_STATUS_INITIALIZED);
17554                       if (tval)
17555                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17556                                     tval);
17557                     }
17558                 }
17559               if (die != NULL)
17560                 {
17561                   call_site_note_count++;
17562                   if (ca_loc->tail_call_p)
17563                     tail_call_site_note_count++;
17564                 }
17565             }
17566         }
17567       call_arg_locations = NULL;
17568       call_arg_loc_last = NULL;
17569       if (tail_call_site_count >= 0
17570           && tail_call_site_count == tail_call_site_note_count
17571           && !dwarf_strict)
17572         {
17573           if (call_site_count >= 0
17574               && call_site_count == call_site_note_count)
17575             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17576           else
17577             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17578         }
17579       call_site_count = -1;
17580       tail_call_site_count = -1;
17581     }
17582   /* Add the calling convention attribute if requested.  */
17583   add_calling_convention_attribute (subr_die, decl);
17584
17585 }
17586
17587 /* Returns a hash value for X (which really is a die_struct).  */
17588
17589 static hashval_t
17590 common_block_die_table_hash (const void *x)
17591 {
17592   const_dw_die_ref d = (const_dw_die_ref) x;
17593   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17594 }
17595
17596 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17597    as decl_id and die_parent of die_struct Y.  */
17598
17599 static int
17600 common_block_die_table_eq (const void *x, const void *y)
17601 {
17602   const_dw_die_ref d = (const_dw_die_ref) x;
17603   const_dw_die_ref e = (const_dw_die_ref) y;
17604   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17605 }
17606
17607 /* Generate a DIE to represent a declared data object.
17608    Either DECL or ORIGIN must be non-null.  */
17609
17610 static void
17611 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17612 {
17613   HOST_WIDE_INT off;
17614   tree com_decl;
17615   tree decl_or_origin = decl ? decl : origin;
17616   tree ultimate_origin;
17617   dw_die_ref var_die;
17618   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17619   dw_die_ref origin_die;
17620   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17621                       || class_or_namespace_scope_p (context_die));
17622   bool specialization_p = false;
17623
17624   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17625   if (decl || ultimate_origin)
17626     origin = ultimate_origin;
17627   com_decl = fortran_common (decl_or_origin, &off);
17628
17629   /* Symbol in common gets emitted as a child of the common block, in the form
17630      of a data member.  */
17631   if (com_decl)
17632     {
17633       dw_die_ref com_die;
17634       dw_loc_list_ref loc;
17635       die_node com_die_arg;
17636
17637       var_die = lookup_decl_die (decl_or_origin);
17638       if (var_die)
17639         {
17640           if (get_AT (var_die, DW_AT_location) == NULL)
17641             {
17642               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17643               if (loc)
17644                 {
17645                   if (off)
17646                     {
17647                       /* Optimize the common case.  */
17648                       if (single_element_loc_list_p (loc)
17649                           && loc->expr->dw_loc_opc == DW_OP_addr
17650                           && loc->expr->dw_loc_next == NULL
17651                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17652                              == SYMBOL_REF)
17653                         loc->expr->dw_loc_oprnd1.v.val_addr
17654                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17655                         else
17656                           loc_list_plus_const (loc, off);
17657                     }
17658                   add_AT_location_description (var_die, DW_AT_location, loc);
17659                   remove_AT (var_die, DW_AT_declaration);
17660                 }
17661             }
17662           return;
17663         }
17664
17665       if (common_block_die_table == NULL)
17666         common_block_die_table
17667           = htab_create_ggc (10, common_block_die_table_hash,
17668                              common_block_die_table_eq, NULL);
17669
17670       com_die_arg.decl_id = DECL_UID (com_decl);
17671       com_die_arg.die_parent = context_die;
17672       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17673       loc = loc_list_from_tree (com_decl, 2);
17674       if (com_die == NULL)
17675         {
17676           const char *cnam
17677             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17678           void **slot;
17679
17680           com_die = new_die (DW_TAG_common_block, context_die, decl);
17681           add_name_and_src_coords_attributes (com_die, com_decl);
17682           if (loc)
17683             {
17684               add_AT_location_description (com_die, DW_AT_location, loc);
17685               /* Avoid sharing the same loc descriptor between
17686                  DW_TAG_common_block and DW_TAG_variable.  */
17687               loc = loc_list_from_tree (com_decl, 2);
17688             }
17689           else if (DECL_EXTERNAL (decl))
17690             add_AT_flag (com_die, DW_AT_declaration, 1);
17691           add_pubname_string (cnam, com_die); /* ??? needed? */
17692           com_die->decl_id = DECL_UID (com_decl);
17693           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17694           *slot = (void *) com_die;
17695         }
17696       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17697         {
17698           add_AT_location_description (com_die, DW_AT_location, loc);
17699           loc = loc_list_from_tree (com_decl, 2);
17700           remove_AT (com_die, DW_AT_declaration);
17701         }
17702       var_die = new_die (DW_TAG_variable, com_die, decl);
17703       add_name_and_src_coords_attributes (var_die, decl);
17704       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17705                           TREE_THIS_VOLATILE (decl), context_die);
17706       add_AT_flag (var_die, DW_AT_external, 1);
17707       if (loc)
17708         {
17709           if (off)
17710             {
17711               /* Optimize the common case.  */
17712               if (single_element_loc_list_p (loc)
17713                   && loc->expr->dw_loc_opc == DW_OP_addr
17714                   && loc->expr->dw_loc_next == NULL
17715                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17716                 loc->expr->dw_loc_oprnd1.v.val_addr
17717                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17718               else
17719                 loc_list_plus_const (loc, off);
17720             }
17721           add_AT_location_description (var_die, DW_AT_location, loc);
17722         }
17723       else if (DECL_EXTERNAL (decl))
17724         add_AT_flag (var_die, DW_AT_declaration, 1);
17725       equate_decl_number_to_die (decl, var_die);
17726       return;
17727     }
17728
17729   /* If the compiler emitted a definition for the DECL declaration
17730      and if we already emitted a DIE for it, don't emit a second
17731      DIE for it again. Allow re-declarations of DECLs that are
17732      inside functions, though.  */
17733   if (old_die && declaration && !local_scope_p (context_die))
17734     return;
17735
17736   /* For static data members, the declaration in the class is supposed
17737      to have DW_TAG_member tag; the specification should still be
17738      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17739   if (declaration && class_scope_p (context_die))
17740     var_die = new_die (DW_TAG_member, context_die, decl);
17741   else
17742     var_die = new_die (DW_TAG_variable, context_die, decl);
17743
17744   origin_die = NULL;
17745   if (origin != NULL)
17746     origin_die = add_abstract_origin_attribute (var_die, origin);
17747
17748   /* Loop unrolling can create multiple blocks that refer to the same
17749      static variable, so we must test for the DW_AT_declaration flag.
17750
17751      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17752      copy decls and set the DECL_ABSTRACT flag on them instead of
17753      sharing them.
17754
17755      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17756
17757      ??? The declare_in_namespace support causes us to get two DIEs for one
17758      variable, both of which are declarations.  We want to avoid considering
17759      one to be a specification, so we must test that this DIE is not a
17760      declaration.  */
17761   else if (old_die && TREE_STATIC (decl) && ! declaration
17762            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17763     {
17764       /* This is a definition of a C++ class level static.  */
17765       add_AT_specification (var_die, old_die);
17766       specialization_p = true;
17767       if (DECL_NAME (decl))
17768         {
17769           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17770           struct dwarf_file_data * file_index = lookup_filename (s.file);
17771
17772           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17773             add_AT_file (var_die, DW_AT_decl_file, file_index);
17774
17775           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17776             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17777
17778           if (old_die->die_tag == DW_TAG_member)
17779             add_linkage_name (var_die, decl);
17780         }
17781     }
17782   else
17783     add_name_and_src_coords_attributes (var_die, decl);
17784
17785   if ((origin == NULL && !specialization_p)
17786       || (origin != NULL
17787           && !DECL_ABSTRACT (decl_or_origin)
17788           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17789                                        decl_function_context
17790                                                         (decl_or_origin))))
17791     {
17792       tree type = TREE_TYPE (decl_or_origin);
17793
17794       if (decl_by_reference_p (decl_or_origin))
17795         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17796       else
17797         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17798                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17799     }
17800
17801   if (origin == NULL && !specialization_p)
17802     {
17803       if (TREE_PUBLIC (decl))
17804         add_AT_flag (var_die, DW_AT_external, 1);
17805
17806       if (DECL_ARTIFICIAL (decl))
17807         add_AT_flag (var_die, DW_AT_artificial, 1);
17808
17809       add_accessibility_attribute (var_die, decl);
17810     }
17811
17812   if (declaration)
17813     add_AT_flag (var_die, DW_AT_declaration, 1);
17814
17815   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17816     equate_decl_number_to_die (decl, var_die);
17817
17818   if (! declaration
17819       && (! DECL_ABSTRACT (decl_or_origin)
17820           /* Local static vars are shared between all clones/inlines,
17821              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17822              already set.  */
17823           || (TREE_CODE (decl_or_origin) == VAR_DECL
17824               && TREE_STATIC (decl_or_origin)
17825               && DECL_RTL_SET_P (decl_or_origin)))
17826       /* When abstract origin already has DW_AT_location attribute, no need
17827          to add it again.  */
17828       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17829     {
17830       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17831           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17832         defer_location (decl_or_origin, var_die);
17833       else
17834         add_location_or_const_value_attribute (var_die, decl_or_origin,
17835                                                decl == NULL, DW_AT_location);
17836       add_pubname (decl_or_origin, var_die);
17837     }
17838   else
17839     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17840 }
17841
17842 /* Generate a DIE to represent a named constant.  */
17843
17844 static void
17845 gen_const_die (tree decl, dw_die_ref context_die)
17846 {
17847   dw_die_ref const_die;
17848   tree type = TREE_TYPE (decl);
17849
17850   const_die = new_die (DW_TAG_constant, context_die, decl);
17851   add_name_and_src_coords_attributes (const_die, decl);
17852   add_type_attribute (const_die, type, 1, 0, context_die);
17853   if (TREE_PUBLIC (decl))
17854     add_AT_flag (const_die, DW_AT_external, 1);
17855   if (DECL_ARTIFICIAL (decl))
17856     add_AT_flag (const_die, DW_AT_artificial, 1);
17857   tree_add_const_value_attribute_for_decl (const_die, decl);
17858 }
17859
17860 /* Generate a DIE to represent a label identifier.  */
17861
17862 static void
17863 gen_label_die (tree decl, dw_die_ref context_die)
17864 {
17865   tree origin = decl_ultimate_origin (decl);
17866   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17867   rtx insn;
17868   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17869
17870   if (origin != NULL)
17871     add_abstract_origin_attribute (lbl_die, origin);
17872   else
17873     add_name_and_src_coords_attributes (lbl_die, decl);
17874
17875   if (DECL_ABSTRACT (decl))
17876     equate_decl_number_to_die (decl, lbl_die);
17877   else
17878     {
17879       insn = DECL_RTL_IF_SET (decl);
17880
17881       /* Deleted labels are programmer specified labels which have been
17882          eliminated because of various optimizations.  We still emit them
17883          here so that it is possible to put breakpoints on them.  */
17884       if (insn
17885           && (LABEL_P (insn)
17886               || ((NOTE_P (insn)
17887                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17888         {
17889           /* When optimization is enabled (via -O) some parts of the compiler
17890              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17891              represent source-level labels which were explicitly declared by
17892              the user.  This really shouldn't be happening though, so catch
17893              it if it ever does happen.  */
17894           gcc_assert (!INSN_DELETED_P (insn));
17895
17896           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17897           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17898         }
17899     }
17900 }
17901
17902 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17903    attributes to the DIE for a block STMT, to describe where the inlined
17904    function was called from.  This is similar to add_src_coords_attributes.  */
17905
17906 static inline void
17907 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17908 {
17909   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17910
17911   if (dwarf_version >= 3 || !dwarf_strict)
17912     {
17913       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17914       add_AT_unsigned (die, DW_AT_call_line, s.line);
17915     }
17916 }
17917
17918
17919 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17920    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17921
17922 static inline void
17923 add_high_low_attributes (tree stmt, dw_die_ref die)
17924 {
17925   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17926
17927   if (BLOCK_FRAGMENT_CHAIN (stmt)
17928       && (dwarf_version >= 3 || !dwarf_strict))
17929     {
17930       tree chain;
17931
17932       if (inlined_function_outer_scope_p (stmt))
17933         {
17934           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17935                                        BLOCK_NUMBER (stmt));
17936           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17937         }
17938
17939       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17940
17941       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17942       do
17943         {
17944           add_ranges (chain);
17945           chain = BLOCK_FRAGMENT_CHAIN (chain);
17946         }
17947       while (chain);
17948       add_ranges (NULL);
17949     }
17950   else
17951     {
17952       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17953                                    BLOCK_NUMBER (stmt));
17954       add_AT_lbl_id (die, DW_AT_low_pc, label);
17955       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
17956                                    BLOCK_NUMBER (stmt));
17957       add_AT_lbl_id (die, DW_AT_high_pc, label);
17958     }
17959 }
17960
17961 /* Generate a DIE for a lexical block.  */
17962
17963 static void
17964 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17965 {
17966   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17967
17968   if (call_arg_locations)
17969     {
17970       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17971         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17972                                BLOCK_NUMBER (stmt) + 1);
17973       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17974     }
17975
17976   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17977     add_high_low_attributes (stmt, stmt_die);
17978
17979   decls_for_scope (stmt, stmt_die, depth);
17980 }
17981
17982 /* Generate a DIE for an inlined subprogram.  */
17983
17984 static void
17985 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17986 {
17987   tree decl;
17988
17989   /* The instance of function that is effectively being inlined shall not
17990      be abstract.  */
17991   gcc_assert (! BLOCK_ABSTRACT (stmt));
17992
17993   decl = block_ultimate_origin (stmt);
17994
17995   /* Emit info for the abstract instance first, if we haven't yet.  We
17996      must emit this even if the block is abstract, otherwise when we
17997      emit the block below (or elsewhere), we may end up trying to emit
17998      a die whose origin die hasn't been emitted, and crashing.  */
17999   dwarf2out_abstract_function (decl);
18000
18001   if (! BLOCK_ABSTRACT (stmt))
18002     {
18003       dw_die_ref subr_die
18004         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18005
18006       if (call_arg_locations)
18007         {
18008           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18009             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18010                                    BLOCK_NUMBER (stmt) + 1);
18011           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18012         }
18013       add_abstract_origin_attribute (subr_die, decl);
18014       if (TREE_ASM_WRITTEN (stmt))
18015         add_high_low_attributes (stmt, subr_die);
18016       add_call_src_coords_attributes (stmt, subr_die);
18017
18018       decls_for_scope (stmt, subr_die, depth);
18019       current_function_has_inlines = 1;
18020     }
18021 }
18022
18023 /* Generate a DIE for a field in a record, or structure.  */
18024
18025 static void
18026 gen_field_die (tree decl, dw_die_ref context_die)
18027 {
18028   dw_die_ref decl_die;
18029
18030   if (TREE_TYPE (decl) == error_mark_node)
18031     return;
18032
18033   decl_die = new_die (DW_TAG_member, context_die, decl);
18034   add_name_and_src_coords_attributes (decl_die, decl);
18035   add_type_attribute (decl_die, member_declared_type (decl),
18036                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18037                       context_die);
18038
18039   if (DECL_BIT_FIELD_TYPE (decl))
18040     {
18041       add_byte_size_attribute (decl_die, decl);
18042       add_bit_size_attribute (decl_die, decl);
18043       add_bit_offset_attribute (decl_die, decl);
18044     }
18045
18046   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18047     add_data_member_location_attribute (decl_die, decl);
18048
18049   if (DECL_ARTIFICIAL (decl))
18050     add_AT_flag (decl_die, DW_AT_artificial, 1);
18051
18052   add_accessibility_attribute (decl_die, decl);
18053
18054   /* Equate decl number to die, so that we can look up this decl later on.  */
18055   equate_decl_number_to_die (decl, decl_die);
18056 }
18057
18058 #if 0
18059 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18060    Use modified_type_die instead.
18061    We keep this code here just in case these types of DIEs may be needed to
18062    represent certain things in other languages (e.g. Pascal) someday.  */
18063
18064 static void
18065 gen_pointer_type_die (tree type, dw_die_ref context_die)
18066 {
18067   dw_die_ref ptr_die
18068     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18069
18070   equate_type_number_to_die (type, ptr_die);
18071   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18072   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18073 }
18074
18075 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18076    Use modified_type_die instead.
18077    We keep this code here just in case these types of DIEs may be needed to
18078    represent certain things in other languages (e.g. Pascal) someday.  */
18079
18080 static void
18081 gen_reference_type_die (tree type, dw_die_ref context_die)
18082 {
18083   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18084
18085   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18086     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18087   else
18088     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18089
18090   equate_type_number_to_die (type, ref_die);
18091   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18092   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18093 }
18094 #endif
18095
18096 /* Generate a DIE for a pointer to a member type.  */
18097
18098 static void
18099 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18100 {
18101   dw_die_ref ptr_die
18102     = new_die (DW_TAG_ptr_to_member_type,
18103                scope_die_for (type, context_die), type);
18104
18105   equate_type_number_to_die (type, ptr_die);
18106   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18107                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18108   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18109 }
18110
18111 /* Generate the DIE for the compilation unit.  */
18112
18113 static dw_die_ref
18114 gen_compile_unit_die (const char *filename)
18115 {
18116   dw_die_ref die;
18117   char producer[250];
18118   const char *language_string = lang_hooks.name;
18119   int language;
18120
18121   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18122
18123   if (filename)
18124     {
18125       add_name_attribute (die, filename);
18126       /* Don't add cwd for <built-in>.  */
18127       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18128         add_comp_dir_attribute (die);
18129     }
18130
18131   sprintf (producer, "%s %s", language_string, version_string);
18132
18133 #ifdef MIPS_DEBUGGING_INFO
18134   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18135      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18136      not appear in the producer string, the debugger reaches the conclusion
18137      that the object file is stripped and has no debugging information.
18138      To get the MIPS/SGI debugger to believe that there is debugging
18139      information in the object file, we add a -g to the producer string.  */
18140   if (debug_info_level > DINFO_LEVEL_TERSE)
18141     strcat (producer, " -g");
18142 #endif
18143
18144   add_AT_string (die, DW_AT_producer, producer);
18145
18146   /* If our producer is LTO try to figure out a common language to use
18147      from the global list of translation units.  */
18148   if (strcmp (language_string, "GNU GIMPLE") == 0)
18149     {
18150       unsigned i;
18151       tree t;
18152       const char *common_lang = NULL;
18153
18154       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18155         {
18156           if (!TRANSLATION_UNIT_LANGUAGE (t))
18157             continue;
18158           if (!common_lang)
18159             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18160           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18161             ;
18162           else if (strncmp (common_lang, "GNU C", 5) == 0
18163                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18164             /* Mixing C and C++ is ok, use C++ in that case.  */
18165             common_lang = "GNU C++";
18166           else
18167             {
18168               /* Fall back to C.  */
18169               common_lang = NULL;
18170               break;
18171             }
18172         }
18173
18174       if (common_lang)
18175         language_string = common_lang;
18176     }
18177
18178   language = DW_LANG_C89;
18179   if (strcmp (language_string, "GNU C++") == 0)
18180     language = DW_LANG_C_plus_plus;
18181   else if (strcmp (language_string, "GNU F77") == 0)
18182     language = DW_LANG_Fortran77;
18183   else if (strcmp (language_string, "GNU Pascal") == 0)
18184     language = DW_LANG_Pascal83;
18185   else if (dwarf_version >= 3 || !dwarf_strict)
18186     {
18187       if (strcmp (language_string, "GNU Ada") == 0)
18188         language = DW_LANG_Ada95;
18189       else if (strcmp (language_string, "GNU Fortran") == 0)
18190         language = DW_LANG_Fortran95;
18191       else if (strcmp (language_string, "GNU Java") == 0)
18192         language = DW_LANG_Java;
18193       else if (strcmp (language_string, "GNU Objective-C") == 0)
18194         language = DW_LANG_ObjC;
18195       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18196         language = DW_LANG_ObjC_plus_plus;
18197     }
18198
18199   add_AT_unsigned (die, DW_AT_language, language);
18200
18201   switch (language)
18202     {
18203     case DW_LANG_Fortran77:
18204     case DW_LANG_Fortran90:
18205     case DW_LANG_Fortran95:
18206       /* Fortran has case insensitive identifiers and the front-end
18207          lowercases everything.  */
18208       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18209       break;
18210     default:
18211       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18212       break;
18213     }
18214   return die;
18215 }
18216
18217 /* Generate the DIE for a base class.  */
18218
18219 static void
18220 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18221 {
18222   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18223
18224   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18225   add_data_member_location_attribute (die, binfo);
18226
18227   if (BINFO_VIRTUAL_P (binfo))
18228     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18229
18230   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18231      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18232      the default has always been DW_ACCESS_private.  */
18233   if (access == access_public_node)
18234     {
18235       if (dwarf_version == 2
18236           || context_die->die_tag == DW_TAG_class_type)
18237       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18238     }
18239   else if (access == access_protected_node)
18240     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18241   else if (dwarf_version > 2
18242            && context_die->die_tag != DW_TAG_class_type)
18243     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18244 }
18245
18246 /* Generate a DIE for a class member.  */
18247
18248 static void
18249 gen_member_die (tree type, dw_die_ref context_die)
18250 {
18251   tree member;
18252   tree binfo = TYPE_BINFO (type);
18253   dw_die_ref child;
18254
18255   /* If this is not an incomplete type, output descriptions of each of its
18256      members. Note that as we output the DIEs necessary to represent the
18257      members of this record or union type, we will also be trying to output
18258      DIEs to represent the *types* of those members. However the `type'
18259      function (above) will specifically avoid generating type DIEs for member
18260      types *within* the list of member DIEs for this (containing) type except
18261      for those types (of members) which are explicitly marked as also being
18262      members of this (containing) type themselves.  The g++ front- end can
18263      force any given type to be treated as a member of some other (containing)
18264      type by setting the TYPE_CONTEXT of the given (member) type to point to
18265      the TREE node representing the appropriate (containing) type.  */
18266
18267   /* First output info about the base classes.  */
18268   if (binfo)
18269     {
18270       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18271       int i;
18272       tree base;
18273
18274       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18275         gen_inheritance_die (base,
18276                              (accesses ? VEC_index (tree, accesses, i)
18277                               : access_public_node), context_die);
18278     }
18279
18280   /* Now output info about the data members and type members.  */
18281   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18282     {
18283       /* If we thought we were generating minimal debug info for TYPE
18284          and then changed our minds, some of the member declarations
18285          may have already been defined.  Don't define them again, but
18286          do put them in the right order.  */
18287
18288       child = lookup_decl_die (member);
18289       if (child)
18290         splice_child_die (context_die, child);
18291       else
18292         gen_decl_die (member, NULL, context_die);
18293     }
18294
18295   /* Now output info about the function members (if any).  */
18296   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18297     {
18298       /* Don't include clones in the member list.  */
18299       if (DECL_ABSTRACT_ORIGIN (member))
18300         continue;
18301
18302       child = lookup_decl_die (member);
18303       if (child)
18304         splice_child_die (context_die, child);
18305       else
18306         gen_decl_die (member, NULL, context_die);
18307     }
18308 }
18309
18310 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18311    is set, we pretend that the type was never defined, so we only get the
18312    member DIEs needed by later specification DIEs.  */
18313
18314 static void
18315 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18316                                 enum debug_info_usage usage)
18317 {
18318   dw_die_ref type_die = lookup_type_die (type);
18319   dw_die_ref scope_die = 0;
18320   int nested = 0;
18321   int complete = (TYPE_SIZE (type)
18322                   && (! TYPE_STUB_DECL (type)
18323                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18324   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18325   complete = complete && should_emit_struct_debug (type, usage);
18326
18327   if (type_die && ! complete)
18328     return;
18329
18330   if (TYPE_CONTEXT (type) != NULL_TREE
18331       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18332           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18333     nested = 1;
18334
18335   scope_die = scope_die_for (type, context_die);
18336
18337   if (! type_die || (nested && is_cu_die (scope_die)))
18338     /* First occurrence of type or toplevel definition of nested class.  */
18339     {
18340       dw_die_ref old_die = type_die;
18341
18342       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18343                           ? record_type_tag (type) : DW_TAG_union_type,
18344                           scope_die, type);
18345       equate_type_number_to_die (type, type_die);
18346       if (old_die)
18347         add_AT_specification (type_die, old_die);
18348       else
18349         {
18350           add_name_attribute (type_die, type_tag (type));
18351           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18352           if (TYPE_ARTIFICIAL (type))
18353             add_AT_flag (type_die, DW_AT_artificial, 1);
18354         }
18355     }
18356   else
18357     remove_AT (type_die, DW_AT_declaration);
18358
18359   /* Generate child dies for template paramaters.  */
18360   if (debug_info_level > DINFO_LEVEL_TERSE
18361       && COMPLETE_TYPE_P (type))
18362     schedule_generic_params_dies_gen (type);
18363
18364   /* If this type has been completed, then give it a byte_size attribute and
18365      then give a list of members.  */
18366   if (complete && !ns_decl)
18367     {
18368       /* Prevent infinite recursion in cases where the type of some member of
18369          this type is expressed in terms of this type itself.  */
18370       TREE_ASM_WRITTEN (type) = 1;
18371       add_byte_size_attribute (type_die, type);
18372       if (TYPE_STUB_DECL (type) != NULL_TREE)
18373         {
18374           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18375           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18376         }
18377
18378       /* If the first reference to this type was as the return type of an
18379          inline function, then it may not have a parent.  Fix this now.  */
18380       if (type_die->die_parent == NULL)
18381         add_child_die (scope_die, type_die);
18382
18383       push_decl_scope (type);
18384       gen_member_die (type, type_die);
18385       pop_decl_scope ();
18386
18387       /* GNU extension: Record what type our vtable lives in.  */
18388       if (TYPE_VFIELD (type))
18389         {
18390           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18391
18392           gen_type_die (vtype, context_die);
18393           add_AT_die_ref (type_die, DW_AT_containing_type,
18394                           lookup_type_die (vtype));
18395         }
18396     }
18397   else
18398     {
18399       add_AT_flag (type_die, DW_AT_declaration, 1);
18400
18401       /* We don't need to do this for function-local types.  */
18402       if (TYPE_STUB_DECL (type)
18403           && ! decl_function_context (TYPE_STUB_DECL (type)))
18404         VEC_safe_push (tree, gc, incomplete_types, type);
18405     }
18406
18407   if (get_AT (type_die, DW_AT_name))
18408     add_pubtype (type, type_die);
18409 }
18410
18411 /* Generate a DIE for a subroutine _type_.  */
18412
18413 static void
18414 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18415 {
18416   tree return_type = TREE_TYPE (type);
18417   dw_die_ref subr_die
18418     = new_die (DW_TAG_subroutine_type,
18419                scope_die_for (type, context_die), type);
18420
18421   equate_type_number_to_die (type, subr_die);
18422   add_prototyped_attribute (subr_die, type);
18423   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18424   gen_formal_types_die (type, subr_die);
18425
18426   if (get_AT (subr_die, DW_AT_name))
18427     add_pubtype (type, subr_die);
18428 }
18429
18430 /* Generate a DIE for a type definition.  */
18431
18432 static void
18433 gen_typedef_die (tree decl, dw_die_ref context_die)
18434 {
18435   dw_die_ref type_die;
18436   tree origin;
18437
18438   if (TREE_ASM_WRITTEN (decl))
18439     return;
18440
18441   TREE_ASM_WRITTEN (decl) = 1;
18442   type_die = new_die (DW_TAG_typedef, context_die, decl);
18443   origin = decl_ultimate_origin (decl);
18444   if (origin != NULL)
18445     add_abstract_origin_attribute (type_die, origin);
18446   else
18447     {
18448       tree type;
18449
18450       add_name_and_src_coords_attributes (type_die, decl);
18451       if (DECL_ORIGINAL_TYPE (decl))
18452         {
18453           type = DECL_ORIGINAL_TYPE (decl);
18454
18455           gcc_assert (type != TREE_TYPE (decl));
18456           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18457         }
18458       else
18459         {
18460           type = TREE_TYPE (decl);
18461
18462           if (is_naming_typedef_decl (TYPE_NAME (type)))
18463             {
18464               /* Here, we are in the case of decl being a typedef naming
18465                  an anonymous type, e.g:
18466                      typedef struct {...} foo;
18467                  In that case TREE_TYPE (decl) is not a typedef variant
18468                  type and TYPE_NAME of the anonymous type is set to the
18469                  TYPE_DECL of the typedef. This construct is emitted by
18470                  the C++ FE.
18471
18472                  TYPE is the anonymous struct named by the typedef
18473                  DECL. As we need the DW_AT_type attribute of the
18474                  DW_TAG_typedef to point to the DIE of TYPE, let's
18475                  generate that DIE right away. add_type_attribute
18476                  called below will then pick (via lookup_type_die) that
18477                  anonymous struct DIE.  */
18478               if (!TREE_ASM_WRITTEN (type))
18479                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18480
18481               /* This is a GNU Extension.  We are adding a
18482                  DW_AT_linkage_name attribute to the DIE of the
18483                  anonymous struct TYPE.  The value of that attribute
18484                  is the name of the typedef decl naming the anonymous
18485                  struct.  This greatly eases the work of consumers of
18486                  this debug info.  */
18487               add_linkage_attr (lookup_type_die (type), decl);
18488             }
18489         }
18490
18491       add_type_attribute (type_die, type, TREE_READONLY (decl),
18492                           TREE_THIS_VOLATILE (decl), context_die);
18493
18494       if (is_naming_typedef_decl (decl))
18495         /* We want that all subsequent calls to lookup_type_die with
18496            TYPE in argument yield the DW_TAG_typedef we have just
18497            created.  */
18498         equate_type_number_to_die (type, type_die);
18499
18500       add_accessibility_attribute (type_die, decl);
18501     }
18502
18503   if (DECL_ABSTRACT (decl))
18504     equate_decl_number_to_die (decl, type_die);
18505
18506   if (get_AT (type_die, DW_AT_name))
18507     add_pubtype (decl, type_die);
18508 }
18509
18510 /* Generate a DIE for a struct, class, enum or union type.  */
18511
18512 static void
18513 gen_tagged_type_die (tree type,
18514                      dw_die_ref context_die,
18515                      enum debug_info_usage usage)
18516 {
18517   int need_pop;
18518
18519   if (type == NULL_TREE
18520       || !is_tagged_type (type))
18521     return;
18522
18523   /* If this is a nested type whose containing class hasn't been written
18524      out yet, writing it out will cover this one, too.  This does not apply
18525      to instantiations of member class templates; they need to be added to
18526      the containing class as they are generated.  FIXME: This hurts the
18527      idea of combining type decls from multiple TUs, since we can't predict
18528      what set of template instantiations we'll get.  */
18529   if (TYPE_CONTEXT (type)
18530       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18531       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18532     {
18533       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18534
18535       if (TREE_ASM_WRITTEN (type))
18536         return;
18537
18538       /* If that failed, attach ourselves to the stub.  */
18539       push_decl_scope (TYPE_CONTEXT (type));
18540       context_die = lookup_type_die (TYPE_CONTEXT (type));
18541       need_pop = 1;
18542     }
18543   else if (TYPE_CONTEXT (type) != NULL_TREE
18544            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18545     {
18546       /* If this type is local to a function that hasn't been written
18547          out yet, use a NULL context for now; it will be fixed up in
18548          decls_for_scope.  */
18549       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18550       /* A declaration DIE doesn't count; nested types need to go in the
18551          specification.  */
18552       if (context_die && is_declaration_die (context_die))
18553         context_die = NULL;
18554       need_pop = 0;
18555     }
18556   else
18557     {
18558       context_die = declare_in_namespace (type, context_die);
18559       need_pop = 0;
18560     }
18561
18562   if (TREE_CODE (type) == ENUMERAL_TYPE)
18563     {
18564       /* This might have been written out by the call to
18565          declare_in_namespace.  */
18566       if (!TREE_ASM_WRITTEN (type))
18567         gen_enumeration_type_die (type, context_die);
18568     }
18569   else
18570     gen_struct_or_union_type_die (type, context_die, usage);
18571
18572   if (need_pop)
18573     pop_decl_scope ();
18574
18575   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18576      it up if it is ever completed.  gen_*_type_die will set it for us
18577      when appropriate.  */
18578 }
18579
18580 /* Generate a type description DIE.  */
18581
18582 static void
18583 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18584                          enum debug_info_usage usage)
18585 {
18586   struct array_descr_info info;
18587
18588   if (type == NULL_TREE || type == error_mark_node)
18589     return;
18590
18591   if (TYPE_NAME (type) != NULL_TREE
18592       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18593       && is_redundant_typedef (TYPE_NAME (type))
18594       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18595     /* The DECL of this type is a typedef we don't want to emit debug
18596        info for but we want debug info for its underlying typedef.
18597        This can happen for e.g, the injected-class-name of a C++
18598        type.  */
18599     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18600
18601   /* If TYPE is a typedef type variant, let's generate debug info
18602      for the parent typedef which TYPE is a type of.  */
18603   if (typedef_variant_p (type))
18604     {
18605       if (TREE_ASM_WRITTEN (type))
18606         return;
18607
18608       /* Prevent broken recursion; we can't hand off to the same type.  */
18609       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18610
18611       /* Use the DIE of the containing namespace as the parent DIE of
18612          the type description DIE we want to generate.  */
18613       if (DECL_CONTEXT (TYPE_NAME (type))
18614           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18615         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18616
18617       TREE_ASM_WRITTEN (type) = 1;
18618
18619       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18620       return;
18621     }
18622
18623   /* If type is an anonymous tagged type named by a typedef, let's
18624      generate debug info for the typedef.  */
18625   if (is_naming_typedef_decl (TYPE_NAME (type)))
18626     {
18627       /* Use the DIE of the containing namespace as the parent DIE of
18628          the type description DIE we want to generate.  */
18629       if (DECL_CONTEXT (TYPE_NAME (type))
18630           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18631         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18632       
18633       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18634       return;
18635     }
18636
18637   /* If this is an array type with hidden descriptor, handle it first.  */
18638   if (!TREE_ASM_WRITTEN (type)
18639       && lang_hooks.types.get_array_descr_info
18640       && lang_hooks.types.get_array_descr_info (type, &info)
18641       && (dwarf_version >= 3 || !dwarf_strict))
18642     {
18643       gen_descr_array_type_die (type, &info, context_die);
18644       TREE_ASM_WRITTEN (type) = 1;
18645       return;
18646     }
18647
18648   /* We are going to output a DIE to represent the unqualified version
18649      of this type (i.e. without any const or volatile qualifiers) so
18650      get the main variant (i.e. the unqualified version) of this type
18651      now.  (Vectors are special because the debugging info is in the
18652      cloned type itself).  */
18653   if (TREE_CODE (type) != VECTOR_TYPE)
18654     type = type_main_variant (type);
18655
18656   if (TREE_ASM_WRITTEN (type))
18657     return;
18658
18659   switch (TREE_CODE (type))
18660     {
18661     case ERROR_MARK:
18662       break;
18663
18664     case POINTER_TYPE:
18665     case REFERENCE_TYPE:
18666       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18667          ensures that the gen_type_die recursion will terminate even if the
18668          type is recursive.  Recursive types are possible in Ada.  */
18669       /* ??? We could perhaps do this for all types before the switch
18670          statement.  */
18671       TREE_ASM_WRITTEN (type) = 1;
18672
18673       /* For these types, all that is required is that we output a DIE (or a
18674          set of DIEs) to represent the "basis" type.  */
18675       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18676                                 DINFO_USAGE_IND_USE);
18677       break;
18678
18679     case OFFSET_TYPE:
18680       /* This code is used for C++ pointer-to-data-member types.
18681          Output a description of the relevant class type.  */
18682       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18683                                         DINFO_USAGE_IND_USE);
18684
18685       /* Output a description of the type of the object pointed to.  */
18686       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18687                                         DINFO_USAGE_IND_USE);
18688
18689       /* Now output a DIE to represent this pointer-to-data-member type
18690          itself.  */
18691       gen_ptr_to_mbr_type_die (type, context_die);
18692       break;
18693
18694     case FUNCTION_TYPE:
18695       /* Force out return type (in case it wasn't forced out already).  */
18696       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18697                                         DINFO_USAGE_DIR_USE);
18698       gen_subroutine_type_die (type, context_die);
18699       break;
18700
18701     case METHOD_TYPE:
18702       /* Force out return type (in case it wasn't forced out already).  */
18703       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18704                                         DINFO_USAGE_DIR_USE);
18705       gen_subroutine_type_die (type, context_die);
18706       break;
18707
18708     case ARRAY_TYPE:
18709       gen_array_type_die (type, context_die);
18710       break;
18711
18712     case VECTOR_TYPE:
18713       gen_array_type_die (type, context_die);
18714       break;
18715
18716     case ENUMERAL_TYPE:
18717     case RECORD_TYPE:
18718     case UNION_TYPE:
18719     case QUAL_UNION_TYPE:
18720       gen_tagged_type_die (type, context_die, usage);
18721       return;
18722
18723     case VOID_TYPE:
18724     case INTEGER_TYPE:
18725     case REAL_TYPE:
18726     case FIXED_POINT_TYPE:
18727     case COMPLEX_TYPE:
18728     case BOOLEAN_TYPE:
18729       /* No DIEs needed for fundamental types.  */
18730       break;
18731
18732     case NULLPTR_TYPE:
18733     case LANG_TYPE:
18734       /* Just use DW_TAG_unspecified_type.  */
18735       {
18736         dw_die_ref type_die = lookup_type_die (type);
18737         if (type_die == NULL)
18738           {
18739             tree name = TYPE_NAME (type);
18740             if (TREE_CODE (name) == TYPE_DECL)
18741               name = DECL_NAME (name);
18742             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18743             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18744             equate_type_number_to_die (type, type_die);
18745           }
18746       }
18747       break;
18748
18749     default:
18750       gcc_unreachable ();
18751     }
18752
18753   TREE_ASM_WRITTEN (type) = 1;
18754 }
18755
18756 static void
18757 gen_type_die (tree type, dw_die_ref context_die)
18758 {
18759   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18760 }
18761
18762 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18763    things which are local to the given block.  */
18764
18765 static void
18766 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18767 {
18768   int must_output_die = 0;
18769   bool inlined_func;
18770
18771   /* Ignore blocks that are NULL.  */
18772   if (stmt == NULL_TREE)
18773     return;
18774
18775   inlined_func = inlined_function_outer_scope_p (stmt);
18776
18777   /* If the block is one fragment of a non-contiguous block, do not
18778      process the variables, since they will have been done by the
18779      origin block.  Do process subblocks.  */
18780   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18781     {
18782       tree sub;
18783
18784       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18785         gen_block_die (sub, context_die, depth + 1);
18786
18787       return;
18788     }
18789
18790   /* Determine if we need to output any Dwarf DIEs at all to represent this
18791      block.  */
18792   if (inlined_func)
18793     /* The outer scopes for inlinings *must* always be represented.  We
18794        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18795     must_output_die = 1;
18796   else
18797     {
18798       /* Determine if this block directly contains any "significant"
18799          local declarations which we will need to output DIEs for.  */
18800       if (debug_info_level > DINFO_LEVEL_TERSE)
18801         /* We are not in terse mode so *any* local declaration counts
18802            as being a "significant" one.  */
18803         must_output_die = ((BLOCK_VARS (stmt) != NULL
18804                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18805                            && (TREE_USED (stmt)
18806                                || TREE_ASM_WRITTEN (stmt)
18807                                || BLOCK_ABSTRACT (stmt)));
18808       else if ((TREE_USED (stmt)
18809                 || TREE_ASM_WRITTEN (stmt)
18810                 || BLOCK_ABSTRACT (stmt))
18811                && !dwarf2out_ignore_block (stmt))
18812         must_output_die = 1;
18813     }
18814
18815   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18816      DIE for any block which contains no significant local declarations at
18817      all.  Rather, in such cases we just call `decls_for_scope' so that any
18818      needed Dwarf info for any sub-blocks will get properly generated. Note
18819      that in terse mode, our definition of what constitutes a "significant"
18820      local declaration gets restricted to include only inlined function
18821      instances and local (nested) function definitions.  */
18822   if (must_output_die)
18823     {
18824       if (inlined_func)
18825         {
18826           /* If STMT block is abstract, that means we have been called
18827              indirectly from dwarf2out_abstract_function.
18828              That function rightfully marks the descendent blocks (of
18829              the abstract function it is dealing with) as being abstract,
18830              precisely to prevent us from emitting any
18831              DW_TAG_inlined_subroutine DIE as a descendent
18832              of an abstract function instance. So in that case, we should
18833              not call gen_inlined_subroutine_die.
18834
18835              Later though, when cgraph asks dwarf2out to emit info
18836              for the concrete instance of the function decl into which
18837              the concrete instance of STMT got inlined, the later will lead
18838              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18839           if (! BLOCK_ABSTRACT (stmt))
18840             gen_inlined_subroutine_die (stmt, context_die, depth);
18841         }
18842       else
18843         gen_lexical_block_die (stmt, context_die, depth);
18844     }
18845   else
18846     decls_for_scope (stmt, context_die, depth);
18847 }
18848
18849 /* Process variable DECL (or variable with origin ORIGIN) within
18850    block STMT and add it to CONTEXT_DIE.  */
18851 static void
18852 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18853 {
18854   dw_die_ref die;
18855   tree decl_or_origin = decl ? decl : origin;
18856
18857   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18858     die = lookup_decl_die (decl_or_origin);
18859   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18860            && TYPE_DECL_IS_STUB (decl_or_origin))
18861     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18862   else
18863     die = NULL;
18864
18865   if (die != NULL && die->die_parent == NULL)
18866     add_child_die (context_die, die);
18867   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18868     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18869                                          stmt, context_die);
18870   else
18871     gen_decl_die (decl, origin, context_die);
18872 }
18873
18874 /* Generate all of the decls declared within a given scope and (recursively)
18875    all of its sub-blocks.  */
18876
18877 static void
18878 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18879 {
18880   tree decl;
18881   unsigned int i;
18882   tree subblocks;
18883
18884   /* Ignore NULL blocks.  */
18885   if (stmt == NULL_TREE)
18886     return;
18887
18888   /* Output the DIEs to represent all of the data objects and typedefs
18889      declared directly within this block but not within any nested
18890      sub-blocks.  Also, nested function and tag DIEs have been
18891      generated with a parent of NULL; fix that up now.  */
18892   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18893     process_scope_var (stmt, decl, NULL_TREE, context_die);
18894   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18895     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18896                        context_die);
18897
18898   /* If we're at -g1, we're not interested in subblocks.  */
18899   if (debug_info_level <= DINFO_LEVEL_TERSE)
18900     return;
18901
18902   /* Output the DIEs to represent all sub-blocks (and the items declared
18903      therein) of this block.  */
18904   for (subblocks = BLOCK_SUBBLOCKS (stmt);
18905        subblocks != NULL;
18906        subblocks = BLOCK_CHAIN (subblocks))
18907     gen_block_die (subblocks, context_die, depth + 1);
18908 }
18909
18910 /* Is this a typedef we can avoid emitting?  */
18911
18912 static inline int
18913 is_redundant_typedef (const_tree decl)
18914 {
18915   if (TYPE_DECL_IS_STUB (decl))
18916     return 1;
18917
18918   if (DECL_ARTIFICIAL (decl)
18919       && DECL_CONTEXT (decl)
18920       && is_tagged_type (DECL_CONTEXT (decl))
18921       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18922       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18923     /* Also ignore the artificial member typedef for the class name.  */
18924     return 1;
18925
18926   return 0;
18927 }
18928
18929 /* Return TRUE if TYPE is a typedef that names a type for linkage
18930    purposes. This kind of typedefs is produced by the C++ FE for
18931    constructs like:
18932
18933    typedef struct {...} foo;
18934
18935    In that case, there is no typedef variant type produced for foo.
18936    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
18937    struct type.  */
18938
18939 static bool
18940 is_naming_typedef_decl (const_tree decl)
18941 {
18942   if (decl == NULL_TREE
18943       || TREE_CODE (decl) != TYPE_DECL
18944       || !is_tagged_type (TREE_TYPE (decl))
18945       || DECL_IS_BUILTIN (decl)
18946       || is_redundant_typedef (decl)
18947       /* It looks like Ada produces TYPE_DECLs that are very similar
18948          to C++ naming typedefs but that have different
18949          semantics. Let's be specific to c++ for now.  */
18950       || !is_cxx ())
18951     return FALSE;
18952
18953   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
18954           && TYPE_NAME (TREE_TYPE (decl)) == decl
18955           && (TYPE_STUB_DECL (TREE_TYPE (decl))
18956               != TYPE_NAME (TREE_TYPE (decl))));
18957 }
18958
18959 /* Returns the DIE for a context.  */
18960
18961 static inline dw_die_ref
18962 get_context_die (tree context)
18963 {
18964   if (context)
18965     {
18966       /* Find die that represents this context.  */
18967       if (TYPE_P (context))
18968         {
18969           context = TYPE_MAIN_VARIANT (context);
18970           return strip_naming_typedef (context, force_type_die (context));
18971         }
18972       else
18973         return force_decl_die (context);
18974     }
18975   return comp_unit_die ();
18976 }
18977
18978 /* Returns the DIE for decl.  A DIE will always be returned.  */
18979
18980 static dw_die_ref
18981 force_decl_die (tree decl)
18982 {
18983   dw_die_ref decl_die;
18984   unsigned saved_external_flag;
18985   tree save_fn = NULL_TREE;
18986   decl_die = lookup_decl_die (decl);
18987   if (!decl_die)
18988     {
18989       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
18990
18991       decl_die = lookup_decl_die (decl);
18992       if (decl_die)
18993         return decl_die;
18994
18995       switch (TREE_CODE (decl))
18996         {
18997         case FUNCTION_DECL:
18998           /* Clear current_function_decl, so that gen_subprogram_die thinks
18999              that this is a declaration. At this point, we just want to force
19000              declaration die.  */
19001           save_fn = current_function_decl;
19002           current_function_decl = NULL_TREE;
19003           gen_subprogram_die (decl, context_die);
19004           current_function_decl = save_fn;
19005           break;
19006
19007         case VAR_DECL:
19008           /* Set external flag to force declaration die. Restore it after
19009            gen_decl_die() call.  */
19010           saved_external_flag = DECL_EXTERNAL (decl);
19011           DECL_EXTERNAL (decl) = 1;
19012           gen_decl_die (decl, NULL, context_die);
19013           DECL_EXTERNAL (decl) = saved_external_flag;
19014           break;
19015
19016         case NAMESPACE_DECL:
19017           if (dwarf_version >= 3 || !dwarf_strict)
19018             dwarf2out_decl (decl);
19019           else
19020             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19021             decl_die = comp_unit_die ();
19022           break;
19023
19024         case TRANSLATION_UNIT_DECL:
19025           decl_die = comp_unit_die ();
19026           break;
19027
19028         default:
19029           gcc_unreachable ();
19030         }
19031
19032       /* We should be able to find the DIE now.  */
19033       if (!decl_die)
19034         decl_die = lookup_decl_die (decl);
19035       gcc_assert (decl_die);
19036     }
19037
19038   return decl_die;
19039 }
19040
19041 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19042    always returned.  */
19043
19044 static dw_die_ref
19045 force_type_die (tree type)
19046 {
19047   dw_die_ref type_die;
19048
19049   type_die = lookup_type_die (type);
19050   if (!type_die)
19051     {
19052       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19053
19054       type_die = modified_type_die (type, TYPE_READONLY (type),
19055                                     TYPE_VOLATILE (type), context_die);
19056       gcc_assert (type_die);
19057     }
19058   return type_die;
19059 }
19060
19061 /* Force out any required namespaces to be able to output DECL,
19062    and return the new context_die for it, if it's changed.  */
19063
19064 static dw_die_ref
19065 setup_namespace_context (tree thing, dw_die_ref context_die)
19066 {
19067   tree context = (DECL_P (thing)
19068                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19069   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19070     /* Force out the namespace.  */
19071     context_die = force_decl_die (context);
19072
19073   return context_die;
19074 }
19075
19076 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19077    type) within its namespace, if appropriate.
19078
19079    For compatibility with older debuggers, namespace DIEs only contain
19080    declarations; all definitions are emitted at CU scope.  */
19081
19082 static dw_die_ref
19083 declare_in_namespace (tree thing, dw_die_ref context_die)
19084 {
19085   dw_die_ref ns_context;
19086
19087   if (debug_info_level <= DINFO_LEVEL_TERSE)
19088     return context_die;
19089
19090   /* If this decl is from an inlined function, then don't try to emit it in its
19091      namespace, as we will get confused.  It would have already been emitted
19092      when the abstract instance of the inline function was emitted anyways.  */
19093   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19094     return context_die;
19095
19096   ns_context = setup_namespace_context (thing, context_die);
19097
19098   if (ns_context != context_die)
19099     {
19100       if (is_fortran ())
19101         return ns_context;
19102       if (DECL_P (thing))
19103         gen_decl_die (thing, NULL, ns_context);
19104       else
19105         gen_type_die (thing, ns_context);
19106     }
19107   return context_die;
19108 }
19109
19110 /* Generate a DIE for a namespace or namespace alias.  */
19111
19112 static void
19113 gen_namespace_die (tree decl, dw_die_ref context_die)
19114 {
19115   dw_die_ref namespace_die;
19116
19117   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19118      they are an alias of.  */
19119   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19120     {
19121       /* Output a real namespace or module.  */
19122       context_die = setup_namespace_context (decl, comp_unit_die ());
19123       namespace_die = new_die (is_fortran ()
19124                                ? DW_TAG_module : DW_TAG_namespace,
19125                                context_die, decl);
19126       /* For Fortran modules defined in different CU don't add src coords.  */
19127       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19128         {
19129           const char *name = dwarf2_name (decl, 0);
19130           if (name)
19131             add_name_attribute (namespace_die, name);
19132         }
19133       else
19134         add_name_and_src_coords_attributes (namespace_die, decl);
19135       if (DECL_EXTERNAL (decl))
19136         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19137       equate_decl_number_to_die (decl, namespace_die);
19138     }
19139   else
19140     {
19141       /* Output a namespace alias.  */
19142
19143       /* Force out the namespace we are an alias of, if necessary.  */
19144       dw_die_ref origin_die
19145         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19146
19147       if (DECL_FILE_SCOPE_P (decl)
19148           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19149         context_die = setup_namespace_context (decl, comp_unit_die ());
19150       /* Now create the namespace alias DIE.  */
19151       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19152       add_name_and_src_coords_attributes (namespace_die, decl);
19153       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19154       equate_decl_number_to_die (decl, namespace_die);
19155     }
19156 }
19157
19158 /* Generate Dwarf debug information for a decl described by DECL.
19159    The return value is currently only meaningful for PARM_DECLs,
19160    for all other decls it returns NULL.  */
19161
19162 static dw_die_ref
19163 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19164 {
19165   tree decl_or_origin = decl ? decl : origin;
19166   tree class_origin = NULL, ultimate_origin;
19167
19168   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19169     return NULL;
19170
19171   switch (TREE_CODE (decl_or_origin))
19172     {
19173     case ERROR_MARK:
19174       break;
19175
19176     case CONST_DECL:
19177       if (!is_fortran () && !is_ada ())
19178         {
19179           /* The individual enumerators of an enum type get output when we output
19180              the Dwarf representation of the relevant enum type itself.  */
19181           break;
19182         }
19183
19184       /* Emit its type.  */
19185       gen_type_die (TREE_TYPE (decl), context_die);
19186
19187       /* And its containing namespace.  */
19188       context_die = declare_in_namespace (decl, context_die);
19189
19190       gen_const_die (decl, context_die);
19191       break;
19192
19193     case FUNCTION_DECL:
19194       /* Don't output any DIEs to represent mere function declarations,
19195          unless they are class members or explicit block externs.  */
19196       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19197           && DECL_FILE_SCOPE_P (decl_or_origin)
19198           && (current_function_decl == NULL_TREE
19199               || DECL_ARTIFICIAL (decl_or_origin)))
19200         break;
19201
19202 #if 0
19203       /* FIXME */
19204       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19205          on local redeclarations of global functions.  That seems broken.  */
19206       if (current_function_decl != decl)
19207         /* This is only a declaration.  */;
19208 #endif
19209
19210       /* If we're emitting a clone, emit info for the abstract instance.  */
19211       if (origin || DECL_ORIGIN (decl) != decl)
19212         dwarf2out_abstract_function (origin
19213                                      ? DECL_ORIGIN (origin)
19214                                      : DECL_ABSTRACT_ORIGIN (decl));
19215
19216       /* If we're emitting an out-of-line copy of an inline function,
19217          emit info for the abstract instance and set up to refer to it.  */
19218       else if (cgraph_function_possibly_inlined_p (decl)
19219                && ! DECL_ABSTRACT (decl)
19220                && ! class_or_namespace_scope_p (context_die)
19221                /* dwarf2out_abstract_function won't emit a die if this is just
19222                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19223                   that case, because that works only if we have a die.  */
19224                && DECL_INITIAL (decl) != NULL_TREE)
19225         {
19226           dwarf2out_abstract_function (decl);
19227           set_decl_origin_self (decl);
19228         }
19229
19230       /* Otherwise we're emitting the primary DIE for this decl.  */
19231       else if (debug_info_level > DINFO_LEVEL_TERSE)
19232         {
19233           /* Before we describe the FUNCTION_DECL itself, make sure that we
19234              have its containing type.  */
19235           if (!origin)
19236             origin = decl_class_context (decl);
19237           if (origin != NULL_TREE)
19238             gen_type_die (origin, context_die);
19239
19240           /* And its return type.  */
19241           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19242
19243           /* And its virtual context.  */
19244           if (DECL_VINDEX (decl) != NULL_TREE)
19245             gen_type_die (DECL_CONTEXT (decl), context_die);
19246
19247           /* Make sure we have a member DIE for decl.  */
19248           if (origin != NULL_TREE)
19249             gen_type_die_for_member (origin, decl, context_die);
19250
19251           /* And its containing namespace.  */
19252           context_die = declare_in_namespace (decl, context_die);
19253         }
19254
19255       /* Now output a DIE to represent the function itself.  */
19256       if (decl)
19257         gen_subprogram_die (decl, context_die);
19258       break;
19259
19260     case TYPE_DECL:
19261       /* If we are in terse mode, don't generate any DIEs to represent any
19262          actual typedefs.  */
19263       if (debug_info_level <= DINFO_LEVEL_TERSE)
19264         break;
19265
19266       /* In the special case of a TYPE_DECL node representing the declaration
19267          of some type tag, if the given TYPE_DECL is marked as having been
19268          instantiated from some other (original) TYPE_DECL node (e.g. one which
19269          was generated within the original definition of an inline function) we
19270          used to generate a special (abbreviated) DW_TAG_structure_type,
19271          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19272          should be actually referencing those DIEs, as variable DIEs with that
19273          type would be emitted already in the abstract origin, so it was always
19274          removed during unused type prunning.  Don't add anything in this
19275          case.  */
19276       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19277         break;
19278
19279       if (is_redundant_typedef (decl))
19280         gen_type_die (TREE_TYPE (decl), context_die);
19281       else
19282         /* Output a DIE to represent the typedef itself.  */
19283         gen_typedef_die (decl, context_die);
19284       break;
19285
19286     case LABEL_DECL:
19287       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19288         gen_label_die (decl, context_die);
19289       break;
19290
19291     case VAR_DECL:
19292     case RESULT_DECL:
19293       /* If we are in terse mode, don't generate any DIEs to represent any
19294          variable declarations or definitions.  */
19295       if (debug_info_level <= DINFO_LEVEL_TERSE)
19296         break;
19297
19298       /* Output any DIEs that are needed to specify the type of this data
19299          object.  */
19300       if (decl_by_reference_p (decl_or_origin))
19301         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19302       else
19303         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19304
19305       /* And its containing type.  */
19306       class_origin = decl_class_context (decl_or_origin);
19307       if (class_origin != NULL_TREE)
19308         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19309
19310       /* And its containing namespace.  */
19311       context_die = declare_in_namespace (decl_or_origin, context_die);
19312
19313       /* Now output the DIE to represent the data object itself.  This gets
19314          complicated because of the possibility that the VAR_DECL really
19315          represents an inlined instance of a formal parameter for an inline
19316          function.  */
19317       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19318       if (ultimate_origin != NULL_TREE
19319           && TREE_CODE (ultimate_origin) == PARM_DECL)
19320         gen_formal_parameter_die (decl, origin,
19321                                   true /* Emit name attribute.  */,
19322                                   context_die);
19323       else
19324         gen_variable_die (decl, origin, context_die);
19325       break;
19326
19327     case FIELD_DECL:
19328       /* Ignore the nameless fields that are used to skip bits but handle C++
19329          anonymous unions and structs.  */
19330       if (DECL_NAME (decl) != NULL_TREE
19331           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19332           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19333         {
19334           gen_type_die (member_declared_type (decl), context_die);
19335           gen_field_die (decl, context_die);
19336         }
19337       break;
19338
19339     case PARM_DECL:
19340       if (DECL_BY_REFERENCE (decl_or_origin))
19341         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19342       else
19343         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19344       return gen_formal_parameter_die (decl, origin,
19345                                        true /* Emit name attribute.  */,
19346                                        context_die);
19347
19348     case NAMESPACE_DECL:
19349     case IMPORTED_DECL:
19350       if (dwarf_version >= 3 || !dwarf_strict)
19351         gen_namespace_die (decl, context_die);
19352       break;
19353
19354     default:
19355       /* Probably some frontend-internal decl.  Assume we don't care.  */
19356       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19357       break;
19358     }
19359
19360   return NULL;
19361 }
19362 \f
19363 /* Output debug information for global decl DECL.  Called from toplev.c after
19364    compilation proper has finished.  */
19365
19366 static void
19367 dwarf2out_global_decl (tree decl)
19368 {
19369   /* Output DWARF2 information for file-scope tentative data object
19370      declarations, file-scope (extern) function declarations (which
19371      had no corresponding body) and file-scope tagged type declarations
19372      and definitions which have not yet been forced out.  */
19373   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19374     dwarf2out_decl (decl);
19375 }
19376
19377 /* Output debug information for type decl DECL.  Called from toplev.c
19378    and from language front ends (to record built-in types).  */
19379 static void
19380 dwarf2out_type_decl (tree decl, int local)
19381 {
19382   if (!local)
19383     dwarf2out_decl (decl);
19384 }
19385
19386 /* Output debug information for imported module or decl DECL.
19387    NAME is non-NULL name in the lexical block if the decl has been renamed.
19388    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19389    that DECL belongs to.
19390    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19391 static void
19392 dwarf2out_imported_module_or_decl_1 (tree decl,
19393                                      tree name,
19394                                      tree lexical_block,
19395                                      dw_die_ref lexical_block_die)
19396 {
19397   expanded_location xloc;
19398   dw_die_ref imported_die = NULL;
19399   dw_die_ref at_import_die;
19400
19401   if (TREE_CODE (decl) == IMPORTED_DECL)
19402     {
19403       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19404       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19405       gcc_assert (decl);
19406     }
19407   else
19408     xloc = expand_location (input_location);
19409
19410   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19411     {
19412       at_import_die = force_type_die (TREE_TYPE (decl));
19413       /* For namespace N { typedef void T; } using N::T; base_type_die
19414          returns NULL, but DW_TAG_imported_declaration requires
19415          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19416       if (!at_import_die)
19417         {
19418           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19419           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19420           at_import_die = lookup_type_die (TREE_TYPE (decl));
19421           gcc_assert (at_import_die);
19422         }
19423     }
19424   else
19425     {
19426       at_import_die = lookup_decl_die (decl);
19427       if (!at_import_die)
19428         {
19429           /* If we're trying to avoid duplicate debug info, we may not have
19430              emitted the member decl for this field.  Emit it now.  */
19431           if (TREE_CODE (decl) == FIELD_DECL)
19432             {
19433               tree type = DECL_CONTEXT (decl);
19434
19435               if (TYPE_CONTEXT (type)
19436                   && TYPE_P (TYPE_CONTEXT (type))
19437                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19438                                                 DINFO_USAGE_DIR_USE))
19439                 return;
19440               gen_type_die_for_member (type, decl,
19441                                        get_context_die (TYPE_CONTEXT (type)));
19442             }
19443           at_import_die = force_decl_die (decl);
19444         }
19445     }
19446
19447   if (TREE_CODE (decl) == NAMESPACE_DECL)
19448     {
19449       if (dwarf_version >= 3 || !dwarf_strict)
19450         imported_die = new_die (DW_TAG_imported_module,
19451                                 lexical_block_die,
19452                                 lexical_block);
19453       else
19454         return;
19455     }
19456   else
19457     imported_die = new_die (DW_TAG_imported_declaration,
19458                             lexical_block_die,
19459                             lexical_block);
19460
19461   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19462   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19463   if (name)
19464     add_AT_string (imported_die, DW_AT_name,
19465                    IDENTIFIER_POINTER (name));
19466   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19467 }
19468
19469 /* Output debug information for imported module or decl DECL.
19470    NAME is non-NULL name in context if the decl has been renamed.
19471    CHILD is true if decl is one of the renamed decls as part of
19472    importing whole module.  */
19473
19474 static void
19475 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19476                                    bool child)
19477 {
19478   /* dw_die_ref at_import_die;  */
19479   dw_die_ref scope_die;
19480
19481   if (debug_info_level <= DINFO_LEVEL_TERSE)
19482     return;
19483
19484   gcc_assert (decl);
19485
19486   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19487      We need decl DIE for reference and scope die. First, get DIE for the decl
19488      itself.  */
19489
19490   /* Get the scope die for decl context. Use comp_unit_die for global module
19491      or decl. If die is not found for non globals, force new die.  */
19492   if (context
19493       && TYPE_P (context)
19494       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19495     return;
19496
19497   if (!(dwarf_version >= 3 || !dwarf_strict))
19498     return;
19499
19500   scope_die = get_context_die (context);
19501
19502   if (child)
19503     {
19504       gcc_assert (scope_die->die_child);
19505       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19506       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19507       scope_die = scope_die->die_child;
19508     }
19509
19510   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19511   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19512
19513 }
19514
19515 /* Write the debugging output for DECL.  */
19516
19517 void
19518 dwarf2out_decl (tree decl)
19519 {
19520   dw_die_ref context_die = comp_unit_die ();
19521
19522   switch (TREE_CODE (decl))
19523     {
19524     case ERROR_MARK:
19525       return;
19526
19527     case FUNCTION_DECL:
19528       /* What we would really like to do here is to filter out all mere
19529          file-scope declarations of file-scope functions which are never
19530          referenced later within this translation unit (and keep all of ones
19531          that *are* referenced later on) but we aren't clairvoyant, so we have
19532          no idea which functions will be referenced in the future (i.e. later
19533          on within the current translation unit). So here we just ignore all
19534          file-scope function declarations which are not also definitions.  If
19535          and when the debugger needs to know something about these functions,
19536          it will have to hunt around and find the DWARF information associated
19537          with the definition of the function.
19538
19539          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19540          nodes represent definitions and which ones represent mere
19541          declarations.  We have to check DECL_INITIAL instead. That's because
19542          the C front-end supports some weird semantics for "extern inline"
19543          function definitions.  These can get inlined within the current
19544          translation unit (and thus, we need to generate Dwarf info for their
19545          abstract instances so that the Dwarf info for the concrete inlined
19546          instances can have something to refer to) but the compiler never
19547          generates any out-of-lines instances of such things (despite the fact
19548          that they *are* definitions).
19549
19550          The important point is that the C front-end marks these "extern
19551          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19552          them anyway. Note that the C++ front-end also plays some similar games
19553          for inline function definitions appearing within include files which
19554          also contain `#pragma interface' pragmas.  */
19555       if (DECL_INITIAL (decl) == NULL_TREE)
19556         return;
19557
19558       /* If we're a nested function, initially use a parent of NULL; if we're
19559          a plain function, this will be fixed up in decls_for_scope.  If
19560          we're a method, it will be ignored, since we already have a DIE.  */
19561       if (decl_function_context (decl)
19562           /* But if we're in terse mode, we don't care about scope.  */
19563           && debug_info_level > DINFO_LEVEL_TERSE)
19564         context_die = NULL;
19565       break;
19566
19567     case VAR_DECL:
19568       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19569          declaration and if the declaration was never even referenced from
19570          within this entire compilation unit.  We suppress these DIEs in
19571          order to save space in the .debug section (by eliminating entries
19572          which are probably useless).  Note that we must not suppress
19573          block-local extern declarations (whether used or not) because that
19574          would screw-up the debugger's name lookup mechanism and cause it to
19575          miss things which really ought to be in scope at a given point.  */
19576       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19577         return;
19578
19579       /* For local statics lookup proper context die.  */
19580       if (TREE_STATIC (decl) && decl_function_context (decl))
19581         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19582
19583       /* If we are in terse mode, don't generate any DIEs to represent any
19584          variable declarations or definitions.  */
19585       if (debug_info_level <= DINFO_LEVEL_TERSE)
19586         return;
19587       break;
19588
19589     case CONST_DECL:
19590       if (debug_info_level <= DINFO_LEVEL_TERSE)
19591         return;
19592       if (!is_fortran () && !is_ada ())
19593         return;
19594       if (TREE_STATIC (decl) && decl_function_context (decl))
19595         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19596       break;
19597
19598     case NAMESPACE_DECL:
19599     case IMPORTED_DECL:
19600       if (debug_info_level <= DINFO_LEVEL_TERSE)
19601         return;
19602       if (lookup_decl_die (decl) != NULL)
19603         return;
19604       break;
19605
19606     case TYPE_DECL:
19607       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19608       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19609         return;
19610
19611       /* Don't bother trying to generate any DIEs to represent any of the
19612          normal built-in types for the language we are compiling.  */
19613       if (DECL_IS_BUILTIN (decl))
19614         return;
19615
19616       /* If we are in terse mode, don't generate any DIEs for types.  */
19617       if (debug_info_level <= DINFO_LEVEL_TERSE)
19618         return;
19619
19620       /* If we're a function-scope tag, initially use a parent of NULL;
19621          this will be fixed up in decls_for_scope.  */
19622       if (decl_function_context (decl))
19623         context_die = NULL;
19624
19625       break;
19626
19627     default:
19628       return;
19629     }
19630
19631   gen_decl_die (decl, NULL, context_die);
19632 }
19633
19634 /* Write the debugging output for DECL.  */
19635
19636 static void
19637 dwarf2out_function_decl (tree decl)
19638 {
19639   dwarf2out_decl (decl);
19640   call_arg_locations = NULL;
19641   call_arg_loc_last = NULL;
19642   call_site_count = -1;
19643   tail_call_site_count = -1;
19644   VEC_free (dw_die_ref, heap, block_map);
19645   htab_empty (decl_loc_table);
19646   htab_empty (cached_dw_loc_list_table);
19647 }
19648
19649 /* Output a marker (i.e. a label) for the beginning of the generated code for
19650    a lexical block.  */
19651
19652 static void
19653 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19654                        unsigned int blocknum)
19655 {
19656   switch_to_section (current_function_section ());
19657   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19658 }
19659
19660 /* Output a marker (i.e. a label) for the end of the generated code for a
19661    lexical block.  */
19662
19663 static void
19664 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19665 {
19666   switch_to_section (current_function_section ());
19667   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19668 }
19669
19670 /* Returns nonzero if it is appropriate not to emit any debugging
19671    information for BLOCK, because it doesn't contain any instructions.
19672
19673    Don't allow this for blocks with nested functions or local classes
19674    as we would end up with orphans, and in the presence of scheduling
19675    we may end up calling them anyway.  */
19676
19677 static bool
19678 dwarf2out_ignore_block (const_tree block)
19679 {
19680   tree decl;
19681   unsigned int i;
19682
19683   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19684     if (TREE_CODE (decl) == FUNCTION_DECL
19685         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19686       return 0;
19687   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19688     {
19689       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19690       if (TREE_CODE (decl) == FUNCTION_DECL
19691           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19692       return 0;
19693     }
19694
19695   return 1;
19696 }
19697
19698 /* Hash table routines for file_hash.  */
19699
19700 static int
19701 file_table_eq (const void *p1_p, const void *p2_p)
19702 {
19703   const struct dwarf_file_data *const p1 =
19704     (const struct dwarf_file_data *) p1_p;
19705   const char *const p2 = (const char *) p2_p;
19706   return filename_cmp (p1->filename, p2) == 0;
19707 }
19708
19709 static hashval_t
19710 file_table_hash (const void *p_p)
19711 {
19712   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19713   return htab_hash_string (p->filename);
19714 }
19715
19716 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19717    dwarf2out.c) and return its "index".  The index of each (known) filename is
19718    just a unique number which is associated with only that one filename.  We
19719    need such numbers for the sake of generating labels (in the .debug_sfnames
19720    section) and references to those files numbers (in the .debug_srcinfo
19721    and.debug_macinfo sections).  If the filename given as an argument is not
19722    found in our current list, add it to the list and assign it the next
19723    available unique index number.  In order to speed up searches, we remember
19724    the index of the filename was looked up last.  This handles the majority of
19725    all searches.  */
19726
19727 static struct dwarf_file_data *
19728 lookup_filename (const char *file_name)
19729 {
19730   void ** slot;
19731   struct dwarf_file_data * created;
19732
19733   /* Check to see if the file name that was searched on the previous
19734      call matches this file name.  If so, return the index.  */
19735   if (file_table_last_lookup
19736       && (file_name == file_table_last_lookup->filename
19737           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19738     return file_table_last_lookup;
19739
19740   /* Didn't match the previous lookup, search the table.  */
19741   slot = htab_find_slot_with_hash (file_table, file_name,
19742                                    htab_hash_string (file_name), INSERT);
19743   if (*slot)
19744     return (struct dwarf_file_data *) *slot;
19745
19746   created = ggc_alloc_dwarf_file_data ();
19747   created->filename = file_name;
19748   created->emitted_number = 0;
19749   *slot = created;
19750   return created;
19751 }
19752
19753 /* If the assembler will construct the file table, then translate the compiler
19754    internal file table number into the assembler file table number, and emit
19755    a .file directive if we haven't already emitted one yet.  The file table
19756    numbers are different because we prune debug info for unused variables and
19757    types, which may include filenames.  */
19758
19759 static int
19760 maybe_emit_file (struct dwarf_file_data * fd)
19761 {
19762   if (! fd->emitted_number)
19763     {
19764       if (last_emitted_file)
19765         fd->emitted_number = last_emitted_file->emitted_number + 1;
19766       else
19767         fd->emitted_number = 1;
19768       last_emitted_file = fd;
19769
19770       if (DWARF2_ASM_LINE_DEBUG_INFO)
19771         {
19772           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19773           output_quoted_string (asm_out_file,
19774                                 remap_debug_filename (fd->filename));
19775           fputc ('\n', asm_out_file);
19776         }
19777     }
19778
19779   return fd->emitted_number;
19780 }
19781
19782 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19783    That generation should happen after function debug info has been
19784    generated. The value of the attribute is the constant value of ARG.  */
19785
19786 static void
19787 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19788 {
19789   die_arg_entry entry;
19790
19791   if (!die || !arg)
19792     return;
19793
19794   if (!tmpl_value_parm_die_table)
19795     tmpl_value_parm_die_table
19796       = VEC_alloc (die_arg_entry, gc, 32);
19797
19798   entry.die = die;
19799   entry.arg = arg;
19800   VEC_safe_push (die_arg_entry, gc,
19801                  tmpl_value_parm_die_table,
19802                  &entry);
19803 }
19804
19805 /* Return TRUE if T is an instance of generic type, FALSE
19806    otherwise.  */
19807
19808 static bool
19809 generic_type_p (tree t)
19810 {
19811   if (t == NULL_TREE || !TYPE_P (t))
19812     return false;
19813   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19814 }
19815
19816 /* Schedule the generation of the generic parameter dies for the
19817   instance of generic type T. The proper generation itself is later
19818   done by gen_scheduled_generic_parms_dies. */
19819
19820 static void
19821 schedule_generic_params_dies_gen (tree t)
19822 {
19823   if (!generic_type_p (t))
19824     return;
19825
19826   if (generic_type_instances == NULL)
19827     generic_type_instances = VEC_alloc (tree, gc, 256);
19828
19829   VEC_safe_push (tree, gc, generic_type_instances, t);
19830 }
19831
19832 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19833    by append_entry_to_tmpl_value_parm_die_table. This function must
19834    be called after function DIEs have been generated.  */
19835
19836 static void
19837 gen_remaining_tmpl_value_param_die_attribute (void)
19838 {
19839   if (tmpl_value_parm_die_table)
19840     {
19841       unsigned i;
19842       die_arg_entry *e;
19843
19844       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19845         tree_add_const_value_attribute (e->die, e->arg);
19846     }
19847 }
19848
19849 /* Generate generic parameters DIEs for instances of generic types
19850    that have been previously scheduled by
19851    schedule_generic_params_dies_gen. This function must be called
19852    after all the types of the CU have been laid out.  */
19853
19854 static void
19855 gen_scheduled_generic_parms_dies (void)
19856 {
19857   unsigned i;
19858   tree t;
19859
19860   if (generic_type_instances == NULL)
19861     return;
19862   
19863   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19864     gen_generic_params_dies (t);
19865 }
19866
19867
19868 /* Replace DW_AT_name for the decl with name.  */
19869
19870 static void
19871 dwarf2out_set_name (tree decl, tree name)
19872 {
19873   dw_die_ref die;
19874   dw_attr_ref attr;
19875   const char *dname;
19876
19877   die = TYPE_SYMTAB_DIE (decl);
19878   if (!die)
19879     return;
19880
19881   dname = dwarf2_name (name, 0);
19882   if (!dname)
19883     return;
19884
19885   attr = get_AT (die, DW_AT_name);
19886   if (attr)
19887     {
19888       struct indirect_string_node *node;
19889
19890       node = find_AT_string (dname);
19891       /* replace the string.  */
19892       attr->dw_attr_val.v.val_str = node;
19893     }
19894
19895   else
19896     add_name_attribute (die, dname);
19897 }
19898
19899 /* Called by the final INSN scan whenever we see a var location.  We
19900    use it to drop labels in the right places, and throw the location in
19901    our lookup table.  */
19902
19903 static void
19904 dwarf2out_var_location (rtx loc_note)
19905 {
19906   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19907   struct var_loc_node *newloc;
19908   rtx next_real;
19909   static const char *last_label;
19910   static const char *last_postcall_label;
19911   static bool last_in_cold_section_p;
19912   tree decl;
19913   bool var_loc_p;
19914
19915   if (!NOTE_P (loc_note))
19916     {
19917       if (CALL_P (loc_note))
19918         {
19919           call_site_count++;
19920           if (SIBLING_CALL_P (loc_note))
19921             tail_call_site_count++;
19922         }
19923       return;
19924     }
19925
19926   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
19927   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
19928     return;
19929
19930   next_real = next_real_insn (loc_note);
19931
19932   /* If there are no instructions which would be affected by this note,
19933      don't do anything.  */
19934   if (var_loc_p
19935       && next_real == NULL_RTX
19936       && !NOTE_DURING_CALL_P (loc_note))
19937     return;
19938
19939   if (next_real == NULL_RTX)
19940     next_real = get_last_insn ();
19941
19942   /* If there were any real insns between note we processed last time
19943      and this note (or if it is the first note), clear
19944      last_{,postcall_}label so that they are not reused this time.  */
19945   if (last_var_location_insn == NULL_RTX
19946       || last_var_location_insn != next_real
19947       || last_in_cold_section_p != in_cold_section_p)
19948     {
19949       last_label = NULL;
19950       last_postcall_label = NULL;
19951     }
19952
19953   if (var_loc_p)
19954     {
19955       decl = NOTE_VAR_LOCATION_DECL (loc_note);
19956       newloc = add_var_loc_to_decl (decl, loc_note,
19957                                     NOTE_DURING_CALL_P (loc_note)
19958                                     ? last_postcall_label : last_label);
19959       if (newloc == NULL)
19960         return;
19961     }
19962   else
19963     {
19964       decl = NULL_TREE;
19965       newloc = NULL;
19966     }
19967
19968   /* If there were no real insns between note we processed last time
19969      and this note, use the label we emitted last time.  Otherwise
19970      create a new label and emit it.  */
19971   if (last_label == NULL)
19972     {
19973       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
19974       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
19975       loclabel_num++;
19976       last_label = ggc_strdup (loclabel);
19977     }
19978
19979   if (!var_loc_p)
19980     {
19981       struct call_arg_loc_node *ca_loc
19982         = ggc_alloc_cleared_call_arg_loc_node ();
19983       rtx prev = prev_real_insn (loc_note), x;
19984       ca_loc->call_arg_loc_note = loc_note;
19985       ca_loc->next = NULL;
19986       ca_loc->label = last_label;
19987       gcc_assert (prev
19988                   && (CALL_P (prev)
19989                       || (NONJUMP_INSN_P (prev)
19990                           && GET_CODE (PATTERN (prev)) == SEQUENCE
19991                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
19992       if (!CALL_P (prev))
19993         prev = XVECEXP (PATTERN (prev), 0, 0);
19994       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
19995       x = PATTERN (prev);
19996       if (GET_CODE (x) == PARALLEL)
19997         x = XVECEXP (x, 0, 0);
19998       if (GET_CODE (x) == SET)
19999         x = SET_SRC (x);
20000       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20001         {
20002           x = XEXP (XEXP (x, 0), 0);
20003           if (GET_CODE (x) == SYMBOL_REF
20004               && SYMBOL_REF_DECL (x)
20005               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20006             ca_loc->symbol_ref = x;
20007         }
20008       ca_loc->block = insn_scope (prev);
20009       if (call_arg_locations)
20010         call_arg_loc_last->next = ca_loc;
20011       else
20012         call_arg_locations = ca_loc;
20013       call_arg_loc_last = ca_loc;
20014     }
20015   else if (!NOTE_DURING_CALL_P (loc_note))
20016     newloc->label = last_label;
20017   else
20018     {
20019       if (!last_postcall_label)
20020         {
20021           sprintf (loclabel, "%s-1", last_label);
20022           last_postcall_label = ggc_strdup (loclabel);
20023         }
20024       newloc->label = last_postcall_label;
20025     }
20026
20027   last_var_location_insn = next_real;
20028   last_in_cold_section_p = in_cold_section_p;
20029 }
20030
20031 /* Note in one location list that text section has changed.  */
20032
20033 static int
20034 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20035 {
20036   var_loc_list *list = (var_loc_list *) *slot;
20037   if (list->first)
20038     list->last_before_switch
20039       = list->last->next ? list->last->next : list->last;
20040   return 1;
20041 }
20042
20043 /* Note in all location lists that text section has changed.  */
20044
20045 static void
20046 var_location_switch_text_section (void)
20047 {
20048   if (decl_loc_table == NULL)
20049     return;
20050
20051   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20052 }
20053
20054 /* Create a new line number table.  */
20055
20056 static dw_line_info_table *
20057 new_line_info_table (void)
20058 {
20059   dw_line_info_table *table;
20060
20061   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20062   table->file_num = 1;
20063   table->line_num = 1;
20064   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20065
20066   return table;
20067 }
20068
20069 /* Lookup the "current" table into which we emit line info, so
20070    that we don't have to do it for every source line.  */
20071
20072 static void
20073 set_cur_line_info_table (section *sec)
20074 {
20075   dw_line_info_table *table;
20076
20077   if (sec == text_section)
20078     table = text_section_line_info;
20079   else if (sec == cold_text_section)
20080     {
20081       table = cold_text_section_line_info;
20082       if (!table)
20083         {
20084           cold_text_section_line_info = table = new_line_info_table ();
20085           table->end_label = cold_end_label;
20086         }
20087     }
20088   else
20089     {
20090       const char *end_label;
20091
20092       if (flag_reorder_blocks_and_partition)
20093         {
20094           if (in_cold_section_p)
20095             end_label = crtl->subsections.cold_section_end_label;
20096           else
20097             end_label = crtl->subsections.hot_section_end_label;
20098         }
20099       else
20100         {
20101           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20102           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20103                                        current_function_funcdef_no);
20104           end_label = ggc_strdup (label);
20105         }
20106
20107       table = new_line_info_table ();
20108       table->end_label = end_label;
20109
20110       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20111     }
20112
20113   cur_line_info_table = table;
20114 }
20115
20116
20117 /* We need to reset the locations at the beginning of each
20118    function. We can't do this in the end_function hook, because the
20119    declarations that use the locations won't have been output when
20120    that hook is called.  Also compute have_multiple_function_sections here.  */
20121
20122 static void
20123 dwarf2out_begin_function (tree fun)
20124 {
20125   section *sec = function_section (fun);
20126
20127   if (sec != text_section)
20128     have_multiple_function_sections = true;
20129
20130   if (flag_reorder_blocks_and_partition && !cold_text_section)
20131     {
20132       gcc_assert (current_function_decl == fun);
20133       cold_text_section = unlikely_text_section ();
20134       switch_to_section (cold_text_section);
20135       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20136       switch_to_section (sec);
20137     }
20138
20139   dwarf2out_note_section_used ();
20140   call_site_count = 0;
20141   tail_call_site_count = 0;
20142
20143   set_cur_line_info_table (sec);
20144 }
20145
20146 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20147
20148 static void
20149 push_dw_line_info_entry (dw_line_info_table *table,
20150                          enum dw_line_info_opcode opcode, unsigned int val)
20151 {
20152   dw_line_info_entry e;
20153   e.opcode = opcode;
20154   e.val = val;
20155   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20156 }
20157
20158 /* Output a label to mark the beginning of a source code line entry
20159    and record information relating to this source line, in
20160    'line_info_table' for later output of the .debug_line section.  */
20161 /* ??? The discriminator parameter ought to be unsigned.  */
20162
20163 static void
20164 dwarf2out_source_line (unsigned int line, const char *filename,
20165                        int discriminator, bool is_stmt)
20166 {
20167   unsigned int file_num;
20168   dw_line_info_table *table;
20169
20170   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20171     return;
20172
20173   /* The discriminator column was added in dwarf4.  Simplify the below
20174      by simply removing it if we're not supposed to output it.  */
20175   if (dwarf_version < 4 && dwarf_strict)
20176     discriminator = 0;
20177
20178   table = cur_line_info_table;
20179   file_num = maybe_emit_file (lookup_filename (filename));
20180
20181   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20182      the debugger has used the second (possibly duplicate) line number
20183      at the beginning of the function to mark the end of the prologue.
20184      We could eliminate any other duplicates within the function.  For
20185      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20186      that second line number entry.  */
20187   /* Recall that this end-of-prologue indication is *not* the same thing
20188      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20189      to which the hook corresponds, follows the last insn that was 
20190      emitted by gen_prologue.  What we need is to preceed the first insn
20191      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20192      insn that corresponds to something the user wrote.  These may be
20193      very different locations once scheduling is enabled.  */
20194
20195   if (0 && file_num == table->file_num
20196       && line == table->line_num
20197       && discriminator == table->discrim_num
20198       && is_stmt == table->is_stmt)
20199     return;
20200
20201   switch_to_section (current_function_section ());
20202
20203   /* If requested, emit something human-readable.  */
20204   if (flag_debug_asm)
20205     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20206
20207   if (DWARF2_ASM_LINE_DEBUG_INFO)
20208     {
20209       /* Emit the .loc directive understood by GNU as.  */
20210       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20211       if (is_stmt != table->is_stmt)
20212         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20213       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20214         fprintf (asm_out_file, " discriminator %d", discriminator);
20215       fputc ('\n', asm_out_file);
20216     }
20217   else
20218     {
20219       unsigned int label_num = ++line_info_label_num;
20220
20221       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20222
20223       push_dw_line_info_entry (table, LI_set_address, label_num);
20224       if (file_num != table->file_num)
20225         push_dw_line_info_entry (table, LI_set_file, file_num);
20226       if (discriminator != table->discrim_num)
20227         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20228       if (is_stmt != table->is_stmt)
20229         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20230       push_dw_line_info_entry (table, LI_set_line, line);
20231     }
20232
20233   table->file_num = file_num;
20234   table->line_num = line;
20235   table->discrim_num = discriminator;
20236   table->is_stmt = is_stmt;
20237   table->in_use = true;
20238 }
20239
20240 /* Record the beginning of a new source file.  */
20241
20242 static void
20243 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20244 {
20245   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20246     {
20247       /* Record the beginning of the file for break_out_includes.  */
20248       dw_die_ref bincl_die;
20249
20250       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20251       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20252     }
20253
20254   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20255     {
20256       macinfo_entry e;
20257       e.code = DW_MACINFO_start_file;
20258       e.lineno = lineno;
20259       e.info = xstrdup (filename);
20260       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20261     }
20262 }
20263
20264 /* Record the end of a source file.  */
20265
20266 static void
20267 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20268 {
20269   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20270     /* Record the end of the file for break_out_includes.  */
20271     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20272
20273   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20274     {
20275       macinfo_entry e;
20276       e.code = DW_MACINFO_end_file;
20277       e.lineno = lineno;
20278       e.info = NULL;
20279       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20280     }
20281 }
20282
20283 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20284    the tail part of the directive line, i.e. the part which is past the
20285    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20286
20287 static void
20288 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20289                   const char *buffer ATTRIBUTE_UNUSED)
20290 {
20291   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20292     {
20293       macinfo_entry e;
20294       e.code = DW_MACINFO_define;
20295       e.lineno = lineno;
20296       e.info = xstrdup (buffer);;
20297       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20298     }
20299 }
20300
20301 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20302    the tail part of the directive line, i.e. the part which is past the
20303    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20304
20305 static void
20306 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20307                  const char *buffer ATTRIBUTE_UNUSED)
20308 {
20309   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20310     {
20311       macinfo_entry e;
20312       e.code = DW_MACINFO_undef;
20313       e.lineno = lineno;
20314       e.info = xstrdup (buffer);;
20315       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20316     }
20317 }
20318
20319 static void
20320 output_macinfo (void)
20321 {
20322   unsigned i;
20323   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20324   macinfo_entry *ref;
20325
20326   if (! length)
20327     return;
20328
20329   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20330     {
20331       switch (ref->code)
20332         {
20333           case DW_MACINFO_start_file:
20334             {
20335               int file_num = maybe_emit_file (lookup_filename (ref->info));
20336               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20337               dw2_asm_output_data_uleb128 
20338                         (ref->lineno, "Included from line number %lu", 
20339                                                 (unsigned long)ref->lineno);
20340               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20341             }
20342             break;
20343           case DW_MACINFO_end_file:
20344             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20345             break;
20346           case DW_MACINFO_define:
20347             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20348             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20349                                                 (unsigned long)ref->lineno);
20350             dw2_asm_output_nstring (ref->info, -1, "The macro");
20351             break;
20352           case DW_MACINFO_undef:
20353             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20354             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20355                                                 (unsigned long)ref->lineno);
20356             dw2_asm_output_nstring (ref->info, -1, "The macro");
20357             break;
20358           default:
20359            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20360              ASM_COMMENT_START, (unsigned long)ref->code);
20361           break;
20362         }
20363     }
20364 }
20365
20366 /* Set up for Dwarf output at the start of compilation.  */
20367
20368 static void
20369 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20370 {
20371   /* Allocate the file_table.  */
20372   file_table = htab_create_ggc (50, file_table_hash,
20373                                 file_table_eq, NULL);
20374
20375   /* Allocate the decl_die_table.  */
20376   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20377                                     decl_die_table_eq, NULL);
20378
20379   /* Allocate the decl_loc_table.  */
20380   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20381                                     decl_loc_table_eq, NULL);
20382
20383   /* Allocate the cached_dw_loc_list_table.  */
20384   cached_dw_loc_list_table
20385     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20386                        cached_dw_loc_list_table_eq, NULL);
20387
20388   /* Allocate the initial hunk of the decl_scope_table.  */
20389   decl_scope_table = VEC_alloc (tree, gc, 256);
20390
20391   /* Allocate the initial hunk of the abbrev_die_table.  */
20392   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20393     (ABBREV_DIE_TABLE_INCREMENT);
20394   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20395   /* Zero-th entry is allocated, but unused.  */
20396   abbrev_die_table_in_use = 1;
20397
20398   /* Allocate the pubtypes and pubnames vectors.  */
20399   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20400   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20401
20402   incomplete_types = VEC_alloc (tree, gc, 64);
20403
20404   used_rtx_array = VEC_alloc (rtx, gc, 32);
20405
20406   debug_info_section = get_section (DEBUG_INFO_SECTION,
20407                                     SECTION_DEBUG, NULL);
20408   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20409                                       SECTION_DEBUG, NULL);
20410   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20411                                        SECTION_DEBUG, NULL);
20412   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20413                                        SECTION_DEBUG, NULL);
20414   debug_line_section = get_section (DEBUG_LINE_SECTION,
20415                                     SECTION_DEBUG, NULL);
20416   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20417                                    SECTION_DEBUG, NULL);
20418   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20419                                         SECTION_DEBUG, NULL);
20420   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20421                                         SECTION_DEBUG, NULL);
20422   debug_str_section = get_section (DEBUG_STR_SECTION,
20423                                    DEBUG_STR_SECTION_FLAGS, NULL);
20424   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20425                                       SECTION_DEBUG, NULL);
20426   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20427                                      SECTION_DEBUG, NULL);
20428
20429   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20430   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20431                                DEBUG_ABBREV_SECTION_LABEL, 0);
20432   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20433   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20434                                COLD_TEXT_SECTION_LABEL, 0);
20435   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20436
20437   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20438                                DEBUG_INFO_SECTION_LABEL, 0);
20439   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20440                                DEBUG_LINE_SECTION_LABEL, 0);
20441   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20442                                DEBUG_RANGES_SECTION_LABEL, 0);
20443   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20444                                DEBUG_MACINFO_SECTION_LABEL, 0);
20445
20446   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20447     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20448
20449   switch_to_section (text_section);
20450   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20451
20452   /* Make sure the line number table for .text always exists.  */
20453   text_section_line_info = new_line_info_table ();
20454   text_section_line_info->end_label = text_end_label;
20455 }
20456
20457 /* Called before cgraph_optimize starts outputtting functions, variables
20458    and toplevel asms into assembly.  */
20459
20460 static void
20461 dwarf2out_assembly_start (void)
20462 {
20463   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20464       && dwarf2out_do_cfi_asm ()
20465       && (!(flag_unwind_tables || flag_exceptions)
20466           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20467     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20468 }
20469
20470 /* A helper function for dwarf2out_finish called through
20471    htab_traverse.  Emit one queued .debug_str string.  */
20472
20473 static int
20474 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20475 {
20476   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20477
20478   if (node->form == DW_FORM_strp)
20479     {
20480       switch_to_section (debug_str_section);
20481       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20482       assemble_string (node->str, strlen (node->str) + 1);
20483     }
20484
20485   return 1;
20486 }
20487
20488 #if ENABLE_ASSERT_CHECKING
20489 /* Verify that all marks are clear.  */
20490
20491 static void
20492 verify_marks_clear (dw_die_ref die)
20493 {
20494   dw_die_ref c;
20495
20496   gcc_assert (! die->die_mark);
20497   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20498 }
20499 #endif /* ENABLE_ASSERT_CHECKING */
20500
20501 /* Clear the marks for a die and its children.
20502    Be cool if the mark isn't set.  */
20503
20504 static void
20505 prune_unmark_dies (dw_die_ref die)
20506 {
20507   dw_die_ref c;
20508
20509   if (die->die_mark)
20510     die->die_mark = 0;
20511   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20512 }
20513
20514 /* Given DIE that we're marking as used, find any other dies
20515    it references as attributes and mark them as used.  */
20516
20517 static void
20518 prune_unused_types_walk_attribs (dw_die_ref die)
20519 {
20520   dw_attr_ref a;
20521   unsigned ix;
20522
20523   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20524     {
20525       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20526         {
20527           /* A reference to another DIE.
20528              Make sure that it will get emitted.
20529              If it was broken out into a comdat group, don't follow it.  */
20530           if (! use_debug_types
20531               || a->dw_attr == DW_AT_specification
20532               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20533             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20534         }
20535       /* Set the string's refcount to 0 so that prune_unused_types_mark
20536          accounts properly for it.  */
20537       if (AT_class (a) == dw_val_class_str)
20538         a->dw_attr_val.v.val_str->refcount = 0;
20539     }
20540 }
20541
20542 /* Mark the generic parameters and arguments children DIEs of DIE.  */
20543
20544 static void
20545 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20546 {
20547   dw_die_ref c;
20548
20549   if (die == NULL || die->die_child == NULL)
20550     return;
20551   c = die->die_child;
20552   do
20553     {
20554       switch (c->die_tag)
20555         {
20556         case DW_TAG_template_type_param:
20557         case DW_TAG_template_value_param:
20558         case DW_TAG_GNU_template_template_param:
20559         case DW_TAG_GNU_template_parameter_pack:
20560           prune_unused_types_mark (c, 1);
20561           break;
20562         default:
20563           break;
20564         }
20565       c = c->die_sib;
20566     } while (c && c != die->die_child);
20567 }
20568
20569 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20570    to DIE's children.  */
20571
20572 static void
20573 prune_unused_types_mark (dw_die_ref die, int dokids)
20574 {
20575   dw_die_ref c;
20576
20577   if (die->die_mark == 0)
20578     {
20579       /* We haven't done this node yet.  Mark it as used.  */
20580       die->die_mark = 1;
20581       /* If this is the DIE of a generic type instantiation,
20582          mark the children DIEs that describe its generic parms and
20583          args.  */
20584       prune_unused_types_mark_generic_parms_dies (die);
20585
20586       /* We also have to mark its parents as used.
20587          (But we don't want to mark our parents' kids due to this.)  */
20588       if (die->die_parent)
20589         prune_unused_types_mark (die->die_parent, 0);
20590
20591       /* Mark any referenced nodes.  */
20592       prune_unused_types_walk_attribs (die);
20593
20594       /* If this node is a specification,
20595          also mark the definition, if it exists.  */
20596       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20597         prune_unused_types_mark (die->die_definition, 1);
20598     }
20599
20600   if (dokids && die->die_mark != 2)
20601     {
20602       /* We need to walk the children, but haven't done so yet.
20603          Remember that we've walked the kids.  */
20604       die->die_mark = 2;
20605
20606       /* If this is an array type, we need to make sure our
20607          kids get marked, even if they're types.  If we're
20608          breaking out types into comdat sections, do this
20609          for all type definitions.  */
20610       if (die->die_tag == DW_TAG_array_type
20611           || (use_debug_types
20612               && is_type_die (die) && ! is_declaration_die (die)))
20613         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20614       else
20615         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20616     }
20617 }
20618
20619 /* For local classes, look if any static member functions were emitted
20620    and if so, mark them.  */
20621
20622 static void
20623 prune_unused_types_walk_local_classes (dw_die_ref die)
20624 {
20625   dw_die_ref c;
20626
20627   if (die->die_mark == 2)
20628     return;
20629
20630   switch (die->die_tag)
20631     {
20632     case DW_TAG_structure_type:
20633     case DW_TAG_union_type:
20634     case DW_TAG_class_type:
20635       break;
20636
20637     case DW_TAG_subprogram:
20638       if (!get_AT_flag (die, DW_AT_declaration)
20639           || die->die_definition != NULL)
20640         prune_unused_types_mark (die, 1);
20641       return;
20642
20643     default:
20644       return;
20645     }
20646
20647   /* Mark children.  */
20648   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20649 }
20650
20651 /* Walk the tree DIE and mark types that we actually use.  */
20652
20653 static void
20654 prune_unused_types_walk (dw_die_ref die)
20655 {
20656   dw_die_ref c;
20657
20658   /* Don't do anything if this node is already marked and
20659      children have been marked as well.  */
20660   if (die->die_mark == 2)
20661     return;
20662
20663   switch (die->die_tag)
20664     {
20665     case DW_TAG_structure_type:
20666     case DW_TAG_union_type:
20667     case DW_TAG_class_type:
20668       if (die->die_perennial_p)
20669         break;
20670
20671       for (c = die->die_parent; c; c = c->die_parent)
20672         if (c->die_tag == DW_TAG_subprogram)
20673           break;
20674
20675       /* Finding used static member functions inside of classes
20676          is needed just for local classes, because for other classes
20677          static member function DIEs with DW_AT_specification
20678          are emitted outside of the DW_TAG_*_type.  If we ever change
20679          it, we'd need to call this even for non-local classes.  */
20680       if (c)
20681         prune_unused_types_walk_local_classes (die);
20682
20683       /* It's a type node --- don't mark it.  */
20684       return;
20685
20686     case DW_TAG_const_type:
20687     case DW_TAG_packed_type:
20688     case DW_TAG_pointer_type:
20689     case DW_TAG_reference_type:
20690     case DW_TAG_rvalue_reference_type:
20691     case DW_TAG_volatile_type:
20692     case DW_TAG_typedef:
20693     case DW_TAG_array_type:
20694     case DW_TAG_interface_type:
20695     case DW_TAG_friend:
20696     case DW_TAG_variant_part:
20697     case DW_TAG_enumeration_type:
20698     case DW_TAG_subroutine_type:
20699     case DW_TAG_string_type:
20700     case DW_TAG_set_type:
20701     case DW_TAG_subrange_type:
20702     case DW_TAG_ptr_to_member_type:
20703     case DW_TAG_file_type:
20704       if (die->die_perennial_p)
20705         break;
20706
20707       /* It's a type node --- don't mark it.  */
20708       return;
20709
20710     default:
20711       /* Mark everything else.  */
20712       break;
20713   }
20714
20715   if (die->die_mark == 0)
20716     {
20717       die->die_mark = 1;
20718
20719       /* Now, mark any dies referenced from here.  */
20720       prune_unused_types_walk_attribs (die);
20721     }
20722
20723   die->die_mark = 2;
20724
20725   /* Mark children.  */
20726   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20727 }
20728
20729 /* Increment the string counts on strings referred to from DIE's
20730    attributes.  */
20731
20732 static void
20733 prune_unused_types_update_strings (dw_die_ref die)
20734 {
20735   dw_attr_ref a;
20736   unsigned ix;
20737
20738   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20739     if (AT_class (a) == dw_val_class_str)
20740       {
20741         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20742         s->refcount++;
20743         /* Avoid unnecessarily putting strings that are used less than
20744            twice in the hash table.  */
20745         if (s->refcount
20746             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20747           {
20748             void ** slot;
20749             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20750                                              htab_hash_string (s->str),
20751                                              INSERT);
20752             gcc_assert (*slot == NULL);
20753             *slot = s;
20754           }
20755       }
20756 }
20757
20758 /* Remove from the tree DIE any dies that aren't marked.  */
20759
20760 static void
20761 prune_unused_types_prune (dw_die_ref die)
20762 {
20763   dw_die_ref c;
20764
20765   gcc_assert (die->die_mark);
20766   prune_unused_types_update_strings (die);
20767
20768   if (! die->die_child)
20769     return;
20770
20771   c = die->die_child;
20772   do {
20773     dw_die_ref prev = c;
20774     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20775       if (c == die->die_child)
20776         {
20777           /* No marked children between 'prev' and the end of the list.  */
20778           if (prev == c)
20779             /* No marked children at all.  */
20780             die->die_child = NULL;
20781           else
20782             {
20783               prev->die_sib = c->die_sib;
20784               die->die_child = prev;
20785             }
20786           return;
20787         }
20788
20789     if (c != prev->die_sib)
20790       prev->die_sib = c;
20791     prune_unused_types_prune (c);
20792   } while (c != die->die_child);
20793 }
20794
20795 /* Remove dies representing declarations that we never use.  */
20796
20797 static void
20798 prune_unused_types (void)
20799 {
20800   unsigned int i;
20801   limbo_die_node *node;
20802   comdat_type_node *ctnode;
20803   pubname_ref pub;
20804   dw_die_ref base_type;
20805
20806 #if ENABLE_ASSERT_CHECKING
20807   /* All the marks should already be clear.  */
20808   verify_marks_clear (comp_unit_die ());
20809   for (node = limbo_die_list; node; node = node->next)
20810     verify_marks_clear (node->die);
20811   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20812     verify_marks_clear (ctnode->root_die);
20813 #endif /* ENABLE_ASSERT_CHECKING */
20814
20815   /* Mark types that are used in global variables.  */
20816   premark_types_used_by_global_vars ();
20817
20818   /* Set the mark on nodes that are actually used.  */
20819   prune_unused_types_walk (comp_unit_die ());
20820   for (node = limbo_die_list; node; node = node->next)
20821     prune_unused_types_walk (node->die);
20822   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20823     {
20824       prune_unused_types_walk (ctnode->root_die);
20825       prune_unused_types_mark (ctnode->type_die, 1);
20826     }
20827
20828   /* Also set the mark on nodes referenced from the
20829      pubname_table.  */
20830   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
20831     prune_unused_types_mark (pub->die, 1);
20832   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
20833     prune_unused_types_mark (base_type, 1);
20834
20835   if (debug_str_hash)
20836     htab_empty (debug_str_hash);
20837   prune_unused_types_prune (comp_unit_die ());
20838   for (node = limbo_die_list; node; node = node->next)
20839     prune_unused_types_prune (node->die);
20840   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20841     prune_unused_types_prune (ctnode->root_die);
20842
20843   /* Leave the marks clear.  */
20844   prune_unmark_dies (comp_unit_die ());
20845   for (node = limbo_die_list; node; node = node->next)
20846     prune_unmark_dies (node->die);
20847   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20848     prune_unmark_dies (ctnode->root_die);
20849 }
20850
20851 /* Set the parameter to true if there are any relative pathnames in
20852    the file table.  */
20853 static int
20854 file_table_relative_p (void ** slot, void *param)
20855 {
20856   bool *p = (bool *) param;
20857   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20858   if (!IS_ABSOLUTE_PATH (d->filename))
20859     {
20860       *p = true;
20861       return 0;
20862     }
20863   return 1;
20864 }
20865
20866 /* Routines to manipulate hash table of comdat type units.  */
20867
20868 static hashval_t
20869 htab_ct_hash (const void *of)
20870 {
20871   hashval_t h;
20872   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20873
20874   memcpy (&h, type_node->signature, sizeof (h));
20875   return h;
20876 }
20877
20878 static int
20879 htab_ct_eq (const void *of1, const void *of2)
20880 {
20881   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20882   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20883
20884   return (! memcmp (type_node_1->signature, type_node_2->signature,
20885                     DWARF_TYPE_SIGNATURE_SIZE));
20886 }
20887
20888 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
20889    to the location it would have been added, should we know its
20890    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20891    probably improve compactness of debug info, removing equivalent
20892    abbrevs, and hide any differences caused by deferring the
20893    computation of the assembler name, triggered by e.g. PCH.  */
20894
20895 static inline void
20896 move_linkage_attr (dw_die_ref die)
20897 {
20898   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20899   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
20900
20901   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
20902               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
20903
20904   while (--ix > 0)
20905     {
20906       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
20907
20908       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
20909         break;
20910     }
20911
20912   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
20913     {
20914       VEC_pop (dw_attr_node, die->die_attr);
20915       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
20916     }
20917 }
20918
20919 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
20920    referenced from typed stack ops and count how often they are used.  */
20921
20922 static void
20923 mark_base_types (dw_loc_descr_ref loc)
20924 {
20925   dw_die_ref base_type = NULL;
20926
20927   for (; loc; loc = loc->dw_loc_next)
20928     {
20929       switch (loc->dw_loc_opc)
20930         {
20931         case DW_OP_GNU_regval_type:
20932         case DW_OP_GNU_deref_type:
20933           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
20934           break;
20935         case DW_OP_GNU_convert:
20936         case DW_OP_GNU_reinterpret:
20937           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
20938             continue;
20939           /* FALLTHRU */
20940         case DW_OP_GNU_const_type:
20941           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
20942           break;
20943         case DW_OP_GNU_entry_value:
20944           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
20945           continue;
20946         default:
20947           continue;
20948         }
20949       gcc_assert (base_type->die_parent == comp_unit_die ());
20950       if (base_type->die_mark)
20951         base_type->die_mark++;
20952       else
20953         {
20954           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
20955           base_type->die_mark = 1;
20956         }
20957     }
20958 }
20959
20960 /* Comparison function for sorting marked base types.  */
20961
20962 static int
20963 base_type_cmp (const void *x, const void *y)
20964 {
20965   dw_die_ref dx = *(const dw_die_ref *) x;
20966   dw_die_ref dy = *(const dw_die_ref *) y;
20967   unsigned int byte_size1, byte_size2;
20968   unsigned int encoding1, encoding2;
20969   if (dx->die_mark > dy->die_mark)
20970     return -1;
20971   if (dx->die_mark < dy->die_mark)
20972     return 1;
20973   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
20974   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
20975   if (byte_size1 < byte_size2)
20976     return 1;
20977   if (byte_size1 > byte_size2)
20978     return -1;
20979   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
20980   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
20981   if (encoding1 < encoding2)
20982     return 1;
20983   if (encoding1 > encoding2)
20984     return -1;
20985   return 0;
20986 }
20987
20988 /* Move base types marked by mark_base_types as early as possible
20989    in the CU, sorted by decreasing usage count both to make the
20990    uleb128 references as small as possible and to make sure they
20991    will have die_offset already computed by calc_die_sizes when
20992    sizes of typed stack loc ops is computed.  */
20993
20994 static void
20995 move_marked_base_types (void)
20996 {
20997   unsigned int i;
20998   dw_die_ref base_type, die, c;
20999
21000   if (VEC_empty (dw_die_ref, base_types))
21001     return;
21002
21003   /* Sort by decreasing usage count, they will be added again in that
21004      order later on.  */
21005   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21006   die = comp_unit_die ();
21007   c = die->die_child;
21008   do
21009     {
21010       dw_die_ref prev = c;
21011       c = c->die_sib;
21012       while (c->die_mark)
21013         {
21014           remove_child_with_prev (c, prev);
21015           /* As base types got marked, there must be at least
21016              one node other than DW_TAG_base_type.  */
21017           gcc_assert (c != c->die_sib);
21018           c = c->die_sib;
21019         }
21020     }
21021   while (c != die->die_child);
21022   gcc_assert (die->die_child);
21023   c = die->die_child;
21024   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21025     {
21026       base_type->die_mark = 0;
21027       base_type->die_sib = c->die_sib;
21028       c->die_sib = base_type;
21029       c = base_type;
21030     }
21031 }
21032
21033 /* Helper function for resolve_addr, attempt to resolve
21034    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21035    SYMBOL_REFs refer to variables emitted in the current CU.  */
21036
21037 static int
21038 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21039 {
21040   rtx rtl = *addr;
21041
21042   if (GET_CODE (rtl) == CONST_STRING)
21043     {
21044       size_t len = strlen (XSTR (rtl, 0)) + 1;
21045       tree t = build_string (len, XSTR (rtl, 0));
21046       tree tlen = size_int (len - 1);
21047       TREE_TYPE (t)
21048         = build_array_type (char_type_node, build_index_type (tlen));
21049       rtl = lookup_constant_def (t);
21050       if (!rtl || !MEM_P (rtl))
21051         return 1;
21052       rtl = XEXP (rtl, 0);
21053       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21054       *addr = rtl;
21055       return 0;
21056     }
21057
21058   if (GET_CODE (rtl) == SYMBOL_REF
21059       && SYMBOL_REF_DECL (rtl))
21060     {
21061       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21062         {
21063           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21064             return 1;
21065         }
21066       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21067         return 1;
21068     }
21069
21070   if (GET_CODE (rtl) == CONST
21071       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21072     return 1;
21073
21074   return 0;
21075 }
21076
21077 /* Helper function for resolve_addr, handle one location
21078    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21079    the location list couldn't be resolved.  */
21080
21081 static bool
21082 resolve_addr_in_expr (dw_loc_descr_ref loc)
21083 {
21084   dw_loc_descr_ref keep = NULL;
21085   for (; loc; loc = loc->dw_loc_next)
21086     switch (loc->dw_loc_opc)
21087       {
21088       case DW_OP_addr:
21089         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21090           return false;
21091         break;
21092       case DW_OP_const4u:
21093       case DW_OP_const8u:
21094         if (loc->dtprel
21095             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21096           return false;
21097         break;
21098       case DW_OP_plus_uconst:
21099         if (size_of_loc_descr (loc)
21100             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21101               + 1
21102             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21103           {
21104             dw_loc_descr_ref repl
21105               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21106             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21107             add_loc_descr (&repl, loc->dw_loc_next);
21108             *loc = *repl;
21109           }
21110         break;
21111       case DW_OP_implicit_value:
21112         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21113             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21114           return false;
21115         break;
21116       case DW_OP_GNU_implicit_pointer:
21117       case DW_OP_GNU_parameter_ref:
21118         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21119           {
21120             dw_die_ref ref
21121               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21122             if (ref == NULL)
21123               return false;
21124             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21125             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21126             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21127           }
21128         break;
21129       case DW_OP_GNU_const_type:
21130       case DW_OP_GNU_regval_type:
21131       case DW_OP_GNU_deref_type:
21132       case DW_OP_GNU_convert:
21133       case DW_OP_GNU_reinterpret:
21134         while (loc->dw_loc_next
21135                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21136           {
21137             dw_die_ref base1, base2;
21138             unsigned enc1, enc2, size1, size2;
21139             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21140                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21141               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21142             else if (loc->dw_loc_oprnd1.val_class
21143                      == dw_val_class_unsigned_const)
21144               break;
21145             else
21146               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21147             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21148                 == dw_val_class_unsigned_const)
21149               break;
21150             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21151             gcc_assert (base1->die_tag == DW_TAG_base_type
21152                         && base2->die_tag == DW_TAG_base_type);
21153             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21154             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21155             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21156             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21157             if (size1 == size2
21158                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21159                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21160                      && loc != keep)
21161                     || enc1 == enc2))
21162               {
21163                 /* Optimize away next DW_OP_GNU_convert after
21164                    adjusting LOC's base type die reference.  */
21165                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21166                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21167                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21168                 else
21169                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21170                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21171                 continue;
21172               }
21173             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21174                point typed stack entry.  */
21175             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21176               keep = loc->dw_loc_next;
21177             break;
21178           }
21179         break;
21180       default:
21181         break;
21182       }
21183   return true;
21184 }
21185
21186 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21187    an address in .rodata section if the string literal is emitted there,
21188    or remove the containing location list or replace DW_AT_const_value
21189    with DW_AT_location and empty location expression, if it isn't found
21190    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21191    to something that has been emitted in the current CU.  */
21192
21193 static void
21194 resolve_addr (dw_die_ref die)
21195 {
21196   dw_die_ref c;
21197   dw_attr_ref a;
21198   dw_loc_list_ref *curr, *start, loc;
21199   unsigned ix;
21200
21201   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21202     switch (AT_class (a))
21203       {
21204       case dw_val_class_loc_list:
21205         start = curr = AT_loc_list_ptr (a);
21206         loc = *curr;
21207         gcc_assert (loc);
21208         /* The same list can be referenced more than once.  See if we have
21209            already recorded the result from a previous pass.  */
21210         if (loc->replaced)
21211           *curr = loc->dw_loc_next;
21212         else if (!loc->resolved_addr)
21213           {
21214             /* As things stand, we do not expect or allow one die to
21215                reference a suffix of another die's location list chain.
21216                References must be identical or completely separate.
21217                There is therefore no need to cache the result of this
21218                pass on any list other than the first; doing so
21219                would lead to unnecessary writes.  */
21220             while (*curr)
21221               {
21222                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21223                 if (!resolve_addr_in_expr ((*curr)->expr))
21224                   {
21225                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21226                     if (next && (*curr)->ll_symbol)
21227                       {
21228                         gcc_assert (!next->ll_symbol);
21229                         next->ll_symbol = (*curr)->ll_symbol;
21230                       }
21231                     *curr = next;
21232                   }
21233                 else
21234                   {
21235                     mark_base_types ((*curr)->expr);
21236                     curr = &(*curr)->dw_loc_next;
21237                   }
21238               }
21239             if (loc == *start)
21240               loc->resolved_addr = 1;
21241             else
21242               {
21243                 loc->replaced = 1;
21244                 loc->dw_loc_next = *start;
21245               }
21246           }
21247         if (!*start)
21248           {
21249             remove_AT (die, a->dw_attr);
21250             ix--;
21251           }
21252         break;
21253       case dw_val_class_loc:
21254         if (!resolve_addr_in_expr (AT_loc (a)))
21255           {
21256             remove_AT (die, a->dw_attr);
21257             ix--;
21258           }
21259         else
21260           mark_base_types (AT_loc (a));
21261         break;
21262       case dw_val_class_addr:
21263         if (a->dw_attr == DW_AT_const_value
21264             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21265           {
21266             remove_AT (die, a->dw_attr);
21267             ix--;
21268           }
21269         if (die->die_tag == DW_TAG_GNU_call_site
21270             && a->dw_attr == DW_AT_abstract_origin)
21271           {
21272             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21273             dw_die_ref tdie = lookup_decl_die (tdecl);
21274             if (tdie == NULL
21275                 && DECL_EXTERNAL (tdecl)
21276                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21277               {
21278                 force_decl_die (tdecl);
21279                 tdie = lookup_decl_die (tdecl);
21280               }
21281             if (tdie)
21282               {
21283                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21284                 a->dw_attr_val.v.val_die_ref.die = tdie;
21285                 a->dw_attr_val.v.val_die_ref.external = 0;
21286               }
21287             else
21288               {
21289                 remove_AT (die, a->dw_attr);
21290                 ix--;
21291               }
21292           }
21293         break;
21294       default:
21295         break;
21296       }
21297
21298   FOR_EACH_CHILD (die, c, resolve_addr (c));
21299 }
21300 \f
21301 /* Helper routines for optimize_location_lists.
21302    This pass tries to share identical local lists in .debug_loc
21303    section.  */
21304
21305 /* Iteratively hash operands of LOC opcode.  */
21306
21307 static inline hashval_t
21308 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21309 {
21310   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21311   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21312
21313   switch (loc->dw_loc_opc)
21314     {
21315     case DW_OP_const4u:
21316     case DW_OP_const8u:
21317       if (loc->dtprel)
21318         goto hash_addr;
21319       /* FALLTHRU */
21320     case DW_OP_const1u:
21321     case DW_OP_const1s:
21322     case DW_OP_const2u:
21323     case DW_OP_const2s:
21324     case DW_OP_const4s:
21325     case DW_OP_const8s:
21326     case DW_OP_constu:
21327     case DW_OP_consts:
21328     case DW_OP_pick:
21329     case DW_OP_plus_uconst:
21330     case DW_OP_breg0:
21331     case DW_OP_breg1:
21332     case DW_OP_breg2:
21333     case DW_OP_breg3:
21334     case DW_OP_breg4:
21335     case DW_OP_breg5:
21336     case DW_OP_breg6:
21337     case DW_OP_breg7:
21338     case DW_OP_breg8:
21339     case DW_OP_breg9:
21340     case DW_OP_breg10:
21341     case DW_OP_breg11:
21342     case DW_OP_breg12:
21343     case DW_OP_breg13:
21344     case DW_OP_breg14:
21345     case DW_OP_breg15:
21346     case DW_OP_breg16:
21347     case DW_OP_breg17:
21348     case DW_OP_breg18:
21349     case DW_OP_breg19:
21350     case DW_OP_breg20:
21351     case DW_OP_breg21:
21352     case DW_OP_breg22:
21353     case DW_OP_breg23:
21354     case DW_OP_breg24:
21355     case DW_OP_breg25:
21356     case DW_OP_breg26:
21357     case DW_OP_breg27:
21358     case DW_OP_breg28:
21359     case DW_OP_breg29:
21360     case DW_OP_breg30:
21361     case DW_OP_breg31:
21362     case DW_OP_regx:
21363     case DW_OP_fbreg:
21364     case DW_OP_piece:
21365     case DW_OP_deref_size:
21366     case DW_OP_xderef_size:
21367       hash = iterative_hash_object (val1->v.val_int, hash);
21368       break;
21369     case DW_OP_skip:
21370     case DW_OP_bra:
21371       {
21372         int offset;
21373
21374         gcc_assert (val1->val_class == dw_val_class_loc);
21375         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21376         hash = iterative_hash_object (offset, hash);
21377       }
21378       break;
21379     case DW_OP_implicit_value:
21380       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21381       switch (val2->val_class)
21382         {
21383         case dw_val_class_const:
21384           hash = iterative_hash_object (val2->v.val_int, hash);
21385           break;
21386         case dw_val_class_vec:
21387           {
21388             unsigned int elt_size = val2->v.val_vec.elt_size;
21389             unsigned int len = val2->v.val_vec.length;
21390
21391             hash = iterative_hash_object (elt_size, hash);
21392             hash = iterative_hash_object (len, hash);
21393             hash = iterative_hash (val2->v.val_vec.array,
21394                                    len * elt_size, hash);
21395           }
21396           break;
21397         case dw_val_class_const_double:
21398           hash = iterative_hash_object (val2->v.val_double.low, hash);
21399           hash = iterative_hash_object (val2->v.val_double.high, hash);
21400           break;
21401         case dw_val_class_addr:
21402           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21403           break;
21404         default:
21405           gcc_unreachable ();
21406         }
21407       break;
21408     case DW_OP_bregx:
21409     case DW_OP_bit_piece:
21410       hash = iterative_hash_object (val1->v.val_int, hash);
21411       hash = iterative_hash_object (val2->v.val_int, hash);
21412       break;
21413     case DW_OP_addr:
21414     hash_addr:
21415       if (loc->dtprel)
21416         {
21417           unsigned char dtprel = 0xd1;
21418           hash = iterative_hash_object (dtprel, hash);
21419         }
21420       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21421       break;
21422     case DW_OP_GNU_implicit_pointer:
21423       hash = iterative_hash_object (val2->v.val_int, hash);
21424       break;
21425     case DW_OP_GNU_entry_value:
21426       hash = hash_loc_operands (val1->v.val_loc, hash);
21427       break;
21428     case DW_OP_GNU_regval_type:
21429     case DW_OP_GNU_deref_type:
21430       {
21431         unsigned int byte_size
21432           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21433         unsigned int encoding
21434           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21435         hash = iterative_hash_object (val1->v.val_int, hash);
21436         hash = iterative_hash_object (byte_size, hash);
21437         hash = iterative_hash_object (encoding, hash);
21438       }
21439       break;
21440     case DW_OP_GNU_convert:
21441     case DW_OP_GNU_reinterpret:
21442       if (val1->val_class == dw_val_class_unsigned_const)
21443         {
21444           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21445           break;
21446         }
21447       /* FALLTHRU */
21448     case DW_OP_GNU_const_type:
21449       {
21450         unsigned int byte_size
21451           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21452         unsigned int encoding
21453           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21454         hash = iterative_hash_object (byte_size, hash);
21455         hash = iterative_hash_object (encoding, hash);
21456         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21457           break;
21458         hash = iterative_hash_object (val2->val_class, hash);
21459         switch (val2->val_class)
21460           {
21461           case dw_val_class_const:
21462             hash = iterative_hash_object (val2->v.val_int, hash);
21463             break;
21464           case dw_val_class_vec:
21465             {
21466               unsigned int elt_size = val2->v.val_vec.elt_size;
21467               unsigned int len = val2->v.val_vec.length;
21468
21469               hash = iterative_hash_object (elt_size, hash);
21470               hash = iterative_hash_object (len, hash);
21471               hash = iterative_hash (val2->v.val_vec.array,
21472                                      len * elt_size, hash);
21473             }
21474             break;
21475           case dw_val_class_const_double:
21476             hash = iterative_hash_object (val2->v.val_double.low, hash);
21477             hash = iterative_hash_object (val2->v.val_double.high, hash);
21478             break;
21479           default:
21480             gcc_unreachable ();
21481           }
21482       }
21483       break;
21484
21485     default:
21486       /* Other codes have no operands.  */
21487       break;
21488     }
21489   return hash;
21490 }
21491
21492 /* Iteratively hash the whole DWARF location expression LOC.  */
21493
21494 static inline hashval_t
21495 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21496 {
21497   dw_loc_descr_ref l;
21498   bool sizes_computed = false;
21499   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21500   size_of_locs (loc);
21501
21502   for (l = loc; l != NULL; l = l->dw_loc_next)
21503     {
21504       enum dwarf_location_atom opc = l->dw_loc_opc;
21505       hash = iterative_hash_object (opc, hash);
21506       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21507         {
21508           size_of_locs (loc);
21509           sizes_computed = true;
21510         }
21511       hash = hash_loc_operands (l, hash);
21512     }
21513   return hash;
21514 }
21515
21516 /* Compute hash of the whole location list LIST_HEAD.  */
21517
21518 static inline void
21519 hash_loc_list (dw_loc_list_ref list_head)
21520 {
21521   dw_loc_list_ref curr = list_head;
21522   hashval_t hash = 0;
21523
21524   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21525     {
21526       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21527       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21528       if (curr->section)
21529         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21530                                hash);
21531       hash = hash_locs (curr->expr, hash);
21532     }
21533   list_head->hash = hash;
21534 }
21535
21536 /* Return true if X and Y opcodes have the same operands.  */
21537
21538 static inline bool
21539 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
21540 {
21541   dw_val_ref valx1 = &x->dw_loc_oprnd1;
21542   dw_val_ref valx2 = &x->dw_loc_oprnd2;
21543   dw_val_ref valy1 = &y->dw_loc_oprnd1;
21544   dw_val_ref valy2 = &y->dw_loc_oprnd2;
21545
21546   switch (x->dw_loc_opc)
21547     {
21548     case DW_OP_const4u:
21549     case DW_OP_const8u:
21550       if (x->dtprel)
21551         goto hash_addr;
21552       /* FALLTHRU */
21553     case DW_OP_const1u:
21554     case DW_OP_const1s:
21555     case DW_OP_const2u:
21556     case DW_OP_const2s:
21557     case DW_OP_const4s:
21558     case DW_OP_const8s:
21559     case DW_OP_constu:
21560     case DW_OP_consts:
21561     case DW_OP_pick:
21562     case DW_OP_plus_uconst:
21563     case DW_OP_breg0:
21564     case DW_OP_breg1:
21565     case DW_OP_breg2:
21566     case DW_OP_breg3:
21567     case DW_OP_breg4:
21568     case DW_OP_breg5:
21569     case DW_OP_breg6:
21570     case DW_OP_breg7:
21571     case DW_OP_breg8:
21572     case DW_OP_breg9:
21573     case DW_OP_breg10:
21574     case DW_OP_breg11:
21575     case DW_OP_breg12:
21576     case DW_OP_breg13:
21577     case DW_OP_breg14:
21578     case DW_OP_breg15:
21579     case DW_OP_breg16:
21580     case DW_OP_breg17:
21581     case DW_OP_breg18:
21582     case DW_OP_breg19:
21583     case DW_OP_breg20:
21584     case DW_OP_breg21:
21585     case DW_OP_breg22:
21586     case DW_OP_breg23:
21587     case DW_OP_breg24:
21588     case DW_OP_breg25:
21589     case DW_OP_breg26:
21590     case DW_OP_breg27:
21591     case DW_OP_breg28:
21592     case DW_OP_breg29:
21593     case DW_OP_breg30:
21594     case DW_OP_breg31:
21595     case DW_OP_regx:
21596     case DW_OP_fbreg:
21597     case DW_OP_piece:
21598     case DW_OP_deref_size:
21599     case DW_OP_xderef_size:
21600       return valx1->v.val_int == valy1->v.val_int;
21601     case DW_OP_skip:
21602     case DW_OP_bra:
21603       gcc_assert (valx1->val_class == dw_val_class_loc
21604                   && valy1->val_class == dw_val_class_loc
21605                   && x->dw_loc_addr == y->dw_loc_addr);
21606       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
21607     case DW_OP_implicit_value:
21608       if (valx1->v.val_unsigned != valy1->v.val_unsigned
21609           || valx2->val_class != valy2->val_class)
21610         return false;
21611       switch (valx2->val_class)
21612         {
21613         case dw_val_class_const:
21614           return valx2->v.val_int == valy2->v.val_int;
21615         case dw_val_class_vec:
21616           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21617                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21618                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21619                             valx2->v.val_vec.elt_size
21620                             * valx2->v.val_vec.length) == 0;
21621         case dw_val_class_const_double:
21622           return valx2->v.val_double.low == valy2->v.val_double.low
21623                  && valx2->v.val_double.high == valy2->v.val_double.high;
21624         case dw_val_class_addr:
21625           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
21626         default:
21627           gcc_unreachable ();
21628         }
21629     case DW_OP_bregx:
21630     case DW_OP_bit_piece:
21631       return valx1->v.val_int == valy1->v.val_int
21632              && valx2->v.val_int == valy2->v.val_int;
21633     case DW_OP_addr:
21634     hash_addr:
21635       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
21636     case DW_OP_GNU_implicit_pointer:
21637       return valx1->val_class == dw_val_class_die_ref
21638              && valx1->val_class == valy1->val_class
21639              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
21640              && valx2->v.val_int == valy2->v.val_int;
21641     case DW_OP_GNU_entry_value:
21642       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
21643     case DW_OP_GNU_const_type:
21644       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
21645           || valx2->val_class != valy2->val_class)
21646         return false;
21647       switch (valx2->val_class)
21648         {
21649         case dw_val_class_const:
21650           return valx2->v.val_int == valy2->v.val_int;
21651         case dw_val_class_vec:
21652           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21653                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21654                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21655                             valx2->v.val_vec.elt_size
21656                             * valx2->v.val_vec.length) == 0;
21657         case dw_val_class_const_double:
21658           return valx2->v.val_double.low == valy2->v.val_double.low
21659                  && valx2->v.val_double.high == valy2->v.val_double.high;
21660         default:
21661           gcc_unreachable ();
21662         }
21663     case DW_OP_GNU_regval_type:
21664     case DW_OP_GNU_deref_type:
21665       return valx1->v.val_int == valy1->v.val_int
21666              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
21667     case DW_OP_GNU_convert:
21668     case DW_OP_GNU_reinterpret:
21669       if (valx1->val_class != valy1->val_class)
21670         return false;
21671       if (valx1->val_class == dw_val_class_unsigned_const)
21672         return valx1->v.val_unsigned == valy1->v.val_unsigned;
21673       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21674     case DW_OP_GNU_parameter_ref:
21675       return valx1->val_class == dw_val_class_die_ref
21676              && valx1->val_class == valy1->val_class
21677              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21678     default:
21679       /* Other codes have no operands.  */
21680       return true;
21681     }
21682 }
21683
21684 /* Return true if DWARF location expressions X and Y are the same.  */
21685
21686 static inline bool
21687 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
21688 {
21689   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
21690     if (x->dw_loc_opc != y->dw_loc_opc
21691         || x->dtprel != y->dtprel
21692         || !compare_loc_operands (x, y))
21693       break;
21694   return x == NULL && y == NULL;
21695 }
21696
21697 /* Return precomputed hash of location list X.  */
21698
21699 static hashval_t
21700 loc_list_hash (const void *x)
21701 {
21702   return ((const struct dw_loc_list_struct *) x)->hash;
21703 }
21704
21705 /* Return 1 if location lists X and Y are the same.  */
21706
21707 static int
21708 loc_list_eq (const void *x, const void *y)
21709 {
21710   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
21711   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
21712   if (a == b)
21713     return 1;
21714   if (a->hash != b->hash)
21715     return 0;
21716   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
21717     if (strcmp (a->begin, b->begin) != 0
21718         || strcmp (a->end, b->end) != 0
21719         || (a->section == NULL) != (b->section == NULL)
21720         || (a->section && strcmp (a->section, b->section) != 0)
21721         || !compare_locs (a->expr, b->expr))
21722       break;
21723   return a == NULL && b == NULL;
21724 }
21725
21726 /* Recursively optimize location lists referenced from DIE
21727    children and share them whenever possible.  */
21728
21729 static void
21730 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
21731 {
21732   dw_die_ref c;
21733   dw_attr_ref a;
21734   unsigned ix;
21735   void **slot;
21736
21737   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21738     if (AT_class (a) == dw_val_class_loc_list)
21739       {
21740         dw_loc_list_ref list = AT_loc_list (a);
21741         /* TODO: perform some optimizations here, before hashing
21742            it and storing into the hash table.  */
21743         hash_loc_list (list);
21744         slot = htab_find_slot_with_hash (htab, list, list->hash,
21745                                          INSERT);
21746         if (*slot == NULL)
21747           *slot = (void *) list;
21748         else
21749           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
21750       }
21751
21752   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
21753 }
21754
21755 /* Optimize location lists referenced from DIE
21756    children and share them whenever possible.  */
21757
21758 static void
21759 optimize_location_lists (dw_die_ref die)
21760 {
21761   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
21762   optimize_location_lists_1 (die, htab);
21763   htab_delete (htab);
21764 }
21765 \f
21766 /* Output stuff that dwarf requires at the end of every file,
21767    and generate the DWARF-2 debugging info.  */
21768
21769 static void
21770 dwarf2out_finish (const char *filename)
21771 {
21772   limbo_die_node *node, *next_node;
21773   comdat_type_node *ctnode;
21774   htab_t comdat_type_table;
21775   unsigned int i;
21776
21777   gen_scheduled_generic_parms_dies ();
21778   gen_remaining_tmpl_value_param_die_attribute ();
21779
21780   /* Add the name for the main input file now.  We delayed this from
21781      dwarf2out_init to avoid complications with PCH.  */
21782   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
21783   if (!IS_ABSOLUTE_PATH (filename))
21784     add_comp_dir_attribute (comp_unit_die ());
21785   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
21786     {
21787       bool p = false;
21788       htab_traverse (file_table, file_table_relative_p, &p);
21789       if (p)
21790         add_comp_dir_attribute (comp_unit_die ());
21791     }
21792
21793   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21794     {
21795       add_location_or_const_value_attribute (
21796         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21797         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21798         false,
21799         DW_AT_location);
21800     }
21801
21802   /* Traverse the limbo die list, and add parent/child links.  The only
21803      dies without parents that should be here are concrete instances of
21804      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21805      For concrete instances, we can get the parent die from the abstract
21806      instance.  */
21807   for (node = limbo_die_list; node; node = next_node)
21808     {
21809       dw_die_ref die = node->die;
21810       next_node = node->next;
21811
21812       if (die->die_parent == NULL)
21813         {
21814           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21815
21816           if (origin && origin->die_parent)
21817             add_child_die (origin->die_parent, die);
21818           else if (is_cu_die (die))
21819             ;
21820           else if (seen_error ())
21821             /* It's OK to be confused by errors in the input.  */
21822             add_child_die (comp_unit_die (), die);
21823           else
21824             {
21825               /* In certain situations, the lexical block containing a
21826                  nested function can be optimized away, which results
21827                  in the nested function die being orphaned.  Likewise
21828                  with the return type of that nested function.  Force
21829                  this to be a child of the containing function.
21830
21831                  It may happen that even the containing function got fully
21832                  inlined and optimized out.  In that case we are lost and
21833                  assign the empty child.  This should not be big issue as
21834                  the function is likely unreachable too.  */
21835               tree context = NULL_TREE;
21836
21837               gcc_assert (node->created_for);
21838
21839               if (DECL_P (node->created_for))
21840                 context = DECL_CONTEXT (node->created_for);
21841               else if (TYPE_P (node->created_for))
21842                 context = TYPE_CONTEXT (node->created_for);
21843
21844               gcc_assert (context
21845                           && (TREE_CODE (context) == FUNCTION_DECL
21846                               || TREE_CODE (context) == NAMESPACE_DECL));
21847
21848               origin = lookup_decl_die (context);
21849               if (origin)
21850                 add_child_die (origin, die);
21851               else
21852                 add_child_die (comp_unit_die (), die);
21853             }
21854         }
21855     }
21856
21857   limbo_die_list = NULL;
21858
21859 #if ENABLE_ASSERT_CHECKING
21860   {
21861     dw_die_ref die = comp_unit_die (), c;
21862     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
21863   }
21864 #endif
21865   resolve_addr (comp_unit_die ());
21866   move_marked_base_types ();
21867
21868   for (node = deferred_asm_name; node; node = node->next)
21869     {
21870       tree decl = node->created_for;
21871       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21872         {
21873           add_linkage_attr (node->die, decl);
21874           move_linkage_attr (node->die);
21875         }
21876     }
21877
21878   deferred_asm_name = NULL;
21879
21880   /* Walk through the list of incomplete types again, trying once more to
21881      emit full debugging info for them.  */
21882   retry_incomplete_types ();
21883
21884   if (flag_eliminate_unused_debug_types)
21885     prune_unused_types ();
21886
21887   /* Generate separate CUs for each of the include files we've seen.
21888      They will go into limbo_die_list.  */
21889   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
21890     break_out_includes (comp_unit_die ());
21891
21892   /* Generate separate COMDAT sections for type DIEs. */
21893   if (use_debug_types)
21894     {
21895       break_out_comdat_types (comp_unit_die ());
21896
21897       /* Each new type_unit DIE was added to the limbo die list when created.
21898          Since these have all been added to comdat_type_list, clear the
21899          limbo die list.  */
21900       limbo_die_list = NULL;
21901
21902       /* For each new comdat type unit, copy declarations for incomplete
21903          types to make the new unit self-contained (i.e., no direct
21904          references to the main compile unit).  */
21905       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21906         copy_decls_for_unworthy_types (ctnode->root_die);
21907       copy_decls_for_unworthy_types (comp_unit_die ());
21908
21909       /* In the process of copying declarations from one unit to another,
21910          we may have left some declarations behind that are no longer
21911          referenced.  Prune them.  */
21912       prune_unused_types ();
21913     }
21914
21915   /* Traverse the DIE's and add add sibling attributes to those DIE's
21916      that have children.  */
21917   add_sibling_attributes (comp_unit_die ());
21918   for (node = limbo_die_list; node; node = node->next)
21919     add_sibling_attributes (node->die);
21920   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21921     add_sibling_attributes (ctnode->root_die);
21922
21923   /* Output a terminator label for the .text section.  */
21924   switch_to_section (text_section);
21925   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
21926   if (cold_text_section)
21927     {
21928       switch_to_section (cold_text_section);
21929       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
21930     }
21931
21932   /* We can only use the low/high_pc attributes if all of the code was
21933      in .text.  */
21934   if (!have_multiple_function_sections 
21935       || (dwarf_version < 3 && dwarf_strict))
21936     {
21937       /* Don't add if the CU has no associated code.  */
21938       if (text_section_used)
21939         {
21940           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
21941           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
21942         }
21943     }
21944   else
21945     {
21946       unsigned fde_idx;
21947       dw_fde_ref fde;
21948       bool range_list_added = false;
21949
21950       if (text_section_used)
21951         add_ranges_by_labels (comp_unit_die (), text_section_label,
21952                               text_end_label, &range_list_added);
21953       if (cold_text_section_used)
21954         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
21955                               cold_end_label, &range_list_added);
21956
21957       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
21958         {
21959           if (!fde->in_std_section)
21960             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
21961                                   fde->dw_fde_end, &range_list_added);
21962           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
21963             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
21964                                   fde->dw_fde_second_end, &range_list_added);
21965         }
21966
21967       if (range_list_added)
21968         {
21969           /* We need to give .debug_loc and .debug_ranges an appropriate
21970              "base address".  Use zero so that these addresses become
21971              absolute.  Historically, we've emitted the unexpected
21972              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
21973              Emit both to give time for other tools to adapt.  */
21974           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
21975           if (! dwarf_strict && dwarf_version < 4)
21976             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
21977
21978           add_ranges (NULL);
21979         }
21980     }
21981
21982   if (debug_info_level >= DINFO_LEVEL_NORMAL)
21983     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
21984                     debug_line_section_label);
21985
21986   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21987     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
21988
21989   if (have_location_lists)
21990     optimize_location_lists (comp_unit_die ());
21991
21992   /* Output all of the compilation units.  We put the main one last so that
21993      the offsets are available to output_pubnames.  */
21994   for (node = limbo_die_list; node; node = node->next)
21995     output_comp_unit (node->die, 0);
21996
21997   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
21998   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21999     {
22000       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22001
22002       /* Don't output duplicate types.  */
22003       if (*slot != HTAB_EMPTY_ENTRY)
22004         continue;
22005
22006       /* Add a pointer to the line table for the main compilation unit
22007          so that the debugger can make sense of DW_AT_decl_file
22008          attributes.  */
22009       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22010         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22011                         debug_line_section_label);
22012
22013       output_comdat_type_unit (ctnode);
22014       *slot = ctnode;
22015     }
22016   htab_delete (comdat_type_table);
22017
22018   /* Output the main compilation unit if non-empty or if .debug_macinfo
22019      will be emitted.  */
22020   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22021
22022   /* Output the abbreviation table.  */
22023   if (abbrev_die_table_in_use != 1)
22024     {
22025       switch_to_section (debug_abbrev_section);
22026       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22027       output_abbrev_section ();
22028     }
22029
22030   /* Output location list section if necessary.  */
22031   if (have_location_lists)
22032     {
22033       /* Output the location lists info.  */
22034       switch_to_section (debug_loc_section);
22035       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22036                                    DEBUG_LOC_SECTION_LABEL, 0);
22037       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22038       output_location_lists (comp_unit_die ());
22039     }
22040
22041   /* Output public names table if necessary.  */
22042   if (!VEC_empty (pubname_entry, pubname_table))
22043     {
22044       gcc_assert (info_section_emitted);
22045       switch_to_section (debug_pubnames_section);
22046       output_pubnames (pubname_table);
22047     }
22048
22049   /* Output public types table if necessary.  */
22050   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22051      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22052      simply won't look for the section.  */
22053   if (!VEC_empty (pubname_entry, pubtype_table))
22054     {
22055       bool empty = false;
22056       
22057       if (flag_eliminate_unused_debug_types)
22058         {
22059           /* The pubtypes table might be emptied by pruning unused items.  */
22060           unsigned i;
22061           pubname_ref p;
22062           empty = true;
22063           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22064             if (p->die->die_offset != 0)
22065               {
22066                 empty = false;
22067                 break;
22068               }
22069         }
22070       if (!empty)
22071         {
22072           gcc_assert (info_section_emitted);
22073           switch_to_section (debug_pubtypes_section);
22074           output_pubnames (pubtype_table);
22075         }
22076     }
22077
22078   /* Output the address range information if a CU (.debug_info section)
22079      was emitted.  We output an empty table even if we had no functions
22080      to put in it.  This because the consumer has no way to tell the
22081      difference between an empty table that we omitted and failure to
22082      generate a table that would have contained data.  */
22083   if (info_section_emitted)
22084     {
22085       unsigned long aranges_length = size_of_aranges ();
22086
22087       switch_to_section (debug_aranges_section);
22088       output_aranges (aranges_length);
22089     }
22090
22091   /* Output ranges section if necessary.  */
22092   if (ranges_table_in_use)
22093     {
22094       switch_to_section (debug_ranges_section);
22095       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22096       output_ranges ();
22097     }
22098
22099   /* Output the source line correspondence table.  We must do this
22100      even if there is no line information.  Otherwise, on an empty
22101      translation unit, we will generate a present, but empty,
22102      .debug_info section.  IRIX 6.5 `nm' will then complain when
22103      examining the file.  This is done late so that any filenames
22104      used by the debug_info section are marked as 'used'.  */
22105   switch_to_section (debug_line_section);
22106   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22107   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22108     output_line_info ();
22109
22110   /* Have to end the macro section.  */
22111   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22112     {
22113       switch_to_section (debug_macinfo_section);
22114       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22115       if (!VEC_empty (macinfo_entry, macinfo_table))
22116         output_macinfo ();
22117       dw2_asm_output_data (1, 0, "End compilation unit");
22118     }
22119
22120   /* If we emitted any DW_FORM_strp form attribute, output the string
22121      table too.  */
22122   if (debug_str_hash)
22123     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22124 }
22125
22126 #include "gt-dwarf2out.h"